Add MediaChunkListIterator

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=219472988
This commit is contained in:
eguven 2018-10-31 07:57:14 -07:00 committed by Oliver Woodman
parent 140d5fc9a9
commit 9d947a56f8
5 changed files with 198 additions and 39 deletions

View File

@ -56,14 +56,14 @@ public abstract class BaseMediaChunkIterator implements MediaChunkIterator {
*
* @throws NoSuchElementException If the iterator does not point to a valid element.
*/
protected void checkInBounds() {
protected final void checkInBounds() {
if (currentIndex < fromIndex || currentIndex > toIndex) {
throw new NoSuchElementException();
}
}
/** Returns the current index this iterator is pointing to. */
protected long getCurrentIndex() {
protected final long getCurrentIndex() {
return currentIndex;
}
}

View File

@ -0,0 +1,61 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.android.exoplayer2.source.chunk;
import com.google.android.exoplayer2.upstream.DataSpec;
import java.util.List;
/** A {@link MediaChunkIterator} which iterates over a {@link List} of {@link MediaChunk}s. */
public final class MediaChunkListIterator extends BaseMediaChunkIterator {
private final List<? extends MediaChunk> chunks;
private final boolean reverseOrder;
/**
* Creates iterator.
*
* @param chunks The list of chunks to iterate over.
* @param reverseOrder Whether to iterate in reverse order.
*/
public MediaChunkListIterator(List<? extends MediaChunk> chunks, boolean reverseOrder) {
super(0, chunks.size() - 1);
this.chunks = chunks;
this.reverseOrder = reverseOrder;
}
@Override
public DataSpec getDataSpec() {
return getCurrentChunk().dataSpec;
}
@Override
public long getChunkStartTimeUs() {
return getCurrentChunk().startTimeUs;
}
@Override
public long getChunkEndTimeUs() {
return getCurrentChunk().endTimeUs;
}
private MediaChunk getCurrentChunk() {
int index = (int) super.getCurrentIndex();
if (reverseOrder) {
index = chunks.size() - 1 - index;
}
return chunks.get(index);
}
}

View File

@ -0,0 +1,76 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.android.exoplayer2.source.chunk;
import static com.google.common.truth.Truth.assertThat;
import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.testutil.FakeMediaChunk;
import java.util.Arrays;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
/** Tests for {@link MediaChunkListIterator}. */
@RunWith(RobolectricTestRunner.class)
public class MediaChunkListIteratorTest {
private static final Format TEST_FORMAT = Format.createSampleFormat(null, null, 0);
private FakeMediaChunk testChunk1;
private FakeMediaChunk testChunk2;
@Before
public void setUp() {
testChunk1 = new FakeMediaChunk(TEST_FORMAT, 0, 10);
testChunk2 = new FakeMediaChunk(TEST_FORMAT, 10, 20);
}
@Test
public void iterator_reverseOrderFalse_returnsItemsInNormalOrder() {
MediaChunkListIterator iterator =
new MediaChunkListIterator(
Arrays.asList(testChunk1, testChunk2), /* reverseOrder= */ false);
assertThat(iterator.isEnded()).isFalse();
assertThat(iterator.next()).isTrue();
assertEqual(iterator, testChunk1);
assertThat(iterator.next()).isTrue();
assertEqual(iterator, testChunk2);
assertThat(iterator.next()).isFalse();
assertThat(iterator.isEnded()).isTrue();
}
@Test
public void iterator_reverseOrderTrue_returnsItemsInReverseOrder() {
MediaChunkListIterator iterator =
new MediaChunkListIterator(
Arrays.asList(testChunk1, testChunk2), /* reverseOrder= */ true);
assertThat(iterator.isEnded()).isFalse();
assertThat(iterator.next()).isTrue();
assertEqual(iterator, testChunk2);
assertThat(iterator.next()).isTrue();
assertEqual(iterator, testChunk1);
assertThat(iterator.next()).isFalse();
assertThat(iterator.isEnded()).isTrue();
}
private static void assertEqual(MediaChunkListIterator iterator, FakeMediaChunk chunk) {
assertThat(iterator.getChunkStartTimeUs()).isEqualTo(chunk.startTimeUs);
assertThat(iterator.getChunkEndTimeUs()).isEqualTo(chunk.endTimeUs);
assertThat(iterator.getDataSpec()).isEqualTo(chunk.dataSpec);
}
}

View File

@ -23,19 +23,14 @@ import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
import android.net.Uri;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.source.TrackGroup;
import com.google.android.exoplayer2.source.chunk.MediaChunk;
import com.google.android.exoplayer2.source.chunk.MediaChunkIterator;
import com.google.android.exoplayer2.testutil.FakeClock;
import com.google.android.exoplayer2.testutil.FakeMediaChunk;
import com.google.android.exoplayer2.upstream.BandwidthMeter;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DataSpec;
import com.google.android.exoplayer2.upstream.DefaultHttpDataSource;
import com.google.android.exoplayer2.util.MimeTypes;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@ -409,35 +404,4 @@ public final class AdaptiveTrackSelectionTest {
/* drmInitData= */ null);
}
private static final class FakeMediaChunk extends MediaChunk {
private static final DataSource DATA_SOURCE = new DefaultHttpDataSource("TEST_AGENT", null);
public FakeMediaChunk(Format trackFormat, long startTimeUs, long endTimeUs) {
super(
DATA_SOURCE,
new DataSpec(Uri.EMPTY),
trackFormat,
C.SELECTION_REASON_ADAPTIVE,
null,
startTimeUs,
endTimeUs,
0);
}
@Override
public void cancelLoad() {
// Do nothing.
}
@Override
public void load() throws IOException, InterruptedException {
// Do nothing.
}
@Override
public boolean isLoadCompleted() {
return true;
}
}
}

View File

@ -0,0 +1,58 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.android.exoplayer2.testutil;
import android.net.Uri;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.source.chunk.MediaChunk;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DataSpec;
import com.google.android.exoplayer2.upstream.DefaultHttpDataSource;
import java.io.IOException;
/** Fake {@link MediaChunk}. */
public final class FakeMediaChunk extends MediaChunk {
private static final DataSource DATA_SOURCE = new DefaultHttpDataSource("TEST_AGENT", null);
public FakeMediaChunk(Format trackFormat, long startTimeUs, long endTimeUs) {
super(
DATA_SOURCE,
new DataSpec(Uri.EMPTY),
trackFormat,
C.SELECTION_REASON_ADAPTIVE,
/* trackSelectionData= */ null,
startTimeUs,
endTimeUs,
/* chunkIndex= */ 0);
}
@Override
public void cancelLoad() {
// Do nothing.
}
@Override
public void load() throws IOException, InterruptedException {
// Do nothing.
}
@Override
public boolean isLoadCompleted() {
return true;
}
}