[Android] createtrack_l

Source: Internet
Author: User
<span id="Label3"></p><p><p>When analyzing audiotrack, The first step is to new Audiotrack and calls his set Method. The createtrack_l is called at the end of the set method to create the audio track. We will now analyze the createtrack_l process.</p></p><p><p>Before analyzing createtrack_l, Let's look at the route from PCM to output of the Android audio Stream. First of all, our PCM audio data will generally be on the client side, and the mix will be on the Audioflinger side, so the PCM data needs to be transmitted to audioflinger, so a piece of memory needs to be developed for data transmission; after the audioflinger, the data You can adjust the volume of the PCM data, add sound effects, and so on (that is, mix), so you also need a piece of memory for sound processing, the buffer has been opened in the getoutput, and the PCM data can be output to the audio device for playback when the mix is Complete.</p></p><p><p></p></p><p><p></p></p><p><p>The main task of creattrack_l is to create the audio track, which is to open up the memory of the data transfer. The implementation is to create a piece of share buffer, which can be audiotrack written, and can be Audioflinger read to Mix.</p></p><p><p>Createtrack total can be divided into three steps:</p></p> <ol> <ol> <li>Get the parameters you need to create sharebuffer, such as latency,framecount,samplerate, from audioflinger, and then compare them with the incoming parameters (framecount,samplerate). The aim is to calculate the correct framecount</li> <li>Create a buffer from Audioflinger and create an object that controls Sharebuffer Audiotrackserverproxy</li> <li>Create an object that can control Sharebuffer Audiotrackclientproxy</li> </ol> </ol><p><p></p></p>1. Get the correct framecount<p><p>Audiotrack get Framecount in the following ways</p></p><pre class="brush: cpp; auto-links: true; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;">status_t audiotrack::createtrack_l (status = audiosystem::getlatency (output, streamtype, &aflatency); Status = Audiosystem::getframecount (output, streamtype, &afframecount); Status = Audiosystem::getsamplingrate (output, streamtype, &afsamplerate); If (!AUDIO_IS_LINEAR_PCM (format)) {if (sharedbuffer! = 0) {//same comment as below about ignoring FRA Mecount parameter for set () Framecount = Sharedbuffer->size (); } else if (framecount = = 0) {framecount = afframecount; } If (mnotificationframesact! = Framecount) {mnotificationframesact = framecount; }} else If (sharedbuffer! = 0) {//user share buffer,we donot neet to allocate//ensure that buffer al Ignment matches channel count//8-bit data in shared memory are not currently supported by Audioflinger size _t Alignment =/* format = = audio_format_pcm_8_bit? 1: */2; If (mchannelcount > 1) {alignment <<= 1; } if ((size_t) sharedbuffer->pointer () & (alignment-1))! = 0) {return bad_value; } framecount = Sharedbuffer->size ()/mchannelcount/sizeof (int16_t); } else if (! ( Flags & Audio_output_flag_fast)) {//non-fast uint32_t minbufcount = 2; If (minbufcount <= Nbuffering) {minbufcount = nbuffering; }//calculate buffer size by param from audioflinger size_t minframecount = (afframecount*samplerate*minbuf Count)/afsamplerate; if (framecount = = 0) {framecount = minframecount; } else if (framecount < Minframecount) {framecount = minframecount; }} Else {//for Fast tracks, the frame count calculations and checks is done by server}</pre><p><p></p></p><p><p>First look at the audiotrack when calculating framecount:</p></p><pre class="brush: cpp; auto-links: true; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;"><pre class="brush: cpp; auto-links: true; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;">Minframecount = (afframecount*samplerate*minbufcount)/afsamplerate;</pre></pre><p><p>Both Afframecount and afsamplerate are two parameters obtained from Audioflinger.</p></p> <ol> <ol> <li>Afframecount represents the size of mixerbuffer, which is a frame. Frame is defined as a "number of sampled * tracks" for PCM audio Data.</li> <li>Afsamplerate represents the default sample rate for mixerbuffer, which is the number of frames contained within a second.</li> </ol> </ol><p><p>therefore, The following formula is Available:</p></p><p align="center"><p align="center">$BufferSeconds = \frac{afframecount}{afsamplerate} = \frac{framecount}{samplerate}$</p></p><p><p>Calculates how many seconds of audio data is contained in Buffer.</p></p><p><p>Here is a buffer instance, although the sample rate is generally 44100, but in order to facilitate the drawing, the following 5 instead</p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p>Audioflinger gets the afframecount process as Follows:</p></p><pre class="brush: cpp; auto-links: true; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;">audioflinger.cppsize_t audioflinger::framecount (audio_io_handle_t output) const{return thread->framecount ();} Thread.hvirtual size_t framecount () const {return mnormalframecount;} Thread.cppvoid Audioflinger::P laybackthread::readoutputparameters () {mframecount = moutput->stream-> Common.get_buffer_size (&moutput->stream->common)/mframesize; Mnormalframecount = multiplier * mframecount;} Audio_hw.c#define short_period_size 512static size_t out_get_buffer_size_low_latency (const struct Audio_stream * Stream) {struct tuna_stream_out *out = (struct tuna_stream_out *) stream; /* take resampling to account and return the closest majoring multiple of frames, as Audioflinger expects audio bu Ffers to be a multiple of frames. Note:we Use the default rate here from Pcm_config_tones.rate. */size_t size = (short_period_size * default_out_sampling_rate)/pcm_config_tones.rate; Size = ((size + 15)/16) * 16; Return size * AUdio_stream_frame_size ((struct Audio_stream *) stream); </pre><p><p></p></p><p><p>The process of obtaining and afsamplerate is as Follows:</p></p><pre class="brush: cpp; auto-links: true; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;"><pre class="brush: cpp; auto-links: true; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;">audioflinger.cppuint32_t audioflinger::samplerate (audio_io_handle_t output) const{ return thread-> Samplerate ();} thread.huint32_t samplerate () const {return msamplerate;} Thread.cpp where sample rate is initializedvoid Audioflinger::P laybackthread::readoutputparameters () { Msamplerate = Moutput->stream->common.get_sample_rate (&moutput->stream->common);} Audio_hw.c#define default_out_sampling_rate 44100//48000 is possible, interacts poorly with hdmistatic uint32_t out _get_sample_rate (const struct Audio_stream *stream) { return default_out_sampling_rate;}</pre></pre><p><p></p></p><p><p>The minframecount contains the minbuffercount, which is the size of the share buffer with the number of mixer buffer</p></p><pre class="brush: cpp; auto-links: true; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;"><pre class="brush: cpp; auto-links: true; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;"> The client ' s Audiotrack buffer is divided to n parts for purpose of wakeup by server, where // n = 1 fast Track Nbuffering is ignored // n = 2 normal track, No sample rate conversion // n = 3 normal track, with Sample rate Conversion // (pessimistic; some non-1:1 conversion ratios don ' t actually need Triple-buffering) <c 12 />// n > 3 Very high latency or very small notification interval; nbuffering is ignored</pre></pre><p><p></p></p> <ol> <ol> <li>If flag is specified as fast track when the set method is called, the data in the audio buffer is expected to be processed as soon as possible, so buffer is created relatively small, with a single buffer</li> <li>In general, the sample rate of the input PCM audio data is the same as the sample rate of the output audio data, it is not necessary to convert the sample rate, using double buffer</li> <li>In cases where the sample rate conversion is required, The triple buffer is used</li> <li>In cases of high latency, such as the inability of the hardware to output PCM audio in time, a larger buffer is required to cache the data</li> </ol> </ol><p><p></p></p><p><p></p></p>2. Audioflinger Create share buffer<p><p>Audiotrack is implemented by calling Audioflinger's Createtrack method to create share Buffer. The steps for Createtrack are as Follows:</p></p> <ol> <ol> <li>Get Output Thread Playbackthread</li> <li>Call the createtrack_l function of the acquired Playbackthread to create a track object that creates a share buffer inside the track object</li> <li>Creates a binder object for track Trackhandle,track because it needs to be returned to Audiotrack by binder, It is a binder object that contains share buffer information</li> </ol> </ol><pre class="brush: cpp; auto-links: true; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;"><pre class="brush: cpp; auto-links: true; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;">Sp<iaudiotrack> Audioflinger::createtrack (...) { playbackthread *thread = checkplaybackthread_l (output); Track = thread->createtrack_l (client, streamtype, samplerate, format, channelmask, framecount, sharedbuffer, lsessionid, flags, tid, clientuid, &lstatus); Trackhandle = new Trackhandle (track); Return trackhandle;}</pre></pre><p><p></p></p>①. Get output thread Playbackthread<p><p>Remember the playbackthread you created when you were getoutput? Playbackthread will be created together when the Mixerthread is Created. Within the getoutput, we put the thread in the mplaybackthreads for Maintenance. Now we have to take it Out.</p></p><pre class="brush: cpp; auto-links: true; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;"><pre class="brush: cpp; auto-links: true; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;">Audioflinger::P laybackthread *audioflinger::checkplaybackthread_l (audio_io_handle_t output) const{ return Mplaybackthreads.valuefor (output). Get ();}</pre></pre><p><p></p></p>②. calling Playbackthread's createtrack_l<p><p>New track was called within createtrack_l to create share buffer</p></p><pre class="brush: cpp; auto-links: true; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;"><pre class="brush: cpp; auto-links: true; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;">Sp<audioflinger::P laybackthread::track> Audioflinger::P laybackthread::createtrack_l (...) {track = new track (this, client, streamtype, samplerate, format, channelmask, framecount, sharedbuffer, sessionId, uid, *flags);}</pre></pre><p><p></p></p><p><p>The parent class of track is trackbase, so the Trackbase object is built first</p></p><pre class="brush: cpp; auto-links: true; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;">Trackbase constructor must is called with Audioflinger::mlock heldaudioflinger::threadbase::trackbase::trackbase ( ...) {//buffer header size_t size = sizeof (audio_track_cblk_t); Buffer content size size_t buffersize = (sharedbuffer = = 0? Roundup (framecount): framecount) * mframesize; if (sharedbuffer = = 0) {size + = buffersize; } If (client! = 0) {//allocate share buffer mcblkmemory = client->heap ()->allocate (size); If (mcblkmemory! = 0) {mcblk = static_cast<audio_track_cblk_t *> (mcblkmemory->pointer ()); Can ' t assume mcblk! = NULL} else {aloge ("not enough memory for Audiotrack size=%u", size); Client->heap ()->dump ("audiotrack"); Return }} else {//this syntax avoids calling the audio_track_cblk_t constructor twice mcblk = (AUDIO_TRACK_CB lk_t *) New uint8_t[size]; Assume mcblk! = NULL}//constructThe shared structure In-place. If (mcblk! = NULL) {//this is header above buffer content new (mcblk) audio_track_cblk_t (); Clear all buffers Mcblk->framecount_ = framecount; if (sharedbuffer = = 0) {mbuffer = (char*) mcblk + sizeof (audio_track_cblk_t); memset (mbuffer, 0, buffersize); } else {mbuffer = Sharedbuffer->pointer (); } }}</pre><p><p>The created buffer needs to contain the share buffer that holds audio PCM data, and it needs to contain the buffer header of Audio_track_cblk_t. Call Heap->allocate This function to create a share Buffer,buffer head call to new (mcblk) audio_track_cblk_t; The structure of the buffer is as Follows:</p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p>New track creates a audiotrackserverproxy in the constructor body, which is used as a buffer operation on the Audioflinger side, because share buffer is cross-threading, even cross-process, The proxy can guarantee the thread safety of buffer access.</p></p><pre class="brush: cpp; auto-links: true; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;"><pre class="brush: cpp; auto-links: true; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;">Audioflinger::P laybackthread::track::track ({ maudiotrackserverproxy = new Audiotrackserverproxy (mCblk, mBuffer , framecount,mframesize); Mserverproxy = maudiotrackserverproxy;}</pre></pre><p><p></p></p><p><p></p></p>③. Creating Trackhandle<p><p>Since share buffer is not only read on the Audioflinger side, It is also written on the Audiotrack side, so the track created will need to be routed back to Audiotrack. The transfer of objects between binders is only binder objects, so the Binder object Trackhandle needs to be constructed and returned to Audiotrack.</p></p><pre class="brush: cpp; auto-links: true; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;"><pre class="brush: cpp; auto-links: true; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;">Sp<iaudiotrack> Audioflinger::createtrack (...) { trackhandle = new Trackhandle (track);} Trackhandle is a bnbinder objectclass trackhandle:public android::bnaudiotrack {...}</pre></pre><p><p></p></p><p><p>At this point, createtrack_l in Audioflinger this end of the work is basically completed.</p></p><p><p></p></p><p><p></p></p>3. Create Clientproxy<p><p>There are serverproxy, and correspondingly clientproxy,audiotrackclientproxy are classes that can operate on the audiotrack side of the track (share buffer).</p></p><p><p>After returning Trackhandle from the createtrack of audioflinger, the track information can be obtained through the relevant functions of trackhandle, such as the starting address of Buffer. Use this information to construct Audiotrackclientproxy.</p></p><pre class="brush: cpp; auto-links: true; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;"><pre class="brush: cpp; auto-links: true; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;">status_t audiotrack::createtrack_l (...) { sp<iaudiotrack> track = Audioflinger->createtrack (...); sp<imemory> Imem = track->getcblk (); audio_track_cblk_t* cblk = static_cast<audio_track_cblk_t*> (imem->pointer ()); Mproxy = new Audiotrackclientproxy (cblk, buffers, framecount, mframesizeaf);}</pre></pre><p><p></p></p><p><p></p></p>4. Summary<p><p>finally, summarize the relationships among the Objects.</p></p><p><p>Audioflinger:</p></p> <ul> <ul> <li>first, the creation of buffer and the maintenance of buffer pointers are included in the Audioflinger side creation Track,track</li> <li>There is a Audiotrackserverproxy member object inside the track that is used to perform the related operation of the buffer</li> <li>Trackhandle is a binder instance of the track object that is used to return the binder to Audiotrack</li> </ul> </ul><p><p>Audiotrack:</p></p> <ul> <ul> <li>Iaudiotrack is the class Trackhandle in Audiotrack retransmitting, which is used to provide information about the buffer to Audiotrackclientproxy</li> <li>After Audiotrackclientproxy obtains the buffer information, the buffer can be associated with the operation</li> </ul> </ul><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p>The overall flow of createtrack_l is as Follows:</p></p><p><p></p></p><p><p></p></p><p><p>[Android] createtrack_l</p></p></span>

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.