blob: 1cb4410575a1b9cd5ce8ff49463696eb7a911077 [file] [log] [blame]
Glenn Kasten01066232012-02-27 11:50:44 -08001/*
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>
Glenn Kasten2dd4bdd2012-08-29 11:10:32 -070021#include <media/nbaio/NBAIO.h>
Glenn Kasten01066232012-02-27 11:50:44 -080022
23namespace android {
24
Glenn Kasten72e54af2014-01-31 09:37:35 -080025size_t Format_frameSize(const NBAIO_Format& format)
Glenn Kasten01066232012-02-27 11:50:44 -080026{
Glenn Kasten983f0572014-03-06 08:23:11 -080027 return format.mFrameSize;
Glenn Kasten01066232012-02-27 11:50:44 -080028}
29
Glenn Kasten2b7b9102014-03-06 08:02:51 -080030const NBAIO_Format Format_Invalid = { 0, 0, AUDIO_FORMAT_INVALID, 0 };
Glenn Kasten51d53cd2014-01-31 09:38:33 -080031
Glenn Kasten72e54af2014-01-31 09:37:35 -080032unsigned Format_sampleRate(const NBAIO_Format& format)
Glenn Kasten01066232012-02-27 11:50:44 -080033{
Glenn Kasten6e0d67d2014-01-31 09:41:08 -080034 if (!Format_isValid(format)) {
Glenn Kastenb64497e2012-10-01 09:47:30 -070035 return 0;
36 }
Glenn Kasten2b7b9102014-03-06 08:02:51 -080037 return format.mSampleRate;
Glenn Kasten01066232012-02-27 11:50:44 -080038}
39
Glenn Kasten72e54af2014-01-31 09:37:35 -080040unsigned Format_channelCount(const NBAIO_Format& format)
Glenn Kasten01066232012-02-27 11:50:44 -080041{
Glenn Kasten6e0d67d2014-01-31 09:41:08 -080042 if (!Format_isValid(format)) {
Glenn Kastenb64497e2012-10-01 09:47:30 -070043 return 0;
44 }
Glenn Kasten2b7b9102014-03-06 08:02:51 -080045 return format.mChannelCount;
Glenn Kasten01066232012-02-27 11:50:44 -080046}
47
Glenn Kastenf95a3c42014-03-06 07:59:49 -080048NBAIO_Format Format_from_SR_C(unsigned sampleRate, unsigned channelCount,
Glenn Kasten2b7b9102014-03-06 08:02:51 -080049 audio_format_t format)
Glenn Kasten01066232012-02-27 11:50:44 -080050{
Glenn Kasten2b7b9102014-03-06 08:02:51 -080051 if (sampleRate == 0 || channelCount == 0 || !audio_is_valid_format(format)) {
Glenn Kastenb64497e2012-10-01 09:47:30 -070052 return Format_Invalid;
53 }
Glenn Kastenc4b8b322014-01-31 09:39:01 -080054 NBAIO_Format ret;
Glenn Kasten2b7b9102014-03-06 08:02:51 -080055 ret.mSampleRate = sampleRate;
56 ret.mChannelCount = channelCount;
57 ret.mFormat = format;
58 ret.mFrameSize = audio_is_linear_pcm(format) ?
59 channelCount * audio_bytes_per_sample(format) : sizeof(uint8_t);
Glenn Kastenc4b8b322014-01-31 09:39:01 -080060 return ret;
Glenn Kasten01066232012-02-27 11:50:44 -080061}
62
63// This is a default implementation; it is expected that subclasses will optimize this.
64ssize_t NBAIO_Sink::writeVia(writeVia_t via, size_t total, void *user, size_t block)
65{
66 if (!mNegotiated) {
67 return (ssize_t) NEGOTIATE;
68 }
69 static const size_t maxBlock = 32;
70 size_t frameSize = Format_frameSize(mFormat);
71 ALOG_ASSERT(frameSize > 0 && frameSize <= 8);
72 // double guarantees alignment for stack similar to what malloc() gives for heap
73 if (block == 0 || block > maxBlock) {
74 block = maxBlock;
75 }
76 double buffer[((frameSize * block) + sizeof(double) - 1) / sizeof(double)];
77 size_t accumulator = 0;
78 while (accumulator < total) {
79 size_t count = total - accumulator;
80 if (count > block) {
81 count = block;
82 }
83 ssize_t ret = via(user, buffer, count);
84 if (ret > 0) {
85 ALOG_ASSERT((size_t) ret <= count);
86 size_t maxRet = ret;
87 ret = write(buffer, maxRet);
88 if (ret > 0) {
89 ALOG_ASSERT((size_t) ret <= maxRet);
90 accumulator += ret;
91 continue;
92 }
93 }
94 return accumulator > 0 ? accumulator : ret;
95 }
96 return accumulator;
97}
98
99// This is a default implementation; it is expected that subclasses will optimize this.
Glenn Kastend79072e2016-01-06 08:41:20 -0800100ssize_t NBAIO_Source::readVia(readVia_t via, size_t total, void *user, size_t block)
Glenn Kasten01066232012-02-27 11:50:44 -0800101{
102 if (!mNegotiated) {
103 return (ssize_t) NEGOTIATE;
104 }
105 static const size_t maxBlock = 32;
106 size_t frameSize = Format_frameSize(mFormat);
107 ALOG_ASSERT(frameSize > 0 && frameSize <= 8);
108 // double guarantees alignment for stack similar to what malloc() gives for heap
109 if (block == 0 || block > maxBlock) {
110 block = maxBlock;
111 }
112 double buffer[((frameSize * block) + sizeof(double) - 1) / sizeof(double)];
113 size_t accumulator = 0;
114 while (accumulator < total) {
115 size_t count = total - accumulator;
116 if (count > block) {
117 count = block;
118 }
Glenn Kastend79072e2016-01-06 08:41:20 -0800119 ssize_t ret = read(buffer, count);
Glenn Kasten01066232012-02-27 11:50:44 -0800120 if (ret > 0) {
121 ALOG_ASSERT((size_t) ret <= count);
122 size_t maxRet = ret;
Glenn Kastend79072e2016-01-06 08:41:20 -0800123 ret = via(user, buffer, maxRet);
Glenn Kasten01066232012-02-27 11:50:44 -0800124 if (ret > 0) {
125 ALOG_ASSERT((size_t) ret <= maxRet);
126 accumulator += ret;
127 continue;
128 }
129 }
130 return accumulator > 0 ? accumulator : ret;
131 }
132 return accumulator;
133}
134
135// Default implementation that only accepts my mFormat
136ssize_t NBAIO_Port::negotiate(const NBAIO_Format offers[], size_t numOffers,
137 NBAIO_Format counterOffers[], size_t& numCounterOffers)
138{
Mark Salyzyn0f6a0432014-06-18 16:32:00 -0700139 ALOGV("negotiate offers=%p numOffers=%zu countersOffers=%p numCounterOffers=%zu",
Glenn Kasten01066232012-02-27 11:50:44 -0800140 offers, numOffers, counterOffers, numCounterOffers);
Glenn Kasten6e0d67d2014-01-31 09:41:08 -0800141 if (Format_isValid(mFormat)) {
Glenn Kasten01066232012-02-27 11:50:44 -0800142 for (size_t i = 0; i < numOffers; ++i) {
Glenn Kasten6e0d67d2014-01-31 09:41:08 -0800143 if (Format_isEqual(offers[i], mFormat)) {
Glenn Kasten01066232012-02-27 11:50:44 -0800144 mNegotiated = true;
145 return i;
146 }
147 }
148 if (numCounterOffers > 0) {
149 counterOffers[0] = mFormat;
150 }
151 numCounterOffers = 1;
152 } else {
153 numCounterOffers = 0;
154 }
155 return (ssize_t) NEGOTIATE;
156}
157
Glenn Kastencc1e0e82014-01-31 09:48:42 -0800158bool Format_isValid(const NBAIO_Format& format)
159{
Glenn Kasten2b7b9102014-03-06 08:02:51 -0800160 return format.mSampleRate != 0 && format.mChannelCount != 0 &&
161 format.mFormat != AUDIO_FORMAT_INVALID && format.mFrameSize != 0;
Glenn Kastencc1e0e82014-01-31 09:48:42 -0800162}
163
164bool Format_isEqual(const NBAIO_Format& format1, const NBAIO_Format& format2)
165{
Glenn Kasten2b7b9102014-03-06 08:02:51 -0800166 return format1.mSampleRate == format2.mSampleRate &&
167 format1.mChannelCount == format2.mChannelCount && format1.mFormat == format2.mFormat &&
168 format1.mFrameSize == format2.mFrameSize;
Glenn Kastencc1e0e82014-01-31 09:48:42 -0800169}
170
Glenn Kasten01066232012-02-27 11:50:44 -0800171} // namespace android