From 91341df7b05c44637b0cf474de55f56031ba869e Mon Sep 17 00:00:00 2001 From: tonihei Date: Mon, 17 Jul 2017 07:11:01 -0700 Subject: [PATCH] Add fake adaptive data set. This class defines the data of an adaptive media source. It currently has chunks of equal length and size corresponding to the declared average bitrate of the Formats. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=162208008 --- .../testutil/FakeAdaptiveDataSet.java | 87 +++++++++++++++++++ .../exoplayer2/testutil/FakeDataSet.java | 2 +- 2 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 testutils/src/main/java/com/google/android/exoplayer2/testutil/FakeAdaptiveDataSet.java diff --git a/testutils/src/main/java/com/google/android/exoplayer2/testutil/FakeAdaptiveDataSet.java b/testutils/src/main/java/com/google/android/exoplayer2/testutil/FakeAdaptiveDataSet.java new file mode 100644 index 0000000000..961da2c9dd --- /dev/null +++ b/testutils/src/main/java/com/google/android/exoplayer2/testutil/FakeAdaptiveDataSet.java @@ -0,0 +1,87 @@ +/* + * Copyright (C) 2017 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 com.google.android.exoplayer2.C; +import com.google.android.exoplayer2.Format; +import com.google.android.exoplayer2.trackselection.TrackSelection; + +/** + * Fake data set emulating the data of an adaptive media source. + * It provides chunk data for all {@link Format}s in the given {@link TrackSelection}. + */ +public final class FakeAdaptiveDataSet extends FakeDataSet { + + /** + * Factory for {@link FakeAdaptiveDataSet}s. + */ + public static final class Factory { + + private final long chunkDurationUs; + + public Factory(long chunkDurationUs) { + this.chunkDurationUs = chunkDurationUs; + } + + public FakeAdaptiveDataSet createDataSet(TrackSelection trackSelection, long mediaDurationUs) { + return new FakeAdaptiveDataSet(trackSelection, mediaDurationUs, chunkDurationUs); + } + + } + + private final long chunkDurationUs; + private final long lastChunkDurationUs; + + public FakeAdaptiveDataSet(TrackSelection trackSelection, long mediaDurationUs, + long chunkDurationUs) { + this.chunkDurationUs = chunkDurationUs; + int selectionCount = trackSelection.length(); + long lastChunkDurationUs = mediaDurationUs % chunkDurationUs; + int fullChunks = (int) (mediaDurationUs / chunkDurationUs); + for (int i = 0; i < selectionCount; i++) { + String uri = getUri(i); + Format format = trackSelection.getFormat(i); + int chunkLength = (int) (format.bitrate * chunkDurationUs / (8 * C.MICROS_PER_SECOND)); + FakeData newData = this.newData(uri); + for (int j = 0; j < fullChunks; j++) { + newData.appendReadData(chunkLength); + } + if (lastChunkDurationUs > 0) { + int lastChunkLength = (int) (format.bitrate * (mediaDurationUs % chunkDurationUs) + / (8 * C.MICROS_PER_SECOND)); + newData.appendReadData(lastChunkLength); + } + } + this.lastChunkDurationUs = lastChunkDurationUs == 0 ? chunkDurationUs : lastChunkDurationUs; + } + + public String getUri(int trackSelectionIndex) { + return "fake://adaptive.media/" + Integer.toString(trackSelectionIndex); + } + + public long getChunkDuration(int chunkIndex) { + return chunkIndex == getAllData().size() - 1 ? lastChunkDurationUs : chunkDurationUs; + } + + public long getStartTime(int chunkIndex) { + return chunkIndex * chunkDurationUs; + } + + public int getChunkIndexByPosition(long positionUs) { + return (int) (positionUs / chunkDurationUs); + } + +} diff --git a/testutils/src/main/java/com/google/android/exoplayer2/testutil/FakeDataSet.java b/testutils/src/main/java/com/google/android/exoplayer2/testutil/FakeDataSet.java index f56bcae6a0..777d46f1a1 100644 --- a/testutils/src/main/java/com/google/android/exoplayer2/testutil/FakeDataSet.java +++ b/testutils/src/main/java/com/google/android/exoplayer2/testutil/FakeDataSet.java @@ -67,7 +67,7 @@ import java.util.List; * .endData(); * */ -public final class FakeDataSet { +public class FakeDataSet { /** Container of fake data to be served by a {@link FakeDataSource}. */ public static final class FakeData {