public final class

SsDownloader

extends SegmentDownloader<SsManifest>

 java.lang.Object

androidx.media3.exoplayer.offline.SegmentDownloader<SsManifest>

↳androidx.media3.exoplayer.smoothstreaming.offline.SsDownloader

Gradle dependencies

compile group: 'androidx.media3', name: 'media3-exoplayer-smoothstreaming', version: '1.0.0-alpha03'

  • groupId: androidx.media3
  • artifactId: media3-exoplayer-smoothstreaming
  • version: 1.0.0-alpha03

Artifact androidx.media3:media3-exoplayer-smoothstreaming:1.0.0-alpha03 it located at Google repository (https://maven.google.com/)

Overview

A downloader for SmoothStreaming streams.

Example usage:

 SimpleCache cache = new SimpleCache(downloadFolder, new NoOpCacheEvictor(), databaseProvider);
 CacheDataSource.Factory cacheDataSourceFactory =
     new CacheDataSource.Factory()
         .setCache(cache)
         .setUpstreamDataSourceFactory(new DefaultHttpDataSource.Factory());
 // Create a downloader for the first track of the first stream element.
 SsDownloader ssDownloader =
     new SsDownloader(
         new MediaItem.Builder()
             .setUri(manifestUri)
             .setStreamKeys(Collections.singletonList(new StreamKey(0, 0)))
             .build(),
         cacheDataSourceFactory);
 // Perform the download.
 ssDownloader.download(progressListener);
 // Use the downloaded data for playback.
 SsMediaSource mediaSource =
     new SsMediaSource.Factory(cacheDataSourceFactory).createMediaSource(mediaItem);
 

Summary

Constructors
publicSsDownloader(MediaItem mediaItem, CacheDataSource.Factory cacheDataSourceFactory)

Creates an instance.

publicSsDownloader(MediaItem mediaItem, CacheDataSource.Factory cacheDataSourceFactory, java.util.concurrent.Executor executor)

Creates an instance.

publicSsDownloader(MediaItem mediaItem, ParsingLoadable.Parser<SsManifest> manifestParser, CacheDataSource.Factory cacheDataSourceFactory, java.util.concurrent.Executor executor)

Creates a new instance.

Methods
protected abstract java.util.List<SegmentDownloader.Segment>getSegments(DataSource dataSource, FilterableManifest<T> manifest, boolean removing)

Returns a list of all downloadable SegmentDownloader.Segments for a given manifest.

from SegmentDownloader<M>cancel, download, execute, getCompressibleDataSpec, getManifest, remove
from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructors

public SsDownloader(MediaItem mediaItem, CacheDataSource.Factory cacheDataSourceFactory)

Creates an instance.

Parameters:

mediaItem: The MediaItem to be downloaded.
cacheDataSourceFactory: A for the cache into which the download will be written.

public SsDownloader(MediaItem mediaItem, CacheDataSource.Factory cacheDataSourceFactory, java.util.concurrent.Executor executor)

Creates an instance.

Parameters:

mediaItem: The MediaItem to be downloaded.
cacheDataSourceFactory: A for the cache into which the download will be written.
executor: An java.util.concurrent.Executor used to make requests for the media being downloaded. Providing an java.util.concurrent.Executor that uses multiple threads will speed up the download by allowing parts of it to be executed in parallel.

public SsDownloader(MediaItem mediaItem, ParsingLoadable.Parser<SsManifest> manifestParser, CacheDataSource.Factory cacheDataSourceFactory, java.util.concurrent.Executor executor)

Creates a new instance.

Parameters:

mediaItem: The MediaItem to be downloaded.
manifestParser: A parser for SmoothStreaming manifests.
cacheDataSourceFactory: A for the cache into which the download will be written.
executor: An java.util.concurrent.Executor used to make requests for the media being downloaded. Providing an java.util.concurrent.Executor that uses multiple threads will speed up the download by allowing parts of it to be executed in parallel.

Methods

protected abstract java.util.List<SegmentDownloader.Segment> getSegments(DataSource dataSource, FilterableManifest<T> manifest, boolean removing)

Returns a list of all downloadable SegmentDownloader.Segments for a given manifest. Any required data should be loaded using SegmentDownloader.getManifest(DataSource, DataSpec, boolean) or SegmentDownloader.execute(RunnableFutureTask, boolean).

Parameters:

dataSource: The DataSource through which to load any required data.
manifest: The manifest containing the segments.
removing: Whether the segments are being obtained as part of a removal. If true then a partial segment list is returned in the case that a load error prevents all segments from being listed. If false then an java.io.IOException will be thrown in this case.

Returns:

The list of downloadable SegmentDownloader.Segments.

Source

/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package androidx.media3.exoplayer.smoothstreaming.offline;

import static androidx.media3.common.util.Assertions.checkNotNull;

import androidx.media3.common.MediaItem;
import androidx.media3.common.util.UnstableApi;
import androidx.media3.common.util.Util;
import androidx.media3.datasource.DataSource;
import androidx.media3.datasource.DataSpec;
import androidx.media3.datasource.cache.CacheDataSource;
import androidx.media3.exoplayer.offline.SegmentDownloader;
import androidx.media3.exoplayer.smoothstreaming.manifest.SsManifest;
import androidx.media3.exoplayer.smoothstreaming.manifest.SsManifest.StreamElement;
import androidx.media3.exoplayer.smoothstreaming.manifest.SsManifestParser;
import androidx.media3.exoplayer.upstream.ParsingLoadable.Parser;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executor;

/**
 * A downloader for SmoothStreaming streams.
 *
 * <p>Example usage:
 *
 * <pre>{@code
 * SimpleCache cache = new SimpleCache(downloadFolder, new NoOpCacheEvictor(), databaseProvider);
 * CacheDataSource.Factory cacheDataSourceFactory =
 *     new CacheDataSource.Factory()
 *         .setCache(cache)
 *         .setUpstreamDataSourceFactory(new DefaultHttpDataSource.Factory());
 * // Create a downloader for the first track of the first stream element.
 * SsDownloader ssDownloader =
 *     new SsDownloader(
 *         new MediaItem.Builder()
 *             .setUri(manifestUri)
 *             .setStreamKeys(Collections.singletonList(new StreamKey(0, 0)))
 *             .build(),
 *         cacheDataSourceFactory);
 * // Perform the download.
 * ssDownloader.download(progressListener);
 * // Use the downloaded data for playback.
 * SsMediaSource mediaSource =
 *     new SsMediaSource.Factory(cacheDataSourceFactory).createMediaSource(mediaItem);
 * }</pre>
 */
@UnstableApi
public final class SsDownloader extends SegmentDownloader<SsManifest> {

  /**
   * Creates an instance.
   *
   * @param mediaItem The {@link MediaItem} to be downloaded.
   * @param cacheDataSourceFactory A {@link CacheDataSource.Factory} for the cache into which the
   *     download will be written.
   */
  public SsDownloader(MediaItem mediaItem, CacheDataSource.Factory cacheDataSourceFactory) {
    this(mediaItem, cacheDataSourceFactory, Runnable::run);
  }

  /**
   * Creates an instance.
   *
   * @param mediaItem The {@link MediaItem} to be downloaded.
   * @param cacheDataSourceFactory A {@link CacheDataSource.Factory} for the cache into which the
   *     download will be written.
   * @param executor An {@link Executor} used to make requests for the media being downloaded.
   *     Providing an {@link Executor} that uses multiple threads will speed up the download by
   *     allowing parts of it to be executed in parallel.
   */
  public SsDownloader(
      MediaItem mediaItem, CacheDataSource.Factory cacheDataSourceFactory, Executor executor) {
    this(
        mediaItem
            .buildUpon()
            .setUri(
                Util.fixSmoothStreamingIsmManifestUri(
                    checkNotNull(mediaItem.localConfiguration).uri))
            .build(),
        new SsManifestParser(),
        cacheDataSourceFactory,
        executor);
  }

  /**
   * Creates a new instance.
   *
   * @param mediaItem The {@link MediaItem} to be downloaded.
   * @param manifestParser A parser for SmoothStreaming manifests.
   * @param cacheDataSourceFactory A {@link CacheDataSource.Factory} for the cache into which the
   *     download will be written.
   * @param executor An {@link Executor} used to make requests for the media being downloaded.
   *     Providing an {@link Executor} that uses multiple threads will speed up the download by
   *     allowing parts of it to be executed in parallel.
   */
  public SsDownloader(
      MediaItem mediaItem,
      Parser<SsManifest> manifestParser,
      CacheDataSource.Factory cacheDataSourceFactory,
      Executor executor) {
    super(mediaItem, manifestParser, cacheDataSourceFactory, executor);
  }

  @Override
  protected List<Segment> getSegments(
      DataSource dataSource, SsManifest manifest, boolean allowIncompleteList) {
    ArrayList<Segment> segments = new ArrayList<>();
    for (StreamElement streamElement : manifest.streamElements) {
      for (int i = 0; i < streamElement.formats.length; i++) {
        for (int j = 0; j < streamElement.chunkCount; j++) {
          segments.add(
              new Segment(
                  streamElement.getStartTimeUs(j),
                  new DataSpec(streamElement.buildRequestUri(i, j))));
        }
      }
    }
    return segments;
  }
}