Add renderer message to update priority

This can be used to set the new codec importance values.

PiperOrigin-RevId: 629073176
This commit is contained in:
tonihei 2024-04-29 08:15:03 -07:00 committed by Copybara-Service
parent 344fc8a8c0
commit a49b625cc5
3 changed files with 58 additions and 13 deletions

View File

@ -116,7 +116,8 @@ public interface Renderer extends PlayerMessage.Target {
MSG_SET_WAKEUP_LISTENER, MSG_SET_WAKEUP_LISTENER,
MSG_SET_VIDEO_EFFECTS, MSG_SET_VIDEO_EFFECTS,
MSG_SET_VIDEO_OUTPUT_RESOLUTION, MSG_SET_VIDEO_OUTPUT_RESOLUTION,
MSG_SET_IMAGE_OUTPUT MSG_SET_IMAGE_OUTPUT,
MSG_SET_PRIORITY
}) })
public @interface MessageType {} public @interface MessageType {}
@ -250,6 +251,13 @@ public interface Renderer extends PlayerMessage.Target {
*/ */
int MSG_SET_IMAGE_OUTPUT = 15; int MSG_SET_IMAGE_OUTPUT = 15;
/**
* The type of message that can be passed to a renderer to set its priority. The message payload
* should be an {@link Integer} instance for the priority of the renderer. See {@code C.PRIORITY_}
* constants for predefined values.
*/
int MSG_SET_PRIORITY = 16;
/** /**
* Applications or extensions may define custom {@code MSG_*} constants that can be passed to * Applications or extensions may define custom {@code MSG_*} constants that can be passed to
* renderers. These custom constants must be greater than or equal to this value. * renderers. These custom constants must be greater than or equal to this value.

View File

@ -28,6 +28,7 @@ import android.media.AudioFormat;
import android.media.MediaCodec; import android.media.MediaCodec;
import android.media.MediaCrypto; import android.media.MediaCrypto;
import android.media.MediaFormat; import android.media.MediaFormat;
import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import androidx.annotation.CallSuper; import androidx.annotation.CallSuper;
import androidx.annotation.DoNotInline; import androidx.annotation.DoNotInline;
@ -121,6 +122,7 @@ public class MediaCodecAudioRenderer extends MediaCodecRenderer implements Media
@Nullable private WakeupListener wakeupListener; @Nullable private WakeupListener wakeupListener;
private boolean hasPendingReportedSkippedSilence; private boolean hasPendingReportedSkippedSilence;
private int rendererPriority;
/** /**
* @param context A context. * @param context A context.
@ -259,6 +261,7 @@ public class MediaCodecAudioRenderer extends MediaCodecRenderer implements Media
context = context.getApplicationContext(); context = context.getApplicationContext();
this.context = context; this.context = context;
this.audioSink = audioSink; this.audioSink = audioSink;
rendererPriority = C.PRIORITY_PLAYBACK;
eventDispatcher = new EventDispatcher(eventHandler, eventListener); eventDispatcher = new EventDispatcher(eventHandler, eventListener);
audioSink.setListener(new AudioSinkListener()); audioSink.setListener(new AudioSinkListener());
} }
@ -822,11 +825,10 @@ public class MediaCodecAudioRenderer extends MediaCodecRenderer implements Media
case MSG_SET_WAKEUP_LISTENER: case MSG_SET_WAKEUP_LISTENER:
this.wakeupListener = (WakeupListener) message; this.wakeupListener = (WakeupListener) message;
break; break;
case MSG_SET_CAMERA_MOTION_LISTENER: case MSG_SET_PRIORITY:
case MSG_SET_CHANGE_FRAME_RATE_STRATEGY: rendererPriority = (int) checkNotNull(message);
case MSG_SET_SCALING_MODE: updateCodecImportance();
case MSG_SET_VIDEO_FRAME_METADATA_LISTENER: break;
case MSG_SET_VIDEO_OUTPUT:
default: default:
super.handleMessage(messageType, message); super.handleMessage(messageType, message);
break; break;
@ -938,10 +940,27 @@ public class MediaCodecAudioRenderer extends MediaCodecRenderer implements Media
if (Util.SDK_INT >= 32) { if (Util.SDK_INT >= 32) {
mediaFormat.setInteger(MediaFormat.KEY_MAX_OUTPUT_CHANNEL_COUNT, 99); mediaFormat.setInteger(MediaFormat.KEY_MAX_OUTPUT_CHANNEL_COUNT, 99);
} }
if (Util.SDK_INT >= 35) {
// TODO: b/333552477 - Use MediaFormat.KEY_IMPORTANCE once compileSdk >= 35
mediaFormat.setInteger("importance", max(0, -rendererPriority));
}
return mediaFormat; return mediaFormat;
} }
private void updateCodecImportance() {
@Nullable MediaCodecAdapter codec = getCodec();
if (codec == null) {
// If codec is null, then the importance will be set when initializing the codec.
return;
}
if (Util.SDK_INT >= 35) {
Bundle codecParameters = new Bundle();
// TODO: b/333552477 - Use MediaFormat.KEY_IMPORTANCE once compileSdk >= 35
codecParameters.putInt("importance", max(0, -rendererPriority));
codec.setParameters(codecParameters);
}
}
private void updateCurrentPosition() { private void updateCurrentPosition() {
long newCurrentPositionUs = audioSink.getCurrentPositionUs(isEnded()); long newCurrentPositionUs = audioSink.getCurrentPositionUs(isEnded());
if (newCurrentPositionUs != AudioSink.CURRENT_POSITION_NOT_SET) { if (newCurrentPositionUs != AudioSink.CURRENT_POSITION_NOT_SET) {

View File

@ -169,6 +169,7 @@ public class MediaCodecVideoRenderer extends MediaCodecRenderer
@Nullable private VideoSize reportedVideoSize; @Nullable private VideoSize reportedVideoSize;
private boolean hasEffects; private boolean hasEffects;
private boolean hasInitializedPlayback; private boolean hasInitializedPlayback;
private int rendererPriority;
private boolean tunneling; private boolean tunneling;
private int tunnelingAudioSessionId; private int tunnelingAudioSessionId;
@ -413,6 +414,7 @@ public class MediaCodecVideoRenderer extends MediaCodecRenderer
decodedVideoSize = VideoSize.UNKNOWN; decodedVideoSize = VideoSize.UNKNOWN;
tunnelingAudioSessionId = C.AUDIO_SESSION_ID_UNSET; tunnelingAudioSessionId = C.AUDIO_SESSION_ID_UNSET;
reportedVideoSize = null; reportedVideoSize = null;
rendererPriority = C.PRIORITY_PLAYBACK;
} }
// FrameTimingEvaluator methods // FrameTimingEvaluator methods
@ -782,12 +784,10 @@ public class MediaCodecVideoRenderer extends MediaCodecRenderer
videoSinkProvider.setOutputSurfaceInfo(displaySurface, checkNotNull(outputResolution)); videoSinkProvider.setOutputSurfaceInfo(displaySurface, checkNotNull(outputResolution));
} }
break; break;
case MSG_SET_AUDIO_ATTRIBUTES: case MSG_SET_PRIORITY:
case MSG_SET_AUX_EFFECT_INFO: rendererPriority = (int) checkNotNull(message);
case MSG_SET_CAMERA_MOTION_LISTENER: updateCodecImportance();
case MSG_SET_SKIP_SILENCE_ENABLED: break;
case MSG_SET_VOLUME:
case MSG_SET_WAKEUP_LISTENER:
default: default:
super.handleMessage(messageType, message); super.handleMessage(messageType, message);
} }
@ -1722,6 +1722,20 @@ public class MediaCodecVideoRenderer extends MediaCodecRenderer
} }
} }
private void updateCodecImportance() {
@Nullable MediaCodecAdapter codec = getCodec();
if (codec == null) {
// If codec is null, then the importance will be set when initializing the codec.
return;
}
if (Util.SDK_INT >= 35) {
Bundle codecParameters = new Bundle();
// TODO: b/333552477 - Use MediaFormat.KEY_IMPORTANCE once compileSdk >= 35
codecParameters.putInt("importance", max(0, -rendererPriority));
codec.setParameters(codecParameters);
}
}
private void maybeNotifyRenderedFirstFrame() { private void maybeNotifyRenderedFirstFrame() {
if (videoFrameReleaseControl.onFrameReleasedIsFirstFrame() && displaySurface != null) { if (videoFrameReleaseControl.onFrameReleasedIsFirstFrame() && displaySurface != null) {
notifyRenderedFirstFrame(); notifyRenderedFirstFrame();
@ -1852,6 +1866,10 @@ public class MediaCodecVideoRenderer extends MediaCodecRenderer
if (tunnelingAudioSessionId != C.AUDIO_SESSION_ID_UNSET) { if (tunnelingAudioSessionId != C.AUDIO_SESSION_ID_UNSET) {
configureTunnelingV21(mediaFormat, tunnelingAudioSessionId); configureTunnelingV21(mediaFormat, tunnelingAudioSessionId);
} }
if (Util.SDK_INT >= 35) {
// TODO: b/333552477 - Use MediaFormat.KEY_IMPORTANCE once compileSdk >= 35
mediaFormat.setInteger("importance", max(0, -rendererPriority));
}
return mediaFormat; return mediaFormat;
} }