Skip to content

Commit fe2b846

Browse files
yschimkemarcbaechinger
authored andcommitted
Expose AudioOffload track state.
Adds a new event to AudioOffloadListener to get the offload state of the track, which indicates when software decoding is taking place. PiperOrigin-RevId: 465264362
1 parent 2fbe1bb commit fe2b846

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

library/core/src/main/java/com/google/android/exoplayer2/ExoPlayer.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,16 @@ default void onExperimentalOffloadSchedulingEnabledChanged(boolean offloadSchedu
423423
* <p>This method is experimental, and will be renamed or removed in a future release.
424424
*/
425425
default void onExperimentalSleepingForOffloadChanged(boolean sleepingForOffload) {}
426+
427+
/**
428+
* Called when the value of {@link AudioTrack#isOffloadedPlayback} changes.
429+
*
430+
* <p>This should not be generally required to be acted upon. But when offload is critical for
431+
* efficiency, or audio features (gapless, playback speed), this will let the app know.
432+
*
433+
* <p>This method is experimental, and will be renamed or removed in a future release.
434+
*/
435+
default void onExperimentalOffloadedPlayback(boolean offloadedPlayback) {}
426436
}
427437

428438
/**

library/core/src/main/java/com/google/android/exoplayer2/audio/DefaultAudioSink.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import androidx.annotation.Nullable;
4040
import androidx.annotation.RequiresApi;
4141
import com.google.android.exoplayer2.C;
42+
import com.google.android.exoplayer2.ExoPlayer.AudioOffloadListener;
4243
import com.google.android.exoplayer2.Format;
4344
import com.google.android.exoplayer2.PlaybackParameters;
4445
import com.google.android.exoplayer2.analytics.PlayerId;
@@ -265,6 +266,7 @@ public static final class Builder {
265266
private boolean enableAudioTrackPlaybackParams;
266267
private int offloadMode;
267268
AudioTrackBufferSizeProvider audioTrackBufferSizeProvider;
269+
@Nullable AudioOffloadListener audioOffloadListener;
268270

269271
/** Creates a new builder. */
270272
public Builder() {
@@ -370,6 +372,19 @@ public Builder setAudioTrackBufferSizeProvider(
370372
return this;
371373
}
372374

375+
/**
376+
* Sets an optional {@link AudioOffloadListener} to receive events relevant to offloaded
377+
* playback.
378+
*
379+
* <p>The default value is null.
380+
*/
381+
@CanIgnoreReturnValue
382+
public Builder setExperimentalAudioOffloadListener(
383+
@Nullable AudioOffloadListener audioOffloadListener) {
384+
this.audioOffloadListener = audioOffloadListener;
385+
return this;
386+
}
387+
373388
/** Builds the {@link DefaultAudioSink}. Must only be called once per Builder instance. */
374389
public DefaultAudioSink build() {
375390
if (audioProcessorChain == null) {
@@ -500,6 +515,7 @@ public DefaultAudioSink build() {
500515
initializationExceptionPendingExceptionHolder;
501516
private final PendingExceptionHolder<WriteException> writeExceptionPendingExceptionHolder;
502517
private final AudioTrackBufferSizeProvider audioTrackBufferSizeProvider;
518+
@Nullable private final AudioOffloadListener audioOffloadListener;
503519

504520
@Nullable private PlayerId playerId;
505521
@Nullable private Listener listener;
@@ -660,6 +676,7 @@ private DefaultAudioSink(Builder builder) {
660676
new PendingExceptionHolder<>(AUDIO_TRACK_RETRY_DURATION_MS);
661677
writeExceptionPendingExceptionHolder =
662678
new PendingExceptionHolder<>(AUDIO_TRACK_RETRY_DURATION_MS);
679+
audioOffloadListener = builder.audioOffloadListener;
663680
}
664681

665682
// AudioSink implementation.
@@ -1087,7 +1104,12 @@ private AudioTrack buildAudioTrackWithRetry() throws InitializationException {
10871104

10881105
private AudioTrack buildAudioTrack(Configuration configuration) throws InitializationException {
10891106
try {
1090-
return configuration.buildAudioTrack(tunneling, audioAttributes, audioSessionId);
1107+
AudioTrack audioTrack =
1108+
configuration.buildAudioTrack(tunneling, audioAttributes, audioSessionId);
1109+
if (audioOffloadListener != null) {
1110+
audioOffloadListener.onExperimentalOffloadedPlayback(isOffloadedPlayback(audioTrack));
1111+
}
1112+
return audioTrack;
10911113
} catch (InitializationException e) {
10921114
if (listener != null) {
10931115
listener.onAudioSinkError(e);

0 commit comments

Comments
 (0)