From 11c6b04adafc0aee5588505d94f66db14e636634 Mon Sep 17 00:00:00 2001 From: Tamim Attafi Date: Tue, 6 Aug 2024 22:55:27 +0700 Subject: [PATCH] fix: make ios data channel delegate strong reference --- gradle.properties | 2 +- .../com/shepeliev/webrtckmp/DataChannel.kt | 36 +++++++++++-------- .../kotlin/com/shepeliev/webrtckmp/NSData.kt | 2 ++ webrtc-kmp/webrtc_kmp.podspec | 2 +- 4 files changed, 25 insertions(+), 17 deletions(-) diff --git a/gradle.properties b/gradle.properties index e03bc9d..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.0 +webRtcKmpVersion=0.125.1 diff --git a/webrtc-kmp/src/iosMain/kotlin/com/shepeliev/webrtckmp/DataChannel.kt b/webrtc-kmp/src/iosMain/kotlin/com/shepeliev/webrtckmp/DataChannel.kt index 6fa7498..efa4c82 100644 --- a/webrtc-kmp/src/iosMain/kotlin/com/shepeliev/webrtckmp/DataChannel.kt +++ b/webrtc-kmp/src/iosMain/kotlin/com/shepeliev/webrtckmp/DataChannel.kt @@ -6,6 +6,7 @@ import WebRTC.RTCDataBuffer import WebRTC.RTCDataChannel import WebRTC.RTCDataChannelDelegateProtocol import WebRTC.RTCDataChannelState +import kotlinx.cinterop.BetaInteropApi import kotlinx.cinterop.ExperimentalForeignApi import kotlinx.coroutines.MainScope import kotlinx.coroutines.cancel @@ -35,22 +36,10 @@ actual class DataChannel(val ios: RTCDataChannel) { private val coroutineScope = MainScope() private val dataChannelEvent = MutableSharedFlow() - init { - ios.delegate = object : NSObject(), RTCDataChannelDelegateProtocol { - override fun dataChannel(dataChannel: RTCDataChannel, didChangeBufferedAmount: uint64_t) { - // not implemented - } - - override fun dataChannel(dataChannel: RTCDataChannel, didReceiveMessageWithBuffer: RTCDataBuffer) { - coroutineScope.launch { - dataChannelEvent.emit(DataChannelEvent.MessageReceived(didReceiveMessageWithBuffer)) - } - } + private val delegate = Delegate() - override fun dataChannelDidChangeState(dataChannel: RTCDataChannel) { - coroutineScope.launch { dataChannelEvent.emit(DataChannelEvent.StateChanged) } - } - } + init { + ios.delegate = delegate } actual val onOpen: Flow = dataChannelEvent @@ -72,6 +61,7 @@ actual class DataChannel(val ios: RTCDataChannel) { .filterNotNull() .map { it.buffer.data.toByteArray() } + @BetaInteropApi actual fun send(data: ByteArray): Boolean { val buffer = RTCDataBuffer(data.toNSData(), true) return ios.sendData(buffer) @@ -99,4 +89,20 @@ actual class DataChannel(val ios: RTCDataChannel) { else -> error("Unknown RTCDataChannelState: $state") } } + + private inner class Delegate : NSObject(), RTCDataChannelDelegateProtocol { + override fun dataChannel(dataChannel: RTCDataChannel, didChangeBufferedAmount: uint64_t) { + // not implemented + } + + override fun dataChannel(dataChannel: RTCDataChannel, didReceiveMessageWithBuffer: RTCDataBuffer) { + coroutineScope.launch { + dataChannelEvent.emit(DataChannelEvent.MessageReceived(didReceiveMessageWithBuffer)) + } + } + + override fun dataChannelDidChangeState(dataChannel: RTCDataChannel) { + coroutineScope.launch { dataChannelEvent.emit(DataChannelEvent.StateChanged) } + } + } } diff --git a/webrtc-kmp/src/iosMain/kotlin/com/shepeliev/webrtckmp/NSData.kt b/webrtc-kmp/src/iosMain/kotlin/com/shepeliev/webrtckmp/NSData.kt index 97a3fcf..fb9d075 100644 --- a/webrtc-kmp/src/iosMain/kotlin/com/shepeliev/webrtckmp/NSData.kt +++ b/webrtc-kmp/src/iosMain/kotlin/com/shepeliev/webrtckmp/NSData.kt @@ -2,6 +2,7 @@ package com.shepeliev.webrtckmp +import kotlinx.cinterop.BetaInteropApi import kotlinx.cinterop.ExperimentalForeignApi import kotlinx.cinterop.addressOf import kotlinx.cinterop.memScoped @@ -11,6 +12,7 @@ import platform.Foundation.NSData import platform.Foundation.create import platform.posix.memcpy +@BetaInteropApi internal fun ByteArray.toNSData(): NSData = memScoped { NSData.create(bytes = this@toNSData.toCValues().ptr, length = size.toULong()) } diff --git a/webrtc-kmp/webrtc_kmp.podspec b/webrtc-kmp/webrtc_kmp.podspec index 8aaa216..d9b0189 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.1' spec.homepage = 'https://github.com/shepeliev/webrtc-kmp' spec.source = { :http=> ''} spec.authors = ''