• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <any>
20 #include <functional>
21 #include <mutex>
22 
23 #include <media/AudioSystem.h>
24 #include <utils/RefBase.h>
25 
26 namespace android::audioflinger {
27 
28 class SyncEvent;
29 using SyncEventCallback = std::function<void(const wp<SyncEvent>& event)>;
30 
31 class SyncEvent : public RefBase {
32 public:
SyncEvent(AudioSystem::sync_event_t type,audio_session_t triggerSession,audio_session_t listenerSession,const SyncEventCallback & callBack,const std::any & cookie)33     SyncEvent(AudioSystem::sync_event_t type,
34               audio_session_t triggerSession,
35               audio_session_t listenerSession,
36               const SyncEventCallback& callBack,
37               const std::any& cookie)
38     : mType(type), mTriggerSession(triggerSession), mListenerSession(listenerSession),
39       mCookie(cookie), mCallback(callBack)
40     {}
41 
trigger()42     void trigger() {
43         std::lock_guard l(mLock);
44         if (mCallback) mCallback(wp<SyncEvent>::fromExisting(this));
45     }
46 
isCancelled()47     bool isCancelled() const {
48         std::lock_guard l(mLock);
49         return mCallback == nullptr;
50     }
51 
cancel()52     void cancel() {
53         std::lock_guard l(mLock);
54         mCallback = nullptr;
55     }
56 
type()57     AudioSystem::sync_event_t type() const { return mType; }
triggerSession()58     audio_session_t triggerSession() const { return mTriggerSession; }
listenerSession()59     audio_session_t listenerSession() const { return mListenerSession; }
cookie()60     const std::any& cookie() const { return mCookie; }
61 
62 private:
63       const AudioSystem::sync_event_t mType;
64       const audio_session_t mTriggerSession;
65       const audio_session_t mListenerSession;
66       const std::any mCookie;
67       mutable std::mutex mLock;
68       SyncEventCallback mCallback GUARDED_BY(mLock);
69 };
70 
71 } // namespace android::audioflinger
72