-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathcall.cpp
687 lines (602 loc) · 22 KB
/
call.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
/*
* Copyright (C) 2004-2021 Savoir-faire Linux Inc.
*
* Author: Yan Morin <yan.morin@savoirfairelinux.com>
* Author: Laurielle Lea <laurielle.lea@savoirfairelinux.com>
* Author: Guillaume Roguez <guillaume.roguez@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "call.h"
#include "account.h"
#include "manager.h"
#ifdef ENABLE_PLUGIN
#include "plugin/jamipluginmanager.h"
#include "plugin/streamdata.h"
#endif
#include "audio/ringbufferpool.h"
#include "dring/call_const.h"
#include "client/ring_signal.h"
#include "sip/sip_utils.h"
#include "ip_utils.h"
#include "array_size.h"
#include "map_utils.h"
#include "call_factory.h"
#include "string_utils.h"
#include "enumclass_utils.h"
#include "errno.h"
#include <opendht/thread_pool.h>
#include <stdexcept>
#include <system_error>
#include <algorithm>
#include <functional>
#include <utility>
namespace jami {
/// Hangup many calls with same error code, filtered by a predicate
///
/// For each call pointer given by iterating on given \a callptr_list
/// calls the unary predicate \a pred with this call pointer and hangup the call with given error
/// code \a errcode when the predicate return true.
/// The predicate should have <code>bool(Call*) signature</code>.
template<typename T>
inline void
hangupCallsIf(Call::SubcallSet&& calls,
int errcode,
T pred)
{
for (auto& call : calls) {
if (not pred(call.get()))
continue;
dht::ThreadPool::io().run([call = std::move(call), errcode] {
call->hangup(errcode);
});
}
}
/// Hangup many calls with same error code.
///
/// Works as hangupCallsIf() with a predicate that always return true.
inline void
hangupCalls(Call::SubcallSet&& callptr_list, int errcode)
{
hangupCallsIf(std::move(callptr_list), errcode, [](Call*) { return true; });
}
//==============================================================================
Call::Call(const std::shared_ptr<Account>& account,
const std::string& id,
Call::CallType type,
const std::map<std::string, std::string>& details)
: id_(id)
, type_(type)
, account_(account)
{
updateDetails(details);
addStateListener(
[this](Call::CallState call_state, Call::ConnectionState cnx_state, UNUSED int code) {
checkPendingIM();
std::weak_ptr<Call> callWkPtr = shared_from_this();
runOnMainThread([callWkPtr] {
if (auto call = callWkPtr.lock())
call->checkAudio();
});
// if call just started ringing, schedule call timeout
if (type_ == CallType::INCOMING and cnx_state == ConnectionState::RINGING) {
auto timeout = Manager::instance().getRingingTimeout();
JAMI_DBG("Scheduling call timeout in %d seconds", timeout);
std::weak_ptr<Call> callWkPtr = shared_from_this();
Manager::instance().scheduler().scheduleIn(
[callWkPtr] {
if (auto callShPtr = callWkPtr.lock()) {
if (callShPtr->getConnectionState() == Call::ConnectionState::RINGING) {
JAMI_DBG(
"Call %s is still ringing after timeout, setting state to BUSY",
callShPtr->getCallId().c_str());
callShPtr->hangup(PJSIP_SC_BUSY_HERE);
Manager::instance().callFailure(*callShPtr);
}
}
},
std::chrono::seconds(timeout));
}
// kill pending subcalls at disconnect
if (call_state == CallState::OVER)
hangupCalls(safePopSubcalls(), 0);
});
time(×tamp_start_);
if (auto shared = account_.lock())
shared->attachCall(id_);
}
Call::~Call()
{
if (auto shared = account_.lock())
shared->detachCall(id_);
}
void
Call::removeCall()
{
auto this_ = shared_from_this();
Manager::instance().callFactory.removeCall(*this);
setState(CallState::OVER);
if (Recordable::isRecording())
Recordable::stopRecording();
}
std::string
Call::getAccountId() const
{
if (auto shared = account_.lock())
return shared->getAccountID();
JAMI_ERR("No account detected");
return {};
}
Call::ConnectionState
Call::getConnectionState() const
{
std::lock_guard<std::recursive_mutex> lock(callMutex_);
return connectionState_;
}
Call::CallState
Call::getState() const
{
std::lock_guard<std::recursive_mutex> lock(callMutex_);
return callState_;
}
bool
Call::validStateTransition(CallState newState)
{
// Notice to developper:
// - list only permitted transition (return true)
// - let non permitted ones as default case (return false)
// always permited
if (newState == CallState::OVER)
return true;
switch (callState_) {
case CallState::INACTIVE:
switch (newState) {
case CallState::ACTIVE:
case CallState::BUSY:
case CallState::PEER_BUSY:
case CallState::MERROR:
return true;
default: // INACTIVE, HOLD
return false;
}
case CallState::ACTIVE:
switch (newState) {
case CallState::BUSY:
case CallState::PEER_BUSY:
case CallState::HOLD:
case CallState::MERROR:
return true;
default: // INACTIVE, ACTIVE
return false;
}
case CallState::HOLD:
switch (newState) {
case CallState::ACTIVE:
case CallState::MERROR:
return true;
default: // INACTIVE, HOLD, BUSY, PEER_BUSY, MERROR
return false;
}
case CallState::BUSY:
switch (newState) {
case CallState::MERROR:
return true;
default: // INACTIVE, ACTIVE, HOLD, BUSY, PEER_BUSY
return false;
}
default: // MERROR
return false;
}
}
bool
Call::setState(CallState call_state, ConnectionState cnx_state, signed code)
{
std::lock_guard<std::recursive_mutex> lock(callMutex_);
JAMI_DBG("[call:%s] state change %u/%u, cnx %u/%u, code %d",
id_.c_str(),
(unsigned) callState_,
(unsigned) call_state,
(unsigned) connectionState_,
(unsigned) cnx_state,
code);
if (callState_ != call_state) {
if (not validStateTransition(call_state)) {
JAMI_ERR("[call:%s] invalid call state transition from %u to %u",
id_.c_str(),
(unsigned) callState_,
(unsigned) call_state);
return false;
}
} else if (connectionState_ == cnx_state)
return true; // no changes as no-op
// Emit client state only if changed
auto old_client_state = getStateStr();
callState_ = call_state;
connectionState_ = cnx_state;
auto new_client_state = getStateStr();
for (auto& l : stateChangedListeners_)
l(callState_, connectionState_, code);
if (old_client_state != new_client_state) {
if (not parent_) {
JAMI_DBG("[call:%s] emit client call state change %s, code %d",
id_.c_str(),
new_client_state.c_str(),
code);
emitSignal<DRing::CallSignal::StateChange>(id_, new_client_state, code);
}
}
return true;
}
bool
Call::setState(CallState call_state, signed code)
{
std::lock_guard<std::recursive_mutex> lock(callMutex_);
return setState(call_state, connectionState_, code);
}
bool
Call::setState(ConnectionState cnx_state, signed code)
{
std::lock_guard<std::recursive_mutex> lock(callMutex_);
return setState(callState_, cnx_state, code);
}
std::string
Call::getStateStr() const
{
using namespace DRing::Call;
switch (getState()) {
case CallState::ACTIVE:
switch (getConnectionState()) {
case ConnectionState::PROGRESSING:
return StateEvent::CONNECTING;
case ConnectionState::RINGING:
return isIncoming() ? StateEvent::INCOMING : StateEvent::RINGING;
case ConnectionState::DISCONNECTED:
return StateEvent::HUNGUP;
case ConnectionState::CONNECTED:
default:
return StateEvent::CURRENT;
}
case CallState::HOLD:
if (getConnectionState() == ConnectionState::DISCONNECTED)
return StateEvent::HUNGUP;
return StateEvent::HOLD;
case CallState::BUSY:
return StateEvent::BUSY;
case CallState::PEER_BUSY:
return StateEvent::PEER_BUSY;
case CallState::INACTIVE:
switch (getConnectionState()) {
case ConnectionState::PROGRESSING:
return StateEvent::CONNECTING;
case ConnectionState::RINGING:
return isIncoming() ? StateEvent::INCOMING : StateEvent::RINGING;
case ConnectionState::CONNECTED:
return StateEvent::CURRENT;
default:
return StateEvent::INACTIVE;
}
case CallState::OVER:
return StateEvent::OVER;
case CallState::MERROR:
default:
return StateEvent::FAILURE;
}
}
bool
Call::toggleRecording()
{
const bool startRecording = Recordable::toggleRecording();
return startRecording;
}
void
Call::updateDetails(const std::map<std::string, std::string>& details)
{
const auto& iter = details.find(DRing::Call::Details::AUDIO_ONLY);
if (iter != std::end(details))
isAudioOnly_ = iter->second == TRUE_STR;
}
std::map<std::string, std::string>
Call::getDetails() const
{
return {
{DRing::Call::Details::CALL_TYPE, std::to_string((unsigned) type_)},
{DRing::Call::Details::PEER_NUMBER, peerNumber_},
{DRing::Call::Details::DISPLAY_NAME, peerDisplayName_},
{DRing::Call::Details::CALL_STATE, getStateStr()},
{DRing::Call::Details::CONF_ID, confID_},
{DRing::Call::Details::TIMESTAMP_START, std::to_string(timestamp_start_)},
{DRing::Call::Details::ACCOUNTID, getAccountId()},
{DRing::Call::Details::AUDIO_MUTED, std::string(bool_to_str(isAudioMuted_))},
{DRing::Call::Details::VIDEO_MUTED, std::string(bool_to_str(isVideoMuted_))},
{DRing::Call::Details::AUDIO_ONLY, std::string(bool_to_str(isAudioOnly_))},
};
}
std::map<std::string, std::string>
Call::getNullDetails()
{
return {
{DRing::Call::Details::CALL_TYPE, "0"},
{DRing::Call::Details::PEER_NUMBER, ""},
{DRing::Call::Details::DISPLAY_NAME, ""},
{DRing::Call::Details::CALL_STATE, "UNKNOWN"},
{DRing::Call::Details::CONF_ID, ""},
{DRing::Call::Details::TIMESTAMP_START, ""},
{DRing::Call::Details::ACCOUNTID, ""},
{DRing::Call::Details::VIDEO_SOURCE, "UNKNOWN"},
{DRing::Call::Details::AUDIO_ONLY, ""},
};
}
void
Call::onTextMessage(std::map<std::string, std::string>&& messages)
{
auto it = messages.find("application/confInfo+json");
if (it != messages.end()) {
setConferenceInfo(it->second);
return;
}
it = messages.find("application/confOrder+json");
if (it != messages.end()) {
if (auto conf = Manager::instance().getConferenceFromID(confID_))
conf->onConfOrder(getCallId(), it->second);
return;
}
{
std::lock_guard<std::recursive_mutex> lk {callMutex_};
if (parent_) {
pendingInMessages_.emplace_back(std::move(messages), "");
return;
}
}
#ifdef ENABLE_PLUGIN
auto& pluginChatManager
= jami::Manager::instance().getJamiPluginManager().getChatServicesManager();
std::shared_ptr<JamiMessage> cm = std::make_shared<JamiMessage>(
getAccountId(), getPeerNumber(), true, const_cast<std::map<std::string, std::string>&>(messages), false);
pluginChatManager.publishMessage(cm);
#endif
Manager::instance().incomingMessage(getCallId(), getPeerNumber(), messages);
}
void
Call::peerHungup()
{
const auto state = getState();
const auto aborted = state == CallState::ACTIVE or state == CallState::HOLD;
setState(ConnectionState::DISCONNECTED, aborted ? ECONNABORTED : ECONNREFUSED);
}
void
Call::addSubCall(Call& subcall)
{
std::lock_guard<std::recursive_mutex> lk {callMutex_};
// Add subCall only if call is not connected or terminated
// Because we only want to addSubCall if the peer didn't answer
// So till it's <= RINGING
if (connectionState_ == ConnectionState::CONNECTED
|| connectionState_ == ConnectionState::DISCONNECTED || callState_ == CallState::OVER) {
subcall.removeCall();
return;
}
if (not subcalls_.emplace(getPtr(subcall)).second) {
JAMI_ERR("[call:%s] add twice subcall %s", getCallId().c_str(), subcall.getCallId().c_str());
return;
}
JAMI_DBG("[call:%s] add subcall %s", getCallId().c_str(), subcall.getCallId().c_str());
subcall.parent_ = getPtr(*this);
for (const auto& msg : pendingOutMessages_)
subcall.sendTextMessage(msg.first, msg.second);
subcall.addStateListener(
[sub = subcall.weak(),
parent = weak()](Call::CallState new_state, Call::ConnectionState new_cstate, int code) {
runOnMainThread([sub, parent, new_state, new_cstate, code]() {
if (auto p = parent.lock()) {
if (auto s = sub.lock()) {
p->subcallStateChanged(*s, new_state, new_cstate);
}
}
});
});
}
/// Called by a subcall when its states change (multidevice)
///
/// Its purpose is to manage per device call and try to found the first responding.
/// Parent call states are managed by these subcalls.
/// \note this method may decrease the given \a subcall ref count.
void
Call::subcallStateChanged(Call& subcall, Call::CallState new_state, Call::ConnectionState new_cstate)
{
{
// This condition happens when a subcall hangups/fails after removed from parent's list.
// This is normal to keep parent_ != nullptr on the subcall, as it's the way to flag it
// as an subcall and not a master call.
// XXX: having a way to unsubscribe the state listener could be better than such test
std::lock_guard<std::recursive_mutex> lk {callMutex_};
auto sit = subcalls_.find(getPtr(subcall));
if (sit == subcalls_.end())
return;
}
// We found a responding device: hangup all other subcalls and merge
if (new_state == CallState::ACTIVE and new_cstate == ConnectionState::CONNECTED) {
JAMI_DBG("[call:%s] subcall %s answered by peer",
getCallId().c_str(),
subcall.getCallId().c_str());
hangupCallsIf(safePopSubcalls(), 0, [&](const Call* call) { return call != &subcall; });
merge(subcall);
Manager::instance().peerAnsweredCall(*this);
return;
}
// Hangup the call if any device hangup
// XXX: not sure it's what we really want
if (new_state == CallState::ACTIVE and new_cstate == ConnectionState::DISCONNECTED) {
JAMI_WARN("[call:%s] subcall %s hangup by peer",
getCallId().c_str(),
subcall.getCallId().c_str());
hangupCalls(safePopSubcalls(), 0);
Manager::instance().peerHungupCall(*this);
removeCall();
return;
}
// Subcall is busy or failed
if (new_state >= CallState::BUSY) {
if (new_state == CallState::BUSY || new_state == CallState::PEER_BUSY)
JAMI_WARN("[call:%s] subcall %s busy", getCallId().c_str(), subcall.getCallId().c_str());
else
JAMI_WARN("[call:%s] subcall %s failed",
getCallId().c_str(),
subcall.getCallId().c_str());
std::lock_guard<std::recursive_mutex> lk {callMutex_};
subcalls_.erase(getPtr(subcall));
// Parent call fails if last subcall is busy or failed
if (subcalls_.empty()) {
if (new_state == CallState::BUSY) {
setState(CallState::BUSY,
ConnectionState::DISCONNECTED,
static_cast<int>(std::errc::device_or_resource_busy));
} else if (new_state == CallState::PEER_BUSY) {
setState(CallState::PEER_BUSY,
ConnectionState::DISCONNECTED,
static_cast<int>(std::errc::device_or_resource_busy));
} else {
// XXX: first idea was to use std::errc::host_unreachable, but it's not available on
// some platforms like mingw.
setState(CallState::MERROR,
ConnectionState::DISCONNECTED,
static_cast<int>(std::errc::io_error));
}
removeCall();
} else {
JAMI_DBG("[call:%s] remains %zu subcall(s)", getCallId().c_str(), subcalls_.size());
}
return;
}
// Copy call/cnx states (forward only)
if (new_state == CallState::ACTIVE && callState_ == CallState::INACTIVE) {
setState(new_state);
}
if (static_cast<unsigned>(connectionState_) < static_cast<unsigned>(new_cstate)
and static_cast<unsigned>(new_cstate) <= static_cast<unsigned>(ConnectionState::RINGING)) {
setState(new_cstate);
}
}
/// Replace current call data with ones from the given \a subcall.
/// Must be called while locked by subclass
void
Call::merge(Call& subcall)
{
JAMI_DBG("[call:%s] merge subcall %s", getCallId().c_str(), subcall.getCallId().c_str());
// Merge data
pendingInMessages_ = std::move(subcall.pendingInMessages_);
if (peerNumber_.empty())
peerNumber_ = std::move(subcall.peerNumber_);
peerDisplayName_ = std::move(subcall.peerDisplayName_);
setState(subcall.getState(), subcall.getConnectionState());
std::weak_ptr<Call> subCallWeak = subcall.shared_from_this();
runOnMainThread([subCallWeak] {
if (auto subcall = subCallWeak.lock())
subcall->removeCall();
});
}
/// Handle pending IM message
///
/// Used in multi-device context to send pending IM when the master call is connected.
void
Call::checkPendingIM()
{
std::lock_guard<std::recursive_mutex> lk {callMutex_};
auto state = getStateStr();
// Let parent call handles IM after the merge
if (not parent_) {
if (state == DRing::Call::StateEvent::CURRENT) {
for (const auto& msg : pendingInMessages_)
Manager::instance().incomingMessage(getCallId(), getPeerNumber(), msg.first);
pendingInMessages_.clear();
std::weak_ptr<Call> callWkPtr = shared_from_this();
runOnMainThread([callWkPtr, pending = std::move(pendingOutMessages_)] {
if (auto call = callWkPtr.lock())
for (const auto& msg : pending)
call->sendTextMessage(msg.first, msg.second);
});
}
}
}
/// Handle tones for RINGING and BUSY calls
///
void
Call::checkAudio()
{
using namespace DRing::Call;
auto state = getStateStr();
if (state == StateEvent::RINGING) {
Manager::instance().peerRingingCall(*this);
} else if (state == StateEvent::BUSY) {
Manager::instance().callBusy(*this);
}
}
// Helper to safely pop subcalls list
Call::SubcallSet
Call::safePopSubcalls()
{
std::lock_guard<std::recursive_mutex> lk {callMutex_};
// std::exchange is C++14
auto old_value = std::move(subcalls_);
subcalls_.clear();
return old_value;
}
void
Call::setConferenceInfo(const std::string& msg)
{
ConfInfo newInfo;
Json::Value json;
std::string err;
Json::CharReaderBuilder rbuilder;
auto reader = std::unique_ptr<Json::CharReader>(rbuilder.newCharReader());
if (reader->parse(msg.data(), msg.data() + msg.size(), &json, &err)) {
for (const auto& participantInfo : json) {
ParticipantInfo pInfo;
if (!participantInfo.isMember("uri"))
continue;
pInfo.fromJson(participantInfo);
newInfo.emplace_back(pInfo);
}
}
{
std::lock_guard<std::mutex> lk(confInfoMutex_);
if (confID_.empty()) {
// confID_ empty -> participant set confInfo with the received one
confInfo_ = std::move(newInfo);
// Inform client that layout has changed
jami::emitSignal<DRing::CallSignal::OnConferenceInfosUpdated>(id_, confInfo_.toVectorMapStringString());
} else {
// confID_ not empty -> host merge confInfo with the received confInfo
for (auto& newI : newInfo) {
bool isNewParticipant = true;
for (auto& oldI : confInfo_) {
if (newI.uri == oldI.uri) {
oldI = newI;
isNewParticipant = false;
break;
}
}
if (isNewParticipant) {
// ParticipantInfo not present in confInfo -> the sender of newInfo ...
// is currently hosting another conference. Add the unknown participant ...
// to the confInfo
confInfo_.emplace_back(newI);
}
}
if (auto conf = Manager::instance().getConferenceFromID(confID_))
conf->updateConferenceInfo(confInfo_);
}
}
}
} // namespace jami