BGTLinkMicPublishController.m 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. //
  2. // BGTLinkMicPublishController.m
  3. // BuguLive
  4. //
  5. // Created by xfg on 2017/1/19.
  6. // Copyright © 2017年 xfg. All rights reserved.
  7. //
  8. #import "BGTLinkMicPublishController.h"
  9. #define MAX_LINKMIC_MEMBER_SUPPORT 1
  10. #define VIDEO_VIEW_WIDTH 120
  11. #define VIDEO_VIEW_HEIGHT 160
  12. #define VIDEO_VIEW_MARGIN_BOTTOM 70
  13. #define VIDEO_VIEW_MARGIN_RIGHT 20
  14. //小图标
  15. #define BOTTOM_BTN_ICON_WIDTH 35
  16. @implementation TCLivePlayListenerImpl
  17. - (void)onPlayEvent:(int)evtID withParam:(NSDictionary*)param
  18. {
  19. if (self.delegate)
  20. {
  21. [self.delegate onLivePlayEvent:self.playUrl withEvtID:evtID andParam:param];
  22. }
  23. }
  24. - (void)onNetStatus:(NSDictionary*) param
  25. {
  26. if (self.delegate)
  27. {
  28. [self.delegate onLivePlayNetStatus:self.playUrl withParam:param];
  29. }
  30. }
  31. @end
  32. @interface BGTLinkMicPublishController ()<ITCLivePlayListener>
  33. {
  34. BOOL _isSupprotHardware;
  35. }
  36. @end
  37. @implementation BGTLinkMicPublishController
  38. - (void)viewDidLoad
  39. {
  40. [super viewDidLoad];
  41. _playItemArray = [NSMutableArray array];
  42. self.linkMemeberSet = [NSMutableSet set];
  43. _isSupprotHardware = ( [[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0);
  44. [self addLinkMicPlayItem];
  45. // [self initBeautyView];
  46. }
  47. - (void)addLinkMicPlayItem
  48. {
  49. for (int i = 0; i<3; i++)
  50. {
  51. BGTLinkMicPlayItem* playItem = [[BGTLinkMicPlayItem alloc] init];
  52. playItem.videoView = [[UIView alloc] init];
  53. [self.view addSubview:playItem.videoView];
  54. playItem.livePlayListener = [[TCLivePlayListenerImpl alloc] init];
  55. playItem.livePlayListener.delegate = self;
  56. TXLivePlayConfig * playConfig0 = [[TXLivePlayConfig alloc] init];
  57. playItem.livePlayer = [[TXLivePlayer alloc] init];
  58. playItem.livePlayer.delegate = playItem.livePlayListener;
  59. [playItem.livePlayer setConfig: playConfig0];
  60. [playItem.livePlayer setRenderMode:RENDER_MODE_FILL_EDGE];
  61. playItem.pending = false;
  62. playItem.itemIndex = i;
  63. [playItem emptyPlayInfo];
  64. [_playItemArray addObject:playItem];
  65. }
  66. }
  67. -(void)initBeautyView{
  68. //#error TiSDK Key, 与包名对应,请联系商务获取
  69. NSString* key = [GlobalVariables sharedInstance].appModel.bogo_beauty_key;
  70. if([BGUtils isBlankString:key])
  71. {
  72. //这里写上那个key
  73. key = @"517a990947274dd8b51e1525feb0fb79";
  74. // [[BGHUDHelper sharedInstance] tipMessage:ASLocalizedString(@"美颜key为空,请尝试重新打开app获取!")];
  75. }
  76. else
  77. {
  78. // [TiSDK init:key];
  79. //在这里创建好像会被覆盖掉 你看看这里
  80. // dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  81. if([[GlobalVariables sharedInstance].appModel.spear_live isEqualToString:@"1"])
  82. {
  83. // NSString* key = [GlobalVariables sharedInstance].appModel.bogo_beauty_key;
  84. //
  85. // // NSString* key = @"";
  86. // [TiSDK init:key CallBack:^(InitStatus initStatus) {
  87. // [[NSNotificationCenter defaultCenter] postNotificationName:@"TiSDKInitStatusNotification" object:nil];
  88. // }];
  89. // [[TiUIManager shareManager] loadToSuperview:self.view];
  90. // [TiUIManager shareManager].showsDefaultUI = YES;
  91. // [[TiUIManager shareManager]loadToWindowDelegate:nil];
  92. }
  93. }
  94. }
  95. #pragma mark ---------美颜增加-------------
  96. //隐藏拓幻美颜
  97. /*
  98. - (GLuint)onPreProcessTexture:(GLuint)texture width:(CGFloat)width height:(CGFloat)height{
  99. return [[TiSDKManager shareManager] renderTexture2D:texture Width:width Height:height Rotation:CLOCKWISE_0 Mirror:self.txLivePublisher.config.frontCamera];
  100. }
  101. */
  102. #pragma mark - ----------------------- 推流模块 -----------------------
  103. - (BOOL)startRtmp
  104. {
  105. return [super startPushRtmpLiveMethod];
  106. }
  107. - (void)stopRtmp
  108. {
  109. [super stopRtmp];
  110. for (BGTLinkMicPlayItem* playItem in _playItemArray)
  111. {
  112. [playItem stopPlay];
  113. }
  114. }
  115. #pragma mark - ----------------------- 连麦 -----------------------
  116. #pragma mark 同意连麦
  117. - (void)agreeLinkMick:(NSString *)streamPlayUrl applicant:(NSString *)userID
  118. {
  119. self.txLivePushonfig.enableAEC = YES;
  120. [self.txLivePublisher setConfig:self.txLivePushonfig];
  121. if ([BGUtils isBlankString:streamPlayUrl] || [BGUtils isBlankString:userID])
  122. {
  123. return;
  124. }
  125. }
  126. #pragma mark 断开连麦
  127. - (void)breakLinkMick:(NSString *)userID
  128. {
  129. [BGLiveSDKViewModel tLiveStopMick:self.roomIDStr toUserId:userID];
  130. if (![BGUtils isBlankString:userID])
  131. {
  132. BGTLinkMicPlayItem *playItem = [self getPlayItemByUserID:userID];
  133. if (playItem)
  134. {
  135. [playItem stopPlay];
  136. [playItem emptyPlayInfo];
  137. if (_linkMicPublishDelegate && [_linkMicPublishDelegate respondsToSelector:@selector(playMickResult:userID:)])
  138. {
  139. [_linkMicPublishDelegate playMickResult:NO userID:userID];
  140. }
  141. }
  142. }
  143. }
  144. //语音连麦
  145. - (void)adjustPlayItemVoiceUserList:(NSArray *)userlist;
  146. {
  147. [super adjustPlayItemVoiceUserList:userlist];
  148. }
  149. #pragma mark 调整连麦窗口
  150. - (void)adjustPlayItem:(TLiveMickListModel *)mickListModel
  151. {
  152. [self adjustPlayItemAgora:mickListModel];
  153. }
  154. #pragma mark 通过用户ID来获取连麦视图
  155. - (BGTLinkMicPlayItem *)getPlayItemByUserID:(NSString*)userID
  156. {
  157. if (userID)
  158. {
  159. for (BGTLinkMicPlayItem* playItem in _playItemArray)
  160. {
  161. if ([userID isEqualToString:playItem.userID])
  162. {
  163. return playItem;
  164. }
  165. }
  166. }
  167. return nil;
  168. }
  169. - (BGTLinkMicPlayItem*)getPlayItemByStreamUrl:(NSString*)streamUrl
  170. {
  171. if (streamUrl)
  172. {
  173. for (BGTLinkMicPlayItem* playItem in _playItemArray)
  174. {
  175. if ([streamUrl isEqualToString:playItem.playUrl])
  176. {
  177. return playItem;
  178. }
  179. }
  180. }
  181. return nil;
  182. }
  183. #pragma mark 连麦失败处理
  184. - (void)handleLinkMicFailed:(NSString*)userID message:(NSString*)message
  185. {
  186. if (userID)
  187. {
  188. BGTLinkMicPlayItem* playItem = [self getPlayItemByUserID:userID];
  189. if (playItem == nil)
  190. {
  191. return;
  192. }
  193. if (_linkMicPublishDelegate && [_linkMicPublishDelegate respondsToSelector:@selector(playMickResult:userID:)])
  194. {
  195. [_linkMicPublishDelegate playMickResult:NO userID:userID];
  196. }
  197. [playItem stopPlay];
  198. [playItem emptyPlayInfo];
  199. if (message != nil && message.length > 0)
  200. {
  201. [self toastTip:message];
  202. }
  203. }
  204. }
  205. #pragma mark - ----------------------- ITCLivePlayListener代理事件 -----------------------
  206. - (void)onLivePlayEvent:(NSString*)strStreamUrl withEvtID:(int)event andParam:(NSDictionary*)param
  207. {
  208. if (event != PLAY_EVT_PLAY_PROGRESS)
  209. {
  210. NSLog(@"==========playEvtID2:%d",event);
  211. }
  212. BGTLinkMicPlayItem * playItem = [self getPlayItemByStreamUrl:strStreamUrl];
  213. if (playItem == nil)
  214. {
  215. return;
  216. }
  217. if (event == PLAY_EVT_PLAY_BEGIN)
  218. {
  219. if (playItem.pending == YES)
  220. {
  221. playItem.pending = NO;
  222. [playItem stopLoading];
  223. if (_linkMicPublishDelegate && [_linkMicPublishDelegate respondsToSelector:@selector(playMickResult:userID:)])
  224. {
  225. [_linkMicPublishDelegate playMickResult:YES userID:playItem.userID];
  226. }
  227. }
  228. }
  229. else if (event == PLAY_EVT_PLAY_END)
  230. {
  231. [playItem stopLoading];
  232. if (playItem.pending == YES)
  233. {
  234. [self handleLinkMicFailed:playItem.userID message:ASLocalizedString(@"拉流失败,结束连麦")];
  235. }
  236. else
  237. {
  238. [self handleLinkMicFailed:playItem.userID message:ASLocalizedString(@"连麦观众视频断流,结束连麦")];
  239. }
  240. [self breakLinkMick:playItem.userID];
  241. }
  242. else if (event == PLAY_ERR_NET_DISCONNECT)
  243. {
  244. [playItem stopLoading];
  245. // [BGLiveSDKViewModel tLiveStopMick:self.roomIDStr toUserId:playItem.userID];
  246. if (playItem.pending == YES)
  247. {
  248. // [self handleLinkMicFailed:playItem.userID message:ASLocalizedString(@"拉流失败,继续尝试连麦")];
  249. [playItem reStartPlay];
  250. }
  251. else
  252. {
  253. [self handleLinkMicFailed:playItem.userID message:ASLocalizedString(@"连麦观众视频断流,结束连麦")];
  254. }
  255. // [self breakLinkMick:playItem.userID];
  256. }
  257. else if (event == PLAY_ERR_GET_RTMP_ACC_URL_FAIL)
  258. {
  259. if (playItem.reStartTimes < 5)
  260. {
  261. [playItem reStartPlay];
  262. }
  263. else
  264. {
  265. [playItem stopLoading];
  266. [BGLiveSDKViewModel tLiveStopMick:self.roomIDStr toUserId:playItem.userID];
  267. if (playItem.pending == YES)
  268. {
  269. [self handleLinkMicFailed:playItem.userID message:ASLocalizedString(@"拉流失败,结束连麦")];
  270. }
  271. else
  272. {
  273. [self handleLinkMicFailed:playItem.userID message:ASLocalizedString(@"连麦观众视频断流,结束连麦")];
  274. }
  275. }
  276. playItem.reStartTimes ++;
  277. }
  278. else if (event == PUSH_WARNING_HW_ACCELERATION_FAIL || event == PLAY_WARNING_HW_ACCELERATION_FAIL)
  279. {
  280. [playItem stopLoading];
  281. [self handleLinkMicFailed:playItem.userID message:ASLocalizedString(@"系统不支持硬编或硬解")];
  282. _isSupprotHardware = NO;
  283. }
  284. }
  285. - (void)onLivePlayNetStatus:(NSString*)playUrl withParam:(NSDictionary*)param
  286. {
  287. }
  288. - (void)clickCloseBtn:(UserView *)view
  289. {
  290. if([self.linkMicPublishDelegate respondsToSelector:@selector(clickCloseBtn:)])
  291. {
  292. [self.linkMicPublishDelegate clickCloseBtn:view];
  293. }
  294. }
  295. @end