Data Stream encapsulation and awesomeplayer process of Android-stagefright

Source: Internet
Author: User

Encapsulation of Data Streams
I,

Mediaextractor is generated by the data source datasource.

Mediaextractor: Create (datasource. The create method generates the corresponding mediaextractor in two steps:

1. Use datasource-> sniff to detect Data Types

2. Generate the corresponding extractor:

123456789101112131415161718192021222324252627282930313233
if (!strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_MPEG4)             || !strcasecmp(mime, "audio/mp4")) {         return new MPEG4Extractor(source);     } else if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_MPEG)) {         return new MP3Extractor(source, meta);     } else if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_AMR_NB)             || !strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_AMR_WB)) {         return new AMRExtractor(source);     } else if (!strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_WAV)) {         return new WAVExtractor(source);     } else if (!strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_OGG)) {         return new OggExtractor(source);     } else if (!strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_MATROSKA)) {         return new MatroskaExtractor(source);     } else if (!strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_MPEG2TS)) {         return new MPEG2TSExtractor(source);     }

II,

Next, use the following code to separate audio and video tracks:

1234567891011
if (!haveVideo && !strncasecmp(mime, "video/", 6)) {         setVideoSource(extractor->getTrack(i));         haveVideo = true;    } else if (!haveAudio && !strncasecmp(mime, "audio/", 6)) {         setAudioSource(extractor->getTrack(i));         haveAudio = true;

Generate two mediasource: mvideotrack and maudiotrack.

III,

The mediasource we have obtained so far only has the parser function and does not have the decode function. Further packaging of these two mediasource is required:

1234567891011121314151617
mVideoSource = OMXCodec::Create(             mClient.interface(), mVideoTrack->getFormat(),             false, // createEncoder             mVideoTrack,             NULL, flags); mAudioSource = OMXCodec::Create(                 mClient.interface(), mAudioTrack->getFormat(),                 false, // createEncoder                 mAudioTrack);

This time, two mediasource instances are obtained. They have the parse and decode functions. After the mediasource. Start () method is called, it will start to obtain data from the data source and parse the data, and stop when the buffer is full. In awesomeplayer, you can call mediasource's read method to read decoded data.

For mvideosource:

Read data.

1
mVideoSource->read(&mVideoBuffer, &options)

It is handed over to the display module for rendering.

1
mVideoRenderer->render(mVideoBuffer);

For maudiosource:

Maudioplayer is used to encapsulate maudiosource, and then maudioplayer is responsible for reading data and playing control.

Awesomeplayer Process
Uri, FD

|

Datasource

|

Mediaextractor

|

Mvideotrack maudiotrack // audio and video data streams

|

Mvideosource maudiosource // Audio/Video Decoder

|

Mvideobuffer maudioplayer

The process from the source to the final decoded data is illustrated above.

1. Set datasource. There are two types of data sources: URI and FD. Uri can be http: //, RTSP: //, etc. FD is a local file descriptor. You can find the corresponding file through FD.

2. mediaextractor is generated by datasource. Use SP extractor = mediaextractor: Create (datasource. Mediaextractor: Create (datasource) creates different Data Reading Objects Based on different data content.

3. Call setvideosource to break down the audio data stream (maudiotrack) and video data stream (mvideotrack) by the mediaextractor ).

4. onprepareasyncevent ()

If datasource is a URL, obtain data based on the address and buffer the data until mvideotrack and maudiotrack are obtained.

Mvideotrack and maudiotrack call initvideodecoder () and initaudiodecoder () to generate the audio and video decoder mvideosource and maudiosource.

Then call postbufferingevent_l () to submit the event to enable the buffer. The data buffer execution function is onbufferingupdate ().

When the buffer zone has enough data to play back, call play_l () to start playing. In play_l (), the key is to call postvideoevent_l () and submit the mvideoevent. The onvideoevent () function is called when this event is executed (). This function calls mvideosource-> Read (& mvideobuffer, & options) to decode the video. Audio Decoding is implemented through maudioplayer.

After decoding, the video decoder reads a frame of data through mvideosource-> Read, stores the data in mvideobuffer, and finally sends the video data to the display module through mvideorenderer-> render (mvideobuffer.

When you need to pause or stop, call cancelplayerevents to submit the event to stop decoding. You can also choose whether to continue buffering data.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.