blob: ededb2616485c2050479b3c5544937cdbe0b8dd1 [file] [log] [blame]
Andy Hung9a820082023-05-15 18:20:49 -07001/*
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
Andy Hung0e03c932023-06-26 14:02:50 -070019#include <any>
Andy Hung068e08e2023-05-15 19:02:55 -070020#include <functional>
21#include <mutex>
22
23#include <media/AudioSystem.h>
24#include <utils/RefBase.h>
25
26namespace android::audioflinger {
Andy Hung9a820082023-05-15 18:20:49 -070027
28class SyncEvent;
Andy Hung068e08e2023-05-15 19:02:55 -070029using SyncEventCallback = std::function<void(const wp<SyncEvent>& event)>;
Andy Hung9a820082023-05-15 18:20:49 -070030
31class SyncEvent : public RefBase {
32public:
33 SyncEvent(AudioSystem::sync_event_t type,
34 audio_session_t triggerSession,
35 audio_session_t listenerSession,
Andy Hung068e08e2023-05-15 19:02:55 -070036 const SyncEventCallback& callBack,
Andy Hung0e03c932023-06-26 14:02:50 -070037 const std::any& cookie)
Andy Hung9a820082023-05-15 18:20:49 -070038 : mType(type), mTriggerSession(triggerSession), mListenerSession(listenerSession),
Andy Hung068e08e2023-05-15 19:02:55 -070039 mCookie(cookie), mCallback(callBack)
Andy Hung9a820082023-05-15 18:20:49 -070040 {}
41
Andy Hung9a820082023-05-15 18:20:49 -070042 void trigger() {
Andy Hung068e08e2023-05-15 19:02:55 -070043 std::lock_guard l(mLock);
44 if (mCallback) mCallback(wp<SyncEvent>::fromExisting(this));
Andy Hung9a820082023-05-15 18:20:49 -070045 }
Andy Hung068e08e2023-05-15 19:02:55 -070046
47 bool isCancelled() const {
48 std::lock_guard l(mLock);
49 return mCallback == nullptr;
50 }
51
52 void cancel() {
53 std::lock_guard l(mLock);
54 mCallback = nullptr;
55 }
56
Andy Hung9a820082023-05-15 18:20:49 -070057 AudioSystem::sync_event_t type() const { return mType; }
58 audio_session_t triggerSession() const { return mTriggerSession; }
59 audio_session_t listenerSession() const { return mListenerSession; }
Andy Hung0e03c932023-06-26 14:02:50 -070060 const std::any& cookie() const { return mCookie; }
Andy Hung9a820082023-05-15 18:20:49 -070061
62private:
63 const AudioSystem::sync_event_t mType;
64 const audio_session_t mTriggerSession;
65 const audio_session_t mListenerSession;
Andy Hung0e03c932023-06-26 14:02:50 -070066 const std::any mCookie;
Andy Hung068e08e2023-05-15 19:02:55 -070067 mutable std::mutex mLock;
68 SyncEventCallback mCallback GUARDED_BY(mLock);
Andy Hung9a820082023-05-15 18:20:49 -070069};
70
Andy Hung068e08e2023-05-15 19:02:55 -070071} // namespace android::audioflinger