From e60ae409138160884f2c68c6754b35569370a4bb Mon Sep 17 00:00:00 2001 From: Alex Shepeliev Date: Tue, 9 Jul 2024 11:40:11 +0300 Subject: [PATCH 1/9] android: add VideoProcessorFactory --- .../kotlin/com/shepeliev/webrtckmp/MediaDevices.kt | 2 ++ .../com/shepeliev/webrtckmp/VideoProcessorFactory.kt | 7 +++++++ .../androidMain/kotlin/com/shepeliev/webrtckmp/WebRtc.kt | 8 +++++--- webrtc-kmp/webrtc_kmp.podspec | 4 ++-- 4 files changed, 16 insertions(+), 5 deletions(-) create mode 100644 webrtc-kmp/src/androidMain/kotlin/com/shepeliev/webrtckmp/VideoProcessorFactory.kt diff --git a/webrtc-kmp/src/androidMain/kotlin/com/shepeliev/webrtckmp/MediaDevices.kt b/webrtc-kmp/src/androidMain/kotlin/com/shepeliev/webrtckmp/MediaDevices.kt index ade6cf7..bead01e 100644 --- a/webrtc-kmp/src/androidMain/kotlin/com/shepeliev/webrtckmp/MediaDevices.kt +++ b/webrtc-kmp/src/androidMain/kotlin/com/shepeliev/webrtckmp/MediaDevices.kt @@ -43,6 +43,7 @@ private object MediaDevicesImpl : MediaDevices { if (constraints.video != null) { checkCameraPermission() val videoSource = WebRtc.peerConnectionFactory.createVideoSource(false) + videoSource.setVideoProcessor(WebRtc.videoProcessorFactory?.createVideoProcessor()) val videoCaptureController = CameraVideoCaptureController( constraints.video, videoSource @@ -62,6 +63,7 @@ private object MediaDevicesImpl : MediaDevices { override suspend fun getDisplayMedia(): MediaStream { val videoSource = WebRtc.peerConnectionFactory.createVideoSource(false) + videoSource.setVideoProcessor(WebRtc.videoProcessorFactory?.createVideoProcessor()) val screenCaptureController = ScreenCaptureController(videoSource) val videoTrack = WebRtc.peerConnectionFactory.createVideoTrack( UUID.randomUUID().toString(), diff --git a/webrtc-kmp/src/androidMain/kotlin/com/shepeliev/webrtckmp/VideoProcessorFactory.kt b/webrtc-kmp/src/androidMain/kotlin/com/shepeliev/webrtckmp/VideoProcessorFactory.kt new file mode 100644 index 0000000..bcc002c --- /dev/null +++ b/webrtc-kmp/src/androidMain/kotlin/com/shepeliev/webrtckmp/VideoProcessorFactory.kt @@ -0,0 +1,7 @@ +package com.shepeliev.webrtckmp + +import org.webrtc.VideoProcessor + +fun interface VideoProcessorFactory { + fun createVideoProcessor(): VideoProcessor +} diff --git a/webrtc-kmp/src/androidMain/kotlin/com/shepeliev/webrtckmp/WebRtc.kt b/webrtc-kmp/src/androidMain/kotlin/com/shepeliev/webrtckmp/WebRtc.kt index 498b2b4..3236f36 100644 --- a/webrtc-kmp/src/androidMain/kotlin/com/shepeliev/webrtckmp/WebRtc.kt +++ b/webrtc-kmp/src/androidMain/kotlin/com/shepeliev/webrtckmp/WebRtc.kt @@ -20,12 +20,14 @@ object WebRtc { _rootEglBase ?: EglBase.create().also { _rootEglBase = it } } + lateinit var factoryInitializationOptionsBuilder: PeerConnectionFactory.InitializationOptions.Builder + private set + var videoEncoderFactory: VideoEncoderFactory? = null var videoDecoderFactory: VideoDecoderFactory? = null var customCameraEnumerator: CameraEnumerator? = null var customPeerConnectionFactory: PeerConnectionFactory? = null - lateinit var factoryInitializationOptionsBuilder: PeerConnectionFactory.InitializationOptions.Builder - private set + var videoProcessorFactory: VideoProcessorFactory? = null internal lateinit var applicationContext: Context private set @@ -60,7 +62,7 @@ object WebRtc { _rootEglBase = eglBase } - internal fun initialize(context: Context) { + fun initialize(context: Context) { applicationContext = context.applicationContext factoryInitializationOptionsBuilder = PeerConnectionFactory.InitializationOptions.builder(context) } diff --git a/webrtc-kmp/webrtc_kmp.podspec b/webrtc-kmp/webrtc_kmp.podspec index 24ed451..f44b02f 100644 --- a/webrtc-kmp/webrtc_kmp.podspec +++ b/webrtc-kmp/webrtc_kmp.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |spec| spec.name = 'webrtc_kmp' - spec.version = '0.125.1' + spec.version = '0.125.1-local' spec.homepage = 'https://github.com/shepeliev/webrtc-kmp' spec.source = { :http=> ''} spec.authors = '' @@ -51,4 +51,4 @@ Pod::Spec.new do |spec| } ] -end \ No newline at end of file +end From 4cd4f85c0abb05482ad6ea41f8170e56d3587b1d Mon Sep 17 00:00:00 2001 From: Alex Shepeliev Date: Tue, 9 Jul 2024 11:40:31 +0300 Subject: [PATCH 2/9] android: fix arg name --- .../kotlin/com/shepeliev/webrtckmp/MediaTrackConstraints.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/webrtc-kmp/src/commonMain/kotlin/com/shepeliev/webrtckmp/MediaTrackConstraints.kt b/webrtc-kmp/src/commonMain/kotlin/com/shepeliev/webrtckmp/MediaTrackConstraints.kt index 4c203d3..c30e9b6 100644 --- a/webrtc-kmp/src/commonMain/kotlin/com/shepeliev/webrtckmp/MediaTrackConstraints.kt +++ b/webrtc-kmp/src/commonMain/kotlin/com/shepeliev/webrtckmp/MediaTrackConstraints.kt @@ -162,8 +162,8 @@ class MediaTrackConstraintsBuilder(internal var constraints: MediaTrackConstrain constraints = constraints.copy(facingMode = constrain) } - fun frameRate(ratio: Double) { - constraints = constraints.copy(frameRate = ratio.asValueConstrain()) + fun frameRate(fps: Double) { + constraints = constraints.copy(frameRate = fps.asValueConstrain()) } fun frameRate(build: ValueOrConstrain.Constrain.() -> Unit) { From aef0c62b44ac014caa8498bb7b568dfc48680a1e Mon Sep 17 00:00:00 2001 From: Alex Shepeliev Date: Sat, 20 Jul 2024 09:08:34 +0300 Subject: [PATCH 3/9] ios: add VideoProcessorFactory --- .../webrtckmp/CameraVideoCaptureController.kt | 4 ++-- .../kotlin/com/shepeliev/webrtckmp/MediaDevices.kt | 14 ++++++++++---- .../shepeliev/webrtckmp/VideoProcessorFactory.kt | 10 ++++++++++ .../kotlin/com/shepeliev/webrtckmp/WebRtc.kt | 1 + webrtc-kmp/webrtc_kmp.podspec | 2 +- 5 files changed, 24 insertions(+), 7 deletions(-) create mode 100644 webrtc-kmp/src/iosMain/kotlin/com/shepeliev/webrtckmp/VideoProcessorFactory.kt diff --git a/webrtc-kmp/src/iosMain/kotlin/com/shepeliev/webrtckmp/CameraVideoCaptureController.kt b/webrtc-kmp/src/iosMain/kotlin/com/shepeliev/webrtckmp/CameraVideoCaptureController.kt index 694f00d..6a5a8e3 100644 --- a/webrtc-kmp/src/iosMain/kotlin/com/shepeliev/webrtckmp/CameraVideoCaptureController.kt +++ b/webrtc-kmp/src/iosMain/kotlin/com/shepeliev/webrtckmp/CameraVideoCaptureController.kt @@ -3,7 +3,7 @@ package com.shepeliev.webrtckmp import WebRTC.RTCCameraVideoCapturer -import WebRTC.RTCVideoSource +import WebRTC.RTCVideoCapturerDelegateProtocol import kotlinx.cinterop.ExperimentalForeignApi import kotlinx.cinterop.useContents import platform.AVFoundation.AVCaptureDevice @@ -19,7 +19,7 @@ import kotlin.math.abs internal class CameraVideoCaptureController( private val constraints: MediaTrackConstraints, - private val videoSource: RTCVideoSource, + private val videoSource: RTCVideoCapturerDelegateProtocol, ) : VideoCaptureController { private var videoCapturer: RTCCameraVideoCapturer? = null private var position: AVCaptureDevicePosition = AVCaptureDevicePositionBack diff --git a/webrtc-kmp/src/iosMain/kotlin/com/shepeliev/webrtckmp/MediaDevices.kt b/webrtc-kmp/src/iosMain/kotlin/com/shepeliev/webrtckmp/MediaDevices.kt index df9929f..0abd7a1 100644 --- a/webrtc-kmp/src/iosMain/kotlin/com/shepeliev/webrtckmp/MediaDevices.kt +++ b/webrtc-kmp/src/iosMain/kotlin/com/shepeliev/webrtckmp/MediaDevices.kt @@ -1,5 +1,3 @@ -@file:OptIn(ExperimentalForeignApi::class) - package com.shepeliev.webrtckmp import WebRTC.RTCCameraVideoCapturer @@ -10,6 +8,7 @@ import platform.Foundation.NSUUID internal actual val mediaDevices: MediaDevices = MediaDevicesImpl +@OptIn(ExperimentalForeignApi::class) private object MediaDevicesImpl : MediaDevices { override suspend fun getUserMedia(streamConstraints: MediaStreamConstraintsBuilder.() -> Unit): MediaStream { val constraints = MediaStreamConstraintsBuilder().let { @@ -29,8 +28,15 @@ private object MediaDevicesImpl : MediaDevices { val videoTrack = constraints.video?.let { videoConstraints -> val videoSource = WebRtc.peerConnectionFactory.videoSource() - val iosVideoTrack = WebRtc.peerConnectionFactory.videoTrackWithSource(videoSource, NSUUID.UUID().UUIDString()) - val videoCaptureController = CameraVideoCaptureController(videoConstraints, videoSource) + val videoProcessor = WebRtc.videoProcessorFactory?.createVideoProcessor(videoSource) + val iosVideoTrack = WebRtc.peerConnectionFactory.videoTrackWithSource( + source = videoSource, + trackId = NSUUID.UUID().UUIDString() + ) + val videoCaptureController = CameraVideoCaptureController( + constraints = videoConstraints, + videoSource = videoProcessor ?: videoSource + ) LocalVideoStreamTrack(iosVideoTrack, videoCaptureController) } diff --git a/webrtc-kmp/src/iosMain/kotlin/com/shepeliev/webrtckmp/VideoProcessorFactory.kt b/webrtc-kmp/src/iosMain/kotlin/com/shepeliev/webrtckmp/VideoProcessorFactory.kt new file mode 100644 index 0000000..90eb90e --- /dev/null +++ b/webrtc-kmp/src/iosMain/kotlin/com/shepeliev/webrtckmp/VideoProcessorFactory.kt @@ -0,0 +1,10 @@ +package com.shepeliev.webrtckmp + +import WebRTC.RTCVideoCapturerDelegateProtocol +import WebRTC.RTCVideoSource +import kotlinx.cinterop.ExperimentalForeignApi + +@OptIn(ExperimentalForeignApi::class) +fun interface VideoProcessorFactory { + fun createVideoProcessor(videoSource: RTCVideoSource): RTCVideoCapturerDelegateProtocol +} diff --git a/webrtc-kmp/src/iosMain/kotlin/com/shepeliev/webrtckmp/WebRtc.kt b/webrtc-kmp/src/iosMain/kotlin/com/shepeliev/webrtckmp/WebRtc.kt index 3e3c31a..58e0735 100644 --- a/webrtc-kmp/src/iosMain/kotlin/com/shepeliev/webrtckmp/WebRtc.kt +++ b/webrtc-kmp/src/iosMain/kotlin/com/shepeliev/webrtckmp/WebRtc.kt @@ -16,6 +16,7 @@ object WebRtc { var videoDecoderFactory: RTCVideoDecoderFactoryProtocol? = null var peerConnectionFactoryOptions: RTCPeerConnectionFactoryOptions? = null var customPeerConnectionFactory: RTCPeerConnectionFactory? = null + var videoProcessorFactory: VideoProcessorFactory? = null internal val peerConnectionFactory: RTCPeerConnectionFactory by lazy { customPeerConnectionFactory ?: run { diff --git a/webrtc-kmp/webrtc_kmp.podspec b/webrtc-kmp/webrtc_kmp.podspec index f44b02f..bb05ac8 100644 --- a/webrtc-kmp/webrtc_kmp.podspec +++ b/webrtc-kmp/webrtc_kmp.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |spec| spec.name = 'webrtc_kmp' - spec.version = '0.125.1-local' + spec.version = '0.125.0' spec.homepage = 'https://github.com/shepeliev/webrtc-kmp' spec.source = { :http=> ''} spec.authors = '' From c91d539e79a2f439fb4fc70a5b33c4d08fc65e90 Mon Sep 17 00:00:00 2001 From: Alex Shepeliev Date: Wed, 14 Aug 2024 16:37:32 +0300 Subject: [PATCH 4/9] Bump version --- gradle.properties | 2 +- webrtc-kmp/webrtc_kmp.podspec | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/gradle.properties b/gradle.properties index 3a23efd..caecd0c 100644 --- a/gradle.properties +++ b/gradle.properties @@ -24,4 +24,4 @@ org.jetbrains.compose.experimental.jscanvas.enabled=true org.jetbrains.compose.experimental.wasm.enabled=true # Versions -webRtcKmpVersion=0.125.1 +webRtcKmpVersion=0.125.2 diff --git a/webrtc-kmp/webrtc_kmp.podspec b/webrtc-kmp/webrtc_kmp.podspec index bb05ac8..c812823 100644 --- a/webrtc-kmp/webrtc_kmp.podspec +++ b/webrtc-kmp/webrtc_kmp.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |spec| spec.name = 'webrtc_kmp' - spec.version = '0.125.0' + spec.version = '0.125.2' spec.homepage = 'https://github.com/shepeliev/webrtc-kmp' spec.source = { :http=> ''} spec.authors = '' From c1c320c25b0efdd5d0e5e40b1941548df5fd29e6 Mon Sep 17 00:00:00 2001 From: Alex Shepeliev Date: Wed, 14 Aug 2024 17:41:50 +0300 Subject: [PATCH 5/9] Format code --- .../androidMain/kotlin/com/shepeliev/webrtckmp/WebRtc.kt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/webrtc-kmp/src/androidMain/kotlin/com/shepeliev/webrtckmp/WebRtc.kt b/webrtc-kmp/src/androidMain/kotlin/com/shepeliev/webrtckmp/WebRtc.kt index 3236f36..d42f54e 100644 --- a/webrtc-kmp/src/androidMain/kotlin/com/shepeliev/webrtckmp/WebRtc.kt +++ b/webrtc-kmp/src/androidMain/kotlin/com/shepeliev/webrtckmp/WebRtc.kt @@ -20,15 +20,15 @@ object WebRtc { _rootEglBase ?: EglBase.create().also { _rootEglBase = it } } - lateinit var factoryInitializationOptionsBuilder: PeerConnectionFactory.InitializationOptions.Builder - private set - var videoEncoderFactory: VideoEncoderFactory? = null var videoDecoderFactory: VideoDecoderFactory? = null var customCameraEnumerator: CameraEnumerator? = null var customPeerConnectionFactory: PeerConnectionFactory? = null var videoProcessorFactory: VideoProcessorFactory? = null + lateinit var factoryInitializationOptionsBuilder: PeerConnectionFactory.InitializationOptions.Builder + private set + internal lateinit var applicationContext: Context private set @@ -62,7 +62,7 @@ object WebRtc { _rootEglBase = eglBase } - fun initialize(context: Context) { + internal fun initialize(context: Context) { applicationContext = context.applicationContext factoryInitializationOptionsBuilder = PeerConnectionFactory.InitializationOptions.builder(context) } From 85d2f46811476fe61c107c0375b64d42b9edb213 Mon Sep 17 00:00:00 2001 From: Alex Shepeliev Date: Wed, 14 Aug 2024 17:46:26 +0300 Subject: [PATCH 6/9] Rename property --- .../com/shepeliev/webrtckmp/CameraVideoCaptureController.kt | 4 ++-- .../iosMain/kotlin/com/shepeliev/webrtckmp/MediaDevices.kt | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/webrtc-kmp/src/iosMain/kotlin/com/shepeliev/webrtckmp/CameraVideoCaptureController.kt b/webrtc-kmp/src/iosMain/kotlin/com/shepeliev/webrtckmp/CameraVideoCaptureController.kt index 6a5a8e3..7db8976 100644 --- a/webrtc-kmp/src/iosMain/kotlin/com/shepeliev/webrtckmp/CameraVideoCaptureController.kt +++ b/webrtc-kmp/src/iosMain/kotlin/com/shepeliev/webrtckmp/CameraVideoCaptureController.kt @@ -19,7 +19,7 @@ import kotlin.math.abs internal class CameraVideoCaptureController( private val constraints: MediaTrackConstraints, - private val videoSource: RTCVideoCapturerDelegateProtocol, + private val videoCapturerDelegate: RTCVideoCapturerDelegateProtocol, ) : VideoCaptureController { private var videoCapturer: RTCCameraVideoCapturer? = null private var position: AVCaptureDevicePosition = AVCaptureDevicePositionBack @@ -32,7 +32,7 @@ internal class CameraVideoCaptureController( override fun startCapture() { if (videoCapturer != null) return - videoCapturer = RTCCameraVideoCapturer(videoSource) + videoCapturer = RTCCameraVideoCapturer(videoCapturerDelegate) if (!this::device.isInitialized) selectDevice() selectFormat() selectFps() diff --git a/webrtc-kmp/src/iosMain/kotlin/com/shepeliev/webrtckmp/MediaDevices.kt b/webrtc-kmp/src/iosMain/kotlin/com/shepeliev/webrtckmp/MediaDevices.kt index 0abd7a1..955d2a3 100644 --- a/webrtc-kmp/src/iosMain/kotlin/com/shepeliev/webrtckmp/MediaDevices.kt +++ b/webrtc-kmp/src/iosMain/kotlin/com/shepeliev/webrtckmp/MediaDevices.kt @@ -35,7 +35,7 @@ private object MediaDevicesImpl : MediaDevices { ) val videoCaptureController = CameraVideoCaptureController( constraints = videoConstraints, - videoSource = videoProcessor ?: videoSource + videoCapturerDelegate = videoProcessor ?: videoSource ) LocalVideoStreamTrack(iosVideoTrack, videoCaptureController) } From 0479173aedfea9bc564430b607656b1f5fa035c9 Mon Sep 17 00:00:00 2001 From: Alex Shepeliev Date: Sat, 17 Aug 2024 18:49:16 +0300 Subject: [PATCH 7/9] Revert version --- gradle.properties | 2 +- webrtc-kmp/webrtc_kmp.podspec | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/gradle.properties b/gradle.properties index caecd0c..3a23efd 100644 --- a/gradle.properties +++ b/gradle.properties @@ -24,4 +24,4 @@ org.jetbrains.compose.experimental.jscanvas.enabled=true org.jetbrains.compose.experimental.wasm.enabled=true # Versions -webRtcKmpVersion=0.125.2 +webRtcKmpVersion=0.125.1 diff --git a/webrtc-kmp/webrtc_kmp.podspec b/webrtc-kmp/webrtc_kmp.podspec index c812823..c2c5e64 100644 --- a/webrtc-kmp/webrtc_kmp.podspec +++ b/webrtc-kmp/webrtc_kmp.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |spec| spec.name = 'webrtc_kmp' - spec.version = '0.125.2' + spec.version = '0.125.1' spec.homepage = 'https://github.com/shepeliev/webrtc-kmp' spec.source = { :http=> ''} spec.authors = '' From 3a9f286b8964d21f91ebd76ceca5d2fea06276fb Mon Sep 17 00:00:00 2001 From: Alex Shepeliev Date: Sat, 17 Aug 2024 18:52:39 +0300 Subject: [PATCH 8/9] Format --- webrtc-kmp/webrtc_kmp.podspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/webrtc-kmp/webrtc_kmp.podspec b/webrtc-kmp/webrtc_kmp.podspec index c2c5e64..24ed451 100644 --- a/webrtc-kmp/webrtc_kmp.podspec +++ b/webrtc-kmp/webrtc_kmp.podspec @@ -51,4 +51,4 @@ Pod::Spec.new do |spec| } ] -end +end \ No newline at end of file From fbdd99e001d2ae103e92a396ed58e596468782a4 Mon Sep 17 00:00:00 2001 From: Alex Shepeliev Date: Sat, 17 Aug 2024 18:58:51 +0300 Subject: [PATCH 9/9] Set VideoProcessor only if it os not null --- .../androidMain/kotlin/com/shepeliev/webrtckmp/MediaDevices.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/webrtc-kmp/src/androidMain/kotlin/com/shepeliev/webrtckmp/MediaDevices.kt b/webrtc-kmp/src/androidMain/kotlin/com/shepeliev/webrtckmp/MediaDevices.kt index bead01e..f0a0f6a 100644 --- a/webrtc-kmp/src/androidMain/kotlin/com/shepeliev/webrtckmp/MediaDevices.kt +++ b/webrtc-kmp/src/androidMain/kotlin/com/shepeliev/webrtckmp/MediaDevices.kt @@ -63,7 +63,7 @@ private object MediaDevicesImpl : MediaDevices { override suspend fun getDisplayMedia(): MediaStream { val videoSource = WebRtc.peerConnectionFactory.createVideoSource(false) - videoSource.setVideoProcessor(WebRtc.videoProcessorFactory?.createVideoProcessor()) + WebRtc.videoProcessorFactory?.createVideoProcessor()?.let { videoSource.setVideoProcessor(it) } val screenCaptureController = ScreenCaptureController(videoSource) val videoTrack = WebRtc.peerConnectionFactory.createVideoTrack( UUID.randomUUID().toString(),