summaryrefslogtreecommitdiffstats
path: root/libs/audioflinger/A2dpAudioInterface.cpp
blob: eb00f8cc9a6c96b9c8b04fa271c206887bbef5a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <math.h>

#define LOG_NDEBUG 0
#define LOG_TAG "A2dpAudioInterface"
#include <utils/Log.h>
#include <utils/String8.h>

#include "A2dpAudioInterface.h"
#include "audio/liba2dp.h"


namespace android {

// ----------------------------------------------------------------------------

A2dpAudioInterface::A2dpAudioInterface() :
    mOutput(0)
{
}

A2dpAudioInterface::~A2dpAudioInterface()
{
    delete mOutput;
}

status_t A2dpAudioInterface::initCheck()
{
    return 0;
}

AudioStreamOut* A2dpAudioInterface::openOutputStream(
        int format, int channelCount, uint32_t sampleRate, status_t *status)
{
    LOGD("A2dpAudioInterface::openOutputStream %d, %d, %d\n", format, channelCount, sampleRate);
    Mutex::Autolock lock(mLock);
    status_t err = 0;

    // only one output stream allowed
    if (mOutput) {
        if (status)
            *status = -1;
        return NULL;
    }

    // create new output stream
    A2dpAudioStreamOut* out = new A2dpAudioStreamOut();
    if ((err = out->set(format, channelCount, sampleRate)) == NO_ERROR) {
        mOutput = out;
    } else {
        delete out;
    }
    
    if (status)
        *status = err;
    return mOutput;
}

AudioStreamIn* A2dpAudioInterface::openInputStream(
        int format, int channelCount, uint32_t sampleRate, status_t *status,
        AudioSystem::audio_in_acoustics acoustics)
{
    if (status)
        *status = -1;
    return NULL;
}

status_t A2dpAudioInterface::setMicMute(bool state)
{
    return 0;
}

status_t A2dpAudioInterface::getMicMute(bool* state)
{
    return 0;
}

status_t A2dpAudioInterface::setParameter(const char *key, const char *value)
{
    LOGD("setParameter %s,%s\n", key, value);
    
    if (!key || !value)
        return -EINVAL;
    
    if (strcmp(key, "a2dp_sink_address") == 0) {        
        return mOutput->setAddress(value);
    }
    if (strcmp(key, "bluetooth_enabled") == 0 &&
        strcmp(value, "false") == 0) {
        return mOutput->close();
    }

    return 0;
}

status_t A2dpAudioInterface::setVoiceVolume(float v)
{
    return 0;
}

status_t A2dpAudioInterface::setMasterVolume(float v)
{
    return 0;
}

status_t A2dpAudioInterface::doRouting()
{
    return 0;
}

status_t A2dpAudioInterface::dump(int fd, const Vector<String16>& args)
{
    return 0;
}

// ----------------------------------------------------------------------------

A2dpAudioInterface::A2dpAudioStreamOut::A2dpAudioStreamOut() :
    mFd(-1), mStandby(true), mStartCount(0), mRetryCount(0), mData(NULL)
{
    // use any address by default
    strncpy(mA2dpAddress, "00:00:00:00:00:00", sizeof(mA2dpAddress));
}

status_t A2dpAudioInterface::A2dpAudioStreamOut::set(
        int format, int channels, uint32_t rate)
{
    LOGD("A2dpAudioStreamOut::set %d, %d, %d\n", format, channels, rate);

    // fix up defaults
    if (format == 0) format = AudioSystem::PCM_16_BIT;
    if (channels == 0) channels = channelCount();
    if (rate == 0) rate = sampleRate();

    // check values
    if ((format != AudioSystem::PCM_16_BIT) ||
            (channels != channelCount()) ||
            (rate != sampleRate()))
        return BAD_VALUE;

    return NO_ERROR;
}

A2dpAudioInterface::A2dpAudioStreamOut::~A2dpAudioStreamOut()
{
    close();
}

ssize_t A2dpAudioInterface::A2dpAudioStreamOut::write(const void* buffer, size_t bytes)
{    
    status_t status = NO_INIT;
    size_t remaining = bytes;

    if (!mData) {
        status = a2dp_init(44100, 2, &mData);
        if (status < 0) {
            LOGE("a2dp_init failed err: %d\n", status);
            mData = NULL;
            goto Error;
        }
        a2dp_set_sink(mData, mA2dpAddress);
    }
    
    while (remaining > 0) {
        status = a2dp_write(mData, buffer, remaining);
        if (status <= 0) {
            LOGE("a2dp_write failed err: %d\n", status);
            goto Error;
        }
        remaining -= status;
        buffer = ((char *)buffer) + status;
    }

    mStandby = false;
    
    return bytes;

Error:
    // Simulate audio output timing in case of error
    usleep(bytes * 1000000 / frameSize() / sampleRate());

    return status;
}

status_t A2dpAudioInterface::A2dpAudioStreamOut::standby()
{
    int result = 0;

    if (!mStandby) {
        result = a2dp_stop(mData);
        if (result == 0)
            mStandby = true;
    }

    return result;
}

status_t A2dpAudioInterface::A2dpAudioStreamOut::setAddress(const char* address)
{
    if (strlen(address) < sizeof(mA2dpAddress))
        return -EINVAL;

    if (strcmp(address, mA2dpAddress)) {
        strcpy(mA2dpAddress, address);
        if (mData)
            a2dp_set_sink(mData, mA2dpAddress);
    }
    
    return NO_ERROR;
}

status_t A2dpAudioInterface::A2dpAudioStreamOut::close()
{
    if (mData) {
        a2dp_cleanup(mData);
        mData = NULL;
    }
    return NO_ERROR;
}

status_t A2dpAudioInterface::A2dpAudioStreamOut::dump(int fd, const Vector<String16>& args)
{
    return NO_ERROR;
}


}; // namespace android