iOS Webrtc - 捕获本地视频流时崩溃

3

我正在尝试使用来自Google代码库的webrtc库。我按照步骤创建了一个类似于APPRTC的项目,并编写了指令和代码,最终使其正常工作。我能够在两个设备之间进行视频会议。但是,当我尝试将其集成到旧项目中时,Webrtc会崩溃。以下是复现崩溃的步骤:

  1. 初始化Peer连接工厂
  2. 尝试在主线程中创建本地视频流
  3. 应用崩溃

在下面的代码片段中,当我尝试创建VideoSource时,出现了崩溃。欢迎任何提示或建议。

- (RTCVideoTrack *)createLocalVideoTrack {
  RTCVideoTrack *localVideoTrack = nil;
  if (_peerConnection && self.localMediaStream) {
    [_peerConnection removeStream:self.localMediaStream];
    self.localMediaStream=nil;
    self.localVideoTrack=nil;
    self.localAudioTrack=nil;
  }
  NSString *cameraID = nil;
  AVCaptureDevicePosition devicePosition;
  if (self.captureDevice == kWebrtcMediaCaptureDeviceFrontCam) {
    devicePosition = AVCaptureDevicePositionFront;
  }
  else{
    devicePosition = AVCaptureDevicePositionBack;
  }
  for (AVCaptureDevice *captureDevice in
       [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]) {
    if (captureDevice.position == devicePosition) {
      //[self configureCameraForHighestFrameRate:captureDevice];
      cameraID = [captureDevice localizedName];
      break;
    }
  }
  NSAssert(cameraID, @"Unable to get the front camera id");
  RTCVideoCapturer *capturer = [RTCVideoCapturer capturerWithDeviceName:cameraID];
  RTCMediaConstraints *mediaConstraints = [self defaultMediaStreamConstraints];
  RTCVideoSource *videoSource = [_factory videoSourceWithCapturer:capturer
                                                      constraints:mediaConstraints];
  localVideoTrack = [_factory videoTrackWithID:@"ARDAMSv0" source:videoSource];
  return localVideoTrack;
}

And the crash log

* thread #1: tid = 0x33125c, 0x320b9b2c libsystem_kernel.dylib`__psynch_cvwait + 24, queue = 'com.apple.main-thread'
    frame #0: 0x320b9b2c libsystem_kernel.dylib`__psynch_cvwait + 24
    frame #1: 0x32137388 libsystem_pthread.dylib`_pthread_cond_wait + 520
    frame #2: 0x3213826c libsystem_pthread.dylib`pthread_cond_wait + 40
    frame #3: 0x00515230  `rtc::Event::Wait(int) + 160
  * frame #4: 0x003e4912  `webrtc::MethodCall2<webrtc::PeerConnectionFactoryInterface, rtc::scoped_refptr<webrtc::VideoSourceInterface>, cricket::VideoCapturer*, webrtc::MediaConstraintsInterface const*>::Marshal(rtc::Thread*) + 46
    frame #5: 0x003e419c  `webrtc::PeerConnectionFactoryProxy::CreateVideoSource(cricket::VideoCapturer*, webrtc::MediaConstraintsInterface const*) + 68
    frame #6: 0x00414470  `-[RTCPeerConnectionFactory videoSourceWithCapturer:constraints:] + 192
    frame #7: 0x0001fc4e  `-[WebrtcManager createLocalVideoTrack](self=0x01896620, _cmd=0x0083e058) + 1662 at WebrtcManager.m:360
    frame #8: 0x0001ca96  `__40-[WebrtcManager initializeWebrtcManager]_block_invoke(.block_descriptor=<unavailable>) + 46 at WebrtcManager.m:46
    frame #9: 0x01420172 libdispatch.dylib`_dispatch_call_block_and_release + 10
    frame #10: 0x0142015e libdispatch.dylib`_dispatch_client_callout + 22
    frame #11: 0x01423e44 libdispatch.dylib`_dispatch_main_queue_callback_4CF + 1512
    frame #12: 0x234ad608 CoreFoundation`__CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 8
    frame #13: 0x234abd08 CoreFoundation`__CFRunLoopRun + 1512
    frame #14: 0x233f8200 CoreFoundation`CFRunLoopRunSpecific + 476
    frame #15: 0x233f8012 CoreFoundation`CFRunLoopRunInMode + 106
    frame #16: 0x2ac91200 GraphicsServices`GSEventRunModal + 136
    frame #17: 0x26b9ca58 UIKit`UIApplicationMain + 1440
    frame #18: 0x00279f60  `main(argc=1, argv=0x00e37a78) + 132 at main.m:17


我尝试了你的解决方案,但无法解决它。还有其他建议吗? - Hardik Kardani
我的建议是研究AppRTC代码的线程模型,并将其复制到您自己的应用程序中。当我提出崩溃问题时,webrtc团队向我推荐了这种方法。这对我很有效。 - Dhilip
2个回答

3

我的错!我试图在工作线程中创建PeerConnectionFactory和LocalVideoTrack!将它们移动到主线程后问题得到解决。我已经上传了Apprtc版本,在Apprtc-Swift上附有说明,可以在这个教程中找到。


您能详细说明一下您是如何解决这个问题的吗?我遇到了类似的情况,感谢您。 - vcalfa
在主线程中创建PeerConnectionFactory和本地视频轨道有很多方法。将您的代码包装在dispatch_async(dispatch_queue_main,.. )中是其中一种方法。 - Dhilip

1

我曾经遇到过同样的问题,但是通过添加两个Info-plist键值对来解决了相机和麦克风权限提示:

隐私-麦克风使用说明

隐私-相机使用说明

键值对的值是您希望在权限提示中显示的字符串。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接