Skip to content

Instantly share code, notes, and snippets.

@wangchauyan
Created February 19, 2017 13:33
Show Gist options
  • 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;
}
@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