| #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 | #include "api/call/audio_sink.h" |
| 21 | #include "modules/audio_processing/audio_buffer.h" |
| 22 |
| 23 | namespace tgcalls { |
| 24 | namespace { |
| 25 |
| 26 | constexpr uint32_t ssrcAudioIncoming = 1; |
| 27 | constexpr uint32_t ssrcAudioOutgoing = 2; |
| 28 | constexpr uint32_t ssrcAudioFecIncoming = 5; |
| 29 | constexpr uint32_t ssrcAudioFecOutgoing = 6; |
| 30 | constexpr uint32_t ssrcVideoIncoming = 3; |
| 31 | constexpr uint32_t ssrcVideoOutgoing = 4; |
| 32 | constexpr uint32_t ssrcVideoFecIncoming = 7; |
| 33 | constexpr uint32_t ssrcVideoFecOutgoing = 8; |
| 34 |
| 35 | rtc::Thread *makeWorkerThread() { |
| 36 | static std::unique_ptr<rtc::Thread> value = rtc::Thread::Create(); |
| 37 | value->SetName("WebRTC-Worker", nullptr); |
| 38 | value->Start(); |
| 39 | return value.get(); |
| 40 | } |
| 41 |
| 42 | VideoCaptureInterfaceObject *GetVideoCaptureAssumingSameThread(VideoCaptureInterface *videoCapture) { |
| 43 | return videoCapture |
| 44 | ? static_cast<VideoCaptureInterfaceImpl*>(videoCapture)->object()->getSyncAssumingSameThread() |
| 45 | : nullptr; |
| 46 | } |
| 47 |
| 48 | } // namespace |
| 49 |
| 50 | class VideoSinkInterfaceProxyImpl : public rtc::VideoSinkInterface<webrtc::VideoFrame> { |
| 51 | public: |
| 52 | VideoSinkInterfaceProxyImpl(bool rewriteRotation) : |
| 53 | _rewriteRotation(rewriteRotation) { |
| 54 | } |
| 55 |
| 56 | virtual ~VideoSinkInterfaceProxyImpl() { |
| 57 | } |
| 58 |
| 59 | virtual void OnFrame(const webrtc::VideoFrame& frame) override { |
| 60 | if (_impl) { |
| 61 | if (_rewriteRotation) { |
| 62 | webrtc::VideoFrame updatedFrame = frame; |
| 63 | //updatedFrame.set_rotation(webrtc::VideoRotation::kVideoRotation_90); |
| 64 | _impl->OnFrame(updatedFrame); |
| 65 | } else { |
| 66 | _impl->OnFrame(frame); |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 |
| 71 | virtual void OnDiscardedFrame() override { |
| 72 | if (_impl) { |
| 73 | _impl->OnDiscardedFrame(); |
| 74 | } |
| 75 | } |
| 76 |
| 77 | void setSink(std::shared_ptr<rtc::VideoSinkInterface<webrtc::VideoFrame>> impl) { |
| 78 | _impl = impl; |
| 79 | } |
| 80 |
| 81 | private: |
| 82 | bool _rewriteRotation = false; |
| 83 | std::shared_ptr<rtc::VideoSinkInterface<webrtc::VideoFrame>> _impl; |
| 84 |
| 85 | }; |
| 86 |
| 87 | class AudioTrackSinkInterfaceImpl: public webrtc::AudioSinkInterface { |
| 88 | private: |
| 89 | std::function<void(float)> _update; |
| 90 |
| 91 | int _peakCount = 0; |
| 92 | uint16_t _peak = 0; |
| 93 |
| 94 | public: |
| 95 | AudioTrackSinkInterfaceImpl(std::function<void(float)> update) : |
| 96 | _update(update) { |
| 97 | } |
| 98 |
| 99 | virtual ~AudioTrackSinkInterfaceImpl() { |
| 100 | } |
| 101 |
| 102 | virtual void OnData(const Data& audio) override { |
| 103 | if (audio.channels == 1) { |
| 104 | int16_t *samples = (int16_t *)audio.data; |
| 105 | int numberOfSamplesInFrame = (int)audio.samples_per_channel; |
| 106 |
| 107 | for (int i = 0; i < numberOfSamplesInFrame; i++) { |
| 108 | int16_t sample = samples[i]; |
| 109 | if (sample < 0) { |
| 110 | sample = -sample; |
| 111 | } |
| 112 | if (_peak < sample) { |
| 113 | _peak = sample; |
| 114 | } |
| 115 | _peakCount += 1; |
| 116 | } |
| 117 |
| 118 | if (_peakCount >= 1200) { |
| 119 | float level = ((float)(_peak)) / 4000.0f; |
| 120 | _peak = 0; |
| 121 | _peakCount = 0; |
| 122 | _update(level); |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | }; |
| 127 |
| 128 | rtc::Thread *MediaManager::getWorkerThread() { |
| 129 | static rtc::Thread *value = makeWorkerThread(); |
| 130 | return value; |
| 131 | } |
| 132 |
| 133 | MediaManager::MediaManager( |
| 134 | rtc::Thread *thread, |
| 135 | bool isOutgoing, |
| 136 | ProtocolVersion protocolVersion, |
| 137 | const MediaDevicesConfig &devicesConfig, |
| 138 | std::shared_ptr<VideoCaptureInterface> videoCapture, |
| 139 | std::function<void(Message &&)> sendSignalingMessage, |
| 140 | std::function<void(Message &&)> sendTransportMessage, |
| 141 | std::function<void(int)> signalBarsUpdated, |
| 142 | std::function<void(float)> audioLevelUpdated, |
| 143 | bool enableHighBitrateVideo, |
| 144 | std::vector<std::string> preferredCodecs, |
| 145 | std::shared_ptr<PlatformContext> platformContext) : |
| 146 | _thread(thread), |
| 147 | _eventLog(std::make_unique<webrtc::RtcEventLogNull>()), |
| 148 | _taskQueueFactory(webrtc::CreateDefaultTaskQueueFactory()), |
| 149 | _sendSignalingMessage(std::move(sendSignalingMessage)), |
| 150 | _sendTransportMessage(std::move(sendTransportMessage)), |
| 151 | _signalBarsUpdated(std::move(signalBarsUpdated)), |
| 152 | _audioLevelUpdated(std::move(audioLevelUpdated)), |
| 153 | _protocolVersion(protocolVersion), |
| 154 | _outgoingVideoState(videoCapture ? VideoState::Active : VideoState::Inactive), |
| 155 | _videoCapture(std::move(videoCapture)), |
| 156 | _enableHighBitrateVideo(enableHighBitrateVideo), |
| 157 | _platformContext(platformContext) { |
| 158 | bool rewriteFrameRotation = false; |
| 159 | switch (_protocolVersion) { |
| 160 | case ProtocolVersion::V0: |
| 161 | rewriteFrameRotation = true; |
| 162 | break; |
| 163 | case ProtocolVersion::V1: |
| 164 | rewriteFrameRotation = false; |
| 165 | break; |
| 166 | default: |
| 167 | break; |
| 168 | } |
| 169 | _incomingVideoSinkProxy.reset(new VideoSinkInterfaceProxyImpl(rewriteFrameRotation)); |
| 170 |
| 171 | _ssrcAudio.incoming = isOutgoing ? ssrcAudioIncoming : ssrcAudioOutgoing; |
| 172 | _ssrcAudio.outgoing = (!isOutgoing) ? ssrcAudioIncoming : ssrcAudioOutgoing; |
| 173 | _ssrcAudio.fecIncoming = isOutgoing ? ssrcAudioFecIncoming : ssrcAudioFecOutgoing; |
| 174 | _ssrcAudio.fecOutgoing = (!isOutgoing) ? ssrcAudioFecIncoming : ssrcAudioFecOutgoing; |
| 175 | _ssrcVideo.incoming = isOutgoing ? ssrcVideoIncoming : ssrcVideoOutgoing; |
| 176 | _ssrcVideo.outgoing = (!isOutgoing) ? ssrcVideoIncoming : ssrcVideoOutgoing; |
| 177 | _ssrcVideo.fecIncoming = isOutgoing ? ssrcVideoFecIncoming : ssrcVideoFecOutgoing; |
| 178 | _ssrcVideo.fecOutgoing = (!isOutgoing) ? ssrcVideoFecIncoming : ssrcVideoFecOutgoing; |
| 179 |
| 180 | _audioNetworkInterface = std::unique_ptr<MediaManager::NetworkInterfaceImpl>(new MediaManager::NetworkInterfaceImpl(this, false)); |
| 181 | _videoNetworkInterface = std::unique_ptr<MediaManager::NetworkInterfaceImpl>(new MediaManager::NetworkInterfaceImpl(this, true)); |
| 182 |
| 183 | webrtc::field_trial::InitFieldTrialsFromString( |
| 184 | "WebRTC-Audio-SendSideBwe/Enabled/" |
| 185 | "WebRTC-Audio-Allocation/min:6kbps,max:32kbps/" |
| 186 | "WebRTC-Audio-OpusMinPacketLossRate/Enabled-1/" |
| 187 | "WebRTC-FlexFEC-03/Enabled/" |
| 188 | "WebRTC-FlexFEC-03-Advertised/Enabled/" |
| 189 | ); |
| 190 |
| 191 | PlatformInterface::SharedInstance()->configurePlatformAudio(); |
| 192 |
| 193 | _videoBitrateAllocatorFactory = webrtc::CreateBuiltinVideoBitrateAllocatorFactory(); |
| 194 |
| 195 | cricket::MediaEngineDependencies mediaDeps; |
| 196 | mediaDeps.task_queue_factory = _taskQueueFactory.get(); |
| 197 | mediaDeps.audio_encoder_factory = webrtc::CreateAudioEncoderFactory<webrtc::AudioEncoderOpus>(); |
| 198 | mediaDeps.audio_decoder_factory = webrtc::CreateAudioDecoderFactory<webrtc::AudioDecoderOpus>(); |
| 199 |
| 200 | mediaDeps.video_encoder_factory = PlatformInterface::SharedInstance()->makeVideoEncoderFactory(_platformContext); |
| 201 | mediaDeps.video_decoder_factory = PlatformInterface::SharedInstance()->makeVideoDecoderFactory(_platformContext); |
| 202 |
| 203 | _myVideoFormats = ComposeSupportedFormats( |
| 204 | mediaDeps.video_encoder_factory->GetSupportedFormats(), |
| 205 | mediaDeps.video_decoder_factory->GetSupportedFormats(), |
| 206 | preferredCodecs, |
| 207 | _platformContext); |
| 208 |
| 209 | mediaDeps.audio_processing = webrtc::AudioProcessingBuilder().Create(); |
| 210 |
| 211 | /*_audioDeviceModule = createAudioDeviceModule(); |
| 212 | if (!_audioDeviceModule) { |
| 213 | return; |
| 214 | } |
| 215 | mediaDeps.adm = _audioDeviceModule;*/ |
| 216 |
| 217 | _mediaEngine = cricket::CreateMediaEngine(std::move(mediaDeps)); |
| 218 | _mediaEngine->Init(); |
| 219 |
| 220 | /*setAudioInputDevice(devicesConfig.audioInputId); |
| 221 | setAudioOutputDevice(devicesConfig.audioOutputId); |
| 222 | setInputVolume(devicesConfig.inputVolume); |
| 223 | setOutputVolume(devicesConfig.outputVolume);*/ |
| 224 |
| 225 | webrtc::Call::Config callConfig(_eventLog.get()); |
| 226 | callConfig.task_queue_factory = _taskQueueFactory.get(); |
| 227 | callConfig.trials = &_fieldTrials; |
| 228 | callConfig.audio_state = _mediaEngine->voice().GetAudioState(); |
| 229 | _call.reset(webrtc::Call::Create(callConfig)); |
| 230 |
| 231 | cricket::AudioOptions audioOptions; |
| 232 | audioOptions.echo_cancellation = true; |
| 233 | audioOptions.noise_suppression = true; |
| 234 | audioOptions.audio_jitter_buffer_fast_accelerate = true; |
| 235 |
| 236 | std::vector<std::string> streamIds; |
| 237 | streamIds.push_back("1"); |
| 238 |
| 239 | _audioChannel.reset(_mediaEngine->voice().CreateMediaChannel(_call.get(), cricket::MediaConfig(), audioOptions, webrtc::CryptoOptions::NoGcm())); |
| 240 | _videoChannel.reset(_mediaEngine->video().CreateMediaChannel(_call.get(), cricket::MediaConfig(), cricket::VideoOptions(), webrtc::CryptoOptions::NoGcm(), _videoBitrateAllocatorFactory.get())); |
| 241 |
| 242 | const uint32_t opusClockrate = 48000; |
| 243 | const uint16_t opusSdpPayload = 111; |
| 244 | const char *opusSdpName = "opus"; |
| 245 | const uint8_t opusSdpChannels = 2; |
| 246 | const uint32_t opusSdpBitrate = 0; |
| 247 |
| 248 | const uint8_t opusMinBitrateKbps = 6; |
| 249 | const uint8_t opusMaxBitrateKbps = 32; |
| 250 | const uint8_t opusStartBitrateKbps = 8; |
| 251 | const uint8_t opusPTimeMs = 120; |
| 252 |
| 253 | cricket::AudioCodec opusCodec(opusSdpPayload, opusSdpName, opusClockrate, opusSdpBitrate, opusSdpChannels); |
| 254 | opusCodec.AddFeedbackParam(cricket::FeedbackParam(cricket::kRtcpFbParamTransportCc)); |
| 255 | opusCodec.SetParam(cricket::kCodecParamMinBitrate, opusMinBitrateKbps); |
| 256 | opusCodec.SetParam(cricket::kCodecParamStartBitrate, opusStartBitrateKbps); |
| 257 | opusCodec.SetParam(cricket::kCodecParamMaxBitrate, opusMaxBitrateKbps); |
| 258 | opusCodec.SetParam(cricket::kCodecParamUseInbandFec, 1); |
| 259 | opusCodec.SetParam(cricket::kCodecParamPTime, opusPTimeMs); |
| 260 |
| 261 | cricket::AudioSendParameters audioSendPrameters; |
| 262 | audioSendPrameters.codecs.push_back(opusCodec); |
| 263 | audioSendPrameters.extensions.emplace_back(webrtc::RtpExtension::kTransportSequenceNumberUri, 1); |
| 264 | audioSendPrameters.options.echo_cancellation = true; |
| 265 | //audioSendPrameters.options.experimental_ns = false; |
| 266 | audioSendPrameters.options.noise_suppression = true; |
| 267 | audioSendPrameters.options.auto_gain_control = true; |
| 268 | //audioSendPrameters.options.highpass_filter = false; |
| 269 | audioSendPrameters.options.typing_detection = false; |
| 270 | //audioSendPrameters.max_bandwidth_bps = 16000; |
| 271 | audioSendPrameters.rtcp.reduced_size = true; |
| 272 | audioSendPrameters.rtcp.remote_estimate = true; |
| 273 | _audioChannel->SetSendParameters(audioSendPrameters); |
| 274 | _audioChannel->AddSendStream(cricket::StreamParams::CreateLegacy(_ssrcAudio.outgoing)); |
| 275 | _audioChannel->SetInterface(_audioNetworkInterface.get()); |
| 276 |
| 277 | cricket::AudioRecvParameters audioRecvParameters; |
| 278 | audioRecvParameters.codecs.emplace_back(opusSdpPayload, opusSdpName, opusClockrate, opusSdpBitrate, opusSdpChannels); |
| 279 | audioRecvParameters.extensions.emplace_back(webrtc::RtpExtension::kTransportSequenceNumberUri, 1); |
| 280 | audioRecvParameters.rtcp.reduced_size = true; |
| 281 | audioRecvParameters.rtcp.remote_estimate = true; |
| 282 |
| 283 | _audioChannel->SetRecvParameters(audioRecvParameters); |
| 284 | cricket::StreamParams audioRecvStreamParams = cricket::StreamParams::CreateLegacy(_ssrcAudio.incoming); |
| 285 | audioRecvStreamParams.set_stream_ids(streamIds); |
| 286 | _audioChannel->AddRecvStream(audioRecvStreamParams); |
| 287 | _audioChannel->SetPlayout(true); |
| 288 |
| 289 | _videoChannel->SetInterface(_videoNetworkInterface.get()); |
| 290 |
| 291 | adjustBitratePreferences(true); |
| 292 | } |
| 293 |
| 294 | rtc::scoped_refptr<webrtc::AudioDeviceModule> MediaManager::createAudioDeviceModule() { |
| 295 | const auto check = [&](webrtc::AudioDeviceModule::AudioLayer layer) { |
| 296 | auto result = webrtc::AudioDeviceModule::Create( |
| 297 | layer, |
| 298 | _taskQueueFactory.get()); |
| 299 | return (result && (result->Init() == 0)) ? result : nullptr; |
| 300 | }; |
| 301 | if (auto result = check(webrtc::AudioDeviceModule::kPlatformDefaultAudio)) { |
| 302 | return result; |
| 303 | #ifdef WEBRTC_LINUX |
| 304 | } else if (auto result = check(webrtc::AudioDeviceModule::kLinuxAlsaAudio)) { |
| 305 | return result; |
| 306 | #endif // WEBRTC_LINUX |
| 307 | } |
| 308 | return nullptr; |
| 309 | } |
| 310 |
| 311 | void MediaManager::start() { |
| 312 | const auto weak = std::weak_ptr<MediaManager>(shared_from_this()); |
| 313 |
| 314 | // Here we hope that thread outlives the sink |
| 315 | rtc::Thread *thread = _thread; |
| 316 | std::unique_ptr<AudioTrackSinkInterfaceImpl> incomingSink(new AudioTrackSinkInterfaceImpl([weak, thread](float level) { |
| 317 | thread->PostTask(RTC_FROM_HERE, [weak, level] { |
| 318 | if (const auto strong = weak.lock()) { |
| 319 | strong->_currentAudioLevel = level; |
| 320 | } |
| 321 | }); |
| 322 | })); |
| 323 | std::unique_ptr<AudioTrackSinkInterfaceImpl> outgoingSink(new AudioTrackSinkInterfaceImpl([weak, thread](float level) { |
| 324 | thread->PostTask(RTC_FROM_HERE, [weak, level] { |
| 325 | if (const auto strong = weak.lock()) { |
| 326 | strong->_currentMyAudioLevel = level; |
| 327 | } |
| 328 | }); |
| 329 | })); |
| 330 | _audioChannel->SetRawAudioSink(_ssrcAudio.incoming, std::move(incomingSink)); |
| 331 | _audioChannel->SetRawAudioSink(_ssrcAudio.outgoing, std::move(outgoingSink)); |
| 332 |
| 333 | _sendSignalingMessage({ _myVideoFormats }); |
| 334 |
| 335 | if (_videoCapture != nullptr) { |
| 336 | setSendVideo(_videoCapture); |
| 337 | } |
| 338 |
| 339 | beginStatsTimer(3000); |
| 340 | if (_audioLevelUpdated != nullptr) { |
| 341 | beginLevelsTimer(50); |
| 342 | } |
| 343 | } |
| 344 |
| 345 | MediaManager::~MediaManager() { |
| 346 | assert(_thread->IsCurrent()); |
| 347 |
| 348 | RTC_LOG(LS_INFO) << "MediaManager::~MediaManager()"; |
| 349 |
| 350 | _call->SignalChannelNetworkState(webrtc::MediaType::AUDIO, webrtc::kNetworkDown); |
| 351 | _call->SignalChannelNetworkState(webrtc::MediaType::VIDEO, webrtc::kNetworkDown); |
| 352 |
| 353 | _audioChannel->OnReadyToSend(false); |
| 354 | _audioChannel->SetSend(false); |
| 355 | _audioChannel->SetAudioSend(_ssrcAudio.outgoing, false, nullptr, &_audioSource); |
| 356 |
| 357 | _audioChannel->SetPlayout(false); |
| 358 |
| 359 | _audioChannel->RemoveRecvStream(_ssrcAudio.incoming); |
| 360 | _audioChannel->RemoveSendStream(_ssrcAudio.outgoing); |
| 361 |
| 362 | _audioChannel->SetInterface(nullptr); |
| 363 |
| 364 | setSendVideo(nullptr); |
| 365 |
| 366 | if (computeIsReceivingVideo()) { |
| 367 | _videoChannel->RemoveRecvStream(_ssrcVideo.incoming); |
| 368 | if (_enableFlexfec) { |
| 369 | _videoChannel->RemoveRecvStream(_ssrcVideo.fecIncoming); |
| 370 | } |
| 371 | } |
| 372 |
| 373 | if (_didConfigureVideo) { |
| 374 | _videoChannel->OnReadyToSend(false); |
| 375 | _videoChannel->SetSend(false); |
| 376 |
| 377 | if (_enableFlexfec) { |
| 378 | _videoChannel->RemoveSendStream(_ssrcVideo.outgoing); |
| 379 | _videoChannel->RemoveSendStream(_ssrcVideo.fecOutgoing); |
| 380 | } else { |
| 381 | _videoChannel->RemoveSendStream(_ssrcVideo.outgoing); |
| 382 | } |
| 383 | } |
| 384 |
| 385 | _videoChannel->SetInterface(nullptr); |
| 386 | } |
| 387 |
| 388 | void MediaManager::setIsConnected(bool isConnected) { |
| 389 | if (_isConnected == isConnected) { |
| 390 | return; |
| 391 | } |
| 392 | bool isFirstConnection = false; |
| 393 | if (!_isConnected && isConnected) { |
| 394 | _didConnectOnce = true; |
| 395 | isFirstConnection = true; |
| 396 | } |
| 397 | _isConnected = isConnected; |
| 398 |
| 399 | if (_isConnected) { |
| 400 | _call->SignalChannelNetworkState(webrtc::MediaType::AUDIO, webrtc::kNetworkUp); |
| 401 | _call->SignalChannelNetworkState(webrtc::MediaType::VIDEO, webrtc::kNetworkUp); |
| 402 | } else { |
| 403 | _call->SignalChannelNetworkState(webrtc::MediaType::AUDIO, webrtc::kNetworkDown); |
| 404 | _call->SignalChannelNetworkState(webrtc::MediaType::VIDEO, webrtc::kNetworkDown); |
| 405 | } |
| 406 | if (_audioChannel) { |
| 407 | _audioChannel->OnReadyToSend(_isConnected); |
| 408 | _audioChannel->SetSend(_isConnected); |
| 409 | _audioChannel->SetAudioSend(_ssrcAudio.outgoing, _isConnected && (_outgoingAudioState == AudioState::Active), nullptr, &_audioSource); |
| 410 | } |
| 411 | if (computeIsSendingVideo() && _videoChannel) { |
| 412 | _videoChannel->OnReadyToSend(_isConnected); |
| 413 | _videoChannel->SetSend(_isConnected); |
| 414 | } |
| 415 | if (isFirstConnection) { |
| 416 | sendVideoParametersMessage(); |
| 417 | sendOutgoingMediaStateMessage(); |
| 418 | } |
| 419 | } |
| 420 |
| 421 | void MediaManager::sendVideoParametersMessage() { |
| 422 | const auto aspectRatioValue = uint32_t(_localPreferredVideoAspectRatio * 1000.0); |
| 423 | _sendTransportMessage({ VideoParametersMessage{ aspectRatioValue } }); |
| 424 | } |
| 425 |
| 426 | void MediaManager::sendOutgoingMediaStateMessage() { |
| 427 | _sendTransportMessage({ RemoteMediaStateMessage{ _outgoingAudioState, _outgoingVideoState } }); |
| 428 | } |
| 429 |
| 430 | void MediaManager::beginStatsTimer(int timeoutMs) { |
| 431 | const auto weak = std::weak_ptr<MediaManager>(shared_from_this()); |
| 432 | _thread->PostDelayedTask(RTC_FROM_HERE, [weak]() { |
| 433 | auto strong = weak.lock(); |
| 434 | if (!strong) { |
| 435 | return; |
| 436 | } |
| 437 | strong->collectStats(); |
| 438 | }, timeoutMs); |
| 439 | } |
| 440 |
| 441 | void MediaManager::beginLevelsTimer(int timeoutMs) { |
| 442 | const auto weak = std::weak_ptr<MediaManager>(shared_from_this()); |
| 443 | _thread->PostDelayedTask(RTC_FROM_HERE, [weak]() { |
| 444 | auto strong = weak.lock(); |
| 445 | if (!strong) { |
| 446 | return; |
| 447 | } |
| 448 |
| 449 | float effectiveLevel = fmaxf(strong->_currentAudioLevel, strong->_currentMyAudioLevel); |
| 450 | strong->_audioLevelUpdated(effectiveLevel); |
| 451 |
| 452 | strong->beginLevelsTimer(50); |
| 453 | }, timeoutMs); |
| 454 | } |
| 455 |
| 456 | void MediaManager::collectStats() { |
| 457 | auto stats = _call->GetStats(); |
| 458 | float bitrateNorm = 16.0f; |
| 459 | switch (_outgoingVideoState) { |
| 460 | case VideoState::Active: |
| 461 | bitrateNorm = 600.0f; |
| 462 | break; |
| 463 | default: |
| 464 | break; |
| 465 | } |
| 466 | float sendBitrateKbps = ((float)stats.send_bandwidth_bps / 1000.0f); |
| 467 |
| 468 | RTC_LOG(LS_INFO) << "MediaManager sendBitrateKbps=" << (stats.send_bandwidth_bps / 1000); |
| 469 |
| 470 | float signalBarsNorm = 4.0f; |
| 471 | float adjustedQuality = sendBitrateKbps / bitrateNorm; |
| 472 | adjustedQuality = fmaxf(0.0f, adjustedQuality); |
| 473 | adjustedQuality = fminf(1.0f, adjustedQuality); |
| 474 | if (_signalBarsUpdated) { |
| 475 | _signalBarsUpdated((int)(adjustedQuality * signalBarsNorm)); |
| 476 | } |
| 477 |
| 478 | _bitrateRecords.push_back(CallStatsBitrateRecord { (int32_t)(rtc::TimeMillis() / 1000), stats.send_bandwidth_bps / 1000 }); |
| 479 |
| 480 | beginStatsTimer(2000); |
| 481 | } |
| 482 |
| 483 | void MediaManager::notifyPacketSent(const rtc::SentPacket &sentPacket) { |
| 484 | _call->OnSentPacket(sentPacket); |
| 485 | } |
| 486 |
| 487 | void MediaManager::setPeerVideoFormats(VideoFormatsMessage &&peerFormats) { |
| 488 | if (!_videoCodecs.empty()) { |
| 489 | return; |
| 490 | } |
| 491 |
| 492 | bool wasReceivingVideo = computeIsReceivingVideo(); |
| 493 |
| 494 | assert(!_videoCodecOut.has_value()); |
| 495 | auto formats = ComputeCommonFormats( |
| 496 | _myVideoFormats, |
| 497 | std::move(peerFormats)); |
| 498 | auto codecs = AssignPayloadTypesAndDefaultCodecs(std::move(formats)); |
| 499 | if (codecs.myEncoderIndex >= 0) { |
| 500 | assert(codecs.myEncoderIndex < codecs.list.size()); |
| 501 | _videoCodecOut = codecs.list[codecs.myEncoderIndex]; |
| 502 | } |
| 503 | _videoCodecs = std::move(codecs.list); |
| 504 | if (_videoCodecOut.has_value()) { |
| 505 | checkIsSendingVideoChanged(false); |
| 506 | } |
| 507 | if (_videoCodecs.size() != 0) { |
| 508 | checkIsReceivingVideoChanged(wasReceivingVideo); |
| 509 | } |
| 510 | } |
| 511 |
| 512 | bool MediaManager::videoCodecsNegotiated() const { |
| 513 | return !_videoCodecs.empty(); |
| 514 | } |
| 515 |
| 516 | bool MediaManager::computeIsSendingVideo() const { |
| 517 | return _videoCapture != nullptr && _videoCodecOut.has_value(); |
| 518 | } |
| 519 |
| 520 | bool MediaManager::computeIsReceivingVideo() const { |
| 521 | return _videoCodecs.size() != 0; |
| 522 | } |
| 523 |
| 524 | void MediaManager::setSendVideo(std::shared_ptr<VideoCaptureInterface> videoCapture) { |
| 525 | const auto wasSending = computeIsSendingVideo(); |
| 526 | const auto wasReceiving = computeIsReceivingVideo(); |
| 527 |
| 528 | if (_videoCapture) { |
| 529 | GetVideoCaptureAssumingSameThread(_videoCapture.get())->setStateUpdated(nullptr); |
| 530 | } |
| 531 | _videoCapture = videoCapture; |
| 532 | if (_videoCapture) { |
| 533 | _videoCapture->setPreferredAspectRatio(_preferredAspectRatio); |
| 534 |
| 535 | const auto thread = _thread; |
| 536 | const auto weak = std::weak_ptr<MediaManager>(shared_from_this()); |
| 537 | GetVideoCaptureAssumingSameThread(_videoCapture.get())->setStateUpdated([=](VideoState state) { |
| 538 | thread->PostTask(RTC_FROM_HERE, [=] { |
| 539 | if (const auto strong = weak.lock()) { |
| 540 | strong->setOutgoingVideoState(state); |
| 541 | } |
| 542 | }); |
| 543 | }); |
| 544 | setOutgoingVideoState(VideoState::Active); |
| 545 | } else { |
| 546 | setOutgoingVideoState(VideoState::Inactive); |
| 547 | } |
| 548 |
| 549 | checkIsSendingVideoChanged(wasSending); |
| 550 | checkIsReceivingVideoChanged(wasReceiving); |
| 551 | } |
| 552 |
| 553 | void MediaManager::setRequestedVideoAspect(float aspect) { |
| 554 | if (_localPreferredVideoAspectRatio != aspect) { |
| 555 | _localPreferredVideoAspectRatio = aspect; |
| 556 | if (_didConnectOnce) { |
| 557 | sendVideoParametersMessage(); |
| 558 | } |
| 559 | } |
| 560 | } |
| 561 |
| 562 | void MediaManager::configureSendingVideoIfNeeded() { |
| 563 | if (_didConfigureVideo) { |
| 564 | return; |
| 565 | } |
| 566 | if (!_videoCodecOut.has_value()) { |
| 567 | return; |
| 568 | } |
| 569 | _didConfigureVideo = true; |
| 570 |
| 571 | auto codec = *_videoCodecOut; |
| 572 |
| 573 | codec.SetParam(cricket::kCodecParamMinBitrate, 64); |
| 574 | codec.SetParam(cricket::kCodecParamStartBitrate, 400); |
| 575 | codec.SetParam(cricket::kCodecParamMaxBitrate, _enableHighBitrateVideo ? 2000 : 800); |
| 576 |
| 577 | cricket::VideoSendParameters videoSendParameters; |
| 578 | videoSendParameters.codecs.push_back(codec); |
| 579 |
| 580 | if (_enableFlexfec) { |
| 581 | for (auto &c : _videoCodecs) { |
| 582 | if (c.name == cricket::kFlexfecCodecName) { |
| 583 | videoSendParameters.codecs.push_back(c); |
| 584 | break; |
| 585 | } |
| 586 | } |
| 587 | } |
| 588 |
| 589 | videoSendParameters.extensions.emplace_back(webrtc::RtpExtension::kTransportSequenceNumberUri, 2); |
| 590 | switch (_protocolVersion) { |
| 591 | case ProtocolVersion::V1: |
| 592 | videoSendParameters.extensions.emplace_back(webrtc::RtpExtension::kVideoRotationUri, 3); |
| 593 | videoSendParameters.extensions.emplace_back( |
| 594 | webrtc::RtpExtension::kTimestampOffsetUri, 4); |
| 595 | break; |
| 596 | default: |
| 597 | break; |
| 598 | } |
| 599 | videoSendParameters.rtcp.remote_estimate = true; |
| 600 | _videoChannel->SetSendParameters(videoSendParameters); |
| 601 |
| 602 | if (_enableFlexfec) { |
| 603 | cricket::StreamParams videoSendStreamParams; |
| 604 | cricket::SsrcGroup videoSendSsrcGroup(cricket::kFecFrSsrcGroupSemantics, {_ssrcVideo.outgoing, _ssrcVideo.fecOutgoing}); |
| 605 | videoSendStreamParams.ssrcs = {_ssrcVideo.outgoing}; |
| 606 | videoSendStreamParams.ssrc_groups.push_back(videoSendSsrcGroup); |
| 607 | videoSendStreamParams.cname = "cname"; |
| 608 | _videoChannel->AddSendStream(videoSendStreamParams); |
| 609 | } else { |
| 610 | _videoChannel->AddSendStream(cricket::StreamParams::CreateLegacy(_ssrcVideo.outgoing)); |
| 611 | } |
| 612 |
| 613 | adjustBitratePreferences(true); |
| 614 | } |
| 615 |
| 616 | void MediaManager::checkIsSendingVideoChanged(bool wasSending) { |
| 617 | const auto sending = computeIsSendingVideo(); |
| 618 | if (sending == wasSending) { |
| 619 | return; |
| 620 | } else if (sending) { |
| 621 | configureSendingVideoIfNeeded(); |
| 622 |
| 623 | if (_enableFlexfec) { |
| 624 | _videoChannel->SetVideoSend(_ssrcVideo.outgoing, NULL, GetVideoCaptureAssumingSameThread(_videoCapture.get())->source()); |
| 625 | _videoChannel->SetVideoSend(_ssrcVideo.fecOutgoing, NULL, nullptr); |
| 626 | } else { |
| 627 | _videoChannel->SetVideoSend(_ssrcVideo.outgoing, NULL, GetVideoCaptureAssumingSameThread(_videoCapture.get())->source()); |
| 628 | } |
| 629 |
| 630 | _videoChannel->OnReadyToSend(_isConnected); |
| 631 | _videoChannel->SetSend(_isConnected); |
| 632 | } else { |
| 633 | _videoChannel->SetVideoSend(_ssrcVideo.outgoing, NULL, nullptr); |
| 634 | _videoChannel->SetVideoSend(_ssrcVideo.fecOutgoing, NULL, nullptr); |
| 635 | } |
| 636 |
| 637 | adjustBitratePreferences(true); |
| 638 | } |
| 639 |
| 640 | int MediaManager::getMaxVideoBitrate() const { |
| 641 | return (_enableHighBitrateVideo && _isLowCostNetwork) ? 2000000 : 800000; |
| 642 | } |
| 643 |
| 644 | int MediaManager::getMaxAudioBitrate() const { |
| 645 | if (_isDataSavingActive) { |
| 646 | return 16000; |
| 647 | } else { |
| 648 | return 32000; |
| 649 | } |
| 650 | } |
| 651 |
| 652 | void MediaManager::adjustBitratePreferences(bool resetStartBitrate) { |
| 653 | if (computeIsSendingVideo()) { |
| 654 | webrtc::BitrateConstraints preferences; |
| 655 | preferences.min_bitrate_bps = 64000; |
| 656 | if (resetStartBitrate) { |
| 657 | preferences.start_bitrate_bps = 400000; |
| 658 | } |
| 659 | preferences.max_bitrate_bps = getMaxVideoBitrate(); |
| 660 |
| 661 | _call->GetTransportControllerSend()->SetSdpBitrateParameters(preferences); |
| 662 | } else { |
| 663 | webrtc::BitrateConstraints preferences; |
| 664 | if (_didConfigureVideo) { |
| 665 | // After we have configured outgoing video, RTCP stops working for outgoing audio |
| 666 | // TODO: investigate |
| 667 | preferences.min_bitrate_bps = 16000; |
| 668 | if (resetStartBitrate) { |
| 669 | preferences.start_bitrate_bps = 16000; |
| 670 | } |
| 671 | preferences.max_bitrate_bps = 32000; |
| 672 | } else { |
| 673 | preferences.min_bitrate_bps = 8000; |
| 674 | if (resetStartBitrate) { |
| 675 | preferences.start_bitrate_bps = 16000; |
| 676 | } |
| 677 | preferences.max_bitrate_bps = getMaxAudioBitrate(); |
| 678 | } |
| 679 |
| 680 | _call->GetTransportControllerSend()->SetSdpBitrateParameters(preferences); |
| 681 | } |
| 682 | } |
| 683 |
| 684 | void MediaManager::checkIsReceivingVideoChanged(bool wasReceiving) { |
| 685 | const auto receiving = computeIsReceivingVideo(); |
| 686 | if (receiving == wasReceiving) { |
| 687 | return; |
| 688 | } else { |
| 689 | cricket::VideoRecvParameters videoRecvParameters; |
| 690 |
| 691 | const auto codecs = { |
| 692 | cricket::kFlexfecCodecName, |
| 693 | cricket::kH264CodecName, |
| 694 | cricket::kH265CodecName, |
| 695 | cricket::kVp8CodecName, |
| 696 | cricket::kVp9CodecName, |
| 697 | cricket::kAv1CodecName, |
| 698 | }; |
| 699 | for (const auto &c : _videoCodecs) { |
| 700 | for (const auto known : codecs) { |
| 701 | if (c.name == known) { |
| 702 | videoRecvParameters.codecs.push_back(c); |
| 703 | break; |
| 704 | } |
| 705 | } |
| 706 | } |
| 707 |
| 708 | videoRecvParameters.extensions.emplace_back(webrtc::RtpExtension::kTransportSequenceNumberUri, 2); |
| 709 | switch (_protocolVersion) { |
| 710 | case ProtocolVersion::V1: |
| 711 | videoRecvParameters.extensions.emplace_back(webrtc::RtpExtension::kVideoRotationUri, 3); |
| 712 | videoRecvParameters.extensions.emplace_back( |
| 713 | webrtc::RtpExtension::kTimestampOffsetUri, 4); |
| 714 | break; |
| 715 | default: |
| 716 | break; |
| 717 | } |
| 718 | videoRecvParameters.rtcp.reduced_size = true; |
| 719 | videoRecvParameters.rtcp.remote_estimate = true; |
| 720 |
| 721 | cricket::StreamParams videoRecvStreamParams; |
| 722 | cricket::SsrcGroup videoRecvSsrcGroup(cricket::kFecFrSsrcGroupSemantics, {_ssrcVideo.incoming, _ssrcVideo.fecIncoming}); |
| 723 | videoRecvStreamParams.ssrcs = {_ssrcVideo.incoming}; |
| 724 | videoRecvStreamParams.ssrc_groups.push_back(videoRecvSsrcGroup); |
| 725 | videoRecvStreamParams.cname = "cname"; |
| 726 | std::vector<std::string> streamIds; |
| 727 | streamIds.push_back("1"); |
| 728 | videoRecvStreamParams.set_stream_ids(streamIds); |
| 729 |
| 730 | _videoChannel->SetRecvParameters(videoRecvParameters); |
| 731 | _videoChannel->AddRecvStream(videoRecvStreamParams); |
| 732 | _readyToReceiveVideo = true; |
| 733 | _videoChannel->SetSink(_ssrcVideo.incoming, _incomingVideoSinkProxy.get()); |
| 734 | } |
| 735 | } |
| 736 |
| 737 | void MediaManager::setMuteOutgoingAudio(bool mute) { |
| 738 | setOutgoingAudioState(mute ? AudioState::Muted : AudioState::Active); |
| 739 | _audioChannel->SetAudioSend(_ssrcAudio.outgoing, _isConnected && (_outgoingAudioState == AudioState::Active), nullptr, &_audioSource); |
| 740 | } |
| 741 |
| 742 | void MediaManager::setOutgoingAudioState(AudioState state) { |
| 743 | if (_outgoingAudioState == state) { |
| 744 | return; |
| 745 | } |
| 746 | _outgoingAudioState = state; |
| 747 | sendOutgoingMediaStateMessage(); |
| 748 | } |
| 749 |
| 750 | void MediaManager::setOutgoingVideoState(VideoState state) { |
| 751 | if (_outgoingVideoState == state) { |
| 752 | return; |
| 753 | } |
| 754 | _outgoingVideoState = state; |
| 755 | sendOutgoingMediaStateMessage(); |
| 756 | } |
| 757 |
| 758 | void MediaManager::setIncomingVideoOutput(std::shared_ptr<rtc::VideoSinkInterface<webrtc::VideoFrame>> sink) { |
| 759 | _incomingVideoSinkProxy->setSink(sink); |
| 760 | } |
| 761 |
| 762 | static bool IsRtcp(const uint8_t* packet, size_t length) { |
| 763 | webrtc::RtpUtility::RtpHeaderParser rtp_parser(packet, length); |
| 764 | return rtp_parser.RTCP(); |
| 765 | } |
| 766 |
| 767 | void MediaManager::receiveMessage(DecryptedMessage &&message) { |
| 768 | const auto data = &message.message.data; |
| 769 | if (const auto formats = absl::get_if<VideoFormatsMessage>(data)) { |
| 770 | setPeerVideoFormats(std::move(*formats)); |
| 771 | } else if (const auto audio = absl::get_if<AudioDataMessage>(data)) { |
| 772 | if (IsRtcp(audio->data.data(), audio->data.size())) { |
| 773 | RTC_LOG(LS_VERBOSE) << "Deliver audio RTCP"; |
| 774 | } |
| 775 | _call->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO, audio->data, -1); |
| 776 | } else if (const auto video = absl::get_if<VideoDataMessage>(data)) { |
| 777 | if (_videoChannel) { |
| 778 | if (_readyToReceiveVideo) { |
| 779 | _call->Receiver()->DeliverPacket(webrtc::MediaType::VIDEO, video->data, -1); |
| 780 | } else { |
| 781 | // maybe we need to queue packets for some time? |
| 782 | } |
| 783 | } |
| 784 | } else if (const auto videoParameters = absl::get_if<VideoParametersMessage>(data)) { |
| 785 | float value = ((float)videoParameters->aspectRatio) / 1000.0; |
| 786 | _preferredAspectRatio = value; |
| 787 | if (_videoCapture) { |
| 788 | _videoCapture->setPreferredAspectRatio(value); |
| 789 | } |
| 790 | } |
| 791 | } |
| 792 |
| 793 | void MediaManager::remoteVideoStateUpdated(VideoState videoState) { |
| 794 | switch (videoState) { |
| 795 | case VideoState::Active: |
| 796 | case VideoState::Paused: |
| 797 | configureSendingVideoIfNeeded(); |
| 798 | break; |
| 799 | default: |
| 800 | break; |
| 801 | } |
| 802 | } |
| 803 |
| 804 | void MediaManager::setNetworkParameters(bool isLowCost, bool isDataSavingActive) { |
| 805 | if (_isLowCostNetwork != isLowCost || _isDataSavingActive != isDataSavingActive) { |
| 806 | _isLowCostNetwork = isLowCost; |
| 807 | _isDataSavingActive = isDataSavingActive; |
| 808 | RTC_LOG(LS_INFO) << "MediaManager isLowCostNetwork: " << (isLowCost ? 1 : 0) << ", isDataSavingActive: " << (isDataSavingActive ? 1 : 0); |
| 809 | adjustBitratePreferences(false); |
| 810 | } |
| 811 | } |
| 812 |
| 813 | void MediaManager::fillCallStats(CallStats &callStats) { |
| 814 | if (_videoCodecOut.has_value()) { |
| 815 | callStats.outgoingCodec = _videoCodecOut->name; |
| 816 | } |
| 817 | callStats.bitrateRecords = std::move(_bitrateRecords); |
| 818 | } |
| 819 |
| 820 | void MediaManager::setAudioInputDevice(std::string id) { |
| 821 | const auto recording = _audioDeviceModule->Recording(); |
| 822 | if (recording) { |
| 823 | _audioDeviceModule->StopRecording(); |
| 824 | } |
| 825 | const auto finish = [&] { |
| 826 | if (recording) { |
| 827 | _audioDeviceModule->InitRecording(); |
| 828 | _audioDeviceModule->StartRecording(); |
| 829 | } |
| 830 | }; |
| 831 | if (id == "default" || id.empty()) { |
| 832 | if (const auto result = _audioDeviceModule->SetRecordingDevice(webrtc::AudioDeviceModule::kDefaultCommunicationDevice)) { |
| 833 | RTC_LOG(LS_ERROR) << "setAudioInputDevice(" << id << "): SetRecordingDevice(kDefaultCommunicationDevice) failed: " << result << "."; |
| 834 | } else { |
| 835 | RTC_LOG(LS_INFO) << "setAudioInputDevice(" << id << "): SetRecordingDevice(kDefaultCommunicationDevice) success."; |
| 836 | } |
| 837 | return finish(); |
| 838 | } |
| 839 | const auto count = _audioDeviceModule |
| 840 | ? _audioDeviceModule->RecordingDevices() |
| 841 | : int16_t(-666); |
| 842 | if (count <= 0) { |
| 843 | RTC_LOG(LS_ERROR) << "setAudioInputDevice(" << id << "): Could not get recording devices count: " << count << "."; |
| 844 | return finish(); |
| 845 | } |
| 846 | for (auto i = 0; i != count; ++i) { |
| 847 | char name[webrtc::kAdmMaxDeviceNameSize + 1] = { 0 }; |
| 848 | char guid[webrtc::kAdmMaxGuidSize + 1] = { 0 }; |
| 849 | _audioDeviceModule->RecordingDeviceName(i, name, guid); |
| 850 | if (id == guid) { |
| 851 | const auto result = _audioDeviceModule->SetRecordingDevice(i); |
| 852 | if (result != 0) { |
| 853 | RTC_LOG(LS_ERROR) << "setAudioInputDevice(" << id << ") name '" << std::string(name) << "' failed: " << result << "."; |
| 854 | } else { |
| 855 | RTC_LOG(LS_INFO) << "setAudioInputDevice(" << id << ") name '" << std::string(name) << "' success."; |
| 856 | } |
| 857 | return finish(); |
| 858 | } |
| 859 | } |
| 860 | RTC_LOG(LS_ERROR) << "setAudioInputDevice(" << id << "): Could not find recording device."; |
| 861 | return finish(); |
| 862 | } |
| 863 |
| 864 | void MediaManager::setAudioOutputDevice(std::string id) { |
| 865 | const auto playing = _audioDeviceModule->Playing(); |
| 866 | if (playing) { |
| 867 | _audioDeviceModule->StopPlayout(); |
| 868 | } |
| 869 | const auto finish = [&] { |
| 870 | if (playing) { |
| 871 | _audioDeviceModule->InitPlayout(); |
| 872 | _audioDeviceModule->StartPlayout(); |
| 873 | } |
| 874 | }; |
| 875 | if (id == "default" || id.empty()) { |
| 876 | if (const auto result = _audioDeviceModule->SetPlayoutDevice(webrtc::AudioDeviceModule::kDefaultCommunicationDevice)) { |
| 877 | RTC_LOG(LS_ERROR) << "setAudioOutputDevice(" << id << "): SetPlayoutDevice(kDefaultCommunicationDevice) failed: " << result << "."; |
| 878 | } else { |
| 879 | RTC_LOG(LS_INFO) << "setAudioOutputDevice(" << id << "): SetPlayoutDevice(kDefaultCommunicationDevice) success."; |
| 880 | } |
| 881 | return finish(); |
| 882 | } |
| 883 | const auto count = _audioDeviceModule |
| 884 | ? _audioDeviceModule->PlayoutDevices() |
| 885 | : int16_t(-666); |
| 886 | if (count <= 0) { |
| 887 | RTC_LOG(LS_ERROR) << "setAudioOutputDevice(" << id << "): Could not get playout devices count: " << count << "."; |
| 888 | return finish(); |
| 889 | } |
| 890 | for (auto i = 0; i != count; ++i) { |
| 891 | char name[webrtc::kAdmMaxDeviceNameSize + 1] = { 0 }; |
| 892 | char guid[webrtc::kAdmMaxGuidSize + 1] = { 0 }; |
| 893 | _audioDeviceModule->PlayoutDeviceName(i, name, guid); |
| 894 | if (id == guid) { |
| 895 | const auto result = _audioDeviceModule->SetPlayoutDevice(i); |
| 896 | if (result != 0) { |
| 897 | RTC_LOG(LS_ERROR) << "setAudioOutputDevice(" << id << ") name '" << std::string(name) << "' failed: " << result << "."; |
| 898 | } else { |
| 899 | RTC_LOG(LS_INFO) << "setAudioOutputDevice(" << id << ") name '" << std::string(name) << "' success."; |
| 900 | } |
| 901 | return finish(); |
| 902 | } |
| 903 | } |
| 904 | RTC_LOG(LS_ERROR) << "setAudioOutputDevice(" << id << "): Could not find playout device."; |
| 905 | return finish(); |
| 906 | } |
| 907 |
| 908 | void MediaManager::setInputVolume(float level) { |
| 909 | // This is not what we want, it changes OS volume on macOS. |
| 910 | // auto min = uint32_t(); |
| 911 | // auto max = uint32_t(); |
| 912 | // if (const auto result = _audioDeviceModule->MinMicrophoneVolume(&min)) { |
| 913 | // RTC_LOG(LS_ERROR) << "setInputVolume(" << level << "): MinMicrophoneVolume failed: " << result << "."; |
| 914 | // return; |
| 915 | // } else if (const auto result = _audioDeviceModule->MaxMicrophoneVolume(&max)) { |
| 916 | // RTC_LOG(LS_ERROR) << "setInputVolume(" << level << "): MaxMicrophoneVolume failed: " << result << "."; |
| 917 | // return; |
| 918 | // } |
| 919 | // const auto volume = min + uint32_t(std::round((max - min) * std::min(std::max(level, 0.f), 1.f))); |
| 920 | // if (const auto result = _audioDeviceModule->SetMicrophoneVolume(volume)) { |
| 921 | // RTC_LOG(LS_ERROR) << "setInputVolume(" << level << "): SetMicrophoneVolume(" << volume << ") failed: " << result << "."; |
| 922 | // } else { |
| 923 | // RTC_LOG(LS_INFO) << "setInputVolume(" << level << ") volume " << volume << " success."; |
| 924 | // } |
| 925 | } |
| 926 |
| 927 | void MediaManager::setOutputVolume(float level) { |
| 928 | // This is not what we want, it changes OS volume on macOS. |
| 929 | // auto min = uint32_t(); |
| 930 | // auto max = uint32_t(); |
| 931 | // if (const auto result = _audioDeviceModule->MinSpeakerVolume(&min)) { |
| 932 | // RTC_LOG(LS_ERROR) << "setOutputVolume(" << level << "): MinSpeakerVolume failed: " << result << "."; |
| 933 | // return; |
| 934 | // } else if (const auto result = _audioDeviceModule->MaxSpeakerVolume(&max)) { |
| 935 | // RTC_LOG(LS_ERROR) << "setOutputVolume(" << level << "): MaxSpeakerVolume failed: " << result << "."; |
| 936 | // return; |
| 937 | // } |
| 938 | // const auto volume = min + uint32_t(std::round((max - min) * std::min(std::max(level, 0.f), 1.f))); |
| 939 | // if (const auto result = _audioDeviceModule->SetSpeakerVolume(volume)) { |
| 940 | // RTC_LOG(LS_ERROR) << "setOutputVolume(" << level << "): SetSpeakerVolume(" << volume << ") failed: " << result << "."; |
| 941 | // } else { |
| 942 | // RTC_LOG(LS_INFO) << "setOutputVolume(" << level << ") volume " << volume << " success."; |
| 943 | // } |
| 944 | } |
| 945 |
| 946 | MediaManager::NetworkInterfaceImpl::NetworkInterfaceImpl(MediaManager *mediaManager, bool isVideo) : |
| 947 | _mediaManager(mediaManager), |
| 948 | _isVideo(isVideo) { |
| 949 | } |
| 950 |
| 951 | bool MediaManager::NetworkInterfaceImpl::SendPacket(rtc::CopyOnWriteBuffer *packet, const rtc::PacketOptions& options) { |
| 952 | return sendTransportMessage(packet, options); |
| 953 | } |
| 954 |
| 955 | bool MediaManager::NetworkInterfaceImpl::SendRtcp(rtc::CopyOnWriteBuffer *packet, const rtc::PacketOptions& options) { |
| 956 | return sendTransportMessage(packet, options); |
| 957 | } |
| 958 |
| 959 | bool MediaManager::NetworkInterfaceImpl::sendTransportMessage(rtc::CopyOnWriteBuffer *packet, const rtc::PacketOptions& options) { |
| 960 | if (_isVideo) { |
| 961 | RTC_LOG(LS_VERBOSE) << "Send video packet"; |
| 962 | } |
| 963 | _mediaManager->_sendTransportMessage(_isVideo |
| 964 | ? Message{ VideoDataMessage{ *packet } } |
| 965 | : Message{ AudioDataMessage{ *packet } }); |
| 966 | rtc::SentPacket sentPacket(options.packet_id, rtc::TimeMillis(), options.info_signaled_after_sent); |
| 967 | _mediaManager->notifyPacketSent(sentPacket); |
| 968 | return true; |
| 969 | } |
| 970 |
| 971 | int MediaManager::NetworkInterfaceImpl::SetOption(cricket::MediaChannel::NetworkInterface::SocketType, rtc::Socket::Option, int) { |
| 972 | return -1; |
| 973 | } |
| 974 |
| 975 | } // namespace tgcalls |
| 976 |