NBAIO.cpp revision f95a3c4122d67273d930c7d83c3df99f136603ed
1/*
2 * Copyright (C) 2012 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#define LOG_TAG "NBAIO"
18//#define LOG_NDEBUG 0
19
20#include <utils/Log.h>
21#include <media/nbaio/NBAIO.h>
22
23namespace android {
24
25size_t Format_frameSize(const NBAIO_Format& format)
26{
27    // FIXME The sample format is hard-coded to AUDIO_FORMAT_PCM_16_BIT
28    return Format_channelCount(format) * sizeof(short);
29}
30
31int Format_frameBitShift(const NBAIO_Format& format)
32{
33    // FIXME The sample format is hard-coded to AUDIO_FORMAT_PCM_16_BIT
34    // sizeof(short) == 2, so frame size == 1 << channels
35    return Format_channelCount(format);
36    // FIXME must return -1 for non-power of 2
37}
38
39const NBAIO_Format Format_Invalid = { 0 };
40
41enum {
42    Format_SR_8000,
43    Format_SR_11025,
44    Format_SR_16000,
45    Format_SR_22050,
46    Format_SR_24000,
47    Format_SR_32000,
48    Format_SR_44100,
49    Format_SR_48000,
50    Format_SR_Mask = 7
51};
52
53enum {
54    Format_C_1 = 0x08,
55    Format_C_2 = 0x10,
56    Format_C_Mask = 0x18
57};
58
59unsigned Format_sampleRate(const NBAIO_Format& format)
60{
61    if (!Format_isValid(format)) {
62        return 0;
63    }
64    switch (format.mPacked & Format_SR_Mask) {
65    case Format_SR_8000:
66        return 8000;
67    case Format_SR_11025:
68        return 11025;
69    case Format_SR_16000:
70        return 16000;
71    case Format_SR_22050:
72        return 22050;
73    case Format_SR_24000:
74        return 24000;
75    case Format_SR_32000:
76        return 32000;
77    case Format_SR_44100:
78        return 44100;
79    case Format_SR_48000:
80        return 48000;
81    default:
82        return 0;
83    }
84}
85
86unsigned Format_channelCount(const NBAIO_Format& format)
87{
88    if (!Format_isValid(format)) {
89        return 0;
90    }
91    switch (format.mPacked & Format_C_Mask) {
92    case Format_C_1:
93        return 1;
94    case Format_C_2:
95        return 2;
96    default:
97        return 0;
98    }
99}
100
101NBAIO_Format Format_from_SR_C(unsigned sampleRate, unsigned channelCount,
102        audio_format_t format_ __unused)
103{
104    unsigned format;
105    switch (sampleRate) {
106    case 8000:
107        format = Format_SR_8000;
108        break;
109    case 11025:
110        format = Format_SR_11025;
111        break;
112    case 16000:
113        format = Format_SR_16000;
114        break;
115    case 22050:
116        format = Format_SR_22050;
117        break;
118    case 24000:
119        format = Format_SR_24000;
120        break;
121    case 32000:
122        format = Format_SR_32000;
123        break;
124    case 44100:
125        format = Format_SR_44100;
126        break;
127    case 48000:
128        format = Format_SR_48000;
129        break;
130    default:
131        return Format_Invalid;
132    }
133    switch (channelCount) {
134    case 1:
135        format |= Format_C_1;
136        break;
137    case 2:
138        format |= Format_C_2;
139        break;
140    default:
141        return Format_Invalid;
142    }
143    NBAIO_Format ret;
144    ret.mPacked = format;
145    return ret;
146}
147
148// This is a default implementation; it is expected that subclasses will optimize this.
149ssize_t NBAIO_Sink::writeVia(writeVia_t via, size_t total, void *user, size_t block)
150{
151    if (!mNegotiated) {
152        return (ssize_t) NEGOTIATE;
153    }
154    static const size_t maxBlock = 32;
155    size_t frameSize = Format_frameSize(mFormat);
156    ALOG_ASSERT(frameSize > 0 && frameSize <= 8);
157    // double guarantees alignment for stack similar to what malloc() gives for heap
158    if (block == 0 || block > maxBlock) {
159        block = maxBlock;
160    }
161    double buffer[((frameSize * block) + sizeof(double) - 1) / sizeof(double)];
162    size_t accumulator = 0;
163    while (accumulator < total) {
164        size_t count = total - accumulator;
165        if (count > block) {
166            count = block;
167        }
168        ssize_t ret = via(user, buffer, count);
169        if (ret > 0) {
170            ALOG_ASSERT((size_t) ret <= count);
171            size_t maxRet = ret;
172            ret = write(buffer, maxRet);
173            if (ret > 0) {
174                ALOG_ASSERT((size_t) ret <= maxRet);
175                accumulator += ret;
176                continue;
177            }
178        }
179        return accumulator > 0 ? accumulator : ret;
180    }
181    return accumulator;
182}
183
184// This is a default implementation; it is expected that subclasses will optimize this.
185ssize_t NBAIO_Source::readVia(readVia_t via, size_t total, void *user,
186                              int64_t readPTS, size_t block)
187{
188    if (!mNegotiated) {
189        return (ssize_t) NEGOTIATE;
190    }
191    static const size_t maxBlock = 32;
192    size_t frameSize = Format_frameSize(mFormat);
193    ALOG_ASSERT(frameSize > 0 && frameSize <= 8);
194    // double guarantees alignment for stack similar to what malloc() gives for heap
195    if (block == 0 || block > maxBlock) {
196        block = maxBlock;
197    }
198    double buffer[((frameSize * block) + sizeof(double) - 1) / sizeof(double)];
199    size_t accumulator = 0;
200    while (accumulator < total) {
201        size_t count = total - accumulator;
202        if (count > block) {
203            count = block;
204        }
205        ssize_t ret = read(buffer, count, readPTS);
206        if (ret > 0) {
207            ALOG_ASSERT((size_t) ret <= count);
208            size_t maxRet = ret;
209            ret = via(user, buffer, maxRet, readPTS);
210            if (ret > 0) {
211                ALOG_ASSERT((size_t) ret <= maxRet);
212                accumulator += ret;
213                continue;
214            }
215        }
216        return accumulator > 0 ? accumulator : ret;
217    }
218    return accumulator;
219}
220
221// Default implementation that only accepts my mFormat
222ssize_t NBAIO_Port::negotiate(const NBAIO_Format offers[], size_t numOffers,
223                                  NBAIO_Format counterOffers[], size_t& numCounterOffers)
224{
225    ALOGV("negotiate offers=%p numOffers=%u countersOffers=%p numCounterOffers=%u",
226            offers, numOffers, counterOffers, numCounterOffers);
227    if (Format_isValid(mFormat)) {
228        for (size_t i = 0; i < numOffers; ++i) {
229            if (Format_isEqual(offers[i], mFormat)) {
230                mNegotiated = true;
231                return i;
232            }
233        }
234        if (numCounterOffers > 0) {
235            counterOffers[0] = mFormat;
236        }
237        numCounterOffers = 1;
238    } else {
239        numCounterOffers = 0;
240    }
241    return (ssize_t) NEGOTIATE;
242}
243
244bool Format_isValid(const NBAIO_Format& format)
245{
246    return format.mPacked != Format_Invalid.mPacked;
247}
248
249bool Format_isEqual(const NBAIO_Format& format1, const NBAIO_Format& format2)
250{
251    return format1.mPacked == format2.mPacked;
252}
253
254}   // namespace android
255