blob: d1b7af38fa9964a8c3210946eeec902271839a1e [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(
The Android Open Source Projectd2bd26d2009-02-19 10:57:31 -080075 int format, int channelCount, uint32_t sampleRate, status_t *status,
76 AudioSystem::audio_in_acoustics acoustics)
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -080077{
78 if (status)
79 *status = -1;
80 return NULL;
81}
82
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -080083status_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);
The Android Open Source Project8a7a6752009-01-15 16:12:10 -080096
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 Projecta6938ba2009-02-10 15:44:00 -0800103 if (strcmp(key, "bluetooth_enabled") == 0 &&
104 strcmp(value, "false") == 0) {
105 return mOutput->close();
106 }
The Android Open Source Project8a7a6752009-01-15 16:12:10 -0800107
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800108 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() :
The Android Open Source Project5f78a482009-01-20 14:03:58 -0800134 mFd(-1), mStandby(true), mStartCount(0), mRetryCount(0), mData(NULL),
The Android Open Source Project27629322009-01-09 17:51:23 -0800135 mInitialized(false)
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800136{
The Android Open Source Project8a7a6752009-01-15 16:12:10 -0800137 // use any address by default
138 strncpy(mA2dpAddress, "00:00:00:00:00:00", sizeof(mA2dpAddress));
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800139}
140
141status_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
160A2dpAudioInterface::A2dpAudioStreamOut::~A2dpAudioStreamOut()
161{
The Android Open Source Projecta6938ba2009-02-10 15:44:00 -0800162 close();
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800163}
164
165ssize_t A2dpAudioInterface::A2dpAudioStreamOut::write(const void* buffer, size_t bytes)
166{
The Android Open Source Project27629322009-01-09 17:51:23 -0800167 status_t status = NO_INIT;
168 size_t remaining = bytes;
169
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800170 if (!mInitialized) {
The Android Open Source Project8a7a6752009-01-15 16:12:10 -0800171 status = a2dp_init(mA2dpAddress, 44100, 2, &mData);
The Android Open Source Project27629322009-01-09 17:51:23 -0800172 if (status < 0) {
173 LOGE("a2dp_init failed err: %d\n", status);
174 goto Error;
175 }
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800176 mInitialized = true;
177 }
178
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800179 while (remaining > 0) {
The Android Open Source Project27629322009-01-09 17:51:23 -0800180 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 Projecte09fd9e2008-12-17 18:05:43 -0800187 }
The Android Open Source Project27629322009-01-09 17:51:23 -0800188
189 mStandby = false;
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800190
191 return bytes;
The Android Open Source Project27629322009-01-09 17:51:23 -0800192
The Android Open Source Projecta6938ba2009-02-10 15:44:00 -0800193Error:
194 close();
The Android Open Source Project27629322009-01-09 17:51:23 -0800195 // Simulate audio output timing in case of error
196 usleep(bytes * 1000000 / frameSize() / sampleRate());
197
198 return status;
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800199}
200
201status_t A2dpAudioInterface::A2dpAudioStreamOut::standby()
202{
The Android Open Source Project27629322009-01-09 17:51:23 -0800203 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 Projecte09fd9e2008-12-17 18:05:43 -0800212}
213
The Android Open Source Project8a7a6752009-01-15 16:12:10 -0800214status_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 Projecta6938ba2009-02-10 15:44:00 -0800221 close();
The Android Open Source Project8a7a6752009-01-15 16:12:10 -0800222 }
223
224 return NO_ERROR;
225}
226
The Android Open Source Projecta6938ba2009-02-10 15:44:00 -0800227status_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 Projecte09fd9e2008-12-17 18:05:43 -0800237status_t A2dpAudioInterface::A2dpAudioStreamOut::dump(int fd, const Vector<String16>& args)
238{
239 return NO_ERROR;
240}
241
242
243}; // namespace android