OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/media/engine/webrtcvideoengine2.h" | 11 #include "webrtc/media/engine/webrtcvideoengine2.h" |
12 | 12 |
13 #include <stdio.h> | 13 #include <stdio.h> |
14 #include <algorithm> | 14 #include <algorithm> |
15 #include <set> | 15 #include <set> |
16 #include <string> | 16 #include <string> |
17 #include <utility> | |
18 | 17 |
19 #include "webrtc/base/copyonwritebuffer.h" | 18 #include "webrtc/base/copyonwritebuffer.h" |
20 #include "webrtc/base/logging.h" | 19 #include "webrtc/base/logging.h" |
21 #include "webrtc/base/stringutils.h" | 20 #include "webrtc/base/stringutils.h" |
22 #include "webrtc/base/timeutils.h" | 21 #include "webrtc/base/timeutils.h" |
23 #include "webrtc/base/trace_event.h" | 22 #include "webrtc/base/trace_event.h" |
24 #include "webrtc/call.h" | 23 #include "webrtc/call.h" |
25 #include "webrtc/media/engine/constants.h" | 24 #include "webrtc/media/engine/constants.h" |
26 #include "webrtc/media/engine/simulcast.h" | 25 #include "webrtc/media/engine/simulcast.h" |
27 #include "webrtc/media/engine/webrtcmediaengine.h" | 26 #include "webrtc/media/engine/webrtcmediaengine.h" |
(...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
316 } | 315 } |
317 | 316 |
318 int GetDefaultVp9TemporalLayers() { | 317 int GetDefaultVp9TemporalLayers() { |
319 int num_sl; | 318 int num_sl; |
320 int num_tl; | 319 int num_tl; |
321 if (GetVp9LayersFromFieldTrialGroup(&num_sl, &num_tl)) { | 320 if (GetVp9LayersFromFieldTrialGroup(&num_sl, &num_tl)) { |
322 return num_tl; | 321 return num_tl; |
323 } | 322 } |
324 return 1; | 323 return 1; |
325 } | 324 } |
326 | |
327 class EncoderStreamFactory | |
328 : public webrtc::VideoEncoderConfig::VideoStreamFactoryInterface { | |
329 public: | |
330 EncoderStreamFactory(std::string codec_name, | |
331 int max_qp, | |
332 int max_framerate, | |
333 bool is_screencast, | |
334 bool conference_mode) | |
335 : codec_name_(codec_name), | |
336 max_qp_(max_qp), | |
337 max_framerate_(max_framerate), | |
338 is_screencast_(is_screencast), | |
339 conference_mode_(conference_mode) {} | |
340 | |
341 private: | |
342 std::vector<webrtc::VideoStream> CreateEncoderStreams( | |
343 int width, | |
344 int height, | |
345 const webrtc::VideoEncoderConfig& encoder_config) override { | |
346 RTC_DCHECK(encoder_config.number_of_streams > 1 ? !is_screencast_ : true); | |
347 if (encoder_config.number_of_streams > 1) { | |
348 return GetSimulcastConfig(encoder_config.number_of_streams, width, height, | |
349 encoder_config.max_bitrate_bps, max_qp_, | |
350 max_framerate_); | |
351 } | |
352 | |
353 // For unset max bitrates set default bitrate for non-simulcast. | |
354 int max_bitrate_bps = | |
355 (encoder_config.max_bitrate_bps > 0) | |
356 ? encoder_config.max_bitrate_bps | |
357 : GetMaxDefaultVideoBitrateKbps(width, height) * 1000; | |
358 | |
359 webrtc::VideoStream stream; | |
360 stream.width = width; | |
361 stream.height = height; | |
362 stream.max_framerate = max_framerate_; | |
363 stream.min_bitrate_bps = kMinVideoBitrateKbps * 1000; | |
364 stream.target_bitrate_bps = stream.max_bitrate_bps = max_bitrate_bps; | |
365 stream.max_qp = max_qp_; | |
366 | |
367 // Conference mode screencast uses 2 temporal layers split at 100kbit. | |
368 if (conference_mode_ && is_screencast_) { | |
369 ScreenshareLayerConfig config = ScreenshareLayerConfig::GetDefault(); | |
370 // For screenshare in conference mode, tl0 and tl1 bitrates are | |
371 // piggybacked | |
372 // on the VideoCodec struct as target and max bitrates, respectively. | |
373 // See eg. webrtc::VP8EncoderImpl::SetRates(). | |
374 stream.target_bitrate_bps = config.tl0_bitrate_kbps * 1000; | |
375 stream.max_bitrate_bps = config.tl1_bitrate_kbps * 1000; | |
376 stream.temporal_layer_thresholds_bps.clear(); | |
377 stream.temporal_layer_thresholds_bps.push_back(config.tl0_bitrate_kbps * | |
378 1000); | |
379 } | |
380 | |
381 if (CodecNamesEq(codec_name_, kVp9CodecName) && !is_screencast_) { | |
382 stream.temporal_layer_thresholds_bps.resize( | |
383 GetDefaultVp9TemporalLayers() - 1); | |
384 } | |
385 | |
386 std::vector<webrtc::VideoStream> streams; | |
387 streams.push_back(stream); | |
388 return streams; | |
389 } | |
390 | |
391 const std::string codec_name_; | |
392 const int max_qp_; | |
393 const int max_framerate_; | |
394 const bool is_screencast_; | |
395 const bool conference_mode_; | |
396 }; | |
397 | |
398 } // namespace | 325 } // namespace |
399 | 326 |
400 // Constants defined in webrtc/media/engine/constants.h | 327 // Constants defined in webrtc/media/engine/constants.h |
401 // TODO(pbos): Move these to a separate constants.cc file. | 328 // TODO(pbos): Move these to a separate constants.cc file. |
402 const int kMinVideoBitrateKbps = 30; | 329 const int kMinVideoBitrate = 30; |
| 330 const int kStartVideoBitrate = 300; |
403 | 331 |
404 const int kVideoMtu = 1200; | 332 const int kVideoMtu = 1200; |
405 const int kVideoRtpBufferSize = 65536; | 333 const int kVideoRtpBufferSize = 65536; |
406 | 334 |
407 // This constant is really an on/off, lower-level configurable NACK history | 335 // This constant is really an on/off, lower-level configurable NACK history |
408 // duration hasn't been implemented. | 336 // duration hasn't been implemented. |
409 static const int kNackHistoryMs = 1000; | 337 static const int kNackHistoryMs = 1000; |
410 | 338 |
411 static const int kDefaultQpMax = 56; | 339 static const int kDefaultQpMax = 56; |
412 | 340 |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
463 codec.SetParam(kH264FmtpLevelAsymmetryAllowed, "1"); | 391 codec.SetParam(kH264FmtpLevelAsymmetryAllowed, "1"); |
464 codec.SetParam(kH264FmtpPacketizationMode, "1"); | 392 codec.SetParam(kH264FmtpPacketizationMode, "1"); |
465 AddCodecAndMaybeRtxCodec(codec, &codecs); | 393 AddCodecAndMaybeRtxCodec(codec, &codecs); |
466 } | 394 } |
467 AddCodecAndMaybeRtxCodec(VideoCodec(kDefaultRedPlType, kRedCodecName), | 395 AddCodecAndMaybeRtxCodec(VideoCodec(kDefaultRedPlType, kRedCodecName), |
468 &codecs); | 396 &codecs); |
469 codecs.push_back(VideoCodec(kDefaultUlpfecType, kUlpfecCodecName)); | 397 codecs.push_back(VideoCodec(kDefaultUlpfecType, kUlpfecCodecName)); |
470 return codecs; | 398 return codecs; |
471 } | 399 } |
472 | 400 |
| 401 std::vector<webrtc::VideoStream> |
| 402 WebRtcVideoChannel2::WebRtcVideoSendStream::CreateSimulcastVideoStreams( |
| 403 const VideoCodec& codec, |
| 404 const VideoOptions& options, |
| 405 int max_bitrate_bps, |
| 406 size_t num_streams) { |
| 407 int max_qp = kDefaultQpMax; |
| 408 codec.GetParam(kCodecParamMaxQuantization, &max_qp); |
| 409 |
| 410 return GetSimulcastConfig( |
| 411 num_streams, codec.width, codec.height, max_bitrate_bps, max_qp, |
| 412 codec.framerate != 0 ? codec.framerate : kDefaultVideoMaxFramerate); |
| 413 } |
| 414 |
| 415 std::vector<webrtc::VideoStream> |
| 416 WebRtcVideoChannel2::WebRtcVideoSendStream::CreateVideoStreams( |
| 417 const VideoCodec& codec, |
| 418 const VideoOptions& options, |
| 419 int max_bitrate_bps, |
| 420 size_t num_streams) { |
| 421 int codec_max_bitrate_kbps; |
| 422 if (codec.GetParam(kCodecParamMaxBitrate, &codec_max_bitrate_kbps)) { |
| 423 max_bitrate_bps = codec_max_bitrate_kbps * 1000; |
| 424 } |
| 425 if (num_streams != 1) { |
| 426 return CreateSimulcastVideoStreams(codec, options, max_bitrate_bps, |
| 427 num_streams); |
| 428 } |
| 429 |
| 430 // For unset max bitrates set default bitrate for non-simulcast. |
| 431 if (max_bitrate_bps <= 0) { |
| 432 max_bitrate_bps = |
| 433 GetMaxDefaultVideoBitrateKbps(codec.width, codec.height) * 1000; |
| 434 } |
| 435 |
| 436 webrtc::VideoStream stream; |
| 437 stream.width = codec.width; |
| 438 stream.height = codec.height; |
| 439 stream.max_framerate = |
| 440 codec.framerate != 0 ? codec.framerate : kDefaultVideoMaxFramerate; |
| 441 |
| 442 stream.min_bitrate_bps = kMinVideoBitrate * 1000; |
| 443 stream.target_bitrate_bps = stream.max_bitrate_bps = max_bitrate_bps; |
| 444 |
| 445 int max_qp = kDefaultQpMax; |
| 446 codec.GetParam(kCodecParamMaxQuantization, &max_qp); |
| 447 stream.max_qp = max_qp; |
| 448 std::vector<webrtc::VideoStream> streams; |
| 449 streams.push_back(stream); |
| 450 return streams; |
| 451 } |
| 452 |
473 rtc::scoped_refptr<webrtc::VideoEncoderConfig::EncoderSpecificSettings> | 453 rtc::scoped_refptr<webrtc::VideoEncoderConfig::EncoderSpecificSettings> |
474 WebRtcVideoChannel2::WebRtcVideoSendStream::ConfigureVideoEncoderSettings( | 454 WebRtcVideoChannel2::WebRtcVideoSendStream::ConfigureVideoEncoderSettings( |
475 const VideoCodec& codec) { | 455 const VideoCodec& codec) { |
476 RTC_DCHECK_RUN_ON(&thread_checker_); | |
477 bool is_screencast = parameters_.options.is_screencast.value_or(false); | 456 bool is_screencast = parameters_.options.is_screencast.value_or(false); |
478 // No automatic resizing when using simulcast or screencast. | 457 // No automatic resizing when using simulcast or screencast. |
479 bool automatic_resize = | 458 bool automatic_resize = |
480 !is_screencast && parameters_.config.rtp.ssrcs.size() == 1; | 459 !is_screencast && parameters_.config.rtp.ssrcs.size() == 1; |
481 bool frame_dropping = !is_screencast; | 460 bool frame_dropping = !is_screencast; |
482 bool denoising; | 461 bool denoising; |
483 bool codec_default_denoising = false; | 462 bool codec_default_denoising = false; |
484 if (is_screencast) { | 463 if (is_screencast) { |
485 denoising = false; | 464 denoising = false; |
486 } else { | 465 } else { |
(...skipping 1070 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1557 | 1536 |
1558 WebRtcVideoChannel2::WebRtcVideoSendStream::VideoSendStreamParameters:: | 1537 WebRtcVideoChannel2::WebRtcVideoSendStream::VideoSendStreamParameters:: |
1559 VideoSendStreamParameters( | 1538 VideoSendStreamParameters( |
1560 webrtc::VideoSendStream::Config config, | 1539 webrtc::VideoSendStream::Config config, |
1561 const VideoOptions& options, | 1540 const VideoOptions& options, |
1562 int max_bitrate_bps, | 1541 int max_bitrate_bps, |
1563 const rtc::Optional<VideoCodecSettings>& codec_settings) | 1542 const rtc::Optional<VideoCodecSettings>& codec_settings) |
1564 : config(std::move(config)), | 1543 : config(std::move(config)), |
1565 options(options), | 1544 options(options), |
1566 max_bitrate_bps(max_bitrate_bps), | 1545 max_bitrate_bps(max_bitrate_bps), |
1567 conference_mode(false), | |
1568 codec_settings(codec_settings) {} | 1546 codec_settings(codec_settings) {} |
1569 | 1547 |
1570 WebRtcVideoChannel2::WebRtcVideoSendStream::AllocatedEncoder::AllocatedEncoder( | 1548 WebRtcVideoChannel2::WebRtcVideoSendStream::AllocatedEncoder::AllocatedEncoder( |
1571 webrtc::VideoEncoder* encoder, | 1549 webrtc::VideoEncoder* encoder, |
1572 webrtc::VideoCodecType type, | 1550 webrtc::VideoCodecType type, |
1573 bool external) | 1551 bool external) |
1574 : encoder(encoder), | 1552 : encoder(encoder), |
1575 external_encoder(nullptr), | 1553 external_encoder(nullptr), |
1576 type(type), | 1554 type(type), |
1577 external(external) { | 1555 external(external) { |
(...skipping 24 matching lines...) Expand all Loading... |
1602 cpu_restricted_counter_(0), | 1580 cpu_restricted_counter_(0), |
1603 number_of_cpu_adapt_changes_(0), | 1581 number_of_cpu_adapt_changes_(0), |
1604 frame_count_(0), | 1582 frame_count_(0), |
1605 cpu_restricted_frame_count_(0), | 1583 cpu_restricted_frame_count_(0), |
1606 source_(nullptr), | 1584 source_(nullptr), |
1607 external_encoder_factory_(external_encoder_factory), | 1585 external_encoder_factory_(external_encoder_factory), |
1608 stream_(nullptr), | 1586 stream_(nullptr), |
1609 encoder_sink_(nullptr), | 1587 encoder_sink_(nullptr), |
1610 parameters_(std::move(config), options, max_bitrate_bps, codec_settings), | 1588 parameters_(std::move(config), options, max_bitrate_bps, codec_settings), |
1611 rtp_parameters_(CreateRtpParametersWithOneEncoding()), | 1589 rtp_parameters_(CreateRtpParametersWithOneEncoding()), |
| 1590 pending_encoder_reconfiguration_(false), |
1612 allocated_encoder_(nullptr, webrtc::kVideoCodecUnknown, false), | 1591 allocated_encoder_(nullptr, webrtc::kVideoCodecUnknown, false), |
1613 sending_(false), | 1592 sending_(false), |
1614 last_frame_timestamp_us_(0) { | 1593 last_frame_timestamp_us_(0) { |
1615 parameters_.config.rtp.max_packet_size = kVideoMtu; | 1594 parameters_.config.rtp.max_packet_size = kVideoMtu; |
1616 parameters_.conference_mode = send_params.conference_mode; | 1595 parameters_.conference_mode = send_params.conference_mode; |
1617 | 1596 |
1618 sp.GetPrimarySsrcs(¶meters_.config.rtp.ssrcs); | 1597 sp.GetPrimarySsrcs(¶meters_.config.rtp.ssrcs); |
1619 sp.GetFidSsrcs(parameters_.config.rtp.ssrcs, | 1598 sp.GetFidSsrcs(parameters_.config.rtp.ssrcs, |
1620 ¶meters_.config.rtp.rtx.ssrcs); | 1599 ¶meters_.config.rtp.rtx.ssrcs); |
1621 parameters_.config.rtp.c_name = sp.cname; | 1600 parameters_.config.rtp.c_name = sp.cname; |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1669 rtc::CritScope cs(&lock_); | 1648 rtc::CritScope cs(&lock_); |
1670 | 1649 |
1671 if (video_frame.width() != last_frame_info_.width || | 1650 if (video_frame.width() != last_frame_info_.width || |
1672 video_frame.height() != last_frame_info_.height || | 1651 video_frame.height() != last_frame_info_.height || |
1673 video_frame.rotation() != last_frame_info_.rotation || | 1652 video_frame.rotation() != last_frame_info_.rotation || |
1674 video_frame.is_texture() != last_frame_info_.is_texture) { | 1653 video_frame.is_texture() != last_frame_info_.is_texture) { |
1675 last_frame_info_.width = video_frame.width(); | 1654 last_frame_info_.width = video_frame.width(); |
1676 last_frame_info_.height = video_frame.height(); | 1655 last_frame_info_.height = video_frame.height(); |
1677 last_frame_info_.rotation = video_frame.rotation(); | 1656 last_frame_info_.rotation = video_frame.rotation(); |
1678 last_frame_info_.is_texture = video_frame.is_texture(); | 1657 last_frame_info_.is_texture = video_frame.is_texture(); |
| 1658 pending_encoder_reconfiguration_ = true; |
1679 | 1659 |
1680 LOG(LS_INFO) << "Video frame parameters changed: dimensions=" | 1660 LOG(LS_INFO) << "Video frame parameters changed: dimensions=" |
1681 << last_frame_info_.width << "x" << last_frame_info_.height | 1661 << last_frame_info_.width << "x" << last_frame_info_.height |
1682 << ", rotation=" << last_frame_info_.rotation | 1662 << ", rotation=" << last_frame_info_.rotation |
1683 << ", texture=" << last_frame_info_.is_texture; | 1663 << ", texture=" << last_frame_info_.is_texture; |
1684 } | 1664 } |
1685 | 1665 |
1686 if (encoder_sink_ == NULL) { | 1666 if (encoder_sink_ == NULL) { |
1687 // Frame input before send codecs are configured, dropping frame. | 1667 // Frame input before send codecs are configured, dropping frame. |
1688 return; | 1668 return; |
1689 } | 1669 } |
1690 | 1670 |
1691 last_frame_timestamp_us_ = video_frame.timestamp_us(); | 1671 last_frame_timestamp_us_ = video_frame.timestamp_us(); |
1692 | 1672 |
| 1673 if (pending_encoder_reconfiguration_) { |
| 1674 ReconfigureEncoder(); |
| 1675 pending_encoder_reconfiguration_ = false; |
| 1676 } |
| 1677 |
| 1678 // Not sending, abort after reconfiguration. Reconfiguration should still |
| 1679 // occur to permit sending this input as quickly as possible once we start |
| 1680 // sending (without having to reconfigure then). |
| 1681 if (!sending_) { |
| 1682 return; |
| 1683 } |
| 1684 |
1693 ++frame_count_; | 1685 ++frame_count_; |
1694 if (cpu_restricted_counter_ > 0) | 1686 if (cpu_restricted_counter_ > 0) |
1695 ++cpu_restricted_frame_count_; | 1687 ++cpu_restricted_frame_count_; |
1696 | 1688 |
1697 // Forward frame to the encoder regardless if we are sending or not. This is | |
1698 // to ensure that the encoder can be reconfigured with the correct frame size | |
1699 // as quickly as possible. | |
1700 encoder_sink_->OnFrame(video_frame); | 1689 encoder_sink_->OnFrame(video_frame); |
1701 } | 1690 } |
1702 | 1691 |
1703 bool WebRtcVideoChannel2::WebRtcVideoSendStream::SetVideoSend( | 1692 bool WebRtcVideoChannel2::WebRtcVideoSendStream::SetVideoSend( |
1704 bool enable, | 1693 bool enable, |
1705 const VideoOptions* options, | 1694 const VideoOptions* options, |
1706 rtc::VideoSourceInterface<cricket::VideoFrame>* source) { | 1695 rtc::VideoSourceInterface<cricket::VideoFrame>* source) { |
1707 TRACE_EVENT0("webrtc", "WebRtcVideoSendStream::SetVideoSend"); | 1696 TRACE_EVENT0("webrtc", "WebRtcVideoSendStream::SetVideoSend"); |
1708 RTC_DCHECK_RUN_ON(&thread_checker_); | 1697 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
1709 | 1698 |
1710 // Ignore |options| pointer if |enable| is false. | 1699 // Ignore |options| pointer if |enable| is false. |
1711 bool options_present = enable && options; | 1700 bool options_present = enable && options; |
1712 bool source_changing = source_ != source; | 1701 bool source_changing = source_ != source; |
1713 if (source_changing) { | 1702 if (source_changing) { |
1714 DisconnectSource(); | 1703 DisconnectSource(); |
1715 } | 1704 } |
1716 | 1705 |
1717 if (options_present) { | 1706 if (options_present || source_changing) { |
1718 VideoOptions old_options = parameters_.options; | 1707 rtc::CritScope cs(&lock_); |
1719 parameters_.options.SetAll(*options); | 1708 |
1720 // If options has changed and SetCodec has been called. | 1709 if (options_present) { |
1721 if (parameters_.options != old_options && stream_) { | 1710 VideoOptions old_options = parameters_.options; |
1722 ReconfigureEncoder(); | 1711 parameters_.options.SetAll(*options); |
| 1712 // Reconfigure encoder settings on the next frame or stream |
| 1713 // recreation if the options changed. |
| 1714 if (parameters_.options != old_options) { |
| 1715 pending_encoder_reconfiguration_ = true; |
| 1716 } |
| 1717 } |
| 1718 |
| 1719 if (source_changing) { |
| 1720 if (source == nullptr && encoder_sink_ != nullptr) { |
| 1721 LOG(LS_VERBOSE) << "Disabling capturer, sending black frame."; |
| 1722 // Force this black frame not to be dropped due to timestamp order |
| 1723 // check. As IncomingCapturedFrame will drop the frame if this frame's |
| 1724 // timestamp is less than or equal to last frame's timestamp, it is |
| 1725 // necessary to give this black frame a larger timestamp than the |
| 1726 // previous one. |
| 1727 last_frame_timestamp_us_ += rtc::kNumMicrosecsPerMillisec; |
| 1728 rtc::scoped_refptr<webrtc::I420Buffer> black_buffer( |
| 1729 webrtc::I420Buffer::Create(last_frame_info_.width, |
| 1730 last_frame_info_.height)); |
| 1731 black_buffer->SetToBlack(); |
| 1732 |
| 1733 encoder_sink_->OnFrame(webrtc::VideoFrame( |
| 1734 black_buffer, last_frame_info_.rotation, last_frame_timestamp_us_)); |
| 1735 } |
| 1736 source_ = source; |
1723 } | 1737 } |
1724 } | 1738 } |
1725 | 1739 |
1726 if (source_changing) { | 1740 // |source_->AddOrUpdateSink| may not be called while holding |lock_| since |
1727 rtc::CritScope cs(&lock_); | 1741 // that might cause a lock order inversion. |
1728 if (source == nullptr && encoder_sink_ != nullptr && | |
1729 last_frame_info_.width > 0) { | |
1730 LOG(LS_VERBOSE) << "Disabling capturer, sending black frame."; | |
1731 // Force this black frame not to be dropped due to timestamp order | |
1732 // check. As IncomingCapturedFrame will drop the frame if this frame's | |
1733 // timestamp is less than or equal to last frame's timestamp, it is | |
1734 // necessary to give this black frame a larger timestamp than the | |
1735 // previous one. | |
1736 last_frame_timestamp_us_ += rtc::kNumMicrosecsPerMillisec; | |
1737 rtc::scoped_refptr<webrtc::I420Buffer> black_buffer( | |
1738 webrtc::I420Buffer::Create(last_frame_info_.width, | |
1739 last_frame_info_.height)); | |
1740 black_buffer->SetToBlack(); | |
1741 | |
1742 encoder_sink_->OnFrame(webrtc::VideoFrame( | |
1743 black_buffer, last_frame_info_.rotation, last_frame_timestamp_us_)); | |
1744 } | |
1745 source_ = source; | |
1746 } | |
1747 | |
1748 if (source_changing && source_) { | 1742 if (source_changing && source_) { |
1749 // |source_->AddOrUpdateSink| may not be called while holding |lock_| since | |
1750 // that might cause a lock order inversion. | |
1751 source_->AddOrUpdateSink(this, sink_wants_); | 1743 source_->AddOrUpdateSink(this, sink_wants_); |
1752 } | 1744 } |
1753 return true; | 1745 return true; |
1754 } | 1746 } |
1755 | 1747 |
1756 void WebRtcVideoChannel2::WebRtcVideoSendStream::DisconnectSource() { | 1748 void WebRtcVideoChannel2::WebRtcVideoSendStream::DisconnectSource() { |
1757 RTC_DCHECK_RUN_ON(&thread_checker_); | 1749 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
1758 if (source_ == nullptr) { | 1750 if (source_ == nullptr) { |
1759 return; | 1751 return; |
1760 } | 1752 } |
1761 | 1753 |
1762 // |source_->RemoveSink| may not be called while holding |lock_| since | 1754 // |source_->RemoveSink| may not be called while holding |lock_| since |
1763 // that might cause a lock order inversion. | 1755 // that might cause a lock order inversion. |
1764 source_->RemoveSink(this); | 1756 source_->RemoveSink(this); |
1765 source_ = nullptr; | 1757 source_ = nullptr; |
1766 // Reset |cpu_restricted_counter_| if the source is changed. It is not | 1758 // Reset |cpu_restricted_counter_| if the source is changed. It is not |
1767 // possible to know if the video resolution is restricted by CPU usage after | 1759 // possible to know if the video resolution is restricted by CPU usage after |
(...skipping 14 matching lines...) Expand all Loading... |
1782 return webrtc::kVideoCodecVP9; | 1774 return webrtc::kVideoCodecVP9; |
1783 } else if (CodecNamesEq(name, kH264CodecName)) { | 1775 } else if (CodecNamesEq(name, kH264CodecName)) { |
1784 return webrtc::kVideoCodecH264; | 1776 return webrtc::kVideoCodecH264; |
1785 } | 1777 } |
1786 return webrtc::kVideoCodecUnknown; | 1778 return webrtc::kVideoCodecUnknown; |
1787 } | 1779 } |
1788 | 1780 |
1789 WebRtcVideoChannel2::WebRtcVideoSendStream::AllocatedEncoder | 1781 WebRtcVideoChannel2::WebRtcVideoSendStream::AllocatedEncoder |
1790 WebRtcVideoChannel2::WebRtcVideoSendStream::CreateVideoEncoder( | 1782 WebRtcVideoChannel2::WebRtcVideoSendStream::CreateVideoEncoder( |
1791 const VideoCodec& codec) { | 1783 const VideoCodec& codec) { |
1792 RTC_DCHECK_RUN_ON(&thread_checker_); | |
1793 webrtc::VideoCodecType type = CodecTypeFromName(codec.name); | 1784 webrtc::VideoCodecType type = CodecTypeFromName(codec.name); |
1794 | 1785 |
1795 // Do not re-create encoders of the same type. | 1786 // Do not re-create encoders of the same type. |
1796 if (type == allocated_encoder_.type && allocated_encoder_.encoder != NULL) { | 1787 if (type == allocated_encoder_.type && allocated_encoder_.encoder != NULL) { |
1797 return allocated_encoder_; | 1788 return allocated_encoder_; |
1798 } | 1789 } |
1799 | 1790 |
1800 if (external_encoder_factory_ != NULL) { | 1791 if (external_encoder_factory_ != NULL) { |
1801 webrtc::VideoEncoder* encoder = | 1792 webrtc::VideoEncoder* encoder = |
1802 external_encoder_factory_->CreateVideoEncoder(type); | 1793 external_encoder_factory_->CreateVideoEncoder(type); |
(...skipping 14 matching lines...) Expand all Loading... |
1817 } | 1808 } |
1818 | 1809 |
1819 // This shouldn't happen, we should not be trying to create something we don't | 1810 // This shouldn't happen, we should not be trying to create something we don't |
1820 // support. | 1811 // support. |
1821 RTC_DCHECK(false); | 1812 RTC_DCHECK(false); |
1822 return AllocatedEncoder(NULL, webrtc::kVideoCodecUnknown, false); | 1813 return AllocatedEncoder(NULL, webrtc::kVideoCodecUnknown, false); |
1823 } | 1814 } |
1824 | 1815 |
1825 void WebRtcVideoChannel2::WebRtcVideoSendStream::DestroyVideoEncoder( | 1816 void WebRtcVideoChannel2::WebRtcVideoSendStream::DestroyVideoEncoder( |
1826 AllocatedEncoder* encoder) { | 1817 AllocatedEncoder* encoder) { |
1827 RTC_DCHECK_RUN_ON(&thread_checker_); | |
1828 if (encoder->external) { | 1818 if (encoder->external) { |
1829 external_encoder_factory_->DestroyVideoEncoder(encoder->external_encoder); | 1819 external_encoder_factory_->DestroyVideoEncoder(encoder->external_encoder); |
1830 } | 1820 } |
1831 delete encoder->encoder; | 1821 delete encoder->encoder; |
1832 } | 1822 } |
1833 | 1823 |
1834 void WebRtcVideoChannel2::WebRtcVideoSendStream::SetCodec( | 1824 void WebRtcVideoChannel2::WebRtcVideoSendStream::SetCodec( |
1835 const VideoCodecSettings& codec_settings) { | 1825 const VideoCodecSettings& codec_settings) { |
1836 RTC_DCHECK_RUN_ON(&thread_checker_); | |
1837 parameters_.encoder_config = CreateVideoEncoderConfig(codec_settings.codec); | 1826 parameters_.encoder_config = CreateVideoEncoderConfig(codec_settings.codec); |
1838 RTC_DCHECK_GT(parameters_.encoder_config.number_of_streams, 0u); | 1827 RTC_DCHECK(!parameters_.encoder_config.streams.empty()); |
1839 | 1828 |
1840 AllocatedEncoder new_encoder = CreateVideoEncoder(codec_settings.codec); | 1829 AllocatedEncoder new_encoder = CreateVideoEncoder(codec_settings.codec); |
1841 parameters_.config.encoder_settings.encoder = new_encoder.encoder; | 1830 parameters_.config.encoder_settings.encoder = new_encoder.encoder; |
1842 parameters_.config.encoder_settings.full_overuse_time = new_encoder.external; | 1831 parameters_.config.encoder_settings.full_overuse_time = new_encoder.external; |
1843 parameters_.config.encoder_settings.payload_name = codec_settings.codec.name; | 1832 parameters_.config.encoder_settings.payload_name = codec_settings.codec.name; |
1844 parameters_.config.encoder_settings.payload_type = codec_settings.codec.id; | 1833 parameters_.config.encoder_settings.payload_type = codec_settings.codec.id; |
1845 if (new_encoder.external) { | 1834 if (new_encoder.external) { |
1846 webrtc::VideoCodecType type = CodecTypeFromName(codec_settings.codec.name); | 1835 webrtc::VideoCodecType type = CodecTypeFromName(codec_settings.codec.name); |
1847 parameters_.config.encoder_settings.internal_source = | 1836 parameters_.config.encoder_settings.internal_source = |
1848 external_encoder_factory_->EncoderTypeHasInternalSource(type); | 1837 external_encoder_factory_->EncoderTypeHasInternalSource(type); |
(...skipping 20 matching lines...) Expand all Loading... |
1869 LOG(LS_INFO) << "RecreateWebRtcStream (send) because of SetCodec."; | 1858 LOG(LS_INFO) << "RecreateWebRtcStream (send) because of SetCodec."; |
1870 RecreateWebRtcStream(); | 1859 RecreateWebRtcStream(); |
1871 if (allocated_encoder_.encoder != new_encoder.encoder) { | 1860 if (allocated_encoder_.encoder != new_encoder.encoder) { |
1872 DestroyVideoEncoder(&allocated_encoder_); | 1861 DestroyVideoEncoder(&allocated_encoder_); |
1873 allocated_encoder_ = new_encoder; | 1862 allocated_encoder_ = new_encoder; |
1874 } | 1863 } |
1875 } | 1864 } |
1876 | 1865 |
1877 void WebRtcVideoChannel2::WebRtcVideoSendStream::SetSendParameters( | 1866 void WebRtcVideoChannel2::WebRtcVideoSendStream::SetSendParameters( |
1878 const ChangedSendParameters& params) { | 1867 const ChangedSendParameters& params) { |
1879 RTC_DCHECK_RUN_ON(&thread_checker_); | 1868 { |
1880 // |recreate_stream| means construction-time parameters have changed and the | 1869 rtc::CritScope cs(&lock_); |
1881 // sending stream needs to be reset with the new config. | 1870 // |recreate_stream| means construction-time parameters have changed and the |
1882 bool recreate_stream = false; | 1871 // sending stream needs to be reset with the new config. |
1883 if (params.rtcp_mode) { | 1872 bool recreate_stream = false; |
1884 parameters_.config.rtp.rtcp_mode = *params.rtcp_mode; | 1873 if (params.rtcp_mode) { |
1885 recreate_stream = true; | 1874 parameters_.config.rtp.rtcp_mode = *params.rtcp_mode; |
1886 } | 1875 recreate_stream = true; |
1887 if (params.rtp_header_extensions) { | 1876 } |
1888 parameters_.config.rtp.extensions = *params.rtp_header_extensions; | 1877 if (params.rtp_header_extensions) { |
1889 recreate_stream = true; | 1878 parameters_.config.rtp.extensions = *params.rtp_header_extensions; |
1890 } | 1879 recreate_stream = true; |
1891 if (params.max_bandwidth_bps) { | 1880 } |
1892 parameters_.max_bitrate_bps = *params.max_bandwidth_bps; | 1881 if (params.max_bandwidth_bps) { |
1893 ReconfigureEncoder(); | 1882 parameters_.max_bitrate_bps = *params.max_bandwidth_bps; |
1894 } | 1883 pending_encoder_reconfiguration_ = true; |
1895 if (params.conference_mode) { | 1884 } |
1896 parameters_.conference_mode = *params.conference_mode; | 1885 if (params.conference_mode) { |
1897 } | 1886 parameters_.conference_mode = *params.conference_mode; |
| 1887 } |
1898 | 1888 |
1899 // Set codecs and options. | 1889 // Set codecs and options. |
1900 if (params.codec) { | 1890 if (params.codec) { |
1901 SetCodec(*params.codec); | 1891 SetCodec(*params.codec); |
1902 recreate_stream = false; // SetCodec has already recreated the stream. | 1892 recreate_stream = false; // SetCodec has already recreated the stream. |
1903 } else if (params.conference_mode && parameters_.codec_settings) { | 1893 } else if (params.conference_mode && parameters_.codec_settings) { |
1904 SetCodec(*parameters_.codec_settings); | 1894 SetCodec(*parameters_.codec_settings); |
1905 recreate_stream = false; // SetCodec has already recreated the stream. | 1895 recreate_stream = false; // SetCodec has already recreated the stream. |
1906 } | 1896 } |
1907 if (recreate_stream) { | 1897 if (recreate_stream) { |
1908 LOG(LS_INFO) << "RecreateWebRtcStream (send) because of SetSendParameters"; | 1898 LOG(LS_INFO) |
1909 RecreateWebRtcStream(); | 1899 << "RecreateWebRtcStream (send) because of SetSendParameters"; |
1910 } | 1900 RecreateWebRtcStream(); |
| 1901 } |
| 1902 } // release |lock_| |
1911 | 1903 |
1912 // |source_->AddOrUpdateSink| may not be called while holding |lock_| since | 1904 // |source_->AddOrUpdateSink| may not be called while holding |lock_| since |
1913 // that might cause a lock order inversion. | 1905 // that might cause a lock order inversion. |
1914 if (params.rtp_header_extensions) { | 1906 if (params.rtp_header_extensions) { |
1915 sink_wants_.rotation_applied = !ContainsHeaderExtension( | 1907 sink_wants_.rotation_applied = !ContainsHeaderExtension( |
1916 *params.rtp_header_extensions, webrtc::RtpExtension::kVideoRotationUri); | 1908 *params.rtp_header_extensions, webrtc::RtpExtension::kVideoRotationUri); |
1917 if (source_) { | 1909 if (source_) { |
1918 source_->AddOrUpdateSink(this, sink_wants_); | 1910 source_->AddOrUpdateSink(this, sink_wants_); |
1919 } | 1911 } |
1920 } | 1912 } |
1921 } | 1913 } |
1922 | 1914 |
1923 bool WebRtcVideoChannel2::WebRtcVideoSendStream::SetRtpParameters( | 1915 bool WebRtcVideoChannel2::WebRtcVideoSendStream::SetRtpParameters( |
1924 const webrtc::RtpParameters& new_parameters) { | 1916 const webrtc::RtpParameters& new_parameters) { |
1925 RTC_DCHECK_RUN_ON(&thread_checker_); | |
1926 if (!ValidateRtpParameters(new_parameters)) { | 1917 if (!ValidateRtpParameters(new_parameters)) { |
1927 return false; | 1918 return false; |
1928 } | 1919 } |
1929 | 1920 |
1930 bool reconfigure_encoder = new_parameters.encodings[0].max_bitrate_bps != | 1921 rtc::CritScope cs(&lock_); |
1931 rtp_parameters_.encodings[0].max_bitrate_bps; | 1922 if (new_parameters.encodings[0].max_bitrate_bps != |
| 1923 rtp_parameters_.encodings[0].max_bitrate_bps) { |
| 1924 pending_encoder_reconfiguration_ = true; |
| 1925 } |
1932 rtp_parameters_ = new_parameters; | 1926 rtp_parameters_ = new_parameters; |
1933 // Codecs are currently handled at the WebRtcVideoChannel2 level. | 1927 // Codecs are currently handled at the WebRtcVideoChannel2 level. |
1934 rtp_parameters_.codecs.clear(); | 1928 rtp_parameters_.codecs.clear(); |
1935 if (reconfigure_encoder) { | |
1936 ReconfigureEncoder(); | |
1937 } | |
1938 // Encoding may have been activated/deactivated. | 1929 // Encoding may have been activated/deactivated. |
1939 UpdateSendState(); | 1930 UpdateSendState(); |
1940 return true; | 1931 return true; |
1941 } | 1932 } |
1942 | 1933 |
1943 webrtc::RtpParameters | 1934 webrtc::RtpParameters |
1944 WebRtcVideoChannel2::WebRtcVideoSendStream::GetRtpParameters() const { | 1935 WebRtcVideoChannel2::WebRtcVideoSendStream::GetRtpParameters() const { |
1945 RTC_DCHECK_RUN_ON(&thread_checker_); | 1936 rtc::CritScope cs(&lock_); |
1946 return rtp_parameters_; | 1937 return rtp_parameters_; |
1947 } | 1938 } |
1948 | 1939 |
1949 bool WebRtcVideoChannel2::WebRtcVideoSendStream::ValidateRtpParameters( | 1940 bool WebRtcVideoChannel2::WebRtcVideoSendStream::ValidateRtpParameters( |
1950 const webrtc::RtpParameters& rtp_parameters) { | 1941 const webrtc::RtpParameters& rtp_parameters) { |
1951 if (rtp_parameters.encodings.size() != 1) { | 1942 if (rtp_parameters.encodings.size() != 1) { |
1952 LOG(LS_ERROR) | 1943 LOG(LS_ERROR) |
1953 << "Attempted to set RtpParameters without exactly one encoding"; | 1944 << "Attempted to set RtpParameters without exactly one encoding"; |
1954 return false; | 1945 return false; |
1955 } | 1946 } |
1956 return true; | 1947 return true; |
1957 } | 1948 } |
1958 | 1949 |
1959 void WebRtcVideoChannel2::WebRtcVideoSendStream::UpdateSendState() { | 1950 void WebRtcVideoChannel2::WebRtcVideoSendStream::UpdateSendState() { |
1960 RTC_DCHECK_RUN_ON(&thread_checker_); | |
1961 // TODO(deadbeef): Need to handle more than one encoding in the future. | 1951 // TODO(deadbeef): Need to handle more than one encoding in the future. |
1962 RTC_DCHECK(rtp_parameters_.encodings.size() == 1u); | 1952 RTC_DCHECK(rtp_parameters_.encodings.size() == 1u); |
1963 if (sending_ && rtp_parameters_.encodings[0].active) { | 1953 if (sending_ && rtp_parameters_.encodings[0].active) { |
1964 RTC_DCHECK(stream_ != nullptr); | 1954 RTC_DCHECK(stream_ != nullptr); |
1965 stream_->Start(); | 1955 stream_->Start(); |
1966 } else { | 1956 } else { |
1967 if (stream_ != nullptr) { | 1957 if (stream_ != nullptr) { |
1968 stream_->Stop(); | 1958 stream_->Stop(); |
1969 } | 1959 } |
1970 } | 1960 } |
1971 } | 1961 } |
1972 | 1962 |
1973 webrtc::VideoEncoderConfig | 1963 webrtc::VideoEncoderConfig |
1974 WebRtcVideoChannel2::WebRtcVideoSendStream::CreateVideoEncoderConfig( | 1964 WebRtcVideoChannel2::WebRtcVideoSendStream::CreateVideoEncoderConfig( |
1975 const VideoCodec& codec) const { | 1965 const VideoCodec& codec) const { |
1976 RTC_DCHECK_RUN_ON(&thread_checker_); | |
1977 webrtc::VideoEncoderConfig encoder_config; | 1966 webrtc::VideoEncoderConfig encoder_config; |
1978 bool is_screencast = parameters_.options.is_screencast.value_or(false); | 1967 bool is_screencast = parameters_.options.is_screencast.value_or(false); |
1979 if (is_screencast) { | 1968 if (is_screencast) { |
1980 encoder_config.min_transmit_bitrate_bps = | 1969 encoder_config.min_transmit_bitrate_bps = |
1981 1000 * parameters_.options.screencast_min_bitrate_kbps.value_or(0); | 1970 1000 * parameters_.options.screencast_min_bitrate_kbps.value_or(0); |
1982 encoder_config.content_type = | 1971 encoder_config.content_type = |
1983 webrtc::VideoEncoderConfig::ContentType::kScreen; | 1972 webrtc::VideoEncoderConfig::ContentType::kScreen; |
1984 } else { | 1973 } else { |
1985 encoder_config.min_transmit_bitrate_bps = 0; | 1974 encoder_config.min_transmit_bitrate_bps = 0; |
1986 encoder_config.content_type = | 1975 encoder_config.content_type = |
1987 webrtc::VideoEncoderConfig::ContentType::kRealtimeVideo; | 1976 webrtc::VideoEncoderConfig::ContentType::kRealtimeVideo; |
1988 } | 1977 } |
1989 | 1978 |
| 1979 // Restrict dimensions according to codec max. |
| 1980 int width = last_frame_info_.width; |
| 1981 int height = last_frame_info_.height; |
| 1982 if (!is_screencast) { |
| 1983 if (codec.width < width) |
| 1984 width = codec.width; |
| 1985 if (codec.height < height) |
| 1986 height = codec.height; |
| 1987 } |
| 1988 |
| 1989 VideoCodec clamped_codec = codec; |
| 1990 clamped_codec.width = width; |
| 1991 clamped_codec.height = height; |
| 1992 |
1990 // By default, the stream count for the codec configuration should match the | 1993 // By default, the stream count for the codec configuration should match the |
1991 // number of negotiated ssrcs. But if the codec is blacklisted for simulcast | 1994 // number of negotiated ssrcs. But if the codec is blacklisted for simulcast |
1992 // or a screencast, only configure a single stream. | 1995 // or a screencast, only configure a single stream. |
1993 encoder_config.number_of_streams = parameters_.config.rtp.ssrcs.size(); | 1996 size_t stream_count = parameters_.config.rtp.ssrcs.size(); |
1994 if (IsCodecBlacklistedForSimulcast(codec.name) || is_screencast) { | 1997 if (IsCodecBlacklistedForSimulcast(codec.name) || is_screencast) { |
1995 encoder_config.number_of_streams = 1; | 1998 stream_count = 1; |
1996 } | 1999 } |
1997 | 2000 |
1998 int stream_max_bitrate = | 2001 int stream_max_bitrate = |
1999 MinPositive(rtp_parameters_.encodings[0].max_bitrate_bps, | 2002 MinPositive(rtp_parameters_.encodings[0].max_bitrate_bps, |
2000 parameters_.max_bitrate_bps); | 2003 parameters_.max_bitrate_bps); |
| 2004 encoder_config.streams = CreateVideoStreams( |
| 2005 clamped_codec, parameters_.options, stream_max_bitrate, stream_count); |
| 2006 encoder_config.expect_encode_from_texture = last_frame_info_.is_texture; |
2001 | 2007 |
2002 int codec_max_bitrate_kbps; | 2008 // Conference mode screencast uses 2 temporal layers split at 100kbit. |
2003 if (codec.GetParam(kCodecParamMaxBitrate, &codec_max_bitrate_kbps)) { | 2009 if (parameters_.conference_mode && is_screencast && |
2004 stream_max_bitrate = codec_max_bitrate_kbps * 1000; | 2010 encoder_config.streams.size() == 1) { |
| 2011 ScreenshareLayerConfig config = ScreenshareLayerConfig::GetDefault(); |
| 2012 |
| 2013 // For screenshare in conference mode, tl0 and tl1 bitrates are piggybacked |
| 2014 // on the VideoCodec struct as target and max bitrates, respectively. |
| 2015 // See eg. webrtc::VP8EncoderImpl::SetRates(). |
| 2016 encoder_config.streams[0].target_bitrate_bps = |
| 2017 config.tl0_bitrate_kbps * 1000; |
| 2018 encoder_config.streams[0].max_bitrate_bps = config.tl1_bitrate_kbps * 1000; |
| 2019 encoder_config.streams[0].temporal_layer_thresholds_bps.clear(); |
| 2020 encoder_config.streams[0].temporal_layer_thresholds_bps.push_back( |
| 2021 config.tl0_bitrate_kbps * 1000); |
2005 } | 2022 } |
2006 encoder_config.max_bitrate_bps = stream_max_bitrate; | 2023 if (CodecNamesEq(codec.name, kVp9CodecName) && !is_screencast && |
2007 | 2024 encoder_config.streams.size() == 1) { |
2008 int max_qp = kDefaultQpMax; | 2025 encoder_config.streams[0].temporal_layer_thresholds_bps.resize( |
2009 codec.GetParam(kCodecParamMaxQuantization, &max_qp); | 2026 GetDefaultVp9TemporalLayers() - 1); |
2010 int max_framerate = | 2027 } |
2011 codec.framerate != 0 ? codec.framerate : kDefaultVideoMaxFramerate; | |
2012 | |
2013 encoder_config.video_stream_factory = | |
2014 new rtc::RefCountedObject<EncoderStreamFactory>( | |
2015 codec.name, max_qp, max_framerate, is_screencast, | |
2016 parameters_.conference_mode); | |
2017 return encoder_config; | 2028 return encoder_config; |
2018 } | 2029 } |
2019 | 2030 |
2020 void WebRtcVideoChannel2::WebRtcVideoSendStream::ReconfigureEncoder() { | 2031 void WebRtcVideoChannel2::WebRtcVideoSendStream::ReconfigureEncoder() { |
2021 RTC_DCHECK_RUN_ON(&thread_checker_); | 2032 RTC_DCHECK(!parameters_.encoder_config.streams.empty()); |
2022 RTC_DCHECK_GT(parameters_.encoder_config.number_of_streams, 0u); | |
2023 | 2033 |
2024 RTC_CHECK(parameters_.codec_settings); | 2034 RTC_CHECK(parameters_.codec_settings); |
2025 VideoCodecSettings codec_settings = *parameters_.codec_settings; | 2035 VideoCodecSettings codec_settings = *parameters_.codec_settings; |
2026 | 2036 |
2027 webrtc::VideoEncoderConfig encoder_config = | 2037 webrtc::VideoEncoderConfig encoder_config = |
2028 CreateVideoEncoderConfig(codec_settings.codec); | 2038 CreateVideoEncoderConfig(codec_settings.codec); |
2029 | 2039 |
2030 encoder_config.encoder_specific_settings = ConfigureVideoEncoderSettings( | 2040 encoder_config.encoder_specific_settings = ConfigureVideoEncoderSettings( |
2031 codec_settings.codec); | 2041 codec_settings.codec); |
2032 | 2042 |
2033 stream_->ReconfigureVideoEncoder(encoder_config.Copy()); | 2043 stream_->ReconfigureVideoEncoder(encoder_config.Copy()); |
2034 | 2044 |
2035 encoder_config.encoder_specific_settings = NULL; | 2045 encoder_config.encoder_specific_settings = NULL; |
2036 | 2046 |
2037 parameters_.encoder_config = std::move(encoder_config); | 2047 parameters_.encoder_config = std::move(encoder_config); |
2038 } | 2048 } |
2039 | 2049 |
2040 void WebRtcVideoChannel2::WebRtcVideoSendStream::SetSend(bool send) { | 2050 void WebRtcVideoChannel2::WebRtcVideoSendStream::SetSend(bool send) { |
2041 RTC_DCHECK_RUN_ON(&thread_checker_); | 2051 rtc::CritScope cs(&lock_); |
2042 sending_ = send; | 2052 sending_ = send; |
2043 UpdateSendState(); | 2053 UpdateSendState(); |
2044 } | 2054 } |
2045 | 2055 |
2046 void WebRtcVideoChannel2::WebRtcVideoSendStream::AddOrUpdateSink( | 2056 void WebRtcVideoChannel2::WebRtcVideoSendStream::AddOrUpdateSink( |
2047 VideoSinkInterface<webrtc::VideoFrame>* sink, | 2057 VideoSinkInterface<webrtc::VideoFrame>* sink, |
2048 const rtc::VideoSinkWants& wants) { | 2058 const rtc::VideoSinkWants& wants) { |
2049 // TODO(perkj): Actually consider the encoder |wants| and remove | 2059 // TODO(perkj): Actually consider the encoder |wants| and remove |
2050 // WebRtcVideoSendStream::OnLoadUpdate(Load load). | 2060 // WebRtcVideoSendStream::OnLoadUpdate(Load load). |
2051 rtc::CritScope cs(&lock_); | 2061 rtc::CritScope cs(&lock_); |
2052 RTC_DCHECK(!encoder_sink_ || encoder_sink_ == sink); | 2062 RTC_DCHECK(!encoder_sink_ || encoder_sink_ == sink); |
2053 encoder_sink_ = sink; | 2063 encoder_sink_ = sink; |
2054 } | 2064 } |
2055 | 2065 |
2056 void WebRtcVideoChannel2::WebRtcVideoSendStream::RemoveSink( | 2066 void WebRtcVideoChannel2::WebRtcVideoSendStream::RemoveSink( |
2057 VideoSinkInterface<webrtc::VideoFrame>* sink) { | 2067 VideoSinkInterface<webrtc::VideoFrame>* sink) { |
2058 rtc::CritScope cs(&lock_); | 2068 rtc::CritScope cs(&lock_); |
2059 RTC_DCHECK_EQ(encoder_sink_, sink); | 2069 RTC_DCHECK_EQ(encoder_sink_, sink); |
2060 encoder_sink_ = nullptr; | 2070 encoder_sink_ = nullptr; |
2061 } | 2071 } |
2062 | 2072 |
2063 void WebRtcVideoChannel2::WebRtcVideoSendStream::OnLoadUpdate(Load load) { | 2073 void WebRtcVideoChannel2::WebRtcVideoSendStream::OnLoadUpdate(Load load) { |
2064 if (worker_thread_ != rtc::Thread::Current()) { | 2074 if (worker_thread_ != rtc::Thread::Current()) { |
2065 invoker_.AsyncInvoke<void>( | 2075 invoker_.AsyncInvoke<void>( |
2066 RTC_FROM_HERE, worker_thread_, | 2076 RTC_FROM_HERE, worker_thread_, |
2067 rtc::Bind(&WebRtcVideoChannel2::WebRtcVideoSendStream::OnLoadUpdate, | 2077 rtc::Bind(&WebRtcVideoChannel2::WebRtcVideoSendStream::OnLoadUpdate, |
2068 this, load)); | 2078 this, load)); |
2069 return; | 2079 return; |
2070 } | 2080 } |
2071 RTC_DCHECK_RUN_ON(&thread_checker_); | 2081 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
2072 if (!source_) { | 2082 if (!source_) { |
2073 return; | 2083 return; |
2074 } | 2084 } |
| 2085 { |
| 2086 rtc::CritScope cs(&lock_); |
| 2087 LOG(LS_INFO) << "OnLoadUpdate " << load << ", is_screencast: " |
| 2088 << (parameters_.options.is_screencast |
| 2089 ? (*parameters_.options.is_screencast ? "true" |
| 2090 : "false") |
| 2091 : "unset"); |
| 2092 // Do not adapt resolution for screen content as this will likely result in |
| 2093 // blurry and unreadable text. |
| 2094 if (parameters_.options.is_screencast.value_or(false)) |
| 2095 return; |
2075 | 2096 |
2076 LOG(LS_INFO) << "OnLoadUpdate " << load << ", is_screencast: " | 2097 rtc::Optional<int> max_pixel_count; |
2077 << (parameters_.options.is_screencast | 2098 rtc::Optional<int> max_pixel_count_step_up; |
2078 ? (*parameters_.options.is_screencast ? "true" : "false") | 2099 if (load == kOveruse) { |
2079 : "unset"); | 2100 if (cpu_restricted_counter_ >= kMaxCpuDowngrades) { |
2080 // Do not adapt resolution for screen content as this will likely result in | 2101 return; |
2081 // blurry and unreadable text. | 2102 } |
2082 if (parameters_.options.is_screencast.value_or(false)) | 2103 // The input video frame size will have a resolution with less than or |
2083 return; | 2104 // equal to |max_pixel_count| depending on how the source can scale the |
2084 | 2105 // input frame size. |
2085 rtc::Optional<int> max_pixel_count; | 2106 max_pixel_count = rtc::Optional<int>( |
2086 rtc::Optional<int> max_pixel_count_step_up; | 2107 (last_frame_info_.height * last_frame_info_.width * 3) / 5); |
2087 if (load == kOveruse) { | 2108 // Increase |number_of_cpu_adapt_changes_| if |
2088 rtc::CritScope cs(&lock_); | 2109 // sink_wants_.max_pixel_count will be changed since |
2089 if (cpu_restricted_counter_ >= kMaxCpuDowngrades) { | 2110 // last time |source_->AddOrUpdateSink| was called. That is, this will |
2090 return; | 2111 // result in a new request for the source to change resolution. |
| 2112 if (!sink_wants_.max_pixel_count || |
| 2113 *sink_wants_.max_pixel_count > *max_pixel_count) { |
| 2114 ++number_of_cpu_adapt_changes_; |
| 2115 ++cpu_restricted_counter_; |
| 2116 } |
| 2117 } else { |
| 2118 RTC_DCHECK(load == kUnderuse); |
| 2119 // The input video frame size will have a resolution with "one step up" |
| 2120 // pixels than |max_pixel_count_step_up| where "one step up" depends on |
| 2121 // how the source can scale the input frame size. |
| 2122 max_pixel_count_step_up = |
| 2123 rtc::Optional<int>(last_frame_info_.height * last_frame_info_.width); |
| 2124 // Increase |number_of_cpu_adapt_changes_| if |
| 2125 // sink_wants_.max_pixel_count_step_up will be changed since |
| 2126 // last time |source_->AddOrUpdateSink| was called. That is, this will |
| 2127 // result in a new request for the source to change resolution. |
| 2128 if (sink_wants_.max_pixel_count || |
| 2129 (sink_wants_.max_pixel_count_step_up && |
| 2130 *sink_wants_.max_pixel_count_step_up < *max_pixel_count_step_up)) { |
| 2131 ++number_of_cpu_adapt_changes_; |
| 2132 --cpu_restricted_counter_; |
| 2133 } |
2091 } | 2134 } |
2092 // The input video frame size will have a resolution with less than or | 2135 sink_wants_.max_pixel_count = max_pixel_count; |
2093 // equal to |max_pixel_count| depending on how the source can scale the | 2136 sink_wants_.max_pixel_count_step_up = max_pixel_count_step_up; |
2094 // input frame size. | |
2095 max_pixel_count = rtc::Optional<int>( | |
2096 (last_frame_info_.height * last_frame_info_.width * 3) / 5); | |
2097 // Increase |number_of_cpu_adapt_changes_| if | |
2098 // sink_wants_.max_pixel_count will be changed since | |
2099 // last time |source_->AddOrUpdateSink| was called. That is, this will | |
2100 // result in a new request for the source to change resolution. | |
2101 if (!sink_wants_.max_pixel_count || | |
2102 *sink_wants_.max_pixel_count > *max_pixel_count) { | |
2103 ++number_of_cpu_adapt_changes_; | |
2104 ++cpu_restricted_counter_; | |
2105 } | |
2106 } else { | |
2107 RTC_DCHECK(load == kUnderuse); | |
2108 rtc::CritScope cs(&lock_); | |
2109 // The input video frame size will have a resolution with "one step up" | |
2110 // pixels than |max_pixel_count_step_up| where "one step up" depends on | |
2111 // how the source can scale the input frame size. | |
2112 max_pixel_count_step_up = | |
2113 rtc::Optional<int>(last_frame_info_.height * last_frame_info_.width); | |
2114 // Increase |number_of_cpu_adapt_changes_| if | |
2115 // sink_wants_.max_pixel_count_step_up will be changed since | |
2116 // last time |source_->AddOrUpdateSink| was called. That is, this will | |
2117 // result in a new request for the source to change resolution. | |
2118 if (sink_wants_.max_pixel_count || | |
2119 (sink_wants_.max_pixel_count_step_up && | |
2120 *sink_wants_.max_pixel_count_step_up < *max_pixel_count_step_up)) { | |
2121 ++number_of_cpu_adapt_changes_; | |
2122 --cpu_restricted_counter_; | |
2123 } | |
2124 } | 2137 } |
2125 sink_wants_.max_pixel_count = max_pixel_count; | |
2126 sink_wants_.max_pixel_count_step_up = max_pixel_count_step_up; | |
2127 // |source_->AddOrUpdateSink| may not be called while holding |lock_| since | 2138 // |source_->AddOrUpdateSink| may not be called while holding |lock_| since |
2128 // that might cause a lock order inversion. | 2139 // that might cause a lock order inversion. |
2129 source_->AddOrUpdateSink(this, sink_wants_); | 2140 source_->AddOrUpdateSink(this, sink_wants_); |
2130 } | 2141 } |
2131 | 2142 |
2132 VideoSenderInfo WebRtcVideoChannel2::WebRtcVideoSendStream::GetVideoSenderInfo( | 2143 VideoSenderInfo WebRtcVideoChannel2::WebRtcVideoSendStream::GetVideoSenderInfo( |
2133 bool log_stats) { | 2144 bool log_stats) { |
2134 VideoSenderInfo info; | 2145 VideoSenderInfo info; |
2135 RTC_DCHECK_RUN_ON(&thread_checker_); | 2146 webrtc::VideoSendStream::Stats stats; |
2136 for (uint32_t ssrc : parameters_.config.rtp.ssrcs) | 2147 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
2137 info.add_ssrc(ssrc); | 2148 { |
| 2149 rtc::CritScope cs(&lock_); |
| 2150 for (uint32_t ssrc : parameters_.config.rtp.ssrcs) |
| 2151 info.add_ssrc(ssrc); |
2138 | 2152 |
2139 if (parameters_.codec_settings) | 2153 if (parameters_.codec_settings) |
2140 info.codec_name = parameters_.codec_settings->codec.name; | 2154 info.codec_name = parameters_.codec_settings->codec.name; |
2141 | 2155 |
2142 if (stream_ == NULL) | 2156 if (stream_ == NULL) |
2143 return info; | 2157 return info; |
2144 | 2158 |
2145 webrtc::VideoSendStream::Stats stats = stream_->GetStats(); | 2159 stats = stream_->GetStats(); |
| 2160 } |
2146 | 2161 |
2147 if (log_stats) | 2162 if (log_stats) |
2148 LOG(LS_INFO) << stats.ToString(rtc::TimeMillis()); | 2163 LOG(LS_INFO) << stats.ToString(rtc::TimeMillis()); |
2149 | 2164 |
2150 info.adapt_changes = number_of_cpu_adapt_changes_; | 2165 info.adapt_changes = number_of_cpu_adapt_changes_; |
2151 info.adapt_reason = | 2166 info.adapt_reason = |
2152 cpu_restricted_counter_ <= 0 ? ADAPTREASON_NONE : ADAPTREASON_CPU; | 2167 cpu_restricted_counter_ <= 0 ? ADAPTREASON_NONE : ADAPTREASON_CPU; |
2153 | 2168 |
2154 // Get bandwidth limitation info from stream_->GetStats(). | 2169 // Get bandwidth limitation info from stream_->GetStats(). |
2155 // Input resolution (output from video_adapter) can be further scaled down or | 2170 // Input resolution (output from video_adapter) can be further scaled down or |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2196 info.fraction_lost = | 2211 info.fraction_lost = |
2197 static_cast<float>(first_stream_stats.rtcp_stats.fraction_lost) / | 2212 static_cast<float>(first_stream_stats.rtcp_stats.fraction_lost) / |
2198 (1 << 8); | 2213 (1 << 8); |
2199 } | 2214 } |
2200 | 2215 |
2201 return info; | 2216 return info; |
2202 } | 2217 } |
2203 | 2218 |
2204 void WebRtcVideoChannel2::WebRtcVideoSendStream::FillBandwidthEstimationInfo( | 2219 void WebRtcVideoChannel2::WebRtcVideoSendStream::FillBandwidthEstimationInfo( |
2205 BandwidthEstimationInfo* bwe_info) { | 2220 BandwidthEstimationInfo* bwe_info) { |
2206 RTC_DCHECK_RUN_ON(&thread_checker_); | 2221 rtc::CritScope cs(&lock_); |
2207 if (stream_ == NULL) { | 2222 if (stream_ == NULL) { |
2208 return; | 2223 return; |
2209 } | 2224 } |
2210 webrtc::VideoSendStream::Stats stats = stream_->GetStats(); | 2225 webrtc::VideoSendStream::Stats stats = stream_->GetStats(); |
2211 for (std::map<uint32_t, webrtc::VideoSendStream::StreamStats>::iterator it = | 2226 for (std::map<uint32_t, webrtc::VideoSendStream::StreamStats>::iterator it = |
2212 stats.substreams.begin(); | 2227 stats.substreams.begin(); |
2213 it != stats.substreams.end(); ++it) { | 2228 it != stats.substreams.end(); ++it) { |
2214 bwe_info->transmit_bitrate += it->second.total_bitrate_bps; | 2229 bwe_info->transmit_bitrate += it->second.total_bitrate_bps; |
2215 bwe_info->retransmit_bitrate += it->second.retransmit_bitrate_bps; | 2230 bwe_info->retransmit_bitrate += it->second.retransmit_bitrate_bps; |
2216 } | 2231 } |
2217 bwe_info->target_enc_bitrate += stats.target_media_bitrate_bps; | 2232 bwe_info->target_enc_bitrate += stats.target_media_bitrate_bps; |
2218 bwe_info->actual_enc_bitrate += stats.media_bitrate_bps; | 2233 bwe_info->actual_enc_bitrate += stats.media_bitrate_bps; |
2219 } | 2234 } |
2220 | 2235 |
2221 void WebRtcVideoChannel2::WebRtcVideoSendStream::RecreateWebRtcStream() { | 2236 void WebRtcVideoChannel2::WebRtcVideoSendStream::RecreateWebRtcStream() { |
2222 RTC_DCHECK_RUN_ON(&thread_checker_); | |
2223 if (stream_ != NULL) { | 2237 if (stream_ != NULL) { |
2224 call_->DestroyVideoSendStream(stream_); | 2238 call_->DestroyVideoSendStream(stream_); |
2225 } | 2239 } |
2226 | 2240 |
2227 RTC_CHECK(parameters_.codec_settings); | 2241 RTC_CHECK(parameters_.codec_settings); |
2228 RTC_DCHECK_EQ((parameters_.encoder_config.content_type == | 2242 RTC_DCHECK_EQ((parameters_.encoder_config.content_type == |
2229 webrtc::VideoEncoderConfig::ContentType::kScreen), | 2243 webrtc::VideoEncoderConfig::ContentType::kScreen), |
2230 parameters_.options.is_screencast.value_or(false)) | 2244 parameters_.options.is_screencast.value_or(false)) |
2231 << "encoder content type inconsistent with screencast option"; | 2245 << "encoder content type inconsistent with screencast option"; |
2232 parameters_.encoder_config.encoder_specific_settings = | 2246 parameters_.encoder_config.encoder_specific_settings = |
2233 ConfigureVideoEncoderSettings(parameters_.codec_settings->codec); | 2247 ConfigureVideoEncoderSettings(parameters_.codec_settings->codec); |
2234 | 2248 |
2235 webrtc::VideoSendStream::Config config = parameters_.config.Copy(); | 2249 webrtc::VideoSendStream::Config config = parameters_.config.Copy(); |
2236 if (!config.rtp.rtx.ssrcs.empty() && config.rtp.rtx.payload_type == -1) { | 2250 if (!config.rtp.rtx.ssrcs.empty() && config.rtp.rtx.payload_type == -1) { |
2237 LOG(LS_WARNING) << "RTX SSRCs configured but there's no configured RTX " | 2251 LOG(LS_WARNING) << "RTX SSRCs configured but there's no configured RTX " |
2238 "payload type the set codec. Ignoring RTX."; | 2252 "payload type the set codec. Ignoring RTX."; |
2239 config.rtp.rtx.ssrcs.clear(); | 2253 config.rtp.rtx.ssrcs.clear(); |
2240 } | 2254 } |
2241 stream_ = call_->CreateVideoSendStream(std::move(config), | 2255 stream_ = call_->CreateVideoSendStream(std::move(config), |
2242 parameters_.encoder_config.Copy()); | 2256 parameters_.encoder_config.Copy()); |
2243 stream_->SetSource(this); | 2257 stream_->SetSource(this); |
2244 | 2258 |
2245 parameters_.encoder_config.encoder_specific_settings = NULL; | 2259 parameters_.encoder_config.encoder_specific_settings = NULL; |
| 2260 pending_encoder_reconfiguration_ = false; |
2246 | 2261 |
2247 // Call stream_->Start() if necessary conditions are met. | 2262 // Call stream_->Start() if necessary conditions are met. |
2248 UpdateSendState(); | 2263 UpdateSendState(); |
2249 } | 2264 } |
2250 | 2265 |
2251 WebRtcVideoChannel2::WebRtcVideoReceiveStream::WebRtcVideoReceiveStream( | 2266 WebRtcVideoChannel2::WebRtcVideoReceiveStream::WebRtcVideoReceiveStream( |
2252 webrtc::Call* call, | 2267 webrtc::Call* call, |
2253 const StreamParams& sp, | 2268 const StreamParams& sp, |
2254 webrtc::VideoReceiveStream::Config config, | 2269 webrtc::VideoReceiveStream::Config config, |
2255 WebRtcVideoDecoderFactory* external_decoder_factory, | 2270 WebRtcVideoDecoderFactory* external_decoder_factory, |
(...skipping 435 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2691 rtx_mapping[video_codecs[i].codec.id] != | 2706 rtx_mapping[video_codecs[i].codec.id] != |
2692 fec_settings.red_payload_type) { | 2707 fec_settings.red_payload_type) { |
2693 video_codecs[i].rtx_payload_type = rtx_mapping[video_codecs[i].codec.id]; | 2708 video_codecs[i].rtx_payload_type = rtx_mapping[video_codecs[i].codec.id]; |
2694 } | 2709 } |
2695 } | 2710 } |
2696 | 2711 |
2697 return video_codecs; | 2712 return video_codecs; |
2698 } | 2713 } |
2699 | 2714 |
2700 } // namespace cricket | 2715 } // namespace cricket |
OLD | NEW |