Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Difference between MediaEncoder and MediaCodec (useMediaRecorder option in EncoderWrapper) #551

Open
mrousavy opened this issue Aug 1, 2023 · 5 comments

Comments

@mrousavy
Copy link

mrousavy commented Aug 1, 2023

Hey!

Sorry for asking this here, not sure if this is the right place - but what exactly does the useMediaRecorder option in EncoderWrapper do?
What is the difference between the MediaEncoder and the MediaCodec implementation? I see some difference in handling HDR formats, does one support HDR better?
Is one more efficient than the other?
Or is one simply not available with certain API levels?

Thanks!

@borgerag
Copy link
Contributor

borgerag commented Aug 9, 2023

MediaRecorder is a simpler interface for encoding videos. It handles the boilerplate threading for you in exchange for less flexibility. In most cases it's the interface you'll want to use.

MediaCodec gives you more control over when frames get encoded - see EncoderWrapper.EncoderThread. The code there is not particularly interesting. You'd use this if you have some sort of unusual real time constraints and want direct control over how to drop frames / reduce the bitrate etc.

Neither is intrinsically more efficient - that will depend on the use case. MediaCodec was added in API level 16 whereas MediaRecorder existed from the beginning, but API level 16 is old at this point anyway.

Right now only the MediaRecorder path handles audio.

@mrousavy
Copy link
Author

Gotcha, thanks for the insights.

I'm using OpenGL (+Skia) to draw to my Camera Frames in realtime to apply filters. I can configure this pipeline to also output to the MediaRecorder Surface, but I read on some stackoverflow posts that the MediaRecorder is often a bit weird with this and a stabler approach might be to use MediaCodec. Wdyt?

Also, I need to be able to flip the Camera while recording, so it might happen at some point that different sized Frames are coming in than what it was originally configured to, but I think I'll just force resize them in my OpenGL pipeline to not confuse it... lol.

@borgerag
Copy link
Contributor

Would CameraX meet your use case needs? Per the CameraX team, you should be able to create a media3 effect for your filter and CameraX will handle the preview / encoding split. Then you won't have to care about which one to use, CameraX will choose the right configuration for you.

@mrousavy
Copy link
Author

Unfortunately not, I've been using CameraX but it was too limiting for my use-cases.

Stuff like flipping cameras while recording, fully customizing the pipelines, etc just made it impossible to use CameraX for me.

Also I want to draw using OpenGL + Skia, as this is targeting a cross-platform environment with iOS, and on iOS the Skia drawing already works perfectly.

On Android Camera2 I got it to draw onto a SurfaceView, but I haven't quite figured out how to draw to a MediaRecorder, maybe I need to use MediaCodec for this and draw to that Surface w/ the OpenGL pipeline.

@mrousavy
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants