Nucleus
Media player

StreamingNew

Play HLS, MPEG-DASH and Smooth Streaming, live streams with a DVR window, audio languages, video variants, subtitles, request headers and timed metadata.

View as Markdown

Streaming formats follow every rule of a file: exact positions, the frame containing a seek target on screen whatever segment it is in, one end, the same errors. Each format is a module: add it and the players open its URLs.

media-hlsmacOSWindowsLinuxAndroidiOSWebDesktop Gradle plugin: Not neededDesktop window runtime: Not needed
media-dashWindowsLinuxAndroidWebDesktop Gradle plugin: Not neededDesktop window runtime: Not needed
media-smoothstreamingLinuxAndroidWebDesktop Gradle plugin: Not neededDesktop window runtime: Not needed

Add a format

FormatModuleAndroidWebWindowsmacOS / iOSLinux
HLS (.m3u8)media-hlsmedia3-exoplayer-hlshls.js over MSEMedia FoundationAVFoundationGStreamer
MPEG-DASH (.mpd)media-dashmedia3-exoplayer-dashdash.jsMedia Foundation—GStreamer
Smooth Streaming (.ism/Manifest)media-smoothstreamingmedia3-exoplayer-smoothstreamingdash.js + its MSS plugin——GStreamer (no seeks)
build.gradle.kts
commonMain.dependencies {
    implementation("dev.nucleusframework:nucleus.media-hls:3.0.0")
    implementation("dev.nucleusframework:nucleus.media-dash:3.0.0")
}

On the desktop and iOS the modules carry nothing: the platform plays the format natively. On the web, hls.js and dash.js are loaded on the first stream only.

player.openUri("https://example.com/live/master.m3u8")

Live streams

A playlist with no end is live: duration is 0 and isLive is true. When the stream keeps a seekable window (DVR), it is seekableStartMs..seekableEndMs; the slider spans it and a seek within it lands exactly, clamped to the live edge. Without a window, seeks are ignored.

if (player.isLive) {
    Text("LIVE")
    Slider(value = player.sliderPos, onValueChange = { player.seekTo(it) }, valueRange = 0f..1000f)
}

Tracks and subtitles

tracks lists the stream's audio languages, video variants and subtitle tracks (MediaTrack: type, label, language, size, bitrate, selected):

val french = player.tracks.first { it.type == TrackType.AUDIO && it.language == "fr" }
player.selectTrack(french)

val subtitles = player.tracks.first { it.type == TrackType.TEXT }
player.selectTrack(subtitles)           // cues appear in player.cues, drawn by the surface

player.clearTrackSelection(TrackType.VIDEO)   // back to adaptive
player.clearTrackSelection(TrackType.TEXT)    // subtitles off

The stream's default audio plays first, as the stream names it (DEFAULT=YES, the DASH main role), on every platform: Android and AVFoundation do not pick a track in the device's languages instead. Subtitles stay off until selected. A video variant is selected when it is the one on screen.

On macOS and iOS, choosing a variant caps AVPlayer's adaptation at it: it is the highest variant it may play, reached when AVPlayer adapts up by itself.

Request headers

Headers go with every request for the source: the playlist, the segments, the keys.

player.openUri(url, headers = mapOf("Authorization" to "Bearer $token"))

Each platform does it its own way: ExoPlayer's HTTP data source, hls.js and dash.js request hooks, a fetch for a plain file on the web, and on the desktop a loopback proxy in the JVM, since Media Foundation and AVFoundation take no headers (GStreamer goes through it too, the same on every desktop). A server answering 4xx without them reports a SourceError; an HTTP 4xx is never retried.

Timed metadata

Events carried by the stream arrive when playback reaches them, once, on the main thread:

val playback = player.playback
playback.onTimedMetadata = { event ->
    // event.schemeIdUri, event.value, event.id, event.data, event.presentationTimeMs
}

Two carriers are delivered: DASH EventStream events, and ID3 text frames of an HLS stream (timed ID3, ID3_SCHEME, with the frame id as value and a TXXX description as id).

Media Foundation exposes no stream event: on Windows onTimedMetadata is never called. On Linux, Nucleus reads the DASH events from the manifest and the timed ID3 from the stream itself, since GStreamer surfaces neither. HLS EXT-X-DATERANGE is not delivered.

What's next

  • Offline: download a stream and play it without network.
  • Platform support: what Media Foundation does differently with streams.
Was this page helpful?
Edit on GitHub

Last updated on