123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- //
- // VideoManager.m
- // SikeyComm
- //
- // Created by 刘振兴 on 2024/3/21.
- // Copyright © 2024 BaH Cy. All rights reserved.
- //
- #import "VideoManager.h"
- #import "VideoManager+juphoon.h"
- #import "VideoManager+agora.h"
- #import "VideoManager+timer.h"
- @interface VideoManager() <JCClientCallback, JCMediaDeviceCallback, JCCallCallback, AgoraRtcEngineDelegate>
- @end
- @implementation VideoManager
- + (VideoManager *)shared {
- static VideoManager *manager;
- static dispatch_once_t token;
- dispatch_once(&token, ^{
- manager = [[VideoManager alloc] init];
- });
- return manager;
- }
- - (void)initManager {
- //[self initJuphoon];
- //[self initAgora];
- }
- - (void)clear {
- [self logoutJuphoon];
- [self clearAgora];
- }
- - (void)requestVideo:(NSString*)sid ticket:(NSString*)ticket dial:(NSInteger)dial {
- HDNormalLog(([NSString stringWithFormat:@"VideoManager: requestVideo ticket:%@ dial:%ld", ticket, dial]));
- NSDictionary *param = @{
- @"sid":sid,
- @"roomId":ticket,
- @"dial":@(dial), //是否是拨号 -1 挂断 1 拨号
- };
- NSString* url = dial == 1? URL_VIDEO_DIAL : URL_VIDEO_HANGUP;
- [ERequest httpRequest:param httpURL:url httpMethod:@"GET" onSuccess:^(NSDictionary *result) {
- if ([ERequest isSuccessWithResult:result]) {
- } else {
- [EasyTextView showErrorText:result[@"message"]];
- }
- } onFailure:^(NSError *error) {
- [EasyTextView showErrorText:NSLocalizedString(@"Network.Error", nil)];
- }];
- }
- - (void)videoStart:(NSString*)sid ticket:(NSString*)ticket dial:(NSInteger)dial {
- BOOL isReachable = [[AFNetworkReachabilityManager sharedManager] isReachable];
- if (!isReachable) {
- [EasyTextView showErrorText:NSLocalizedString(@"Network.Error", nil)];
- return;
- }
- HDNormalLog(([NSString stringWithFormat:@"VideoManager: videoStart: sid:%@ ticket:%@ dial:%ld", sid, ticket, dial]));
- BOOL isok = [self call:sid ticket:ticket];
- if (!isok) {
- //如果失败,很有可能登录失败,重新登录//
- [self loginJuphoon];
- [EasyTextView showErrorText:[NSString stringWithFormat:NSLocalizedString(@"Video.Error.User.Login", nil)]];
- } else {
- [self requestVideo:sid ticket:ticket dial:1];
- [self startTimeoutTimer];
- }
- }
- - (void)videoEnd:(BOOL)isIncomming isSendMessage:(BOOL)isSendMessage {
- HDNormalLog(([NSString stringWithFormat:@"VideoManager: videoEnd:%d", isIncomming]));
- //发消息
- if (isSendMessage) {
- JCCallItem *activeCall = self.mJuphoonCall.callItems.firstObject;
- if(activeCall.state == JCCallStateInit || activeCall.state == JCCallStatePending) {
- NSString* ticket = [self getJuphoonName];
- NSString* sid = [self getJuphoonSID];
- [self requestVideo:sid ticket:ticket dial:-1];
- }
- }
- [self autoTerm];
- }
- #pragma mark -
- - (void)actionFromSocket:(SocketModel*)model dial:(NSInteger)dial {
- if (dial <= 0) {
- NSString* talkId = [self getJuphoonName];
- ChildModel* childModel = [[DataManager shared] getChildWithTicket:talkId];
- BOOL isEquel = [model.sendId isEqualToString:childModel.cid];
- if (isEquel) {
- [EasyTextView showInfoText:NSLocalizedString(@"Video.Error.Hangup", nil)];
- [self autoTerm];
- }
- } else {
- [self loginJuphoon];
- }
- }
- - (void)showInCallVC {
- if (self.mInCallVC)
- return;
- NSLog(@"VideoManager: showInCallVC");
- UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
- self.mInCallVC = [storyboard instantiateViewControllerWithIdentifier:@"InCallVC"];
- [self.mInCallVC setHidesBottomBarWhenPushed:YES];
- [EUtil pushViewController:self.mInCallVC animated:YES];
- }
- - (void)removeInCallVC {
- if (self.mInCallVC) {
- [self.mInCallVC.navigationController popViewControllerAnimated:YES];
- }
- self.mInCallVC = nil;
- }
- //挂断
- - (void)autoTerm {
- for (JCCallItem* item in self.mJuphoonCall.callItems) {
- if (item.active) {
- [self.mJuphoonCall term:item reason:JCCallReasonNone description:@"test"];
- return;
- }
- }
- }
- - (void)autoTermWithTimeout {
- NSString* sid = [self getJuphoonSID];
- NSString* ticket = [self getJuphoonName];
- if (!ticket || ticket.length <= 0)
- return;
- [EasyTextView showErrorText:NSLocalizedString(@"Video.Error.Timeout", nil)];
- [self requestVideo:sid ticket:ticket dial:-1];
- [self autoTerm];
- }
- - (JCCallItem*)getActiveCall {
- for (JCCallItem* item in self.mJuphoonCall.callItems) {
- if (item.active) {
- return item;
- }
- }
- return nil;
- }
- - (NSString*)getJuphoonSID {
- JCCallItem *activeCall = self.mJuphoonCall.callItems.firstObject;
- if (activeCall.direction == JCCallDirectionOut) {
- return self.mSID;
- } else {
- return activeCall.userId;
- }
- }
- - (NSString*)getJuphoonName {
- JCCallItem *activeCall = self.mJuphoonCall.callItems.firstObject;
- if (activeCall.direction == JCCallDirectionOut) {
- return self.mTicket;
- } else {
- return activeCall.userId;
- }
- }
- - (NSString*)getTalkName {
- NSString* talkId = [self getJuphoonName];
- ChildModel* model = [[DataManager shared] getChildWithTicket:talkId];
- return model ? model.name : @"";
- }
- - (NSString*)getTalkHeadImageURL {
- NSString* talkId = [self getJuphoonName];
- ChildModel* model = [[DataManager shared] getChildWithTicket:talkId];
- return model ? model.avatar : @"";
- }
- @end
|