blob: b2a8e96c36d573aaf7b3129b129576c2042fa874 [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);
95 return 0;
96}
97
98status_t A2dpAudioInterface::setVoiceVolume(float v)
99{
100 return 0;
101}
102
103status_t A2dpAudioInterface::setMasterVolume(float v)
104{
105 return 0;
106}
107
108status_t A2dpAudioInterface::doRouting()
109{
110 return 0;
111}
112
113status_t A2dpAudioInterface::dump(int fd, const Vector<String16>& args)
114{
115 return 0;
116}
117
118// ----------------------------------------------------------------------------
119
120A2dpAudioInterface::A2dpAudioStreamOut::A2dpAudioStreamOut() :
The Android Open Source Project27629322009-01-09 17:51:23 -0800121 mFd(-1), mStandby(false), mStartCount(0), mRetryCount(0), mData(NULL),
122 mInitialized(false)
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800123{
124}
125
126status_t A2dpAudioInterface::A2dpAudioStreamOut::set(
127 int format, int channels, uint32_t rate)
128{
129 LOGD("A2dpAudioStreamOut::set %d, %d, %d\n", format, channels, rate);
130
131 // fix up defaults
132 if (format == 0) format = AudioSystem::PCM_16_BIT;
133 if (channels == 0) channels = channelCount();
134 if (rate == 0) rate = sampleRate();
135
136 // check values
137 if ((format != AudioSystem::PCM_16_BIT) ||
138 (channels != channelCount()) ||
139 (rate != sampleRate()))
140 return BAD_VALUE;
141
142 return NO_ERROR;
143}
144
145A2dpAudioInterface::A2dpAudioStreamOut::~A2dpAudioStreamOut()
146{
147 if (mData)
148 a2dp_cleanup(mData);
149}
150
151ssize_t A2dpAudioInterface::A2dpAudioStreamOut::write(const void* buffer, size_t bytes)
152{
The Android Open Source Project27629322009-01-09 17:51:23 -0800153 status_t status = NO_INIT;
154 size_t remaining = bytes;
155
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800156 if (!mInitialized) {
The Android Open Source Project27629322009-01-09 17:51:23 -0800157 status = a2dp_init("00:00:00:00:00:00", 44100, 2, &mData);
158 if (status < 0) {
159 LOGE("a2dp_init failed err: %d\n", status);
160 goto Error;
161 }
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800162 mInitialized = true;
163 }
164
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800165 while (remaining > 0) {
The Android Open Source Project27629322009-01-09 17:51:23 -0800166 status = a2dp_write(mData, buffer, remaining);
167 if (status <= 0) {
168 LOGE("a2dp_write failed err: %d\n", status);
169 goto Error;
170 }
171 remaining -= status;
172 buffer = ((char *)buffer) + status;
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800173 }
The Android Open Source Project27629322009-01-09 17:51:23 -0800174
175 mStandby = false;
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800176
177 return bytes;
The Android Open Source Project27629322009-01-09 17:51:23 -0800178
179Error:
180 // Simulate audio output timing in case of error
181 usleep(bytes * 1000000 / frameSize() / sampleRate());
182
183 return status;
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800184}
185
186status_t A2dpAudioInterface::A2dpAudioStreamOut::standby()
187{
The Android Open Source Project27629322009-01-09 17:51:23 -0800188 int result = 0;
189
190 if (!mStandby) {
191 result = a2dp_stop(mData);
192 if (result == 0)
193 mStandby = true;
194 }
195
196 return result;
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800197}
198
199status_t A2dpAudioInterface::A2dpAudioStreamOut::dump(int fd, const Vector<String16>& args)
200{
201 return NO_ERROR;
202}
203
204
205}; // namespace android