123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- //
- // 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+http.h"
- #import "VideoManager+timer.h"
- #import "VideoManager+view.h"
- @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];
- }
- #pragma mark -
- - (void)actionFromSocket:(SocketModel*)model dial:(NSInteger)dial {
- if (dial <= 0) {
- ChildModel* childModel = [[DataManager shared] getChildWithTicket:self.mTicket];
- BOOL isEquel = [model.sendId isEqualToString:childModel.cid];
- if (isEquel) {
- [EasyTextView showInfoText:NSLocalizedString(@"Video.Error.Hangup", nil)];
- [self term];
- }
- } else {
- self.uid = [DataManager shared].loginModel.uid;
- self.mTicket = [[DataManager shared] getDevice:model.sendId].ticket;
- self.mToken = ((SocketContentModel*)model.content).token;
- self.mChannelName = ((SocketContentModel*)model.content).channelName;
- [self videoReceiveStart];
- }
- }
- - (void)videoCallStart:(NSString*)uid ticket:(NSString*)ticket {
- @weakify(self);
- BOOL isReachable = [[AFNetworkReachabilityManager sharedManager] isReachable];
- if (!isReachable) {
- [EasyTextView showErrorText:NSLocalizedString(@"Network.Error", nil)];
- return;
- }
- HDNormalLog(([NSString stringWithFormat:@"VideoManager: videoCallStart: uid:%@ ticket:%@", uid, ticket]));
- DeviceModel* model = [[DataManager shared]getDeviceWithTicket:ticket];
- if (!model)
- return;
- self.uid = uid;
- self.mTicket = ticket;
-
- if (model.videoType == VIDEO_TYPE_JUPHOON) {
- BOOL isok = [self call:uid ticket:ticket];
- if (!isok) {
- //如果失败,很有可能登录失败,重新登录//
- [self loginJuphoon];
- [EasyTextView showErrorText:[NSString stringWithFormat:NSLocalizedString(@"Video.Error.User.Login", nil)]];
- } else {
- [self requestVideo:uid ticket:ticket dial:1 callback:^(BOOL isOK, NSString *token) {
- }];
- [self startTimeoutTimer];
- }
- } else if (model.videoType == VIDEO_TYPE_AGORA) {
- [self requestVideo:uid ticket:ticket dial:1 callback:^(BOOL isOK, NSString *token) {
- if (isOK && token.length > 0) {
- [weak_self joinChannel:token channelId:[self getChannelName:uid ticket:ticket] uid:0];
- [weak_self startTimeoutTimer];
- }
- }];
- }
- }
- - (void)videoReceiveStart {
- DeviceModel* model = [[DataManager shared]getDeviceWithTicket:self.mTicket];
- if (model.videoType == VIDEO_TYPE_JUPHOON) {
- [self loginJuphoon];
- } else if (model.videoType == VIDEO_TYPE_AGORA) {
- [self showInCallVC];
- }
- }
- - (void)videoAnswer {
- DeviceModel* model = [[DataManager shared]getDeviceWithTicket:self.mTicket];
- if (model.videoType == VIDEO_TYPE_JUPHOON) {
- JCCallItem *activeCall = [self getActiveCall];
- [self.mJuphoonCall answer:activeCall video:true];
- } else if (model.videoType == VIDEO_TYPE_AGORA) {
- [self joinChannel:self.mToken channelId:self.mChannelName uid:0];
- }
- }
- - (void)videoEnd:(BOOL)isIncomming isSendMessage:(BOOL)isSendMessage {
- HDNormalLog(([NSString stringWithFormat:@"VideoManager: videoEnd:%d", isIncomming]));
- //发消息
- if (isSendMessage) {
- [self requestVideo:self.uid ticket:self.mTicket dial:-1 callback:^(BOOL isOK, NSString *token) {
- }];
- }
- [self term];
- }
- - (void)termWithTimeout {
- [EasyTextView showErrorText:NSLocalizedString(@"Video.Error.Timeout", nil)];
- [self videoEnd:YES isSendMessage:YES];
- }
- - (void)term {
- DeviceModel* model = [[DataManager shared] getDeviceWithTicket:self.mTicket];
- if (model.videoType == VIDEO_TYPE_JUPHOON) {
- [self termJuphoon];
- } else if (model.videoType == VIDEO_TYPE_AGORA) {
- [self termAgora];
- }
- }
- - (void)switchCamera {
- DeviceModel* model = [[DataManager shared] getDeviceWithTicket:self.mTicket];
- if (model.videoType == VIDEO_TYPE_JUPHOON) {
- [self switchCameraJuphoon];
- } else if (model.videoType == VIDEO_TYPE_AGORA) {
- [self switchCameraAgora];
- }
- }
- - (BOOL)isVideoTalking {
- DeviceModel* model = [[DataManager shared] getDeviceWithTicket:self.mTicket];
- if (model.videoType == VIDEO_TYPE_JUPHOON) {
- JCCallItem *activeCall = [[VideoManager shared] getActiveCall];
- return activeCall.video || activeCall.state == JCCallStateTalking;
- } else if (model.videoType == VIDEO_TYPE_AGORA) {
- return self.uid.length > 0 && self.mChannelName.length > 0;
- }
- return NO;
- }
- - (JCCallItem*)getActiveCall {
- for (JCCallItem* item in self.mJuphoonCall.callItems) {
- if (item.active) {
- return item;
- }
- }
- return nil;
- }
- - (NSString*)getTalkName {
- NSString* talkId = self.mTicket;
- ChildModel* model = [[DataManager shared] getChildWithTicket:talkId];
- return model ? model.name : @"";
- }
- - (NSString*)getTalkHeadImageURL {
- NSString* talkId = self.mTicket;
- ChildModel* model = [[DataManager shared] getChildWithTicket:talkId];
- return model ? model.avatar : @"";
- }
- - (NSString*)getChannelName:(NSString*)sid ticket:(NSString*)ticket {
- return [NSString stringWithFormat:@"%@_%@", sid, ticket];
- }
- @end
|