-
-
Save wangchauyan/8c8fe2697d9131fd683e3fe9aa59dd4b to your computer and use it in GitHub Desktop.
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; | |
} |
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. :)
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..
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 :)
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 at1920, 1080
withvideo/avc
andOMX.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 runningMediaCodec
on the device, like the camera) until I restart the device.