【问题标题】:How to add blur and background blur to both sent and received WebRTC video tracks如何为发送和接收的 WebRTC 视频轨道添加模糊和背景模糊
【发布时间】:2022-09-29 11:24:37
【问题描述】:

如何为 webRTC 视频轨道添加模糊效果?我正在使用 WebRTC 构建一个视频通话应用程序,出于安全原因,我需要模糊使用我的应用程序的人的背景,还需要模糊来自另一端的所有视频。 (随机的人可以打电话,所以模糊他们所有的视频是一种安全措施,你可以在感觉舒服时关闭它)。

我如何开始本地视频捕获的一些代码

private var videoCapturer: CameraVideoCapturer = getCamera()

private fun getCamera(isFrontFacing: Boolean = true): CameraVideoCapturer {
    return Camera1Enumerator(true).run {
        deviceNames.find {
            if (isFrontFacing)
                isFrontFacing(it)
            else
                isBackFacing(it)
        }?.let {
            createCapturer(it, null)
        } ?: throw IllegalStateException()
    }
}


fun startLocalVideoCapture(localVideoOutput: SurfaceViewRenderer, localVideoOutputPiP: SurfaceViewRenderer? = null, localVideoOutputInAppPip: SurfaceViewRenderer? = null, isMicOn: Boolean = true) {
    localVideoOutput.setMirror(true)
    val localVideoSource = peerConnectionFactory.createVideoSource(false)
    val surfaceTextureHelper = SurfaceTextureHelper.create(Thread.currentThread().name, rootEglBase.eglBaseContext)
    (videoCapturer as VideoCapturer).initialize(surfaceTextureHelper, localVideoOutput.context, localVideoSource.capturerObserver)
    videoCapturer.startCapture(1280, 962, 24)
    localVideoTrack = peerConnectionFactory.createVideoTrack(LOCAL_TRACK_ID, localVideoSource)
    val localAudioTrack = peerConnectionFactory.createAudioTrack(
        LOCAL_AUDIO_TRACK_ID, peerConnectionFactory.createAudioSource(MediaConstraints())
    )
    localAudioTrack.setEnabled(isMicOn)
    localVideoTrack?.addSink(localVideoOutput)

    localStream = peerConnectionFactory.createLocalMediaStream(LOCAL_STREAM_ID)
    localStream.audioTracks.add(localAudioTrack)
    localStream.videoTracks.add(localVideoTrack)
    videoSender = peerConnection?.addTrack(localVideoTrack, arrayListOf(LOCAL_STREAM_ID))
    peerConnection?.addTrack(localAudioTrack, arrayListOf(LOCAL_STREAM_ID))
}

以及我如何接收视频:

override fun onAddStream(p0: MediaStream?) {
                super.onAddStream(p0)
                if (p0?.videoTracks?.isNotEmpty() == true) {
                    p0.videoTracks?.get(0)?.addSink(remote_view)
                    remoteVideoTrack = p0.videoTracks?.get(0)
                    callControlsViewModel.isClientCamOn.postValue(true)
                }
                if (p0?.audioTracks?.isNotEmpty() == true) {
                    remoteAudioTrack = p0.audioTracks?.get(0)
                    callControlsViewModel.isClientMicOn.postValue(true)
                }
            }

    标签: android android-studio webrtc webrtc-android


    【解决方案1】:

    对于模糊背景,您需要使用某种 AI 解决方案,例如https://google.github.io/mediapipe/solutions/selfie_segmentation.html。问题是这是非常 CPU/GPU 密集型的。如果您可以控制传出的 WebRTC VideoTrack,最好先进行背景模糊处理并发送修改后的 Track。

    如果您收到“正常”VideoTracks 并希望在收到后对其进行模糊处理,则对于任何设备来说都可能难以处理。最重要的是,由于您提到安全原因,因此接收“Vanilla”轨道并处理应用程序代码中的模糊是不明智的。它应该已经到达“安全”状态。

    【讨论】:

      【解决方案2】:

      我会尽量解释。

      1. 要模糊您自己的提要,您需要在发送前对视频帧进行一些预处理。以及如何做到这一点?

        1.1 不要使用 CameraVideoCapturer/VideoCapturer,而是扩展 VideoCapturer 类并在调用 sendFrame()/onFrameAvailable() 之前在那里处理您的帧

        1.2 首先为您的相机提要创建一个字节数组/位图。

        1.3 使用 OpenCV 操作你的帧(在你的情况下模糊背景)

        1.4 将帧从位图转换为 webrtc 视频帧,并将其传递给扩展 VideoCapturer 中的函数以发送到 webrtc(在步骤 1 中解释)

      2. 如何模糊远程视频?

        • 你不能。基本上,android 上的 Native WebRTC 不会为任何远程参与者提供对原始帧数据的访问权限。这也适用于音频流。你只会得到一个ID,就是这样。此外,不建议这样做,这应该取决于远程参与者是否要模糊他们的背景。
        • 如果你想实现这一点,你必须分叉 WebRTC 源代码并添加你自己的回调,它将传递给你远程用户的视频样本。没试过,但绝对可以。
        • 请记住,如果您还处理远程用户的视频流,则会导致延迟、发热和电池耗尽,您的处理开销将会增加。 OpenCV 速度很快,但以 15FPS 的速度处理帧可能是处理密集型的,而且您已经在本地视频流上进行了处理。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-11-23
        • 2017-01-11
        • 2014-06-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多