Send piid to the native MediaPlayer's AudioTrack

The piid is sent in the prepare and prepareAsync methods after the data source is being
set and the native variables are initialized.

Test: dumpsys audio & atest android.media.audio.cts.AudioPlaybackConfigurationTest
Bug: 235521198
Change-Id: Ie3cbdb69814ff014fafc1737462d4fa8a8f687ca
This commit is contained in:
Vlad Popa
2022-08-12 16:05:05 +02:00
parent 1ec8c40e03
commit 66824a3f9a
2 changed files with 54 additions and 13 deletions

View File

@@ -696,6 +696,13 @@ public class MediaPlayer extends PlayerBase
baseRegisterPlayer(sessionId);
}
private Parcel createPlayerIIdParcel() {
Parcel parcel = newRequest();
parcel.writeInt(INVOKE_ID_SET_PLAYER_IID);
parcel.writeInt(mPlayerIId);
return parcel;
}
/*
* Update the MediaPlayer SurfaceTexture.
* Call after setting a new display surface.
@@ -712,6 +719,7 @@ public class MediaPlayer extends PlayerBase
private static final int INVOKE_ID_DESELECT_TRACK = 5;
private static final int INVOKE_ID_SET_VIDEO_SCALE_MODE = 6;
private static final int INVOKE_ID_GET_SELECTED_TRACK = 7;
private static final int INVOKE_ID_SET_PLAYER_IID = 8;
/**
* Create a request parcel which can be routed to the native media
@@ -1309,16 +1317,26 @@ public class MediaPlayer extends PlayerBase
* @throws IllegalStateException if it is called in an invalid state
*/
public void prepare() throws IOException, IllegalStateException {
_prepare();
Parcel piidParcel = createPlayerIIdParcel();
try {
int retCode = _prepare(piidParcel);
if (retCode != 0) {
Log.w(TAG, "prepare(): could not set piid " + mPlayerIId);
}
} finally {
piidParcel.recycle();
}
scanInternalSubtitleTracks();
// DrmInfo, if any, has been resolved by now.
synchronized (mDrmLock) {
mDrmInfoResolved = true;
}
}
private native void _prepare() throws IOException, IllegalStateException;
/** Returns the result of sending the {@code piidParcel} to the MediaPlayerService. */
private native int _prepare(Parcel piidParcel) throws IOException, IllegalStateException;
/**
* Prepares the player for playback, asynchronously.
@@ -1330,7 +1348,20 @@ public class MediaPlayer extends PlayerBase
*
* @throws IllegalStateException if it is called in an invalid state
*/
public native void prepareAsync() throws IllegalStateException;
public void prepareAsync() throws IllegalStateException {
Parcel piidParcel = createPlayerIIdParcel();
try {
int retCode = _prepareAsync(piidParcel);
if (retCode != 0) {
Log.w(TAG, "prepareAsync(): could not set piid " + mPlayerIId);
}
} finally {
piidParcel.recycle();
}
}
/** Returns the result of sending the {@code piidParcel} to the MediaPlayerService. */
private native int _prepareAsync(Parcel piidParcel) throws IllegalStateException;
/**
* Starts or resumes playback. If playback had previously been paused,

View File

@@ -369,13 +369,13 @@ android_media_MediaPlayer_setVideoSurface(JNIEnv *env, jobject thiz, jobject jsu
setVideoSurface(env, thiz, jsurface, true /* mediaPlayerMustBeAlive */);
}
static void
android_media_MediaPlayer_prepare(JNIEnv *env, jobject thiz)
static jint
android_media_MediaPlayer_prepare(JNIEnv *env, jobject thiz, jobject piidParcel)
{
sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
if (mp == NULL ) {
if (mp == nullptr) {
jniThrowException(env, "java/lang/IllegalStateException", NULL);
return;
return UNKNOWN_ERROR;
}
// Handle the case where the display surface was set before the mp was
@@ -384,15 +384,20 @@ android_media_MediaPlayer_prepare(JNIEnv *env, jobject thiz)
mp->setVideoSurfaceTexture(st);
process_media_player_call( env, thiz, mp->prepare(), "java/io/IOException", "Prepare failed." );
// update the piid
Parcel *request = parcelForJavaObject(env, piidParcel);
auto reply = std::make_unique<Parcel>();
return static_cast<jint>(mp->invoke(*request, reply.get()));
}
static void
android_media_MediaPlayer_prepareAsync(JNIEnv *env, jobject thiz)
static jint
android_media_MediaPlayer_prepareAsync(JNIEnv *env, jobject thiz, jobject piidParcel)
{
sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
if (mp == NULL ) {
if (mp == nullptr) {
jniThrowException(env, "java/lang/IllegalStateException", NULL);
return;
return UNKNOWN_ERROR;
}
// Handle the case where the display surface was set before the mp was
@@ -401,6 +406,11 @@ android_media_MediaPlayer_prepareAsync(JNIEnv *env, jobject thiz)
mp->setVideoSurfaceTexture(st);
process_media_player_call( env, thiz, mp->prepareAsync(), "java/io/IOException", "Prepare Async failed." );
// update the piid
Parcel *request = parcelForJavaObject(env, piidParcel);
auto reply = std::make_unique<Parcel>();
return static_cast<jint>(mp->invoke(*request, reply.get()));
}
static void
@@ -1380,8 +1390,8 @@ static const JNINativeMethod gMethods[] = {
{"_setDataSource", "(Ljava/io/FileDescriptor;JJ)V", (void *)android_media_MediaPlayer_setDataSourceFD},
{"_setDataSource", "(Landroid/media/MediaDataSource;)V",(void *)android_media_MediaPlayer_setDataSourceCallback },
{"_setVideoSurface", "(Landroid/view/Surface;)V", (void *)android_media_MediaPlayer_setVideoSurface},
{"_prepare", "()V", (void *)android_media_MediaPlayer_prepare},
{"prepareAsync", "()V", (void *)android_media_MediaPlayer_prepareAsync},
{"_prepare", "(Landroid/os/Parcel;)I", (void *)android_media_MediaPlayer_prepare},
{"_prepareAsync", "(Landroid/os/Parcel;)I", (void *)android_media_MediaPlayer_prepareAsync},
{"_start", "()V", (void *)android_media_MediaPlayer_start},
{"_stop", "()V", (void *)android_media_MediaPlayer_stop},
{"getVideoWidth", "()I", (void *)android_media_MediaPlayer_getVideoWidth},