File: data_group_call.h

package info (click to toggle)
telegram-desktop 4.6.5%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 53,300 kB
  • sloc: cpp: 605,857; python: 3,978; ansic: 1,636; sh: 965; makefile: 841; objc: 652; javascript: 187; xml: 165
file content (254 lines) | stat: -rw-r--r-- 7,501 bytes parent folder | download | duplicates (3)
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
/*
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
*/
#pragma once

#include "base/timer.h"

class PeerData;

class ApiWrap;

namespace Calls {
struct ParticipantVideoParams;
} // namespace Calls

namespace Data {

[[nodiscard]] const std::string &RtmpEndpointId();

struct LastSpokeTimes {
	crl::time anything = 0;
	crl::time voice = 0;
};

struct GroupCallParticipant {
	not_null<PeerData*> peer;
	std::shared_ptr<Calls::ParticipantVideoParams> videoParams;
	TimeId date = 0;
	TimeId lastActive = 0;
	uint64 raisedHandRating = 0;
	uint32 ssrc = 0;
	int volume = 0;
	bool sounding : 1 = false;
	bool speaking : 1 = false;
	bool additionalSounding : 1 = false;
	bool additionalSpeaking : 1 = false;
	bool muted : 1 = false;
	bool mutedByMe : 1 = false;
	bool canSelfUnmute : 1 = false;
	bool onlyMinLoaded : 1 = false;
	bool videoJoined = false;
	bool applyVolumeFromMin = true;

	[[nodiscard]] const std::string &cameraEndpoint() const;
	[[nodiscard]] const std::string &screenEndpoint() const;
	[[nodiscard]] bool cameraPaused() const;
	[[nodiscard]] bool screenPaused() const;
};

class GroupCall final {
public:
	GroupCall(
		not_null<PeerData*> peer,
		CallId id,
		CallId accessHash,
		TimeId scheduleDate,
		bool rtmp);
	~GroupCall();

	[[nodiscard]] CallId id() const;
	[[nodiscard]] bool loaded() const;
	[[nodiscard]] bool rtmp() const;
	[[nodiscard]] bool listenersHidden() const;
	[[nodiscard]] not_null<PeerData*> peer() const;
	[[nodiscard]] MTPInputGroupCall input() const;
	[[nodiscard]] QString title() const {
		return _title.current();
	}
	[[nodiscard]] rpl::producer<QString> titleValue() const {
		return _title.value();
	}
	void setTitle(const QString &title) {
		_title = title;
	}
	[[nodiscard]] TimeId recordStartDate() const {
		return _recordStartDate.current();
	}
	[[nodiscard]] rpl::producer<TimeId> recordStartDateValue() const {
		return _recordStartDate.value();
	}
	[[nodiscard]] rpl::producer<TimeId> recordStartDateChanges() const {
		return _recordStartDate.changes();
	}
	[[nodiscard]] TimeId scheduleDate() const {
		return _scheduleDate.current();
	}
	[[nodiscard]] rpl::producer<TimeId> scheduleDateValue() const {
		return _scheduleDate.value();
	}
	[[nodiscard]] rpl::producer<TimeId> scheduleDateChanges() const {
		return _scheduleDate.changes();
	}
	[[nodiscard]] bool scheduleStartSubscribed() const {
		return _scheduleStartSubscribed.current();
	}
	[[nodiscard]] rpl::producer<bool> scheduleStartSubscribedValue() const {
		return _scheduleStartSubscribed.value();
	}
	[[nodiscard]] int unmutedVideoLimit() const {
		return _unmutedVideoLimit.current();
	}
	[[nodiscard]] bool recordVideo() const {
		return _recordVideo.current();
	}

	void setPeer(not_null<PeerData*> peer);

	using Participant = GroupCallParticipant;
	struct ParticipantUpdate {
		std::optional<Participant> was;
		std::optional<Participant> now;
	};

	static constexpr auto kSoundStatusKeptFor = crl::time(1500);

	[[nodiscard]] auto participants() const
		-> const std::vector<Participant> &;
	void requestParticipants();
	[[nodiscard]] bool participantsLoaded() const;
	[[nodiscard]] PeerData *participantPeerByAudioSsrc(uint32 ssrc) const;
	[[nodiscard]] const Participant *participantByPeer(
		not_null<PeerData*> peer) const;
	[[nodiscard]] const Participant *participantByEndpoint(
		const std::string &endpoint) const;

	[[nodiscard]] rpl::producer<> participantsReloaded();
	[[nodiscard]] auto participantUpdated() const
		-> rpl::producer<ParticipantUpdate>;
	[[nodiscard]] auto participantSpeaking() const
		-> rpl::producer<not_null<Participant*>>;

