blob: 3c1803633cb0b7455cc18c48720abd322511cf70 [file] [log] [blame]
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -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);
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
74AudioStreamIn* A2dpAudioInterface::openInputStream(
75 int format, int channelCount, uint32_t sampleRate, status_t *status)
76{
77 if (status)
78 *status = -1;
79 return NULL;
80}
81
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -080082status_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);
The Android Open Source Project8a7a6752009-01-15 16:12:10 -080095
96 if (!key || !value)
97 return -EINVAL;
98
99 if (strcmp(key, "a2dp_sink_address") == 0) {
100 return mOutput->setAddress(value);
101 }
The Android Open Source Projecta6938ba2009-02-10 15:44:00 -0800102 if (strcmp(key, "bluetooth_enabled") == 0 &&
103 strcmp(value, "false") == 0) {
104 return mOutput->close();
105 }
The Android Open Source Project8a7a6752009-01-15 16:12:10 -0800106
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800107 return 0;
108}
109
110status_t A2dpAudioInterface::setVoiceVolume(float v)
111{
112 return 0;
113}
114
115status_t A2dpAudioInterface::setMasterVolume(float v)
116{
117 return 0;
118}
119
120status_t A2dpAudioInterface::doRouting()
121{
122 return 0;
123}
124
125status_t A2dpAudioInterface::dump(int fd, const Vector<String16>& args)
126{
127 return 0;
128}
129
130// ----------------------------------------------------------------------------
131
132A2dpAudioInterface::A2dpAudioStreamOut::A2dpAudioStreamOut() :
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800133 mFd(-1), mStandby(true), mStartCount(0), mRetryCount(0), mData(NULL),
The Android Open Source Project27629322009-01-09 17:51:23 -0800134 mInitialized(false)
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800135{
The Android Open Source Project8a7a6752009-01-15 16:12:10 -0800136 // use any address by default
137 strncpy(mA2dpAddress, "00:00:00:00:00:00", sizeof(mA2dpAddress));
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800138}
139
140status_t A2dpAudioInterface::A2dpAudioStreamOut::set(
141 int format, int channels, uint32_t rate)
142{
143 LOGD("A2dpAudioStreamOut::set %d, %d, %d\n", format, channels, rate);
144
145 // fix up defaults
146 if (format == 0) format = AudioSystem::PCM_16_BIT;
147 if (channels == 0) channels = channelCount();
148 if (rate == 0) rate = sampleRate();
149
150 // check values
151 if ((format != AudioSystem::PCM_16_BIT) ||
152 (channels != channelCount()) ||
153 (rate != sampleRate()))
154 return BAD_VALUE;
155
156 return NO_ERROR;
157}
158
159A2dpAudioInterface::A2dpAudioStreamOut::~A2dpAudioStreamOut()
160{
The Android Open Source Projecta6938ba2009-02-10 15:44:00 -0800161 close();
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800162}
163
164ssize_t A2dpAudioInterface::A2dpAudioStreamOut::write(const void* buffer, size_t bytes)
165{
The Android Open Source Project27629322009-01-09 17:51:23 -0800166 status_t status = NO_INIT;
167 size_t remaining = bytes;
168
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800169 if (!mInitialized) {
The Android Open Source Project8a7a6752009-01-15 16:12:10 -0800170 status = a2dp_init(mA2dpAddress, 44100, 2, &mData);
The Android Open Source Project27629322009-01-09 17:51:23 -0800171 if (status < 0) {
172 LOGE("a2dp_init failed err: %d\n", status);
173 goto Error;
174 }
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800175 mInitialized = true;
176 }
177
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800178 while (remaining > 0) {
The Android Open Source Project27629322009-01-09 17:51:23 -0800179 status = a2dp_write(mData, buffer, remaining);
180 if (status <= 0) {
181 LOGE("a2dp_write failed err: %d\n", status);
182 goto Error;
183 }
184 remaining -= status;
185 buffer = ((char *)buffer) + status;
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800186 }
The Android Open Source Project27629322009-01-09 17:51:23 -0800187
188 mStandby = false;
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800189
190 return bytes;
The Android Open Source Project27629322009-01-09 17:51:23 -0800191
The Android Open Source Projecta6938ba2009-02-10 15:44:00 -0800192Error:
193 close();
The Android Open Source Project27629322009-01-09 17:51:23 -0800194 // Simulate audio output timing in case of error
195 usleep(bytes * 1000000 / frameSize() / sampleRate());
196
197 return status;
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800198}
199
200status_t A2dpAudioInterface::A2dpAudioStreamOut::standby()
201{
The Android Open Source Project27629322009-01-09 17:51:23 -0800202 int result = 0;
203
204 if (!mStandby) {
205 result = a2dp_stop(mData);
206 if (result == 0)
207 mStandby = true;
208 }
209
210 return result;
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800211}
212
The Android Open Source Project8a7a6752009-01-15 16:12:10 -0800213status_t A2dpAudioInterface::A2dpAudioStreamOut::setAddress(const char* address)
214{
215 if (strlen(address) < sizeof(mA2dpAddress))
216 return -EINVAL;
217
218 if (strcmp(address, mA2dpAddress)) {
219 strcpy(mA2dpAddress, address);
The Android Open Source Projecta6938ba2009-02-10 15:44:00 -0800220 close();
The Android Open Source Project8a7a6752009-01-15 16:12:10 -0800221 }
222
223 return NO_ERROR;
224}
225
The Android Open Source Projecta6938ba2009-02-10 15:44:00 -0800226status_t A2dpAudioInterface::A2dpAudioStreamOut::close()
227{
228 if (mData) {
229 a2dp_cleanup(mData);
230 mData = NULL;
231 mInitialized = false;
232 }
233 return NO_ERROR;
234}
235
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800236status_t A2dpAudioInterface::A2dpAudioStreamOut::dump(int fd, const Vector<String16>& args)
237{
238 return NO_ERROR;
239}
240
241
242}; // namespace android