VideoManager.m 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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+http.h"
  12. #import "VideoManager+timer.h"
  13. #import "VideoManager+view.h"
  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. HDNormalLog(@"VideoManager initManager");
  25. [self initJuphoon];
  26. [self initAgora];
  27. }
  28. - (void)clearManager {
  29. HDNormalLog(@"VideoManager clearManager");
  30. [self logoutJuphoon];
  31. [self clearAgora];
  32. [self clearValue];
  33. }
  34. - (void)clearValue {
  35. HDNormalLog(@"VideoManager clearValue");
  36. self.uid = @"";
  37. self.mTicket = @"";
  38. self.mToken = @"";
  39. self.roomId = @"";
  40. }
  41. - (void)videoCallIn {
  42. HDNormalLog(@"VideoManager videoCallIn");
  43. DeviceModel* model = [[DataManager shared]getDeviceWithTicket:self.mTicket];
  44. if (model.videoCallChannel == VIDEO_TYPE_JUPHOON) {
  45. [self loginJuphoon];
  46. } else if (model.videoCallChannel == VIDEO_TYPE_AGORA) {
  47. [self showInCallVC:model.videoCallChannel isCallIn:YES];
  48. [self startTimeoutTimer];
  49. }
  50. }
  51. - (void)videoCallOut:(NSString*)uid ticket:(NSString*)ticket {
  52. @weakify(self);
  53. BOOL isReachable = [[AFNetworkReachabilityManager sharedManager] isReachable];
  54. if (!isReachable) {
  55. [EasyTextView showErrorText:NSLocalizedString(@"Network.Error", nil)];
  56. return;
  57. }
  58. HDNormalLog(([NSString stringWithFormat:@"VideoManager: videoCallOut: uid:%@ ticket:%@", uid, ticket]));
  59. DeviceModel* model = [[DataManager shared] getDeviceWithTicket:ticket];
  60. if (!model)
  61. return;
  62. self.uid = uid;
  63. self.mTicket = ticket;
  64. self.roomId = model.videoCallChannel == VIDEO_TYPE_JUPHOON ? ticket : [self getRoomId:self.uid ticket:self.mTicket];
  65. if (model.videoCallChannel == VIDEO_TYPE_JUPHOON) {
  66. BOOL isok = [self call:uid ticket:ticket];
  67. if (!isok) {
  68. //如果失败,很有可能登录失败,重新登录//
  69. [self loginJuphoon];
  70. [EasyTextView showErrorText:[NSString stringWithFormat:NSLocalizedString(@"Video.Error.User.Login", nil)]];
  71. } else {
  72. [self requestVideo:uid ticket:ticket dial:1 callback:^(BOOL isOK, NSString *token) {
  73. }];
  74. [self startTimeoutTimer];
  75. }
  76. } else if (model.videoCallChannel == VIDEO_TYPE_AGORA) {
  77. [self requestVideo:uid ticket:ticket dial:1 callback:^(BOOL isOK, NSString *token) {
  78. if (isOK && token.length > 0) {
  79. self.mToken = token;
  80. [weak_self showInCallVC:model.videoCallChannel isCallIn:NO];
  81. [weak_self startTimeoutTimer];
  82. }
  83. }];
  84. }
  85. }
  86. - (void)videoAnswer {
  87. HDNormalLog(@"VideoManager videoAnswer");
  88. DeviceModel* model = [[DataManager shared]getDeviceWithTicket:self.mTicket];
  89. if (model.videoCallChannel == VIDEO_TYPE_JUPHOON) {
  90. [self answerJuphoon];
  91. } else if (model.videoCallChannel == VIDEO_TYPE_AGORA) {
  92. [self joinChannel:self.mToken channelId:self.roomId uid:0];
  93. }
  94. }
  95. - (void)termWithTimeout {
  96. [EasyTextView showErrorText:NSLocalizedString(@"Video.Error.Timeout", nil)];
  97. [self termWithSelf];
  98. }
  99. - (void)termWithSelf {
  100. [self stopTimeoutTimer];
  101. [self requestVideo:self.uid ticket:self.mTicket dial:-1 callback:^(BOOL isOK, NSString *token) {
  102. }];
  103. [self term];
  104. }
  105. - (void)termWithOther {
  106. [EasyTextView showInfoText:NSLocalizedString(@"Video.Error.Hangup", nil)];
  107. [self term];
  108. }
  109. //别人呼叫我,我拒接
  110. - (void)termWithRefuse {
  111. [self requestVideo:self.uid ticket:self.mTicket dial:-1 callback:^(BOOL isOK, NSString *token) {
  112. }];
  113. }
  114. - (void)term {
  115. DeviceModel* model = [[DataManager shared] getDeviceWithTicket:self.mTicket];
  116. if (model.videoCallChannel == VIDEO_TYPE_JUPHOON) {
  117. [self termJuphoon];
  118. [self removeInCallVC];
  119. } else if (model.videoCallChannel == VIDEO_TYPE_AGORA) {
  120. [self termAgora];
  121. [self removeInCallVC];
  122. }
  123. [self clearValue];
  124. }
  125. - (void)switchCamera {
  126. DeviceModel* model = [[DataManager shared] getDeviceWithTicket:self.mTicket];
  127. if (model.videoCallChannel == VIDEO_TYPE_JUPHOON) {
  128. [self switchCameraJuphoon];
  129. } else if (model.videoCallChannel == VIDEO_TYPE_AGORA) {
  130. [self switchCameraAgora];
  131. }
  132. }
  133. - (BOOL)isVideoTalking {
  134. return self.uid.length > 0 && self.mTicket.length > 0;
  135. }
  136. - (NSString*)getTalkName {
  137. NSString* talkId = self.mTicket;
  138. ChildModel* model = [[DataManager shared] getChildWithTicket:talkId];
  139. return model ? model.name : @"";
  140. }
  141. - (NSString*)getTalkHeadImageURL {
  142. NSString* talkId = self.mTicket;
  143. ChildModel* model = [[DataManager shared] getChildWithTicket:talkId];
  144. return model ? model.avatar : @"";
  145. }
  146. - (NSString*)getRoomId:(NSString*)sid ticket:(NSString*)ticket {
  147. return [NSString stringWithFormat:@"%@_%@_%@", sid, ticket, EUtil.getCurrentTimeMS];
  148. }
  149. @end