The Android Open Source Project | edbf3b6 | 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 | |
| 19 | #define LOG_NDEBUG 0 |
| 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" |
| 26 | |
| 27 | |
| 28 | namespace android { |
| 29 | |
| 30 | // ---------------------------------------------------------------------------- |
| 31 | |
| 32 | A2dpAudioInterface::A2dpAudioInterface() : |
| 33 | mOutput(0) |
| 34 | { |
| 35 | } |
| 36 | |
| 37 | A2dpAudioInterface::~A2dpAudioInterface() |
| 38 | { |
| 39 | delete mOutput; |
| 40 | } |
| 41 | |
| 42 | status_t A2dpAudioInterface::initCheck() |
| 43 | { |
| 44 | return 0; |
| 45 | } |
| 46 | |
| 47 | AudioStreamOut* A2dpAudioInterface::openOutputStream( |
| 48 | int format, int channelCount, uint32_t sampleRate, status_t *status) |
| 49 | { |
| 50 | LOGD("A2dpAudioInterface::openOutputStream %d, %d, %d\n", format, channelCount, sampleRate); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | status_t err = 0; |
| 52 | |
| 53 | // only one output stream allowed |
| 54 | if (mOutput) { |
| 55 | if (status) |
| 56 | *status = -1; |
| 57 | return NULL; |
| 58 | } |
| 59 | |
| 60 | // create new output stream |
| 61 | A2dpAudioStreamOut* out = new A2dpAudioStreamOut(); |
| 62 | if ((err = out->set(format, channelCount, sampleRate)) == NO_ERROR) { |
| 63 | mOutput = out; |
| 64 | } else { |
| 65 | delete out; |
| 66 | } |
| 67 | |
| 68 | if (status) |
| 69 | *status = err; |
| 70 | return mOutput; |
| 71 | } |
| 72 | |
| 73 | AudioStreamIn* A2dpAudioInterface::openInputStream( |
| 74 | int format, int channelCount, uint32_t sampleRate, status_t *status, |
| 75 | AudioSystem::audio_in_acoustics acoustics) |
| 76 | { |
| 77 | if (status) |
| 78 | *status = -1; |
| 79 | return NULL; |
| 80 | } |
| 81 | |
| 82 | status_t A2dpAudioInterface::setMicMute(bool state) |
| 83 | { |
| 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | status_t A2dpAudioInterface::getMicMute(bool* state) |
| 88 | { |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | status_t A2dpAudioInterface::setParameter(const char *key, const char *value) |
| 93 | { |
| 94 | LOGD("setParameter %s,%s\n", key, value); |
Nick Pelly | 42781c7 | 2009-04-02 10:30:39 -0700 | [diff] [blame] | 95 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | if (!key || !value) |
| 97 | return -EINVAL; |
Nick Pelly | 42781c7 | 2009-04-02 10:30:39 -0700 | [diff] [blame] | 98 | |
| 99 | if (strcmp(key, "a2dp_sink_address") == 0) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 | return mOutput->setAddress(value); |
| 101 | } |
Nick Pelly | 42781c7 | 2009-04-02 10:30:39 -0700 | [diff] [blame] | 102 | if (strcmp(key, "bluetooth_enabled") == 0) { |
| 103 | mOutput->setBluetoothEnabled(strcmp(value, "true") == 0); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | status_t A2dpAudioInterface::setVoiceVolume(float v) |
| 110 | { |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | status_t A2dpAudioInterface::setMasterVolume(float v) |
| 115 | { |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | status_t A2dpAudioInterface::doRouting() |
| 120 | { |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | status_t A2dpAudioInterface::dump(int fd, const Vector<String16>& args) |
| 125 | { |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | // ---------------------------------------------------------------------------- |
| 130 | |
| 131 | A2dpAudioInterface::A2dpAudioStreamOut::A2dpAudioStreamOut() : |
Nick Pelly | 42781c7 | 2009-04-02 10:30:39 -0700 | [diff] [blame] | 132 | mFd(-1), mStandby(true), mStartCount(0), mRetryCount(0), mData(NULL), |
| 133 | // assume BT enabled to start, this is safe because its only the |
| 134 | // enabled->disabled transition we are worried about |
| 135 | mBluetoothEnabled(true) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 136 | { |
| 137 | // use any address by default |
The Android Open Source Project | 22f8def | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 138 | strcpy(mA2dpAddress, "00:00:00:00:00:00"); |
| 139 | init(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | status_t A2dpAudioInterface::A2dpAudioStreamOut::set( |
| 143 | int format, int channels, uint32_t rate) |
| 144 | { |
| 145 | LOGD("A2dpAudioStreamOut::set %d, %d, %d\n", format, channels, rate); |
| 146 | |
| 147 | // fix up defaults |
| 148 | if (format == 0) format = AudioSystem::PCM_16_BIT; |
| 149 | if (channels == 0) channels = channelCount(); |
| 150 | if (rate == 0) rate = sampleRate(); |
| 151 | |
| 152 | // check values |
| 153 | if ((format != AudioSystem::PCM_16_BIT) || |
| 154 | (channels != channelCount()) || |
| 155 | (rate != sampleRate())) |
| 156 | return BAD_VALUE; |
| 157 | |
| 158 | return NO_ERROR; |
| 159 | } |
| 160 | |
| 161 | A2dpAudioInterface::A2dpAudioStreamOut::~A2dpAudioStreamOut() |
| 162 | { |
| 163 | close(); |
| 164 | } |
| 165 | |
| 166 | ssize_t A2dpAudioInterface::A2dpAudioStreamOut::write(const void* buffer, size_t bytes) |
Nick Pelly | 42781c7 | 2009-04-02 10:30:39 -0700 | [diff] [blame] | 167 | { |
The Android Open Source Project | 22f8def | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 168 | Mutex::Autolock lock(mLock); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 169 | |
The Android Open Source Project | 22f8def | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 170 | size_t remaining = bytes; |
Nick Pelly | 42781c7 | 2009-04-02 10:30:39 -0700 | [diff] [blame] | 171 | status_t status = -1; |
| 172 | |
| 173 | if (!mBluetoothEnabled) { |
| 174 | LOGW("A2dpAudioStreamOut::write(), but bluetooth disabled"); |
| 175 | goto Error; |
| 176 | } |
| 177 | |
| 178 | status = init(); |
The Android Open Source Project | 22f8def | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 179 | if (status < 0) |
| 180 | goto Error; |
Nick Pelly | 42781c7 | 2009-04-02 10:30:39 -0700 | [diff] [blame] | 181 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 182 | while (remaining > 0) { |
| 183 | status = a2dp_write(mData, buffer, remaining); |
| 184 | if (status <= 0) { |
| 185 | LOGE("a2dp_write failed err: %d\n", status); |
| 186 | goto Error; |
| 187 | } |
| 188 | remaining -= status; |
| 189 | buffer = ((char *)buffer) + status; |
| 190 | } |
| 191 | |
| 192 | mStandby = false; |
Nick Pelly | 42781c7 | 2009-04-02 10:30:39 -0700 | [diff] [blame] | 193 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 194 | return bytes; |
| 195 | |
| 196 | Error: |
| 197 | // Simulate audio output timing in case of error |
| 198 | usleep(bytes * 1000000 / frameSize() / sampleRate()); |
| 199 | |
| 200 | return status; |
| 201 | } |
| 202 | |
The Android Open Source Project | 22f8def | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 203 | status_t A2dpAudioInterface::A2dpAudioStreamOut::init() |
| 204 | { |
| 205 | if (!mData) { |
| 206 | status_t status = a2dp_init(44100, 2, &mData); |
| 207 | if (status < 0) { |
| 208 | LOGE("a2dp_init failed err: %d\n", status); |
| 209 | mData = NULL; |
| 210 | return status; |
| 211 | } |
| 212 | a2dp_set_sink(mData, mA2dpAddress); |
| 213 | } |
| 214 | |
| 215 | return 0; |
| 216 | } |
| 217 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 218 | status_t A2dpAudioInterface::A2dpAudioStreamOut::standby() |
| 219 | { |
| 220 | int result = 0; |
| 221 | |
The Android Open Source Project | 22f8def | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 222 | Mutex::Autolock lock(mLock); |
| 223 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 224 | if (!mStandby) { |
| 225 | result = a2dp_stop(mData); |
| 226 | if (result == 0) |
| 227 | mStandby = true; |
| 228 | } |
| 229 | |
| 230 | return result; |
| 231 | } |
| 232 | |
| 233 | status_t A2dpAudioInterface::A2dpAudioStreamOut::setAddress(const char* address) |
| 234 | { |
The Android Open Source Project | 22f8def | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 235 | Mutex::Autolock lock(mLock); |
| 236 | |
| 237 | if (strlen(address) != strlen("00:00:00:00:00:00")) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 238 | return -EINVAL; |
| 239 | |
The Android Open Source Project | 22f8def | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 240 | strcpy(mA2dpAddress, address); |
| 241 | if (mData) |
| 242 | a2dp_set_sink(mData, mA2dpAddress); |
| 243 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 244 | return NO_ERROR; |
| 245 | } |
| 246 | |
Nick Pelly | 42781c7 | 2009-04-02 10:30:39 -0700 | [diff] [blame] | 247 | status_t A2dpAudioInterface::A2dpAudioStreamOut::setBluetoothEnabled(bool enabled) |
| 248 | { |
| 249 | LOGD("setBluetoothEnabled %d", enabled); |
| 250 | |
| 251 | Mutex::Autolock lock(mLock); |
| 252 | |
| 253 | mBluetoothEnabled = enabled; |
| 254 | if (!enabled) { |
| 255 | return close_l(); |
| 256 | } |
| 257 | return NO_ERROR; |
| 258 | } |
| 259 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 260 | status_t A2dpAudioInterface::A2dpAudioStreamOut::close() |
| 261 | { |
Nick Pelly | 42781c7 | 2009-04-02 10:30:39 -0700 | [diff] [blame] | 262 | Mutex::Autolock lock(mLock); |
| 263 | return close_l(); |
| 264 | } |
| 265 | |
| 266 | status_t A2dpAudioInterface::A2dpAudioStreamOut::close_l() |
| 267 | { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 268 | if (mData) { |
| 269 | a2dp_cleanup(mData); |
| 270 | mData = NULL; |
| 271 | } |
| 272 | return NO_ERROR; |
| 273 | } |
| 274 | |
| 275 | status_t A2dpAudioInterface::A2dpAudioStreamOut::dump(int fd, const Vector<String16>& args) |
| 276 | { |
| 277 | return NO_ERROR; |
| 278 | } |
| 279 | |
| 280 | |
| 281 | }; // namespace android |