Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: DataChannel not emitting new data on iOS #123

Merged
merged 1 commit into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -35,22 +36,10 @@ actual class DataChannel(val ios: RTCDataChannel) {
private val coroutineScope = MainScope()
private val dataChannelEvent = MutableSharedFlow<DataChannelEvent>()

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<Unit> = dataChannelEvent
Expand All @@ -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)
Expand Down Expand Up @@ -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) }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

package com.shepeliev.webrtckmp

import kotlinx.cinterop.BetaInteropApi
import kotlinx.cinterop.ExperimentalForeignApi
import kotlinx.cinterop.addressOf
import kotlinx.cinterop.memScoped
Expand All @@ -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 = [email protected]().ptr, length = size.toULong())
}
Expand Down
2 changes: 1 addition & 1 deletion webrtc-kmp/webrtc_kmp.podspec
Original file line number Diff line number Diff line change
@@ -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 = ''
Expand Down
Loading