SrtConnection

class SrtConnection(scope: CoroutineScope, options: SrtConnection.Options = Options()) : Closeable

Owns one SRT peer connection. connect suspends until a caller connects or a listener accepts a peer. No stream multiplexing or media framing is performed.

scope owns disconnection monitoring; its cancellation closes this connection. Close the connection (for example with use) before leaving that scope normally. After close/failure, this instance can connect again while its scope is active.

Parameters

scope

Owner of connection monitoring; its context must contain a Job.

options

Default socket configuration, overridden by supported URI query parameters.

Constructors

Link copied to clipboard
constructor(scope: CoroutineScope, options: SrtConnection.Options = Options())

Types

Link copied to clipboard
object Companion
Link copied to clipboard

Handshake role, independent of whether media is sent or received.

Link copied to clipboard
class Options(val latencyMs: Int = 120, val connectTimeoutMs: Int = 3000, val passphrase: String? = null, val streamId: String? = null)

Default native socket configuration. Typed durations are milliseconds; URI latency uses microseconds and is rounded up to whole milliseconds.

Link copied to clipboard

Observable lifecycle of the current connection attempt.

Properties

Link copied to clipboard
val inputs: Flow<ByteArray>

Cold, unbuffered message flow for a single collector. Each collection uses the currently open peer. Completion or cancellation closes that peer. Concurrent readers are rejected without interrupting the existing reader. I/O failures propagate to the collector; no reconnection is automatic.

Link copied to clipboard

Reads the native peer state; false while connecting, listening or closed.

Link copied to clipboard

Native local address once bound; null before binding or after close. Port zero exposes the allocated port here.

Link copied to clipboard

Selected handshake role, or null when closed.

Link copied to clipboard

A fresh native statistics snapshot while OPEN; null in all other states.

Link copied to clipboard

State of the current attempt. CLOSED is the initial state; fast transitions may be conflated.

Link copied to clipboard
val uri: URI?

Original endpoint, including its query, retained until the attempt/connection closes.

Functions

Link copied to clipboard
open override fun close()

Cancels pending setup and monitoring and releases listener/peer sockets. Idempotent and reusable: a later connect starts a new native socket. Queued transport bytes are discarded; close does not flush pending sends. The owning scope is never cancelled by this method.

Link copied to clipboard
suspend fun connect(uri: URI)

Establishes one peer connection. In listener mode this suspends until accept; observe LISTENING and localAddress to discover a dynamically allocated port.

suspend fun connect(uri: String)

Parses an endpoint string and connects using connect.

Link copied to clipboard
suspend fun receive(): ByteArray

Receives one message into an owned byte array. Only one receive or inputs collector may be active. Cancellation closes an active receive's connection.

Link copied to clipboard
suspend fun send(data: ByteArray)

Sends transport bytes as messages of at most 1316 bytes, applying native backpressure. Concurrent calls retain their chunk ordering. Do not mutate data until return. Success means libsrt accepted the bytes, not that the peer has consumed them. Cancelling an active send closes the connection; cancelling a queued sender leaves the active sender intact. Empty arrays produce no messages.