Skip to content

Instantly share code, notes, and snippets.

@wangchauyan
Created February 19, 2017 13:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wangchauyan/8c8fe2697d9131fd683e3fe9aa59dd4b to your computer and use it in GitHub Desktop.
Save wangchauyan/8c8fe2697d9131fd683e3fe9aa59dd4b to your computer and use it in GitHub Desktop.
Check How Many MediaCodec Instance Your Device Support
public static int getMaxCodecInstanceByName(String name) {
final Vector<MediaCodec> codecs = new Vector<>();
final MediaFormat format = MediaFormat.createVideoFormat(MIME_TYPE, 1920, 1080);
MediaCodec codec = null;
for (int i = 0; i < max; i++) {
try {
codec = MediaCodec.createDecoderByType(MediaFormat.MIMETYPE_VIDEO_AVC);
codec.configure(format, null, null, 0);
codec.start();
codecs.add(codec);
codec = null;
}
catch (IllegalArgumentException e) {
e.printStackTrace();
break;
}
catch (IOException e) {
e.printStackTrace();
break;
}
catch (Exception e) {
e.printStackTrace();
break;
}
finally {
if (codec != null) {
codec.release();
codec = null;
}
}
}
final int actualMax = codecs.size();
for (int i = 0; i < actualMax; i++) {
codecs.get(i).release();
}
codecs.clear();
return actualMax;
}
@HBiSoft
Copy link

HBiSoft commented Feb 10, 2020

What is max in for (int i = 0; i < max; i++) { refering to?

@wangchauyan
Copy link
Author

wangchauyan commented Feb 11, 2020

What is max in for (int i = 0; i < max; i++) { refering to?

GetMaxSupportedInstances
https://developer.android.com/reference/android/media/MediaCodecInfo.CodecCapabilities.html#getMaxSupportedInstances()

@HBiSoft
Copy link

HBiSoft commented Feb 11, 2020

I've taken a different approach, see below:

I keep creating MediaCodec instances until it fails with Error 0xffffec77, then I return the amount minus the fail.
Doing it this way, I can at least run it on Lollipop >.

I think it is also worth mentioning that the video dimensions should also be taken into account. Same goes for the mime type,
because this will decided what decoder will be selected (if available).

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public static int getMaxCodecInstanceByName() {
    final MediaFormat format = MediaFormat.createVideoFormat("video/avc", 1920, 1080);
    MediaCodec codec = null;

    // Number of concurrent instances supported by the device with the given dimensions and mime type
    int supportedInstances = 0;

    // The number of instances it will try to create
    int createAmount = 30;

    // The loop will continue until it fails, then return the number of instances it
    // was able to create minus the the last attempt which failed

    for (int i = 0; i < createAmount; i++) {
        try {
            supportedInstances++;
            codec = MediaCodec.createDecoderByType(MediaFormat.MIMETYPE_VIDEO_AVC);
            codec.configure(format, null, null, 0);
            codec.start();
            codec = null;
        }
       catch (Exception e) {
            if (Objects.equals(e.getMessage(), "Error 0xffffec77")){
                return supportedInstances-1;
            }
            return supportedInstances;
        }
        finally {
            if (codec != null) {
                codec.release();
                codec = null;
            }
        }
    }

    return supportedInstances;
}

@wangchauyan
Copy link
Author

wangchauyan commented Feb 11, 2020

I think the only question for the code snap is that we don't know if the createAmount equals to 30 is reasonable or not. Cause it might exceed 30 instances (not sure, depends on the device you are testing). But in general, it would be ok to use a larger number said 128 to test how many actual instances we can create on a physical device.

It looks good to me.

@HBiSoft
Copy link

HBiSoft commented Feb 12, 2020

I doubt that there will be any device that will come close to running 30 instances of MediaCodec smoothly. Devices can barely run 2-3 instances at 1920, 1080 with video/avc and OMX.Exynos.avc.dec selected. A Software decoder might be able to create more than 30 instances, but it will not run as expected.

On a few devices that I've tested OMX.google.h264.decoder creates 20-35 instances, but none of the devices can even run 2 instances smoothly.

Running more than one is not even recommended, it can cause the decoder to block on older devices. This will result in it the user having to restart their device before it will be able to run any instances of MediaCodec. For example, on the Galaxy Tab A 2019, I was only able to create one instance. If I try to create more, MediaCodec API gets "blocked" completely (this includes any application running MediaCodec on the device, like the camera) until I restart the device.

@wangchauyan
Copy link
Author

From my experiences, it's really hard to say. But now, the createAmount would be ok because I haven't got any device that can open 30 instances for watching (1920, 1080) with video/avc and OMX.Exynos.avc.dec selected. As I mentioned, if we try to estimate if a code snap is reasonable, I would say we should use fewer magic numbers in the code.

That's why I said, it looks good to me, but the only problem for me is that I am not sure if createAmount = 30 is ok or not.
Cause the purpose of this function is trying to get the maximum creatable instance numbers back to the caller. We don't worry about if creating that many instances would run smoothly or not. :)

@HBiSoft
Copy link

HBiSoft commented Feb 12, 2020

As I mentioned, if we try to estimate if a code snap is reasonable, I would say we should use fewer magic numbers in the code.

Then who every uses this code in the future can change int createAmount = 30; to 1000, just to be sure ; )

Cause the purpose of this function is trying to get the maximum creatable instance numbers back to the caller. We don't worry about if creating that many instances would run smoothly or not. :)

I hear what you are saying - It's like buying a car, but you do not care if the car has wheels, as long as it starts.

So there is no point in any of this then, why would we then want know how many can be created if the once that are create doesn't work..

@wangchauyan
Copy link
Author

Lol, I have to admit you are right.
IMO, I just don't want to use magic numbers in the project.
Cause that might cause some accidents if other developers don't know what the number is for.
I am not saying your code is not ok or bad (I basically love that cause it's more clear), just try to figure out if there is a better way we can do :)

But anyway, you are the first one (and probably the only one) who is interested in this example and are willing to discuss it.
Really appreciate that. Thanks :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment