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 | |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame] | 22 | |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 23 | // PatchPanel is concealed within AudioFlinger, their lifetimes are the same. |
| 24 | class PatchPanel { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 25 | public: |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame] | 26 | class SoftwarePatch { |
| 27 | public: |
| 28 | SoftwarePatch(const PatchPanel &patchPanel, audio_patch_handle_t patchHandle, |
| 29 | audio_io_handle_t playbackThreadHandle, audio_io_handle_t recordThreadHandle) |
| 30 | : mPatchPanel(patchPanel), mPatchHandle(patchHandle), |
| 31 | mPlaybackThreadHandle(playbackThreadHandle), |
| 32 | mRecordThreadHandle(recordThreadHandle) {} |
| 33 | SoftwarePatch(const SoftwarePatch&) = default; |
| 34 | SoftwarePatch& operator=(const SoftwarePatch&) = default; |
| 35 | |
| 36 | // Must be called under AudioFlinger::mLock |
| 37 | status_t getLatencyMs_l(double *latencyMs) const; |
Andy Hung | 2dbffc2 | 2018-08-08 18:50:41 -0700 | [diff] [blame] | 38 | audio_patch_handle_t getPatchHandle() const { return mPatchHandle; }; |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame] | 39 | audio_io_handle_t getPlaybackThreadHandle() const { return mPlaybackThreadHandle; }; |
| 40 | audio_io_handle_t getRecordThreadHandle() const { return mRecordThreadHandle; }; |
| 41 | private: |
| 42 | const PatchPanel &mPatchPanel; |
| 43 | const audio_patch_handle_t mPatchHandle; |
| 44 | const audio_io_handle_t mPlaybackThreadHandle; |
| 45 | const audio_io_handle_t mRecordThreadHandle; |
| 46 | }; |
| 47 | |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 48 | explicit PatchPanel(AudioFlinger* audioFlinger) : mAudioFlinger(*audioFlinger) {} |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 49 | |
| 50 | /* List connected audio ports and their attributes */ |
| 51 | status_t listAudioPorts(unsigned int *num_ports, |
| 52 | struct audio_port *ports); |
| 53 | |
| 54 | /* Get supported attributes for a given audio port */ |
| 55 | status_t getAudioPort(struct audio_port *port); |
| 56 | |
| 57 | /* Create a patch between several source and sink ports */ |
| 58 | status_t createAudioPatch(const struct audio_patch *patch, |
| 59 | audio_patch_handle_t *handle); |
| 60 | |
| 61 | /* Release a patch */ |
| 62 | status_t releaseAudioPatch(audio_patch_handle_t handle); |
| 63 | |
| 64 | /* List connected audio devices and they attributes */ |
| 65 | status_t listAudioPatches(unsigned int *num_patches, |
| 66 | struct audio_patch *patches); |
| 67 | |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame] | 68 | // Retrieves all currently estrablished software patches for a stream |
| 69 | // opened on an intermediate module. |
| 70 | status_t getDownstreamSoftwarePatches(audio_io_handle_t stream, |
| 71 | std::vector<SoftwarePatch> *patches) const; |
| 72 | |
| 73 | // Notifies patch panel about all opened and closed streams. |
| 74 | void notifyStreamOpened(AudioHwDevice *audioHwDevice, audio_io_handle_t stream); |
| 75 | void notifyStreamClosed(audio_io_handle_t stream); |
| 76 | |
| 77 | void dump(int fd) const; |
Mikhail Naganov | 201369b | 2018-05-16 16:52:32 -0700 | [diff] [blame] | 78 | |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 79 | private: |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 80 | template<typename ThreadType, typename TrackType> |
| 81 | class Endpoint { |
| 82 | public: |
Mikhail Naganov | 9dfa264 | 2018-05-10 10:09:19 -0700 | [diff] [blame] | 83 | Endpoint() = default; |
Mikhail Naganov | fa97d9b | 2019-02-05 14:08:21 -0800 | [diff] [blame] | 84 | Endpoint(const Endpoint&) = delete; |
| 85 | Endpoint& operator=(const Endpoint&) = delete; |
| 86 | Endpoint(Endpoint&& other) noexcept { swap(other); } |
| 87 | Endpoint& operator=(Endpoint&& other) noexcept { |
| 88 | swap(other); |
| 89 | return *this; |
| 90 | } |
| 91 | ~Endpoint() { |
Mikhail Naganov | 9dfa264 | 2018-05-10 10:09:19 -0700 | [diff] [blame] | 92 | ALOGE_IF(mHandle != AUDIO_PATCH_HANDLE_NONE, |
| 93 | "A non empty Patch Endpoint leaked, handle %d", mHandle); |
Mikhail Naganov | 9dfa264 | 2018-05-10 10:09:19 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 96 | status_t checkTrack(TrackType *trackOrNull) const { |
| 97 | if (trackOrNull == nullptr) return NO_MEMORY; |
| 98 | return trackOrNull->initCheck(); |
| 99 | } |
| 100 | audio_patch_handle_t handle() const { return mHandle; } |
| 101 | sp<ThreadType> thread() { return mThread; } |
| 102 | sp<TrackType> track() { return mTrack; } |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame] | 103 | sp<const ThreadType> const_thread() const { return mThread; } |
Andy Hung | c3ab773 | 2018-06-01 13:46:29 -0700 | [diff] [blame] | 104 | sp<const TrackType> const_track() const { return mTrack; } |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 105 | |
| 106 | void closeConnections(PatchPanel *panel) { |
| 107 | if (mHandle != AUDIO_PATCH_HANDLE_NONE) { |
| 108 | panel->releaseAudioPatch(mHandle); |
| 109 | mHandle = AUDIO_PATCH_HANDLE_NONE; |
| 110 | } |
| 111 | if (mThread != 0) { |
| 112 | if (mTrack != 0) { |
| 113 | mThread->deletePatchTrack(mTrack); |
| 114 | } |
| 115 | if (mCloseThread) { |
| 116 | panel->mAudioFlinger.closeThreadInternal_l(mThread); |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | audio_patch_handle_t* handlePtr() { return &mHandle; } |
| 121 | void setThread(const sp<ThreadType>& thread, bool closeThread = true) { |
| 122 | mThread = thread; |
| 123 | mCloseThread = closeThread; |
| 124 | } |
Andy Hung | abfab20 | 2019-03-07 19:45:54 -0800 | [diff] [blame] | 125 | template <typename T> |
Mikhail Naganov | dd91ce2 | 2020-01-13 11:34:30 -0800 | [diff] [blame^] | 126 | void setTrackAndPeer(const sp<TrackType>& track, const sp<T> &peer, bool holdReference) { |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 127 | mTrack = track; |
| 128 | mThread->addPatchTrack(mTrack); |
Mikhail Naganov | dd91ce2 | 2020-01-13 11:34:30 -0800 | [diff] [blame^] | 129 | mTrack->setPeerProxy(peer, holdReference); |
| 130 | mClearPeerProxy = holdReference; |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 131 | } |
Mikhail Naganov | dd91ce2 | 2020-01-13 11:34:30 -0800 | [diff] [blame^] | 132 | void clearTrackPeer() { if (mClearPeerProxy && mTrack) mTrack->clearPeerProxy(); } |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 133 | void stopTrack() { if (mTrack) mTrack->stop(); } |
| 134 | |
Mikhail Naganov | fa97d9b | 2019-02-05 14:08:21 -0800 | [diff] [blame] | 135 | void swap(Endpoint &other) noexcept { |
| 136 | using std::swap; |
| 137 | swap(mThread, other.mThread); |
| 138 | swap(mCloseThread, other.mCloseThread); |
Mikhail Naganov | dd91ce2 | 2020-01-13 11:34:30 -0800 | [diff] [blame^] | 139 | swap(mClearPeerProxy, other.mClearPeerProxy); |
Mikhail Naganov | fa97d9b | 2019-02-05 14:08:21 -0800 | [diff] [blame] | 140 | swap(mHandle, other.mHandle); |
| 141 | swap(mTrack, other.mTrack); |
| 142 | } |
Mikhail Naganov | 9dfa264 | 2018-05-10 10:09:19 -0700 | [diff] [blame] | 143 | |
Mikhail Naganov | fa97d9b | 2019-02-05 14:08:21 -0800 | [diff] [blame] | 144 | friend void swap(Endpoint &a, Endpoint &b) noexcept { |
| 145 | a.swap(b); |
| 146 | } |
| 147 | |
| 148 | private: |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 149 | sp<ThreadType> mThread; |
| 150 | bool mCloseThread = true; |
Mikhail Naganov | dd91ce2 | 2020-01-13 11:34:30 -0800 | [diff] [blame^] | 151 | bool mClearPeerProxy = true; |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 152 | audio_patch_handle_t mHandle = AUDIO_PATCH_HANDLE_NONE; |
| 153 | sp<TrackType> mTrack; |
| 154 | }; |
| 155 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 156 | class Patch { |
| 157 | public: |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 158 | explicit Patch(const struct audio_patch &patch) : mAudioPatch(patch) {} |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 159 | ~Patch(); |
Mikhail Naganov | 9dfa264 | 2018-05-10 10:09:19 -0700 | [diff] [blame] | 160 | Patch(const Patch&) = delete; |
| 161 | Patch(Patch&&) = default; |
| 162 | Patch& operator=(const Patch&) = delete; |
| 163 | Patch& operator=(Patch&&) = default; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 164 | |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 165 | status_t createConnections(PatchPanel *panel); |
| 166 | void clearConnections(PatchPanel *panel); |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 167 | bool isSoftware() const { |
| 168 | return mRecord.handle() != AUDIO_PATCH_HANDLE_NONE || |
| 169 | mPlayback.handle() != AUDIO_PATCH_HANDLE_NONE; } |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 170 | |
Andy Hung | c3ab773 | 2018-06-01 13:46:29 -0700 | [diff] [blame] | 171 | // returns the latency of the patch (from record to playback). |
| 172 | status_t getLatencyMs(double *latencyMs) const; |
| 173 | |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame] | 174 | String8 dump(audio_patch_handle_t myHandle) const; |
Mikhail Naganov | 201369b | 2018-05-16 16:52:32 -0700 | [diff] [blame] | 175 | |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 176 | // Note that audio_patch::id is only unique within a HAL module |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 177 | struct audio_patch mAudioPatch; |
Eric Laurent | b997d3a | 2016-06-07 18:23:45 -0700 | [diff] [blame] | 178 | // handle for audio HAL patch handle present only when the audio HAL version is >= 3.0 |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 179 | audio_patch_handle_t mHalHandle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | b997d3a | 2016-06-07 18:23:45 -0700 | [diff] [blame] | 180 | // below members are used by a software audio patch connecting a source device from a |
| 181 | // given audio HW module to a sink device on an other audio HW module. |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 182 | // the objects are created by createConnections() and released by clearConnections() |
| 183 | // playback thread is created if no existing playback thread can be used |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 184 | // connects playback thread output to sink device |
| 185 | Endpoint<PlaybackThread, PlaybackThread::PatchTrack> mPlayback; |
| 186 | // connects source device to record thread input |
| 187 | Endpoint<RecordThread, RecordThread::PatchRecord> mRecord; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 188 | }; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 189 | |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame] | 190 | AudioHwDevice* findAudioHwDeviceByModule(audio_module_handle_t module); |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 191 | sp<DeviceHalInterface> findHwDeviceByModule(audio_module_handle_t module); |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame] | 192 | void addSoftwarePatchToInsertedModules( |
| 193 | audio_module_handle_t module, audio_patch_handle_t handle); |
| 194 | void removeSoftwarePatchFromInsertedModules(audio_patch_handle_t handle); |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 195 | |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 196 | AudioFlinger &mAudioFlinger; |
| 197 | std::map<audio_patch_handle_t, Patch> mPatches; |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame] | 198 | |
| 199 | // This map allows going from a thread to "downstream" software patches |
| 200 | // when a processing module inserted in between. Example: |
| 201 | // |
| 202 | // from map value.streams map key |
| 203 | // [Mixer thread] --> [Virtual output device] --> [Processing module] ---\ |
| 204 | // [Harware module] <-- [Physical output device] <-- [S/W Patch] <--/ |
| 205 | // from map value.sw_patches |
| 206 | // |
| 207 | // This allows the mixer thread to look up the threads of the software patch |
| 208 | // for propagating timing info, parameters, etc. |
| 209 | // |
| 210 | // The current assumptions are: |
| 211 | // 1) The processing module acts as a mixer with several outputs which |
| 212 | // represent differently downmixed and / or encoded versions of the same |
| 213 | // mixed stream. There is no 1:1 correspondence between the input streams |
| 214 | // and the software patches, but rather a N:N correspondence between |
| 215 | // a group of streams and a group of patches. |
| 216 | // 2) There are only a couple of inserted processing modules in the system, |
| 217 | // so when looking for a stream or patch handle we can iterate over |
| 218 | // all modules. |
| 219 | struct ModuleConnections { |
| 220 | std::set<audio_io_handle_t> streams; |
| 221 | std::set<audio_patch_handle_t> sw_patches; |
| 222 | }; |
| 223 | std::map<audio_module_handle_t, ModuleConnections> mInsertedModules; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 224 | }; |