RTCVideoCapturer capturerWithDeviceName: (NSString *) gets very slow after subsequent calls

I am creating an application voip


. I am calling the following code before every call I make:

NSMutableArray *m = [[NSMutableArray alloc] init];
NSMutableArray *o = [[NSMutableArray alloc] init];

//[m addObject:[[RTCPair alloc] initWithKey:@"maxFrameRate" value:@"30"]];
//[m addObject:[[RTCPair alloc] initWithKey:@"maxFrameRate" value:@"24"]];
//[m addObject:[[RTCPair alloc] initWithKey:@"maxHeight" value:@"180"]];
NSString* cameraID = nil;
for (AVCaptureDevice *captureDevice in [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]) {
    if (captureDevice.position == AVCaptureDevicePositionFront) {
        cameraID = [captureDevice localizedName];

capturer = [RTCVideoCapturer capturerWithDeviceName:cameraID]; //why so slow :(

RTCMediaConstraints *videoConstraints = [[RTCMediaConstraints alloc] initWithMandatoryConstraints:m optionalConstraints:o];
videoSource = [peerConnectionFactory videoSourceWithCapturer:capturer constraints:videoConstraints];

localVideoTrack = [peerConnectionFactory videoTrackWithID:@"ARDAMSv0" source:videoSource];

lms = [peerConnectionFactory mediaStreamWithLabel:@"ARDAMS"];

if (localVideoTrack) [lms addVideoTrack:localVideoTrack];
[lms addAudioTrack:[peerConnectionFactory audioTrackWithID:@"ARDAMSa0"]];

[peerConnection addStream:lms constraints:[[RTCMediaConstraints alloc] init]];

dispatch_async(dispatch_get_main_queue(), ^{
    [callViewController setLocalVideo];


After the call completes, I reset all of these variables (basically just set them to nil). I've seen this approach work very well, but a bug sneaked into it recently. Each subsequent call increases the execution time of this line

capturer = [RTCVideoCapturer capturerWithDeviceName:cameraID];


I tried to set up the capture only once at the start of the app, but then the app crashes on

videoSource = [peerConnectionFactory videoSourceWithCapturer:capturer constraints:videoConstraints];  


although all the parameters are initialized (I checked with the debugger).

Does anyone have any idea what this might be? I'm pretty sure this same code worked great. I haven't updated the library webrtc

, xcode

or iOS



source to share

1 answer

You are on the right track!

Instead, you need to create not only a capturer, but also a video source, localvideotrack, localaudiostream, and an RTCMediaStream object. Once localvideotrack and localaudiostream are created, add em to the RTCMediaStream object available to the class. Then, when you want to start a conversation, all you have to do is reuse the RTCMediaStream object you created at the beginning by adding it to a new peerconnection! Just remember to kill the peerconnection object when the conversation is over. Nothing needs to be done at that time for the RTCMediaStream object.

The reason things get so slow is that every time you create a new invader, it doesn't properly kill any previous instances that might already happen.



All Articles