Search in sources :

Example 1 with Download

use of com.google.android.exoplayer2.offline.Download in project ExoPlayer by google.

the class OfflineLicenseHelper method download.

/**
   * Downloads an offline license.
   *
   * @param dataSource The {@link HttpDataSource} to be used for download.
   * @param dashManifest The {@link DashManifest} of the DASH content.
   * @return The downloaded offline license key set id.
   * @throws IOException If an error occurs reading data from the stream.
   * @throws InterruptedException If the thread has been interrupted.
   * @throws DrmSessionException Thrown when there is an error during DRM session.
   */
public byte[] download(HttpDataSource dataSource, DashManifest dashManifest) throws IOException, InterruptedException, DrmSessionException {
    // as per DASH IF Interoperability Recommendations V3.0, 7.5.3.
    if (dashManifest.getPeriodCount() < 1) {
        return null;
    }
    Period period = dashManifest.getPeriod(0);
    int adaptationSetIndex = period.getAdaptationSetIndex(C.TRACK_TYPE_VIDEO);
    if (adaptationSetIndex == C.INDEX_UNSET) {
        adaptationSetIndex = period.getAdaptationSetIndex(C.TRACK_TYPE_AUDIO);
        if (adaptationSetIndex == C.INDEX_UNSET) {
            return null;
        }
    }
    AdaptationSet adaptationSet = period.adaptationSets.get(adaptationSetIndex);
    if (adaptationSet.representations.isEmpty()) {
        return null;
    }
    Representation representation = adaptationSet.representations.get(0);
    DrmInitData drmInitData = representation.format.drmInitData;
    if (drmInitData == null) {
        Format sampleFormat = DashUtil.loadSampleFormat(dataSource, representation);
        if (sampleFormat != null) {
            drmInitData = sampleFormat.drmInitData;
        }
        if (drmInitData == null) {
            return null;
        }
    }
    blockingKeyRequest(DefaultDrmSessionManager.MODE_DOWNLOAD, null, drmInitData);
    return drmSessionManager.getOfflineLicenseKeySetId();
}
Also used : Format(com.google.android.exoplayer2.Format) Period(com.google.android.exoplayer2.source.dash.manifest.Period) AdaptationSet(com.google.android.exoplayer2.source.dash.manifest.AdaptationSet) Representation(com.google.android.exoplayer2.source.dash.manifest.Representation)

Example 2 with Download

use of com.google.android.exoplayer2.offline.Download in project ExoPlayer by google.

the class DownloadHelper method addAudioLanguagesToSelection.

/**
 * Convenience method to add selections of tracks for all specified audio languages. If an audio
 * track in one of the specified languages is not available, the default fallback audio track is
 * used instead. Must not be called until after preparation completes.
 *
 * @param languages A list of audio languages for which tracks should be added to the download
 *     selection, as IETF BCP 47 conformant tags.
 */
public void addAudioLanguagesToSelection(String... languages) {
    assertPreparedWithMedia();
    for (int periodIndex = 0; periodIndex < mappedTrackInfos.length; periodIndex++) {
        DefaultTrackSelector.ParametersBuilder parametersBuilder = DEFAULT_TRACK_SELECTOR_PARAMETERS_WITHOUT_CONTEXT.buildUpon();
        MappedTrackInfo mappedTrackInfo = mappedTrackInfos[periodIndex];
        int rendererCount = mappedTrackInfo.getRendererCount();
        for (int rendererIndex = 0; rendererIndex < rendererCount; rendererIndex++) {
            if (mappedTrackInfo.getRendererType(rendererIndex) != C.TRACK_TYPE_AUDIO) {
                parametersBuilder.setRendererDisabled(rendererIndex, /* disabled= */
                true);
            }
        }
        for (String language : languages) {
            parametersBuilder.setPreferredAudioLanguage(language);
            addTrackSelection(periodIndex, parametersBuilder.build());
        }
    }
}
Also used : DefaultTrackSelector(com.google.android.exoplayer2.trackselection.DefaultTrackSelector) MappedTrackInfo(com.google.android.exoplayer2.trackselection.MappingTrackSelector.MappedTrackInfo)

Example 3 with Download

use of com.google.android.exoplayer2.offline.Download in project ExoPlayer by google.

the class DefaultDownloadIndex method putDownload.

@Override
public void putDownload(Download download) throws DatabaseIOException {
    ensureInitialized();
    try {
        SQLiteDatabase writableDatabase = databaseProvider.getWritableDatabase();
        putDownloadInternal(download, writableDatabase);
    } catch (SQLiteException e) {
        throw new DatabaseIOException(e);
    }
}
Also used : SQLiteDatabase(android.database.sqlite.SQLiteDatabase) SQLiteException(android.database.sqlite.SQLiteException) DatabaseIOException(com.google.android.exoplayer2.database.DatabaseIOException)

Example 4 with Download

use of com.google.android.exoplayer2.offline.Download in project ExoPlayer by google.

the class DownloadTracker method loadDownloads.

private void loadDownloads() {
    try (DownloadCursor loadedDownloads = downloadIndex.getDownloads()) {
        while (loadedDownloads.moveToNext()) {
            Download download = loadedDownloads.getDownload();
            downloads.put(download.request.uri, download);
        }
    } catch (IOException e) {
        Log.w(TAG, "Failed to query downloads", e);
    }
}
Also used : DownloadCursor(com.google.android.exoplayer2.offline.DownloadCursor) IOException(java.io.IOException) Download(com.google.android.exoplayer2.offline.Download)

Example 5 with Download

use of com.google.android.exoplayer2.offline.Download in project ExoPlayer by google.

the class DashDownloadTest method download.

// Download tests
@Test
public void download() throws Exception {
    DashDownloader dashDownloader = downloadContent();
    dashDownloader.download(/* progressListener= */
    null);
    testRunner.setStreamName("test_h264_fixed_download").setDataSourceFactory(offlineDataSourceFactory).run();
    dashDownloader.remove();
    assertWithMessage("There should be no cache key left").that(cache.getKeys()).isEmpty();
    assertWithMessage("There should be no content left").that(cache.getCacheSpace()).isEqualTo(0);
}
Also used : DashDownloader(com.google.android.exoplayer2.source.dash.offline.DashDownloader) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)22 DownloadBuilder (com.google.android.exoplayer2.testutil.DownloadBuilder)17 CacheDataSource (com.google.android.exoplayer2.upstream.cache.CacheDataSource)5 DefaultDownloaderFactory (com.google.android.exoplayer2.offline.DefaultDownloaderFactory)4 StreamKey (com.google.android.exoplayer2.offline.StreamKey)4 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)3 Download (com.google.android.exoplayer2.offline.Download)3 Downloader (com.google.android.exoplayer2.offline.Downloader)3 DownloaderFactory (com.google.android.exoplayer2.offline.DownloaderFactory)3 Nullable (androidx.annotation.Nullable)2 DatabaseIOException (com.google.android.exoplayer2.database.DatabaseIOException)2 DefaultTrackSelector (com.google.android.exoplayer2.trackselection.DefaultTrackSelector)2 MappedTrackInfo (com.google.android.exoplayer2.trackselection.MappingTrackSelector.MappedTrackInfo)2 IOException (java.io.IOException)2 Notification (android.app.Notification)1 Context (android.content.Context)1 SQLException (android.database.SQLException)1 SQLiteException (android.database.sqlite.SQLiteException)1 Format (com.google.android.exoplayer2.Format)1 StandaloneDatabaseProvider (com.google.android.exoplayer2.database.StandaloneDatabaseProvider)1