• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android::AudioRecord::IAudioRecordCallback {
34   public:
AudioRecordCallback(CAudioRecorder * audioRecorder)35     AudioRecordCallback(CAudioRecorder * audioRecorder) : mAr(audioRecorder) {}
36     AudioRecordCallback(const AudioRecordCallback&) = delete;
37     AudioRecordCallback& operator=(const AudioRecordCallback&) = delete;
38 
39   private:
onMoreData(const android::AudioRecord::Buffer & buffer)40     size_t onMoreData(const android::AudioRecord::Buffer& buffer) override {
41         if (!android::CallbackProtector::enterCbIfOk(mAr->mCallbackProtector)) {
42             // it is not safe to enter the callback (the track is about to go away)
43             return buffer.size(); // replicate existing behavior
44         }
45         size_t bytesRead = audioRecorder_handleMoreData_lockRecord(mAr, buffer);
46         mAr->mCallbackProtector->exitCb();
47         return bytesRead;
48     }
49 
50 
onOverrun()51     void onOverrun() override {
52         if (!android::CallbackProtector::enterCbIfOk(mAr->mCallbackProtector)) {
53             // it is not safe to enter the callback (the track is about to go away)
54             return;
55         }
56         audioRecorder_handleOverrun_lockRecord(mAr);
57         mAr->mCallbackProtector->exitCb();
58     }
onMarker(uint32_t)59     void onMarker(uint32_t) override {
60         if (!android::CallbackProtector::enterCbIfOk(mAr->mCallbackProtector)) {
61             // it is not safe to enter the callback (the track is about to go away)
62             return;
63         }
64 
65         audioRecorder_handleMarker_lockRecord(mAr);
66         mAr->mCallbackProtector->exitCb();
67     }
onNewPos(uint32_t)68     void onNewPos(uint32_t) override {
69         if (!android::CallbackProtector::enterCbIfOk(mAr->mCallbackProtector)) {
70             // it is not safe to enter the callback (the track is about to go away)
71             return;
72         }
73 
74         audioRecorder_handleNewPos_lockRecord(mAr);
75         mAr->mCallbackProtector->exitCb();
76     }
77     CAudioRecorder * const mAr;
78 };
79 
80 } // namespace android
81