Android Multimedia Framework Summary (iii) MediaPlayer in Create to Setdatasource process

Source: Internet
Author: User

Reprint please the head source link and the tail two-dimensional code reproduced together, this article from: http://blog.csdn.net/hejjunlin/article/details/52392430

Preface: The MediaPlayer framework of the previous article, for each module relationship, must first from the core class MediaPlayer roll out, the same look at today's agenda:

    • MediaPlayer from create to Setdisplay timing diagram
    • MediaPlayer the Create process
    • The Setdatasource process of MediaPlayer
    • The Setdisplay process of MediaPlayer

Today's analysis is from MediaPlayer creation to MediaPlayer calling the Setdatasource process
In the past always put the timing diagram in the final summary, some people feel that there is no general, not to start, so, the timing diagram is attached, step by step to the timing diagram to see each stage of the process of experience

MediaPlayer from creation to Setdisplay timing diagram

MediaPlayer time series Diagram One (Create->setdatasource, later in the article also, for the time being named):

MediaPlayer the creation process

When externally calling Mediaplayer.create (this, "Http://www.xxx.mp4"), enter the creation process of our MediaPlayer:

The above code can be summarized as follows: When MediaPlayer creates the player through create, the internal new MediaPlayer object, and Setdatasource, and do the prepare action. At this point, only the next start method must be called, audio and video resources will be able to play up.

Usually we use it by constructing MediaPlayer, and then setdatasource,prepare our own operations. Whichever comes first through new MediaPlayer (), let's look at what's being done in the construct:

Note Here is a EventHandler, although today is not the point to say, we can first understand it:

Next look at how the native layer creates a mediaplayer, before we say native_up, we notice that the general load so is in the static code block, there is a static block of code in MediaPlayer for the initial JNI correlation, earlier than the construction method, Executes when the class is loaded. General is the global data, variables, can be placed in this. Here is the load media_jni.so file.

To get started with android_media_mediaplayer.cpp analysis, the first method is to native_init the Java static code block into:

is called by the native layer, is the reverse call, only used EventHandler post event back to the main thread, many with the beginning of post, basically post to the main thread, with a soft reference to the native MediaPlayer, so that the native code is safe, When MediaPlayer can be released in native, the implementation part of the transfer to Java is as follows:

The above is the Native_init method, you can see, is to do some preparatory work, get some methods, some of the member variables to use. And then back to what I said before, create in the MediaPlayer constructor, there is a native_setup, in Android_media_mediaplayer.cpp find the corresponding method:

You can see that some callback listener are set and the MediaPlayer object in C + + is created.

The Setdatasource process of MediaPlayer

Above is the construction process of MediaPlayer: After the construction of the next to set the data source, and then to the Setdatasource operation, we look at what the Setdatasource do:


Let's see if the URI in the sent Setdatasource is the file type:

Starting into the JNI layer and discovering that the Android_media_mediaplayer_setdatasource method cannot be found, you can see that there is a method name corresponding to the mapping method declaration:

Above this method name mapping, if read jnienv * source words, to above these are not unfamiliar, nothing is also mapping, do not affect our analysis, here we go to find Android_media_mediaplayer_ SETDATASOURCEFD this function to see:

Then look at the Process_media_player_call method:

The above code summarizes: when Mp->setdatasource (FD, offset, length) method gets status after, the various states are notify. There is an exception to the direct throw, so that it does not affect the execution of MediaPlayer behind the process.
Next, take a look at the Nativesetdatasource method that corresponds to the Java layer in HTTP/RTSP to JNI:

In JNI, the mapping table can correspond to android_media_mediaplayer_setdatasourceandheaders:

By this, the Setdatasource process is complete. Note here that there are two points, one from the java->jni->c++ forward call process (the front from Java to native is the forward process), and one is the Java layer process, C + +, JNI, such as Mp->setdatasource (Httpservice, Pathstr, headersvector.size () > 0 &headersvector:null), then someone would have asked? What are the benefits of this callback?

    • Security, encapsulated in the native layer code in so form, the risk of breaking the ring is small
    • Fast speed, C + + execution time is fast, and the underlying is C + + write. The native is the best choice for complex rendering and time-efficient requirements.
    • Connectivity, the forward call passes the value in, and the reverse call handles the value notification back. Equivalent to a pipe.
The Setdisplay process of MediaPlayer

Next look at the Setdatasource after the start of Mp.setdisplay (holder)
Mediaplayer.java-Setdisplay

For 2, the corresponding method is also found in Android_media_mediaplayer.cpp:



Here are a few concepts to understand:

  • Surfacetexture:surfacetexture is a new class that is added from Android3.0 (API 11). This class is similar to Surfaceview and can get the image stream from the video decode. However, unlike Surfaceview, Surfacetexture does not need to be displayed after it receives the image stream. Surfacetexture does not need to be displayed on the screen, so we can use Surfacetexture to receive the image stream from decode, and then take a copy of the image frame from the surfacetexture to process, After processing, send another surfaceview for display.
  • Surface: The surface in the native buffer,android that is sorted by the screen is a place to draw (graphics) or images (image), and for view and its subclasses, it's on surface, Each surface object is synthesized to framebuffer by surfaceflinger, and each surface is double-buffered (actually two threads, one render thread, one UI update thread), It has a backbuffer and a frontbuffer,surface to create a canvas object that manages surface drawing operations, canvas for bitmap, and storage for content on your surface.
  • Surfaceview: This can often be said to be used to display images in Camera,mediarecorder,mediaplayer. Surfaceview is a subclass of view, implements the Parcelable interface and implements the Parcelable interface, which contains a surface,surfaceview that is designed for drawing to control the format and size of the surface. And where surface is drawn. It can be understood that surface is the place where data is managed, and Surfaceview is where the data is presented.
  • Surfaceholder: As the name implies, a container for managing Surfaceholder. Surfaceholder is an interface that can be understood as a surface listener.
    Monitor the creation of surface with callback method Addcallback (Surfaceholder.callback Callback)
    By getting the canvas object in your surface and locking it. The resulting Canvas object
    After you've modified the data in your surface, release the sync Lock and submit the new image data by changing the state and image of your surface.
  • And the final synthesis: Surfaceview calls the Getholder method, you can get the current Surfaceview in the corresponding surfaceholder,surfaceholder surface to begin to manage the operation of surface. It's better to understand the MVC pattern. M:surface (image data), V:surfaceview (image display), C:surfaceholder (image Data management). The Setdisplay action in Mediaplayer.java is to set pre-settings for the video that will be displayed.
    The above is the setdisplay process, the last line in Java Setdisplay, is to get ready for updates with the surface of the JNI return.

The first time to get blog update reminders, as well as more Android dry, source code Analysis , Welcome to follow my public number, sweep the bottom QR code or long press to identify two-dimensional code, you can pay attention to.

If you feel good, easy to praise, but also to the author's affirmation, can also share this public number to you more people, original not easy

Android Multimedia Framework Summary (iii) MediaPlayer in Create to Setdatasource process

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.