Last active
December 8, 2020 15:39
-
-
Save HBiSoft/f7013e9de9ab21dfc7a1bbe908df32fb to your computer and use it in GitHub Desktop.
Get number of concurrent MediaCodec instances
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* | |
* This will return the number of `MediaCodec` instances a device can run | |
* at the same time with the given dimensions and mime type. | |
* | |
* This will only work on API 21> | |
* | |
* It keeps creating instances until Error 0xffffec77 is called | |
* It then returns the number of instances that was successfully created | |
* | |
* It can be called like this: | |
* Toast.makeText(this, String.valueOf(getMaxCodecInstances()), Toast.LENGTH_LONG).show(); | |
* | |
*/ | |
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) | |
public static int getMaxCodecInstances() { | |
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment