| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- //
- // webRtcManager.m
- // 双子星云手机
- //
- // Created by xd h on 2024/9/5.
- //
- #import "webRtcManager.h"
- #import "webRtcManager+StatisticsReport.h"
- @interface webRtcManager ()<MediaStreamClientEventsDelegate>
- @end
- @implementation webRtcManager
- + (instancetype)shareManager {
- static webRtcManager *_instance;
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- _instance = [[self alloc] init];
- });
- return _instance;
- }
- - (instancetype)init {
- if (self = [super init]) {
- //[self registeNotification];
-
- _mediaStream = [[RTC_OBJC_TYPE(AMediaStream) alloc] initWithFrame:CGRectZero];
- [_mediaStream setEventDelegate:self];
- }
- return self;
- }
- #pragma mark 开始链接
- - (void)beginToLinkWebRtcFun
- {
- webRtcMsgModel * _webRtcMsgMod = ksharedAppDelegate.DeviceWebRtcMsgMod;
-
- //链接用
- NSString *signallingUrl = [[NSString alloc] initWithFormat:@"%@:%@",_webRtcMsgMod.data.signalling.domainName,_webRtcMsgMod.data.signalling.port];
- NSURL *url = [NSURL URLWithString:signallingUrl];
-
- //ice用
- NSString *iceUrl = [[NSString alloc] initWithFormat:@"%@:%@",_webRtcMsgMod.data.turn.domainName,_webRtcMsgMod.data.turn.port];
-
- NSMutableDictionary *ice = [NSMutableDictionary new];
- if(iceUrl){
- [ice setValue:iceUrl forKey:@"CHINANET"];
- [ice setValue:iceUrl forKey:@"CMNET"];
- [ice setValue:iceUrl forKey:@"UNICOM"];
- }
-
- NSString *roomName = _webRtcMsgMod.data.uniqueIdentifier;
-
- NSInteger result = [_mediaStream startUploadChannel:url ice:ice sn:roomName token:@"vclusters"];
-
- // NSInteger result = [_mediaStream start:url
- // ice:ice
- // sn:roomName
- // direct:0
- // fmt:1//1(h264) 5(h265)
- // videoWidth:1080.0
- // videoHeight:1920.0
- // fps:30
- // bitrate:3000
- // cardWidth:0
- // cardHeight:0
- // cardDensity:0
- // token:@"vclusters"];
-
- HLog(@"result:%ld",result)
- [_mediaStream setShouldGetStats:YES];
- }
- - (void)relinkWebRtcFun{
- [self beginToLinkWebRtcFun];
- }
- #pragma mark WebRTC 回调 MediaStreamClientEventsDelegate
- #pragma mark 链接状态发生变化
- -(void)onChangeConnectionStateFromPeerName:(NSString*)peerName didChangeIceConnectionState:(RTCIceConnectionState)state
- {
- HLog(@"channel onChangeConnectionStateFromPeerName: state:%ld",state)
- switch (state) {
- case RTCIceConnectionStateConnected:{
- //链接成功
-
- }
- break;
- case RTCIceConnectionStateCompleted:
- //链接完成
- break;
- case RTCIceConnectionStateFailed:
- //链接失败
- break;
- case RTCIceConnectionStateDisconnected:
- //链接断开
- [self relinkWebRtcFun];
- break;
- case RTCIceConnectionStateClosed:
- //链接关闭
- break;
-
- default:
- break;
- }
-
-
- }
- -(void)onIceConnectedFromPeerName:(NSString*)peerName{
- HLog(@"onIceConnectedFromPeerName:%@",peerName);
-
- }
- -(void)didGetStats:(NSString*)peerName stats:(RTC_OBJC_TYPE(RTCStatisticsReport) *)stats
- {
- //HLog(@"didGetStats:%@",stats)
- if(!_needToReportWebRtcType){
- [self reportWebRtcRePoportTypeIsChannel:YES withStats:stats];
- _needToReportWebRtcType = YES;
- [_mediaStream setShouldGetStats:NO];
- }
- }
- -(void)onAuthResultFromPeerName:(NSString*)peerName code:(int)code descriptions:(NSString*)descriptions
- {
- HLog(@"onAuthResultFromPeerName")
- }
- @end
|