| #include "VideoCapturerInterface.h" |
| 6 | #include "CodecSelectHelper.h" |
| 7 | #include "Message.h" |
| 8 | #include "platform/PlatformInterface.h" |
| 9 |
| 10 | #include "api/audio_codecs/audio_decoder_factory_template.h" |
| 11 | #include "api/audio_codecs/audio_encoder_factory_template.h" |
| 12 | #include "api/audio_codecs/opus/audio_decoder_opus.h" |
| 13 | #include "api/audio_codecs/opus/audio_encoder_opus.h" |
| 14 | #include "api/task_queue/default_task_queue_factory.h" |
| 15 | #include "media/engine/webrtc_media_engine.h" |
| 16 | #include "system_wrappers/include/field_trial.h" |
| 17 | #include "api/video/builtin_video_bitrate_allocator_factory.h" |
| 18 | #include "call/call.h" |
| 19 | #include "modules/rtp_rtcp/source/rtp_utility.h" |
| 20 |
| 21 | namespace tgcalls { |
| 22 | namespace { |
| 23 |
| 24 | constexpr uint32_t ssrcAudioIncoming = 1; |
| 25 | constexpr uint32_t ssrcAudioOutgoing = 2; |
| 26 | constexpr uint32_t ssrcAudioFecIncoming = 5; |
| 27 | constexpr uint32_t ssrcAudioFecOutgoing = 6; |
| 28 | constexpr uint32_t ssrcVideoIncoming = 3; |
| 29 | constexpr uint32_t ssrcVideoOutgoing = 4; |
| 30 | constexpr uint32_t ssrcVideoFecIncoming = 7; |
| 31 | constexpr uint32_t ssrcVideoFecOutgoing = 8; |
| 32 |
| 33 | rtc::Thread *makeWorkerThread() { |
| 34 | static std::unique_ptr<rtc::Thread> value = rtc::Thread::Create(); |
| 35 | value->SetName("WebRTC-Worker", nullptr); |
| 36 | value->Start(); |
| 37 | return value.get(); |
| 38 | } |
| 39 |
| 40 | VideoCaptureInterfaceObject *GetVideoCaptureAssumingSameThread(VideoCaptureInterface *videoCapture) { |
| 41 | return videoCapture |
| 42 | ? static_cast<VideoCaptureInterfaceImpl*>(videoCapture)->object()->getSyncAssumingSameThread() |
| 43 | : nullptr; |
| 44 | } |
| 45 |
| 46 | } // namespace |
| 47 |
| 48 | rtc::Thread *MediaManager::getWorkerThread() { |
| 49 | static rtc::Thread *value = makeWorkerThread(); |
| 50 | return value; |
| 51 | } |
| 52 |
| 53 | MediaManager::MediaManager( |
| 54 | rtc::Thread *thread, |
| 55 | bool isOutgoing, |
| 56 | std::shared_ptr<VideoCaptureInterface> videoCapture, |
| 57 | std::function<void(Message &&)> sendSignalingMessage, |
| 58 | std::function<void(Message &&)> sendTransportMessage, |
| 59 | std::function<void(int)> signalBarsUpdated, |
| 60 | float localPreferredVideoAspectRatio, |
| 61 | bool enableHighBitrateVideo, |
| 62 | std::vector<std::string> preferredCodecs, |
| 63 | std::shared_ptr<PlatformContext> platformContext) : |
| 64 | _thread(thread), |
| 65 | _eventLog(std::make_unique<webrtc::RtcEventLogNull>()), |
| 66 | _taskQueueFactory(webrtc::CreateDefaultTaskQueueFactory()), |
| 67 | _sendSignalingMessage(std::move(sendSignalingMessage)), |
| 68 | _sendTransportMessage(std::move(sendTransportMessage)), |
| 69 | _signalBarsUpdated(std::move(signalBarsUpdated)), |
| 70 | _outgoingVideoState(videoCapture ? VideoState::Active : VideoState::Inactive), |
| 71 | _videoCapture(std::move(videoCapture)), |
| 72 | _localPreferredVideoAspectRatio(localPreferredVideoAspectRatio), |
| 73 | _enableHighBitrateVideo(enableHighBitrateVideo), |
| 74 | _platformContext(platformContext) { |
| 75 | _ssrcAudio.incoming = isOutgoing ? ssrcAudioIncoming : ssrcAudioOutgoing; |
| 76 | _ssrcAudio.outgoing = (!isOutgoing) ? ssrcAudioIncoming : ssrcAudioOutgoing; |
| 77 | _ssrcAudio.fecIncoming = isOutgoing ? ssrcAudioFecIncoming : ssrcAudioFecOutgoing; |
| 78 | _ssrcAudio.fecOutgoing = (!isOutgoing) ? ssrcAudioFecIncoming : ssrcAudioFecOutgoing; |
| 79 | _ssrcVideo.incoming = isOutgoing ? ssrcVideoIncoming : ssrcVideoOutgoing; |
| 80 | _ssrcVideo.outgoing = (!isOutgoing) ? ssrcVideoIncoming : ssrcVideoOutgoing; |
| 81 | _ssrcVideo.fecIncoming = isOutgoing ? ssrcVideoFecIncoming : ssrcVideoFecOutgoing; |
| 82 | _ssrcVideo.fecOutgoing = (!isOutgoing) ? ssrcVideoFecIncoming : ssrcVideoFecOutgoing; |
| 83 |
| 84 | _audioNetworkInterface = std::unique_ptr<MediaManager::NetworkInterfaceImpl>(new MediaManager::NetworkInterfaceImpl(this, false)); |
| 85 | _videoNetworkInterface = std::unique_ptr<MediaManager::NetworkInterfaceImpl>(new MediaManager::NetworkInterfaceImpl(this, true)); |
| 86 |
| 87 | webrtc::field_trial::InitFieldTrialsFromString( |
| 88 | "WebRTC-Audio-SendSideBwe/Enabled/" |
| 89 | "WebRTC-Audio-Allocation/min:6kbps,max:32kbps/" |
| 90 | "WebRTC-Audio-OpusMinPacketLossRate/Enabled-1/" |
| 91 | "WebRTC-FlexFEC-03/Enabled/" |
| 92 | "WebRTC-FlexFEC-03-Advertised/Enabled/" |
| 93 | ); |
| 94 |
| 95 | PlatformInterface::SharedInstance()->configurePlatformAudio(); |
| 96 |
| 97 | _videoBitrateAllocatorFactory = webrtc::CreateBuiltinVideoBitrateAllocatorFactory(); |
| 98 |
| 99 | cricket::MediaEngineDependencies mediaDeps; |
| 100 | mediaDeps.task_queue_factory = _taskQueueFactory.get(); |
| 101 | mediaDeps.audio_encoder_factory = webrtc::CreateAudioEncoderFactory<webrtc::AudioEncoderOpus>(); |
| 102 | mediaDeps.audio_decoder_factory = webrtc::CreateAudioDecoderFactory<webrtc::AudioDecoderOpus>(); |
| 103 |
| 104 | mediaDeps.video_encoder_factory = PlatformInterface::SharedInstance()->makeVideoEncoderFactory(_platformContext); |
| 105 | mediaDeps.video_decoder_factory = PlatformInterface::SharedInstance()->makeVideoDecoderFactory(_platformContext); |
| 106 |
| 107 | _myVideoFormats = ComposeSupportedFormats( |
| 108 | mediaDeps.video_encoder_factory->GetSupportedFormats(), |
| 109 | mediaDeps.video_decoder_factory->GetSupportedFormats(), |
| 110 | preferredCodecs, |
| 111 | _platformContext); |
| 112 |
| 113 | mediaDeps.audio_processing = webrtc::AudioProcessingBuilder().Create(); |
| 114 | _mediaEngine = cricket::CreateMediaEngine(std::move(mediaDeps)); |
| 115 | _mediaEngine->Init(); |
| 116 | webrtc::Call::Config callConfig(_eventLog.get()); |
| 117 | callConfig.task_queue_factory = _taskQueueFactory.get(); |
| 118 | callConfig.trials = &_fieldTrials; |
| 119 | callConfig.audio_state = _mediaEngine->voice().GetAudioState(); |
| 120 | _call.reset(webrtc::Call::Create(callConfig)); |
| 121 |
| 122 | cricket::AudioOptions audioOptions; |
| 123 | audioOptions.echo_cancellation = true; |
| 124 | audioOptions.noise_suppression = true; |
| 125 | audioOptions.audio_jitter_buffer_fast_accelerate = true; |
| 126 |
| 127 | std::vector<std::string> streamIds; |
| 128 | streamIds.push_back("1"); |
| 129 |
| 130 | _audioChannel.reset(_mediaEngine->voice().CreateMediaChannel(_call.get(), cricket::MediaConfig(), audioOptions, webrtc::CryptoOptions::NoGcm())); |
| 131 | _videoChannel.reset(_mediaEngine->video().CreateMediaChannel(_call.get(), cricket::MediaConfig(), cricket::VideoOptions(), webrtc::CryptoOptions::NoGcm(), _videoBitrateAllocatorFactory.get())); |
| 132 |
| 133 | const uint32_t opusClockrate = 48000; |
| 134 | const uint16_t opusSdpPayload = 111; |
| 135 | const char *opusSdpName = "opus"; |
| 136 | const uint8_t opusSdpChannels = 2; |
| 137 | const uint32_t opusSdpBitrate = 0; |
| 138 |
| 139 | const uint8_t opusMinBitrateKbps = 6; |
| 140 | const uint8_t opusMaxBitrateKbps = 32; |
| 141 | const uint8_t opusStartBitrateKbps = 8; |
| 142 | const uint8_t opusPTimeMs = 120; |
| 143 |
| 144 | cricket::AudioCodec opusCodec(opusSdpPayload, opusSdpName, opusClockrate, opusSdpBitrate, opusSdpChannels); |
| 145 | opusCodec.AddFeedbackParam(cricket::FeedbackParam(cricket::kRtcpFbParamTransportCc)); |
| 146 | opusCodec.SetParam(cricket::kCodecParamMinBitrate, opusMinBitrateKbps); |
| 147 | opusCodec.SetParam(cricket::kCodecParamStartBitrate, opusStartBitrateKbps); |
| 148 | opusCodec.SetParam(cricket::kCodecParamMaxBitrate, opusMaxBitrateKbps); |
| 149 | opusCodec.SetParam(cricket::kCodecParamUseInbandFec, 1); |
| 150 | opusCodec.SetParam(cricket::kCodecParamPTime, opusPTimeMs); |
| 151 |
| 152 | cricket::AudioSendParameters audioSendPrameters; |
| 153 | audioSendPrameters.codecs.push_back(opusCodec); |
| 154 | audioSendPrameters.extensions.emplace_back(webrtc::RtpExtension::kTransportSequenceNumberUri, 1); |
| 155 | audioSendPrameters.options.echo_cancellation = true; |
| 156 | //audioSendPrameters.options.experimental_ns = false; |
| 157 | audioSendPrameters.options.noise_suppression = true; |
| 158 | audioSendPrameters.options.auto_gain_control = true; |
| 159 | //audioSendPrameters.options.highpass_filter = false; |
| 160 | audioSendPrameters.options.typing_detection = false; |
| 161 | //audioSendPrameters.max_bandwidth_bps = 16000; |
| 162 | audioSendPrameters.rtcp.reduced_size = true; |
| 163 | audioSendPrameters.rtcp.remote_estimate = true; |
| 164 | _audioChannel->SetSendParameters(audioSendPrameters); |
| 165 | _audioChannel->AddSendStream(cricket::StreamParams::CreateLegacy(_ssrcAudio.outgoing)); |
| 166 | _audioChannel->SetInterface(_audioNetworkInterface.get()); |
| 167 |
| 168 | cricket::AudioRecvParameters audioRecvParameters; |
| 169 | audioRecvParameters.codecs.emplace_back(opusSdpPayload, opusSdpName, opusClockrate, opusSdpBitrate, opusSdpChannels); |
| 170 | audioRecvParameters.extensions.emplace_back(webrtc::RtpExtension::kTransportSequenceNumberUri, 1); |
| 171 | audioRecvParameters.rtcp.reduced_size = true; |
| 172 | audioRecvParameters.rtcp.remote_estimate = true; |
| 173 |
| 174 | _audioChannel->SetRecvParameters(audioRecvParameters); |
| 175 | cricket::StreamParams audioRecvStreamParams = cricket::StreamParams::CreateLegacy(_ssrcAudio.incoming); |
| 176 | audioRecvStreamParams.set_stream_ids(streamIds); |
| 177 | _audioChannel->AddRecvStream(audioRecvStreamParams); |
| 178 | _audioChannel->SetPlayout(true); |
| 179 |
| 180 | _videoChannel->SetInterface(_videoNetworkInterface.get()); |
| 181 |
| 182 | adjustBitratePreferences(true); |
| 183 | } |
| 184 |
| 185 | void MediaManager::start() { |
| 186 | _sendSignalingMessage({ _myVideoFormats }); |
| 187 |
| 188 | if (_videoCapture != nullptr) { |
| 189 | setSendVideo(_videoCapture); |
| 190 | } |
| 191 |
| 192 | beginStatsTimer(3000); |
| 193 | } |
| 194 |
| 195 | MediaManager::~MediaManager() { |
| 196 | assert(_thread->IsCurrent()); |
| 197 |
| 198 | RTC_LOG(LS_INFO) << "MediaManager::~MediaManager()"; |
| 199 |
| 200 | _call->SignalChannelNetworkState(webrtc::MediaType::AUDIO, webrtc::kNetworkDown); |
| 201 | _call->SignalChannelNetworkState(webrtc::MediaType::VIDEO, webrtc::kNetworkDown); |
| 202 |
| 203 | _audioChannel->OnReadyToSend(false); |
| 204 | _audioChannel->SetSend(false); |
| 205 | _audioChannel->SetAudioSend(_ssrcAudio.outgoing, false, nullptr, &_audioSource); |
| 206 |
| 207 | _audioChannel->SetPlayout(false); |
| 208 |
| 209 | _audioChannel->RemoveRecvStream(_ssrcAudio.incoming); |
| 210 | _audioChannel->RemoveSendStream(_ssrcAudio.outgoing); |
| 211 |
| 212 | _audioChannel->SetInterface(nullptr); |
| 213 |
| 214 | setSendVideo(nullptr); |
| 215 |
| 216 | if (computeIsReceivingVideo()) { |
| 217 | _videoChannel->RemoveRecvStream(_ssrcVideo.incoming); |
| 218 | if (_enableFlexfec) { |
| 219 | _videoChannel->RemoveRecvStream(_ssrcVideo.fecIncoming); |
| 220 | } |
| 221 | } |
| 222 |
| 223 | if (_didConfigureVideo) { |
| 224 | _videoChannel->OnReadyToSend(false); |
| 225 | _videoChannel->SetSend(false); |
| 226 |
| 227 | if (_enableFlexfec) { |
| 228 | _videoChannel->RemoveSendStream(_ssrcVideo.outgoing); |
| 229 | _videoChannel->RemoveSendStream(_ssrcVideo.fecOutgoing); |
| 230 | } else { |
| 231 | _videoChannel->RemoveSendStream(_ssrcVideo.outgoing); |
| 232 | } |
| 233 | } |
| 234 |
| 235 | _videoChannel->SetInterface(nullptr); |
| 236 | } |
| 237 |
| 238 | void MediaManager::setIsConnected(bool isConnected) { |
| 239 | if (_isConnected == isConnected) { |
| 240 | return; |
| 241 | } |
| 242 | _isConnected = isConnected; |
| 243 |
| 244 | if (_isConnected) { |
| 245 | _call->SignalChannelNetworkState(webrtc::MediaType::AUDIO, webrtc::kNetworkUp); |
| 246 | _call->SignalChannelNetworkState(webrtc::MediaType::VIDEO, webrtc::kNetworkUp); |
| 247 | } else { |
| 248 | _call->SignalChannelNetworkState(webrtc::MediaType::AUDIO, webrtc::kNetworkDown); |
| 249 | _call->SignalChannelNetworkState(webrtc::MediaType::VIDEO, webrtc::kNetworkDown); |
| 250 | } |
| 251 | if (_audioChannel) { |
| 252 | _audioChannel->OnReadyToSend(_isConnected); |
| 253 | _audioChannel->SetSend(_isConnected); |
| 254 | _audioChannel->SetAudioSend(_ssrcAudio.outgoing, _isConnected && (_outgoingAudioState == AudioState::Active), nullptr, &_audioSource); |
| 255 | } |
| 256 | if (computeIsSendingVideo() && _videoChannel) { |
| 257 | _videoChannel->OnReadyToSend(_isConnected); |
| 258 | _videoChannel->SetSend(_isConnected); |
| 259 | } |
| 260 | sendVideoParametersMessage(); |
| 261 | sendOutgoingMediaStateMessage(); |
| 262 | } |
| 263 |
| 264 | void MediaManager::sendVideoParametersMessage() { |
| 265 | const auto aspectRatioValue = uint32_t(_localPreferredVideoAspectRatio * 1000.0); |
| 266 | _sendTransportMessage({ VideoParametersMessage{ aspectRatioValue } }); |
| 267 | } |
| 268 |
| 269 | void MediaManager::sendOutgoingMediaStateMessage() { |
| 270 | _sendTransportMessage({ RemoteMediaStateMessage{ _outgoingAudioState, _outgoingVideoState } }); |
| 271 | } |
| 272 |
| 273 | void MediaManager::beginStatsTimer(int timeoutMs) { |
| 274 | const auto weak = std::weak_ptr<MediaManager>(shared_from_this()); |
| 275 | _thread->PostDelayedTask(RTC_FROM_HERE, [weak]() { |
| 276 | auto strong = weak.lock(); |
| 277 | if (!strong) { |
| 278 | return; |
| 279 | } |
| 280 | strong->collectStats(); |
| 281 | }, timeoutMs); |
| 282 | } |
| 283 |
| 284 | void MediaManager::collectStats() { |
| 285 | auto stats = _call->GetStats(); |
| 286 | float bitrateNorm = 16.0f; |
| 287 | switch (_outgoingVideoState) { |
| 288 | case VideoState::Active: |
| 289 | bitrateNorm = 600.0f; |
| 290 | break; |
| 291 | default: |
| 292 | break; |
| 293 | } |
| 294 | float sendBitrateKbps = ((float)stats.send_bandwidth_bps / 1000.0f); |
| 295 |
| 296 | RTC_LOG(LS_INFO) << "MediaManager sendBitrateKbps=" << (stats.send_bandwidth_bps / 1000); |
| 297 |
| 298 | float signalBarsNorm = 4.0f; |
| 299 | float adjustedQuality = sendBitrateKbps / bitrateNorm; |
| 300 | adjustedQuality = fmaxf(0.0f, adjustedQuality); |
| 301 | adjustedQuality = fminf(1.0f, adjustedQuality); |
| 302 | if (_signalBarsUpdated) { |
| 303 | _signalBarsUpdated((int)(adjustedQuality * signalBarsNorm)); |
| 304 | } |
| 305 |
| 306 | beginStatsTimer(2000); |
| 307 | } |
| 308 |
| 309 | void MediaManager::notifyPacketSent(const rtc::SentPacket &sentPacket) { |
| 310 | _call->OnSentPacket(sentPacket); |
| 311 | } |
| 312 |
| 313 | void MediaManager::setPeerVideoFormats(VideoFormatsMessage &&peerFormats) { |
| 314 | if (!_videoCodecs.empty()) { |
| 315 | return; |
| 316 | } |
| 317 |
| 318 | bool wasReceivingVideo = computeIsReceivingVideo(); |
| 319 |
| 320 | assert(!_videoCodecOut.has_value()); |
| 321 | auto formats = ComputeCommonFormats( |
| 322 | _myVideoFormats, |
| 323 | std::move(peerFormats)); |
| 324 | auto codecs = AssignPayloadTypesAndDefaultCodecs(std::move(formats)); |
| 325 | if (codecs.myEncoderIndex >= 0) { |
| 326 | assert(codecs.myEncoderIndex < codecs.list.size()); |
| 327 | _videoCodecOut = codecs.list[codecs.myEncoderIndex]; |
| 328 | } |
| 329 | _videoCodecs = std::move(codecs.list); |
| 330 | if (_videoCodecOut.has_value()) { |
| 331 | checkIsSendingVideoChanged(false); |
| 332 | } |
| 333 | if (_videoCodecs.size() != 0) { |
| 334 | checkIsReceivingVideoChanged(wasReceivingVideo); |
| 335 | } |
| 336 | } |
| 337 |
| 338 | bool MediaManager::videoCodecsNegotiated() const { |
| 339 | return !_videoCodecs.empty(); |
| 340 | } |
| 341 |
| 342 | bool MediaManager::computeIsSendingVideo() const { |
| 343 | return _videoCapture != nullptr && _videoCodecOut.has_value(); |
| 344 | } |
| 345 |
| 346 | bool MediaManager::computeIsReceivingVideo() const { |
| 347 | return _videoCodecs.size() != 0; |
| 348 | } |
| 349 |
| 350 | void MediaManager::setSendVideo(std::shared_ptr<VideoCaptureInterface> videoCapture) { |
| 351 | const auto wasSending = computeIsSendingVideo(); |
| 352 | const auto wasReceiving = computeIsReceivingVideo(); |
| 353 |
| 354 | if (_videoCapture) { |
| 355 | GetVideoCaptureAssumingSameThread(_videoCapture.get())->setStateUpdated(nullptr); |
| 356 | } |
| 357 | _videoCapture = videoCapture; |
| 358 | if (_videoCapture) { |
| 359 | _videoCapture->setPreferredAspectRatio(_preferredAspectRatio); |
| 360 |
| 361 | const auto thread = _thread; |
| 362 | const auto weak = std::weak_ptr<MediaManager>(shared_from_this()); |
| 363 | GetVideoCaptureAssumingSameThread(_videoCapture.get())->setStateUpdated([=](VideoState state) { |
| 364 | thread->PostTask(RTC_FROM_HERE, [=] { |
| 365 | if (const auto strong = weak.lock()) { |
| 366 | strong->setOutgoingVideoState(state); |
| 367 | } |
| 368 | }); |
| 369 | }); |
| 370 | setOutgoingVideoState(VideoState::Active); |
| 371 | } else { |
| 372 | setOutgoingVideoState(VideoState::Inactive); |
| 373 | } |
| 374 |
| 375 | checkIsSendingVideoChanged(wasSending); |
| 376 | checkIsReceivingVideoChanged(wasReceiving); |
| 377 | } |
| 378 |
| 379 | void MediaManager::configureSendingVideoIfNeeded() { |
| 380 | if (_didConfigureVideo) { |
| 381 | return; |
| 382 | } |
| 383 | if (!_videoCodecOut.has_value()) { |
| 384 | return; |
| 385 | } |
| 386 | _didConfigureVideo = true; |
| 387 |
| 388 | auto codec = *_videoCodecOut; |
| 389 |
| 390 | codec.SetParam(cricket::kCodecParamMinBitrate, 64); |
| 391 | codec.SetParam(cricket::kCodecParamStartBitrate, 400); |
| 392 | codec.SetParam(cricket::kCodecParamMaxBitrate, _enableHighBitrateVideo ? 2000 : 800); |
| 393 |
| 394 | cricket::VideoSendParameters videoSendParameters; |
| 395 | videoSendParameters.codecs.push_back(codec); |
| 396 |
| 397 | if (_enableFlexfec) { |
| 398 | for (auto &c : _videoCodecs) { |
| 399 | if (c.name == cricket::kFlexfecCodecName) { |
| 400 | videoSendParameters.codecs.push_back(c); |
| 401 | break; |
| 402 | } |
| 403 | } |
| 404 | } |
| 405 |
| 406 | videoSendParameters.extensions.emplace_back(webrtc::RtpExtension::kTransportSequenceNumberUri, 2); |
| 407 | videoSendParameters.rtcp.remote_estimate = true; |
| 408 | _videoChannel->SetSendParameters(videoSendParameters); |
| 409 |
| 410 | if (_enableFlexfec) { |
| 411 | cricket::StreamParams videoSendStreamParams; |
| 412 | cricket::SsrcGroup videoSendSsrcGroup(cricket::kFecFrSsrcGroupSemantics, {_ssrcVideo.outgoing, _ssrcVideo.fecOutgoing}); |
| 413 | videoSendStreamParams.ssrcs = {_ssrcVideo.outgoing}; |
| 414 | videoSendStreamParams.ssrc_groups.push_back(videoSendSsrcGroup); |
| 415 | videoSendStreamParams.cname = "cname"; |
| 416 | _videoChannel->AddSendStream(videoSendStreamParams); |
| 417 | } else { |
| 418 | _videoChannel->AddSendStream(cricket::StreamParams::CreateLegacy(_ssrcVideo.outgoing)); |
| 419 | } |
| 420 |
| 421 | adjustBitratePreferences(true); |
| 422 | } |
| 423 |
| 424 | void MediaManager::checkIsSendingVideoChanged(bool wasSending) { |
| 425 | const auto sending = computeIsSendingVideo(); |
| 426 | if (sending == wasSending) { |
| 427 | return; |
| 428 | } else if (sending) { |
| 429 | configureSendingVideoIfNeeded(); |
| 430 |
| 431 | if (_enableFlexfec) { |
| 432 | _videoChannel->SetVideoSend(_ssrcVideo.outgoing, NULL, GetVideoCaptureAssumingSameThread(_videoCapture.get())->_videoSource); |
| 433 | _videoChannel->SetVideoSend(_ssrcVideo.fecOutgoing, NULL, nullptr); |
| 434 | } else { |
| 435 | _videoChannel->SetVideoSend(_ssrcVideo.outgoing, NULL, GetVideoCaptureAssumingSameThread(_videoCapture.get())->_videoSource); |
| 436 | } |
| 437 |
| 438 | _videoChannel->OnReadyToSend(_isConnected); |
| 439 | _videoChannel->SetSend(_isConnected); |
| 440 | } else { |
| 441 | _videoChannel->SetVideoSend(_ssrcVideo.outgoing, NULL, nullptr); |
| 442 | _videoChannel->SetVideoSend(_ssrcVideo.fecOutgoing, NULL, nullptr); |
| 443 | } |
| 444 |
| 445 | adjustBitratePreferences(true); |
| 446 | } |
| 447 |
| 448 | int MediaManager::getMaxVideoBitrate() const { |
| 449 | return (_enableHighBitrateVideo && _isLowCostNetwork) ? 2000000 : 800000; |
| 450 | } |
| 451 |
| 452 | void MediaManager::adjustBitratePreferences(bool resetStartBitrate) { |
| 453 | if (computeIsSendingVideo()) { |
| 454 | webrtc::BitrateConstraints preferences; |
| 455 | preferences.min_bitrate_bps = 64000; |
| 456 | if (resetStartBitrate) { |
| 457 | preferences.start_bitrate_bps = 400000; |
| 458 | } |
| 459 | preferences.max_bitrate_bps = getMaxVideoBitrate(); |
| 460 |
| 461 | _call->GetTransportControllerSend()->SetSdpBitrateParameters(preferences); |
| 462 | } else { |
| 463 | webrtc::BitrateConstraints preferences; |
| 464 | if (_didConfigureVideo) { |
| 465 | // After we have configured outgoing video, RTCP stops working for outgoing audio |
| 466 | // TODO: investigate |
| 467 | preferences.min_bitrate_bps = 16000; |
| 468 | if (resetStartBitrate) { |
| 469 | preferences.start_bitrate_bps = 16000; |
| 470 | } |
| 471 | preferences.max_bitrate_bps = 32000; |
| 472 | } else { |
| 473 | preferences.min_bitrate_bps = 8000; |
| 474 | if (resetStartBitrate) { |
| 475 | preferences.start_bitrate_bps = 16000; |
| 476 | } |
| 477 | preferences.max_bitrate_bps = 32000; |
| 478 | } |
| 479 |
| 480 | _call->GetTransportControllerSend()->SetSdpBitrateParameters(preferences); |
| 481 | } |
| 482 | } |
| 483 |
| 484 | void MediaManager::checkIsReceivingVideoChanged(bool wasReceiving) { |
| 485 | const auto receiving = computeIsReceivingVideo(); |
| 486 | if (receiving == wasReceiving) { |
| 487 | return; |
| 488 | } else { |
| 489 | cricket::VideoRecvParameters videoRecvParameters; |
| 490 |
| 491 | const auto codecs = { |
| 492 | cricket::kFlexfecCodecName, |
| 493 | cricket::kH264CodecName, |
| 494 | cricket::kH265CodecName, |
| 495 | cricket::kVp8CodecName, |
| 496 | cricket::kVp9CodecName, |
| 497 | cricket::kAv1CodecName, |
| 498 | }; |
| 499 | for (const auto &c : _videoCodecs) { |
| 500 | for (const auto known : codecs) { |
| 501 | if (c.name == known) { |
| 502 | videoRecvParameters.codecs.push_back(c); |
| 503 | break; |
| 504 | } |
| 505 | } |
| 506 | } |
| 507 |
| 508 | videoRecvParameters.extensions.emplace_back(webrtc::RtpExtension::kTransportSequenceNumberUri, 2); |
| 509 | //recv_parameters.rtcp.reduced_size = true; |
| 510 | videoRecvParameters.rtcp.remote_estimate = true; |
| 511 |
| 512 | cricket::StreamParams videoRecvStreamParams; |
| 513 | cricket::SsrcGroup videoRecvSsrcGroup(cricket::kFecFrSsrcGroupSemantics, {_ssrcVideo.incoming, _ssrcVideo.fecIncoming}); |
| 514 | videoRecvStreamParams.ssrcs = {_ssrcVideo.incoming}; |
| 515 | videoRecvStreamParams.ssrc_groups.push_back(videoRecvSsrcGroup); |
| 516 | videoRecvStreamParams.cname = "cname"; |
| 517 | std::vector<std::string> streamIds; |
| 518 | streamIds.push_back("1"); |
| 519 | videoRecvStreamParams.set_stream_ids(streamIds); |
| 520 |
| 521 | _videoChannel->SetRecvParameters(videoRecvParameters); |
| 522 | _videoChannel->AddRecvStream(videoRecvStreamParams); |
| 523 | _readyToReceiveVideo = true; |
| 524 | if (_currentIncomingVideoSink) { |
| 525 | _videoChannel->SetSink(_ssrcVideo.incoming, _currentIncomingVideoSink.get()); |
| 526 | } |
| 527 | } |
| 528 | } |
| 529 |
| 530 | void MediaManager::setMuteOutgoingAudio(bool mute) { |
| 531 | setOutgoingAudioState(mute ? AudioState::Muted : AudioState::Active); |
| 532 | _audioChannel->SetAudioSend(_ssrcAudio.outgoing, _isConnected && (_outgoingAudioState == AudioState::Active), nullptr, &_audioSource); |
| 533 | } |
| 534 |
| 535 | void MediaManager::setOutgoingAudioState(AudioState state) { |
| 536 | if (_outgoingAudioState == state) { |
| 537 | return; |
| 538 | } |
| 539 | _outgoingAudioState = state; |
| 540 | sendOutgoingMediaStateMessage(); |
| 541 | } |
| 542 |
| 543 | void MediaManager::setOutgoingVideoState(VideoState state) { |
| 544 | if (_outgoingVideoState == state) { |
| 545 | return; |
| 546 | } |
| 547 | _outgoingVideoState = state; |
| 548 | sendOutgoingMediaStateMessage(); |
| 549 | } |
| 550 |
| 551 | void MediaManager::setIncomingVideoOutput(std::shared_ptr<rtc::VideoSinkInterface<webrtc::VideoFrame>> sink) { |
| 552 | _currentIncomingVideoSink = sink; |
| 553 | _videoChannel->SetSink(_ssrcVideo.incoming, _currentIncomingVideoSink.get()); |
| 554 | } |
| 555 |
| 556 | static bool IsRtcp(const uint8_t* packet, size_t length) { |
| 557 | webrtc::RtpUtility::RtpHeaderParser rtp_parser(packet, length); |
| 558 | return rtp_parser.RTCP(); |
| 559 | } |
| 560 |
| 561 | void MediaManager::receiveMessage(DecryptedMessage &&message) { |
| 562 | const auto data = &message.message.data; |
| 563 | if (const auto formats = absl::get_if<VideoFormatsMessage>(data)) { |
| 564 | setPeerVideoFormats(std::move(*formats)); |
| 565 | } else if (const auto audio = absl::get_if<AudioDataMessage>(data)) { |
| 566 | if (IsRtcp(audio->data.data(), audio->data.size())) { |
| 567 | RTC_LOG(LS_VERBOSE) << "Deliver audio RTCP"; |
| 568 | } |
| 569 | _call->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO, audio->data, -1); |
| 570 | } else if (const auto video = absl::get_if<VideoDataMessage>(data)) { |
| 571 | if (_videoChannel) { |
| 572 | if (_readyToReceiveVideo) { |
| 573 | _call->Receiver()->DeliverPacket(webrtc::MediaType::VIDEO, video->data, -1); |
| 574 | } else { |
| 575 | // maybe we need to queue packets for some time? |
| 576 | } |
| 577 | } |
| 578 | } else if (const auto videoParameters = absl::get_if<VideoParametersMessage>(data)) { |
| 579 | float value = ((float)videoParameters->aspectRatio) / 1000.0; |
| 580 | _preferredAspectRatio = value; |
| 581 | if (_videoCapture) { |
| 582 | _videoCapture->setPreferredAspectRatio(value); |
| 583 | } |
| 584 | } |
| 585 | } |
| 586 |
| 587 | void MediaManager::remoteVideoStateUpdated(VideoState videoState) { |
| 588 | switch (videoState) { |
| 589 | case VideoState::Active: |
| 590 | case VideoState::Paused: |
| 591 | configureSendingVideoIfNeeded(); |
| 592 | break; |
| 593 | default: |
| 594 | break; |
| 595 | } |
| 596 | } |
| 597 |
| 598 | void MediaManager::setIsCurrentNetworkLowCost(bool isCurrentNetworkLowCost) { |
| 599 | if (_isLowCostNetwork != isCurrentNetworkLowCost) { |
| 600 | _isLowCostNetwork = isCurrentNetworkLowCost; |
| 601 | RTC_LOG(LS_INFO) << "MediaManager isLowCostNetwork updated: " << isCurrentNetworkLowCost ? 1 : 0; |
| 602 | adjustBitratePreferences(false); |
| 603 | } |
| 604 | } |
| 605 |
| 606 | MediaManager::NetworkInterfaceImpl::NetworkInterfaceImpl(MediaManager *mediaManager, bool isVideo) : |
| 607 | _mediaManager(mediaManager), |
| 608 | _isVideo(isVideo) { |
| 609 | } |
| 610 |
| 611 | bool MediaManager::NetworkInterfaceImpl::SendPacket(rtc::CopyOnWriteBuffer *packet, const rtc::PacketOptions& options) { |
| 612 | return sendTransportMessage(packet, options); |
| 613 | } |
| 614 |
| 615 | bool MediaManager::NetworkInterfaceImpl::SendRtcp(rtc::CopyOnWriteBuffer *packet, const rtc::PacketOptions& options) { |
| 616 | return sendTransportMessage(packet, options); |
| 617 | } |
| 618 |
| 619 | bool MediaManager::NetworkInterfaceImpl::sendTransportMessage(rtc::CopyOnWriteBuffer *packet, const rtc::PacketOptions& options) { |
| 620 | if (_isVideo) { |
| 621 | RTC_LOG(LS_VERBOSE) << "Send video packet"; |
| 622 | } |
| 623 | _mediaManager->_sendTransportMessage(_isVideo |
| 624 | ? Message{ VideoDataMessage{ *packet } } |
| 625 | : Message{ AudioDataMessage{ *packet } }); |
| 626 | rtc::SentPacket sentPacket(options.packet_id, rtc::TimeMillis(), options.info_signaled_after_sent); |
| 627 | _mediaManager->notifyPacketSent(sentPacket); |
| 628 | return true; |
| 629 | } |
| 630 |
| 631 | int MediaManager::NetworkInterfaceImpl::SetOption(cricket::MediaChannel::NetworkInterface::SocketType, rtc::Socket::Option, int) { |
| 632 | return -1; |
| 633 | } |
| 634 |
| 635 | } // namespace tgcalls |
| 636 |