Audiotrack and audioflinger exchange audio data

Source: Internet
Author: User

Audiotrack and audioflinger exchange audio data (1)

Reproduced from: http://www.eoeandroid.com/forum.php? MoD = viewthread & tid = 98290.

In the Audio Subsystem of the android framework, each audio stream corresponds to an audiotrack instance. Each audiotrack is registered to audioflinger at creation, audioflinger is used to mix all audiotracks and then deliver them to audiohardware for playback. Currently, froyo of Android allows you to create up to 32 audio streams at the same time. That is to say, mixer can process up to 32 audiotrack data streams simultaneously.

How to Use audiotrack
The main code of audiotrack is in frameworks/base/Media/libmedia/audiotrack. cpp. Now let's take an example to learn how to use audiotrack. tonegenerator is an implementation of Telephone Dialing and other tone waveforms in Android. We will take it as an example:

The initialization function of tonegenerator:

bool ToneGenerator::initAudioTrack() { // Open audio track in mono, PCM 16bit, default sampling rate, default buffer size mpAudioTrack = new AudioTrack(); mpAudioTrack->set (mStreamType, 0, AudioSystem::PCM_16_BIT, AudioSystem::CHANNEL_OUT_MONO, 0, 0, audioCallback, this , 0, 0, mThreadCanCallJava); if (mpAudioTrack->initCheck() != NO_ERROR) { LOGE("AudioTrack->initCheck failed" ); goto initAudioTrack_exit; } mpAudioTrack->setVolume(mVolume, mVolume); mState = TONE_INIT; } 

It can be seen that the creation process is very simple. First, a new audiotrack instance is created, and then the set member function is called to complete parameter settings and register it in audioflinger, then, you can call other functions such as setting the volume to further set audio parameters. An important parameter is audiocallback. audiocallback is a callback function responsible for responding
Audiotrack notifications, such as filling data, loop playback, and playing position triggering. The callback function is usually written as follows:

Void tonegenerator: audiocallback (INT event, void * user, void * info) {If (event! = Audiotrack: event_more_data) return; audiotrack: Buffer * buffer = static_cast <audiotrack: Buffer *> (Info); tonegenerator * lptonegen = static_cast <tonegenerator *> (User ); short * lpout = buffer-> I16; unsigned int lnumsmp = buffer-> size/sizeof (short); const tonedescriptor * lptonedesc = lptonegen-> mptonedesc; if (buffer-> size = 0) return; // clear output buffer: wavegenerator accumulates into lpout buffer memset (lpout, 0, buffer-> size ); // The following code generates the tone data ....}

This function first determines whether the event type is event_more_data. If yes, the subsequent code will fill in the corresponding audio data and return it. Of course, you can handle other events. The following are available event types.

Java code:

enum event_type { EVENT_MORE_DATA = 0, // Request to write more data to PCM buffer. EVENT_UNDERRUN = 1, // PCM buffer underrun occured. EVENT_LOOP_END = 2, // Sample loop end was reached; playback restarted from loop start if loop count was not 0. EVENT_MARKER = 3, // Playback head is at the specified marker position (See setMarkerPosition()). EVENT_NEW_POS = 4, // Playback head is at a new position (See setPositionUpdatePeriod()). EVENT_BUFFER_END = 5 // Playback head is at the end of the buffer. }; 

For Start and Stop playback, you can simply call start () and stop () to mpaudiotrack-> Start ();

Post link http://www.eoeandroid.com/thread-98297-1-1.html for audiotrack and audioflinger switching audio data series (II)

Audiotrack and audioflinger exchange audio data (2)

Generally, audiotrack and audioflinger are not in the same process. They are connected through the Binder Mechanism in Android.
Audioflinger is a service in Android and has been loaded at Android startup.

We can understand this as follows:
Audio_track_cblk_t implements a circular FIFO;
Audiotrack is a FIFO data producer;
Audioflinger is a FIFO data consumer.

Establishing a connection
Explain the process:
The framework or Java layer uses JNI, new audiotrack ();
Getoutput () is called in a series of ways based on streamtype and other parameters ();
If necessary, audioflinger enables different hardware devices based on streamtype;
Audioflinger creates a mixing thread: mixerthread () for the output device, and returns the ID of this thread as the return value of getoutput ()

Audiotrack;
Audiotrack calls audioflinger's createtrack () through the Binder Mechanism ();
Audioflinger registers the audiotrack to mixerthread;
Audioflinger creates a trackhandle for control and uses the iaudiotrack interface as the return value of createtrack;
Audiotrack uses the iaudiotrack interface to obtain the FIFO (audio_track_cblk_t) created in audioflinger );
Audiotrack creates its own monitoring thread: audiotrackthread;

Since then, audiotrack has established all contact work with audioflinger. Next, audiotrack can:
Use the iaudiotrack interface to control the status of the audio track, such as start, stop, and pause;
Writes data to the FIFO to enable continuous audio playback;
Monitors the occurrence of events and interacts with user programs through the audiocallback callback function;

FIFO management
Audio_track_cblk_t
Audio_track_cblk_t is the key to implement FIFO. This structure is applied for memory by audioflinger during createtrack, and then returns audiotrack through the imemory interface, in this way, audiotrack and audioflinger manage the same audio_track_cblk_t through which audio data is written to the FIFO, and audioflinger manages the same audio_track_cblk_t.
Audio Data is read and sent to audiohardware for playback after being mixer.

The main data member of audio_track_cblk_t:
User -- audiotrack current write location offset
Userbase -- the reference position of the audiotrack write offset. The real FIFO address pointer can be determined based on the user value.
Server -- audioflinger current read location offset
Serverbase -- the reference location of the audioflinger read offset. The real FIFO address pointer can be determined based on the server value.
Framecount -- the size of the FIFO, in the unit of audio data frames. The size of each 16-bit audio frame is 2 bytes.
Buffers -- the starting address pointing to the FIFO address
Out -- audio stream direction. For audiotrack, out = 1, for audiorecord, out = 0

The main member functions of audio_track_cblk_t:
Framesavailable_l () and framesavailable () are used to obtain the size of the writable free space in the first-in-first-out (only the difference between locking and non-locking.



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.