Search in sources :

Example 1 with TransferListener

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

the class CronetDataSourceTest method testOverread.

@Test
public void testOverread() throws HttpDataSourceException {
    testDataSpec = new DataSpec(Uri.parse(TEST_URL), 0, 16, null);
    testResponseHeader.put("Content-Length", Long.toString(16L));
    mockResponseStartSuccess();
    mockReadSuccess(0, 16);
    dataSourceUnderTest.open(testDataSpec);
    byte[] returnedBuffer = new byte[8];
    int bytesRead = dataSourceUnderTest.read(returnedBuffer, 0, 8);
    assertEquals(8, bytesRead);
    assertArrayEquals(buildTestDataArray(0, 8), returnedBuffer);
    // The current buffer is kept if not completely consumed by DataSource reader.
    returnedBuffer = new byte[8];
    bytesRead += dataSourceUnderTest.read(returnedBuffer, 0, 6);
    assertEquals(14, bytesRead);
    assertArrayEquals(suffixZeros(buildTestDataArray(8, 6), 8), returnedBuffer);
    // 2 bytes left at this point.
    returnedBuffer = new byte[8];
    bytesRead += dataSourceUnderTest.read(returnedBuffer, 0, 8);
    assertEquals(16, bytesRead);
    assertArrayEquals(suffixZeros(buildTestDataArray(14, 2), 8), returnedBuffer);
    // Should have only called read on cronet once.
    verify(mockUrlRequest, times(1)).read(any(ByteBuffer.class));
    verify(mockTransferListener, times(1)).onBytesTransferred(dataSourceUnderTest, 8);
    verify(mockTransferListener, times(1)).onBytesTransferred(dataSourceUnderTest, 6);
    verify(mockTransferListener, times(1)).onBytesTransferred(dataSourceUnderTest, 2);
    // Now we already returned the 16 bytes initially asked.
    // Try to read again even though all requested 16 bytes are already returned.
    // Return C.RESULT_END_OF_INPUT
    returnedBuffer = new byte[16];
    int bytesOverRead = dataSourceUnderTest.read(returnedBuffer, 0, 16);
    assertEquals(C.RESULT_END_OF_INPUT, bytesOverRead);
    assertArrayEquals(new byte[16], returnedBuffer);
    // C.RESULT_END_OF_INPUT should not be reported though the TransferListener.
    verify(mockTransferListener, never()).onBytesTransferred(dataSourceUnderTest, C.RESULT_END_OF_INPUT);
    // There should still be only one call to read on cronet.
    verify(mockUrlRequest, times(1)).read(any(ByteBuffer.class));
    // Check for connection not automatically closed.
    verify(mockUrlRequest, never()).cancel();
    assertEquals(16, bytesRead);
}
Also used : DataSpec(com.google.android.exoplayer2.upstream.DataSpec) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 2 with TransferListener

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

the class BaseMediaSource method prepareSource.

@Override
public final void prepareSource(MediaSourceCaller caller, @Nullable TransferListener mediaTransferListener, PlayerId playerId) {
    Looper looper = Looper.myLooper();
    Assertions.checkArgument(this.looper == null || this.looper == looper);
    this.playerId = playerId;
    @Nullable Timeline timeline = this.timeline;
    mediaSourceCallers.add(caller);
    if (this.looper == null) {
        this.looper = looper;
        enabledMediaSourceCallers.add(caller);
        prepareSourceInternal(mediaTransferListener);
    } else if (timeline != null) {
        enable(caller);
        caller.onSourceInfoRefreshed(/* source= */
        this, timeline);
    }
}
Also used : Looper(android.os.Looper) Timeline(com.google.android.exoplayer2.Timeline) Nullable(androidx.annotation.Nullable)

Example 3 with TransferListener

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

the class CronetDataSourceTest method overread.

