1 /*
2 * Copyright 2019 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 #include "common/OboeDebug.h"
18 #include "FullDuplexStream.h"
19
onAudioReady(oboe::AudioStream * outputStream,void * audioData,int numFrames)20 oboe::DataCallbackResult FullDuplexStream::onAudioReady(
21 oboe::AudioStream *outputStream,
22 void *audioData,
23 int numFrames) {
24 oboe::DataCallbackResult callbackResult = oboe::DataCallbackResult::Continue;
25 int32_t actualFramesRead = 0;
26
27 // Silence the output.
28 int32_t numBytes = numFrames * outputStream->getBytesPerFrame();
29 memset(audioData, 0 /* value */, numBytes);
30
31 if (mCountCallbacksToDrain > 0) {
32 // Drain the input.
33 int32_t totalFramesRead = 0;
34 do {
35 oboe::ResultWithValue<int32_t> result = getInputStream()->read(mInputBuffer.get(),
36 numFrames,
37 0 /* timeout */);
38 if (!result) {
39 // Ignore errors because input stream may not be started yet.
40 break;
41 }
42 actualFramesRead = result.value();
43 totalFramesRead += actualFramesRead;
44 } while (actualFramesRead > 0);
45 // Only counts if we actually got some data.
46 if (totalFramesRead > 0) {
47 mCountCallbacksToDrain--;
48 }
49
50 } else if (mCountInputBurstsCushion > 0) {
51 // Let the input fill up a bit so we are not so close to the write pointer.
52 mCountInputBurstsCushion--;
53
54 } else if (mCountCallbacksToDiscard > 0) {
55 mCountCallbacksToDiscard--;
56 // Ignore. Allow the input to reach to equilibrium with the output.
57 oboe::ResultWithValue<int32_t> resultAvailable = getInputStream()->getAvailableFrames();
58 if (!resultAvailable) {
59 LOGE("%s() getAvailableFrames() returned %s\n",
60 __func__, convertToText(resultAvailable.error()));
61 callbackResult = oboe::DataCallbackResult::Stop;
62 } else {
63 int32_t framesAvailable = resultAvailable.value();
64 if (framesAvailable >= mMinimumFramesBeforeRead) {
65 oboe::ResultWithValue<int32_t> resultRead = getInputStream()->read(mInputBuffer.get(), numFrames, 0 /* timeout */);
66 if (!resultRead) {
67 LOGE("%s() read() returned %s\n", __func__, convertToText(resultRead.error()));
68 callbackResult = oboe::DataCallbackResult::Stop;
69 }
70 }
71 }
72 } else {
73 int32_t framesRead = 0;
74 oboe::ResultWithValue<int32_t> resultAvailable = getInputStream()->getAvailableFrames();
75 if (!resultAvailable) {
76 LOGE("%s() getAvailableFrames() returned %s\n", __func__, convertToText(resultAvailable.error()));
77 callbackResult = oboe::DataCallbackResult::Stop;
78 } else {
79 int32_t framesAvailable = resultAvailable.value();
80 if (framesAvailable >= mMinimumFramesBeforeRead) {
81 // Read data into input buffer.
82 oboe::ResultWithValue<int32_t> resultRead = getInputStream()->read(mInputBuffer.get(), numFrames, 0 /* timeout */);
83 if (!resultRead) {
84 LOGE("%s() read() returned %s\n", __func__, convertToText(resultRead.error()));
85 callbackResult = oboe::DataCallbackResult::Stop;
86 } else {
87 framesRead = resultRead.value();
88 }
89 }
90 }
91
92 if (callbackResult == oboe::DataCallbackResult::Continue) {
93 callbackResult = onBothStreamsReady(
94 mInputBuffer.get(), framesRead,
95 audioData, numFrames);
96
97 }
98 }
99
100 if (callbackResult == oboe::DataCallbackResult::Stop) {
101 getInputStream()->requestStop();
102 }
103
104 return callbackResult;
105 }
106
start()107 oboe::Result FullDuplexStream::start() {
108 mCountCallbacksToDrain = kNumCallbacksToDrain;
109 mCountInputBurstsCushion = mNumInputBurstsCushion;
110 mCountCallbacksToDiscard = kNumCallbacksToDiscard;
111
112 // Determine maximum size that could possibly be called.
113 int32_t bufferSize = getOutputStream()->getBufferCapacityInFrames()
114 * getOutputStream()->getChannelCount();
115 if (bufferSize > mBufferSize) {
116 mInputBuffer = std::make_unique<float[]>(bufferSize);
117 mBufferSize = bufferSize;
118 }
119 oboe::Result result = getInputStream()->requestStart();
120 if (result != oboe::Result::OK) {
121 return result;
122 }
123 return getOutputStream()->requestStart();
124 }
125
stop()126 oboe::Result FullDuplexStream::stop() {
127 getOutputStream()->requestStop(); // TODO result?
128 return getInputStream()->requestStop();
129 }
130
getMNumInputBurstsCushion() const131 int32_t FullDuplexStream::getMNumInputBurstsCushion() const {
132 return mNumInputBurstsCushion;
133 }
134
setMNumInputBurstsCushion(int32_t numBursts)135 void FullDuplexStream::setMNumInputBurstsCushion(int32_t numBursts) {
136 FullDuplexStream::mNumInputBurstsCushion = numBursts;
137 }
138