Commit cc1ad85c by jbibik Committed by Tofunmi Adigun-Hameed

Allow ExoPlayer to opt into volume device control, forbidden by default

PiperOrigin-RevId: 532136692
(cherry picked from commit 4518dbfdc8e6d6228d2a39e0db4bb1b6245e15d7)
parent 7ebf05c3
......@@ -467,6 +467,7 @@ public interface ExoPlayer extends Player {
@C.WakeMode /* package */ int wakeMode;
/* package */ boolean handleAudioBecomingNoisy;
/* package */ boolean skipSilenceEnabled;
/* package */ boolean deviceVolumeControlEnabled;
@C.VideoScalingMode /* package */ int videoScalingMode;
@C.VideoChangeFrameRateStrategy /* package */ int videoChangeFrameRateStrategy;
/* package */ boolean useLazyPreparation;
......@@ -894,6 +895,20 @@ public interface ExoPlayer extends Player {
}
/**
* Sets whether the player is allowed to set, increase, decrease or mute device volume.
*
* @param deviceVolumeControlEnabled Whether controlling device volume is enabled.
* @return This builder.
* @throws IllegalStateException If {@link #build()} has already been called.
*/
@CanIgnoreReturnValue
public Builder setDeviceVolumeControlEnabled(boolean deviceVolumeControlEnabled) {
checkState(!buildCalled);
this.deviceVolumeControlEnabled = deviceVolumeControlEnabled;
return this;
}
/**
* Sets the {@link C.VideoScalingMode} that will be used by the player.
*
* <p>The scaling mode only applies if a {@link MediaCodec}-based video {@link Renderer} is
......
......@@ -157,7 +157,7 @@ import java.util.concurrent.TimeoutException;
private final FrameMetadataListener frameMetadataListener;
private final AudioBecomingNoisyManager audioBecomingNoisyManager;
private final AudioFocusManager audioFocusManager;
private final StreamVolumeManager streamVolumeManager;
@Nullable private final StreamVolumeManager streamVolumeManager;
private final WakeLockManager wakeLockManager;
private final WifiLockManager wifiLockManager;
private final long detachSurfaceTimeoutMs;
......@@ -217,6 +217,7 @@ import java.util.concurrent.TimeoutException;
private long maskingWindowPositionMs;
@SuppressLint("HandlerLeak")
@SuppressWarnings("deprecation") // Control flow for old volume commands
public ExoPlayerImpl(ExoPlayer.Builder builder, @Nullable Player wrappingPlayer) {
constructorFinished = new ConditionVariable();
try {
......@@ -295,17 +296,17 @@ import java.util.concurrent.TimeoutException;
COMMAND_GET_TRACKS,
COMMAND_GET_AUDIO_ATTRIBUTES,
COMMAND_GET_VOLUME,
COMMAND_GET_DEVICE_VOLUME,
COMMAND_SET_VOLUME,
COMMAND_SET_DEVICE_VOLUME,
COMMAND_SET_DEVICE_VOLUME_WITH_FLAGS,
COMMAND_ADJUST_DEVICE_VOLUME,
COMMAND_ADJUST_DEVICE_VOLUME_WITH_FLAGS,
COMMAND_SET_VIDEO_SURFACE,
COMMAND_GET_TEXT,
COMMAND_RELEASE)
.addIf(
COMMAND_SET_TRACK_SELECTION_PARAMETERS, trackSelector.isSetParametersSupported())
.addIf(COMMAND_GET_DEVICE_VOLUME, builder.deviceVolumeControlEnabled)
.addIf(COMMAND_SET_DEVICE_VOLUME, builder.deviceVolumeControlEnabled)
.addIf(COMMAND_SET_DEVICE_VOLUME_WITH_FLAGS, builder.deviceVolumeControlEnabled)
.addIf(COMMAND_ADJUST_DEVICE_VOLUME, builder.deviceVolumeControlEnabled)
.addIf(COMMAND_ADJUST_DEVICE_VOLUME_WITH_FLAGS, builder.deviceVolumeControlEnabled)
.build();
availableCommands =
new Commands.Builder()
......@@ -370,9 +371,13 @@ import java.util.concurrent.TimeoutException;
audioBecomingNoisyManager.setEnabled(builder.handleAudioBecomingNoisy);
audioFocusManager = new AudioFocusManager(builder.context, eventHandler, componentListener);
audioFocusManager.setAudioAttributes(builder.handleAudioFocus ? audioAttributes : null);
streamVolumeManager =
new StreamVolumeManager(builder.context, eventHandler, componentListener);
streamVolumeManager.setStreamType(Util.getStreamTypeForAudioUsage(audioAttributes.usage));
if (builder.deviceVolumeControlEnabled) {
streamVolumeManager =
new StreamVolumeManager(builder.context, eventHandler, componentListener);
streamVolumeManager.setStreamType(Util.getStreamTypeForAudioUsage(audioAttributes.usage));
} else {
streamVolumeManager = null;
}
wakeLockManager = new WakeLockManager(builder.context);
wakeLockManager.setEnabled(builder.wakeMode != C.WAKE_MODE_NONE);
wifiLockManager = new WifiLockManager(builder.context);
......@@ -988,7 +993,9 @@ import java.util.concurrent.TimeoutException;
keepSessionIdAudioTrack = null;
}
audioBecomingNoisyManager.setEnabled(false);
streamVolumeManager.release();
if (streamVolumeManager != null) {
streamVolumeManager.release();
}
wakeLockManager.setStayAwake(false);
wifiLockManager.setStayAwake(false);
audioFocusManager.release();
......@@ -1410,7 +1417,10 @@ import java.util.concurrent.TimeoutException;
if (!Util.areEqual(this.audioAttributes, newAudioAttributes)) {
this.audioAttributes = newAudioAttributes;
sendRendererMessage(TRACK_TYPE_AUDIO, MSG_SET_AUDIO_ATTRIBUTES, newAudioAttributes);
streamVolumeManager.setStreamType(Util.getStreamTypeForAudioUsage(newAudioAttributes.usage));
if (streamVolumeManager != null) {
streamVolumeManager.setStreamType(
Util.getStreamTypeForAudioUsage(newAudioAttributes.usage));
}
// Queue event only and flush after updating playWhenReady in case both events are triggered.
listeners.queueEvent(
EVENT_AUDIO_ATTRIBUTES_CHANGED,
......@@ -1692,13 +1702,21 @@ import java.util.concurrent.TimeoutException;
@Override
public int getDeviceVolume() {
verifyApplicationThread();
return streamVolumeManager.getVolume();
if (streamVolumeManager != null) {
return streamVolumeManager.getVolume();
} else {
return 0;
}
}
@Override
public boolean isDeviceMuted() {
verifyApplicationThread();
return streamVolumeManager.isMuted();
if (streamVolumeManager != null) {
return streamVolumeManager.isMuted();
} else {
return false;
}
}
/**
......@@ -1708,13 +1726,17 @@ import java.util.concurrent.TimeoutException;
@Override
public void setDeviceVolume(int volume) {
verifyApplicationThread();
streamVolumeManager.setVolume(volume, C.VOLUME_FLAG_SHOW_UI);
if (streamVolumeManager != null) {
streamVolumeManager.setVolume(volume, C.VOLUME_FLAG_SHOW_UI);
}
}
@Override
public void setDeviceVolume(int volume, @C.VolumeFlags int flags) {
verifyApplicationThread();
streamVolumeManager.setVolume(volume, flags);
if (streamVolumeManager != null) {
streamVolumeManager.setVolume(volume, flags);
}
}
/**
......@@ -1724,13 +1746,17 @@ import java.util.concurrent.TimeoutException;
@Override
public void increaseDeviceVolume() {
verifyApplicationThread();
streamVolumeManager.increaseVolume(C.VOLUME_FLAG_SHOW_UI);
if (streamVolumeManager != null) {
streamVolumeManager.increaseVolume(C.VOLUME_FLAG_SHOW_UI);
}
}
@Override
public void increaseDeviceVolume(@C.VolumeFlags int flags) {
verifyApplicationThread();
streamVolumeManager.increaseVolume(flags);
if (streamVolumeManager != null) {
streamVolumeManager.increaseVolume(flags);
}
}
/**
......@@ -1740,13 +1766,17 @@ import java.util.concurrent.TimeoutException;
@Override
public void decreaseDeviceVolume() {
verifyApplicationThread();
streamVolumeManager.decreaseVolume(C.VOLUME_FLAG_SHOW_UI);
if (streamVolumeManager != null) {
streamVolumeManager.decreaseVolume(C.VOLUME_FLAG_SHOW_UI);
}
}
@Override
public void decreaseDeviceVolume(@C.VolumeFlags int flags) {
verifyApplicationThread();
streamVolumeManager.decreaseVolume(flags);
if (streamVolumeManager != null) {
streamVolumeManager.decreaseVolume(flags);
}
}
/**
......@@ -1756,13 +1786,17 @@ import java.util.concurrent.TimeoutException;
@Override
public void setDeviceMuted(boolean muted) {
verifyApplicationThread();
streamVolumeManager.setMuted(muted, C.VOLUME_FLAG_SHOW_UI);
if (streamVolumeManager != null) {
streamVolumeManager.setMuted(muted, C.VOLUME_FLAG_SHOW_UI);
}
}
@Override
public void setDeviceMuted(boolean muted, @C.VolumeFlags int flags) {
verifyApplicationThread();
streamVolumeManager.setMuted(muted, flags);
if (streamVolumeManager != null) {
streamVolumeManager.setMuted(muted, flags);
}
}
@Override
......@@ -2786,10 +2820,10 @@ import java.util.concurrent.TimeoutException;
}
}
private static DeviceInfo createDeviceInfo(StreamVolumeManager streamVolumeManager) {
private static DeviceInfo createDeviceInfo(@Nullable StreamVolumeManager streamVolumeManager) {
return new DeviceInfo.Builder(DeviceInfo.PLAYBACK_TYPE_LOCAL)
.setMinVolume(streamVolumeManager.getMinVolume())
.setMaxVolume(streamVolumeManager.getMaxVolume())
.setMinVolume(streamVolumeManager != null ? streamVolumeManager.getMinVolume() : 0)
.setMaxVolume(streamVolumeManager != null ? streamVolumeManager.getMaxVolume() : 0)
.build();
}
......
......@@ -52,6 +52,7 @@ public class TestExoPlayerBuilder {
private @MonotonicNonNull Looper looper;
private long seekBackIncrementMs;
private long seekForwardIncrementMs;
private boolean deviceVolumeControlEnabled;
public TestExoPlayerBuilder(Context context) {
this.context = context;
......@@ -65,6 +66,7 @@ public class TestExoPlayerBuilder {
}
seekBackIncrementMs = C.DEFAULT_SEEK_BACK_INCREMENT_MS;
seekForwardIncrementMs = C.DEFAULT_SEEK_FORWARD_INCREMENT_MS;
deviceVolumeControlEnabled = false;
}
/**
......@@ -280,6 +282,18 @@ public class TestExoPlayerBuilder {
return this;
}
/**
* Sets the variable controlling player's ability to get/set device volume.
*
* @param deviceVolumeControlEnabled Whether the player can get/set device volume.
* @return This builder.
*/
@CanIgnoreReturnValue
public TestExoPlayerBuilder setDeviceVolumeControlEnabled(boolean deviceVolumeControlEnabled) {
this.deviceVolumeControlEnabled = deviceVolumeControlEnabled;
return this;
}
/** Returns the seek forward increment used by the player. */
public long getSeekForwardIncrementMs() {
return seekForwardIncrementMs;
......@@ -320,7 +334,8 @@ public class TestExoPlayerBuilder {
.setUseLazyPreparation(useLazyPreparation)
.setLooper(looper)
.setSeekBackIncrementMs(seekBackIncrementMs)
.setSeekForwardIncrementMs(seekForwardIncrementMs);
.setSeekForwardIncrementMs(seekForwardIncrementMs)
.setDeviceVolumeControlEnabled(deviceVolumeControlEnabled);
if (mediaSourceFactory != null) {
builder.setMediaSourceFactory(mediaSourceFactory);
}
......
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