forked from ubports/telepathy-ofono
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ofonocallchannel.cpp
380 lines (334 loc) · 14.2 KB
/
ofonocallchannel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/**
* Copyright (C) 2013 Canonical, Ltd.
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License version 3, as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,
* SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Authors: Tiago Salem Herrmann <[email protected]>
*/
#include "ofonocallchannel.h"
#ifdef USE_PULSEAUDIO
#include "qpulseaudioengine.h"
#endif
oFonoCallChannel::oFonoCallChannel(oFonoConnection *conn, QString phoneNumber, uint targetHandle, QString voiceObj, QObject *parent):
OfonoVoiceCall(voiceObj),
mIncoming(false),
mRequestedHangup(false),
mConnection(conn),
mPhoneNumber(phoneNumber),
mTargetHandle(targetHandle),
mDtmfLock(false),
mMultiparty(false)
{
Tp::BaseChannelPtr baseChannel = Tp::BaseChannel::create(mConnection, TP_QT_IFACE_CHANNEL_TYPE_CALL, Tp::HandleTypeContact, targetHandle);
Tp::BaseChannelCallTypePtr callType = Tp::BaseChannelCallType::create(baseChannel.data(),
true,
Tp::StreamTransportTypeUnknown,
true,
false, "","");
baseChannel->plugInterface(Tp::AbstractChannelInterfacePtr::dynamicCast(callType));
mHoldIface = Tp::BaseChannelHoldInterface::create();
mHoldIface->setSetHoldStateCallback(Tp::memFun(this,&oFonoCallChannel::onHoldStateChanged));
mMuteIface = Tp::BaseCallMuteInterface::create();
mMuteIface->setSetMuteStateCallback(Tp::memFun(this,&oFonoCallChannel::onMuteStateChanged));
mAudioOutputsIface = BaseChannelAudioOutputsInterface::create();
mAudioOutputsIface->setSetActiveAudioOutputCallback(Tp::memFun(this,&oFonoCallChannel::onSetActiveAudioOutput));
mSplittableIface = Tp::BaseChannelSplittableInterface::create();
mSplittableIface->setSplitCallback(Tp::memFun(this,&oFonoCallChannel::onSplit));
baseChannel->plugInterface(Tp::AbstractChannelInterfacePtr::dynamicCast(mHoldIface));
baseChannel->plugInterface(Tp::AbstractChannelInterfacePtr::dynamicCast(mMuteIface));
baseChannel->plugInterface(Tp::AbstractChannelInterfacePtr::dynamicCast(mAudioOutputsIface));
baseChannel->plugInterface(Tp::AbstractChannelInterfacePtr::dynamicCast(mSplittableIface));
mBaseChannel = baseChannel;
mCallChannel = Tp::BaseChannelCallTypePtr::dynamicCast(mBaseChannel->interface(TP_QT_IFACE_CHANNEL_TYPE_CALL));
mCallChannel->setHangupCallback(Tp::memFun(this,&oFonoCallChannel::onHangup));
mCallChannel->setAcceptCallback(Tp::memFun(this,&oFonoCallChannel::onAccept));
QObject::connect(this, SIGNAL(hangupComplete(bool)), this, SLOT(onHangupComplete(bool)));
QObject::connect(this, SIGNAL(answerComplete(bool)), this, SLOT(onAnswerComplete(bool)));
// init must be called after initialization, otherwise we will have no object path registered.
QTimer::singleShot(0, this, SLOT(init()));
#ifdef USE_PULSEAUDIO
QByteArray pulseAudioDisabled = qgetenv("PA_DISABLED");
mHasPulseAudio = true;
if (!pulseAudioDisabled.isEmpty())
mHasPulseAudio = false;
#endif
}
Tp::CallState oFonoCallChannel::callState()
{
return (Tp::CallState)mCallChannel->callState();
}
void oFonoCallChannel::onSplit(Tp::DBusError *error)
{
mConnection->voiceCallManager()->privateChat(path());
}
void oFonoCallChannel::onSetActiveAudioOutput(const QString &id, Tp::DBusError *error)
{
#ifdef USE_PULSEAUDIO
// fallback to earpiece/headset
AudioMode mode = AudioModeWiredOrEarpiece;
if (id == "bluetooth") {
mode = AudioModeBluetooth;
} else if (id == "speaker") {
mode = AudioModeSpeaker;
}
if (mHasPulseAudio)
QPulseAudioEngine::instance()->setCallMode(CallActive, mode);
#endif
}
void oFonoCallChannel::onHangupComplete(bool status)
{
if (!status) {
QTimer::singleShot(2000, this, SLOT(hangup()));
}
}
void oFonoCallChannel::onAnswerComplete(bool status)
{
if (!status) {
QTimer::singleShot(1000, this, SLOT(answer()));
}
}
void oFonoCallChannel::onHangup(uint reason, const QString &detailedReason, const QString &message, Tp::DBusError *error)
{
// TODO: use the parameters sent by telepathy
mRequestedHangup = true;
hangup();
}
void oFonoCallChannel::onAccept(Tp::DBusError*)
{
QVariantMap stateDetails;
Tp::CallStateReason reason;
reason.actor = 0;
reason.reason = Tp::CallStateChangeReasonUserRequested;
reason.message = "";
reason.DBusReason = "";
mCallChannel->setCallState(Tp::CallStateAccepted, 0, reason, stateDetails);
if (this->state() == "waiting") {
mConnection->voiceCallManager()->holdAndAnswer();
} else {
answer();
}
}
void oFonoCallChannel::init()
{
mIncoming = this->state() == "incoming" || this->state() == "waiting";
mMultiparty = this->multiparty();
mPreviousState = this->state();
mObjPath = mBaseChannel->objectPath();
Tp::CallMemberMap memberFlags;
Tp::HandleIdentifierMap identifiers;
QVariantMap stateDetails;
Tp::CallStateReason reason;
identifiers[mTargetHandle] = mPhoneNumber;
reason.actor = 0;
reason.reason = Tp::CallStateChangeReasonProgressMade;
reason.message = "";
reason.DBusReason = "";
if (mIncoming) {
memberFlags[mTargetHandle] = 0;
} else {
memberFlags[mTargetHandle] = Tp::CallMemberFlagRinging;
}
mCallChannel->setCallState(Tp::CallStateInitialising, 0, reason, stateDetails);
mCallContent = Tp::BaseCallContent::create(baseChannel()->dbusConnection(), baseChannel().data(), "audio", Tp::MediaStreamTypeAudio, Tp::MediaStreamDirectionNone);
mDTMFIface = Tp::BaseCallContentDTMFInterface::create();
mCallContent->plugInterface(Tp::AbstractCallContentInterfacePtr::dynamicCast(mDTMFIface));
mCallChannel->addContent(mCallContent);
mDTMFIface->setStartToneCallback(Tp::memFun(this,&oFonoCallChannel::onDTMFStartTone));
mDTMFIface->setStopToneCallback(Tp::memFun(this,&oFonoCallChannel::onDTMFStopTone));
mCallChannel->setMembersFlags(memberFlags, identifiers, Tp::UIntList(), reason);
mCallChannel->setCallState(Tp::CallStateInitialised, 0, reason, stateDetails);
QObject::connect(mBaseChannel.data(), SIGNAL(closed()), this, SLOT(deleteLater()));
QObject::connect(mConnection->callVolume(), SIGNAL(mutedChanged(bool)), SLOT(onOfonoMuteChanged(bool)));
QObject::connect(this, SIGNAL(stateChanged(QString)), SLOT(onOfonoCallStateChanged(QString)));
QObject::connect(mConnection, SIGNAL(activeAudioOutputChanged(QString)), mAudioOutputsIface.data(), SLOT(setActiveAudioOutput(QString)));
QObject::connect(mConnection, SIGNAL(audioOutputsChanged(AudioOutputList)), mAudioOutputsIface.data(), SLOT(setAudioOutputs(AudioOutputList)));
QObject::connect(mConnection->voiceCallManager(), SIGNAL(sendTonesComplete(bool)), SLOT(onDtmfComplete(bool)));
QObject::connect(this, SIGNAL(multipartyChanged(bool)), this, SLOT(onMultipartyChanged(bool)));
QObject::connect(this, SIGNAL(disconnectReason(const QString &)), this, SLOT(onDisconnectReason(const QString &)));
mAudioOutputsIface->setAudioOutputs(mConnection->audioOutputs());
mAudioOutputsIface->setActiveAudioOutput(mConnection->activeAudioOutput());
}
void oFonoCallChannel::onDisconnectReason(const QString &reason) {
mRequestedHangup = reason == "local";
}
void oFonoCallChannel::onMultipartyChanged(bool multiparty)
{
// if previous state was multparty then split
if (multiparty) {
Q_EMIT merged();
} else {
Q_EMIT splitted();
}
mMultiparty = multiparty;
}
void oFonoCallChannel::onOfonoMuteChanged(bool mute)
{
Tp::LocalMuteState state = mute ? Tp::LocalMuteStateMuted : Tp::LocalMuteStateUnmuted;
mMuteIface->setMuteState(state);
}
void oFonoCallChannel::onHoldStateChanged(const Tp::LocalHoldState &state, const Tp::LocalHoldStateReason &reason, Tp::DBusError *error)
{
if (state == Tp::LocalHoldStateHeld && this->state() == "active") {
QObject::connect(mConnection->voiceCallManager(), SIGNAL(swapCallsComplete(bool)), this, SLOT(onSwapCallsComplete(bool)));
mConnection->voiceCallManager()->swapCalls();
mHoldIface->setHoldState(Tp::LocalHoldStatePendingHold, Tp::LocalHoldStateReasonRequested);
} else if (state == Tp::LocalHoldStateUnheld && this->state() == "held") {
QObject::connect(mConnection->voiceCallManager(), SIGNAL(swapCallsComplete(bool)), this, SLOT(onSwapCallsComplete(bool)));
mConnection->voiceCallManager()->swapCalls();
mHoldIface->setHoldState(Tp::LocalHoldStatePendingUnhold, Tp::LocalHoldStateReasonRequested);
}
}
void oFonoCallChannel::onSwapCallsComplete(bool success)
{
if (!success && errorName() == "org.ofono.Error.InProgress") {
QTimer::singleShot(2000, mConnection->voiceCallManager(), SLOT(swapCalls()));
return;
}
Tp::LocalHoldState holdState = this->state() == "active" ? Tp::LocalHoldStateUnheld : Tp::LocalHoldStateHeld;
Tp::LocalHoldStateReason reason = success ? Tp::LocalHoldStateReasonRequested : Tp::LocalHoldStateReasonResourceNotAvailable;
QObject::disconnect(mConnection->voiceCallManager(), SIGNAL(swapCallsComplete(bool)), this, SLOT(onSwapCallsComplete(bool)));
mHoldIface->setHoldState(holdState, reason);
}
void oFonoCallChannel::onMuteStateChanged(const Tp::LocalMuteState &state, Tp::DBusError *error)
{
if (state == Tp::LocalMuteStateMuted) {
mConnection->callVolume()->setMuted(true);
#ifdef USE_PULSEAUDIO
if (mHasPulseAudio)
QPulseAudioEngine::instance()->setMicMute(true);
#endif
} else if (state == Tp::LocalMuteStateUnmuted) {
mConnection->callVolume()->setMuted(false);
#ifdef USE_PULSEAUDIO
if (mHasPulseAudio)
QPulseAudioEngine::instance()->setMicMute(false);
#endif
}
}
void oFonoCallChannel::sendNextDtmf()
{
if (mDtmfLock) {
return;
}
if (!mDtmfPendingStrings.isEmpty()) {
mDtmfLock = true;
mConnection->voiceCallManager()->sendTones(mDtmfPendingStrings.front());
}
}
void oFonoCallChannel::onDtmfComplete(bool success)
{
// this might be a response for another channel
if (mCallChannel->callState() != Tp::CallStateActive) {
return;
}
mDtmfLock = false;
if (success) {
if (mDtmfPendingStrings.count() > 0) {
mDtmfPendingStrings.removeFirst();
}
if (mDtmfPendingStrings.isEmpty()) {
return;
}
sendNextDtmf();
} else {
QTimer::singleShot(1000, this, SLOT(sendNextDtmf()));
}
}
void oFonoCallChannel::onDTMFStartTone(uchar event, Tp::DBusError *error)
{
QString finalString;
if (event == 10) {
finalString = "*";
} else if (event == 11) {
finalString = "#";
} else {
finalString = QString::number(event);
}
qDebug() << "start tone" << finalString;
// we can't append to the first item in the queue as it is being sent and
// we dont know yet if it will succeed or not.
if (mDtmfPendingStrings.count() > 1) {
mDtmfPendingStrings[1] += finalString;
} else {
mDtmfPendingStrings << finalString;
}
sendNextDtmf();
}
void oFonoCallChannel::onDTMFStopTone(Tp::DBusError *error)
{
}
oFonoCallChannel::~oFonoCallChannel()
{
qDebug() << "call channel closed";
// TODO - for some reason the object is not being removed
mConnection->dbusConnection().unregisterObject(mObjPath, QDBusConnection::UnregisterTree);
}
Tp::BaseChannelPtr oFonoCallChannel::baseChannel()
{
return mBaseChannel;
}
void oFonoCallChannel::onOfonoCallStateChanged(const QString &state)
{
Tp::CallStateReason reason;
QVariantMap stateDetails;
reason.actor = 0;
reason.reason = Tp::CallStateChangeReasonUserRequested;
reason.message = "";
reason.DBusReason = "";
// we invalidate the pending dtmf strings if the call status is changed
mDtmfPendingStrings.clear();
mDtmfLock = false;
if (state == "disconnected") {
qDebug() << "disconnected";
if (mIncoming && (mPreviousState == "incoming" || mPreviousState == "waiting") && !mRequestedHangup) {
reason.reason = Tp::CallStateChangeReasonNoAnswer;
}
mCallChannel->setCallState(Tp::CallStateEnded, 0, reason, stateDetails);
// just in case, leave the channel opened for one more second before unregistering from bus
QThread::msleep(1000);
Q_EMIT closed();
mBaseChannel->close();
} else if (state == "active") {
qDebug() << "active";
mHoldIface->setHoldState(Tp::LocalHoldStateUnheld, Tp::LocalHoldStateReasonNone);
if (mMultiparty) {
Q_EMIT multipartyCallActive();
}
if (mPreviousState == "incoming") {
mConnection->updateAudioRouteToEarpiece();
}
if (mPreviousState == "dialing" || mPreviousState == "alerting" ||
mPreviousState == "incoming") {
mConnection->callVolume()->setMuted(false);
mCallChannel->setCallState(Tp::CallStateAccepted, 0, reason, stateDetails);
}
mCallChannel->setCallState(Tp::CallStateActive, 0, reason, stateDetails);
} else if (state == "held") {
mHoldIface->setHoldState(Tp::LocalHoldStateHeld, Tp::LocalHoldStateReasonNone);
if (mMultiparty) {
Q_EMIT multipartyCallHeld();
}
qDebug() << "held";
} else if (state == "dialing") {
qDebug() << "dialing";
} else if (state == "alerting") {
qDebug() << "alerting";
} else if (state == "incoming") {
qDebug() << "incoming";
} else if (state == "waiting") {
qDebug() << "waiting";
}
// always update the audio route when call state changes
mConnection->updateAudioRoute();
mPreviousState = state;
}