blob: a312fb7a62d67fe5b078b81e2a4a3028d7af45d8 [file] [log] [blame]
Vlad Popa5161f8a2022-10-10 16:17:20 +02001/*
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 Hung61c06172023-07-13 19:33:17 -070018#pragma once
19
Andy Hung663dce52023-07-18 18:31:50 -070020#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 Hung61c06172023-07-13 19:33:17 -070028namespace android {
Vlad Popa5161f8a2022-10-10 16:17:20 +020029
30class Command;
31
32// Thread to execute create and release patch commands asynchronously. This is needed because
Andy Hungc0ab56b2023-07-13 18:11:33 -070033// IAfPatchPanel::createAudioPatch and releaseAudioPatch are executed from audio policy service
Vlad Popa5161f8a2022-10-10 16:17:20 +020034// with mutex locked and effect management requires to call back into audio policy service
35class PatchCommandThread : public Thread {
36public:
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 Hungc0ab56b2023-07-13 18:11:33 -070046 const IAfPatchPanel::Patch& patch) = 0;
Vlad Popa5161f8a2022-10-10 16:17:20 +020047 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 Hungc0ab56b2023-07-13 18:11:33 -070055 void createAudioPatch(audio_patch_handle_t handle, const IAfPatchPanel::Patch& patch);
Vlad Popa5161f8a2022-10-10 16:17:20 +020056 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 Hungc0ab56b2023-07-13 18:11:33 -070065 const IAfPatchPanel::Patch& patch);
Vlad Popa5161f8a2022-10-10 16:17:20 +020066 void releaseAudioPatchCommand(audio_patch_handle_t handle);
67
68private:
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 Hungc0ab56b2023-07-13 18:11:33 -070086 CreateAudioPatchData(audio_patch_handle_t handle, const IAfPatchPanel::Patch& patch)
Vlad Popa5161f8a2022-10-10 16:17:20 +020087 : mHandle(handle), mPatch(patch) {}
88
89 const audio_patch_handle_t mHandle;
Andy Hungc0ab56b2023-07-13 18:11:33 -070090 const IAfPatchPanel::Patch mPatch;
Vlad Popa5161f8a2022-10-10 16:17:20 +020091 };
92
93 class ReleaseAudioPatchData : public CommandData {
94 public:
Andy Hung920f6572022-10-06 12:09:49 -070095 explicit ReleaseAudioPatchData(audio_patch_handle_t handle)
Vlad Popa5161f8a2022-10-10 16:17:20 +020096 : 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 Hung61c06172023-07-13 19:33:17 -0700111
112} // namespace android