blob: b5a3b40a29f98badff88ef34765b6c4d5413d7a8 [file] [log] [blame]
Andy Hunge8986cc2023-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 Hunge45f2192023-05-15 19:02:55 -070019#include <functional>
20#include <mutex>
21
22#include <media/AudioSystem.h>
23#include <utils/RefBase.h>
24
25namespace android::audioflinger {
Andy Hunge8986cc2023-05-15 18:20:49 -070026
27class SyncEvent;
Andy Hunge45f2192023-05-15 19:02:55 -070028using SyncEventCallback = std::function<void(const wp<SyncEvent>& event)>;
Andy Hunge8986cc2023-05-15 18:20:49 -070029
30class SyncEvent : public RefBase {
31public:
32 SyncEvent(AudioSystem::sync_event_t type,
33 audio_session_t triggerSession,
34 audio_session_t listenerSession,
Andy Hunge45f2192023-05-15 19:02:55 -070035 const SyncEventCallback& callBack,
Andy Hunge8986cc2023-05-15 18:20:49 -070036 const wp<RefBase>& cookie)
37 : mType(type), mTriggerSession(triggerSession), mListenerSession(listenerSession),
Andy Hunge45f2192023-05-15 19:02:55 -070038 mCookie(cookie), mCallback(callBack)
Andy Hunge8986cc2023-05-15 18:20:49 -070039 {}
40
Andy Hunge8986cc2023-05-15 18:20:49 -070041 void trigger() {
Andy Hunge45f2192023-05-15 19:02:55 -070042 std::lock_guard l(mLock);
43 if (mCallback) mCallback(wp<SyncEvent>::fromExisting(this));
Andy Hunge8986cc2023-05-15 18:20:49 -070044 }
Andy Hunge45f2192023-05-15 19:02:55 -070045
46 bool isCancelled() const {
47 std::lock_guard l(mLock);
48 return mCallback == nullptr;
49 }
50
51 void cancel() {
52 std::lock_guard l(mLock);
53 mCallback = nullptr;
54 }
55
Andy Hunge8986cc2023-05-15 18:20:49 -070056 AudioSystem::sync_event_t type() const { return mType; }
57 audio_session_t triggerSession() const { return mTriggerSession; }
58 audio_session_t listenerSession() const { return mListenerSession; }
Andy Hunge45f2192023-05-15 19:02:55 -070059 const wp<RefBase>& cookie() const { return mCookie; }
Andy Hunge8986cc2023-05-15 18:20:49 -070060
61private:
62 const AudioSystem::sync_event_t mType;
63 const audio_session_t mTriggerSession;
64 const audio_session_t mListenerSession;
Andy Hunge8986cc2023-05-15 18:20:49 -070065 const wp<RefBase> mCookie;
Andy Hunge45f2192023-05-15 19:02:55 -070066 mutable std::mutex mLock;
67 SyncEventCallback mCallback GUARDED_BY(mLock);
Andy Hunge8986cc2023-05-15 18:20:49 -070068};
69
Andy Hunge45f2192023-05-15 19:02:55 -070070} // namespace android::audioflinger