NBAIO.cpp revision c326e1c3d122917462f1cda4f03d9c639ad92902
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    return format.mFrameSize;
28}
29
30#if 0
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#endif
39
40const NBAIO_Format Format_Invalid = { 0, 0, AUDIO_FORMAT_INVALID, 0 };
41
42enum {
43    Format_SR_8000,
44    Format_SR_11025,
45    Format_SR_16000,
46    Format_SR_22050,
47    Format_SR_24000,
48    Format_SR_32000,
49    Format_SR_44100,
50    Format_SR_48000,
51    Format_SR_Mask = 7
52};
53
54enum {
55    Format_C_1 = 0x08,
56    Format_C_2 = 0x10,
57    Format_C_Mask = 0x18
58};
59
60unsigned Format_sampleRate(const NBAIO_Format& format)
61{
62    if (!Format_isValid(format)) {
63        return 0;
64    }
65    return format.mSampleRate;
66}
67
68unsigned Format_channelCount(const NBAIO_Format& format)
69{
70    if (!Format_isValid(format)) {
71        return 0;
72    }
73    return format.mChannelCount;
74}
75
76NBAIO_Format Format_from_SR_C(unsigned sampleRate, unsigned channelCount,
77        audio_format_t format)
78{
79    if (sampleRate == 0 || channelCount == 0 || !audio_is_valid_format(format)) {
80        return Format_Invalid;
81    }
82    NBAIO_Format ret;
83    ret.mSampleRate = sampleRate;
84    ret.mChannelCount = channelCount;
85    ret.mFormat = format;
86    ret.mFrameSize = audio_is_linear_pcm(format) ?
87            channelCount * audio_bytes_per_sample(format) : sizeof(uint8_t);
88    return ret;
89}
90
91// This is a default implementation; it is expected that subclasses will optimize this.
92ssize_t NBAIO_Sink::writeVia(writeVia_t via, size_t total, void *user, size_t block)
93{
94    if (!mNegotiated) {
95        return (ssize_t) NEGOTIATE;
96    }
97    static const size_t maxBlock = 32;
98    size_t frameSize = Format_frameSize(mFormat);
99    ALOG_ASSERT(frameSize > 0 && frameSize <= 8);
100    // double guarantees alignment for stack similar to what malloc() gives for heap
101    if (block == 0 || block > maxBlock) {
102        block = maxBlock;
103    }
104    double buffer[((frameSize * block) + sizeof(double) - 1) / sizeof(double)];
105    size_t accumulator = 0;
106    while (accumulator < total) {
107        size_t count = total - accumulator;
108        if (count > block) {
109            count = block;
110        }
111        ssize_t ret = via(user, buffer, count);
112        if (ret > 0) {
113            ALOG_ASSERT((size_t) ret <= count);
114            size_t maxRet = ret;
115            ret = write(buffer, maxRet);
116            if (ret > 0) {
117                ALOG_ASSERT((size_t) ret <= maxRet);
118                accumulator += ret;
119                continue;
120            }
121        }
122        return accumulator > 0 ? accumulator : ret;
123    }
124    return accumulator;
125}
126
127// This is a default implementation; it is expected that subclasses will optimize this.
128ssize_t NBAIO_Source::readVia(readVia_t via, size_t total, void *user,
129                              int64_t readPTS, size_t block)
130{
131    if (!mNegotiated) {
132        return (ssize_t) NEGOTIATE;
133    }
134    static const size_t maxBlock = 32;
135    size_t frameSize = Format_frameSize(mFormat);
136    ALOG_ASSERT(frameSize > 0 && frameSize <= 8);
137    // double guarantees alignment for stack similar to what malloc() gives for heap
138    if (block == 0 || block > maxBlock) {
139        block = maxBlock;
140    }
141    double buffer[((frameSize * block) + sizeof(double) - 1) / sizeof(double)];
142    size_t accumulator = 0;
143    while (accumulator < total) {
144        size_t count = total - accumulator;
145        if (count > block) {
146            count = block;
147        }
148        ssize_t ret = read(buffer, count, readPTS);
149        if (ret > 0) {
150            ALOG_ASSERT((size_t) ret <= count);
151            size_t maxRet = ret;
152            ret = via(user, buffer, maxRet, readPTS);
153            if (ret > 0) {
154                ALOG_ASSERT((size_t) ret <= maxRet);
155                accumulator += ret;
156                continue;
157            }
158        }
159        return accumulator > 0 ? accumulator : ret;
160    }
161    return accumulator;
162}
163
164// Default implementation that only accepts my mFormat
165ssize_t NBAIO_Port::negotiate(const NBAIO_Format offers[], size_t numOffers,
166                                  NBAIO_Format counterOffers[], size_t& numCounterOffers)
167{
168    ALOGV("negotiate offers=%p numOffers=%u countersOffers=%p numCounterOffers=%u",
169            offers, numOffers, counterOffers, numCounterOffers);
170    if (Format_isValid(mFormat)) {
171        for (size_t i = 0; i < numOffers; ++i) {
172            if (Format_isEqual(offers[i], mFormat)) {
173                mNegotiated = true;
174                return i;
175            }
176        }
177        if (numCounterOffers > 0) {
178            counterOffers[0] = mFormat;
179        }
180        numCounterOffers = 1;
181    } else {
182        numCounterOffers = 0;
183    }
184    return (ssize_t) NEGOTIATE;
185}
186
187bool Format_isValid(const NBAIO_Format& format)
188{
189    return format.mSampleRate != 0 && format.mChannelCount != 0 &&
190            format.mFormat != AUDIO_FORMAT_INVALID && format.mFrameSize != 0;
191}
192
193bool Format_isEqual(const NBAIO_Format& format1, const NBAIO_Format& format2)
194{
195    return format1.mSampleRate == format2.mSampleRate &&
196            format1.mChannelCount == format2.mChannelCount && format1.mFormat == format2.mFormat &&
197            format1.mFrameSize == format2.mFrameSize;
198}
199
200}   // namespace android
201