Search in sources :

Example 1 with Allocation

use of androidx.media3.exoplayer.upstream.Allocation in project media by androidx.

the class SampleDataQueue method readData.

/**
 * Reads data from {@code allocationNode} and its following nodes.
 *
 * @param allocationNode The first {@link AllocationNode} containing data yet to be read.
 * @param absolutePosition The absolute position from which data should be read.
 * @param target The array into which data should be written.
 * @param length The number of bytes to read.
 * @return The first {@link AllocationNode} that contains unread bytes after this method returns.
 */
private static AllocationNode readData(AllocationNode allocationNode, long absolutePosition, byte[] target, int length) {
    allocationNode = getNodeContainingPosition(allocationNode, absolutePosition);
    int remaining = length;
    while (remaining > 0) {
        int toCopy = min(remaining, (int) (allocationNode.endPosition - absolutePosition));
        Allocation allocation = allocationNode.allocation;
        System.arraycopy(allocation.data, allocationNode.translateOffset(absolutePosition), target, length - remaining, toCopy);
        remaining -= toCopy;
        absolutePosition += toCopy;
        if (absolutePosition == allocationNode.endPosition) {
            allocationNode = allocationNode.next;
        }
    }
    return allocationNode;
}
Also used : Allocation(androidx.media3.exoplayer.upstream.Allocation)

Example 2 with Allocation

use of androidx.media3.exoplayer.upstream.Allocation in project media by androidx.

the class ExoPlayerTest method infiniteLoading_withSmallAllocations_oomIsPreventedByLoadControl_andThrowsStuckBufferingIllegalStateException.

