Search in sources :

Example 26 with Factory

use of com.google.android.exoplayer2.upstream.DataSource.Factory in project ExoPlayer by google.

the class HlsDownloaderTest method createWithDefaultDownloaderFactory.

@Test
public void createWithDefaultDownloaderFactory() {
    CacheDataSource.Factory cacheDataSourceFactory = new CacheDataSource.Factory().setCache(Mockito.mock(Cache.class)).setUpstreamDataSourceFactory(DummyDataSource.FACTORY);
    DownloaderFactory factory = new DefaultDownloaderFactory(cacheDataSourceFactory, /* executor= */
    Runnable::run);
    Downloader downloader = factory.createDownloader(new DownloadRequest.Builder(/* id= */
    "id", Uri.parse("https://www.test.com/download")).setMimeType(MimeTypes.APPLICATION_M3U8).setStreamKeys(Collections.singletonList(new StreamKey(/* groupIndex= */
    0, /* trackIndex= */
    0))).build());
    assertThat(downloader).isInstanceOf(HlsDownloader.class);
}
Also used : CacheDataSource(com.google.android.exoplayer2.upstream.cache.CacheDataSource) DefaultDownloaderFactory(com.google.android.exoplayer2.offline.DefaultDownloaderFactory) DefaultDownloaderFactory(com.google.android.exoplayer2.offline.DefaultDownloaderFactory) DownloaderFactory(com.google.android.exoplayer2.offline.DownloaderFactory) Downloader(com.google.android.exoplayer2.offline.Downloader) DefaultDownloaderFactory(com.google.android.exoplayer2.offline.DefaultDownloaderFactory) DownloaderFactory(com.google.android.exoplayer2.offline.DownloaderFactory) StreamKey(com.google.android.exoplayer2.offline.StreamKey) Test(org.junit.Test)

Example 27 with Factory

use of com.google.android.exoplayer2.upstream.DataSource.Factory in project ExoPlayer by google.

the class MediaPeriodAsserts method assertGetStreamKeysAndManifestFilterIntegration.

/**
 * Asserts that the values returns by {@link MediaPeriod#getStreamKeys(List)} are compatible with
 * a {@link FilterableManifest} using these stream keys.
 *
 * @param mediaPeriodFactory A factory to create a {@link MediaPeriod} based on a manifest.
 * @param manifest The manifest which is to be tested.
 * @param periodIndex The index of period in the manifest.
 * @param ignoredMimeType Optional mime type whose existence in the filtered track groups is not
 *     asserted.
 */
public static <T extends FilterableManifest<T>> void assertGetStreamKeysAndManifestFilterIntegration(FilterableManifestMediaPeriodFactory<T> mediaPeriodFactory, T manifest, int periodIndex, @Nullable String ignoredMimeType) {
    MediaPeriod mediaPeriod = mediaPeriodFactory.createMediaPeriod(manifest, periodIndex);
    TrackGroupArray trackGroupArray = prepareAndGetTrackGroups(mediaPeriod);
    // Create test vector of query test selections:
    // - One selection with one track per group, two tracks or all tracks.
    // - Two selections with tracks from multiple groups, or tracks from a single group.
    // - Multiple selections with tracks from all groups.
    List<List<ExoTrackSelection>> testSelections = new ArrayList<>();
    for (int i = 0; i < trackGroupArray.length; i++) {
        TrackGroup trackGroup = trackGroupArray.get(i);
        for (int j = 0; j < trackGroup.length; j++) {
            testSelections.add(Collections.singletonList(new TestTrackSelection(trackGroup, j)));
        }
        if (trackGroup.length > 1) {
            testSelections.add(Collections.singletonList(new TestTrackSelection(trackGroup, 0, 1)));
            testSelections.add(Arrays.asList(new ExoTrackSelection[] { new TestTrackSelection(trackGroup, 0), new TestTrackSelection(trackGroup, 1) }));
        }
        if (trackGroup.length > 2) {
            int[] allTracks = new int[trackGroup.length];
            for (int j = 0; j < trackGroup.length; j++) {
                allTracks[j] = j;
            }
            testSelections.add(Collections.singletonList(new TestTrackSelection(trackGroup, allTracks)));
        }
    }
    if (trackGroupArray.length > 1) {
        for (int i = 0; i < trackGroupArray.length - 1; i++) {
            for (int j = i + 1; j < trackGroupArray.length; j++) {
                testSelections.add(Arrays.asList(new ExoTrackSelection[] { new TestTrackSelection(trackGroupArray.get(i), 0), new TestTrackSelection(trackGroupArray.get(j), 0) }));
            }
        }
    }
    if (trackGroupArray.length > 2) {
        List<ExoTrackSelection> selectionsFromAllGroups = new ArrayList<>();
        for (int i = 0; i < trackGroupArray.length; i++) {
            selectionsFromAllGroups.add(new TestTrackSelection(trackGroupArray.get(i), 0));
        }
        testSelections.add(selectionsFromAllGroups);
    }
    // contain at least all requested formats.
    for (List<ExoTrackSelection> testSelection : testSelections) {
        List<StreamKey> streamKeys = mediaPeriod.getStreamKeys(testSelection);
        if (streamKeys.isEmpty()) {
            // Manifests won't be filtered if stream key is empty.
            continue;
        }
        T filteredManifest = manifest.copy(streamKeys);
        // The filtered manifest should only have one period left.
        MediaPeriod filteredMediaPeriod = mediaPeriodFactory.createMediaPeriod(filteredManifest, /* periodIndex= */
        0);
        TrackGroupArray filteredTrackGroupArray = prepareAndGetTrackGroups(filteredMediaPeriod);
        for (ExoTrackSelection trackSelection : testSelection) {
            if (ignoredMimeType != null && ignoredMimeType.equals(trackSelection.getFormat(0).sampleMimeType)) {
                continue;
            }
            Format[] expectedFormats = new Format[trackSelection.length()];
            for (int k = 0; k < trackSelection.length(); k++) {
                expectedFormats[k] = trackSelection.getFormat(k);
            }
            assertOneTrackGroupContainsFormats(filteredTrackGroupArray, expectedFormats);
        }
    }
}
Also used : TrackGroupArray(com.google.android.exoplayer2.source.TrackGroupArray) ArrayList(java.util.ArrayList) Format(com.google.android.exoplayer2.Format) ExoTrackSelection(com.google.android.exoplayer2.trackselection.ExoTrackSelection) TrackGroup(com.google.android.exoplayer2.source.TrackGroup) ArrayList(java.util.ArrayList) List(java.util.List) MediaPeriod(com.google.android.exoplayer2.source.MediaPeriod) StreamKey(com.google.android.exoplayer2.offline.StreamKey)

