我终于有了一个可以与另一个对等点建立连接的应用程序,并且两个对等点都从远程接收音频和视频。它是一个使用 native RTC
API
的 iOS
应用程序。我现在正在尝试如何提高质量,因此我开始寻找针对媒体限制的选项。
这是我的初始化代码:
//init
peerConnectionFactory = [[RTCPeerConnectionFactory alloc] init];
[RTCPeerConnectionFactory initializeSSL];
//set 2 arrays to be used for the media constraints
NSMutableArray *m = [[NSMutableArray alloc] init];
NSMutableArray *o = [[NSMutableArray alloc] init];
//add stuff to the mandatory array
[m addObject:[[RTCPair alloc] initWithKey:@"OfferToReceiveAudio" value:@"true"]];
[m addObject:[[RTCPair alloc] initWithKey:@"OfferToReceiveVideo" value:@"true"]];
//add stuff to the optional array
//these lines are disabled for now, because my colleague working on the Android version does not support dtls yet
//[o addObject:[[RTCPair alloc] initWithKey:@"internalSctpDataChannels" value:@"true"]];
//[o addObject:[[RTCPair alloc] initWithKey:@"DtlsSrtpKeyAgreement" value:@"true"]];
//use the 2 arrays to init the media constraints
constraints = [[RTCMediaConstraints alloc] initWithMandatoryConstraints:m optionalConstraints:o];
//init stun and turn servers
RTCICEServer *turn = [[RTCICEServer alloc] initWithURI:[NSURL URLWithString:@"turn:numb.viagenie.ca:3478"] username:@"*" password:@"*"];
RTCICEServer *stun = [[RTCICEServer alloc] initWithURI:[NSURL URLWithString:@"stun:stun.l.google.com:19302"] username:@"" password:@""];
NSArray *iceServers = [[NSArray alloc] initWithObjects:turn, stun, nil];
//init the peerconnection object
peerConnection = [peerConnectionFactory peerConnectionWithICEServers:nil
constraints:constraints
delegate:self];
我尝试设置以下 RTCPair:
首先我尝试了这些,只是想看看它会做什么。
[o addObject:[[RTCPair alloc] initWithKey:@"minWidth" value:@"1280"]];
[o addObject:[[RTCPair alloc] initWithKey:@"minHeight" value:@"720"]];
它什么也没做。
然后我尝试了这些行(没有前 2 行)。
[m addObject:[[RTCPair alloc] initWithKey:@"minWidth" value:@"1280"]];
[m addObject:[[RTCPair alloc] initWithKey:@"minHeight" value:@"720"]];
这会导致应用程序陷入生成ice候选者的困境。
考虑到该设备可能不支持如此高分辨率,我尝试了以下操作。
[o addObject:[[RTCPair alloc] initWithKey:@"maxWidth" value:@"320"]];
[o addObject:[[RTCPair alloc] initWithKey:@"maxHeight" value:@"180"]];
再次没有效果。
我尝试了更多我不记得的组合,但对我来说,似乎将除 OfferToReceiveAudio
和 OfferToReceiveVideo
之外的任何内容添加到强制数组只会破坏整个过程,并且向可选数组添加任何内容都会被忽略(尽管设置 dtls
会导致无法连接到 Android
版本)。
最佳答案
我正在为对等连接初始化设置约束,但必须为视频源设置它们。创建本地媒体流时,您必须指定摄像机,使用该摄像机初始化捕获器,然后设置视频源以使用该捕获器。设置视频源时,您可以传递约束。
关于ios - 如何使用原生 webrtc 中的约束?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25505400/