Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(47)

Side by Side Diff: webrtc/media/engine/webrtcvideoengine2.cc

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

Powered by Google App Engine
This is Rietveld 408576698