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