Skip to content

Commit

Permalink
Use MediaItem.DEFAULT_MEDIA_ID as default media ID
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 368204261
  • Loading branch information
marcbaechinger authored and andrewlewis committed Apr 13, 2021
1 parent 1d3f72c commit cc26a92
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 19 deletions.
2 changes: 2 additions & 0 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@
* Assume Dolby Vision content is encoded as H264 when calculating maximum
codec input size
([#8705](https://github.com/google/ExoPlayer/issues/8705)).
* Use an empty string instead of the URI if the media ID is not explicitly
set with `MediaItem.Builder.setMediaId(String)`.
* HLS:
* Fix bug of ignoring `EXT-X-START` when setting the live target offset
([#8764](https://github.com/google/ExoPlayer/pull/8764)).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public MediaItem convertToExoPlayerMediaItem(androidx.media2.common.MediaItem me

return new MediaItem.Builder()
.setUri(uri)
.setMediaId(mediaId)
.setMediaId(mediaId != null ? mediaId : MediaItem.DEFAULT_MEDIA_ID)
.setMediaMetadata(
new com.google.android.exoplayer2.MediaMetadata.Builder().setTitle(title).build())
.setTag(media2MediaItem)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,17 +147,17 @@ private Builder(MediaItem mediaItem) {
}

/**
* Sets the optional media ID which identifies the media item. If not specified, {@link #setUri}
* must be called and the string representation of {@link PlaybackProperties#uri} is used as the
* media ID.
* Sets the optional media ID which identifies the media item.
*
* <p>By default {@link #DEFAULT_MEDIA_ID} is used.
*/
public Builder setMediaId(@Nullable String mediaId) {
this.mediaId = mediaId;
public Builder setMediaId(String mediaId) {
this.mediaId = checkNotNull(mediaId);
return this;
}

/**
* Sets the optional URI. If not specified, {@link #setMediaId(String)} must be called.
* Sets the optional URI.
*
* <p>If {@code uri} is null or unset no {@link PlaybackProperties} object is created during
* {@link #build()} and any other {@code Builder} methods that would populate {@link
Expand All @@ -168,7 +168,7 @@ public Builder setUri(@Nullable String uri) {
}

/**
* Sets the optional URI. If not specified, {@link #setMediaId(String)} must be called.
* Sets the optional URI.
*
* <p>If {@code uri} is null or unset no {@link PlaybackProperties} object is created during
* {@link #build()} and any other {@code Builder} methods that would populate {@link
Expand Down Expand Up @@ -587,10 +587,9 @@ public MediaItem build() {
customCacheKey,
subtitles,
tag);
mediaId = mediaId != null ? mediaId : uri.toString();
}
return new MediaItem(
checkNotNull(mediaId),
mediaId != null ? mediaId : DEFAULT_MEDIA_ID,
new ClippingProperties(
clipStartPositionMs,
clipEndPositionMs,
Expand Down Expand Up @@ -1194,6 +1193,12 @@ private static String keyForField(@ClippingProperties.FieldNumber int field) {
}
}

/**
* The default media ID that is used if the media ID is not explicitly set by {@link
* Builder#setMediaId(String)}.
*/
public static final String DEFAULT_MEDIA_ID = "";

/** Identifies the media item. */
public final String mediaId;

Expand Down Expand Up @@ -1296,7 +1301,7 @@ public Bundle toBundle() {
public static final Creator<MediaItem> CREATOR = MediaItem::fromBundle;

private static MediaItem fromBundle(Bundle bundle) {
String mediaId = checkNotNull(bundle.getString(keyForField(FIELD_MEDIA_ID)));
String mediaId = checkNotNull(bundle.getString(keyForField(FIELD_MEDIA_ID), DEFAULT_MEDIA_ID));
@Nullable
Bundle liveConfigurationBundle = bundle.getBundle(keyForField(FIELD_LIVE_CONFIGURATION));
LiveConfiguration liveConfiguration;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,13 @@ public class MediaItemTest {

private static final String URI_STRING = "http://www.google.com";

@Test
public void builder_needsUriOrMediaId() {
assertThrows(NullPointerException.class, () -> new MediaItem.Builder().build());
}

@Test
public void builderWithUri_setsUri() {
Uri uri = Uri.parse(URI_STRING);

MediaItem mediaItem = MediaItem.fromUri(uri);

assertThat(mediaItem.playbackProperties.uri.toString()).isEqualTo(URI_STRING);
assertThat(mediaItem.mediaId).isEqualTo(URI_STRING);
assertThat(mediaItem.playbackProperties.uri).isEqualTo(uri);
assertThat(mediaItem.mediaMetadata).isNotNull();
}

Expand All @@ -58,7 +52,13 @@ public void builderWithUriAsString_setsUri() {
MediaItem mediaItem = MediaItem.fromUri(URI_STRING);

assertThat(mediaItem.playbackProperties.uri.toString()).isEqualTo(URI_STRING);
assertThat(mediaItem.mediaId).isEqualTo(URI_STRING);
}

@Test
public void builderWithoutMediaId_usesDefaultMediaId() {
MediaItem mediaItem = MediaItem.fromUri(URI_STRING);

assertThat(mediaItem.mediaId).isEqualTo(MediaItem.DEFAULT_MEDIA_ID);
}

@Test
Expand Down

0 comments on commit cc26a92

Please sign in to comment.