Example 28 with Factory

use of com.google.android.exoplayer2.upstream.DataSource.Factory in project ExoPlayer by google.

the class TrackSelectionHelper method setOverride.

private void setOverride(int group, int[] tracks, boolean enableRandomAdaptation) {
    TrackSelection.Factory factory = tracks.length == 1 ? FIXED_FACTORY : (enableRandomAdaptation ? RANDOM_FACTORY : adaptiveTrackSelectionFactory);
    override = new SelectionOverride(factory, group, tracks);
}
Also used : SelectionOverride(com.google.android.exoplayer2.trackselection.MappingTrackSelector.SelectionOverride) TrackSelection(com.google.android.exoplayer2.trackselection.TrackSelection) FixedTrackSelection(com.google.android.exoplayer2.trackselection.FixedTrackSelection) RandomTrackSelection(com.google.android.exoplayer2.trackselection.RandomTrackSelection)

Example 29 with Factory

use of com.google.android.exoplayer2.upstream.DataSource.Factory in project ExoPlayer by google.

the class TsExtractorTest method testCustomPesReader.

public void testCustomPesReader() throws Exception {
    CustomTsPayloadReaderFactory factory = new CustomTsPayloadReaderFactory(true, false);
    TsExtractor tsExtractor = new TsExtractor(TsExtractor.MODE_NORMAL, new TimestampAdjuster(0), factory);
    FakeExtractorInput input = new FakeExtractorInput.Builder().setData(TestUtil.getByteArray(getInstrumentation(), "ts/sample.ts")).setSimulateIOErrors(false).setSimulateUnknownLength(false).setSimulatePartialReads(false).build();
    FakeExtractorOutput output = new FakeExtractorOutput();
    tsExtractor.init(output);
    PositionHolder seekPositionHolder = new PositionHolder();
    int readResult = Extractor.RESULT_CONTINUE;
    while (readResult != Extractor.RESULT_END_OF_INPUT) {
        readResult = tsExtractor.read(input, seekPositionHolder);
    }
    CustomEsReader reader = factory.esReader;
    assertEquals(2, reader.packetsRead);
    TrackOutput trackOutput = reader.getTrackOutput();
    assertTrue(trackOutput == output.trackOutputs.get(257));
    assertEquals(Format.createTextSampleFormat("1/257", "mime", null, 0, 0, "und", null, 0), ((FakeTrackOutput) trackOutput).format);
}
Also used : FakeExtractorInput(com.google.android.exoplayer2.testutil.FakeExtractorInput) PositionHolder(com.google.android.exoplayer2.extractor.PositionHolder) TimestampAdjuster(com.google.android.exoplayer2.util.TimestampAdjuster) FakeExtractorOutput(com.google.android.exoplayer2.testutil.FakeExtractorOutput) TrackOutput(com.google.android.exoplayer2.extractor.TrackOutput) FakeTrackOutput(com.google.android.exoplayer2.testutil.FakeTrackOutput)

Example 30 with Factory

use of com.google.android.exoplayer2.upstream.DataSource.Factory in project react-native-track-player by react-native-kit.

the class ExoPlayback method load.

