1717package io.livekit.android.room
1818
1919import android.os.SystemClock
20+ import androidx.annotation.CheckResult
2021import androidx.annotation.VisibleForTesting
2122import com.google.protobuf.ByteString
2223import com.vdurmont.semver4j.Semver
@@ -82,7 +83,6 @@ import livekit.org.webrtc.RtpSender
8283import livekit.org.webrtc.RtpTransceiver
8384import livekit.org.webrtc.RtpTransceiver.RtpTransceiverInit
8485import livekit.org.webrtc.SessionDescription
85- import java.net.ConnectException
8686import java.nio.ByteBuffer
8787import javax.inject.Inject
8888import javax.inject.Named
@@ -445,7 +445,7 @@ internal constructor(
445445 * reconnect Signal and PeerConnections
446446 */
447447 @Synchronized
448- @VisibleForTesting
448+ @VisibleForTesting(otherwise = VisibleForTesting . PACKAGE_PRIVATE )
449449 fun reconnect () {
450450 if (reconnectingJob?.isActive == true ) {
451451 LKLog .d { " Reconnection is already in progress" }
@@ -625,22 +625,32 @@ internal constructor(
625625 }
626626 }
627627
628- internal suspend fun sendData (dataPacket : LivekitModels .DataPacket ) {
629- ensurePublisherConnected(dataPacket.kind)
628+ @CheckResult
629+ internal suspend fun sendData (dataPacket : LivekitModels .DataPacket ): Result <Unit > {
630+ try {
631+ ensurePublisherConnected(dataPacket.kind)
630632
631- val buf = DataChannel .Buffer (
632- ByteBuffer .wrap(dataPacket.toByteArray()),
633- true ,
634- )
633+ val buf = DataChannel .Buffer (
634+ ByteBuffer .wrap(dataPacket.toByteArray()),
635+ true ,
636+ )
635637
636- val channel = dataChannelForKind(dataPacket.kind)
637- ? : throw TrackException . PublishException (" channel not established for ${dataPacket.kind.name} " )
638+ val channel = dataChannelForKind(dataPacket.kind)
639+ ? : throw RoomException . ConnectException (" channel not established for ${dataPacket.kind.name} " )
638640
639- channel.send(buf)
641+ channel.send(buf)
642+ } catch (e: Exception ) {
643+ return Result .failure(e)
644+ }
645+ return Result .success(Unit )
640646 }
641647
642648 internal suspend fun waitForBufferStatusLow (kind : LivekitModels .DataPacket .Kind ) {
643- ensurePublisherConnected(kind)
649+ try {
650+ ensurePublisherConnected(kind)
651+ } catch (e: Exception ) {
652+ return
653+ }
644654 val manager = when (kind) {
645655 LivekitModels .DataPacket .Kind .RELIABLE -> reliableDataChannelManager
646656 LivekitModels .DataPacket .Kind .LOSSY -> lossyDataChannelManager
@@ -650,11 +660,12 @@ internal constructor(
650660 }
651661
652662 if (manager == null ) {
653- throw IllegalStateException ( " Not connected! " )
663+ return
654664 }
655665 manager.waitForBufferedAmountLow(DATA_CHANNEL_LOW_THRESHOLD .toLong())
656666 }
657667
668+ @Throws(exceptionClasses = [RoomException .ConnectException ::class ])
658669 private suspend fun ensurePublisherConnected (kind : LivekitModels .DataPacket .Kind ) {
659670 if (! isSubscriberPrimary) {
660671 return
@@ -671,7 +682,7 @@ internal constructor(
671682 this .negotiatePublisher()
672683 }
673684
674- val targetChannel = dataChannelForKind(kind) ? : throw IllegalArgumentException ( " Unknown data packet kind !" )
685+ val targetChannel = dataChannelForKind(kind) ? : throw RoomException . ConnectException ( " Publisher isn't setup yet! Is room not connected? !" )
675686 if (targetChannel.state() == DataChannel .State .OPEN ) {
676687 return
677688 }
@@ -685,14 +696,14 @@ internal constructor(
685696 delay(50 )
686697 }
687698
688- throw ConnectException (" could not establish publisher connection" )
699+ throw RoomException . ConnectException (" could not establish publisher connection" )
689700 }
690701
691702 private fun dataChannelForKind (kind : LivekitModels .DataPacket .Kind ) =
692703 when (kind) {
693704 LivekitModels .DataPacket .Kind .RELIABLE -> reliableDataChannel
694705 LivekitModels .DataPacket .Kind .LOSSY -> lossyDataChannel
695- else -> null
706+ LivekitModels . DataPacket . Kind . UNRECOGNIZED -> throw IllegalArgumentException ( " Unknown data packet kind! " )
696707 }
697708
698709 private fun getPublisherOfferConstraints (): MediaConstraints {
@@ -1137,6 +1148,7 @@ internal constructor(
11371148
11381149 val dataChannelInfos = LivekitModels .DataPacket .Kind .values()
11391150 .toList()
1151+ .filterNot { it == LivekitModels .DataPacket .Kind .UNRECOGNIZED }
11401152 .mapNotNull { kind -> dataChannelForKind(kind) }
11411153 .map { dataChannel ->
11421154 LivekitRtc .DataChannelInfo .newBuilder()
0 commit comments