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 | d6e8639 | 2023-07-13 19:33:17 -0700 | [diff] [blame] | 18 | #pragma once |
| 19 | |
Andy Hung | c6f227f | 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 | d6e8639 | 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 | 8e6b62a | 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, |
François Gaffie | 58e73af | 2023-02-15 11:47:24 +0100 | [diff] [blame] | 41 | UPDATE_AUDIO_PATCH, |
Vlad Popa | 5161f8a | 2022-10-10 16:17:20 +0200 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | class PatchCommandListener : public virtual RefBase { |
| 45 | public: |
| 46 | virtual void onCreateAudioPatch(audio_patch_handle_t handle, |
Andy Hung | 8e6b62a | 2023-07-13 18:11:33 -0700 | [diff] [blame] | 47 | const IAfPatchPanel::Patch& patch) = 0; |
Vlad Popa | 5161f8a | 2022-10-10 16:17:20 +0200 | [diff] [blame] | 48 | virtual void onReleaseAudioPatch(audio_patch_handle_t handle) = 0; |
François Gaffie | 58e73af | 2023-02-15 11:47:24 +0100 | [diff] [blame] | 49 | virtual void onUpdateAudioPatch(audio_patch_handle_t oldHandle, |
| 50 | audio_patch_handle_t newHandle, |
Andy Hung | 8e6b62a | 2023-07-13 18:11:33 -0700 | [diff] [blame] | 51 | const IAfPatchPanel::Patch& patch) = 0; |
Vlad Popa | 5161f8a | 2022-10-10 16:17:20 +0200 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | PatchCommandThread() : Thread(false /* canCallJava */) {} |
| 55 | ~PatchCommandThread() override; |
| 56 | |
| 57 | void addListener(const sp<PatchCommandListener>& listener); |
| 58 | |
Andy Hung | 8e6b62a | 2023-07-13 18:11:33 -0700 | [diff] [blame] | 59 | void createAudioPatch(audio_patch_handle_t handle, const IAfPatchPanel::Patch& patch); |
Vlad Popa | 5161f8a | 2022-10-10 16:17:20 +0200 | [diff] [blame] | 60 | void releaseAudioPatch(audio_patch_handle_t handle); |
François Gaffie | 58e73af | 2023-02-15 11:47:24 +0100 | [diff] [blame] | 61 | void updateAudioPatch(audio_patch_handle_t oldHandle, |
| 62 | audio_patch_handle_t newHandle, |
Andy Hung | 8e6b62a | 2023-07-13 18:11:33 -0700 | [diff] [blame] | 63 | const IAfPatchPanel::Patch& patch); |
Vlad Popa | 5161f8a | 2022-10-10 16:17:20 +0200 | [diff] [blame] | 64 | |
| 65 | // Thread virtuals |
| 66 | void onFirstRef() override; |
| 67 | bool threadLoop() override; |
| 68 | |
| 69 | void exit(); |
| 70 | |
| 71 | void createAudioPatchCommand(audio_patch_handle_t handle, |
Andy Hung | 8e6b62a | 2023-07-13 18:11:33 -0700 | [diff] [blame] | 72 | const IAfPatchPanel::Patch& patch); |
Vlad Popa | 5161f8a | 2022-10-10 16:17:20 +0200 | [diff] [blame] | 73 | void releaseAudioPatchCommand(audio_patch_handle_t handle); |
François Gaffie | 58e73af | 2023-02-15 11:47:24 +0100 | [diff] [blame] | 74 | void updateAudioPatchCommand(audio_patch_handle_t oldHandle, |
| 75 | audio_patch_handle_t newHandle, |
Andy Hung | 8e6b62a | 2023-07-13 18:11:33 -0700 | [diff] [blame] | 76 | const IAfPatchPanel::Patch& patch); |
Vlad Popa | 5161f8a | 2022-10-10 16:17:20 +0200 | [diff] [blame] | 77 | private: |
| 78 | class CommandData; |
| 79 | |
| 80 | // Command type received from the PatchPanel |
| 81 | class Command: public RefBase { |
| 82 | public: |
| 83 | Command() = default; |
| 84 | Command(int command, const sp<CommandData>& data) |
| 85 | : mCommand(command), mData(data) {} |
| 86 | |
| 87 | const int mCommand = -1; |
| 88 | const sp<CommandData> mData; |
| 89 | }; |
| 90 | |
| 91 | class CommandData: public RefBase {}; |
| 92 | |
| 93 | class CreateAudioPatchData : public CommandData { |
| 94 | public: |
Andy Hung | 8e6b62a | 2023-07-13 18:11:33 -0700 | [diff] [blame] | 95 | CreateAudioPatchData(audio_patch_handle_t handle, const IAfPatchPanel::Patch& patch) |
Vlad Popa | 5161f8a | 2022-10-10 16:17:20 +0200 | [diff] [blame] | 96 | : mHandle(handle), mPatch(patch) {} |
| 97 | |
| 98 | const audio_patch_handle_t mHandle; |
Andy Hung | 8e6b62a | 2023-07-13 18:11:33 -0700 | [diff] [blame] | 99 | const IAfPatchPanel::Patch mPatch; |
Vlad Popa | 5161f8a | 2022-10-10 16:17:20 +0200 | [diff] [blame] | 100 | }; |
| 101 | |
| 102 | class ReleaseAudioPatchData : public CommandData { |
| 103 | public: |
Andy Hung | f9829e4 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 104 | explicit ReleaseAudioPatchData(audio_patch_handle_t handle) |
Vlad Popa | 5161f8a | 2022-10-10 16:17:20 +0200 | [diff] [blame] | 105 | : mHandle(handle) {} |
| 106 | |
| 107 | audio_patch_handle_t mHandle; |
| 108 | }; |
| 109 | |
François Gaffie | 58e73af | 2023-02-15 11:47:24 +0100 | [diff] [blame] | 110 | class UpdateAudioPatchData : public CommandData { |
| 111 | public: |
| 112 | UpdateAudioPatchData(audio_patch_handle_t oldHandle, |
| 113 | audio_patch_handle_t newHandle, |
Andy Hung | 8e6b62a | 2023-07-13 18:11:33 -0700 | [diff] [blame] | 114 | const IAfPatchPanel::Patch& patch) |
François Gaffie | 58e73af | 2023-02-15 11:47:24 +0100 | [diff] [blame] | 115 | : mOldHandle(oldHandle), mNewHandle(newHandle), mPatch(patch) {} |
| 116 | |
| 117 | const audio_patch_handle_t mOldHandle; |
| 118 | const audio_patch_handle_t mNewHandle; |
Andy Hung | 8e6b62a | 2023-07-13 18:11:33 -0700 | [diff] [blame] | 119 | const IAfPatchPanel::Patch mPatch; |
François Gaffie | 58e73af | 2023-02-15 11:47:24 +0100 | [diff] [blame] | 120 | }; |
| 121 | |
Vlad Popa | 5161f8a | 2022-10-10 16:17:20 +0200 | [diff] [blame] | 122 | void sendCommand(const sp<Command>& command); |
| 123 | |
| 124 | std::string mThreadName; |
| 125 | std::mutex mLock; |
| 126 | std::condition_variable mWaitWorkCV; |
| 127 | std::deque<sp<Command>> mCommands GUARDED_BY(mLock); // list of pending commands |
| 128 | |
| 129 | std::mutex mListenerLock; |
| 130 | std::vector<wp<PatchCommandListener>> mListeners GUARDED_BY(mListenerLock); |
| 131 | }; |
Andy Hung | d6e8639 | 2023-07-13 19:33:17 -0700 | [diff] [blame] | 132 | |
| 133 | } // namespace android |