calls_controller_webrtc.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #include "calls/calls_controller_webrtc.h"
  8. #include "webrtc/webrtc_call_context.h"
  9. namespace Calls {
  10. namespace {
  11. using namespace Webrtc;
  12. [[nodiscard]] CallConnectionDescription ConvertEndpoint(const TgVoipEndpoint &data) {
  13. return CallConnectionDescription{
  14. .ip = QString::fromStdString(data.host.ipv4),
  15. .ipv6 = QString::fromStdString(data.host.ipv6),
  16. .peerTag = QByteArray(
  17. reinterpret_cast<const char*>(data.peerTag),
  18. base::array_size(data.peerTag)),
  19. .connectionId = data.endpointId,
  20. .port = data.port,
  21. };
  22. }
  23. [[nodiscard]] CallContext::Config MakeContextConfig(
  24. const TgVoipConfig &config,
  25. const TgVoipPersistentState &persistentState,
  26. const std::vector<TgVoipEndpoint> &endpoints,
  27. const TgVoipProxy *proxy,
  28. TgVoipNetworkType initialNetworkType,
  29. const TgVoipEncryptionKey &encryptionKey,
  30. Fn<void(QByteArray)> sendSignalingData,
  31. Fn<void(QImage)> displayNextFrame) {
  32. Expects(!endpoints.empty());
  33. auto result = CallContext::Config{
  34. .proxy = (proxy
  35. ? ProxyServer{
  36. .host = QString::fromStdString(proxy->host),
  37. .username = QString::fromStdString(proxy->login),
  38. .password = QString::fromStdString(proxy->password),
  39. .port = proxy->port }
  40. : ProxyServer()),
  41. .dataSaving = (config.dataSaving != TgVoipDataSaving::Never),
  42. .key = QByteArray(
  43. reinterpret_cast<const char*>(encryptionKey.value.data()),
  44. encryptionKey.value.size()),
  45. .outgoing = encryptionKey.isOutgoing,
  46. .primary = ConvertEndpoint(endpoints.front()),
  47. .alternatives = endpoints | ranges::views::drop(
  48. 1
  49. ) | ranges::views::transform(ConvertEndpoint) | ranges::to_vector,
  50. .maxLayer = config.maxApiLayer,
  51. .allowP2P = config.enableP2P,
  52. .sendSignalingData = std::move(sendSignalingData),
  53. .displayNextFrame = std::move(displayNextFrame),
  54. };
  55. return result;
  56. }
  57. } // namespace
  58. WebrtcController::WebrtcController(
  59. const TgVoipConfig &config,
  60. const TgVoipPersistentState &persistentState,
  61. const std::vector<TgVoipEndpoint> &endpoints,
  62. const TgVoipProxy *proxy,
  63. TgVoipNetworkType initialNetworkType,
  64. const TgVoipEncryptionKey &encryptionKey,
  65. Fn<void(QByteArray)> sendSignalingData,
  66. Fn<void(QImage)> displayNextFrame)
  67. : _impl(std::make_unique<CallContext>(MakeContextConfig(
  68. config,
  69. persistentState,
  70. endpoints,
  71. proxy,
  72. initialNetworkType,
  73. encryptionKey,
  74. std::move(sendSignalingData),
  75. std::move(displayNextFrame)))) {
  76. }
  77. WebrtcController::~WebrtcController() = default;
  78. std::string WebrtcController::Version() {
  79. return CallContext::Version().toStdString();
  80. }
  81. std::string WebrtcController::version() {
  82. return Version();
  83. }
  84. void WebrtcController::setNetworkType(TgVoipNetworkType networkType) {
  85. }
  86. void WebrtcController::setMuteMicrophone(bool muteMicrophone) {
  87. _impl->setIsMuted(muteMicrophone);
  88. }
  89. void WebrtcController::setAudioOutputGainControlEnabled(bool enabled) {
  90. }
  91. void WebrtcController::setEchoCancellationStrength(int strength) {
  92. }
  93. void WebrtcController::setAudioInputDevice(std::string id) {
  94. }
  95. void WebrtcController::setAudioOutputDevice(std::string id) {
  96. }
  97. void WebrtcController::setInputVolume(float level) {
  98. }
  99. void WebrtcController::setOutputVolume(float level) {
  100. }
  101. void WebrtcController::setAudioOutputDuckingEnabled(bool enabled) {
  102. }
  103. bool WebrtcController::receiveSignalingData(const QByteArray &data) {
  104. return _impl->receiveSignalingData(data);
  105. }
  106. std::string WebrtcController::getLastError() {
  107. return {};
  108. }
  109. std::string WebrtcController::getDebugInfo() {
  110. return _impl->getDebugInfo().toStdString();
  111. }
  112. int64_t WebrtcController::getPreferredRelayId() {
  113. return 0;
  114. }
  115. TgVoipTrafficStats WebrtcController::getTrafficStats() {
  116. return {};
  117. }
  118. TgVoipPersistentState WebrtcController::getPersistentState() {
  119. return TgVoipPersistentState{};
  120. }
  121. void WebrtcController::setOnStateUpdated(
  122. Fn<void(TgVoipState)> onStateUpdated) {
  123. _stateUpdatedLifetime.destroy();
  124. _impl->state().changes(
  125. ) | rpl::start_with_next([=](CallState state) {
  126. onStateUpdated([&] {
  127. switch (state) {
  128. case CallState::Initializing: return TgVoipState::WaitInit;
  129. case CallState::Reconnecting: return TgVoipState::Reconnecting;
  130. case CallState::Connected: return TgVoipState::Established;
  131. case CallState::Failed: return TgVoipState::Failed;
  132. }
  133. Unexpected("State value in Webrtc::CallContext::state.");
  134. }());
  135. }, _stateUpdatedLifetime);
  136. }
  137. void WebrtcController::setOnSignalBarsUpdated(
  138. Fn<void(int)> onSignalBarsUpdated) {
  139. }
  140. TgVoipFinalState WebrtcController::stop() {
  141. _impl->stop();
  142. return TgVoipFinalState();
  143. }
  144. } // namespace Calls