How does MediaCodec find the codec inside the Android framework?

I am trying to understand how MediaCodec is used for hardware decoding.

My knowledge of Android is very limited.

Here are my findings:

There is an XML file that represents the details of the codec in the Android system.

device/ti/omap3evm/media_codecs.xml for an example. 

This means that if we create a codec from a Java application with Media Codec

 MediaCodec codec = MediaCodec.createDecoderByType(type); 

It should find the appropriate encoder using the xml file.

What am I doing?

I am trying to figure out which part of the code is reading xml and finding a codec based on this type.

1) Application Level:

  MediaCodec codec = MediaCodec.createDecoderByType(type); 

2) MediaCodec.java β†’ [ frameworks / base / media / java / android / media / MediaCodec.java ]

  public static MediaCodec createDecoderByType(String type) { return new MediaCodec(type, true /* nameIsType */, false /* encoder */); } 

3)

 private MediaCodec( String name, boolean nameIsType, boolean encoder) { native_setup(name, nameIsType, encoder); --> JNI Call. } 

4) JNI implementation β†’ [ frameworks / base / media / jni / android_media_MediaCodec.cpp ]

 static void android_media_MediaCodec_native_setup (..) { ....... const char *tmp = env->GetStringUTFChars(name, NULL); sp<JMediaCodec> codec = new JMediaCodec(env, thiz, tmp, nameIsType, encoder); ---> Here } 

from frameworks / base / media / jni / android _media_MediaCodec.cpp

  JMediaCodec::JMediaCodec( ..) { .... mCodec = MediaCodec::CreateByType(mLooper, name, encoder); //Call goes to libstagefright .... } sp<MediaCodec> MediaCodec::CreateByType( const sp<ALooper> &looper, const char *mime, bool encoder) { sp<MediaCodec> codec = new MediaCodec(looper); if (codec->init(mime, true /* nameIsType */, encoder) != OK) { --> HERE. return NULL; } return codec; } status_t MediaCodec::init(const char *name, bool nameIsType, bool encoder) { // MediaCodec } 

This stream amazes me. If someone points out how to do this, that will help a lot.

thanks.

+7
android android-internet stagefright openmax opencore
source share
1 answer

Phased flow transition.

  • MediaCodec::CreateByType will create a new MediaCodec object

    Constructor
  • MediaCodec will create a new ACodec object and save it as mCodec

  • When MediaCodec::init is called, it internally instructs the underlying ACodec to allocate the OMX component through mCodec->initiateAllocateComponent .

  • ACodec::initiateAllocateComponent will call onAllocateComponent

  • ACodec::UninitializedState::onAllocateComponent will call OMXCodec::findMatchingCodecs to find codecs matching the MIME type passed from the caller.

  • In OMXCodec::findMatchingCodecs there is a call to get an instance of MediaCodecList as MediaCodecList::getInstance() .

  • In MediaCodecList::getInstance there is a check for the presence of an existing MediaCodecList or a new MediaCodecList object .

  • In the constructor of MediaCodecList there is a call to parseXMLFile with a file name like /etc/media_codecs.xml .

  • parseXMLFile reads the contents and stores different component names, etc. in MediaCodecList , which can be used for any other codec instance. A helper function used for parsing is startElementHandler . The function of interest may be addMediaCodec .

Using these steps, the contents of the XML file are converted to a list that can be used by any other module. MediaCodecList displayed at the Java level, as it is here.

I skipped a few transitions in which MediaCodec and ACodec use messages to actually communicate and call methods, but the stream presented should give a good idea of ​​the underlying mechanism.

+10
source share

All Articles