xref: /aosp_15_r20/frameworks/wilhelm/src/android/AudioRecordCallback.h (revision bebae9c0e76121f8312ccb50385c080b3a0b023c)
1 /*
2  * Copyright (C) 2022 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 #ifndef SL_PREFETCHEVENT_NONE // This is defined in slesl_allinclusive, which isn't guarded
20 #include "sles_allinclusive.h"
21 #endif
22 
23 #include "media/AudioRecord.h"
24 
25 void audioRecorder_handleOverrun_lockRecord(CAudioRecorder* ar);
26 void audioRecorder_handleNewPos_lockRecord(CAudioRecorder* ar);
27 void audioRecorder_handleMarker_lockRecord(CAudioRecorder* ar);
28 size_t audioRecorder_handleMoreData_lockRecord(CAudioRecorder* ar,
29                                                const android::AudioRecord::Buffer&);
30 //--------------------------------------------------------------------------------------------------
31 namespace android {
32 
33 class AudioRecordCallback : public AudioRecord::IAudioRecordCallback {
34   public:
AudioRecordCallback(CAudioRecorder * audioRecorder)35     AudioRecordCallback(CAudioRecorder * audioRecorder) : mAr(audioRecorder),
36             mCallbackProtector(mAr->mCallbackProtector) {}
37     AudioRecordCallback(const AudioRecordCallback&) = delete;
38     AudioRecordCallback& operator=(const AudioRecordCallback&) = delete;
39 
40   private:
onMoreData(const AudioRecord::Buffer & buffer)41     size_t onMoreData(const AudioRecord::Buffer& buffer) override {
42         if (!CallbackProtector::enterCbIfOk(mCallbackProtector)) {
43             // it is not safe to enter the callback (the track is about to go away)
44             return buffer.size(); // replicate existing behavior
45         }
46         size_t bytesRead = audioRecorder_handleMoreData_lockRecord(mAr, buffer);
47         mCallbackProtector->exitCb();
48         return bytesRead;
49     }
50 
51 
onOverrun()52     void onOverrun() override {
53         if (!CallbackProtector::enterCbIfOk(mCallbackProtector)) {
54             // it is not safe to enter the callback (the track is about to go away)
55             return;
56         }
57         audioRecorder_handleOverrun_lockRecord(mAr);
58         mCallbackProtector->exitCb();
59     }
onMarker(uint32_t)60     void onMarker(uint32_t) override {
61         if (!CallbackProtector::enterCbIfOk(mCallbackProtector)) {
62             // it is not safe to enter the callback (the track is about to go away)
63             return;
64         }
65 
66         audioRecorder_handleMarker_lockRecord(mAr);
67         mCallbackProtector->exitCb();
68     }
onNewPos(uint32_t)69     void onNewPos(uint32_t) override {
70         if (!CallbackProtector::enterCbIfOk(mCallbackProtector)) {
71             // it is not safe to enter the callback (the track is about to go away)
72             return;
73         }
74 
75         audioRecorder_handleNewPos_lockRecord(mAr);
76         mCallbackProtector->exitCb();
77     }
78 
79     CAudioRecorder * const mAr;
80     const sp<CallbackProtector> mCallbackProtector;
81 };
82 
83 } // namespace android
84