The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <math.h> |
| 18 | |
Eric Laurent | 80a6a22 | 2009-10-08 10:58:19 -0700 | [diff] [blame] | 19 | //#define LOG_NDEBUG 0 |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | #define LOG_TAG "A2dpAudioInterface" |
| 21 | #include <utils/Log.h> |
| 22 | #include <utils/String8.h> |
| 23 | |
| 24 | #include "A2dpAudioInterface.h" |
| 25 | #include "audio/liba2dp.h" |
Eric Laurent | 6dc08b3 | 2010-11-23 17:59:39 -0800 | [diff] [blame] | 26 | #include <hardware_legacy/power.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | |
| 28 | namespace android { |
| 29 | |
Eric Laurent | 6dc08b3 | 2010-11-23 17:59:39 -0800 | [diff] [blame] | 30 | static const char *sA2dpWakeLock = "A2dpOutputStream"; |
| 31 | #define MAX_WRITE_RETRIES 5 |
| 32 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | // ---------------------------------------------------------------------------- |
| 34 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 35 | //AudioHardwareInterface* A2dpAudioInterface::createA2dpInterface() |
| 36 | //{ |
| 37 | // AudioHardwareInterface* hw = 0; |
| 38 | // |
| 39 | // hw = AudioHardwareInterface::create(); |
| 40 | // LOGD("new A2dpAudioInterface(hw: %p)", hw); |
| 41 | // hw = new A2dpAudioInterface(hw); |
| 42 | // return hw; |
| 43 | //} |
| 44 | |
| 45 | A2dpAudioInterface::A2dpAudioInterface(AudioHardwareInterface* hw) : |
Eric Laurent | 80a6a22 | 2009-10-08 10:58:19 -0700 | [diff] [blame] | 46 | mOutput(0), mHardwareInterface(hw), mBluetoothEnabled(true), mSuspended(false) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | { |
| 48 | } |
| 49 | |
| 50 | A2dpAudioInterface::~A2dpAudioInterface() |
| 51 | { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 52 | closeOutputStream((AudioStreamOut *)mOutput); |
| 53 | delete mHardwareInterface; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | status_t A2dpAudioInterface::initCheck() |
| 57 | { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 58 | if (mHardwareInterface == 0) return NO_INIT; |
| 59 | return mHardwareInterface->initCheck(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | AudioStreamOut* A2dpAudioInterface::openOutputStream( |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 63 | uint32_t devices, int *format, uint32_t *channels, uint32_t *sampleRate, status_t *status) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 65 | if (!AudioSystem::isA2dpDevice((AudioSystem::audio_devices)devices)) { |
| 66 | LOGV("A2dpAudioInterface::openOutputStream() open HW device: %x", devices); |
| 67 | return mHardwareInterface->openOutputStream(devices, format, channels, sampleRate, status); |
| 68 | } |
| 69 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | status_t err = 0; |
| 71 | |
| 72 | // only one output stream allowed |
| 73 | if (mOutput) { |
| 74 | if (status) |
| 75 | *status = -1; |
| 76 | return NULL; |
| 77 | } |
| 78 | |
| 79 | // create new output stream |
| 80 | A2dpAudioStreamOut* out = new A2dpAudioStreamOut(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 81 | if ((err = out->set(devices, format, channels, sampleRate)) == NO_ERROR) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | mOutput = out; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 83 | mOutput->setBluetoothEnabled(mBluetoothEnabled); |
Eric Laurent | 80a6a22 | 2009-10-08 10:58:19 -0700 | [diff] [blame] | 84 | mOutput->setSuspended(mSuspended); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | } else { |
| 86 | delete out; |
| 87 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 88 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 89 | if (status) |
| 90 | *status = err; |
| 91 | return mOutput; |
| 92 | } |
| 93 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 94 | void A2dpAudioInterface::closeOutputStream(AudioStreamOut* out) { |
| 95 | if (mOutput == 0 || mOutput != out) { |
Eric Laurent | 351def2 | 2009-08-04 07:43:10 -0700 | [diff] [blame] | 96 | mHardwareInterface->closeOutputStream(out); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 97 | } |
| 98 | else { |
| 99 | delete mOutput; |
| 100 | mOutput = 0; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 105 | AudioStreamIn* A2dpAudioInterface::openInputStream( |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 106 | uint32_t devices, int *format, uint32_t *channels, uint32_t *sampleRate, status_t *status, |
| 107 | AudioSystem::audio_in_acoustics acoustics) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 109 | return mHardwareInterface->openInputStream(devices, format, channels, sampleRate, status, acoustics); |
| 110 | } |
| 111 | |
| 112 | void A2dpAudioInterface::closeInputStream(AudioStreamIn* in) |
| 113 | { |
| 114 | return mHardwareInterface->closeInputStream(in); |
| 115 | } |
| 116 | |
| 117 | status_t A2dpAudioInterface::setMode(int mode) |
| 118 | { |
| 119 | return mHardwareInterface->setMode(mode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | status_t A2dpAudioInterface::setMicMute(bool state) |
| 123 | { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 124 | return mHardwareInterface->setMicMute(state); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | status_t A2dpAudioInterface::getMicMute(bool* state) |
| 128 | { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 129 | return mHardwareInterface->getMicMute(state); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 130 | } |
| 131 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 132 | status_t A2dpAudioInterface::setParameters(const String8& keyValuePairs) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 134 | AudioParameter param = AudioParameter(keyValuePairs); |
| 135 | String8 value; |
| 136 | String8 key; |
| 137 | status_t status = NO_ERROR; |
Nick Pelly | 0827c81 | 2009-04-02 01:21:13 -0700 | [diff] [blame] | 138 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 139 | LOGV("setParameters() %s", keyValuePairs.string()); |
Nick Pelly | 0827c81 | 2009-04-02 01:21:13 -0700 | [diff] [blame] | 140 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 141 | key = "bluetooth_enabled"; |
| 142 | if (param.get(key, value) == NO_ERROR) { |
| 143 | mBluetoothEnabled = (value == "true"); |
| 144 | if (mOutput) { |
| 145 | mOutput->setBluetoothEnabled(mBluetoothEnabled); |
| 146 | } |
| 147 | param.remove(key); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 148 | } |
Eric Laurent | 80a6a22 | 2009-10-08 10:58:19 -0700 | [diff] [blame] | 149 | key = String8("A2dpSuspended"); |
| 150 | if (param.get(key, value) == NO_ERROR) { |
| 151 | mSuspended = (value == "true"); |
| 152 | if (mOutput) { |
| 153 | mOutput->setSuspended(mSuspended); |
| 154 | } |
| 155 | param.remove(key); |
| 156 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 157 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 158 | if (param.size()) { |
| 159 | status_t hwStatus = mHardwareInterface->setParameters(param.toString()); |
| 160 | if (status == NO_ERROR) { |
| 161 | status = hwStatus; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | return status; |
| 166 | } |
| 167 | |
| 168 | String8 A2dpAudioInterface::getParameters(const String8& keys) |
| 169 | { |
| 170 | AudioParameter param = AudioParameter(keys); |
| 171 | AudioParameter a2dpParam = AudioParameter(); |
| 172 | String8 value; |
| 173 | String8 key; |
| 174 | |
| 175 | key = "bluetooth_enabled"; |
| 176 | if (param.get(key, value) == NO_ERROR) { |
| 177 | value = mBluetoothEnabled ? "true" : "false"; |
| 178 | a2dpParam.add(key, value); |
| 179 | param.remove(key); |
| 180 | } |
Eric Laurent | 80a6a22 | 2009-10-08 10:58:19 -0700 | [diff] [blame] | 181 | key = "A2dpSuspended"; |
| 182 | if (param.get(key, value) == NO_ERROR) { |
| 183 | value = mSuspended ? "true" : "false"; |
| 184 | a2dpParam.add(key, value); |
| 185 | param.remove(key); |
| 186 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 187 | |
| 188 | String8 keyValuePairs = a2dpParam.toString(); |
| 189 | |
| 190 | if (param.size()) { |
Eric Laurent | 4ad9ec4 | 2009-11-25 06:08:44 -0800 | [diff] [blame] | 191 | if (keyValuePairs != "") { |
| 192 | keyValuePairs += ";"; |
| 193 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 194 | keyValuePairs += mHardwareInterface->getParameters(param.toString()); |
| 195 | } |
| 196 | |
| 197 | LOGV("getParameters() %s", keyValuePairs.string()); |
| 198 | return keyValuePairs; |
| 199 | } |
| 200 | |
| 201 | size_t A2dpAudioInterface::getInputBufferSize(uint32_t sampleRate, int format, int channelCount) |
| 202 | { |
| 203 | return mHardwareInterface->getInputBufferSize(sampleRate, format, channelCount); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | status_t A2dpAudioInterface::setVoiceVolume(float v) |
| 207 | { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 208 | return mHardwareInterface->setVoiceVolume(v); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | status_t A2dpAudioInterface::setMasterVolume(float v) |
| 212 | { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 213 | return mHardwareInterface->setMasterVolume(v); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | status_t A2dpAudioInterface::dump(int fd, const Vector<String16>& args) |
| 217 | { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 218 | return mHardwareInterface->dumpState(fd, args); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | // ---------------------------------------------------------------------------- |
| 222 | |
| 223 | A2dpAudioInterface::A2dpAudioStreamOut::A2dpAudioStreamOut() : |
Nick Pelly | 0827c81 | 2009-04-02 01:21:13 -0700 | [diff] [blame] | 224 | mFd(-1), mStandby(true), mStartCount(0), mRetryCount(0), mData(NULL), |
| 225 | // assume BT enabled to start, this is safe because its only the |
| 226 | // enabled->disabled transition we are worried about |
Eric Laurent | 80a6a22 | 2009-10-08 10:58:19 -0700 | [diff] [blame] | 227 | mBluetoothEnabled(true), mDevice(0), mClosing(false), mSuspended(false) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 228 | { |
| 229 | // use any address by default |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 230 | strcpy(mA2dpAddress, "00:00:00:00:00:00"); |
| 231 | init(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | status_t A2dpAudioInterface::A2dpAudioStreamOut::set( |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 235 | uint32_t device, int *pFormat, uint32_t *pChannels, uint32_t *pRate) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 236 | { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 237 | int lFormat = pFormat ? *pFormat : 0; |
| 238 | uint32_t lChannels = pChannels ? *pChannels : 0; |
| 239 | uint32_t lRate = pRate ? *pRate : 0; |
| 240 | |
| 241 | LOGD("A2dpAudioStreamOut::set %x, %d, %d, %d\n", device, lFormat, lChannels, lRate); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 242 | |
| 243 | // fix up defaults |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 244 | if (lFormat == 0) lFormat = format(); |
| 245 | if (lChannels == 0) lChannels = channels(); |
| 246 | if (lRate == 0) lRate = sampleRate(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 247 | |
| 248 | // check values |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 249 | if ((lFormat != format()) || |
| 250 | (lChannels != channels()) || |
| 251 | (lRate != sampleRate())){ |
| 252 | if (pFormat) *pFormat = format(); |
| 253 | if (pChannels) *pChannels = channels(); |
| 254 | if (pRate) *pRate = sampleRate(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 255 | return BAD_VALUE; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 256 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 257 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 258 | if (pFormat) *pFormat = lFormat; |
| 259 | if (pChannels) *pChannels = lChannels; |
| 260 | if (pRate) *pRate = lRate; |
| 261 | |
| 262 | mDevice = device; |
Eric Laurent | e7155e6 | 2010-12-17 10:27:02 -0800 | [diff] [blame] | 263 | mBufferDurationUs = ((bufferSize() * 1000 )/ frameSize() / sampleRate()) * 1000; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 264 | return NO_ERROR; |
| 265 | } |
| 266 | |
| 267 | A2dpAudioInterface::A2dpAudioStreamOut::~A2dpAudioStreamOut() |
| 268 | { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 269 | LOGV("A2dpAudioStreamOut destructor"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 270 | close(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 271 | LOGV("A2dpAudioStreamOut destructor returning from close()"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | ssize_t A2dpAudioInterface::A2dpAudioStreamOut::write(const void* buffer, size_t bytes) |
Nick Pelly | 0827c81 | 2009-04-02 01:21:13 -0700 | [diff] [blame] | 275 | { |
Nick Pelly | 0827c81 | 2009-04-02 01:21:13 -0700 | [diff] [blame] | 276 | status_t status = -1; |
Eric Laurent | 6dc08b3 | 2010-11-23 17:59:39 -0800 | [diff] [blame] | 277 | { |
| 278 | Mutex::Autolock lock(mLock); |
Nick Pelly | 0827c81 | 2009-04-02 01:21:13 -0700 | [diff] [blame] | 279 | |
Eric Laurent | 6dc08b3 | 2010-11-23 17:59:39 -0800 | [diff] [blame] | 280 | size_t remaining = bytes; |
Nick Pelly | 0827c81 | 2009-04-02 01:21:13 -0700 | [diff] [blame] | 281 | |
Eric Laurent | 6dc08b3 | 2010-11-23 17:59:39 -0800 | [diff] [blame] | 282 | if (!mBluetoothEnabled || mClosing || mSuspended) { |
| 283 | LOGV("A2dpAudioStreamOut::write(), but bluetooth disabled \ |
| 284 | mBluetoothEnabled %d, mClosing %d, mSuspended %d", |
| 285 | mBluetoothEnabled, mClosing, mSuspended); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 286 | goto Error; |
| 287 | } |
Eric Laurent | 6dc08b3 | 2010-11-23 17:59:39 -0800 | [diff] [blame] | 288 | |
| 289 | if (mStandby) { |
| 290 | acquire_wake_lock (PARTIAL_WAKE_LOCK, sA2dpWakeLock); |
| 291 | mStandby = false; |
Eric Laurent | e7155e6 | 2010-12-17 10:27:02 -0800 | [diff] [blame] | 292 | mLastWriteTime = systemTime(); |
Eric Laurent | 6dc08b3 | 2010-11-23 17:59:39 -0800 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | status = init(); |
| 296 | if (status < 0) |
| 297 | goto Error; |
| 298 | |
| 299 | int retries = MAX_WRITE_RETRIES; |
| 300 | while (remaining > 0 && retries) { |
| 301 | status = a2dp_write(mData, buffer, remaining); |
| 302 | if (status < 0) { |
| 303 | LOGE("a2dp_write failed err: %d\n", status); |
| 304 | goto Error; |
| 305 | } |
| 306 | if (status == 0) { |
| 307 | retries--; |
| 308 | } |
| 309 | remaining -= status; |
| 310 | buffer = (char *)buffer + status; |
| 311 | } |
| 312 | |
Eric Laurent | e7155e6 | 2010-12-17 10:27:02 -0800 | [diff] [blame] | 313 | // if A2DP sink runs abnormally fast, sleep a little so that audioflinger mixer thread |
| 314 | // does no spin and starve other threads. |
| 315 | // NOTE: It is likely that the A2DP headset is being disconnected |
| 316 | nsecs_t now = systemTime(); |
| 317 | if ((uint32_t)ns2us(now - mLastWriteTime) < (mBufferDurationUs >> 2)) { |
| 318 | LOGV("A2DP sink runs too fast"); |
| 319 | usleep(mBufferDurationUs - (uint32_t)ns2us(now - mLastWriteTime)); |
| 320 | } |
| 321 | mLastWriteTime = now; |
Eric Laurent | 6dc08b3 | 2010-11-23 17:59:39 -0800 | [diff] [blame] | 322 | return bytes; |
| 323 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 324 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 325 | Error: |
Eric Laurent | 6dc08b3 | 2010-11-23 17:59:39 -0800 | [diff] [blame] | 326 | |
| 327 | standby(); |
| 328 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 329 | // Simulate audio output timing in case of error |
Eric Laurent | e7155e6 | 2010-12-17 10:27:02 -0800 | [diff] [blame] | 330 | usleep(mBufferDurationUs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 331 | |
| 332 | return status; |
| 333 | } |
| 334 | |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 335 | status_t A2dpAudioInterface::A2dpAudioStreamOut::init() |
| 336 | { |
| 337 | if (!mData) { |
| 338 | status_t status = a2dp_init(44100, 2, &mData); |
| 339 | if (status < 0) { |
| 340 | LOGE("a2dp_init failed err: %d\n", status); |
| 341 | mData = NULL; |
| 342 | return status; |
| 343 | } |
| 344 | a2dp_set_sink(mData, mA2dpAddress); |
| 345 | } |
| 346 | |
| 347 | return 0; |
| 348 | } |
| 349 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 350 | status_t A2dpAudioInterface::A2dpAudioStreamOut::standby() |
| 351 | { |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 352 | Mutex::Autolock lock(mLock); |
Eric Laurent | 6dc08b3 | 2010-11-23 17:59:39 -0800 | [diff] [blame] | 353 | return standby_l(); |
| 354 | } |
| 355 | |
| 356 | status_t A2dpAudioInterface::A2dpAudioStreamOut::standby_l() |
| 357 | { |
| 358 | int result = NO_ERROR; |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 359 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 360 | if (!mStandby) { |
Eric Laurent | 6dc08b3 | 2010-11-23 17:59:39 -0800 | [diff] [blame] | 361 | LOGV_IF(mClosing || !mBluetoothEnabled, "Standby skip stop: closing %d enabled %d", |
| 362 | mClosing, mBluetoothEnabled); |
| 363 | if (!mClosing && mBluetoothEnabled) { |
| 364 | result = a2dp_stop(mData); |
| 365 | } |
| 366 | release_wake_lock(sA2dpWakeLock); |
| 367 | mStandby = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | return result; |
| 371 | } |
| 372 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 373 | status_t A2dpAudioInterface::A2dpAudioStreamOut::setParameters(const String8& keyValuePairs) |
| 374 | { |
| 375 | AudioParameter param = AudioParameter(keyValuePairs); |
| 376 | String8 value; |
| 377 | String8 key = String8("a2dp_sink_address"); |
| 378 | status_t status = NO_ERROR; |
| 379 | int device; |
| 380 | LOGV("A2dpAudioStreamOut::setParameters() %s", keyValuePairs.string()); |
| 381 | |
| 382 | if (param.get(key, value) == NO_ERROR) { |
| 383 | if (value.length() != strlen("00:00:00:00:00:00")) { |
| 384 | status = BAD_VALUE; |
| 385 | } else { |
| 386 | setAddress(value.string()); |
| 387 | } |
| 388 | param.remove(key); |
| 389 | } |
Eric Laurent | fe4fc91 | 2009-08-12 05:49:58 -0700 | [diff] [blame] | 390 | key = String8("closing"); |
| 391 | if (param.get(key, value) == NO_ERROR) { |
| 392 | mClosing = (value == "true"); |
Eric Laurent | 6dc08b3 | 2010-11-23 17:59:39 -0800 | [diff] [blame] | 393 | if (mClosing) { |
| 394 | standby(); |
| 395 | } |
Eric Laurent | fe4fc91 | 2009-08-12 05:49:58 -0700 | [diff] [blame] | 396 | param.remove(key); |
| 397 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 398 | key = AudioParameter::keyRouting; |
| 399 | if (param.getInt(key, device) == NO_ERROR) { |
| 400 | if (AudioSystem::isA2dpDevice((AudioSystem::audio_devices)device)) { |
| 401 | mDevice = device; |
| 402 | status = NO_ERROR; |
| 403 | } else { |
| 404 | status = BAD_VALUE; |
| 405 | } |
| 406 | param.remove(key); |
| 407 | } |
| 408 | |
| 409 | if (param.size()) { |
| 410 | status = BAD_VALUE; |
| 411 | } |
| 412 | return status; |
| 413 | } |
| 414 | |
| 415 | String8 A2dpAudioInterface::A2dpAudioStreamOut::getParameters(const String8& keys) |
| 416 | { |
| 417 | AudioParameter param = AudioParameter(keys); |
| 418 | String8 value; |
| 419 | String8 key = String8("a2dp_sink_address"); |
| 420 | |
| 421 | if (param.get(key, value) == NO_ERROR) { |
| 422 | value = mA2dpAddress; |
| 423 | param.add(key, value); |
| 424 | } |
| 425 | key = AudioParameter::keyRouting; |
| 426 | if (param.get(key, value) == NO_ERROR) { |
| 427 | param.addInt(key, (int)mDevice); |
| 428 | } |
| 429 | |
| 430 | LOGV("A2dpAudioStreamOut::getParameters() %s", param.toString().string()); |
| 431 | return param.toString(); |
| 432 | } |
| 433 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 434 | status_t A2dpAudioInterface::A2dpAudioStreamOut::setAddress(const char* address) |
| 435 | { |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 436 | Mutex::Autolock lock(mLock); |
| 437 | |
| 438 | if (strlen(address) != strlen("00:00:00:00:00:00")) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 439 | return -EINVAL; |
| 440 | |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 441 | strcpy(mA2dpAddress, address); |
| 442 | if (mData) |
| 443 | a2dp_set_sink(mData, mA2dpAddress); |
| 444 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 445 | return NO_ERROR; |
| 446 | } |
| 447 | |
Nick Pelly | 0827c81 | 2009-04-02 01:21:13 -0700 | [diff] [blame] | 448 | status_t A2dpAudioInterface::A2dpAudioStreamOut::setBluetoothEnabled(bool enabled) |
| 449 | { |
| 450 | LOGD("setBluetoothEnabled %d", enabled); |
| 451 | |
| 452 | Mutex::Autolock lock(mLock); |
| 453 | |
| 454 | mBluetoothEnabled = enabled; |
| 455 | if (!enabled) { |
| 456 | return close_l(); |
| 457 | } |
| 458 | return NO_ERROR; |
| 459 | } |
| 460 | |
Eric Laurent | 80a6a22 | 2009-10-08 10:58:19 -0700 | [diff] [blame] | 461 | status_t A2dpAudioInterface::A2dpAudioStreamOut::setSuspended(bool onOff) |
| 462 | { |
| 463 | LOGV("setSuspended %d", onOff); |
| 464 | mSuspended = onOff; |
| 465 | standby(); |
| 466 | return NO_ERROR; |
| 467 | } |
| 468 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 469 | status_t A2dpAudioInterface::A2dpAudioStreamOut::close() |
| 470 | { |
Nick Pelly | 0827c81 | 2009-04-02 01:21:13 -0700 | [diff] [blame] | 471 | Mutex::Autolock lock(mLock); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 472 | LOGV("A2dpAudioStreamOut::close() calling close_l()"); |
Nick Pelly | 0827c81 | 2009-04-02 01:21:13 -0700 | [diff] [blame] | 473 | return close_l(); |
| 474 | } |
| 475 | |
| 476 | status_t A2dpAudioInterface::A2dpAudioStreamOut::close_l() |
| 477 | { |
Eric Laurent | 6dc08b3 | 2010-11-23 17:59:39 -0800 | [diff] [blame] | 478 | standby_l(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 479 | if (mData) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 480 | LOGV("A2dpAudioStreamOut::close_l() calling a2dp_cleanup(mData)"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 481 | a2dp_cleanup(mData); |
| 482 | mData = NULL; |
| 483 | } |
| 484 | return NO_ERROR; |
| 485 | } |
| 486 | |
| 487 | status_t A2dpAudioInterface::A2dpAudioStreamOut::dump(int fd, const Vector<String16>& args) |
| 488 | { |
| 489 | return NO_ERROR; |
| 490 | } |
| 491 | |
Eric Laurent | 0986e79 | 2010-01-19 17:37:09 -0800 | [diff] [blame] | 492 | status_t A2dpAudioInterface::A2dpAudioStreamOut::getRenderPosition(uint32_t *driverFrames) |
| 493 | { |
| 494 | //TODO: enable when supported by driver |
| 495 | return INVALID_OPERATION; |
| 496 | } |
| 497 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 498 | }; // namespace android |