blob: f4e43e2a51a5c644e77be991890b175f5c047e3d [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
18#ifndef INCLUDING_FROM_AUDIOFLINGER_H
19 #error This header file should only be included from AudioFlinger.h
20#endif
21
Mikhail Naganovdea53042018-04-26 13:10:21 -070022// PatchPanel is concealed within AudioFlinger, their lifetimes are the same.
23class PatchPanel {
Eric Laurent1c333e22014-05-20 10:48:17 -070024public:
Mikhail Naganovdea53042018-04-26 13:10:21 -070025 explicit PatchPanel(AudioFlinger* audioFlinger) : mAudioFlinger(*audioFlinger) {}
Eric Laurent1c333e22014-05-20 10:48:17 -070026
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 Naganovdea53042018-04-26 13:10:21 -070045private:
Mikhail Naganov444ecc32018-05-01 17:40:05 -070046 template<typename ThreadType, typename TrackType>
47 class Endpoint {
48 public:
Mikhail Naganov9dfa2642018-05-10 10:09:19 -070049 Endpoint() = default;
50 Endpoint(Endpoint&& other) { *this = std::move(other); }
51 Endpoint& operator=(Endpoint&& other) {
52 ALOGE_IF(mHandle != AUDIO_PATCH_HANDLE_NONE,
53 "A non empty Patch Endpoint leaked, handle %d", mHandle);
54 *this = other;
55 other.mHandle = AUDIO_PATCH_HANDLE_NONE;
56 return *this;
57 }
58
Mikhail Naganov444ecc32018-05-01 17:40:05 -070059 status_t checkTrack(TrackType *trackOrNull) const {
60 if (trackOrNull == nullptr) return NO_MEMORY;
61 return trackOrNull->initCheck();
62 }
63 audio_patch_handle_t handle() const { return mHandle; }
64 sp<ThreadType> thread() { return mThread; }
65 sp<TrackType> track() { return mTrack; }
66
67 void closeConnections(PatchPanel *panel) {
68 if (mHandle != AUDIO_PATCH_HANDLE_NONE) {
69 panel->releaseAudioPatch(mHandle);
70 mHandle = AUDIO_PATCH_HANDLE_NONE;
71 }
72 if (mThread != 0) {
73 if (mTrack != 0) {
74 mThread->deletePatchTrack(mTrack);
75 }
76 if (mCloseThread) {
77 panel->mAudioFlinger.closeThreadInternal_l(mThread);
78 }
79 }
80 }
81 audio_patch_handle_t* handlePtr() { return &mHandle; }
82 void setThread(const sp<ThreadType>& thread, bool closeThread = true) {
83 mThread = thread;
84 mCloseThread = closeThread;
85 }
86 void setTrackAndPeer(const sp<TrackType>& track,
87 ThreadBase::PatchProxyBufferProvider *peer) {
88 mTrack = track;
89 mThread->addPatchTrack(mTrack);
90 mTrack->setPeerProxy(peer);
91 }
92 void stopTrack() { if (mTrack) mTrack->stop(); }
93
94 private:
Mikhail Naganov9dfa2642018-05-10 10:09:19 -070095 Endpoint(const Endpoint&) = default;
96 Endpoint& operator=(const Endpoint&) = default;
97
Mikhail Naganov444ecc32018-05-01 17:40:05 -070098 sp<ThreadType> mThread;
99 bool mCloseThread = true;
100 audio_patch_handle_t mHandle = AUDIO_PATCH_HANDLE_NONE;
101 sp<TrackType> mTrack;
102 };
103
Eric Laurent1c333e22014-05-20 10:48:17 -0700104 class Patch {
105 public:
Mikhail Naganovdea53042018-04-26 13:10:21 -0700106 explicit Patch(const struct audio_patch &patch) : mAudioPatch(patch) {}
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700107 ~Patch();
Mikhail Naganov9dfa2642018-05-10 10:09:19 -0700108 Patch(const Patch&) = delete;
109 Patch(Patch&&) = default;
110 Patch& operator=(const Patch&) = delete;
111 Patch& operator=(Patch&&) = default;
Eric Laurent1c333e22014-05-20 10:48:17 -0700112
Mikhail Naganovdea53042018-04-26 13:10:21 -0700113 status_t createConnections(PatchPanel *panel);
114 void clearConnections(PatchPanel *panel);
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700115 bool isSoftware() const {
116 return mRecord.handle() != AUDIO_PATCH_HANDLE_NONE ||
117 mPlayback.handle() != AUDIO_PATCH_HANDLE_NONE; }
Mikhail Naganovdea53042018-04-26 13:10:21 -0700118
119 // Note that audio_patch::id is only unique within a HAL module
Eric Laurent83b88082014-06-20 18:31:16 -0700120 struct audio_patch mAudioPatch;
Eric Laurentb997d3a2016-06-07 18:23:45 -0700121 // handle for audio HAL patch handle present only when the audio HAL version is >= 3.0
Mikhail Naganovdea53042018-04-26 13:10:21 -0700122 audio_patch_handle_t mHalHandle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurentb997d3a2016-06-07 18:23:45 -0700123 // below members are used by a software audio patch connecting a source device from a
124 // given audio HW module to a sink device on an other audio HW module.
Mikhail Naganovdea53042018-04-26 13:10:21 -0700125 // the objects are created by createConnections() and released by clearConnections()
126 // playback thread is created if no existing playback thread can be used
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700127 // connects playback thread output to sink device
128 Endpoint<PlaybackThread, PlaybackThread::PatchTrack> mPlayback;
129 // connects source device to record thread input
130 Endpoint<RecordThread, RecordThread::PatchRecord> mRecord;
Eric Laurent1c333e22014-05-20 10:48:17 -0700131 };
Eric Laurent83b88082014-06-20 18:31:16 -0700132
Mikhail Naganov444ecc32018-05-01 17:40:05 -0700133 sp<DeviceHalInterface> findHwDeviceByModule(audio_module_handle_t module);
134
Mikhail Naganovdea53042018-04-26 13:10:21 -0700135 AudioFlinger &mAudioFlinger;
136 std::map<audio_patch_handle_t, Patch> mPatches;
Eric Laurent1c333e22014-05-20 10:48:17 -0700137};