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

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

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

Powered by Google App Engine
This is Rietveld 408576698