Commit 621f1b0b by bachinger Committed by Oliver Woodman

Make SilenceMediaSource add a media item to the timeline

PiperOrigin-RevId: 311295749
parent fefb2a03
......@@ -15,10 +15,12 @@
*/
package com.google.android.exoplayer2.source;
import android.net.Uri;
import androidx.annotation.Nullable;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.FormatHolder;
import com.google.android.exoplayer2.MediaItem;
import com.google.android.exoplayer2.SeekParameters;
import com.google.android.exoplayer2.decoder.DecoderInputBuffer;
import com.google.android.exoplayer2.trackselection.TrackSelection;
......@@ -40,7 +42,7 @@ public final class SilenceMediaSource extends BaseMediaSource {
@Nullable private Object tag;
/**
* Sets the duration of the silent audio.
* Sets the duration of the silent audio. The value needs to be a positive value.
*
* @param durationUs The duration of silent audio to output, in microseconds.
* @return This factory, for convenience.
......@@ -63,12 +65,20 @@ public final class SilenceMediaSource extends BaseMediaSource {
return this;
}
/** Creates a new {@link SilenceMediaSource}. */
/**
* Creates a new {@link SilenceMediaSource}.
*
* @throws IllegalStateException if the duration is a non-positive value.
*/
public SilenceMediaSource createMediaSource() {
return new SilenceMediaSource(durationUs, tag);
Assertions.checkState(durationUs > 0);
return new SilenceMediaSource(durationUs, MEDIA_ITEM.buildUpon().setTag(tag).build());
}
}
/** The media id used by any media item of silence media sources. */
public static final String MEDIA_ID = "com.google.android.exoplayer2.source.SilenceMediaSource";
private static final int SAMPLE_RATE_HZ = 44100;
@C.PcmEncoding private static final int PCM_ENCODING = C.ENCODING_PCM_16BIT;
private static final int CHANNEL_COUNT = 2;
......@@ -79,11 +89,17 @@ public final class SilenceMediaSource extends BaseMediaSource {
.setSampleRate(SAMPLE_RATE_HZ)
.setPcmEncoding(PCM_ENCODING)
.build();
private static final MediaItem MEDIA_ITEM =
new MediaItem.Builder()
.setMediaId(MEDIA_ID)
.setUri(Uri.EMPTY)
.setMimeType(FORMAT.sampleMimeType)
.build();
private static final byte[] SILENCE_SAMPLE =
new byte[Util.getPcmFrameSize(PCM_ENCODING, CHANNEL_COUNT) * 1024];
private final long durationUs;
@Nullable private final Object tag;
private final MediaItem mediaItem;
/**
* Creates a new media source providing silent audio of the given duration.
......@@ -91,13 +107,19 @@ public final class SilenceMediaSource extends BaseMediaSource {
* @param durationUs The duration of silent audio to output, in microseconds.
*/
public SilenceMediaSource(long durationUs) {
this(durationUs, /* tag= */ null);
this(durationUs, MEDIA_ITEM);
}
private SilenceMediaSource(long durationUs, @Nullable Object tag) {
/**
* Creates a new media source providing silent audio of the given duration.
*
* @param durationUs The duration of silent audio to output, in microseconds.
* @param mediaItem The media item associated with this media source.
*/
private SilenceMediaSource(long durationUs, MediaItem mediaItem) {
Assertions.checkArgument(durationUs >= 0);
this.durationUs = durationUs;
this.tag = tag;
this.mediaItem = mediaItem;
}
@Override
......@@ -109,7 +131,7 @@ public final class SilenceMediaSource extends BaseMediaSource {
/* isDynamic= */ false,
/* isLive= */ false,
/* manifest= */ null,
tag));
mediaItem));
}
@Override
......@@ -123,6 +145,12 @@ public final class SilenceMediaSource extends BaseMediaSource {
@Override
public void releasePeriod(MediaPeriod mediaPeriod) {}
/** Returns the {@link MediaItem} of this media source. */
// TODO(bachinger): add @Override annotation once the method is defined by MediaSource.
public MediaItem getMediaItem() {
return mediaItem;
}
@Override
protected void releaseSourceInternal() {}
......
/*
* Copyright 2020 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;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;
import android.net.Uri;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.google.android.exoplayer2.MediaItem;
import com.google.android.exoplayer2.util.MimeTypes;
import org.junit.Test;
import org.junit.runner.RunWith;
/** Unit tests for {@link SilenceMediaSource}. */
@RunWith(AndroidJUnit4.class)
public class SilenceMediaSourceTest {
@Test
public void builder_setsMediaItem() {
SilenceMediaSource mediaSource =
new SilenceMediaSource.Factory().setDurationUs(1_000_000).createMediaSource();
MediaItem mediaItem = mediaSource.getMediaItem();
assertThat(mediaItem).isNotNull();
assertThat(mediaItem.mediaId).isEqualTo(SilenceMediaSource.MEDIA_ID);
assertThat(mediaItem.playbackProperties.uri).isEqualTo(Uri.EMPTY);
assertThat(mediaItem.playbackProperties.mimeType).isEqualTo(MimeTypes.AUDIO_RAW);
}
@Test
public void builderSetTag_setsTagOfMediaItem() {
Object tag = new Object();
SilenceMediaSource mediaSource =
new SilenceMediaSource.Factory().setTag(tag).setDurationUs(1_000_000).createMediaSource();
assertThat(mediaSource.getMediaItem().playbackProperties.tag).isEqualTo(tag);
}
@Test
public void builder_setDurationUsNotCalled_throwsIllegalStateException() {
assertThrows(IllegalStateException.class, new SilenceMediaSource.Factory()::createMediaSource);
}
@Test
public void builderSetDurationUs_nonPositiveValue_throwsIllegalStateException() {
SilenceMediaSource.Factory factory = new SilenceMediaSource.Factory().setDurationUs(-1);
assertThrows(IllegalStateException.class, factory::createMediaSource);
}
@Test
public void newInstance_setsMediaItem() {
SilenceMediaSource mediaSource = new SilenceMediaSource(1_000_000);
MediaItem mediaItem = mediaSource.getMediaItem();
assertThat(mediaItem).isNotNull();
assertThat(mediaItem.mediaId).isEqualTo(SilenceMediaSource.MEDIA_ID);
assertThat(mediaSource.getMediaItem().playbackProperties.uri).isEqualTo(Uri.EMPTY);
assertThat(mediaItem.playbackProperties.mimeType).isEqualTo(MimeTypes.AUDIO_RAW);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment