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