blob: a80a0e05fbcb228ac1eb1fb61a66b55053212ff9 [file] [log] [blame]
Eric Laurent1c333e22014-05-20 10:48:17 -07001/*
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
Andy Hung8e6b62a2023-07-13 18:11:33 -070018#pragma once
Eric Laurent1c333e22014-05-20 10:48:17 -070019
Andy Hung8e6b62a2023-07-13 18:11:33 -070020namespace android {
21
Andy Hungb6692eb2023-07-13 16:52:46 -070022class PatchPanel : public IAfPatchPanel {
Eric Laurent1c333e22014-05-20 10:48:17 -070023public:
Andy Hung2dc61c42023-07-17 14:36:08 -070024 explicit PatchPanel(const sp<IAfPatchPanelCallback>& afPatchPanelCallback)
25 : mAfPatchPanelCallback(afPatchPanelCallback) {}
Eric Laurent1c333e22014-05-20 10:48:17 -070026
27 /* List connected audio ports and their attributes */
28 status_t listAudioPorts(unsigned int *num_ports,
Andy Hungb6692eb2023-07-13 16:52:46 -070029 struct audio_port* ports) final;
Eric Laurent1c333e22014-05-20 10:48:17 -070030
31 /* Get supported attributes for a given audio port */
Andy Hungb6692eb2023-07-13 16:52:46 -070032 status_t getAudioPort(struct audio_port_v7* port) final;
Eric Laurent1c333e22014-05-20 10:48:17 -070033
34 /* Create a patch between several source and sink ports */
35 status_t createAudioPatch(const struct audio_patch *patch,
Francois Gaffiee0dc36d2021-04-01 16:01:00 +020036 audio_patch_handle_t *handle,
Andy Hungb6692eb2023-07-13 16:52:46 -070037 bool endpointPatch = false) final;
Eric Laurent1c333e22014-05-20 10:48:17 -070038
39 /* Release a patch */
Andy Hungb6692eb2023-07-13 16:52:46 -070040 status_t releaseAudioPatch(audio_patch_handle_t handle) final;
Eric Laurent1c333e22014-05-20 10:48:17 -070041
42 /* List connected audio devices and they attributes */
43 status_t listAudioPatches(unsigned int *num_patches,
Andy Hungb6692eb2023-07-13 16:52:46 -070044 struct audio_patch* patches) final;
Eric Laurent1c333e22014-05-20 10:48:17 -070045
Mikhail Naganovadca70f2018-07-09 12:49:25 -070046 // Retrieves all currently estrablished software patches for a stream
47 // opened on an intermediate module.
48 status_t getDownstreamSoftwarePatches(audio_io_handle_t stream,
Andy Hungb6692eb2023-07-13 16:52:46 -070049 std::vector<SoftwarePatch>* patches) const final;
Mikhail Naganovadca70f2018-07-09 12:49:25 -070050
51 // Notifies patch panel about all opened and closed streams.
Eric Laurent74c38dc2020-12-23 18:19:44 +010052 void notifyStreamOpened(AudioHwDevice *audioHwDevice, audio_io_handle_t stream,
Andy Hungb6692eb2023-07-13 16:52:46 -070053 struct audio_patch* patch) final;
54 void notifyStreamClosed(audio_io_handle_t stream) final;
Mikhail Naganovadca70f2018-07-09 12:49:25 -070055
Andy Hungb6692eb2023-07-13 16:52:46 -070056 void dump(int fd) const final;
Eric Laurent83b88082014-06-20 18:31:16 -070057
Eric Laurent9b2064c2019-11-22 17:25:04 -080058 // Call with AudioFlinger mLock held
Andy Hungb6692eb2023-07-13 16:52:46 -070059 const std::map<audio_patch_handle_t, Patch>& patches_l() const final { return mPatches; }
60
61 // Must be called under AudioFlinger::mLock
62 status_t getLatencyMs_l(audio_patch_handle_t patchHandle, double* latencyMs) const final;
63
64 void closeThreadInternal_l(const sp<IAfThreadBase>& thread) const final;
Eric Laurent9b2064c2019-11-22 17:25:04 -080065
66private:
Mikhail Naganovadca70f2018-07-09 12:49:25 -070067 AudioHwDevice* findAudioHwDeviceByModule(audio_module_handle_t module);
Mikhail Naganov444ecc32018-05-01 17:40:05 -070068 sp<DeviceHalInterface> findHwDeviceByModule(audio_module_handle_t module);
Mikhail Naganovadca70f2018-07-09 12:49:25 -070069 void addSoftwarePatchToInsertedModules(
Eric Laurent74c38dc2020-12-23 18:19:44 +010070 audio_module_handle_t module, audio_patch_handle_t handle,
71 const struct audio_patch *patch);
Mikhail Naganovadca70f2018-07-09 12:49:25 -070072 void removeSoftwarePatchFromInsertedModules(audio_patch_handle_t handle);
François Gaffie58e73af2023-02-15 11:47:24 +010073 /**
74 * erase the patch referred by its handle.
75 * @param handle of the patch to be erased
76 * @param reuseExistingHalPatch if set, do not trig the callback of listeners, listener
77 * would receive instead a onUpdateAudioPatch when the patch will be recreated.
78 * It prevents for example DeviceEffectManager to spuriously remove / add a device on an already
79 * opened input / output mix.
80 */
81 void erasePatch(audio_patch_handle_t handle, bool reuseExistingHalPatch = false);
82
83 /**
84 * Returns true if the old and new patches passed as arguments describe the same
85 * connections between the first sink and the first source
86 * @param oldPatch previous patch
87 * @param newPatch new patch
88 * @return true if the route is unchanged between the old and new patch, false otherwise
89 */
90 inline bool patchesHaveSameRoute(
91 const struct audio_patch &newPatch, const struct audio_patch &oldPatch) const {
92 return (newPatch.sources[0].type == AUDIO_PORT_TYPE_DEVICE &&
93 oldPatch.sources[0].type == AUDIO_PORT_TYPE_DEVICE &&
94 newPatch.sources[0].id == oldPatch.sources[0].id &&
95 newPatch.sinks[0].type == AUDIO_PORT_TYPE_MIX &&
96 oldPatch.sinks[0].type == AUDIO_PORT_TYPE_MIX &&
97 newPatch.sinks[0].ext.mix.handle == oldPatch.sinks[0].ext.mix.handle) ||
98 (newPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE &&
99 oldPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE &&
100 newPatch.sinks[0].id == oldPatch.sinks[0].id &&
101 newPatch.sources[0].type == AUDIO_PORT_TYPE_MIX &&
102 oldPatch.sources[0].type == AUDIO_PORT_TYPE_MIX &&
103 newPatch.sources[0].ext.mix.handle == oldPatch.sources[0].ext.mix.handle);
104 }
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700105
Andy Hung2dc61c42023-07-17 14:36:08 -0700106 const sp<IAfPatchPanelCallback> mAfPatchPanelCallback;
Mikhail Naganovdea53042018-04-26 13:10:21 -0700107 std::map<audio_patch_handle_t, Patch> mPatches;
Mikhail Naganovadca70f2018-07-09 12:49:25 -0700108
109 // This map allows going from a thread to "downstream" software patches
110 // when a processing module inserted in between. Example:
111 //
112 // from map value.streams map key
113 // [Mixer thread] --> [Virtual output device] --> [Processing module] ---\
114 // [Harware module] <-- [Physical output device] <-- [S/W Patch] <--/
115 // from map value.sw_patches
116 //
117 // This allows the mixer thread to look up the threads of the software patch
118 // for propagating timing info, parameters, etc.
119 //
120 // The current assumptions are:
121 // 1) The processing module acts as a mixer with several outputs which
122 // represent differently downmixed and / or encoded versions of the same
123 // mixed stream. There is no 1:1 correspondence between the input streams
124 // and the software patches, but rather a N:N correspondence between
125 // a group of streams and a group of patches.
126 // 2) There are only a couple of inserted processing modules in the system,
127 // so when looking for a stream or patch handle we can iterate over
128 // all modules.
129 struct ModuleConnections {
130 std::set<audio_io_handle_t> streams;
131 std::set<audio_patch_handle_t> sw_patches;
132 };
133 std::map<audio_module_handle_t, ModuleConnections> mInsertedModules;
Eric Laurent1c333e22014-05-20 10:48:17 -0700134};
Andy Hung6ac17eb2023-06-20 18:56:17 -0700135
Andy Hung8e6b62a2023-07-13 18:11:33 -0700136} // namespace android