HLS

Hex.pm Version

HTTP Live Streaming (HLS) library implementing RFC 8216 specifications.

Installation

Add kim_hls to your dependencies in mix.exs:

def deps do
  [
    {:kim_hls, "~> 3.0"}
  ]
end

From v2.x.x to v3.x.x

This release is a major architectural update focused on a fully functional packager and stricter RFC 8216 compliance. Highlights:

Architecture

Core Component

Supporting Modules

Usage

Basic Packager Usage

# Initialize packager state
{:ok, state} = HLS.Packager.new(
  manifest_uri: URI.new!("stream.m3u8"),
  max_segments: 10  # Optional: enables sliding window
)

# Add a variant stream
{state, []} = HLS.Packager.add_track(state, "video_480p",
  stream: %HLS.VariantStream{
    bandwidth: 800_000,
    resolution: {854, 480},
    codecs: ["avc1.64001e"]
  },
  segment_extension: ".ts",
  target_segment_duration: 6.0
)

# Add segment (returns upload action)
{state, [action]} = HLS.Packager.put_segment(state, "video_480p", duration: 6.0, pts: 0)

# Caller uploads the segment via storage helper
storage = HLS.Storage.File.new(base_dir: "./output")
:ok = HLS.Storage.put(storage, action.uri, segment_data)

# Confirm upload (may return playlist write actions)
{state, actions} = HLS.Packager.confirm_upload(state, action.id)

# Execute write actions
Enum.each(actions, fn
  %HLS.Packager.Action.WritePlaylist{uri: uri, content: content} ->
    HLS.Storage.put(storage, uri, content)
end)

# Sync and flush to create VOD playlist
{state, actions} = HLS.Packager.flush(state)
Enum.each(actions, &execute_action(&1, storage))

Storage Helpers

# File-backed storage for local output
storage = HLS.Storage.File.new(base_dir: "./output")

# Req-backed storage for HTTP(S)
req = Req.new(base_url: "https://cdn.example.com")
storage = HLS.Storage.Req.new(req)

Configuration Options

Key options for HLS.Packager.new/1:

For resuming from existing playlists, use HLS.Packager.resume/1 with loaded playlist data. The caller must load the master and media playlists; resume trims tracks to the last common sync point and schedules a discontinuity at the next segment. If a referenced media playlist is missing or empty, the track is marked incomplete and put_segment/3 returns {:error, %HLS.Packager.Error{code: :resume_track_not_ready}, state} until the caller reconciles it via add_track/3 and put_init_section/2.

Packager Edge Cases (Production)

HLS.Packager enforces RFC compliance and returns errors or stall warnings instead of emitting non-compliant playlists:

Copyright and License

Copyright 2024, KIM Keep In Mind GmbH

Licensed under the Apache License, Version 2.0