2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 //#define LOG_NDEBUG 0
20 #define LOG_TAG "A2dpAudioInterface"
21 #include <utils/Log.h>
22 #include <utils/String8.h>
23 #include <utils/Timers.h>
25 #include "A2dpAudioInterface.h"
26 #include "audio/liba2dp.h"
27 #include <hardware_legacy/power.h>
30 namespace android_audio_legacy {
32 static const char *sA2dpWakeLock = "A2dpOutputStream";
33 #define MAX_WRITE_RETRIES 5
35 // ----------------------------------------------------------------------------
37 //AudioHardwareInterface* A2dpAudioInterface::createA2dpInterface()
39 // AudioHardwareInterface* hw = 0;
41 // hw = AudioHardwareInterface::create();
42 // ALOGD("new A2dpAudioInterface(hw: %p)", hw);
43 // hw = new A2dpAudioInterface(hw);
47 A2dpAudioInterface::A2dpAudioInterface(AudioHardwareInterface* hw) :
48 mOutput(0), mHardwareInterface(hw), mBluetoothEnabled(true), mSuspended(false)
52 A2dpAudioInterface::~A2dpAudioInterface()
54 closeOutputStream((AudioStreamOut *)mOutput);
55 delete mHardwareInterface;
58 status_t A2dpAudioInterface::initCheck()
60 if (mHardwareInterface == 0) return NO_INIT;
61 return mHardwareInterface->initCheck();
64 AudioStreamOut* A2dpAudioInterface::openOutputStream(
65 uint32_t devices, int *format, uint32_t *channels, uint32_t *sampleRate, status_t *status)
67 if (!audio_is_a2dp_out_device(devices)) {
68 ALOGV("A2dpAudioInterface::openOutputStream() open HW device: %x", devices);
69 return mHardwareInterface->openOutputStream(devices, format, channels, sampleRate, status);
74 // only one output stream allowed
81 // create new output stream
82 A2dpAudioStreamOut* out = new A2dpAudioStreamOut();
83 if ((err = out->set(devices, format, channels, sampleRate)) == NO_ERROR) {
85 mOutput->setBluetoothEnabled(mBluetoothEnabled);
86 mOutput->setSuspended(mSuspended);
96 void A2dpAudioInterface::closeOutputStream(AudioStreamOut* out) {
97 if (mOutput == 0 || mOutput != out) {
98 mHardwareInterface->closeOutputStream(out);
107 AudioStreamIn* A2dpAudioInterface::openInputStream(
108 uint32_t devices, int *format, uint32_t *channels, uint32_t *sampleRate, status_t *status,
109 AudioSystem::audio_in_acoustics acoustics)
111 return mHardwareInterface->openInputStream(devices, format, channels, sampleRate, status, acoustics);
114 void A2dpAudioInterface::closeInputStream(AudioStreamIn* in)
116 return mHardwareInterface->closeInputStream(in);
119 status_t A2dpAudioInterface::setMode(int mode)
121 return mHardwareInterface->setMode(mode);
124 status_t A2dpAudioInterface::setMicMute(bool state)
126 return mHardwareInterface->setMicMute(state);
129 status_t A2dpAudioInterface::getMicMute(bool* state)
131 return mHardwareInterface->getMicMute(state);
134 status_t A2dpAudioInterface::setParameters(const String8& keyValuePairs)
136 AudioParameter param = AudioParameter(keyValuePairs);
139 status_t status = NO_ERROR;
141 ALOGV("setParameters() %s", keyValuePairs.string());
143 key = "bluetooth_enabled";
144 if (param.get(key, value) == NO_ERROR) {
145 mBluetoothEnabled = (value == "true");
147 mOutput->setBluetoothEnabled(mBluetoothEnabled);
151 key = String8("A2dpSuspended");
152 if (param.get(key, value) == NO_ERROR) {
153 mSuspended = (value == "true");
155 mOutput->setSuspended(mSuspended);
161 status_t hwStatus = mHardwareInterface->setParameters(param.toString());
162 if (status == NO_ERROR) {
170 String8 A2dpAudioInterface::getParameters(const String8& keys)
172 AudioParameter param = AudioParameter(keys);
173 AudioParameter a2dpParam = AudioParameter();
177 key = "bluetooth_enabled";
178 if (param.get(key, value) == NO_ERROR) {
179 value = mBluetoothEnabled ? "true" : "false";
180 a2dpParam.add(key, value);
183 key = "A2dpSuspended";
184 if (param.get(key, value) == NO_ERROR) {
185 value = mSuspended ? "true" : "false";
186 a2dpParam.add(key, value);
190 String8 keyValuePairs = a2dpParam.toString();
193 if (keyValuePairs != "") {
194 keyValuePairs += ";";
196 keyValuePairs += mHardwareInterface->getParameters(param.toString());
199 ALOGV("getParameters() %s", keyValuePairs.string());
200 return keyValuePairs;
203 size_t A2dpAudioInterface::getInputBufferSize(uint32_t sampleRate, int format, int channelCount)
205 return mHardwareInterface->getInputBufferSize(sampleRate, format, channelCount);
208 status_t A2dpAudioInterface::setVoiceVolume(float v)
210 return mHardwareInterface->setVoiceVolume(v);
213 status_t A2dpAudioInterface::setMasterVolume(float v)
215 return mHardwareInterface->setMasterVolume(v);
218 status_t A2dpAudioInterface::dump(int fd, const Vector<String16>& args)
220 return mHardwareInterface->dumpState(fd, args);
223 // ----------------------------------------------------------------------------
225 A2dpAudioInterface::A2dpAudioStreamOut::A2dpAudioStreamOut() :
226 mFd(-1), mStandby(true), mStartCount(0), mRetryCount(0), mData(NULL),
227 // assume BT enabled to start, this is safe because its only the
228 // enabled->disabled transition we are worried about
229 mBluetoothEnabled(true), mDevice(0), mClosing(false), mSuspended(false)
231 // use any address by default
232 strcpy(mA2dpAddress, "00:00:00:00:00:00");
236 status_t A2dpAudioInterface::A2dpAudioStreamOut::set(
237 uint32_t device, int *pFormat, uint32_t *pChannels, uint32_t *pRate)
239 int lFormat = pFormat ? *pFormat : 0;
240 uint32_t lChannels = pChannels ? *pChannels : 0;
241 uint32_t lRate = pRate ? *pRate : 0;
243 ALOGD("A2dpAudioStreamOut::set %x, %d, %d, %d\n", device, lFormat, lChannels, lRate);
246 if (lFormat == 0) lFormat = format();
247 if (lChannels == 0) lChannels = channels();
248 if (lRate == 0) lRate = sampleRate();
251 if ((lFormat != format()) ||
252 (lChannels != channels()) ||
253 (lRate != sampleRate())){
254 if (pFormat) *pFormat = format();
255 if (pChannels) *pChannels = channels();
256 if (pRate) *pRate = sampleRate();
260 if (pFormat) *pFormat = lFormat;
261 if (pChannels) *pChannels = lChannels;
262 if (pRate) *pRate = lRate;
265 mBufferDurationUs = ((bufferSize() * 1000 )/ frameSize() / sampleRate()) * 1000;
269 A2dpAudioInterface::A2dpAudioStreamOut::~A2dpAudioStreamOut()
271 ALOGV("A2dpAudioStreamOut destructor");
273 ALOGV("A2dpAudioStreamOut destructor returning from close()");
276 ssize_t A2dpAudioInterface::A2dpAudioStreamOut::write(const void* buffer, size_t bytes)
278 status_t status = -1;
280 Mutex::Autolock lock(mLock);
282 size_t remaining = bytes;
284 if (!mBluetoothEnabled || mClosing || mSuspended) {
285 ALOGV("A2dpAudioStreamOut::write(), but bluetooth disabled \
286 mBluetoothEnabled %d, mClosing %d, mSuspended %d",
287 mBluetoothEnabled, mClosing, mSuspended);
292 acquire_wake_lock (PARTIAL_WAKE_LOCK, sA2dpWakeLock);
294 mLastWriteTime = systemTime();
301 int retries = MAX_WRITE_RETRIES;
302 while (remaining > 0 && retries) {
303 status = a2dp_write(mData, buffer, remaining);
305 ALOGE("a2dp_write failed err: %d\n", status);
312 buffer = (char *)buffer + status;
315 // if A2DP sink runs abnormally fast, sleep a little so that audioflinger mixer thread
316 // does no spin and starve other threads.
317 // NOTE: It is likely that the A2DP headset is being disconnected
318 nsecs_t now = systemTime();
319 if ((uint32_t)ns2us(now - mLastWriteTime) < (mBufferDurationUs >> 2)) {
320 ALOGV("A2DP sink runs too fast");
321 usleep(mBufferDurationUs - (uint32_t)ns2us(now - mLastWriteTime));
323 mLastWriteTime = now;
331 // Simulate audio output timing in case of error
332 usleep(mBufferDurationUs);
337 status_t A2dpAudioInterface::A2dpAudioStreamOut::init()
340 status_t status = a2dp_init(44100, 2, &mData);
342 ALOGE("a2dp_init failed err: %d\n", status);
346 a2dp_set_sink(mData, mA2dpAddress);
352 status_t A2dpAudioInterface::A2dpAudioStreamOut::standby()
354 Mutex::Autolock lock(mLock);
358 status_t A2dpAudioInterface::A2dpAudioStreamOut::standby_l()
360 int result = NO_ERROR;
363 ALOGV_IF(mClosing || !mBluetoothEnabled, "Standby skip stop: closing %d enabled %d",
364 mClosing, mBluetoothEnabled);
365 if (!mClosing && mBluetoothEnabled) {
366 result = a2dp_stop(mData);
368 release_wake_lock(sA2dpWakeLock);
375 status_t A2dpAudioInterface::A2dpAudioStreamOut::setParameters(const String8& keyValuePairs)
377 AudioParameter param = AudioParameter(keyValuePairs);
379 String8 key = String8("a2dp_sink_address");
380 status_t status = NO_ERROR;
382 ALOGV("A2dpAudioStreamOut::setParameters() %s", keyValuePairs.string());
384 if (param.get(key, value) == NO_ERROR) {
385 if (value.length() != strlen("00:00:00:00:00:00")) {
388 setAddress(value.string());
392 key = String8("closing");
393 if (param.get(key, value) == NO_ERROR) {
394 mClosing = (value == "true");
400 key = AudioParameter::keyRouting;
401 if (param.getInt(key, device) == NO_ERROR) {
402 if (audio_is_a2dp_out_device(device)) {
417 String8 A2dpAudioInterface::A2dpAudioStreamOut::getParameters(const String8& keys)
419 AudioParameter param = AudioParameter(keys);
421 String8 key = String8("a2dp_sink_address");
423 if (param.get(key, value) == NO_ERROR) {
424 value = mA2dpAddress;
425 param.add(key, value);
427 key = AudioParameter::keyRouting;
428 if (param.get(key, value) == NO_ERROR) {
429 param.addInt(key, (int)mDevice);
432 ALOGV("A2dpAudioStreamOut::getParameters() %s", param.toString().string());
433 return param.toString();
436 status_t A2dpAudioInterface::A2dpAudioStreamOut::setAddress(const char* address)
438 Mutex::Autolock lock(mLock);
440 if (strlen(address) != strlen("00:00:00:00:00:00"))
443 strcpy(mA2dpAddress, address);
445 a2dp_set_sink(mData, mA2dpAddress);
450 status_t A2dpAudioInterface::A2dpAudioStreamOut::setBluetoothEnabled(bool enabled)
452 ALOGD("setBluetoothEnabled %d", enabled);
454 Mutex::Autolock lock(mLock);
456 mBluetoothEnabled = enabled;
463 status_t A2dpAudioInterface::A2dpAudioStreamOut::setSuspended(bool onOff)
465 ALOGV("setSuspended %d", onOff);
471 status_t A2dpAudioInterface::A2dpAudioStreamOut::close()
473 Mutex::Autolock lock(mLock);
474 ALOGV("A2dpAudioStreamOut::close() calling close_l()");
478 status_t A2dpAudioInterface::A2dpAudioStreamOut::close_l()
482 ALOGV("A2dpAudioStreamOut::close_l() calling a2dp_cleanup(mData)");
489 status_t A2dpAudioInterface::A2dpAudioStreamOut::dump(int fd, const Vector<String16>& args)
494 status_t A2dpAudioInterface::A2dpAudioStreamOut::getRenderPosition(uint32_t *driverFrames)
496 //TODO: enable when supported by driver
497 return INVALID_OPERATION;
500 }; // namespace android