1 /*
2  * Copyright (C) 2023 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 #pragma once
18 
19 namespace android {
20 
21 class IAfRecordTrack;
22 
23 /* The ResamplerBufferProvider is used to retrieve recorded input data from the
24  * RecordThread.  It maintains local state on the relative position of the read
25  * position of the RecordTrack compared with the RecordThread.
26  */
27 class ResamplerBufferProvider : public AudioBufferProvider
28 {
29 public:
ResamplerBufferProvider(IAfRecordTrack * recordTrack)30     explicit ResamplerBufferProvider(IAfRecordTrack* recordTrack) :
31         mRecordTrack(recordTrack) {}
32 
33     // called to set the ResamplerBufferProvider to head of the RecordThread data buffer,
34     // skipping any previous data read from the hal.
35     void reset();
36 
37     /* Synchronizes RecordTrack position with the RecordThread.
38      * Calculates available frames and handle overruns if the RecordThread
39      * has advanced faster than the ResamplerBufferProvider has retrieved data.
40      * TODO: why not do this for every getNextBuffer?
41      *
42      * Parameters
43      * framesAvailable:  pointer to optional output size_t to store record track
44      *                   frames available.
45      *      hasOverrun:  pointer to optional boolean, returns true if track has overrun.
46      */
47 
48     void sync(size_t* framesAvailable = nullptr, bool* hasOverrun = nullptr);
49 
50     // AudioBufferProvider interface
51     status_t getNextBuffer(AudioBufferProvider::Buffer* buffer) final;
52     void releaseBuffer(AudioBufferProvider::Buffer* buffer) final;
53 
getFront()54     int32_t getFront() const { return mRsmpInFront; }
setFront(int32_t front)55     void setFront(int32_t front) { mRsmpInFront = front; }
56 
57 private:
58     IAfRecordTrack* const mRecordTrack;
59     size_t mRsmpInUnrel = 0;   // unreleased frames remaining from
60                                // most recent getNextBuffer
61                                // for debug only
62     int32_t mRsmpInFront = 0;  // next available frame
63                                // rolling counter that is never cleared
64 };
65 
66 } // namespace android