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