blob: 2974e32ee179e0eaef8d4f84fa9677e87ec1d3e3 [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);
95
96 if (!key || !value)
97 return -EINVAL;
98
99 if (strcmp(key, "a2dp_sink_address") == 0) {
100 return mOutput->setAddress(value);
101 }
102 if (strcmp(key, "bluetooth_enabled") == 0 &&
103 strcmp(value, "false") == 0) {
104 return mOutput->close();
105 }
106
107 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() :
133 mFd(-1), mStandby(true), mStartCount(0), mRetryCount(0), mData(NULL)
134{
135 // use any address by default
The Android Open Source Project22f8def2009-03-09 11:52:12 -0700136 strcpy(mA2dpAddress, "00:00:00:00:00:00");
137 init();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -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{
161 close();
162}
163
164ssize_t A2dpAudioInterface::A2dpAudioStreamOut::write(const void* buffer, size_t bytes)
165{
The Android Open Source Project22f8def2009-03-09 11:52:12 -0700166 Mutex::Autolock lock(mLock);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800167
The Android Open Source Project22f8def2009-03-09 11:52:12 -0700168 size_t remaining = bytes;
169 status_t status = init();
170 if (status < 0)
171 goto Error;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800172
173 while (remaining > 0) {
174 status = a2dp_write(mData, buffer, remaining);
175 if (status <= 0) {
176 LOGE("a2dp_write failed err: %d\n", status);
177 goto Error;
178 }
179 remaining -= status;
180 buffer = ((char *)buffer) + status;
181 }
182
183 mStandby = false;
184
185 return bytes;
186
187Error:
188 // Simulate audio output timing in case of error
189 usleep(bytes * 1000000 / frameSize() / sampleRate());
190
191 return status;
192}
193
The Android Open Source Project22f8def2009-03-09 11:52:12 -0700194status_t A2dpAudioInterface::A2dpAudioStreamOut::init()
195{
196 if (!mData) {
197 status_t status = a2dp_init(44100, 2, &mData);
198 if (status < 0) {
199 LOGE("a2dp_init failed err: %d\n", status);
200 mData = NULL;
201 return status;
202 }
203 a2dp_set_sink(mData, mA2dpAddress);
204 }
205
206 return 0;
207}
208
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800209status_t A2dpAudioInterface::A2dpAudioStreamOut::standby()
210{
211 int result = 0;
212
The Android Open Source Project22f8def2009-03-09 11:52:12 -0700213 Mutex::Autolock lock(mLock);
214
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800215 if (!mStandby) {
216 result = a2dp_stop(mData);
217 if (result == 0)
218 mStandby = true;
219 }
220
221 return result;
222}
223
224status_t A2dpAudioInterface::A2dpAudioStreamOut::setAddress(const char* address)
225{
The Android Open Source Project22f8def2009-03-09 11:52:12 -0700226 Mutex::Autolock lock(mLock);
227
228 if (strlen(address) != strlen("00:00:00:00:00:00"))
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800229 return -EINVAL;
230
The Android Open Source Project22f8def2009-03-09 11:52:12 -0700231 strcpy(mA2dpAddress, address);
232 if (mData)
233 a2dp_set_sink(mData, mA2dpAddress);
234
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800235 return NO_ERROR;
236}
237
238status_t A2dpAudioInterface::A2dpAudioStreamOut::close()
239{
240 if (mData) {
241 a2dp_cleanup(mData);
242 mData = NULL;
243 }
244 return NO_ERROR;
245}
246
247status_t A2dpAudioInterface::A2dpAudioStreamOut::dump(int fd, const Vector<String16>& args)
248{
249 return NO_ERROR;
250}
251
252
253}; // namespace android