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
|
/*
(c) Copyright 2012 - 2018 Anton Sviridenko
https://picapica.im
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, version 3.
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, see <https://www.gnu.org/licenses/>.
*/
#include "audiovideocallcontroller.h"
#include "globals.h"
#include "skynet.h"
#include "settings.h"
#include <QByteArray>
#include <QDebug>
#include <QMetaObject>
AudioVideoCallController::AudioVideoCallController(QObject *parent)
: QObject(parent), callwindow(0), is_active(false)
{
connect(skynet, SIGNAL(IncomingCall(QByteArray)), this, SLOT(call_from(QByteArray)));
connect(skynet, SIGNAL(CallFailed(QString)), this, SLOT(call_failed(QString)));
connect(skynet, SIGNAL(CallAccepted(QByteArray)), this, SLOT(call_accepted(QByteArray)));
connect(skynet, SIGNAL(CallRejected(QByteArray)), this, SLOT(call_rejected(QByteArray)));
connect(skynet, SIGNAL(CallHungup(QByteArray)), this, SLOT(call_hungup(QByteArray)));
// Earpiece tones (ringback/busy/unreachable) and the incoming call bell
// are played by two TonePlayer instances, each living on its own
// QThread since TonePlayer::play() blocks the calling thread for the
// duration of the tone sequence.
toneplayer = new TonePlayer();
toneplayer->moveToThread(&toneplayer_thread);
toneplayer_thread.start();
ringtoneplayer = new TonePlayer();
ringtoneplayer->moveToThread(&ringtoneplayer_thread);
ringtoneplayer_thread.start();
}
AudioVideoCallController::~AudioVideoCallController()
{
stopTones();
toneplayer_thread.quit();
toneplayer_thread.wait();
delete toneplayer;
ringtoneplayer_thread.quit();
ringtoneplayer_thread.wait();
delete ringtoneplayer;
}
void AudioVideoCallController::playEarpieceTone(void (TonePlayer::*tone)())
{
Settings st(config_dbname);
QString dev = st.loadValue("audio.playback_device", "default").toString();
toneplayer->stop();
QMetaObject::invokeMethod(toneplayer, "setDeviceName", Qt::QueuedConnection, Q_ARG(QString, dev));
if (tone == &TonePlayer::playCallInProgress)
QMetaObject::invokeMethod(toneplayer, "playCallInProgress", Qt::QueuedConnection);
else if (tone == &TonePlayer::playUnreachable)
QMetaObject::invokeMethod(toneplayer, "playUnreachable", Qt::QueuedConnection);
else if (tone == &TonePlayer::playBusy)
QMetaObject::invokeMethod(toneplayer, "playBusy", Qt::QueuedConnection);
}
void AudioVideoCallController::playRingTone()
{
Settings st(config_dbname);
QString dev = st.loadValue("audio.ring_device", "default").toString();
ringtoneplayer->stop();
QMetaObject::invokeMethod(ringtoneplayer, "setDeviceName", Qt::QueuedConnection, Q_ARG(QString, dev));
QMetaObject::invokeMethod(ringtoneplayer, "playClassicRingtone", Qt::QueuedConnection);
}
void AudioVideoCallController::stopTones()
{
toneplayer->stop();
ringtoneplayer->stop();
}
void AudioVideoCallController::call_failed(QString reason)
{
qDebug() << "Call failed: " << reason << "\n";
/* play unreachable */
playEarpieceTone(&TonePlayer::playUnreachable);
}
void AudioVideoCallController::call_accepted(QByteArray peer_id)
{
stopTones();
}
void AudioVideoCallController::call_rejected(QByteArray peer_id)
{
/* play busy tone */
playEarpieceTone(&TonePlayer::playBusy);
}
void AudioVideoCallController::call_hungup(QByteArray peer_id)
{
/* play busy tone */
playEarpieceTone(&TonePlayer::playBusy);
}
void AudioVideoCallController::start_call(QByteArray peer_id)
{
if (callwindow)
{
/* call is already in progress */
return;
}
m_peer_id = peer_id;
callwindow = new CallWindow(peer_id, false);
connect(callwindow, SIGNAL(start_call_pressed()), this, SLOT(initiate_call()));
connect(callwindow, SIGNAL(hang_call_pressed()), this, SLOT(end_call()));
callwindow->show();
/* play ringing tone */
playEarpieceTone(&TonePlayer::playCallInProgress);
}
void AudioVideoCallController::call_from(QByteArray peer_id)
{
if (callwindow)
{
/* HANDLE BUSY CASE!!! */
/* call is already in progress */
return;
}
m_peer_id = peer_id;
callwindow = new CallWindow(peer_id, true);
connect(callwindow, SIGNAL(accept_call_pressed()), this, SLOT(accept_call()));
connect(callwindow, SIGNAL(hang_call_pressed()), this, SLOT(end_call()));
callwindow->show();
/* play ring tone */
playRingTone();
}
void AudioVideoCallController::initiate_call()
{
skynet->StartCall(m_peer_id);
}
void AudioVideoCallController::accept_call()
{
stopTones();
skynet->AcceptCall(m_peer_id);
}
void AudioVideoCallController::end_call()
{
stopTones();
if (is_active)
skynet->HangupCall(m_peer_id);
else
skynet->RejectCall(m_peer_id);
}
|