Skip to content

Commit 55e23a5

Browse files
tamimattafiTamim Attafi
and
Tamim Attafi
authored
fix: make ios data channel delegate strong reference (#123)
Co-authored-by: Tamim Attafi <[email protected]>
1 parent 0ae6d01 commit 55e23a5

File tree

4 files changed

+25
-17
lines changed

4 files changed

+25
-17
lines changed

gradle.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ org.jetbrains.compose.experimental.jscanvas.enabled=true
2424
org.jetbrains.compose.experimental.wasm.enabled=true
2525

2626
# Versions
27-
webRtcKmpVersion=0.125.0
27+
webRtcKmpVersion=0.125.1

webrtc-kmp/src/iosMain/kotlin/com/shepeliev/webrtckmp/DataChannel.kt

+21-15
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import WebRTC.RTCDataBuffer
66
import WebRTC.RTCDataChannel
77
import WebRTC.RTCDataChannelDelegateProtocol
88
import WebRTC.RTCDataChannelState
9+
import kotlinx.cinterop.BetaInteropApi
910
import kotlinx.cinterop.ExperimentalForeignApi
1011
import kotlinx.coroutines.MainScope
1112
import kotlinx.coroutines.cancel
@@ -35,22 +36,10 @@ actual class DataChannel(val ios: RTCDataChannel) {
3536
private val coroutineScope = MainScope()
3637
private val dataChannelEvent = MutableSharedFlow<DataChannelEvent>()
3738

38-
init {
39-
ios.delegate = object : NSObject(), RTCDataChannelDelegateProtocol {
40-
override fun dataChannel(dataChannel: RTCDataChannel, didChangeBufferedAmount: uint64_t) {
41-
// not implemented
42-
}
43-
44-
override fun dataChannel(dataChannel: RTCDataChannel, didReceiveMessageWithBuffer: RTCDataBuffer) {
45-
coroutineScope.launch {
46-
dataChannelEvent.emit(DataChannelEvent.MessageReceived(didReceiveMessageWithBuffer))
47-
}
48-
}
39+
private val delegate = Delegate()
4940

50-
override fun dataChannelDidChangeState(dataChannel: RTCDataChannel) {
51-
coroutineScope.launch { dataChannelEvent.emit(DataChannelEvent.StateChanged) }
52-
}
53-
}
41+
init {
42+
ios.delegate = delegate
5443
}
5544

5645
actual val onOpen: Flow<Unit> = dataChannelEvent
@@ -72,6 +61,7 @@ actual class DataChannel(val ios: RTCDataChannel) {
7261
.filterNotNull()
7362
.map { it.buffer.data.toByteArray() }
7463

64+
@BetaInteropApi
7565
actual fun send(data: ByteArray): Boolean {
7666
val buffer = RTCDataBuffer(data.toNSData(), true)
7767
return ios.sendData(buffer)
@@ -99,4 +89,20 @@ actual class DataChannel(val ios: RTCDataChannel) {
9989
else -> error("Unknown RTCDataChannelState: $state")
10090
}
10191
}
92+
93+
private inner class Delegate : NSObject(), RTCDataChannelDelegateProtocol {
94+
override fun dataChannel(dataChannel: RTCDataChannel, didChangeBufferedAmount: uint64_t) {
95+
// not implemented
96+
}
97+
98+
override fun dataChannel(dataChannel: RTCDataChannel, didReceiveMessageWithBuffer: RTCDataBuffer) {
99+
coroutineScope.launch {
100+
dataChannelEvent.emit(DataChannelEvent.MessageReceived(didReceiveMessageWithBuffer))
101+
}
102+
}
103+
104+
override fun dataChannelDidChangeState(dataChannel: RTCDataChannel) {
105+
coroutineScope.launch { dataChannelEvent.emit(DataChannelEvent.StateChanged) }
106+
}
107+
}
102108
}

webrtc-kmp/src/iosMain/kotlin/com/shepeliev/webrtckmp/NSData.kt

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
package com.shepeliev.webrtckmp
44

5+
import kotlinx.cinterop.BetaInteropApi
56
import kotlinx.cinterop.ExperimentalForeignApi
67
import kotlinx.cinterop.addressOf
78
import kotlinx.cinterop.memScoped
@@ -11,6 +12,7 @@ import platform.Foundation.NSData
1112
import platform.Foundation.create
1213
import platform.posix.memcpy
1314

15+
@BetaInteropApi
1416
internal fun ByteArray.toNSData(): NSData = memScoped {
1517
NSData.create(bytes = this@toNSData.toCValues().ptr, length = size.toULong())
1618
}

webrtc-kmp/webrtc_kmp.podspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |spec|
22
spec.name = 'webrtc_kmp'
3-
spec.version = '0.125.0'
3+
spec.version = '0.125.1'
44
spec.homepage = 'https://github.com/shepeliev/webrtc-kmp'
55
spec.source = { :http=> ''}
66
spec.authors = ''

0 commit comments

Comments
 (0)