blob: 7242575957492af8fad42074063f079a6bdf0250 [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 }
102
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800103 return 0;
104}
105
106status_t A2dpAudioInterface::setVoiceVolume(float v)
107{
108 return 0;
109}
110
111status_t A2dpAudioInterface::setMasterVolume(float v)
112{
113 return 0;
114}
115
116status_t A2dpAudioInterface::doRouting()
117{
118 return 0;
119}
120
121status_t A2dpAudioInterface::dump(int fd, const Vector<String16>& args)
122{
123 return 0;
124}
125
126// ----------------------------------------------------------------------------
127
128A2dpAudioInterface::A2dpAudioStreamOut::A2dpAudioStreamOut() :
The Android Open Source Project27629322009-01-09 17:51:23 -0800129 mFd(-1), mStandby(false), mStartCount(0), mRetryCount(0), mData(NULL),
130 mInitialized(false)
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800131{
The Android Open Source Project8a7a6752009-01-15 16:12:10 -0800132 // use any address by default
133 strncpy(mA2dpAddress, "00:00:00:00:00:00", sizeof(mA2dpAddress));
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800134}
135
136status_t A2dpAudioInterface::A2dpAudioStreamOut::set(
137 int format, int channels, uint32_t rate)
138{
139 LOGD("A2dpAudioStreamOut::set %d, %d, %d\n", format, channels, rate);
140
141 // fix up defaults
142 if (format == 0) format = AudioSystem::PCM_16_BIT;
143 if (channels == 0) channels = channelCount();
144 if (rate == 0) rate = sampleRate();
145
146 // check values
147 if ((format != AudioSystem::PCM_16_BIT) ||
148 (channels != channelCount()) ||
149 (rate != sampleRate()))
150 return BAD_VALUE;
151
152 return NO_ERROR;
153}
154
155A2dpAudioInterface::A2dpAudioStreamOut::~A2dpAudioStreamOut()
156{
157 if (mData)
158 a2dp_cleanup(mData);
159}
160
161ssize_t A2dpAudioInterface::A2dpAudioStreamOut::write(const void* buffer, size_t bytes)
162{
The Android Open Source Project27629322009-01-09 17:51:23 -0800163 status_t status = NO_INIT;
164 size_t remaining = bytes;
165
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800166 if (!mInitialized) {
The Android Open Source Project8a7a6752009-01-15 16:12:10 -0800167 status = a2dp_init(mA2dpAddress, 44100, 2, &mData);
The Android Open Source Project27629322009-01-09 17:51:23 -0800168 if (status < 0) {
169 LOGE("a2dp_init failed err: %d\n", status);
170 goto Error;
171 }
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800172 mInitialized = true;
173 }
174
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800175 while (remaining > 0) {
The Android Open Source Project27629322009-01-09 17:51:23 -0800176 status = a2dp_write(mData, buffer, remaining);
177 if (status <= 0) {
178 LOGE("a2dp_write failed err: %d\n", status);
179 goto Error;
180 }
181 remaining -= status;
182 buffer = ((char *)buffer) + status;
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800183 }
The Android Open Source Project27629322009-01-09 17:51:23 -0800184
185 mStandby = false;
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800186
187 return bytes;
The Android Open Source Project27629322009-01-09 17:51:23 -0800188
189Error:
190 // Simulate audio output timing in case of error
191 usleep(bytes * 1000000 / frameSize() / sampleRate());
192
193 return status;
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800194}
195
196status_t A2dpAudioInterface::A2dpAudioStreamOut::standby()
197{
The Android Open Source Project27629322009-01-09 17:51:23 -0800198 int result = 0;
199
200 if (!mStandby) {
201 result = a2dp_stop(mData);
202 if (result == 0)
203 mStandby = true;
204 }
205
206 return result;
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800207}
208
The Android Open Source Project8a7a6752009-01-15 16:12:10 -0800209status_t A2dpAudioInterface::A2dpAudioStreamOut::setAddress(const char* address)
210{
211 if (strlen(address) < sizeof(mA2dpAddress))
212 return -EINVAL;
213
214 if (strcmp(address, mA2dpAddress)) {
215 strcpy(mA2dpAddress, address);
216
217 if (mInitialized) {
218 a2dp_cleanup(mData);
219 mData = NULL;
220 mInitialized = false;
221 }
222 }
223
224 return NO_ERROR;
225}
226
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800227status_t A2dpAudioInterface::A2dpAudioStreamOut::dump(int fd, const Vector<String16>& args)
228{
229 return NO_ERROR;
230}
231
232
233}; // namespace android