| #include "rtc_base/thread.h" |
| 6 | #include "rtc_base/logging.h" |
| 7 | #include "api/peer_connection_interface.h" |
| 8 | #include "api/task_queue/default_task_queue_factory.h" |
| 9 | #include "media/engine/webrtc_media_engine.h" |
| 10 | #include "api/audio_codecs/builtin_audio_encoder_factory.h" |
| 11 | #include "api/audio_codecs/builtin_audio_decoder_factory.h" |
| 12 | #include "api/rtc_event_log/rtc_event_log_factory.h" |
| 13 | #include "sdk/media_constraints.h" |
| 14 | #include "api/peer_connection_interface.h" |
| 15 | #include "api/video_track_source_proxy.h" |
| 16 | #include "system_wrappers/include/field_trial.h" |
| 17 | #include "api/stats/rtcstats_objects.h" |
| 18 |
| 19 | #include "ThreadLocalObject.h" |
| 20 | #include "Manager.h" |
| 21 | #include "NetworkManager.h" |
| 22 | #include "VideoCaptureInterfaceImpl.h" |
| 23 | #include "platform/PlatformInterface.h" |
| 24 | #include "LogSinkImpl.h" |
| 25 | #include "StaticThreads.h" |
| 26 |
| 27 | namespace tgcalls { |
| 28 | namespace { |
| 29 |
| 30 | VideoCaptureInterfaceObject *GetVideoCaptureAssumingSameThread(VideoCaptureInterface *videoCapture) { |
| 31 | return videoCapture |
| 32 | ? static_cast<VideoCaptureInterfaceImpl*>(videoCapture)->object()->getSyncAssumingSameThread() |
| 33 | : nullptr; |
| 34 | } |
| 35 |
| 36 | class PeerConnectionObserverImpl : public webrtc::PeerConnectionObserver { |
| 37 | private: |
| 38 | std::function<void(std::string, int, std::string)> _discoveredIceCandidate; |
| 39 | std::function<void(bool)> _connectionStateChanged; |
| 40 | std::function<void(rtc::scoped_refptr<webrtc::RtpTransceiverInterface> transceiver)> _onTrack; |
| 41 |
| 42 | public: |
| 43 | PeerConnectionObserverImpl( |
| 44 | std::function<void(std::string, int, std::string)> discoveredIceCandidate, |
| 45 | std::function<void(bool)> connectionStateChanged, |
| 46 | std::function<void(rtc::scoped_refptr<webrtc::RtpTransceiverInterface> transceiver)> onTrack |
| 47 | ) : |
| 48 | _discoveredIceCandidate(discoveredIceCandidate), |
| 49 | _connectionStateChanged(connectionStateChanged), |
| 50 | _onTrack(onTrack) { |
| 51 | } |
| 52 |
| 53 | virtual void OnSignalingChange(webrtc::PeerConnectionInterface::SignalingState new_state) { |
| 54 | bool isConnected = false; |
| 55 | if (new_state == webrtc::PeerConnectionInterface::SignalingState::kStable) { |
| 56 | isConnected = true; |
| 57 | } |
| 58 | _connectionStateChanged(isConnected); |
| 59 | } |
| 60 |
| 61 | virtual void OnAddStream(rtc::scoped_refptr<webrtc::MediaStreamInterface> stream) { |
| 62 | } |
| 63 |
| 64 | virtual void OnRemoveStream(rtc::scoped_refptr<webrtc::MediaStreamInterface> stream) { |
| 65 | } |
| 66 |
| 67 | virtual void OnDataChannel(rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel) { |
| 68 | } |
| 69 |
| 70 | virtual void OnRenegotiationNeeded() { |
| 71 | } |
| 72 |
| 73 | virtual void OnIceConnectionChange(webrtc::PeerConnectionInterface::IceConnectionState new_state) { |
| 74 | } |
| 75 |
| 76 | virtual void OnStandardizedIceConnectionChange(webrtc::PeerConnectionInterface::IceConnectionState new_state) { |
| 77 | } |
| 78 |
| 79 | virtual void OnConnectionChange(webrtc::PeerConnectionInterface::PeerConnectionState new_state) { |
| 80 | } |
| 81 |
| 82 | virtual void OnIceGatheringChange(webrtc::PeerConnectionInterface::IceGatheringState new_state) { |
| 83 | } |
| 84 |
| 85 | virtual void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) { |
| 86 | std::string sdp; |
| 87 | candidate->ToString(&sdp); |
| 88 | _discoveredIceCandidate(sdp, candidate->sdp_mline_index(), candidate->sdp_mid()); |
| 89 | } |
| 90 |
| 91 | virtual void OnIceCandidateError(const std::string& host_candidate, const std::string& url, int error_code, const std::string& error_text) { |
| 92 | } |
| 93 |
| 94 | virtual void OnIceCandidateError(const std::string& address, |
| 95 | int port, |
| 96 | const std::string& url, |
| 97 | int error_code, |
| 98 | const std::string& error_text) { |
| 99 | } |
| 100 |
| 101 | virtual void OnIceCandidatesRemoved(const std::vector<cricket::Candidate>& candidates) { |
| 102 | } |
| 103 |
| 104 | virtual void OnIceConnectionReceivingChange(bool receiving) { |
| 105 | } |
| 106 |
| 107 | virtual void OnIceSelectedCandidatePairChanged(const cricket::CandidatePairChangeEvent& event) { |
| 108 | } |
| 109 |
| 110 | virtual void OnAddTrack(rtc::scoped_refptr<webrtc::RtpReceiverInterface> receiver, const std::vector<rtc::scoped_refptr<webrtc::MediaStreamInterface>>& streams) { |
| 111 | } |
| 112 |
| 113 | virtual void OnTrack(rtc::scoped_refptr<webrtc::RtpTransceiverInterface> transceiver) { |
| 114 | _onTrack(transceiver); |
| 115 | } |
| 116 |
| 117 | virtual void OnRemoveTrack(rtc::scoped_refptr<webrtc::RtpReceiverInterface> receiver) { |
| 118 | } |
| 119 |
| 120 | virtual void OnInterestingUsage(int usage_pattern) { |
| 121 | } |
| 122 | }; |
| 123 |
| 124 | class RTCStatsCollectorCallbackImpl : public webrtc::RTCStatsCollectorCallback { |
| 125 | public: |
| 126 | RTCStatsCollectorCallbackImpl(std::function<void(const rtc::scoped_refptr<const webrtc::RTCStatsReport> &)> completion) : |
| 127 | _completion(completion) { |
| 128 | } |
| 129 |
| 130 | virtual void OnStatsDelivered(const rtc::scoped_refptr<const webrtc::RTCStatsReport> &report) override { |
| 131 | _completion(report); |
| 132 | } |
| 133 |
| 134 | private: |
| 135 | std::function<void(const rtc::scoped_refptr<const webrtc::RTCStatsReport> &)> _completion; |
| 136 | }; |
| 137 |
| 138 | class CreateSessionDescriptionObserverImpl : public webrtc::CreateSessionDescriptionObserver { |
| 139 | private: |
| 140 | std::function<void(std::string, std::string)> _completion; |
| 141 |
| 142 | public: |
| 143 | CreateSessionDescriptionObserverImpl(std::function<void(std::string, std::string)> completion) : |
| 144 | _completion(completion) { |
| 145 | } |
| 146 |
| 147 | virtual void OnSuccess(webrtc::SessionDescriptionInterface* desc) override { |
| 148 | if (desc) { |
| 149 | std::string sdp; |
| 150 | desc->ToString(&sdp); |
| 151 |
| 152 | _completion(sdp, desc->type()); |
| 153 | } |
| 154 | } |
| 155 |
| 156 | virtual void OnFailure(webrtc::RTCError error) override { |
| 157 | } |
| 158 | }; |
| 159 |
| 160 | class SetSessionDescriptionObserverImpl : public webrtc::SetSessionDescriptionObserver { |
| 161 | private: |
| 162 | std::function<void()> _completion; |
| 163 |
| 164 | public: |
| 165 | SetSessionDescriptionObserverImpl(std::function<void()> completion) : |
| 166 | _completion(completion) { |
| 167 | } |
| 168 |
| 169 | virtual void OnSuccess() override { |
| 170 | _completion(); |
| 171 | } |
| 172 |
| 173 | virtual void OnFailure(webrtc::RTCError error) override { |
| 174 | } |
| 175 | }; |
| 176 |
| 177 | struct StatsData { |
| 178 | int32_t packetsReceived = 0; |
| 179 | int32_t packetsLost = 0; |
| 180 | }; |
| 181 |
| 182 | struct IceCandidateData { |
| 183 | std::string sdpMid; |
| 184 | int mid; |
| 185 | std::string sdp; |
| 186 |
| 187 | IceCandidateData(std::string _sdpMid, int _mid, std::string _sdp) : |
| 188 | sdpMid(_sdpMid), |
| 189 | mid(_mid), |
| 190 | sdp(_sdp) { |
| 191 | } |
| 192 | }; |
| 193 |
| 194 | } //namespace |
| 195 |
| 196 | class InstanceImplReferenceInternal final : public std::enable_shared_from_this<InstanceImplReferenceInternal> { |
| 197 | public: |
| 198 | InstanceImplReferenceInternal( |
| 199 | const Descriptor &descriptor |
| 200 | ) : |
| 201 | _encryptionKey(descriptor.encryptionKey), |
| 202 | _rtcServers(descriptor.rtcServers), |
| 203 | _enableP2P(descriptor.config.enableP2P), |
| 204 | _stateUpdated(descriptor.stateUpdated), |
| 205 | _signalBarsUpdated(descriptor.signalBarsUpdated), |
| 206 | _signalingDataEmitted(descriptor.signalingDataEmitted), |
| 207 | _remoteMediaStateUpdated(descriptor.remoteMediaStateUpdated), |
| 208 | _remoteBatteryLevelIsLowUpdated(descriptor.remoteBatteryLevelIsLowUpdated), |
| 209 | _remotePrefferedAspectRatioUpdated(descriptor.remotePrefferedAspectRatioUpdated), |
| 210 | _videoCapture(descriptor.videoCapture), |
| 211 | _state(State::Reconnecting), |
| 212 | _videoState(_videoCapture ? VideoState::Active : VideoState::Inactive), |
| 213 | _platformContext(descriptor.platformContext) { |
| 214 | assert(StaticThreads::getMediaThread()->IsCurrent()); |
| 215 |
| 216 | rtc::LogMessage::LogToDebug(rtc::LS_INFO); |
| 217 | rtc::LogMessage::SetLogToStderr(false); |
| 218 |
| 219 | /*webrtc::field_trial::InitFieldTrialsFromString( |
| 220 | "WebRTC-Audio-SendSideBwe/Enabled/" |
| 221 | "WebRTC-Audio-Allocation/min:6kbps,max:32kbps/" |
| 222 | "WebRTC-Audio-OpusMinPacketLossRate/Enabled-1/" |
| 223 | "WebRTC-FlexFEC-03/Enabled/" |
| 224 | "WebRTC-FlexFEC-03-Advertised/Enabled/" |
| 225 | "WebRTC-Audio-BitrateAdaptation/Enabled/WebRTC-Audio-FecAdaptation/Enabled/" |
| 226 | );*/ |
| 227 |
| 228 | _streamIds.push_back("stream"); |
| 229 | } |
| 230 |
| 231 | ~InstanceImplReferenceInternal() { |
| 232 | assert(StaticThreads::getMediaThread()->IsCurrent()); |
| 233 |
| 234 | _peerConnection->Close(); |
| 235 | } |
| 236 |
| 237 | void start() { |
| 238 | const auto weak = std::weak_ptr<InstanceImplReferenceInternal>(shared_from_this()); |
| 239 |
| 240 | PlatformInterface::SharedInstance()->configurePlatformAudio(); |
| 241 |
| 242 | _signalingConnection.reset(new EncryptedConnection( |
| 243 | EncryptedConnection::Type::Signaling, |
| 244 | _encryptionKey, |
| 245 | [weak](int delayMs, int cause) { |
| 246 | if (delayMs == 0) { |
| 247 | StaticThreads::getMediaThread()->PostTask(RTC_FROM_HERE, [weak, cause](){ |
| 248 | auto strong = weak.lock(); |
| 249 | if (!strong) { |
| 250 | return; |
| 251 | } |
| 252 | strong->sendPendingServiceMessages(cause); |
| 253 | }); |
| 254 | } else { |
| 255 | StaticThreads::getMediaThread()->PostDelayedTask(RTC_FROM_HERE, [weak, cause]() { |
| 256 | auto strong = weak.lock(); |
| 257 | if (!strong) { |
| 258 | return; |
| 259 | } |
| 260 | strong->sendPendingServiceMessages(cause); |
| 261 | }, delayMs); |
| 262 | } |
| 263 | } |
| 264 | )); |
| 265 |
| 266 | webrtc::PeerConnectionFactoryDependencies dependencies; |
| 267 | dependencies.network_thread = StaticThreads::getNetworkThread(); |
| 268 | dependencies.worker_thread = StaticThreads::getWorkerThread(); |
| 269 | dependencies.signaling_thread = StaticThreads::getMediaThread(); |
| 270 | dependencies.task_queue_factory = webrtc::CreateDefaultTaskQueueFactory(); |
| 271 |
| 272 | cricket::MediaEngineDependencies mediaDeps; |
| 273 | mediaDeps.task_queue_factory = dependencies.task_queue_factory.get(); |
| 274 | mediaDeps.audio_encoder_factory = webrtc::CreateBuiltinAudioEncoderFactory(); |
| 275 | mediaDeps.audio_decoder_factory = webrtc::CreateBuiltinAudioDecoderFactory(); |
| 276 | mediaDeps.video_encoder_factory = PlatformInterface::SharedInstance()->makeVideoEncoderFactory(_platformContext); |
| 277 | mediaDeps.video_decoder_factory = PlatformInterface::SharedInstance()->makeVideoDecoderFactory(_platformContext); |
| 278 |
| 279 | webrtc::AudioProcessing *apm = webrtc::AudioProcessingBuilder().Create(); |
| 280 | webrtc::AudioProcessing::Config audioConfig; |
| 281 | webrtc::AudioProcessing::Config::NoiseSuppression noiseSuppression; |
| 282 | noiseSuppression.enabled = true; |
| 283 | noiseSuppression.level = webrtc::AudioProcessing::Config::NoiseSuppression::kHigh; |
| 284 | audioConfig.noise_suppression = noiseSuppression; |
| 285 |
| 286 | audioConfig.high_pass_filter.enabled = true; |
| 287 |
| 288 | apm->ApplyConfig(audioConfig); |
| 289 |
| 290 | mediaDeps.audio_processing = apm; |
| 291 |
| 292 | dependencies.media_engine = cricket::CreateMediaEngine(std::move(mediaDeps)); |
| 293 | dependencies.call_factory = webrtc::CreateCallFactory(); |
| 294 | dependencies.event_log_factory = |
| 295 | std::make_unique<webrtc::RtcEventLogFactory>(dependencies.task_queue_factory.get()); |
| 296 | dependencies.network_controller_factory = nullptr; |
| 297 | //dependencies.media_transport_factory = nullptr; |
| 298 |
| 299 | _nativeFactory = webrtc::CreateModularPeerConnectionFactory(std::move(dependencies)); |
| 300 |
| 301 | webrtc::PeerConnectionInterface::RTCConfiguration config; |
| 302 | config.sdp_semantics = webrtc::SdpSemantics::kUnifiedPlan; |
| 303 | //config.continual_gathering_policy = webrtc::PeerConnectionInterface::ContinualGatheringPolicy::GATHER_CONTINUALLY; |
| 304 | /*config.audio_jitter_buffer_fast_accelerate = true; |
| 305 | config.prioritize_most_likely_ice_candidate_pairs = true; |
| 306 | config.presume_writable_when_fully_relayed = true; |
| 307 | config.audio_jitter_buffer_enable_rtx_handling = true;*/ |
| 308 |
| 309 | for (auto &server : _rtcServers) { |
| 310 | if (server.isTurn) { |
| 311 | webrtc::PeerConnectionInterface::IceServer iceServer; |
| 312 | std::ostringstream uri; |
| 313 | uri << "turn:"; |
| 314 | uri << server.host; |
| 315 | uri << ":"; |
| 316 | uri << server.port; |
| 317 | iceServer.uri = uri.str(); |
| 318 | iceServer.username = server.login; |
| 319 | iceServer.password = server.password; |
| 320 | config.servers.push_back(iceServer); |
| 321 | } else { |
| 322 | webrtc::PeerConnectionInterface::IceServer iceServer; |
| 323 | std::ostringstream uri; |
| 324 | uri << "stun:"; |
| 325 | uri << server.host; |
| 326 | uri << ":"; |
| 327 | uri << server.port; |
| 328 | iceServer.uri = uri.str(); |
| 329 | config.servers.push_back(iceServer); |
| 330 | } |
| 331 | } |
| 332 |
| 333 | if (true || !_enableP2P) { |
| 334 | config.type = webrtc::PeerConnectionInterface::kRelay; |
| 335 | } |
| 336 |
| 337 | _observer.reset(new PeerConnectionObserverImpl( |
| 338 | [weak](std::string sdp, int mid, std::string sdpMid) { |
| 339 | StaticThreads::getMediaThread()->PostTask(RTC_FROM_HERE, [weak, sdp, mid, sdpMid](){ |
| 340 | auto strong = weak.lock(); |
| 341 | if (strong) { |
| 342 | strong->emitIceCandidate(sdp, mid, sdpMid); |
| 343 | } |
| 344 | }); |
| 345 | }, |
| 346 | [weak](bool isConnected) { |
| 347 | StaticThreads::getMediaThread()->PostTask(RTC_FROM_HERE, [weak, isConnected](){ |
| 348 | auto strong = weak.lock(); |
| 349 | if (strong) { |
| 350 | strong->updateIsConnected(isConnected); |
| 351 | } |
| 352 | }); |
| 353 | }, |
| 354 | [weak](rtc::scoped_refptr<webrtc::RtpTransceiverInterface> transceiver) { |
| 355 | StaticThreads::getMediaThread()->PostTask(RTC_FROM_HERE, [weak, transceiver](){ |
| 356 | auto strong = weak.lock(); |
| 357 | if (!strong) { |
| 358 | return; |
| 359 | } |
| 360 | strong->onTrack(transceiver); |
| 361 | }); |
| 362 | } |
| 363 | )); |
| 364 | _peerConnection = _nativeFactory->CreatePeerConnection(config, nullptr, nullptr, _observer.get()); |
| 365 | assert(_peerConnection != nullptr); |
| 366 |
| 367 | cricket::AudioOptions options; |
| 368 | rtc::scoped_refptr<webrtc::AudioSourceInterface> audioSource = _nativeFactory->CreateAudioSource(options); |
| 369 | _localAudioTrack = _nativeFactory->CreateAudioTrack("audio0", audioSource); |
| 370 | _peerConnection->AddTrack(_localAudioTrack, _streamIds); |
| 371 |
| 372 | if (_videoCapture) { |
| 373 | beginSendingVideo(); |
| 374 | } |
| 375 |
| 376 | if (_encryptionKey.isOutgoing) { |
| 377 | emitOffer(); |
| 378 | } |
| 379 |
| 380 | beginStatsTimer(1000); |
| 381 | } |
| 382 |
| 383 | void setMuteMicrophone(bool muteMicrophone) { |
| 384 | _localAudioTrack->set_enabled(!muteMicrophone); |
| 385 | changeAudioState(muteMicrophone ? AudioState::Muted : AudioState::Active); |
| 386 | } |
| 387 |
| 388 | void setIncomingVideoOutput(std::shared_ptr<rtc::VideoSinkInterface<webrtc::VideoFrame>> sink) { |
| 389 | if (!sink) { |
| 390 | return; |
| 391 | } |
| 392 | _currentSink = sink; |
| 393 | if (_remoteVideoTrack) { |
| 394 | _remoteVideoTrack->AddOrUpdateSink(_currentSink.get(), rtc::VideoSinkWants()); |
| 395 | } |
| 396 | } |
| 397 |
| 398 | void setVideoCapture(std::shared_ptr<VideoCaptureInterface> videoCapture) { |
| 399 | assert(videoCapture != nullptr); |
| 400 |
| 401 | _videoCapture = videoCapture; |
| 402 |
| 403 | if (_preferredAspectRatio > 0.01f) { |
| 404 | VideoCaptureInterfaceObject *videoCaptureImpl = GetVideoCaptureAssumingSameThread(_videoCapture.get()); |
| 405 | videoCaptureImpl->setPreferredAspectRatio(_preferredAspectRatio); |
| 406 | } |
| 407 | beginSendingVideo(); |
| 408 | } |
| 409 |
| 410 | void sendVideoDeviceUpdated() { |
| 411 | } |
| 412 |
| 413 | void setRequestedVideoAspect(float aspect) { |
| 414 | } |
| 415 |
| 416 | void receiveSignalingData(const std::vector<uint8_t> &data) { |
| 417 | if (true) { |
| 418 | rtc::CopyOnWriteBuffer packet; |
| 419 | packet.SetData(data.data(), data.size()); |
| 420 | processSignalingData(packet); |
| 421 | return; |
| 422 | } |
| 423 |
| 424 | if (const auto packet = _signalingConnection->handleIncomingPacket((const char *)data.data(), data.size())) { |
| 425 | const auto mainMessage = &packet->main.message.data; |
| 426 | if (const auto signalingData = absl::get_if<UnstructuredDataMessage>(mainMessage)) { |
| 427 | processSignalingData(signalingData->data); |
| 428 | } |
| 429 | for (auto &it : packet->additional) { |
| 430 | const auto additionalMessage = &it.message.data; |
| 431 | if (const auto signalingData = absl::get_if<UnstructuredDataMessage>(additionalMessage)) { |
| 432 | processSignalingData(signalingData->data); |
| 433 | } |
| 434 | } |
| 435 | } |
| 436 | } |
| 437 |
| 438 | void processSignalingData(const rtc::CopyOnWriteBuffer &decryptedPacket) { |
| 439 | rtc::ByteBufferReader reader((const char *)decryptedPacket.data(), decryptedPacket.size()); |
| 440 | uint8_t command = 0; |
| 441 | if (!reader.ReadUInt8(&command)) { |
| 442 | return; |
| 443 | } |
| 444 | if (command == 1) { |
| 445 | uint32_t sdpLength = 0; |
| 446 | if (!reader.ReadUInt32(&sdpLength)) { |
| 447 | return; |
| 448 | } |
| 449 | std::string sdp; |
| 450 | if (!reader.ReadString(&sdp, sdpLength)) { |
| 451 | return; |
| 452 | } |
| 453 | uint32_t mid = 0; |
| 454 | if (!reader.ReadUInt32(&mid)) { |
| 455 | return; |
| 456 | } |
| 457 | uint32_t sdpMidLength = 0; |
| 458 | if (!reader.ReadUInt32(&sdpMidLength)) { |
| 459 | return; |
| 460 | } |
| 461 | std::string sdpMid; |
| 462 | if (!reader.ReadString(&sdpMid, sdpMidLength)) { |
| 463 | return; |
| 464 | } |
| 465 | _pendingRemoteIceCandidates.push_back(std::make_shared<IceCandidateData>(sdpMid, mid, sdp)); |
| 466 | processRemoteIceCandidatesIfReady(); |
| 467 | } else if (command == 2) { |
| 468 | uint32_t sdpLength = 0; |
| 469 | if (!reader.ReadUInt32(&sdpLength)) { |
| 470 | return; |
| 471 | } |
| 472 | std::string sdp; |
| 473 | if (!reader.ReadString(&sdp, sdpLength)) { |
| 474 | return; |
| 475 | } |
| 476 | uint32_t typeLength = 0; |
| 477 | if (!reader.ReadUInt32(&typeLength)) { |
| 478 | return; |
| 479 | } |
| 480 | std::string type; |
| 481 | if (!reader.ReadString(&type, typeLength)) { |
| 482 | return; |
| 483 | } |
| 484 | webrtc::SdpParseError error; |
| 485 | webrtc::SessionDescriptionInterface *sessionDescription = webrtc::CreateSessionDescription(type, sdp, &error); |
| 486 | if (sessionDescription != nullptr) { |
| 487 | const auto weak = std::weak_ptr<InstanceImplReferenceInternal>(shared_from_this()); |
| 488 | rtc::scoped_refptr<SetSessionDescriptionObserverImpl> observer(new rtc::RefCountedObject<SetSessionDescriptionObserverImpl>([weak]() { |
| 489 | StaticThreads::getMediaThread()->PostTask(RTC_FROM_HERE, [weak](){ |
| 490 | auto strong = weak.lock(); |
| 491 | if (!strong) { |
| 492 | return; |
| 493 | } |
| 494 | strong->emitAnswer(); |
| 495 | }); |
| 496 | })); |
| 497 | _peerConnection->SetRemoteDescription(observer, sessionDescription); |
| 498 | _didSetRemoteDescription = true; |
| 499 | processRemoteIceCandidatesIfReady(); |
| 500 | } |
| 501 | } else if (command == 3) { |
| 502 | uint32_t sdpLength = 0; |
| 503 | if (!reader.ReadUInt32(&sdpLength)) { |
| 504 | return; |
| 505 | } |
| 506 | std::string sdp; |
| 507 | if (!reader.ReadString(&sdp, sdpLength)) { |
| 508 | return; |
| 509 | } |
| 510 | uint32_t typeLength = 0; |
| 511 | if (!reader.ReadUInt32(&typeLength)) { |
| 512 | return; |
| 513 | } |
| 514 | std::string type; |
| 515 | if (!reader.ReadString(&type, typeLength)) { |
| 516 | return; |
| 517 | } |
| 518 | webrtc::SdpParseError error; |
| 519 | webrtc::SessionDescriptionInterface *sessionDescription = webrtc::CreateSessionDescription(type, sdp, &error); |
| 520 | if (sessionDescription != nullptr) { |
| 521 | rtc::scoped_refptr<SetSessionDescriptionObserverImpl> observer(new rtc::RefCountedObject<SetSessionDescriptionObserverImpl>([]() { |
| 522 | })); |
| 523 | _peerConnection->SetRemoteDescription(observer, sessionDescription); |
| 524 | _didSetRemoteDescription = true; |
| 525 | processRemoteIceCandidatesIfReady(); |
| 526 | } |
| 527 | } else if (command == 4) { |
| 528 | uint8_t value = 0; |
| 529 | if (!reader.ReadUInt8(&value)) { |
| 530 | return; |
| 531 | } |
| 532 | const auto audio = AudioState(value & 0x01); |
| 533 | const auto video = VideoState((value >> 1) & 0x03); |
| 534 | if (video == VideoState(0x03)) { |
| 535 | return; |
| 536 | } |
| 537 | _remoteMediaStateUpdated(audio, video); |
| 538 | } else if (command == 6) { |
| 539 | uint32_t value = 0; |
| 540 | if (!reader.ReadUInt32(&value)) { |
| 541 | return; |
| 542 | } |
| 543 | _preferredAspectRatio = ((float)value) / 1000.0f; |
| 544 | if (_videoCapture) { |
| 545 | VideoCaptureInterfaceObject *videoCaptureImpl = GetVideoCaptureAssumingSameThread(_videoCapture.get()); |
| 546 | videoCaptureImpl->setPreferredAspectRatio(_preferredAspectRatio); |
| 547 | } |
| 548 | _remotePrefferedAspectRatioUpdated(_preferredAspectRatio); |
| 549 | } |
| 550 | } |
| 551 |
| 552 | private: |
| 553 | void beginStatsTimer(int timeoutMs) { |
| 554 | const auto weak = std::weak_ptr<InstanceImplReferenceInternal>(shared_from_this()); |
| 555 | StaticThreads::getMediaThread()->PostDelayedTask(RTC_FROM_HERE, [weak]() { |
| 556 | StaticThreads::getMediaThread()->PostTask(RTC_FROM_HERE, [weak](){ |
| 557 | auto strong = weak.lock(); |
| 558 | if (!strong) { |
| 559 | return; |
| 560 | } |
| 561 | strong->collectStats(); |
| 562 | }); |
| 563 | }, timeoutMs); |
| 564 | } |
| 565 |
| 566 | void collectStats() { |
| 567 | const auto weak = std::weak_ptr<InstanceImplReferenceInternal>(shared_from_this()); |
| 568 |
| 569 | rtc::scoped_refptr<RTCStatsCollectorCallbackImpl> observer(new rtc::RefCountedObject<RTCStatsCollectorCallbackImpl>([weak](const rtc::scoped_refptr<const webrtc::RTCStatsReport> &stats) { |
| 570 | StaticThreads::getMediaThread()->PostTask(RTC_FROM_HERE, [weak, stats](){ |
| 571 | auto strong = weak.lock(); |
| 572 | if (!strong) { |
| 573 | return; |
| 574 | } |
| 575 | strong->reportStats(stats); |
| 576 | strong->beginStatsTimer(5000); |
| 577 | }); |
| 578 | })); |
| 579 | _peerConnection->GetStats(observer); |
| 580 | } |
| 581 |
| 582 | void reportStats(const rtc::scoped_refptr<const webrtc::RTCStatsReport> &stats) { |
| 583 | int32_t inboundPacketsReceived = 0; |
| 584 | int32_t inboundPacketsLost = 0; |
| 585 |
| 586 | for (auto it = stats->begin(); it != stats->end(); it++) { |
| 587 | if (it->type() == std::string("inbound-rtp")) { |
| 588 | for (auto &member : it->Members()) { |
| 589 | if (member->name() == std::string("packetsLost")) { |
| 590 | inboundPacketsLost = *(member->cast_to<webrtc::RTCStatsMember<int>>()); |
| 591 | } else if (member->name() == std::string("packetsReceived")) { |
| 592 | inboundPacketsReceived = *(member->cast_to<webrtc::RTCStatsMember<unsigned int>>()); |
| 593 | } |
| 594 | } |
| 595 | } |
| 596 | } |
| 597 |
| 598 | int32_t deltaPacketsReceived = inboundPacketsReceived - _statsData.packetsReceived; |
| 599 | int32_t deltaPacketsLost = inboundPacketsLost - _statsData.packetsLost; |
| 600 |
| 601 | _statsData.packetsReceived = inboundPacketsReceived; |
| 602 | _statsData.packetsLost = inboundPacketsLost; |
| 603 |
| 604 | float signalBarsNorm = 5.0f; |
| 605 |
| 606 | if (deltaPacketsReceived > 0) { |
| 607 | float lossRate = ((float)deltaPacketsLost) / ((float)deltaPacketsReceived); |
| 608 | float adjustedLossRate = lossRate / 0.1f; |
| 609 | adjustedLossRate = fmaxf(0.0f, adjustedLossRate); |
| 610 | adjustedLossRate = fminf(1.0f, adjustedLossRate); |
| 611 | float adjustedQuality = 1.0f - adjustedLossRate; |
| 612 | _signalBarsUpdated((int)(adjustedQuality * signalBarsNorm)); |
| 613 | } else { |
| 614 | _signalBarsUpdated((int)(1.0f * signalBarsNorm)); |
| 615 | } |
| 616 | } |
| 617 |
| 618 | void sendPendingServiceMessages(int cause) { |
| 619 | if (const auto prepared = _signalingConnection->prepareForSendingService(cause)) { |
| 620 | _signalingDataEmitted(prepared->bytes); |
| 621 | } |
| 622 | } |
| 623 |
| 624 | void emitSignaling(const rtc::ByteBufferWriter &buffer) { |
| 625 | rtc::CopyOnWriteBuffer packet; |
| 626 | packet.SetData(buffer.Data(), buffer.Length()); |
| 627 |
| 628 | if (true) { |
| 629 | std::vector<uint8_t> result; |
| 630 | result.resize(buffer.Length()); |
| 631 | memcpy(result.data(), buffer.Data(), buffer.Length()); |
| 632 | _signalingDataEmitted(result); |
| 633 | return; |
| 634 | } |
| 635 |
| 636 | if (const auto prepared = _signalingConnection->prepareForSending(Message{ UnstructuredDataMessage{ packet } })) { |
| 637 | _signalingDataEmitted(prepared->bytes); |
| 638 | } |
| 639 | } |
| 640 |
| 641 | void emitIceCandidate(std::string sdp, int mid, std::string sdpMid) { |
| 642 | RTC_LOG(LS_INFO) << "emitIceCandidate " << sdp << ", " << mid << ", " << sdpMid; |
| 643 |
| 644 | rtc::ByteBufferWriter writer; |
| 645 | writer.WriteUInt8(1); |
| 646 | writer.WriteUInt32((uint32_t)sdp.size()); |
| 647 | writer.WriteString(sdp); |
| 648 | writer.WriteUInt32((uint32_t)mid); |
| 649 | writer.WriteUInt32((uint32_t)sdpMid.size()); |
| 650 | writer.WriteString(sdpMid); |
| 651 |
| 652 | emitSignaling(writer); |
| 653 | } |
| 654 |
| 655 | void emitOffer() { |
| 656 | const auto weak = std::weak_ptr<InstanceImplReferenceInternal>(shared_from_this()); |
| 657 |
| 658 | webrtc::PeerConnectionInterface::RTCOfferAnswerOptions options; |
| 659 | options.offer_to_receive_audio = 1; |
| 660 | if (_videoCapture) { |
| 661 | options.offer_to_receive_video = 1; |
| 662 | } |
| 663 |
| 664 | rtc::scoped_refptr<CreateSessionDescriptionObserverImpl> observer(new rtc::RefCountedObject<CreateSessionDescriptionObserverImpl>([weak](std::string sdp, std::string type) { |
| 665 | StaticThreads::getMediaThread()->PostTask(RTC_FROM_HERE, [weak, sdp, type](){ |
| 666 | auto strong = weak.lock(); |
| 667 | if (!strong) { |
| 668 | return; |
| 669 | } |
| 670 |
| 671 | webrtc::SdpParseError error; |
| 672 | webrtc::SessionDescriptionInterface *sessionDescription = webrtc::CreateSessionDescription(type, sdp, &error); |
| 673 | if (sessionDescription != nullptr) { |
| 674 | rtc::scoped_refptr<SetSessionDescriptionObserverImpl> observer(new rtc::RefCountedObject<SetSessionDescriptionObserverImpl>([weak, sdp, type]() { |
| 675 | auto strong = weak.lock(); |
| 676 | if (!strong) { |
| 677 | return; |
| 678 | } |
| 679 | strong->emitOfferData(sdp, type); |
| 680 | })); |
| 681 | strong->_peerConnection->SetLocalDescription(observer, sessionDescription); |
| 682 | } |
| 683 | }); |
| 684 | })); |
| 685 | _peerConnection->CreateOffer(observer, options); |
| 686 | } |
| 687 |
| 688 | void emitOfferData(std::string sdp, std::string type) { |
| 689 | rtc::ByteBufferWriter writer; |
| 690 | writer.WriteUInt8(2); |
| 691 | writer.WriteUInt32((uint32_t)sdp.size()); |
| 692 | writer.WriteString(sdp); |
| 693 | writer.WriteUInt32((uint32_t)type.size()); |
| 694 | writer.WriteString(type); |
| 695 |
| 696 | emitSignaling(writer); |
| 697 | } |
| 698 |
| 699 | void emitAnswerData(std::string sdp, std::string type) { |
| 700 | rtc::ByteBufferWriter writer; |
| 701 | writer.WriteUInt8(3); |
| 702 | writer.WriteUInt32((uint32_t)sdp.size()); |
| 703 | writer.WriteString(sdp); |
| 704 | writer.WriteUInt32((uint32_t)type.size()); |
| 705 | writer.WriteString(type); |
| 706 |
| 707 | emitSignaling(writer); |
| 708 | } |
| 709 |
| 710 | void emitAnswer() { |
| 711 | const auto weak = std::weak_ptr<InstanceImplReferenceInternal>(shared_from_this()); |
| 712 |
| 713 | webrtc::PeerConnectionInterface::RTCOfferAnswerOptions options; |
| 714 | options.offer_to_receive_audio = 1; |
| 715 | if (_videoCapture) { |
| 716 | options.offer_to_receive_video = 1; |
| 717 | } |
| 718 |
| 719 | rtc::scoped_refptr<CreateSessionDescriptionObserverImpl> observer(new rtc::RefCountedObject<CreateSessionDescriptionObserverImpl>([weak](std::string sdp, std::string type) { |
| 720 | StaticThreads::getMediaThread()->PostTask(RTC_FROM_HERE, [weak, sdp, type](){ |
| 721 | auto strong = weak.lock(); |
| 722 | if (!strong) { |
| 723 | return; |
| 724 | } |
| 725 |
| 726 | webrtc::SdpParseError error; |
| 727 | webrtc::SessionDescriptionInterface *sessionDescription = webrtc::CreateSessionDescription(type, sdp, &error); |
| 728 | if (sessionDescription != nullptr) { |
| 729 | rtc::scoped_refptr<SetSessionDescriptionObserverImpl> observer(new rtc::RefCountedObject<SetSessionDescriptionObserverImpl>([weak, sdp, type]() { |
| 730 | auto strong = weak.lock(); |
| 731 | if (!strong) { |
| 732 | return; |
| 733 | } |
| 734 | strong->emitAnswerData(sdp, type); |
| 735 | })); |
| 736 | strong->_peerConnection->SetLocalDescription(observer, sessionDescription); |
| 737 | } |
| 738 | }); |
| 739 | })); |
| 740 | _peerConnection->CreateAnswer(observer, options); |
| 741 |
| 742 | } |
| 743 |
| 744 | void changeVideoState(VideoState state) { |
| 745 | if (_videoState != state) { |
| 746 | _videoState = state; |
| 747 | emitMediaState(); |
| 748 | } |
| 749 | } |
| 750 |
| 751 | void changeAudioState(AudioState state) { |
| 752 | if (_audioState != state) { |
| 753 | _audioState = state; |
| 754 | emitMediaState(); |
| 755 | } |
| 756 | } |
| 757 |
| 758 | void emitMediaState() { |
| 759 | rtc::ByteBufferWriter writer; |
| 760 | writer.WriteUInt8(4); |
| 761 | writer.WriteUInt8((uint8_t(_videoState) << 1) | uint8_t(_audioState)); |
| 762 |
| 763 | emitSignaling(writer); |
| 764 | } |
| 765 |
| 766 | void emitRequestVideo() { |
| 767 | rtc::ByteBufferWriter writer; |
| 768 | writer.WriteUInt8(5); |
| 769 |
| 770 | emitSignaling(writer); |
| 771 | } |
| 772 |
| 773 | void emitVideoParameters() { |
| 774 | if (_localPreferredVideoAspectRatio > 0.01f) { |
| 775 | rtc::ByteBufferWriter writer; |
| 776 | writer.WriteUInt8(6); |
| 777 | writer.WriteUInt32((uint32_t)(_localPreferredVideoAspectRatio * 1000.0f)); |
| 778 |
| 779 | emitSignaling(writer); |
| 780 | } |
| 781 | } |
| 782 |
| 783 | void processRemoteIceCandidatesIfReady() { |
| 784 | if (_pendingRemoteIceCandidates.size() == 0 || !_didSetRemoteDescription) { |
| 785 | return; |
| 786 | } |
| 787 |
| 788 | for (auto &it : _pendingRemoteIceCandidates) { |
| 789 | webrtc::SdpParseError error; |
| 790 | webrtc::IceCandidateInterface *iceCandidate = webrtc::CreateIceCandidate(it->sdpMid, it->mid, it->sdp, &error); |
| 791 | if (iceCandidate != nullptr) { |
| 792 | std::unique_ptr<webrtc::IceCandidateInterface> nativeCandidate = std::unique_ptr<webrtc::IceCandidateInterface>(iceCandidate); |
| 793 | _peerConnection->AddIceCandidate(std::move(nativeCandidate), [](auto error) { |
| 794 | }); |
| 795 | } |
| 796 | } |
| 797 | _pendingRemoteIceCandidates.clear(); |
| 798 | } |
| 799 |
| 800 | void updateIsConnected(bool isConnected) { |
| 801 | if (isConnected) { |
| 802 | _state = State::Established; |
| 803 | if (!_didConnectOnce) { |
| 804 | _didConnectOnce = true; |
| 805 | } |
| 806 | } else { |
| 807 | _state = State::Reconnecting; |
| 808 | } |
| 809 | _stateUpdated(_state); |
| 810 | } |
| 811 |
| 812 | void onTrack(rtc::scoped_refptr<webrtc::RtpTransceiverInterface> transceiver) { |
| 813 | if (!_remoteVideoTrack) { |
| 814 | if (transceiver->media_type() == cricket::MediaType::MEDIA_TYPE_VIDEO) { |
| 815 | _remoteVideoTrack = static_cast<webrtc::VideoTrackInterface *>(transceiver->receiver()->track().get()); |
| 816 | } |
| 817 | if (_remoteVideoTrack && _currentSink) { |
| 818 | _remoteVideoTrack->AddOrUpdateSink(_currentSink.get(), rtc::VideoSinkWants()); |
| 819 | } |
| 820 | } |
| 821 | } |
| 822 |
| 823 | void beginSendingVideo() { |
| 824 | if (!_videoCapture) { |
| 825 | return; |
| 826 | } |
| 827 |
| 828 | VideoCaptureInterfaceObject *videoCaptureImpl = GetVideoCaptureAssumingSameThread(_videoCapture.get()); |
| 829 |
| 830 | const auto weak = std::weak_ptr<InstanceImplReferenceInternal>(shared_from_this()); |
| 831 |
| 832 | videoCaptureImpl->setStateUpdated([weak](VideoState state) { |
| 833 | StaticThreads::getMediaThread()->PostTask(RTC_FROM_HERE, [weak, state](){ |
| 834 | auto strong = weak.lock(); |
| 835 | if (strong) { |
| 836 | strong->changeVideoState(state); |
| 837 | } |
| 838 | }); |
| 839 | }); |
| 840 |
| 841 | _localVideoTrack = _nativeFactory->CreateVideoTrack("video0", videoCaptureImpl->source()); |
| 842 | _peerConnection->AddTrack(_localVideoTrack, _streamIds); |
| 843 | for (auto &it : _peerConnection->GetTransceivers()) { |
| 844 | if (it->media_type() == cricket::MediaType::MEDIA_TYPE_VIDEO) { |
| 845 | auto capabilities = _nativeFactory->GetRtpSenderCapabilities( |
| 846 | cricket::MediaType::MEDIA_TYPE_VIDEO); |
| 847 |
| 848 | std::vector<webrtc::RtpCodecCapability> codecs; |
| 849 | for (auto &codec : capabilities.codecs) { |
| 850 | #ifndef WEBRTC_DISABLE_H265 |
| 851 | if (codec.name == cricket::kH265CodecName) { |
| 852 | codecs.insert(codecs.begin(), codec); |
| 853 | } else { |
| 854 | codecs.push_back(codec); |
| 855 | } |
| 856 | #else |
| 857 | codecs.push_back(codec); |
| 858 | #endif |
| 859 | } |
| 860 | it->SetCodecPreferences(codecs); |
| 861 |
| 862 | break; |
| 863 | } |
| 864 | } |
| 865 |
| 866 | if (_didConnectOnce && _encryptionKey.isOutgoing) { |
| 867 | emitOffer(); |
| 868 | } |
| 869 |
| 870 | emitVideoParameters(); |
| 871 | } |
| 872 |
| 873 | private: |
| 874 | EncryptionKey _encryptionKey; |
| 875 | std::vector<RtcServer> _rtcServers; |
| 876 | bool _enableP2P; |
| 877 | std::function<void(State)> _stateUpdated; |
| 878 | std::function<void(int)> _signalBarsUpdated; |
| 879 | std::function<void(const std::vector<uint8_t> &)> _signalingDataEmitted; |
| 880 | std::function<void(AudioState, VideoState)> _remoteMediaStateUpdated; |
| 881 | std::function<void(bool)> _remoteBatteryLevelIsLowUpdated; |
| 882 | std::function<void(float)> _remotePrefferedAspectRatioUpdated; |
| 883 | std::shared_ptr<VideoCaptureInterface> _videoCapture; |
| 884 | std::unique_ptr<EncryptedConnection> _signalingConnection; |
| 885 | float _localPreferredVideoAspectRatio = 0.0f; |
| 886 | float _preferredAspectRatio = 0.0f; |
| 887 |
| 888 | State _state = State::WaitInit; |
| 889 | AudioState _audioState = AudioState::Active; |
| 890 | VideoState _videoState = VideoState::Inactive; |
| 891 | bool _didConnectOnce = false; |
| 892 |
| 893 | std::vector<std::string> _streamIds; |
| 894 |
| 895 | StatsData _statsData; |
| 896 |
| 897 | rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> _nativeFactory; |
| 898 | std::unique_ptr<PeerConnectionObserverImpl> _observer; |
| 899 | rtc::scoped_refptr<webrtc::PeerConnectionInterface> _peerConnection; |
| 900 | std::unique_ptr<webrtc::MediaConstraints> _nativeConstraints; |
| 901 | rtc::scoped_refptr<webrtc::AudioTrackInterface> _localAudioTrack; |
| 902 | rtc::scoped_refptr<webrtc::VideoTrackInterface> _localVideoTrack; |
| 903 | rtc::scoped_refptr<webrtc::VideoTrackInterface> _remoteVideoTrack; |
| 904 |
| 905 | std::shared_ptr<rtc::VideoSinkInterface<webrtc::VideoFrame>> _currentSink; |
| 906 |
| 907 | bool _didSetRemoteDescription = false; |
| 908 | std::vector<std::shared_ptr<IceCandidateData>> _pendingRemoteIceCandidates; |
| 909 |
| 910 | std::shared_ptr<PlatformContext> _platformContext; |
| 911 | }; |
| 912 |
| 913 | InstanceImplReference::InstanceImplReference(Descriptor &&descriptor) : |
| 914 | logSink_(std::make_unique<LogSinkImpl>(descriptor.config.logPath)) { |
| 915 | rtc::LogMessage::AddLogToStream(logSink_.get(), rtc::LS_INFO); |
| 916 |
| 917 | internal_.reset(new ThreadLocalObject<InstanceImplReferenceInternal>(StaticThreads::getMediaThread(), [descriptor = std::move(descriptor)]() { |
| 918 | return new InstanceImplReferenceInternal( |
| 919 | descriptor |
| 920 | ); |
| 921 | })); |
| 922 | internal_->perform(RTC_FROM_HERE, [](InstanceImplReferenceInternal *internal){ |
| 923 | internal->start(); |
| 924 | }); |
| 925 | } |
| 926 |
| 927 | InstanceImplReference::~InstanceImplReference() { |
| 928 | rtc::LogMessage::RemoveLogToStream(logSink_.get()); |
| 929 | } |
| 930 |
| 931 | void InstanceImplReference::setNetworkType(NetworkType networkType) { |
| 932 | } |
| 933 |
| 934 | void InstanceImplReference::setMuteMicrophone(bool muteMicrophone) { |
| 935 | internal_->perform(RTC_FROM_HERE, [muteMicrophone = muteMicrophone](InstanceImplReferenceInternal *internal) { |
| 936 | internal->setMuteMicrophone(muteMicrophone); |
| 937 | }); |
| 938 | } |
| 939 |
| 940 | void InstanceImplReference::receiveSignalingData(const std::vector<uint8_t> &data) { |
| 941 | internal_->perform(RTC_FROM_HERE, [data](InstanceImplReferenceInternal *internal) { |
| 942 | internal->receiveSignalingData(data); |
| 943 | }); |
| 944 | } |
| 945 |
| 946 | void InstanceImplReference::setVideoCapture(std::shared_ptr<VideoCaptureInterface> videoCapture) { |
| 947 | internal_->perform(RTC_FROM_HERE, [videoCapture](InstanceImplReferenceInternal *internal) { |
| 948 | internal->setVideoCapture(videoCapture); |
| 949 | }); |
| 950 | } |
| 951 |
| 952 | void InstanceImplReference::setRequestedVideoAspect(float aspect) { |
| 953 | internal_->perform(RTC_FROM_HERE, [aspect](InstanceImplReferenceInternal *internal) { |
| 954 | internal->setRequestedVideoAspect(aspect); |
| 955 | }); |
| 956 | } |
| 957 |
| 958 | void InstanceImplReference::setIncomingVideoOutput(std::shared_ptr<rtc::VideoSinkInterface<webrtc::VideoFrame>> sink) { |
| 959 | internal_->perform(RTC_FROM_HERE, [sink](InstanceImplReferenceInternal *internal) { |
| 960 | internal->setIncomingVideoOutput(sink); |
| 961 | }); |
| 962 | } |
| 963 |
| 964 | void InstanceImplReference::setAudioOutputGainControlEnabled(bool enabled) { |
| 965 | } |
| 966 |
| 967 | void InstanceImplReference::setEchoCancellationStrength(int strength) { |
| 968 | } |
| 969 |
| 970 | void InstanceImplReference::setAudioInputDevice(std::string id) { |
| 971 | } |
| 972 |
| 973 | void InstanceImplReference::setAudioOutputDevice(std::string id) { |
| 974 | } |
| 975 |
| 976 | void InstanceImplReference::setInputVolume(float level) { |
| 977 | } |
| 978 |
| 979 | void InstanceImplReference::setOutputVolume(float level) { |
| 980 | } |
| 981 |
| 982 | void InstanceImplReference::setAudioOutputDuckingEnabled(bool enabled) { |
| 983 | } |
| 984 |
| 985 | void InstanceImplReference::setIsLowBatteryLevel(bool isLowBatteryLevel) { |
| 986 | } |
| 987 |
| 988 | int InstanceImplReference::GetConnectionMaxLayer() { |
| 989 | return 92; |
| 990 | } |
| 991 |
| 992 | std::vector<std::string> InstanceImplReference::GetVersions() { |
| 993 | std::vector<std::string> result; |
| 994 | result.push_back("2.8.8"); |
| 995 | return result; |
| 996 | } |
| 997 |
| 998 | std::string InstanceImplReference::getLastError() { |
| 999 | return "ERROR_UNKNOWN"; |
| 1000 | } |
| 1001 |
| 1002 | std::string InstanceImplReference::getDebugInfo() { |
| 1003 | return ""; |
| 1004 | } |
| 1005 |
| 1006 | int64_t InstanceImplReference::getPreferredRelayId() { |
| 1007 | return 0; |
| 1008 | } |
| 1009 |
| 1010 | TrafficStats InstanceImplReference::getTrafficStats() { |
| 1011 | auto result = TrafficStats(); |
| 1012 | return result; |
| 1013 | } |
| 1014 |
| 1015 | PersistentState InstanceImplReference::getPersistentState() { |
| 1016 | return PersistentState(); |
| 1017 | } |
| 1018 |
| 1019 | void InstanceImplReference::stop(std::function<void(FinalState)> completion) { |
| 1020 | auto result = FinalState(); |
| 1021 |
| 1022 | result.persistentState = getPersistentState(); |
| 1023 | result.debugLog = logSink_->result(); |
| 1024 | result.trafficStats = getTrafficStats(); |
| 1025 | result.isRatingSuggested = false; |
| 1026 |
| 1027 | completion(result); |
| 1028 | } |
| 1029 |
| 1030 | template <> |
| 1031 | bool Register<InstanceImplReference>() { |
| 1032 | return Meta::RegisterOne<InstanceImplReference>(); |
| 1033 | } |
| 1034 |
| 1035 | } // namespace tgcalls |
| 1036 |