getPlayablePath static method

Future<HlsCacheResult> getPlayablePath(
  1. String hlsUrl, {
  2. int prefetchSegments = 3,
  3. int? targetBandwidth,
  4. Map<String, String>? headers,
})

Get a playable path for an HLS URL. Returns a local playlist path that can be played. Downloads segments progressively in background.

Implementation

static Future<HlsCacheResult> getPlayablePath(
  String hlsUrl, {
  int prefetchSegments = 3,
  int? targetBandwidth,
  Map<String, String>? headers,
}) async {
  // Check if we have a cached playlist
  final cachedPlaylist = await _getCachedPlaylist(hlsUrl);
  if (cachedPlaylist != null) {
    // Return cached playlist with local paths
    return HlsCacheResult(
      playlistPath: cachedPlaylist,
      isFullyCached: await _isFullyCached(hlsUrl),
    );
  }

  // Fetch and parse the playlist
  final playlist = await _fetchAndParsePlaylist(hlsUrl, headers);

  if (playlist is HlsMasterPlaylist) {
    // Get the appropriate variant
    final variant = targetBandwidth != null
        ? playlist.getVariantByBandwidth(targetBandwidth)
        : playlist.bestVariant;

    if (variant == null) {
      throw Exception('No variants found in master playlist');
    }

    // Fetch the media playlist
    final mediaPlaylist = await _fetchAndParsePlaylist(variant.url, headers);
    if (mediaPlaylist is! HlsMediaPlaylist) {
      throw Exception('Expected media playlist');
    }

    return _processMediaPlaylist(
      hlsUrl,
      mediaPlaylist,
      prefetchSegments,
      headers,
    );
  } else if (playlist is HlsMediaPlaylist) {
    return _processMediaPlaylist(
      hlsUrl,
      playlist,
      prefetchSegments,
      headers,
    );
  }

  throw Exception('Unknown playlist type');
}