The Android Open Source Project | e09fd9e | 2008-12-17 18:05:43 -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); |
| 51 | Mutex::Autolock lock(mLock); |
| 52 | status_t err = 0; |
| 53 | |
| 54 | // only one output stream allowed |
| 55 | if (mOutput) { |
| 56 | if (status) |
| 57 | *status = -1; |
| 58 | return NULL; |
| 59 | } |
| 60 | |
| 61 | // create new output stream |
| 62 | A2dpAudioStreamOut* out = new A2dpAudioStreamOut(); |
| 63 | if ((err = out->set(format, channelCount, sampleRate)) == NO_ERROR) { |
| 64 | mOutput = out; |
| 65 | } else { |
| 66 | delete out; |
| 67 | } |
| 68 | |
| 69 | if (status) |
| 70 | *status = err; |
| 71 | return mOutput; |
| 72 | } |
| 73 | |
| 74 | AudioStreamIn* A2dpAudioInterface::openInputStream( |
The Android Open Source Project | d2bd26d | 2009-02-19 10:57:31 -0800 | [diff] [blame^] | 75 | int format, int channelCount, uint32_t sampleRate, status_t *status, |
| 76 | AudioSystem::audio_in_acoustics acoustics) |
The Android Open Source Project | e09fd9e | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 77 | { |
| 78 | if (status) |
| 79 | *status = -1; |
| 80 | return NULL; |
| 81 | } |
| 82 | |
The Android Open Source Project | e09fd9e | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 83 | status_t A2dpAudioInterface::setMicMute(bool state) |
| 84 | { |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | status_t A2dpAudioInterface::getMicMute(bool* state) |
| 89 | { |
| 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | status_t A2dpAudioInterface::setParameter(const char *key, const char *value) |
| 94 | { |
| 95 | LOGD("setParameter %s,%s\n", key, value); |
The Android Open Source Project | 8a7a675 | 2009-01-15 16:12:10 -0800 | [diff] [blame] | 96 | |
| 97 | if (!key || !value) |
| 98 | return -EINVAL; |
| 99 | |
| 100 | if (strcmp(key, "a2dp_sink_address") == 0) { |
| 101 | return mOutput->setAddress(value); |
| 102 | } |
The Android Open Source Project | a6938ba | 2009-02-10 15:44:00 -0800 | [diff] [blame] | 103 | if (strcmp(key, "bluetooth_enabled") == 0 && |
| 104 | strcmp(value, "false") == 0) { |
| 105 | return mOutput->close(); |
| 106 | } |
The Android Open Source Project | 8a7a675 | 2009-01-15 16:12:10 -0800 | [diff] [blame] | 107 | |
The Android Open Source Project | e09fd9e | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | status_t A2dpAudioInterface::setVoiceVolume(float v) |
| 112 | { |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | status_t A2dpAudioInterface::setMasterVolume(float v) |
| 117 | { |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | status_t A2dpAudioInterface::doRouting() |
| 122 | { |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | status_t A2dpAudioInterface::dump(int fd, const Vector<String16>& args) |
| 127 | { |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | // ---------------------------------------------------------------------------- |
| 132 | |
| 133 | A2dpAudioInterface::A2dpAudioStreamOut::A2dpAudioStreamOut() : |
The Android Open Source Project | 5f78a48 | 2009-01-20 14:03:58 -0800 | [diff] [blame] | 134 | mFd(-1), mStandby(true), mStartCount(0), mRetryCount(0), mData(NULL), |
The Android Open Source Project | 2762932 | 2009-01-09 17:51:23 -0800 | [diff] [blame] | 135 | mInitialized(false) |
The Android Open Source Project | e09fd9e | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 136 | { |
The Android Open Source Project | 8a7a675 | 2009-01-15 16:12:10 -0800 | [diff] [blame] | 137 | // use any address by default |
| 138 | strncpy(mA2dpAddress, "00:00:00:00:00:00", sizeof(mA2dpAddress)); |
The Android Open Source Project | e09fd9e | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | status_t A2dpAudioInterface::A2dpAudioStreamOut::set( |
| 142 | int format, int channels, uint32_t rate) |
| 143 | { |
| 144 | LOGD("A2dpAudioStreamOut::set %d, %d, %d\n", format, channels, rate); |
| 145 | |
| 146 | // fix up defaults |
| 147 | if (format == 0) format = AudioSystem::PCM_16_BIT; |
| 148 | if (channels == 0) channels = channelCount(); |
| 149 | if (rate == 0) rate = sampleRate(); |
| 150 | |
| 151 | // check values |
| 152 | if ((format != AudioSystem::PCM_16_BIT) || |
| 153 | (channels != channelCount()) || |
| 154 | (rate != sampleRate())) |
| 155 | return BAD_VALUE; |
| 156 | |
| 157 | return NO_ERROR; |
| 158 | } |
| 159 | |
| 160 | A2dpAudioInterface::A2dpAudioStreamOut::~A2dpAudioStreamOut() |
| 161 | { |
The Android Open Source Project | a6938ba | 2009-02-10 15:44:00 -0800 | [diff] [blame] | 162 | close(); |
The Android Open Source Project | e09fd9e | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | ssize_t A2dpAudioInterface::A2dpAudioStreamOut::write(const void* buffer, size_t bytes) |
| 166 | { |
The Android Open Source Project | 2762932 | 2009-01-09 17:51:23 -0800 | [diff] [blame] | 167 | status_t status = NO_INIT; |
| 168 | size_t remaining = bytes; |
| 169 | |
The Android Open Source Project | e09fd9e | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 170 | if (!mInitialized) { |
The Android Open Source Project | 8a7a675 | 2009-01-15 16:12:10 -0800 | [diff] [blame] | 171 | status = a2dp_init(mA2dpAddress, 44100, 2, &mData); |
The Android Open Source Project | 2762932 | 2009-01-09 17:51:23 -0800 | [diff] [blame] | 172 | if (status < 0) { |
| 173 | LOGE("a2dp_init failed err: %d\n", status); |
| 174 | goto Error; |
| 175 | } |
The Android Open Source Project | e09fd9e | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 176 | mInitialized = true; |
| 177 | } |
| 178 | |
The Android Open Source Project | e09fd9e | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 179 | while (remaining > 0) { |
The Android Open Source Project | 2762932 | 2009-01-09 17:51:23 -0800 | [diff] [blame] | 180 | status = a2dp_write(mData, buffer, remaining); |
| 181 | if (status <= 0) { |
| 182 | LOGE("a2dp_write failed err: %d\n", status); |
| 183 | goto Error; |
| 184 | } |
| 185 | remaining -= status; |
| 186 | buffer = ((char *)buffer) + status; |
The Android Open Source Project | e09fd9e | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 187 | } |
The Android Open Source Project | 2762932 | 2009-01-09 17:51:23 -0800 | [diff] [blame] | 188 | |
| 189 | mStandby = false; |
The Android Open Source Project | e09fd9e | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 190 | |
| 191 | return bytes; |
The Android Open Source Project | 2762932 | 2009-01-09 17:51:23 -0800 | [diff] [blame] | 192 | |
The Android Open Source Project | a6938ba | 2009-02-10 15:44:00 -0800 | [diff] [blame] | 193 | Error: |
| 194 | close(); |
The Android Open Source Project | 2762932 | 2009-01-09 17:51:23 -0800 | [diff] [blame] | 195 | // Simulate audio output timing in case of error |
| 196 | usleep(bytes * 1000000 / frameSize() / sampleRate()); |
| 197 | |
| 198 | return status; |
The Android Open Source Project | e09fd9e | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | status_t A2dpAudioInterface::A2dpAudioStreamOut::standby() |
| 202 | { |
The Android Open Source Project | 2762932 | 2009-01-09 17:51:23 -0800 | [diff] [blame] | 203 | int result = 0; |
| 204 | |
| 205 | if (!mStandby) { |
| 206 | result = a2dp_stop(mData); |
| 207 | if (result == 0) |
| 208 | mStandby = true; |
| 209 | } |
| 210 | |
| 211 | return result; |
The Android Open Source Project | e09fd9e | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 212 | } |
| 213 | |
The Android Open Source Project | 8a7a675 | 2009-01-15 16:12:10 -0800 | [diff] [blame] | 214 | status_t A2dpAudioInterface::A2dpAudioStreamOut::setAddress(const char* address) |
| 215 | { |
| 216 | if (strlen(address) < sizeof(mA2dpAddress)) |
| 217 | return -EINVAL; |
| 218 | |
| 219 | if (strcmp(address, mA2dpAddress)) { |
| 220 | strcpy(mA2dpAddress, address); |
The Android Open Source Project | a6938ba | 2009-02-10 15:44:00 -0800 | [diff] [blame] | 221 | close(); |
The Android Open Source Project | 8a7a675 | 2009-01-15 16:12:10 -0800 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | return NO_ERROR; |
| 225 | } |
| 226 | |
The Android Open Source Project | a6938ba | 2009-02-10 15:44:00 -0800 | [diff] [blame] | 227 | status_t A2dpAudioInterface::A2dpAudioStreamOut::close() |
| 228 | { |
| 229 | if (mData) { |
| 230 | a2dp_cleanup(mData); |
| 231 | mData = NULL; |
| 232 | mInitialized = false; |
| 233 | } |
| 234 | return NO_ERROR; |
| 235 | } |
| 236 | |
The Android Open Source Project | e09fd9e | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 237 | status_t A2dpAudioInterface::A2dpAudioStreamOut::dump(int fd, const Vector<String16>& args) |
| 238 | { |
| 239 | return NO_ERROR; |
| 240 | } |
| 241 | |
| 242 | |
| 243 | }; // namespace android |