@Test
public void overread() throws HttpDataSourceException {
    testDataSpec = new DataSpec(Uri.parse(TEST_URL), 0, 16);
    testResponseHeader.put("Content-Length", Long.toString(16L));
    mockResponseStartSuccess();
    mockReadSuccess(0, 16);
    dataSourceUnderTest.open(testDataSpec);
    byte[] returnedBuffer = new byte[8];
    int bytesRead = dataSourceUnderTest.read(returnedBuffer, 0, 8);
    assertThat(bytesRead).isEqualTo(8);
    assertThat(returnedBuffer).isEqualTo(buildTestDataArray(0, 8));
    // The current buffer is kept if not completely consumed by DataSource reader.
    returnedBuffer = new byte[8];
    bytesRead += dataSourceUnderTest.read(returnedBuffer, 0, 6);
    assertThat(bytesRead).isEqualTo(14);
    assertThat(returnedBuffer).isEqualTo(suffixZeros(buildTestDataArray(8, 6), 8));
    // 2 bytes left at this point.
    returnedBuffer = new byte[8];
    bytesRead += dataSourceUnderTest.read(returnedBuffer, 0, 8);
    assertThat(bytesRead).isEqualTo(16);
    assertThat(returnedBuffer).isEqualTo(suffixZeros(buildTestDataArray(14, 2), 8));
    // Should have only called read on cronet once.
    verify(mockUrlRequest, times(1)).read(any(ByteBuffer.class));
    verify(mockTransferListener, times(1)).onBytesTransferred(dataSourceUnderTest, testDataSpec, /* isNetwork= */
    true, 8);
    verify(mockTransferListener, times(1)).onBytesTransferred(dataSourceUnderTest, testDataSpec, /* isNetwork= */
    true, 6);
    verify(mockTransferListener, times(1)).onBytesTransferred(dataSourceUnderTest, testDataSpec, /* isNetwork= */
    true, 2);
    // Now we already returned the 16 bytes initially asked.
    // Try to read again even though all requested 16 bytes are already returned.
    // Return C.RESULT_END_OF_INPUT
    returnedBuffer = new byte[16];
    int bytesOverRead = dataSourceUnderTest.read(returnedBuffer, 0, 16);
    assertThat(bytesOverRead).isEqualTo(C.RESULT_END_OF_INPUT);
    assertThat(returnedBuffer).isEqualTo(new byte[16]);
    // C.RESULT_END_OF_INPUT should not be reported though the TransferListener.
    verify(mockTransferListener, never()).onBytesTransferred(dataSourceUnderTest, testDataSpec, /* isNetwork= */
    true, C.RESULT_END_OF_INPUT);
    // There should still be only one call to read on cronet.
    verify(mockUrlRequest, times(1)).read(any(ByteBuffer.class));
    // Check for connection not automatically closed.
    verify(mockUrlRequest, never()).cancel();
    assertThat(bytesRead).isEqualTo(16);
}
Also used : DataSpec(com.google.android.exoplayer2.upstream.DataSpec) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 4 with TransferListener

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

the class ImaServerSideAdInsertionMediaSource method prepareSourceInternal.

@Override
public void prepareSourceInternal(@Nullable TransferListener mediaTransferListener) {
    mainHandler.post(() -> assertSingleInstanceInPlaylist(checkNotNull(player)));
    super.prepareSourceInternal(mediaTransferListener);
    if (loader == null) {
        Loader loader = new Loader("ImaServerSideAdInsertionMediaSource");
        player.addListener(componentListener);
        StreamManagerLoadable streamManagerLoadable = new StreamManagerLoadable(sdkAdsLoader, streamRequest, streamPlayer, applicationAdErrorListener, loadVideoTimeoutMs);
        loader.startLoading(streamManagerLoadable, new StreamManagerLoadableCallback(), /* defaultMinRetryCount= */
        0);
        this.loader = loader;
    }
}
Also used : Loader(com.google.android.exoplayer2.upstream.Loader)

Example 5 with TransferListener

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

the class DashMediaPeriod method buildSampleStream.