	void enqueueUpdate(const MTPUpdate &update);
	void applyLocalUpdate(
		const MTPDupdateGroupCallParticipants &update);

	void applyLastSpoke(uint32 ssrc, LastSpokeTimes when, crl::time now);
	void applyActiveUpdate(
		PeerId participantPeerId,
		LastSpokeTimes when,
		PeerData *participantPeerLoaded);

	void resolveParticipants(const base::flat_set<uint32> &ssrcs);
	[[nodiscard]] rpl::producer<
		not_null<const base::flat_map<
			uint32,
			LastSpokeTimes>*>> participantsResolved() const {
		return _participantsResolved.events();
	}

	[[nodiscard]] int fullCount() const;
	[[nodiscard]] rpl::producer<int> fullCountValue() const;

	void setInCall();
	void reload();
	void reloadIfStale();
	void processFullCall(const MTPphone_GroupCall &call);

	void setJoinMutedLocally(bool muted);
	[[nodiscard]] bool joinMuted() const;
	[[nodiscard]] bool canChangeJoinMuted() const;
	[[nodiscard]] bool joinedToTop() const;

private:
	enum class ApplySliceSource {
		FullReloaded,
		SliceLoaded,
		UnknownLoaded,
		UpdateReceived,
		UpdateConstructed,
	};
	enum class QueuedType : uint8 {
		VersionedParticipant,
		Participant,
		Call,
	};
	[[nodiscard]] ApiWrap &api() const;

	void discard(const MTPDgroupCallDiscarded &data);
	[[nodiscard]] bool inCall() const;
	void applyParticipantsSlice(
		const QVector<MTPGroupCallParticipant> &list,
		ApplySliceSource sliceSource);
	void requestUnknownParticipants();
	void changePeerEmptyCallFlag();
	void checkFinishSpeakingByActive();
	void applyCallFields(const MTPDgroupCall &data);
	void applyEnqueuedUpdate(const MTPUpdate &update);
	void setServerParticipantsCount(int count);
	void computeParticipantsCount();
	void processQueuedUpdates();
	void processFullCallUsersChats(const MTPphone_GroupCall &call);
	void processFullCallFields(const MTPphone_GroupCall &call);
	[[nodiscard]] bool requestParticipantsAfterReload(
		const MTPphone_GroupCall &call) const;
	[[nodiscard]] bool processSavedFullCall();
	void finishParticipantsSliceRequest();
	[[nodiscard]] Participant *findParticipant(not_null<PeerData*> peer);

	const CallId _id = 0;
	const CallId _accessHash = 0;

	not_null<PeerData*> _peer;
	int _version = 0;
	mtpRequestId _participantsRequestId = 0;
	mtpRequestId _reloadRequestId = 0;
	crl::time _reloadLastFinished = 0;
	rpl::variable<QString> _title;

	base::flat_multi_map<
		std::pair<int, QueuedType>,
		MTPUpdate> _queuedUpdates;
	base::Timer _reloadByQueuedUpdatesTimer;
	std::optional<MTPphone_GroupCall> _savedFull;

	std::vector<Participant> _participants;
	base::flat_map<uint32, not_null<PeerData*>> _participantPeerByAudioSsrc;
	base::flat_map<not_null<PeerData*>, crl::time> _speakingByActiveFinishes;
	base::Timer _speakingByActiveFinishTimer;
	QString _nextOffset;
	int _serverParticipantsCount = 0;
	rpl::variable<int> _fullCount = 0;
	rpl::variable<int> _unmutedVideoLimit = 0;
	rpl::variable<bool> _recordVideo = 0;
	rpl::variable<TimeId> _recordStartDate = 0;
	rpl::variable<TimeId> _scheduleDate = 0;
	rpl::variable<bool> _scheduleStartSubscribed = false;

	base::flat_map<uint32, LastSpokeTimes> _unknownSpokenSsrcs;
	base::flat_map<PeerId, LastSpokeTimes> _unknownSpokenPeerIds;
	rpl::event_stream<
		not_null<const base::flat_map<
			uint32,
			LastSpokeTimes>*>> _participantsResolved;
	mtpRequestId _unknownParticipantPeersRequestId = 0;

	rpl::event_stream<ParticipantUpdate> _participantUpdates;
	rpl::event_stream<not_null<Participant*>> _participantSpeaking;
	rpl::event_stream<> _participantsReloaded;

	bool _joinMuted = false;
	bool _canChangeJoinMuted = true;
	bool _allParticipantsLoaded = false;
	bool _joinedToTop = false;
	bool _applyingQueuedUpdates = false;
	bool _rtmp = false;
	bool _listenersHidden = false;

};

} // namespace Data