Setting Up Live Streaming
To see the live view the user will have to create a LiveStreamClient object. Which will initialize the live view and start the streaming to a player that can be fetched from the newly created object.
Initiating a Live Stream Connection
To create a LiveStreamClient you will need
device(required): TheDeviceobject of the camera you want to stream from.streamType(optional): Whether to request theMAINSTREAMorSUBSTREAMresolution. Defaults toStreamType.MAINSTREAM.autoDisconnectAfter(optional): Milliseconds of inactivity after which the stream auto-disconnects. Defaults to0L(disabled).prewarmEnabled(optional): Whentrue, the constructor prewarms — fetches credentials, opens signalling, prepares the SDP offer — and holds the offer untilconnect(). Whenfalse(default) the constructor connects immediately.
You can create an object likewise:
val client = LiveStreamClient(
device = device,
)
Prewarming a Connection
Setting up a stream takes time that the user spends looking at a blank frame. When you know a camera is about to be viewed, create the client with prewarmEnabled = true and warm it up in advance, so that starting the stream later is close to instant.
val client = LiveStreamClient(
device = device,
prewarmEnabled = true,
) // the constructor prewarms; calling client.prewarm() again is harmless
A prewarmed client holds its signaling connection open with a keep-alive, which clients created without prewarmEnabled do not. Call connect() when the user actually opens the camera:
client.connect()
To know whether the client is warmed up and ready, the following property can be used:
viewModelScope.launch {
client.isPrewarmed.collectLatest { isPrewarmed ->
if (isPrewarmed) {
// The connection is ready, calling connect() will start the stream quickly
}
}
}