1ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen/*
2ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen**
3ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen** Copyright 2012, The Android Open Source Project
4ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen**
5ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen** Licensed under the Apache License, Version 2.0 (the "License");
6ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen** you may not use this file except in compliance with the License.
7ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen** You may obtain a copy of the License at
8ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen**
9ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen**     http://www.apache.org/licenses/LICENSE-2.0
10ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen**
11ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen** Unless required by applicable law or agreed to in writing, software
12ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen** distributed under the License is distributed on an "AS IS" BASIS,
13ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen** See the License for the specific language governing permissions and
15ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen** limitations under the License.
16ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen*/
17ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
18ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen#define LOG_TAG "AudioHAL:AudioStreamOut"
19ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
20ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen#include <utils/Log.h>
21ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
22ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen#include "AudioHardwareOutput.h"
23ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen#include "AudioStreamOut.h"
24ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
25ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen// Set to 1 to print timestamp data in CSV format.
26ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen#ifndef HAL_PRINT_TIMESTAMP_CSV
27ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen#define HAL_PRINT_TIMESTAMP_CSV 0
28ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen#endif
29ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
30ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen//#define VERY_VERBOSE_LOGGING
31ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen#ifdef VERY_VERBOSE_LOGGING
32ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen#define ALOGVV ALOGV
33ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen#else
34ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen#define ALOGVV(a...) do { } while(0)
35ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen#endif
36ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
37ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chennamespace android {
38ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
39ae089528c7e3a8107be65657e52276dd068c1039Mike J. ChenAudioStreamOut::AudioStreamOut(AudioHardwareOutput& owner, bool mcOut)
40ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    : mFramesPresented(0)
41e78fa3676f07529a565b6b073d8c63d14f8c383aPhil Burk    , mFramesRendered(0)
42ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    , mFramesWrittenRemainder(0)
43ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    , mOwnerHAL(owner)
44ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    , mFramesWritten(0)
45ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    , mTgtDevices(0)
46ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    , mAudioFlingerTgtDevices(0)
47ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    , mIsMCOutput(mcOut)
48ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    , mIsEncoded(false)
49282f06dc6d099dbb010cf9acf0bcb9bdee43fb8dRajat S Gupta    , mInStandby(false)
50ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    , mSPDIFEncoder(this)
51ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen{
52ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    assert(mLocalClock.initCheck());
53ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
54ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    mPhysOutputs.setCapacity(3);
55ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
56ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // Set some reasonable defaults for these.  All of this should be eventually
57ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // be overwritten by a specific audio flinger configuration, but it does not
58ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // hurt to have something here by default.
59ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    mInputSampleRate = 48000;
60ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    mInputChanMask = AUDIO_CHANNEL_OUT_STEREO;
61ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    mInputFormat = AUDIO_FORMAT_PCM_16_BIT;
62ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    mInputNominalChunksInFlight = 4;
63ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    updateInputNums();
64ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
65ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    mThrottleValid = false;
66ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
67ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    memset(&mUSecToLocalTime, 0, sizeof(mUSecToLocalTime));
68ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    mUSecToLocalTime.a_to_b_numer = mLocalClock.getLocalFreq();
69ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    mUSecToLocalTime.a_to_b_denom = 1000000;
70ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    LinearTransform::reduce(&mUSecToLocalTime.a_to_b_numer,
71ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen                            &mUSecToLocalTime.a_to_b_denom);
72ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen}
73ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
74ae089528c7e3a8107be65657e52276dd068c1039Mike J. ChenAudioStreamOut::~AudioStreamOut()
75ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen{
76ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    releaseAllOutputs();
77ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen}
78ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
79ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chenstatus_t AudioStreamOut::set(
80ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        audio_format_t *pFormat,
81ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        uint32_t *pChannels,
82ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        uint32_t *pRate)
83ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen{
84ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    Mutex::Autolock _l(mLock);
85ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    audio_format_t lFormat   = pFormat ? *pFormat : AUDIO_FORMAT_DEFAULT;
86ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    uint32_t       lChannels = pChannels ? *pChannels : 0;
87ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    uint32_t       lRate     = pRate ? *pRate : 0;
88ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
89ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // fix up defaults
90ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    if (lFormat == AUDIO_FORMAT_DEFAULT) lFormat = format();
91ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    if (lChannels == 0)                  lChannels = chanMask();
92ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    if (lRate == 0)                      lRate = sampleRate();
93ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
94ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    if (pFormat)   *pFormat   = lFormat;
95ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    if (pChannels) *pChannels = lChannels;
96ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    if (pRate)     *pRate     = lRate;
97ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
98ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    mIsEncoded = !audio_is_linear_pcm(lFormat);
99ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
100ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    if (!mIsMCOutput && !mIsEncoded) {
101ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        // If this is the primary stream out, then demand our defaults.
102ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        if ((lFormat   != format()) ||
103ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen            (lChannels != chanMask()) ||
104ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen            (lRate     != sampleRate()))
105ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen            return BAD_VALUE;
106ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    } else {
107ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        // Else check to see if our HDMI sink supports this format before proceeding.
108ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        if (!mOwnerHAL.getHDMIAudioCaps().supportsFormat(lFormat,
109ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen                                                     lRate,
110ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen                                                     audio_channel_count_from_out_mask(lChannels)))
111ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen            return BAD_VALUE;
112ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    }
113ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
114ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    mInputFormat = lFormat;
115ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    mInputChanMask = lChannels;
116ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    mInputSampleRate = lRate;
117ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    ALOGI("AudioStreamOut::set: lRate = %u, mIsEncoded = %d\n", lRate, mIsEncoded);
118ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    updateInputNums();
119ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
120ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    return NO_ERROR;
121ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen}
122ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
123ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chenvoid AudioStreamOut::setTgtDevices(uint32_t tgtDevices)
124ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen{
125ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    Mutex::Autolock _l(mRoutingLock);
126ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    if (mTgtDevices != tgtDevices) {
127ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        mTgtDevices = tgtDevices;
128ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    }
129ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen}
130ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
131ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chenstatus_t AudioStreamOut::standby()
132ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen{
133e78fa3676f07529a565b6b073d8c63d14f8c383aPhil Burk    mFramesRendered = 0;
134ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    releaseAllOutputs();
135282f06dc6d099dbb010cf9acf0bcb9bdee43fb8dRajat S Gupta    mOwnerHAL.standbyStatusUpdate(true, mIsMCOutput);
136282f06dc6d099dbb010cf9acf0bcb9bdee43fb8dRajat S Gupta    mInStandby = true;
137282f06dc6d099dbb010cf9acf0bcb9bdee43fb8dRajat S Gupta
138ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    return NO_ERROR;
139ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen}
140ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
141ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chenvoid AudioStreamOut::releaseAllOutputs() {
142ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    Mutex::Autolock _l(mRoutingLock);
143ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
144ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    ALOGI("releaseAllOutputs: releasing %d mPhysOutputs", mPhysOutputs.size());
145ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    AudioOutputList::iterator I;
146ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    for (I = mPhysOutputs.begin(); I != mPhysOutputs.end(); ++I)
147ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        mOwnerHAL.releaseOutput(*this, *I);
148ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
149ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    mPhysOutputs.clear();
150ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen}
151ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
152ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chenvoid AudioStreamOut::updateInputNums()
153ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen{
154ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    assert(mLocalClock.initCheck());
155ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
156ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // mInputBufSize determines how many audio frames AudioFlinger is going to
157ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // mix at a time.  We also use the mInputBufSize to determine the ALSA
158ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // period_size, the number of of samples which need to play out (at most)
159ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // before low level ALSA driver code is required to wake up upper levels of
160ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // SW to fill a new buffer.  As it turns out, ALSA is going to apply some
161ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // rules and modify the period_size which we pass to it.  One of the things
162ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // ALSA seems to do is attempt to round the period_size up to a value which
163ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // will make the period an integral number of 0.5 mSec.  This round-up
164ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // behavior can cause the low levels of ALSA to consume more data per period
165ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // than the AudioFlinger mixer has been told to produce.  If there are only
166ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // two buffers in flight at any given point in time, this can lead to a
167ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // situation where the pipeline ends up slipping an extra buffer and
168ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // underflowing.  There are two approaches to mitigate this, both of which
169ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // are implemented in this HAL...
170ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    //
171ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // 1) Try as hard as possible to make certain that the buffer size we choose
172ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    //    results in a period_size which is not going to get rounded up by ALSA.
173ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    //    This means that we want a buffer size which at the chosen sample rate
174ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    //    and frame size will be an integral multiple of 1/2 mSec.
175ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // 2) Increate the number of chunks we keep in flight.  If the system slips
176ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    //    a single period, its only really a problem if there is no data left in
177ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    //    the pipeline waiting to be played out.  The mixer should going to mix
178ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    //    as fast as possible until the buffer has been topped off.  By
179ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    //    decreasing the buffer size and increasing the number of buffers in
180ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    //    flight, we increase the number of interrups and mix events per second,
181ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    //    but buy ourselves some insurance against the negative side effects of
182ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    //    slipping one buffer in the schedule.  We end up using 4 buffers at
183ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    //    10mSec, making the total audio latency somewhere between 40 and 50
184ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    //    mSec, depending on when a sample begins playback relative to
185ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    //    AudioFlinger's mixing schedule.
186ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    //
187ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    mInputChanCount = audio_channel_count_from_out_mask(mInputChanMask);
188ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
189ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // Picking a chunk duration 10mSec should satisfy #1 for both major families
190ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // of audio sample rates (the 44.1K and 48K families).  In the case of 44.1
191ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // (or higher) we will end up with a multiple of 441 frames of audio per
192ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // chunk, while for 48K, we will have a multiple of 480 frames of audio per
193ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // chunk.  This will not work well for lower sample rates in the 44.1 family
194ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // (22.05K and 11.025K); it is unlikely that we will ever be configured to
195ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // deliver those rates, and if we ever do, we will need to rely on having
196ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // extra chunks in flight to deal with the jitter problem described above.
197ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    mInputChunkFrames = outputSampleRate() / 100;
198ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
199ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // FIXME: Currently, audio flinger demands an input buffer size which is a
200ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // multiple of 16 audio frames.  Right now, there is no good way to
201ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // reconcile this with ALSA round-up behavior described above when the
202ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // desired sample rate is a member of the 44.1 family.  For now, we just
203ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // round up to the nearest multiple of 16 frames and roll the dice, but
204ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // someday it would be good to fix one or the other halves of the problem
205ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // (either ALSA or AudioFlinger)
206ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    mInputChunkFrames = (mInputChunkFrames + 0xF) & ~0xF;
207ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
208ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    ALOGD("AudioStreamOut::updateInputNums: chunk size %u from output rate %u\n",
209ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        mInputChunkFrames, outputSampleRate());
210ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
211ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // Buffer size is just the frame size multiplied by the number of
212ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // frames per chunk.
213ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    mInputBufSize = mInputChunkFrames * getBytesPerOutputFrame();
214ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
215ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // The nominal latency is just the duration of a chunk * the number of
216ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // chunks we nominally keep in flight at any given point in time.
217ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    mInputNominalLatencyUSec = static_cast<uint32_t>(((
218ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen                    static_cast<uint64_t>(mInputChunkFrames)
219ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen                    * 1000000 * mInputNominalChunksInFlight)
220ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen                    / mInputSampleRate));
221ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
222ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    memset(&mLocalTimeToFrames, 0, sizeof(mLocalTimeToFrames));
223ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    mLocalTimeToFrames.a_to_b_numer = mInputSampleRate;
224ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    mLocalTimeToFrames.a_to_b_denom = mLocalClock.getLocalFreq();
225ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    LinearTransform::reduce(
226ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen            &mLocalTimeToFrames.a_to_b_numer,
227ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen            &mLocalTimeToFrames.a_to_b_denom);
228ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen}
229ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
230ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chenvoid AudioStreamOut::finishedWriteOp(size_t framesWritten,
231ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen                                     bool needThrottle)
232ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen{
233ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    assert(mLocalClock.initCheck());
234ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
235ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    int64_t now = mLocalClock.getLocalTime();
236ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
237ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    if (!mThrottleValid || !needThrottle) {
238ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        mThrottleValid = true;
239ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        mWriteStartLT  = now;
240ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        mFramesWritten = 0;
241ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    }
242ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
243ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    size_t framesWrittenAppRate;
244ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    uint32_t multiplier = getRateMultiplier();
245ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    if (multiplier != 1) {
246ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        // Accumulate round-off error from previous call.
247ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        framesWritten += mFramesWrittenRemainder;
248ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        // Scale from device sample rate to application rate.
249ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        framesWrittenAppRate = framesWritten / multiplier;
250ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        ALOGV("finishedWriteOp() framesWrittenAppRate = %d = %d / %d\n",
251ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen            framesWrittenAppRate, framesWritten, multiplier);
252ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        // Save remainder for next time to prevent error accumulation.
253ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        mFramesWrittenRemainder = framesWritten - (framesWrittenAppRate * multiplier);
254ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    } else {
255ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        framesWrittenAppRate = framesWritten;
256ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    }
257ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
258ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    mFramesWritten += framesWrittenAppRate;
259ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    mFramesPresented += framesWrittenAppRate;
260e78fa3676f07529a565b6b073d8c63d14f8c383aPhil Burk    mFramesRendered += framesWrittenAppRate;
261ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
262ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    if (needThrottle) {
263ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        int64_t deltaLT;
264ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        mLocalTimeToFrames.doReverseTransform(mFramesWritten, &deltaLT);
265ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        deltaLT += mWriteStartLT;
266ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        deltaLT -= now;
267ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
268ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        int64_t deltaUSec;
269ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        mUSecToLocalTime.doReverseTransform(deltaLT, &deltaUSec);
270ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
271ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        if (deltaUSec > 0) {
272ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen            useconds_t sleep_time;
273ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
274ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen            // We should never be a full second ahead of schedule; sanity check
275ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen            // our throttle time and cap the max sleep time at 1 second.
276821688ed8ee498455d39b6b7d5d853c16f74ae98Phil Burk            if (deltaUSec > 1000000) {
277821688ed8ee498455d39b6b7d5d853c16f74ae98Phil Burk                ALOGW("throttle time clipped! deltaLT = %lld deltaUSec = %lld",
278821688ed8ee498455d39b6b7d5d853c16f74ae98Phil Burk                    deltaLT, deltaUSec);
279ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen                sleep_time = 1000000;
280821688ed8ee498455d39b6b7d5d853c16f74ae98Phil Burk            } else {
281ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen                sleep_time = static_cast<useconds_t>(deltaUSec);
282821688ed8ee498455d39b6b7d5d853c16f74ae98Phil Burk            }
283ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen            usleep(sleep_time);
284ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        }
285ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    }
286ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen}
287ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
288ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chenstatic const String8 keyRouting(AudioParameter::keyRouting);
289ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chenstatic const String8 keySupSampleRates("sup_sampling_rates");
290ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chenstatic const String8 keySupFormats("sup_formats");
291ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chenstatic const String8 keySupChannels("sup_channels");
292ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chenstatus_t AudioStreamOut::setParameters(__unused struct audio_stream *stream, const char *kvpairs)
293ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen{
294ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    AudioParameter param = AudioParameter(String8(kvpairs));
295ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    String8 key = String8(AudioParameter::keyRouting);
296ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    int tmpInt;
297ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
298ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    if (param.getInt(key, tmpInt) == NO_ERROR) {
299ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        // The audio HAL handles routing to physical devices entirely
300ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        // internally and mostly ignores what audio flinger tells it to do.  JiC
301ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        // there is something (now or in the future) in audio flinger which
302ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        // cares about the routing value in a call to getParameters, we hang on
303ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        // to the last routing value set by audio flinger so we can at least be
304ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        // consistent when we lie to the upper levels about doing what they told
305ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        // us to do.
306ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        mAudioFlingerTgtDevices = static_cast<uint32_t>(tmpInt);
307ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    }
308ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
309ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    return NO_ERROR;
310ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen}
311ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
312ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chenchar* AudioStreamOut::getParameters(const char* k)
313ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen{
314ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    AudioParameter param = AudioParameter(String8(k));
315ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    String8 value;
316ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
317ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    if (param.get(keyRouting, value) == NO_ERROR) {
318ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        param.addInt(keyRouting, (int)mAudioFlingerTgtDevices);
319ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    }
320ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
321ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    HDMIAudioCaps& hdmiCaps = mOwnerHAL.getHDMIAudioCaps();
322ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
323ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    if (param.get(keySupSampleRates, value) == NO_ERROR) {
324ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        if (mIsMCOutput) {
325ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen            hdmiCaps.getRatesForAF(value);
326ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen            param.add(keySupSampleRates, value);
327ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        } else {
328ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen            param.add(keySupSampleRates, String8("48000"));
329ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        }
330ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    }
331ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
332ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    if (param.get(keySupFormats, value) == NO_ERROR) {
333ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        if (mIsMCOutput) {
334ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen            hdmiCaps.getFmtsForAF(value);
335ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen            param.add(keySupFormats, value);
336ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        } else {
337ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen            param.add(keySupFormats, String8("AUDIO_FORMAT_PCM_16_BIT"));
338ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        }
339ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    }
340ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
341ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    if (param.get(keySupChannels, value) == NO_ERROR) {
342ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        if (mIsMCOutput) {
3433ea12695eb0674e1f62c2afa585a047d4c81676bEric Laurent            hdmiCaps.getChannelMasksForAF(value);
344ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen            param.add(keySupChannels, value);
345ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        } else {
346ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen            param.add(keySupChannels, String8("AUDIO_CHANNEL_OUT_STEREO"));
347ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        }
348ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    }
349ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
350ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    return strdup(param.toString().string());
351ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen}
352ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
353ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chenuint32_t AudioStreamOut::getRateMultiplier() const
354ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen{
355ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    return (mIsEncoded) ? mSPDIFEncoder.getRateMultiplier() : 1;
356ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen}
357ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
358ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chenuint32_t AudioStreamOut::outputSampleRate() const
359ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen{
360ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    return mInputSampleRate * getRateMultiplier();
361ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen}
362ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
363ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chenint AudioStreamOut::getBytesPerOutputFrame()
364ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen{
365ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    return (mIsEncoded) ? mSPDIFEncoder.getBytesPerOutputFrame()
366ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        : (mInputChanCount * sizeof(int16_t));
367ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen}
368ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
369ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chenuint32_t AudioStreamOut::latency() const {
370ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    uint32_t uSecLatency = mInputNominalLatencyUSec;
371ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    uint32_t vcompDelay = mOwnerHAL.getVideoDelayCompUsec();
372ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
373ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    if (uSecLatency < vcompDelay)
374ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        return 0;
375ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
376ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    return ((uSecLatency - vcompDelay) / 1000);
377ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen}
378ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
379ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen// Used to implement get_presentation_position() for Audio HAL.
380ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen// According to the prototype in audio.h, the frame count should not get
381ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen// reset on standby().
382ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chenstatus_t AudioStreamOut::getPresentationPosition(uint64_t *frames,
383ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        struct timespec *timestamp)
384ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen{
385ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    Mutex::Autolock _l(mRoutingLock);
386ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    status_t result = -ENODEV;
387ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // The presentation timestamp should be the same for all devices.
388ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // Also Molly only has one output device at the moment.
389ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // So just use the first one in the list.
390ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    if (!mPhysOutputs.isEmpty()) {
391ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        const unsigned int kInsaneAvail = 10 * 48000;
392ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        unsigned int avail = 0;
393ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        sp<AudioOutput> audioOutput = mPhysOutputs.itemAt(0);
394ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        if (audioOutput->getHardwareTimestamp(&avail, timestamp) == 0) {
395ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen            if (avail < kInsaneAvail) {
396ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen                // FIXME av sync fudge factor
397ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen                // Use a fudge factor to account for hidden buffering in the
398ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen                // HDMI output path. This is a hack until we can determine the
399ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen                // actual buffer sizes.
400ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen                // Increasing kFudgeMSec will move the audio earlier in
401ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen                // relation to the video.
4024bea5661c671cb49255c9825f371d2bdf6c669e5Phil Burk                const int kFudgeMSec = 50;
403ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen                int fudgeFrames = kFudgeMSec * sampleRate() / 1000;
404ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
405ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen                // Scale the frames in the driver because it might be running at
406ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen                // a higher rate for EAC3.
407ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen                int64_t framesInDriverBuffer =
408ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen                    (int64_t)audioOutput->getKernelBufferSize() - (int64_t)avail;
409ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen                framesInDriverBuffer = framesInDriverBuffer / getRateMultiplier();
410ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
411ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen                int64_t pendingFrames = framesInDriverBuffer + fudgeFrames;
412ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen                int64_t signedFrames = mFramesPresented - pendingFrames;
413ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen                if (pendingFrames < 0) {
414ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen                    ALOGE("getPresentationPosition: negative pendingFrames = %lld",
415ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen                        pendingFrames);
416ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen                } else if (signedFrames < 0) {
417ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen                    ALOGI("getPresentationPosition: playing silent preroll"
418ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen                        ", mFramesPresented = %llu, pendingFrames = %lld",
419ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen                        mFramesPresented, pendingFrames);
420ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen                } else {
421ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen#if HAL_PRINT_TIMESTAMP_CSV
422ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen                    // Print comma separated values for spreadsheet analysis.
423ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen                    uint64_t nanos = (((uint64_t)timestamp->tv_sec) * 1000000000L)
424ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen                            + timestamp->tv_nsec;
425ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen                    ALOGI("getPresentationPosition, %lld, %4u, %lld, %llu",
426ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen                            mFramesPresented, avail, signedFrames, nanos);
427ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen#endif
428ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen                    *frames = (uint64_t) signedFrames;
429ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen                    result = NO_ERROR;
430ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen                }
431ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen            } else {
432ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen                ALOGE("getPresentationPosition: avail too large = %u", avail);
433ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen            }
434ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        } else {
435ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen            ALOGE("getPresentationPosition: getHardwareTimestamp returned non-zero");
436ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        }
437ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    } else {
438b7d477723e64b7526106641de168788e59152617Andre Eisenbach        ALOGVV("getPresentationPosition: no physical outputs! This HAL is inactive!");
439ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    }
440ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    return result;
441ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen}
442ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
443ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chenstatus_t AudioStreamOut::getRenderPosition(__unused uint32_t *dspFrames)
444ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen{
445e78fa3676f07529a565b6b073d8c63d14f8c383aPhil Burk    if (dspFrames == NULL) {
446e78fa3676f07529a565b6b073d8c63d14f8c383aPhil Burk        return -EINVAL;
447e78fa3676f07529a565b6b073d8c63d14f8c383aPhil Burk    }
448e78fa3676f07529a565b6b073d8c63d14f8c383aPhil Burk    if (mPhysOutputs.isEmpty()) {
449e78fa3676f07529a565b6b073d8c63d14f8c383aPhil Burk        *dspFrames = 0;
450e78fa3676f07529a565b6b073d8c63d14f8c383aPhil Burk        return -ENODEV;
451e78fa3676f07529a565b6b073d8c63d14f8c383aPhil Burk    }
452e78fa3676f07529a565b6b073d8c63d14f8c383aPhil Burk    *dspFrames = (uint32_t) mFramesRendered;
453e78fa3676f07529a565b6b073d8c63d14f8c383aPhil Burk    return NO_ERROR;
454ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen}
455ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
456ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chenvoid AudioStreamOut::updateTargetOutputs()
457ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen{
458ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    Mutex::Autolock _l(mRoutingLock);
459ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
460ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    AudioOutputList::iterator I;
461ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    uint32_t cur_outputs = 0;
462ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
463ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    for (I = mPhysOutputs.begin(); I != mPhysOutputs.end(); ++I)
464ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        cur_outputs |= (*I)->devMask();
465ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
466ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    if (cur_outputs == mTgtDevices)
467ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        return;
468ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
469ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    uint32_t outputsToObtain  = mTgtDevices & ~cur_outputs;
470ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    uint32_t outputsToRelease = cur_outputs & ~mTgtDevices;
471ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
472ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // Start by releasing any outputs we should no longer have back to the HAL.
473ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    if (outputsToRelease) {
474ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
475ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        I = mPhysOutputs.begin();
476ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        while (I != mPhysOutputs.end()) {
477ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen            if (!(outputsToRelease & (*I)->devMask())) {
478ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen                ++I;
479ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen                continue;
480ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen            }
481ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
482ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen            outputsToRelease &= ~((*I)->devMask());
483ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen            mOwnerHAL.releaseOutput(*this, *I);
484ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen            I = mPhysOutputs.erase(I);
485ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        }
486ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    }
487ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
488ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    if (outputsToRelease) {
489ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        ALOGW("Bookkeeping error!  Still have outputs to release (%08x), but"
490ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen              " none of them appear to be in the mPhysOutputs list!",
491ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen              outputsToRelease);
492ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    }
493ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
494ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // Now attempt to obtain any outputs we should be using, but are not
495ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // currently.
496ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    if (outputsToObtain) {
497ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        uint32_t mask;
498ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
499ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        // Buffer configuration may need updating now that we have decoded
500ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        // the start of a stream. For example, EAC3, needs 4X sampleRate.
501ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        updateInputNums();
502ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
503ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        for (mask = 0x1; outputsToObtain; mask <<= 1) {
504ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen            if (!(mask & outputsToObtain))
505ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen                continue;
506ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
507ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen            sp<AudioOutput> newOutput;
508ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen            status_t res;
509ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
510ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen            res = mOwnerHAL.obtainOutput(*this, mask, &newOutput);
511ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen            outputsToObtain &= ~mask;
512ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
513ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen            if (OK != res) {
514ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen                // If we get an error back from obtain output, it means that
515ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen                // something went really wrong at a lower level (probably failed
516ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen                // to open the driver).  We should not try to obtain this output
517ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen                // again, at least until the next routing change.
518ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen                ALOGW("Failed to obtain output %08x for %s audio stream out."
519ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen                      " (res %d)", mask, getName(), res);
520ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen                mTgtDevices &= ~mask;
521ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen                continue;
522ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen            }
523ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
524ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen            if (newOutput != NULL) {
525ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen                // If we actually got an output, go ahead and add it to our list
526ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen                // of physical outputs.  The rest of the system will handle
527ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen                // starting it up.  If we didn't get an output, but also go no
528ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen                // error code, it just means that the output is currently busy
529ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen                // and should become available soon.
530ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen                ALOGI("updateTargetOutputs: adding output back to mPhysOutputs");
531ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen                mPhysOutputs.push_back(newOutput);
532ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen            }
533ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        }
534ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    }
535ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen}
536ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
537ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chenvoid AudioStreamOut::adjustOutputs(int64_t maxTime)
538ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen{
539821688ed8ee498455d39b6b7d5d853c16f74ae98Phil Burk    int64_t a_zero_original = mLocalTimeToFrames.a_zero;
540821688ed8ee498455d39b6b7d5d853c16f74ae98Phil Burk    int64_t b_zero_original = mLocalTimeToFrames.b_zero;
541ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    AudioOutputList::iterator I;
542ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
543ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // Check to see if any outputs are active and see what their buffer levels
544ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // are.
545ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    for (I = mPhysOutputs.begin(); I != mPhysOutputs.end(); ++I) {
546ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        if ((*I)->getState() == AudioOutput::DMA_START) {
547ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen            int64_t lastWriteTS = (*I)->getLastNextWriteTS();
548ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen            int64_t padAmt;
549ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
550ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen            mLocalTimeToFrames.a_zero = lastWriteTS;
551ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen            mLocalTimeToFrames.b_zero = 0;
552ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen            if (mLocalTimeToFrames.doForwardTransform(maxTime,
553ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen                                                      &padAmt)) {
554ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen                (*I)->adjustDelay(((int32_t)padAmt));
555ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen            }
556ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        }
557ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    }
558821688ed8ee498455d39b6b7d5d853c16f74ae98Phil Burk    // Restore original offset so that the sleep time calculation for
559821688ed8ee498455d39b6b7d5d853c16f74ae98Phil Burk    // throttling is not broken in finishedWriteOp().
560821688ed8ee498455d39b6b7d5d853c16f74ae98Phil Burk    mLocalTimeToFrames.a_zero = a_zero_original;
561821688ed8ee498455d39b6b7d5d853c16f74ae98Phil Burk    mLocalTimeToFrames.b_zero = b_zero_original;
562ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen}
563ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
564ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chenssize_t AudioStreamOut::write(const void* buffer, size_t bytes)
565ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen{
566ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    uint8_t *data = (uint8_t *)buffer;
567ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    ALOGVV("AudioStreamOut::write(%u)   0x%02X, 0x%02X, 0x%02X, 0x%02X,"
568ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen          " 0x%02X, 0x%02X, 0x%02X, 0x%02X,"
569ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen          " 0x%02X, 0x%02X, 0x%02X, 0x%02X,"
570ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen          " 0x%02X, 0x%02X, 0x%02X, 0x%02X ====",
571ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        bytes, data[0], data[1], data[2], data[3],
572ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        data[4], data[5], data[6], data[7],
573ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        data[8], data[9], data[10], data[11],
574ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        data[12], data[13], data[14], data[15]
575ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        );
576ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    if (mIsEncoded) {
577ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        return mSPDIFEncoder.write(buffer, bytes);
578ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    } else {
579ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        return writeInternal(buffer, bytes);
580ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    }
581ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen}
582ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
583ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chenssize_t AudioStreamOut::writeInternal(const void* buffer, size_t bytes)
584ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen{
585ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    uint8_t *data = (uint8_t *)buffer;
586ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    ALOGVV("AudioStreamOut::write_l(%u) 0x%02X, 0x%02X, 0x%02X, 0x%02X,"
587ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen          " 0x%02X, 0x%02X, 0x%02X, 0x%02X,"
588ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen          " 0x%02X, 0x%02X, 0x%02X, 0x%02X,"
589ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen          " 0x%02X, 0x%02X, 0x%02X, 0x%02X",
590ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        bytes, data[0], data[1], data[2], data[3],
591ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        data[4], data[5], data[6], data[7],
592ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        data[8], data[9], data[10], data[11],
593ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        data[12], data[13], data[14], data[15]
594ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        );
595282f06dc6d099dbb010cf9acf0bcb9bdee43fb8dRajat S Gupta
596ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // Note: no lock is obtained here.  Calls to write and getNextWriteTimestamp
597ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // happen only on the AudioFlinger mixer thread which owns this particular
598ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // output stream, so there is no need to worry that there will be two
599ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // threads in this instance method concurrently.
600ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    //
601ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // In addition, only calls to write change the contents of the mPhysOutputs
602ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // collection (during the call to updateTargetOutputs).  updateTargetOutputs
603ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // will hold the routing lock during the operation, as should any reader of
604ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // mPhysOutputs, unless the reader is a call to write or
605ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // getNextWriteTimestamp (we know that it is safe for write and gnwt to read
606ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // the collection because the only collection mutator is the same thread
607ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // which calls write and gnwt).
608282f06dc6d099dbb010cf9acf0bcb9bdee43fb8dRajat S Gupta
609282f06dc6d099dbb010cf9acf0bcb9bdee43fb8dRajat S Gupta    // If the stream is in standby, then the first write should bring it out
610282f06dc6d099dbb010cf9acf0bcb9bdee43fb8dRajat S Gupta    // of standby
611282f06dc6d099dbb010cf9acf0bcb9bdee43fb8dRajat S Gupta    if (mInStandby) {
612282f06dc6d099dbb010cf9acf0bcb9bdee43fb8dRajat S Gupta        mOwnerHAL.standbyStatusUpdate(false, mIsMCOutput);
613282f06dc6d099dbb010cf9acf0bcb9bdee43fb8dRajat S Gupta        mInStandby = false;
614282f06dc6d099dbb010cf9acf0bcb9bdee43fb8dRajat S Gupta    }
615282f06dc6d099dbb010cf9acf0bcb9bdee43fb8dRajat S Gupta
616ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    updateTargetOutputs();
617ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
618ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // If any of our outputs is in the PRIMED state when ::write is called, it
619ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // means one of two things.  First, it could be that the DMA output really
620ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // has not started yet.  This is odd, but certainly not impossible.  The
621ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // other possibility is that AudioFlinger is in its silence-pushing mode and
622ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // is not calling getNextWriteTimestamp.  After an output is primed, its in
623ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // GNWTS where the amt of padding to compensate for different DMA start
624ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // times is taken into account.  Go ahead and force a call to GNWTS, just to
625ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // be certain that we have checked recently and are not stuck in silence
626ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // fill mode.  Failure to do this will cause the AudioOutput state machine
627ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // to eventually give up on DMA starting and reset the output over and over
628ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // again (spamming the log and producing general confusion).
629ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    //
630ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // While we are in the process of checking our various output states, check
631ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // to see if any outputs have made it to the ACTIVE state.  Pass this
632ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // information along to the call to processOneChunk.  If any of our outputs
633ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // are waiting to be primed while other outputs have made it to steady
634ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // state, we need to change our priming behavior slightly.  Instead of
635ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // filling an output's buffer completely, we want to fill it to slightly
636ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // less than full and let the adjustDelay mechanism take care of the rest.
637ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    //
638ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // Failure to do this during steady state operation will almost certainly
639ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // lead to the new output being over-filled relative to the other outputs
640ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // causing it to be slightly out of sync.
641ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    AudioOutputList::iterator I;
642ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    bool checkDMAStart = false;
643ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    bool hasActiveOutputs = false;
644ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    for (I = mPhysOutputs.begin(); I != mPhysOutputs.end(); ++I) {
645ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        if (AudioOutput::PRIMED == (*I)->getState())
646ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen            checkDMAStart = true;
647ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
648ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        if ((*I)->getState() == AudioOutput::ACTIVE)
649ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen            hasActiveOutputs = true;
650ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    }
651ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
652ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    if (checkDMAStart) {
653ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        int64_t junk;
654ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        getNextWriteTimestamp_internal(&junk);
655ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    }
656ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
657ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // We always call processOneChunk on the outputs, as it is the
658ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // tick for their state machines.
659ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    for (I = mPhysOutputs.begin(); I != mPhysOutputs.end(); ++I) {
660ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        (*I)->processOneChunk((uint8_t *)buffer, bytes, hasActiveOutputs);
661ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    }
662ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
663ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // If we don't actually have any physical outputs to write to, just sleep
664ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // for the proper amt of time in order to simulate the throttle that writing
665ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // to the hardware would impose.
666ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    finishedWriteOp(bytes / getBytesPerOutputFrame(), (0 == mPhysOutputs.size()));
667ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
668ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    return static_cast<ssize_t>(bytes);
669ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen}
670ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
671ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chenstatus_t AudioStreamOut::getNextWriteTimestamp(int64_t *timestamp)
672ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen{
673ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    return getNextWriteTimestamp_internal(timestamp);
674ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen}
675ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
676ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chenstatus_t AudioStreamOut::getNextWriteTimestamp_internal(
677ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        int64_t *timestamp)
678ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen{
679ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    int64_t max_time = LLONG_MIN;
680ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    bool    max_time_valid = false;
681ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    bool    need_adjust = false;
682ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
683ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // Across all of our physical outputs, figure out the max time when
684ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // a write operation will hit the speakers.  Assume that if an
685ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // output cannot answer the question, its because it has never
686ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // started or because it has recently underflowed and needs to be
687ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // restarted.  If this is the case, we will need to prime the
688ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // pipeline with a chunk's worth of data before proceeding.
689ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // If any of the outputs indicate a discontinuity (meaning that the
690ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // DMA start time was valid and is now invalid, or was and is valid
691ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // but was different from before; almost certainly caused by a low
692ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // level underfow), then just stop now.  We will need to reset and
693ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // re-prime all of the outputs in order to make certain that the
694ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // lead-times on all of the outputs match.
695ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
696ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    AudioOutputList::iterator I;
697ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    bool discon = false;
698ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
699ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // Find the largest next write timestamp. The goal is to make EVERY
700ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // output have the same value, but we also need this to pass back
701ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // up the layers.
702ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    for (I = mPhysOutputs.begin(); I != mPhysOutputs.end(); ++I) {
703ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        int64_t tmp;
704ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        if (OK == (*I)->getNextWriteTimestamp(&tmp, &discon)) {
705ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen            if (!max_time_valid || (max_time < tmp)) {
706ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen                max_time = tmp;
707ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen                max_time_valid = true;
708ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen            }
709ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        }
710ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    }
711ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
712ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // Check the state of each output and determine if we need to align them.
713ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // Make sure to do this after we have called each outputs'
714ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // getNextWriteTimestamp as the transition from PRIMED to DMA_START happens
715ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // there.
716ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    for (I = mPhysOutputs.begin(); I != mPhysOutputs.end(); ++I) {
717ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        if ((*I)->getState() == AudioOutput::DMA_START) {
718ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen            need_adjust = true;
719ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen            break;
720ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        }
721ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    }
722ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
723ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // At this point, if we still have not found at least one output
724ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // who knows when their data is going to hit the speakers, then we
725ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // just can't answer the getNextWriteTimestamp question and we
726ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // should give up.
727ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    if (!max_time_valid) {
728ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        return INVALID_OPERATION;
729ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    }
730ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
731ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // Stuff silence into the non-aligned outputs so that the effective
732ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // timestamp is the same for all the outputs.
733ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    if (need_adjust)
734ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        adjustOutputs(max_time);
735ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
736ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // We are done. The time at which the next written audio should
737ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // hit the speakers is just max_time plus the maximum amt of delay
738ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    // compensation in the system.
739ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    *timestamp = max_time;
740ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    return OK;
741ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen}
742ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
743ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen#define DUMP(a...) \
744ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    snprintf(buffer, SIZE, a); \
745ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    buffer[SIZE - 1] = 0; \
746ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    result.append(buffer);
747ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen#define B2STR(b) b ? "true" : "false"
748ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
749ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chenstatus_t AudioStreamOut::dump(int fd)
750ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen{
751ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    const size_t SIZE = 256;
752ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    char buffer[SIZE];
753ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    String8 result;
754ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    DUMP("\n%s AudioStreamOut::dump\n", getName());
755ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    DUMP("\tsample rate            : %d\n", sampleRate());
756ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    DUMP("\tbuffer size            : %d\n", bufferSize());
757ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    DUMP("\tchannel mask           : 0x%04x\n", chanMask());
758ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    DUMP("\tformat                 : %d\n", format());
759ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    DUMP("\tdevice mask            : 0x%04x\n", mTgtDevices);
760282f06dc6d099dbb010cf9acf0bcb9bdee43fb8dRajat S Gupta    DUMP("\tIn standby             : %s\n", mInStandby? "yes" : "no");
761ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
762ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    mRoutingLock.lock();
763ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    AudioOutputList outSnapshot(mPhysOutputs);
764ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    mRoutingLock.unlock();
765ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
766ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    AudioOutputList::iterator I;
767ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    for (I = outSnapshot.begin(); I != outSnapshot.end(); ++I)
768ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen        (*I)->dump(result);
769ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
770ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    ::write(fd, result.string(), result.size());
771ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
772ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen    return NO_ERROR;
773ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen}
774ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
775ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen#undef B2STR
776ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen#undef DUMP
777ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen
778ae089528c7e3a8107be65657e52276dd068c1039Mike J. Chen}  // android
779