@Override
public void load(Track track, Promise callback) {
    loadCallback = callback;
    Uri url = track.url;
    String userAgent = Util.getUserAgent(context, "react-native-track-player");
    DefaultHttpDataSourceFactory httpDataSourceFactory = new DefaultHttpDataSourceFactory(userAgent, null, DefaultHttpDataSource.DEFAULT_CONNECT_TIMEOUT_MILLIS, DefaultHttpDataSource.DEFAULT_READ_TIMEOUT_MILLIS, true);
    DataSource.Factory factory = new DefaultDataSourceFactory(context, null, httpDataSourceFactory);
    MediaSource source;
    if (cacheMaxSize > 0 && !track.urlLocal) {
        File cacheDir = new File(context.getCacheDir(), "TrackPlayer");
        Cache cache = new SimpleCache(cacheDir, new LeastRecentlyUsedCacheEvictor(cacheMaxSize));
        factory = new CacheDataSourceFactory(cache, factory, 0, cacheMaxSize);
    }
    if (track.type == TrackType.DASH) {
        source = new DashMediaSource(url, factory, new DefaultDashChunkSource.Factory(factory), null, null);
    } else if (track.type == TrackType.HLS) {
        source = new HlsMediaSource(url, factory, null, null);
    } else if (track.type == TrackType.SMOOTH_STREAMING) {
        source = new SsMediaSource(url, factory, new DefaultSsChunkSource.Factory(factory), null, null);
    } else {
        source = new ExtractorMediaSource(url, factory, new DefaultExtractorsFactory(), null, null);
    }
    player.prepare(source);
}
Also used : DefaultExtractorsFactory(com.google.android.exoplayer2.extractor.DefaultExtractorsFactory) LeastRecentlyUsedCacheEvictor(com.google.android.exoplayer2.upstream.cache.LeastRecentlyUsedCacheEvictor) DashMediaSource(com.google.android.exoplayer2.source.dash.DashMediaSource) CacheDataSourceFactory(com.google.android.exoplayer2.upstream.cache.CacheDataSourceFactory) DefaultHttpDataSourceFactory(com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory) DefaultDataSourceFactory(com.google.android.exoplayer2.upstream.DefaultDataSourceFactory) DefaultExtractorsFactory(com.google.android.exoplayer2.extractor.DefaultExtractorsFactory) ExtractorMediaSource(com.google.android.exoplayer2.source.ExtractorMediaSource) SsMediaSource(com.google.android.exoplayer2.source.smoothstreaming.SsMediaSource) Uri(android.net.Uri) DataSource(com.google.android.exoplayer2.upstream.DataSource) DefaultHttpDataSource(com.google.android.exoplayer2.upstream.DefaultHttpDataSource) HlsMediaSource(com.google.android.exoplayer2.source.hls.HlsMediaSource) HlsMediaSource(com.google.android.exoplayer2.source.hls.HlsMediaSource) DashMediaSource(com.google.android.exoplayer2.source.dash.DashMediaSource) ExtractorMediaSource(com.google.android.exoplayer2.source.ExtractorMediaSource) MediaSource(com.google.android.exoplayer2.source.MediaSource) SsMediaSource(com.google.android.exoplayer2.source.smoothstreaming.SsMediaSource) SimpleCache(com.google.android.exoplayer2.upstream.cache.SimpleCache) CacheDataSourceFactory(com.google.android.exoplayer2.upstream.cache.CacheDataSourceFactory) DefaultHttpDataSourceFactory(com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory) DefaultDataSourceFactory(com.google.android.exoplayer2.upstream.DefaultDataSourceFactory) File(java.io.File) Cache(com.google.android.exoplayer2.upstream.cache.Cache) SimpleCache(com.google.android.exoplayer2.upstream.cache.SimpleCache)

Aggregations

Test (org.junit.Test)34 MediaItem (com.google.android.exoplayer2.MediaItem)23 Timeline (com.google.android.exoplayer2.Timeline)21 StreamKey (com.google.android.exoplayer2.offline.StreamKey)6 Format (com.google.android.exoplayer2.Format)5 MediaSource (com.google.android.exoplayer2.source.MediaSource)5 FakeExtractorInput (com.google.android.exoplayer2.testutil.FakeExtractorInput)5 ArrayList (java.util.ArrayList)5 PositionHolder (com.google.android.exoplayer2.extractor.PositionHolder)4 DefaultDownloaderFactory (com.google.android.exoplayer2.offline.DefaultDownloaderFactory)4 FakeExtractorOutput (com.google.android.exoplayer2.testutil.FakeExtractorOutput)4 DataSpec (com.google.android.exoplayer2.upstream.DataSpec)4 CacheDataSource (com.google.android.exoplayer2.upstream.cache.CacheDataSource)4 TimestampAdjuster (com.google.android.exoplayer2.util.TimestampAdjuster)4 List (java.util.List)4 Extractor (com.google.android.exoplayer2.extractor.Extractor)3 Downloader (com.google.android.exoplayer2.offline.Downloader)3 DownloaderFactory (com.google.android.exoplayer2.offline.DownloaderFactory)3 TrackGroup (com.google.android.exoplayer2.source.TrackGroup)3 FakeDataSet (com.google.android.exoplayer2.testutil.FakeDataSet)3