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