Skip to content

Commit 85e5bf5

Browse files
committed
Update docs for stream receivers and errors
1 parent 990b5a5 commit 85e5bf5

5 files changed

Lines changed: 62 additions & 6 deletions

File tree

.changeset/new-tomatoes-grow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"client-sdk-android": patch
3+
---
4+
5+
Clarified documentation regarding data stream receivers and errors

livekit-android-sdk/src/main/java/io/livekit/android/room/datastream/StreamException.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2025 LiveKit, Inc.
2+
* Copyright 2025-2026 LiveKit, Inc.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -63,7 +63,7 @@ sealed class StreamException(message: String? = null) : Exception(message) {
6363
class FileInfoUnavailableException : StreamException()
6464

6565
/**
66-
*
66+
* Encryption of the data chunks did not match the declared encryption type.
6767
*/
6868
class EncryptionTypeMismatch(message: String? = null) : StreamException(message)
6969
}

livekit-android-sdk/src/main/java/io/livekit/android/room/datastream/incoming/BaseStreamReceiver.kt

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2025 LiveKit, Inc.
2+
* Copyright 2025-2026 LiveKit, Inc.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,14 +16,42 @@
1616

1717
package io.livekit.android.room.datastream.incoming
1818

19+
import io.livekit.android.room.datastream.StreamException
1920
import kotlinx.coroutines.channels.Channel
2021
import kotlinx.coroutines.flow.Flow
2122
import kotlinx.coroutines.flow.catch
2223
import kotlinx.coroutines.flow.first
2324
import kotlinx.coroutines.flow.fold
2425

26+
/**
27+
* Base class for reading incoming data streams.
28+
*
29+
* @see [flow]
30+
* @see [readNext]
31+
* @see [readAll]
32+
*/
2533
abstract class BaseStreamReceiver<T>(private val source: Channel<ByteArray>) {
2634

35+
/**
36+
* A [Flow] of stream data as it arrives.
37+
*
38+
* Collect this flow to process incoming data incrementally. The flow completes normally when
39+
* the stream receives all the data without error. If the stream ends abnormally, the
40+
* flow fails with a [StreamException] after all buffered chunks have been emitted.
41+
*
42+
* Example:
43+
* ```
44+
* reader.flow
45+
* .catch { e ->
46+
* if (e is StreamException) {
47+
* handleStreamError(e)
48+
* } else {
49+
* throw e
50+
* }
51+
* }
52+
* .collect { chunk -> process(chunk) }
53+
* ```
54+
*/
2755
abstract val flow: Flow<T>
2856

2957
internal fun close(error: Exception?) {
@@ -34,14 +62,18 @@ abstract class BaseStreamReceiver<T>(private val source: Channel<ByteArray>) {
3462
* Suspends and waits for the next piece of data.
3563
*
3664
* @return the next available piece of data.
37-
* @throws NoSuchElementException when the stream is closed and no more data is available.
65+
* @throws NoSuchElementException when the stream is closed normally and no more data is available.
66+
* @throws StreamException when the stream is closed abnormally.
3867
*/
3968
suspend fun readNext(): T {
4069
return flow.first()
4170
}
4271

4372
/**
4473
* Suspends and waits for all available data until the stream is closed.
74+
*
75+
* [StreamException]s are swallowed; this returns all data received before the stream closed,
76+
* whether normally or abnormally.
4577
*/
4678
suspend fun readAll(): List<T> {
4779
flow.catch { }

livekit-android-sdk/src/main/java/io/livekit/android/room/datastream/incoming/ByteStreamReceiver.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2025 LiveKit, Inc.
2+
* Copyright 2025-2026 LiveKit, Inc.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,12 +16,21 @@
1616

1717
package io.livekit.android.room.datastream.incoming
1818

19+
import io.livekit.android.room.Room
1920
import io.livekit.android.room.datastream.ByteStreamInfo
2021
import kotlinx.coroutines.channels.Channel
2122
import kotlinx.coroutines.flow.Flow
2223
import kotlinx.coroutines.flow.receiveAsFlow
2324

25+
/**
26+
* Receiver for an incoming byte stream.
27+
*
28+
* Provided to [ByteStreamHandler] callbacks registered through [Room].
29+
*
30+
* @see Room.registerByteStreamHandler
31+
*/
2432
class ByteStreamReceiver(
33+
/** Metadata for this stream. */
2534
val info: ByteStreamInfo,
2635
channel: Channel<ByteArray>,
2736
) : BaseStreamReceiver<ByteArray>(channel) {

livekit-android-sdk/src/main/java/io/livekit/android/room/datastream/incoming/TextStreamReceiver.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2025 LiveKit, Inc.
2+
* Copyright 2025-2026 LiveKit, Inc.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,13 +16,23 @@
1616

1717
package io.livekit.android.room.datastream.incoming
1818

19+
import io.livekit.android.room.Room
1920
import io.livekit.android.room.datastream.TextStreamInfo
2021
import kotlinx.coroutines.channels.Channel
2122
import kotlinx.coroutines.flow.Flow
2223
import kotlinx.coroutines.flow.map
2324
import kotlinx.coroutines.flow.receiveAsFlow
2425

26+
/**
27+
* Receiver for an incoming text stream.
28+
*
29+
* Chunks are decoded as UTF-8 strings. Provided to [TextStreamHandler] callbacks registered
30+
* through [Room].
31+
*
32+
* @see Room.registerTextStreamHandler
33+
*/
2534
class TextStreamReceiver(
35+
/** Metadata for this stream. */
2636
val info: TextStreamInfo,
2737
source: Channel<ByteArray>,
2838
) : BaseStreamReceiver<String>(source) {

0 commit comments

Comments
 (0)