@Test
public void infiniteLoading_withSmallAllocations_oomIsPreventedByLoadControl_andThrowsStuckBufferingIllegalStateException() {
    DefaultLoadControl loadControl = new DefaultLoadControl.Builder().setTargetBufferBytes(10 * C.DEFAULT_BUFFER_SEGMENT_SIZE).build();
    // Return no end of stream signal to prevent playback from ending.
    FakeMediaPeriod.TrackDataFactory trackDataWithoutEos = (format, periodId) -> ImmutableList.of();
    MediaSource continuouslyAllocatingMediaSource = new FakeMediaSource(new FakeTimeline(), ExoPlayerTestRunner.VIDEO_FORMAT) {

        @Override
        protected MediaPeriod createMediaPeriod(MediaPeriodId id, TrackGroupArray trackGroupArray, Allocator allocator, MediaSourceEventListener.EventDispatcher mediaSourceEventDispatcher, DrmSessionManager drmSessionManager, DrmSessionEventListener.EventDispatcher drmEventDispatcher, @Nullable TransferListener transferListener) {
            return new FakeMediaPeriod(trackGroupArray, allocator, trackDataWithoutEos, mediaSourceEventDispatcher, drmSessionManager, drmEventDispatcher, /* deferOnPrepared= */
            false) {

                private final List<Allocation> allocations = new ArrayList<>();

                private Callback callback;

                @Override
                public synchronized void prepare(Callback callback, long positionUs) {
                    this.callback = callback;
                    super.prepare(callback, positionUs);
                }

                @Override
                public long getBufferedPositionUs() {
                    // Pretend not to make loading progress, so that continueLoading keeps being called.
                    return 0;
                }

                @Override
                public long getNextLoadPositionUs() {
                    // Pretend not to make loading progress, so that continueLoading keeps being called.
                    return 0;
                }

                @Override
                public boolean continueLoading(long positionUs) {
                    allocations.add(allocator.allocate());
                    callback.onContinueLoadingRequested(this);
                    return true;
                }
            };
        }
    };
    ExoPlayerTestRunner testRunner = new ExoPlayerTestRunner.Builder(context).setMediaSources(continuouslyAllocatingMediaSource).setLoadControl(loadControl).build();
    ExoPlaybackException exception = assertThrows(ExoPlaybackException.class, () -> testRunner.start().blockUntilEnded(TIMEOUT_MS));
    assertThat(exception.type).isEqualTo(ExoPlaybackException.TYPE_UNEXPECTED);
    assertThat(exception.getUnexpectedException()).isInstanceOf(IllegalStateException.class);
}
Also used : IllegalSeekPositionException(androidx.media3.common.IllegalSeekPositionException) COMMAND_ADJUST_DEVICE_VOLUME(androidx.media3.common.Player.COMMAND_ADJUST_DEVICE_VOLUME) Arrays(java.util.Arrays) ArgumentMatchers(org.mockito.ArgumentMatchers) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Uri(android.net.Uri) COMMAND_SEEK_TO_NEXT_MEDIA_ITEM(androidx.media3.common.Player.COMMAND_SEEK_TO_NEXT_MEDIA_ITEM) COMMAND_GET_CURRENT_MEDIA_ITEM(androidx.media3.common.Player.COMMAND_GET_CURRENT_MEDIA_ITEM) Allocator(androidx.media3.exoplayer.upstream.Allocator) COMMAND_GET_MEDIA_ITEMS_METADATA(androidx.media3.common.Player.COMMAND_GET_MEDIA_ITEMS_METADATA) CompositeMediaSource(androidx.media3.exoplayer.source.CompositeMediaSource) DefaultTrackSelector(androidx.media3.exoplayer.trackselection.DefaultTrackSelector) COMMAND_SEEK_TO_MEDIA_ITEM(androidx.media3.common.Player.COMMAND_SEEK_TO_MEDIA_ITEM) COMMAND_GET_TIMELINE(androidx.media3.common.Player.COMMAND_GET_TIMELINE) ServerSideAdInsertionUtil.addAdGroupToAdPlaybackState(androidx.media3.exoplayer.source.ads.ServerSideAdInsertionUtil.addAdGroupToAdPlaybackState) Looper(android.os.Looper) COMMAND_SET_REPEAT_MODE(androidx.media3.common.Player.COMMAND_SET_REPEAT_MODE) ClippingMediaSource(androidx.media3.exoplayer.source.ClippingMediaSource) Surface(android.view.Surface) Mockito.atLeastOnce(org.mockito.Mockito.atLeastOnce) Range(com.google.common.collect.Range) FakeSampleStream(androidx.media3.test.utils.FakeSampleStream) TextInformationFrame(androidx.media3.extractor.metadata.id3.TextInformationFrame) TestPlayerRunHelper(androidx.media3.test.utils.robolectric.TestPlayerRunHelper) CountDownLatch(java.util.concurrent.CountDownLatch) Nullable(androidx.annotation.Nullable) Allocation(androidx.media3.exoplayer.upstream.Allocation) ArgumentMatchers.anyFloat(org.mockito.ArgumentMatchers.anyFloat) Timeline(androidx.media3.common.Timeline) BinaryFrame(androidx.media3.extractor.metadata.id3.BinaryFrame) FakeRenderer(androidx.media3.test.utils.FakeRenderer) NoUidTimeline(androidx.media3.test.utils.NoUidTimeline) FakeClock(androidx.media3.test.utils.FakeClock) Mockito.mock(org.mockito.Mockito.mock) AnalyticsListener(androidx.media3.exoplayer.analytics.AnalyticsListener) ExoPlayerTestRunner(androidx.media3.test.utils.ExoPlayerTestRunner) Iterables(com.google.common.collect.Iterables) AdPlaybackState(androidx.media3.common.AdPlaybackState) Loader(androidx.media3.exoplayer.upstream.Loader) COMMAND_SEEK_IN_CURRENT_MEDIA_ITEM(androidx.media3.common.Player.COMMAND_SEEK_IN_CURRENT_MEDIA_ITEM) TestPlayerRunHelper.runUntilPositionDiscontinuity(androidx.media3.test.utils.robolectric.TestPlayerRunHelper.runUntilPositionDiscontinuity) RunWith(org.junit.runner.RunWith) Config(org.robolectric.annotation.Config) COMMAND_PLAY_PAUSE(androidx.media3.common.Player.COMMAND_PLAY_PAUSE) ArgumentMatchers.anyBoolean(org.mockito.ArgumentMatchers.anyBoolean) ArrayList(java.util.ArrayList) FakeTrackSelection(androidx.media3.test.utils.FakeTrackSelection) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) Assertions(androidx.media3.common.util.Assertions) DiscontinuityReason(androidx.media3.common.Player.DiscontinuityReason) COMMAND_SEEK_TO_DEFAULT_POSITION(androidx.media3.common.Player.COMMAND_SEEK_TO_DEFAULT_POSITION) Before(org.junit.Before) TransferListener(androidx.media3.datasource.TransferListener) MediaSourceEventListener(androidx.media3.exoplayer.source.MediaSourceEventListener) Player(androidx.media3.common.Player) ShadowLooper(org.robolectric.shadows.ShadowLooper) COMMAND_SET_DEVICE_VOLUME(androidx.media3.common.Player.COMMAND_SET_DEVICE_VOLUME) FakeMediaSource(androidx.media3.test.utils.FakeMediaSource) TestPlayerRunHelper.runUntilSleepingForOffload(androidx.media3.test.utils.robolectric.TestPlayerRunHelper.runUntilSleepingForOffload) Mockito.times(org.mockito.Mockito.times) IOException(java.io.IOException) Test(org.junit.Test) Listener(androidx.media3.common.Player.Listener) TestUtil.assertTimelinesSame(androidx.media3.test.utils.TestUtil.assertTimelinesSame) FakeShuffleOrder(androidx.media3.test.utils.FakeShuffleOrder) FakeMediaPeriod(androidx.media3.test.utils.FakeMediaPeriod) AtomicLong(java.util.concurrent.atomic.AtomicLong) Mockito.never(org.mockito.Mockito.never) COMMAND_GET_AUDIO_ATTRIBUTES(androidx.media3.common.Player.COMMAND_GET_AUDIO_ATTRIBUTES) FakeVideoRenderer(androidx.media3.test.utils.FakeVideoRenderer) COMMAND_GET_DEVICE_VOLUME(androidx.media3.common.Player.COMMAND_GET_DEVICE_VOLUME) MediaPeriodId(androidx.media3.exoplayer.source.MediaSource.MediaPeriodId) Mockito.reset(org.mockito.Mockito.reset) FakeMediaSourceFactory(androidx.media3.test.utils.FakeMediaSourceFactory) COMMAND_SET_VIDEO_SURFACE(androidx.media3.common.Player.COMMAND_SET_VIDEO_SURFACE) COMMAND_SEEK_TO_PREVIOUS(androidx.media3.common.Player.COMMAND_SEEK_TO_PREVIOUS) PlayerRunnable(androidx.media3.test.utils.ActionSchedule.PlayerRunnable) COMMAND_CHANGE_MEDIA_ITEMS(androidx.media3.common.Player.COMMAND_CHANGE_MEDIA_ITEMS) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) Action(androidx.media3.test.utils.Action) Random(java.util.Random) MaskingMediaSource(androidx.media3.exoplayer.source.MaskingMediaSource) COMMAND_STOP(androidx.media3.common.Player.COMMAND_STOP) AndroidJUnit4(androidx.test.ext.junit.runners.AndroidJUnit4) AudioAttributes(androidx.media3.common.AudioAttributes) ApplicationProvider(androidx.test.core.app.ApplicationProvider) DrmSessionManager(androidx.media3.exoplayer.drm.DrmSessionManager) FakeTrackSelector(androidx.media3.test.utils.FakeTrackSelector) ArgumentMatcher(org.mockito.ArgumentMatcher) END_OF_STREAM_ITEM(androidx.media3.test.utils.FakeSampleStream.FakeSampleStreamItem.END_OF_STREAM_ITEM) COMMAND_SET_SPEED_AND_PITCH(androidx.media3.common.Player.COMMAND_SET_SPEED_AND_PITCH) FakeAdaptiveDataSet(androidx.media3.test.utils.FakeAdaptiveDataSet) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FakeMediaClockRenderer(androidx.media3.test.utils.FakeMediaClockRenderer) Mockito.atLeast(org.mockito.Mockito.atLeast) Assert.fail(org.junit.Assert.fail) FakeSampleStreamItem(androidx.media3.test.utils.FakeSampleStream.FakeSampleStreamItem) MediaItem(androidx.media3.common.MediaItem) TestPlayerRunHelper.runUntilTimelineChanged(androidx.media3.test.utils.robolectric.TestPlayerRunHelper.runUntilTimelineChanged) COMMAND_GET_TRACK_INFOS(androidx.media3.common.Player.COMMAND_GET_TRACK_INFOS) ServerSideAdInsertionMediaSource(androidx.media3.exoplayer.source.ads.ServerSideAdInsertionMediaSource) FakeChunkSource(androidx.media3.test.utils.FakeChunkSource) TestPlayerRunHelper.playUntilPosition(androidx.media3.test.utils.robolectric.TestPlayerRunHelper.playUntilPosition) PlaybackException(androidx.media3.common.PlaybackException) TrackGroupArray(androidx.media3.common.TrackGroupArray) Util(androidx.media3.common.util.Util) Collectors(java.util.stream.Collectors) Format(androidx.media3.common.Format) COMMAND_SET_TRACK_SELECTION_PARAMETERS(androidx.media3.common.Player.COMMAND_SET_TRACK_SELECTION_PARAMETERS) COMMAND_SEEK_TO_PREVIOUS_MEDIA_ITEM(androidx.media3.common.Player.COMMAND_SEEK_TO_PREVIOUS_MEDIA_ITEM) PlaybackParameters(androidx.media3.common.PlaybackParameters) List(java.util.List) FakeAdaptiveMediaSource(androidx.media3.test.utils.FakeAdaptiveMediaSource) TrackSelectionArray(androidx.media3.common.TrackSelectionArray) Mockito.inOrder(org.mockito.Mockito.inOrder) ConcatenatingMediaSource(androidx.media3.exoplayer.source.ConcatenatingMediaSource) AdditionalMatchers.not(org.mockito.AdditionalMatchers.not) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) TrackGroup(androidx.media3.common.TrackGroup) Context(android.content.Context) DEFAULT_WINDOW_DURATION_US(androidx.media3.test.utils.FakeTimeline.TimelineWindowDefinition.DEFAULT_WINDOW_DURATION_US) COMMAND_SET_SHUFFLE_MODE(androidx.media3.common.Player.COMMAND_SET_SHUFFLE_MODE) COMMAND_GET_TEXT(androidx.media3.common.Player.COMMAND_GET_TEXT) Assert.assertThrows(org.junit.Assert.assertThrows) MediaMetadata(androidx.media3.common.MediaMetadata) Metadata(androidx.media3.common.Metadata) TestPlayerRunHelper.runUntilPendingCommandsAreFullyHandled(androidx.media3.test.utils.robolectric.TestPlayerRunHelper.runUntilPendingCommandsAreFullyHandled) TracksInfo(androidx.media3.common.TracksInfo) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestPlayerRunHelper.playUntilStartOfMediaItem(androidx.media3.test.utils.robolectric.TestPlayerRunHelper.playUntilStartOfMediaItem) Intent(android.content.Intent) COMMAND_PREPARE(androidx.media3.common.Player.COMMAND_PREPARE) FakeTimeline(androidx.media3.test.utils.FakeTimeline) AtomicReference(java.util.concurrent.atomic.AtomicReference) Shadows.shadowOf(org.robolectric.Shadows.shadowOf) ActionSchedule(androidx.media3.test.utils.ActionSchedule) AudioManager(android.media.AudioManager) FakeDataSource(androidx.media3.test.utils.FakeDataSource) HashSet(java.util.HashSet) COMMAND_SEEK_FORWARD(androidx.media3.common.Player.COMMAND_SEEK_FORWARD) COMMAND_SET_VOLUME(androidx.media3.common.Player.COMMAND_SET_VOLUME) TimelineWindowDefinition(androidx.media3.test.utils.FakeTimeline.TimelineWindowDefinition) PlayerTarget(androidx.media3.test.utils.ActionSchedule.PlayerTarget) ArgumentCaptor(org.mockito.ArgumentCaptor) ShadowAudioManager(org.robolectric.shadows.ShadowAudioManager) SinglePeriodTimeline(androidx.media3.exoplayer.source.SinglePeriodTimeline) ImmutableList(com.google.common.collect.ImmutableList) DrmSessionEventListener(androidx.media3.exoplayer.drm.DrmSessionEventListener) Window(androidx.media3.common.Timeline.Window) TestExoPlayerBuilder(androidx.media3.test.utils.TestExoPlayerBuilder) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) RobolectricUtil.runMainLooperUntil(androidx.media3.test.utils.robolectric.RobolectricUtil.runMainLooperUntil) SurfaceTexture(android.graphics.SurfaceTexture) DEFAULT_WINDOW_OFFSET_IN_FIRST_PERIOD_US(androidx.media3.test.utils.FakeTimeline.TimelineWindowDefinition.DEFAULT_WINDOW_OFFSET_IN_FIRST_PERIOD_US) MediaSource(androidx.media3.exoplayer.source.MediaSource) InOrder(org.mockito.InOrder) COMMAND_SEEK_BACK(androidx.media3.common.Player.COMMAND_SEEK_BACK) MimeTypes(androidx.media3.common.MimeTypes) COMMAND_SEEK_TO_NEXT(androidx.media3.common.Player.COMMAND_SEEK_TO_NEXT) Clock(androidx.media3.common.util.Clock) Truth.assertThat(com.google.common.truth.Truth.assertThat) COMMAND_SET_MEDIA_ITEMS_METADATA(androidx.media3.common.Player.COMMAND_SET_MEDIA_ITEMS_METADATA) TestPlayerRunHelper.runUntilReceiveOffloadSchedulingEnabledNewState(androidx.media3.test.utils.robolectric.TestPlayerRunHelper.runUntilReceiveOffloadSchedulingEnabledNewState) Mockito.verify(org.mockito.Mockito.verify) COMMAND_GET_VOLUME(androidx.media3.common.Player.COMMAND_GET_VOLUME) C(androidx.media3.common.C) Mockito(org.mockito.Mockito) PositionInfo(androidx.media3.common.Player.PositionInfo) FakeSampleStreamItem.oneByteSample(androidx.media3.test.utils.FakeSampleStream.FakeSampleStreamItem.oneByteSample) STATE_ENDED(androidx.media3.common.Player.STATE_ENDED) MediaPeriod(androidx.media3.exoplayer.source.MediaPeriod) Collections(java.util.Collections) TestPlayerRunHelper.runUntilPlaybackState(androidx.media3.test.utils.robolectric.TestPlayerRunHelper.runUntilPlaybackState) TransferListener(androidx.media3.datasource.TransferListener) Allocator(androidx.media3.exoplayer.upstream.Allocator) FakeMediaSource(androidx.media3.test.utils.FakeMediaSource) TestExoPlayerBuilder(androidx.media3.test.utils.TestExoPlayerBuilder) DrmSessionManager(androidx.media3.exoplayer.drm.DrmSessionManager) TrackGroupArray(androidx.media3.common.TrackGroupArray) FakeMediaPeriod(androidx.media3.test.utils.FakeMediaPeriod) CompositeMediaSource(androidx.media3.exoplayer.source.CompositeMediaSource) ClippingMediaSource(androidx.media3.exoplayer.source.ClippingMediaSource) FakeMediaSource(androidx.media3.test.utils.FakeMediaSource) MaskingMediaSource(androidx.media3.exoplayer.source.MaskingMediaSource) ServerSideAdInsertionMediaSource(androidx.media3.exoplayer.source.ads.ServerSideAdInsertionMediaSource) FakeAdaptiveMediaSource(androidx.media3.test.utils.FakeAdaptiveMediaSource) ConcatenatingMediaSource(androidx.media3.exoplayer.source.ConcatenatingMediaSource) MediaSource(androidx.media3.exoplayer.source.MediaSource) FakeTimeline(androidx.media3.test.utils.FakeTimeline) MediaPeriodId(androidx.media3.exoplayer.source.MediaSource.MediaPeriodId) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) ExoPlayerTestRunner(androidx.media3.test.utils.ExoPlayerTestRunner) Nullable(androidx.annotation.Nullable) Test(org.junit.Test)

