2
0
mirror of https://github.com/telegramdesktop/tdesktop synced 2025-08-24 11:17:10 +00:00

382 lines
9.5 KiB
C
Raw Permalink Normal View History

/*
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/weak_ptr.h"
#include "base/timer.h"
#include "base/bytes.h"
#include "mtproto/sender.h"
2019-11-18 12:28:14 +03:00
#include "mtproto/mtproto_auth_key.h"
#include "webrtc/webrtc_device_resolver.h"
2025-03-30 00:53:11 +05:00
namespace Data {
class GroupCall;
} // namespace Data
2017-05-03 16:43:01 +03:00
namespace Media {
namespace Audio {
class Track;
} // namespace Audio
} // namespace Media
2020-07-09 21:38:26 +04:00
namespace tgcalls {
class Instance;
2020-07-16 20:23:55 +04:00
class VideoCaptureInterface;
2020-07-09 21:38:26 +04:00
enum class State;
2020-07-14 21:45:34 +04:00
enum class VideoState;
2020-08-05 16:11:18 +04:00
enum class AudioState;
2020-07-09 21:38:26 +04:00
} // namespace tgcalls
2020-08-11 13:59:48 +04:00
namespace Webrtc {
2020-08-05 16:11:18 +04:00
enum class VideoState;
2020-07-17 22:59:38 +04:00
class VideoTrack;
struct DeviceResolvedId;
2020-08-11 13:59:48 +04:00
} // namespace Webrtc
2020-07-17 22:59:38 +04:00
namespace Calls {
struct StartConferenceInfo;
struct DhConfig {
int32 version = 0;
int32 g = 0;
bytes::vector p;
};
enum class ErrorType {
NoCamera,
NoMicrophone,
NotStartedCall,
NotVideoCall,
Unknown,
};
struct Error {
ErrorType type = ErrorType::Unknown;
QString details;
};
enum class CallType {
Incoming,
Outgoing,
};
2024-03-15 14:42:31 +04:00
class Call final
: public base::has_weak_ptr
, private Webrtc::CaptureMuteTracker {
public:
class Delegate {
public:
virtual DhConfig getDhConfig() const = 0;
virtual void callFinished(not_null<Call*> call) = 0;
virtual void callFailed(not_null<Call*> call) = 0;
virtual void callRedial(not_null<Call*> call) = 0;
enum class CallSound {
2017-05-03 16:43:01 +03:00
Connecting,
Busy,
Ended,
};
virtual void callPlaySound(CallSound sound) = 0;
virtual void callRequestPermissionsOrFail(
Fn<void()> onSuccess,
bool video) = 0;
virtual auto callGetVideoCapture(
const QString &deviceId,
bool isScreenCapture)
-> std::shared_ptr<tgcalls::VideoCaptureInterface> = 0;
2017-05-03 16:43:01 +03:00
virtual ~Delegate() = default;
2018-09-18 13:43:14 +03:00
};
static constexpr auto kSoundSampleMs = 100;
using Type = CallType;
Call(
not_null<Delegate*> delegate,
not_null<UserData*> user,
Type type,
bool video);
2025-03-31 11:44:39 +04:00
Call(
not_null<Delegate*> delegate,
not_null<UserData*> user,
CallId conferenceId,
MsgId conferenceInviteMsgId,
2025-04-01 21:50:39 +05:00
std::vector<not_null<PeerData*>> conferenceParticipants,
bool video);
2020-06-25 21:57:36 +04:00
[[nodiscard]] Type type() const {
return _type;
}
2020-06-25 21:57:36 +04:00
[[nodiscard]] not_null<UserData*> user() const {
return _user;
}
2021-11-01 03:56:30 +03:00
[[nodiscard]] CallId id() const {
return _id;
}
2025-03-31 11:44:39 +04:00
[[nodiscard]] bool conferenceInvite() const {
return _conferenceId != 0;
}
[[nodiscard]] CallId conferenceId() const {
return _conferenceId;
}
[[nodiscard]] MsgId conferenceInviteMsgId() const {
return _conferenceInviteMsgId;
}
2025-04-01 21:50:39 +05:00
[[nodiscard]] auto conferenceParticipants() const
-> const std::vector<not_null<PeerData*>> & {
return _conferenceParticipants;
}
2020-06-25 21:57:36 +04:00
[[nodiscard]] bool isIncomingWaiting() const;
void start(bytes::const_span random);
bool handleUpdate(const MTPPhoneCall &call);
bool handleSignalingData(const MTPDupdatePhoneCallSignalingData &data);
enum State {
Starting,
WaitingInit,
WaitingInitAck,
Established,
FailedHangingUp,
Failed,
HangingUp,
2025-03-30 00:53:11 +05:00
MigrationHangingUp,
Ended,
EndedByOtherDevice,
ExchangingKeys,
Waiting,
Requesting,
WaitingIncoming,
Ringing,
Busy,
WaitingUserConfirmation,
};
2020-07-16 20:23:55 +04:00
[[nodiscard]] State state() const {
2020-06-25 21:57:36 +04:00
return _state.current();
}
2020-07-16 20:23:55 +04:00
[[nodiscard]] rpl::producer<State> stateValue() const {
2020-06-25 21:57:36 +04:00
return _state.value();
}
[[nodiscard]] rpl::producer<Error> errors() const {
return _errors.events();
}
2025-04-04 16:15:33 +05:00
[[nodiscard]] rpl::producer<bool> confereceSupportedValue() const {
return _conferenceSupported.value();
}
2020-08-05 16:11:18 +04:00
enum class RemoteAudioState {
Muted,
Active,
2020-07-16 20:23:55 +04:00
};
2020-08-05 16:11:18 +04:00
[[nodiscard]] RemoteAudioState remoteAudioState() const {
return _remoteAudioState.current();
}
[[nodiscard]] auto remoteAudioStateValue() const
-> rpl::producer<RemoteAudioState> {
return _remoteAudioState.value();
}
2020-08-11 13:59:48 +04:00
[[nodiscard]] Webrtc::VideoState remoteVideoState() const {
2020-08-05 16:11:18 +04:00
return _remoteVideoState.current();
2020-07-16 20:23:55 +04:00
}
2020-08-05 16:11:18 +04:00
[[nodiscard]] auto remoteVideoStateValue() const
2020-08-11 13:59:48 +04:00
-> rpl::producer<Webrtc::VideoState> {
2020-08-05 16:11:18 +04:00
return _remoteVideoState.value();
2020-07-16 20:23:55 +04:00
}
enum class RemoteBatteryState {
Low,
Normal,
};
[[nodiscard]] RemoteBatteryState remoteBatteryState() const {
return _remoteBatteryState.current();
}
[[nodiscard]] auto remoteBatteryStateValue() const
-> rpl::producer<RemoteBatteryState> {
return _remoteBatteryState.value();
}
2018-05-27 11:24:47 +03:00
static constexpr auto kSignalBarStarting = -1;
static constexpr auto kSignalBarFinished = -2;
static constexpr auto kSignalBarCount = 4;
2020-07-16 20:23:55 +04:00
[[nodiscard]] rpl::producer<int> signalBarCountValue() const {
return _signalBarCount.value();
}
void setMuted(bool mute);
[[nodiscard]] bool muted() const {
return _muted.current();
}
[[nodiscard]] rpl::producer<bool> mutedValue() const {
return _muted.value();
2018-05-27 11:24:47 +03:00
}
2020-08-11 13:59:48 +04:00
[[nodiscard]] not_null<Webrtc::VideoTrack*> videoIncoming() const;
[[nodiscard]] not_null<Webrtc::VideoTrack*> videoOutgoing() const;
crl::time getDurationMs() const;
float64 getWaitingSoundPeakValue() const;
void applyUserConfirmation();
void answer();
2025-03-30 00:53:11 +05:00
void hangup(
Data::GroupCall *migrateCall = nullptr,
const QString &migrateSlug = QString());
void redial();
bool isKeyShaForFingerprintReady() const;
bytes::vector getKeyShaForFingerprint() const;
QString getDebugLog() const;
//void setAudioVolume(bool input, float level);
2019-01-05 14:08:02 +03:00
void setAudioDuckingEnabled(bool enabled);
[[nodiscard]] QString videoDeviceId() const {
return _videoCaptureDeviceId;
}
[[nodiscard]] bool isSharingVideo() const;
[[nodiscard]] bool isSharingCamera() const;
[[nodiscard]] bool isSharingScreen() const;
[[nodiscard]] QString cameraSharingDeviceId() const;
[[nodiscard]] QString screenSharingDeviceId() const;
void toggleCameraSharing(bool enabled);
void toggleScreenSharing(std::optional<QString> uniqueId);
2025-04-02 00:13:14 +05:00
[[nodiscard]] auto peekVideoCapture() const
-> std::shared_ptr<tgcalls::VideoCaptureInterface>;
[[nodiscard]] auto playbackDeviceIdValue() const
-> rpl::producer<Webrtc::DeviceResolvedId>;
[[nodiscard]] auto captureDeviceIdValue() const
-> rpl::producer<Webrtc::DeviceResolvedId>;
[[nodiscard]] auto cameraDeviceIdValue() const
-> rpl::producer<Webrtc::DeviceResolvedId>;
2020-06-25 21:57:36 +04:00
[[nodiscard]] rpl::lifetime &lifetime() {
return _lifetime;
}
~Call();
private:
enum class FinishType {
None,
Ended,
Failed,
};
2020-11-24 14:56:46 +03:00
void handleRequestError(const QString &error);
void handleControllerError(const QString &error);
2020-08-05 16:11:18 +04:00
void finish(
FinishType type,
const MTPPhoneCallDiscardReason &reason
2025-03-30 00:53:11 +05:00
= MTP_phoneCallDiscardReasonDisconnect(),
Data::GroupCall *migrateCall = nullptr);
void finishByMigration(const QString &slug);
void startOutgoing();
void startIncoming();
2017-05-03 16:43:01 +03:00
void startWaitingTrack();
void sendSignalingData(const QByteArray &data);
void generateModExpFirst(bytes::const_span randomSeed);
2020-08-05 16:11:18 +04:00
void handleControllerStateChange(tgcalls::State state);
void handleControllerBarCountChange(int count);
void createAndStartController(const MTPDphoneCall &call);
template <typename T>
bool checkCallCommonFields(const T &call);
bool checkCallFields(const MTPDphoneCall &call);
bool checkCallFields(const MTPDphoneCallAccepted &call);
void actuallyAnswer();
2025-03-31 11:44:39 +04:00
void acceptConferenceInvite();
void confirmAcceptedCall(const MTPDphoneCallAccepted &call);
void startConfirmedCall(const MTPDphoneCall &call);
void setState(State state);
void setStateQueued(State state);
void setFailedQueued(const QString &error);
2018-05-27 11:24:47 +03:00
void setSignalBarCount(int count);
void destroyController();
2024-03-15 14:42:31 +04:00
void captureMuteChanged(bool mute) override;
rpl::producer<Webrtc::DeviceResolvedId> captureMuteDeviceId() override;
void setupMediaDevices();
2020-07-31 18:36:35 +04:00
void setupOutgoingVideo();
2020-08-05 16:11:18 +04:00
void updateRemoteMediaState(
tgcalls::AudioState audio,
tgcalls::VideoState video);
2020-07-31 18:36:35 +04:00
[[nodiscard]] StartConferenceInfo migrateConferenceInfo(
StartConferenceInfo extend);
2020-07-31 18:36:35 +04:00
const not_null<Delegate*> _delegate;
const not_null<UserData*> _user;
2019-11-27 11:02:56 +03:00
MTP::Sender _api;
Type _type = Type::Outgoing;
2023-03-02 08:52:24 +03:00
rpl::variable<State> _state = State::Starting;
2025-04-04 16:15:33 +05:00
rpl::variable<bool> _conferenceSupported = false;
rpl::variable<RemoteAudioState> _remoteAudioState
= RemoteAudioState::Active;
2020-08-11 13:59:48 +04:00
rpl::variable<Webrtc::VideoState> _remoteVideoState;
rpl::variable<RemoteBatteryState> _remoteBatteryState
= RemoteBatteryState::Normal;
rpl::event_stream<Error> _errors;
FinishType _finishAfterRequestingCall = FinishType::None;
bool _answerAfterDhConfigReceived = false;
2020-07-16 20:23:55 +04:00
rpl::variable<int> _signalBarCount = kSignalBarStarting;
crl::time _startTime = 0;
base::DelayedCallTimer _finishByTimeoutTimer;
base::Timer _discardByTimeoutTimer;
Fn<void(Webrtc::DeviceResolvedId)> _setDeviceIdCallback;
Webrtc::DeviceResolver _playbackDeviceId;
Webrtc::DeviceResolver _captureDeviceId;
Webrtc::DeviceResolver _cameraDeviceId;
2020-07-16 20:23:55 +04:00
rpl::variable<bool> _muted = false;
DhConfig _dhConfig;
bytes::vector _ga;
bytes::vector _gb;
bytes::vector _gaHash;
bytes::vector _randomPower;
MTP::AuthKey::Data _authKey;
2021-11-01 03:56:30 +03:00
CallId _id = 0;
uint64 _accessHash = 0;
uint64 _keyFingerprint = 0;
2025-03-31 11:44:39 +04:00
CallId _conferenceId = 0;
MsgId _conferenceInviteMsgId = 0;
2025-04-01 21:50:39 +05:00
std::vector<not_null<PeerData*>> _conferenceParticipants;
2025-03-31 11:44:39 +04:00
2020-07-09 21:38:26 +04:00
std::unique_ptr<tgcalls::Instance> _instance;
2020-07-16 20:23:55 +04:00
std::shared_ptr<tgcalls::VideoCaptureInterface> _videoCapture;
QString _videoCaptureDeviceId;
bool _videoCaptureIsScreencast = false;
2020-08-11 13:59:48 +04:00
const std::unique_ptr<Webrtc::VideoTrack> _videoIncoming;
const std::unique_ptr<Webrtc::VideoTrack> _videoOutgoing;
2017-05-03 16:43:01 +03:00
std::unique_ptr<Media::Audio::Track> _waitingTrack;
2024-03-15 14:42:31 +04:00
rpl::lifetime _instanceLifetime;
2020-06-25 21:57:36 +04:00
rpl::lifetime _lifetime;
};
void UpdateConfig(const std::string &data);
} // namespace Calls