xref: /aosp_15_r20/external/oboe/src/common/AudioSourceCaller.cpp (revision 05767d913155b055644481607e6fa1e35e2fe72c)
1*05767d91SRobert Wu /*
2*05767d91SRobert Wu  * Copyright 2019 The Android Open Source Project
3*05767d91SRobert Wu  *
4*05767d91SRobert Wu  * Licensed under the Apache License, Version 2.0 (the "License");
5*05767d91SRobert Wu  * you may not use this file except in compliance with the License.
6*05767d91SRobert Wu  * You may obtain a copy of the License at
7*05767d91SRobert Wu  *
8*05767d91SRobert Wu  *      http://www.apache.org/licenses/LICENSE-2.0
9*05767d91SRobert Wu  *
10*05767d91SRobert Wu  * Unless required by applicable law or agreed to in writing, software
11*05767d91SRobert Wu  * distributed under the License is distributed on an "AS IS" BASIS,
12*05767d91SRobert Wu  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*05767d91SRobert Wu  * See the License for the specific language governing permissions and
14*05767d91SRobert Wu  * limitations under the License.
15*05767d91SRobert Wu  */
16*05767d91SRobert Wu 
17*05767d91SRobert Wu #include "AudioSourceCaller.h"
18*05767d91SRobert Wu 
19*05767d91SRobert Wu using namespace oboe;
20*05767d91SRobert Wu using namespace flowgraph;
21*05767d91SRobert Wu 
onProcessFixedBlock(uint8_t * buffer,int32_t numBytes)22*05767d91SRobert Wu int32_t AudioSourceCaller::onProcessFixedBlock(uint8_t *buffer, int32_t numBytes) {
23*05767d91SRobert Wu     AudioStreamDataCallback *callback = mStream->getDataCallback();
24*05767d91SRobert Wu     int32_t result = 0;
25*05767d91SRobert Wu     int32_t numFrames = numBytes / mStream->getBytesPerFrame();
26*05767d91SRobert Wu     if (callback != nullptr) {
27*05767d91SRobert Wu         DataCallbackResult callbackResult = callback->onAudioReady(mStream, buffer, numFrames);
28*05767d91SRobert Wu         // onAudioReady() does not return the number of bytes processed so we have to assume all.
29*05767d91SRobert Wu         result = (callbackResult == DataCallbackResult::Continue)
30*05767d91SRobert Wu                 ? numBytes
31*05767d91SRobert Wu                 : -1;
32*05767d91SRobert Wu     } else {
33*05767d91SRobert Wu         auto readResult = mStream->read(buffer, numFrames, mTimeoutNanos);
34*05767d91SRobert Wu         if (!readResult) return (int32_t) readResult.error();
35*05767d91SRobert Wu         result = readResult.value() * mStream->getBytesPerFrame();
36*05767d91SRobert Wu     }
37*05767d91SRobert Wu     return result;
38*05767d91SRobert Wu }
39