diff --git a/android/src/main/java/io/agora/rtc/rawdata/react/AgoraRawdataModule.kt b/android/src/main/java/io/agora/rtc/rawdata/react/AgoraRawdataModule.kt index 3247063..c8b38da 100644 --- a/android/src/main/java/io/agora/rtc/rawdata/react/AgoraRawdataModule.kt +++ b/android/src/main/java/io/agora/rtc/rawdata/react/AgoraRawdataModule.kt @@ -11,48 +11,76 @@ import io.agora.rtc.rawdata.base.VideoFrame import java.util.* class AgoraRawdataModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) { + private var audioObserver: IAudioFrameObserver? = null + private var videoObserver: IVideoFrameObserver? = null + override fun getName(): String { return "AgoraRawdata" } @ReactMethod fun registerAudioFrameObserver(engineHandle: Double, promise: Promise) { - object : IAudioFrameObserver(engineHandle.toLong()) { - override fun onRecordAudioFrame(audioFrame: AudioFrame): Boolean { - return true - } + if (audioObserver == null) { + audioObserver = object : IAudioFrameObserver(engineHandle.toLong()) { + override fun onRecordAudioFrame(audioFrame: AudioFrame): Boolean { + return true + } - override fun onPlaybackAudioFrame(audioFrame: AudioFrame): Boolean { - return true - } + override fun onPlaybackAudioFrame(audioFrame: AudioFrame): Boolean { + return true + } - override fun onMixedAudioFrame(audioFrame: AudioFrame): Boolean { - return true - } + override fun onMixedAudioFrame(audioFrame: AudioFrame): Boolean { + return true + } - override fun onPlaybackAudioFrameBeforeMixing(uid: Int, audioFrame: AudioFrame): Boolean { - return true + override fun onPlaybackAudioFrameBeforeMixing(uid: Int, audioFrame: AudioFrame): Boolean { + return true + } } - }.registerAudioFrameObserver() - promise.resolve(0) + } + audioObserver?.registerAudioFrameObserver() + promise.resolve(null) + } + + @ReactMethod + fun unregisterAudioFrameObserver(promise: Promise) { + audioObserver?.let { + it.unregisterAudioFrameObserver() + audioObserver = null + } + promise.resolve(null) } @ReactMethod fun registerVideoFrameObserver(engineHandle: Double, promise: Promise) { - object : IVideoFrameObserver(engineHandle.toLong()) { - override fun onCaptureVideoFrame(videoFrame: VideoFrame): Boolean { - Arrays.fill(videoFrame.getuBuffer(), 0) - Arrays.fill(videoFrame.getvBuffer(), 0) - return true - } + if (videoObserver == null) { + videoObserver = object : IVideoFrameObserver(engineHandle.toLong()) { + override fun onCaptureVideoFrame(videoFrame: VideoFrame): Boolean { + Arrays.fill(videoFrame.getuBuffer(), 0) + Arrays.fill(videoFrame.getvBuffer(), 0) + return true + } - override fun onRenderVideoFrame(uid: Int, videoFrame: VideoFrame): Boolean { - Arrays.fill(videoFrame.getuBuffer(), -128) - Arrays.fill(videoFrame.getvBuffer(), -128) - return true + override fun onRenderVideoFrame(uid: Int, videoFrame: VideoFrame): Boolean { + // unsigned char value 255 + Arrays.fill(videoFrame.getuBuffer(), -1) + Arrays.fill(videoFrame.getvBuffer(), -1) + return true + } } - }.registerVideoFrameObserver() - promise.resolve(0) + } + videoObserver?.registerVideoFrameObserver() + promise.resolve(null) + } + + @ReactMethod + fun unregisterVideoFrameObserver(promise: Promise) { + videoObserver?.let { + it.unregisterVideoFrameObserver() + videoObserver = null + } + promise.resolve(null) } companion object { diff --git a/ios/React/AgoraRawdata.swift b/ios/React/AgoraRawdata.swift index adca685..c78deb5 100644 --- a/ios/React/AgoraRawdata.swift +++ b/ios/React/AgoraRawdata.swift @@ -77,13 +77,15 @@ class AgoraRawdata: NSObject, RCTBridgeModule, AgoraAudioFrameDelegate, AgoraVid return true } - func onCapture(_: AgoraVideoFrame) -> Bool { + func onCapture(_ videoFrame: AgoraVideoFrame) -> Bool { + memset(videoFrame.uBuffer, 0, Int(videoFrame.uStride * videoFrame.height) / 2) + memset(videoFrame.vBuffer, 0, Int(videoFrame.vStride * videoFrame.height) / 2) return true } func onRenderVideoFrame(_ videoFrame: AgoraVideoFrame, uid _: UInt) -> Bool { - memset(videoFrame.uBuffer, 0, Int(videoFrame.uStride * videoFrame.height) / 2) - memset(videoFrame.vBuffer, 0, Int(videoFrame.vStride * videoFrame.height) / 2) + memset(videoFrame.uBuffer, 255, Int(videoFrame.uStride * videoFrame.height) / 2) + memset(videoFrame.vBuffer, 255, Int(videoFrame.vStride * videoFrame.height) / 2) return true } }