VideoManager.m 4.7 KB

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