Search in sources :

Example 31 with Factory

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

the class CronetDataSourceTest method factory_noFallbackFactoryCronetNotAvailable_delegateDefaultRequestPropertiesToInternalFallbackFactoryAfterCreation.

// Tests deprecated fallback functionality.
@SuppressWarnings("deprecation")
@Test
public void factory_noFallbackFactoryCronetNotAvailable_delegateDefaultRequestPropertiesToInternalFallbackFactoryAfterCreation() throws HttpDataSourceException, InterruptedException {
    MockWebServer mockWebServer = new MockWebServer();
    mockWebServer.enqueue(new MockResponse());
    CronetEngineWrapper cronetEngineWrapper = new CronetEngineWrapper((CronetEngine) null);
    Map<String, String> defaultRequestProperties = new HashMap<>();
    defaultRequestProperties.put("0", "defaultRequestProperty0");
    CronetDataSource.Factory factory = new CronetDataSource.Factory(cronetEngineWrapper, executorService);
    HttpDataSource dataSourceUnderTest = factory.setDefaultRequestProperties(defaultRequestProperties).createDataSource();
    defaultRequestProperties.clear();
    defaultRequestProperties.put("1", "defaultRequestPropertyAfterCreation");
    factory.setDefaultRequestProperties(defaultRequestProperties);
    dataSourceUnderTest.open(new DataSpec.Builder().setUri(mockWebServer.url("/test-path").toString()).build());
    Headers headers = mockWebServer.takeRequest(10, SECONDS).getHeaders();
    assertThat(headers.get("0")).isNull();
    assertThat(headers.get("1")).isEqualTo("defaultRequestPropertyAfterCreation");
    assertThat(dataSourceUnderTest).isInstanceOf(DefaultHttpDataSource.class);
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) HashMap(java.util.HashMap) Headers(okhttp3.Headers) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) MockWebServer(okhttp3.mockwebserver.MockWebServer) DataSpec(com.google.android.exoplayer2.upstream.DataSpec) DefaultHttpDataSource(com.google.android.exoplayer2.upstream.DefaultHttpDataSource) HttpDataSource(com.google.android.exoplayer2.upstream.HttpDataSource) Test(org.junit.Test)

Example 32 with Factory

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

the class ExtractorAsserts method assertAllBehaviors.

/**
 * Asserts that an extractor behaves correctly given valid input data:
 *
 * <ul>
 *   <li>Calls {@link Extractor#seek(long, long)} and {@link Extractor#release()} without calling
 *       {@link Extractor#init(ExtractorOutput)} to check these calls do not fail.
 *   <li>Calls {@link #assertOutput(Extractor, String, byte[], Context, boolean, boolean, boolean,
 *       boolean, boolean)} with all possible combinations of "simulate" parameters.
 * </ul>
 *
 * @param factory An {@link ExtractorFactory} which creates instances of the {@link Extractor}
 *     class which is to be tested.
 * @param file The path to the input sample.
 * @param dumpFilesPrefix The dump files prefix appended to the dump files path.
 * @throws IOException If reading from the input fails.
 */
public static void assertAllBehaviors(ExtractorFactory factory, String file, String dumpFilesPrefix) throws IOException {
    // Check behavior prior to initialization.
    Extractor extractor = factory.create();
    extractor.seek(0, 0);
    extractor.release();
    // Assert output.
    Context context = ApplicationProvider.getApplicationContext();
    byte[] fileData = TestUtil.getByteArray(context, file);
    assertOutput(factory.create(), dumpFilesPrefix, fileData, context, false, true, false, false, false);
    assertOutput(factory.create(), dumpFilesPrefix, fileData, context, false, true, false, false, true);
    assertOutput(factory.create(), dumpFilesPrefix, fileData, context, false, true, false, true, false);
    assertOutput(factory.create(), dumpFilesPrefix, fileData, context, false, true, false, true, true);
    assertOutput(factory.create(), dumpFilesPrefix, fileData, context, false, true, true, false, false);
    assertOutput(factory.create(), dumpFilesPrefix, fileData, context, false, true, true, false, true);
    assertOutput(factory.create(), dumpFilesPrefix, fileData, context, false, true, true, true, false);
    assertOutput(factory.create(), dumpFilesPrefix, fileData, context, false, true, true, true, true);
    assertOutput(factory.create(), dumpFilesPrefix, fileData, context, false, false, false, false, false);
}
Also used : Context(android.content.Context) Extractor(com.google.android.exoplayer2.extractor.Extractor)

Example 33 with Factory

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

the class ExtractorAsserts method assertBehavior.

/**
 * Asserts that an extractor consumes valid input data successfully successfully under the
 * conditions specified by {@code simulationConfig}.
 *
 * <p>The output of the extractor is compared against prerecorded dump files.
 *
 * @param assertionConfig Details of how to read and process the source and dump files.
 * @param simulationConfig Details on the environment to simulate and behaviours to assert.
 * @throws IOException If reading from the input fails.
 */
