2020-11-20 19:25:35 +00:00
|
|
|
/*
|
|
|
|
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 "data/data_group_call.h"
|
|
|
|
|
2020-12-11 11:04:34 +00:00
|
|
|
#include "base/unixtime.h"
|
2020-11-20 19:25:35 +00:00
|
|
|
#include "data/data_channel.h"
|
2020-12-14 12:52:18 +00:00
|
|
|
#include "data/data_chat.h"
|
2020-11-20 19:25:35 +00:00
|
|
|
#include "data/data_changes.h"
|
|
|
|
#include "data/data_session.h"
|
|
|
|
#include "main/main_session.h"
|
2020-12-11 11:04:34 +00:00
|
|
|
#include "calls/calls_instance.h"
|
|
|
|
#include "calls/calls_group_call.h"
|
2021-01-14 00:25:59 +00:00
|
|
|
#include "calls/calls_group_common.h"
|
2020-12-11 11:04:34 +00:00
|
|
|
#include "core/application.h"
|
2020-11-20 19:25:35 +00:00
|
|
|
#include "apiwrap.h"
|
|
|
|
|
|
|
|
namespace Data {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
constexpr auto kRequestPerPage = 30;
|
2020-12-11 11:04:34 +00:00
|
|
|
constexpr auto kSpeakingAfterActive = crl::time(6000);
|
|
|
|
constexpr auto kActiveAfterJoined = crl::time(1000);
|
2020-11-20 19:25:35 +00:00
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
GroupCall::GroupCall(
|
2020-12-14 12:52:18 +00:00
|
|
|
not_null<PeerData*> peer,
|
2020-11-20 19:25:35 +00:00
|
|
|
uint64 id,
|
|
|
|
uint64 accessHash)
|
2020-12-14 12:52:18 +00:00
|
|
|
: _id(id)
|
2020-12-11 11:04:34 +00:00
|
|
|
, _accessHash(accessHash)
|
2020-12-21 19:43:23 +00:00
|
|
|
, _peer(peer)
|
2020-12-11 11:04:34 +00:00
|
|
|
, _speakingByActiveFinishTimer([=] { checkFinishSpeakingByActive(); }) {
|
2020-11-20 19:25:35 +00:00
|
|
|
}
|
|
|
|
|
2020-11-24 11:56:46 +00:00
|
|
|
GroupCall::~GroupCall() {
|
2020-12-11 11:04:34 +00:00
|
|
|
api().request(_unknownUsersRequestId).cancel();
|
2020-11-29 15:18:51 +00:00
|
|
|
api().request(_participantsRequestId).cancel();
|
|
|
|
api().request(_reloadRequestId).cancel();
|
2020-11-24 11:56:46 +00:00
|
|
|
}
|
|
|
|
|
2020-11-20 19:25:35 +00:00
|
|
|
uint64 GroupCall::id() const {
|
|
|
|
return _id;
|
|
|
|
}
|
|
|
|
|
2020-12-14 12:52:18 +00:00
|
|
|
not_null<PeerData*> GroupCall::peer() const {
|
|
|
|
return _peer;
|
2020-11-24 11:56:46 +00:00
|
|
|
}
|
|
|
|
|
2020-11-20 19:25:35 +00:00
|
|
|
MTPInputGroupCall GroupCall::input() const {
|
|
|
|
return MTP_inputGroupCall(MTP_long(_id), MTP_long(_accessHash));
|
|
|
|
}
|
|
|
|
|
2020-12-14 12:52:18 +00:00
|
|
|
void GroupCall::setPeer(not_null<PeerData*> peer) {
|
|
|
|
Expects(peer->migrateFrom() == _peer);
|
|
|
|
Expects(_peer->migrateTo() == peer);
|
|
|
|
|
|
|
|
_peer = peer;
|
|
|
|
}
|
|
|
|
|
2020-11-20 19:25:35 +00:00
|
|
|
auto GroupCall::participants() const
|
|
|
|
-> const std::vector<Participant> & {
|
|
|
|
return _participants;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GroupCall::requestParticipants() {
|
2020-11-24 11:56:46 +00:00
|
|
|
if (_participantsRequestId || _reloadRequestId) {
|
2020-11-20 19:25:35 +00:00
|
|
|
return;
|
2020-11-27 14:50:41 +00:00
|
|
|
} else if (_participants.size() >= _fullCount.current() && _allReceived) {
|
2020-11-20 19:25:35 +00:00
|
|
|
return;
|
2020-11-26 11:04:00 +00:00
|
|
|
} else if (_allReceived) {
|
|
|
|
reload();
|
|
|
|
return;
|
2020-11-20 19:25:35 +00:00
|
|
|
}
|
2020-11-29 15:18:51 +00:00
|
|
|
_participantsRequestId = api().request(MTPphone_GetGroupParticipants(
|
2020-11-20 19:25:35 +00:00
|
|
|
input(),
|
2020-11-28 06:38:55 +00:00
|
|
|
MTP_vector<MTPint>(), // ids
|
2020-12-01 09:06:06 +00:00
|
|
|
MTP_vector<MTPint>(), // ssrcs
|
2020-11-26 11:04:00 +00:00
|
|
|
MTP_string(_nextOffset),
|
2020-11-20 19:25:35 +00:00
|
|
|
MTP_int(kRequestPerPage)
|
|
|
|
)).done([=](const MTPphone_GroupParticipants &result) {
|
|
|
|
result.match([&](const MTPDphone_groupParticipants &data) {
|
2020-11-26 11:04:00 +00:00
|
|
|
_nextOffset = qs(data.vnext_offset());
|
2020-12-14 12:52:18 +00:00
|
|
|
_peer->owner().processUsers(data.vusers());
|
2020-11-29 15:18:51 +00:00
|
|
|
applyParticipantsSlice(
|
|
|
|
data.vparticipants().v,
|
|
|
|
ApplySliceSource::SliceLoaded);
|
2020-11-24 11:56:46 +00:00
|
|
|
_fullCount = data.vcount().v;
|
2020-11-20 19:25:35 +00:00
|
|
|
if (!_allReceived
|
|
|
|
&& (data.vparticipants().v.size() < kRequestPerPage)) {
|
|
|
|
_allReceived = true;
|
|
|
|
}
|
|
|
|
if (_allReceived) {
|
|
|
|
_fullCount = _participants.size();
|
|
|
|
}
|
|
|
|
});
|
2020-11-27 14:50:41 +00:00
|
|
|
_participantsSliceAdded.fire({});
|
2020-11-24 11:56:46 +00:00
|
|
|
_participantsRequestId = 0;
|
2020-12-14 12:52:18 +00:00
|
|
|
changePeerEmptyCallFlag();
|
2020-11-20 19:25:35 +00:00
|
|
|
}).fail([=](const RPCError &error) {
|
|
|
|
_fullCount = _participants.size();
|
2020-11-24 11:56:46 +00:00
|
|
|
_allReceived = true;
|
|
|
|
_participantsRequestId = 0;
|
2020-12-14 12:52:18 +00:00
|
|
|
changePeerEmptyCallFlag();
|
2020-11-20 19:25:35 +00:00
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
2020-12-14 12:52:18 +00:00
|
|
|
void GroupCall::changePeerEmptyCallFlag() {
|
|
|
|
const auto chat = _peer->asChat();
|
|
|
|
const auto channel = _peer->asChannel();
|
|
|
|
constexpr auto chatFlag = MTPDchat::Flag::f_call_not_empty;
|
|
|
|
constexpr auto channelFlag = MTPDchannel::Flag::f_call_not_empty;
|
|
|
|
if (_peer->groupCall() != this) {
|
2020-12-06 13:28:27 +00:00
|
|
|
return;
|
|
|
|
} else if (fullCount() > 0) {
|
2020-12-14 12:52:18 +00:00
|
|
|
if (chat && !(chat->flags() & chatFlag)) {
|
|
|
|
chat->addFlags(chatFlag);
|
|
|
|
chat->session().changes().peerUpdated(
|
|
|
|
chat,
|
|
|
|
Data::PeerUpdate::Flag::GroupCall);
|
|
|
|
} else if (channel && !(channel->flags() & channelFlag)) {
|
|
|
|
channel->addFlags(channelFlag);
|
|
|
|
channel->session().changes().peerUpdated(
|
|
|
|
channel,
|
2020-12-06 13:28:27 +00:00
|
|
|
Data::PeerUpdate::Flag::GroupCall);
|
|
|
|
}
|
2020-12-14 12:52:18 +00:00
|
|
|
} else if (chat && (chat->flags() & chatFlag)) {
|
|
|
|
chat->removeFlags(chatFlag);
|
|
|
|
chat->session().changes().peerUpdated(
|
|
|
|
chat,
|
|
|
|
Data::PeerUpdate::Flag::GroupCall);
|
|
|
|
} else if (channel && (channel->flags() & channelFlag)) {
|
|
|
|
channel->removeFlags(channelFlag);
|
|
|
|
channel->session().changes().peerUpdated(
|
|
|
|
channel,
|
2020-12-06 13:28:27 +00:00
|
|
|
Data::PeerUpdate::Flag::GroupCall);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-20 19:25:35 +00:00
|
|
|
int GroupCall::fullCount() const {
|
2020-11-27 14:50:41 +00:00
|
|
|
return _fullCount.current();
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<int> GroupCall::fullCountValue() const {
|
|
|
|
return _fullCount.value();
|
2020-11-20 19:25:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool GroupCall::participantsLoaded() const {
|
|
|
|
return _allReceived;
|
|
|
|
}
|
|
|
|
|
2020-12-01 09:06:06 +00:00
|
|
|
UserData *GroupCall::userBySsrc(uint32 ssrc) const {
|
|
|
|
const auto i = _userBySsrc.find(ssrc);
|
|
|
|
return (i != end(_userBySsrc)) ? i->second.get() : nullptr;
|
2020-11-28 09:01:50 +00:00
|
|
|
}
|
|
|
|
|
2020-11-27 14:50:41 +00:00
|
|
|
rpl::producer<> GroupCall::participantsSliceAdded() {
|
|
|
|
return _participantsSliceAdded.events();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto GroupCall::participantUpdated() const
|
|
|
|
-> rpl::producer<ParticipantUpdate> {
|
|
|
|
return _participantUpdates.events();
|
|
|
|
}
|
|
|
|
|
2020-11-24 11:56:46 +00:00
|
|
|
void GroupCall::applyUpdate(const MTPGroupCall &update) {
|
|
|
|
applyCall(update, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GroupCall::applyCall(const MTPGroupCall &call, bool force) {
|
|
|
|
call.match([&](const MTPDgroupCall &data) {
|
|
|
|
const auto changed = (_version != data.vversion().v)
|
2020-11-28 16:18:51 +00:00
|
|
|
|| (_fullCount.current() != data.vparticipants_count().v)
|
|
|
|
|| (_joinMuted != data.is_join_muted())
|
|
|
|
|| (_canChangeJoinMuted != data.is_can_change_join_muted());
|
2020-11-24 11:56:46 +00:00
|
|
|
if (!force && !changed) {
|
|
|
|
return;
|
|
|
|
} else if (!force && _version > data.vversion().v) {
|
|
|
|
reload();
|
|
|
|
return;
|
|
|
|
}
|
2020-11-28 16:18:51 +00:00
|
|
|
_joinMuted = data.is_join_muted();
|
|
|
|
_canChangeJoinMuted = data.is_can_change_join_muted();
|
2020-11-24 11:56:46 +00:00
|
|
|
_version = data.vversion().v;
|
|
|
|
_fullCount = data.vparticipants_count().v;
|
2020-12-14 12:52:18 +00:00
|
|
|
changePeerEmptyCallFlag();
|
2020-11-24 11:56:46 +00:00
|
|
|
}, [&](const MTPDgroupCallDiscarded &data) {
|
2020-11-28 14:07:36 +00:00
|
|
|
const auto id = _id;
|
2020-12-14 12:52:18 +00:00
|
|
|
const auto peer = _peer;
|
|
|
|
crl::on_main(&peer->session(), [=] {
|
|
|
|
if (peer->groupCall() && peer->groupCall()->id() == id) {
|
|
|
|
if (const auto chat = peer->asChat()) {
|
|
|
|
chat->clearGroupCall();
|
|
|
|
} else if (const auto channel = peer->asChannel()) {
|
|
|
|
channel->clearGroupCall();
|
|
|
|
}
|
2020-11-28 14:07:36 +00:00
|
|
|
}
|
|
|
|
});
|
2020-11-24 11:56:46 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void GroupCall::reload() {
|
|
|
|
if (_reloadRequestId) {
|
|
|
|
return;
|
|
|
|
} else if (_participantsRequestId) {
|
2020-11-29 15:18:51 +00:00
|
|
|
api().request(_participantsRequestId).cancel();
|
2020-11-24 11:56:46 +00:00
|
|
|
_participantsRequestId = 0;
|
|
|
|
}
|
2020-11-29 15:18:51 +00:00
|
|
|
_reloadRequestId = api().request(
|
2020-11-24 11:56:46 +00:00
|
|
|
MTPphone_GetGroupCall(input())
|
|
|
|
).done([=](const MTPphone_GroupCall &result) {
|
|
|
|
result.match([&](const MTPDphone_groupCall &data) {
|
2020-12-14 12:52:18 +00:00
|
|
|
_peer->owner().processUsers(data.vusers());
|
2020-11-24 11:56:46 +00:00
|
|
|
_participants.clear();
|
2020-12-11 11:04:34 +00:00
|
|
|
_speakingByActiveFinishes.clear();
|
2020-12-01 09:06:06 +00:00
|
|
|
_userBySsrc.clear();
|
2020-11-29 15:18:51 +00:00
|
|
|
applyParticipantsSlice(
|
|
|
|
data.vparticipants().v,
|
|
|
|
ApplySliceSource::SliceLoaded);
|
2020-11-24 11:56:46 +00:00
|
|
|
applyCall(data.vcall(), true);
|
2020-11-27 14:50:41 +00:00
|
|
|
_allReceived = (_fullCount.current() == _participants.size());
|
|
|
|
_participantsSliceAdded.fire({});
|
2020-11-24 11:56:46 +00:00
|
|
|
});
|
|
|
|
_reloadRequestId = 0;
|
|
|
|
}).fail([=](const RPCError &error) {
|
|
|
|
_reloadRequestId = 0;
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GroupCall::applyParticipantsSlice(
|
2020-11-27 14:50:41 +00:00
|
|
|
const QVector<MTPGroupCallParticipant> &list,
|
2020-11-29 15:18:51 +00:00
|
|
|
ApplySliceSource sliceSource) {
|
2020-12-11 11:04:34 +00:00
|
|
|
const auto amInCall = inCall();
|
|
|
|
const auto now = base::unixtime::now();
|
|
|
|
const auto speakingAfterActive = TimeId(kSpeakingAfterActive / 1000);
|
|
|
|
|
2020-11-29 15:18:51 +00:00
|
|
|
auto changedCount = _fullCount.current();
|
2020-11-24 11:56:46 +00:00
|
|
|
for (const auto &participant : list) {
|
|
|
|
participant.match([&](const MTPDgroupCallParticipant &data) {
|
|
|
|
const auto userId = data.vuser_id().v;
|
2020-12-14 12:52:18 +00:00
|
|
|
const auto user = _peer->owner().user(userId);
|
2020-11-24 11:56:46 +00:00
|
|
|
const auto i = ranges::find(
|
|
|
|
_participants,
|
|
|
|
user,
|
|
|
|
&Participant::user);
|
|
|
|
if (data.is_left()) {
|
|
|
|
if (i != end(_participants)) {
|
2020-11-27 14:50:41 +00:00
|
|
|
auto update = ParticipantUpdate{
|
2020-11-29 09:20:33 +00:00
|
|
|
.was = *i,
|
2020-11-27 14:50:41 +00:00
|
|
|
};
|
2020-12-01 09:06:06 +00:00
|
|
|
_userBySsrc.erase(i->ssrc);
|
2020-12-11 11:04:34 +00:00
|
|
|
_speakingByActiveFinishes.remove(user);
|
2020-11-24 11:56:46 +00:00
|
|
|
_participants.erase(i);
|
2020-11-29 15:18:51 +00:00
|
|
|
if (sliceSource != ApplySliceSource::SliceLoaded) {
|
2020-11-27 14:50:41 +00:00
|
|
|
_participantUpdates.fire(std::move(update));
|
|
|
|
}
|
2020-11-24 11:56:46 +00:00
|
|
|
}
|
2020-11-29 15:18:51 +00:00
|
|
|
if (changedCount > _participants.size()) {
|
|
|
|
--changedCount;
|
2020-11-24 11:56:46 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2020-11-29 09:20:33 +00:00
|
|
|
const auto was = (i != end(_participants))
|
|
|
|
? std::make_optional(*i)
|
|
|
|
: std::nullopt;
|
2020-12-06 12:16:11 +00:00
|
|
|
const auto canSelfUnmute = !data.is_muted()
|
|
|
|
|| data.is_can_self_unmute();
|
2020-12-11 11:04:34 +00:00
|
|
|
const auto lastActive = data.vactive_date().value_or(
|
|
|
|
was ? was->lastActive : 0);
|
|
|
|
const auto speaking = canSelfUnmute
|
|
|
|
&& ((was ? was->speaking : false)
|
|
|
|
|| (!amInCall
|
|
|
|
&& (lastActive + speakingAfterActive > now)));
|
2021-02-01 12:25:38 +00:00
|
|
|
const auto volume = (was && data.is_min())
|
|
|
|
? was->volume
|
|
|
|
: data.vvolume().value_or(Calls::Group::kDefaultVolume);
|
|
|
|
const auto mutedByMe = (was && data.is_min())
|
|
|
|
? was->mutedByMe
|
|
|
|
: data.is_muted_by_you();
|
|
|
|
const auto onlyMinLoaded = data.is_min()
|
|
|
|
&& (!was || was->onlyMinLoaded);
|
2020-11-24 11:56:46 +00:00
|
|
|
const auto value = Participant{
|
|
|
|
.user = user,
|
|
|
|
.date = data.vdate().v,
|
2020-12-11 11:04:34 +00:00
|
|
|
.lastActive = lastActive,
|
2020-12-01 09:06:06 +00:00
|
|
|
.ssrc = uint32(data.vsource().v),
|
2021-02-01 12:25:38 +00:00
|
|
|
.volume = volume,
|
2020-12-06 12:16:11 +00:00
|
|
|
.speaking = canSelfUnmute && (was ? was->speaking : false),
|
2020-11-24 11:56:46 +00:00
|
|
|
.muted = data.is_muted(),
|
2021-02-01 12:25:38 +00:00
|
|
|
.mutedByMe = mutedByMe,
|
2020-12-06 12:16:11 +00:00
|
|
|
.canSelfUnmute = canSelfUnmute,
|
2021-02-01 12:25:38 +00:00
|
|
|
.onlyMinLoaded = onlyMinLoaded,
|
2020-11-24 11:56:46 +00:00
|
|
|
};
|
|
|
|
if (i == end(_participants)) {
|
2020-12-01 09:06:06 +00:00
|
|
|
_userBySsrc.emplace(value.ssrc, user);
|
2020-11-24 11:56:46 +00:00
|
|
|
_participants.push_back(value);
|
2020-12-14 12:52:18 +00:00
|
|
|
_peer->owner().unregisterInvitedToCallUser(_id, user);
|
2020-11-24 11:56:46 +00:00
|
|
|
} else {
|
2020-12-01 09:06:06 +00:00
|
|
|
if (i->ssrc != value.ssrc) {
|
|
|
|
_userBySsrc.erase(i->ssrc);
|
|
|
|
_userBySsrc.emplace(value.ssrc, user);
|
2020-11-28 09:01:50 +00:00
|
|
|
}
|
2020-11-24 11:56:46 +00:00
|
|
|
*i = value;
|
|
|
|
}
|
2020-12-14 16:08:47 +00:00
|
|
|
if (data.is_just_joined()) {
|
|
|
|
++changedCount;
|
|
|
|
}
|
2020-11-29 15:18:51 +00:00
|
|
|
if (sliceSource != ApplySliceSource::SliceLoaded) {
|
|
|
|
_participantUpdates.fire({
|
|
|
|
.was = was,
|
|
|
|
.now = value,
|
|
|
|
});
|
|
|
|
}
|
2020-11-27 14:50:41 +00:00
|
|
|
});
|
|
|
|
}
|
2020-11-29 15:18:51 +00:00
|
|
|
if (sliceSource == ApplySliceSource::UpdateReceived) {
|
|
|
|
_fullCount = changedCount;
|
2020-12-14 12:52:18 +00:00
|
|
|
changePeerEmptyCallFlag();
|
2020-11-29 15:18:51 +00:00
|
|
|
}
|
2020-11-27 14:50:41 +00:00
|
|
|
}
|
|
|
|
|
2020-12-15 10:16:44 +00:00
|
|
|
void GroupCall::applyLastSpoke(
|
|
|
|
uint32 ssrc,
|
|
|
|
LastSpokeTimes when,
|
|
|
|
crl::time now) {
|
2020-12-01 09:06:06 +00:00
|
|
|
const auto i = _userBySsrc.find(ssrc);
|
|
|
|
if (i == end(_userBySsrc)) {
|
2020-12-11 11:04:34 +00:00
|
|
|
_unknownSpokenSsrcs[ssrc] = when;
|
|
|
|
requestUnknownParticipants();
|
2020-11-29 09:20:33 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto j = ranges::find(_participants, i->second, &Participant::user);
|
|
|
|
Assert(j != end(_participants));
|
|
|
|
|
2020-12-11 11:04:34 +00:00
|
|
|
_speakingByActiveFinishes.remove(j->user);
|
2020-12-15 10:16:44 +00:00
|
|
|
const auto sounding = (when.anything + kSoundStatusKeptFor >= now)
|
2020-12-06 12:16:11 +00:00
|
|
|
&& j->canSelfUnmute;
|
2020-12-15 10:16:44 +00:00
|
|
|
const auto speaking = sounding
|
|
|
|
&& (when.voice + kSoundStatusKeptFor >= now);
|
|
|
|
if (j->sounding != sounding || j->speaking != speaking) {
|
2020-11-29 09:20:33 +00:00
|
|
|
const auto was = *j;
|
2020-12-15 10:16:44 +00:00
|
|
|
j->sounding = sounding;
|
2020-11-29 09:20:33 +00:00
|
|
|
j->speaking = speaking;
|
|
|
|
_participantUpdates.fire({
|
|
|
|
.was = was,
|
|
|
|
.now = *j,
|
|
|
|
});
|
|
|
|
}
|
2020-11-24 11:56:46 +00:00
|
|
|
}
|
|
|
|
|
2020-12-11 11:04:34 +00:00
|
|
|
void GroupCall::applyActiveUpdate(
|
|
|
|
UserId userId,
|
2020-12-15 10:16:44 +00:00
|
|
|
LastSpokeTimes when,
|
2020-12-11 11:04:34 +00:00
|
|
|
UserData *userLoaded) {
|
|
|
|
if (inCall()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto i = userLoaded
|
|
|
|
? ranges::find(
|
|
|
|
_participants,
|
|
|
|
not_null{ userLoaded },
|
|
|
|
&Participant::user)
|
|
|
|
: _participants.end();
|
2021-02-01 12:25:38 +00:00
|
|
|
const auto notFound = (i == end(_participants));
|
|
|
|
const auto loadByUserId = notFound || i->onlyMinLoaded;
|
|
|
|
if (loadByUserId) {
|
2020-12-11 11:04:34 +00:00
|
|
|
_unknownSpokenUids[userId] = when;
|
|
|
|
requestUnknownParticipants();
|
2021-02-01 12:25:38 +00:00
|
|
|
}
|
|
|
|
if (notFound || !i->canSelfUnmute) {
|
2020-12-11 11:04:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto was = std::make_optional(*i);
|
|
|
|
const auto now = crl::now();
|
2020-12-15 10:16:44 +00:00
|
|
|
const auto elapsed = TimeId((now - when.anything) / crl::time(1000));
|
2020-12-11 11:04:34 +00:00
|
|
|
const auto lastActive = base::unixtime::now() - elapsed;
|
2020-12-15 10:16:44 +00:00
|
|
|
const auto finishes = when.anything + kSpeakingAfterActive;
|
2020-12-11 11:04:34 +00:00
|
|
|
if (lastActive <= i->lastActive || finishes <= now) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_speakingByActiveFinishes[i->user] = finishes;
|
|
|
|
if (!_speakingByActiveFinishTimer.isActive()) {
|
|
|
|
_speakingByActiveFinishTimer.callOnce(finishes - now);
|
|
|
|
}
|
|
|
|
|
|
|
|
i->lastActive = lastActive;
|
|
|
|
i->speaking = true;
|
|
|
|
i->canSelfUnmute = true;
|
|
|
|
if (!was->speaking || !was->canSelfUnmute) {
|
|
|
|
_participantUpdates.fire({
|
|
|
|
.was = was,
|
|
|
|
.now = *i,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GroupCall::checkFinishSpeakingByActive() {
|
|
|
|
const auto now = crl::now();
|
|
|
|
auto nearest = 0;
|
|
|
|
auto stop = std::vector<not_null<UserData*>>();
|
|
|
|
for (auto i = begin(_speakingByActiveFinishes); i != end(_speakingByActiveFinishes);) {
|
|
|
|
const auto when = i->second;
|
|
|
|
if (now >= when) {
|
|
|
|
stop.push_back(i->first);
|
|
|
|
i = _speakingByActiveFinishes.erase(i);
|
|
|
|
} else {
|
|
|
|
if (!nearest || nearest > when) {
|
|
|
|
nearest = when;
|
|
|
|
}
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (const auto user : stop) {
|
|
|
|
const auto i = ranges::find(_participants, user, &Participant::user);
|
|
|
|
if (i->speaking) {
|
|
|
|
const auto was = *i;
|
|
|
|
i->speaking = false;
|
|
|
|
_participantUpdates.fire({
|
|
|
|
.was = was,
|
|
|
|
.now = *i,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (nearest) {
|
|
|
|
_speakingByActiveFinishTimer.callOnce(nearest - now);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GroupCall::requestUnknownParticipants() {
|
|
|
|
if (_unknownUsersRequestId
|
|
|
|
|| (_unknownSpokenSsrcs.empty() && _unknownSpokenUids.empty())) {
|
2020-11-29 15:18:51 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-12-01 09:06:06 +00:00
|
|
|
const auto ssrcs = [&] {
|
|
|
|
if (_unknownSpokenSsrcs.size() < kRequestPerPage) {
|
|
|
|
return base::take(_unknownSpokenSsrcs);
|
2020-11-29 15:18:51 +00:00
|
|
|
}
|
2020-12-15 10:16:44 +00:00
|
|
|
auto result = base::flat_map<uint32, LastSpokeTimes>();
|
2020-11-29 15:18:51 +00:00
|
|
|
result.reserve(kRequestPerPage);
|
|
|
|
while (result.size() < kRequestPerPage) {
|
2020-12-01 09:06:06 +00:00
|
|
|
const auto [ssrc, when] = _unknownSpokenSsrcs.back();
|
|
|
|
result.emplace(ssrc, when);
|
|
|
|
_unknownSpokenSsrcs.erase(_unknownSpokenSsrcs.end() - 1);
|
2020-11-29 15:18:51 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}();
|
2020-12-11 11:04:34 +00:00
|
|
|
const auto uids = [&] {
|
|
|
|
if (_unknownSpokenUids.size() + ssrcs.size() < kRequestPerPage) {
|
|
|
|
return base::take(_unknownSpokenUids);
|
|
|
|
}
|
2020-12-15 10:16:44 +00:00
|
|
|
auto result = base::flat_map<UserId, LastSpokeTimes>();
|
2020-12-11 11:04:34 +00:00
|
|
|
const auto available = (kRequestPerPage - int(ssrcs.size()));
|
|
|
|
if (available > 0) {
|
|
|
|
result.reserve(available);
|
|
|
|
while (result.size() < available) {
|
|
|
|
const auto [userId, when] = _unknownSpokenUids.back();
|
|
|
|
result.emplace(userId, when);
|
|
|
|
_unknownSpokenUids.erase(_unknownSpokenUids.end() - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}();
|
|
|
|
auto ssrcInputs = QVector<MTPint>();
|
|
|
|
ssrcInputs.reserve(ssrcs.size());
|
2020-12-01 09:06:06 +00:00
|
|
|
for (const auto [ssrc, when] : ssrcs) {
|
2020-12-11 11:04:34 +00:00
|
|
|
ssrcInputs.push_back(MTP_int(ssrc));
|
|
|
|
}
|
|
|
|
auto uidInputs = QVector<MTPint>();
|
|
|
|
uidInputs.reserve(uids.size());
|
|
|
|
for (const auto [userId, when] : uids) {
|
|
|
|
uidInputs.push_back(MTP_int(userId));
|
2020-11-29 15:18:51 +00:00
|
|
|
}
|
2020-12-11 11:04:34 +00:00
|
|
|
_unknownUsersRequestId = api().request(MTPphone_GetGroupParticipants(
|
2020-11-29 15:18:51 +00:00
|
|
|
input(),
|
2020-12-11 11:04:34 +00:00
|
|
|
MTP_vector<MTPint>(uidInputs),
|
|
|
|
MTP_vector<MTPint>(ssrcInputs),
|
2020-11-29 15:18:51 +00:00
|
|
|
MTP_string(QString()),
|
|
|
|
MTP_int(kRequestPerPage)
|
|
|
|
)).done([=](const MTPphone_GroupParticipants &result) {
|
|
|
|
result.match([&](const MTPDphone_groupParticipants &data) {
|
2020-12-14 12:52:18 +00:00
|
|
|
_peer->owner().processUsers(data.vusers());
|
2020-11-29 15:18:51 +00:00
|
|
|
applyParticipantsSlice(
|
|
|
|
data.vparticipants().v,
|
|
|
|
ApplySliceSource::UnknownLoaded);
|
|
|
|
});
|
2020-12-11 11:04:34 +00:00
|
|
|
_unknownUsersRequestId = 0;
|
2020-11-29 15:18:51 +00:00
|
|
|
const auto now = crl::now();
|
2020-12-01 09:06:06 +00:00
|
|
|
for (const auto [ssrc, when] : ssrcs) {
|
|
|
|
applyLastSpoke(ssrc, when, now);
|
|
|
|
_unknownSpokenSsrcs.remove(ssrc);
|
2020-11-29 15:18:51 +00:00
|
|
|
}
|
2020-12-11 11:04:34 +00:00
|
|
|
for (const auto [userId, when] : uids) {
|
2020-12-14 12:52:18 +00:00
|
|
|
if (const auto user = _peer->owner().userLoaded(userId)) {
|
2020-12-11 11:04:34 +00:00
|
|
|
const auto isParticipant = ranges::contains(
|
|
|
|
_participants,
|
|
|
|
not_null{ user },
|
|
|
|
&Participant::user);
|
|
|
|
if (isParticipant) {
|
|
|
|
applyActiveUpdate(userId, when, user);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_unknownSpokenUids.remove(userId);
|
|
|
|
}
|
|
|
|
requestUnknownParticipants();
|
2020-11-29 15:18:51 +00:00
|
|
|
}).fail([=](const RPCError &error) {
|
2020-12-11 11:04:34 +00:00
|
|
|
_unknownUsersRequestId = 0;
|
2020-12-01 09:06:06 +00:00
|
|
|
for (const auto [ssrc, when] : ssrcs) {
|
|
|
|
_unknownSpokenSsrcs.remove(ssrc);
|
2020-11-29 15:18:51 +00:00
|
|
|
}
|
2020-12-11 11:04:34 +00:00
|
|
|
for (const auto [userId, when] : uids) {
|
|
|
|
_unknownSpokenUids.remove(userId);
|
|
|
|
}
|
|
|
|
requestUnknownParticipants();
|
2020-11-29 15:18:51 +00:00
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
2020-12-11 11:04:34 +00:00
|
|
|
void GroupCall::setInCall() {
|
|
|
|
_unknownSpokenUids.clear();
|
|
|
|
if (_speakingByActiveFinishes.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto restartTimer = true;
|
|
|
|
const auto latest = crl::now() + kActiveAfterJoined;
|
|
|
|
for (auto &[user, when] : _speakingByActiveFinishes) {
|
|
|
|
if (when > latest) {
|
|
|
|
when = latest;
|
|
|
|
} else {
|
|
|
|
restartTimer = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (restartTimer) {
|
|
|
|
_speakingByActiveFinishTimer.callOnce(kActiveAfterJoined);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GroupCall::inCall() const {
|
|
|
|
const auto current = Core::App().calls().currentGroupCall();
|
|
|
|
return (current != nullptr)
|
|
|
|
&& (current->id() == _id)
|
|
|
|
&& (current->state() == Calls::GroupCall::State::Joined);
|
|
|
|
}
|
|
|
|
|
2020-11-24 11:56:46 +00:00
|
|
|
void GroupCall::applyUpdate(const MTPDupdateGroupCallParticipants &update) {
|
2020-11-27 14:50:41 +00:00
|
|
|
const auto version = update.vversion().v;
|
2020-12-22 13:48:50 +00:00
|
|
|
const auto applyUpdate = [&] {
|
|
|
|
if (version < _version) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
auto versionShouldIncrement = false;
|
|
|
|
for (const auto &participant : update.vparticipants().v) {
|
|
|
|
const auto versioned = participant.match([&](
|
|
|
|
const MTPDgroupCallParticipant &data) {
|
|
|
|
return data.is_versioned();
|
|
|
|
});
|
|
|
|
if (versioned) {
|
|
|
|
versionShouldIncrement = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return versionShouldIncrement
|
|
|
|
? (version == _version + 1)
|
|
|
|
: (version == _version);
|
|
|
|
}();
|
|
|
|
if (!applyUpdate) {
|
2020-11-24 11:56:46 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-12-22 13:48:50 +00:00
|
|
|
_version = version;
|
2020-11-27 14:50:41 +00:00
|
|
|
applyUpdateChecked(update);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GroupCall::applyUpdateChecked(
|
|
|
|
const MTPDupdateGroupCallParticipants &update) {
|
2020-11-29 15:18:51 +00:00
|
|
|
applyParticipantsSlice(
|
|
|
|
update.vparticipants().v,
|
|
|
|
ApplySliceSource::UpdateReceived);
|
2020-11-24 11:56:46 +00:00
|
|
|
}
|
|
|
|
|
2020-11-28 16:18:51 +00:00
|
|
|
void GroupCall::setJoinMutedLocally(bool muted) {
|
|
|
|
_joinMuted = muted;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GroupCall::joinMuted() const {
|
|
|
|
return _joinMuted;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GroupCall::canChangeJoinMuted() const {
|
|
|
|
return _canChangeJoinMuted;
|
|
|
|
}
|
|
|
|
|
2020-11-29 15:18:51 +00:00
|
|
|
ApiWrap &GroupCall::api() const {
|
2020-12-14 12:52:18 +00:00
|
|
|
return _peer->session().api();
|
2020-11-29 15:18:51 +00:00
|
|
|
}
|
|
|
|
|
2020-11-20 19:25:35 +00:00
|
|
|
} // namespace Data
|