| Vlad Popa | 5161f8a | 2022-10-10 16:17:20 +0200 | [diff] [blame] | 1 | /* |
| 2 | ** |
| 3 | ** Copyright 2022, The Android Open Source Project |
| 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| Andy Hung | 61c0617 | 2023-07-13 19:33:17 -0700 | [diff] [blame] | 18 | #pragma once |
| 19 | |
| Andy Hung | 663dce5 | 2023-07-18 18:31:50 -0700 | [diff] [blame] | 20 | #include "IAfPatchPanel.h" |
| 21 | |
| 22 | #include <utils/RefBase.h> // avoid transitive dependency |
| 23 | #include <utils/Thread.h> // avoid transitive dependency |
| 24 | |
| 25 | #include <deque> |
| 26 | #include <mutex> // avoid transitive dependency |
| 27 | |
| Andy Hung | 61c0617 | 2023-07-13 19:33:17 -0700 | [diff] [blame] | 28 | namespace android { |
| Vlad Popa | 5161f8a | 2022-10-10 16:17:20 +0200 | [diff] [blame] | 29 | |
| 30 | class Command; |
| 31 | |
| 32 | // Thread to execute create and release patch commands asynchronously. This is needed because |
| Andy Hung | c0ab56b | 2023-07-13 18:11:33 -0700 | [diff] [blame] | 33 | // IAfPatchPanel::createAudioPatch and releaseAudioPatch are executed from audio policy service |
| Vlad Popa | 5161f8a | 2022-10-10 16:17:20 +0200 | [diff] [blame] | 34 | // with mutex locked and effect management requires to call back into audio policy service |
| 35 | class PatchCommandThread : public Thread { |
| 36 | public: |
| 37 | |
| 38 | enum { |
| 39 | CREATE_AUDIO_PATCH, |
| 40 | RELEASE_AUDIO_PATCH, |
| 41 | }; |
| 42 | |
| 43 | class PatchCommandListener : public virtual RefBase { |
| 44 | public: |
| 45 | virtual void onCreateAudioPatch(audio_patch_handle_t handle, |
| Andy Hung | c0ab56b | 2023-07-13 18:11:33 -0700 | [diff] [blame] | 46 | const IAfPatchPanel::Patch& patch) = 0; |
| Vlad Popa | 5161f8a | 2022-10-10 16:17:20 +0200 | [diff] [blame] | 47 | virtual void onReleaseAudioPatch(audio_patch_handle_t handle) = 0; |
| 48 | }; |
| 49 | |
| 50 | PatchCommandThread() : Thread(false /* canCallJava */) {} |
| 51 | ~PatchCommandThread() override; |
| 52 | |
| 53 | void addListener(const sp<PatchCommandListener>& listener); |
| 54 | |
| Andy Hung | c0ab56b | 2023-07-13 18:11:33 -0700 | [diff] [blame] | 55 | void createAudioPatch(audio_patch_handle_t handle, const IAfPatchPanel::Patch& patch); |
| Vlad Popa | 5161f8a | 2022-10-10 16:17:20 +0200 | [diff] [blame] | 56 | void releaseAudioPatch(audio_patch_handle_t handle); |
| 57 | |
| 58 | // Thread virtuals |
| 59 | void onFirstRef() override; |
| 60 | bool threadLoop() override; |
| 61 | |
| 62 | void exit(); |
| 63 | |
| 64 | void createAudioPatchCommand(audio_patch_handle_t handle, |
| Andy Hung | c0ab56b | 2023-07-13 18:11:33 -0700 | [diff] [blame] | 65 | const IAfPatchPanel::Patch& patch); |
| Vlad Popa | 5161f8a | 2022-10-10 16:17:20 +0200 | [diff] [blame] | 66 | void releaseAudioPatchCommand(audio_patch_handle_t handle); |
| 67 | |
| 68 | private: |
| 69 | class CommandData; |
| 70 | |
| 71 | // Command type received from the PatchPanel |
| 72 | class Command: public RefBase { |
| 73 | public: |
| 74 | Command() = default; |
| 75 | Command(int command, const sp<CommandData>& data) |
| 76 | : mCommand(command), mData(data) {} |
| 77 | |
| 78 | const int mCommand = -1; |
| 79 | const sp<CommandData> mData; |
| 80 | }; |
| 81 | |
| 82 | class CommandData: public RefBase {}; |
| 83 | |
| 84 | class CreateAudioPatchData : public CommandData { |
| 85 | public: |
| Andy Hung | c0ab56b | 2023-07-13 18:11:33 -0700 | [diff] [blame] | 86 | CreateAudioPatchData(audio_patch_handle_t handle, const IAfPatchPanel::Patch& patch) |
| Vlad Popa | 5161f8a | 2022-10-10 16:17:20 +0200 | [diff] [blame] | 87 | : mHandle(handle), mPatch(patch) {} |
| 88 | |
| 89 | const audio_patch_handle_t mHandle; |
| Andy Hung | c0ab56b | 2023-07-13 18:11:33 -0700 | [diff] [blame] | 90 | const IAfPatchPanel::Patch mPatch; |
| Vlad Popa | 5161f8a | 2022-10-10 16:17:20 +0200 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | class ReleaseAudioPatchData : public CommandData { |
| 94 | public: |
| Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 95 | explicit ReleaseAudioPatchData(audio_patch_handle_t handle) |
| Vlad Popa | 5161f8a | 2022-10-10 16:17:20 +0200 | [diff] [blame] | 96 | : mHandle(handle) {} |
| 97 | |
| 98 | audio_patch_handle_t mHandle; |
| 99 | }; |
| 100 | |
| 101 | void sendCommand(const sp<Command>& command); |
| 102 | |
| 103 | std::string mThreadName; |
| 104 | std::mutex mLock; |
| 105 | std::condition_variable mWaitWorkCV; |
| 106 | std::deque<sp<Command>> mCommands GUARDED_BY(mLock); // list of pending commands |
| 107 | |
| 108 | std::mutex mListenerLock; |
| 109 | std::vector<wp<PatchCommandListener>> mListeners GUARDED_BY(mListenerLock); |
| 110 | }; |
| Andy Hung | 61c0617 | 2023-07-13 19:33:17 -0700 | [diff] [blame] | 111 | |
| 112 | } // namespace android |