-
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2) public class MediaEncoderEngine
The entry point for encoding video files. The external API is simple but the internal mechanism is not easy. Basically the engine controls a MediaEncoder instance for each track (e.g. one for video, one for audio). 1. We prepare the MediaEncoders: prepare MediaEncoders can be prepared synchronously or not. 2. Someone calls start from any thread. As a consequence, we start the MediaEncoders: start. 3. MediaEncoders do not start synchronously. Instead, they call notifyStarted when they have a legit format, and we keep track of who has started. 4. When all MediaEncoders have started, we actually start the muxer. 5. Someone calls stop from any thread. As a consequence, we stop the MediaEncoders: stop. 6. MediaEncoders do not stop synchronously. Instead, they will stop reading but keep draining the codec until there's no data left. At that point, they can call notifyStopped. 7. When all MediaEncoders have been released, we actually stop the muxer and notify. There is another possibility where MediaEncoders themselves want to stop, for example because they reach some limit or constraint (e.g. max duration). For this, they should call requestStop. Once all MediaEncoders have stopped, we will actually call stop on ourselves.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description public interfaceMediaEncoderEngine.ListenerReceives the stop event callback to know when the videowas written (or what went wrong).
public classMediaEncoderEngine.ControllerA handle for MediaEncoders to pass information to this engine.All methods here can be called for multiple threads.
-
Field Summary
Fields Modifier and Type Field Description public final static intEND_BY_USERpublic final static intEND_BY_MAX_DURATIONpublic final static intEND_BY_MAX_SIZE
-
Constructor Summary
Constructors Constructor Description MediaEncoderEngine(File file, VideoMediaEncoder videoEncoder, AudioMediaEncoder audioEncoder, int maxDuration, long maxSize, MediaEncoderEngine.Listener listener)Creates a new engine for the given file, with the given encoders and max limits,and listener to receive events.
-
Method Summary
Modifier and Type Method Description final voidstart()Asks encoders to start (each one on its own track). final voidnotify(String event, Object data)Notifies encoders of some event with the given payload.Can be used for example to notify the video encoder of new frame available. final voidstop()Asks encoders to stop. VideoMediaEncodergetVideoEncoder()Returns the current video encoder. AudioMediaEncodergetAudioEncoder()Returns the current audio encoder. -
-
Constructor Detail
-
MediaEncoderEngine
MediaEncoderEngine(File file, VideoMediaEncoder videoEncoder, AudioMediaEncoder audioEncoder, int maxDuration, long maxSize, MediaEncoderEngine.Listener listener)
Creates a new engine for the given file, with the given encoders and max limits,and listener to receive events.- Parameters:
file- output filevideoEncoder- video encoder to useaudioEncoder- audio encoder to usemaxDuration- max duration in millismaxSize- max sizelistener- a listener
-
-
Method Detail
-
start
final void start()
Asks encoders to start (each one on its own track).
-
notify
final void notify(String event, Object data)
Notifies encoders of some event with the given payload.Can be used for example to notify the video encoder of new frame available.
- Parameters:
event- an event stringdata- an event payload
-
stop
final void stop()
Asks encoders to stop. This is not sync, of course we will ask for encodersto call notifyStopped before actually stop the muxer.When all encoders request a release, end is called to do cleanupand notify the listener.
-
getVideoEncoder
@NonNull() VideoMediaEncoder getVideoEncoder()
Returns the current video encoder.
-
getAudioEncoder
@Nullable() AudioMediaEncoder getAudioEncoder()
Returns the current audio encoder.
-
-
-
-