Skip to content

Commit b390b80

Browse files
authored
Data stream helper methods (#732)
1 parent caa474d commit b390b80

File tree

5 files changed

+67
-1
lines changed

5 files changed

+67
-1
lines changed

.changeset/rich-penguins-walk.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+
Add sendText and sendFile helper methods to LocalParticipant for ease of use

.changeset/tall-trees-tan.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+
Add default name of "unknown" for StreamByteOptions to allow for no-arg construction

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ data class StreamBytesOptions(
5656
/**
5757
* The name of the file being sent.
5858
*/
59-
val name: String,
59+
val name: String = "unknown",
6060
/**
6161
* The total exact size in bytes, if known.
6262
*/

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,18 @@ import androidx.annotation.CheckResult
2020
import io.livekit.android.room.datastream.ByteStreamInfo
2121
import okio.Buffer
2222
import okio.FileSystem
23+
import okio.Path.Companion.toOkioPath
2324
import okio.Path.Companion.toPath
2425
import okio.Source
2526
import okio.source
27+
import java.io.File
2628
import java.io.InputStream
2729
import java.util.Arrays
2830
import kotlin.math.min
2931

32+
/**
33+
* A stream sender for sending byte-based messages (e.g. files, images, etc.)
34+
*/
3035
class ByteStreamSender(
3136
val info: ByteStreamInfo,
3237
destination: StreamDestination<ByteArray>,
@@ -51,8 +56,20 @@ private val byteDataChunker: DataChunker<ByteArray> = { data: ByteArray, chunkSi
5156
}
5257
}
5358

59+
/**
60+
* Reads the [file] and writes it to the data stream.
61+
*
62+
* @param file the file to read from
63+
*/
64+
@CheckResult
65+
suspend fun ByteStreamSender.writeFile(file: File): Result<Unit> {
66+
return write(FileSystem.SYSTEM.source(file.toOkioPath()))
67+
}
68+
5469
/**
5570
* Reads the file from [filePath] and writes it to the data stream.
71+
*
72+
* @param filePath absolute path to the file
5673
*/
5774
@CheckResult
5875
suspend fun ByteStreamSender.writeFile(filePath: String): Result<Unit> {

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ import io.livekit.android.room.participant.Participant
2929
import io.livekit.android.util.LKLog
3030
import livekit.LivekitModels.DataPacket
3131
import livekit.LivekitModels.DataStream
32+
import java.io.File
33+
import java.io.InputStream
3234
import java.util.Collections
3335
import java.util.Date
3436
import java.util.concurrent.atomic.AtomicLong
@@ -38,16 +40,53 @@ interface OutgoingDataStreamManager {
3840
/**
3941
* Start sending a stream of text. Call [TextStreamSender.close] when finished sending.
4042
*
43+
* @see [TextStreamSender.write]
4144
* @throws StreamException if the stream failed to open.
4245
*/
4346
suspend fun streamText(options: StreamTextOptions = StreamTextOptions()): TextStreamSender
4447

4548
/**
4649
* Start sending a stream of bytes. Call [ByteStreamSender.close] when finished sending.
4750
*
51+
* Extension functions are available for sending bytes from sources such as [InputStream] or [File].
52+
*
53+
* @see [ByteStreamSender.write]
54+
* @see [ByteStreamSender.writeFile]
4855
* @throws StreamException if the stream failed to open.
4956
*/
5057
suspend fun streamBytes(options: StreamBytesOptions): ByteStreamSender
58+
59+
/**
60+
* Send text through a data stream.
61+
*/
62+
@CheckResult
63+
suspend fun sendText(text: String, options: StreamTextOptions = StreamTextOptions()): Result<Unit> {
64+
val sender = streamText(options)
65+
val result = sender.write(text)
66+
67+
if (result.isFailure) {
68+
sender.close(result.exceptionOrNull()?.message ?: "Unknown error.")
69+
} else {
70+
sender.close()
71+
}
72+
return result
73+
}
74+
75+
/**
76+
* Send a file through a data stream.
77+
*/
78+
@CheckResult
79+
suspend fun sendFile(file: File, options: StreamBytesOptions = StreamBytesOptions()): Result<Unit> {
80+
val sender = streamBytes(options)
81+
val result = sender.writeFile(file)
82+
83+
if (result.isFailure) {
84+
sender.close(result.exceptionOrNull()?.message ?: "Unknown error.")
85+
} else {
86+
sender.close()
87+
}
88+
return result
89+
}
5190
}
5291

5392
/**

0 commit comments

Comments
 (0)