FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
tdesktop/Telegram/SourceFiles/calls/calls_instance.cpp at dev · 0Knowledge/tdesktop · GitHub
0Knowledge
/
tdesktop
Public
forked from
telegramdesktop/tdesktop
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
tdesktop
/
Telegram
/
SourceFiles
/
calls
/
calls_instance.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
290 lines (257 loc) · 8.22 KB
Breadcrumbs
tdesktop
/
Telegram
/
SourceFiles
/
calls
/
calls_instance.cpp
Copy path
File metadata and controls
290 lines (257 loc) · 8.22 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#
include
"
calls/calls_instance.h
"
#
include
"
mtproto/connection.h
"
#
include
"
core/application.h
"
#
include
"
auth_session.h
"
#
include
"
apiwrap.h
"
#
include
"
lang/lang_keys.h
"
#
include
"
boxes/confirm_box.h
"
#
include
"
calls/calls_call.h
"
#
include
"
calls/calls_panel.h
"
#
include
"
data/data_user.h
"
#
include
"
data/data_session.h
"
#
include
"
media/audio/media_audio_track.h
"
#
include
"
platform/platform_specific.h
"
#
include
"
mainwidget.h
"
#
include
"
boxes/rate_call_box.h
"
namespace
Calls
{
namespace
{
constexpr
auto
kServerConfigUpdateTimeoutMs
=
24
*
3600
*
TimeMs
(
1000
);
}
//
namespace
Instance::Instance
() =
default
;
void
Instance::startOutgoingCall
(not_null<UserData*> user) {
if
(
alreadyInCall
()) {
//
Already in a call.
_currentCallPanel->
showAndActivate
();
return
;
}
if
(user->
callsStatus
() == UserData::CallsStatus::Private) {
//
Request full user once more to refresh the setting in case it was changed.
Auth
().
api
().
requestFullPeer
(user);
Ui::show
(Box<InformBox>(
lng_call_error_not_available
(lt_user,
App::peerName
(user))));
return
;
}
requestMicrophonePermissionOrFail
(
crl::guard
(
this
, [=] {
createCall
(user, Call::Type::Outgoing);
}));
}
void
Instance::callFinished
(not_null<Call*> call) {
destroyCall
(call);
}
void
Instance::callFailed
(not_null<Call*> call) {
destroyCall
(call);
}
void
Instance::callRedial
(not_null<Call*> call) {
if
(_currentCall.
get
() == call) {
refreshDhConfig
();
}
}
void
Instance::playSound
(Sound sound) {
switch
(sound) {
case
Sound::Busy: {
if
(!_callBusyTrack) {
_callBusyTrack =
Media::Audio::Current
().
createTrack
();
_callBusyTrack->
fillFromFile
(
Auth
().
settings
().
getSoundPath
(
qsl
(
"
call_busy
"
)));
}
_callBusyTrack->
playOnce
();
}
break
;
case
Sound::Ended: {
if
(!_callEndedTrack) {
_callEndedTrack =
Media::Audio::Current
().
createTrack
();
_callEndedTrack->
fillFromFile
(
Auth
().
settings
().
getSoundPath
(
qsl
(
"
call_end
"
)));
}
_callEndedTrack->
playOnce
();
}
break
;
case
Sound::Connecting: {
if
(!_callConnectingTrack) {
_callConnectingTrack =
Media::Audio::Current
().
createTrack
();
_callConnectingTrack->
fillFromFile
(
Auth
().
settings
().
getSoundPath
(
qsl
(
"
call_connect
"
)));
}
_callConnectingTrack->
playOnce
();
}
break
;
}
}
void
Instance::destroyCall
(not_null<Call*> call) {
if
(_currentCall.
get
() == call) {
destroyCurrentPanel
();
_currentCall.
reset
();
_currentCallChanged.
notify
(
nullptr
,
true
);
if
(
App::quitting
()) {
LOG
((
"
Calls::Instance doesn't prevent quit any more.
"
));
}
Core::App
().
quitPreventFinished
();
}
}
void
Instance::destroyCurrentPanel
() {
_pendingPanels.
erase
(
std::remove_if
(_pendingPanels.
begin
(), _pendingPanels.
end
(), [](
auto
&&panel) {
return
!panel;
}), _pendingPanels.
end
());
_pendingPanels.
push_back
(_currentCallPanel.
release
());
_pendingPanels.
back
()->
hideAndDestroy
();
//
Always queues the destruction.
}
void
Instance::createCall
(not_null<UserData*> user, Call::Type type) {
auto
call = std::make_unique<Call>(
getCallDelegate
(), user, type);;
if
(_currentCall) {
_currentCallPanel->
replaceCall
(call.
get
());
std::swap
(_currentCall, call);
call->
hangup
();
}
else
{
_currentCallPanel = std::make_unique<Panel>(call.
get
());
_currentCall =
std::move
(call);
}
_currentCallChanged.
notify
(_currentCall.
get
(),
true
);
refreshServerConfig
();
refreshDhConfig
();
}
void
Instance::refreshDhConfig
() {
Expects
(_currentCall !=
nullptr
);
request
(
MTPmessages_GetDhConfig
(
MTP_int
(_dhConfig.
version
),
MTP_int
(
MTP
::ModExpFirst::
kRandomPowerSize
)
)).
done
([
this
, call =
base::make_weak
(_currentCall)](
const
MTPmessages_DhConfig &result) {
auto
random =
bytes::const_span
();
switch
(result.
type
()) {
case
mtpc_messages_dhConfig: {
auto
&config = result.
c_messages_dhConfig
();
if
(!
MTP::IsPrimeAndGood
(
bytes::make_span
(config.
vp
.
v
), config.
vg
.
v
)) {
LOG
((
"
API Error: bad p/g received in dhConfig.
"
));
callFailed
(call.
get
());
return
;
}
_dhConfig.
g
= config.
vg
.
v
;
_dhConfig.
p
=
bytes::make_vector
(config.
vp
.
v
);
random =
bytes::make_span
(config.
vrandom
.
v
);
}
break
;
case
mtpc_messages_dhConfigNotModified: {
auto
&config = result.
c_messages_dhConfigNotModified
();
random =
bytes::make_span
(config.
vrandom
.
v
);
if
(!_dhConfig.
g
|| _dhConfig.
p
.
empty
()) {
LOG
((
"
API Error: dhConfigNotModified on zero version.
"
));
callFailed
(call.
get
());
return
;
}
}
break
;
default
:
Unexpected
(
"
Type in messages.getDhConfig
"
);
}
if
(random.
size
() !=
MTP
::ModExpFirst::
kRandomPowerSize
) {
LOG
((
"
API Error: dhConfig random bytes wrong size: %1
"
).
arg
(random.
size
()));
callFailed
(call.
get
());
return
;
}
if
(call) {
call->
start
(random);
}
}).
fail
([
this
, call =
base::make_weak
(_currentCall)](
const
RPCError &error) {
if
(!call) {
DEBUG_LOG
((
"
API Warning: call was destroyed before got dhConfig.
"
));
return
;
}
callFailed
(call.
get
());
}).
send
();
}
void
Instance::refreshServerConfig
() {
if
(_serverConfigRequestId) {
return
;
}
if
(_lastServerConfigUpdateTime && (
getms
(
true
) - _lastServerConfigUpdateTime) <
kServerConfigUpdateTimeoutMs
) {
return
;
}
_serverConfigRequestId =
request
(
MTPphone_GetCallConfig
()).
done
([
this
](
const
MTPDataJSON &result) {
_serverConfigRequestId =
0
;
_lastServerConfigUpdateTime =
getms
(
true
);
const
auto
&json = result.
c_dataJSON
().
vdata
.
v
;
UpdateConfig
(
std::string
(json.
data
(), json.
size
()));
}).
fail
([
this
](
const
RPCError &error) {
_serverConfigRequestId =
0
;
}).
send
();
}
void
Instance::handleUpdate
(
const
MTPDupdatePhoneCall& update) {
handleCallUpdate
(update.
vphone_call
);
}
void
Instance::showInfoPanel
(not_null<Call*> call) {
if
(_currentCall.
get
() == call) {
_currentCallPanel->
showAndActivate
();
}
}
bool
Instance::isQuitPrevent
() {
if
(!_currentCall || _currentCall->
isIncomingWaiting
()) {
return
false
;
}
_currentCall->
hangup
();
if
(!_currentCall) {
return
false
;
}
LOG
((
"
Calls::Instance prevents quit, saving drafts...
"
));
return
true
;
}
void
Instance::handleCallUpdate
(
const
MTPPhoneCall &call) {
if
(call.
type
() == mtpc_phoneCallRequested) {
auto
&phoneCall = call.
c_phoneCallRequested
();
auto
user =
Auth
().
data
().
userLoaded
(phoneCall.
vadmin_id
.
v
);
if
(!user) {
LOG
((
"
API Error: User not loaded for phoneCallRequested.
"
));
}
else
if
(user->
isSelf
()) {
LOG
((
"
API Error: Self found in phoneCallRequested.
"
));
}
if
(
alreadyInCall
() || !user || user->
isSelf
()) {
request
(
MTPphone_DiscardCall
(
MTP_inputPhoneCall
(phoneCall.
vid
, phoneCall.
vaccess_hash
),
MTP_int
(
0
),
MTP_phoneCallDiscardReasonBusy
(),
MTP_long
(
0
))).
send
();
}
else
if
(phoneCall.
vdate
.
v
+ (
Global::CallRingTimeoutMs
() /
1000
) <
unixtime
()) {
LOG
((
"
Ignoring too old call.
"
));
}
else
{
createCall
(user, Call::Type::Incoming);
_currentCall->
handleUpdate
(call);
}
}
else
if
(!_currentCall || !_currentCall->
handleUpdate
(call)) {
DEBUG_LOG
((
"
API Warning: unexpected phone call update %1
"
).
arg
(call.
type
()));
}
}
bool
Instance::alreadyInCall
() {
return
(_currentCall && _currentCall->
state
() != Call::State::Busy);
}
Call *
Instance::currentCall
() {
return
_currentCall.
get
();
}
void
Instance::requestMicrophonePermissionOrFail
(Fn<
void
()> onSuccess) {
Platform::PermissionStatus status=
Platform::GetPermissionStatus
(Platform::PermissionType::Microphone);
if
(status==Platform::PermissionStatus::Granted) {
onSuccess
();
}
else
if
(status==Platform::PermissionStatus::CanRequest) {
Platform::RequestPermission
(Platform::PermissionType::Microphone,
crl::guard
(
this
, [=](Platform::PermissionStatus status) {
if
(status==Platform::PermissionStatus::Granted) {
crl::on_main
(onSuccess);
}
else
{
if
(_currentCall) {
_currentCall->
hangup
();
}
}
}));
}
else
{
if
(
alreadyInCall
()) {
_currentCall->
hangup
();
}
Ui::show
(Box<ConfirmBox>(
lang
(lng_no_mic_permission),
lang
(lng_menu_settings),
crl::guard
(
this
, [] {
Platform::OpenSystemSettingsForPermission
(Platform::PermissionType::Microphone);
Ui::hideLayer
();
})));
}
}
Instance::~Instance
() {
for
(
auto
panel : _pendingPanels) {
if
(panel) {
delete
panel;
}
}
}
Instance &
Current
() {
return
Auth
().
calls
();
}
}
//
namespace Calls
Back
|
FazBrowse Home
|
New Git URL