| Andy Hung | 9a82008 | 2023-05-15 18:20:49 -0700 | [diff] [blame] | 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 | |
| Andy Hung | 0e03c93 | 2023-06-26 14:02:50 -0700 | [diff] [blame] | 19 | #include <any> |
| Andy Hung | 068e08e | 2023-05-15 19:02:55 -0700 | [diff] [blame] | 20 | #include <functional> |
| 21 | #include <mutex> |
| 22 | |
| 23 | #include <media/AudioSystem.h> |
| 24 | #include <utils/RefBase.h> |
| 25 | |
| 26 | namespace android::audioflinger { |
| Andy Hung | 9a82008 | 2023-05-15 18:20:49 -0700 | [diff] [blame] | 27 | |
| 28 | class SyncEvent; |
| Andy Hung | 068e08e | 2023-05-15 19:02:55 -0700 | [diff] [blame] | 29 | using SyncEventCallback = std::function<void(const wp<SyncEvent>& event)>; |
| Andy Hung | 9a82008 | 2023-05-15 18:20:49 -0700 | [diff] [blame] | 30 | |
| 31 | class SyncEvent : public RefBase { |
| 32 | public: |
| 33 | SyncEvent(AudioSystem::sync_event_t type, |
| 34 | audio_session_t triggerSession, |
| 35 | audio_session_t listenerSession, |
| Andy Hung | 068e08e | 2023-05-15 19:02:55 -0700 | [diff] [blame] | 36 | const SyncEventCallback& callBack, |
| Andy Hung | 0e03c93 | 2023-06-26 14:02:50 -0700 | [diff] [blame] | 37 | const std::any& cookie) |
| Andy Hung | 9a82008 | 2023-05-15 18:20:49 -0700 | [diff] [blame] | 38 | : mType(type), mTriggerSession(triggerSession), mListenerSession(listenerSession), |
| Andy Hung | 068e08e | 2023-05-15 19:02:55 -0700 | [diff] [blame] | 39 | mCookie(cookie), mCallback(callBack) |
| Andy Hung | 9a82008 | 2023-05-15 18:20:49 -0700 | [diff] [blame] | 40 | {} |
| 41 | |
| Andy Hung | 9a82008 | 2023-05-15 18:20:49 -0700 | [diff] [blame] | 42 | void trigger() { |
| Andy Hung | 068e08e | 2023-05-15 19:02:55 -0700 | [diff] [blame] | 43 | std::lock_guard l(mLock); |
| 44 | if (mCallback) mCallback(wp<SyncEvent>::fromExisting(this)); |
| Andy Hung | 9a82008 | 2023-05-15 18:20:49 -0700 | [diff] [blame] | 45 | } |
| Andy Hung | 068e08e | 2023-05-15 19:02:55 -0700 | [diff] [blame] | 46 | |
| 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 Hung | 9a82008 | 2023-05-15 18:20:49 -0700 | [diff] [blame] | 57 | 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 Hung | 0e03c93 | 2023-06-26 14:02:50 -0700 | [diff] [blame] | 60 | const std::any& cookie() const { return mCookie; } |
| Andy Hung | 9a82008 | 2023-05-15 18:20:49 -0700 | [diff] [blame] | 61 | |
| 62 | private: |
| 63 | const AudioSystem::sync_event_t mType; |
| 64 | const audio_session_t mTriggerSession; |
| 65 | const audio_session_t mListenerSession; |
| Andy Hung | 0e03c93 | 2023-06-26 14:02:50 -0700 | [diff] [blame] | 66 | const std::any mCookie; |
| Andy Hung | 068e08e | 2023-05-15 19:02:55 -0700 | [diff] [blame] | 67 | mutable std::mutex mLock; |
| 68 | SyncEventCallback mCallback GUARDED_BY(mLock); |
| Andy Hung | 9a82008 | 2023-05-15 18:20:49 -0700 | [diff] [blame] | 69 | }; |
| 70 | |
| Andy Hung | 068e08e | 2023-05-15 19:02:55 -0700 | [diff] [blame] | 71 | } // namespace android::audioflinger |