NBAIO.cpp revision 4d7b3f876b21997680ae32a340d746ed1cae6ab1
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{
103    unsigned format;
104    switch (sampleRate) {
105    case 8000:
106        format = Format_SR_8000;
107        break;
108    case 11025:
109        format = Format_SR_11025;
110        break;
111    case 16000:
112        format = Format_SR_16000;
113        break;
114    case 22050:
115        format = Format_SR_22050;
116        break;
117    case 24000:
118        format = Format_SR_24000;
119        break;
120    case 32000:
121        format = Format_SR_32000;
122        break;
123    case 44100:
124        format = Format_SR_44100;
125        break;
126    case 48000:
127        format = Format_SR_48000;
128        break;
129    default:
130        return Format_Invalid;
131    }
132    switch (channelCount) {
133    case 1:
134        format |= Format_C_1;
135        break;
136    case 2:
137        format |= Format_C_2;
138        break;
139    default:
140        return Format_Invalid;
141    }
142    NBAIO_Format ret;
143    ret.mPacked = format;
144    return ret;
145}
146
147// This is a default implementation; it is expected that subclasses will optimize this.
148ssize_t NBAIO_Sink::writeVia(writeVia_t via, size_t total, void *user, size_t block)
149{
150    if (!mNegotiated) {
151        return (ssize_t) NEGOTIATE;
152    }
153    static const size_t maxBlock = 32;
154    size_t frameSize = Format_frameSize(mFormat);
155    ALOG_ASSERT(frameSize > 0 && frameSize <= 8);
156    // double guarantees alignment for stack similar to what malloc() gives for heap
157    if (block == 0 || block > maxBlock) {
158        block = maxBlock;
159    }
160    double buffer[((frameSize * block) + sizeof(double) - 1) / sizeof(double)];
161    size_t accumulator = 0;
162    while (accumulator < total) {
163        size_t count = total - accumulator;
164        if (count > block) {
165            count = block;
166        }
167        ssize_t ret = via(user, buffer, count);
168        if (ret > 0) {
169            ALOG_ASSERT((size_t) ret <= count);
170            size_t maxRet = ret;
171            ret = write(buffer, maxRet);
172            if (ret > 0) {
173                ALOG_ASSERT((size_t) ret <= maxRet);
174                accumulator += ret;
175                continue;
176            }
177        }
178        return accumulator > 0 ? accumulator : ret;
179    }
180    return accumulator;
181}
182
183// This is a default implementation; it is expected that subclasses will optimize this.
184ssize_t NBAIO_Source::readVia(readVia_t via, size_t total, void *user,
185                              int64_t readPTS, size_t block)
186{
187    if (!mNegotiated) {
188        return (ssize_t) NEGOTIATE;
189    }
190    static const size_t maxBlock = 32;
191    size_t frameSize = Format_frameSize(mFormat);
192    ALOG_ASSERT(frameSize > 0 && frameSize <= 8);
193    // double guarantees alignment for stack similar to what malloc() gives for heap
194    if (block == 0 || block > maxBlock) {
195        block = maxBlock;
196    }
197    double buffer[((frameSize * block) + sizeof(double) - 1) / sizeof(double)];
198    size_t accumulator = 0;
199    while (accumulator < total) {
200        size_t count = total - accumulator;
201        if (count > block) {
202            count = block;
203        }
204        ssize_t ret = read(buffer, count, readPTS);
205        if (ret > 0) {
206            ALOG_ASSERT((size_t) ret <= count);
207            size_t maxRet = ret;
208            ret = via(user, buffer, maxRet, readPTS);
209            if (ret > 0) {
210                ALOG_ASSERT((size_t) ret <= maxRet);
211                accumulator += ret;
212                continue;
213            }
214        }
215        return accumulator > 0 ? accumulator : ret;
216    }
217    return accumulator;
218}
219
220// Default implementation that only accepts my mFormat
221ssize_t NBAIO_Port::negotiate(const NBAIO_Format offers[], size_t numOffers,
222                                  NBAIO_Format counterOffers[], size_t& numCounterOffers)
223{
224    ALOGV("negotiate offers=%p numOffers=%u countersOffers=%p numCounterOffers=%u",
225            offers, numOffers, counterOffers, numCounterOffers);
226    if (Format_isValid(mFormat)) {
227        for (size_t i = 0; i < numOffers; ++i) {
228            if (Format_isEqual(offers[i], mFormat)) {
229                mNegotiated = true;
230                return i;
231            }
232        }
233        if (numCounterOffers > 0) {
234            counterOffers[0] = mFormat;
235        }
236        numCounterOffers = 1;
237    } else {
238        numCounterOffers = 0;
239    }
240    return (ssize_t) NEGOTIATE;
241}
242
243bool Format_isValid(const NBAIO_Format& format)
244{
245    return format.mPacked != Format_Invalid.mPacked;
246}
247
248bool Format_isEqual(const NBAIO_Format& format1, const NBAIO_Format& format2)
249{
250    return format1.mPacked == format2.mPacked;
251}
252
253}   // namespace android
254