Example 3 with Allocation

use of androidx.media3.exoplayer.upstream.Allocation in project media by androidx.

the class SampleQueueTest method readSingleSamples.

@Test
public void readSingleSamples() {
    sampleQueue.sampleData(new ParsableByteArray(DATA), ALLOCATION_SIZE);
    assertAllocationCount(1);
    // Nothing to read.
    assertNoSamplesToRead(null);
    sampleQueue.format(FORMAT_1);
    // Read the format.
    assertReadFormat(false, FORMAT_1);
    // Nothing to read.
    assertNoSamplesToRead(FORMAT_1);
    sampleQueue.sampleMetadata(1000, C.BUFFER_FLAG_KEY_FRAME, ALLOCATION_SIZE, 0, null);
    // If formatRequired, should read the format rather than the sample.
    assertReadFormat(true, FORMAT_1);
    // Otherwise should read the sample.
    assertReadSample(1000, /* isKeyFrame= */
    true, /* isDecodeOnly= */
    false, /* isEncrypted= */
    false, DATA, /* offset= */
    0, ALLOCATION_SIZE);
    // Allocation should still be held.
    assertAllocationCount(1);
    sampleQueue.discardToRead();
    // The allocation should have been released.
    assertAllocationCount(0);
    // Nothing to read.
    assertNoSamplesToRead(FORMAT_1);
    // Write a second sample followed by one byte that does not belong to it.
    sampleQueue.sampleData(new ParsableByteArray(DATA), ALLOCATION_SIZE);
    sampleQueue.sampleMetadata(2000, 0, ALLOCATION_SIZE - 1, 1, null);
    // If formatRequired, should read the format rather than the sample.
    assertReadFormat(true, FORMAT_1);
    // Read the sample.
    assertReadSample(2000, /* isKeyFrame= */
    false, /* isDecodeOnly= */
    false, /* isEncrypted= */
    false, DATA, /* offset= */
    0, ALLOCATION_SIZE - 1);
    // Allocation should still be held.
    assertAllocationCount(1);
    sampleQueue.discardToRead();
    // The last byte written to the sample queue may belong to a sample whose metadata has yet to be
    // written, so an allocation should still be held.
    assertAllocationCount(1);
    // Write metadata for a third sample containing the remaining byte.
    sampleQueue.sampleMetadata(3000, 0, 1, 0, null);
    // If formatRequired, should read the format rather than the sample.
    assertReadFormat(true, FORMAT_1);
    // Read the sample.
    assertReadSample(3000, /* isKeyFrame= */
    false, /* isDecodeOnly= */
    false, /* isEncrypted= */
    false, DATA, ALLOCATION_SIZE - 1, 1);
    // Allocation should still be held.
    assertAllocationCount(1);
    sampleQueue.discardToRead();
    // The allocation should have been released.
    assertAllocationCount(0);
}
Also used : ParsableByteArray(androidx.media3.common.util.ParsableByteArray) Test(org.junit.Test)

