MediaCodec initialisation fails.
Issue description
On certain devices that use OMX.MTK.VIDEO.DECODER.AVC decoder, on DRM content ONLY we receive the below error.
Reproduction steps
- Initialise SimpleExoPlayer
- Prepare MediaSource with drm.
- Note, that in prepare step, we are NOT attaching SimpleExoplayerView to view hierarchy, and only doing so, after player state READY received.
- Get the crash, immediately after prepare() called.
Link to test content
"uri": "https://storage.googleapis.com/wvmedia/cenc/h264/tears/tears.mpd" "drm_license_url": "https://proxy.uat.widevine.com/proxy?provider=widevine_test"
Version of ExoPlayer being used
We can see this issue reproduced on Exoplayer 2.5.0 and above (including 2.6.1) Previous Exoplayer versions work as expected.
Device(s) and version(s) of Android being used
Device - Meizu M5C, Lenovo K4 and Moto C+ (all of them with OMX.MTK.VIDEO.DECODER.AVC) Android version - 6.0
### A full bug report captured from the device
com.google.android.exoplayer2.ExoPlaybackException
at com.google.android.exoplayer2.mediacodec.MediaCodecRenderer.throwDecoderInitError(MediaCodecRenderer.java:418)
at com.google.android.exoplayer2.mediacodec.MediaCodecRenderer.maybeInitCodec(MediaCodecRenderer.java:405)
at com.google.android.exoplayer2.mediacodec.MediaCodecRenderer.onInputFormatChanged(MediaCodecRenderer.java:839)
at com.google.android.exoplayer2.video.MediaCodecVideoRenderer.onInputFormatChanged(MediaCodecVideoRenderer.java:455)
at com.google.android.exoplayer2.mediacodec.MediaCodecRenderer.render(MediaCodecRenderer.java:536)
at com.google.android.exoplayer2.ExoPlayerImplInternal.doSomeWork(ExoPlayerImplInternal.java:560)
at com.google.android.exoplayer2.ExoPlayerImplInternal.handleMessage(ExoPlayerImplInternal.java:306)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:207)
at android.os.HandlerThread.run(HandlerThread.java:61)
Caused by: com.google.android.exoplayer2.mediacodec.MediaCodecRenderer$DecoderInitializationException: Decoder init failed: OMX.MTK.VIDEO.DECODER.AVC, Format(1, null, video/avc, 720
at com.google.android.exoplayer2.mediacodec.MediaCodecRenderer.maybeInitCodec(MediaCodecRenderer.java:405)
at com.google.android.exoplayer2.mediacodec.MediaCodecRenderer.onInputFormatChanged(MediaCodecRenderer.java:839)
at com.google.android.exoplayer2.video.MediaCodecVideoRenderer.onInputFormatChanged(MediaCodecVideoRenderer.java:455)
at com.google.android.exoplayer2.mediacodec.MediaCodecRenderer.render(MediaCodecRenderer.java:536)
at com.google.android.exoplayer2.ExoPlayerImplInternal.doSomeWork(ExoPlayerImplInternal.java:560)
at com.google.android.exoplayer2.ExoPlayerImplInternal.handleMessage(ExoPlayerImplInternal.java:306)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:207)
at android.os.HandlerThread.run(HandlerThread.java:61)
Caused by: android.media.MediaCodec$CodecException: start failed
at android.media.MediaCodec.native_start(Native Method)
at android.media.MediaCodec.start(MediaCodec.java:1898)
at com.google.android.exoplayer2.mediacodec.MediaCodecRenderer.maybeInitCodec(MediaCodecRenderer.java:397)
at com.google.android.exoplayer2.mediacodec.MediaCodecRenderer.onInputFormatChanged(MediaCodecRenderer.java:839)
at com.google.android.exoplayer2.video.MediaCodecVideoRenderer.onInputFormatChanged(MediaCodecVideoRenderer.java:455)
at com.google.android.exoplayer2.mediacodec.MediaCodecRenderer.render(MediaCodecRenderer.java:536)
at com.google.android.exoplayer2.ExoPlayerImplInternal.doSomeWork(ExoPlayerImplInternal.java:560)
at com.google.android.exoplayer2.ExoPlayerImplInternal.handleMessage(ExoPlayerImplInternal.java:306)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:207)
at android.os.HandlerThread.run(HandlerThread.java:61)
Was able to reproduce your problem on a Moto C+. It seems the device has problems switching output surfaces and needs a workaround. This is similar to issues with other devices we had in the past (e.g. #3355, #3439).
Can you provide us with the Build.DEVICE names of Meizu M5C and Lenovo K4 to add them to our workaround?
As i see you already support something related to OMX.MTK codec in your workaround. Or you talking about some other workaround? Will provide you with device info tomorrow.
Yes, the existing workaround checks decoder name and device name to be as restrictive as possible (see here). That's why I need the device names of the devices where it doesn't work to add to this list.
Here is the device names(same as appear in your Util.DEVICE) Meizu M5C - M5c Lenovo K4 Note - A7010a48
Anyway, I tried to change the codecNeedsSetOutputSurfaceWorkaround(String name), so that Meizu M5c will be included in the list of workaround needed devices. The result was the same as in reported bug. Even if I force codecNeedsSetOutputSurfaceWorkaround always return true, it is still crashing. Do you manage to reslove issue on Moto C++ by adding it to the workaround list?
Is there something more that I need to do in order to apply this workaround on Meizu?
Yes, I added "panell_s".equals(Util.DEVICE) to the workaround list and this solved the issue for the Moto C+.
Were you able to test if it works for the Lenovo K4?
First of all, that is good news, because one of the devices our customer reported on, was Moto C+. So I will check it with them. I am testing it on Meizu. Unfortunately I have no Lenovo K4 around me. As I mentioned above, even i force hardcoded codecNeedsSetOutputSurfaceWorkaround always return true and compile on Meizu it does not have any effect. Is there anything that I probably missing?
Not if it's exactly the same issue.
Unfortunately, I don't have access to neither Meizu M5c nor Lenovo K4, so it's difficult to check what may cause it. Are the reproduction steps and the error message above the same for all three devices? Or is there something else?
Yes, the flow is the same. For the matter of fact, I even dont see that exoplayer enter this code block. getCodecInfo always return null, so dummySurface never initialised.
private void setSurface(Surface surface) throws ExoPlaybackException {
if (surface == null) {
// Use a dummy surface if possible.
if (dummySurface != null) {
surface = dummySurface;
} else {
MediaCodecInfo codecInfo = getCodecInfo(); // getCodecInfo always return null, so dummySurface never initialised.
if (codecInfo != null && shouldUseDummySurface(codecInfo.secure)) {
dummySurface = DummySurface.newInstanceV17(context, codecInfo.secure);
surface = dummySurface;
}
}
}
}
maybeInitCodec() always happened only after setSurface(Surface surface) was called two times and the incoming Surface is always null. Actually, onCodecInitialized(String name, long initializedTimestampMs, long initializationDurationMs) does not happen also.
Can you provide more details on why setSurface is called two times? It's usually only called once when you attach the UI.
setSurface was called twice because we used old exoplayer api where you was supposed to call player.setSurfaceView(null) before you apply new surface (in order to clean the previous one. Now you are doing it with player.clearVideoSurfaceView() so the surface can be removed by SurfaceHolder) I updated our api to be aligned with yours. Now setSurface() called only once. But the issue still there.
Another thing that I see is that setOutputSurfaceV23(codec, surface); never happened.
surface is null, so if (this.surface != surface) will never enter the block where you actually apply the workaround.
From what I see the first time DummySurface created is in configureCodec(MediaCodecInfo codecInfo, MediaCodec codec, Format format,
MediaCrypto crypto){
}
with if (dummySurface == null) {
dummySurface = DummySurface.newInstanceV17(context, codecInfo.secure);
}
But I think it is too late, because almost immediately called codec.start(); that actually crash the player
With the workaround in place, dummySurface should never be created. The creation in configureCodec is guarded by a call to shouldInitCodec which also checks whether this workaround is needed.
When exactly do you attach the UI? I added a listener to onPlayerStateChanged and when the playback state changes to Player.STATE_READY for the first time, the UI is attached. When doing this, the codec.start method is called as a result of setSurface and not because onInputFormatChanged as in your case. There seems to be a difference in the way I try to reproduce the problem.
So in order to be maximum aligned with you I have moved to work on your demo application. I want to be sure that we dont miss anything. I did some small changes in player_activity.xml and PlayerActivity.java in order to reproduce issue on my device. Changes I made: player_activity.xml ---> I comment out this block
<com.google.android.exoplayer2.ui.SimpleExoPlayerView android:id="@+id/player_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
and added this view instead:
<FrameLayout android:id="@+id/test_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
This should simulate my parent view to which I attach player view.
In PlayerActivity In onCreate()
-
I get the reference to the test_view,
testRootView = findViewById(R.id.test_view); -
create SimpleExoplayerView programmatically
simpleExoPlayerView = new SimpleExoPlayerView(this); -
subscribe to STATE_READY and when it received add simpleExoPlayerView to testRootView
if(playbackState == Player.STATE_READY) {
testRootView.addView(simpleExoPlayerView);
}
Also I added Meizu device name in MediaCodecVideoRenederer. Workaround method now looks like that:
private static boolean codecNeedsSetOutputSurfaceWorkaround(String name) {
// Work around https://github.com/google/ExoPlayer/issues/3236,
// https://github.com/google/ExoPlayer/issues/3355 and
// https://github.com/google/ExoPlayer/issues/3439.
return (("deb".equals(Util.DEVICE) || "flo".equals(Util.DEVICE))
&& "OMX.qcom.video.decoder.avc".equals(name))
|| (("tcl_eu".equals(Util.DEVICE) || "SVP-DTV15".equals(Util.DEVICE)
|| "BRAVIA_ATV2".equals(Util.DEVICE))
&& "OMX.MTK.VIDEO.DECODER.AVC".equals(name)
|| "M5c".equals(Util.DEVICE) && "OMX.MTK.VIDEO.DECODER.AVC".equals(name));
}
From what I see from debugging the code flow looks like that:
setSurface(Surface surface)called with surface null, so we exit this function without doing anything.- We get in
onInputFormatChanged(Format newFormat)with audio codec first. so we continue tomaybeInitCodec()and exiting this function ok. - We get
onInputFormatChanged(Format newFormat)once again. This time with video codec. And going intomaybeInitCodec() shouldInitCodec()returns true.surface != nullis false butshouldUseDummySurface(codecInfo.secure);is true. So the statement is true.shouldUseDummySurface()returns true. Here all the statements return true exceptDummySurface.isSecureSupported(context)that returns false. but because of || sign it is enough that!codecIsSecurereturn true for this statement to be also true.- So then we going into
configureCodec()(all the workaround flags inmaybeInitCodec()are set to false). inconfigureCodec()the surface is null. Assertion not throwing exception. dummySurface instantiated.
dummySurface = DummySurface.newInstanceV17(context, codecInfo.secure);
- And from there we continue to
codec.start();and Boom! exception thrown.
You say that dummySurface should never be created in a proper workaround mode. But in my case it happens to work in exact that way. Also the codec.start is called not from setSurface. Any idea for reason why it can happen? Note, that I in purpose reproduce this issue in your demo app with your drm content in order to be maximum aligned to your workspace. exoplayer branch is release-v2
It will be grate, if you add Meizu and Lenovo K4 devices to the list of excluded devices and push it on some side branch, that I can check if this fix apply for them. I am quite sure, that there is something that I am missing.
Here is the device names(same as appear in your Util.DEVICE) Meizu M5C - M5c Lenovo K4 Note - A7010a48
Thanks a lot for this detailed analysis! Not many people make this effort to help us debug a problem :)
It turns out that there is a difference I didn't know about (thanks to @ojw28 for pointing this out). The original fix for other devices (#3439) included some further changes to prevent the DummySurface from being instantiated. Please have a look at this commit to see the details. I guess it will work when you add this change to your code (especially that shoulduseDummySurface also checks for the workaround).
I'll add the Meizu M5C and the Lenovo K4 to our list.
Thanks for your solution. I made the changes you suggest and it seems to work both on Meizu M5c and Lenovo K4 Note.
For some reason it did not work on Moto C+ that we tested on. When I print it Util.DEVICE, I could see that the name is slightly differ from the one you provide.
You told that it is "panell_s" , but in my case the name was "panell_p". Probably there is some minor difference in devices, and therefore they have different names. Anyway, adding "panell_p" to the device list solved the issue.
What I am wondering now, is how many devices out there that we does not know about them having this problem. Is there more general solution in plan, then just excluding device-device? Also, I observed that all of the our problematic devices have same chipset and codec (just to take it in your consideration). Maybe there we can detect some patterns and exclude devices by them and not by name.
Another thing I want to ask you about, is your release plans. I am not asking for exact date, just want to know if for now I should think about some temporary solution, or just wait for your release.
Thank you very much for your support.
Yes, there is also panell_d, panell_dl and panell_dp. Guess I better change it to include all devices starting with "panell" then.
We also discussed the same question - whether we should enable the workaround for certain API levels and decoders for all devices or something similar. Because we also don't know for sure how many devices out there have this problem. Using the chipset may be a good idea, need to think about this a little bit further.
For our release plans, there is probably a new release in the next couple of days.
Thanks again.
I was thinking about possible solutions, and came to a little bit different possible approach. If you provide some sort of Settings API that can give application (me) ability to decide if I want this workaround or not for example: player.useDummySurfaceWorkaround(true) , I can catch this specific exception once and enable workaround for all previous sessions for this device. This will release you from responsibility of tracking all the problematic devices and adding them to the endless workaround list. On my side I can also relax and forget about this bug :)
The only thing I am worried about is this exception unique for this sort of behaviour, or it can be thrown in some other circumstances?
This problem often results in the application not responding on these devices instead of an exception. Especially when switching between two real surfaces. That means we can't just always try first and enable the workaround as soon as it fails.
If you want to catch the exception and then set a flag in your app, you can still do so by overriding shouldInitCodec in MediaCodecVideoRenderer to disable initializing the dummy surface if surface == null. This extended renderer can then be used in ExoPlayer by overriding the buildVideoRenderers in DefaultRenderersFactory to return your renderer. And then pass in this renderers factory to ExoPlayerFactory.newSimpleInstance.