private ChunkSampleStream<DashChunkSource> buildSampleStream(TrackGroupInfo trackGroupInfo, ExoTrackSelection selection, long positionUs) {
    int embeddedTrackCount = 0;
    boolean enableEventMessageTrack = trackGroupInfo.embeddedEventMessageTrackGroupIndex != C.INDEX_UNSET;
    TrackGroup embeddedEventMessageTrackGroup = null;
    if (enableEventMessageTrack) {
        embeddedEventMessageTrackGroup = trackGroups.get(trackGroupInfo.embeddedEventMessageTrackGroupIndex);
        embeddedTrackCount++;
    }
    boolean enableClosedCaptionTrack = trackGroupInfo.embeddedClosedCaptionTrackGroupIndex != C.INDEX_UNSET;
    TrackGroup embeddedClosedCaptionTrackGroup = null;
    if (enableClosedCaptionTrack) {
        embeddedClosedCaptionTrackGroup = trackGroups.get(trackGroupInfo.embeddedClosedCaptionTrackGroupIndex);
        embeddedTrackCount += embeddedClosedCaptionTrackGroup.length;
    }
    Format[] embeddedTrackFormats = new Format[embeddedTrackCount];
    int[] embeddedTrackTypes = new int[embeddedTrackCount];
    embeddedTrackCount = 0;
    if (enableEventMessageTrack) {
        embeddedTrackFormats[embeddedTrackCount] = embeddedEventMessageTrackGroup.getFormat(0);
        embeddedTrackTypes[embeddedTrackCount] = C.TRACK_TYPE_METADATA;
        embeddedTrackCount++;
    }
    List<Format> embeddedClosedCaptionTrackFormats = new ArrayList<>();
    if (enableClosedCaptionTrack) {
        for (int i = 0; i < embeddedClosedCaptionTrackGroup.length; i++) {
            embeddedTrackFormats[embeddedTrackCount] = embeddedClosedCaptionTrackGroup.getFormat(i);
            embeddedTrackTypes[embeddedTrackCount] = C.TRACK_TYPE_TEXT;
            embeddedClosedCaptionTrackFormats.add(embeddedTrackFormats[embeddedTrackCount]);
            embeddedTrackCount++;
        }
    }
    PlayerTrackEmsgHandler trackPlayerEmsgHandler = manifest.dynamic && enableEventMessageTrack ? playerEmsgHandler.newPlayerTrackEmsgHandler() : null;
    DashChunkSource chunkSource = chunkSourceFactory.createDashChunkSource(manifestLoaderErrorThrower, manifest, baseUrlExclusionList, periodIndex, trackGroupInfo.adaptationSetIndices, selection, trackGroupInfo.trackType, elapsedRealtimeOffsetMs, enableEventMessageTrack, embeddedClosedCaptionTrackFormats, trackPlayerEmsgHandler, transferListener, playerId);
    ChunkSampleStream<DashChunkSource> stream = new ChunkSampleStream<>(trackGroupInfo.trackType, embeddedTrackTypes, embeddedTrackFormats, chunkSource, this, allocator, positionUs, drmSessionManager, drmEventDispatcher, loadErrorHandlingPolicy, mediaSourceEventDispatcher);
    synchronized (this) {
        // The map is also accessed on the loading thread so synchronize access.
        trackEmsgHandlerBySampleStream.put(stream, trackPlayerEmsgHandler);
    }
    return stream;
}
Also used : Format(com.google.android.exoplayer2.Format) ChunkSampleStream(com.google.android.exoplayer2.source.chunk.ChunkSampleStream) TrackGroup(com.google.android.exoplayer2.source.TrackGroup) ArrayList(java.util.ArrayList) PlayerTrackEmsgHandler(com.google.android.exoplayer2.source.dash.PlayerEmsgHandler.PlayerTrackEmsgHandler)

Aggregations

Test (org.junit.Test)17 Nullable (androidx.annotation.Nullable)15 TransferListener (com.google.android.exoplayer2.upstream.TransferListener)15 FakeMediaSource (com.google.android.exoplayer2.testutil.FakeMediaSource)14 FakeTimeline (com.google.android.exoplayer2.testutil.FakeTimeline)14 MediaPeriodId (com.google.android.exoplayer2.source.MediaSource.MediaPeriodId)13 TrackGroupArray (com.google.android.exoplayer2.source.TrackGroupArray)13 FakeMediaPeriod (com.google.android.exoplayer2.testutil.FakeMediaPeriod)13 Allocator (com.google.android.exoplayer2.upstream.Allocator)13 DrmSessionManager (com.google.android.exoplayer2.drm.DrmSessionManager)12 TestExoPlayerBuilder (com.google.android.exoplayer2.testutil.TestExoPlayerBuilder)11 ExoPlayerTestRunner (com.google.android.exoplayer2.testutil.ExoPlayerTestRunner)8 ClippingMediaSource (com.google.android.exoplayer2.source.ClippingMediaSource)7 CompositeMediaSource (com.google.android.exoplayer2.source.CompositeMediaSource)7 ConcatenatingMediaSource (com.google.android.exoplayer2.source.ConcatenatingMediaSource)7 MaskingMediaSource (com.google.android.exoplayer2.source.MaskingMediaSource)7 MediaSource (com.google.android.exoplayer2.source.MediaSource)7 SinglePeriodTimeline (com.google.android.exoplayer2.source.SinglePeriodTimeline)7 ServerSideAdInsertionMediaSource (com.google.android.exoplayer2.source.ads.ServerSideAdInsertionMediaSource)7 ActionSchedule (com.google.android.exoplayer2.testutil.ActionSchedule)7