Package 

Class MediaEncoderEngine


  • @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 interface MediaEncoderEngine.Listener

      Receives the stop event callback to know when the videowas written (or what went wrong).

      public class MediaEncoderEngine.Controller

      A handle for MediaEncoders to pass information to this engine.All methods here can be called for multiple threads.

    • 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 void start() Asks encoders to start (each one on its own track).
      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.
      final void stop() Asks encoders to stop.
      VideoMediaEncoder getVideoEncoder() Returns the current video encoder.
      AudioMediaEncoder getAudioEncoder() Returns the current audio encoder.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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 file
        videoEncoder - video encoder to use
        audioEncoder - audio encoder to use
        maxDuration - max duration in millis
        maxSize - max size
        listener - 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 string
        data - 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.