public static void assertBehavior(ExtractorFactory factory, String file, AssertionConfig assertionConfig, SimulationConfig simulationConfig) throws IOException {
    // Check behavior prior to initialization.
    Extractor extractor = factory.create();
    extractor.seek(0, 0);
    extractor.release();
    // Assert output.
    Context context = ApplicationProvider.getApplicationContext();
    byte[] fileData = TestUtil.getByteArray(context, file);
    String dumpFilesPrefix;
    if (assertionConfig.dumpFilesPrefix != null) {
        dumpFilesPrefix = assertionConfig.dumpFilesPrefix;
    } else {
        String[] path = file.split("/");
        checkState(path.length > 0 && path[0].equals("media"), "AssertionConfig.dumpFilesPrefix == null but file isn't in a media/ sub-directory.\n" + "Expected : 'media/<path-to-file>'\n" + "Found    : '" + file + "'\n" + "You need to set AssertionConfig.dumpFilesPrefix explicitly if your media and dump" + " file aren't located in the expected structure (see docs on" + " AssertionConfig.dumpFilesPrefix)");
        path[0] = "extractordumps";
        dumpFilesPrefix = Joiner.on('/').join(path);
    }
    assertOutput(factory.create(), dumpFilesPrefix, fileData, context, assertionConfig.deduplicateConsecutiveFormats, simulationConfig.sniffFirst, simulationConfig.simulateIOErrors, simulationConfig.simulateUnknownLength, simulationConfig.simulatePartialReads);
}
Also used : Context(android.content.Context) Extractor(com.google.android.exoplayer2.extractor.Extractor)

Example 34 with Factory

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

the class TsExtractorTest method customPesReader.

@Test
public void customPesReader() throws Exception {
    CustomTsPayloadReaderFactory factory = new CustomTsPayloadReaderFactory(true, false);
    TsExtractor tsExtractor = new TsExtractor(TsExtractor.MODE_MULTI_PMT, new TimestampAdjuster(0), factory);
    FakeExtractorInput input = new FakeExtractorInput.Builder().setData(TestUtil.getByteArray(ApplicationProvider.getApplicationContext(), "media/ts/sample_h262_mpeg_audio.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);
        if (readResult == Extractor.RESULT_SEEK) {
            input.setPosition((int) seekPositionHolder.position);
        }
    }
    CustomEsReader reader = factory.esReader;
    assertThat(reader.packetsRead).isEqualTo(2);
    TrackOutput trackOutput = reader.getTrackOutput();
    assertThat(trackOutput == output.trackOutputs.get(257)).isTrue();
    assertThat(((FakeTrackOutput) trackOutput).lastFormat).isEqualTo(new Format.Builder().setId("1/257").setSampleMimeType("mime").setLanguage("und").build());
}
Also used : FakeExtractorInput(com.google.android.exoplayer2.testutil.FakeExtractorInput) FakeTrackOutput(com.google.android.exoplayer2.testutil.FakeTrackOutput) TimestampAdjuster(com.google.android.exoplayer2.util.TimestampAdjuster) Format(com.google.android.exoplayer2.Format) PositionHolder(com.google.android.exoplayer2.extractor.PositionHolder) FakeExtractorOutput(com.google.android.exoplayer2.testutil.FakeExtractorOutput) TrackOutput(com.google.android.exoplayer2.extractor.TrackOutput) FakeTrackOutput(com.google.android.exoplayer2.testutil.FakeTrackOutput) Test(org.junit.Test)

Example 35 with Factory

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

the class DownloadManagerDashTest method createDownloadManager.

private void createDownloadManager() {
    runOnMainThread(() -> {
        Factory fakeDataSourceFactory = new FakeDataSource.Factory().setFakeDataSet(fakeDataSet);
        DefaultDownloaderFactory downloaderFactory = new DefaultDownloaderFactory(new CacheDataSource.Factory().setCache(cache).setUpstreamDataSourceFactory(fakeDataSourceFactory), /* executor= */
        Runnable::run);
        downloadManager = new DownloadManager(ApplicationProvider.getApplicationContext(), downloadIndex, downloaderFactory);
        downloadManager.setRequirements(new Requirements(0));
        downloadManagerListener = new TestDownloadManagerListener(downloadManager);
        downloadManager.resumeDownloads();
    });
}
Also used : FakeDataSource(com.google.android.exoplayer2.testutil.FakeDataSource) CacheDataSource(com.google.android.exoplayer2.upstream.cache.CacheDataSource) DefaultDownloaderFactory(com.google.android.exoplayer2.offline.DefaultDownloaderFactory) TestRunnable(com.google.android.exoplayer2.testutil.DummyMainThread.TestRunnable) DefaultDownloaderFactory(com.google.android.exoplayer2.offline.DefaultDownloaderFactory) Factory(com.google.android.exoplayer2.upstream.DataSource.Factory) DownloadManager(com.google.android.exoplayer2.offline.DownloadManager) Requirements(com.google.android.exoplayer2.scheduler.Requirements) TestDownloadManagerListener(com.google.android.exoplayer2.robolectric.TestDownloadManagerListener)

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