Example 4 with Allocation

use of androidx.media3.exoplayer.upstream.Allocation in project media by androidx.

the class ExoPlayerTest method loading_withLargeAllocationCausingOom_playsRemainingMediaAndThenThrows.

@Test
public void loading_withLargeAllocationCausingOom_playsRemainingMediaAndThenThrows() {
    Loader.Loadable loadable = new Loader.Loadable() {

        @SuppressWarnings("UnusedVariable")
        @Override
        public void load() throws IOException {
            // This test needs the allocation to cause an OOM.
            @SuppressWarnings("unused") byte[] largeBuffer = new byte[Integer.MAX_VALUE];
        }

        @Override
        public void cancelLoad() {
        }
    };
    // Create 3 samples without end of stream signal to test that all 3 samples are
    // still played before the sample stream exception is thrown.
    FakeSampleStreamItem sample = oneByteSample(TimelineWindowDefinition.DEFAULT_WINDOW_OFFSET_IN_FIRST_PERIOD_US, C.BUFFER_FLAG_KEY_FRAME);
    FakeMediaPeriod.TrackDataFactory threeSamplesWithoutEos = (format, mediaPeriodId) -> ImmutableList.of(sample, sample, sample);
    MediaSource largeBufferAllocatingMediaSource = new FakeMediaSource(new FakeTimeline(), ExoPlayerTestRunner.VIDEO_FORMAT) {

        @Override
        protected MediaPeriod createMediaPeriod(MediaPeriodId id, TrackGroupArray trackGroupArray, Allocator allocator, MediaSourceEventListener.EventDispatcher mediaSourceEventDispatcher, DrmSessionManager drmSessionManager, DrmSessionEventListener.EventDispatcher drmEventDispatcher, @Nullable TransferListener transferListener) {
            return new FakeMediaPeriod(trackGroupArray, allocator, threeSamplesWithoutEos, mediaSourceEventDispatcher, drmSessionManager, drmEventDispatcher, /* deferOnPrepared= */
            false) {

                private final Loader loader = new Loader("ExoPlayerTest");

                @Override
                public boolean continueLoading(long positionUs) {
                    super.continueLoading(positionUs);
                    if (!loader.isLoading()) {
                        loader.startLoading(loadable, new FakeLoaderCallback(), /* defaultMinRetryCount= */
                        1);
                    }
                    return true;
                }

                @Override
                protected FakeSampleStream createSampleStream(Allocator allocator, @Nullable MediaSourceEventListener.EventDispatcher mediaSourceEventDispatcher, DrmSessionManager drmSessionManager, DrmSessionEventListener.EventDispatcher drmEventDispatcher, Format initialFormat, List<FakeSampleStreamItem> fakeSampleStreamItems) {
                    return new FakeSampleStream(allocator, mediaSourceEventDispatcher, drmSessionManager, drmEventDispatcher, initialFormat, fakeSampleStreamItems) {

                        @Override
                        public void maybeThrowError() throws IOException {
                            loader.maybeThrowError();
                        }
                    };
                }
            };
        }
    };
    FakeRenderer renderer = new FakeRenderer(C.TRACK_TYPE_VIDEO);
    ExoPlayerTestRunner testRunner = new ExoPlayerTestRunner.Builder(context).setMediaSources(largeBufferAllocatingMediaSource).setRenderers(renderer).build();
    ExoPlaybackException exception = assertThrows(ExoPlaybackException.class, () -> testRunner.start().blockUntilEnded(TIMEOUT_MS));
    assertThat(exception.type).isEqualTo(ExoPlaybackException.TYPE_SOURCE);
    assertThat(exception.getSourceException()).isInstanceOf(Loader.UnexpectedLoaderException.class);
    assertThat(exception.getSourceException().getCause()).isInstanceOf(OutOfMemoryError.class);
    assertThat(renderer.sampleBufferReadCount).isEqualTo(3);
}
Also used : IllegalSeekPositionException(androidx.media3.common.IllegalSeekPositionException) COMMAND_ADJUST_DEVICE_VOLUME(androidx.media3.common.Player.COMMAND_ADJUST_DEVICE_VOLUME) Arrays(java.util.Arrays) ArgumentMatchers(org.mockito.ArgumentMatchers) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Uri(android.net.Uri) COMMAND_SEEK_TO_NEXT_MEDIA_ITEM(androidx.media3.common.Player.COMMAND_SEEK_TO_NEXT_MEDIA_ITEM) COMMAND_GET_CURRENT_MEDIA_ITEM(androidx.media3.common.Player.COMMAND_GET_CURRENT_MEDIA_ITEM) Allocator(androidx.media3.exoplayer.upstream.Allocator) COMMAND_GET_MEDIA_ITEMS_METADATA(androidx.media3.common.Player.COMMAND_GET_MEDIA_ITEMS_METADATA) CompositeMediaSource(androidx.media3.exoplayer.source.CompositeMediaSource) DefaultTrackSelector(androidx.media3.exoplayer.trackselection.DefaultTrackSelector) COMMAND_SEEK_TO_MEDIA_ITEM(androidx.media3.common.Player.COMMAND_SEEK_TO_MEDIA_ITEM) COMMAND_GET_TIMELINE(androidx.media3.common.Player.COMMAND_GET_TIMELINE) ServerSideAdInsertionUtil.addAdGroupToAdPlaybackState(androidx.media3.exoplayer.source.ads.ServerSideAdInsertionUtil.addAdGroupToAdPlaybackState) Looper(android.os.Looper) COMMAND_SET_REPEAT_MODE(androidx.media3.common.Player.COMMAND_SET_REPEAT_MODE) ClippingMediaSource(androidx.media3.exoplayer.source.ClippingMediaSource) Surface(android.view.Surface) Mockito.atLeastOnce(org.mockito.Mockito.atLeastOnce) Range(com.google.common.collect.Range) FakeSampleStream(androidx.media3.test.utils.FakeSampleStream) TextInformationFrame(androidx.media3.extractor.metadata.id3.TextInformationFrame) TestPlayerRunHelper(androidx.media3.test.utils.robolectric.TestPlayerRunHelper) CountDownLatch(java.util.concurrent.CountDownLatch) Nullable(androidx.annotation.Nullable) Allocation(androidx.media3.exoplayer.upstream.Allocation) ArgumentMatchers.anyFloat(org.mockito.ArgumentMatchers.anyFloat) Timeline(androidx.media3.common.Timeline) BinaryFrame(androidx.media3.extractor.metadata.id3.BinaryFrame) FakeRenderer(androidx.media3.test.utils.FakeRenderer) NoUidTimeline(androidx.media3.test.utils.NoUidTimeline) FakeClock(androidx.media3.test.utils.FakeClock) Mockito.mock(org.mockito.Mockito.mock) AnalyticsListener(androidx.media3.exoplayer.analytics.AnalyticsListener) ExoPlayerTestRunner(androidx.media3.test.utils.ExoPlayerTestRunner) Iterables(com.google.common.collect.Iterables) AdPlaybackState(androidx.media3.common.AdPlaybackState) Loader(androidx.media3.exoplayer.upstream.Loader) COMMAND_SEEK_IN_CURRENT_MEDIA_ITEM(androidx.media3.common.Player.COMMAND_SEEK_IN_CURRENT_MEDIA_ITEM) TestPlayerRunHelper.runUntilPositionDiscontinuity(androidx.media3.test.utils.robolectric.TestPlayerRunHelper.runUntilPositionDiscontinuity) RunWith(org.junit.runner.RunWith) Config(org.robolectric.annotation.Config) COMMAND_PLAY_PAUSE(androidx.media3.common.Player.COMMAND_PLAY_PAUSE) ArgumentMatchers.anyBoolean(org.mockito.ArgumentMatchers.anyBoolean) ArrayList(java.util.ArrayList) FakeTrackSelection(androidx.media3.test.utils.FakeTrackSelection) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) Assertions(androidx.media3.common.util.Assertions) DiscontinuityReason(androidx.media3.common.Player.DiscontinuityReason) COMMAND_SEEK_TO_DEFAULT_POSITION(androidx.media3.common.Player.COMMAND_SEEK_TO_DEFAULT_POSITION) Before(org.junit.Before) TransferListener(androidx.media3.datasource.TransferListener) MediaSourceEventListener(androidx.media3.exoplayer.source.MediaSourceEventListener) Player(androidx.media3.common.Player) ShadowLooper(org.robolectric.shadows.ShadowLooper) COMMAND_SET_DEVICE_VOLUME(androidx.media3.common.Player.COMMAND_SET_DEVICE_VOLUME) FakeMediaSource(androidx.media3.test.utils.FakeMediaSource) TestPlayerRunHelper.runUntilSleepingForOffload(androidx.media3.test.utils.robolectric.TestPlayerRunHelper.runUntilSleepingForOffload) Mockito.times(org.mockito.Mockito.times) IOException(java.io.IOException) Test(org.junit.Test) Listener(androidx.media3.common.Player.Listener) TestUtil.assertTimelinesSame(androidx.media3.test.utils.TestUtil.assertTimelinesSame) FakeShuffleOrder(androidx.media3.test.utils.FakeShuffleOrder) FakeMediaPeriod(androidx.media3.test.utils.FakeMediaPeriod) AtomicLong(java.util.concurrent.atomic.AtomicLong) Mockito.never(org.mockito.Mockito.never) COMMAND_GET_AUDIO_ATTRIBUTES(androidx.media3.common.Player.COMMAND_GET_AUDIO_ATTRIBUTES) FakeVideoRenderer(androidx.media3.test.utils.FakeVideoRenderer) COMMAND_GET_DEVICE_VOLUME(androidx.media3.common.Player.COMMAND_GET_DEVICE_VOLUME) MediaPeriodId(androidx.media3.exoplayer.source.MediaSource.MediaPeriodId) Mockito.reset(org.mockito.Mockito.reset) FakeMediaSourceFactory(androidx.media3.test.utils.FakeMediaSourceFactory) COMMAND_SET_VIDEO_SURFACE(androidx.media3.common.Player.COMMAND_SET_VIDEO_SURFACE) COMMAND_SEEK_TO_PREVIOUS(androidx.media3.common.Player.COMMAND_SEEK_TO_PREVIOUS) PlayerRunnable(androidx.media3.test.utils.ActionSchedule.PlayerRunnable) COMMAND_CHANGE_MEDIA_ITEMS(androidx.media3.common.Player.COMMAND_CHANGE_MEDIA_ITEMS) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) Action(androidx.media3.test.utils.Action) Random(java.util.Random) MaskingMediaSource(androidx.media3.exoplayer.source.MaskingMediaSource) COMMAND_STOP(androidx.media3.common.Player.COMMAND_STOP) AndroidJUnit4(androidx.test.ext.junit.runners.AndroidJUnit4) AudioAttributes(androidx.media3.common.AudioAttributes) ApplicationProvider(androidx.test.core.app.ApplicationProvider) DrmSessionManager(androidx.media3.exoplayer.drm.DrmSessionManager) FakeTrackSelector(androidx.media3.test.utils.FakeTrackSelector) ArgumentMatcher(org.mockito.ArgumentMatcher) END_OF_STREAM_ITEM(androidx.media3.test.utils.FakeSampleStream.FakeSampleStreamItem.END_OF_STREAM_ITEM) COMMAND_SET_SPEED_AND_PITCH(androidx.media3.common.Player.COMMAND_SET_SPEED_AND_PITCH) FakeAdaptiveDataSet(androidx.media3.test.utils.FakeAdaptiveDataSet) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FakeMediaClockRenderer(androidx.media3.test.utils.FakeMediaClockRenderer) Mockito.atLeast(org.mockito.Mockito.atLeast) Assert.fail(org.junit.Assert.fail) FakeSampleStreamItem(androidx.media3.test.utils.FakeSampleStream.FakeSampleStreamItem) MediaItem(androidx.media3.common.MediaItem) TestPlayerRunHelper.runUntilTimelineChanged(androidx.media3.test.utils.robolectric.TestPlayerRunHelper.runUntilTimelineChanged) COMMAND_GET_TRACK_INFOS(androidx.media3.common.Player.COMMAND_GET_TRACK_INFOS) ServerSideAdInsertionMediaSource(androidx.media3.exoplayer.source.ads.ServerSideAdInsertionMediaSource) FakeChunkSource(androidx.media3.test.utils.FakeChunkSource) TestPlayerRunHelper.playUntilPosition(androidx.media3.test.utils.robolectric.TestPlayerRunHelper.playUntilPosition) PlaybackException(androidx.media3.common.PlaybackException) TrackGroupArray(androidx.media3.common.TrackGroupArray) Util(androidx.media3.common.util.Util) Collectors(java.util.stream.Collectors) Format(androidx.media3.common.Format) COMMAND_SET_TRACK_SELECTION_PARAMETERS(androidx.media3.common.Player.COMMAND_SET_TRACK_SELECTION_PARAMETERS) COMMAND_SEEK_TO_PREVIOUS_MEDIA_ITEM(androidx.media3.common.Player.COMMAND_SEEK_TO_PREVIOUS_MEDIA_ITEM) PlaybackParameters(androidx.media3.common.PlaybackParameters) List(java.util.List) FakeAdaptiveMediaSource(androidx.media3.test.utils.FakeAdaptiveMediaSource) TrackSelectionArray(androidx.media3.common.TrackSelectionArray) Mockito.inOrder(org.mockito.Mockito.inOrder) ConcatenatingMediaSource(androidx.media3.exoplayer.source.ConcatenatingMediaSource) AdditionalMatchers.not(org.mockito.AdditionalMatchers.not) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) TrackGroup(androidx.media3.common.TrackGroup) Context(android.content.Context) DEFAULT_WINDOW_DURATION_US(androidx.media3.test.utils.FakeTimeline.TimelineWindowDefinition.DEFAULT_WINDOW_DURATION_US) COMMAND_SET_SHUFFLE_MODE(androidx.media3.common.Player.COMMAND_SET_SHUFFLE_MODE) COMMAND_GET_TEXT(androidx.media3.common.Player.COMMAND_GET_TEXT) Assert.assertThrows(org.junit.Assert.assertThrows) MediaMetadata(androidx.media3.common.MediaMetadata) Metadata(androidx.media3.common.Metadata) TestPlayerRunHelper.runUntilPendingCommandsAreFullyHandled(androidx.media3.test.utils.robolectric.TestPlayerRunHelper.runUntilPendingCommandsAreFullyHandled) TracksInfo(androidx.media3.common.TracksInfo) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestPlayerRunHelper.playUntilStartOfMediaItem(androidx.media3.test.utils.robolectric.TestPlayerRunHelper.playUntilStartOfMediaItem) Intent(android.content.Intent) COMMAND_PREPARE(androidx.media3.common.Player.COMMAND_PREPARE) FakeTimeline(androidx.media3.test.utils.FakeTimeline) AtomicReference(java.util.concurrent.atomic.AtomicReference) Shadows.shadowOf(org.robolectric.Shadows.shadowOf) ActionSchedule(androidx.media3.test.utils.ActionSchedule) AudioManager(android.media.AudioManager) FakeDataSource(androidx.media3.test.utils.FakeDataSource) HashSet(java.util.HashSet) COMMAND_SEEK_FORWARD(androidx.media3.common.Player.COMMAND_SEEK_FORWARD) COMMAND_SET_VOLUME(androidx.media3.common.Player.COMMAND_SET_VOLUME) TimelineWindowDefinition(androidx.media3.test.utils.FakeTimeline.TimelineWindowDefinition) PlayerTarget(androidx.media3.test.utils.ActionSchedule.PlayerTarget) ArgumentCaptor(org.mockito.ArgumentCaptor) ShadowAudioManager(org.robolectric.shadows.ShadowAudioManager) SinglePeriodTimeline(androidx.media3.exoplayer.source.SinglePeriodTimeline) ImmutableList(com.google.common.collect.ImmutableList) DrmSessionEventListener(androidx.media3.exoplayer.drm.DrmSessionEventListener) Window(androidx.media3.common.Timeline.Window) TestExoPlayerBuilder(androidx.media3.test.utils.TestExoPlayerBuilder) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) RobolectricUtil.runMainLooperUntil(androidx.media3.test.utils.robolectric.RobolectricUtil.runMainLooperUntil) SurfaceTexture(android.graphics.SurfaceTexture) DEFAULT_WINDOW_OFFSET_IN_FIRST_PERIOD_US(androidx.media3.test.utils.FakeTimeline.TimelineWindowDefinition.DEFAULT_WINDOW_OFFSET_IN_FIRST_PERIOD_US) MediaSource(androidx.media3.exoplayer.source.MediaSource) InOrder(org.mockito.InOrder) COMMAND_SEEK_BACK(androidx.media3.common.Player.COMMAND_SEEK_BACK) MimeTypes(androidx.media3.common.MimeTypes) COMMAND_SEEK_TO_NEXT(androidx.media3.common.Player.COMMAND_SEEK_TO_NEXT) Clock(androidx.media3.common.util.Clock) Truth.assertThat(com.google.common.truth.Truth.assertThat) COMMAND_SET_MEDIA_ITEMS_METADATA(androidx.media3.common.Player.COMMAND_SET_MEDIA_ITEMS_METADATA) TestPlayerRunHelper.runUntilReceiveOffloadSchedulingEnabledNewState(androidx.media3.test.utils.robolectric.TestPlayerRunHelper.runUntilReceiveOffloadSchedulingEnabledNewState) Mockito.verify(org.mockito.Mockito.verify) COMMAND_GET_VOLUME(androidx.media3.common.Player.COMMAND_GET_VOLUME) C(androidx.media3.common.C) Mockito(org.mockito.Mockito) PositionInfo(androidx.media3.common.Player.PositionInfo) FakeSampleStreamItem.oneByteSample(androidx.media3.test.utils.FakeSampleStream.FakeSampleStreamItem.oneByteSample) STATE_ENDED(androidx.media3.common.Player.STATE_ENDED) MediaPeriod(androidx.media3.exoplayer.source.MediaPeriod) Collections(java.util.Collections) TestPlayerRunHelper.runUntilPlaybackState(androidx.media3.test.utils.robolectric.TestPlayerRunHelper.runUntilPlaybackState) TransferListener(androidx.media3.datasource.TransferListener) FakeMediaSource(androidx.media3.test.utils.FakeMediaSource) Loader(androidx.media3.exoplayer.upstream.Loader) FakeRenderer(androidx.media3.test.utils.FakeRenderer) FakeMediaPeriod(androidx.media3.test.utils.FakeMediaPeriod) FakeSampleStreamItem(androidx.media3.test.utils.FakeSampleStream.FakeSampleStreamItem) Format(androidx.media3.common.Format) FakeTimeline(androidx.media3.test.utils.FakeTimeline) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) ExoPlayerTestRunner(androidx.media3.test.utils.ExoPlayerTestRunner) Allocator(androidx.media3.exoplayer.upstream.Allocator) DrmSessionManager(androidx.media3.exoplayer.drm.DrmSessionManager) TrackGroupArray(androidx.media3.common.TrackGroupArray) FakeSampleStream(androidx.media3.test.utils.FakeSampleStream) CompositeMediaSource(androidx.media3.exoplayer.source.CompositeMediaSource) ClippingMediaSource(androidx.media3.exoplayer.source.ClippingMediaSource) FakeMediaSource(androidx.media3.test.utils.FakeMediaSource) MaskingMediaSource(androidx.media3.exoplayer.source.MaskingMediaSource) ServerSideAdInsertionMediaSource(androidx.media3.exoplayer.source.ads.ServerSideAdInsertionMediaSource) FakeAdaptiveMediaSource(androidx.media3.test.utils.FakeAdaptiveMediaSource) ConcatenatingMediaSource(androidx.media3.exoplayer.source.ConcatenatingMediaSource) MediaSource(androidx.media3.exoplayer.source.MediaSource) MediaPeriodId(androidx.media3.exoplayer.source.MediaSource.MediaPeriodId) Nullable(androidx.annotation.Nullable) Test(org.junit.Test)

