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>
24 #include "A2dpAudioInterface.h"
25 #include "audio/liba2dp.h"
26 #include <hardware_legacy/power.h>
29 namespace android_audio_legacy {
31 static const char *sA2dpWakeLock = "A2dpOutputStream";
32 #define MAX_WRITE_RETRIES 5
34 // ----------------------------------------------------------------------------
36 //AudioHardwareInterface* A2dpAudioInterface::createA2dpInterface()
38 // AudioHardwareInterface* hw = 0;
40 // hw = AudioHardwareInterface::create();
41 // ALOGD("new A2dpAudioInterface(hw: %p)", hw);
42 // hw = new A2dpAudioInterface(hw);
46 A2dpAudioInterface::A2dpAudioInterface(AudioHardwareInterface* hw) :
47 mOutput(0), mHardwareInterface(hw), mBluetoothEnabled(true), mSuspended(false)
51 A2dpAudioInterface::~A2dpAudioInterface()
53 closeOutputStream((AudioStreamOut *)mOutput);
54 delete mHardwareInterface;
57 status_t A2dpAudioInterface::initCheck()
59 if (mHardwareInterface == 0) return NO_INIT;
60 return mHardwareInterface->initCheck();
63 AudioStreamOut* A2dpAudioInterface::openOutputStream(
64 uint32_t devices, int *format, uint32_t *channels, uint32_t *sampleRate, status_t *status)
66 if (!audio_is_a2dp_device(devices)) {
67 ALOGV("A2dpAudioInterface::openOutputStream() open HW device: %x", devices);
68 return mHardwareInterface->openOutputStream(devices, format, channels, sampleRate, status);
73 // only one output stream allowed
80 // create new output stream
81 A2dpAudioStreamOut* out = new A2dpAudioStreamOut();
82 if ((err = out->set(devices, format, channels, sampleRate)) == NO_ERROR) {
84 mOutput->setBluetoothEnabled(mBluetoothEnabled);
85 mOutput->setSuspended(mSuspended);
95 void A2dpAudioInterface::closeOutputStream(AudioStreamOut* out) {
96 if (mOutput == 0 || mOutput != out) {
97 mHardwareInterface->closeOutputStream(out);
106 AudioStreamIn* A2dpAudioInterface::openInputStream(
107 uint32_t devices, int *format, uint32_t *channels, uint32_t *sampleRate, status_t *status,
108 AudioSystem::audio_in_acoustics acoustics)
110 return mHardwareInterface->openInputStream(devices, format, channels, sampleRate, status, acoustics);
113 void A2dpAudioInterface::closeInputStream(AudioStreamIn* in)
115 return mHardwareInterface->closeInputStream(in);
118 status_t A2dpAudioInterface::setMode(int mode)
120 return mHardwareInterface->setMode(mode);
123 status_t A2dpAudioInterface::setMicMute(bool state)
125 return mHardwareInterface->setMicMute(state);
128 status_t A2dpAudioInterface::getMicMute(bool* state)
130 return mHardwareInterface->getMicMute(state);
133 status_t A2dpAudioInterface::setParameters(const String8& keyValuePairs)
135 AudioParameter param = AudioParameter(keyValuePairs);
138 status_t status = NO_ERROR;
140 ALOGV("setParameters() %s", keyValuePairs.string());
142 key = "bluetooth_enabled";
143 if (param.get(key, value) == NO_ERROR) {
144 mBluetoothEnabled = (value == "true");
146 mOutput->setBluetoothEnabled(mBluetoothEnabled);
150 key = String8("A2dpSuspended");
151 if (param.get(key, value) == NO_ERROR) {
152 mSuspended = (value == "true");
154 mOutput->setSuspended(mSuspended);
160 status_t hwStatus = mHardwareInterface->setParameters(param.toString());
161 if (status == NO_ERROR) {
169 String8 A2dpAudioInterface::getParameters(const String8& keys)
171 AudioParameter param = AudioParameter(keys);
172 AudioParameter a2dpParam = AudioParameter();
176 key = "bluetooth_enabled";
177 if (param.get(key, value) == NO_ERROR) {
178 value = mBluetoothEnabled ? "true" : "false";
179 a2dpParam.add(key, value);
182 key = "A2dpSuspended";
183 if (param.get(key, value) == NO_ERROR) {
184 value = mSuspended ? "true" : "false";
185 a2dpParam.add(key, value);
189 String8 keyValuePairs = a2dpParam.toString();
192 if (keyValuePairs != "") {
193 keyValuePairs += ";";
195 keyValuePairs += mHardwareInterface->getParameters(param.toString());
198 ALOGV("getParameters() %s", keyValuePairs.string());
199 return keyValuePairs;
202 size_t A2dpAudioInterface::getInputBufferSize(uint32_t sampleRate, int format, int channelCount)
204 return mHardwareInterface->getInputBufferSize(sampleRate, format, channelCount);
207 status_t A2dpAudioInterface::setVoiceVolume(float v)
209 return mHardwareInterface->setVoiceVolume(v);
212 status_t A2dpAudioInterface::setMasterVolume(float v)
214 return mHardwareInterface->setMasterVolume(v);
217 status_t A2dpAudioInterface::dump(int fd, const Vector<String16>& args)
219 return mHardwareInterface->dumpState(fd, args);
222 // ----------------------------------------------------------------------------
224 A2dpAudioInterface::A2dpAudioStreamOut::A2dpAudioStreamOut() :
225 mFd(-1), mStandby(true), mStartCount(0), mRetryCount(0), mData(NULL),
226 // assume BT enabled to start, this is safe because its only the
227 // enabled->disabled transition we are worried about
228 mBluetoothEnabled(true), mDevice(0), mClosing(false), mSuspended(false)
230 // use any address by default
231 strcpy(mA2dpAddress, "00:00:00:00:00:00");
235 status_t A2dpAudioInterface::A2dpAudioStreamOut::set(
236 uint32_t device, int *pFormat, uint32_t *pChannels, uint32_t *pRate)
238 int lFormat = pFormat ? *pFormat : 0;
239 uint32_t lChannels = pChannels ? *pChannels : 0;
240 uint32_t lRate = pRate ? *pRate : 0;
242 ALOGD("A2dpAudioStreamOut::set %x, %d, %d, %d\n", device, lFormat, lChannels, lRate);
245 if (lFormat == 0) lFormat = format();
246 if (lChannels == 0) lChannels = channels();
247 if (lRate == 0) lRate = sampleRate();
250 if ((lFormat != format()) ||
251 (lChannels != channels()) ||
252 (lRate != sampleRate())){
253 if (pFormat) *pFormat = format();
254 if (pChannels) *pChannels = channels();
255 if (pRate) *pRate = sampleRate();
259 if (pFormat) *pFormat = lFormat;
260 if (pChannels) *pChannels = lChannels;
261 if (pRate) *pRate = lRate;
264 mBufferDurationUs = ((bufferSize() * 1000 )/ frameSize() / sampleRate()) * 1000;
268 A2dpAudioInterface::A2dpAudioStreamOut::~A2dpAudioStreamOut()
270 ALOGV("A2dpAudioStreamOut destructor");
272 ALOGV("A2dpAudioStreamOut destructor returning from close()");
275 ssize_t A2dpAudioInterface::A2dpAudioStreamOut::write(const void* buffer, size_t bytes)
277 status_t status = -1;
279 Mutex::Autolock lock(mLock);
281 size_t remaining = bytes;
283 if (!mBluetoothEnabled || mClosing || mSuspended) {
284 ALOGV("A2dpAudioStreamOut::write(), but bluetooth disabled \
285 mBluetoothEnabled %d, mClosing %d, mSuspended %d",
286 mBluetoothEnabled, mClosing, mSuspended);
291 acquire_wake_lock (PARTIAL_WAKE_LOCK, sA2dpWakeLock);
293 mLastWriteTime = systemTime();
300 int retries = MAX_WRITE_RETRIES;
301 while (remaining > 0 && retries) {
302 status = a2dp_write(mData, buffer, remaining);
304 ALOGE("a2dp_write failed err: %d\n", status);
311 buffer = (char *)buffer + status;
314 // if A2DP sink runs abnormally fast, sleep a little so that audioflinger mixer thread
315 // does no spin and starve other threads.
316 // NOTE: It is likely that the A2DP headset is being disconnected
317 nsecs_t now = systemTime();
318 if ((uint32_t)ns2us(now - mLastWriteTime) < (mBufferDurationUs >> 2)) {
319 ALOGV("A2DP sink runs too fast");
320 usleep(mBufferDurationUs - (uint32_t)ns2us(now - mLastWriteTime));
322 mLastWriteTime = now;
330 // Simulate audio output timing in case of error
331 usleep(mBufferDurationUs);
336 status_t A2dpAudioInterface::A2dpAudioStreamOut::init()
339 status_t status = a2dp_init(44100, 2, &mData);
341 ALOGE("a2dp_init failed err: %d\n", status);
345 a2dp_set_sink(mData, mA2dpAddress);
351 status_t A2dpAudioInterface::A2dpAudioStreamOut::standby()
353 Mutex::Autolock lock(mLock);
357 status_t A2dpAudioInterface::A2dpAudioStreamOut::standby_l()
359 int result = NO_ERROR;
362 ALOGV_IF(mClosing || !mBluetoothEnabled, "Standby skip stop: closing %d enabled %d",
363 mClosing, mBluetoothEnabled);
364 if (!mClosing && mBluetoothEnabled) {
365 result = a2dp_stop(mData);
367 release_wake_lock(sA2dpWakeLock);
374 status_t A2dpAudioInterface::A2dpAudioStreamOut::setParameters(const String8& keyValuePairs)
376 AudioParameter param = AudioParameter(keyValuePairs);
378 String8 key = String8("a2dp_sink_address");
379 status_t status = NO_ERROR;
381 ALOGV("A2dpAudioStreamOut::setParameters() %s", keyValuePairs.string());
383 if (param.get(key, value) == NO_ERROR) {
384 if (value.length() != strlen("00:00:00:00:00:00")) {
387 setAddress(value.string());
391 key = String8("closing");
392 if (param.get(key, value) == NO_ERROR) {
393 mClosing = (value == "true");
399 key = AudioParameter::keyRouting;
400 if (param.getInt(key, device) == NO_ERROR) {
401 if (audio_is_a2dp_device(device)) {
416 String8 A2dpAudioInterface::A2dpAudioStreamOut::getParameters(const String8& keys)
418 AudioParameter param = AudioParameter(keys);
420 String8 key = String8("a2dp_sink_address");
422 if (param.get(key, value) == NO_ERROR) {
423 value = mA2dpAddress;
424 param.add(key, value);
426 key = AudioParameter::keyRouting;
427 if (param.get(key, value) == NO_ERROR) {
428 param.addInt(key, (int)mDevice);
431 ALOGV("A2dpAudioStreamOut::getParameters() %s", param.toString().string());
432 return param.toString();
435 status_t A2dpAudioInterface::A2dpAudioStreamOut::setAddress(const char* address)
437 Mutex::Autolock lock(mLock);
439 if (strlen(address) != strlen("00:00:00:00:00:00"))
442 strcpy(mA2dpAddress, address);
444 a2dp_set_sink(mData, mA2dpAddress);
449 status_t A2dpAudioInterface::A2dpAudioStreamOut::setBluetoothEnabled(bool enabled)
451 ALOGD("setBluetoothEnabled %d", enabled);
453 Mutex::Autolock lock(mLock);
455 mBluetoothEnabled = enabled;
462 status_t A2dpAudioInterface::A2dpAudioStreamOut::setSuspended(bool onOff)
464 ALOGV("setSuspended %d", onOff);
470 status_t A2dpAudioInterface::A2dpAudioStreamOut::close()
472 Mutex::Autolock lock(mLock);
473 ALOGV("A2dpAudioStreamOut::close() calling close_l()");
477 status_t A2dpAudioInterface::A2dpAudioStreamOut::close_l()
481 ALOGV("A2dpAudioStreamOut::close_l() calling a2dp_cleanup(mData)");
488 status_t A2dpAudioInterface::A2dpAudioStreamOut::dump(int fd, const Vector<String16>& args)
493 status_t A2dpAudioInterface::A2dpAudioStreamOut::getRenderPosition(uint32_t *driverFrames)
495 //TODO: enable when supported by driver
496 return INVALID_OPERATION;
499 }; // namespace android