VideoManager.m 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. //
  2. // VideoManager.m
  3. // SikeyComm
  4. //
  5. // Created by 刘振兴 on 2024/3/21.
  6. // Copyright © 2024 BaH Cy. All rights reserved.
  7. //
  8. #import "VideoManager.h"
  9. #import "VideoManager+juphoon.h"
  10. #import "VideoManager+agora.h"
  11. #import "VideoManager+timer.h"
  12. @interface VideoManager() <JCClientCallback, JCMediaDeviceCallback, JCCallCallback, AgoraRtcEngineDelegate>
  13. @end
  14. @implementation VideoManager
  15. + (VideoManager *)shared {
  16. static VideoManager *manager;
  17. static dispatch_once_t token;
  18. dispatch_once(&token, ^{
  19. manager = [[VideoManager alloc] init];
  20. });
  21. return manager;
  22. }
  23. - (void)initManager {
  24. //[self initJuphoon];
  25. //[self initAgora];
  26. }
  27. - (void)clear {
  28. [self logoutJuphoon];
  29. [self clearAgora];
  30. }
  31. - (void)requestVideo:(NSString*)sid ticket:(NSString*)ticket dial:(NSInteger)dial {
  32. HDNormalLog(([NSString stringWithFormat:@"VideoManager: requestVideo ticket:%@ dial:%ld", ticket, dial]));
  33. NSDictionary *param = @{
  34. @"sid":sid,
  35. @"roomId":ticket,
  36. @"dial":@(dial), //是否是拨号 -1 挂断 1 拨号
  37. };
  38. NSString* url = dial == 1? URL_VIDEO_DIAL : URL_VIDEO_HANGUP;
  39. [ERequest httpRequest:param httpURL:url httpMethod:@"GET" onSuccess:^(NSDictionary *result) {
  40. if ([ERequest isSuccessWithResult:result]) {
  41. } else {
  42. [EasyTextView showErrorText:result[@"message"]];
  43. }
  44. } onFailure:^(NSError *error) {
  45. [EasyTextView showErrorText:NSLocalizedString(@"Network.Error", nil)];
  46. }];
  47. }
  48. - (void)videoStart:(NSString*)sid ticket:(NSString*)ticket dial:(NSInteger)dial {
  49. BOOL isReachable = [[AFNetworkReachabilityManager sharedManager] isReachable];
  50. if (!isReachable) {
  51. [EasyTextView showErrorText:NSLocalizedString(@"Network.Error", nil)];
  52. return;
  53. }
  54. HDNormalLog(([NSString stringWithFormat:@"VideoManager: videoStart: sid:%@ ticket:%@ dial:%ld", sid, ticket, dial]));
  55. BOOL isok = [self call:sid ticket:ticket];
  56. if (!isok) {
  57. //如果失败,很有可能登录失败,重新登录//
  58. [self loginJuphoon];
  59. [EasyTextView showErrorText:[NSString stringWithFormat:NSLocalizedString(@"Video.Error.User.Login", nil)]];
  60. } else {
  61. [self requestVideo:sid ticket:ticket dial:1];
  62. [self startTimeoutTimer];
  63. }
  64. }
  65. - (void)videoEnd:(BOOL)isIncomming isSendMessage:(BOOL)isSendMessage {
  66. HDNormalLog(([NSString stringWithFormat:@"VideoManager: videoEnd:%d", isIncomming]));
  67. //发消息
  68. if (isSendMessage) {
  69. JCCallItem *activeCall = self.mJuphoonCall.callItems.firstObject;
  70. if(activeCall.state == JCCallStateInit || activeCall.state == JCCallStatePending) {
  71. NSString* ticket = [self getJuphoonName];
  72. NSString* sid = [self getJuphoonSID];
  73. [self requestVideo:sid ticket:ticket dial:-1];
  74. }
  75. }
  76. [self autoTerm];
  77. }
  78. #pragma mark -
  79. - (void)actionFromSocket:(SocketModel*)model dial:(NSInteger)dial {
  80. if (dial <= 0) {
  81. NSString* talkId = [self getJuphoonName];
  82. ChildModel* childModel = [[DataManager shared] getChildWithTicket:talkId];
  83. BOOL isEquel = [model.sendId isEqualToString:childModel.cid];
  84. if (isEquel) {
  85. [EasyTextView showInfoText:NSLocalizedString(@"Video.Error.Hangup", nil)];
  86. [self autoTerm];
  87. }
  88. } else {
  89. [self loginJuphoon];
  90. }
  91. }
  92. - (void)showInCallVC {
  93. if (self.mInCallVC)
  94. return;
  95. NSLog(@"VideoManager: showInCallVC");
  96. UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
  97. self.mInCallVC = [storyboard instantiateViewControllerWithIdentifier:@"InCallVC"];
  98. [self.mInCallVC setHidesBottomBarWhenPushed:YES];
  99. [EUtil pushViewController:self.mInCallVC animated:YES];
  100. }
  101. - (void)removeInCallVC {
  102. if (self.mInCallVC) {
  103. [self.mInCallVC.navigationController popViewControllerAnimated:YES];
  104. }
  105. self.mInCallVC = nil;
  106. }
  107. //挂断
  108. - (void)autoTerm {
  109. for (JCCallItem* item in self.mJuphoonCall.callItems) {
  110. if (item.active) {
  111. [self.mJuphoonCall term:item reason:JCCallReasonNone description:@"test"];
  112. return;
  113. }
  114. }
  115. }
  116. - (void)autoTermWithTimeout {
  117. NSString* sid = [self getJuphoonSID];
  118. NSString* ticket = [self getJuphoonName];
  119. if (!ticket || ticket.length <= 0)
  120. return;
  121. [EasyTextView showErrorText:NSLocalizedString(@"Video.Error.Timeout", nil)];
  122. [self requestVideo:sid ticket:ticket dial:-1];
  123. [self autoTerm];
  124. }
  125. - (JCCallItem*)getActiveCall {
  126. for (JCCallItem* item in self.mJuphoonCall.callItems) {
  127. if (item.active) {
  128. return item;
  129. }
  130. }
  131. return nil;
  132. }
  133. - (NSString*)getJuphoonSID {
  134. JCCallItem *activeCall = self.mJuphoonCall.callItems.firstObject;
  135. if (activeCall.direction == JCCallDirectionOut) {
  136. return self.mSID;
  137. } else {
  138. return activeCall.userId;
  139. }
  140. }
  141. - (NSString*)getJuphoonName {
  142. JCCallItem *activeCall = self.mJuphoonCall.callItems.firstObject;
  143. if (activeCall.direction == JCCallDirectionOut) {
  144. return self.mTicket;
  145. } else {
  146. return activeCall.userId;
  147. }
  148. }
  149. - (NSString*)getTalkName {
  150. NSString* talkId = [self getJuphoonName];
  151. ChildModel* model = [[DataManager shared] getChildWithTicket:talkId];
  152. return model ? model.name : @"";
  153. }
  154. - (NSString*)getTalkHeadImageURL {
  155. NSString* talkId = [self getJuphoonName];
  156. ChildModel* model = [[DataManager shared] getChildWithTicket:talkId];
  157. return model ? model.avatar : @"";
  158. }
  159. @end