Example 5 with Allocation

use of androidx.media3.exoplayer.upstream.Allocation in project media by androidx.

the class SampleDataQueue method readData.

/**
 * Reads data from {@code allocationNode} and its following nodes.
 *
 * @param allocationNode The first {@link AllocationNode} containing data yet to be read.
 * @param absolutePosition The absolute position from which data should be read.
 * @param target The buffer into which data should be written.
 * @param length The number of bytes to read.
 * @return The first {@link AllocationNode} that contains unread bytes after this method returns.
 */
private static AllocationNode readData(AllocationNode allocationNode, long absolutePosition, ByteBuffer target, int length) {
    allocationNode = getNodeContainingPosition(allocationNode, absolutePosition);
    int remaining = length;
    while (remaining > 0) {
        int toCopy = min(remaining, (int) (allocationNode.endPosition - absolutePosition));
        Allocation allocation = allocationNode.allocation;
        target.put(allocation.data, allocationNode.translateOffset(absolutePosition), toCopy);
        remaining -= toCopy;
        absolutePosition += toCopy;
        if (absolutePosition == allocationNode.endPosition) {
            allocationNode = allocationNode.next;
        }
    }
    return allocationNode;
}
Also used : Allocation(androidx.media3.exoplayer.upstream.Allocation)

Aggregations

Allocation (androidx.media3.exoplayer.upstream.Allocation)4 Context (android.content.Context)2 Intent (android.content.Intent)2 SurfaceTexture (android.graphics.SurfaceTexture)2 AudioManager (android.media.AudioManager)2 Uri (android.net.Uri)2 Looper (android.os.Looper)2 Surface (android.view.Surface)2 Nullable (androidx.annotation.Nullable)2 AdPlaybackState (androidx.media3.common.AdPlaybackState)2 AudioAttributes (androidx.media3.common.AudioAttributes)2 C (androidx.media3.common.C)2 Format (androidx.media3.common.Format)2 IllegalSeekPositionException (androidx.media3.common.IllegalSeekPositionException)2 MediaItem (androidx.media3.common.MediaItem)2 MediaMetadata (androidx.media3.common.MediaMetadata)2 Metadata (androidx.media3.common.Metadata)2 MimeTypes (androidx.media3.common.MimeTypes)2 PlaybackException (androidx.media3.common.PlaybackException)2 PlaybackParameters (androidx.media3.common.PlaybackParameters)2