davidliu
Committed by GitHub

Data stream helper methods (#732)

---
"client-sdk-android": patch
---
Add sendText and sendFile helper methods to LocalParticipant for ease of use
... ...
---
"client-sdk-android": patch
---
Add default name of "unknown" for StreamByteOptions to allow for no-arg construction
... ...
... ... @@ -56,7 +56,7 @@ data class StreamBytesOptions(
/**
* The name of the file being sent.
*/
val name: String,
val name: String = "unknown",
/**
* The total exact size in bytes, if known.
*/
... ...
... ... @@ -20,13 +20,18 @@ import androidx.annotation.CheckResult
import io.livekit.android.room.datastream.ByteStreamInfo
import okio.Buffer
import okio.FileSystem
import okio.Path.Companion.toOkioPath
import okio.Path.Companion.toPath
import okio.Source
import okio.source
import java.io.File
import java.io.InputStream
import java.util.Arrays
import kotlin.math.min
/**
* A stream sender for sending byte-based messages (e.g. files, images, etc.)
*/
class ByteStreamSender(
val info: ByteStreamInfo,
destination: StreamDestination<ByteArray>,
... ... @@ -52,7 +57,19 @@ private val byteDataChunker: DataChunker<ByteArray> = { data: ByteArray, chunkSi
}
/**
* Reads the [file] and writes it to the data stream.
*
* @param file the file to read from
*/
@CheckResult
suspend fun ByteStreamSender.writeFile(file: File): Result<Unit> {
return write(FileSystem.SYSTEM.source(file.toOkioPath()))
}
/**
* Reads the file from [filePath] and writes it to the data stream.
*
* @param filePath absolute path to the file
*/
@CheckResult
suspend fun ByteStreamSender.writeFile(filePath: String): Result<Unit> {
... ...
... ... @@ -29,6 +29,8 @@ import io.livekit.android.room.participant.Participant
import io.livekit.android.util.LKLog
import livekit.LivekitModels.DataPacket
import livekit.LivekitModels.DataStream
import java.io.File
import java.io.InputStream
import java.util.Collections
import java.util.Date
import java.util.concurrent.atomic.AtomicLong
... ... @@ -38,6 +40,7 @@ interface OutgoingDataStreamManager {
/**
* Start sending a stream of text. Call [TextStreamSender.close] when finished sending.
*
* @see [TextStreamSender.write]
* @throws StreamException if the stream failed to open.
*/
suspend fun streamText(options: StreamTextOptions = StreamTextOptions()): TextStreamSender
... ... @@ -45,9 +48,45 @@ interface OutgoingDataStreamManager {
/**
* Start sending a stream of bytes. Call [ByteStreamSender.close] when finished sending.
*
* Extension functions are available for sending bytes from sources such as [InputStream] or [File].
*
* @see [ByteStreamSender.write]
* @see [ByteStreamSender.writeFile]
* @throws StreamException if the stream failed to open.
*/
suspend fun streamBytes(options: StreamBytesOptions): ByteStreamSender
/**
* Send text through a data stream.
*/
@CheckResult
suspend fun sendText(text: String, options: StreamTextOptions = StreamTextOptions()): Result<Unit> {
val sender = streamText(options)
val result = sender.write(text)
if (result.isFailure) {
sender.close(result.exceptionOrNull()?.message ?: "Unknown error.")
} else {
sender.close()
}
return result
}
/**
* Send a file through a data stream.
*/
@CheckResult
suspend fun sendFile(file: File, options: StreamBytesOptions = StreamBytesOptions()): Result<Unit> {
val sender = streamBytes(options)
val result = sender.writeFile(file)
if (result.isFailure) {
sender.close(result.exceptionOrNull()?.message ?: "Unknown error.")
} else {
sender.close()
}
return result
}
}
/**
... ...