1 /*
2  * Copyright (C) 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 <audiomanager/AudioManager.h>
18 #include <audiomanager/IAudioManager.h>
19 #include <binder/Binder.h>
20 #include <binder/IServiceManager.h>
21 #include <media/RecordingActivityTracker.h>
22 
23 namespace android {
24 
RecordingActivityTracker()25 RecordingActivityTracker::RecordingActivityTracker()
26         : mRIId(RECORD_RIID_INVALID), mToken(new BBinder())
27 {
28     // use checkService() to avoid blocking if audio service is not up yet
29     sp<IBinder> binder = defaultServiceManager()->checkService(String16("audio"));
30     if (binder != 0) {
31         mAudioManager = interface_cast<IAudioManager>(binder);
32     } else {
33         ALOGE("RecordingActivityTracker(): binding to audio service failed, service up?");
34     }
35 }
36 
~RecordingActivityTracker()37 RecordingActivityTracker::~RecordingActivityTracker()
38 {
39     if (mRIId != RECORD_RIID_INVALID && mAudioManager) {
40         mAudioManager->releaseRecorder(mRIId);
41     }
42 }
43 
getRiid()44 audio_unique_id_t RecordingActivityTracker::getRiid()
45 {
46     if (mRIId == RECORD_RIID_INVALID && mAudioManager) {
47         mRIId = mAudioManager->trackRecorder(mToken);
48     }
49     return mRIId;
50 }
51 
recordingStarted()52 void RecordingActivityTracker::recordingStarted()
53 {
54     if (getRiid() != RECORD_RIID_INVALID && mAudioManager) {
55         mAudioManager->recorderEvent(mRIId, RECORDER_STATE_STARTED);
56     }
57 }
58 
recordingStopped()59 void RecordingActivityTracker::recordingStopped()
60 {
61     if (getRiid() != RECORD_RIID_INVALID && mAudioManager) {
62         mAudioManager->recorderEvent(mRIId, RECORDER_STATE_STOPPED);
63     }
64 }
65 
66 } // namespace android
67