blob: eb00f8cc9a6c96b9c8b04fa271c206887bbef5a8 [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);
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 AudioSystem::audio_in_acoustics acoustics)
77{
78 if (status)
79 *status = -1;
80 return NULL;
81}
82
83status_t A2dpAudioInterface::setMicMute(bool state)
84{
85 return 0;
86}
87
88status_t A2dpAudioInterface::getMicMute(bool* state)
89{
90 return 0;
91}
92
93status_t A2dpAudioInterface::setParameter(const char *key, const char *value)
94{
95 LOGD("setParameter %s,%s\n", key, value);
96
97 if (!key || !value)
98 return -EINVAL;
99
100 if (strcmp(key, "a2dp_sink_address") == 0) {
101 return mOutput->setAddress(value);
102 }
103 if (strcmp(key, "bluetooth_enabled") == 0 &&
104 strcmp(value, "false") == 0) {
105 return mOutput->close();
106 }
107
108 return 0;
109}
110
111status_t A2dpAudioInterface::setVoiceVolume(float v)
112{
113 return 0;
114}
115
116status_t A2dpAudioInterface::setMasterVolume(float v)
117{
118 return 0;
119}
120
121status_t A2dpAudioInterface::doRouting()
122{
123 return 0;
124}
125
126status_t A2dpAudioInterface::dump(int fd, const Vector<String16>& args)
127{
128 return 0;
129}
130
131// ----------------------------------------------------------------------------
132
133A2dpAudioInterface::A2dpAudioStreamOut::A2dpAudioStreamOut() :
134 mFd(-1), mStandby(true), mStartCount(0), mRetryCount(0), mData(NULL)
135{
136 // use any address by default
137 strncpy(mA2dpAddress, "00:00:00:00:00:00", sizeof(mA2dpAddress));
138}
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{
166 status_t status = NO_INIT;
167 size_t remaining = bytes;
168
169 if (!mData) {
170 status = a2dp_init(44100, 2, &mData);
171 if (status < 0) {
172 LOGE("a2dp_init failed err: %d\n", status);
173 mData = NULL;
174 goto Error;
175 }
176 a2dp_set_sink(mData, mA2dpAddress);
177 }
178
179 while (remaining > 0) {
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;
187 }
188
189 mStandby = false;
190
191 return bytes;
192
193Error:
194 // Simulate audio output timing in case of error
195 usleep(bytes * 1000000 / frameSize() / sampleRate());
196
197 return status;
198}
199
200status_t A2dpAudioInterface::A2dpAudioStreamOut::standby()
201{
202 int result = 0;
203
204 if (!mStandby) {
205 result = a2dp_stop(mData);
206 if (result == 0)
207 mStandby = true;
208 }
209
210 return result;
211}
212
213status_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);
220 if (mData)
221 a2dp_set_sink(mData, mA2dpAddress);
222 }
223
224 return NO_ERROR;
225}
226
227status_t A2dpAudioInterface::A2dpAudioStreamOut::close()
228{
229 if (mData) {
230 a2dp_cleanup(mData);
231 mData = NULL;
232 }
233 return NO_ERROR;
234}
235
236status_t A2dpAudioInterface::A2dpAudioStreamOut::dump(int fd, const Vector<String16>& args)
237{
238 return NO_ERROR;
239}
240
241
242}; // namespace android