blob: b6d5078c44f56c38a27d60606f846d2f0e523638 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-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
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
28namespace android {
29
30// ----------------------------------------------------------------------------
31
32A2dpAudioInterface::A2dpAudioInterface() :
33 mOutput(0)
34{
35}
36
37A2dpAudioInterface::~A2dpAudioInterface()
38{
39 delete mOutput;
40}
41
42status_t A2dpAudioInterface::initCheck()
43{
44 return 0;
45}
46
47AudioStreamOut* 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 Projectedbf3b62009-03-03 19:31:44 -080051 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
73AudioStreamIn* 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
82status_t A2dpAudioInterface::setMicMute(bool state)
83{
84 return 0;
85}
86
87status_t A2dpAudioInterface::getMicMute(bool* state)
88{
89 return 0;
90}
91
92status_t A2dpAudioInterface::setParameter(const char *key, const char *value)
93{
94 LOGD("setParameter %s,%s\n", key, value);
Nick Pelly42781c72009-04-02 10:30:39 -070095
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080096 if (!key || !value)
97 return -EINVAL;
Nick Pelly42781c72009-04-02 10:30:39 -070098
99 if (strcmp(key, "a2dp_sink_address") == 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800100 return mOutput->setAddress(value);
101 }
Nick Pelly42781c72009-04-02 10:30:39 -0700102 if (strcmp(key, "bluetooth_enabled") == 0) {
103 mOutput->setBluetoothEnabled(strcmp(value, "true") == 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800104 }
105
106 return 0;
107}
108
109status_t A2dpAudioInterface::setVoiceVolume(float v)
110{
111 return 0;
112}
113
114status_t A2dpAudioInterface::setMasterVolume(float v)
115{
116 return 0;
117}
118
119status_t A2dpAudioInterface::doRouting()
120{
121 return 0;
122}
123
124status_t A2dpAudioInterface::dump(int fd, const Vector<String16>& args)
125{
126 return 0;
127}
128
129// ----------------------------------------------------------------------------
130
131A2dpAudioInterface::A2dpAudioStreamOut::A2dpAudioStreamOut() :
Nick Pelly42781c72009-04-02 10:30:39 -0700132 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 Projectedbf3b62009-03-03 19:31:44 -0800136{
137 // use any address by default
The Android Open Source Project22f8def2009-03-09 11:52:12 -0700138 strcpy(mA2dpAddress, "00:00:00:00:00:00");
139 init();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800140}
141
142status_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
161A2dpAudioInterface::A2dpAudioStreamOut::~A2dpAudioStreamOut()
162{
163 close();
164}
165
166ssize_t A2dpAudioInterface::A2dpAudioStreamOut::write(const void* buffer, size_t bytes)
Nick Pelly42781c72009-04-02 10:30:39 -0700167{
The Android Open Source Project22f8def2009-03-09 11:52:12 -0700168 Mutex::Autolock lock(mLock);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800169
The Android Open Source Project22f8def2009-03-09 11:52:12 -0700170 size_t remaining = bytes;
Nick Pelly42781c72009-04-02 10:30:39 -0700171 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 Project22f8def2009-03-09 11:52:12 -0700179 if (status < 0)
180 goto Error;
Nick Pelly42781c72009-04-02 10:30:39 -0700181
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800182 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 Pelly42781c72009-04-02 10:30:39 -0700193
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800194 return bytes;
195
196Error:
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 Project22f8def2009-03-09 11:52:12 -0700203status_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 Projectedbf3b62009-03-03 19:31:44 -0800218status_t A2dpAudioInterface::A2dpAudioStreamOut::standby()
219{
220 int result = 0;
221
The Android Open Source Project22f8def2009-03-09 11:52:12 -0700222 Mutex::Autolock lock(mLock);
223
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800224 if (!mStandby) {
225 result = a2dp_stop(mData);
226 if (result == 0)
227 mStandby = true;
228 }
229
230 return result;
231}
232
233status_t A2dpAudioInterface::A2dpAudioStreamOut::setAddress(const char* address)
234{
The Android Open Source Project22f8def2009-03-09 11:52:12 -0700235 Mutex::Autolock lock(mLock);
236
237 if (strlen(address) != strlen("00:00:00:00:00:00"))
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800238 return -EINVAL;
239
The Android Open Source Project22f8def2009-03-09 11:52:12 -0700240 strcpy(mA2dpAddress, address);
241 if (mData)
242 a2dp_set_sink(mData, mA2dpAddress);
243
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800244 return NO_ERROR;
245}
246
Nick Pelly42781c72009-04-02 10:30:39 -0700247status_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 Projectedbf3b62009-03-03 19:31:44 -0800260status_t A2dpAudioInterface::A2dpAudioStreamOut::close()
261{
Nick Pelly42781c72009-04-02 10:30:39 -0700262 Mutex::Autolock lock(mLock);
263 return close_l();
264}
265
266status_t A2dpAudioInterface::A2dpAudioStreamOut::close_l()
267{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800268 if (mData) {
269 a2dp_cleanup(mData);
270 mData = NULL;
271 }
272 return NO_ERROR;
273}
274
275status_t A2dpAudioInterface::A2dpAudioStreamOut::dump(int fd, const Vector<String16>& args)
276{
277 return NO_ERROR;
278}
279
280
281}; // namespace android