VideoManager.m 4.8 KB

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