Android MediaPlayer Framework UML diagram

Source: Internet
Author: User

Android Development Exchange Group: 50342056 Purpose

This paper uses a UML class diagram to explain the framework flow of MP3 file playback. The content of the following aspects:

1.UML class Diagram

How the 2.stagefrightPlayer was created;

A brief introduction to parsing and decoding 3.mp3 files

4. The relationship between producers and consumers during the playback of MP3 files;

Message mechanism for 5.openmax and stagefright frameworks

UML Class Diagram


How the Stagefrightplayer is created

To follow the UML diagram, look at the process of stagefrightplayer creation.

The beginning of the story is the Java layer MediaPlayer called the Setdatasource function (the parameter is a path), resulting in native corresponding MediaPlayer Through the binder communication mechanism in the Mediaplayerservice opened an "account", that is, the creation of client object, this object is an anonymous binder. This "account" is represented as a Imediaplayer interface in the native MediaPlayer.

The above procedure code is as follows:

Path: Frameworks/av/media/libmedia/mediaplayer.cpp:setdatasource           sp<imediaplayer> player (service-> Create (this, Maudiosessionid));            if ((No_error! = dosetretransmitendpoint (player)) | |                (No_error! = Player->setdatasource (URL, headers))) {                player.clear ();            }            Err = Attachnewplayer (player);

Once the Imediaplayer object has been created, its Setdatasource method is called. After the conversion of the binder's communication mechanism, the calling process comes to MediaPlayerService.cpp:

Code path: Frameworks/av/media/libmediaplayerservice/mediaplayerservice

status_t mediaplayerservice::client::setdatasource (const char *url, const KEYEDVECTOR&LT;STRING8, String8> *hea    DERs) {ALOGV ("Setdatasource (%s)", URL);    if (url = = NULL) return unknown_error;        if (strncmp (URL, "http.//", 7) = = 0) | |        (strncmp (URL, "https://", 8) = = 0) | | (strncmp (URL, "rtsp://", 7) = = 0))        {if (!checkpermission ("Android.permission.INTERNET")) {return permission_denied;  }} if (strncmp (URL, "content://", ten) = = 0) {//Get a filedescriptor for the content Uri and//Pass        It to the Setdatasource (FD) method String16 url16 (URL);        int fd = Android::opencontentproviderfile (URL16);            if (FD < 0) {Aloge ("couldn ' t open fd for%s", URL);        return unknown_error; } setdatasource (FD, 0, 0X7FFFFFFFFFLL);        This sets Mstatus close (FD);    return mstatus; } else {<span style= "color: #FF0000;" >player_type PlayertyPE = mediaplayerfactory::getplayertype (this, URL);</span> <span style= "color: #FF0000;" >sp<MediaPlayerBase> p = setdatasource_pre (Playertype);</span> if (p = = NULL) {return NO        _init; } setdatasource_post (P,<span style= "color: #FF0000;"        > P->setdatasource (URL, headers) </span>);    return mstatus; }}
The above section of the Code and analysis process is not related, skim it. To create a suitable player, analyze the format of the file to match a suitable player according to the format of the file. The work was handed over to Mediaplayerfactory. Many of the factories in which they are registered are used to produce a suitable player. The basic principle is to read the song file at the beginning of a byte, according to the relevant container to parse the format of the song, do the rough point of the word directly according to the suffix name to judge. This implementation depends on the vendor or component provider.

Anyway, we now assume that Mediaplayerfactory has a suitable player based on the path, and that its base class is mediaplayerbase. The default in Android is Stagefrightplayer, which inherits the Mediaplayerbase. So Stagefrightplayer was produced in the factory. Then call its setdatasource.

In fact, Stagefrightplayer is just an empty shell, the real work is awesomeplayer to do, this object is generated in the Stagefrightplayer constructor function. When you see this, be sure to look at the UML class diagram. Look at the relationship between the two of them. So this path is eventually saved inside the Awesomeplayer.

A brief introduction to parsing and decoding MP3 files

After the mediaplayer of the Java layer is set, the prepare is also called. The calling process is the same as the above set path: Mediaplayer->client->stagefrightplayer->awesomeplayer, skip table. Awesomeplayer's prepare did two things. Send an event queue to Awesomeplayer and wait for the event to complete.

The handling of the event is done in the onprepareasyncevent () function. The following work has been done:

1. Create an original audio track stream. Reads the compressed data from this stream, corresponding to the maudiotrack in the diagram.

2. Create a stream to read the PCM data. Corresponds to the maudiosource in the diagram.

Maudiotrack actually points to a mp3source, and maudiosource points to a omxcodec. Mp3source provides MP3 raw data for Omxcodec decoding, and then passes the decoded data to Audioplayer. The decoding work of Omxcodec is actually done by the specific decoding component. A omxcodec corresponds to a node Instance,node instance operation decoding component in OMX. Node instance provides messages that Calback accept components.

The blue part of the figure indicates the process of reading the original MP3 file; Green indicates the callback delivery process of the component;

The relationship between producers and consumers during the playback of MP3 files

There are two groups of producers and consumers during playback.

The first group: The producers of raw data, Mp3source; raw data consumer omxcodec;

The second group: PCM data Provider Omxcodec and PCM data consumer Audioplayer.

One of the Audioplayer is actually a middle bridge, the real PCM data consumer is audiotrack. It continuously reads the PCM data from the Audioplayer through the callback mechanism.

Message mechanisms for OpenMAX and stagefright frameworks

This section has two main lines:

1.OMXCodec How to operate the components;

2. How the message of the component is passed to Stagefrightplayer.

First, say first: Omxcodec's constructor invokes OMX's interface to create an Omxnodeinstance instance that operates the component through the Omxnodeinstance instance , while creating omxnodeinstance, it is passed to omxnodeinstance an observer omxcodecobserver. Once the omxnodeinstance receives a message from the component, it passes the message to OMXCODEC through the observer.

If OMXCODEC errors occur during decoding, Audioplayer detects a read error and passes the relevant information through Awesomeplayer to Stagefrightplayer. And then notify the native of the MediaPlayer. Finally, the MediaPlayer is post to Java via JNI.

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.