【问题标题】:CameraCapturer must be initialized before calling startCaptureCameraCapturer 必须在调用 startCapture 之前初始化
【发布时间】:2020-04-10 09:14:10
【问题描述】:

在 Android 中实现 WebRTC 时遇到此问题:

原因:java.lang.RuntimeException:CameraCapturer 必须在调用 startCapture 之前进行初始化。

build.gradle(:app)

dependencies {
  ......
  implementation 'org.webrtc:google-webrtc:1.0.+'
  ......
}

// 导致问题的块:

private void getVideoSource() {
    // isScreenCast = false
    videoSource = peerConnectionFactory.createVideoSource(false);
    surfaceTextureHelper = SurfaceTextureHelper.create(Thread.currentThread().getName(), rootEglBase.getEglBaseContext());
    VideoCapturer videoCapturer = createCameraCapturer(new Camera1Enumerator(false));
    localVideoTrack = peerConnectionFactory.createVideoTrack("200", videoSource);
    localVideoTrack.addSink(local_renderer);
    if(videoCapturer != null)
        videoCapturer.startCapture(1000,1000,30); // <- Here is the Exception

}

CameraCapturer 已弃用。 Camera1Capturer 现在可用。

【问题讨论】:

    标签: java android webrtc webrtc-android


    【解决方案1】:

    使用前需要初始化

     private void getVideoSource() {
        VideoCapturer videoCapturer = createVideoCapturer();
        VideoSource videoSource;
        //Create a VideoSource instance
        if (videoCapturer != null) {
            SurfaceTextureHelper surfaceTextureHelper = SurfaceTextureHelper.create("CaptureThread", rootEglBase.getEglBaseContext());
            videoSource = factory.createVideoSource(videoCapturer.isScreencast());
            videoCapturer.initialize(surfaceTextureHelper, this, videoSource.getCapturerObserver());
        }
       
    
        localVideoTrack = factory.createVideoTrack("100", videoSource);
    
        //Create MediaConstraints - Will be useful for specifying video and audio constraints.
        audioConstraints = new MediaConstraints();
        videoConstraints = new MediaConstraints();
    
        //create an AudioSource instance
        audioSource = factory.createAudioSource(audioConstraints);
        localAudioTrack = factory.createAudioTrack("101", audioSource);
    
        if (videoCapturer != null) {
            videoCapturer.startCapture(1024, 720, 30);
        }
        binding.localGlSurfaceView.setVisibility(View.VISIBLE);
        // And finally, with our VideoRenderer ready, we
        // can add our renderer to the VideoTrack.
        localVideoTrack.addSink(binding.localGlSurfaceView);
    }
    

    【讨论】:

    • 谢谢。您能指导我如何使用设备公共 IP 地址(假设我从 STUN 服务器获得)并将其保存在 SessionDescription 中吗?
    • 还没有……我还在纠结如何在不涉及任何服务器的情况下进行简单的 P2P 视频聊天。虽然我使用 Firebase Messaging 发送报价和答案。但这还没有完成。
    • 好的,让我分享一些代码。我会分享你的github链接。我最近做了这项工作。我们有一台公共服务器
    • 谢谢!非常感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-20
    • 2019-03-05
    • 2019-08-16
    • 2013-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多