| #include "VideoCaptureInterfaceImpl.h" |
| 6 | #include "VideoCapturerInterface.h" |
| 7 | #include "v2/NativeNetworkingImpl.h" |
| 8 | #include "v2/Signaling.h" |
| 9 | #include "v2/ContentNegotiation.h" |
| 10 |
| 11 | #include "CodecSelectHelper.h" |
| 12 | #include "platform/PlatformInterface.h" |
| 13 |
| 14 | #include "api/audio_codecs/audio_decoder_factory_template.h" |
| 15 | #include "api/audio_codecs/audio_encoder_factory_template.h" |
| 16 | #include "api/audio_codecs/opus/audio_decoder_opus.h" |
| 17 | #include "api/audio_codecs/opus/audio_decoder_multi_channel_opus.h" |
| 18 | #include "api/audio_codecs/opus/audio_encoder_opus.h" |
| 19 | #include "api/audio_codecs/L16/audio_decoder_L16.h" |
| 20 | #include "api/audio_codecs/L16/audio_encoder_L16.h" |
| 21 | #include "api/task_queue/default_task_queue_factory.h" |
| 22 | #include "media/engine/webrtc_media_engine.h" |
| 23 | #include "system_wrappers/include/field_trial.h" |
| 24 | #include "api/video/builtin_video_bitrate_allocator_factory.h" |
| 25 | #include "call/call.h" |
| 26 | #include "api/call/audio_sink.h" |
| 27 | #include "modules/audio_processing/audio_buffer.h" |
| 28 | #include "absl/strings/match.h" |
| 29 | #include "pc/channel.h" |
| 30 | #include "audio/audio_state.h" |
| 31 | #include "modules/audio_coding/neteq/default_neteq_factory.h" |
| 32 | #include "modules/audio_coding/include/audio_coding_module.h" |
| 33 | #include "api/candidate.h" |
| 34 | #include "api/jsep_ice_candidate.h" |
| 35 | #include "pc/used_ids.h" |
| 36 | #include "media/base/sdp_video_format_utils.h" |
| 37 | #include "pc/media_session.h" |
| 38 | #include "rtc_base/rtc_certificate_generator.h" |
| 39 | #include "api/peer_connection_interface.h" |
| 40 | #include "api/environment/environment_factory.h" |
| 41 | #include "api/enable_media.h" |
| 42 |
| 43 | #include "AudioFrame.h" |
| 44 | #include "ThreadLocalObject.h" |
| 45 | #include "Manager.h" |
| 46 | #include "NetworkManager.h" |
| 47 | #include "VideoCaptureInterfaceImpl.h" |
| 48 | #include "platform/PlatformInterface.h" |
| 49 | #include "LogSinkImpl.h" |
| 50 | #include "CodecSelectHelper.h" |
| 51 | #include "AudioDeviceHelper.h" |
| 52 | #include "SignalingEncryption.h" |
| 53 | #ifdef WEBRTC_IOS |
| 54 | #include "platform/darwin/iOS/tgcalls_audio_device_module_ios.h" |
| 55 | #endif |
| 56 | #include <random> |
| 57 | #include <sstream> |
| 58 |
| 59 | #include "FieldTrialsConfig.h" |
| 60 |
| 61 | #include "third-party/json11.hpp" |
| 62 |
| 63 | #include "ChannelManager.h" |
| 64 | #include "SignalingConnection.h" |
| 65 | #include "ExternalSignalingConnection.h" |
| 66 | #include "SignalingSctpConnection.h" |
| 67 | #include "SignalingKcpConnection.h" |
| 68 | #include "utils/gzip.h" |
| 69 |
| 70 | namespace tgcalls { |
| 71 | namespace { |
| 72 |
| 73 | [[maybe_unused]] bool getCustomParameterBool(std::map<std::string, json11::Json> const ¶meters, std::string const &name) { |
| 74 | const auto value = parameters.find(name); |
| 75 | if (value != parameters.end() && value->second.is_bool() && value->second.bool_value()) { |
| 76 | return true; |
| 77 | } else { |
| 78 | return false; |
| 79 | } |
| 80 | } |
| 81 |
| 82 | enum class SignalingProtocolVersion { |
| 83 | V1, |
| 84 | V2, |
| 85 | V3 |
| 86 | }; |
| 87 |
| 88 | SignalingProtocolVersion signalingProtocolVersion(std::string const &version) { |
| 89 | if (version == "7.0.0") { |
| 90 | return SignalingProtocolVersion::V1; |
| 91 | } else if (version == "8.0.0") { |
| 92 | return SignalingProtocolVersion::V2; |
| 93 | } else if (version == "9.0.0") { |
| 94 | return SignalingProtocolVersion::V2; |
| 95 | } else if (version == "12.0.0") { |
| 96 | return SignalingProtocolVersion::V3; |
| 97 | } else if (version == "13.0.0") { |
| 98 | return SignalingProtocolVersion::V3; |
| 99 | } else { |
| 100 | RTC_LOG(LS_ERROR) << "signalingProtocolVersion: unknown version " << version; |
| 101 |
| 102 | return SignalingProtocolVersion::V2; |
| 103 | } |
| 104 | } |
| 105 |
| 106 | bool signalingProtocolSupportsCompression(SignalingProtocolVersion version) { |
| 107 | switch (version) { |
| 108 | case SignalingProtocolVersion::V1: |
| 109 | case SignalingProtocolVersion::V2: |
| 110 | return false; |
| 111 | case SignalingProtocolVersion::V3: |
| 112 | return true; |
| 113 | default: |
| 114 | RTC_DCHECK_NOTREACHED(); |
| 115 | break; |
| 116 | } |
| 117 | return false; |
| 118 | } |
| 119 |
| 120 | static VideoCaptureInterfaceObject *GetVideoCaptureAssumingSameThread(VideoCaptureInterface *videoCapture) { |
| 121 | return videoCapture |
| 122 | ? static_cast<VideoCaptureInterfaceImpl*>(videoCapture)->object()->getSyncAssumingSameThread() |
| 123 | : nullptr; |
| 124 | } |
| 125 |
| 126 | class OutgoingAudioChannel : public sigslot::has_slots<> { |
| 127 | public: |
| 128 | OutgoingAudioChannel( |
| 129 | webrtc::Call *call, |
| 130 | ChannelManager *channelManager, |
| 131 | rtc::UniqueRandomIdGenerator *uniqueRandomIdGenerator, |
| 132 | webrtc::LocalAudioSinkAdapter *audioSource, |
| 133 | webrtc::RtpTransport *rtpTransport, |
| 134 | signaling::MediaContent const &mediaContent, |
| 135 | std::shared_ptr<Threads> threads |
| 136 | ) : |
| 137 | _threads(threads), |
| 138 | _ssrc(mediaContent.ssrc), |
| 139 | _call(call), |
| 140 | _channelManager(channelManager), |
| 141 | _audioSource(audioSource) { |
| 142 | cricket::AudioOptions audioOptions; |
| 143 | bool _disableOutgoingAudioProcessing = false; |
| 144 |
| 145 | if (_disableOutgoingAudioProcessing) { |
| 146 | audioOptions.echo_cancellation = false; |
| 147 | audioOptions.noise_suppression = false; |
| 148 | audioOptions.auto_gain_control = false; |
| 149 | audioOptions.highpass_filter = false; |
| 150 | //audioOptions.typing_detection = false; |
| 151 | //audioOptions.residual_echo_detector = false; |
| 152 | } else { |
| 153 | audioOptions.echo_cancellation = true; |
| 154 | audioOptions.noise_suppression = true; |
| 155 | } |
| 156 |
| 157 | const auto contentId = std::to_string(_ssrc); |
| 158 |
| 159 | std::vector<std::string> streamIds; |
| 160 | streamIds.push_back(contentId); |
| 161 |
| 162 | _outgoingAudioChannel = _channelManager->CreateVoiceChannel(call, cricket::MediaConfig(), contentId, false, NativeNetworkingImpl::getDefaulCryptoOptions(), audioOptions); |
| 163 | _threads->getNetworkThread()->BlockingCall([&]() { |
| 164 | _outgoingAudioChannel->SetRtpTransport(rtpTransport); |
| 165 | }); |
| 166 |
| 167 | std::vector<cricket::AudioCodec> codecs; |
| 168 | for (const auto &payloadType : mediaContent.payloadTypes) { |
| 169 | if (payloadType.name == "opus") { |
| 170 | cricket::AudioCodec codec = cricket::CreateAudioCodec(payloadType.id, payloadType.name, payloadType.clockrate, payloadType.channels); |
| 171 |
| 172 | codec.SetParam(cricket::kCodecParamUseInbandFec, 1); |
| 173 | codec.SetParam(cricket::kCodecParamPTime, 60); |
| 174 |
| 175 | for (const auto &feedbackType : payloadType.feedbackTypes) { |
| 176 | codec.AddFeedbackParam(cricket::FeedbackParam(feedbackType.type, feedbackType.subtype)); |
| 177 | } |
| 178 |
| 179 | codecs.push_back(std::move(codec)); |
| 180 |
| 181 | break; |
| 182 | } |
| 183 | } |
| 184 |
| 185 | auto outgoingAudioDescription = std::make_unique<cricket::AudioContentDescription>(); |
| 186 | for (const auto &rtpExtension : mediaContent.rtpExtensions) { |
| 187 | outgoingAudioDescription->AddRtpHeaderExtension(webrtc::RtpExtension(rtpExtension.uri, rtpExtension.id)); |
| 188 | } |
| 189 | outgoingAudioDescription->set_rtcp_mux(true); |
| 190 | outgoingAudioDescription->set_rtcp_reduced_size(true); |
| 191 | outgoingAudioDescription->set_direction(webrtc::RtpTransceiverDirection::kSendOnly); |
| 192 | outgoingAudioDescription->set_codecs(codecs); |
| 193 | outgoingAudioDescription->set_bandwidth(-1); |
| 194 | outgoingAudioDescription->AddStream(cricket::StreamParams::CreateLegacy(_ssrc)); |
| 195 |
| 196 | auto incomingAudioDescription = std::make_unique<cricket::AudioContentDescription>(); |
| 197 | for (const auto &rtpExtension : mediaContent.rtpExtensions) { |
| 198 | incomingAudioDescription->AddRtpHeaderExtension(webrtc::RtpExtension(rtpExtension.uri, rtpExtension.id)); |
| 199 | } |
| 200 | incomingAudioDescription->set_rtcp_mux(true); |
| 201 | incomingAudioDescription->set_rtcp_reduced_size(true); |
| 202 | incomingAudioDescription->set_direction(webrtc::RtpTransceiverDirection::kRecvOnly); |
| 203 | incomingAudioDescription->set_codecs(codecs); |
| 204 | incomingAudioDescription->set_bandwidth(-1); |
| 205 |
| 206 | _threads->getWorkerThread()->BlockingCall([&]() { |
| 207 | _outgoingAudioChannel->SetPayloadTypeDemuxingEnabled(false); |
| 208 | std::string errorDesc; |
| 209 | _outgoingAudioChannel->SetLocalContent(outgoingAudioDescription.get(), webrtc::SdpType::kOffer, errorDesc); |
| 210 | _outgoingAudioChannel->SetRemoteContent(incomingAudioDescription.get(), webrtc::SdpType::kAnswer, errorDesc); |
| 211 | }); |
| 212 |
| 213 | setIsMuted(false); |
| 214 | } |
| 215 |
| 216 | ~OutgoingAudioChannel() { |
| 217 | _outgoingAudioChannel->Enable(false); |
| 218 | _threads->getNetworkThread()->BlockingCall([&]() { |
| 219 | _outgoingAudioChannel->SetRtpTransport(nullptr); |
| 220 | }); |
| 221 | _channelManager->DestroyChannel(_outgoingAudioChannel); |
| 222 | _outgoingAudioChannel = nullptr; |
| 223 | } |
| 224 |
| 225 | void setIsMuted(bool isMuted) { |
| 226 | if (_isMuted != isMuted) { |
| 227 | _isMuted = isMuted; |
| 228 |
| 229 | _outgoingAudioChannel->Enable(!_isMuted); |
| 230 | _threads->getWorkerThread()->BlockingCall([&]() { |
| 231 | _outgoingAudioChannel->send_channel()->SetAudioSend(_ssrc, !_isMuted, nullptr, _audioSource); |
| 232 | }); |
| 233 | } |
| 234 | } |
| 235 |
| 236 | uint32_t ssrc() const { |
| 237 | return _ssrc; |
| 238 | } |
| 239 |
| 240 | void setMaxBitrate(int bitrate) { |
| 241 | _threads->getWorkerThread()->BlockingCall([&]() { |
| 242 | webrtc::RtpParameters initialParameters = _outgoingAudioChannel->send_channel()->GetRtpSendParameters(_ssrc); |
| 243 | webrtc::RtpParameters updatedParameters = initialParameters; |
| 244 |
| 245 | if (updatedParameters.encodings.empty()) { |
| 246 | updatedParameters.encodings.push_back(webrtc::RtpEncodingParameters()); |
| 247 | } |
| 248 |
| 249 | updatedParameters.encodings[0].max_bitrate_bps = bitrate; |
| 250 |
| 251 | if (initialParameters != updatedParameters) { |
| 252 | _outgoingAudioChannel->send_channel()->SetRtpSendParameters(_ssrc, updatedParameters); |
| 253 | } |
| 254 | }); |
| 255 | } |
| 256 |
| 257 | private: |
| 258 | void OnSentPacket_w(const rtc::SentPacket& sent_packet) { |
| 259 | _call->OnSentPacket(sent_packet); |
| 260 | } |
| 261 |
| 262 | private: |
| 263 | std::shared_ptr<Threads> _threads; |
| 264 | uint32_t _ssrc = 0; |
| 265 | webrtc::Call *_call = nullptr; |
| 266 | ChannelManager *_channelManager = nullptr; |
| 267 | webrtc::LocalAudioSinkAdapter *_audioSource = nullptr; |
| 268 | cricket::VoiceChannel *_outgoingAudioChannel = nullptr; |
| 269 |
| 270 | bool _isMuted = true; |
| 271 | }; |
| 272 |
| 273 | namespace { |
| 274 | class AudioSinkImpl: public webrtc::AudioSinkInterface { |
| 275 | public: |
| 276 | AudioSinkImpl(std::function<void(float, float)> update) : |
| 277 | _update(update) { |
| 278 | } |
| 279 |
| 280 | virtual ~AudioSinkImpl() { |
| 281 | } |
| 282 |
| 283 | virtual void OnData(const Data& audio) override { |
| 284 | if (_update && audio.channels == 1) { |
| 285 | const int16_t *samples = (const int16_t *)audio.data; |
| 286 | int numberOfSamplesInFrame = (int)audio.samples_per_channel; |
| 287 |
| 288 | int16_t currentPeak = 0; |
| 289 | for (int i = 0; i < numberOfSamplesInFrame; i++) { |
| 290 | int16_t sample = samples[i]; |
| 291 | if (sample < 0) { |
| 292 | sample = -sample; |
| 293 | } |
| 294 | if (_peak < sample) { |
| 295 | _peak = sample; |
| 296 | } |
| 297 | if (currentPeak < sample) { |
| 298 | currentPeak = sample; |
| 299 | } |
| 300 | _peakCount += 1; |
| 301 | } |
| 302 |
| 303 | if (_peakCount >= 4400) { |
| 304 | float level = ((float)(_peak)) / 8000.0f; |
| 305 | _peak = 0; |
| 306 | _peakCount = 0; |
| 307 | _update(0, level); |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 |
| 312 | private: |
| 313 | std::function<void(float, float)> _update; |
| 314 |
| 315 | int _peakCount = 0; |
| 316 | uint16_t _peak = 0; |
| 317 | }; |
| 318 |
| 319 | } |
| 320 |
| 321 | class IncomingV2AudioChannel : public sigslot::has_slots<> { |
| 322 | public: |
| 323 | IncomingV2AudioChannel( |
| 324 | ChannelManager *channelManager, |
| 325 | webrtc::Call *call, |
| 326 | webrtc::RtpTransport *rtpTransport, |
| 327 | rtc::UniqueRandomIdGenerator *randomIdGenerator, |
| 328 | signaling::MediaContent const &mediaContent, |
| 329 | std::function<void(float, float)> onAudioLevelsUpdated, |
| 330 | std::shared_ptr<Threads> threads) : |
| 331 | _threads(threads), |
| 332 | _ssrc(mediaContent.ssrc), |
| 333 | _channelManager(channelManager), |
| 334 | _call(call) { |
| 335 | _creationTimestamp = rtc::TimeMillis(); |
| 336 |
| 337 | cricket::AudioOptions audioOptions; |
| 338 | audioOptions.audio_jitter_buffer_fast_accelerate = true; |
| 339 | audioOptions.audio_jitter_buffer_min_delay_ms = 50; |
| 340 |
| 341 | const auto streamId = std::to_string(_ssrc); |
| 342 |
| 343 | _audioChannel = _channelManager->CreateVoiceChannel(call, cricket::MediaConfig(), streamId, false, NativeNetworkingImpl::getDefaulCryptoOptions(), audioOptions); |
| 344 | _threads->getNetworkThread()->BlockingCall([&]() { |
| 345 | _audioChannel->SetRtpTransport(rtpTransport); |
| 346 | }); |
| 347 |
| 348 | std::vector<cricket::AudioCodec> codecs; |
| 349 | for (const auto &payloadType : mediaContent.payloadTypes) { |
| 350 | cricket::AudioCodec codec = cricket::CreateAudioCodec(payloadType.id, payloadType.name, payloadType.clockrate, payloadType.channels); |
| 351 | for (const auto ¶meter : payloadType.parameters) { |
| 352 | codec.SetParam(parameter.first, parameter.second); |
| 353 | } |
| 354 | for (const auto &feedbackType : payloadType.feedbackTypes) { |
| 355 | codec.AddFeedbackParam(cricket::FeedbackParam(feedbackType.type, feedbackType.subtype)); |
| 356 | } |
| 357 | codecs.push_back(std::move(codec)); |
| 358 | } |
| 359 |
| 360 | auto outgoingAudioDescription = std::make_unique<cricket::AudioContentDescription>(); |
| 361 | for (const auto &rtpExtension : mediaContent.rtpExtensions) { |
| 362 | outgoingAudioDescription->AddRtpHeaderExtension(webrtc::RtpExtension(rtpExtension.uri, rtpExtension.id)); |
| 363 | } |
| 364 |
| 365 | outgoingAudioDescription->set_rtcp_mux(true); |
| 366 | outgoingAudioDescription->set_rtcp_reduced_size(true); |
| 367 | outgoingAudioDescription->set_direction(webrtc::RtpTransceiverDirection::kRecvOnly); |
| 368 | outgoingAudioDescription->set_codecs(codecs); |
| 369 | outgoingAudioDescription->set_bandwidth(-1); |
| 370 |
| 371 | auto incomingAudioDescription = std::make_unique<cricket::AudioContentDescription>(); |
| 372 | for (const auto &rtpExtension : mediaContent.rtpExtensions) { |
| 373 | incomingAudioDescription->AddRtpHeaderExtension(webrtc::RtpExtension(rtpExtension.uri, rtpExtension.id)); |
| 374 | } |
| 375 | incomingAudioDescription->set_rtcp_mux(true); |
| 376 | incomingAudioDescription->set_rtcp_reduced_size(true); |
| 377 | incomingAudioDescription->set_direction(webrtc::RtpTransceiverDirection::kSendOnly); |
| 378 | incomingAudioDescription->set_codecs(codecs); |
| 379 | incomingAudioDescription->set_bandwidth(-1); |
| 380 | cricket::StreamParams streamParams = cricket::StreamParams::CreateLegacy(mediaContent.ssrc); |
| 381 | streamParams.set_stream_ids({ streamId }); |
| 382 | incomingAudioDescription->AddStream(streamParams); |
| 383 |
| 384 | threads->getWorkerThread()->BlockingCall([this, &outgoingAudioDescription, &incomingAudioDescription, onAudioLevelsUpdated = std::move(onAudioLevelsUpdated), ssrc = mediaContent.ssrc]() { |
| 385 | _audioChannel->SetPayloadTypeDemuxingEnabled(false); |
| 386 | std::string errorDesc; |
| 387 | _audioChannel->SetLocalContent(outgoingAudioDescription.get(), webrtc::SdpType::kOffer, errorDesc); |
| 388 | _audioChannel->SetRemoteContent(incomingAudioDescription.get(), webrtc::SdpType::kAnswer, errorDesc); |
| 389 |
| 390 | std::unique_ptr<AudioSinkImpl> audioLevelSink(new AudioSinkImpl(std::move(onAudioLevelsUpdated))); |
| 391 | _audioChannel->receive_channel()->SetRawAudioSink(ssrc, std::move(audioLevelSink)); |
| 392 | }); |
| 393 |
| 394 | outgoingAudioDescription.reset(); |
| 395 | incomingAudioDescription.reset(); |
| 396 |
| 397 |
| 398 | //std::unique_ptr<AudioSinkImpl> audioLevelSink(new AudioSinkImpl(onAudioLevelUpdated, _ssrc, std::move(onAudioFrame))); |
| 399 | //_audioChannel->media_channel()->SetRawAudioSink(ssrc.networkSsrc, std::move(audioLevelSink)); |
| 400 |
| 401 | _audioChannel->Enable(true); |
| 402 |
| 403 | } |
| 404 |
| 405 | ~IncomingV2AudioChannel() { |
| 406 | _audioChannel->Enable(false); |
| 407 | _threads->getNetworkThread()->BlockingCall([&]() { |
| 408 | _audioChannel->SetRtpTransport(nullptr); |
| 409 | }); |
| 410 | _channelManager->DestroyChannel(_audioChannel); |
| 411 | _audioChannel = nullptr; |
| 412 | } |
| 413 |
| 414 | void setVolume(double value) { |
| 415 | _audioChannel->receive_channel()->SetOutputVolume(_ssrc, value); |
| 416 | } |
| 417 |
| 418 | void updateActivity() { |
| 419 | _activityTimestamp = rtc::TimeMillis(); |
| 420 | } |
| 421 |
| 422 | int64_t getActivity() { |
| 423 | return _activityTimestamp; |
| 424 | } |
| 425 |
| 426 | uint32_t ssrc() const { |
| 427 | return _ssrc; |
| 428 | } |
| 429 |
| 430 | private: |
| 431 | void OnSentPacket_w(const rtc::SentPacket& sent_packet) { |
| 432 | _call->OnSentPacket(sent_packet); |
| 433 | } |
| 434 |
| 435 | private: |
| 436 | std::shared_ptr<Threads> _threads; |
| 437 | uint32_t _ssrc = 0; |
| 438 | // Memory is managed by _channelManager |
| 439 | cricket::VoiceChannel *_audioChannel = nullptr; |
| 440 | // Memory is managed externally |
| 441 | ChannelManager *_channelManager = nullptr; |
| 442 | webrtc::Call *_call = nullptr; |
| 443 | int64_t _creationTimestamp = 0; |
| 444 | int64_t _activityTimestamp = 0; |
| 445 | }; |
| 446 |
| 447 | class OutgoingVideoChannel : public sigslot::has_slots<>, public std::enable_shared_from_this<OutgoingVideoChannel> { |
| 448 | public: |
| 449 | OutgoingVideoChannel( |
| 450 | std::shared_ptr<Threads> threads, |
| 451 | ChannelManager *channelManager, |
| 452 | webrtc::Call *call, |
| 453 | webrtc::RtpTransport *rtpTransport, |
| 454 | rtc::UniqueRandomIdGenerator *randomIdGenerator, |
| 455 | webrtc::VideoBitrateAllocatorFactory *videoBitrateAllocatorFactory, |
| 456 | std::function<void()> rotationUpdated, |
| 457 | signaling::MediaContent const &mediaContent, |
| 458 | bool isScreencast |
| 459 | ) : |
| 460 | _threads(threads), |
| 461 | _mainSsrc(mediaContent.ssrc), |
| 462 | _call(call), |
| 463 | _channelManager(channelManager), |
| 464 | _rotationUpdated(rotationUpdated) { |
| 465 | cricket::VideoOptions videoOptions; |
| 466 | videoOptions.is_screencast = isScreencast; |
| 467 |
| 468 | _outgoingVideoChannel = _channelManager->CreateVideoChannel(call, cricket::MediaConfig(), std::to_string(mediaContent.ssrc), false, NativeNetworkingImpl::getDefaulCryptoOptions(), videoOptions, videoBitrateAllocatorFactory); |
| 469 | _threads->getNetworkThread()->BlockingCall([&]() { |
| 470 | _outgoingVideoChannel->SetRtpTransport(rtpTransport); |
| 471 | }); |
| 472 |
| 473 | std::vector<cricket::VideoCodec> unsortedCodecs; |
| 474 | for (const auto &payloadType : mediaContent.payloadTypes) { |
| 475 | cricket::VideoCodec codec = cricket::CreateVideoCodec(payloadType.id, payloadType.name); |
| 476 | for (const auto ¶meter : payloadType.parameters) { |
| 477 | codec.SetParam(parameter.first, parameter.second); |
| 478 | } |
| 479 | for (const auto &feedbackType : payloadType.feedbackTypes) { |
| 480 | codec.AddFeedbackParam(cricket::FeedbackParam(feedbackType.type, feedbackType.subtype)); |
| 481 | } |
| 482 | unsortedCodecs.push_back(std::move(codec)); |
| 483 | } |
| 484 |
| 485 | std::vector<std::string> codecPreferences = { |
| 486 | #ifndef WEBRTC_DISABLE_H265 |
| 487 | cricket::kH265CodecName, |
| 488 | #endif |
| 489 | cricket::kH264CodecName |
| 490 | }; |
| 491 |
| 492 | std::vector<cricket::VideoCodec> codecs; |
| 493 | for (const auto &name : codecPreferences) { |
| 494 | for (const auto &codec : unsortedCodecs) { |
| 495 | if (codec.name == name) { |
| 496 | codecs.push_back(codec); |
| 497 | } |
| 498 | } |
| 499 | } |
| 500 | for (const auto &codec : unsortedCodecs) { |
| 501 | if (std::find(codecs.begin(), codecs.end(), codec) == codecs.end()) { |
| 502 | codecs.push_back(codec); |
| 503 | } |
| 504 | } |
| 505 |
| 506 | auto outgoingVideoDescription = std::make_unique<cricket::VideoContentDescription>(); |
| 507 | for (const auto &rtpExtension : mediaContent.rtpExtensions) { |
| 508 | outgoingVideoDescription->AddRtpHeaderExtension(rtpExtension); |
| 509 | } |
| 510 |
| 511 | outgoingVideoDescription->set_rtcp_mux(true); |
| 512 | outgoingVideoDescription->set_rtcp_reduced_size(true); |
| 513 | outgoingVideoDescription->set_direction(webrtc::RtpTransceiverDirection::kSendOnly); |
| 514 | outgoingVideoDescription->set_codecs(codecs); |
| 515 | outgoingVideoDescription->set_bandwidth(-1); |
| 516 |
| 517 | cricket::StreamParams videoSendStreamParams; |
| 518 |
| 519 | for (const auto &ssrcGroup : mediaContent.ssrcGroups) { |
| 520 | for (auto ssrc : ssrcGroup.ssrcs) { |
| 521 | if (!videoSendStreamParams.has_ssrc(ssrc)) { |
| 522 | videoSendStreamParams.ssrcs.push_back(ssrc); |
| 523 | } |
| 524 | } |
| 525 |
| 526 | cricket::SsrcGroup mappedGroup(ssrcGroup.semantics, ssrcGroup.ssrcs); |
| 527 | videoSendStreamParams.ssrc_groups.push_back(std::move(mappedGroup)); |
| 528 | } |
| 529 |
| 530 | videoSendStreamParams.cname = "cname"; |
| 531 |
| 532 | outgoingVideoDescription->AddStream(videoSendStreamParams); |
| 533 |
| 534 | auto incomingVideoDescription = std::make_unique<cricket::VideoContentDescription>(); |
| 535 | for (const auto &rtpExtension : mediaContent.rtpExtensions) { |
| 536 | incomingVideoDescription->AddRtpHeaderExtension(webrtc::RtpExtension(rtpExtension.uri, rtpExtension.id)); |
| 537 | } |
| 538 | incomingVideoDescription->set_rtcp_mux(true); |
| 539 | incomingVideoDescription->set_rtcp_reduced_size(true); |
| 540 | incomingVideoDescription->set_direction(webrtc::RtpTransceiverDirection::kRecvOnly); |
| 541 | incomingVideoDescription->set_codecs(codecs); |
| 542 | incomingVideoDescription->set_bandwidth(-1); |
| 543 |
| 544 | threads->getWorkerThread()->BlockingCall([&]() { |
| 545 | _outgoingVideoChannel->SetPayloadTypeDemuxingEnabled(false); |
| 546 | std::string errorDesc; |
| 547 | _outgoingVideoChannel->SetLocalContent(outgoingVideoDescription.get(), webrtc::SdpType::kOffer, errorDesc); |
| 548 | _outgoingVideoChannel->SetRemoteContent(incomingVideoDescription.get(), webrtc::SdpType::kAnswer, errorDesc); |
| 549 |
| 550 | webrtc::RtpParameters rtpParameters = _outgoingVideoChannel->send_channel()->GetRtpSendParameters(mediaContent.ssrc); |
| 551 |
| 552 | if (isScreencast) { |
| 553 | rtpParameters.degradation_preference = webrtc::DegradationPreference::MAINTAIN_RESOLUTION; |
| 554 | } |
| 555 |
| 556 | _outgoingVideoChannel->send_channel()->SetRtpSendParameters(mediaContent.ssrc, rtpParameters); |
| 557 | }); |
| 558 |
| 559 | _outgoingVideoChannel->Enable(false); |
| 560 |
| 561 | threads->getWorkerThread()->BlockingCall([&]() { |
| 562 | _outgoingVideoChannel->send_channel()->SetVideoSend(mediaContent.ssrc, NULL, nullptr); |
| 563 | }); |
| 564 | } |
| 565 |
| 566 | ~OutgoingVideoChannel() { |
| 567 | _outgoingVideoChannel->Enable(false); |
| 568 | _threads->getNetworkThread()->BlockingCall([&]() { |
| 569 | _outgoingVideoChannel->SetRtpTransport(nullptr); |
| 570 | }); |
| 571 | _channelManager->DestroyChannel(_outgoingVideoChannel); |
| 572 | _outgoingVideoChannel = nullptr; |
| 573 | } |
| 574 |
| 575 | void setVideoCapture(std::shared_ptr<VideoCaptureInterface> videoCapture) { |
| 576 | _videoCapture = videoCapture; |
| 577 |
| 578 | if (_videoCapture) { |
| 579 | _outgoingVideoChannel->Enable(true); |
| 580 | auto videoCaptureImpl = GetVideoCaptureAssumingSameThread(_videoCapture.get()); |
| 581 |
| 582 | _threads->getWorkerThread()->BlockingCall([&]() { |
| 583 | _outgoingVideoChannel->send_channel()->SetVideoSend(_mainSsrc, NULL, videoCaptureImpl->source().get()); |
| 584 | }); |
| 585 |
| 586 | const auto weak = std::weak_ptr<OutgoingVideoChannel>(shared_from_this()); |
| 587 | videoCaptureImpl->setRotationUpdated([threads = _threads, weak](int angle) { |
| 588 | threads->getMediaThread()->PostTask([=] { |
| 589 | const auto strong = weak.lock(); |
| 590 | if (!strong) { |
| 591 | return; |
| 592 | } |
| 593 | signaling::MediaStateMessage::VideoRotation videoRotation = signaling::MediaStateMessage::VideoRotation::Rotation0; |
| 594 | switch (angle) { |
| 595 | case 0: { |
| 596 | videoRotation = signaling::MediaStateMessage::VideoRotation::Rotation0; |
| 597 | break; |
| 598 | } |
| 599 | case 90: { |
| 600 | videoRotation = signaling::MediaStateMessage::VideoRotation::Rotation90; |
| 601 | break; |
| 602 | } |
| 603 | case 180: { |
| 604 | videoRotation = signaling::MediaStateMessage::VideoRotation::Rotation180; |
| 605 | break; |
| 606 | } |
| 607 | case 270: { |
| 608 | videoRotation = signaling::MediaStateMessage::VideoRotation::Rotation270; |
| 609 | break; |
| 610 | } |
| 611 | default: { |
| 612 | videoRotation = signaling::MediaStateMessage::VideoRotation::Rotation0; |
| 613 | break; |
| 614 | } |
| 615 | } |
| 616 | if (strong->_videoRotation != videoRotation) { |
| 617 | strong->_videoRotation = videoRotation; |
| 618 | strong->_rotationUpdated(); |
| 619 | } |
| 620 | }); |
| 621 | }); |
| 622 |
| 623 | switch (videoCaptureImpl->getRotation()) { |
| 624 | case 0: { |
| 625 | _videoRotation = signaling::MediaStateMessage::VideoRotation::Rotation0; |
| 626 | break; |
| 627 | } |
| 628 | case 90: { |
| 629 | _videoRotation = signaling::MediaStateMessage::VideoRotation::Rotation90; |
| 630 | break; |
| 631 | } |
| 632 | case 180: { |
| 633 | _videoRotation = signaling::MediaStateMessage::VideoRotation::Rotation180; |
| 634 | break; |
| 635 | } |
| 636 | case 270: { |
| 637 | _videoRotation = signaling::MediaStateMessage::VideoRotation::Rotation270; |
| 638 | break; |
| 639 | } |
| 640 | default: { |
| 641 | _videoRotation = signaling::MediaStateMessage::VideoRotation::Rotation0; |
| 642 | break; |
| 643 | } |
| 644 | } |
| 645 | } else { |
| 646 | _videoRotation = signaling::MediaStateMessage::VideoRotation::Rotation0; |
| 647 | _outgoingVideoChannel->Enable(false); |
| 648 |
| 649 | _threads->getWorkerThread()->BlockingCall([&]() { |
| 650 | _outgoingVideoChannel->send_channel()->SetVideoSend(_mainSsrc, NULL, nullptr); |
| 651 | }); |
| 652 | } |
| 653 | } |
| 654 |
| 655 | uint32_t ssrc() const { |
| 656 | return _mainSsrc; |
| 657 | } |
| 658 |
| 659 | void setMaxBitrate(int bitrate) { |
| 660 | _threads->getWorkerThread()->BlockingCall([&]() { |
| 661 | webrtc::RtpParameters initialParameters = _outgoingVideoChannel->send_channel()->GetRtpSendParameters(_mainSsrc); |
| 662 | webrtc::RtpParameters updatedParameters = initialParameters; |
| 663 |
| 664 | if (updatedParameters.encodings.empty()) { |
| 665 | updatedParameters.encodings.push_back(webrtc::RtpEncodingParameters()); |
| 666 | } |
| 667 |
| 668 | updatedParameters.encodings[0].max_bitrate_bps = bitrate; |
| 669 |
| 670 | if (initialParameters != updatedParameters) { |
| 671 | _outgoingVideoChannel->send_channel()->SetRtpSendParameters(_mainSsrc, updatedParameters); |
| 672 | } |
| 673 | }); |
| 674 | } |
| 675 |
| 676 | public: |
| 677 | std::shared_ptr<VideoCaptureInterface> videoCapture() { |
| 678 | return _videoCapture; |
| 679 | } |
| 680 |
| 681 | signaling::MediaStateMessage::VideoRotation getRotation() { |
| 682 | return _videoRotation; |
| 683 | } |
| 684 |
| 685 | private: |
| 686 | void OnSentPacket_w(const rtc::SentPacket& sent_packet) { |
| 687 | _call->OnSentPacket(sent_packet); |
| 688 | } |
| 689 |
| 690 | private: |
| 691 | std::shared_ptr<Threads> _threads; |
| 692 |
| 693 | uint32_t _mainSsrc = 0; |
| 694 | webrtc::Call *_call = nullptr; |
| 695 | ChannelManager *_channelManager = nullptr; |
| 696 | cricket::VideoChannel *_outgoingVideoChannel = nullptr; |
| 697 |
| 698 | std::function<void()> _rotationUpdated; |
| 699 |
| 700 | std::shared_ptr<VideoCaptureInterface> _videoCapture; |
| 701 | signaling::MediaStateMessage::VideoRotation _videoRotation = signaling::MediaStateMessage::VideoRotation::Rotation0; |
| 702 | }; |
| 703 |
| 704 | class VideoSinkImpl : public rtc::VideoSinkInterface<webrtc::VideoFrame> { |
| 705 | public: |
| 706 | VideoSinkImpl() { |
| 707 | } |
| 708 |
| 709 | virtual ~VideoSinkImpl() { |
| 710 | } |
| 711 |
| 712 | virtual void OnFrame(const webrtc::VideoFrame& frame) override { |
| 713 | //_lastFrame = frame; |
| 714 | for (int i = (int)(_sinks.size()) - 1; i >= 0; i--) { |
| 715 | auto strong = _sinks[i].lock(); |
| 716 | if (!strong) { |
| 717 | _sinks.erase(_sinks.begin() + i); |
| 718 | } else { |
| 719 | strong->OnFrame(frame); |
| 720 | } |
| 721 | } |
| 722 | } |
| 723 |
| 724 | virtual void OnDiscardedFrame() override { |
| 725 | for (int i = (int)(_sinks.size()) - 1; i >= 0; i--) { |
| 726 | auto strong = _sinks[i].lock(); |
| 727 | if (!strong) { |
| 728 | _sinks.erase(_sinks.begin() + i); |
| 729 | } else { |
| 730 | strong->OnDiscardedFrame(); |
| 731 | } |
| 732 | } |
| 733 | } |
| 734 |
| 735 | void addSink(std::weak_ptr<rtc::VideoSinkInterface<webrtc::VideoFrame>> impl) { |
| 736 | _sinks.push_back(impl); |
| 737 | if (_lastFrame) { |
| 738 | auto strong = impl.lock(); |
| 739 | if (strong) { |
| 740 | strong->OnFrame(_lastFrame.value()); |
| 741 | } |
| 742 | } |
| 743 | } |
| 744 |
| 745 | private: |
| 746 | std::vector<std::weak_ptr<rtc::VideoSinkInterface<webrtc::VideoFrame>>> _sinks; |
| 747 | absl::optional<webrtc::VideoFrame> _lastFrame; |
| 748 | }; |
| 749 |
| 750 | class IncomingV2VideoChannel : public sigslot::has_slots<> { |
| 751 | public: |
| 752 | IncomingV2VideoChannel( |
| 753 | ChannelManager *channelManager, |
| 754 | webrtc::Call *call, |
| 755 | webrtc::RtpTransport *rtpTransport, |
| 756 | rtc::UniqueRandomIdGenerator *randomIdGenerator, |
| 757 | signaling::MediaContent const &mediaContent, |
| 758 | std::shared_ptr<Threads> threads) : |
| 759 | _threads(threads), |
| 760 | _channelManager(channelManager), |
| 761 | _call(call) { |
| 762 | _videoSink.reset(new VideoSinkImpl()); |
| 763 |
| 764 | _videoBitrateAllocatorFactory = webrtc::CreateBuiltinVideoBitrateAllocatorFactory(); |
| 765 |
| 766 | const auto contentId = std::to_string(mediaContent.ssrc); |
| 767 |
| 768 | _videoChannel = _channelManager->CreateVideoChannel(call, cricket::MediaConfig(), contentId, false, NativeNetworkingImpl::getDefaulCryptoOptions(), cricket::VideoOptions(), _videoBitrateAllocatorFactory.get()); |
| 769 | _threads->getNetworkThread()->BlockingCall([&]() { |
| 770 | _videoChannel->SetRtpTransport(rtpTransport); |
| 771 | }); |
| 772 |
| 773 | std::vector<cricket::VideoCodec> codecs; |
| 774 | for (const auto &payloadType : mediaContent.payloadTypes) { |
| 775 | cricket::VideoCodec codec = cricket::CreateVideoCodec(payloadType.id, payloadType.name); |
| 776 | for (const auto ¶meter : payloadType.parameters) { |
| 777 | codec.SetParam(parameter.first, parameter.second); |
| 778 | } |
| 779 | for (const auto &feedbackType : payloadType.feedbackTypes) { |
| 780 | codec.AddFeedbackParam(cricket::FeedbackParam(feedbackType.type, feedbackType.subtype)); |
| 781 | } |
| 782 | codecs.push_back(std::move(codec)); |
| 783 | } |
| 784 |
| 785 | auto outgoingVideoDescription = std::make_unique<cricket::VideoContentDescription>(); |
| 786 | for (const auto &rtpExtension : mediaContent.rtpExtensions) { |
| 787 | outgoingVideoDescription->AddRtpHeaderExtension(webrtc::RtpExtension(rtpExtension.uri, rtpExtension.id)); |
| 788 | } |
| 789 | outgoingVideoDescription->set_rtcp_mux(true); |
| 790 | outgoingVideoDescription->set_rtcp_reduced_size(true); |
| 791 | outgoingVideoDescription->set_direction(webrtc::RtpTransceiverDirection::kRecvOnly); |
| 792 | outgoingVideoDescription->set_codecs(codecs); |
| 793 | outgoingVideoDescription->set_bandwidth(-1); |
| 794 |
| 795 | cricket::StreamParams videoRecvStreamParams; |
| 796 |
| 797 | _mainVideoSsrc = mediaContent.ssrc; |
| 798 |
| 799 | std::vector<uint32_t> allSsrcs; |
| 800 | for (const auto &group : mediaContent.ssrcGroups) { |
| 801 | for (auto ssrc : group.ssrcs) { |
| 802 | if (std::find(allSsrcs.begin(), allSsrcs.end(), ssrc) == allSsrcs.end()) { |
| 803 | allSsrcs.push_back(ssrc); |
| 804 | } |
| 805 | } |
| 806 |
| 807 | cricket::SsrcGroup parsedGroup(group.semantics, group.ssrcs); |
| 808 | videoRecvStreamParams.ssrc_groups.push_back(parsedGroup); |
| 809 | } |
| 810 | videoRecvStreamParams.ssrcs = allSsrcs; |
| 811 |
| 812 | videoRecvStreamParams.cname = "cname"; |
| 813 | videoRecvStreamParams.set_stream_ids({ contentId }); |
| 814 |
| 815 | auto incomingVideoDescription = std::make_unique<cricket::VideoContentDescription>(); |
| 816 | for (const auto &rtpExtension : mediaContent.rtpExtensions) { |
| 817 | incomingVideoDescription->AddRtpHeaderExtension(webrtc::RtpExtension(rtpExtension.uri, rtpExtension.id)); |
| 818 | } |
| 819 | incomingVideoDescription->set_rtcp_mux(true); |
| 820 | incomingVideoDescription->set_rtcp_reduced_size(true); |
| 821 | incomingVideoDescription->set_direction(webrtc::RtpTransceiverDirection::kSendOnly); |
| 822 | incomingVideoDescription->set_codecs(codecs); |
| 823 | incomingVideoDescription->set_bandwidth(-1); |
| 824 |
| 825 | incomingVideoDescription->AddStream(videoRecvStreamParams); |
| 826 |
| 827 | threads->getWorkerThread()->BlockingCall([&]() { |
| 828 | _videoChannel->SetPayloadTypeDemuxingEnabled(false); |
| 829 | std::string errorDesc; |
| 830 | _videoChannel->SetLocalContent(outgoingVideoDescription.get(), webrtc::SdpType::kOffer, errorDesc); |
| 831 | _videoChannel->SetRemoteContent(incomingVideoDescription.get(), webrtc::SdpType::kAnswer, errorDesc); |
| 832 |
| 833 | _videoChannel->receive_channel()->SetSink(_mainVideoSsrc, _videoSink.get()); |
| 834 | }); |
| 835 |
| 836 | _videoChannel->Enable(true); |
| 837 | } |
| 838 |
| 839 | ~IncomingV2VideoChannel() { |
| 840 | _videoChannel->Enable(false); |
| 841 | _threads->getNetworkThread()->BlockingCall([&]() { |
| 842 | _videoChannel->SetRtpTransport(nullptr); |
| 843 | }); |
| 844 | _channelManager->DestroyChannel(_videoChannel); |
| 845 | _videoChannel = nullptr; |
| 846 | } |
| 847 |
| 848 | void addSink(std::weak_ptr<rtc::VideoSinkInterface<webrtc::VideoFrame>> impl) { |
| 849 | _videoSink->addSink(impl); |
| 850 | } |
| 851 |
| 852 | uint32_t ssrc() const { |
| 853 | return _mainVideoSsrc; |
| 854 | } |
| 855 |
| 856 | private: |
| 857 | void OnSentPacket_w(const rtc::SentPacket& sent_packet) { |
| 858 | _call->OnSentPacket(sent_packet); |
| 859 | } |
| 860 |
| 861 | private: |
| 862 | std::shared_ptr<Threads> _threads; |
| 863 | uint32_t _mainVideoSsrc = 0; |
| 864 | std::unique_ptr<VideoSinkImpl> _videoSink; |
| 865 | std::unique_ptr<webrtc::VideoBitrateAllocatorFactory> _videoBitrateAllocatorFactory; |
| 866 | // Memory is managed by _channelManager |
| 867 | cricket::VideoChannel *_videoChannel; |
| 868 | // Memory is managed externally |
| 869 | ChannelManager *_channelManager = nullptr; |
| 870 | webrtc::Call *_call = nullptr; |
| 871 | }; |
| 872 |
| 873 | template<typename T> |
| 874 | struct StateLogRecord { |
| 875 | int64_t timestamp = 0; |
| 876 | T record; |
| 877 |
| 878 | explicit StateLogRecord(int32_t timestamp_, T &&record_) : |
| 879 | timestamp(timestamp_), |
| 880 | record(std::move(record_)) { |
| 881 | } |
| 882 | }; |
| 883 |
| 884 | struct NetworkStateLogRecord { |
| 885 | bool isConnected = false; |
| 886 | bool isFailed = false; |
| 887 | absl::optional<InstanceNetworking::RouteDescription> route; |
| 888 | absl::optional<InstanceNetworking::ConnectionDescription> connection; |
| 889 |
| 890 | bool operator==(NetworkStateLogRecord const &rhs) const { |
| 891 | if (isConnected != rhs.isConnected) { |
| 892 | return false; |
| 893 | } |
| 894 | if (isFailed != rhs.isFailed) { |
| 895 | return false; |
| 896 | } |
| 897 | if (route != rhs.route) { |
| 898 | return false; |
| 899 | } |
| 900 | if (connection != rhs.connection) { |
| 901 | return false; |
| 902 | } |
| 903 |
| 904 | return true; |
| 905 | } |
| 906 | }; |
| 907 |
| 908 | struct NetworkBitrateLogRecord { |
| 909 | int32_t bitrate = 0; |
| 910 | }; |
| 911 |
| 912 | } // namespace |
| 913 |
| 914 | class InstanceV2ImplInternal : public std::enable_shared_from_this<InstanceV2ImplInternal> { |
| 915 | public: |
| 916 | InstanceV2ImplInternal(Descriptor &&descriptor, std::shared_ptr<Threads> threads) : |
| 917 | _webrtcEnvironment(webrtc::EnvironmentFactory().Create()), |
| 918 | _threads(threads), |
| 919 | _rtcServers(descriptor.rtcServers), |
| 920 | _proxy(std::move(descriptor.proxy)), |
| 921 | _directConnectionChannel(descriptor.directConnectionChannel), |
| 922 | _enableP2P(descriptor.config.enableP2P), |
| 923 | _enableStunMarking(descriptor.config.enableStunMarking), |
| 924 | _encryptionKey(std::move(descriptor.encryptionKey)), |
| 925 | _stateUpdated(descriptor.stateUpdated), |
| 926 | _signalBarsUpdated(descriptor.signalBarsUpdated), |
| 927 | _audioLevelsUpdated(descriptor.audioLevelsUpdated), |
| 928 | _remoteBatteryLevelIsLowUpdated(descriptor.remoteBatteryLevelIsLowUpdated), |
| 929 | _remoteMediaStateUpdated(descriptor.remoteMediaStateUpdated), |
| 930 | _remotePrefferedAspectRatioUpdated(descriptor.remotePrefferedAspectRatioUpdated), |
| 931 | _signalingDataEmitted(descriptor.signalingDataEmitted), |
| 932 | _createAudioDeviceModule(descriptor.createAudioDeviceModule), |
| 933 | _createWrappedAudioDeviceModule(descriptor.createWrappedAudioDeviceModule), |
| 934 | _devicesConfig(descriptor.mediaDevicesConfig), |
| 935 | _statsLogPath(descriptor.config.statsLogPath), |
| 936 | _eventLog(std::make_unique<webrtc::RtcEventLogNull>()), |
| 937 | _taskQueueFactory(webrtc::CreateDefaultTaskQueueFactory()), |
| 938 | _initialInputDeviceId(std::move(descriptor.initialInputDeviceId)), |
| 939 | _initialOutputDeviceId(std::move(descriptor.initialOutputDeviceId)), |
| 940 | _videoCapture(descriptor.videoCapture), |
| 941 | _platformContext(descriptor.platformContext) { |
| 942 | webrtc::field_trial::InitFieldTrialsFromString( |
| 943 | "WebRTC-DataChannel-Dcsctp/Enabled/" |
| 944 | "WebRTC-Audio-MinimizeResamplingOnMobile/Enabled/" |
| 945 | "WebRTC-Audio-iOS-Holding/Enabled/" |
| 946 | "WebRTC-IceFieldTrials/skip_relay_to_non_relay_connections:true/" |
| 947 | ); |
| 948 |
| 949 | if (!descriptor.config.customParameters.empty()) { |
| 950 | std::string parsingError; |
| 951 | auto customParametersJson = json11::Json::parse(descriptor.config.customParameters, parsingError); |
| 952 | if (customParametersJson.is_object()) { |
| 953 | _customParameters = customParametersJson.object_items(); |
| 954 | } |
| 955 | } |
| 956 |
| 957 | _signalingProtocolVersion = signalingProtocolVersion(descriptor.version); |
| 958 | if (getCustomParameterBool(_customParameters, "network_kcp_experiment")) { |
| 959 | _signalingProtocolVersion = SignalingProtocolVersion::V3; |
| 960 | } |
| 961 | } |
| 962 |
| 963 | ~InstanceV2ImplInternal() { |
| 964 | _incomingAudioChannel.reset(); |
| 965 | _incomingVideoChannel.reset(); |
| 966 | _incomingScreencastChannel.reset(); |
| 967 | _outgoingAudioChannel.reset(); |
| 968 | _outgoingVideoChannel.reset(); |
| 969 | _outgoingScreencastChannel.reset(); |
| 970 | _currentSink.reset(); |
| 971 |
| 972 | _channelManager.reset(); |
| 973 |
| 974 | _threads->getWorkerThread()->BlockingCall([&]() { |
| 975 | _call.reset(); |
| 976 | _audioDeviceModule = nullptr; |
| 977 | }); |
| 978 |
| 979 | _contentNegotiationContext.reset(); |
| 980 |
| 981 | _networking->perform([](InstanceNetworking *networking) { |
| 982 | networking->stop(); |
| 983 | }); |
| 984 |
| 985 | _threads->getNetworkThread()->BlockingCall([]() { |
| 986 | }); |
| 987 | } |
| 988 |
| 989 | void start() { |
| 990 | _startTimestamp = rtc::TimeMillis(); |
| 991 |
| 992 | const auto weak = std::weak_ptr<InstanceV2ImplInternal>(shared_from_this()); |
| 993 |
| 994 | if (getCustomParameterBool(_customParameters, "network_kcp_experiment")) { |
| 995 | _signalingConnection = std::make_shared<SignalingKcpConnection>( |
| 996 | _threads, |
| 997 | [threads = _threads, weak](const std::vector<uint8_t> &data) { |
| 998 | threads->getMediaThread()->PostTask([weak, data] { |
| 999 | const auto strong = weak.lock(); |
| 1000 | if (!strong) { |
| 1001 | return; |
| 1002 | } |
| 1003 |
| 1004 | strong->onSignalingData(data); |
| 1005 | }); |
| 1006 | }, |
| 1007 | [signalingDataEmitted = _signalingDataEmitted](const std::vector<uint8_t> &data) { |
| 1008 | signalingDataEmitted(data); |
| 1009 | } |
| 1010 | ); |
| 1011 | } else if (_signalingProtocolVersion == SignalingProtocolVersion::V3 && !getCustomParameterBool(_customParameters, "network_signaling_nosctp")) { |
| 1012 | _signalingConnection = std::make_shared<SignalingSctpConnection>( |
| 1013 | _threads, |
| 1014 | [threads = _threads, weak](const std::vector<uint8_t> &data) { |
| 1015 | threads->getMediaThread()->PostTask([weak, data] { |
| 1016 | const auto strong = weak.lock(); |
| 1017 | if (!strong) { |
| 1018 | return; |
| 1019 | } |
| 1020 |
| 1021 | strong->onSignalingData(data); |
| 1022 | }); |
| 1023 | }, |
| 1024 | [signalingDataEmitted = _signalingDataEmitted](const std::vector<uint8_t> &data) { |
| 1025 | signalingDataEmitted(data); |
| 1026 | } |
| 1027 | ); |
| 1028 | } |
| 1029 | if (!_signalingConnection) { |
| 1030 | _signalingConnection = std::make_shared<ExternalSignalingConnection>( |
| 1031 | [threads = _threads, weak](const std::vector<uint8_t> &data) { |
| 1032 | threads->getMediaThread()->PostTask([weak, data] { |
| 1033 | const auto strong = weak.lock(); |
| 1034 | if (!strong) { |
| 1035 | return; |
| 1036 | } |
| 1037 |
| 1038 | strong->onSignalingData(data); |
| 1039 | }); |
| 1040 | }, |
| 1041 | [signalingDataEmitted = _signalingDataEmitted](const std::vector<uint8_t> &data) { |
| 1042 | signalingDataEmitted(data); |
| 1043 | } |
| 1044 | ); |
| 1045 | } |
| 1046 |
| 1047 | _signalingConnection->start(); |
| 1048 |
| 1049 | absl::optional<Proxy> proxy; |
| 1050 | if (_proxy) { |
| 1051 | proxy = *(_proxy.get()); |
| 1052 | } |
| 1053 |
| 1054 | _networking.reset(new ThreadLocalObject<InstanceNetworking>(_threads->getNetworkThread(), [weak, threads = _threads, encryptionKey = _encryptionKey, isOutgoing = _encryptionKey.isOutgoing, enableStunMarking = _enableStunMarking, rtcServers = _rtcServers, proxy, enableP2P = _enableP2P, customParameters = _customParameters]() { |
| 1055 | return std::static_pointer_cast<InstanceNetworking>(std::make_shared<NativeNetworkingImpl>(InstanceNetworking::Configuration{ |
| 1056 | .encryptionKey = encryptionKey, |
| 1057 | .isOutgoing = isOutgoing, |
| 1058 | .enableStunMarking = enableStunMarking, |
| 1059 | .enableTCP = false, |
| 1060 | .enableP2P = enableP2P, |
| 1061 | .rtcServers = rtcServers, |
| 1062 | .proxy = proxy, |
| 1063 | .stateUpdated = [threads, weak](const InstanceNetworking::State &state) { |
| 1064 | threads->getMediaThread()->PostTask([=] { |
| 1065 | const auto strong = weak.lock(); |
| 1066 | if (!strong) { |
| 1067 | return; |
| 1068 | } |
| 1069 | strong->onNetworkStateUpdated(state); |
| 1070 | }); |
| 1071 | }, |
| 1072 | .candidateGathered = [threads, weak](const cricket::Candidate &candidate) { |
| 1073 | threads->getMediaThread()->PostTask([=] { |
| 1074 | const auto strong = weak.lock(); |
| 1075 | if (!strong) { |
| 1076 | return; |
| 1077 | } |
| 1078 |
| 1079 | strong->sendCandidate(candidate); |
| 1080 | }); |
| 1081 | }, |
| 1082 | .transportMessageReceived = [threads, weak](rtc::CopyOnWriteBuffer const &packet, bool isMissing) { |
| 1083 | threads->getMediaThread()->PostTask([=] { |
| 1084 | const auto strong = weak.lock(); |
| 1085 | if (!strong) { |
| 1086 | return; |
| 1087 | } |
| 1088 | }); |
| 1089 | }, |
| 1090 | .rtcpPacketReceived = [threads, weak](rtc::CopyOnWriteBuffer const &packet, int64_t timestamp) { |
| 1091 | threads->getWorkerThread()->PostTask([weak, packet]() { |
| 1092 | const auto strong = weak.lock(); |
| 1093 | if (!strong) { |
| 1094 | return; |
| 1095 | } |
| 1096 | strong->_call->Receiver()->DeliverRtcpPacket(packet); |
| 1097 | }); |
| 1098 | }, |
| 1099 | .dataChannelStateUpdated = [threads, weak](bool isDataChannelOpen) { |
| 1100 | threads->getMediaThread()->PostTask([=] { |
| 1101 | const auto strong = weak.lock(); |
| 1102 | if (!strong) { |
| 1103 | return; |
| 1104 | } |
| 1105 | strong->onDataChannelStateUpdated(isDataChannelOpen); |
| 1106 | }); |
| 1107 | }, |
| 1108 | .dataChannelMessageReceived = [threads, weak](std::string const &message) { |
| 1109 | threads->getMediaThread()->PostTask([=] { |
| 1110 | const auto strong = weak.lock(); |
| 1111 | if (!strong) { |
| 1112 | return; |
| 1113 | } |
| 1114 | strong->onDataChannelMessage(message); |
| 1115 | }); |
| 1116 | }, |
| 1117 | .threads = threads, |
| 1118 | .customParameters = customParameters |
| 1119 | })); |
| 1120 | })); |
| 1121 |
| 1122 | PlatformInterface::SharedInstance()->configurePlatformAudio(); |
| 1123 |
| 1124 | _threads->getWorkerThread()->BlockingCall([&]() { |
| 1125 | _audioDeviceModule = createAudioDeviceModule(); |
| 1126 | }); |
| 1127 |
| 1128 | webrtc::PeerConnectionFactoryDependencies peerConnectionFactoryDependencies; |
| 1129 | peerConnectionFactoryDependencies.signaling_thread = _threads->getMediaThread(); |
| 1130 | peerConnectionFactoryDependencies.worker_thread = _threads->getWorkerThread(); |
| 1131 | peerConnectionFactoryDependencies.task_queue_factory = webrtc::CreateDefaultTaskQueueFactory(); |
| 1132 | peerConnectionFactoryDependencies.network_thread = _threads->getNetworkThread(); |
| 1133 | peerConnectionFactoryDependencies.network_monitor_factory = PlatformInterface::SharedInstance()->createNetworkMonitorFactory(); |
| 1134 |
| 1135 | peerConnectionFactoryDependencies.audio_encoder_factory = webrtc::CreateAudioEncoderFactory<webrtc::AudioEncoderOpus, webrtc::AudioEncoderL16>(); |
| 1136 | peerConnectionFactoryDependencies.audio_decoder_factory = webrtc::CreateAudioDecoderFactory<webrtc::AudioDecoderOpus, webrtc::AudioDecoderL16>(); |
| 1137 |
| 1138 | peerConnectionFactoryDependencies.video_encoder_factory = PlatformInterface::SharedInstance()->makeVideoEncoderFactory(_platformContext, true); |
| 1139 | peerConnectionFactoryDependencies.video_decoder_factory = PlatformInterface::SharedInstance()->makeVideoDecoderFactory(_platformContext); |
| 1140 |
| 1141 | peerConnectionFactoryDependencies.adm = _audioDeviceModule; |
| 1142 |
| 1143 | webrtc::AudioProcessingBuilder builder; |
| 1144 | peerConnectionFactoryDependencies.audio_processing = builder.Create(); |
| 1145 |
| 1146 | _availableVideoFormats = peerConnectionFactoryDependencies.video_encoder_factory->GetSupportedFormats(); |
| 1147 |
| 1148 | webrtc::EnableMedia(peerConnectionFactoryDependencies); |
| 1149 |
| 1150 | auto mediaEngine = peerConnectionFactoryDependencies.media_factory->CreateMediaEngine(_webrtcEnvironment, peerConnectionFactoryDependencies); |
| 1151 |
| 1152 | _channelManager = ChannelManager::Create( |
| 1153 | std::move(mediaEngine), |
| 1154 | _threads->getWorkerThread(), |
| 1155 | _threads->getNetworkThread() |
| 1156 | ); |
| 1157 |
| 1158 | _threads->getNetworkThread()->BlockingCall([&]() { |
| 1159 | _rtpTransport = _networking->getSyncAssumingSameThread()->getRtpTransport(); |
| 1160 | }); |
| 1161 |
| 1162 | _threads->getWorkerThread()->BlockingCall([&]() { |
| 1163 | webrtc::CallConfig callConfig(_webrtcEnvironment, _threads->getNetworkThread()); |
| 1164 | callConfig.audio_state = _channelManager->media_engine()->voice().GetAudioState(); |
| 1165 | _call = peerConnectionFactoryDependencies.media_factory->CreateCall(callConfig); |
| 1166 |
| 1167 | SetAudioInputDeviceById(_audioDeviceModule.get(), _devicesConfig.audioInputId); |
| 1168 | SetAudioOutputDeviceById(_audioDeviceModule.get(), _devicesConfig.audioOutputId); |
| 1169 | }); |
| 1170 |
| 1171 | _uniqueRandomIdGenerator.reset(new rtc::UniqueRandomIdGenerator()); |
| 1172 |
| 1173 | _contentNegotiationContext = std::make_unique<ContentNegotiationContext>(fieldTrialsBasedConfig, _encryptionKey.isOutgoing, _channelManager->media_engine(), _uniqueRandomIdGenerator.get()); |
| 1174 | _contentNegotiationContext->copyCodecsFromChannelManager(_channelManager->media_engine(), false); |
| 1175 |
| 1176 | _outgoingAudioChannelId = _contentNegotiationContext->addOutgoingChannel(signaling::MediaContent::Type::Audio); |
| 1177 |
| 1178 | _videoBitrateAllocatorFactory = webrtc::CreateBuiltinVideoBitrateAllocatorFactory(); |
| 1179 |
| 1180 | _networking->perform([](InstanceNetworking *networking) { |
| 1181 | networking->start(); |
| 1182 | }); |
| 1183 |
| 1184 | if (_videoCapture) { |
| 1185 | setVideoCapture(_videoCapture); |
| 1186 | } |
| 1187 |
| 1188 | beginSignaling(); |
| 1189 |
| 1190 | adjustBitratePreferences(true); |
| 1191 |
| 1192 | beginQualityTimer(0); |
| 1193 | beginLogTimer(0); |
| 1194 |
| 1195 | InstanceNetworking::State initialNetworkState; |
| 1196 | initialNetworkState.isReadyToSendData = false; |
| 1197 | onNetworkStateUpdated(initialNetworkState); |
| 1198 | } |
| 1199 |
| 1200 | void beginQualityTimer(int delayMs) { |
| 1201 | const auto weak = std::weak_ptr<InstanceV2ImplInternal>(shared_from_this()); |
| 1202 | _threads->getMediaThread()->PostDelayedTask([weak]() { |
| 1203 | auto strong = weak.lock(); |
| 1204 | if (!strong) { |
| 1205 | return; |
| 1206 | } |
| 1207 |
| 1208 |
| 1209 |
| 1210 | strong->beginQualityTimer(500); |
| 1211 | }, webrtc::TimeDelta::Millis(delayMs)); |
| 1212 | } |
| 1213 |
| 1214 | void beginLogTimer(int delayMs) { |
| 1215 | const auto weak = std::weak_ptr<InstanceV2ImplInternal>(shared_from_this()); |
| 1216 | _threads->getMediaThread()->PostDelayedTask([weak]() { |
| 1217 | auto strong = weak.lock(); |
| 1218 | if (!strong) { |
| 1219 | return; |
| 1220 | } |
| 1221 |
| 1222 | strong->writeStateLogRecords(); |
| 1223 |
| 1224 | strong->beginLogTimer(1000); |
| 1225 | }, webrtc::TimeDelta::Millis(delayMs)); |
| 1226 | } |
| 1227 |
| 1228 | void writeStateLogRecords() { |
| 1229 | const auto weak = std::weak_ptr<InstanceV2ImplInternal>(shared_from_this()); |
| 1230 | _threads->getWorkerThread()->PostTask([weak]() { |
| 1231 | auto strong = weak.lock(); |
| 1232 | if (!strong) { |
| 1233 | return; |
| 1234 | } |
| 1235 |
| 1236 | auto stats = strong->_call->GetStats(); |
| 1237 | float sendBitrateKbps = ((float)stats.send_bandwidth_bps / 1000.0f); |
| 1238 |
| 1239 | strong->_threads->getMediaThread()->PostTask([strong = std::move(strong), sendBitrateKbps]() mutable { |
| 1240 | if (!strong) { |
| 1241 | return; |
| 1242 | } |
| 1243 |
| 1244 | float bitrateNorm = 16.0f; |
| 1245 | if (strong->_outgoingVideoChannel) { |
| 1246 | bitrateNorm = 600.0f; |
| 1247 | } |
| 1248 |
| 1249 | float signalBarsNorm = 4.0f; |
| 1250 | float adjustedQuality = sendBitrateKbps / bitrateNorm; |
| 1251 | adjustedQuality = fmaxf(0.0f, adjustedQuality); |
| 1252 | adjustedQuality = fminf(1.0f, adjustedQuality); |
| 1253 | if (strong->_signalBarsUpdated) { |
| 1254 | strong->_signalBarsUpdated((int)(adjustedQuality * signalBarsNorm)); |
| 1255 | } |
| 1256 |
| 1257 | NetworkBitrateLogRecord networkBitrateLogRecord; |
| 1258 | networkBitrateLogRecord.bitrate = (int32_t)sendBitrateKbps; |
| 1259 |
| 1260 | strong->_networkBitrateLogRecords.emplace_back(rtc::TimeMillis(), std::move(networkBitrateLogRecord)); |
| 1261 |
| 1262 | strong.reset(); |
| 1263 | }); |
| 1264 | }); |
| 1265 | } |
| 1266 |
| 1267 | void sendSignalingMessage(signaling::Message const &message) { |
| 1268 | auto data = message.serialize(); |
| 1269 | sendRawSignalingMessage(data); |
| 1270 | } |
| 1271 |
| 1272 | void sendRawSignalingMessage(std::vector<uint8_t> const &data) { |
| 1273 | RTC_LOG(LS_INFO) << "sendSignalingMessage: " << std::string(data.begin(), data.end()); |
| 1274 |
| 1275 | if (_signalingConnection && _signalingEncryptedConnection) { |
| 1276 | switch (_signalingProtocolVersion) { |
| 1277 | case SignalingProtocolVersion::V1: |
| 1278 | case SignalingProtocolVersion::V3: { |
| 1279 | std::vector<uint8_t> packetData; |
| 1280 | if (signalingProtocolSupportsCompression(_signalingProtocolVersion)) { |
| 1281 | if (const auto compressedData = gzipData(data)) { |
| 1282 | packetData = std::move(compressedData.value()); |
| 1283 | } else { |
| 1284 | RTC_LOG(LS_ERROR) << "Could not gzip signaling message"; |
| 1285 | } |
| 1286 | } else { |
| 1287 | packetData = data; |
| 1288 | } |
| 1289 |
| 1290 | if (const auto message = _signalingEncryptedConnection->encryptRawPacket(rtc::CopyOnWriteBuffer(packetData.data(), packetData.size()))) { |
| 1291 | _signalingConnection->send(std::vector<uint8_t>(message.value().data(), message.value().data() + message.value().size())); |
| 1292 | } else { |
| 1293 | RTC_LOG(LS_ERROR) << "Could not encrypt signaling message"; |
| 1294 | } |
| 1295 | break; |
| 1296 | } |
| 1297 | case SignalingProtocolVersion::V2: { |
| 1298 | rtc::CopyOnWriteBuffer message; |
| 1299 | message.AppendData(data.data(), data.size()); |
| 1300 |
| 1301 | commitSendSignalingMessage(_signalingEncryptedConnection->prepareForSendingRawMessage(message, true)); |
| 1302 |
| 1303 | break; |
| 1304 | } |
| 1305 | default: { |
| 1306 | RTC_DCHECK_NOTREACHED(); |
| 1307 |
| 1308 | break; |
| 1309 | } |
| 1310 | } |
| 1311 | } else { |
| 1312 | RTC_LOG(LS_ERROR) << "sendSignalingMessage encryption not available"; |
| 1313 | } |
| 1314 | } |
| 1315 |
| 1316 | void commitSendSignalingMessage(absl::optional<EncryptedConnection::EncryptedPacket> packet) { |
| 1317 | if (!packet) { |
| 1318 | return; |
| 1319 | } |
| 1320 |
| 1321 | if (_signalingConnection) { |
| 1322 | _signalingConnection->send(packet.value().bytes); |
| 1323 | } |
| 1324 | } |
| 1325 |
| 1326 | void beginSignaling() { |
| 1327 | const auto weak = std::weak_ptr<InstanceV2ImplInternal>(shared_from_this()); |
| 1328 |
| 1329 | _signalingEncryptedConnection = std::make_unique<EncryptedConnection>( |
| 1330 | EncryptedConnection::Type::Signaling, |
| 1331 | _encryptionKey, |
| 1332 | [weak, threads = _threads](int delayMs, int cause) { |
| 1333 | if (delayMs == 0) { |
| 1334 | threads->getMediaThread()->PostTask([weak, cause]() { |
| 1335 | const auto strong = weak.lock(); |
| 1336 | if (!strong) { |
| 1337 | return; |
| 1338 | } |
| 1339 |
| 1340 | strong->sendPendingSignalingServiceData(cause); |
| 1341 | }); |
| 1342 | } else { |
| 1343 | threads->getMediaThread()->PostDelayedTask([weak, cause]() { |
| 1344 | const auto strong = weak.lock(); |
| 1345 | if (!strong) { |
| 1346 | return; |
| 1347 | } |
| 1348 |
| 1349 | strong->sendPendingSignalingServiceData(cause); |
| 1350 | }, webrtc::TimeDelta::Millis(delayMs)); |
| 1351 | } |
| 1352 | } |
| 1353 | ); |
| 1354 |
| 1355 | if (_encryptionKey.isOutgoing) { |
| 1356 | sendInitialSetup(); |
| 1357 | sendOfferIfNeeded(); |
| 1358 | } |
| 1359 | } |
| 1360 |
| 1361 | void sendPendingSignalingServiceData(int cause) { |
| 1362 | commitSendSignalingMessage(_signalingEncryptedConnection->prepareForSendingService(cause)); |
| 1363 | } |
| 1364 |
| 1365 | void createNegotiatedChannels() { |
| 1366 | const auto coordinatedState = _contentNegotiationContext->coordinatedState(); |
| 1367 | if (!coordinatedState) { |
| 1368 | return; |
| 1369 | } |
| 1370 |
| 1371 | if (_outgoingAudioChannelId) { |
| 1372 | const auto audioSsrc = _contentNegotiationContext->outgoingChannelSsrc(_outgoingAudioChannelId.value()); |
| 1373 | if (audioSsrc) { |
| 1374 | if (_outgoingAudioChannel && _outgoingAudioChannel->ssrc() != audioSsrc.value()) { |
| 1375 | _outgoingAudioChannel.reset(); |
| 1376 | } |
| 1377 |
| 1378 | absl::optional<signaling::MediaContent> outgoingAudioContent; |
| 1379 | for (const auto &content : coordinatedState->outgoingContents) { |
| 1380 | if (content.type == signaling::MediaContent::Type::Audio && content.ssrc == audioSsrc.value()) { |
| 1381 | outgoingAudioContent = content; |
| 1382 | break; |
| 1383 | } |
| 1384 | } |
| 1385 |
| 1386 | if (outgoingAudioContent) { |
| 1387 | if (!_outgoingAudioChannel) { |
| 1388 | _outgoingAudioChannel.reset(new OutgoingAudioChannel( |
| 1389 | _call.get(), |
| 1390 | _channelManager.get(), |
| 1391 | _uniqueRandomIdGenerator.get(), |
| 1392 | &_audioSource, |
| 1393 | _rtpTransport, |
| 1394 | outgoingAudioContent.value(), |
| 1395 | _threads |
| 1396 | )); |
| 1397 | _outgoingAudioChannel->setIsMuted(_isMicrophoneMuted); |
| 1398 | } |
| 1399 | } |
| 1400 | } |
| 1401 | } |
| 1402 |
| 1403 | if (_outgoingVideoChannelId) { |
| 1404 | const auto videoSsrc = _contentNegotiationContext->outgoingChannelSsrc(_outgoingVideoChannelId.value()); |
| 1405 | if (videoSsrc) { |
| 1406 | if (_outgoingVideoChannel && _outgoingVideoChannel->ssrc() != videoSsrc.value()) { |
| 1407 | _outgoingVideoChannel.reset(); |
| 1408 | } |
| 1409 |
| 1410 | absl::optional<signaling::MediaContent> outgoingVideoContent; |
| 1411 | for (const auto &content : coordinatedState->outgoingContents) { |
| 1412 | if (content.type == signaling::MediaContent::Type::Video && content.ssrc == videoSsrc.value()) { |
| 1413 | outgoingVideoContent = content; |
| 1414 | break; |
| 1415 | } |
| 1416 | } |
| 1417 |
| 1418 | if (outgoingVideoContent) { |
| 1419 | if (!_outgoingVideoChannel) { |
| 1420 | const auto weak = std::weak_ptr<InstanceV2ImplInternal>(shared_from_this()); |
| 1421 |
| 1422 | _outgoingVideoChannel.reset(new OutgoingVideoChannel( |
| 1423 | _threads, |
| 1424 | _channelManager.get(), |
| 1425 | _call.get(), |
| 1426 | _rtpTransport, |
| 1427 | _uniqueRandomIdGenerator.get(), |
| 1428 | _videoBitrateAllocatorFactory.get(), |
| 1429 | [threads = _threads, weak]() { |
| 1430 | threads->getMediaThread()->PostTask([=] { |
| 1431 | const auto strong = weak.lock(); |
| 1432 | if (!strong) { |
| 1433 | return; |
| 1434 | } |
| 1435 | strong->sendMediaState(); |
| 1436 | }); |
| 1437 | }, |
| 1438 | outgoingVideoContent.value(), |
| 1439 | false |
| 1440 | )); |
| 1441 |
| 1442 | if (_videoCapture) { |
| 1443 | _outgoingVideoChannel->setVideoCapture(_videoCapture); |
| 1444 | } |
| 1445 | } |
| 1446 | } |
| 1447 | } |
| 1448 | } |
| 1449 |
| 1450 | if (_outgoingScreencastChannelId) { |
| 1451 | const auto screencastSsrc = _contentNegotiationContext->outgoingChannelSsrc(_outgoingScreencastChannelId.value()); |
| 1452 | if (screencastSsrc) { |
| 1453 | if (_outgoingScreencastChannel && _outgoingScreencastChannel->ssrc() != screencastSsrc.value()) { |
| 1454 | _outgoingScreencastChannel.reset(); |
| 1455 | } |
| 1456 |
| 1457 | absl::optional<signaling::MediaContent> outgoingScreencastContent; |
| 1458 | for (const auto &content : coordinatedState->outgoingContents) { |
| 1459 | if (content.type == signaling::MediaContent::Type::Video && content.ssrc == screencastSsrc.value()) { |
| 1460 | outgoingScreencastContent = content; |
| 1461 | break; |
| 1462 | } |
| 1463 | } |
| 1464 |
| 1465 | if (outgoingScreencastContent) { |
| 1466 | if (!_outgoingScreencastChannel) { |
| 1467 | const auto weak = std::weak_ptr<InstanceV2ImplInternal>(shared_from_this()); |
| 1468 |
| 1469 | _outgoingScreencastChannel.reset(new OutgoingVideoChannel( |
| 1470 | _threads, |
| 1471 | _channelManager.get(), |
| 1472 | _call.get(), |
| 1473 | _rtpTransport, |
| 1474 | _uniqueRandomIdGenerator.get(), |
| 1475 | _videoBitrateAllocatorFactory.get(), |
| 1476 | [threads = _threads, weak]() { |
| 1477 | threads->getMediaThread()->PostTask([=] { |
| 1478 | const auto strong = weak.lock(); |
| 1479 | if (!strong) { |
| 1480 | return; |
| 1481 | } |
| 1482 | strong->sendMediaState(); |
| 1483 | }); |
| 1484 | }, |
| 1485 | outgoingScreencastContent.value(), |
| 1486 | true |
| 1487 | )); |
| 1488 |
| 1489 | if (_screencastCapture) { |
| 1490 | _outgoingScreencastChannel->setVideoCapture(_screencastCapture); |
| 1491 | } |
| 1492 | } |
| 1493 | } |
| 1494 | } |
| 1495 | } |
| 1496 |
| 1497 | for (const auto &content : coordinatedState->incomingContents) { |
| 1498 | switch (content.type) { |
| 1499 | case signaling::MediaContent::Type::Audio: { |
| 1500 | if (_incomingAudioChannel && _incomingAudioChannel->ssrc() != content.ssrc) { |
| 1501 | _incomingAudioChannel.reset(); |
| 1502 | } |
| 1503 |
| 1504 | if (!_incomingAudioChannel) { |
| 1505 | _incomingAudioChannel.reset(new IncomingV2AudioChannel( |
| 1506 | _channelManager.get(), |
| 1507 | _call.get(), |
| 1508 | _rtpTransport, |
| 1509 | _uniqueRandomIdGenerator.get(), |
| 1510 | content, |
| 1511 | _audioLevelsUpdated, |
| 1512 | _threads |
| 1513 | )); |
| 1514 | } |
| 1515 |
| 1516 | break; |
| 1517 | } |
| 1518 | case signaling::MediaContent::Type::Video: { |
| 1519 | if (_incomingVideoChannel && _incomingVideoChannel->ssrc() != content.ssrc) { |
| 1520 | _incomingVideoChannel.reset(); |
| 1521 | } |
| 1522 |
| 1523 | if (!_incomingVideoChannel) { |
| 1524 | _incomingVideoChannel.reset(new IncomingV2VideoChannel( |
| 1525 | _channelManager.get(), |
| 1526 | _call.get(), |
| 1527 | _rtpTransport, |
| 1528 | _uniqueRandomIdGenerator.get(), |
| 1529 | content, |
| 1530 | _threads |
| 1531 | )); |
| 1532 | _incomingVideoChannel->addSink(_currentSink); |
| 1533 | } |
| 1534 |
| 1535 | break; |
| 1536 | } |
| 1537 | default: { |
| 1538 | RTC_FATAL() << "Unknown media type"; |
| 1539 | break; |
| 1540 | } |
| 1541 | } |
| 1542 | } |
| 1543 |
| 1544 | /* |
| 1545 |
| 1546 | if (_negotiatedOutgoingScreencastContent) { |
| 1547 | const auto weak = std::weak_ptr<InstanceV2ImplInternal>(shared_from_this()); |
| 1548 |
| 1549 | _outgoingScreencastChannel.reset(new OutgoingVideoChannel( |
| 1550 | _threads, |
| 1551 | _channelManager.get(), |
| 1552 | _call.get(), |
| 1553 | _rtpTransport, |
| 1554 | _uniqueRandomIdGenerator.get(), |
| 1555 | _videoBitrateAllocatorFactory.get(), |
| 1556 | [threads = _threads, weak]() { |
| 1557 | threads->getMediaThread()->PostTask([=] { |
| 1558 | const auto strong = weak.lock(); |
| 1559 | if (!strong) { |
| 1560 | return; |
| 1561 | } |
| 1562 | strong->sendMediaState(); |
| 1563 | }); |
| 1564 | }, |
| 1565 | _negotiatedOutgoingScreencastContent.value(), |
| 1566 | true |
| 1567 | )); |
| 1568 |
| 1569 | if (_screencastCapture) { |
| 1570 | _outgoingScreencastChannel->setVideoCapture(_screencastCapture); |
| 1571 | } |
| 1572 | } |
| 1573 |
| 1574 | if (_negotiatedOutgoingAudioContent) { |
| 1575 | _outgoingAudioChannel.reset(new OutgoingAudioChannel( |
| 1576 | _call.get(), |
| 1577 | _channelManager.get(), |
| 1578 | _uniqueRandomIdGenerator.get(), |
| 1579 | &_audioSource, |
| 1580 | _rtpTransport, |
| 1581 | _negotiatedOutgoingAudioContent.value(), |
| 1582 | _threads |
| 1583 | )); |
| 1584 | }*/ |
| 1585 |
| 1586 | adjustBitratePreferences(true); |
| 1587 | sendMediaState(); |
| 1588 | } |
| 1589 |
| 1590 | void sendInitialSetup() { |
| 1591 | const auto weak = std::weak_ptr<InstanceV2ImplInternal>(shared_from_this()); |
| 1592 |
| 1593 | _networking->perform([weak, threads = _threads, isOutgoing = _encryptionKey.isOutgoing](InstanceNetworking *networking) { |
| 1594 | auto localFingerprint = networking->getLocalFingerprint(); |
| 1595 | std::string hash; |
| 1596 | std::string fingerprint; |
| 1597 |
| 1598 | if (localFingerprint) { |
| 1599 | hash = localFingerprint->algorithm; |
| 1600 | fingerprint = localFingerprint->GetRfc4572Fingerprint(); |
| 1601 | } |
| 1602 |
| 1603 | std::string setup; |
| 1604 | if (isOutgoing) { |
| 1605 | setup = "actpass"; |
| 1606 | } else { |
| 1607 | setup = "passive"; |
| 1608 | } |
| 1609 |
| 1610 | auto localIceParams = networking->getLocalIceParameters(); |
| 1611 | std::string ufrag = localIceParams.ufrag; |
| 1612 | std::string pwd = localIceParams.pwd; |
| 1613 | bool supportsRenomination = localIceParams.supportsRenomination; |
| 1614 |
| 1615 | threads->getMediaThread()->PostTask([weak, ufrag, pwd, supportsRenomination, hash, fingerprint, setup]() { |
| 1616 | const auto strong = weak.lock(); |
| 1617 | if (!strong) { |
| 1618 | return; |
| 1619 | } |
| 1620 |
| 1621 | signaling::InitialSetupMessage data; |
| 1622 |
| 1623 | data.ufrag = ufrag; |
| 1624 | data.pwd = pwd; |
| 1625 | data.supportsRenomination = supportsRenomination; |
| 1626 |
| 1627 | signaling::DtlsFingerprint dtlsFingerprint; |
| 1628 | dtlsFingerprint.hash = hash; |
| 1629 | dtlsFingerprint.fingerprint = fingerprint; |
| 1630 | dtlsFingerprint.setup = setup; |
| 1631 | data.fingerprints.push_back(std::move(dtlsFingerprint)); |
| 1632 |
| 1633 | signaling::Message message; |
| 1634 | message.data = std::move(data); |
| 1635 | strong->sendSignalingMessage(message); |
| 1636 | }); |
| 1637 | }); |
| 1638 | } |
| 1639 |
| 1640 | void sendOfferIfNeeded() { |
| 1641 | if (const auto offer = _contentNegotiationContext->getPendingOffer()) { |
| 1642 | signaling::NegotiateChannelsMessage data; |
| 1643 |
| 1644 | data.exchangeId = offer->exchangeId; |
| 1645 |
| 1646 | data.contents = offer->contents; |
| 1647 |
| 1648 | signaling::Message message; |
| 1649 | message.data = std::move(data); |
| 1650 | sendSignalingMessage(message); |
| 1651 | } |
| 1652 | } |
| 1653 |
| 1654 | void receiveSignalingData(const std::vector<uint8_t> &data) { |
| 1655 | if (_signalingConnection) { |
| 1656 | _signalingConnection->receiveExternal(data); |
| 1657 | } else { |
| 1658 | RTC_LOG(LS_ERROR) << "receiveSignalingData: signalingConnection is not available"; |
| 1659 | } |
| 1660 | } |
| 1661 |
| 1662 | void onSignalingData(const std::vector<uint8_t> &data) { |
| 1663 | if (_signalingEncryptedConnection) { |
| 1664 | switch (_signalingProtocolVersion) { |
| 1665 | case SignalingProtocolVersion::V1: |
| 1666 | case SignalingProtocolVersion::V3: { |
| 1667 | if (const auto message = _signalingEncryptedConnection->decryptRawPacket(rtc::CopyOnWriteBuffer(data.data(), data.size()))) { |
| 1668 | processSignalingMessage(message.value()); |
| 1669 | } else { |
| 1670 | RTC_LOG(LS_ERROR) << "receiveSignalingData could not decrypt signaling data"; |
| 1671 | } |
| 1672 |
| 1673 | break; |
| 1674 | } |
| 1675 | case SignalingProtocolVersion::V2: { |
| 1676 | if (const auto packet = _signalingEncryptedConnection->handleIncomingRawPacket((const char *)data.data(), data.size())) { |
| 1677 | processSignalingMessage(packet.value().main.message); |
| 1678 |
| 1679 | for (const auto &additional : packet.value().additional) { |
| 1680 | processSignalingMessage(additional.message); |
| 1681 | } |
| 1682 | } |
| 1683 |
| 1684 | break; |
| 1685 | } |
| 1686 | default: { |
| 1687 | RTC_DCHECK_NOTREACHED(); |
| 1688 |
| 1689 | break; |
| 1690 | } |
| 1691 | } |
| 1692 | } else { |
| 1693 | RTC_LOG(LS_ERROR) << "receiveSignalingData encryption not available"; |
| 1694 | } |
| 1695 | } |
| 1696 |
| 1697 | void processSignalingMessage(rtc::CopyOnWriteBuffer const &data) { |
| 1698 | std::vector<uint8_t> decryptedData = std::vector<uint8_t>(data.data(), data.data() + data.size()); |
| 1699 |
| 1700 | if (isGzip(decryptedData)) { |
| 1701 | if (const auto decompressedData = gunzipData(decryptedData, 2 * 1024 * 1024)) { |
| 1702 | processSignalingData(decompressedData.value()); |
| 1703 | } else { |
| 1704 | RTC_LOG(LS_ERROR) << "receiveSignalingData could not decompress gzipped data"; |
| 1705 | } |
| 1706 | } else { |
| 1707 | processSignalingData(decryptedData); |
| 1708 | } |
| 1709 | } |
| 1710 |
| 1711 | void processSignalingData(const std::vector<uint8_t> &data) { |
| 1712 | RTC_LOG(LS_INFO) << "processSignalingData: " << std::string(data.begin(), data.end()); |
| 1713 |
| 1714 | const auto message = signaling::Message::parse(data); |
| 1715 | if (!message) { |
| 1716 | return; |
| 1717 | } |
| 1718 | const auto messageData = &message->data; |
| 1719 | if (const auto initialSetup = absl::get_if<signaling::InitialSetupMessage>(messageData)) { |
| 1720 | PeerIceParameters remoteIceParameters; |
| 1721 | remoteIceParameters.ufrag = initialSetup->ufrag; |
| 1722 | remoteIceParameters.pwd = initialSetup->pwd; |
| 1723 | remoteIceParameters.supportsRenomination = initialSetup->supportsRenomination; |
| 1724 |
| 1725 | std::unique_ptr<rtc::SSLFingerprint> fingerprint; |
| 1726 | std::string sslSetup; |
| 1727 | if (initialSetup->fingerprints.size() != 0) { |
| 1728 | fingerprint = rtc::SSLFingerprint::CreateUniqueFromRfc4572(initialSetup->fingerprints[0].hash, initialSetup->fingerprints[0].fingerprint); |
| 1729 | sslSetup = initialSetup->fingerprints[0].setup; |
| 1730 | } |
| 1731 |
| 1732 | _networking->perform([threads = _threads, remoteIceParameters = std::move(remoteIceParameters), fingerprint = std::move(fingerprint), sslSetup = std::move(sslSetup)](InstanceNetworking *networking) { |
| 1733 | networking->setRemoteParams(remoteIceParameters, fingerprint.get(), sslSetup); |
| 1734 | }); |
| 1735 |
| 1736 | _handshakeCompleted = true; |
| 1737 |
| 1738 | if (!_encryptionKey.isOutgoing) { |
| 1739 | sendInitialSetup(); |
| 1740 | } |
| 1741 |
| 1742 | commitPendingIceCandidates(); |
| 1743 | } else if (const auto offerAnwer = absl::get_if<signaling::NegotiateChannelsMessage>(messageData)) { |
| 1744 | auto negotiationContents = std::make_unique<ContentNegotiationContext::NegotiationContents>(); |
| 1745 | negotiationContents->exchangeId = offerAnwer->exchangeId; |
| 1746 | negotiationContents->contents = offerAnwer->contents; |
| 1747 |
| 1748 | if (const auto response = _contentNegotiationContext->setRemoteNegotiationContent(std::move(negotiationContents))) { |
| 1749 | signaling::NegotiateChannelsMessage data; |
| 1750 |
| 1751 | data.exchangeId = response->exchangeId; |
| 1752 | data.contents = response->contents; |
| 1753 |
| 1754 | signaling::Message message; |
| 1755 | message.data = std::move(data); |
| 1756 | sendSignalingMessage(message); |
| 1757 | } |
| 1758 |
| 1759 | sendOfferIfNeeded(); |
| 1760 |
| 1761 | createNegotiatedChannels(); |
| 1762 | } else if (const auto candidatesList = absl::get_if<signaling::CandidatesMessage>(messageData)) { |
| 1763 | for (const auto &candidate : candidatesList->iceCandidates) { |
| 1764 | webrtc::JsepIceCandidate parseCandidate{ std::string(), 0 }; |
| 1765 | if (!parseCandidate.Initialize(candidate.sdpString, nullptr)) { |
| 1766 | RTC_LOG(LS_ERROR) << "Could not parse candidate: " << candidate.sdpString; |
| 1767 | continue; |
| 1768 | } |
| 1769 |
| 1770 | _pendingIceCandidates.push_back(parseCandidate.candidate()); |
| 1771 | } |
| 1772 |
| 1773 | if (_handshakeCompleted) { |
| 1774 | commitPendingIceCandidates(); |
| 1775 | } |
| 1776 | } else if (const auto mediaState = absl::get_if<signaling::MediaStateMessage>(messageData)) { |
| 1777 | AudioState mappedAudioState; |
| 1778 | if (mediaState->isMuted) { |
| 1779 | mappedAudioState = AudioState::Muted; |
| 1780 | } else { |
| 1781 | mappedAudioState = AudioState::Active; |
| 1782 | } |
| 1783 |
| 1784 | VideoState mappedVideoState; |
| 1785 | switch (mediaState->videoState) { |
| 1786 | case signaling::MediaStateMessage::VideoState::Inactive: { |
| 1787 | mappedVideoState = VideoState::Inactive; |
| 1788 | break; |
| 1789 | } |
| 1790 | case signaling::MediaStateMessage::VideoState::Suspended: { |
| 1791 | mappedVideoState = VideoState::Paused; |
| 1792 | break; |
| 1793 | } |
| 1794 | case signaling::MediaStateMessage::VideoState::Active: { |
| 1795 | mappedVideoState = VideoState::Active; |
| 1796 | break; |
| 1797 | } |
| 1798 | default: { |
| 1799 | RTC_FATAL() << "Unknown videoState"; |
| 1800 | break; |
| 1801 | } |
| 1802 | } |
| 1803 |
| 1804 | VideoState mappedScreencastState; |
| 1805 | switch (mediaState->screencastState) { |
| 1806 | case signaling::MediaStateMessage::VideoState::Inactive: { |
| 1807 | mappedScreencastState = VideoState::Inactive; |
| 1808 | break; |
| 1809 | } |
| 1810 | case signaling::MediaStateMessage::VideoState::Suspended: { |
| 1811 | mappedScreencastState = VideoState::Paused; |
| 1812 | break; |
| 1813 | } |
| 1814 | case signaling::MediaStateMessage::VideoState::Active: { |
| 1815 | mappedScreencastState = VideoState::Active; |
| 1816 | break; |
| 1817 | } |
| 1818 | default: { |
| 1819 | RTC_FATAL() << "Unknown videoState"; |
| 1820 | break; |
| 1821 | } |
| 1822 | } |
| 1823 |
| 1824 | VideoState effectiveVideoState = mappedVideoState; |
| 1825 | if (mappedScreencastState == VideoState::Active || mappedScreencastState == VideoState::Paused) { |
| 1826 | effectiveVideoState = mappedScreencastState; |
| 1827 | } |
| 1828 |
| 1829 | if (_remoteMediaStateUpdated) { |
| 1830 | _remoteMediaStateUpdated(mappedAudioState, effectiveVideoState); |
| 1831 | } |
| 1832 |
| 1833 | if (_remoteBatteryLevelIsLowUpdated) { |
| 1834 | _remoteBatteryLevelIsLowUpdated(mediaState->isBatteryLow); |
| 1835 | } |
| 1836 | } |
| 1837 | } |
| 1838 |
| 1839 | void commitPendingIceCandidates() { |
| 1840 | if (_pendingIceCandidates.size() == 0) { |
| 1841 | return; |
| 1842 | } |
| 1843 | _networking->perform([threads = _threads, parsedCandidates = _pendingIceCandidates](InstanceNetworking *networking) { |
| 1844 | networking->addCandidates(parsedCandidates); |
| 1845 | }); |
| 1846 | _pendingIceCandidates.clear(); |
| 1847 | } |
| 1848 |
| 1849 | void onNetworkStateUpdated(InstanceNetworking::State const &state) { |
| 1850 | State mappedState; |
| 1851 | if (state.isFailed) { |
| 1852 | mappedState = State::Failed; |
| 1853 | } else if (state.isReadyToSendData) { |
| 1854 | mappedState = State::Established; |
| 1855 | } else { |
| 1856 | mappedState = State::Reconnecting; |
| 1857 | } |
| 1858 |
| 1859 | NetworkStateLogRecord record; |
| 1860 | record.isConnected = state.isReadyToSendData; |
| 1861 | record.route = state.route; |
| 1862 | record.connection = state.connection; |
| 1863 | record.isFailed = state.isFailed; |
| 1864 |
| 1865 | if (state.isReadyToSendData && !_hasBeenConnected) { |
| 1866 | _hasBeenConnected = true; |
| 1867 | auto connectionTimeMs = rtc::TimeMillis() - _startTimestamp; |
| 1868 | RTC_LOG(LS_INFO) << "Connected in " << connectionTimeMs << " ms"; |
| 1869 | } |
| 1870 |
| 1871 | if (!_currentNetworkStateLogRecord || !(_currentNetworkStateLogRecord.value() == record)) { |
| 1872 | _currentNetworkStateLogRecord = record; |
| 1873 | _networkStateLogRecords.emplace_back(rtc::TimeMillis(), std::move(record)); |
| 1874 | } |
| 1875 |
| 1876 | _networkState = state; |
| 1877 | _stateUpdated(mappedState); |
| 1878 | } |
| 1879 |
| 1880 | void onDataChannelStateUpdated(bool isDataChannelOpen) { |
| 1881 | if (_isDataChannelOpen != isDataChannelOpen) { |
| 1882 | _isDataChannelOpen = isDataChannelOpen; |
| 1883 |
| 1884 | if (_isDataChannelOpen) { |
| 1885 | sendMediaState(); |
| 1886 | } |
| 1887 | } |
| 1888 | } |
| 1889 |
| 1890 | void sendDataChannelMessage(signaling::Message const &message) { |
| 1891 | if (!_isDataChannelOpen) { |
| 1892 | RTC_LOG(LS_ERROR) << "sendDataChannelMessage called, but data channel is not open"; |
| 1893 | return; |
| 1894 | } |
| 1895 | auto data = message.serialize(); |
| 1896 | std::string stringData(data.begin(), data.end()); |
| 1897 | RTC_LOG(LS_INFO) << "sendDataChannelMessage: " << stringData; |
| 1898 | _networking->perform([stringData = std::move(stringData)](InstanceNetworking *networking) { |
| 1899 | networking->sendDataChannelMessage(stringData); |
| 1900 | }); |
| 1901 | } |
| 1902 |
| 1903 | void onDataChannelMessage(std::string const &message) { |
| 1904 | RTC_LOG(LS_INFO) << "dataChannelMessage received: " << message; |
| 1905 | std::vector<uint8_t> data(message.begin(), message.end()); |
| 1906 | processSignalingData(data); |
| 1907 | } |
| 1908 |
| 1909 | void sendMediaState() { |
| 1910 | if (!_isDataChannelOpen) { |
| 1911 | return; |
| 1912 | } |
| 1913 | signaling::Message message; |
| 1914 | signaling::MediaStateMessage data; |
| 1915 | data.isMuted = _isMicrophoneMuted; |
| 1916 | data.isBatteryLow = _isBatteryLow; |
| 1917 | if (_outgoingVideoChannel) { |
| 1918 | if (_outgoingVideoChannel->videoCapture()) { |
| 1919 | data.videoState = signaling::MediaStateMessage::VideoState::Active; |
| 1920 | } else{ |
| 1921 | data.videoState = signaling::MediaStateMessage::VideoState::Inactive; |
| 1922 | } |
| 1923 | data.videoRotation = _outgoingVideoChannel->getRotation(); |
| 1924 | } else { |
| 1925 | data.videoState = signaling::MediaStateMessage::VideoState::Inactive; |
| 1926 | data.videoRotation = signaling::MediaStateMessage::VideoRotation::Rotation0; |
| 1927 | } |
| 1928 | if (_outgoingScreencastChannel) { |
| 1929 | if (_outgoingScreencastChannel->videoCapture()) { |
| 1930 | data.screencastState = signaling::MediaStateMessage::VideoState::Active; |
| 1931 | } else{ |
| 1932 | data.screencastState = signaling::MediaStateMessage::VideoState::Inactive; |
| 1933 | } |
| 1934 | } else { |
| 1935 | data.screencastState = signaling::MediaStateMessage::VideoState::Inactive; |
| 1936 | } |
| 1937 | message.data = std::move(data); |
| 1938 | sendDataChannelMessage(message); |
| 1939 | } |
| 1940 |
| 1941 | void sendCandidate(const cricket::Candidate &candidate) { |
| 1942 | cricket::Candidate patchedCandidate = candidate; |
| 1943 | patchedCandidate.set_component(1); |
| 1944 |
| 1945 | signaling::CandidatesMessage data; |
| 1946 |
| 1947 | signaling::IceCandidate serializedCandidate; |
| 1948 |
| 1949 | webrtc::JsepIceCandidate iceCandidate{ std::string(), 0 }; |
| 1950 | iceCandidate.SetCandidate(patchedCandidate); |
| 1951 | std::string serialized; |
| 1952 | const auto success = iceCandidate.ToString(&serialized); |
| 1953 | assert(success); |
| 1954 | (void)success; |
| 1955 |
| 1956 | serializedCandidate.sdpString = serialized; |
| 1957 |
| 1958 | data.iceCandidates.push_back(std::move(serializedCandidate)); |
| 1959 |
| 1960 | signaling::Message message; |
| 1961 | message.data = std::move(data); |
| 1962 | sendSignalingMessage(message); |
| 1963 | } |
| 1964 |
| 1965 | void setVideoCapture(std::shared_ptr<VideoCaptureInterface> videoCapture) { |
| 1966 | auto videoCaptureImpl = GetVideoCaptureAssumingSameThread(videoCapture.get()); |
| 1967 | if (videoCaptureImpl) { |
| 1968 | if (videoCaptureImpl->isScreenCapture()) { |
| 1969 | _videoCapture = nullptr; |
| 1970 | _screencastCapture = videoCapture; |
| 1971 |
| 1972 | if (_outgoingVideoChannelId) { |
| 1973 | _contentNegotiationContext->removeOutgoingChannel(_outgoingVideoChannelId.value()); |
| 1974 | _outgoingVideoChannelId.reset(); |
| 1975 | } |
| 1976 | if (_outgoingVideoChannel) { |
| 1977 | _outgoingVideoChannel->setVideoCapture(nullptr); |
| 1978 | _outgoingVideoChannel.reset(); |
| 1979 | } |
| 1980 |
| 1981 | if (_outgoingScreencastChannel) { |
| 1982 | _outgoingScreencastChannel->setVideoCapture(videoCapture); |
| 1983 | } |
| 1984 | if (!_outgoingScreencastChannelId) { |
| 1985 | _outgoingScreencastChannelId = _contentNegotiationContext->addOutgoingChannel(signaling::MediaContent::Type::Video); |
| 1986 | } |
| 1987 | } else { |
| 1988 | _videoCapture = videoCapture; |
| 1989 | _screencastCapture = nullptr; |
| 1990 |
| 1991 | if (_outgoingScreencastChannelId) { |
| 1992 | _contentNegotiationContext->removeOutgoingChannel(_outgoingScreencastChannelId.value()); |
| 1993 | _outgoingScreencastChannelId.reset(); |
| 1994 | } |
| 1995 | if (_outgoingScreencastChannel) { |
| 1996 | _outgoingScreencastChannel->setVideoCapture(nullptr); |
| 1997 | _outgoingScreencastChannel.reset(); |
| 1998 | } |
| 1999 |
| 2000 | if (_outgoingVideoChannel) { |
| 2001 | _outgoingVideoChannel->setVideoCapture(videoCapture); |
| 2002 | } |
| 2003 | if (!_outgoingVideoChannelId) { |
| 2004 | _outgoingVideoChannelId = _contentNegotiationContext->addOutgoingChannel(signaling::MediaContent::Type::Video); |
| 2005 | } |
| 2006 | } |
| 2007 | } else { |
| 2008 | _videoCapture = nullptr; |
| 2009 | _screencastCapture = nullptr; |
| 2010 |
| 2011 | if (_outgoingVideoChannelId) { |
| 2012 | _contentNegotiationContext->removeOutgoingChannel(_outgoingVideoChannelId.value()); |
| 2013 | _outgoingVideoChannelId.reset(); |
| 2014 | } |
| 2015 | if (_outgoingVideoChannel) { |
| 2016 | _outgoingVideoChannel->setVideoCapture(nullptr); |
| 2017 | _outgoingVideoChannel.reset(); |
| 2018 | } |
| 2019 |
| 2020 | if (_outgoingScreencastChannelId) { |
| 2021 | _contentNegotiationContext->removeOutgoingChannel(_outgoingScreencastChannelId.value()); |
| 2022 | _outgoingScreencastChannelId.reset(); |
| 2023 | } |
| 2024 | if (_outgoingScreencastChannel) { |
| 2025 | _outgoingScreencastChannel->setVideoCapture(nullptr); |
| 2026 | _outgoingScreencastChannel.reset(); |
| 2027 | } |
| 2028 | } |
| 2029 |
| 2030 | if (_handshakeCompleted) { |
| 2031 | sendOfferIfNeeded(); |
| 2032 | sendMediaState(); |
| 2033 | adjustBitratePreferences(true); |
| 2034 | createNegotiatedChannels(); |
| 2035 | } |
| 2036 | } |
| 2037 |
| 2038 | void setRequestedVideoAspect(float aspect) { |
| 2039 | } |
| 2040 |
| 2041 | void setNetworkType(NetworkType networkType) { |
| 2042 |
| 2043 | } |
| 2044 |
| 2045 | void setMuteMicrophone(bool muteMicrophone) { |
| 2046 | if (_isMicrophoneMuted != muteMicrophone) { |
| 2047 | _isMicrophoneMuted = muteMicrophone; |
| 2048 |
| 2049 | if (_outgoingAudioChannel) { |
| 2050 | _outgoingAudioChannel->setIsMuted(muteMicrophone); |
| 2051 | } |
| 2052 |
| 2053 | sendMediaState(); |
| 2054 | } |
| 2055 | } |
| 2056 |
| 2057 | void setIncomingVideoOutput(std::weak_ptr<rtc::VideoSinkInterface<webrtc::VideoFrame>> sink) { |
| 2058 | _currentSink = sink; |
| 2059 | if (_incomingVideoChannel) { |
| 2060 | _incomingVideoChannel->addSink(sink); |
| 2061 | } |
| 2062 | if (_incomingScreencastChannel) { |
| 2063 | _incomingScreencastChannel->addSink(sink); |
| 2064 | } |
| 2065 | } |
| 2066 |
| 2067 | void setAudioInputDevice(std::string id) { |
| 2068 | _threads->getWorkerThread()->BlockingCall([&]() { |
| 2069 | SetAudioInputDeviceById(_audioDeviceModule.get(), id); |
| 2070 | }); |
| 2071 | } |
| 2072 |
| 2073 | void setAudioOutputDevice(std::string id) { |
| 2074 | _threads->getWorkerThread()->BlockingCall([&]() { |
| 2075 | SetAudioOutputDeviceById(_audioDeviceModule.get(), id); |
| 2076 | }); |
| 2077 | } |
| 2078 |
| 2079 | void setIsLowBatteryLevel(bool isLowBatteryLevel) { |
| 2080 | if (_isBatteryLow != isLowBatteryLevel) { |
| 2081 | _isBatteryLow = isLowBatteryLevel; |
| 2082 | sendMediaState(); |
| 2083 | } |
| 2084 | } |
| 2085 |
| 2086 | void stop(std::function<void(FinalState)> completion) { |
| 2087 | FinalState finalState; |
| 2088 |
| 2089 | json11::Json::object statsLog; |
| 2090 |
| 2091 | for (int i = (int)_networkStateLogRecords.size() - 1; i >= 1; i--) { |
| 2092 | // coalesce events within 5ms |
| 2093 | if (_networkStateLogRecords[i].timestamp - _networkStateLogRecords[i - 1].timestamp < 5) { |
| 2094 | _networkStateLogRecords.erase(_networkStateLogRecords.begin() + i - 1); |
| 2095 | } |
| 2096 | } |
| 2097 |
| 2098 | json11::Json::array jsonNetworkStateLogRecords; |
| 2099 | int64_t baseTimestamp = 0; |
| 2100 | for (const auto &record : _networkStateLogRecords) { |
| 2101 | json11::Json::object jsonRecord; |
| 2102 |
| 2103 | if (baseTimestamp == 0) { |
| 2104 | baseTimestamp = record.timestamp; |
| 2105 | } |
| 2106 | jsonRecord.insert(std::make_pair("t", json11::Json(std::to_string(record.timestamp - baseTimestamp)))); |
| 2107 | jsonRecord.insert(std::make_pair("c", json11::Json(record.record.isConnected ? 1 : 0))); |
| 2108 | if (record.record.route) { |
| 2109 | jsonRecord.insert(std::make_pair("local", json11::Json(record.record.route->localDescription))); |
| 2110 | jsonRecord.insert(std::make_pair("remote", json11::Json(record.record.route->remoteDescription))); |
| 2111 | } |
| 2112 | if (record.record.connection) { |
| 2113 | json11::Json::object jsonConnection; |
| 2114 |
| 2115 | auto serializeCandidate = [](InstanceNetworking::ConnectionDescription::CandidateDescription const &candidate) -> json11::Json::object { |
| 2116 | json11::Json::object jsonCandidate; |
| 2117 |
| 2118 | jsonCandidate.insert(std::make_pair("type", json11::Json(candidate.type))); |
| 2119 | jsonCandidate.insert(std::make_pair("protocol", json11::Json(candidate.protocol))); |
| 2120 | jsonCandidate.insert(std::make_pair("address", json11::Json(candidate.address))); |
| 2121 |
| 2122 | return jsonCandidate; |
| 2123 | }; |
| 2124 |
| 2125 | jsonConnection.insert(std::make_pair("local", serializeCandidate(record.record.connection->local))); |
| 2126 | jsonConnection.insert(std::make_pair("remote", serializeCandidate(record.record.connection->remote))); |
| 2127 |
| 2128 | jsonRecord.insert(std::make_pair("network", std::move(jsonConnection))); |
| 2129 | } |
| 2130 | if (record.record.isFailed) { |
| 2131 | jsonRecord.insert(std::make_pair("failed", json11::Json(1))); |
| 2132 | } |
| 2133 |
| 2134 | jsonNetworkStateLogRecords.push_back(std::move(jsonRecord)); |
| 2135 | } |
| 2136 | statsLog.insert(std::make_pair("network", std::move(jsonNetworkStateLogRecords))); |
| 2137 |
| 2138 | json11::Json::array jsonNetworkBitrateLogRecords; |
| 2139 | for (const auto &record : _networkBitrateLogRecords) { |
| 2140 | json11::Json::object jsonRecord; |
| 2141 |
| 2142 | jsonRecord.insert(std::make_pair("b", json11::Json(record.record.bitrate))); |
| 2143 |
| 2144 | jsonNetworkBitrateLogRecords.push_back(std::move(jsonRecord)); |
| 2145 | } |
| 2146 | statsLog.insert(std::make_pair("bitrate", std::move(jsonNetworkBitrateLogRecords))); |
| 2147 |
| 2148 | auto jsonStatsLog = json11::Json(std::move(statsLog)); |
| 2149 |
| 2150 | if (!_statsLogPath.data.empty()) { |
| 2151 | std::ofstream file; |
| 2152 | file.open(_statsLogPath.data); |
| 2153 |
| 2154 | file << jsonStatsLog.dump(); |
| 2155 |
| 2156 | file.close(); |
| 2157 | } |
| 2158 |
| 2159 | completion(finalState); |
| 2160 | } |
| 2161 |
| 2162 | void adjustBitratePreferences(bool resetStartBitrate) { |
| 2163 | if (_outgoingAudioChannel) { |
| 2164 | _outgoingAudioChannel->setMaxBitrate(32 * 1024); |
| 2165 | } |
| 2166 | if (_outgoingVideoChannel) { |
| 2167 | _outgoingVideoChannel->setMaxBitrate(1000 * 1024); |
| 2168 | } |
| 2169 | } |
| 2170 |
| 2171 | private: |
| 2172 | webrtc::scoped_refptr<webrtc::AudioDeviceModule> createAudioDeviceModule() { |
| 2173 | const auto create = [&](webrtc::AudioDeviceModule::AudioLayer layer) { |
| 2174 | #ifdef WEBRTC_IOS |
| 2175 | return rtc::make_ref_counted<webrtc::tgcalls_ios_adm::AudioDeviceModuleIOS>(false, false, false, 1); |
| 2176 | #else |
| 2177 | return webrtc::AudioDeviceModule::Create( |
| 2178 | layer, |
| 2179 | _taskQueueFactory.get()); |
| 2180 | #endif |
| 2181 | }; |
| 2182 | const auto check = [&](const webrtc::scoped_refptr<webrtc::AudioDeviceModule> &result) { |
| 2183 | return (result && result->Init() == 0) ? result : nullptr; |
| 2184 | }; |
| 2185 | if (_createWrappedAudioDeviceModule) { |
| 2186 | auto result = _createWrappedAudioDeviceModule(_taskQueueFactory.get()); |
| 2187 | if (result) { |
| 2188 | return result; |
| 2189 | } |
| 2190 | } |
| 2191 | if (_createAudioDeviceModule) { |
| 2192 | if (const auto result = check(_createAudioDeviceModule(_taskQueueFactory.get()))) { |
| 2193 | return result; |
| 2194 | } |
| 2195 | } |
| 2196 | return check(create(webrtc::AudioDeviceModule::kPlatformDefaultAudio)); |
| 2197 | } |
| 2198 |
| 2199 | private: |
| 2200 | webrtc::Environment _webrtcEnvironment; |
| 2201 | SignalingProtocolVersion _signalingProtocolVersion = SignalingProtocolVersion::V3; |
| 2202 | std::shared_ptr<Threads> _threads; |
| 2203 | std::vector<RtcServer> _rtcServers; |
| 2204 | std::unique_ptr<Proxy> _proxy; |
| 2205 | std::shared_ptr<DirectConnectionChannel> _directConnectionChannel; |
| 2206 | bool _enableP2P = false; |
| 2207 | bool _enableStunMarking = false; |
| 2208 | EncryptionKey _encryptionKey; |
| 2209 | std::function<void(State)> _stateUpdated; |
| 2210 | std::function<void(int)> _signalBarsUpdated; |
| 2211 | std::function<void(float, float)> _audioLevelsUpdated; |
| 2212 | std::function<void(bool)> _remoteBatteryLevelIsLowUpdated; |
| 2213 | std::function<void(AudioState, VideoState)> _remoteMediaStateUpdated; |
| 2214 | std::function<void(float)> _remotePrefferedAspectRatioUpdated; |
| 2215 | std::function<void(const std::vector<uint8_t> &)> _signalingDataEmitted; |
| 2216 | std::function<webrtc::scoped_refptr<webrtc::AudioDeviceModule>(webrtc::TaskQueueFactory*)> _createAudioDeviceModule; |
| 2217 | std::function<webrtc::scoped_refptr<WrappedAudioDeviceModule>(webrtc::TaskQueueFactory*)> _createWrappedAudioDeviceModule; |
| 2218 | MediaDevicesConfig _devicesConfig; |
| 2219 | FilePath _statsLogPath; |
| 2220 |
| 2221 | std::map<std::string, json11::Json> _customParameters; |
| 2222 |
| 2223 | std::shared_ptr<SignalingConnection> _signalingConnection; |
| 2224 | std::unique_ptr<EncryptedConnection> _signalingEncryptedConnection; |
| 2225 |
| 2226 | int64_t _startTimestamp = 0; |
| 2227 | bool _hasBeenConnected = false; |
| 2228 |
| 2229 | absl::optional<NetworkStateLogRecord> _currentNetworkStateLogRecord; |
| 2230 | std::vector<StateLogRecord<NetworkStateLogRecord>> _networkStateLogRecords; |
| 2231 | std::vector<StateLogRecord<NetworkBitrateLogRecord>> _networkBitrateLogRecords; |
| 2232 |
| 2233 | absl::optional<InstanceNetworking::State> _networkState; |
| 2234 |
| 2235 | bool _handshakeCompleted = false; |
| 2236 | std::vector<cricket::Candidate> _pendingIceCandidates; |
| 2237 | bool _isDataChannelOpen = false; |
| 2238 |
| 2239 | std::unique_ptr<webrtc::RtcEventLogNull> _eventLog; |
| 2240 | std::unique_ptr<webrtc::TaskQueueFactory> _taskQueueFactory; |
| 2241 | std::unique_ptr<webrtc::Call> _call; |
| 2242 | webrtc::LocalAudioSinkAdapter _audioSource; |
| 2243 | webrtc::scoped_refptr<webrtc::AudioDeviceModule> _audioDeviceModule; |
| 2244 |
| 2245 | std::unique_ptr<rtc::UniqueRandomIdGenerator> _uniqueRandomIdGenerator; |
| 2246 | webrtc::RtpTransport *_rtpTransport = nullptr; |
| 2247 | std::unique_ptr<ChannelManager> _channelManager; |
| 2248 | std::unique_ptr<webrtc::VideoBitrateAllocatorFactory> _videoBitrateAllocatorFactory; |
| 2249 | std::string _initialInputDeviceId; |
| 2250 | std::string _initialOutputDeviceId; |
| 2251 |
| 2252 | std::unique_ptr<ContentNegotiationContext> _contentNegotiationContext; |
| 2253 |
| 2254 | std::shared_ptr<ThreadLocalObject<InstanceNetworking>> _networking; |
| 2255 |
| 2256 | absl::optional<std::string> _outgoingAudioChannelId; |
| 2257 | std::unique_ptr<OutgoingAudioChannel> _outgoingAudioChannel; |
| 2258 | bool _isMicrophoneMuted = false; |
| 2259 |
| 2260 | std::vector<webrtc::SdpVideoFormat> _availableVideoFormats; |
| 2261 |
| 2262 | absl::optional<std::string> _outgoingVideoChannelId; |
| 2263 | std::shared_ptr<OutgoingVideoChannel> _outgoingVideoChannel; |
| 2264 | absl::optional<std::string> _outgoingScreencastChannelId; |
| 2265 | std::shared_ptr<OutgoingVideoChannel> _outgoingScreencastChannel; |
| 2266 |
| 2267 | bool _isBatteryLow = false; |
| 2268 |
| 2269 | std::unique_ptr<IncomingV2AudioChannel> _incomingAudioChannel; |
| 2270 | std::unique_ptr<IncomingV2VideoChannel> _incomingVideoChannel; |
| 2271 | std::unique_ptr<IncomingV2VideoChannel> _incomingScreencastChannel; |
| 2272 |
| 2273 | std::weak_ptr<rtc::VideoSinkInterface<webrtc::VideoFrame>> _currentSink; |
| 2274 |
| 2275 | std::shared_ptr<VideoCaptureInterface> _videoCapture; |
| 2276 | std::shared_ptr<VideoCaptureInterface> _screencastCapture; |
| 2277 | std::shared_ptr<PlatformContext> _platformContext; |
| 2278 | }; |
| 2279 |
| 2280 | InstanceV2Impl::InstanceV2Impl(Descriptor &&descriptor) { |
| 2281 | if (descriptor.config.logPath.data.size() != 0) { |
| 2282 | _logSink = std::make_unique<LogSinkImpl>(descriptor.config.logPath); |
| 2283 | } |
| 2284 | #ifdef DEBUG |
| 2285 | rtc::LogMessage::LogToDebug(rtc::LS_VERBOSE); |
| 2286 | #else |
| 2287 | rtc::LogMessage::LogToDebug(rtc::LS_INFO); |
| 2288 | #endif |
| 2289 | rtc::LogMessage::SetLogToStderr(false); |
| 2290 | if (_logSink) { |
| 2291 | rtc::LogMessage::AddLogToStream(_logSink.get(), rtc::LS_INFO); |
| 2292 | } |
| 2293 |
| 2294 | _threads = StaticThreads::getThreads(); |
| 2295 | _internal.reset(new ThreadLocalObject<InstanceV2ImplInternal>(_threads->getMediaThread(), [descriptor = std::move(descriptor), threads = _threads]() mutable { |
| 2296 | return std::make_shared<InstanceV2ImplInternal>(std::move(descriptor), threads); |
| 2297 | })); |
| 2298 | _internal->perform([](InstanceV2ImplInternal *internal) { |
| 2299 | internal->start(); |
| 2300 | }); |
| 2301 | } |
| 2302 |
| 2303 | InstanceV2Impl::~InstanceV2Impl() { |
| 2304 | rtc::LogMessage::RemoveLogToStream(_logSink.get()); |
| 2305 | } |
| 2306 |
| 2307 | void InstanceV2Impl::receiveSignalingData(const std::vector<uint8_t> &data) { |
| 2308 | _internal->perform([data](InstanceV2ImplInternal *internal) { |
| 2309 | internal->receiveSignalingData(data); |
| 2310 | }); |
| 2311 | } |
| 2312 |
| 2313 | void InstanceV2Impl::setVideoCapture(std::shared_ptr<VideoCaptureInterface> videoCapture) { |
| 2314 | _internal->perform([videoCapture](InstanceV2ImplInternal *internal) { |
| 2315 | internal->setVideoCapture(videoCapture); |
| 2316 | }); |
| 2317 | } |
| 2318 |
| 2319 | void InstanceV2Impl::setRequestedVideoAspect(float aspect) { |
| 2320 | _internal->perform([aspect](InstanceV2ImplInternal *internal) { |
| 2321 | internal->setRequestedVideoAspect(aspect); |
| 2322 | }); |
| 2323 | } |
| 2324 |
| 2325 | void InstanceV2Impl::setNetworkType(NetworkType networkType) { |
| 2326 | _internal->perform([networkType](InstanceV2ImplInternal *internal) { |
| 2327 | internal->setNetworkType(networkType); |
| 2328 | }); |
| 2329 | } |
| 2330 |
| 2331 | void InstanceV2Impl::setMuteMicrophone(bool muteMicrophone) { |
| 2332 | _internal->perform([muteMicrophone](InstanceV2ImplInternal *internal) { |
| 2333 | internal->setMuteMicrophone(muteMicrophone); |
| 2334 | }); |
| 2335 | } |
| 2336 |
| 2337 | void InstanceV2Impl::setIncomingVideoOutput(std::weak_ptr<rtc::VideoSinkInterface<webrtc::VideoFrame>> sink) { |
| 2338 | _internal->perform([sink](InstanceV2ImplInternal *internal) { |
| 2339 | internal->setIncomingVideoOutput(sink); |
| 2340 | }); |
| 2341 | } |
| 2342 |
| 2343 | void InstanceV2Impl::setAudioInputDevice(std::string id) { |
| 2344 | _internal->perform([id](InstanceV2ImplInternal *internal) { |
| 2345 | internal->setAudioInputDevice(id); |
| 2346 | }); |
| 2347 | } |
| 2348 |
| 2349 | void InstanceV2Impl::setAudioOutputDevice(std::string id) { |
| 2350 | _internal->perform([id](InstanceV2ImplInternal *internal) { |
| 2351 | internal->setAudioOutputDevice(id); |
| 2352 | }); |
| 2353 | } |
| 2354 |
| 2355 | void InstanceV2Impl::setIsLowBatteryLevel(bool isLowBatteryLevel) { |
| 2356 | _internal->perform([isLowBatteryLevel](InstanceV2ImplInternal *internal) { |
| 2357 | internal->setIsLowBatteryLevel(isLowBatteryLevel); |
| 2358 | }); |
| 2359 | } |
| 2360 |
| 2361 | void InstanceV2Impl::setInputVolume(float level) { |
| 2362 | } |
| 2363 |
| 2364 | void InstanceV2Impl::setOutputVolume(float level) { |
| 2365 | } |
| 2366 |
| 2367 | void InstanceV2Impl::setAudioOutputDuckingEnabled(bool enabled) { |
| 2368 | } |
| 2369 |
| 2370 | void InstanceV2Impl::setAudioOutputGainControlEnabled(bool enabled) { |
| 2371 | } |
| 2372 |
| 2373 | void InstanceV2Impl::setEchoCancellationStrength(int strength) { |
| 2374 | } |
| 2375 |
| 2376 | std::vector<std::string> InstanceV2Impl::GetVersions() { |
| 2377 | std::vector<std::string> result; |
| 2378 | result.push_back("7.0.0"); |
| 2379 | result.push_back("8.0.0"); |
| 2380 | result.push_back("9.0.0"); |
| 2381 | result.push_back("12.0.0"); |
| 2382 | result.push_back("13.0.0"); |
| 2383 | return result; |
| 2384 | } |
| 2385 |
| 2386 | int InstanceV2Impl::GetConnectionMaxLayer() { |
| 2387 | return 92; |
| 2388 | } |
| 2389 |
| 2390 | std::string InstanceV2Impl::getLastError() { |
| 2391 | return ""; |
| 2392 | } |
| 2393 |
| 2394 | std::string InstanceV2Impl::getDebugInfo() { |
| 2395 | return ""; |
| 2396 | } |
| 2397 |
| 2398 | int64_t InstanceV2Impl::getPreferredRelayId() { |
| 2399 | return 0; |
| 2400 | } |
| 2401 |
| 2402 | TrafficStats InstanceV2Impl::getTrafficStats() { |
| 2403 | return {}; |
| 2404 | } |
| 2405 |
| 2406 | PersistentState InstanceV2Impl::getPersistentState() { |
| 2407 | return {}; |
| 2408 | } |
| 2409 |
| 2410 | void InstanceV2Impl::stop(std::function<void(FinalState)> completion) { |
| 2411 | std::string debugLog; |
| 2412 | if (_logSink) { |
| 2413 | debugLog = _logSink->result(); |
| 2414 | } |
| 2415 | _internal->perform([completion, debugLog = std::move(debugLog)](InstanceV2ImplInternal *internal) mutable { |
| 2416 | internal->stop([completion, debugLog = std::move(debugLog)](FinalState finalState) mutable { |
| 2417 | finalState.debugLog = debugLog; |
| 2418 | completion(finalState); |
| 2419 | }); |
| 2420 | }); |
| 2421 | } |
| 2422 |
| 2423 | template <> |
| 2424 | bool Register<InstanceV2Impl>() { |
| 2425 | return Meta::RegisterOne<InstanceV2Impl>(); |
| 2426 | } |
| 2427 |
| 2428 | } // namespace tgcalls |
| 2429 |