The behavior of this class depends on the underlying Cast session, which is obtained from the + * Cast context passed to {@link #CastPlayer}. To keep track of the session, {@link + * #isCastSessionAvailable()} can be queried and {@link SessionAvailabilityListener} can be + * implemented and attached to the player. + * + *
If no session is available, the player state will remain unchanged and calls to methods that + * alter it will be ignored. Querying the player state is possible even when no session is + * available, in which case, the last observed receiver app state is reported. + * + *
Methods should be called on the application's main thread.
+ */
+public final class CastPlayer extends BasePlayer {
+
+ private static final String TAG = "CastPlayer";
+
+ private static final int RENDERER_COUNT = 3;
+ private static final int RENDERER_INDEX_VIDEO = 0;
+ private static final int RENDERER_INDEX_AUDIO = 1;
+ private static final int RENDERER_INDEX_TEXT = 2;
+ private static final long PROGRESS_REPORT_PERIOD_MS = 1000;
+ private static final TrackSelectionArray EMPTY_TRACK_SELECTION_ARRAY =
+ new TrackSelectionArray(null, null, null);
+ private static final long[] EMPTY_TRACK_ID_ARRAY = new long[0];
+
+ private final CastContext castContext;
+ // TODO: Allow custom implementations of CastTimelineTracker.
+ private final CastTimelineTracker timelineTracker;
+ private final Timeline.Period period;
+
+ private RemoteMediaClient remoteMediaClient;
+
+ // Result callbacks.
+ private final StatusListener statusListener;
+ private final SeekResultCallback seekResultCallback;
+
+ // Listeners.
+ private final CopyOnWriteArraySet This class keeps track of the duration reported by the current item to fill any missing
+ * durations in the media queue items [See internal: b/65152553].
+ */
+/* package */ final class CastTimelineTracker {
+
+ private final HashMap The usage of this mime type is optional and player implementation specific.
+ */
+ public final String mimeType;
+
+ // TODO: Add support for sideloaded tracks, artwork, icon, and subtitle.
+
+ @Override
+ public boolean equals(@Nullable Object other) {
+ if (this == other) {
+ return true;
+ }
+ if (other == null || getClass() != other.getClass()) {
+ return false;
+ }
+ MediaItem mediaItem = (MediaItem) other;
+ return startPositionUs == mediaItem.startPositionUs
+ && endPositionUs == mediaItem.endPositionUs
+ && uuid.equals(mediaItem.uuid)
+ && title.equals(mediaItem.title)
+ && description.equals(mediaItem.description)
+ && media.equals(mediaItem.media)
+ && Util.areEqual(attachment, mediaItem.attachment)
+ && drmSchemes.equals(mediaItem.drmSchemes)
+ && mimeType.equals(mediaItem.mimeType);
+ }
+
+ @Override
+ public int hashCode() {
+ int result = uuid.hashCode();
+ result = 31 * result + title.hashCode();
+ result = 31 * result + description.hashCode();
+ result = 31 * result + media.hashCode();
+ result = 31 * result + (attachment != null ? attachment.hashCode() : 0);
+ result = 31 * result + drmSchemes.hashCode();
+ result = 31 * result + (int) (startPositionUs ^ (startPositionUs >>> 32));
+ result = 31 * result + (int) (endPositionUs ^ (endPositionUs >>> 32));
+ result = 31 * result + mimeType.hashCode();
+ return result;
+ }
+
+ private MediaItem(
+ UUID uuid,
+ String title,
+ String description,
+ UriBundle media,
+ @Nullable Object attachment,
+ List Calling this method is equivalent to removing the item at position {@code indexFrom} and
+ * immediately inserting it at position {@code indexTo}. If the moved item is being played at the
+ * moment of the invocation, playback will stick with the moved item.
+ *
+ * @param indexFrom The index of the item to move.
+ * @param indexTo The index at which the item will be placed after this operation.
+ * @throws IndexOutOfBoundsException If for either index, {@code index < 0 || index >= getSize()}.
+ */
+ void move(int indexFrom, int indexTo);
+
+ /**
+ * Removes an item from the queue.
+ *
+ * @param index The index of the item to remove from the queue.
+ * @throws IndexOutOfBoundsException If {@code index < 0 || index >= getSize()}.
+ */
+ void remove(int index);
+
+ /**
+ * Removes a range of items from the queue.
+ *
+ * Does nothing if an empty range ({@code from == exclusiveTo}) is passed.
+ *
+ * @param from The inclusive index at which the range to remove starts.
+ * @param exclusiveTo The exclusive index at which the range to remove ends.
+ * @throws IndexOutOfBoundsException If {@code from < 0 || exclusiveTo > getSize() || from >
+ * exclusiveTo}.
+ */
+ void removeRange(int from, int exclusiveTo);
+
+ /** Removes all items in the queue. */
+ void clear();
+}
diff --git a/extensions/cast/src/main/java/com/google/android/exoplayer2/ext/cast/SessionAvailabilityListener.java b/extensions/cast/src/main/java/com/google/android/exoplayer2/ext/cast/SessionAvailabilityListener.java
new file mode 100644
index 0000000000..c686c496c6
--- /dev/null
+++ b/extensions/cast/src/main/java/com/google/android/exoplayer2/ext/cast/SessionAvailabilityListener.java
@@ -0,0 +1,26 @@
+/*
+ * 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.ext.cast;
+
+/** Listener of changes in the cast session availability. */
+public interface SessionAvailabilityListener {
+
+ /** Called when a cast session becomes available to the player. */
+ void onCastSessionAvailable();
+
+ /** Called when the cast session becomes unavailable. */
+ void onCastSessionUnavailable();
+}
diff --git a/extensions/cast/src/test/AndroidManifest.xml b/extensions/cast/src/test/AndroidManifest.xml
new file mode 100644
index 0000000000..aea8bda663
--- /dev/null
+++ b/extensions/cast/src/test/AndroidManifest.xml
@@ -0,0 +1,17 @@
+
+
+
+