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 | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 22 | // PatchPanel is concealed within AudioFlinger, their lifetimes are the same. |
| 23 | class PatchPanel { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 24 | public: |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 25 | explicit PatchPanel(AudioFlinger* audioFlinger) : mAudioFlinger(*audioFlinger) {} |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 26 | |
| 27 | /* List connected audio ports and their attributes */ |
| 28 | status_t listAudioPorts(unsigned int *num_ports, |
| 29 | struct audio_port *ports); |
| 30 | |
| 31 | /* Get supported attributes for a given audio port */ |
| 32 | status_t getAudioPort(struct audio_port *port); |
| 33 | |
| 34 | /* Create a patch between several source and sink ports */ |
| 35 | status_t createAudioPatch(const struct audio_patch *patch, |
| 36 | audio_patch_handle_t *handle); |
| 37 | |
| 38 | /* Release a patch */ |
| 39 | status_t releaseAudioPatch(audio_patch_handle_t handle); |
| 40 | |
| 41 | /* List connected audio devices and they attributes */ |
| 42 | status_t listAudioPatches(unsigned int *num_patches, |
| 43 | struct audio_patch *patches); |
| 44 | |
Mikhail Naganov | 201369b | 2018-05-16 16:52:32 -0700 | [diff] [blame^] | 45 | void dump(int fd); |
| 46 | |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 47 | private: |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 48 | template<typename ThreadType, typename TrackType> |
| 49 | class Endpoint { |
| 50 | public: |
Mikhail Naganov | 9dfa264 | 2018-05-10 10:09:19 -0700 | [diff] [blame] | 51 | Endpoint() = default; |
| 52 | Endpoint(Endpoint&& other) { *this = std::move(other); } |
| 53 | Endpoint& operator=(Endpoint&& other) { |
| 54 | ALOGE_IF(mHandle != AUDIO_PATCH_HANDLE_NONE, |
| 55 | "A non empty Patch Endpoint leaked, handle %d", mHandle); |
| 56 | *this = other; |
| 57 | other.mHandle = AUDIO_PATCH_HANDLE_NONE; |
| 58 | return *this; |
| 59 | } |
| 60 | |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 61 | status_t checkTrack(TrackType *trackOrNull) const { |
| 62 | if (trackOrNull == nullptr) return NO_MEMORY; |
| 63 | return trackOrNull->initCheck(); |
| 64 | } |
| 65 | audio_patch_handle_t handle() const { return mHandle; } |
| 66 | sp<ThreadType> thread() { return mThread; } |
| 67 | sp<TrackType> track() { return mTrack; } |
| 68 | |
| 69 | void closeConnections(PatchPanel *panel) { |
| 70 | if (mHandle != AUDIO_PATCH_HANDLE_NONE) { |
| 71 | panel->releaseAudioPatch(mHandle); |
| 72 | mHandle = AUDIO_PATCH_HANDLE_NONE; |
| 73 | } |
| 74 | if (mThread != 0) { |
| 75 | if (mTrack != 0) { |
| 76 | mThread->deletePatchTrack(mTrack); |
| 77 | } |
| 78 | if (mCloseThread) { |
| 79 | panel->mAudioFlinger.closeThreadInternal_l(mThread); |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | audio_patch_handle_t* handlePtr() { return &mHandle; } |
| 84 | void setThread(const sp<ThreadType>& thread, bool closeThread = true) { |
| 85 | mThread = thread; |
| 86 | mCloseThread = closeThread; |
| 87 | } |
| 88 | void setTrackAndPeer(const sp<TrackType>& track, |
| 89 | ThreadBase::PatchProxyBufferProvider *peer) { |
| 90 | mTrack = track; |
| 91 | mThread->addPatchTrack(mTrack); |
| 92 | mTrack->setPeerProxy(peer); |
| 93 | } |
| 94 | void stopTrack() { if (mTrack) mTrack->stop(); } |
| 95 | |
| 96 | private: |
Mikhail Naganov | 9dfa264 | 2018-05-10 10:09:19 -0700 | [diff] [blame] | 97 | Endpoint(const Endpoint&) = default; |
| 98 | Endpoint& operator=(const Endpoint&) = default; |
| 99 | |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 100 | sp<ThreadType> mThread; |
| 101 | bool mCloseThread = true; |
| 102 | audio_patch_handle_t mHandle = AUDIO_PATCH_HANDLE_NONE; |
| 103 | sp<TrackType> mTrack; |
| 104 | }; |
| 105 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 106 | class Patch { |
| 107 | public: |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 108 | explicit Patch(const struct audio_patch &patch) : mAudioPatch(patch) {} |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 109 | ~Patch(); |
Mikhail Naganov | 9dfa264 | 2018-05-10 10:09:19 -0700 | [diff] [blame] | 110 | Patch(const Patch&) = delete; |
| 111 | Patch(Patch&&) = default; |
| 112 | Patch& operator=(const Patch&) = delete; |
| 113 | Patch& operator=(Patch&&) = default; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 114 | |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 115 | status_t createConnections(PatchPanel *panel); |
| 116 | void clearConnections(PatchPanel *panel); |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 117 | bool isSoftware() const { |
| 118 | return mRecord.handle() != AUDIO_PATCH_HANDLE_NONE || |
| 119 | mPlayback.handle() != AUDIO_PATCH_HANDLE_NONE; } |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 120 | |
Mikhail Naganov | 201369b | 2018-05-16 16:52:32 -0700 | [diff] [blame^] | 121 | String8 dump(audio_patch_handle_t myHandle); |
| 122 | |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 123 | // Note that audio_patch::id is only unique within a HAL module |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 124 | struct audio_patch mAudioPatch; |
Eric Laurent | b997d3a | 2016-06-07 18:23:45 -0700 | [diff] [blame] | 125 | // 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] | 126 | audio_patch_handle_t mHalHandle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | b997d3a | 2016-06-07 18:23:45 -0700 | [diff] [blame] | 127 | // below members are used by a software audio patch connecting a source device from a |
| 128 | // 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] | 129 | // the objects are created by createConnections() and released by clearConnections() |
| 130 | // playback thread is created if no existing playback thread can be used |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 131 | // connects playback thread output to sink device |
| 132 | Endpoint<PlaybackThread, PlaybackThread::PatchTrack> mPlayback; |
| 133 | // connects source device to record thread input |
| 134 | Endpoint<RecordThread, RecordThread::PatchRecord> mRecord; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 135 | }; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 136 | |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 137 | sp<DeviceHalInterface> findHwDeviceByModule(audio_module_handle_t module); |
| 138 | |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 139 | AudioFlinger &mAudioFlinger; |
| 140 | std::map<audio_patch_handle_t, Patch> mPatches; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 141 | }; |