webRtcManager.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. //
  2. // webRtcManager.m
  3. // 双子星云手机
  4. //
  5. // Created by xd h on 2024/9/5.
  6. //
  7. #import "webRtcManager.h"
  8. #import "webRtcManager+StatisticsReport.h"
  9. @interface webRtcManager ()<MediaStreamClientEventsDelegate>
  10. @end
  11. @implementation webRtcManager
  12. + (instancetype)shareManager {
  13. static webRtcManager *_instance;
  14. static dispatch_once_t onceToken;
  15. dispatch_once(&onceToken, ^{
  16. _instance = [[self alloc] init];
  17. });
  18. return _instance;
  19. }
  20. - (instancetype)init {
  21. if (self = [super init]) {
  22. //[self registeNotification];
  23. _mediaStream = [[RTC_OBJC_TYPE(AMediaStream) alloc] initWithFrame:CGRectZero];
  24. [_mediaStream setEventDelegate:self];
  25. }
  26. return self;
  27. }
  28. #pragma mark 开始链接
  29. - (void)beginToLinkWebRtcFun
  30. {
  31. webRtcMsgModel * _webRtcMsgMod = ksharedAppDelegate.DeviceWebRtcMsgMod;
  32. //链接用
  33. NSString *signallingUrl = [[NSString alloc] initWithFormat:@"%@:%@",_webRtcMsgMod.data.signalling.domainName,_webRtcMsgMod.data.signalling.port];
  34. NSURL *url = [NSURL URLWithString:signallingUrl];
  35. //ice用
  36. NSString *iceUrl = [[NSString alloc] initWithFormat:@"%@:%@",_webRtcMsgMod.data.turn.domainName,_webRtcMsgMod.data.turn.port];
  37. NSMutableDictionary *ice = [NSMutableDictionary new];
  38. if(iceUrl){
  39. [ice setValue:iceUrl forKey:@"CHINANET"];
  40. [ice setValue:iceUrl forKey:@"CMNET"];
  41. [ice setValue:iceUrl forKey:@"UNICOM"];
  42. }
  43. NSString *roomName = _webRtcMsgMod.data.uniqueIdentifier;
  44. NSInteger result = [_mediaStream startUploadChannel:url ice:ice sn:roomName token:@"vclusters"];
  45. // NSInteger result = [_mediaStream start:url
  46. // ice:ice
  47. // sn:roomName
  48. // direct:0
  49. // fmt:1//1(h264) 5(h265)
  50. // videoWidth:1080.0
  51. // videoHeight:1920.0
  52. // fps:30
  53. // bitrate:3000
  54. // cardWidth:0
  55. // cardHeight:0
  56. // cardDensity:0
  57. // token:@"vclusters"];
  58. HLog(@"result:%ld",result)
  59. [_mediaStream setShouldGetStats:YES];
  60. }
  61. - (void)relinkWebRtcFun{
  62. [self beginToLinkWebRtcFun];
  63. }
  64. #pragma mark WebRTC 回调 MediaStreamClientEventsDelegate
  65. #pragma mark 链接状态发生变化
  66. -(void)onChangeConnectionStateFromPeerName:(NSString*)peerName didChangeIceConnectionState:(RTCIceConnectionState)state
  67. {
  68. HLog(@"channel onChangeConnectionStateFromPeerName: state:%ld",state)
  69. switch (state) {
  70. case RTCIceConnectionStateConnected:{
  71. //链接成功
  72. }
  73. break;
  74. case RTCIceConnectionStateCompleted:
  75. //链接完成
  76. break;
  77. case RTCIceConnectionStateFailed:
  78. //链接失败
  79. break;
  80. case RTCIceConnectionStateDisconnected:
  81. //链接断开
  82. [self relinkWebRtcFun];
  83. break;
  84. case RTCIceConnectionStateClosed:
  85. //链接关闭
  86. break;
  87. default:
  88. break;
  89. }
  90. }
  91. -(void)onIceConnectedFromPeerName:(NSString*)peerName{
  92. HLog(@"onIceConnectedFromPeerName:%@",peerName);
  93. }
  94. -(void)didGetStats:(NSString*)peerName stats:(RTC_OBJC_TYPE(RTCStatisticsReport) *)stats
  95. {
  96. //HLog(@"didGetStats:%@",stats)
  97. if(!_needToReportWebRtcType){
  98. [self reportWebRtcRePoportTypeIsChannel:YES withStats:stats];
  99. _needToReportWebRtcType = YES;
  100. [_mediaStream setShouldGetStats:NO];
  101. }
  102. }
  103. -(void)onAuthResultFromPeerName:(NSString*)peerName code:(int)code descriptions:(NSString*)descriptions
  104. {
  105. HLog(@"onAuthResultFromPeerName")
  106. }
  107. @end