Merge lp:~phablet-team/telephony-service/multiple_performance_improvements into lp:telephony-service/staging
- multiple_performance_improvements
- Merge into staging
Proposed by
Tiago Salem Herrmann
Status: | Needs review |
---|---|
Proposed branch: | lp:~phablet-team/telephony-service/multiple_performance_improvements |
Merge into: | lp:telephony-service/staging |
Prerequisite: | lp:~phablet-team/telephony-service/audio_route_manager |
Diff against target: |
3337 lines (+41/-1537) 13 files modified
handler/audioroutemanager.cpp (+0/-231) handler/audioroutemanager.h (+0/-75) handler/powerd.h (+0/-34) handler/powerdaudiomodemediator.cpp (+0/-63) handler/powerdaudiomodemediator.h (+0/-46) handler/powerddbus.cpp (+0/-43) handler/powerddbus.h (+0/-38) handler/qpulseaudioengine.cpp (+0/-853) handler/qpulseaudioengine.h (+0/-126) libtelephonyservice/chatentry.cpp (+26/-16) libtelephonyservice/chatentry.h (+1/-0) libtelephonyservice/contactwatcher.cpp (+1/-7) tests/Ubuntu.Telephony/ContactWatcherTest.cpp (+13/-5) |
To merge this branch: | bzr merge lp:~phablet-team/telephony-service/multiple_performance_improvements |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Ubuntu Phablet Team | Pending | ||
Review via email: mp+316375@code.launchpad.net |
Commit message
Multiple performance improvements on Roles Interfaces and ContactWatcher
Description of the change
Multiple performance improvements on Roles Interfaces and ContactWatcher
To post a comment you must log in.
- 1242. By Tiago Salem Herrmann
-
merge parent branch
Unmerged revisions
- 1242. By Tiago Salem Herrmann
-
merge parent branch
- 1241. By Tiago Salem Herrmann
-
merge parent
- 1240. By Tiago Salem Herrmann
-
Multiple performance fixes for contact watcher and roles interface
- 1239. By Tiago Salem Herrmann
-
merge parent
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === added file 'handler/audioroutemanager.cpp' | |||
2 | --- handler/audioroutemanager.cpp 1970-01-01 00:00:00 +0000 | |||
3 | +++ handler/audioroutemanager.cpp 2017-02-06 13:26:33 +0000 | |||
4 | @@ -0,0 +1,231 @@ | |||
5 | 1 | /* | ||
6 | 2 | * Copyright (C) 2016 Canonical, Ltd. | ||
7 | 3 | * | ||
8 | 4 | * Authors: | ||
9 | 5 | * Tiago Salem Herrmann <tiago.herrmann@canonical.com> | ||
10 | 6 | * | ||
11 | 7 | * This file is part of telephony-service. | ||
12 | 8 | * | ||
13 | 9 | * telephony-service is free software; you can redistribute it and/or modify | ||
14 | 10 | * it under the terms of the GNU General Public License as published by | ||
15 | 11 | * the Free Software Foundation; version 3. | ||
16 | 12 | * | ||
17 | 13 | * telephony-service is distributed in the hope that it will be useful, | ||
18 | 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
19 | 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
20 | 16 | * GNU General Public License for more details. | ||
21 | 17 | * | ||
22 | 18 | * You should have received a copy of the GNU General Public License | ||
23 | 19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
24 | 20 | */ | ||
25 | 21 | |||
26 | 22 | #include "audioroutemanager.h" | ||
27 | 23 | #include "telepathyhelper.h" | ||
28 | 24 | #include "accountentry.h" | ||
29 | 25 | #include <TelepathyQt/Contact> | ||
30 | 26 | #include <TelepathyQt/Functors> | ||
31 | 27 | |||
32 | 28 | |||
33 | 29 | static void enable_earpiece() | ||
34 | 30 | { | ||
35 | 31 | #ifdef USE_PULSEAUDIO | ||
36 | 32 | QPulseAudioEngine::instance()->setCallMode(CallActive, AudioModeBtOrWiredOrEarpiece); | ||
37 | 33 | #endif | ||
38 | 34 | } | ||
39 | 35 | |||
40 | 36 | static void enable_normal() | ||
41 | 37 | { | ||
42 | 38 | #ifdef USE_PULSEAUDIO | ||
43 | 39 | QTimer* timer = new QTimer(); | ||
44 | 40 | timer->setSingleShot(true); | ||
45 | 41 | QObject::connect(timer, &QTimer::timeout, [=](){ | ||
46 | 42 | QPulseAudioEngine::instance()->setMicMute(false); | ||
47 | 43 | QPulseAudioEngine::instance()->setCallMode(CallEnded, AudioModeWiredOrSpeaker); | ||
48 | 44 | timer->deleteLater(); | ||
49 | 45 | }); | ||
50 | 46 | timer->start(2000); | ||
51 | 47 | #endif | ||
52 | 48 | } | ||
53 | 49 | |||
54 | 50 | static void enable_speaker() | ||
55 | 51 | { | ||
56 | 52 | #ifdef USE_PULSEAUDIO | ||
57 | 53 | QPulseAudioEngine::instance()->setCallMode(CallActive, AudioModeSpeaker); | ||
58 | 54 | #endif | ||
59 | 55 | } | ||
60 | 56 | |||
61 | 57 | static void enable_ringtone() | ||
62 | 58 | { | ||
63 | 59 | #ifdef USE_PULSEAUDIO | ||
64 | 60 | QPulseAudioEngine::instance()->setCallMode(CallRinging, AudioModeBtOrWiredOrSpeaker); | ||
65 | 61 | #endif | ||
66 | 62 | } | ||
67 | 63 | |||
68 | 64 | AudioRouteManager *AudioRouteManager::instance() | ||
69 | 65 | { | ||
70 | 66 | static AudioRouteManager *self = new AudioRouteManager(); | ||
71 | 67 | return self; | ||
72 | 68 | } | ||
73 | 69 | |||
74 | 70 | AudioRouteManager::AudioRouteManager(QObject *parent) : | ||
75 | 71 | QObject(parent), mAudioModeMediator(mPowerDDBus) | ||
76 | 72 | { | ||
77 | 73 | TelepathyHelper::instance()->registerChannelObserver("TelephonyServiceHandlerAudioRouteManager"); | ||
78 | 74 | |||
79 | 75 | QObject::connect(TelepathyHelper::instance()->channelObserver(), SIGNAL(callChannelAvailable(Tp::CallChannelPtr)), | ||
80 | 76 | this, SLOT(onCallChannelAvailable(Tp::CallChannelPtr))); | ||
81 | 77 | |||
82 | 78 | #ifdef USE_PULSEAUDIO | ||
83 | 79 | // update audio modes | ||
84 | 80 | QObject::connect(QPulseAudioEngine::instance(), SIGNAL(audioModeChanged(AudioMode)), SLOT(onAudioModeChanged(AudioMode))); | ||
85 | 81 | QObject::connect(QPulseAudioEngine::instance(), SIGNAL(availableAudioModesChanged(AudioModes)), SLOT(onAvailableAudioModesChanged(AudioModes))); | ||
86 | 82 | |||
87 | 83 | // check if we should indeed use pulseaudio | ||
88 | 84 | QByteArray pulseAudioDisabled = qgetenv("PA_DISABLED"); | ||
89 | 85 | mHasPulseAudio = true; | ||
90 | 86 | if (!pulseAudioDisabled.isEmpty()) | ||
91 | 87 | mHasPulseAudio = false; | ||
92 | 88 | #endif | ||
93 | 89 | |||
94 | 90 | connect(this, &AudioRouteManager::activeAudioOutputChanged, Tp::memFun(&mAudioModeMediator, &PowerDAudioModeMediator::audioModeChanged)); | ||
95 | 91 | connect(this, &AudioRouteManager::lastChannelClosed, Tp::memFun(&mAudioModeMediator, &PowerDAudioModeMediator::audioOutputClosed)); | ||
96 | 92 | } | ||
97 | 93 | |||
98 | 94 | void AudioRouteManager::onCallChannelAvailable(Tp::CallChannelPtr callChannel) | ||
99 | 95 | { | ||
100 | 96 | connect(callChannel.data(), | ||
101 | 97 | SIGNAL(callStateChanged(Tp::CallState)), | ||
102 | 98 | SLOT(onCallStateChanged(Tp::CallState))); | ||
103 | 99 | |||
104 | 100 | mChannels.append(callChannel); | ||
105 | 101 | updateAudioRoute(true); | ||
106 | 102 | } | ||
107 | 103 | |||
108 | 104 | void AudioRouteManager::onCallStateChanged(Tp::CallState state) | ||
109 | 105 | { | ||
110 | 106 | Tp::CallChannelPtr channel(qobject_cast<Tp::CallChannel*>(sender())); | ||
111 | 107 | if (!channel) { | ||
112 | 108 | return; | ||
113 | 109 | } | ||
114 | 110 | |||
115 | 111 | if (channel->callState() == Tp::CallStateEnded) { | ||
116 | 112 | mChannels.removeOne(channel); | ||
117 | 113 | } | ||
118 | 114 | updateAudioRoute(false); | ||
119 | 115 | } | ||
120 | 116 | |||
121 | 117 | void AudioRouteManager::setActiveAudioOutput(const QString &id) | ||
122 | 118 | { | ||
123 | 119 | #ifdef USE_PULSEAUDIO | ||
124 | 120 | // fallback to earpiece/headset | ||
125 | 121 | AudioMode mode = AudioModeWiredOrEarpiece; | ||
126 | 122 | if (id == "bluetooth") { | ||
127 | 123 | mode = AudioModeBluetooth; | ||
128 | 124 | } else if (id == "speaker") { | ||
129 | 125 | mode = AudioModeSpeaker; | ||
130 | 126 | } | ||
131 | 127 | if (mHasPulseAudio) | ||
132 | 128 | QPulseAudioEngine::instance()->setCallMode(CallActive, mode); | ||
133 | 129 | #endif | ||
134 | 130 | } | ||
135 | 131 | |||
136 | 132 | QString AudioRouteManager::activeAudioOutput() | ||
137 | 133 | { | ||
138 | 134 | return mActiveAudioOutput; | ||
139 | 135 | } | ||
140 | 136 | |||
141 | 137 | AudioOutputDBusList AudioRouteManager::audioOutputs() const | ||
142 | 138 | { | ||
143 | 139 | return mAudioOutputs; | ||
144 | 140 | } | ||
145 | 141 | |||
146 | 142 | void AudioRouteManager::updateAudioRoute(bool newCall) | ||
147 | 143 | { | ||
148 | 144 | #ifdef USE_PULSEAUDIO | ||
149 | 145 | if (!mHasPulseAudio) | ||
150 | 146 | return; | ||
151 | 147 | #endif | ||
152 | 148 | |||
153 | 149 | int currentCalls = mChannels.size(); | ||
154 | 150 | if (currentCalls != 0) { | ||
155 | 151 | if (currentCalls == 1) { | ||
156 | 152 | // if we have only one call, check if it's incoming and | ||
157 | 153 | // enable speaker mode so the ringtone is audible | ||
158 | 154 | Tp::CallChannelPtr callChannel = mChannels.first(); | ||
159 | 155 | AccountEntry *accountEntry = TelepathyHelper::instance()->accountForConnection(callChannel->connection()); | ||
160 | 156 | if (!accountEntry || !callChannel) { | ||
161 | 157 | return; | ||
162 | 158 | } | ||
163 | 159 | |||
164 | 160 | bool incoming = callChannel->initiatorContact() != accountEntry->account()->connection()->selfContact(); | ||
165 | 161 | Tp::CallState state = callChannel->callState(); | ||
166 | 162 | if (incoming && newCall) { | ||
167 | 163 | enable_ringtone(); | ||
168 | 164 | return; | ||
169 | 165 | } | ||
170 | 166 | if (state == Tp::CallStateEnded) { | ||
171 | 167 | enable_normal(); | ||
172 | 168 | return; | ||
173 | 169 | } | ||
174 | 170 | // if only one call and dialing, or incoming call just accepted, then default to earpiece | ||
175 | 171 | if (newCall || (state == Tp::CallStateAccepted && incoming)) { | ||
176 | 172 | enable_earpiece(); | ||
177 | 173 | return; | ||
178 | 174 | } | ||
179 | 175 | } | ||
180 | 176 | } else { | ||
181 | 177 | enable_normal(); | ||
182 | 178 | Q_EMIT lastChannelClosed(); | ||
183 | 179 | } | ||
184 | 180 | } | ||
185 | 181 | |||
186 | 182 | #ifdef USE_PULSEAUDIO | ||
187 | 183 | void AudioRouteManager::onAudioModeChanged(AudioMode mode) | ||
188 | 184 | { | ||
189 | 185 | qDebug("PulseAudio audio mode changed: 0x%x", mode); | ||
190 | 186 | |||
191 | 187 | if (mode == AudioModeEarpiece && mActiveAudioOutput != "earpiece") { | ||
192 | 188 | mActiveAudioOutput = "earpiece"; | ||
193 | 189 | } else if (mode == AudioModeWiredHeadset && mActiveAudioOutput != "wired_headset") { | ||
194 | 190 | mActiveAudioOutput = "wired_headset"; | ||
195 | 191 | } else if (mode == AudioModeSpeaker && mActiveAudioOutput != "speaker") { | ||
196 | 192 | mActiveAudioOutput = "speaker"; | ||
197 | 193 | } else if (mode == AudioModeBluetooth && mActiveAudioOutput != "bluetooth") { | ||
198 | 194 | mActiveAudioOutput = "bluetooth"; | ||
199 | 195 | } | ||
200 | 196 | Q_EMIT activeAudioOutputChanged(mActiveAudioOutput); | ||
201 | 197 | } | ||
202 | 198 | |||
203 | 199 | void AudioRouteManager::onAvailableAudioModesChanged(AudioModes modes) | ||
204 | 200 | { | ||
205 | 201 | qDebug("PulseAudio available audio modes changed"); | ||
206 | 202 | bool defaultFound = false; | ||
207 | 203 | mAudioOutputs.clear(); | ||
208 | 204 | Q_FOREACH(const AudioMode &mode, modes) { | ||
209 | 205 | AudioOutputDBus output; | ||
210 | 206 | if (mode == AudioModeBluetooth) { | ||
211 | 207 | // there can be only one bluetooth | ||
212 | 208 | output.id = "bluetooth"; | ||
213 | 209 | output.type = "bluetooth"; | ||
214 | 210 | // we dont support names for now, so we set a default value | ||
215 | 211 | output.name = "bluetooth"; | ||
216 | 212 | } else if (mode == AudioModeEarpiece || mode == AudioModeWiredHeadset) { | ||
217 | 213 | if (!defaultFound) { | ||
218 | 214 | defaultFound = true; | ||
219 | 215 | output.id = "default"; | ||
220 | 216 | output.type = "default"; | ||
221 | 217 | output.name = "default"; | ||
222 | 218 | } else { | ||
223 | 219 | continue; | ||
224 | 220 | } | ||
225 | 221 | } else if (mode == AudioModeSpeaker) { | ||
226 | 222 | output.id = "speaker"; | ||
227 | 223 | output.type = "speaker"; | ||
228 | 224 | output.name = "speaker"; | ||
229 | 225 | } | ||
230 | 226 | mAudioOutputs << output; | ||
231 | 227 | } | ||
232 | 228 | Q_EMIT audioOutputsChanged(mAudioOutputs); | ||
233 | 229 | } | ||
234 | 230 | #endif | ||
235 | 231 | |||
236 | 0 | 232 | ||
237 | === removed file 'handler/audioroutemanager.cpp' | |||
238 | --- handler/audioroutemanager.cpp 2017-02-06 13:26:33 +0000 | |||
239 | +++ handler/audioroutemanager.cpp 1970-01-01 00:00:00 +0000 | |||
240 | @@ -1,231 +0,0 @@ | |||
241 | 1 | /* | ||
242 | 2 | * Copyright (C) 2016 Canonical, Ltd. | ||
243 | 3 | * | ||
244 | 4 | * Authors: | ||
245 | 5 | * Tiago Salem Herrmann <tiago.herrmann@canonical.com> | ||
246 | 6 | * | ||
247 | 7 | * This file is part of telephony-service. | ||
248 | 8 | * | ||
249 | 9 | * telephony-service is free software; you can redistribute it and/or modify | ||
250 | 10 | * it under the terms of the GNU General Public License as published by | ||
251 | 11 | * the Free Software Foundation; version 3. | ||
252 | 12 | * | ||
253 | 13 | * telephony-service is distributed in the hope that it will be useful, | ||
254 | 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
255 | 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
256 | 16 | * GNU General Public License for more details. | ||
257 | 17 | * | ||
258 | 18 | * You should have received a copy of the GNU General Public License | ||
259 | 19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
260 | 20 | */ | ||
261 | 21 | |||
262 | 22 | #include "audioroutemanager.h" | ||
263 | 23 | #include "telepathyhelper.h" | ||
264 | 24 | #include "accountentry.h" | ||
265 | 25 | #include <TelepathyQt/Contact> | ||
266 | 26 | #include <TelepathyQt/Functors> | ||
267 | 27 | |||
268 | 28 | |||
269 | 29 | static void enable_earpiece() | ||
270 | 30 | { | ||
271 | 31 | #ifdef USE_PULSEAUDIO | ||
272 | 32 | QPulseAudioEngine::instance()->setCallMode(CallActive, AudioModeBtOrWiredOrEarpiece); | ||
273 | 33 | #endif | ||
274 | 34 | } | ||
275 | 35 | |||
276 | 36 | static void enable_normal() | ||
277 | 37 | { | ||
278 | 38 | #ifdef USE_PULSEAUDIO | ||
279 | 39 | QTimer* timer = new QTimer(); | ||
280 | 40 | timer->setSingleShot(true); | ||
281 | 41 | QObject::connect(timer, &QTimer::timeout, [=](){ | ||
282 | 42 | QPulseAudioEngine::instance()->setMicMute(false); | ||
283 | 43 | QPulseAudioEngine::instance()->setCallMode(CallEnded, AudioModeWiredOrSpeaker); | ||
284 | 44 | timer->deleteLater(); | ||
285 | 45 | }); | ||
286 | 46 | timer->start(2000); | ||
287 | 47 | #endif | ||
288 | 48 | } | ||
289 | 49 | |||
290 | 50 | static void enable_speaker() | ||
291 | 51 | { | ||
292 | 52 | #ifdef USE_PULSEAUDIO | ||
293 | 53 | QPulseAudioEngine::instance()->setCallMode(CallActive, AudioModeSpeaker); | ||
294 | 54 | #endif | ||
295 | 55 | } | ||
296 | 56 | |||
297 | 57 | static void enable_ringtone() | ||
298 | 58 | { | ||
299 | 59 | #ifdef USE_PULSEAUDIO | ||
300 | 60 | QPulseAudioEngine::instance()->setCallMode(CallRinging, AudioModeBtOrWiredOrSpeaker); | ||
301 | 61 | #endif | ||
302 | 62 | } | ||
303 | 63 | |||
304 | 64 | AudioRouteManager *AudioRouteManager::instance() | ||
305 | 65 | { | ||
306 | 66 | static AudioRouteManager *self = new AudioRouteManager(); | ||
307 | 67 | return self; | ||
308 | 68 | } | ||
309 | 69 | |||
310 | 70 | AudioRouteManager::AudioRouteManager(QObject *parent) : | ||
311 | 71 | QObject(parent), mAudioModeMediator(mPowerDDBus) | ||
312 | 72 | { | ||
313 | 73 | TelepathyHelper::instance()->registerChannelObserver("TelephonyServiceHandlerAudioRouteManager"); | ||
314 | 74 | |||
315 | 75 | QObject::connect(TelepathyHelper::instance()->channelObserver(), SIGNAL(callChannelAvailable(Tp::CallChannelPtr)), | ||
316 | 76 | this, SLOT(onCallChannelAvailable(Tp::CallChannelPtr))); | ||
317 | 77 | |||
318 | 78 | #ifdef USE_PULSEAUDIO | ||
319 | 79 | // update audio modes | ||
320 | 80 | QObject::connect(QPulseAudioEngine::instance(), SIGNAL(audioModeChanged(AudioMode)), SLOT(onAudioModeChanged(AudioMode))); | ||
321 | 81 | QObject::connect(QPulseAudioEngine::instance(), SIGNAL(availableAudioModesChanged(AudioModes)), SLOT(onAvailableAudioModesChanged(AudioModes))); | ||
322 | 82 | |||
323 | 83 | // check if we should indeed use pulseaudio | ||
324 | 84 | QByteArray pulseAudioDisabled = qgetenv("PA_DISABLED"); | ||
325 | 85 | mHasPulseAudio = true; | ||
326 | 86 | if (!pulseAudioDisabled.isEmpty()) | ||
327 | 87 | mHasPulseAudio = false; | ||
328 | 88 | #endif | ||
329 | 89 | |||
330 | 90 | connect(this, &AudioRouteManager::activeAudioOutputChanged, Tp::memFun(&mAudioModeMediator, &PowerDAudioModeMediator::audioModeChanged)); | ||
331 | 91 | connect(this, &AudioRouteManager::lastChannelClosed, Tp::memFun(&mAudioModeMediator, &PowerDAudioModeMediator::audioOutputClosed)); | ||
332 | 92 | } | ||
333 | 93 | |||
334 | 94 | void AudioRouteManager::onCallChannelAvailable(Tp::CallChannelPtr callChannel) | ||
335 | 95 | { | ||
336 | 96 | connect(callChannel.data(), | ||
337 | 97 | SIGNAL(callStateChanged(Tp::CallState)), | ||
338 | 98 | SLOT(onCallStateChanged(Tp::CallState))); | ||
339 | 99 | |||
340 | 100 | mChannels.append(callChannel); | ||
341 | 101 | updateAudioRoute(true); | ||
342 | 102 | } | ||
343 | 103 | |||
344 | 104 | void AudioRouteManager::onCallStateChanged(Tp::CallState state) | ||
345 | 105 | { | ||
346 | 106 | Tp::CallChannelPtr channel(qobject_cast<Tp::CallChannel*>(sender())); | ||
347 | 107 | if (!channel) { | ||
348 | 108 | return; | ||
349 | 109 | } | ||
350 | 110 | |||
351 | 111 | if (channel->callState() == Tp::CallStateEnded) { | ||
352 | 112 | mChannels.removeOne(channel); | ||
353 | 113 | } | ||
354 | 114 | updateAudioRoute(false); | ||
355 | 115 | } | ||
356 | 116 | |||
357 | 117 | void AudioRouteManager::setActiveAudioOutput(const QString &id) | ||
358 | 118 | { | ||
359 | 119 | #ifdef USE_PULSEAUDIO | ||
360 | 120 | // fallback to earpiece/headset | ||
361 | 121 | AudioMode mode = AudioModeWiredOrEarpiece; | ||
362 | 122 | if (id == "bluetooth") { | ||
363 | 123 | mode = AudioModeBluetooth; | ||
364 | 124 | } else if (id == "speaker") { | ||
365 | 125 | mode = AudioModeSpeaker; | ||
366 | 126 | } | ||
367 | 127 | if (mHasPulseAudio) | ||
368 | 128 | QPulseAudioEngine::instance()->setCallMode(CallActive, mode); | ||
369 | 129 | #endif | ||
370 | 130 | } | ||
371 | 131 | |||
372 | 132 | QString AudioRouteManager::activeAudioOutput() | ||
373 | 133 | { | ||
374 | 134 | return mActiveAudioOutput; | ||
375 | 135 | } | ||
376 | 136 | |||
377 | 137 | AudioOutputDBusList AudioRouteManager::audioOutputs() const | ||
378 | 138 | { | ||
379 | 139 | return mAudioOutputs; | ||
380 | 140 | } | ||
381 | 141 | |||
382 | 142 | void AudioRouteManager::updateAudioRoute(bool newCall) | ||
383 | 143 | { | ||
384 | 144 | #ifdef USE_PULSEAUDIO | ||
385 | 145 | if (!mHasPulseAudio) | ||
386 | 146 | return; | ||
387 | 147 | #endif | ||
388 | 148 | |||
389 | 149 | int currentCalls = mChannels.size(); | ||
390 | 150 | if (currentCalls != 0) { | ||
391 | 151 | if (currentCalls == 1) { | ||
392 | 152 | // if we have only one call, check if it's incoming and | ||
393 | 153 | // enable speaker mode so the ringtone is audible | ||
394 | 154 | Tp::CallChannelPtr callChannel = mChannels.first(); | ||
395 | 155 | AccountEntry *accountEntry = TelepathyHelper::instance()->accountForConnection(callChannel->connection()); | ||
396 | 156 | if (!accountEntry || !callChannel) { | ||
397 | 157 | return; | ||
398 | 158 | } | ||
399 | 159 | |||
400 | 160 | bool incoming = callChannel->initiatorContact() != accountEntry->account()->connection()->selfContact(); | ||
401 | 161 | Tp::CallState state = callChannel->callState(); | ||
402 | 162 | if (incoming && newCall) { | ||
403 | 163 | enable_ringtone(); | ||
404 | 164 | return; | ||
405 | 165 | } | ||
406 | 166 | if (state == Tp::CallStateEnded) { | ||
407 | 167 | enable_normal(); | ||
408 | 168 | return; | ||
409 | 169 | } | ||
410 | 170 | // if only one call and dialing, or incoming call just accepted, then default to earpiece | ||
411 | 171 | if (newCall || (state == Tp::CallStateAccepted && incoming)) { | ||
412 | 172 | enable_earpiece(); | ||
413 | 173 | return; | ||
414 | 174 | } | ||
415 | 175 | } | ||
416 | 176 | } else { | ||
417 | 177 | enable_normal(); | ||
418 | 178 | Q_EMIT lastChannelClosed(); | ||
419 | 179 | } | ||
420 | 180 | } | ||
421 | 181 | |||
422 | 182 | #ifdef USE_PULSEAUDIO | ||
423 | 183 | void AudioRouteManager::onAudioModeChanged(AudioMode mode) | ||
424 | 184 | { | ||
425 | 185 | qDebug("PulseAudio audio mode changed: 0x%x", mode); | ||
426 | 186 | |||
427 | 187 | if (mode == AudioModeEarpiece && mActiveAudioOutput != "earpiece") { | ||
428 | 188 | mActiveAudioOutput = "earpiece"; | ||
429 | 189 | } else if (mode == AudioModeWiredHeadset && mActiveAudioOutput != "wired_headset") { | ||
430 | 190 | mActiveAudioOutput = "wired_headset"; | ||
431 | 191 | } else if (mode == AudioModeSpeaker && mActiveAudioOutput != "speaker") { | ||
432 | 192 | mActiveAudioOutput = "speaker"; | ||
433 | 193 | } else if (mode == AudioModeBluetooth && mActiveAudioOutput != "bluetooth") { | ||
434 | 194 | mActiveAudioOutput = "bluetooth"; | ||
435 | 195 | } | ||
436 | 196 | Q_EMIT activeAudioOutputChanged(mActiveAudioOutput); | ||
437 | 197 | } | ||
438 | 198 | |||
439 | 199 | void AudioRouteManager::onAvailableAudioModesChanged(AudioModes modes) | ||
440 | 200 | { | ||
441 | 201 | qDebug("PulseAudio available audio modes changed"); | ||
442 | 202 | bool defaultFound = false; | ||
443 | 203 | mAudioOutputs.clear(); | ||
444 | 204 | Q_FOREACH(const AudioMode &mode, modes) { | ||
445 | 205 | AudioOutputDBus output; | ||
446 | 206 | if (mode == AudioModeBluetooth) { | ||
447 | 207 | // there can be only one bluetooth | ||
448 | 208 | output.id = "bluetooth"; | ||
449 | 209 | output.type = "bluetooth"; | ||
450 | 210 | // we dont support names for now, so we set a default value | ||
451 | 211 | output.name = "bluetooth"; | ||
452 | 212 | } else if (mode == AudioModeEarpiece || mode == AudioModeWiredHeadset) { | ||
453 | 213 | if (!defaultFound) { | ||
454 | 214 | defaultFound = true; | ||
455 | 215 | output.id = "default"; | ||
456 | 216 | output.type = "default"; | ||
457 | 217 | output.name = "default"; | ||
458 | 218 | } else { | ||
459 | 219 | continue; | ||
460 | 220 | } | ||
461 | 221 | } else if (mode == AudioModeSpeaker) { | ||
462 | 222 | output.id = "speaker"; | ||
463 | 223 | output.type = "speaker"; | ||
464 | 224 | output.name = "speaker"; | ||
465 | 225 | } | ||
466 | 226 | mAudioOutputs << output; | ||
467 | 227 | } | ||
468 | 228 | Q_EMIT audioOutputsChanged(mAudioOutputs); | ||
469 | 229 | } | ||
470 | 230 | #endif | ||
471 | 231 | |||
472 | 232 | 0 | ||
473 | === added file 'handler/audioroutemanager.h' | |||
474 | --- handler/audioroutemanager.h 1970-01-01 00:00:00 +0000 | |||
475 | +++ handler/audioroutemanager.h 2017-02-06 13:26:33 +0000 | |||
476 | @@ -0,0 +1,75 @@ | |||
477 | 1 | /* | ||
478 | 2 | * Copyright (C) 2016 Canonical, Ltd. | ||
479 | 3 | * | ||
480 | 4 | * Authors: | ||
481 | 5 | * Tiago Salem Herrmann <tiago.herrmann@canonical.com> | ||
482 | 6 | * | ||
483 | 7 | * This file is part of telephony-service. | ||
484 | 8 | * | ||
485 | 9 | * telephony-service is free software; you can redistribute it and/or modify | ||
486 | 10 | * it under the terms of the GNU General Public License as published by | ||
487 | 11 | * the Free Software Foundation; version 3. | ||
488 | 12 | * | ||
489 | 13 | * telephony-service is distributed in the hope that it will be useful, | ||
490 | 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
491 | 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
492 | 16 | * GNU General Public License for more details. | ||
493 | 17 | * | ||
494 | 18 | * You should have received a copy of the GNU General Public License | ||
495 | 19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
496 | 20 | */ | ||
497 | 21 | |||
498 | 22 | #ifndef AUDIOROUTEMANAGER_H | ||
499 | 23 | #define AUDIOROUTEMANAGER_H | ||
500 | 24 | |||
501 | 25 | #ifdef USE_PULSEAUDIO | ||
502 | 26 | #include "qpulseaudioengine.h" | ||
503 | 27 | #endif | ||
504 | 28 | #include "audiooutput.h" | ||
505 | 29 | #include "powerdaudiomodemediator.h" | ||
506 | 30 | #include "powerddbus.h" | ||
507 | 31 | #include <QObject> | ||
508 | 32 | #include <TelepathyQt/CallChannel> | ||
509 | 33 | |||
510 | 34 | |||
511 | 35 | class AudioRouteManager : public QObject | ||
512 | 36 | { | ||
513 | 37 | Q_OBJECT | ||
514 | 38 | |||
515 | 39 | public: | ||
516 | 40 | static AudioRouteManager *instance(); | ||
517 | 41 | void setActiveAudioOutput(const QString &id); | ||
518 | 42 | QString activeAudioOutput(); | ||
519 | 43 | AudioOutputDBusList audioOutputs() const; | ||
520 | 44 | void updateAudioRoute(bool newCall = false); | ||
521 | 45 | |||
522 | 46 | public Q_SLOTS: | ||
523 | 47 | void onCallChannelAvailable(Tp::CallChannelPtr callChannel); | ||
524 | 48 | |||
525 | 49 | Q_SIGNALS: | ||
526 | 50 | void audioOutputsChanged(const AudioOutputDBusList &outputs); | ||
527 | 51 | void activeAudioOutputChanged(const QString &id); | ||
528 | 52 | void lastChannelClosed(); | ||
529 | 53 | |||
530 | 54 | protected Q_SLOTS: | ||
531 | 55 | void onCallStateChanged(Tp::CallState state); | ||
532 | 56 | |||
533 | 57 | private Q_SLOTS: | ||
534 | 58 | #ifdef USE_PULSEAUDIO | ||
535 | 59 | void onAudioModeChanged(AudioMode mode); | ||
536 | 60 | void onAvailableAudioModesChanged(AudioModes modes); | ||
537 | 61 | #endif | ||
538 | 62 | |||
539 | 63 | private: | ||
540 | 64 | explicit AudioRouteManager(QObject *parent = 0); | ||
541 | 65 | QList<Tp::CallChannelPtr> mChannels; | ||
542 | 66 | AudioOutputDBusList mAudioOutputs; | ||
543 | 67 | QString mActiveAudioOutput; | ||
544 | 68 | PowerDDBus mPowerDDBus; | ||
545 | 69 | PowerDAudioModeMediator mAudioModeMediator; | ||
546 | 70 | #ifdef USE_PULSEAUDIO | ||
547 | 71 | bool mHasPulseAudio; | ||
548 | 72 | #endif | ||
549 | 73 | }; | ||
550 | 74 | |||
551 | 75 | #endif // AUDIOROUTEMANAGER_H | ||
552 | 0 | 76 | ||
553 | === removed file 'handler/audioroutemanager.h' | |||
554 | --- handler/audioroutemanager.h 2017-02-06 13:26:33 +0000 | |||
555 | +++ handler/audioroutemanager.h 1970-01-01 00:00:00 +0000 | |||
556 | @@ -1,75 +0,0 @@ | |||
557 | 1 | /* | ||
558 | 2 | * Copyright (C) 2016 Canonical, Ltd. | ||
559 | 3 | * | ||
560 | 4 | * Authors: | ||
561 | 5 | * Tiago Salem Herrmann <tiago.herrmann@canonical.com> | ||
562 | 6 | * | ||
563 | 7 | * This file is part of telephony-service. | ||
564 | 8 | * | ||
565 | 9 | * telephony-service is free software; you can redistribute it and/or modify | ||
566 | 10 | * it under the terms of the GNU General Public License as published by | ||
567 | 11 | * the Free Software Foundation; version 3. | ||
568 | 12 | * | ||
569 | 13 | * telephony-service is distributed in the hope that it will be useful, | ||
570 | 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
571 | 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
572 | 16 | * GNU General Public License for more details. | ||
573 | 17 | * | ||
574 | 18 | * You should have received a copy of the GNU General Public License | ||
575 | 19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
576 | 20 | */ | ||
577 | 21 | |||
578 | 22 | #ifndef AUDIOROUTEMANAGER_H | ||
579 | 23 | #define AUDIOROUTEMANAGER_H | ||
580 | 24 | |||
581 | 25 | #ifdef USE_PULSEAUDIO | ||
582 | 26 | #include "qpulseaudioengine.h" | ||
583 | 27 | #endif | ||
584 | 28 | #include "audiooutput.h" | ||
585 | 29 | #include "powerdaudiomodemediator.h" | ||
586 | 30 | #include "powerddbus.h" | ||
587 | 31 | #include <QObject> | ||
588 | 32 | #include <TelepathyQt/CallChannel> | ||
589 | 33 | |||
590 | 34 | |||
591 | 35 | class AudioRouteManager : public QObject | ||
592 | 36 | { | ||
593 | 37 | Q_OBJECT | ||
594 | 38 | |||
595 | 39 | public: | ||
596 | 40 | static AudioRouteManager *instance(); | ||
597 | 41 | void setActiveAudioOutput(const QString &id); | ||
598 | 42 | QString activeAudioOutput(); | ||
599 | 43 | AudioOutputDBusList audioOutputs() const; | ||
600 | 44 | void updateAudioRoute(bool newCall = false); | ||
601 | 45 | |||
602 | 46 | public Q_SLOTS: | ||
603 | 47 | void onCallChannelAvailable(Tp::CallChannelPtr callChannel); | ||
604 | 48 | |||
605 | 49 | Q_SIGNALS: | ||
606 | 50 | void audioOutputsChanged(const AudioOutputDBusList &outputs); | ||
607 | 51 | void activeAudioOutputChanged(const QString &id); | ||
608 | 52 | void lastChannelClosed(); | ||
609 | 53 | |||
610 | 54 | protected Q_SLOTS: | ||
611 | 55 | void onCallStateChanged(Tp::CallState state); | ||
612 | 56 | |||
613 | 57 | private Q_SLOTS: | ||
614 | 58 | #ifdef USE_PULSEAUDIO | ||
615 | 59 | void onAudioModeChanged(AudioMode mode); | ||
616 | 60 | void onAvailableAudioModesChanged(AudioModes modes); | ||
617 | 61 | #endif | ||
618 | 62 | |||
619 | 63 | private: | ||
620 | 64 | explicit AudioRouteManager(QObject *parent = 0); | ||
621 | 65 | QList<Tp::CallChannelPtr> mChannels; | ||
622 | 66 | AudioOutputDBusList mAudioOutputs; | ||
623 | 67 | QString mActiveAudioOutput; | ||
624 | 68 | PowerDDBus mPowerDDBus; | ||
625 | 69 | PowerDAudioModeMediator mAudioModeMediator; | ||
626 | 70 | #ifdef USE_PULSEAUDIO | ||
627 | 71 | bool mHasPulseAudio; | ||
628 | 72 | #endif | ||
629 | 73 | }; | ||
630 | 74 | |||
631 | 75 | #endif // AUDIOROUTEMANAGER_H | ||
632 | 76 | 0 | ||
633 | === added file 'handler/powerd.h' | |||
634 | --- handler/powerd.h 1970-01-01 00:00:00 +0000 | |||
635 | +++ handler/powerd.h 2017-02-06 13:26:33 +0000 | |||
636 | @@ -0,0 +1,34 @@ | |||
637 | 1 | /** | ||
638 | 2 | * Copyright (C) 2014 Canonical, Ltd. | ||
639 | 3 | * | ||
640 | 4 | * This program is free software: you can redistribute it and/or modify it under | ||
641 | 5 | * the terms of the GNU Lesser General Public License version 3, as published by | ||
642 | 6 | * the Free Software Foundation. | ||
643 | 7 | * | ||
644 | 8 | * This program is distributed in the hope that it will be useful, but WITHOUT | ||
645 | 9 | * ANY WARRANTY; without even the implied warranties of MERCHANTABILITY, | ||
646 | 10 | * SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
647 | 11 | * Lesser General Public License for more details. | ||
648 | 12 | * | ||
649 | 13 | * You should have received a copy of the GNU Lesser General Public License | ||
650 | 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
651 | 15 | * | ||
652 | 16 | * Authors: Andreas Pokorny <andreas.pokorny@canonical.com> | ||
653 | 17 | */ | ||
654 | 18 | |||
655 | 19 | #ifndef POWERD_H | ||
656 | 20 | #define POWERD_H | ||
657 | 21 | |||
658 | 22 | class PowerD | ||
659 | 23 | { | ||
660 | 24 | public: | ||
661 | 25 | PowerD() = default; | ||
662 | 26 | virtual ~PowerD() = default; | ||
663 | 27 | PowerD(PowerD const&) = delete; | ||
664 | 28 | PowerD& operator=(PowerD const&) = delete; | ||
665 | 29 | |||
666 | 30 | virtual void enableProximityHandling() = 0; | ||
667 | 31 | virtual void disableProximityHandling() = 0; | ||
668 | 32 | }; | ||
669 | 33 | |||
670 | 34 | #endif // POWERD_H | ||
671 | 0 | 35 | ||
672 | === removed file 'handler/powerd.h' | |||
673 | --- handler/powerd.h 2017-02-06 13:26:33 +0000 | |||
674 | +++ handler/powerd.h 1970-01-01 00:00:00 +0000 | |||
675 | @@ -1,34 +0,0 @@ | |||
676 | 1 | /** | ||
677 | 2 | * Copyright (C) 2014 Canonical, Ltd. | ||
678 | 3 | * | ||
679 | 4 | * This program is free software: you can redistribute it and/or modify it under | ||
680 | 5 | * the terms of the GNU Lesser General Public License version 3, as published by | ||
681 | 6 | * the Free Software Foundation. | ||
682 | 7 | * | ||
683 | 8 | * This program is distributed in the hope that it will be useful, but WITHOUT | ||
684 | 9 | * ANY WARRANTY; without even the implied warranties of MERCHANTABILITY, | ||
685 | 10 | * SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
686 | 11 | * Lesser General Public License for more details. | ||
687 | 12 | * | ||
688 | 13 | * You should have received a copy of the GNU Lesser General Public License | ||
689 | 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
690 | 15 | * | ||
691 | 16 | * Authors: Andreas Pokorny <andreas.pokorny@canonical.com> | ||
692 | 17 | */ | ||
693 | 18 | |||
694 | 19 | #ifndef POWERD_H | ||
695 | 20 | #define POWERD_H | ||
696 | 21 | |||
697 | 22 | class PowerD | ||
698 | 23 | { | ||
699 | 24 | public: | ||
700 | 25 | PowerD() = default; | ||
701 | 26 | virtual ~PowerD() = default; | ||
702 | 27 | PowerD(PowerD const&) = delete; | ||
703 | 28 | PowerD& operator=(PowerD const&) = delete; | ||
704 | 29 | |||
705 | 30 | virtual void enableProximityHandling() = 0; | ||
706 | 31 | virtual void disableProximityHandling() = 0; | ||
707 | 32 | }; | ||
708 | 33 | |||
709 | 34 | #endif // POWERD_H | ||
710 | 35 | 0 | ||
711 | === added file 'handler/powerdaudiomodemediator.cpp' | |||
712 | --- handler/powerdaudiomodemediator.cpp 1970-01-01 00:00:00 +0000 | |||
713 | +++ handler/powerdaudiomodemediator.cpp 2017-02-06 13:26:33 +0000 | |||
714 | @@ -0,0 +1,63 @@ | |||
715 | 1 | /** | ||
716 | 2 | * Copyright (C) 2014 Canonical, Ltd. | ||
717 | 3 | * | ||
718 | 4 | * This program is free software: you can redistribute it and/or modify it under | ||
719 | 5 | * the terms of the GNU Lesser General Public License version 3, as published by | ||
720 | 6 | * the Free Software Foundation. | ||
721 | 7 | * | ||
722 | 8 | * This program is distributed in the hope that it will be useful, but WITHOUT | ||
723 | 9 | * ANY WARRANTY; without even the implied warranties of MERCHANTABILITY, | ||
724 | 10 | * SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
725 | 11 | * Lesser General Public License for more details. | ||
726 | 12 | * | ||
727 | 13 | * You should have received a copy of the GNU Lesser General Public License | ||
728 | 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
729 | 15 | * | ||
730 | 16 | * Authors: Andreas Pokorny <andreas.pokorny@canonical.com> | ||
731 | 17 | */ | ||
732 | 18 | |||
733 | 19 | #include <QDBusInterface> | ||
734 | 20 | #include "powerdaudiomodemediator.h" | ||
735 | 21 | |||
736 | 22 | PowerDAudioModeMediator::PowerDAudioModeMediator(PowerD &powerd) | ||
737 | 23 | : powerd(powerd) | ||
738 | 24 | { | ||
739 | 25 | } | ||
740 | 26 | |||
741 | 27 | void PowerDAudioModeMediator::audioModeChanged(const QString &mode) | ||
742 | 28 | { | ||
743 | 29 | bool enableProximity = !(mode == "speaker" || mode == "bluetooth" || mode == "wired_headset"); | ||
744 | 30 | |||
745 | 31 | if (mProximityEnabled != enableProximity) | ||
746 | 32 | { | ||
747 | 33 | mProximityEnabled = enableProximity; | ||
748 | 34 | apply(); | ||
749 | 35 | } | ||
750 | 36 | } | ||
751 | 37 | |||
752 | 38 | void PowerDAudioModeMediator::apply() const | ||
753 | 39 | { | ||
754 | 40 | if (mProximityEnabled) { | ||
755 | 41 | powerd.enableProximityHandling(); | ||
756 | 42 | } else { | ||
757 | 43 | // we need to power the screen on before disabling the proximity handling | ||
758 | 44 | QDBusInterface unityIface("com.canonical.Unity.Screen", | ||
759 | 45 | "/com/canonical/Unity/Screen", | ||
760 | 46 | "com.canonical.Unity.Screen", | ||
761 | 47 | QDBusConnection::systemBus()); | ||
762 | 48 | QList<QVariant> args; | ||
763 | 49 | args.append("on"); | ||
764 | 50 | args.append(3); | ||
765 | 51 | unityIface.callWithArgumentList(QDBus::NoBlock, "setScreenPowerMode", args); | ||
766 | 52 | powerd.disableProximityHandling(); | ||
767 | 53 | } | ||
768 | 54 | } | ||
769 | 55 | |||
770 | 56 | void PowerDAudioModeMediator::audioOutputClosed() | ||
771 | 57 | { | ||
772 | 58 | if (mProximityEnabled) | ||
773 | 59 | { | ||
774 | 60 | mProximityEnabled = false; | ||
775 | 61 | apply(); | ||
776 | 62 | } | ||
777 | 63 | } | ||
778 | 0 | 64 | ||
779 | === removed file 'handler/powerdaudiomodemediator.cpp' | |||
780 | --- handler/powerdaudiomodemediator.cpp 2017-02-06 13:26:33 +0000 | |||
781 | +++ handler/powerdaudiomodemediator.cpp 1970-01-01 00:00:00 +0000 | |||
782 | @@ -1,63 +0,0 @@ | |||
783 | 1 | /** | ||
784 | 2 | * Copyright (C) 2014 Canonical, Ltd. | ||
785 | 3 | * | ||
786 | 4 | * This program is free software: you can redistribute it and/or modify it under | ||
787 | 5 | * the terms of the GNU Lesser General Public License version 3, as published by | ||
788 | 6 | * the Free Software Foundation. | ||
789 | 7 | * | ||
790 | 8 | * This program is distributed in the hope that it will be useful, but WITHOUT | ||
791 | 9 | * ANY WARRANTY; without even the implied warranties of MERCHANTABILITY, | ||
792 | 10 | * SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
793 | 11 | * Lesser General Public License for more details. | ||
794 | 12 | * | ||
795 | 13 | * You should have received a copy of the GNU Lesser General Public License | ||
796 | 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
797 | 15 | * | ||
798 | 16 | * Authors: Andreas Pokorny <andreas.pokorny@canonical.com> | ||
799 | 17 | */ | ||
800 | 18 | |||
801 | 19 | #include <QDBusInterface> | ||
802 | 20 | #include "powerdaudiomodemediator.h" | ||
803 | 21 | |||
804 | 22 | PowerDAudioModeMediator::PowerDAudioModeMediator(PowerD &powerd) | ||
805 | 23 | : powerd(powerd) | ||
806 | 24 | { | ||
807 | 25 | } | ||
808 | 26 | |||
809 | 27 | void PowerDAudioModeMediator::audioModeChanged(const QString &mode) | ||
810 | 28 | { | ||
811 | 29 | bool enableProximity = !(mode == "speaker" || mode == "bluetooth" || mode == "wired_headset"); | ||
812 | 30 | |||
813 | 31 | if (mProximityEnabled != enableProximity) | ||
814 | 32 | { | ||
815 | 33 | mProximityEnabled = enableProximity; | ||
816 | 34 | apply(); | ||
817 | 35 | } | ||
818 | 36 | } | ||
819 | 37 | |||
820 | 38 | void PowerDAudioModeMediator::apply() const | ||
821 | 39 | { | ||
822 | 40 | if (mProximityEnabled) { | ||
823 | 41 | powerd.enableProximityHandling(); | ||
824 | 42 | } else { | ||
825 | 43 | // we need to power the screen on before disabling the proximity handling | ||
826 | 44 | QDBusInterface unityIface("com.canonical.Unity.Screen", | ||
827 | 45 | "/com/canonical/Unity/Screen", | ||
828 | 46 | "com.canonical.Unity.Screen", | ||
829 | 47 | QDBusConnection::systemBus()); | ||
830 | 48 | QList<QVariant> args; | ||
831 | 49 | args.append("on"); | ||
832 | 50 | args.append(3); | ||
833 | 51 | unityIface.callWithArgumentList(QDBus::NoBlock, "setScreenPowerMode", args); | ||
834 | 52 | powerd.disableProximityHandling(); | ||
835 | 53 | } | ||
836 | 54 | } | ||
837 | 55 | |||
838 | 56 | void PowerDAudioModeMediator::audioOutputClosed() | ||
839 | 57 | { | ||
840 | 58 | if (mProximityEnabled) | ||
841 | 59 | { | ||
842 | 60 | mProximityEnabled = false; | ||
843 | 61 | apply(); | ||
844 | 62 | } | ||
845 | 63 | } | ||
846 | 64 | 0 | ||
847 | === added file 'handler/powerdaudiomodemediator.h' | |||
848 | --- handler/powerdaudiomodemediator.h 1970-01-01 00:00:00 +0000 | |||
849 | +++ handler/powerdaudiomodemediator.h 2017-02-06 13:26:33 +0000 | |||
850 | @@ -0,0 +1,46 @@ | |||
851 | 1 | /**************************************************************************** | ||
852 | 2 | ** | ||
853 | 3 | ** Copyright (C) 2014 Canonical, Ltd. | ||
854 | 4 | ** | ||
855 | 5 | ** Authors: | ||
856 | 6 | ** Andreas Pokorny <andreas.pokorny@canonical.com> | ||
857 | 7 | ** | ||
858 | 8 | ** GNU Lesser General Public License Usage | ||
859 | 9 | ** Alternatively, this file may be used under the terms of the GNU Lesser | ||
860 | 10 | ** General Public License version 2.1 as published by the Free Software | ||
861 | 11 | ** Foundation and appearing in the file LICENSE.LGPL included in the | ||
862 | 12 | ** packaging of this file. Please review the following information to | ||
863 | 13 | ** ensure the GNU Lesser General Public License version 2.1 requirements | ||
864 | 14 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | ||
865 | 15 | ** | ||
866 | 16 | ****************************************************************************/ | ||
867 | 17 | |||
868 | 18 | #ifndef POWERDAUDIOMODEMEDIATOR_H | ||
869 | 19 | #define POWERDAUDIOMODEMEDIATOR_H | ||
870 | 20 | |||
871 | 21 | #include "powerd.h" | ||
872 | 22 | |||
873 | 23 | #include <QString> | ||
874 | 24 | #include <fstream> | ||
875 | 25 | #include <memory> | ||
876 | 26 | |||
877 | 27 | class PowerD; | ||
878 | 28 | /*! | ||
879 | 29 | * \brief PowerDAudioModeMediator is responsible for configuring proximity | ||
880 | 30 | * handling of powerd during different call states and used audio outputs. | ||
881 | 31 | * In General that mean enabling sreen blanking on proximity events, when | ||
882 | 32 | * a call is active and neither a bluetooth headset nor the speakers are used. | ||
883 | 33 | */ | ||
884 | 34 | class PowerDAudioModeMediator | ||
885 | 35 | { | ||
886 | 36 | public: | ||
887 | 37 | PowerDAudioModeMediator(PowerD &powerd); | ||
888 | 38 | void audioModeChanged(const QString &mode); | ||
889 | 39 | void audioOutputClosed(); | ||
890 | 40 | private: | ||
891 | 41 | void apply() const; | ||
892 | 42 | PowerD &powerd; | ||
893 | 43 | bool mProximityEnabled{false}; | ||
894 | 44 | }; | ||
895 | 45 | |||
896 | 46 | #endif | ||
897 | 0 | 47 | ||
898 | === removed file 'handler/powerdaudiomodemediator.h' | |||
899 | --- handler/powerdaudiomodemediator.h 2017-02-06 13:26:33 +0000 | |||
900 | +++ handler/powerdaudiomodemediator.h 1970-01-01 00:00:00 +0000 | |||
901 | @@ -1,46 +0,0 @@ | |||
902 | 1 | /**************************************************************************** | ||
903 | 2 | ** | ||
904 | 3 | ** Copyright (C) 2014 Canonical, Ltd. | ||
905 | 4 | ** | ||
906 | 5 | ** Authors: | ||
907 | 6 | ** Andreas Pokorny <andreas.pokorny@canonical.com> | ||
908 | 7 | ** | ||
909 | 8 | ** GNU Lesser General Public License Usage | ||
910 | 9 | ** Alternatively, this file may be used under the terms of the GNU Lesser | ||
911 | 10 | ** General Public License version 2.1 as published by the Free Software | ||
912 | 11 | ** Foundation and appearing in the file LICENSE.LGPL included in the | ||
913 | 12 | ** packaging of this file. Please review the following information to | ||
914 | 13 | ** ensure the GNU Lesser General Public License version 2.1 requirements | ||
915 | 14 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | ||
916 | 15 | ** | ||
917 | 16 | ****************************************************************************/ | ||
918 | 17 | |||
919 | 18 | #ifndef POWERDAUDIOMODEMEDIATOR_H | ||
920 | 19 | #define POWERDAUDIOMODEMEDIATOR_H | ||
921 | 20 | |||
922 | 21 | #include "powerd.h" | ||
923 | 22 | |||
924 | 23 | #include <QString> | ||
925 | 24 | #include <fstream> | ||
926 | 25 | #include <memory> | ||
927 | 26 | |||
928 | 27 | class PowerD; | ||
929 | 28 | /*! | ||
930 | 29 | * \brief PowerDAudioModeMediator is responsible for configuring proximity | ||
931 | 30 | * handling of powerd during different call states and used audio outputs. | ||
932 | 31 | * In General that mean enabling sreen blanking on proximity events, when | ||
933 | 32 | * a call is active and neither a bluetooth headset nor the speakers are used. | ||
934 | 33 | */ | ||
935 | 34 | class PowerDAudioModeMediator | ||
936 | 35 | { | ||
937 | 36 | public: | ||
938 | 37 | PowerDAudioModeMediator(PowerD &powerd); | ||
939 | 38 | void audioModeChanged(const QString &mode); | ||
940 | 39 | void audioOutputClosed(); | ||
941 | 40 | private: | ||
942 | 41 | void apply() const; | ||
943 | 42 | PowerD &powerd; | ||
944 | 43 | bool mProximityEnabled{false}; | ||
945 | 44 | }; | ||
946 | 45 | |||
947 | 46 | #endif | ||
948 | 47 | 0 | ||
949 | === added file 'handler/powerddbus.cpp' | |||
950 | --- handler/powerddbus.cpp 1970-01-01 00:00:00 +0000 | |||
951 | +++ handler/powerddbus.cpp 2017-02-06 13:26:33 +0000 | |||
952 | @@ -0,0 +1,43 @@ | |||
953 | 1 | /** | ||
954 | 2 | * Copyright (C) 2014 Canonical, Ltd. | ||
955 | 3 | * | ||
956 | 4 | * This program is free software: you can redistribute it and/or modify it under | ||
957 | 5 | * the terms of the GNU Lesser General Public License version 3, as published by | ||
958 | 6 | * the Free Software Foundation. | ||
959 | 7 | * | ||
960 | 8 | * This program is distributed in the hope that it will be useful, but WITHOUT | ||
961 | 9 | * ANY WARRANTY; without even the implied warranties of MERCHANTABILITY, | ||
962 | 10 | * SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
963 | 11 | * Lesser General Public License for more details. | ||
964 | 12 | * | ||
965 | 13 | * You should have received a copy of the GNU Lesser General Public License | ||
966 | 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
967 | 15 | * | ||
968 | 16 | * Authors: Andreas Pokorny <andreas.pokorny@canonical.com> | ||
969 | 17 | */ | ||
970 | 18 | |||
971 | 19 | #include "powerddbus.h" | ||
972 | 20 | |||
973 | 21 | #include <QDBusConnection> | ||
974 | 22 | #include <QDBusInterface> | ||
975 | 23 | #include <QDBusReply> | ||
976 | 24 | |||
977 | 25 | PowerDDBus::PowerDDBus() | ||
978 | 26 | : mPowerDIface{ | ||
979 | 27 | new QDBusInterface( | ||
980 | 28 | "com.canonical.powerd", | ||
981 | 29 | "/com/canonical/powerd", | ||
982 | 30 | "com.canonical.powerd", | ||
983 | 31 | QDBusConnection::systemBus())} | ||
984 | 32 | { | ||
985 | 33 | } | ||
986 | 34 | |||
987 | 35 | void PowerDDBus::enableProximityHandling() | ||
988 | 36 | { | ||
989 | 37 | mPowerDIface->call("enableProximityHandling", "telephony-service-handler"); | ||
990 | 38 | } | ||
991 | 39 | |||
992 | 40 | void PowerDDBus::disableProximityHandling() | ||
993 | 41 | { | ||
994 | 42 | mPowerDIface->call("disableProximityHandling", "telephony-service-handler"); | ||
995 | 43 | } | ||
996 | 0 | 44 | ||
997 | === removed file 'handler/powerddbus.cpp' | |||
998 | --- handler/powerddbus.cpp 2017-02-06 13:26:33 +0000 | |||
999 | +++ handler/powerddbus.cpp 1970-01-01 00:00:00 +0000 | |||
1000 | @@ -1,43 +0,0 @@ | |||
1001 | 1 | /** | ||
1002 | 2 | * Copyright (C) 2014 Canonical, Ltd. | ||
1003 | 3 | * | ||
1004 | 4 | * This program is free software: you can redistribute it and/or modify it under | ||
1005 | 5 | * the terms of the GNU Lesser General Public License version 3, as published by | ||
1006 | 6 | * the Free Software Foundation. | ||
1007 | 7 | * | ||
1008 | 8 | * This program is distributed in the hope that it will be useful, but WITHOUT | ||
1009 | 9 | * ANY WARRANTY; without even the implied warranties of MERCHANTABILITY, | ||
1010 | 10 | * SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
1011 | 11 | * Lesser General Public License for more details. | ||
1012 | 12 | * | ||
1013 | 13 | * You should have received a copy of the GNU Lesser General Public License | ||
1014 | 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
1015 | 15 | * | ||
1016 | 16 | * Authors: Andreas Pokorny <andreas.pokorny@canonical.com> | ||
1017 | 17 | */ | ||
1018 | 18 | |||
1019 | 19 | #include "powerddbus.h" | ||
1020 | 20 | |||
1021 | 21 | #include <QDBusConnection> | ||
1022 | 22 | #include <QDBusInterface> | ||
1023 | 23 | #include <QDBusReply> | ||
1024 | 24 | |||
1025 | 25 | PowerDDBus::PowerDDBus() | ||
1026 | 26 | : mPowerDIface{ | ||
1027 | 27 | new QDBusInterface( | ||
1028 | 28 | "com.canonical.powerd", | ||
1029 | 29 | "/com/canonical/powerd", | ||
1030 | 30 | "com.canonical.powerd", | ||
1031 | 31 | QDBusConnection::systemBus())} | ||
1032 | 32 | { | ||
1033 | 33 | } | ||
1034 | 34 | |||
1035 | 35 | void PowerDDBus::enableProximityHandling() | ||
1036 | 36 | { | ||
1037 | 37 | mPowerDIface->call("enableProximityHandling", "telephony-service-handler"); | ||
1038 | 38 | } | ||
1039 | 39 | |||
1040 | 40 | void PowerDDBus::disableProximityHandling() | ||
1041 | 41 | { | ||
1042 | 42 | mPowerDIface->call("disableProximityHandling", "telephony-service-handler"); | ||
1043 | 43 | } | ||
1044 | 44 | 0 | ||
1045 | === added file 'handler/powerddbus.h' | |||
1046 | --- handler/powerddbus.h 1970-01-01 00:00:00 +0000 | |||
1047 | +++ handler/powerddbus.h 2017-02-06 13:26:33 +0000 | |||
1048 | @@ -0,0 +1,38 @@ | |||
1049 | 1 | /** | ||
1050 | 2 | * Copyright (C) 2014 Canonical, Ltd. | ||
1051 | 3 | * | ||
1052 | 4 | * This program is free software: you can redistribute it and/or modify it under | ||
1053 | 5 | * the terms of the GNU Lesser General Public License version 3, as published by | ||
1054 | 6 | * the Free Software Foundation. | ||
1055 | 7 | * | ||
1056 | 8 | * This program is distributed in the hope that it will be useful, but WITHOUT | ||
1057 | 9 | * ANY WARRANTY; without even the implied warranties of MERCHANTABILITY, | ||
1058 | 10 | * SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
1059 | 11 | * Lesser General Public License for more details. | ||
1060 | 12 | * | ||
1061 | 13 | * You should have received a copy of the GNU Lesser General Public License | ||
1062 | 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
1063 | 15 | * | ||
1064 | 16 | * Authors: Andreas Pokorny <andreas.pokorny@canonical.com> | ||
1065 | 17 | */ | ||
1066 | 18 | |||
1067 | 19 | #ifndef POWERD_DUBS_H | ||
1068 | 20 | #define POWERD_DBUS_H | ||
1069 | 21 | |||
1070 | 22 | #include "powerd.h" | ||
1071 | 23 | |||
1072 | 24 | #include <memory> | ||
1073 | 25 | |||
1074 | 26 | class QDBusInterface; | ||
1075 | 27 | |||
1076 | 28 | class PowerDDBus : public PowerD | ||
1077 | 29 | { | ||
1078 | 30 | public: | ||
1079 | 31 | PowerDDBus(); | ||
1080 | 32 | void enableProximityHandling() override; | ||
1081 | 33 | void disableProximityHandling() override; | ||
1082 | 34 | private: | ||
1083 | 35 | std::unique_ptr<QDBusInterface> mPowerDIface; | ||
1084 | 36 | }; | ||
1085 | 37 | |||
1086 | 38 | #endif | ||
1087 | 0 | 39 | ||
1088 | === removed file 'handler/powerddbus.h' | |||
1089 | --- handler/powerddbus.h 2017-02-06 13:26:33 +0000 | |||
1090 | +++ handler/powerddbus.h 1970-01-01 00:00:00 +0000 | |||
1091 | @@ -1,38 +0,0 @@ | |||
1092 | 1 | /** | ||
1093 | 2 | * Copyright (C) 2014 Canonical, Ltd. | ||
1094 | 3 | * | ||
1095 | 4 | * This program is free software: you can redistribute it and/or modify it under | ||
1096 | 5 | * the terms of the GNU Lesser General Public License version 3, as published by | ||
1097 | 6 | * the Free Software Foundation. | ||
1098 | 7 | * | ||
1099 | 8 | * This program is distributed in the hope that it will be useful, but WITHOUT | ||
1100 | 9 | * ANY WARRANTY; without even the implied warranties of MERCHANTABILITY, | ||
1101 | 10 | * SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
1102 | 11 | * Lesser General Public License for more details. | ||
1103 | 12 | * | ||
1104 | 13 | * You should have received a copy of the GNU Lesser General Public License | ||
1105 | 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
1106 | 15 | * | ||
1107 | 16 | * Authors: Andreas Pokorny <andreas.pokorny@canonical.com> | ||
1108 | 17 | */ | ||
1109 | 18 | |||
1110 | 19 | #ifndef POWERD_DUBS_H | ||
1111 | 20 | #define POWERD_DBUS_H | ||
1112 | 21 | |||
1113 | 22 | #include "powerd.h" | ||
1114 | 23 | |||
1115 | 24 | #include <memory> | ||
1116 | 25 | |||
1117 | 26 | class QDBusInterface; | ||
1118 | 27 | |||
1119 | 28 | class PowerDDBus : public PowerD | ||
1120 | 29 | { | ||
1121 | 30 | public: | ||
1122 | 31 | PowerDDBus(); | ||
1123 | 32 | void enableProximityHandling() override; | ||
1124 | 33 | void disableProximityHandling() override; | ||
1125 | 34 | private: | ||
1126 | 35 | std::unique_ptr<QDBusInterface> mPowerDIface; | ||
1127 | 36 | }; | ||
1128 | 37 | |||
1129 | 38 | #endif | ||
1130 | 39 | 0 | ||
1131 | === added file 'handler/qpulseaudioengine.cpp' | |||
1132 | --- handler/qpulseaudioengine.cpp 1970-01-01 00:00:00 +0000 | |||
1133 | +++ handler/qpulseaudioengine.cpp 2017-02-06 13:26:33 +0000 | |||
1134 | @@ -0,0 +1,853 @@ | |||
1135 | 1 | /**************************************************************************** | ||
1136 | 2 | ** | ||
1137 | 3 | ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). | ||
1138 | 4 | ** Contact: http://www.qt-project.org/legal | ||
1139 | 5 | ** | ||
1140 | 6 | ** This file was taken from qt5 and modified by | ||
1141 | 7 | ** David Henningsson <david.henningsson@canonical.com> for usage in | ||
1142 | 8 | ** telepathy-ofono. | ||
1143 | 9 | ** | ||
1144 | 10 | ** GNU Lesser General Public License Usage | ||
1145 | 11 | ** Alternatively, this file may be used under the terms of the GNU Lesser | ||
1146 | 12 | ** General Public License version 2.1 as published by the Free Software | ||
1147 | 13 | ** Foundation and appearing in the file LICENSE.LGPL included in the | ||
1148 | 14 | ** packaging of this file. Please review the following information to | ||
1149 | 15 | ** ensure the GNU Lesser General Public License version 2.1 requirements | ||
1150 | 16 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | ||
1151 | 17 | ** | ||
1152 | 18 | ****************************************************************************/ | ||
1153 | 19 | |||
1154 | 20 | #include <QtCore/qdebug.h> | ||
1155 | 21 | |||
1156 | 22 | #include "qpulseaudioengine.h" | ||
1157 | 23 | #include <sys/types.h> | ||
1158 | 24 | #include <unistd.h> | ||
1159 | 25 | |||
1160 | 26 | #define PULSEAUDIO_PROFILE_HSP "headset_head_unit" | ||
1161 | 27 | #define PULSEAUDIO_PROFILE_A2DP "a2dp_sink" | ||
1162 | 28 | |||
1163 | 29 | QT_BEGIN_NAMESPACE | ||
1164 | 30 | |||
1165 | 31 | static void contextStateCallbackInit(pa_context *context, void *userdata) | ||
1166 | 32 | { | ||
1167 | 33 | Q_UNUSED(context); | ||
1168 | 34 | QPulseAudioEngineWorker *pulseEngine = reinterpret_cast<QPulseAudioEngineWorker*>(userdata); | ||
1169 | 35 | pa_threaded_mainloop_signal(pulseEngine->mainloop(), 0); | ||
1170 | 36 | } | ||
1171 | 37 | |||
1172 | 38 | static void contextStateCallback(pa_context *context, void *userdata) | ||
1173 | 39 | { | ||
1174 | 40 | Q_UNUSED(userdata); | ||
1175 | 41 | Q_UNUSED(context); | ||
1176 | 42 | } | ||
1177 | 43 | |||
1178 | 44 | static void success_cb(pa_context *context, int success, void *userdata) | ||
1179 | 45 | { | ||
1180 | 46 | QPulseAudioEngineWorker *pulseEngine = reinterpret_cast<QPulseAudioEngineWorker*>(userdata); | ||
1181 | 47 | pa_threaded_mainloop_signal(pulseEngine->mainloop(), 0); | ||
1182 | 48 | } | ||
1183 | 49 | |||
1184 | 50 | /* Callbacks used when handling events from PulseAudio */ | ||
1185 | 51 | static void plug_card_cb(pa_context *c, const pa_card_info *info, int isLast, void *userdata) | ||
1186 | 52 | { | ||
1187 | 53 | QPulseAudioEngineWorker *pulseEngine = static_cast<QPulseAudioEngineWorker*>(userdata); | ||
1188 | 54 | if (isLast != 0 || !pulseEngine || !info) { | ||
1189 | 55 | pa_threaded_mainloop_signal(pulseEngine->mainloop(), 0); | ||
1190 | 56 | return; | ||
1191 | 57 | } | ||
1192 | 58 | pulseEngine->plugCardCallback(info); | ||
1193 | 59 | } | ||
1194 | 60 | |||
1195 | 61 | static void update_card_cb(pa_context *c, const pa_card_info *info, int isLast, void *userdata) | ||
1196 | 62 | { | ||
1197 | 63 | QPulseAudioEngineWorker *pulseEngine = static_cast<QPulseAudioEngineWorker*>(userdata); | ||
1198 | 64 | if (isLast != 0 || !pulseEngine || !info) { | ||
1199 | 65 | pa_threaded_mainloop_signal(pulseEngine->mainloop(), 0); | ||
1200 | 66 | return; | ||
1201 | 67 | } | ||
1202 | 68 | pulseEngine->updateCardCallback(info); | ||
1203 | 69 | } | ||
1204 | 70 | |||
1205 | 71 | static void unplug_card_cb(pa_context *c, const pa_card_info *info, int isLast, void *userdata) | ||
1206 | 72 | { | ||
1207 | 73 | QPulseAudioEngineWorker *pulseEngine = static_cast<QPulseAudioEngineWorker*>(userdata); | ||
1208 | 74 | if (!pulseEngine) { | ||
1209 | 75 | pa_threaded_mainloop_signal(pulseEngine->mainloop(), 0); | ||
1210 | 76 | return; | ||
1211 | 77 | } | ||
1212 | 78 | |||
1213 | 79 | if (info == NULL) { | ||
1214 | 80 | /* That means that the card used to query card_info was removed */ | ||
1215 | 81 | pulseEngine->unplugCardCallback(); | ||
1216 | 82 | } | ||
1217 | 83 | } | ||
1218 | 84 | |||
1219 | 85 | static void subscribeCallback(pa_context *context, pa_subscription_event_type_t t, uint32_t idx, void *userdata) | ||
1220 | 86 | { | ||
1221 | 87 | /* For card change events (slot plug/unplug and add/remove card) */ | ||
1222 | 88 | if ((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_CARD) { | ||
1223 | 89 | if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_CHANGE) { | ||
1224 | 90 | QMetaObject::invokeMethod((QPulseAudioEngineWorker *) userdata, "handleCardEvent", | ||
1225 | 91 | Qt::QueuedConnection, Q_ARG(int, PA_SUBSCRIPTION_EVENT_CHANGE), Q_ARG(unsigned int, idx)); | ||
1226 | 92 | } else if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_NEW) { | ||
1227 | 93 | QMetaObject::invokeMethod((QPulseAudioEngineWorker *) userdata, "handleCardEvent", | ||
1228 | 94 | Qt::QueuedConnection, Q_ARG(int, PA_SUBSCRIPTION_EVENT_NEW), Q_ARG(unsigned int, idx)); | ||
1229 | 95 | } else if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_REMOVE) { | ||
1230 | 96 | QMetaObject::invokeMethod((QPulseAudioEngineWorker *) userdata, "handleCardEvent", | ||
1231 | 97 | Qt::QueuedConnection, Q_ARG(int, PA_SUBSCRIPTION_EVENT_REMOVE), Q_ARG(unsigned int, idx)); | ||
1232 | 98 | } | ||
1233 | 99 | } | ||
1234 | 100 | } | ||
1235 | 101 | |||
1236 | 102 | QPulseAudioEngineWorker::QPulseAudioEngineWorker(QObject *parent) | ||
1237 | 103 | : QObject(parent) | ||
1238 | 104 | , m_mainLoopApi(0) | ||
1239 | 105 | , m_context(0) | ||
1240 | 106 | , m_callstatus(CallEnded) | ||
1241 | 107 | , m_audiomode(AudioModeSpeaker) | ||
1242 | 108 | , m_micmute(false) | ||
1243 | 109 | , m_defaultsink("sink.primary") | ||
1244 | 110 | , m_defaultsource("source.primary") | ||
1245 | 111 | , m_voicecallcard("") | ||
1246 | 112 | , m_voicecallhighest("") | ||
1247 | 113 | , m_voicecallprofile("") | ||
1248 | 114 | , m_bt_hsp("") | ||
1249 | 115 | , m_bt_hsp_a2dp("") | ||
1250 | 116 | , m_default_bt_card_fallback("") | ||
1251 | 117 | |||
1252 | 118 | { | ||
1253 | 119 | m_mainLoop = pa_threaded_mainloop_new(); | ||
1254 | 120 | if (m_mainLoop == 0) { | ||
1255 | 121 | qWarning("Unable to create pulseaudio mainloop"); | ||
1256 | 122 | return; | ||
1257 | 123 | } | ||
1258 | 124 | |||
1259 | 125 | if (pa_threaded_mainloop_start(m_mainLoop) != 0) { | ||
1260 | 126 | qWarning("Unable to start pulseaudio mainloop"); | ||
1261 | 127 | pa_threaded_mainloop_free(m_mainLoop); | ||
1262 | 128 | m_mainLoop = 0; | ||
1263 | 129 | return; | ||
1264 | 130 | } | ||
1265 | 131 | |||
1266 | 132 | createPulseContext(); | ||
1267 | 133 | } | ||
1268 | 134 | |||
1269 | 135 | bool QPulseAudioEngineWorker::createPulseContext() | ||
1270 | 136 | { | ||
1271 | 137 | bool keepGoing = true; | ||
1272 | 138 | bool ok = true; | ||
1273 | 139 | |||
1274 | 140 | if (m_context) | ||
1275 | 141 | return true; | ||
1276 | 142 | |||
1277 | 143 | m_mainLoopApi = pa_threaded_mainloop_get_api(m_mainLoop); | ||
1278 | 144 | |||
1279 | 145 | pa_threaded_mainloop_lock(m_mainLoop); | ||
1280 | 146 | |||
1281 | 147 | m_context = pa_context_new(m_mainLoopApi, QString(QLatin1String("QtmPulseContext:%1")).arg(::getpid()).toLatin1().constData()); | ||
1282 | 148 | pa_context_set_state_callback(m_context, contextStateCallbackInit, this); | ||
1283 | 149 | |||
1284 | 150 | if (!m_context) { | ||
1285 | 151 | qWarning("Unable to create new pulseaudio context"); | ||
1286 | 152 | pa_threaded_mainloop_unlock(m_mainLoop); | ||
1287 | 153 | return false; | ||
1288 | 154 | } | ||
1289 | 155 | |||
1290 | 156 | if (pa_context_connect(m_context, NULL, PA_CONTEXT_NOAUTOSPAWN, NULL) < 0) { | ||
1291 | 157 | qWarning("Unable to create a connection to the pulseaudio context"); | ||
1292 | 158 | pa_threaded_mainloop_unlock(m_mainLoop); | ||
1293 | 159 | releasePulseContext(); | ||
1294 | 160 | return false; | ||
1295 | 161 | } | ||
1296 | 162 | |||
1297 | 163 | pa_threaded_mainloop_wait(m_mainLoop); | ||
1298 | 164 | |||
1299 | 165 | while (keepGoing) { | ||
1300 | 166 | switch (pa_context_get_state(m_context)) { | ||
1301 | 167 | case PA_CONTEXT_CONNECTING: | ||
1302 | 168 | case PA_CONTEXT_AUTHORIZING: | ||
1303 | 169 | case PA_CONTEXT_SETTING_NAME: | ||
1304 | 170 | break; | ||
1305 | 171 | |||
1306 | 172 | case PA_CONTEXT_READY: | ||
1307 | 173 | qDebug("Pulseaudio connection established."); | ||
1308 | 174 | keepGoing = false; | ||
1309 | 175 | break; | ||
1310 | 176 | |||
1311 | 177 | case PA_CONTEXT_TERMINATED: | ||
1312 | 178 | qCritical("Pulseaudio context terminated."); | ||
1313 | 179 | keepGoing = false; | ||
1314 | 180 | ok = false; | ||
1315 | 181 | break; | ||
1316 | 182 | |||
1317 | 183 | case PA_CONTEXT_FAILED: | ||
1318 | 184 | default: | ||
1319 | 185 | qCritical() << QString("Pulseaudio connection failure: %1").arg(pa_strerror(pa_context_errno(m_context))); | ||
1320 | 186 | keepGoing = false; | ||
1321 | 187 | ok = false; | ||
1322 | 188 | } | ||
1323 | 189 | |||
1324 | 190 | if (keepGoing) { | ||
1325 | 191 | pa_threaded_mainloop_wait(m_mainLoop); | ||
1326 | 192 | } | ||
1327 | 193 | } | ||
1328 | 194 | |||
1329 | 195 | if (ok) { | ||
1330 | 196 | pa_context_set_state_callback(m_context, contextStateCallback, this); | ||
1331 | 197 | pa_context_set_subscribe_callback(m_context, subscribeCallback, this); | ||
1332 | 198 | pa_context_subscribe(m_context, PA_SUBSCRIPTION_MASK_CARD, NULL, this); | ||
1333 | 199 | } else { | ||
1334 | 200 | if (m_context) { | ||
1335 | 201 | pa_context_unref(m_context); | ||
1336 | 202 | m_context = 0; | ||
1337 | 203 | } | ||
1338 | 204 | } | ||
1339 | 205 | |||
1340 | 206 | pa_threaded_mainloop_unlock(m_mainLoop); | ||
1341 | 207 | return true; | ||
1342 | 208 | } | ||
1343 | 209 | |||
1344 | 210 | |||
1345 | 211 | void QPulseAudioEngineWorker::releasePulseContext() | ||
1346 | 212 | { | ||
1347 | 213 | if (m_context) { | ||
1348 | 214 | pa_threaded_mainloop_lock(m_mainLoop); | ||
1349 | 215 | pa_context_disconnect(m_context); | ||
1350 | 216 | pa_context_unref(m_context); | ||
1351 | 217 | pa_threaded_mainloop_unlock(m_mainLoop); | ||
1352 | 218 | m_context = 0; | ||
1353 | 219 | } | ||
1354 | 220 | |||
1355 | 221 | } | ||
1356 | 222 | |||
1357 | 223 | QPulseAudioEngineWorker::~QPulseAudioEngineWorker() | ||
1358 | 224 | { | ||
1359 | 225 | releasePulseContext(); | ||
1360 | 226 | |||
1361 | 227 | if (m_mainLoop) { | ||
1362 | 228 | pa_threaded_mainloop_stop(m_mainLoop); | ||
1363 | 229 | pa_threaded_mainloop_free(m_mainLoop); | ||
1364 | 230 | m_mainLoop = 0; | ||
1365 | 231 | } | ||
1366 | 232 | } | ||
1367 | 233 | |||
1368 | 234 | void QPulseAudioEngineWorker::cardInfoCallback(const pa_card_info *info) | ||
1369 | 235 | { | ||
1370 | 236 | pa_card_profile_info2 *voice_call = NULL, *highest = NULL; | ||
1371 | 237 | pa_card_profile_info2 *hsp = NULL, *a2dp = NULL; | ||
1372 | 238 | |||
1373 | 239 | /* For now we only support one card with the voicecall feature */ | ||
1374 | 240 | for (int i = 0; i < info->n_profiles; i++) { | ||
1375 | 241 | if (!highest || info->profiles2[i]->priority > highest->priority) | ||
1376 | 242 | highest = info->profiles2[i]; | ||
1377 | 243 | if (!strcmp(info->profiles2[i]->name, "voicecall")) | ||
1378 | 244 | voice_call = info->profiles2[i]; | ||
1379 | 245 | else if (!strcmp(info->profiles2[i]->name, PULSEAUDIO_PROFILE_HSP) && | ||
1380 | 246 | info->profiles2[i]->available != 0) | ||
1381 | 247 | hsp = info->profiles2[i]; | ||
1382 | 248 | else if (!strcmp(info->profiles2[i]->name, PULSEAUDIO_PROFILE_A2DP) && | ||
1383 | 249 | info->profiles2[i]->available != 0) | ||
1384 | 250 | a2dp = info->profiles2[i]; | ||
1385 | 251 | } | ||
1386 | 252 | |||
1387 | 253 | /* Record the card that supports voicecall (default one to be used) */ | ||
1388 | 254 | if (voice_call) { | ||
1389 | 255 | qDebug("Found card that supports voicecall: '%s'", info->name); | ||
1390 | 256 | m_voicecallcard = info->name; | ||
1391 | 257 | m_voicecallhighest = highest->name; | ||
1392 | 258 | qDebug("1"); | ||
1393 | 259 | m_voicecallprofile = voice_call->name; | ||
1394 | 260 | qDebug("2"); | ||
1395 | 261 | } | ||
1396 | 262 | |||
1397 | 263 | /* Handle the use cases needed for bluetooth */ | ||
1398 | 264 | if (hsp && a2dp) { | ||
1399 | 265 | qDebug("Found card that supports hsp and a2dp: '%s'", info->name); | ||
1400 | 266 | m_bt_hsp_a2dp = info->name; | ||
1401 | 267 | } else if (hsp && (a2dp == NULL)) { | ||
1402 | 268 | /* This card only provides the hsp profile */ | ||
1403 | 269 | qDebug("Found card that supports only hsp: '%s'", info->name); | ||
1404 | 270 | m_bt_hsp = info->name; | ||
1405 | 271 | } | ||
1406 | 272 | qDebug("3"); | ||
1407 | 273 | } | ||
1408 | 274 | |||
1409 | 275 | void QPulseAudioEngineWorker::sinkInfoCallback(const pa_sink_info *info) | ||
1410 | 276 | { | ||
1411 | 277 | pa_sink_port_info *earpiece = NULL, *speaker = NULL; | ||
1412 | 278 | pa_sink_port_info *wired_headset = NULL, *wired_headphone = NULL; | ||
1413 | 279 | pa_sink_port_info *preferred = NULL; | ||
1414 | 280 | pa_sink_port_info *bluetooth_sco = NULL; | ||
1415 | 281 | pa_sink_port_info *speaker_and_wired_headphone = NULL; | ||
1416 | 282 | AudioMode audiomodetoset; | ||
1417 | 283 | AudioModes modes; | ||
1418 | 284 | |||
1419 | 285 | for (int i = 0; i < info->n_ports; i++) { | ||
1420 | 286 | if (!strcmp(info->ports[i]->name, "output-earpiece")) | ||
1421 | 287 | earpiece = info->ports[i]; | ||
1422 | 288 | else if (!strcmp(info->ports[i]->name, "output-wired_headset") && | ||
1423 | 289 | (info->ports[i]->available != PA_PORT_AVAILABLE_NO)) | ||
1424 | 290 | wired_headset = info->ports[i]; | ||
1425 | 291 | else if (!strcmp(info->ports[i]->name, "output-wired_headphone") && | ||
1426 | 292 | (info->ports[i]->available != PA_PORT_AVAILABLE_NO)) | ||
1427 | 293 | wired_headphone = info->ports[i]; | ||
1428 | 294 | else if (!strcmp(info->ports[i]->name, "output-speaker")) | ||
1429 | 295 | speaker = info->ports[i]; | ||
1430 | 296 | else if (!strcmp(info->ports[i]->name, "output-bluetooth_sco")) | ||
1431 | 297 | bluetooth_sco = info->ports[i]; | ||
1432 | 298 | else if (!strcmp(info->ports[i]->name, "output-speaker+wired_headphone")) | ||
1433 | 299 | speaker_and_wired_headphone = info->ports[i]; | ||
1434 | 300 | } | ||
1435 | 301 | |||
1436 | 302 | if (!earpiece || !speaker) | ||
1437 | 303 | return; /* Not the right sink */ | ||
1438 | 304 | |||
1439 | 305 | /* Refresh list of available audio modes */ | ||
1440 | 306 | modes.append(AudioModeEarpiece); | ||
1441 | 307 | modes.append(AudioModeSpeaker); | ||
1442 | 308 | if (wired_headset || wired_headphone) | ||
1443 | 309 | modes.append(AudioModeWiredHeadset); | ||
1444 | 310 | if (bluetooth_sco && ((m_bt_hsp != "") || (m_bt_hsp_a2dp != ""))) | ||
1445 | 311 | modes.append(AudioModeBluetooth); | ||
1446 | 312 | |||
1447 | 313 | /* Check if the requested mode is available (earpiece*/ | ||
1448 | 314 | if (((m_audiomode == AudioModeWiredHeadset) && !modes.contains(AudioModeWiredHeadset)) || | ||
1449 | 315 | ((m_audiomode == AudioModeBluetooth) && !modes.contains(AudioModeBluetooth))) | ||
1450 | 316 | return; | ||
1451 | 317 | |||
1452 | 318 | /* Now to decide which output to be used, depending on the active mode */ | ||
1453 | 319 | if (m_audiomode & AudioModeEarpiece) { | ||
1454 | 320 | preferred = earpiece; | ||
1455 | 321 | audiomodetoset = AudioModeEarpiece; | ||
1456 | 322 | } | ||
1457 | 323 | if (m_audiomode & AudioModeSpeaker) { | ||
1458 | 324 | preferred = speaker; | ||
1459 | 325 | audiomodetoset = AudioModeSpeaker; | ||
1460 | 326 | } | ||
1461 | 327 | if ((m_audiomode & AudioModeWiredHeadset) && (modes.contains(AudioModeWiredHeadset))) { | ||
1462 | 328 | preferred = wired_headset ? wired_headset : wired_headphone; | ||
1463 | 329 | audiomodetoset = AudioModeWiredHeadset; | ||
1464 | 330 | } | ||
1465 | 331 | if (m_callstatus == CallRinging && speaker_and_wired_headphone) { | ||
1466 | 332 | preferred = speaker_and_wired_headphone; | ||
1467 | 333 | } | ||
1468 | 334 | if ((m_audiomode & AudioModeBluetooth) && (modes.contains(AudioModeBluetooth))) { | ||
1469 | 335 | preferred = bluetooth_sco; | ||
1470 | 336 | audiomodetoset = AudioModeBluetooth; | ||
1471 | 337 | } | ||
1472 | 338 | |||
1473 | 339 | m_audiomode = audiomodetoset; | ||
1474 | 340 | |||
1475 | 341 | m_nametoset = info->name; | ||
1476 | 342 | if (info->active_port != preferred) | ||
1477 | 343 | m_valuetoset = preferred->name; | ||
1478 | 344 | |||
1479 | 345 | if (modes != m_availableAudioModes) | ||
1480 | 346 | m_availableAudioModes = modes; | ||
1481 | 347 | } | ||
1482 | 348 | |||
1483 | 349 | void QPulseAudioEngineWorker::sourceInfoCallback(const pa_source_info *info) | ||
1484 | 350 | { | ||
1485 | 351 | pa_source_port_info *builtin_mic = NULL, *preferred = NULL; | ||
1486 | 352 | pa_source_port_info *wired_headset = NULL, *bluetooth_sco = NULL; | ||
1487 | 353 | |||
1488 | 354 | if (info->monitor_of_sink != PA_INVALID_INDEX) | ||
1489 | 355 | return; /* Not the right source */ | ||
1490 | 356 | |||
1491 | 357 | for (int i = 0; i < info->n_ports; i++) { | ||
1492 | 358 | if (!strcmp(info->ports[i]->name, "input-builtin_mic")) | ||
1493 | 359 | builtin_mic = info->ports[i]; | ||
1494 | 360 | else if (!strcmp(info->ports[i]->name, "input-wired_headset") && | ||
1495 | 361 | (info->ports[i]->available != PA_PORT_AVAILABLE_NO)) | ||
1496 | 362 | wired_headset = info->ports[i]; | ||
1497 | 363 | else if (!strcmp(info->ports[i]->name, "input-bluetooth_sco_headset")) | ||
1498 | 364 | bluetooth_sco = info->ports[i]; | ||
1499 | 365 | } | ||
1500 | 366 | |||
1501 | 367 | if (!builtin_mic) | ||
1502 | 368 | return; /* Not the right source */ | ||
1503 | 369 | |||
1504 | 370 | /* Now to decide which output to be used, depending on the active mode */ | ||
1505 | 371 | if ((m_audiomode & AudioModeEarpiece) || (m_audiomode & AudioModeSpeaker)) | ||
1506 | 372 | preferred = builtin_mic; | ||
1507 | 373 | if ((m_audiomode & AudioModeWiredHeadset) && (m_availableAudioModes.contains(AudioModeWiredHeadset))) | ||
1508 | 374 | preferred = wired_headset ? wired_headset : builtin_mic; | ||
1509 | 375 | if ((m_audiomode & AudioModeBluetooth) && (m_availableAudioModes.contains(AudioModeBluetooth))) | ||
1510 | 376 | preferred = bluetooth_sco; | ||
1511 | 377 | |||
1512 | 378 | m_nametoset = info->name; | ||
1513 | 379 | if (info->active_port != preferred) | ||
1514 | 380 | m_valuetoset = preferred->name; | ||
1515 | 381 | } | ||
1516 | 382 | |||
1517 | 383 | void QPulseAudioEngineWorker::serverInfoCallback(const pa_server_info *info) | ||
1518 | 384 | { | ||
1519 | 385 | /* Saving default sink/source to restore after call hangup */ | ||
1520 | 386 | m_defaultsink = info->default_sink_name; | ||
1521 | 387 | m_defaultsource = info->default_source_name; | ||
1522 | 388 | |||
1523 | 389 | /* In the case of a server callback we need to signal the mainloop */ | ||
1524 | 390 | pa_threaded_mainloop_signal(mainloop(), 0); | ||
1525 | 391 | } | ||
1526 | 392 | |||
1527 | 393 | static void cardinfo_cb(pa_context *context, const pa_card_info *info, int isLast, void *userdata) | ||
1528 | 394 | { | ||
1529 | 395 | QPulseAudioEngineWorker *pulseEngine = static_cast<QPulseAudioEngineWorker*>(userdata); | ||
1530 | 396 | if (isLast != 0 || !pulseEngine || !info) { | ||
1531 | 397 | pa_threaded_mainloop_signal(pulseEngine->mainloop(), 0); | ||
1532 | 398 | return; | ||
1533 | 399 | } | ||
1534 | 400 | pulseEngine->cardInfoCallback(info); | ||
1535 | 401 | } | ||
1536 | 402 | |||
1537 | 403 | static void sinkinfo_cb(pa_context *context, const pa_sink_info *info, int isLast, void *userdata) | ||
1538 | 404 | { | ||
1539 | 405 | QPulseAudioEngineWorker *pulseEngine = static_cast<QPulseAudioEngineWorker*>(userdata); | ||
1540 | 406 | if (isLast != 0 || !pulseEngine || !info) { | ||
1541 | 407 | pa_threaded_mainloop_signal(pulseEngine->mainloop(), 0); | ||
1542 | 408 | return; | ||
1543 | 409 | } | ||
1544 | 410 | pulseEngine->sinkInfoCallback(info); | ||
1545 | 411 | } | ||
1546 | 412 | |||
1547 | 413 | static void sourceinfo_cb(pa_context *context, const pa_source_info *info, int isLast, void *userdata) | ||
1548 | 414 | { | ||
1549 | 415 | QPulseAudioEngineWorker *pulseEngine = static_cast<QPulseAudioEngineWorker*>(userdata); | ||
1550 | 416 | if (isLast != 0 || !pulseEngine || !info) { | ||
1551 | 417 | pa_threaded_mainloop_signal(pulseEngine->mainloop(), 0); | ||
1552 | 418 | return; | ||
1553 | 419 | } | ||
1554 | 420 | pulseEngine->sourceInfoCallback(info); | ||
1555 | 421 | } | ||
1556 | 422 | |||
1557 | 423 | static void serverinfo_cb(pa_context *context, const pa_server_info *info, void *userdata) | ||
1558 | 424 | { | ||
1559 | 425 | QPulseAudioEngineWorker *pulseEngine = static_cast<QPulseAudioEngineWorker*>(userdata); | ||
1560 | 426 | if (!pulseEngine || !info) { | ||
1561 | 427 | pa_threaded_mainloop_signal(pulseEngine->mainloop(), 0); | ||
1562 | 428 | return; | ||
1563 | 429 | } | ||
1564 | 430 | pulseEngine->serverInfoCallback(info); | ||
1565 | 431 | } | ||
1566 | 432 | |||
1567 | 433 | bool QPulseAudioEngineWorker::handleOperation(pa_operation *operation, const char *func_name) | ||
1568 | 434 | { | ||
1569 | 435 | if (!operation) { | ||
1570 | 436 | qCritical("'%s' failed (lost PulseAudio connection?)", func_name); | ||
1571 | 437 | /* Free resources so it can retry a new connection during next operation */ | ||
1572 | 438 | pa_threaded_mainloop_unlock(m_mainLoop); | ||
1573 | 439 | releasePulseContext(); | ||
1574 | 440 | return false; | ||
1575 | 441 | } | ||
1576 | 442 | |||
1577 | 443 | while (pa_operation_get_state(operation) == PA_OPERATION_RUNNING) | ||
1578 | 444 | pa_threaded_mainloop_wait(m_mainLoop); | ||
1579 | 445 | pa_operation_unref(operation); | ||
1580 | 446 | return true; | ||
1581 | 447 | } | ||
1582 | 448 | |||
1583 | 449 | int QPulseAudioEngineWorker::setupVoiceCall() | ||
1584 | 450 | { | ||
1585 | 451 | pa_operation *o; | ||
1586 | 452 | |||
1587 | 453 | qDebug("Setting up pulseaudio for voice call"); | ||
1588 | 454 | |||
1589 | 455 | pa_threaded_mainloop_lock(m_mainLoop); | ||
1590 | 456 | |||
1591 | 457 | /* Get and set the default sink/source to be restored later */ | ||
1592 | 458 | o = pa_context_get_server_info(m_context, serverinfo_cb, this); | ||
1593 | 459 | if (!handleOperation(o, "pa_context_get_server_info")) | ||
1594 | 460 | return -1; | ||
1595 | 461 | |||
1596 | 462 | qDebug("Recorded default sink: %s default source: %s", | ||
1597 | 463 | m_defaultsink.c_str(), m_defaultsource.c_str()); | ||
1598 | 464 | |||
1599 | 465 | /* Walk through the list of devices, find the voice call capable card and | ||
1600 | 466 | * identify if we have bluetooth capable devices (hsp and a2dp) */ | ||
1601 | 467 | m_voicecallcard = m_voicecallhighest = m_voicecallprofile = ""; | ||
1602 | 468 | m_bt_hsp = m_bt_hsp_a2dp = ""; | ||
1603 | 469 | o = pa_context_get_card_info_list(m_context, cardinfo_cb, this); | ||
1604 | 470 | if (!handleOperation(o, "pa_context_get_card_info_list")) | ||
1605 | 471 | return -1; | ||
1606 | 472 | /* In case we have only one bt device that provides hsp and a2dp, we need | ||
1607 | 473 | * to make sure we switch the default profile for that card (to hsp) */ | ||
1608 | 474 | if ((m_bt_hsp_a2dp != "") && (m_bt_hsp == "")) { | ||
1609 | 475 | qDebug("Setting PulseAudio card '%s' profile '%s'", | ||
1610 | 476 | m_bt_hsp_a2dp.c_str(), PULSEAUDIO_PROFILE_HSP); | ||
1611 | 477 | o = pa_context_set_card_profile_by_name(m_context, | ||
1612 | 478 | m_bt_hsp_a2dp.c_str(), PULSEAUDIO_PROFILE_HSP, success_cb, this); | ||
1613 | 479 | if (!handleOperation(o, "pa_context_set_card_profile_by_name")) | ||
1614 | 480 | return -1; | ||
1615 | 481 | } | ||
1616 | 482 | |||
1617 | 483 | pa_threaded_mainloop_unlock(m_mainLoop); | ||
1618 | 484 | |||
1619 | 485 | return 0; | ||
1620 | 486 | } | ||
1621 | 487 | |||
1622 | 488 | void QPulseAudioEngineWorker::restoreVoiceCall() | ||
1623 | 489 | { | ||
1624 | 490 | pa_operation *o; | ||
1625 | 491 | |||
1626 | 492 | qDebug("Restoring pulseaudio previous state"); | ||
1627 | 493 | |||
1628 | 494 | /* Then restore previous settings */ | ||
1629 | 495 | pa_threaded_mainloop_lock(m_mainLoop); | ||
1630 | 496 | |||
1631 | 497 | /* See if we need to restore any HSP+AD2P device state */ | ||
1632 | 498 | if ((m_bt_hsp_a2dp != "") && (m_bt_hsp == "")) { | ||
1633 | 499 | qDebug("Restoring PulseAudio card '%s' to profile '%s'", | ||
1634 | 500 | m_bt_hsp_a2dp.c_str(), PULSEAUDIO_PROFILE_A2DP); | ||
1635 | 501 | o = pa_context_set_card_profile_by_name(m_context, | ||
1636 | 502 | m_bt_hsp_a2dp.c_str(), PULSEAUDIO_PROFILE_A2DP, success_cb, this); | ||
1637 | 503 | if (!handleOperation(o, "pa_context_set_card_profile_by_name")) | ||
1638 | 504 | return; | ||
1639 | 505 | } | ||
1640 | 506 | |||
1641 | 507 | /* Restore default sink/source */ | ||
1642 | 508 | if (m_defaultsink != "") { | ||
1643 | 509 | qDebug("Restoring PulseAudio default sink to '%s'", m_defaultsink.c_str()); | ||
1644 | 510 | o = pa_context_set_default_sink(m_context, m_defaultsink.c_str(), success_cb, this); | ||
1645 | 511 | if (!handleOperation(o, "pa_context_set_default_sink")) | ||
1646 | 512 | return; | ||
1647 | 513 | } | ||
1648 | 514 | if (m_defaultsource != "") { | ||
1649 | 515 | qDebug("Restoring PulseAudio default source to '%s'", m_defaultsource.c_str()); | ||
1650 | 516 | o = pa_context_set_default_source(m_context, m_defaultsource.c_str(), success_cb, this); | ||
1651 | 517 | if (!handleOperation(o, "pa_context_set_default_source")) | ||
1652 | 518 | return; | ||
1653 | 519 | } | ||
1654 | 520 | |||
1655 | 521 | pa_threaded_mainloop_unlock(m_mainLoop); | ||
1656 | 522 | } | ||
1657 | 523 | |||
1658 | 524 | void QPulseAudioEngineWorker::setCallMode(CallStatus callstatus, AudioMode audiomode) | ||
1659 | 525 | { | ||
1660 | 526 | if (!createPulseContext()) { | ||
1661 | 527 | return; | ||
1662 | 528 | } | ||
1663 | 529 | CallStatus p_callstatus = m_callstatus; | ||
1664 | 530 | AudioMode p_audiomode = m_audiomode; | ||
1665 | 531 | AudioModes p_availableAudioModes = m_availableAudioModes; | ||
1666 | 532 | pa_operation *o; | ||
1667 | 533 | |||
1668 | 534 | /* Check if we need to save the current pulseaudio state (e.g. when starting a call) */ | ||
1669 | 535 | if ((callstatus != CallEnded) && (p_callstatus == CallEnded)) { | ||
1670 | 536 | if (setupVoiceCall() < 0) { | ||
1671 | 537 | qCritical("Failed to setup PulseAudio for Voice Call"); | ||
1672 | 538 | return; | ||
1673 | 539 | } | ||
1674 | 540 | } | ||
1675 | 541 | |||
1676 | 542 | /* If we have an active call, update internal state (used later when updating sink/source ports) */ | ||
1677 | 543 | m_callstatus = callstatus; | ||
1678 | 544 | m_audiomode = audiomode; | ||
1679 | 545 | |||
1680 | 546 | pa_threaded_mainloop_lock(m_mainLoop); | ||
1681 | 547 | |||
1682 | 548 | /* Switch the virtual card mode when call is active and not active | ||
1683 | 549 | * This needs to be done before sink/source gets updated, because after changing mode | ||
1684 | 550 | * it will automatically move to input/output-parking */ | ||
1685 | 551 | if ((m_callstatus == CallActive) && (p_callstatus != CallActive) && | ||
1686 | 552 | (m_voicecallcard != "") && (m_voicecallprofile != "")) { | ||
1687 | 553 | qDebug("Setting PulseAudio card '%s' profile '%s'", | ||
1688 | 554 | m_voicecallcard.c_str(), m_voicecallprofile.c_str()); | ||
1689 | 555 | o = pa_context_set_card_profile_by_name(m_context, | ||
1690 | 556 | m_voicecallcard.c_str(), m_voicecallprofile.c_str(), success_cb, this); | ||
1691 | 557 | if (!handleOperation(o, "pa_context_set_card_profile_by_name")) | ||
1692 | 558 | return; | ||
1693 | 559 | } else if ((m_callstatus == CallEnded) && (m_voicecallcard != "") && (m_voicecallhighest != "")) { | ||
1694 | 560 | /* If using droid, make sure to restore to the profile that has the highest score */ | ||
1695 | 561 | qDebug("Restoring PulseAudio card '%s' to profile '%s'", | ||
1696 | 562 | m_voicecallcard.c_str(), m_voicecallhighest.c_str()); | ||
1697 | 563 | o = pa_context_set_card_profile_by_name(m_context, | ||
1698 | 564 | m_voicecallcard.c_str(), m_voicecallhighest.c_str(), success_cb, this); | ||
1699 | 565 | if (!handleOperation(o, "pa_context_set_card_profile_by_name")) | ||
1700 | 566 | return; | ||
1701 | 567 | } | ||
1702 | 568 | |||
1703 | 569 | /* Find highest compatible sink/source elements from the voicecall | ||
1704 | 570 | compatible card (on touch this means the pulse droid element) */ | ||
1705 | 571 | m_nametoset = m_valuetoset = ""; | ||
1706 | 572 | o = pa_context_get_sink_info_list(m_context, sinkinfo_cb, this); | ||
1707 | 573 | if (!handleOperation(o, "pa_context_get_sink_info_list")) | ||
1708 | 574 | return; | ||
1709 | 575 | if ((m_nametoset != "") && (m_nametoset != m_defaultsink)) { | ||
1710 | 576 | qDebug("Setting PulseAudio default sink to '%s'", m_nametoset.c_str()); | ||
1711 | 577 | o = pa_context_set_default_sink(m_context, m_nametoset.c_str(), success_cb, this); | ||
1712 | 578 | if (!handleOperation(o, "pa_context_set_default_sink")) | ||
1713 | 579 | return; | ||
1714 | 580 | } | ||
1715 | 581 | if (m_valuetoset != "") { | ||
1716 | 582 | qDebug("Setting PulseAudio sink '%s' port '%s'", | ||
1717 | 583 | m_nametoset.c_str(), m_valuetoset.c_str()); | ||
1718 | 584 | o = pa_context_set_sink_port_by_name(m_context, m_nametoset.c_str(), | ||
1719 | 585 | m_valuetoset.c_str(), success_cb, this); | ||
1720 | 586 | if (!handleOperation(o, "pa_context_set_sink_port_by_name")) | ||
1721 | 587 | return; | ||
1722 | 588 | } | ||
1723 | 589 | |||
1724 | 590 | /* Same for source */ | ||
1725 | 591 | m_nametoset = m_valuetoset = ""; | ||
1726 | 592 | o = pa_context_get_source_info_list(m_context, sourceinfo_cb, this); | ||
1727 | 593 | if (!handleOperation(o, "pa_context_get_source_info_list")) | ||
1728 | 594 | return; | ||
1729 | 595 | if ((m_nametoset != "") && (m_nametoset != m_defaultsource)) { | ||
1730 | 596 | qDebug("Setting PulseAudio default source to '%s'", m_nametoset.c_str()); | ||
1731 | 597 | o = pa_context_set_default_source(m_context, m_nametoset.c_str(), success_cb, this); | ||
1732 | 598 | if (!handleOperation(o, "pa_context_set_default_source")) | ||
1733 | 599 | return; | ||
1734 | 600 | } | ||
1735 | 601 | if (m_valuetoset != "") { | ||
1736 | 602 | qDebug("Setting PulseAudio source '%s' port '%s'", | ||
1737 | 603 | m_nametoset.c_str(), m_valuetoset.c_str()); | ||
1738 | 604 | o = pa_context_set_source_port_by_name(m_context, m_nametoset.c_str(), | ||
1739 | 605 | m_valuetoset.c_str(), success_cb, this); | ||
1740 | 606 | if (!handleOperation(o, "pa_context_set_source_port_by_name")) | ||
1741 | 607 | return; | ||
1742 | 608 | } | ||
1743 | 609 | |||
1744 | 610 | pa_threaded_mainloop_unlock(m_mainLoop); | ||
1745 | 611 | |||
1746 | 612 | /* Notify if the list of audio modes changed */ | ||
1747 | 613 | if (p_availableAudioModes != m_availableAudioModes) | ||
1748 | 614 | Q_EMIT availableAudioModesChanged(m_availableAudioModes); | ||
1749 | 615 | |||
1750 | 616 | /* Notify if call mode changed */ | ||
1751 | 617 | if (p_audiomode != m_audiomode) { | ||
1752 | 618 | Q_EMIT audioModeChanged(m_audiomode); | ||
1753 | 619 | } | ||
1754 | 620 | |||
1755 | 621 | /* If no more active voicecall, restore previous saved pulseaudio state */ | ||
1756 | 622 | if (callstatus == CallEnded) { | ||
1757 | 623 | restoreVoiceCall(); | ||
1758 | 624 | } | ||
1759 | 625 | |||
1760 | 626 | /* In case the app had set mute when the call wasn't active, make sure we reflect it here */ | ||
1761 | 627 | if (m_callstatus != CallEnded) | ||
1762 | 628 | setMicMute(m_micmute); | ||
1763 | 629 | } | ||
1764 | 630 | |||
1765 | 631 | void QPulseAudioEngineWorker::setMicMute(bool muted) | ||
1766 | 632 | { | ||
1767 | 633 | if (!createPulseContext()) { | ||
1768 | 634 | return; | ||
1769 | 635 | } | ||
1770 | 636 | |||
1771 | 637 | m_micmute = muted; | ||
1772 | 638 | |||
1773 | 639 | if (m_callstatus == CallEnded) | ||
1774 | 640 | return; | ||
1775 | 641 | |||
1776 | 642 | pa_threaded_mainloop_lock(m_mainLoop); | ||
1777 | 643 | |||
1778 | 644 | m_nametoset = ""; | ||
1779 | 645 | pa_operation *o = pa_context_get_source_info_list(m_context, sourceinfo_cb, this); | ||
1780 | 646 | if (!handleOperation(o, "pa_context_get_source_info_list")) | ||
1781 | 647 | return; | ||
1782 | 648 | |||
1783 | 649 | if (m_nametoset != "") { | ||
1784 | 650 | int m = m_micmute ? 1 : 0; | ||
1785 | 651 | qDebug("Setting PulseAudio source '%s' muted '%d'", m_nametoset.c_str(), m); | ||
1786 | 652 | o = pa_context_set_source_mute_by_name(m_context, | ||
1787 | 653 | m_nametoset.c_str(), m, success_cb, this); | ||
1788 | 654 | if (!handleOperation(o, "pa_context_set_source_mute_by_name")) | ||
1789 | 655 | return; | ||
1790 | 656 | } | ||
1791 | 657 | |||
1792 | 658 | pa_threaded_mainloop_unlock(m_mainLoop); | ||
1793 | 659 | } | ||
1794 | 660 | |||
1795 | 661 | void QPulseAudioEngineWorker::plugCardCallback(const pa_card_info *info) | ||
1796 | 662 | { | ||
1797 | 663 | qDebug("Notified about card (%s) add event from PulseAudio", info->name); | ||
1798 | 664 | |||
1799 | 665 | /* Check if it's indeed a BT device (with at least one hsp profile) */ | ||
1800 | 666 | pa_card_profile_info2 *hsp = NULL, *a2dp = NULL; | ||
1801 | 667 | for (int i = 0; i < info->n_profiles; i++) { | ||
1802 | 668 | if (!strcmp(info->profiles2[i]->name, PULSEAUDIO_PROFILE_HSP)) | ||
1803 | 669 | hsp = info->profiles2[i]; | ||
1804 | 670 | else if (!strcmp(info->profiles2[i]->name, PULSEAUDIO_PROFILE_A2DP) && | ||
1805 | 671 | info->profiles2[i]->available != 0) { | ||
1806 | 672 | qDebug("Found a2dp"); | ||
1807 | 673 | a2dp = info->profiles2[i]; | ||
1808 | 674 | } | ||
1809 | 675 | qDebug("%s", info->profiles2[i]->name); | ||
1810 | 676 | } | ||
1811 | 677 | |||
1812 | 678 | if ((!info->active_profile || !strcmp(info->active_profile->name, "off")) && a2dp) { | ||
1813 | 679 | qDebug("No profile set"); | ||
1814 | 680 | m_default_bt_card_fallback = info->name; | ||
1815 | 681 | } | ||
1816 | 682 | |||
1817 | 683 | /* We only care about BT (HSP) devices, and if one is not already available */ | ||
1818 | 684 | if ((m_callstatus != CallEnded) && ((m_bt_hsp == "") || (m_bt_hsp_a2dp == ""))) { | ||
1819 | 685 | if (hsp) | ||
1820 | 686 | m_handleevent = true; | ||
1821 | 687 | } | ||
1822 | 688 | } | ||
1823 | 689 | |||
1824 | 690 | void QPulseAudioEngineWorker::updateCardCallback(const pa_card_info *info) | ||
1825 | 691 | { | ||
1826 | 692 | qDebug("Notified about card (%s) changes event from PulseAudio", info->name); | ||
1827 | 693 | |||
1828 | 694 | /* Check if it's indeed a BT device (with at least one hsp profile) */ | ||
1829 | 695 | pa_card_profile_info2 *hsp = NULL, *a2dp = NULL; | ||
1830 | 696 | for (int i = 0; i < info->n_profiles; i++) { | ||
1831 | 697 | if (!strcmp(info->profiles2[i]->name, PULSEAUDIO_PROFILE_HSP)) | ||
1832 | 698 | hsp = info->profiles2[i]; | ||
1833 | 699 | else if (!strcmp(info->profiles2[i]->name, PULSEAUDIO_PROFILE_A2DP) && | ||
1834 | 700 | info->profiles2[i]->available != 0) { | ||
1835 | 701 | qDebug("Found a2dp"); | ||
1836 | 702 | a2dp = info->profiles2[i]; | ||
1837 | 703 | } | ||
1838 | 704 | qDebug("%s", info->profiles2[i]->name); | ||
1839 | 705 | } | ||
1840 | 706 | |||
1841 | 707 | if ((!info->active_profile || !strcmp(info->active_profile->name, "off")) && a2dp) { | ||
1842 | 708 | qDebug("No profile set"); | ||
1843 | 709 | m_default_bt_card_fallback = info->name; | ||
1844 | 710 | } | ||
1845 | 711 | |||
1846 | 712 | |||
1847 | 713 | /* We only care if the card event for the voicecall capable card */ | ||
1848 | 714 | if ((m_callstatus == CallActive) && (!strcmp(info->name, m_voicecallcard.c_str()))) { | ||
1849 | 715 | if (m_audiomode == AudioModeWiredHeadset) { | ||
1850 | 716 | /* If previous mode is wired, it means it got unplugged */ | ||
1851 | 717 | m_handleevent = true; | ||
1852 | 718 | m_audiomodetoset = AudioModeBtOrWiredOrEarpiece; | ||
1853 | 719 | } else if ((m_audiomode == AudioModeEarpiece) || ((m_audiomode == AudioModeSpeaker))) { | ||
1854 | 720 | /* Now only trigger the event in case wired headset/headphone is now available */ | ||
1855 | 721 | pa_card_port_info *port_info = NULL; | ||
1856 | 722 | for (int i = 0; i < info->n_ports; i++) { | ||
1857 | 723 | if (info->ports[i] && (info->ports[i]->available == PA_PORT_AVAILABLE_YES) && ( | ||
1858 | 724 | !strcmp(info->ports[i]->name, "output-wired_headset") || | ||
1859 | 725 | !strcmp(info->ports[i]->name, "output-wired_headphone"))) { | ||
1860 | 726 | m_handleevent = true; | ||
1861 | 727 | m_audiomodetoset = AudioModeWiredOrEarpiece; | ||
1862 | 728 | } | ||
1863 | 729 | } | ||
1864 | 730 | } else if (m_audiomode == AudioModeBluetooth) { | ||
1865 | 731 | /* Handle the event so we can update the audiomodes */ | ||
1866 | 732 | m_handleevent = true; | ||
1867 | 733 | m_audiomodetoset = AudioModeBluetooth; | ||
1868 | 734 | } | ||
1869 | 735 | } | ||
1870 | 736 | } | ||
1871 | 737 | |||
1872 | 738 | void QPulseAudioEngineWorker::unplugCardCallback() | ||
1873 | 739 | { | ||
1874 | 740 | if (m_callstatus != CallEnded) { | ||
1875 | 741 | m_handleevent = true; | ||
1876 | 742 | } | ||
1877 | 743 | } | ||
1878 | 744 | |||
1879 | 745 | void QPulseAudioEngineWorker::handleCardEvent(const int evt, const unsigned int idx) | ||
1880 | 746 | { | ||
1881 | 747 | pa_operation *o = NULL; | ||
1882 | 748 | |||
1883 | 749 | /* Internal state var used to know if we need to update our internal state */ | ||
1884 | 750 | m_handleevent = false; | ||
1885 | 751 | |||
1886 | 752 | if (evt == PA_SUBSCRIPTION_EVENT_NEW) { | ||
1887 | 753 | o = pa_context_get_card_info_by_index(m_context, idx, plug_card_cb, this); | ||
1888 | 754 | if (!handleOperation(o, "pa_context_get_card_info_by_index")) | ||
1889 | 755 | return; | ||
1890 | 756 | |||
1891 | 757 | if (m_default_bt_card_fallback != "") { | ||
1892 | 758 | o = pa_context_set_card_profile_by_name(m_context, | ||
1893 | 759 | m_default_bt_card_fallback.c_str(), PULSEAUDIO_PROFILE_A2DP, success_cb, this); | ||
1894 | 760 | if (!handleOperation(o, "pa_context_set_card_profile_by_name")) | ||
1895 | 761 | return; | ||
1896 | 762 | m_default_bt_card_fallback = ""; | ||
1897 | 763 | } | ||
1898 | 764 | |||
1899 | 765 | if (m_handleevent) { | ||
1900 | 766 | qDebug("Adding new BT-HSP capable device"); | ||
1901 | 767 | /* In case A2DP is available, switch to HSP */ | ||
1902 | 768 | if (setupVoiceCall() < 0) | ||
1903 | 769 | return; | ||
1904 | 770 | /* Enable the HSP output port */ | ||
1905 | 771 | setCallMode(m_callstatus, AudioModeBluetooth); | ||
1906 | 772 | } | ||
1907 | 773 | } else if (evt == PA_SUBSCRIPTION_EVENT_CHANGE) { | ||
1908 | 774 | o = pa_context_get_card_info_by_index(m_context, idx, update_card_cb, this); | ||
1909 | 775 | if (!handleOperation(o, "pa_context_get_card_info_by_index")) | ||
1910 | 776 | return; | ||
1911 | 777 | |||
1912 | 778 | if (m_default_bt_card_fallback != "") { | ||
1913 | 779 | o = pa_context_set_card_profile_by_name(m_context, | ||
1914 | 780 | m_default_bt_card_fallback.c_str(), PULSEAUDIO_PROFILE_A2DP, success_cb, this); | ||
1915 | 781 | if (!handleOperation(o, "pa_context_set_card_profile_by_name")) | ||
1916 | 782 | return; | ||
1917 | 783 | m_default_bt_card_fallback = ""; | ||
1918 | 784 | } | ||
1919 | 785 | |||
1920 | 786 | if (m_handleevent) { | ||
1921 | 787 | /* In this case it means the handset state changed */ | ||
1922 | 788 | qDebug("Notifying card changes for the voicecall capable card"); | ||
1923 | 789 | setCallMode(m_callstatus, m_audiomodetoset); | ||
1924 | 790 | } | ||
1925 | 791 | } else if (evt == PA_SUBSCRIPTION_EVENT_REMOVE) { | ||
1926 | 792 | /* Check if the main HSP card was removed */ | ||
1927 | 793 | if (m_bt_hsp != "") { | ||
1928 | 794 | o = pa_context_get_card_info_by_name(m_context, m_bt_hsp.c_str(), unplug_card_cb, this); | ||
1929 | 795 | if (!handleOperation(o, "pa_context_get_sink_info_by_name")) | ||
1930 | 796 | return; | ||
1931 | 797 | } | ||
1932 | 798 | if (m_bt_hsp_a2dp != "") { | ||
1933 | 799 | o = pa_context_get_card_info_by_name(m_context, m_bt_hsp_a2dp.c_str(), unplug_card_cb, this); | ||
1934 | 800 | if (!handleOperation(o, "pa_context_get_sink_info_by_name")) | ||
1935 | 801 | return; | ||
1936 | 802 | } | ||
1937 | 803 | if (m_handleevent) { | ||
1938 | 804 | qDebug("Notifying about BT-HSP card removal"); | ||
1939 | 805 | /* Needed in order to save the default sink/source */ | ||
1940 | 806 | if (setupVoiceCall() < 0) | ||
1941 | 807 | return; | ||
1942 | 808 | /* Enable the default handset output port */ | ||
1943 | 809 | setCallMode(m_callstatus, AudioModeWiredOrEarpiece); | ||
1944 | 810 | } | ||
1945 | 811 | } | ||
1946 | 812 | } | ||
1947 | 813 | |||
1948 | 814 | Q_GLOBAL_STATIC(QPulseAudioEngine, pulseEngine); | ||
1949 | 815 | |||
1950 | 816 | QPulseAudioEngine::QPulseAudioEngine(QObject *parent) : | ||
1951 | 817 | QObject(parent) | ||
1952 | 818 | { | ||
1953 | 819 | qRegisterMetaType<CallStatus>(); | ||
1954 | 820 | qRegisterMetaType<AudioMode>(); | ||
1955 | 821 | qRegisterMetaType<AudioModes>(); | ||
1956 | 822 | mWorker = new QPulseAudioEngineWorker(); | ||
1957 | 823 | QObject::connect(mWorker, SIGNAL(audioModeChanged(const AudioMode)), this, SIGNAL(audioModeChanged(const AudioMode)), Qt::QueuedConnection); | ||
1958 | 824 | QObject::connect(mWorker, SIGNAL(availableAudioModesChanged(const AudioModes)), this, SIGNAL(availableAudioModesChanged(const AudioModes)), Qt::QueuedConnection); | ||
1959 | 825 | mWorker->createPulseContext(); | ||
1960 | 826 | mWorker->moveToThread(&mThread); | ||
1961 | 827 | mThread.start(); | ||
1962 | 828 | } | ||
1963 | 829 | |||
1964 | 830 | QPulseAudioEngine::~QPulseAudioEngine() | ||
1965 | 831 | { | ||
1966 | 832 | mThread.quit(); | ||
1967 | 833 | mThread.wait(); | ||
1968 | 834 | } | ||
1969 | 835 | |||
1970 | 836 | QPulseAudioEngine *QPulseAudioEngine::instance() | ||
1971 | 837 | { | ||
1972 | 838 | QPulseAudioEngine *engine = pulseEngine(); | ||
1973 | 839 | return engine; | ||
1974 | 840 | } | ||
1975 | 841 | |||
1976 | 842 | void QPulseAudioEngine::setCallMode(CallStatus callstatus, AudioMode audiomode) | ||
1977 | 843 | { | ||
1978 | 844 | QMetaObject::invokeMethod(mWorker, "setCallMode", Qt::QueuedConnection, Q_ARG(CallStatus, callstatus), Q_ARG(AudioMode, audiomode)); | ||
1979 | 845 | } | ||
1980 | 846 | |||
1981 | 847 | void QPulseAudioEngine::setMicMute(bool muted) | ||
1982 | 848 | { | ||
1983 | 849 | QMetaObject::invokeMethod(mWorker, "setMicMute", Qt::QueuedConnection, Q_ARG(bool, muted)); | ||
1984 | 850 | } | ||
1985 | 851 | |||
1986 | 852 | QT_END_NAMESPACE | ||
1987 | 853 | |||
1988 | 0 | 854 | ||
1989 | === removed file 'handler/qpulseaudioengine.cpp' | |||
1990 | --- handler/qpulseaudioengine.cpp 2017-02-06 13:26:33 +0000 | |||
1991 | +++ handler/qpulseaudioengine.cpp 1970-01-01 00:00:00 +0000 | |||
1992 | @@ -1,853 +0,0 @@ | |||
1993 | 1 | /**************************************************************************** | ||
1994 | 2 | ** | ||
1995 | 3 | ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). | ||
1996 | 4 | ** Contact: http://www.qt-project.org/legal | ||
1997 | 5 | ** | ||
1998 | 6 | ** This file was taken from qt5 and modified by | ||
1999 | 7 | ** David Henningsson <david.henningsson@canonical.com> for usage in | ||
2000 | 8 | ** telepathy-ofono. | ||
2001 | 9 | ** | ||
2002 | 10 | ** GNU Lesser General Public License Usage | ||
2003 | 11 | ** Alternatively, this file may be used under the terms of the GNU Lesser | ||
2004 | 12 | ** General Public License version 2.1 as published by the Free Software | ||
2005 | 13 | ** Foundation and appearing in the file LICENSE.LGPL included in the | ||
2006 | 14 | ** packaging of this file. Please review the following information to | ||
2007 | 15 | ** ensure the GNU Lesser General Public License version 2.1 requirements | ||
2008 | 16 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | ||
2009 | 17 | ** | ||
2010 | 18 | ****************************************************************************/ | ||
2011 | 19 | |||
2012 | 20 | #include <QtCore/qdebug.h> | ||
2013 | 21 | |||
2014 | 22 | #include "qpulseaudioengine.h" | ||
2015 | 23 | #include <sys/types.h> | ||
2016 | 24 | #include <unistd.h> | ||
2017 | 25 | |||
2018 | 26 | #define PULSEAUDIO_PROFILE_HSP "headset_head_unit" | ||
2019 | 27 | #define PULSEAUDIO_PROFILE_A2DP "a2dp_sink" | ||
2020 | 28 | |||
2021 | 29 | QT_BEGIN_NAMESPACE | ||
2022 | 30 | |||
2023 | 31 | static void contextStateCallbackInit(pa_context *context, void *userdata) | ||
2024 | 32 | { | ||
2025 | 33 | Q_UNUSED(context); | ||
2026 | 34 | QPulseAudioEngineWorker *pulseEngine = reinterpret_cast<QPulseAudioEngineWorker*>(userdata); | ||
2027 | 35 | pa_threaded_mainloop_signal(pulseEngine->mainloop(), 0); | ||
2028 | 36 | } | ||
2029 | 37 | |||
2030 | 38 | static void contextStateCallback(pa_context *context, void *userdata) | ||
2031 | 39 | { | ||
2032 | 40 | Q_UNUSED(userdata); | ||
2033 | 41 | Q_UNUSED(context); | ||
2034 | 42 | } | ||
2035 | 43 | |||
2036 | 44 | static void success_cb(pa_context *context, int success, void *userdata) | ||
2037 | 45 | { | ||
2038 | 46 | QPulseAudioEngineWorker *pulseEngine = reinterpret_cast<QPulseAudioEngineWorker*>(userdata); | ||
2039 | 47 | pa_threaded_mainloop_signal(pulseEngine->mainloop(), 0); | ||
2040 | 48 | } | ||
2041 | 49 | |||
2042 | 50 | /* Callbacks used when handling events from PulseAudio */ | ||
2043 | 51 | static void plug_card_cb(pa_context *c, const pa_card_info *info, int isLast, void *userdata) | ||
2044 | 52 | { | ||
2045 | 53 | QPulseAudioEngineWorker *pulseEngine = static_cast<QPulseAudioEngineWorker*>(userdata); | ||
2046 | 54 | if (isLast != 0 || !pulseEngine || !info) { | ||
2047 | 55 | pa_threaded_mainloop_signal(pulseEngine->mainloop(), 0); | ||
2048 | 56 | return; | ||
2049 | 57 | } | ||
2050 | 58 | pulseEngine->plugCardCallback(info); | ||
2051 | 59 | } | ||
2052 | 60 | |||
2053 | 61 | static void update_card_cb(pa_context *c, const pa_card_info *info, int isLast, void *userdata) | ||
2054 | 62 | { | ||
2055 | 63 | QPulseAudioEngineWorker *pulseEngine = static_cast<QPulseAudioEngineWorker*>(userdata); | ||
2056 | 64 | if (isLast != 0 || !pulseEngine || !info) { | ||
2057 | 65 | pa_threaded_mainloop_signal(pulseEngine->mainloop(), 0); | ||
2058 | 66 | return; | ||
2059 | 67 | } | ||
2060 | 68 | pulseEngine->updateCardCallback(info); | ||
2061 | 69 | } | ||
2062 | 70 | |||
2063 | 71 | static void unplug_card_cb(pa_context *c, const pa_card_info *info, int isLast, void *userdata) | ||
2064 | 72 | { | ||
2065 | 73 | QPulseAudioEngineWorker *pulseEngine = static_cast<QPulseAudioEngineWorker*>(userdata); | ||
2066 | 74 | if (!pulseEngine) { | ||
2067 | 75 | pa_threaded_mainloop_signal(pulseEngine->mainloop(), 0); | ||
2068 | 76 | return; | ||
2069 | 77 | } | ||
2070 | 78 | |||
2071 | 79 | if (info == NULL) { | ||
2072 | 80 | /* That means that the card used to query card_info was removed */ | ||
2073 | 81 | pulseEngine->unplugCardCallback(); | ||
2074 | 82 | } | ||
2075 | 83 | } | ||
2076 | 84 | |||
2077 | 85 | static void subscribeCallback(pa_context *context, pa_subscription_event_type_t t, uint32_t idx, void *userdata) | ||
2078 | 86 | { | ||
2079 | 87 | /* For card change events (slot plug/unplug and add/remove card) */ | ||
2080 | 88 | if ((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_CARD) { | ||
2081 | 89 | if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_CHANGE) { | ||
2082 | 90 | QMetaObject::invokeMethod((QPulseAudioEngineWorker *) userdata, "handleCardEvent", | ||
2083 | 91 | Qt::QueuedConnection, Q_ARG(int, PA_SUBSCRIPTION_EVENT_CHANGE), Q_ARG(unsigned int, idx)); | ||
2084 | 92 | } else if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_NEW) { | ||
2085 | 93 | QMetaObject::invokeMethod((QPulseAudioEngineWorker *) userdata, "handleCardEvent", | ||
2086 | 94 | Qt::QueuedConnection, Q_ARG(int, PA_SUBSCRIPTION_EVENT_NEW), Q_ARG(unsigned int, idx)); | ||
2087 | 95 | } else if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_REMOVE) { | ||
2088 | 96 | QMetaObject::invokeMethod((QPulseAudioEngineWorker *) userdata, "handleCardEvent", | ||
2089 | 97 | Qt::QueuedConnection, Q_ARG(int, PA_SUBSCRIPTION_EVENT_REMOVE), Q_ARG(unsigned int, idx)); | ||
2090 | 98 | } | ||
2091 | 99 | } | ||
2092 | 100 | } | ||
2093 | 101 | |||
2094 | 102 | QPulseAudioEngineWorker::QPulseAudioEngineWorker(QObject *parent) | ||
2095 | 103 | : QObject(parent) | ||
2096 | 104 | , m_mainLoopApi(0) | ||
2097 | 105 | , m_context(0) | ||
2098 | 106 | , m_callstatus(CallEnded) | ||
2099 | 107 | , m_audiomode(AudioModeSpeaker) | ||
2100 | 108 | , m_micmute(false) | ||
2101 | 109 | , m_defaultsink("sink.primary") | ||
2102 | 110 | , m_defaultsource("source.primary") | ||
2103 | 111 | , m_voicecallcard("") | ||
2104 | 112 | , m_voicecallhighest("") | ||
2105 | 113 | , m_voicecallprofile("") | ||
2106 | 114 | , m_bt_hsp("") | ||
2107 | 115 | , m_bt_hsp_a2dp("") | ||
2108 | 116 | , m_default_bt_card_fallback("") | ||
2109 | 117 | |||
2110 | 118 | { | ||
2111 | 119 | m_mainLoop = pa_threaded_mainloop_new(); | ||
2112 | 120 | if (m_mainLoop == 0) { | ||
2113 | 121 | qWarning("Unable to create pulseaudio mainloop"); | ||
2114 | 122 | return; | ||
2115 | 123 | } | ||
2116 | 124 | |||
2117 | 125 | if (pa_threaded_mainloop_start(m_mainLoop) != 0) { | ||
2118 | 126 | qWarning("Unable to start pulseaudio mainloop"); | ||
2119 | 127 | pa_threaded_mainloop_free(m_mainLoop); | ||
2120 | 128 | m_mainLoop = 0; | ||
2121 | 129 | return; | ||
2122 | 130 | } | ||
2123 | 131 | |||
2124 | 132 | createPulseContext(); | ||
2125 | 133 | } | ||
2126 | 134 | |||
2127 | 135 | bool QPulseAudioEngineWorker::createPulseContext() | ||
2128 | 136 | { | ||
2129 | 137 | bool keepGoing = true; | ||
2130 | 138 | bool ok = true; | ||
2131 | 139 | |||
2132 | 140 | if (m_context) | ||
2133 | 141 | return true; | ||
2134 | 142 | |||
2135 | 143 | m_mainLoopApi = pa_threaded_mainloop_get_api(m_mainLoop); | ||
2136 | 144 | |||
2137 | 145 | pa_threaded_mainloop_lock(m_mainLoop); | ||
2138 | 146 | |||
2139 | 147 | m_context = pa_context_new(m_mainLoopApi, QString(QLatin1String("QtmPulseContext:%1")).arg(::getpid()).toLatin1().constData()); | ||
2140 | 148 | pa_context_set_state_callback(m_context, contextStateCallbackInit, this); | ||
2141 | 149 | |||
2142 | 150 | if (!m_context) { | ||
2143 | 151 | qWarning("Unable to create new pulseaudio context"); | ||
2144 | 152 | pa_threaded_mainloop_unlock(m_mainLoop); | ||
2145 | 153 | return false; | ||
2146 | 154 | } | ||
2147 | 155 | |||
2148 | 156 | if (pa_context_connect(m_context, NULL, PA_CONTEXT_NOAUTOSPAWN, NULL) < 0) { | ||
2149 | 157 | qWarning("Unable to create a connection to the pulseaudio context"); | ||
2150 | 158 | pa_threaded_mainloop_unlock(m_mainLoop); | ||
2151 | 159 | releasePulseContext(); | ||
2152 | 160 | return false; | ||
2153 | 161 | } | ||
2154 | 162 | |||
2155 | 163 | pa_threaded_mainloop_wait(m_mainLoop); | ||
2156 | 164 | |||
2157 | 165 | while (keepGoing) { | ||
2158 | 166 | switch (pa_context_get_state(m_context)) { | ||
2159 | 167 | case PA_CONTEXT_CONNECTING: | ||
2160 | 168 | case PA_CONTEXT_AUTHORIZING: | ||
2161 | 169 | case PA_CONTEXT_SETTING_NAME: | ||
2162 | 170 | break; | ||
2163 | 171 | |||
2164 | 172 | case PA_CONTEXT_READY: | ||
2165 | 173 | qDebug("Pulseaudio connection established."); | ||
2166 | 174 | keepGoing = false; | ||
2167 | 175 | break; | ||
2168 | 176 | |||
2169 | 177 | case PA_CONTEXT_TERMINATED: | ||
2170 | 178 | qCritical("Pulseaudio context terminated."); | ||
2171 | 179 | keepGoing = false; | ||
2172 | 180 | ok = false; | ||
2173 | 181 | break; | ||
2174 | 182 | |||
2175 | 183 | case PA_CONTEXT_FAILED: | ||
2176 | 184 | default: | ||
2177 | 185 | qCritical() << QString("Pulseaudio connection failure: %1").arg(pa_strerror(pa_context_errno(m_context))); | ||
2178 | 186 | keepGoing = false; | ||
2179 | 187 | ok = false; | ||
2180 | 188 | } | ||
2181 | 189 | |||
2182 | 190 | if (keepGoing) { | ||
2183 | 191 | pa_threaded_mainloop_wait(m_mainLoop); | ||
2184 | 192 | } | ||
2185 | 193 | } | ||
2186 | 194 | |||
2187 | 195 | if (ok) { | ||
2188 | 196 | pa_context_set_state_callback(m_context, contextStateCallback, this); | ||
2189 | 197 | pa_context_set_subscribe_callback(m_context, subscribeCallback, this); | ||
2190 | 198 | pa_context_subscribe(m_context, PA_SUBSCRIPTION_MASK_CARD, NULL, this); | ||
2191 | 199 | } else { | ||
2192 | 200 | if (m_context) { | ||
2193 | 201 | pa_context_unref(m_context); | ||
2194 | 202 | m_context = 0; | ||
2195 | 203 | } | ||
2196 | 204 | } | ||
2197 | 205 | |||
2198 | 206 | pa_threaded_mainloop_unlock(m_mainLoop); | ||
2199 | 207 | return true; | ||
2200 | 208 | } | ||
2201 | 209 | |||
2202 | 210 | |||
2203 | 211 | void QPulseAudioEngineWorker::releasePulseContext() | ||
2204 | 212 | { | ||
2205 | 213 | if (m_context) { | ||
2206 | 214 | pa_threaded_mainloop_lock(m_mainLoop); | ||
2207 | 215 | pa_context_disconnect(m_context); | ||
2208 | 216 | pa_context_unref(m_context); | ||
2209 | 217 | pa_threaded_mainloop_unlock(m_mainLoop); | ||
2210 | 218 | m_context = 0; | ||
2211 | 219 | } | ||
2212 | 220 | |||
2213 | 221 | } | ||
2214 | 222 | |||
2215 | 223 | QPulseAudioEngineWorker::~QPulseAudioEngineWorker() | ||
2216 | 224 | { | ||
2217 | 225 | releasePulseContext(); | ||
2218 | 226 | |||
2219 | 227 | if (m_mainLoop) { | ||
2220 | 228 | pa_threaded_mainloop_stop(m_mainLoop); | ||
2221 | 229 | pa_threaded_mainloop_free(m_mainLoop); | ||
2222 | 230 | m_mainLoop = 0; | ||
2223 | 231 | } | ||
2224 | 232 | } | ||
2225 | 233 | |||
2226 | 234 | void QPulseAudioEngineWorker::cardInfoCallback(const pa_card_info *info) | ||
2227 | 235 | { | ||
2228 | 236 | pa_card_profile_info2 *voice_call = NULL, *highest = NULL; | ||
2229 | 237 | pa_card_profile_info2 *hsp = NULL, *a2dp = NULL; | ||
2230 | 238 | |||
2231 | 239 | /* For now we only support one card with the voicecall feature */ | ||
2232 | 240 | for (int i = 0; i < info->n_profiles; i++) { | ||
2233 | 241 | if (!highest || info->profiles2[i]->priority > highest->priority) | ||
2234 | 242 | highest = info->profiles2[i]; | ||
2235 | 243 | if (!strcmp(info->profiles2[i]->name, "voicecall")) | ||
2236 | 244 | voice_call = info->profiles2[i]; | ||
2237 | 245 | else if (!strcmp(info->profiles2[i]->name, PULSEAUDIO_PROFILE_HSP) && | ||
2238 | 246 | info->profiles2[i]->available != 0) | ||
2239 | 247 | hsp = info->profiles2[i]; | ||
2240 | 248 | else if (!strcmp(info->profiles2[i]->name, PULSEAUDIO_PROFILE_A2DP) && | ||
2241 | 249 | info->profiles2[i]->available != 0) | ||
2242 | 250 | a2dp = info->profiles2[i]; | ||
2243 | 251 | } | ||
2244 | 252 | |||
2245 | 253 | /* Record the card that supports voicecall (default one to be used) */ | ||
2246 | 254 | if (voice_call) { | ||
2247 | 255 | qDebug("Found card that supports voicecall: '%s'", info->name); | ||
2248 | 256 | m_voicecallcard = info->name; | ||
2249 | 257 | m_voicecallhighest = highest->name; | ||
2250 | 258 | qDebug("1"); | ||
2251 | 259 | m_voicecallprofile = voice_call->name; | ||
2252 | 260 | qDebug("2"); | ||
2253 | 261 | } | ||
2254 | 262 | |||
2255 | 263 | /* Handle the use cases needed for bluetooth */ | ||
2256 | 264 | if (hsp && a2dp) { | ||
2257 | 265 | qDebug("Found card that supports hsp and a2dp: '%s'", info->name); | ||
2258 | 266 | m_bt_hsp_a2dp = info->name; | ||
2259 | 267 | } else if (hsp && (a2dp == NULL)) { | ||
2260 | 268 | /* This card only provides the hsp profile */ | ||
2261 | 269 | qDebug("Found card that supports only hsp: '%s'", info->name); | ||
2262 | 270 | m_bt_hsp = info->name; | ||
2263 | 271 | } | ||
2264 | 272 | qDebug("3"); | ||
2265 | 273 | } | ||
2266 | 274 | |||
2267 | 275 | void QPulseAudioEngineWorker::sinkInfoCallback(const pa_sink_info *info) | ||
2268 | 276 | { | ||
2269 | 277 | pa_sink_port_info *earpiece = NULL, *speaker = NULL; | ||
2270 | 278 | pa_sink_port_info *wired_headset = NULL, *wired_headphone = NULL; | ||
2271 | 279 | pa_sink_port_info *preferred = NULL; | ||
2272 | 280 | pa_sink_port_info *bluetooth_sco = NULL; | ||
2273 | 281 | pa_sink_port_info *speaker_and_wired_headphone = NULL; | ||
2274 | 282 | AudioMode audiomodetoset; | ||
2275 | 283 | AudioModes modes; | ||
2276 | 284 | |||
2277 | 285 | for (int i = 0; i < info->n_ports; i++) { | ||
2278 | 286 | if (!strcmp(info->ports[i]->name, "output-earpiece")) | ||
2279 | 287 | earpiece = info->ports[i]; | ||
2280 | 288 | else if (!strcmp(info->ports[i]->name, "output-wired_headset") && | ||
2281 | 289 | (info->ports[i]->available != PA_PORT_AVAILABLE_NO)) | ||
2282 | 290 | wired_headset = info->ports[i]; | ||
2283 | 291 | else if (!strcmp(info->ports[i]->name, "output-wired_headphone") && | ||
2284 | 292 | (info->ports[i]->available != PA_PORT_AVAILABLE_NO)) | ||
2285 | 293 | wired_headphone = info->ports[i]; | ||
2286 | 294 | else if (!strcmp(info->ports[i]->name, "output-speaker")) | ||
2287 | 295 | speaker = info->ports[i]; | ||
2288 | 296 | else if (!strcmp(info->ports[i]->name, "output-bluetooth_sco")) | ||
2289 | 297 | bluetooth_sco = info->ports[i]; | ||
2290 | 298 | else if (!strcmp(info->ports[i]->name, "output-speaker+wired_headphone")) | ||
2291 | 299 | speaker_and_wired_headphone = info->ports[i]; | ||
2292 | 300 | } | ||
2293 | 301 | |||
2294 | 302 | if (!earpiece || !speaker) | ||
2295 | 303 | return; /* Not the right sink */ | ||
2296 | 304 | |||
2297 | 305 | /* Refresh list of available audio modes */ | ||
2298 | 306 | modes.append(AudioModeEarpiece); | ||
2299 | 307 | modes.append(AudioModeSpeaker); | ||
2300 | 308 | if (wired_headset || wired_headphone) | ||
2301 | 309 | modes.append(AudioModeWiredHeadset); | ||
2302 | 310 | if (bluetooth_sco && ((m_bt_hsp != "") || (m_bt_hsp_a2dp != ""))) | ||
2303 | 311 | modes.append(AudioModeBluetooth); | ||
2304 | 312 | |||
2305 | 313 | /* Check if the requested mode is available (earpiece*/ | ||
2306 | 314 | if (((m_audiomode == AudioModeWiredHeadset) && !modes.contains(AudioModeWiredHeadset)) || | ||
2307 | 315 | ((m_audiomode == AudioModeBluetooth) && !modes.contains(AudioModeBluetooth))) | ||
2308 | 316 | return; | ||
2309 | 317 | |||
2310 | 318 | /* Now to decide which output to be used, depending on the active mode */ | ||
2311 | 319 | if (m_audiomode & AudioModeEarpiece) { | ||
2312 | 320 | preferred = earpiece; | ||
2313 | 321 | audiomodetoset = AudioModeEarpiece; | ||
2314 | 322 | } | ||
2315 | 323 | if (m_audiomode & AudioModeSpeaker) { | ||
2316 | 324 | preferred = speaker; | ||
2317 | 325 | audiomodetoset = AudioModeSpeaker; | ||
2318 | 326 | } | ||
2319 | 327 | if ((m_audiomode & AudioModeWiredHeadset) && (modes.contains(AudioModeWiredHeadset))) { | ||
2320 | 328 | preferred = wired_headset ? wired_headset : wired_headphone; | ||
2321 | 329 | audiomodetoset = AudioModeWiredHeadset; | ||
2322 | 330 | } | ||
2323 | 331 | if (m_callstatus == CallRinging && speaker_and_wired_headphone) { | ||
2324 | 332 | preferred = speaker_and_wired_headphone; | ||
2325 | 333 | } | ||
2326 | 334 | if ((m_audiomode & AudioModeBluetooth) && (modes.contains(AudioModeBluetooth))) { | ||
2327 | 335 | preferred = bluetooth_sco; | ||
2328 | 336 | audiomodetoset = AudioModeBluetooth; | ||
2329 | 337 | } | ||
2330 | 338 | |||
2331 | 339 | m_audiomode = audiomodetoset; | ||
2332 | 340 | |||
2333 | 341 | m_nametoset = info->name; | ||
2334 | 342 | if (info->active_port != preferred) | ||
2335 | 343 | m_valuetoset = preferred->name; | ||
2336 | 344 | |||
2337 | 345 | if (modes != m_availableAudioModes) | ||
2338 | 346 | m_availableAudioModes = modes; | ||
2339 | 347 | } | ||
2340 | 348 | |||
2341 | 349 | void QPulseAudioEngineWorker::sourceInfoCallback(const pa_source_info *info) | ||
2342 | 350 | { | ||
2343 | 351 | pa_source_port_info *builtin_mic = NULL, *preferred = NULL; | ||
2344 | 352 | pa_source_port_info *wired_headset = NULL, *bluetooth_sco = NULL; | ||
2345 | 353 | |||
2346 | 354 | if (info->monitor_of_sink != PA_INVALID_INDEX) | ||
2347 | 355 | return; /* Not the right source */ | ||
2348 | 356 | |||
2349 | 357 | for (int i = 0; i < info->n_ports; i++) { | ||
2350 | 358 | if (!strcmp(info->ports[i]->name, "input-builtin_mic")) | ||
2351 | 359 | builtin_mic = info->ports[i]; | ||
2352 | 360 | else if (!strcmp(info->ports[i]->name, "input-wired_headset") && | ||
2353 | 361 | (info->ports[i]->available != PA_PORT_AVAILABLE_NO)) | ||
2354 | 362 | wired_headset = info->ports[i]; | ||
2355 | 363 | else if (!strcmp(info->ports[i]->name, "input-bluetooth_sco_headset")) | ||
2356 | 364 | bluetooth_sco = info->ports[i]; | ||
2357 | 365 | } | ||
2358 | 366 | |||
2359 | 367 | if (!builtin_mic) | ||
2360 | 368 | return; /* Not the right source */ | ||
2361 | 369 | |||
2362 | 370 | /* Now to decide which output to be used, depending on the active mode */ | ||
2363 | 371 | if ((m_audiomode & AudioModeEarpiece) || (m_audiomode & AudioModeSpeaker)) | ||
2364 | 372 | preferred = builtin_mic; | ||
2365 | 373 | if ((m_audiomode & AudioModeWiredHeadset) && (m_availableAudioModes.contains(AudioModeWiredHeadset))) | ||
2366 | 374 | preferred = wired_headset ? wired_headset : builtin_mic; | ||
2367 | 375 | if ((m_audiomode & AudioModeBluetooth) && (m_availableAudioModes.contains(AudioModeBluetooth))) | ||
2368 | 376 | preferred = bluetooth_sco; | ||
2369 | 377 | |||
2370 | 378 | m_nametoset = info->name; | ||
2371 | 379 | if (info->active_port != preferred) | ||
2372 | 380 | m_valuetoset = preferred->name; | ||
2373 | 381 | } | ||
2374 | 382 | |||
2375 | 383 | void QPulseAudioEngineWorker::serverInfoCallback(const pa_server_info *info) | ||
2376 | 384 | { | ||
2377 | 385 | /* Saving default sink/source to restore after call hangup */ | ||
2378 | 386 | m_defaultsink = info->default_sink_name; | ||
2379 | 387 | m_defaultsource = info->default_source_name; | ||
2380 | 388 | |||
2381 | 389 | /* In the case of a server callback we need to signal the mainloop */ | ||
2382 | 390 | pa_threaded_mainloop_signal(mainloop(), 0); | ||
2383 | 391 | } | ||
2384 | 392 | |||
2385 | 393 | static void cardinfo_cb(pa_context *context, const pa_card_info *info, int isLast, void *userdata) | ||
2386 | 394 | { | ||
2387 | 395 | QPulseAudioEngineWorker *pulseEngine = static_cast<QPulseAudioEngineWorker*>(userdata); | ||
2388 | 396 | if (isLast != 0 || !pulseEngine || !info) { | ||
2389 | 397 | pa_threaded_mainloop_signal(pulseEngine->mainloop(), 0); | ||
2390 | 398 | return; | ||
2391 | 399 | } | ||
2392 | 400 | pulseEngine->cardInfoCallback(info); | ||
2393 | 401 | } | ||
2394 | 402 | |||
2395 | 403 | static void sinkinfo_cb(pa_context *context, const pa_sink_info *info, int isLast, void *userdata) | ||
2396 | 404 | { | ||
2397 | 405 | QPulseAudioEngineWorker *pulseEngine = static_cast<QPulseAudioEngineWorker*>(userdata); | ||
2398 | 406 | if (isLast != 0 || !pulseEngine || !info) { | ||
2399 | 407 | pa_threaded_mainloop_signal(pulseEngine->mainloop(), 0); | ||
2400 | 408 | return; | ||
2401 | 409 | } | ||
2402 | 410 | pulseEngine->sinkInfoCallback(info); | ||
2403 | 411 | } | ||
2404 | 412 | |||
2405 | 413 | static void sourceinfo_cb(pa_context *context, const pa_source_info *info, int isLast, void *userdata) | ||
2406 | 414 | { | ||
2407 | 415 | QPulseAudioEngineWorker *pulseEngine = static_cast<QPulseAudioEngineWorker*>(userdata); | ||
2408 | 416 | if (isLast != 0 || !pulseEngine || !info) { | ||
2409 | 417 | pa_threaded_mainloop_signal(pulseEngine->mainloop(), 0); | ||
2410 | 418 | return; | ||
2411 | 419 | } | ||
2412 | 420 | pulseEngine->sourceInfoCallback(info); | ||
2413 | 421 | } | ||
2414 | 422 | |||
2415 | 423 | static void serverinfo_cb(pa_context *context, const pa_server_info *info, void *userdata) | ||
2416 | 424 | { | ||
2417 | 425 | QPulseAudioEngineWorker *pulseEngine = static_cast<QPulseAudioEngineWorker*>(userdata); | ||
2418 | 426 | if (!pulseEngine || !info) { | ||
2419 | 427 | pa_threaded_mainloop_signal(pulseEngine->mainloop(), 0); | ||
2420 | 428 | return; | ||
2421 | 429 | } | ||
2422 | 430 | pulseEngine->serverInfoCallback(info); | ||
2423 | 431 | } | ||
2424 | 432 | |||
2425 | 433 | bool QPulseAudioEngineWorker::handleOperation(pa_operation *operation, const char *func_name) | ||
2426 | 434 | { | ||
2427 | 435 | if (!operation) { | ||
2428 | 436 | qCritical("'%s' failed (lost PulseAudio connection?)", func_name); | ||
2429 | 437 | /* Free resources so it can retry a new connection during next operation */ | ||
2430 | 438 | pa_threaded_mainloop_unlock(m_mainLoop); | ||
2431 | 439 | releasePulseContext(); | ||
2432 | 440 | return false; | ||
2433 | 441 | } | ||
2434 | 442 | |||
2435 | 443 | while (pa_operation_get_state(operation) == PA_OPERATION_RUNNING) | ||
2436 | 444 | pa_threaded_mainloop_wait(m_mainLoop); | ||
2437 | 445 | pa_operation_unref(operation); | ||
2438 | 446 | return true; | ||
2439 | 447 | } | ||
2440 | 448 | |||
2441 | 449 | int QPulseAudioEngineWorker::setupVoiceCall() | ||
2442 | 450 | { | ||
2443 | 451 | pa_operation *o; | ||
2444 | 452 | |||
2445 | 453 | qDebug("Setting up pulseaudio for voice call"); | ||
2446 | 454 | |||
2447 | 455 | pa_threaded_mainloop_lock(m_mainLoop); | ||
2448 | 456 | |||
2449 | 457 | /* Get and set the default sink/source to be restored later */ | ||
2450 | 458 | o = pa_context_get_server_info(m_context, serverinfo_cb, this); | ||
2451 | 459 | if (!handleOperation(o, "pa_context_get_server_info")) | ||
2452 | 460 | return -1; | ||
2453 | 461 | |||
2454 | 462 | qDebug("Recorded default sink: %s default source: %s", | ||
2455 | 463 | m_defaultsink.c_str(), m_defaultsource.c_str()); | ||
2456 | 464 | |||
2457 | 465 | /* Walk through the list of devices, find the voice call capable card and | ||
2458 | 466 | * identify if we have bluetooth capable devices (hsp and a2dp) */ | ||
2459 | 467 | m_voicecallcard = m_voicecallhighest = m_voicecallprofile = ""; | ||
2460 | 468 | m_bt_hsp = m_bt_hsp_a2dp = ""; | ||
2461 | 469 | o = pa_context_get_card_info_list(m_context, cardinfo_cb, this); | ||
2462 | 470 | if (!handleOperation(o, "pa_context_get_card_info_list")) | ||
2463 | 471 | return -1; | ||
2464 | 472 | /* In case we have only one bt device that provides hsp and a2dp, we need | ||
2465 | 473 | * to make sure we switch the default profile for that card (to hsp) */ | ||
2466 | 474 | if ((m_bt_hsp_a2dp != "") && (m_bt_hsp == "")) { | ||
2467 | 475 | qDebug("Setting PulseAudio card '%s' profile '%s'", | ||
2468 | 476 | m_bt_hsp_a2dp.c_str(), PULSEAUDIO_PROFILE_HSP); | ||
2469 | 477 | o = pa_context_set_card_profile_by_name(m_context, | ||
2470 | 478 | m_bt_hsp_a2dp.c_str(), PULSEAUDIO_PROFILE_HSP, success_cb, this); | ||
2471 | 479 | if (!handleOperation(o, "pa_context_set_card_profile_by_name")) | ||
2472 | 480 | return -1; | ||
2473 | 481 | } | ||
2474 | 482 | |||
2475 | 483 | pa_threaded_mainloop_unlock(m_mainLoop); | ||
2476 | 484 | |||
2477 | 485 | return 0; | ||
2478 | 486 | } | ||
2479 | 487 | |||
2480 | 488 | void QPulseAudioEngineWorker::restoreVoiceCall() | ||
2481 | 489 | { | ||
2482 | 490 | pa_operation *o; | ||
2483 | 491 | |||
2484 | 492 | qDebug("Restoring pulseaudio previous state"); | ||
2485 | 493 | |||
2486 | 494 | /* Then restore previous settings */ | ||
2487 | 495 | pa_threaded_mainloop_lock(m_mainLoop); | ||
2488 | 496 | |||
2489 | 497 | /* See if we need to restore any HSP+AD2P device state */ | ||
2490 | 498 | if ((m_bt_hsp_a2dp != "") && (m_bt_hsp == "")) { | ||
2491 | 499 | qDebug("Restoring PulseAudio card '%s' to profile '%s'", | ||
2492 | 500 | m_bt_hsp_a2dp.c_str(), PULSEAUDIO_PROFILE_A2DP); | ||
2493 | 501 | o = pa_context_set_card_profile_by_name(m_context, | ||
2494 | 502 | m_bt_hsp_a2dp.c_str(), PULSEAUDIO_PROFILE_A2DP, success_cb, this); | ||
2495 | 503 | if (!handleOperation(o, "pa_context_set_card_profile_by_name")) | ||
2496 | 504 | return; | ||
2497 | 505 | } | ||
2498 | 506 | |||
2499 | 507 | /* Restore default sink/source */ | ||
2500 | 508 | if (m_defaultsink != "") { | ||
2501 | 509 | qDebug("Restoring PulseAudio default sink to '%s'", m_defaultsink.c_str()); | ||
2502 | 510 | o = pa_context_set_default_sink(m_context, m_defaultsink.c_str(), success_cb, this); | ||
2503 | 511 | if (!handleOperation(o, "pa_context_set_default_sink")) | ||
2504 | 512 | return; | ||
2505 | 513 | } | ||
2506 | 514 | if (m_defaultsource != "") { | ||
2507 | 515 | qDebug("Restoring PulseAudio default source to '%s'", m_defaultsource.c_str()); | ||
2508 | 516 | o = pa_context_set_default_source(m_context, m_defaultsource.c_str(), success_cb, this); | ||
2509 | 517 | if (!handleOperation(o, "pa_context_set_default_source")) | ||
2510 | 518 | return; | ||
2511 | 519 | } | ||
2512 | 520 | |||
2513 | 521 | pa_threaded_mainloop_unlock(m_mainLoop); | ||
2514 | 522 | } | ||
2515 | 523 | |||
2516 | 524 | void QPulseAudioEngineWorker::setCallMode(CallStatus callstatus, AudioMode audiomode) | ||
2517 | 525 | { | ||
2518 | 526 | if (!createPulseContext()) { | ||
2519 | 527 | return; | ||
2520 | 528 | } | ||
2521 | 529 | CallStatus p_callstatus = m_callstatus; | ||
2522 | 530 | AudioMode p_audiomode = m_audiomode; | ||
2523 | 531 | AudioModes p_availableAudioModes = m_availableAudioModes; | ||
2524 | 532 | pa_operation *o; | ||
2525 | 533 | |||
2526 | 534 | /* Check if we need to save the current pulseaudio state (e.g. when starting a call) */ | ||
2527 | 535 | if ((callstatus != CallEnded) && (p_callstatus == CallEnded)) { | ||
2528 | 536 | if (setupVoiceCall() < 0) { | ||
2529 | 537 | qCritical("Failed to setup PulseAudio for Voice Call"); | ||
2530 | 538 | return; | ||
2531 | 539 | } | ||
2532 | 540 | } | ||
2533 | 541 | |||
2534 | 542 | /* If we have an active call, update internal state (used later when updating sink/source ports) */ | ||
2535 | 543 | m_callstatus = callstatus; | ||
2536 | 544 | m_audiomode = audiomode; | ||
2537 | 545 | |||
2538 | 546 | pa_threaded_mainloop_lock(m_mainLoop); | ||
2539 | 547 | |||
2540 | 548 | /* Switch the virtual card mode when call is active and not active | ||
2541 | 549 | * This needs to be done before sink/source gets updated, because after changing mode | ||
2542 | 550 | * it will automatically move to input/output-parking */ | ||
2543 | 551 | if ((m_callstatus == CallActive) && (p_callstatus != CallActive) && | ||
2544 | 552 | (m_voicecallcard != "") && (m_voicecallprofile != "")) { | ||
2545 | 553 | qDebug("Setting PulseAudio card '%s' profile '%s'", | ||
2546 | 554 | m_voicecallcard.c_str(), m_voicecallprofile.c_str()); | ||
2547 | 555 | o = pa_context_set_card_profile_by_name(m_context, | ||
2548 | 556 | m_voicecallcard.c_str(), m_voicecallprofile.c_str(), success_cb, this); | ||
2549 | 557 | if (!handleOperation(o, "pa_context_set_card_profile_by_name")) | ||
2550 | 558 | return; | ||
2551 | 559 | } else if ((m_callstatus == CallEnded) && (m_voicecallcard != "") && (m_voicecallhighest != "")) { | ||
2552 | 560 | /* If using droid, make sure to restore to the profile that has the highest score */ | ||
2553 | 561 | qDebug("Restoring PulseAudio card '%s' to profile '%s'", | ||
2554 | 562 | m_voicecallcard.c_str(), m_voicecallhighest.c_str()); | ||
2555 | 563 | o = pa_context_set_card_profile_by_name(m_context, | ||
2556 | 564 | m_voicecallcard.c_str(), m_voicecallhighest.c_str(), success_cb, this); | ||
2557 | 565 | if (!handleOperation(o, "pa_context_set_card_profile_by_name")) | ||
2558 | 566 | return; | ||
2559 | 567 | } | ||
2560 | 568 | |||
2561 | 569 | /* Find highest compatible sink/source elements from the voicecall | ||
2562 | 570 | compatible card (on touch this means the pulse droid element) */ | ||
2563 | 571 | m_nametoset = m_valuetoset = ""; | ||
2564 | 572 | o = pa_context_get_sink_info_list(m_context, sinkinfo_cb, this); | ||
2565 | 573 | if (!handleOperation(o, "pa_context_get_sink_info_list")) | ||
2566 | 574 | return; | ||
2567 | 575 | if ((m_nametoset != "") && (m_nametoset != m_defaultsink)) { | ||
2568 | 576 | qDebug("Setting PulseAudio default sink to '%s'", m_nametoset.c_str()); | ||
2569 | 577 | o = pa_context_set_default_sink(m_context, m_nametoset.c_str(), success_cb, this); | ||
2570 | 578 | if (!handleOperation(o, "pa_context_set_default_sink")) | ||
2571 | 579 | return; | ||
2572 | 580 | } | ||
2573 | 581 | if (m_valuetoset != "") { | ||
2574 | 582 | qDebug("Setting PulseAudio sink '%s' port '%s'", | ||
2575 | 583 | m_nametoset.c_str(), m_valuetoset.c_str()); | ||
2576 | 584 | o = pa_context_set_sink_port_by_name(m_context, m_nametoset.c_str(), | ||
2577 | 585 | m_valuetoset.c_str(), success_cb, this); | ||
2578 | 586 | if (!handleOperation(o, "pa_context_set_sink_port_by_name")) | ||
2579 | 587 | return; | ||
2580 | 588 | } | ||
2581 | 589 | |||
2582 | 590 | /* Same for source */ | ||
2583 | 591 | m_nametoset = m_valuetoset = ""; | ||
2584 | 592 | o = pa_context_get_source_info_list(m_context, sourceinfo_cb, this); | ||
2585 | 593 | if (!handleOperation(o, "pa_context_get_source_info_list")) | ||
2586 | 594 | return; | ||
2587 | 595 | if ((m_nametoset != "") && (m_nametoset != m_defaultsource)) { | ||
2588 | 596 | qDebug("Setting PulseAudio default source to '%s'", m_nametoset.c_str()); | ||
2589 | 597 | o = pa_context_set_default_source(m_context, m_nametoset.c_str(), success_cb, this); | ||
2590 | 598 | if (!handleOperation(o, "pa_context_set_default_source")) | ||
2591 | 599 | return; | ||
2592 | 600 | } | ||
2593 | 601 | if (m_valuetoset != "") { | ||
2594 | 602 | qDebug("Setting PulseAudio source '%s' port '%s'", | ||
2595 | 603 | m_nametoset.c_str(), m_valuetoset.c_str()); | ||
2596 | 604 | o = pa_context_set_source_port_by_name(m_context, m_nametoset.c_str(), | ||
2597 | 605 | m_valuetoset.c_str(), success_cb, this); | ||
2598 | 606 | if (!handleOperation(o, "pa_context_set_source_port_by_name")) | ||
2599 | 607 | return; | ||
2600 | 608 | } | ||
2601 | 609 | |||
2602 | 610 | pa_threaded_mainloop_unlock(m_mainLoop); | ||
2603 | 611 | |||
2604 | 612 | /* Notify if the list of audio modes changed */ | ||
2605 | 613 | if (p_availableAudioModes != m_availableAudioModes) | ||
2606 | 614 | Q_EMIT availableAudioModesChanged(m_availableAudioModes); | ||
2607 | 615 | |||
2608 | 616 | /* Notify if call mode changed */ | ||
2609 | 617 | if (p_audiomode != m_audiomode) { | ||
2610 | 618 | Q_EMIT audioModeChanged(m_audiomode); | ||
2611 | 619 | } | ||
2612 | 620 | |||
2613 | 621 | /* If no more active voicecall, restore previous saved pulseaudio state */ | ||
2614 | 622 | if (callstatus == CallEnded) { | ||
2615 | 623 | restoreVoiceCall(); | ||
2616 | 624 | } | ||
2617 | 625 | |||
2618 | 626 | /* In case the app had set mute when the call wasn't active, make sure we reflect it here */ | ||
2619 | 627 | if (m_callstatus != CallEnded) | ||
2620 | 628 | setMicMute(m_micmute); | ||
2621 | 629 | } | ||
2622 | 630 | |||
2623 | 631 | void QPulseAudioEngineWorker::setMicMute(bool muted) | ||
2624 | 632 | { | ||
2625 | 633 | if (!createPulseContext()) { | ||
2626 | 634 | return; | ||
2627 | 635 | } | ||
2628 | 636 | |||
2629 | 637 | m_micmute = muted; | ||
2630 | 638 | |||
2631 | 639 | if (m_callstatus == CallEnded) | ||
2632 | 640 | return; | ||
2633 | 641 | |||
2634 | 642 | pa_threaded_mainloop_lock(m_mainLoop); | ||
2635 | 643 | |||
2636 | 644 | m_nametoset = ""; | ||
2637 | 645 | pa_operation *o = pa_context_get_source_info_list(m_context, sourceinfo_cb, this); | ||
2638 | 646 | if (!handleOperation(o, "pa_context_get_source_info_list")) | ||
2639 | 647 | return; | ||
2640 | 648 | |||
2641 | 649 | if (m_nametoset != "") { | ||
2642 | 650 | int m = m_micmute ? 1 : 0; | ||
2643 | 651 | qDebug("Setting PulseAudio source '%s' muted '%d'", m_nametoset.c_str(), m); | ||
2644 | 652 | o = pa_context_set_source_mute_by_name(m_context, | ||
2645 | 653 | m_nametoset.c_str(), m, success_cb, this); | ||
2646 | 654 | if (!handleOperation(o, "pa_context_set_source_mute_by_name")) | ||
2647 | 655 | return; | ||
2648 | 656 | } | ||
2649 | 657 | |||
2650 | 658 | pa_threaded_mainloop_unlock(m_mainLoop); | ||
2651 | 659 | } | ||
2652 | 660 | |||
2653 | 661 | void QPulseAudioEngineWorker::plugCardCallback(const pa_card_info *info) | ||
2654 | 662 | { | ||
2655 | 663 | qDebug("Notified about card (%s) add event from PulseAudio", info->name); | ||
2656 | 664 | |||
2657 | 665 | /* Check if it's indeed a BT device (with at least one hsp profile) */ | ||
2658 | 666 | pa_card_profile_info2 *hsp = NULL, *a2dp = NULL; | ||
2659 | 667 | for (int i = 0; i < info->n_profiles; i++) { | ||
2660 | 668 | if (!strcmp(info->profiles2[i]->name, PULSEAUDIO_PROFILE_HSP)) | ||
2661 | 669 | hsp = info->profiles2[i]; | ||
2662 | 670 | else if (!strcmp(info->profiles2[i]->name, PULSEAUDIO_PROFILE_A2DP) && | ||
2663 | 671 | info->profiles2[i]->available != 0) { | ||
2664 | 672 | qDebug("Found a2dp"); | ||
2665 | 673 | a2dp = info->profiles2[i]; | ||
2666 | 674 | } | ||
2667 | 675 | qDebug("%s", info->profiles2[i]->name); | ||
2668 | 676 | } | ||
2669 | 677 | |||
2670 | 678 | if ((!info->active_profile || !strcmp(info->active_profile->name, "off")) && a2dp) { | ||
2671 | 679 | qDebug("No profile set"); | ||
2672 | 680 | m_default_bt_card_fallback = info->name; | ||
2673 | 681 | } | ||
2674 | 682 | |||
2675 | 683 | /* We only care about BT (HSP) devices, and if one is not already available */ | ||
2676 | 684 | if ((m_callstatus != CallEnded) && ((m_bt_hsp == "") || (m_bt_hsp_a2dp == ""))) { | ||
2677 | 685 | if (hsp) | ||
2678 | 686 | m_handleevent = true; | ||
2679 | 687 | } | ||
2680 | 688 | } | ||
2681 | 689 | |||
2682 | 690 | void QPulseAudioEngineWorker::updateCardCallback(const pa_card_info *info) | ||
2683 | 691 | { | ||
2684 | 692 | qDebug("Notified about card (%s) changes event from PulseAudio", info->name); | ||
2685 | 693 | |||
2686 | 694 | /* Check if it's indeed a BT device (with at least one hsp profile) */ | ||
2687 | 695 | pa_card_profile_info2 *hsp = NULL, *a2dp = NULL; | ||
2688 | 696 | for (int i = 0; i < info->n_profiles; i++) { | ||
2689 | 697 | if (!strcmp(info->profiles2[i]->name, PULSEAUDIO_PROFILE_HSP)) | ||
2690 | 698 | hsp = info->profiles2[i]; | ||
2691 | 699 | else if (!strcmp(info->profiles2[i]->name, PULSEAUDIO_PROFILE_A2DP) && | ||
2692 | 700 | info->profiles2[i]->available != 0) { | ||
2693 | 701 | qDebug("Found a2dp"); | ||
2694 | 702 | a2dp = info->profiles2[i]; | ||
2695 | 703 | } | ||
2696 | 704 | qDebug("%s", info->profiles2[i]->name); | ||
2697 | 705 | } | ||
2698 | 706 | |||
2699 | 707 | if ((!info->active_profile || !strcmp(info->active_profile->name, "off")) && a2dp) { | ||
2700 | 708 | qDebug("No profile set"); | ||
2701 | 709 | m_default_bt_card_fallback = info->name; | ||
2702 | 710 | } | ||
2703 | 711 | |||
2704 | 712 | |||
2705 | 713 | /* We only care if the card event for the voicecall capable card */ | ||
2706 | 714 | if ((m_callstatus == CallActive) && (!strcmp(info->name, m_voicecallcard.c_str()))) { | ||
2707 | 715 | if (m_audiomode == AudioModeWiredHeadset) { | ||
2708 | 716 | /* If previous mode is wired, it means it got unplugged */ | ||
2709 | 717 | m_handleevent = true; | ||
2710 | 718 | m_audiomodetoset = AudioModeBtOrWiredOrEarpiece; | ||
2711 | 719 | } else if ((m_audiomode == AudioModeEarpiece) || ((m_audiomode == AudioModeSpeaker))) { | ||
2712 | 720 | /* Now only trigger the event in case wired headset/headphone is now available */ | ||
2713 | 721 | pa_card_port_info *port_info = NULL; | ||
2714 | 722 | for (int i = 0; i < info->n_ports; i++) { | ||
2715 | 723 | if (info->ports[i] && (info->ports[i]->available == PA_PORT_AVAILABLE_YES) && ( | ||
2716 | 724 | !strcmp(info->ports[i]->name, "output-wired_headset") || | ||
2717 | 725 | !strcmp(info->ports[i]->name, "output-wired_headphone"))) { | ||
2718 | 726 | m_handleevent = true; | ||
2719 | 727 | m_audiomodetoset = AudioModeWiredOrEarpiece; | ||
2720 | 728 | } | ||
2721 | 729 | } | ||
2722 | 730 | } else if (m_audiomode == AudioModeBluetooth) { | ||
2723 | 731 | /* Handle the event so we can update the audiomodes */ | ||
2724 | 732 | m_handleevent = true; | ||
2725 | 733 | m_audiomodetoset = AudioModeBluetooth; | ||
2726 | 734 | } | ||
2727 | 735 | } | ||
2728 | 736 | } | ||
2729 | 737 | |||
2730 | 738 | void QPulseAudioEngineWorker::unplugCardCallback() | ||
2731 | 739 | { | ||
2732 | 740 | if (m_callstatus != CallEnded) { | ||
2733 | 741 | m_handleevent = true; | ||
2734 | 742 | } | ||
2735 | 743 | } | ||
2736 | 744 | |||
2737 | 745 | void QPulseAudioEngineWorker::handleCardEvent(const int evt, const unsigned int idx) | ||
2738 | 746 | { | ||
2739 | 747 | pa_operation *o = NULL; | ||
2740 | 748 | |||
2741 | 749 | /* Internal state var used to know if we need to update our internal state */ | ||
2742 | 750 | m_handleevent = false; | ||
2743 | 751 | |||
2744 | 752 | if (evt == PA_SUBSCRIPTION_EVENT_NEW) { | ||
2745 | 753 | o = pa_context_get_card_info_by_index(m_context, idx, plug_card_cb, this); | ||
2746 | 754 | if (!handleOperation(o, "pa_context_get_card_info_by_index")) | ||
2747 | 755 | return; | ||
2748 | 756 | |||
2749 | 757 | if (m_default_bt_card_fallback != "") { | ||
2750 | 758 | o = pa_context_set_card_profile_by_name(m_context, | ||
2751 | 759 | m_default_bt_card_fallback.c_str(), PULSEAUDIO_PROFILE_A2DP, success_cb, this); | ||
2752 | 760 | if (!handleOperation(o, "pa_context_set_card_profile_by_name")) | ||
2753 | 761 | return; | ||
2754 | 762 | m_default_bt_card_fallback = ""; | ||
2755 | 763 | } | ||
2756 | 764 | |||
2757 | 765 | if (m_handleevent) { | ||
2758 | 766 | qDebug("Adding new BT-HSP capable device"); | ||
2759 | 767 | /* In case A2DP is available, switch to HSP */ | ||
2760 | 768 | if (setupVoiceCall() < 0) | ||
2761 | 769 | return; | ||
2762 | 770 | /* Enable the HSP output port */ | ||
2763 | 771 | setCallMode(m_callstatus, AudioModeBluetooth); | ||
2764 | 772 | } | ||
2765 | 773 | } else if (evt == PA_SUBSCRIPTION_EVENT_CHANGE) { | ||
2766 | 774 | o = pa_context_get_card_info_by_index(m_context, idx, update_card_cb, this); | ||
2767 | 775 | if (!handleOperation(o, "pa_context_get_card_info_by_index")) | ||
2768 | 776 | return; | ||
2769 | 777 | |||
2770 | 778 | if (m_default_bt_card_fallback != "") { | ||
2771 | 779 | o = pa_context_set_card_profile_by_name(m_context, | ||
2772 | 780 | m_default_bt_card_fallback.c_str(), PULSEAUDIO_PROFILE_A2DP, success_cb, this); | ||
2773 | 781 | if (!handleOperation(o, "pa_context_set_card_profile_by_name")) | ||
2774 | 782 | return; | ||
2775 | 783 | m_default_bt_card_fallback = ""; | ||
2776 | 784 | } | ||
2777 | 785 | |||
2778 | 786 | if (m_handleevent) { | ||
2779 | 787 | /* In this case it means the handset state changed */ | ||
2780 | 788 | qDebug("Notifying card changes for the voicecall capable card"); | ||
2781 | 789 | setCallMode(m_callstatus, m_audiomodetoset); | ||
2782 | 790 | } | ||
2783 | 791 | } else if (evt == PA_SUBSCRIPTION_EVENT_REMOVE) { | ||
2784 | 792 | /* Check if the main HSP card was removed */ | ||
2785 | 793 | if (m_bt_hsp != "") { | ||
2786 | 794 | o = pa_context_get_card_info_by_name(m_context, m_bt_hsp.c_str(), unplug_card_cb, this); | ||
2787 | 795 | if (!handleOperation(o, "pa_context_get_sink_info_by_name")) | ||
2788 | 796 | return; | ||
2789 | 797 | } | ||
2790 | 798 | if (m_bt_hsp_a2dp != "") { | ||
2791 | 799 | o = pa_context_get_card_info_by_name(m_context, m_bt_hsp_a2dp.c_str(), unplug_card_cb, this); | ||
2792 | 800 | if (!handleOperation(o, "pa_context_get_sink_info_by_name")) | ||
2793 | 801 | return; | ||
2794 | 802 | } | ||
2795 | 803 | if (m_handleevent) { | ||
2796 | 804 | qDebug("Notifying about BT-HSP card removal"); | ||
2797 | 805 | /* Needed in order to save the default sink/source */ | ||
2798 | 806 | if (setupVoiceCall() < 0) | ||
2799 | 807 | return; | ||
2800 | 808 | /* Enable the default handset output port */ | ||
2801 | 809 | setCallMode(m_callstatus, AudioModeWiredOrEarpiece); | ||
2802 | 810 | } | ||
2803 | 811 | } | ||
2804 | 812 | } | ||
2805 | 813 | |||
2806 | 814 | Q_GLOBAL_STATIC(QPulseAudioEngine, pulseEngine); | ||
2807 | 815 | |||
2808 | 816 | QPulseAudioEngine::QPulseAudioEngine(QObject *parent) : | ||
2809 | 817 | QObject(parent) | ||
2810 | 818 | { | ||
2811 | 819 | qRegisterMetaType<CallStatus>(); | ||
2812 | 820 | qRegisterMetaType<AudioMode>(); | ||
2813 | 821 | qRegisterMetaType<AudioModes>(); | ||
2814 | 822 | mWorker = new QPulseAudioEngineWorker(); | ||
2815 | 823 | QObject::connect(mWorker, SIGNAL(audioModeChanged(const AudioMode)), this, SIGNAL(audioModeChanged(const AudioMode)), Qt::QueuedConnection); | ||
2816 | 824 | QObject::connect(mWorker, SIGNAL(availableAudioModesChanged(const AudioModes)), this, SIGNAL(availableAudioModesChanged(const AudioModes)), Qt::QueuedConnection); | ||
2817 | 825 | mWorker->createPulseContext(); | ||
2818 | 826 | mWorker->moveToThread(&mThread); | ||
2819 | 827 | mThread.start(); | ||
2820 | 828 | } | ||
2821 | 829 | |||
2822 | 830 | QPulseAudioEngine::~QPulseAudioEngine() | ||
2823 | 831 | { | ||
2824 | 832 | mThread.quit(); | ||
2825 | 833 | mThread.wait(); | ||
2826 | 834 | } | ||
2827 | 835 | |||
2828 | 836 | QPulseAudioEngine *QPulseAudioEngine::instance() | ||
2829 | 837 | { | ||
2830 | 838 | QPulseAudioEngine *engine = pulseEngine(); | ||
2831 | 839 | return engine; | ||
2832 | 840 | } | ||
2833 | 841 | |||
2834 | 842 | void QPulseAudioEngine::setCallMode(CallStatus callstatus, AudioMode audiomode) | ||
2835 | 843 | { | ||
2836 | 844 | QMetaObject::invokeMethod(mWorker, "setCallMode", Qt::QueuedConnection, Q_ARG(CallStatus, callstatus), Q_ARG(AudioMode, audiomode)); | ||
2837 | 845 | } | ||
2838 | 846 | |||
2839 | 847 | void QPulseAudioEngine::setMicMute(bool muted) | ||
2840 | 848 | { | ||
2841 | 849 | QMetaObject::invokeMethod(mWorker, "setMicMute", Qt::QueuedConnection, Q_ARG(bool, muted)); | ||
2842 | 850 | } | ||
2843 | 851 | |||
2844 | 852 | QT_END_NAMESPACE | ||
2845 | 853 | |||
2846 | 854 | 0 | ||
2847 | === added file 'handler/qpulseaudioengine.h' | |||
2848 | --- handler/qpulseaudioengine.h 1970-01-01 00:00:00 +0000 | |||
2849 | +++ handler/qpulseaudioengine.h 2017-02-06 13:26:33 +0000 | |||
2850 | @@ -0,0 +1,126 @@ | |||
2851 | 1 | /**************************************************************************** | ||
2852 | 2 | ** | ||
2853 | 3 | ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). | ||
2854 | 4 | ** Contact: http://www.qt-project.org/legal | ||
2855 | 5 | ** | ||
2856 | 6 | ** This file was taken from qt5 and modified by | ||
2857 | 7 | ** David Henningsson <david.henningsson@canonical.com> for usage in | ||
2858 | 8 | ** telepathy-ofono. | ||
2859 | 9 | ** | ||
2860 | 10 | ** GNU Lesser General Public License Usage | ||
2861 | 11 | ** Alternatively, this file may be used under the terms of the GNU Lesser | ||
2862 | 12 | ** General Public License version 2.1 as published by the Free Software | ||
2863 | 13 | ** Foundation and appearing in the file LICENSE.LGPL included in the | ||
2864 | 14 | ** packaging of this file. Please review the following information to | ||
2865 | 15 | ** ensure the GNU Lesser General Public License version 2.1 requirements | ||
2866 | 16 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | ||
2867 | 17 | ** | ||
2868 | 18 | ****************************************************************************/ | ||
2869 | 19 | |||
2870 | 20 | #ifndef QPULSEAUDIOENGINE_H | ||
2871 | 21 | #define QPULSEAUDIOENGINE_H | ||
2872 | 22 | |||
2873 | 23 | #include <QtCore/qmap.h> | ||
2874 | 24 | #include <QtCore/qbytearray.h> | ||
2875 | 25 | #include <QThread> | ||
2876 | 26 | #include <pulse/pulseaudio.h> | ||
2877 | 27 | |||
2878 | 28 | enum AudioMode { | ||
2879 | 29 | AudioModeEarpiece = 0x0001, | ||
2880 | 30 | AudioModeWiredHeadset = 0x0002, | ||
2881 | 31 | AudioModeSpeaker = 0x0004, | ||
2882 | 32 | AudioModeBluetooth = 0x0008, | ||
2883 | 33 | AudioModeBtOrWiredOrEarpiece = AudioModeBluetooth | AudioModeWiredHeadset | AudioModeEarpiece, | ||
2884 | 34 | AudioModeWiredOrEarpiece = AudioModeWiredHeadset | AudioModeEarpiece, | ||
2885 | 35 | AudioModeWiredOrSpeaker = AudioModeWiredHeadset | AudioModeSpeaker, | ||
2886 | 36 | AudioModeBtOrWiredOrSpeaker = AudioModeBluetooth | AudioModeWiredOrSpeaker | ||
2887 | 37 | }; | ||
2888 | 38 | |||
2889 | 39 | Q_DECLARE_METATYPE(AudioMode) | ||
2890 | 40 | |||
2891 | 41 | typedef QList<AudioMode> AudioModes; | ||
2892 | 42 | Q_DECLARE_METATYPE(AudioModes) | ||
2893 | 43 | |||
2894 | 44 | enum CallStatus { | ||
2895 | 45 | CallRinging, | ||
2896 | 46 | CallActive, | ||
2897 | 47 | CallEnded | ||
2898 | 48 | }; | ||
2899 | 49 | |||
2900 | 50 | Q_DECLARE_METATYPE(CallStatus) | ||
2901 | 51 | |||
2902 | 52 | QT_BEGIN_NAMESPACE | ||
2903 | 53 | |||
2904 | 54 | class QPulseAudioEngineWorker : public QObject | ||
2905 | 55 | { | ||
2906 | 56 | Q_OBJECT | ||
2907 | 57 | |||
2908 | 58 | public: | ||
2909 | 59 | QPulseAudioEngineWorker(QObject *parent = 0); | ||
2910 | 60 | ~QPulseAudioEngineWorker(); | ||
2911 | 61 | |||
2912 | 62 | pa_threaded_mainloop *mainloop() { return m_mainLoop; } | ||
2913 | 63 | pa_context *context() { return m_context; } | ||
2914 | 64 | bool createPulseContext(void); | ||
2915 | 65 | int setupVoiceCall(void); | ||
2916 | 66 | void restoreVoiceCall(void); | ||
2917 | 67 | /* Callbacks to be used internally */ | ||
2918 | 68 | void cardInfoCallback(const pa_card_info *card); | ||
2919 | 69 | void sinkInfoCallback(const pa_sink_info *sink); | ||
2920 | 70 | void sourceInfoCallback(const pa_source_info *source); | ||
2921 | 71 | void serverInfoCallback(const pa_server_info *server); | ||
2922 | 72 | void plugCardCallback(const pa_card_info *card); | ||
2923 | 73 | void updateCardCallback(const pa_card_info *card); | ||
2924 | 74 | void unplugCardCallback(); | ||
2925 | 75 | |||
2926 | 76 | Q_SIGNALS: | ||
2927 | 77 | void audioModeChanged(const AudioMode mode); | ||
2928 | 78 | void availableAudioModesChanged(const AudioModes modes); | ||
2929 | 79 | |||
2930 | 80 | public Q_SLOTS: | ||
2931 | 81 | void handleCardEvent(const int evt, const unsigned int idx); | ||
2932 | 82 | void setCallMode(CallStatus callstatus, AudioMode audiomode); | ||
2933 | 83 | void setMicMute(bool muted); /* True if muted, false if unmuted */ | ||
2934 | 84 | |||
2935 | 85 | private: | ||
2936 | 86 | pa_mainloop_api *m_mainLoopApi; | ||
2937 | 87 | pa_threaded_mainloop *m_mainLoop; | ||
2938 | 88 | pa_context *m_context; | ||
2939 | 89 | |||
2940 | 90 | AudioModes m_availableAudioModes; | ||
2941 | 91 | CallStatus m_callstatus; | ||
2942 | 92 | AudioMode m_audiomode; | ||
2943 | 93 | AudioMode m_audiomodetoset; | ||
2944 | 94 | bool m_micmute, m_handleevent; | ||
2945 | 95 | std::string m_nametoset, m_valuetoset; | ||
2946 | 96 | std::string m_defaultsink, m_defaultsource; | ||
2947 | 97 | std::string m_bt_hsp, m_bt_hsp_a2dp; | ||
2948 | 98 | std::string m_default_bt_card_fallback; | ||
2949 | 99 | std::string m_voicecallcard, m_voicecallhighest, m_voicecallprofile; | ||
2950 | 100 | |||
2951 | 101 | bool handleOperation(pa_operation *operation, const char *func_name); | ||
2952 | 102 | void releasePulseContext(void); | ||
2953 | 103 | }; | ||
2954 | 104 | |||
2955 | 105 | class QPulseAudioEngine : public QObject | ||
2956 | 106 | { | ||
2957 | 107 | Q_OBJECT | ||
2958 | 108 | public: | ||
2959 | 109 | explicit QPulseAudioEngine(QObject *parent = 0); | ||
2960 | 110 | ~QPulseAudioEngine(); | ||
2961 | 111 | static QPulseAudioEngine *instance(); | ||
2962 | 112 | |||
2963 | 113 | void setCallMode(CallStatus callstatus, AudioMode audiomode); | ||
2964 | 114 | void setMicMute(bool muted); /* True if muted, false if unmuted */ | ||
2965 | 115 | |||
2966 | 116 | Q_SIGNALS: | ||
2967 | 117 | void audioModeChanged(const AudioMode mode); | ||
2968 | 118 | void availableAudioModesChanged(const AudioModes modes); | ||
2969 | 119 | private: | ||
2970 | 120 | QPulseAudioEngineWorker *mWorker; | ||
2971 | 121 | QThread mThread; | ||
2972 | 122 | }; | ||
2973 | 123 | |||
2974 | 124 | QT_END_NAMESPACE | ||
2975 | 125 | |||
2976 | 126 | #endif | ||
2977 | 0 | 127 | ||
2978 | === removed file 'handler/qpulseaudioengine.h' | |||
2979 | --- handler/qpulseaudioengine.h 2017-02-06 13:26:33 +0000 | |||
2980 | +++ handler/qpulseaudioengine.h 1970-01-01 00:00:00 +0000 | |||
2981 | @@ -1,126 +0,0 @@ | |||
2982 | 1 | /**************************************************************************** | ||
2983 | 2 | ** | ||
2984 | 3 | ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). | ||
2985 | 4 | ** Contact: http://www.qt-project.org/legal | ||
2986 | 5 | ** | ||
2987 | 6 | ** This file was taken from qt5 and modified by | ||
2988 | 7 | ** David Henningsson <david.henningsson@canonical.com> for usage in | ||
2989 | 8 | ** telepathy-ofono. | ||
2990 | 9 | ** | ||
2991 | 10 | ** GNU Lesser General Public License Usage | ||
2992 | 11 | ** Alternatively, this file may be used under the terms of the GNU Lesser | ||
2993 | 12 | ** General Public License version 2.1 as published by the Free Software | ||
2994 | 13 | ** Foundation and appearing in the file LICENSE.LGPL included in the | ||
2995 | 14 | ** packaging of this file. Please review the following information to | ||
2996 | 15 | ** ensure the GNU Lesser General Public License version 2.1 requirements | ||
2997 | 16 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | ||
2998 | 17 | ** | ||
2999 | 18 | ****************************************************************************/ | ||
3000 | 19 | |||
3001 | 20 | #ifndef QPULSEAUDIOENGINE_H | ||
3002 | 21 | #define QPULSEAUDIOENGINE_H | ||
3003 | 22 | |||
3004 | 23 | #include <QtCore/qmap.h> | ||
3005 | 24 | #include <QtCore/qbytearray.h> | ||
3006 | 25 | #include <QThread> | ||
3007 | 26 | #include <pulse/pulseaudio.h> | ||
3008 | 27 | |||
3009 | 28 | enum AudioMode { | ||
3010 | 29 | AudioModeEarpiece = 0x0001, | ||
3011 | 30 | AudioModeWiredHeadset = 0x0002, | ||
3012 | 31 | AudioModeSpeaker = 0x0004, | ||
3013 | 32 | AudioModeBluetooth = 0x0008, | ||
3014 | 33 | AudioModeBtOrWiredOrEarpiece = AudioModeBluetooth | AudioModeWiredHeadset | AudioModeEarpiece, | ||
3015 | 34 | AudioModeWiredOrEarpiece = AudioModeWiredHeadset | AudioModeEarpiece, | ||
3016 | 35 | AudioModeWiredOrSpeaker = AudioModeWiredHeadset | AudioModeSpeaker, | ||
3017 | 36 | AudioModeBtOrWiredOrSpeaker = AudioModeBluetooth | AudioModeWiredOrSpeaker | ||
3018 | 37 | }; | ||
3019 | 38 | |||
3020 | 39 | Q_DECLARE_METATYPE(AudioMode) | ||
3021 | 40 | |||
3022 | 41 | typedef QList<AudioMode> AudioModes; | ||
3023 | 42 | Q_DECLARE_METATYPE(AudioModes) | ||
3024 | 43 | |||
3025 | 44 | enum CallStatus { | ||
3026 | 45 | CallRinging, | ||
3027 | 46 | CallActive, | ||
3028 | 47 | CallEnded | ||
3029 | 48 | }; | ||
3030 | 49 | |||
3031 | 50 | Q_DECLARE_METATYPE(CallStatus) | ||
3032 | 51 | |||
3033 | 52 | QT_BEGIN_NAMESPACE | ||
3034 | 53 | |||
3035 | 54 | class QPulseAudioEngineWorker : public QObject | ||
3036 | 55 | { | ||
3037 | 56 | Q_OBJECT | ||
3038 | 57 | |||
3039 | 58 | public: | ||
3040 | 59 | QPulseAudioEngineWorker(QObject *parent = 0); | ||
3041 | 60 | ~QPulseAudioEngineWorker(); | ||
3042 | 61 | |||
3043 | 62 | pa_threaded_mainloop *mainloop() { return m_mainLoop; } | ||
3044 | 63 | pa_context *context() { return m_context; } | ||
3045 | 64 | bool createPulseContext(void); | ||
3046 | 65 | int setupVoiceCall(void); | ||
3047 | 66 | void restoreVoiceCall(void); | ||
3048 | 67 | /* Callbacks to be used internally */ | ||
3049 | 68 | void cardInfoCallback(const pa_card_info *card); | ||
3050 | 69 | void sinkInfoCallback(const pa_sink_info *sink); | ||
3051 | 70 | void sourceInfoCallback(const pa_source_info *source); | ||
3052 | 71 | void serverInfoCallback(const pa_server_info *server); | ||
3053 | 72 | void plugCardCallback(const pa_card_info *card); | ||
3054 | 73 | void updateCardCallback(const pa_card_info *card); | ||
3055 | 74 | void unplugCardCallback(); | ||
3056 | 75 | |||
3057 | 76 | Q_SIGNALS: | ||
3058 | 77 | void audioModeChanged(const AudioMode mode); | ||
3059 | 78 | void availableAudioModesChanged(const AudioModes modes); | ||
3060 | 79 | |||
3061 | 80 | public Q_SLOTS: | ||
3062 | 81 | void handleCardEvent(const int evt, const unsigned int idx); | ||
3063 | 82 | void setCallMode(CallStatus callstatus, AudioMode audiomode); | ||
3064 | 83 | void setMicMute(bool muted); /* True if muted, false if unmuted */ | ||
3065 | 84 | |||
3066 | 85 | private: | ||
3067 | 86 | pa_mainloop_api *m_mainLoopApi; | ||
3068 | 87 | pa_threaded_mainloop *m_mainLoop; | ||
3069 | 88 | pa_context *m_context; | ||
3070 | 89 | |||
3071 | 90 | AudioModes m_availableAudioModes; | ||
3072 | 91 | CallStatus m_callstatus; | ||
3073 | 92 | AudioMode m_audiomode; | ||
3074 | 93 | AudioMode m_audiomodetoset; | ||
3075 | 94 | bool m_micmute, m_handleevent; | ||
3076 | 95 | std::string m_nametoset, m_valuetoset; | ||
3077 | 96 | std::string m_defaultsink, m_defaultsource; | ||
3078 | 97 | std::string m_bt_hsp, m_bt_hsp_a2dp; | ||
3079 | 98 | std::string m_default_bt_card_fallback; | ||
3080 | 99 | std::string m_voicecallcard, m_voicecallhighest, m_voicecallprofile; | ||
3081 | 100 | |||
3082 | 101 | bool handleOperation(pa_operation *operation, const char *func_name); | ||
3083 | 102 | void releasePulseContext(void); | ||
3084 | 103 | }; | ||
3085 | 104 | |||
3086 | 105 | class QPulseAudioEngine : public QObject | ||
3087 | 106 | { | ||
3088 | 107 | Q_OBJECT | ||
3089 | 108 | public: | ||
3090 | 109 | explicit QPulseAudioEngine(QObject *parent = 0); | ||
3091 | 110 | ~QPulseAudioEngine(); | ||
3092 | 111 | static QPulseAudioEngine *instance(); | ||
3093 | 112 | |||
3094 | 113 | void setCallMode(CallStatus callstatus, AudioMode audiomode); | ||
3095 | 114 | void setMicMute(bool muted); /* True if muted, false if unmuted */ | ||
3096 | 115 | |||
3097 | 116 | Q_SIGNALS: | ||
3098 | 117 | void audioModeChanged(const AudioMode mode); | ||
3099 | 118 | void availableAudioModesChanged(const AudioModes modes); | ||
3100 | 119 | private: | ||
3101 | 120 | QPulseAudioEngineWorker *mWorker; | ||
3102 | 121 | QThread mThread; | ||
3103 | 122 | }; | ||
3104 | 123 | |||
3105 | 124 | QT_END_NAMESPACE | ||
3106 | 125 | |||
3107 | 126 | #endif | ||
3108 | 127 | 0 | ||
3109 | === modified file 'libtelephonyservice/chatentry.cpp' | |||
3110 | --- libtelephonyservice/chatentry.cpp 2016-11-23 19:37:30 +0000 | |||
3111 | +++ libtelephonyservice/chatentry.cpp 2017-02-06 13:26:33 +0000 | |||
3112 | @@ -165,31 +165,42 @@ | |||
3113 | 165 | 165 | ||
3114 | 166 | void ChatEntry::onRolesChanged(const HandleRolesMap &added, const HandleRolesMap &removed) | 166 | void ChatEntry::onRolesChanged(const HandleRolesMap &added, const HandleRolesMap &removed) |
3115 | 167 | { | 167 | { |
3116 | 168 | Q_UNUSED(added); | ||
3117 | 169 | Q_UNUSED(removed); | ||
3118 | 170 | |||
3119 | 171 | RolesMap rolesMap; | ||
3120 | 172 | Tp::TextChannel* channel = 0; | 168 | Tp::TextChannel* channel = 0; |
3121 | 173 | if (rolesInterface) { | 169 | if (rolesInterface) { |
3123 | 174 | rolesMap = rolesInterface->getRoles(); | 170 | if (mRolesMap.isEmpty()) { |
3124 | 171 | mRolesMap = rolesInterface->getRoles(); | ||
3125 | 172 | } | ||
3126 | 175 | channel = qvariant_cast<Tp::TextChannel*>(rolesInterface->property("channel")); | 173 | channel = qvariant_cast<Tp::TextChannel*>(rolesInterface->property("channel")); |
3127 | 176 | } | 174 | } |
3128 | 177 | 175 | ||
3129 | 176 | QMapIterator<uint, uint> it(removed); | ||
3130 | 177 | while (it.hasNext()) { | ||
3131 | 178 | it.next(); | ||
3132 | 179 | mRolesMap.remove(it.key()); | ||
3133 | 180 | } | ||
3134 | 181 | |||
3135 | 182 | QMapIterator<uint, uint> it2(added); | ||
3136 | 183 | while (it2.hasNext()) { | ||
3137 | 184 | it2.next(); | ||
3138 | 185 | mRolesMap[it2.key()] = it2.value(); | ||
3139 | 186 | } | ||
3140 | 187 | |||
3141 | 188 | // TODO avoid iterating over all participants when not needed | ||
3142 | 178 | Q_FOREACH(Participant* participant, mParticipants) { | 189 | Q_FOREACH(Participant* participant, mParticipants) { |
3145 | 179 | if (rolesMap.contains(participant->handle())) { | 190 | if (mRolesMap.contains(participant->handle())) { |
3146 | 180 | participant->setRoles(rolesMap[participant->handle()]); | 191 | participant->setRoles(mRolesMap[participant->handle()]); |
3147 | 181 | } | 192 | } |
3148 | 182 | } | 193 | } |
3149 | 183 | 194 | ||
3150 | 184 | Q_FOREACH(Participant* participant, mLocalPendingParticipants) { | 195 | Q_FOREACH(Participant* participant, mLocalPendingParticipants) { |
3153 | 185 | if (rolesMap.contains(participant->handle())) { | 196 | if (mRolesMap.contains(participant->handle())) { |
3154 | 186 | participant->setRoles(rolesMap[participant->handle()]); | 197 | participant->setRoles(mRolesMap[participant->handle()]); |
3155 | 187 | } | 198 | } |
3156 | 188 | } | 199 | } |
3157 | 189 | 200 | ||
3158 | 190 | Q_FOREACH(Participant* participant, mRemotePendingParticipants) { | 201 | Q_FOREACH(Participant* participant, mRemotePendingParticipants) { |
3161 | 191 | if (rolesMap.contains(participant->handle())) { | 202 | if (mRolesMap.contains(participant->handle())) { |
3162 | 192 | participant->setRoles(rolesMap[participant->handle()]); | 203 | participant->setRoles(mRolesMap[participant->handle()]); |
3163 | 193 | } | 204 | } |
3164 | 194 | } | 205 | } |
3165 | 195 | 206 | ||
3166 | @@ -202,7 +213,7 @@ | |||
3167 | 202 | return; | 213 | return; |
3168 | 203 | } | 214 | } |
3169 | 204 | 215 | ||
3171 | 205 | mSelfContactRoles = rolesMap[selfContact->handle().at(0)]; | 216 | mSelfContactRoles = mRolesMap[selfContact->handle().at(0)]; |
3172 | 206 | Q_EMIT selfContactRolesChanged(); | 217 | Q_EMIT selfContactRolesChanged(); |
3173 | 207 | } | 218 | } |
3174 | 208 | 219 | ||
3175 | @@ -685,15 +696,14 @@ | |||
3176 | 685 | } | 696 | } |
3177 | 686 | } | 697 | } |
3178 | 687 | 698 | ||
3182 | 688 | RolesMap rolesMap; | 699 | if (rolesInterface && mRolesMap.isEmpty()) { |
3183 | 689 | if (rolesInterface) { | 700 | mRolesMap = rolesInterface->getRoles(); |
3181 | 690 | rolesMap = rolesInterface->getRoles(); | ||
3184 | 691 | } | 701 | } |
3185 | 692 | // now add the new participants | 702 | // now add the new participants |
3186 | 693 | // FIXME: check for duplicates? | 703 | // FIXME: check for duplicates? |
3187 | 694 | Q_FOREACH(Tp::ContactPtr contact, added) { | 704 | Q_FOREACH(Tp::ContactPtr contact, added) { |
3188 | 695 | uint handle = contact->handle().at(0); | 705 | uint handle = contact->handle().at(0); |
3190 | 696 | list << new Participant(contact->id(), rolesMap[handle], handle, this); | 706 | list << new Participant(contact->id(), mRolesMap[handle], handle, this); |
3191 | 697 | } | 707 | } |
3192 | 698 | } | 708 | } |
3193 | 699 | 709 | ||
3194 | 700 | 710 | ||
3195 | === modified file 'libtelephonyservice/chatentry.h' | |||
3196 | --- libtelephonyservice/chatentry.h 2017-02-06 13:26:33 +0000 | |||
3197 | +++ libtelephonyservice/chatentry.h 2017-02-06 13:26:33 +0000 | |||
3198 | @@ -226,6 +226,7 @@ | |||
3199 | 226 | Tp::Client::ChannelInterfaceRoomConfigInterface *roomConfigInterface; | 226 | Tp::Client::ChannelInterfaceRoomConfigInterface *roomConfigInterface; |
3200 | 227 | Tp::Client::ChannelInterfaceSubjectInterface *subjectInterface; | 227 | Tp::Client::ChannelInterfaceSubjectInterface *subjectInterface; |
3201 | 228 | ChannelInterfaceRolesInterface *rolesInterface; | 228 | ChannelInterfaceRolesInterface *rolesInterface; |
3202 | 229 | RolesMap mRolesMap; | ||
3203 | 229 | }; | 230 | }; |
3204 | 230 | 231 | ||
3205 | 231 | #endif // CHATENTRY_H | 232 | #endif // CHATENTRY_H |
3206 | 232 | 233 | ||
3207 | === modified file 'libtelephonyservice/contactwatcher.cpp' | |||
3208 | --- libtelephonyservice/contactwatcher.cpp 2017-02-06 13:26:33 +0000 | |||
3209 | +++ libtelephonyservice/contactwatcher.cpp 2017-02-06 13:26:33 +0000 | |||
3210 | @@ -42,8 +42,6 @@ | |||
3211 | 42 | ContactWatcher::ContactWatcher(QObject *parent) : | 42 | ContactWatcher::ContactWatcher(QObject *parent) : |
3212 | 43 | QObject(parent), mRequest(0), mInteractive(false), mCompleted(false) | 43 | QObject(parent), mRequest(0), mInteractive(false), mCompleted(false) |
3213 | 44 | { | 44 | { |
3214 | 45 | // addressable VCard fields defaults to "tel" only | ||
3215 | 46 | mAddressableFields << "tel"; | ||
3216 | 47 | connect(ContactUtils::sharedManager(), | 45 | connect(ContactUtils::sharedManager(), |
3217 | 48 | SIGNAL(contactsAdded(QList<QContactId>)), | 46 | SIGNAL(contactsAdded(QList<QContactId>)), |
3218 | 49 | SLOT(onContactsAdded(QList<QContactId>))); | 47 | SLOT(onContactsAdded(QList<QContactId>))); |
3219 | @@ -67,7 +65,7 @@ | |||
3220 | 67 | 65 | ||
3221 | 68 | void ContactWatcher::startSearching() | 66 | void ContactWatcher::startSearching() |
3222 | 69 | { | 67 | { |
3224 | 70 | if (!mCompleted || mIdentifier.isEmpty() || !mInteractive) { | 68 | if (!mCompleted || mIdentifier.isEmpty() || !mInteractive || mAddressableFields.isEmpty()) { |
3225 | 71 | // component is not ready yet or no identifier given, | 69 | // component is not ready yet or no identifier given, |
3226 | 72 | // or the number is not interactive and thus doesn't need contact info at all | 70 | // or the number is not interactive and thus doesn't need contact info at all |
3227 | 73 | return; | 71 | return; |
3228 | @@ -271,10 +269,6 @@ | |||
3229 | 271 | void ContactWatcher::setAddressableFields(const QStringList &fields) | 269 | void ContactWatcher::setAddressableFields(const QStringList &fields) |
3230 | 272 | { | 270 | { |
3231 | 273 | mAddressableFields = fields; | 271 | mAddressableFields = fields; |
3232 | 274 | // if the addressable fields is empty, fall back to matching phone numbers | ||
3233 | 275 | if (mAddressableFields.isEmpty()) { | ||
3234 | 276 | mAddressableFields << "tel"; | ||
3235 | 277 | } | ||
3236 | 278 | Q_EMIT addressableFieldsChanged(); | 272 | Q_EMIT addressableFieldsChanged(); |
3237 | 279 | 273 | ||
3238 | 280 | startSearching(); | 274 | startSearching(); |
3239 | 281 | 275 | ||
3240 | === modified file 'tests/Ubuntu.Telephony/ContactWatcherTest.cpp' | |||
3241 | --- tests/Ubuntu.Telephony/ContactWatcherTest.cpp 2015-10-09 19:48:07 +0000 | |||
3242 | +++ tests/Ubuntu.Telephony/ContactWatcherTest.cpp 2017-02-06 13:26:33 +0000 | |||
3243 | @@ -95,7 +95,7 @@ | |||
3244 | 95 | 95 | ||
3245 | 96 | // set the phone number and wait for the match to happen | 96 | // set the phone number and wait for the match to happen |
3246 | 97 | watcher.setIdentifier(identifier); | 97 | watcher.setIdentifier(identifier); |
3248 | 98 | 98 | watcher.setAddressableFields(QStringList() << "tel"); | |
3249 | 99 | 99 | ||
3250 | 100 | // contact fetching is asynchronous so use QTRY_COMPARE for the first signal spy | 100 | // contact fetching is asynchronous so use QTRY_COMPARE for the first signal spy |
3251 | 101 | // for the subsequent ones it is fine to use just QCOMPARE | 101 | // for the subsequent ones it is fine to use just QCOMPARE |
3252 | @@ -128,6 +128,7 @@ | |||
3253 | 128 | QSignalSpy unknownSpy(&watcher, SIGNAL(isUnknownChanged())); | 128 | QSignalSpy unknownSpy(&watcher, SIGNAL(isUnknownChanged())); |
3254 | 129 | 129 | ||
3255 | 130 | watcher.setIdentifier(identifier); | 130 | watcher.setIdentifier(identifier); |
3256 | 131 | watcher.setAddressableFields(QStringList() << "tel"); | ||
3257 | 131 | 132 | ||
3258 | 132 | // now create the contact and wait to see if it gets matched | 133 | // now create the contact and wait to see if it gets matched |
3259 | 133 | QContact contact = createContact("FirstName", | 134 | QContact contact = createContact("FirstName", |
3260 | @@ -169,6 +170,7 @@ | |||
3261 | 169 | ContactWatcher watcher; | 170 | ContactWatcher watcher; |
3262 | 170 | watcher.componentComplete(); | 171 | watcher.componentComplete(); |
3263 | 171 | watcher.setIdentifier(identifier); | 172 | watcher.setIdentifier(identifier); |
3264 | 173 | watcher.setAddressableFields(QStringList() << "tel"); | ||
3265 | 172 | 174 | ||
3266 | 173 | QSignalSpy contactIdSpy(&watcher, SIGNAL(contactIdChanged())); | 175 | QSignalSpy contactIdSpy(&watcher, SIGNAL(contactIdChanged())); |
3267 | 174 | QSignalSpy aliasSpy(&watcher, SIGNAL(aliasChanged())); | 176 | QSignalSpy aliasSpy(&watcher, SIGNAL(aliasChanged())); |
3268 | @@ -218,6 +220,7 @@ | |||
3269 | 218 | 220 | ||
3270 | 219 | // set the phone number and wait for the match to happen | 221 | // set the phone number and wait for the match to happen |
3271 | 220 | watcher.setIdentifier(identifier); | 222 | watcher.setIdentifier(identifier); |
3272 | 223 | watcher.setAddressableFields(QStringList() << "tel"); | ||
3273 | 221 | 224 | ||
3274 | 222 | // at this point we just need to make sure the contactId is correct, the other fields | 225 | // at this point we just need to make sure the contactId is correct, the other fields |
3275 | 223 | // are tested in a separate test | 226 | // are tested in a separate test |
3276 | @@ -269,6 +272,7 @@ | |||
3277 | 269 | 272 | ||
3278 | 270 | // set the phone number and wait for the match to happen | 273 | // set the phone number and wait for the match to happen |
3279 | 271 | watcher.setIdentifier(identifier); | 274 | watcher.setIdentifier(identifier); |
3280 | 275 | watcher.setAddressableFields(QStringList() << "tel"); | ||
3281 | 272 | 276 | ||
3282 | 273 | // at this point we just need to make sure the contactId is correct, the other fields | 277 | // at this point we just need to make sure the contactId is correct, the other fields |
3283 | 274 | // are tested in a separate test | 278 | // are tested in a separate test |
3284 | @@ -318,6 +322,7 @@ | |||
3285 | 318 | 322 | ||
3286 | 319 | // set the phone number and wait for the match to happen | 323 | // set the phone number and wait for the match to happen |
3287 | 320 | watcher.setIdentifier(identifier); | 324 | watcher.setIdentifier(identifier); |
3288 | 325 | watcher.setAddressableFields(QStringList() << "tel"); | ||
3289 | 321 | 326 | ||
3290 | 322 | // at this point we just need to make sure the contactId is correct, the other fields | 327 | // at this point we just need to make sure the contactId is correct, the other fields |
3291 | 323 | // are tested in a separate test | 328 | // are tested in a separate test |
3292 | @@ -396,6 +401,7 @@ | |||
3293 | 396 | 401 | ||
3294 | 397 | // set the phone number and wait for the match to happen | 402 | // set the phone number and wait for the match to happen |
3295 | 398 | watcher.setIdentifier(identifier); | 403 | watcher.setIdentifier(identifier); |
3296 | 404 | watcher.setAddressableFields(QStringList() << "tel"); | ||
3297 | 399 | 405 | ||
3298 | 400 | // component not complete yet | 406 | // component not complete yet |
3299 | 401 | QTRY_COMPARE(contactIdSpy.count(), 0); | 407 | QTRY_COMPARE(contactIdSpy.count(), 0); |
3300 | @@ -430,8 +436,8 @@ | |||
3301 | 430 | ContactWatcher watcher; | 436 | ContactWatcher watcher; |
3302 | 431 | 437 | ||
3303 | 432 | // check that addressable fields contains "tel" by default | 438 | // check that addressable fields contains "tel" by default |
3306 | 433 | QCOMPARE(watcher.addressableFields().count(), 1); | 439 | QCOMPARE(watcher.addressableFields().count(), 0); |
3307 | 434 | QCOMPARE(watcher.addressableFields()[0], QString("tel")); | 440 | QCOMPARE(watcher.addressableFields(), QStringList()); |
3308 | 435 | 441 | ||
3309 | 436 | QSignalSpy addressableFieldsSpy(&watcher, SIGNAL(addressableFieldsChanged())); | 442 | QSignalSpy addressableFieldsSpy(&watcher, SIGNAL(addressableFieldsChanged())); |
3310 | 437 | QStringList addressableFields; | 443 | QStringList addressableFields; |
3311 | @@ -442,8 +448,8 @@ | |||
3312 | 442 | 448 | ||
3313 | 443 | // set the addressable fields to an empty value and make sure it falls back to "tel" | 449 | // set the addressable fields to an empty value and make sure it falls back to "tel" |
3314 | 444 | watcher.setAddressableFields(QStringList()); | 450 | watcher.setAddressableFields(QStringList()); |
3317 | 445 | QCOMPARE(watcher.addressableFields().count(), 1); | 451 | QCOMPARE(watcher.addressableFields().count(), 0); |
3318 | 446 | QCOMPARE(watcher.addressableFields()[0], QString("tel")); | 452 | QCOMPARE(watcher.addressableFields(), QStringList()); |
3319 | 447 | } | 453 | } |
3320 | 448 | 454 | ||
3321 | 449 | void ContactWatcherTest::testExtendedFieldMatch() | 455 | void ContactWatcherTest::testExtendedFieldMatch() |
3322 | @@ -496,6 +502,7 @@ | |||
3323 | 496 | 502 | ||
3324 | 497 | // try to match contact A | 503 | // try to match contact A |
3325 | 498 | watcherA.setIdentifier(contactIdentifierA); | 504 | watcherA.setIdentifier(contactIdentifierA); |
3326 | 505 | watcherA.setAddressableFields(QStringList() << "tel"); | ||
3327 | 499 | 506 | ||
3328 | 500 | // mark as complete | 507 | // mark as complete |
3329 | 501 | watcherA.componentComplete(); | 508 | watcherA.componentComplete(); |
3330 | @@ -513,6 +520,7 @@ | |||
3331 | 513 | 520 | ||
3332 | 514 | // try to match contact B | 521 | // try to match contact B |
3333 | 515 | watcherB.setIdentifier(contactIdentifierB); | 522 | watcherB.setIdentifier(contactIdentifierB); |
3334 | 523 | watcherB.setAddressableFields(QStringList() << "tel"); | ||
3335 | 516 | 524 | ||
3336 | 517 | // signal will be fired now | 525 | // signal will be fired now |
3337 | 518 | QTRY_COMPARE(contactIdSpyB.count(), 1); | 526 | QTRY_COMPARE(contactIdSpyB.count(), 1); |