Class ShadowMediaCodec


  • @Implements(value=android.media.MediaCodec.class,
                minSdk=16,
                looseSignatures=true)
    public class ShadowMediaCodec
    extends java.lang.Object
    Implementation of MediaCodec which supports both asynchronous and synchronous modes.

    By default for any encoded required, a 1 to 1 mapping will be used between the input and output buffers. Data from a queued input buffer will be copied to the output buffer. In the case that is it necessary so simulate some form of data compression, a custom encoder or decoder can be added via addEncoder(String, CodecConfig) and addDecoder(String, CodecConfig) respectively.

    Asynchronous mode: Once the codec is started, a format change will be reported, switching to an empty MediaFormat with fake codec-specific info. Following this, the implementation will present an input buffer, which will be copied to an output buffer once queued, which will be subsequently presented to the callback handler.

    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected void __constructor__​(java.lang.String name, boolean nameIsType, boolean encoder)  
      static void addDecoder​(java.lang.String type, ShadowMediaCodec.CodecConfig config)
      Add a fake decoding codec to the Shadow.
      static void addEncoder​(java.lang.String type, ShadowMediaCodec.CodecConfig config)
      Add a fake encoding codec to the Shadow.
      static void clearCodecs()
      Clears any previously added encoders and decoders.
      protected void freeByteBuffer​(java.nio.ByteBuffer buffer)
      Prevents attempting to free non-direct ByteBuffer objects.
      protected java.nio.ByteBuffer getBuffer​(boolean input, int index)
      Returns the input or output buffer corresponding to the given index, or null if invalid.
      protected java.nio.ByteBuffer[] getBuffers​(boolean input)
      Returns the shadow buffers used for input or output.
      protected android.media.MediaFormat getOutputFormat()
      Returns a default MediaFormat if not set via getOutputFormat().
      protected void invalidateByteBuffer​(java.nio.ByteBuffer[] buffers, int index)
      Prevents calling Android-only methods on basic ByteBuffer objects.
      protected void invalidateByteBuffers​(java.nio.ByteBuffer[] buffers)
      Prevents calling Android-only methods on basic ByteBuffer objects.
      protected void native_configure​(java.lang.Object keys, java.lang.Object values, java.lang.Object surface, java.lang.Object crypto, java.lang.Object descramblerBinder, java.lang.Object flags)  
      protected void native_configure​(java.lang.String[] keys, java.lang.Object[] values, android.view.Surface surface, android.media.MediaCrypto crypto, int flags)  
      protected int native_dequeueInputBuffer​(long timeoutUs)  
      protected int native_dequeueOutputBuffer​(android.media.MediaCodec.BufferInfo info, long timeoutUs)  
      protected void native_flush()
      Flushes the available output buffers.
      protected void native_queueInputBuffer​(int index, int offset, int size, long presentationTimeUs, int flags)
      Triggers presentation of the corresponding output buffer for the given input buffer, and passes the given metadata as buffer info.
      protected void native_setCallback​(android.media.MediaCodec.Callback callback)
      Saves the callback to allow use inside the shadow.
      protected void native_start()
      Starts the async encoding process, by first reporting a format change event, and then presenting an input buffer to the callback.
      protected void revalidateByteBuffer​(java.nio.ByteBuffer[] buffers, int index)
      Prevents calling Android-only methods on basic ByteBuffer objects.
      protected void validateInputByteBuffer​(java.nio.ByteBuffer[] buffers, int index)
      Prevents calling Android-only methods on basic ByteBuffer objects.
      protected void validateOutputByteBuffer​(java.nio.ByteBuffer[] buffers, int index, android.media.MediaCodec.BufferInfo info)
      Prevents calling Android-only methods on basic ByteBuffer objects.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ShadowMediaCodec

        public ShadowMediaCodec()
    • Method Detail

      • addEncoder

        public static void addEncoder​(java.lang.String type,
                                      ShadowMediaCodec.CodecConfig config)
        Add a fake encoding codec to the Shadow.
      • addDecoder

        public static void addDecoder​(java.lang.String type,
                                      ShadowMediaCodec.CodecConfig config)
        Add a fake decoding codec to the Shadow.
      • clearCodecs

        public static void clearCodecs()
        Clears any previously added encoders and decoders.
      • __constructor__

        @Implementation
        protected void __constructor__​(java.lang.String name,
                                       boolean nameIsType,
                                       boolean encoder)
      • native_setCallback

        @Implementation(minSdk=21)
        protected void native_setCallback​(android.media.MediaCodec.Callback callback)
        Saves the callback to allow use inside the shadow.
      • native_configure

        @Implementation(minSdk=21,
                        maxSdk=25)
        protected void native_configure​(java.lang.String[] keys,
                                        java.lang.Object[] values,
                                        android.view.Surface surface,
                                        android.media.MediaCrypto crypto,
                                        int flags)
      • native_configure

        @Implementation(minSdk=26)
        protected void native_configure​(java.lang.Object keys,
                                        java.lang.Object values,
                                        java.lang.Object surface,
                                        java.lang.Object crypto,
                                        java.lang.Object descramblerBinder,
                                        java.lang.Object flags)
      • native_start

        @Implementation(minSdk=21)
        protected void native_start()
        Starts the async encoding process, by first reporting a format change event, and then presenting an input buffer to the callback.
      • native_flush

        @Implementation(minSdk=21)
        protected void native_flush()
        Flushes the available output buffers.
      • getBuffers

        @Implementation
        protected java.nio.ByteBuffer[] getBuffers​(boolean input)
        Returns the shadow buffers used for input or output.
      • getBuffer

        @Implementation(minSdk=21)
        protected java.nio.ByteBuffer getBuffer​(boolean input,
                                                int index)
        Returns the input or output buffer corresponding to the given index, or null if invalid.
      • native_dequeueInputBuffer

        protected int native_dequeueInputBuffer​(long timeoutUs)
      • native_queueInputBuffer

        @Implementation(minSdk=21)
        protected void native_queueInputBuffer​(int index,
                                               int offset,
                                               int size,
                                               long presentationTimeUs,
                                               int flags)
        Triggers presentation of the corresponding output buffer for the given input buffer, and passes the given metadata as buffer info.
      • native_dequeueOutputBuffer

        @Implementation(minSdk=21)
        protected int native_dequeueOutputBuffer​(android.media.MediaCodec.BufferInfo info,
                                                 long timeoutUs)
      • invalidateByteBuffer

        @Implementation(minSdk=21)
        protected void invalidateByteBuffer​(java.nio.ByteBuffer[] buffers,
                                            int index)
        Prevents calling Android-only methods on basic ByteBuffer objects.
      • validateInputByteBuffer

        @Implementation(minSdk=21)
        protected void validateInputByteBuffer​(java.nio.ByteBuffer[] buffers,
                                               int index)
        Prevents calling Android-only methods on basic ByteBuffer objects.
      • revalidateByteBuffer

        @Implementation(minSdk=21)
        protected void revalidateByteBuffer​(java.nio.ByteBuffer[] buffers,
                                            int index)
        Prevents calling Android-only methods on basic ByteBuffer objects.
      • validateOutputByteBuffer

        @Implementation(minSdk=21)
        protected void validateOutputByteBuffer​(java.nio.ByteBuffer[] buffers,
                                                int index,
                                                android.media.MediaCodec.BufferInfo info)
        Prevents calling Android-only methods on basic ByteBuffer objects. Replicates existing behavior adjusting buffer positions and limits.
      • invalidateByteBuffers

        @Implementation(minSdk=21)
        protected void invalidateByteBuffers​(java.nio.ByteBuffer[] buffers)
        Prevents calling Android-only methods on basic ByteBuffer objects.
      • freeByteBuffer

        @Implementation(minSdk=21)
        protected void freeByteBuffer​(java.nio.ByteBuffer buffer)
        Prevents attempting to free non-direct ByteBuffer objects.
      • getOutputFormat

        @Implementation
        protected android.media.MediaFormat getOutputFormat()
        Returns a default MediaFormat if not set via getOutputFormat().