From 54f3dfb4530fa41c63ef13f18daa2bf16f221514 Mon Sep 17 00:00:00 2001 From: krocard Date: Wed, 14 Apr 2021 16:42:56 +0100 Subject: [PATCH] Move DeviceComponent in ExoPlayer PiperOrigin-RevId: 368437660 --- .../exoplayer2/ext/cast/CastPlayer.java | 42 ++++++++-- .../com/google/android/exoplayer2/Player.java | 84 ++++++++----------- .../exoplayer2/device/DeviceListener.java | 4 +- .../google/android/exoplayer2/ExoPlayer.java | 51 +++++++++++ .../android/exoplayer2/ExoPlayerImpl.java | 35 ++++++++ .../android/exoplayer2/SimpleExoPlayer.java | 2 +- .../exoplayer2/testutil/StubExoPlayer.java | 36 ++++++++ 7 files changed, 194 insertions(+), 60 deletions(-) diff --git a/extensions/cast/src/main/java/com/google/android/exoplayer2/ext/cast/CastPlayer.java b/extensions/cast/src/main/java/com/google/android/exoplayer2/ext/cast/CastPlayer.java index 7f675e34eb..75fd2a7d66 100644 --- a/extensions/cast/src/main/java/com/google/android/exoplayer2/ext/cast/CastPlayer.java +++ b/extensions/cast/src/main/java/com/google/android/exoplayer2/ext/cast/CastPlayer.java @@ -31,6 +31,7 @@ import com.google.android.exoplayer2.PlaybackParameters; import com.google.android.exoplayer2.Player; import com.google.android.exoplayer2.Timeline; import com.google.android.exoplayer2.audio.AudioAttributes; +import com.google.android.exoplayer2.device.DeviceInfo; import com.google.android.exoplayer2.metadata.Metadata; import com.google.android.exoplayer2.source.TrackGroup; import com.google.android.exoplayer2.source.TrackGroupArray; @@ -281,13 +282,6 @@ public final class CastPlayer extends BasePlayer { return null; } - @Override - @Nullable - public DeviceComponent getDeviceComponent() { - // TODO(b/151792305): Implement the component. - return null; - } - @Override public Looper getApplicationLooper() { return Looper.getMainLooper(); @@ -665,6 +659,40 @@ public final class CastPlayer extends BasePlayer { return ImmutableList.of(); } + /** This method is not supported and always returns {@link DeviceInfo#UNKNOWN}. */ + @Override + public DeviceInfo getDeviceInfo() { + return DeviceInfo.UNKNOWN; + } + + /** This method is not supported and always returns {@code 0}. */ + @Override + public int getDeviceVolume() { + return 0; + } + + /** This method is not supported and always returns {@code false}. */ + @Override + public boolean isDeviceMuted() { + return false; + } + + /** This method is not supported and does nothing. */ + @Override + public void setDeviceVolume(int volume) {} + + /** This method is not supported and does nothing. */ + @Override + public void increaseDeviceVolume() {} + + /** This method is not supported and does nothing. */ + @Override + public void decreaseDeviceVolume() {} + + /** This method is not supported and does nothing. */ + @Override + public void setDeviceMuted(boolean muted) {} + // Internal methods. // Call deprecated callbacks. diff --git a/library/common/src/main/java/com/google/android/exoplayer2/Player.java b/library/common/src/main/java/com/google/android/exoplayer2/Player.java index edc29a2057..7e6aaece17 100644 --- a/library/common/src/main/java/com/google/android/exoplayer2/Player.java +++ b/library/common/src/main/java/com/google/android/exoplayer2/Player.java @@ -206,51 +206,6 @@ public interface Player { void clearVideoTextureView(@Nullable TextureView textureView); } - /** The device component of a {@link Player}. */ - interface DeviceComponent { - - /** Adds a listener to receive device events. */ - void addDeviceListener(DeviceListener listener); - - /** Removes a listener of device events. */ - void removeDeviceListener(DeviceListener listener); - - /** Gets the device information. */ - DeviceInfo getDeviceInfo(); - - /** - * Gets the current volume of the device. - * - *

For devices with {@link DeviceInfo#PLAYBACK_TYPE_LOCAL local playback}, the volume - * returned by this method varies according to the current {@link C.StreamType stream type}. The - * stream type is determined by {@link AudioAttributes#usage} which can be converted to stream - * type with {@link Util#getStreamTypeForAudioUsage(int)}. - * - *

For devices with {@link DeviceInfo#PLAYBACK_TYPE_REMOTE remote playback}, the volume of - * the remote device is returned. - */ - int getDeviceVolume(); - - /** Gets whether the device is muted or not. */ - boolean isDeviceMuted(); - - /** - * Sets the volume of the device. - * - * @param volume The volume to set. - */ - void setDeviceVolume(int volume); - - /** Increases the volume of the device. */ - void increaseDeviceVolume(); - - /** Decreases the volume of the device. */ - void decreaseDeviceVolume(); - - /** Sets the mute state of the device. */ - void setDeviceMuted(boolean muted); - } - /** * Listener of changes in player state. * @@ -1130,10 +1085,6 @@ public interface Player { @Nullable VideoComponent getVideoComponent(); - /** Returns the component of this player for playback device, or null if it's not supported. */ - @Nullable - DeviceComponent getDeviceComponent(); - /** * Returns the {@link Looper} associated with the application thread that's used to access the * player and on which player events are received. @@ -1810,4 +1761,39 @@ public interface Player { /** Returns the current {@link Cue Cues}. This list may be empty. */ List getCurrentCues(); + + /** Gets the device information. */ + DeviceInfo getDeviceInfo(); + + /** + * Gets the current volume of the device. + * + *

For devices with {@link DeviceInfo#PLAYBACK_TYPE_LOCAL local playback}, the volume returned + * by this method varies according to the current {@link C.StreamType stream type}. The stream + * type is determined by {@link AudioAttributes#usage} which can be converted to stream type with + * {@link Util#getStreamTypeForAudioUsage(int)}. + * + *

For devices with {@link DeviceInfo#PLAYBACK_TYPE_REMOTE remote playback}, the volume of the + * remote device is returned. + */ + int getDeviceVolume(); + + /** Gets whether the device is muted or not. */ + boolean isDeviceMuted(); + + /** + * Sets the volume of the device. + * + * @param volume The volume to set. + */ + void setDeviceVolume(int volume); + + /** Increases the volume of the device. */ + void increaseDeviceVolume(); + + /** Decreases the volume of the device. */ + void decreaseDeviceVolume(); + + /** Sets the mute state of the device. */ + void setDeviceMuted(boolean muted); } diff --git a/library/common/src/main/java/com/google/android/exoplayer2/device/DeviceListener.java b/library/common/src/main/java/com/google/android/exoplayer2/device/DeviceListener.java index 3d35c6ad54..ba273884b9 100644 --- a/library/common/src/main/java/com/google/android/exoplayer2/device/DeviceListener.java +++ b/library/common/src/main/java/com/google/android/exoplayer2/device/DeviceListener.java @@ -15,9 +15,7 @@ */ package com.google.android.exoplayer2.device; -import com.google.android.exoplayer2.Player; - -/** A listener for changes of {@link Player.DeviceComponent}. */ +/** A listener for changes of {@link DeviceInfo} or device volume. */ public interface DeviceListener { /** Called when the device information changes. */ diff --git a/library/core/src/main/java/com/google/android/exoplayer2/ExoPlayer.java b/library/core/src/main/java/com/google/android/exoplayer2/ExoPlayer.java index fe601312ba..99144fbddc 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/ExoPlayer.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/ExoPlayer.java @@ -28,6 +28,8 @@ import com.google.android.exoplayer2.audio.AudioSink; import com.google.android.exoplayer2.audio.AuxEffectInfo; import com.google.android.exoplayer2.audio.DefaultAudioSink; import com.google.android.exoplayer2.audio.MediaCodecAudioRenderer; +import com.google.android.exoplayer2.device.DeviceInfo; +import com.google.android.exoplayer2.device.DeviceListener; import com.google.android.exoplayer2.metadata.MetadataOutput; import com.google.android.exoplayer2.metadata.MetadataRenderer; import com.google.android.exoplayer2.source.DefaultMediaSourceFactory; @@ -260,6 +262,51 @@ public interface ExoPlayer extends Player { void removeMetadataOutput(MetadataOutput output); } + /** The device component of an {@link ExoPlayer}. */ + interface DeviceComponent { + + /** Adds a listener to receive device events. */ + void addDeviceListener(DeviceListener listener); + + /** Removes a listener of device events. */ + void removeDeviceListener(DeviceListener listener); + + /** Gets the device information. */ + DeviceInfo getDeviceInfo(); + + /** + * Gets the current volume of the device. + * + *

For devices with {@link DeviceInfo#PLAYBACK_TYPE_LOCAL local playback}, the volume + * returned by this method varies according to the current {@link C.StreamType stream type}. The + * stream type is determined by {@link AudioAttributes#usage} which can be converted to stream + * type with {@link Util#getStreamTypeForAudioUsage(int)}. + * + *

For devices with {@link DeviceInfo#PLAYBACK_TYPE_REMOTE remote playback}, the volume of + * the remote device is returned. + */ + int getDeviceVolume(); + + /** Gets whether the device is muted or not. */ + boolean isDeviceMuted(); + + /** + * Sets the volume of the device. + * + * @param volume The volume to set. + */ + void setDeviceVolume(int volume); + + /** Increases the volume of the device. */ + void increaseDeviceVolume(); + + /** Decreases the volume of the device. */ + void decreaseDeviceVolume(); + + /** Sets the mute state of the device. */ + void setDeviceMuted(boolean muted); + } + /** * The default timeout for calls to {@link #release} and {@link #setForegroundMode}, in * milliseconds. @@ -613,6 +660,10 @@ public interface ExoPlayer extends Player { @Nullable MetadataComponent getMetadataComponent(); + /** Returns the component of this player for playback device, or null if it's not supported. */ + @Nullable + DeviceComponent getDeviceComponent(); + /** * Adds a listener to receive audio offload events. * diff --git a/library/core/src/main/java/com/google/android/exoplayer2/ExoPlayerImpl.java b/library/core/src/main/java/com/google/android/exoplayer2/ExoPlayerImpl.java index 5373174482..eb9b538293 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/ExoPlayerImpl.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/ExoPlayerImpl.java @@ -29,6 +29,7 @@ import androidx.annotation.Nullable; import com.google.android.exoplayer2.PlayerMessage.Target; import com.google.android.exoplayer2.analytics.AnalyticsCollector; import com.google.android.exoplayer2.audio.AudioAttributes; +import com.google.android.exoplayer2.device.DeviceInfo; import com.google.android.exoplayer2.metadata.Metadata; import com.google.android.exoplayer2.source.MediaSource; import com.google.android.exoplayer2.source.MediaSource.MediaPeriodId; @@ -1009,6 +1010,40 @@ import java.util.concurrent.CopyOnWriteArraySet; return ImmutableList.of(); } + /** This method is not supported and always returns {@link DeviceInfo#UNKNOWN}. */ + @Override + public DeviceInfo getDeviceInfo() { + return DeviceInfo.UNKNOWN; + } + + /** This method is not supported and always returns {@code 0}. */ + @Override + public int getDeviceVolume() { + return 0; + } + + /** This method is not supported and always returns {@link false}. */ + @Override + public boolean isDeviceMuted() { + return false; + } + + /** This method is not supported and does nothing. */ + @Override + public void setDeviceVolume(int volume) {} + + /** This method is not supported and does nothing. */ + @Override + public void increaseDeviceVolume() {} + + /** This method is not supported and does nothing. */ + @Override + public void decreaseDeviceVolume() {} + + /** This method is not supported and does nothing. */ + @Override + public void setDeviceMuted(boolean muted) {} + private int getCurrentWindowIndexInternal() { if (playbackInfo.timeline.isEmpty()) { return maskingWindowIndex; diff --git a/library/core/src/main/java/com/google/android/exoplayer2/SimpleExoPlayer.java b/library/core/src/main/java/com/google/android/exoplayer2/SimpleExoPlayer.java index 66432dfc88..f714475e7a 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/SimpleExoPlayer.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/SimpleExoPlayer.java @@ -82,7 +82,7 @@ public class SimpleExoPlayer extends BasePlayer Player.VideoComponent, ExoPlayer.TextComponent, ExoPlayer.MetadataComponent, - Player.DeviceComponent { + ExoPlayer.DeviceComponent { /** The default timeout for detaching a surface from the player, in milliseconds. */ public static final long DEFAULT_DETACH_SURFACE_TIMEOUT_MS = 2_000; diff --git a/testutils/src/main/java/com/google/android/exoplayer2/testutil/StubExoPlayer.java b/testutils/src/main/java/com/google/android/exoplayer2/testutil/StubExoPlayer.java index 29a57a89c9..7c5b9fa0db 100644 --- a/testutils/src/main/java/com/google/android/exoplayer2/testutil/StubExoPlayer.java +++ b/testutils/src/main/java/com/google/android/exoplayer2/testutil/StubExoPlayer.java @@ -28,6 +28,7 @@ import com.google.android.exoplayer2.PlayerMessage; import com.google.android.exoplayer2.SeekParameters; import com.google.android.exoplayer2.Timeline; import com.google.android.exoplayer2.audio.AudioAttributes; +import com.google.android.exoplayer2.device.DeviceInfo; import com.google.android.exoplayer2.metadata.Metadata; import com.google.android.exoplayer2.source.MediaSource; import com.google.android.exoplayer2.source.ShuffleOrder; @@ -444,6 +445,41 @@ public class StubExoPlayer extends BasePlayer implements ExoPlayer { throw new UnsupportedOperationException(); } + @Override + public DeviceInfo getDeviceInfo() { + throw new UnsupportedOperationException(); + } + + @Override + public int getDeviceVolume() { + throw new UnsupportedOperationException(); + } + + @Override + public boolean isDeviceMuted() { + throw new UnsupportedOperationException(); + } + + @Override + public void setDeviceVolume(int volume) { + throw new UnsupportedOperationException(); + } + + @Override + public void increaseDeviceVolume() { + throw new UnsupportedOperationException(); + } + + @Override + public void decreaseDeviceVolume() { + throw new UnsupportedOperationException(); + } + + @Override + public void setDeviceMuted(boolean muted) { + throw new UnsupportedOperationException(); + } + @Override public void setForegroundMode(boolean foregroundMode) { throw new UnsupportedOperationException();