blob: aee01abde71f344efa36f2afaffe643de35e8cec [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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 Laurent80a6a222009-10-08 10:58:19 -070019//#define LOG_NDEBUG 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020#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 Laurent6dc08b32010-11-23 17:59:39 -080026#include <hardware_legacy/power.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027
28namespace android {
29
Eric Laurent6dc08b32010-11-23 17:59:39 -080030static const char *sA2dpWakeLock = "A2dpOutputStream";
31#define MAX_WRITE_RETRIES 5
32
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033// ----------------------------------------------------------------------------
34
Eric Laurenta553c252009-07-17 12:17:14 -070035//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
45A2dpAudioInterface::A2dpAudioInterface(AudioHardwareInterface* hw) :
Eric Laurent80a6a222009-10-08 10:58:19 -070046 mOutput(0), mHardwareInterface(hw), mBluetoothEnabled(true), mSuspended(false)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047{
48}
49
50A2dpAudioInterface::~A2dpAudioInterface()
51{
Eric Laurenta553c252009-07-17 12:17:14 -070052 closeOutputStream((AudioStreamOut *)mOutput);
53 delete mHardwareInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054}
55
56status_t A2dpAudioInterface::initCheck()
57{
Eric Laurenta553c252009-07-17 12:17:14 -070058 if (mHardwareInterface == 0) return NO_INIT;
59 return mHardwareInterface->initCheck();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060}
61
62AudioStreamOut* A2dpAudioInterface::openOutputStream(
Eric Laurenta553c252009-07-17 12:17:14 -070063 uint32_t devices, int *format, uint32_t *channels, uint32_t *sampleRate, status_t *status)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064{
Eric Laurenta553c252009-07-17 12:17:14 -070065 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 Project9066cfe2009-03-03 19:31:44 -080070 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 Laurenta553c252009-07-17 12:17:14 -070081 if ((err = out->set(devices, format, channels, sampleRate)) == NO_ERROR) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082 mOutput = out;
Eric Laurenta553c252009-07-17 12:17:14 -070083 mOutput->setBluetoothEnabled(mBluetoothEnabled);
Eric Laurent80a6a222009-10-08 10:58:19 -070084 mOutput->setSuspended(mSuspended);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 } else {
86 delete out;
87 }
Eric Laurenta553c252009-07-17 12:17:14 -070088
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089 if (status)
90 *status = err;
91 return mOutput;
92}
93
Eric Laurenta553c252009-07-17 12:17:14 -070094void A2dpAudioInterface::closeOutputStream(AudioStreamOut* out) {
95 if (mOutput == 0 || mOutput != out) {
Eric Laurent351def22009-08-04 07:43:10 -070096 mHardwareInterface->closeOutputStream(out);
Eric Laurenta553c252009-07-17 12:17:14 -070097 }
98 else {
99 delete mOutput;
100 mOutput = 0;
101 }
102}
103
104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105AudioStreamIn* A2dpAudioInterface::openInputStream(
Eric Laurenta553c252009-07-17 12:17:14 -0700106 uint32_t devices, int *format, uint32_t *channels, uint32_t *sampleRate, status_t *status,
107 AudioSystem::audio_in_acoustics acoustics)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108{
Eric Laurenta553c252009-07-17 12:17:14 -0700109 return mHardwareInterface->openInputStream(devices, format, channels, sampleRate, status, acoustics);
110}
111
112void A2dpAudioInterface::closeInputStream(AudioStreamIn* in)
113{
114 return mHardwareInterface->closeInputStream(in);
115}
116
117status_t A2dpAudioInterface::setMode(int mode)
118{
119 return mHardwareInterface->setMode(mode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120}
121
122status_t A2dpAudioInterface::setMicMute(bool state)
123{
Eric Laurenta553c252009-07-17 12:17:14 -0700124 return mHardwareInterface->setMicMute(state);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125}
126
127status_t A2dpAudioInterface::getMicMute(bool* state)
128{
Eric Laurenta553c252009-07-17 12:17:14 -0700129 return mHardwareInterface->getMicMute(state);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130}
131
Eric Laurenta553c252009-07-17 12:17:14 -0700132status_t A2dpAudioInterface::setParameters(const String8& keyValuePairs)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133{
Eric Laurenta553c252009-07-17 12:17:14 -0700134 AudioParameter param = AudioParameter(keyValuePairs);
135 String8 value;
136 String8 key;
137 status_t status = NO_ERROR;
Nick Pelly0827c812009-04-02 01:21:13 -0700138
Eric Laurenta553c252009-07-17 12:17:14 -0700139 LOGV("setParameters() %s", keyValuePairs.string());
Nick Pelly0827c812009-04-02 01:21:13 -0700140
Eric Laurenta553c252009-07-17 12:17:14 -0700141 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 Project9066cfe2009-03-03 19:31:44 -0800148 }
Eric Laurent80a6a222009-10-08 10:58:19 -0700149 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 Project9066cfe2009-03-03 19:31:44 -0800157
Eric Laurenta553c252009-07-17 12:17:14 -0700158 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
168String8 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 Laurent80a6a222009-10-08 10:58:19 -0700181 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 Laurenta553c252009-07-17 12:17:14 -0700187
188 String8 keyValuePairs = a2dpParam.toString();
189
190 if (param.size()) {
Eric Laurent4ad9ec42009-11-25 06:08:44 -0800191 if (keyValuePairs != "") {
192 keyValuePairs += ";";
193 }
Eric Laurenta553c252009-07-17 12:17:14 -0700194 keyValuePairs += mHardwareInterface->getParameters(param.toString());
195 }
196
197 LOGV("getParameters() %s", keyValuePairs.string());
198 return keyValuePairs;
199}
200
201size_t A2dpAudioInterface::getInputBufferSize(uint32_t sampleRate, int format, int channelCount)
202{
203 return mHardwareInterface->getInputBufferSize(sampleRate, format, channelCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204}
205
206status_t A2dpAudioInterface::setVoiceVolume(float v)
207{
Eric Laurenta553c252009-07-17 12:17:14 -0700208 return mHardwareInterface->setVoiceVolume(v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209}
210
211status_t A2dpAudioInterface::setMasterVolume(float v)
212{
Eric Laurenta553c252009-07-17 12:17:14 -0700213 return mHardwareInterface->setMasterVolume(v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214}
215
216status_t A2dpAudioInterface::dump(int fd, const Vector<String16>& args)
217{
Eric Laurenta553c252009-07-17 12:17:14 -0700218 return mHardwareInterface->dumpState(fd, args);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219}
220
221// ----------------------------------------------------------------------------
222
223A2dpAudioInterface::A2dpAudioStreamOut::A2dpAudioStreamOut() :
Nick Pelly0827c812009-04-02 01:21:13 -0700224 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 Laurent80a6a222009-10-08 10:58:19 -0700227 mBluetoothEnabled(true), mDevice(0), mClosing(false), mSuspended(false)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228{
229 // use any address by default
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700230 strcpy(mA2dpAddress, "00:00:00:00:00:00");
231 init();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232}
233
234status_t A2dpAudioInterface::A2dpAudioStreamOut::set(
Eric Laurenta553c252009-07-17 12:17:14 -0700235 uint32_t device, int *pFormat, uint32_t *pChannels, uint32_t *pRate)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236{
Eric Laurenta553c252009-07-17 12:17:14 -0700237 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 Project9066cfe2009-03-03 19:31:44 -0800242
243 // fix up defaults
Eric Laurenta553c252009-07-17 12:17:14 -0700244 if (lFormat == 0) lFormat = format();
245 if (lChannels == 0) lChannels = channels();
246 if (lRate == 0) lRate = sampleRate();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247
248 // check values
Eric Laurenta553c252009-07-17 12:17:14 -0700249 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 Project9066cfe2009-03-03 19:31:44 -0800255 return BAD_VALUE;
Eric Laurenta553c252009-07-17 12:17:14 -0700256 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257
Eric Laurenta553c252009-07-17 12:17:14 -0700258 if (pFormat) *pFormat = lFormat;
259 if (pChannels) *pChannels = lChannels;
260 if (pRate) *pRate = lRate;
261
262 mDevice = device;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 return NO_ERROR;
264}
265
266A2dpAudioInterface::A2dpAudioStreamOut::~A2dpAudioStreamOut()
267{
Eric Laurenta553c252009-07-17 12:17:14 -0700268 LOGV("A2dpAudioStreamOut destructor");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 close();
Eric Laurenta553c252009-07-17 12:17:14 -0700270 LOGV("A2dpAudioStreamOut destructor returning from close()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271}
272
273ssize_t A2dpAudioInterface::A2dpAudioStreamOut::write(const void* buffer, size_t bytes)
Nick Pelly0827c812009-04-02 01:21:13 -0700274{
Nick Pelly0827c812009-04-02 01:21:13 -0700275 status_t status = -1;
Eric Laurent6dc08b32010-11-23 17:59:39 -0800276 {
277 Mutex::Autolock lock(mLock);
Nick Pelly0827c812009-04-02 01:21:13 -0700278
Eric Laurent6dc08b32010-11-23 17:59:39 -0800279 size_t remaining = bytes;
Nick Pelly0827c812009-04-02 01:21:13 -0700280
Eric Laurent6dc08b32010-11-23 17:59:39 -0800281 if (!mBluetoothEnabled || mClosing || mSuspended) {
282 LOGV("A2dpAudioStreamOut::write(), but bluetooth disabled \
283 mBluetoothEnabled %d, mClosing %d, mSuspended %d",
284 mBluetoothEnabled, mClosing, mSuspended);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285 goto Error;
286 }
Eric Laurent6dc08b32010-11-23 17:59:39 -0800287
288 if (mStandby) {
289 acquire_wake_lock (PARTIAL_WAKE_LOCK, sA2dpWakeLock);
290 mStandby = false;
291 }
292
293 status = init();
294 if (status < 0)
295 goto Error;
296
297 int retries = MAX_WRITE_RETRIES;
298 while (remaining > 0 && retries) {
299 status = a2dp_write(mData, buffer, remaining);
300 if (status < 0) {
301 LOGE("a2dp_write failed err: %d\n", status);
302 goto Error;
303 }
304 if (status == 0) {
305 retries--;
306 }
307 remaining -= status;
308 buffer = (char *)buffer + status;
309 }
310
311 return bytes;
312
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314Error:
Eric Laurent6dc08b32010-11-23 17:59:39 -0800315
316 standby();
317
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 // Simulate audio output timing in case of error
Jean-Michel Trivi2f22d352010-04-20 12:12:13 -0700319 usleep(((bytes * 1000 )/ frameSize() / sampleRate()) * 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320
321 return status;
322}
323
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700324status_t A2dpAudioInterface::A2dpAudioStreamOut::init()
325{
326 if (!mData) {
327 status_t status = a2dp_init(44100, 2, &mData);
328 if (status < 0) {
329 LOGE("a2dp_init failed err: %d\n", status);
330 mData = NULL;
331 return status;
332 }
333 a2dp_set_sink(mData, mA2dpAddress);
334 }
335
336 return 0;
337}
338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339status_t A2dpAudioInterface::A2dpAudioStreamOut::standby()
340{
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700341 Mutex::Autolock lock(mLock);
Eric Laurent6dc08b32010-11-23 17:59:39 -0800342 return standby_l();
343}
344
345status_t A2dpAudioInterface::A2dpAudioStreamOut::standby_l()
346{
347 int result = NO_ERROR;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 if (!mStandby) {
Eric Laurent6dc08b32010-11-23 17:59:39 -0800350 LOGV_IF(mClosing || !mBluetoothEnabled, "Standby skip stop: closing %d enabled %d",
351 mClosing, mBluetoothEnabled);
352 if (!mClosing && mBluetoothEnabled) {
353 result = a2dp_stop(mData);
354 }
355 release_wake_lock(sA2dpWakeLock);
356 mStandby = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 }
358
359 return result;
360}
361
Eric Laurenta553c252009-07-17 12:17:14 -0700362status_t A2dpAudioInterface::A2dpAudioStreamOut::setParameters(const String8& keyValuePairs)
363{
364 AudioParameter param = AudioParameter(keyValuePairs);
365 String8 value;
366 String8 key = String8("a2dp_sink_address");
367 status_t status = NO_ERROR;
368 int device;
369 LOGV("A2dpAudioStreamOut::setParameters() %s", keyValuePairs.string());
370
371 if (param.get(key, value) == NO_ERROR) {
372 if (value.length() != strlen("00:00:00:00:00:00")) {
373 status = BAD_VALUE;
374 } else {
375 setAddress(value.string());
376 }
377 param.remove(key);
378 }
Eric Laurentfe4fc912009-08-12 05:49:58 -0700379 key = String8("closing");
380 if (param.get(key, value) == NO_ERROR) {
381 mClosing = (value == "true");
Eric Laurent6dc08b32010-11-23 17:59:39 -0800382 if (mClosing) {
383 standby();
384 }
Eric Laurentfe4fc912009-08-12 05:49:58 -0700385 param.remove(key);
386 }
Eric Laurenta553c252009-07-17 12:17:14 -0700387 key = AudioParameter::keyRouting;
388 if (param.getInt(key, device) == NO_ERROR) {
389 if (AudioSystem::isA2dpDevice((AudioSystem::audio_devices)device)) {
390 mDevice = device;
391 status = NO_ERROR;
392 } else {
393 status = BAD_VALUE;
394 }
395 param.remove(key);
396 }
397
398 if (param.size()) {
399 status = BAD_VALUE;
400 }
401 return status;
402}
403
404String8 A2dpAudioInterface::A2dpAudioStreamOut::getParameters(const String8& keys)
405{
406 AudioParameter param = AudioParameter(keys);
407 String8 value;
408 String8 key = String8("a2dp_sink_address");
409
410 if (param.get(key, value) == NO_ERROR) {
411 value = mA2dpAddress;
412 param.add(key, value);
413 }
414 key = AudioParameter::keyRouting;
415 if (param.get(key, value) == NO_ERROR) {
416 param.addInt(key, (int)mDevice);
417 }
418
419 LOGV("A2dpAudioStreamOut::getParameters() %s", param.toString().string());
420 return param.toString();
421}
422
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423status_t A2dpAudioInterface::A2dpAudioStreamOut::setAddress(const char* address)
424{
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700425 Mutex::Autolock lock(mLock);
426
427 if (strlen(address) != strlen("00:00:00:00:00:00"))
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 return -EINVAL;
429
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700430 strcpy(mA2dpAddress, address);
431 if (mData)
432 a2dp_set_sink(mData, mA2dpAddress);
433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434 return NO_ERROR;
435}
436
Nick Pelly0827c812009-04-02 01:21:13 -0700437status_t A2dpAudioInterface::A2dpAudioStreamOut::setBluetoothEnabled(bool enabled)
438{
439 LOGD("setBluetoothEnabled %d", enabled);
440
441 Mutex::Autolock lock(mLock);
442
443 mBluetoothEnabled = enabled;
444 if (!enabled) {
445 return close_l();
446 }
447 return NO_ERROR;
448}
449
Eric Laurent80a6a222009-10-08 10:58:19 -0700450status_t A2dpAudioInterface::A2dpAudioStreamOut::setSuspended(bool onOff)
451{
452 LOGV("setSuspended %d", onOff);
453 mSuspended = onOff;
454 standby();
455 return NO_ERROR;
456}
457
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458status_t A2dpAudioInterface::A2dpAudioStreamOut::close()
459{
Nick Pelly0827c812009-04-02 01:21:13 -0700460 Mutex::Autolock lock(mLock);
Eric Laurenta553c252009-07-17 12:17:14 -0700461 LOGV("A2dpAudioStreamOut::close() calling close_l()");
Nick Pelly0827c812009-04-02 01:21:13 -0700462 return close_l();
463}
464
465status_t A2dpAudioInterface::A2dpAudioStreamOut::close_l()
466{
Eric Laurent6dc08b32010-11-23 17:59:39 -0800467 standby_l();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468 if (mData) {
Eric Laurenta553c252009-07-17 12:17:14 -0700469 LOGV("A2dpAudioStreamOut::close_l() calling a2dp_cleanup(mData)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 a2dp_cleanup(mData);
471 mData = NULL;
472 }
473 return NO_ERROR;
474}
475
476status_t A2dpAudioInterface::A2dpAudioStreamOut::dump(int fd, const Vector<String16>& args)
477{
478 return NO_ERROR;
479}
480
Eric Laurent0986e792010-01-19 17:37:09 -0800481status_t A2dpAudioInterface::A2dpAudioStreamOut::getRenderPosition(uint32_t *driverFrames)
482{
483 //TODO: enable when supported by driver
484 return INVALID_OPERATION;
485}
486
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487}; // namespace android