# Streaming

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

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.

## Add a format

| Format | Module | Android | Web | Windows | macOS / iOS | Linux |
| ------ | ------ | ------- | --- | ------- | ----------- | ----- |
| HLS (`.m3u8`) | `media-hls` | `media3-exoplayer-hls` | hls.js over MSE | Media Foundation | AVFoundation | GStreamer |
| MPEG-DASH (`.mpd`) | `media-dash` | `media3-exoplayer-dash` | dash.js | Media Foundation | — | GStreamer |
| Smooth Streaming (`.ism/Manifest`) | `media-smoothstreaming` | `media3-exoplayer-smoothstreaming` | dash.js + its MSS plugin | — | — | GStreamer (no seeks) |

```kotlin title="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.

```kotlin
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.

```kotlin
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`):

```kotlin
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.

```kotlin
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:

```kotlin
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](/docs/media/offline): download a stream and play it without network.
- [Platform support](/docs/media/platform-support): what Media Foundation does differently with streams.
