YBLiveRTCManager.m 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. //
  2. // YBLiveRTCManager.m
  3. // YBLive
  4. //
  5. // Created by ybRRR on 2023/1/29.
  6. // Copyright © 2023 cat. All rights reserved.
  7. //
  8. #import "YBLiveRTCManager.h"
  9. @interface YBLiveRTCManager ()<V2TXLivePusherObserver>
  10. @property(nonatomic,strong)V2TXLiveVideoEncoderParam *txLiveVieoParam;
  11. @property(nonatomic,strong)V2TXLivePusher *txLivePusher;
  12. @property(nonatomic,strong)TXAudioEffectManager *audioEffect;
  13. @end
  14. @implementation YBLiveRTCManager
  15. static YBLiveRTCManager *_RTCManager = nil;
  16. +(instancetype)shareInstance{
  17. static dispatch_once_t onceToken;
  18. dispatch_once(&onceToken, ^{
  19. _RTCManager = [[super allocWithZone:NULL]init];
  20. });
  21. return _RTCManager;
  22. }
  23. + (instancetype)allocWithZone:(struct _NSZone *)zone {
  24. return [self shareInstance];
  25. }
  26. #pragma mark -初始化直播 txsdk
  27. -(void)initWithLiveMode:(V2TXLiveMode)liveMode andPushData:(NSDictionary *)pushDic{
  28. //配置推流参数
  29. _txLiveVieoParam = [[V2TXLiveVideoEncoderParam alloc]init];
  30. int videoResolution = [minstr([pushDic valueForKey:@"resolution"]) intValue];
  31. if (videoResolution <= 3) {
  32. _txLiveVieoParam.videoResolution =V2TXLiveVideoResolution640x360;
  33. }
  34. else if (videoResolution == 4){
  35. _txLiveVieoParam.videoResolution =V2TXLiveVideoResolution960x540;
  36. }
  37. else if (videoResolution >= 5){
  38. _txLiveVieoParam.videoResolution =V2TXLiveVideoResolution1280x720;
  39. }
  40. _txLiveVieoParam.videoFps = [minstr([pushDic valueForKey:@"fps"]) intValue];
  41. _txLiveVieoParam.videoBitrate = [minstr([pushDic valueForKey:@"bitrate"]) intValue];
  42. _txLiveVieoParam.minVideoBitrate = [minstr([pushDic valueForKey:@"bitrate_min"]) intValue];
  43. _txLivePusher = [[V2TXLivePusher alloc]initWithLiveMode:V2TXLiveMode_RTC];
  44. [_txLivePusher setVideoQuality:_txLiveVieoParam];
  45. [_txLivePusher enableCustomVideoProcess:true pixelFormat:V2TXLivePixelFormatTexture2D bufferType:V2TXLiveBufferTypeTexture];
  46. [_txLivePusher setEncoderMirror:YES];
  47. [_txLivePusher startCamera:YES];
  48. [_txLivePusher startMicrophone];
  49. _audioEffect =[_txLivePusher getAudioEffectManager];
  50. [_txLivePusher setObserver:self];
  51. }
  52. #pragma mark -初始化语音聊天室 txsdk
  53. -(void)initWithChatLiveMode:(V2TXLiveMode)liveMode{
  54. _txLivePusher = [[V2TXLivePusher alloc]initWithLiveMode:V2TXLiveMode_RTC];
  55. [_txLivePusher startMicrophone];
  56. _audioEffect =[_txLivePusher getAudioEffectManager];
  57. [_txLivePusher setObserver:self];
  58. }
  59. #pragma mark -开始推流
  60. -(void)startPush:(NSString *)pusUrl{
  61. [_txLivePusher startPush:pusUrl];
  62. V2TXLiveCode pushCode = [_txLivePusher startPush:pusUrl];
  63. if ([self.delegate respondsToSelector:@selector(ybPushLiveStatus:)]) {
  64. [self.delegate ybPushLiveStatus:pushCode];
  65. }
  66. }
  67. #pragma mark -推流显示界面
  68. -(void)setPushView:(UIView *)pushView{
  69. [_txLivePusher setRenderView:pushView];
  70. }
  71. #pragma mark -停止推流
  72. -(void)stopPush{
  73. // [_audioEffect stopPlayMusic:666];
  74. [_txLivePusher stopCamera];
  75. [_txLivePusher stopMicrophone];
  76. [_txLivePusher stopPush];
  77. V2TXLiveCode pushCode = [_txLivePusher stopPush];
  78. if ([self.delegate respondsToSelector:@selector(ybPushLiveStatus:)]) {
  79. [self.delegate ybPushLiveStatus:pushCode];
  80. }
  81. }
  82. #pragma mark -暂停推流
  83. -(void)pausePush
  84. {
  85. [_txLivePusher pauseVideo];
  86. }
  87. #pragma mark -恢复推流
  88. -(void)resumePush
  89. {
  90. [_txLivePusher resumeVideo];
  91. }
  92. #pragma mark -切换摄像头
  93. -(void)changeCamera:(BOOL)isFront{
  94. [_txLivePusher startCamera:isFront];
  95. }
  96. #pragma mark -切换镜像
  97. -(void)changeMirror:(BOOL)isMirror{
  98. [_txLivePusher setEncoderMirror:isMirror];
  99. }
  100. #pragma mark -闪光灯
  101. -(void)cameraTorch:(BOOL)isTorch{
  102. TXDeviceManager *deviceManager = [_txLivePusher getDeviceManager];
  103. BOOL isTorSupported = [deviceManager isCameraTorchSupported];
  104. if (isTorSupported) {
  105. [deviceManager enableCameraTorch:isTorch];
  106. }else{
  107. [MBProgressHUD showError:YZMsg(@"只有后置摄像头才能开启闪光灯")];
  108. }
  109. }
  110. #pragma mark -设置美颜
  111. -(void)setBeautyLevel:(float)beautyLevel WhitenessLevel:(float)whitenessLevel IsTXfiter:(NSString *)isTXfiter{
  112. TXBeautyManager *beautyManager = [_txLivePusher getBeautyManager];
  113. if ([isTXfiter isEqual:@"1"]) {
  114. beautyLevel = 9;
  115. whitenessLevel = 3;
  116. }
  117. [beautyManager setBeautyStyle:0];
  118. [beautyManager setBeautyLevel:beautyLevel];
  119. [beautyManager setWhitenessLevel:whitenessLevel];
  120. [beautyManager setRuddyLevel:0];
  121. }
  122. -(void)setBeautyLevel:(float)beautyLevel WhitenessLevel:(float)whitenessLevel {
  123. TXBeautyManager *beautyManager = [_txLivePusher getBeautyManager];
  124. [beautyManager setBeautyStyle:0];
  125. [beautyManager setBeautyLevel:beautyLevel];
  126. [beautyManager setWhitenessLevel:whitenessLevel];
  127. [beautyManager setRuddyLevel:0];
  128. }
  129. #pragma mark -设置大眼级别
  130. -(void)setYBEyeScaleLevel:(float)eyeScaleLevel{
  131. TXBeautyManager *beautyManager = [_txLivePusher getBeautyManager];
  132. [beautyManager setEyeScaleLevel:eyeScaleLevel];
  133. }
  134. #pragma mark -设置瘦脸级别
  135. -(void)setYBFaceScaleLevel:(float)faceSlimLevel{
  136. TXBeautyManager *beautyManager = [_txLivePusher getBeautyManager];
  137. [beautyManager setFaceSlimLevel:faceSlimLevel];
  138. }
  139. #pragma mark -设置红润级别
  140. -(void)setYBRuddyLevel:(float)ruddyLevel{
  141. TXBeautyManager *beautyManager = [_txLivePusher getBeautyManager];
  142. [beautyManager setRuddyLevel:ruddyLevel];
  143. }
  144. #pragma mark -背景音乐音量
  145. -(void)setYBBGMVolume:(float)bgmVolume{
  146. [_audioEffect setMusicPublishVolume:666 volume:bgmVolume];
  147. }
  148. #pragma mark -设置色彩滤镜效果
  149. -(void)setYBFilter:(UIImage *)image{
  150. TXBeautyManager *beautyManager = [_txLivePusher getBeautyManager];
  151. [beautyManager setFilter:image];
  152. }
  153. #pragma mark -播放音乐
  154. -(void)playBGMWithPath:(NSString *)musicPath{
  155. TXAudioMusicParam *musicPar = [[TXAudioMusicParam alloc]init];
  156. musicPar.ID = 666;
  157. musicPar.path = musicPath;
  158. musicPar.publish = YES;
  159. [_audioEffect setAllMusicVolume:60];//设置音量大小
  160. [_audioEffect startPlayMusic:musicPar onStart:^(NSInteger errCode) {
  161. if(errCode == 0){
  162. if([self.delegate respondsToSelector:@selector(playMusicBegin)]){
  163. [self.delegate playMusicBegin];
  164. }
  165. }
  166. } onProgress:^(NSInteger progressMs, NSInteger durationMs) {
  167. if([self.delegate respondsToSelector:@selector(playMusicProgress:Duration:)]){
  168. [self.delegate playMusicProgress:progressMs Duration:durationMs];
  169. }
  170. } onComplete:^(NSInteger errCode) {
  171. }];
  172. }
  173. #pragma mark -停止播放音乐
  174. -(void)stopBGM{
  175. [_audioEffect stopPlayMusic:666];
  176. }
  177. #pragma mark -发起云端混流
  178. -(void)MixTranscoding:(V2TXLiveTranscodingConfig *)config;
  179. {
  180. V2TXLiveCode hunliuCode = [_txLivePusher setMixTranscodingConfig:config];
  181. NSLog(@"ybliveRtcHunliu----:%ld",hunliuCode);
  182. }
  183. #pragma mark --直播推流监听
  184. /**
  185. * 直播推流器错误通知,推流器出现错误时,会回调该通知
  186. *
  187. * @param code 错误码 {@link V2TXLiveCode}。
  188. * @param msg 错误信息。
  189. * @param extraInfo 扩展信息。
  190. */
  191. - (void)onError:(V2TXLiveCode)code message:(NSString *)msg extraInfo:(NSDictionary *)extraInfo;
  192. {
  193. NSLog(@"rtcManager Error ==:%ld =msg=:%@ =dic=%@",code,msg,extraInfo);
  194. }
  195. /**
  196. * 麦克风采集音量值回调
  197. *
  198. * @param volume 音量大小。
  199. * @note 调用 {@link enableVolumeEvaluation} 开启采集音量大小提示之后,会收到这个回调通知。
  200. */
  201. - (void)onMicrophoneVolumeUpdate:(NSInteger)volume{
  202. }
  203. /**
  204. * 推流器连接状态回调通知
  205. *
  206. * @param status 推流器连接状态 {@link V2TXLivePushStatus}。
  207. * @param msg 连接状态信息。
  208. * @param extraInfo 扩展信息。
  209. */
  210. - (void)onPushStatusUpdate:(V2TXLivePushStatus)status message:(NSString *)msg extraInfo:(NSDictionary *)extraInfo;
  211. {
  212. if([self.delegate respondsToSelector:@selector(ybRTCPushStatusUpdate:message:extraInfo:)]){
  213. [self.delegate ybRTCPushStatusUpdate:status message:msg extraInfo:extraInfo];
  214. }
  215. }
  216. /**
  217. * 直播推流器统计数据回调
  218. *
  219. * @param statistics 推流器统计数据 {@link V2TXLivePusherStatistics}
  220. */
  221. - (void)onStatisticsUpdate:(V2TXLivePusherStatistics *)statistics;
  222. {
  223. }
  224. -(void)onWarning:(V2TXLiveCode)code message:(NSString *)msg extraInfo:(NSDictionary *)extraInfo
  225. {
  226. if (code == V2TXLIVE_WARNING_NETWORK_BUSY) {
  227. if ([self.delegate respondsToSelector:@selector(ybPushLiveStatus:)]) {
  228. [self.delegate ybPushLiveStatus:code];
  229. }
  230. }
  231. }
  232. #pragma mark -美狐回调
  233. -(void)onProcessVideoFrame:(V2TXLiveVideoFrame *)srcFrame dstFrame:(V2TXLiveVideoFrame *)dstFrame
  234. {
  235. if([self.delegate respondsToSelector:@selector(MHBeautyBlock:dstFrame:)]){
  236. [self.delegate MHBeautyBlock:srcFrame dstFrame:dstFrame];
  237. }
  238. }
  239. -(void)onGLContextDestroyed
  240. {
  241. if([self.delegate respondsToSelector:@selector(destoryMHBeauty)]){
  242. [self.delegate destoryMHBeauty];
  243. }
  244. }
  245. @end