Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 1 | /* |
| 2 | ** |
| 3 | ** Copyright 2014, 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 | |
| 18 | #ifndef INCLUDING_FROM_AUDIOFLINGER_H |
| 19 | #error This header file should only be included from AudioFlinger.h |
| 20 | #endif |
| 21 | |
Andy Hung | 6ac17eb | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 22 | public: // TODO(b/288339104) extract out of AudioFlinger class |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 23 | // PatchPanel is concealed within AudioFlinger, their lifetimes are the same. |
Andy Hung | b6692eb | 2023-07-13 16:52:46 -0700 | [diff] [blame^] | 24 | class PatchPanel : public IAfPatchPanel { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 25 | public: |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 26 | explicit PatchPanel(AudioFlinger* audioFlinger) : mAudioFlinger(*audioFlinger) {} |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 27 | |
| 28 | /* List connected audio ports and their attributes */ |
| 29 | status_t listAudioPorts(unsigned int *num_ports, |
Andy Hung | b6692eb | 2023-07-13 16:52:46 -0700 | [diff] [blame^] | 30 | struct audio_port* ports) final; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 31 | |
| 32 | /* Get supported attributes for a given audio port */ |
Andy Hung | b6692eb | 2023-07-13 16:52:46 -0700 | [diff] [blame^] | 33 | status_t getAudioPort(struct audio_port_v7* port) final; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 34 | |
| 35 | /* Create a patch between several source and sink ports */ |
| 36 | status_t createAudioPatch(const struct audio_patch *patch, |
Francois Gaffie | e0dc36d | 2021-04-01 16:01:00 +0200 | [diff] [blame] | 37 | audio_patch_handle_t *handle, |
Andy Hung | b6692eb | 2023-07-13 16:52:46 -0700 | [diff] [blame^] | 38 | bool endpointPatch = false) final; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 39 | |
| 40 | /* Release a patch */ |
Andy Hung | b6692eb | 2023-07-13 16:52:46 -0700 | [diff] [blame^] | 41 | status_t releaseAudioPatch(audio_patch_handle_t handle) final; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 42 | |
| 43 | /* List connected audio devices and they attributes */ |
| 44 | status_t listAudioPatches(unsigned int *num_patches, |
Andy Hung | b6692eb | 2023-07-13 16:52:46 -0700 | [diff] [blame^] | 45 | struct audio_patch* patches) final; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 46 | |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame] | 47 | // Retrieves all currently estrablished software patches for a stream |
| 48 | // opened on an intermediate module. |
| 49 | status_t getDownstreamSoftwarePatches(audio_io_handle_t stream, |
Andy Hung | b6692eb | 2023-07-13 16:52:46 -0700 | [diff] [blame^] | 50 | std::vector<SoftwarePatch>* patches) const final; |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame] | 51 | |
| 52 | // Notifies patch panel about all opened and closed streams. |
Eric Laurent | 74c38dc | 2020-12-23 18:19:44 +0100 | [diff] [blame] | 53 | void notifyStreamOpened(AudioHwDevice *audioHwDevice, audio_io_handle_t stream, |
Andy Hung | b6692eb | 2023-07-13 16:52:46 -0700 | [diff] [blame^] | 54 | struct audio_patch* patch) final; |
| 55 | void notifyStreamClosed(audio_io_handle_t stream) final; |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame] | 56 | |
Andy Hung | b6692eb | 2023-07-13 16:52:46 -0700 | [diff] [blame^] | 57 | void dump(int fd) const final; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 58 | |
Eric Laurent | 9b2064c | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 59 | // Call with AudioFlinger mLock held |
Andy Hung | b6692eb | 2023-07-13 16:52:46 -0700 | [diff] [blame^] | 60 | const std::map<audio_patch_handle_t, Patch>& patches_l() const final { return mPatches; } |
| 61 | |
| 62 | // Must be called under AudioFlinger::mLock |
| 63 | status_t getLatencyMs_l(audio_patch_handle_t patchHandle, double* latencyMs) const final; |
| 64 | |
| 65 | void closeThreadInternal_l(const sp<IAfThreadBase>& thread) const final; |
Eric Laurent | 9b2064c | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 66 | |
| 67 | private: |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame] | 68 | AudioHwDevice* findAudioHwDeviceByModule(audio_module_handle_t module); |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 69 | sp<DeviceHalInterface> findHwDeviceByModule(audio_module_handle_t module); |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame] | 70 | void addSoftwarePatchToInsertedModules( |
Eric Laurent | 74c38dc | 2020-12-23 18:19:44 +0100 | [diff] [blame] | 71 | audio_module_handle_t module, audio_patch_handle_t handle, |
| 72 | const struct audio_patch *patch); |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame] | 73 | void removeSoftwarePatchFromInsertedModules(audio_patch_handle_t handle); |
François Gaffie | 58e73af | 2023-02-15 11:47:24 +0100 | [diff] [blame] | 74 | /** |
| 75 | * erase the patch referred by its handle. |
| 76 | * @param handle of the patch to be erased |
| 77 | * @param reuseExistingHalPatch if set, do not trig the callback of listeners, listener |
| 78 | * would receive instead a onUpdateAudioPatch when the patch will be recreated. |
| 79 | * It prevents for example DeviceEffectManager to spuriously remove / add a device on an already |
| 80 | * opened input / output mix. |
| 81 | */ |
| 82 | void erasePatch(audio_patch_handle_t handle, bool reuseExistingHalPatch = false); |
| 83 | |
| 84 | /** |
| 85 | * Returns true if the old and new patches passed as arguments describe the same |
| 86 | * connections between the first sink and the first source |
| 87 | * @param oldPatch previous patch |
| 88 | * @param newPatch new patch |
| 89 | * @return true if the route is unchanged between the old and new patch, false otherwise |
| 90 | */ |
| 91 | inline bool patchesHaveSameRoute( |
| 92 | const struct audio_patch &newPatch, const struct audio_patch &oldPatch) const { |
| 93 | return (newPatch.sources[0].type == AUDIO_PORT_TYPE_DEVICE && |
| 94 | oldPatch.sources[0].type == AUDIO_PORT_TYPE_DEVICE && |
| 95 | newPatch.sources[0].id == oldPatch.sources[0].id && |
| 96 | newPatch.sinks[0].type == AUDIO_PORT_TYPE_MIX && |
| 97 | oldPatch.sinks[0].type == AUDIO_PORT_TYPE_MIX && |
| 98 | newPatch.sinks[0].ext.mix.handle == oldPatch.sinks[0].ext.mix.handle) || |
| 99 | (newPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE && |
| 100 | oldPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE && |
| 101 | newPatch.sinks[0].id == oldPatch.sinks[0].id && |
| 102 | newPatch.sources[0].type == AUDIO_PORT_TYPE_MIX && |
| 103 | oldPatch.sources[0].type == AUDIO_PORT_TYPE_MIX && |
| 104 | newPatch.sources[0].ext.mix.handle == oldPatch.sources[0].ext.mix.handle); |
| 105 | } |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 106 | |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 107 | AudioFlinger &mAudioFlinger; |
| 108 | std::map<audio_patch_handle_t, Patch> mPatches; |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame] | 109 | |
| 110 | // This map allows going from a thread to "downstream" software patches |
| 111 | // when a processing module inserted in between. Example: |
| 112 | // |
| 113 | // from map value.streams map key |
| 114 | // [Mixer thread] --> [Virtual output device] --> [Processing module] ---\ |
| 115 | // [Harware module] <-- [Physical output device] <-- [S/W Patch] <--/ |
| 116 | // from map value.sw_patches |
| 117 | // |
| 118 | // This allows the mixer thread to look up the threads of the software patch |
| 119 | // for propagating timing info, parameters, etc. |
| 120 | // |
| 121 | // The current assumptions are: |
| 122 | // 1) The processing module acts as a mixer with several outputs which |
| 123 | // represent differently downmixed and / or encoded versions of the same |
| 124 | // mixed stream. There is no 1:1 correspondence between the input streams |
| 125 | // and the software patches, but rather a N:N correspondence between |
| 126 | // a group of streams and a group of patches. |
| 127 | // 2) There are only a couple of inserted processing modules in the system, |
| 128 | // so when looking for a stream or patch handle we can iterate over |
| 129 | // all modules. |
| 130 | struct ModuleConnections { |
| 131 | std::set<audio_io_handle_t> streams; |
| 132 | std::set<audio_patch_handle_t> sw_patches; |
| 133 | }; |
| 134 | std::map<audio_module_handle_t, ModuleConnections> mInsertedModules; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 135 | }; |
Andy Hung | 6ac17eb | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 136 | |
| 137 | private: |