pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 10 | #include "video/video_send_stream.h" |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 11 | |
pbos@webrtc.org | dde16f1 | 2014-08-05 23:35:43 +0000 | [diff] [blame] | 12 | #include <algorithm> |
hbos | 706a45e | 2016-11-30 01:53:10 -0800 | [diff] [blame] | 13 | #include <cmath> |
Ying Wang | 3b790f3 | 2018-01-19 17:58:57 +0100 | [diff] [blame] | 14 | #include <set> |
pbos@webrtc.org | 1e92b0a | 2014-05-15 09:35:06 +0000 | [diff] [blame] | 15 | #include <sstream> |
henrik.lundin@webrtc.org | ba975e2 | 2013-10-23 11:04:57 +0000 | [diff] [blame] | 16 | #include <string> |
perkj | 71ee44c | 2016-06-15 00:47:53 -0700 | [diff] [blame] | 17 | #include <utility> |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 18 | #include <vector> |
| 19 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 20 | #include "call/rtp_transport_controller_send_interface.h" |
Sebastian Jansson | cabe383 | 2018-01-12 10:54:18 +0100 | [diff] [blame] | 21 | #include "call/video_send_stream.h" |
Mirko Bonadei | 7120742 | 2017-09-15 13:58:09 +0200 | [diff] [blame] | 22 | #include "common_types.h" // NOLINT(build/include) |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 23 | #include "common_video/include/video_bitrate_allocator.h" |
| 24 | #include "modules/bitrate_controller/include/bitrate_controller.h" |
| 25 | #include "modules/congestion_controller/include/send_side_congestion_controller.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 26 | #include "modules/pacing/packet_router.h" |
| 27 | #include "modules/rtp_rtcp/include/rtp_rtcp.h" |
| 28 | #include "modules/rtp_rtcp/source/rtp_sender.h" |
| 29 | #include "modules/utility/include/process_thread.h" |
Ying Wang | 3b790f3 | 2018-01-19 17:58:57 +0100 | [diff] [blame] | 30 | #include "modules/video_coding/fec_controller_default.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 31 | #include "modules/video_coding/utility/ivf_file_writer.h" |
| 32 | #include "rtc_base/checks.h" |
Sebastian Jansson | cabe383 | 2018-01-12 10:54:18 +0100 | [diff] [blame] | 33 | #include "rtc_base/experiments/alr_experiment.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 34 | #include "rtc_base/file.h" |
| 35 | #include "rtc_base/location.h" |
| 36 | #include "rtc_base/logging.h" |
| 37 | #include "rtc_base/trace_event.h" |
| 38 | #include "rtc_base/weak_ptr.h" |
| 39 | #include "system_wrappers/include/field_trial.h" |
| 40 | #include "video/call_stats.h" |
| 41 | #include "video/payload_router.h" |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 42 | |
| 43 | namespace webrtc { |
mflodman | 949c2f0 | 2015-10-16 02:31:11 -0700 | [diff] [blame] | 44 | |
Per | 83d0910 | 2016-04-15 14:59:13 +0200 | [diff] [blame] | 45 | static const int kMinSendSidePacketHistorySize = 600; |
Ying Wang | 3b790f3 | 2018-01-19 17:58:57 +0100 | [diff] [blame] | 46 | static const int kSendSideSeqNumSetMaxSize = 15000; |
| 47 | |
Per | 83d0910 | 2016-04-15 14:59:13 +0200 | [diff] [blame] | 48 | namespace { |
| 49 | |
nisse | 284542b | 2017-01-10 08:58:32 -0800 | [diff] [blame] | 50 | // We don't do MTU discovery, so assume that we have the standard ethernet MTU. |
| 51 | const size_t kPathMTU = 1500; |
| 52 | |
Per | 83d0910 | 2016-04-15 14:59:13 +0200 | [diff] [blame] | 53 | std::vector<RtpRtcp*> CreateRtpRtcpModules( |
| 54 | Transport* outgoing_transport, |
| 55 | RtcpIntraFrameObserver* intra_frame_callback, |
| 56 | RtcpBandwidthObserver* bandwidth_callback, |
nisse | b8f9a32 | 2017-03-27 05:36:15 -0700 | [diff] [blame] | 57 | RtpTransportControllerSendInterface* transport, |
Per | 83d0910 | 2016-04-15 14:59:13 +0200 | [diff] [blame] | 58 | RtcpRttStats* rtt_stats, |
brandtr | e950cad | 2016-11-15 05:25:41 -0800 | [diff] [blame] | 59 | FlexfecSender* flexfec_sender, |
Per | 83d0910 | 2016-04-15 14:59:13 +0200 | [diff] [blame] | 60 | SendStatisticsProxy* stats_proxy, |
asapersson | 35151f3 | 2016-05-02 23:44:01 -0700 | [diff] [blame] | 61 | SendDelayStats* send_delay_stats, |
terelius | adafe0b | 2016-05-26 01:58:40 -0700 | [diff] [blame] | 62 | RtcEventLog* event_log, |
sprang | cd349d9 | 2016-07-13 09:11:28 -0700 | [diff] [blame] | 63 | RateLimiter* retransmission_rate_limiter, |
michaelt | a332877 | 2016-11-29 09:25:03 -0800 | [diff] [blame] | 64 | OverheadObserver* overhead_observer, |
sprang | 168794c | 2017-07-06 04:38:06 -0700 | [diff] [blame] | 65 | size_t num_modules, |
Jiawei Ou | 3587b83 | 2018-01-31 22:08:26 -0800 | [diff] [blame] | 66 | RtpKeepAliveConfig keepalive_config, |
| 67 | RtcpIntervalConfig rtc_interval_config) { |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 68 | RTC_DCHECK_GT(num_modules, 0); |
Per | 83d0910 | 2016-04-15 14:59:13 +0200 | [diff] [blame] | 69 | RtpRtcp::Configuration configuration; |
Per | 83d0910 | 2016-04-15 14:59:13 +0200 | [diff] [blame] | 70 | configuration.audio = false; |
| 71 | configuration.receiver_only = false; |
brandtr | e950cad | 2016-11-15 05:25:41 -0800 | [diff] [blame] | 72 | configuration.flexfec_sender = flexfec_sender; |
Per | 83d0910 | 2016-04-15 14:59:13 +0200 | [diff] [blame] | 73 | configuration.outgoing_transport = outgoing_transport; |
| 74 | configuration.intra_frame_callback = intra_frame_callback; |
terelius | adafe0b | 2016-05-26 01:58:40 -0700 | [diff] [blame] | 75 | configuration.bandwidth_callback = bandwidth_callback; |
nisse | b8f9a32 | 2017-03-27 05:36:15 -0700 | [diff] [blame] | 76 | configuration.transport_feedback_callback = |
| 77 | transport->transport_feedback_observer(); |
Per | 83d0910 | 2016-04-15 14:59:13 +0200 | [diff] [blame] | 78 | configuration.rtt_stats = rtt_stats; |
| 79 | configuration.rtcp_packet_type_counter_observer = stats_proxy; |
nisse | b8f9a32 | 2017-03-27 05:36:15 -0700 | [diff] [blame] | 80 | configuration.paced_sender = transport->packet_sender(); |
Per | 83d0910 | 2016-04-15 14:59:13 +0200 | [diff] [blame] | 81 | configuration.transport_sequence_number_allocator = |
nisse | b8f9a32 | 2017-03-27 05:36:15 -0700 | [diff] [blame] | 82 | transport->packet_router(); |
Per | 83d0910 | 2016-04-15 14:59:13 +0200 | [diff] [blame] | 83 | configuration.send_bitrate_observer = stats_proxy; |
| 84 | configuration.send_frame_count_observer = stats_proxy; |
| 85 | configuration.send_side_delay_observer = stats_proxy; |
asapersson | 35151f3 | 2016-05-02 23:44:01 -0700 | [diff] [blame] | 86 | configuration.send_packet_observer = send_delay_stats; |
terelius | adafe0b | 2016-05-26 01:58:40 -0700 | [diff] [blame] | 87 | configuration.event_log = event_log; |
sprang | cd349d9 | 2016-07-13 09:11:28 -0700 | [diff] [blame] | 88 | configuration.retransmission_rate_limiter = retransmission_rate_limiter; |
michaelt | a332877 | 2016-11-29 09:25:03 -0800 | [diff] [blame] | 89 | configuration.overhead_observer = overhead_observer; |
sprang | 168794c | 2017-07-06 04:38:06 -0700 | [diff] [blame] | 90 | configuration.keepalive_config = keepalive_config; |
Per | 83d0910 | 2016-04-15 14:59:13 +0200 | [diff] [blame] | 91 | std::vector<RtpRtcp*> modules; |
| 92 | for (size_t i = 0; i < num_modules; ++i) { |
| 93 | RtpRtcp* rtp_rtcp = RtpRtcp::CreateRtpRtcp(configuration); |
| 94 | rtp_rtcp->SetSendingStatus(false); |
| 95 | rtp_rtcp->SetSendingMediaStatus(false); |
| 96 | rtp_rtcp->SetRTCPStatus(RtcpMode::kCompound); |
| 97 | modules.push_back(rtp_rtcp); |
| 98 | } |
| 99 | return modules; |
| 100 | } |
| 101 | |
brandtr | e950cad | 2016-11-15 05:25:41 -0800 | [diff] [blame] | 102 | // TODO(brandtr): Update this function when we support multistream protection. |
| 103 | std::unique_ptr<FlexfecSender> MaybeCreateFlexfecSender( |
brandtr | 48d21a2 | 2017-05-30 02:32:12 -0700 | [diff] [blame] | 104 | const VideoSendStream::Config& config, |
| 105 | const std::map<uint32_t, RtpState>& suspended_ssrcs) { |
brandtr | 3d200bd | 2017-01-16 06:59:19 -0800 | [diff] [blame] | 106 | if (config.rtp.flexfec.payload_type < 0) { |
brandtr | e950cad | 2016-11-15 05:25:41 -0800 | [diff] [blame] | 107 | return nullptr; |
| 108 | } |
brandtr | 3d200bd | 2017-01-16 06:59:19 -0800 | [diff] [blame] | 109 | RTC_DCHECK_GE(config.rtp.flexfec.payload_type, 0); |
| 110 | RTC_DCHECK_LE(config.rtp.flexfec.payload_type, 127); |
| 111 | if (config.rtp.flexfec.ssrc == 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 112 | RTC_LOG(LS_WARNING) << "FlexFEC is enabled, but no FlexFEC SSRC given. " |
| 113 | "Therefore disabling FlexFEC."; |
brandtr | e950cad | 2016-11-15 05:25:41 -0800 | [diff] [blame] | 114 | return nullptr; |
| 115 | } |
| 116 | if (config.rtp.flexfec.protected_media_ssrcs.empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 117 | RTC_LOG(LS_WARNING) |
| 118 | << "FlexFEC is enabled, but no protected media SSRC given. " |
| 119 | "Therefore disabling FlexFEC."; |
brandtr | e950cad | 2016-11-15 05:25:41 -0800 | [diff] [blame] | 120 | return nullptr; |
| 121 | } |
| 122 | |
| 123 | if (config.rtp.ssrcs.size() > 1) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 124 | RTC_LOG(LS_WARNING) |
| 125 | << "Both FlexFEC and simulcast are enabled. This " |
| 126 | "combination is however not supported by our current " |
| 127 | "FlexFEC implementation. Therefore disabling FlexFEC."; |
brandtr | e950cad | 2016-11-15 05:25:41 -0800 | [diff] [blame] | 128 | return nullptr; |
| 129 | } |
| 130 | |
| 131 | if (config.rtp.flexfec.protected_media_ssrcs.size() > 1) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 132 | RTC_LOG(LS_WARNING) |
brandtr | e950cad | 2016-11-15 05:25:41 -0800 | [diff] [blame] | 133 | << "The supplied FlexfecConfig contained multiple protected " |
| 134 | "media streams, but our implementation currently only " |
| 135 | "supports protecting a single media stream. " |
| 136 | "To avoid confusion, disabling FlexFEC completely."; |
| 137 | return nullptr; |
| 138 | } |
| 139 | |
brandtr | 48d21a2 | 2017-05-30 02:32:12 -0700 | [diff] [blame] | 140 | const RtpState* rtp_state = nullptr; |
| 141 | auto it = suspended_ssrcs.find(config.rtp.flexfec.ssrc); |
| 142 | if (it != suspended_ssrcs.end()) { |
| 143 | rtp_state = &it->second; |
| 144 | } |
| 145 | |
brandtr | e950cad | 2016-11-15 05:25:41 -0800 | [diff] [blame] | 146 | RTC_DCHECK_EQ(1U, config.rtp.flexfec.protected_media_ssrcs.size()); |
| 147 | return std::unique_ptr<FlexfecSender>(new FlexfecSender( |
brandtr | 3d200bd | 2017-01-16 06:59:19 -0800 | [diff] [blame] | 148 | config.rtp.flexfec.payload_type, config.rtp.flexfec.ssrc, |
brandtr | e950cad | 2016-11-15 05:25:41 -0800 | [diff] [blame] | 149 | config.rtp.flexfec.protected_media_ssrcs[0], config.rtp.extensions, |
brandtr | 48d21a2 | 2017-05-30 02:32:12 -0700 | [diff] [blame] | 150 | RTPSender::FecExtensionSizes(), rtp_state, Clock::GetRealTimeClock())); |
brandtr | e950cad | 2016-11-15 05:25:41 -0800 | [diff] [blame] | 151 | } |
| 152 | |
Erik Språng | 7c8cca3 | 2017-10-24 17:05:18 +0200 | [diff] [blame] | 153 | bool TransportSeqNumExtensionConfigured(const VideoSendStream::Config& config) { |
| 154 | const std::vector<RtpExtension>& extensions = config.rtp.extensions; |
| 155 | return std::find_if( |
| 156 | extensions.begin(), extensions.end(), [](const RtpExtension& ext) { |
| 157 | return ext.uri == RtpExtension::kTransportSequenceNumberUri; |
| 158 | }) != extensions.end(); |
| 159 | } |
Peter Boström | e449915 | 2016-02-05 11:13:28 +0100 | [diff] [blame] | 160 | |
Ã…sa Persson | 45bbc8a | 2017-11-13 10:16:47 +0100 | [diff] [blame] | 161 | const char kForcedFallbackFieldTrial[] = |
| 162 | "WebRTC-VP8-Forced-Fallback-Encoder-v2"; |
| 163 | |
| 164 | rtc::Optional<int> GetFallbackMinBpsFromFieldTrial() { |
| 165 | if (!webrtc::field_trial::IsEnabled(kForcedFallbackFieldTrial)) |
Oskar Sundbom | 8e07c13 | 2018-01-08 16:45:42 +0100 | [diff] [blame] | 166 | return rtc::nullopt; |
Ã…sa Persson | 45bbc8a | 2017-11-13 10:16:47 +0100 | [diff] [blame] | 167 | |
| 168 | std::string group = |
| 169 | webrtc::field_trial::FindFullName(kForcedFallbackFieldTrial); |
| 170 | if (group.empty()) |
Oskar Sundbom | 8e07c13 | 2018-01-08 16:45:42 +0100 | [diff] [blame] | 171 | return rtc::nullopt; |
Ã…sa Persson | 45bbc8a | 2017-11-13 10:16:47 +0100 | [diff] [blame] | 172 | |
| 173 | int min_pixels; |
| 174 | int max_pixels; |
| 175 | int min_bps; |
| 176 | if (sscanf(group.c_str(), "Enabled-%d,%d,%d", &min_pixels, &max_pixels, |
| 177 | &min_bps) != 3) { |
Oskar Sundbom | 8e07c13 | 2018-01-08 16:45:42 +0100 | [diff] [blame] | 178 | return rtc::nullopt; |
Ã…sa Persson | 45bbc8a | 2017-11-13 10:16:47 +0100 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | if (min_bps <= 0) |
Oskar Sundbom | 8e07c13 | 2018-01-08 16:45:42 +0100 | [diff] [blame] | 182 | return rtc::nullopt; |
Ã…sa Persson | 45bbc8a | 2017-11-13 10:16:47 +0100 | [diff] [blame] | 183 | |
Oskar Sundbom | 8e07c13 | 2018-01-08 16:45:42 +0100 | [diff] [blame] | 184 | return min_bps; |
Ã…sa Persson | 45bbc8a | 2017-11-13 10:16:47 +0100 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | int GetEncoderMinBitrateBps() { |
| 188 | const int kDefaultEncoderMinBitrateBps = 30000; |
| 189 | return GetFallbackMinBpsFromFieldTrial().value_or( |
| 190 | kDefaultEncoderMinBitrateBps); |
| 191 | } |
| 192 | |
Peter Boström | 3959397 | 2016-02-15 11:27:15 +0100 | [diff] [blame] | 193 | bool PayloadTypeSupportsSkippingFecPackets(const std::string& payload_name) { |
kthelgason | 1cdddc9 | 2017-08-24 03:52:48 -0700 | [diff] [blame] | 194 | const VideoCodecType codecType = PayloadStringToCodecType(payload_name); |
| 195 | if (codecType == kVideoCodecVP8 || codecType == kVideoCodecVP9) { |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 196 | return true; |
deadbeef | c964d0b | 2017-04-03 10:03:35 -0700 | [diff] [blame] | 197 | } |
Peter Boström | 3959397 | 2016-02-15 11:27:15 +0100 | [diff] [blame] | 198 | return false; |
| 199 | } |
| 200 | |
Per | 512ecb3 | 2016-09-23 15:52:06 +0200 | [diff] [blame] | 201 | int CalculateMaxPadBitrateBps(std::vector<VideoStream> streams, |
| 202 | int min_transmit_bitrate_bps, |
perkj | 71ee44c | 2016-06-15 00:47:53 -0700 | [diff] [blame] | 203 | bool pad_to_min_bitrate) { |
| 204 | int pad_up_to_bitrate_bps = 0; |
| 205 | // Calculate max padding bitrate for a multi layer codec. |
Per | 512ecb3 | 2016-09-23 15:52:06 +0200 | [diff] [blame] | 206 | if (streams.size() > 1) { |
perkj | 71ee44c | 2016-06-15 00:47:53 -0700 | [diff] [blame] | 207 | // Pad to min bitrate of the highest layer. |
Per | 512ecb3 | 2016-09-23 15:52:06 +0200 | [diff] [blame] | 208 | pad_up_to_bitrate_bps = streams[streams.size() - 1].min_bitrate_bps; |
perkj | 71ee44c | 2016-06-15 00:47:53 -0700 | [diff] [blame] | 209 | // Add target_bitrate_bps of the lower layers. |
Per | 512ecb3 | 2016-09-23 15:52:06 +0200 | [diff] [blame] | 210 | for (size_t i = 0; i < streams.size() - 1; ++i) |
| 211 | pad_up_to_bitrate_bps += streams[i].target_bitrate_bps; |
perkj | 71ee44c | 2016-06-15 00:47:53 -0700 | [diff] [blame] | 212 | } else if (pad_to_min_bitrate) { |
Per | 512ecb3 | 2016-09-23 15:52:06 +0200 | [diff] [blame] | 213 | pad_up_to_bitrate_bps = streams[0].min_bitrate_bps; |
perkj | 71ee44c | 2016-06-15 00:47:53 -0700 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | pad_up_to_bitrate_bps = |
Per | 512ecb3 | 2016-09-23 15:52:06 +0200 | [diff] [blame] | 217 | std::max(pad_up_to_bitrate_bps, min_transmit_bitrate_bps); |
perkj | 71ee44c | 2016-06-15 00:47:53 -0700 | [diff] [blame] | 218 | |
| 219 | return pad_up_to_bitrate_bps; |
| 220 | } |
| 221 | |
michaelt | 192132e | 2017-01-26 09:05:27 -0800 | [diff] [blame] | 222 | uint32_t CalculateOverheadRateBps(int packets_per_second, |
| 223 | size_t overhead_bytes_per_packet, |
| 224 | uint32_t max_overhead_bps) { |
michaelt | 192132e | 2017-01-26 09:05:27 -0800 | [diff] [blame] | 225 | uint32_t overhead_bps = |
| 226 | static_cast<uint32_t>(8 * overhead_bytes_per_packet * packets_per_second); |
| 227 | return std::min(overhead_bps, max_overhead_bps); |
| 228 | } |
| 229 | |
| 230 | int CalculatePacketRate(uint32_t bitrate_bps, size_t packet_size_bytes) { |
| 231 | size_t packet_size_bits = 8 * packet_size_bytes; |
| 232 | // Ceil for int value of bitrate_bps / packet_size_bits. |
| 233 | return static_cast<int>((bitrate_bps + packet_size_bits - 1) / |
| 234 | packet_size_bits); |
| 235 | } |
| 236 | |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame^] | 237 | // TODO(pbos): Lower these thresholds (to closer to 100%) when we handle |
| 238 | // pipelining encoders better (multiple input frames before something comes |
| 239 | // out). This should effectively turn off CPU adaptations for systems that |
| 240 | // remotely cope with the load right now. |
| 241 | CpuOveruseOptions GetCpuOveruseOptions(const VideoSendStream::Config& config) { |
| 242 | CpuOveruseOptions options; |
| 243 | |
| 244 | if (config.encoder_settings.full_overuse_time) { |
| 245 | options.low_encode_usage_threshold_percent = 150; |
| 246 | options.high_encode_usage_threshold_percent = 200; |
| 247 | } |
| 248 | if (config.encoder_settings.experiment_cpu_load_estimator) { |
| 249 | options.filter_time_ms = 5 * rtc::kNumMillisecsPerSec; |
| 250 | } |
| 251 | |
| 252 | return options; |
| 253 | } |
| 254 | |
Peter Boström | e449915 | 2016-02-05 11:13:28 +0100 | [diff] [blame] | 255 | } // namespace |
| 256 | |
pbos@webrtc.org | 024e4d5 | 2014-05-15 10:03:24 +0000 | [diff] [blame] | 257 | namespace internal { |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 258 | |
| 259 | // VideoSendStreamImpl implements internal::VideoSendStream. |
| 260 | // It is created and destroyed on |worker_queue|. The intent is to decrease the |
| 261 | // need for locking and to ensure methods are called in sequence. |
| 262 | // Public methods except |DeliverRtcp| must be called on |worker_queue|. |
| 263 | // DeliverRtcp is called on the libjingle worker thread or a network thread. |
| 264 | // An encoder may deliver frames through the EncodedImageCallback on an |
| 265 | // arbitrary thread. |
| 266 | class VideoSendStreamImpl : public webrtc::BitrateAllocatorObserver, |
michaelt | a332877 | 2016-11-29 09:25:03 -0800 | [diff] [blame] | 267 | public webrtc::OverheadObserver, |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 268 | public webrtc::VCMProtectionCallback, |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 269 | public VideoStreamEncoder::EncoderSink, |
Ying Wang | 3b790f3 | 2018-01-19 17:58:57 +0100 | [diff] [blame] | 270 | public VideoBitrateAllocationObserver, |
| 271 | public webrtc::PacketFeedbackObserver { |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 272 | public: |
Ã…sa Persson | 4bece9a | 2017-10-06 10:04:04 +0200 | [diff] [blame] | 273 | VideoSendStreamImpl( |
| 274 | SendStatisticsProxy* stats_proxy, |
| 275 | rtc::TaskQueue* worker_queue, |
| 276 | CallStats* call_stats, |
| 277 | RtpTransportControllerSendInterface* transport, |
| 278 | BitrateAllocator* bitrate_allocator, |
| 279 | SendDelayStats* send_delay_stats, |
| 280 | VideoStreamEncoder* video_stream_encoder, |
| 281 | RtcEventLog* event_log, |
| 282 | const VideoSendStream::Config* config, |
| 283 | int initial_encoder_max_bitrate, |
Seth Hampson | 24722b3 | 2017-12-22 09:36:42 -0800 | [diff] [blame] | 284 | double initial_encoder_bitrate_priority, |
Ã…sa Persson | 4bece9a | 2017-10-06 10:04:04 +0200 | [diff] [blame] | 285 | std::map<uint32_t, RtpState> suspended_ssrcs, |
| 286 | std::map<uint32_t, RtpPayloadState> suspended_payload_states, |
Ying Wang | 3b790f3 | 2018-01-19 17:58:57 +0100 | [diff] [blame] | 287 | VideoEncoderConfig::ContentType content_type, |
| 288 | std::unique_ptr<FecController> fec_controller); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 289 | ~VideoSendStreamImpl() override; |
| 290 | |
| 291 | // RegisterProcessThread register |module_process_thread| with those objects |
| 292 | // that use it. Registration has to happen on the thread were |
| 293 | // |module_process_thread| was created (libjingle's worker thread). |
| 294 | // TODO(perkj): Replace the use of |module_process_thread| with a TaskQueue, |
| 295 | // maybe |worker_queue|. |
| 296 | void RegisterProcessThread(ProcessThread* module_process_thread); |
| 297 | void DeRegisterProcessThread(); |
| 298 | |
| 299 | void SignalNetworkState(NetworkState state); |
| 300 | bool DeliverRtcp(const uint8_t* packet, size_t length); |
Seth Hampson | cc7125f | 2018-02-02 08:46:16 -0800 | [diff] [blame] | 301 | void UpdateActiveSimulcastLayers(const std::vector<bool> active_layers); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 302 | void Start(); |
| 303 | void Stop(); |
| 304 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 305 | VideoSendStream::RtpStateMap GetRtpStates() const; |
Ã…sa Persson | 4bece9a | 2017-10-06 10:04:04 +0200 | [diff] [blame] | 306 | VideoSendStream::RtpPayloadStateMap GetRtpPayloadStates() const; |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 307 | |
palmkvist | e75f204 | 2016-09-28 06:19:48 -0700 | [diff] [blame] | 308 | void EnableEncodedFrameRecording(const std::vector<rtc::PlatformFile>& files, |
| 309 | size_t byte_limit); |
| 310 | |
nisse | 284542b | 2017-01-10 08:58:32 -0800 | [diff] [blame] | 311 | void SetTransportOverhead(size_t transport_overhead_per_packet); |
michaelt | 79e0588 | 2016-11-08 02:50:09 -0800 | [diff] [blame] | 312 | |
Sebastian Jansson | a45c8da | 2018-01-16 10:55:29 +0100 | [diff] [blame] | 313 | rtc::Optional<float> configured_pacing_factor_; |
| 314 | |
Ying Wang | 3b790f3 | 2018-01-19 17:58:57 +0100 | [diff] [blame] | 315 | // From PacketFeedbackObserver. |
| 316 | void OnPacketAdded(uint32_t ssrc, uint16_t seq_num) override; |
| 317 | void OnPacketFeedbackVector( |
| 318 | const std::vector<PacketFeedback>& packet_feedback_vector) override; |
| 319 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 320 | private: |
| 321 | class CheckEncoderActivityTask; |
Per | 512ecb3 | 2016-09-23 15:52:06 +0200 | [diff] [blame] | 322 | class EncoderReconfiguredTask; |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 323 | |
| 324 | // Implements BitrateAllocatorObserver. |
| 325 | uint32_t OnBitrateUpdated(uint32_t bitrate_bps, |
| 326 | uint8_t fraction_loss, |
minyue | 78b4d56 | 2016-11-30 04:47:39 -0800 | [diff] [blame] | 327 | int64_t rtt, |
| 328 | int64_t probing_interval_ms) override; |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 329 | |
| 330 | // Implements webrtc::VCMProtectionCallback. |
| 331 | int ProtectionRequest(const FecProtectionParams* delta_params, |
| 332 | const FecProtectionParams* key_params, |
| 333 | uint32_t* sent_video_rate_bps, |
| 334 | uint32_t* sent_nack_rate_bps, |
| 335 | uint32_t* sent_fec_rate_bps) override; |
| 336 | |
michaelt | a332877 | 2016-11-29 09:25:03 -0800 | [diff] [blame] | 337 | // Implements OverheadObserver. |
| 338 | void OnOverheadChanged(size_t overhead_bytes_per_packet) override; |
| 339 | |
Per | 512ecb3 | 2016-09-23 15:52:06 +0200 | [diff] [blame] | 340 | void OnEncoderConfigurationChanged(std::vector<VideoStream> streams, |
| 341 | int min_transmit_bitrate_bps) override; |
| 342 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 343 | // Implements EncodedImageCallback. The implementation routes encoded frames |
| 344 | // to the |payload_router_| and |config.pre_encode_callback| if set. |
| 345 | // Called on an arbitrary encoder callback thread. |
| 346 | EncodedImageCallback::Result OnEncodedImage( |
| 347 | const EncodedImage& encoded_image, |
| 348 | const CodecSpecificInfo* codec_specific_info, |
| 349 | const RTPFragmentationHeader* fragmentation) override; |
| 350 | |
sprang | 1a646ee | 2016-12-01 06:34:11 -0800 | [diff] [blame] | 351 | // Implements VideoBitrateAllocationObserver. |
| 352 | void OnBitrateAllocationUpdated(const BitrateAllocation& allocation) override; |
| 353 | |
Seth Hampson | cc7125f | 2018-02-02 08:46:16 -0800 | [diff] [blame] | 354 | // Starts monitoring and sends a keyframe. |
| 355 | void StartupVideoSendStream(); |
| 356 | // Removes the bitrate observer, stops monitoring and notifies the video |
| 357 | // encoder of the bitrate update. |
| 358 | void StopVideoSendStream(); |
| 359 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 360 | void ConfigureProtection(); |
| 361 | void ConfigureSsrcs(); |
| 362 | void SignalEncoderTimedOut(); |
| 363 | void SignalEncoderActive(); |
| 364 | |
elad.alon | 0fe1216 | 2017-01-31 05:48:37 -0800 | [diff] [blame] | 365 | const bool send_side_bwe_with_overhead_; |
Ying Wang | 38a31b0 | 2017-12-21 12:26:19 +0000 | [diff] [blame] | 366 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 367 | SendStatisticsProxy* const stats_proxy_; |
| 368 | const VideoSendStream::Config* const config_; |
| 369 | std::map<uint32_t, RtpState> suspended_ssrcs_; |
| 370 | |
Ying Wang | 3b790f3 | 2018-01-19 17:58:57 +0100 | [diff] [blame] | 371 | std::unique_ptr<FecController> fec_controller_; |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 372 | ProcessThread* module_process_thread_; |
| 373 | rtc::ThreadChecker module_process_thread_checker_; |
| 374 | rtc::TaskQueue* const worker_queue_; |
| 375 | |
| 376 | rtc::CriticalSection encoder_activity_crit_sect_; |
| 377 | CheckEncoderActivityTask* check_encoder_activity_task_ |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 378 | RTC_GUARDED_BY(encoder_activity_crit_sect_); |
Per | 512ecb3 | 2016-09-23 15:52:06 +0200 | [diff] [blame] | 379 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 380 | CallStats* const call_stats_; |
nisse | b8f9a32 | 2017-03-27 05:36:15 -0700 | [diff] [blame] | 381 | RtpTransportControllerSendInterface* const transport_; |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 382 | BitrateAllocator* const bitrate_allocator_; |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 383 | |
brandtr | 3b941be | 2017-03-17 07:02:46 -0700 | [diff] [blame] | 384 | // TODO(brandtr): Move ownership to PayloadRouter. |
brandtr | e950cad | 2016-11-15 05:25:41 -0800 | [diff] [blame] | 385 | std::unique_ptr<FlexfecSender> flexfec_sender_; |
| 386 | |
palmkvist | e75f204 | 2016-09-28 06:19:48 -0700 | [diff] [blame] | 387 | rtc::CriticalSection ivf_writers_crit_; |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 388 | std::unique_ptr<IvfFileWriter> |
| 389 | file_writers_[kMaxSimulcastStreams] RTC_GUARDED_BY(ivf_writers_crit_); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 390 | |
| 391 | int max_padding_bitrate_; |
| 392 | int encoder_min_bitrate_bps_; |
| 393 | uint32_t encoder_max_bitrate_bps_; |
| 394 | uint32_t encoder_target_rate_bps_; |
Seth Hampson | 24722b3 | 2017-12-22 09:36:42 -0800 | [diff] [blame] | 395 | double encoder_bitrate_priority_; |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 396 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 397 | VideoStreamEncoder* const video_stream_encoder_; |
mflodman | 15d8357 | 2016-10-06 08:35:11 -0700 | [diff] [blame] | 398 | EncoderRtcpFeedback encoder_feedback_; |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 399 | |
Sebastian Jansson | 8d9c540 | 2017-11-15 17:22:16 +0100 | [diff] [blame] | 400 | RtcpBandwidthObserver* const bandwidth_observer_; |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 401 | // RtpRtcp modules, declared here as they use other members on construction. |
| 402 | const std::vector<RtpRtcp*> rtp_rtcp_modules_; |
| 403 | PayloadRouter payload_router_; |
perkj | 8ff860a | 2016-10-03 00:30:04 -0700 | [diff] [blame] | 404 | |
| 405 | // |weak_ptr_| to our self. This is used since we can not call |
| 406 | // |weak_ptr_factory_.GetWeakPtr| from multiple sequences but it is ok to copy |
| 407 | // an existing WeakPtr. |
| 408 | rtc::WeakPtr<VideoSendStreamImpl> weak_ptr_; |
| 409 | // |weak_ptr_factory_| must be declared last to make sure all WeakPtr's are |
| 410 | // invalidated before any other members are destroyed. |
| 411 | rtc::WeakPtrFactory<VideoSendStreamImpl> weak_ptr_factory_; |
michaelt | a332877 | 2016-11-29 09:25:03 -0800 | [diff] [blame] | 412 | |
| 413 | rtc::CriticalSection overhead_bytes_per_packet_crit_; |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 414 | size_t overhead_bytes_per_packet_ |
| 415 | RTC_GUARDED_BY(overhead_bytes_per_packet_crit_); |
michaelt | a332877 | 2016-11-29 09:25:03 -0800 | [diff] [blame] | 416 | size_t transport_overhead_bytes_per_packet_; |
Ying Wang | 3b790f3 | 2018-01-19 17:58:57 +0100 | [diff] [blame] | 417 | |
| 418 | std::set<uint16_t> feedback_packet_seq_num_set_; |
| 419 | std::vector<bool> loss_mask_vector_; |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 420 | }; |
| 421 | |
| 422 | // TODO(tommi): See if there's a more elegant way to create a task that creates |
| 423 | // an object on the correct task queue. |
| 424 | class VideoSendStream::ConstructionTask : public rtc::QueuedTask { |
| 425 | public: |
Ã…sa Persson | 4bece9a | 2017-10-06 10:04:04 +0200 | [diff] [blame] | 426 | ConstructionTask( |
| 427 | std::unique_ptr<VideoSendStreamImpl>* send_stream, |
| 428 | rtc::Event* done_event, |
| 429 | SendStatisticsProxy* stats_proxy, |
| 430 | VideoStreamEncoder* video_stream_encoder, |
| 431 | ProcessThread* module_process_thread, |
| 432 | CallStats* call_stats, |
| 433 | RtpTransportControllerSendInterface* transport, |
| 434 | BitrateAllocator* bitrate_allocator, |
| 435 | SendDelayStats* send_delay_stats, |
| 436 | RtcEventLog* event_log, |
| 437 | const VideoSendStream::Config* config, |
| 438 | int initial_encoder_max_bitrate, |
Seth Hampson | 24722b3 | 2017-12-22 09:36:42 -0800 | [diff] [blame] | 439 | double initial_encoder_bitrate_priority, |
Ã…sa Persson | 4bece9a | 2017-10-06 10:04:04 +0200 | [diff] [blame] | 440 | const std::map<uint32_t, RtpState>& suspended_ssrcs, |
| 441 | const std::map<uint32_t, RtpPayloadState>& suspended_payload_states, |
Ying Wang | 3b790f3 | 2018-01-19 17:58:57 +0100 | [diff] [blame] | 442 | VideoEncoderConfig::ContentType content_type, |
| 443 | std::unique_ptr<FecController> fec_controller) |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 444 | : send_stream_(send_stream), |
| 445 | done_event_(done_event), |
| 446 | stats_proxy_(stats_proxy), |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 447 | video_stream_encoder_(video_stream_encoder), |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 448 | call_stats_(call_stats), |
nisse | b8f9a32 | 2017-03-27 05:36:15 -0700 | [diff] [blame] | 449 | transport_(transport), |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 450 | bitrate_allocator_(bitrate_allocator), |
| 451 | send_delay_stats_(send_delay_stats), |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 452 | event_log_(event_log), |
| 453 | config_(config), |
Per | 512ecb3 | 2016-09-23 15:52:06 +0200 | [diff] [blame] | 454 | initial_encoder_max_bitrate_(initial_encoder_max_bitrate), |
Seth Hampson | 24722b3 | 2017-12-22 09:36:42 -0800 | [diff] [blame] | 455 | initial_encoder_bitrate_priority_(initial_encoder_bitrate_priority), |
sprang | 89c4a7e | 2017-06-30 13:27:40 -0700 | [diff] [blame] | 456 | suspended_ssrcs_(suspended_ssrcs), |
Ã…sa Persson | 4bece9a | 2017-10-06 10:04:04 +0200 | [diff] [blame] | 457 | suspended_payload_states_(suspended_payload_states), |
Ying Wang | 3b790f3 | 2018-01-19 17:58:57 +0100 | [diff] [blame] | 458 | content_type_(content_type), |
| 459 | fec_controller_(std::move(fec_controller)) {} |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 460 | |
| 461 | ~ConstructionTask() override { done_event_->Set(); } |
| 462 | |
| 463 | private: |
| 464 | bool Run() override { |
| 465 | send_stream_->reset(new VideoSendStreamImpl( |
nisse | b8f9a32 | 2017-03-27 05:36:15 -0700 | [diff] [blame] | 466 | stats_proxy_, rtc::TaskQueue::Current(), call_stats_, transport_, |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 467 | bitrate_allocator_, send_delay_stats_, video_stream_encoder_, |
| 468 | event_log_, config_, initial_encoder_max_bitrate_, |
Seth Hampson | 24722b3 | 2017-12-22 09:36:42 -0800 | [diff] [blame] | 469 | initial_encoder_bitrate_priority_, std::move(suspended_ssrcs_), |
Ying Wang | 3b790f3 | 2018-01-19 17:58:57 +0100 | [diff] [blame] | 470 | std::move(suspended_payload_states_), content_type_, |
| 471 | std::move(fec_controller_))); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 472 | return true; |
| 473 | } |
| 474 | |
| 475 | std::unique_ptr<VideoSendStreamImpl>* const send_stream_; |
| 476 | rtc::Event* const done_event_; |
| 477 | SendStatisticsProxy* const stats_proxy_; |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 478 | VideoStreamEncoder* const video_stream_encoder_; |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 479 | CallStats* const call_stats_; |
nisse | b8f9a32 | 2017-03-27 05:36:15 -0700 | [diff] [blame] | 480 | RtpTransportControllerSendInterface* const transport_; |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 481 | BitrateAllocator* const bitrate_allocator_; |
| 482 | SendDelayStats* const send_delay_stats_; |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 483 | RtcEventLog* const event_log_; |
| 484 | const VideoSendStream::Config* config_; |
Per | 512ecb3 | 2016-09-23 15:52:06 +0200 | [diff] [blame] | 485 | int initial_encoder_max_bitrate_; |
Seth Hampson | 24722b3 | 2017-12-22 09:36:42 -0800 | [diff] [blame] | 486 | double initial_encoder_bitrate_priority_; |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 487 | std::map<uint32_t, RtpState> suspended_ssrcs_; |
Ã…sa Persson | 4bece9a | 2017-10-06 10:04:04 +0200 | [diff] [blame] | 488 | std::map<uint32_t, RtpPayloadState> suspended_payload_states_; |
sprang | 89c4a7e | 2017-06-30 13:27:40 -0700 | [diff] [blame] | 489 | const VideoEncoderConfig::ContentType content_type_; |
Ying Wang | 3b790f3 | 2018-01-19 17:58:57 +0100 | [diff] [blame] | 490 | std::unique_ptr<FecController> fec_controller_; |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 491 | }; |
| 492 | |
| 493 | class VideoSendStream::DestructAndGetRtpStateTask : public rtc::QueuedTask { |
| 494 | public: |
Ã…sa Persson | 4bece9a | 2017-10-06 10:04:04 +0200 | [diff] [blame] | 495 | DestructAndGetRtpStateTask( |
| 496 | VideoSendStream::RtpStateMap* state_map, |
| 497 | VideoSendStream::RtpPayloadStateMap* payload_state_map, |
| 498 | std::unique_ptr<VideoSendStreamImpl> send_stream, |
| 499 | rtc::Event* done_event) |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 500 | : state_map_(state_map), |
Ã…sa Persson | 4bece9a | 2017-10-06 10:04:04 +0200 | [diff] [blame] | 501 | payload_state_map_(payload_state_map), |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 502 | send_stream_(std::move(send_stream)), |
| 503 | done_event_(done_event) {} |
| 504 | |
| 505 | ~DestructAndGetRtpStateTask() override { RTC_CHECK(!send_stream_); } |
| 506 | |
| 507 | private: |
| 508 | bool Run() override { |
| 509 | send_stream_->Stop(); |
| 510 | *state_map_ = send_stream_->GetRtpStates(); |
Ã…sa Persson | 4bece9a | 2017-10-06 10:04:04 +0200 | [diff] [blame] | 511 | *payload_state_map_ = send_stream_->GetRtpPayloadStates(); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 512 | send_stream_.reset(); |
| 513 | done_event_->Set(); |
| 514 | return true; |
| 515 | } |
| 516 | |
| 517 | VideoSendStream::RtpStateMap* state_map_; |
Ã…sa Persson | 4bece9a | 2017-10-06 10:04:04 +0200 | [diff] [blame] | 518 | VideoSendStream::RtpPayloadStateMap* payload_state_map_; |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 519 | std::unique_ptr<VideoSendStreamImpl> send_stream_; |
| 520 | rtc::Event* done_event_; |
| 521 | }; |
| 522 | |
| 523 | // CheckEncoderActivityTask is used for tracking when the encoder last produced |
| 524 | // and encoded video frame. If the encoder has not produced anything the last |
| 525 | // kEncoderTimeOutMs we also want to stop sending padding. |
| 526 | class VideoSendStreamImpl::CheckEncoderActivityTask : public rtc::QueuedTask { |
| 527 | public: |
| 528 | static const int kEncoderTimeOutMs = 2000; |
perkj | 8ff860a | 2016-10-03 00:30:04 -0700 | [diff] [blame] | 529 | explicit CheckEncoderActivityTask( |
| 530 | const rtc::WeakPtr<VideoSendStreamImpl>& send_stream) |
| 531 | : activity_(0), send_stream_(std::move(send_stream)), timed_out_(false) {} |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 532 | |
| 533 | void Stop() { |
| 534 | RTC_CHECK(task_checker_.CalledSequentially()); |
perkj | 8ff860a | 2016-10-03 00:30:04 -0700 | [diff] [blame] | 535 | send_stream_.reset(); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | void UpdateEncoderActivity() { |
| 539 | // UpdateEncoderActivity is called from VideoSendStreamImpl::Encoded on |
| 540 | // whatever thread the real encoder implementation run on. In the case of |
| 541 | // hardware encoders, there might be several encoders |
| 542 | // running in parallel on different threads. |
| 543 | rtc::AtomicOps::ReleaseStore(&activity_, 1); |
| 544 | } |
| 545 | |
| 546 | private: |
| 547 | bool Run() override { |
| 548 | RTC_CHECK(task_checker_.CalledSequentially()); |
| 549 | if (!send_stream_) |
| 550 | return true; |
| 551 | if (!rtc::AtomicOps::AcquireLoad(&activity_)) { |
| 552 | if (!timed_out_) { |
| 553 | send_stream_->SignalEncoderTimedOut(); |
| 554 | } |
| 555 | timed_out_ = true; |
| 556 | } else if (timed_out_) { |
| 557 | send_stream_->SignalEncoderActive(); |
| 558 | timed_out_ = false; |
| 559 | } |
| 560 | rtc::AtomicOps::ReleaseStore(&activity_, 0); |
| 561 | |
| 562 | rtc::TaskQueue::Current()->PostDelayedTask( |
| 563 | std::unique_ptr<rtc::QueuedTask>(this), kEncoderTimeOutMs); |
| 564 | // Return false to prevent this task from being deleted. Ownership has been |
| 565 | // transferred to the task queue when PostDelayedTask was called. |
| 566 | return false; |
| 567 | } |
| 568 | volatile int activity_; |
| 569 | |
| 570 | rtc::SequencedTaskChecker task_checker_; |
perkj | 8ff860a | 2016-10-03 00:30:04 -0700 | [diff] [blame] | 571 | rtc::WeakPtr<VideoSendStreamImpl> send_stream_; |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 572 | bool timed_out_; |
| 573 | }; |
| 574 | |
Per | 512ecb3 | 2016-09-23 15:52:06 +0200 | [diff] [blame] | 575 | class VideoSendStreamImpl::EncoderReconfiguredTask : public rtc::QueuedTask { |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 576 | public: |
perkj | 8ff860a | 2016-10-03 00:30:04 -0700 | [diff] [blame] | 577 | EncoderReconfiguredTask(const rtc::WeakPtr<VideoSendStreamImpl>& send_stream, |
Per | 512ecb3 | 2016-09-23 15:52:06 +0200 | [diff] [blame] | 578 | std::vector<VideoStream> streams, |
| 579 | int min_transmit_bitrate_bps) |
perkj | 8ff860a | 2016-10-03 00:30:04 -0700 | [diff] [blame] | 580 | : send_stream_(std::move(send_stream)), |
Per | 512ecb3 | 2016-09-23 15:52:06 +0200 | [diff] [blame] | 581 | streams_(std::move(streams)), |
| 582 | min_transmit_bitrate_bps_(min_transmit_bitrate_bps) {} |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 583 | |
| 584 | private: |
| 585 | bool Run() override { |
perkj | 8ff860a | 2016-10-03 00:30:04 -0700 | [diff] [blame] | 586 | if (send_stream_) |
| 587 | send_stream_->OnEncoderConfigurationChanged(std::move(streams_), |
| 588 | min_transmit_bitrate_bps_); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 589 | return true; |
| 590 | } |
| 591 | |
perkj | 8ff860a | 2016-10-03 00:30:04 -0700 | [diff] [blame] | 592 | rtc::WeakPtr<VideoSendStreamImpl> send_stream_; |
Per | 512ecb3 | 2016-09-23 15:52:06 +0200 | [diff] [blame] | 593 | std::vector<VideoStream> streams_; |
| 594 | int min_transmit_bitrate_bps_; |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 595 | }; |
| 596 | |
pbos@webrtc.org | 2bb1bda | 2014-07-07 13:06:48 +0000 | [diff] [blame] | 597 | VideoSendStream::VideoSendStream( |
Peter Boström | 45553ae | 2015-05-08 13:54:38 +0200 | [diff] [blame] | 598 | int num_cpu_cores, |
Peter Boström | f16fcbe | 2015-04-30 12:16:05 +0200 | [diff] [blame] | 599 | ProcessThread* module_process_thread, |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 600 | rtc::TaskQueue* worker_queue, |
mflodman | e378702 | 2015-10-21 13:24:28 +0200 | [diff] [blame] | 601 | CallStats* call_stats, |
nisse | b8f9a32 | 2017-03-27 05:36:15 -0700 | [diff] [blame] | 602 | RtpTransportControllerSendInterface* transport, |
mflodman | 0e7e259 | 2015-11-12 21:02:42 -0800 | [diff] [blame] | 603 | BitrateAllocator* bitrate_allocator, |
asapersson | 35151f3 | 2016-05-02 23:44:01 -0700 | [diff] [blame] | 604 | SendDelayStats* send_delay_stats, |
terelius | adafe0b | 2016-05-26 01:58:40 -0700 | [diff] [blame] | 605 | RtcEventLog* event_log, |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 606 | VideoSendStream::Config config, |
| 607 | VideoEncoderConfig encoder_config, |
Ã…sa Persson | 4bece9a | 2017-10-06 10:04:04 +0200 | [diff] [blame] | 608 | const std::map<uint32_t, RtpState>& suspended_ssrcs, |
Ying Wang | 3b790f3 | 2018-01-19 17:58:57 +0100 | [diff] [blame] | 609 | const std::map<uint32_t, RtpPayloadState>& suspended_payload_states, |
| 610 | std::unique_ptr<FecController> fec_controller) |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 611 | : worker_queue_(worker_queue), |
| 612 | thread_sync_event_(false /* manual_reset */, false), |
| 613 | stats_proxy_(Clock::GetRealTimeClock(), |
sprang | b4a1ae5 | 2015-12-03 08:10:08 -0800 | [diff] [blame] | 614 | config, |
| 615 | encoder_config.content_type), |
sprang | f24a064 | 2017-02-28 13:23:26 -0800 | [diff] [blame] | 616 | config_(std::move(config)), |
| 617 | content_type_(encoder_config.content_type) { |
Niels Möller | 73f29cb | 2018-01-31 16:09:31 +0100 | [diff] [blame^] | 618 | video_stream_encoder_ = rtc::MakeUnique<VideoStreamEncoder>( |
| 619 | num_cpu_cores, &stats_proxy_, |
| 620 | config_.encoder_settings, |
| 621 | config_.pre_encode_callback, |
| 622 | rtc::MakeUnique<OveruseFrameDetector>( |
| 623 | GetCpuOveruseOptions(config_), &stats_proxy_)); |
| 624 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 625 | worker_queue_->PostTask(std::unique_ptr<rtc::QueuedTask>(new ConstructionTask( |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 626 | &send_stream_, &thread_sync_event_, &stats_proxy_, |
| 627 | video_stream_encoder_.get(), module_process_thread, call_stats, transport, |
| 628 | bitrate_allocator, send_delay_stats, event_log, &config_, |
Seth Hampson | 24722b3 | 2017-12-22 09:36:42 -0800 | [diff] [blame] | 629 | encoder_config.max_bitrate_bps, encoder_config.bitrate_priority, |
Ying Wang | 3b790f3 | 2018-01-19 17:58:57 +0100 | [diff] [blame] | 630 | suspended_ssrcs, suspended_payload_states, encoder_config.content_type, |
| 631 | std::move(fec_controller)))); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 632 | |
| 633 | // Wait for ConstructionTask to complete so that |send_stream_| can be used. |
| 634 | // |module_process_thread| must be registered and deregistered on the thread |
| 635 | // it was created on. |
| 636 | thread_sync_event_.Wait(rtc::Event::kForever); |
| 637 | send_stream_->RegisterProcessThread(module_process_thread); |
sprang | 44b3ef6 | 2017-01-13 07:30:25 -0800 | [diff] [blame] | 638 | // TODO(sprang): Enable this also for regular video calls if it works well. |
| 639 | if (encoder_config.content_type == VideoEncoderConfig::ContentType::kScreen) { |
| 640 | // Only signal target bitrate for screenshare streams, for now. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 641 | video_stream_encoder_->SetBitrateObserver(send_stream_.get()); |
sprang | 44b3ef6 | 2017-01-13 07:30:25 -0800 | [diff] [blame] | 642 | } |
Ying Wang | 38a31b0 | 2017-12-21 12:26:19 +0000 | [diff] [blame] | 643 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 644 | ReconfigureVideoEncoder(std::move(encoder_config)); |
| 645 | } |
| 646 | |
| 647 | VideoSendStream::~VideoSendStream() { |
| 648 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 649 | RTC_DCHECK(!send_stream_); |
| 650 | } |
| 651 | |
Seth Hampson | cc7125f | 2018-02-02 08:46:16 -0800 | [diff] [blame] | 652 | void VideoSendStream::UpdateActiveSimulcastLayers( |
| 653 | const std::vector<bool> active_layers) { |
| 654 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 655 | RTC_LOG(LS_INFO) << "VideoSendStream::UpdateActiveSimulcastLayers"; |
| 656 | VideoSendStreamImpl* send_stream = send_stream_.get(); |
| 657 | worker_queue_->PostTask([this, send_stream, active_layers] { |
| 658 | send_stream->UpdateActiveSimulcastLayers(active_layers); |
| 659 | thread_sync_event_.Set(); |
| 660 | }); |
| 661 | |
| 662 | thread_sync_event_.Wait(rtc::Event::kForever); |
| 663 | } |
| 664 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 665 | void VideoSendStream::Start() { |
| 666 | RTC_DCHECK_RUN_ON(&thread_checker_); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 667 | RTC_LOG(LS_INFO) << "VideoSendStream::Start"; |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 668 | VideoSendStreamImpl* send_stream = send_stream_.get(); |
| 669 | worker_queue_->PostTask([this, send_stream] { |
| 670 | send_stream->Start(); |
| 671 | thread_sync_event_.Set(); |
| 672 | }); |
| 673 | |
| 674 | // It is expected that after VideoSendStream::Start has been called, incoming |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 675 | // frames are not dropped in VideoStreamEncoder. To ensure this, Start has to |
| 676 | // be synchronized. |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 677 | thread_sync_event_.Wait(rtc::Event::kForever); |
| 678 | } |
| 679 | |
| 680 | void VideoSendStream::Stop() { |
| 681 | RTC_DCHECK_RUN_ON(&thread_checker_); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 682 | RTC_LOG(LS_INFO) << "VideoSendStream::Stop"; |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 683 | VideoSendStreamImpl* send_stream = send_stream_.get(); |
| 684 | worker_queue_->PostTask([send_stream] { send_stream->Stop(); }); |
| 685 | } |
| 686 | |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 687 | void VideoSendStream::SetSource( |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 688 | rtc::VideoSourceInterface<webrtc::VideoFrame>* source, |
| 689 | const DegradationPreference& degradation_preference) { |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 690 | RTC_DCHECK_RUN_ON(&thread_checker_); |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 691 | video_stream_encoder_->SetSource(source, degradation_preference); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | void VideoSendStream::ReconfigureVideoEncoder(VideoEncoderConfig config) { |
perkj | fa10b55 | 2016-10-02 23:45:26 -0700 | [diff] [blame] | 695 | // TODO(perkj): Some test cases in VideoSendStreamTest call |
| 696 | // ReconfigureVideoEncoder from the network thread. |
| 697 | // RTC_DCHECK_RUN_ON(&thread_checker_); |
sprang | f24a064 | 2017-02-28 13:23:26 -0800 | [diff] [blame] | 698 | RTC_DCHECK(content_type_ == config.content_type); |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 699 | video_stream_encoder_->ConfigureEncoder(std::move(config), |
| 700 | config_.rtp.max_packet_size, |
| 701 | config_.rtp.nack.rtp_history_ms > 0); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | VideoSendStream::Stats VideoSendStream::GetStats() { |
| 705 | // TODO(perkj, solenberg): Some test cases in EndToEndTest call GetStats from |
| 706 | // a network thread. See comment in Call::GetStats(). |
| 707 | // RTC_DCHECK_RUN_ON(&thread_checker_); |
| 708 | return stats_proxy_.GetStats(); |
| 709 | } |
| 710 | |
Sebastian Jansson | a45c8da | 2018-01-16 10:55:29 +0100 | [diff] [blame] | 711 | rtc::Optional<float> VideoSendStream::GetPacingFactorOverride() const { |
| 712 | return send_stream_->configured_pacing_factor_; |
| 713 | } |
| 714 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 715 | void VideoSendStream::SignalNetworkState(NetworkState state) { |
| 716 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 717 | VideoSendStreamImpl* send_stream = send_stream_.get(); |
| 718 | worker_queue_->PostTask( |
| 719 | [send_stream, state] { send_stream->SignalNetworkState(state); }); |
| 720 | } |
| 721 | |
Ã…sa Persson | 4bece9a | 2017-10-06 10:04:04 +0200 | [diff] [blame] | 722 | void VideoSendStream::StopPermanentlyAndGetRtpStates( |
| 723 | VideoSendStream::RtpStateMap* rtp_state_map, |
| 724 | VideoSendStream::RtpPayloadStateMap* payload_state_map) { |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 725 | RTC_DCHECK_RUN_ON(&thread_checker_); |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 726 | video_stream_encoder_->Stop(); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 727 | send_stream_->DeRegisterProcessThread(); |
| 728 | worker_queue_->PostTask( |
| 729 | std::unique_ptr<rtc::QueuedTask>(new DestructAndGetRtpStateTask( |
Ã…sa Persson | 4bece9a | 2017-10-06 10:04:04 +0200 | [diff] [blame] | 730 | rtp_state_map, payload_state_map, std::move(send_stream_), |
| 731 | &thread_sync_event_))); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 732 | thread_sync_event_.Wait(rtc::Event::kForever); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 733 | } |
| 734 | |
nisse | 284542b | 2017-01-10 08:58:32 -0800 | [diff] [blame] | 735 | void VideoSendStream::SetTransportOverhead( |
| 736 | size_t transport_overhead_per_packet) { |
michaelt | 79e0588 | 2016-11-08 02:50:09 -0800 | [diff] [blame] | 737 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 738 | VideoSendStreamImpl* send_stream = send_stream_.get(); |
| 739 | worker_queue_->PostTask([send_stream, transport_overhead_per_packet] { |
| 740 | send_stream->SetTransportOverhead(transport_overhead_per_packet); |
| 741 | }); |
| 742 | } |
| 743 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 744 | bool VideoSendStream::DeliverRtcp(const uint8_t* packet, size_t length) { |
| 745 | // Called on a network thread. |
| 746 | return send_stream_->DeliverRtcp(packet, length); |
| 747 | } |
| 748 | |
palmkvist | e75f204 | 2016-09-28 06:19:48 -0700 | [diff] [blame] | 749 | void VideoSendStream::EnableEncodedFrameRecording( |
| 750 | const std::vector<rtc::PlatformFile>& files, |
| 751 | size_t byte_limit) { |
| 752 | send_stream_->EnableEncodedFrameRecording(files, byte_limit); |
| 753 | } |
| 754 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 755 | VideoSendStreamImpl::VideoSendStreamImpl( |
| 756 | SendStatisticsProxy* stats_proxy, |
| 757 | rtc::TaskQueue* worker_queue, |
| 758 | CallStats* call_stats, |
nisse | b8f9a32 | 2017-03-27 05:36:15 -0700 | [diff] [blame] | 759 | RtpTransportControllerSendInterface* transport, |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 760 | BitrateAllocator* bitrate_allocator, |
| 761 | SendDelayStats* send_delay_stats, |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 762 | VideoStreamEncoder* video_stream_encoder, |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 763 | RtcEventLog* event_log, |
| 764 | const VideoSendStream::Config* config, |
Per | 512ecb3 | 2016-09-23 15:52:06 +0200 | [diff] [blame] | 765 | int initial_encoder_max_bitrate, |
Seth Hampson | 24722b3 | 2017-12-22 09:36:42 -0800 | [diff] [blame] | 766 | double initial_encoder_bitrate_priority, |
sprang | 89c4a7e | 2017-06-30 13:27:40 -0700 | [diff] [blame] | 767 | std::map<uint32_t, RtpState> suspended_ssrcs, |
Ã…sa Persson | 4bece9a | 2017-10-06 10:04:04 +0200 | [diff] [blame] | 768 | std::map<uint32_t, RtpPayloadState> suspended_payload_states, |
Ying Wang | 3b790f3 | 2018-01-19 17:58:57 +0100 | [diff] [blame] | 769 | VideoEncoderConfig::ContentType content_type, |
| 770 | std::unique_ptr<FecController> fec_controller) |
sprang | c1b57a1 | 2017-02-28 08:50:47 -0800 | [diff] [blame] | 771 | : send_side_bwe_with_overhead_( |
| 772 | webrtc::field_trial::IsEnabled("WebRTC-SendSideBwe-WithOverhead")), |
elad.alon | 0fe1216 | 2017-01-31 05:48:37 -0800 | [diff] [blame] | 773 | stats_proxy_(stats_proxy), |
pbos@webrtc.org | 6488761 | 2013-11-14 08:58:14 +0000 | [diff] [blame] | 774 | config_(config), |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 775 | suspended_ssrcs_(std::move(suspended_ssrcs)), |
Ying Wang | 3b790f3 | 2018-01-19 17:58:57 +0100 | [diff] [blame] | 776 | fec_controller_(std::move(fec_controller)), |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 777 | module_process_thread_(nullptr), |
| 778 | worker_queue_(worker_queue), |
| 779 | check_encoder_activity_task_(nullptr), |
mflodman | e378702 | 2015-10-21 13:24:28 +0200 | [diff] [blame] | 780 | call_stats_(call_stats), |
nisse | b8f9a32 | 2017-03-27 05:36:15 -0700 | [diff] [blame] | 781 | transport_(transport), |
mflodman | 86aabb2 | 2016-03-11 15:44:32 +0100 | [diff] [blame] | 782 | bitrate_allocator_(bitrate_allocator), |
brandtr | 48d21a2 | 2017-05-30 02:32:12 -0700 | [diff] [blame] | 783 | flexfec_sender_(MaybeCreateFlexfecSender(*config_, suspended_ssrcs_)), |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 784 | max_padding_bitrate_(0), |
| 785 | encoder_min_bitrate_bps_(0), |
Per | 512ecb3 | 2016-09-23 15:52:06 +0200 | [diff] [blame] | 786 | encoder_max_bitrate_bps_(initial_encoder_max_bitrate), |
perkj | 9b522f8 | 2016-07-07 00:36:28 -0700 | [diff] [blame] | 787 | encoder_target_rate_bps_(0), |
Seth Hampson | 24722b3 | 2017-12-22 09:36:42 -0800 | [diff] [blame] | 788 | encoder_bitrate_priority_(initial_encoder_bitrate_priority), |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 789 | video_stream_encoder_(video_stream_encoder), |
perkj | 600246e | 2016-05-04 11:26:51 -0700 | [diff] [blame] | 790 | encoder_feedback_(Clock::GetRealTimeClock(), |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 791 | config_->rtp.ssrcs, |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 792 | video_stream_encoder), |
Sebastian Jansson | 8d9c540 | 2017-11-15 17:22:16 +0100 | [diff] [blame] | 793 | bandwidth_observer_(transport->send_side_cc()->GetBandwidthObserver()), |
nisse | b8f9a32 | 2017-03-27 05:36:15 -0700 | [diff] [blame] | 794 | rtp_rtcp_modules_(CreateRtpRtcpModules( |
| 795 | config_->send_transport, |
| 796 | &encoder_feedback_, |
Sebastian Jansson | 8d9c540 | 2017-11-15 17:22:16 +0100 | [diff] [blame] | 797 | bandwidth_observer_, |
nisse | b8f9a32 | 2017-03-27 05:36:15 -0700 | [diff] [blame] | 798 | transport, |
| 799 | call_stats_->rtcp_rtt_stats(), |
| 800 | flexfec_sender_.get(), |
| 801 | stats_proxy_, |
| 802 | send_delay_stats, |
| 803 | event_log, |
| 804 | transport->send_side_cc()->GetRetransmissionRateLimiter(), |
| 805 | this, |
sprang | 168794c | 2017-07-06 04:38:06 -0700 | [diff] [blame] | 806 | config_->rtp.ssrcs.size(), |
Jiawei Ou | 3587b83 | 2018-01-31 22:08:26 -0800 | [diff] [blame] | 807 | transport->keepalive_config(), |
| 808 | RtcpIntervalConfig{config_->rtcp.video_report_interval_ms, |
| 809 | config_->rtcp.audio_report_interval_ms})), |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 810 | payload_router_(rtp_rtcp_modules_, |
Ã…sa Persson | 4bece9a | 2017-10-06 10:04:04 +0200 | [diff] [blame] | 811 | config_->rtp.ssrcs, |
| 812 | config_->encoder_settings.payload_type, |
| 813 | suspended_payload_states), |
michaelt | a332877 | 2016-11-29 09:25:03 -0800 | [diff] [blame] | 814 | weak_ptr_factory_(this), |
| 815 | overhead_bytes_per_packet_(0), |
| 816 | transport_overhead_bytes_per_packet_(0) { |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 817 | RTC_DCHECK_RUN_ON(worker_queue_); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 818 | RTC_LOG(LS_INFO) << "VideoSendStreamInternal: " << config_->ToString(); |
perkj | 8ff860a | 2016-10-03 00:30:04 -0700 | [diff] [blame] | 819 | weak_ptr_ = weak_ptr_factory_.GetWeakPtr(); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 820 | module_process_thread_checker_.DetachFromThread(); |
Stefan Holmer | 58c664c | 2016-02-08 14:31:30 +0100 | [diff] [blame] | 821 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 822 | RTC_DCHECK(!config_->rtp.ssrcs.empty()); |
Stefan Holmer | 58c664c | 2016-02-08 14:31:30 +0100 | [diff] [blame] | 823 | RTC_DCHECK(call_stats_); |
nisse | b8f9a32 | 2017-03-27 05:36:15 -0700 | [diff] [blame] | 824 | RTC_DCHECK(transport_); |
| 825 | RTC_DCHECK(transport_->send_side_cc()); |
Seth Hampson | 24722b3 | 2017-12-22 09:36:42 -0800 | [diff] [blame] | 826 | RTC_DCHECK_GT(encoder_max_bitrate_bps_, 0); |
Sebastian Jansson | cabe383 | 2018-01-12 10:54:18 +0100 | [diff] [blame] | 827 | |
| 828 | RTC_CHECK(AlrExperimentSettings::MaxOneFieldTrialEnabled()); |
Erik Språng | 7c8cca3 | 2017-10-24 17:05:18 +0200 | [diff] [blame] | 829 | // If send-side BWE is enabled, check if we should apply updated probing and |
| 830 | // pacing settings. |
| 831 | if (TransportSeqNumExtensionConfigured(*config_)) { |
Sebastian Jansson | cabe383 | 2018-01-12 10:54:18 +0100 | [diff] [blame] | 832 | rtc::Optional<AlrExperimentSettings> alr_settings; |
Erik Språng | 7c8cca3 | 2017-10-24 17:05:18 +0200 | [diff] [blame] | 833 | if (content_type == VideoEncoderConfig::ContentType::kScreen) { |
Sebastian Jansson | cabe383 | 2018-01-12 10:54:18 +0100 | [diff] [blame] | 834 | alr_settings = AlrExperimentSettings::CreateFromFieldTrial( |
| 835 | AlrExperimentSettings::kScreenshareProbingBweExperimentName); |
Erik Språng | 7c8cca3 | 2017-10-24 17:05:18 +0200 | [diff] [blame] | 836 | } else { |
Sebastian Jansson | cabe383 | 2018-01-12 10:54:18 +0100 | [diff] [blame] | 837 | alr_settings = AlrExperimentSettings::CreateFromFieldTrial( |
| 838 | AlrExperimentSettings::kStrictPacingAndProbingExperimentName); |
Erik Språng | 7c8cca3 | 2017-10-24 17:05:18 +0200 | [diff] [blame] | 839 | } |
| 840 | if (alr_settings) { |
| 841 | transport->send_side_cc()->EnablePeriodicAlrProbing(true); |
Sebastian Jansson | 57daeb7 | 2018-02-05 17:15:09 +0100 | [diff] [blame] | 842 | transport->send_side_cc()->SetPacingFactor(alr_settings->pacing_factor); |
Sebastian Jansson | a45c8da | 2018-01-16 10:55:29 +0100 | [diff] [blame] | 843 | configured_pacing_factor_ = alr_settings->pacing_factor; |
Erik Språng | 7c8cca3 | 2017-10-24 17:05:18 +0200 | [diff] [blame] | 844 | transport->pacer()->SetQueueTimeLimit(alr_settings->max_paced_queue_time); |
| 845 | } |
sprang | 89c4a7e | 2017-06-30 13:27:40 -0700 | [diff] [blame] | 846 | } |
| 847 | |
| 848 | if (config_->periodic_alr_bandwidth_probing) { |
| 849 | transport->send_side_cc()->EnablePeriodicAlrProbing(true); |
| 850 | } |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 851 | |
Per | 83d0910 | 2016-04-15 14:59:13 +0200 | [diff] [blame] | 852 | // RTP/RTCP initialization. |
stefan | 16b0221 | 2017-01-27 07:12:16 -0800 | [diff] [blame] | 853 | |
| 854 | // We add the highest spatial layer first to ensure it'll be prioritized |
| 855 | // when sending padding, with the hope that the packet rate will be smaller, |
| 856 | // and that it's more important to protect than the lower layers. |
eladalon | 822ff2b | 2017-08-01 06:30:28 -0700 | [diff] [blame] | 857 | for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) { |
| 858 | constexpr bool remb_candidate = true; |
| 859 | transport->packet_router()->AddSendRtpModule(rtp_rtcp, remb_candidate); |
| 860 | } |
mflodman | 949c2f0 | 2015-10-16 02:31:11 -0700 | [diff] [blame] | 861 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 862 | for (size_t i = 0; i < config_->rtp.extensions.size(); ++i) { |
| 863 | const std::string& extension = config_->rtp.extensions[i].uri; |
| 864 | int id = config_->rtp.extensions[i].id; |
Peter Boström | 23914fe | 2015-03-31 15:08:04 +0200 | [diff] [blame] | 865 | // One-byte-extension local identifiers are in the range 1-14 inclusive. |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 866 | RTC_DCHECK_GE(id, 1); |
| 867 | RTC_DCHECK_LE(id, 14); |
Peter Boström | 9c01725 | 2016-02-26 16:26:20 +0100 | [diff] [blame] | 868 | RTC_DCHECK(RtpExtension::IsSupportedForVideo(extension)); |
| 869 | for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) { |
| 870 | RTC_CHECK_EQ(0, rtp_rtcp->RegisterSendRtpHeaderExtension( |
| 871 | StringToRtpExtensionType(extension), id)); |
pbos@webrtc.org | 2902328 | 2013-09-11 10:14:56 +0000 | [diff] [blame] | 872 | } |
| 873 | } |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 874 | |
Per | 83d0910 | 2016-04-15 14:59:13 +0200 | [diff] [blame] | 875 | ConfigureProtection(); |
pbos@webrtc.org | be9d2a4 | 2014-06-30 13:19:09 +0000 | [diff] [blame] | 876 | ConfigureSsrcs(); |
| 877 | |
Peter Boström | 723ead8 | 2016-02-22 15:14:01 +0100 | [diff] [blame] | 878 | // TODO(pbos): Should we set CNAME on all RTP modules? |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 879 | rtp_rtcp_modules_.front()->SetCNAME(config_->rtp.c_name.c_str()); |
michaelt | a332877 | 2016-11-29 09:25:03 -0800 | [diff] [blame] | 880 | |
Peter Boström | 723ead8 | 2016-02-22 15:14:01 +0100 | [diff] [blame] | 881 | for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) { |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 882 | rtp_rtcp->RegisterRtcpStatisticsCallback(stats_proxy_); |
| 883 | rtp_rtcp->RegisterSendChannelRtpStatisticsCallback(stats_proxy_); |
nisse | 284542b | 2017-01-10 08:58:32 -0800 | [diff] [blame] | 884 | rtp_rtcp->SetMaxRtpPacketSize(config_->rtp.max_packet_size); |
Peter Boström | 8b79b07 | 2016-02-26 16:31:37 +0100 | [diff] [blame] | 885 | rtp_rtcp->RegisterVideoSendPayload( |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 886 | config_->encoder_settings.payload_type, |
| 887 | config_->encoder_settings.payload_name.c_str()); |
Peter Boström | 723ead8 | 2016-02-22 15:14:01 +0100 | [diff] [blame] | 888 | } |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 889 | |
Ying Wang | 3b790f3 | 2018-01-19 17:58:57 +0100 | [diff] [blame] | 890 | fec_controller_->SetProtectionCallback(this); |
| 891 | // Signal congestion controller this object is ready for OnPacket* callbacks. |
Ying Wang | 8ed653d | 2018-01-23 16:37:22 +0100 | [diff] [blame] | 892 | if (fec_controller_->UseLossVectorMask()) { |
| 893 | transport_->send_side_cc()->RegisterPacketFeedbackObserver(this); |
| 894 | } |
Ying Wang | 3b790f3 | 2018-01-19 17:58:57 +0100 | [diff] [blame] | 895 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 896 | RTC_DCHECK(config_->encoder_settings.encoder); |
| 897 | RTC_DCHECK_GE(config_->encoder_settings.payload_type, 0); |
| 898 | RTC_DCHECK_LE(config_->encoder_settings.payload_type, 127); |
pbos@webrtc.org | fe1ef93 | 2013-10-21 10:34:43 +0000 | [diff] [blame] | 899 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 900 | video_stream_encoder_->SetStartBitrate( |
| 901 | bitrate_allocator_->GetStartBitrate(this)); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 902 | |
| 903 | // Only request rotation at the source when we positively know that the remote |
| 904 | // side doesn't support the rotation extension. This allows us to prepare the |
| 905 | // encoder in the expectation that rotation is supported - which is the common |
| 906 | // case. |
| 907 | bool rotation_applied = |
| 908 | std::find_if(config_->rtp.extensions.begin(), |
| 909 | config_->rtp.extensions.end(), |
| 910 | [](const RtpExtension& extension) { |
| 911 | return extension.uri == RtpExtension::kVideoRotationUri; |
| 912 | }) == config_->rtp.extensions.end(); |
| 913 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 914 | video_stream_encoder_->SetSink(this, rotation_applied); |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 915 | } |
| 916 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 917 | void VideoSendStreamImpl::RegisterProcessThread( |
| 918 | ProcessThread* module_process_thread) { |
| 919 | RTC_DCHECK_RUN_ON(&module_process_thread_checker_); |
| 920 | RTC_DCHECK(!module_process_thread_); |
| 921 | module_process_thread_ = module_process_thread; |
mflodman | 86aabb2 | 2016-03-11 15:44:32 +0100 | [diff] [blame] | 922 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 923 | for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) |
tommi | dea489f | 2017-03-03 03:20:24 -0800 | [diff] [blame] | 924 | module_process_thread_->RegisterModule(rtp_rtcp, RTC_FROM_HERE); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 925 | } |
Peter Boström | ca83525 | 2016-02-11 15:59:46 +0100 | [diff] [blame] | 926 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 927 | void VideoSendStreamImpl::DeRegisterProcessThread() { |
| 928 | RTC_DCHECK_RUN_ON(&module_process_thread_checker_); |
| 929 | for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) |
| 930 | module_process_thread_->DeRegisterModule(rtp_rtcp); |
| 931 | } |
Peter Boström | a4c7688 | 2016-03-03 16:29:02 +0100 | [diff] [blame] | 932 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 933 | VideoSendStreamImpl::~VideoSendStreamImpl() { |
| 934 | RTC_DCHECK_RUN_ON(worker_queue_); |
sprang | 1a646ee | 2016-12-01 06:34:11 -0800 | [diff] [blame] | 935 | RTC_DCHECK(!payload_router_.IsActive()) |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 936 | << "VideoSendStreamImpl::Stop not called"; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 937 | RTC_LOG(LS_INFO) << "~VideoSendStreamInternal: " << config_->ToString(); |
Ying Wang | 8ed653d | 2018-01-23 16:37:22 +0100 | [diff] [blame] | 938 | if (fec_controller_->UseLossVectorMask()) { |
| 939 | transport_->send_side_cc()->DeRegisterPacketFeedbackObserver(this); |
| 940 | } |
Per | 83d0910 | 2016-04-15 14:59:13 +0200 | [diff] [blame] | 941 | for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) { |
nisse | fdbfdc9 | 2017-03-31 05:44:52 -0700 | [diff] [blame] | 942 | transport_->packet_router()->RemoveSendRtpModule(rtp_rtcp); |
Per | 83d0910 | 2016-04-15 14:59:13 +0200 | [diff] [blame] | 943 | delete rtp_rtcp; |
| 944 | } |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 945 | } |
| 946 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 947 | bool VideoSendStreamImpl::DeliverRtcp(const uint8_t* packet, size_t length) { |
| 948 | // Runs on a network thread. |
| 949 | RTC_DCHECK(!worker_queue_->IsCurrent()); |
pbos | 1ba8d39 | 2016-05-01 20:18:34 -0700 | [diff] [blame] | 950 | for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) |
| 951 | rtp_rtcp->IncomingRtcpPacket(packet, length); |
| 952 | return true; |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 953 | } |
| 954 | |
Seth Hampson | cc7125f | 2018-02-02 08:46:16 -0800 | [diff] [blame] | 955 | void VideoSendStreamImpl::UpdateActiveSimulcastLayers( |
| 956 | const std::vector<bool> active_layers) { |
| 957 | RTC_DCHECK_RUN_ON(worker_queue_); |
| 958 | RTC_DCHECK_EQ(rtp_rtcp_modules_.size(), active_layers.size()); |
| 959 | RTC_LOG(LS_INFO) << "VideoSendStream::UpdateActiveSimulcastLayers"; |
| 960 | bool previously_active = payload_router_.IsActive(); |
| 961 | payload_router_.SetActiveModules(active_layers); |
| 962 | if (!payload_router_.IsActive() && previously_active) { |
| 963 | // Payload router switched from active to inactive. |
| 964 | StopVideoSendStream(); |
| 965 | } else if (payload_router_.IsActive() && !previously_active) { |
| 966 | // Payload router switched from inactive to active. |
| 967 | StartupVideoSendStream(); |
| 968 | } |
| 969 | } |
| 970 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 971 | void VideoSendStreamImpl::Start() { |
| 972 | RTC_DCHECK_RUN_ON(worker_queue_); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 973 | RTC_LOG(LS_INFO) << "VideoSendStream::Start"; |
sprang | 1a646ee | 2016-12-01 06:34:11 -0800 | [diff] [blame] | 974 | if (payload_router_.IsActive()) |
Peter Boström | 0a9fc05 | 2016-03-02 16:24:10 +0100 | [diff] [blame] | 975 | return; |
Peter Boström | dabc944 | 2016-04-11 11:45:14 +0200 | [diff] [blame] | 976 | TRACE_EVENT_INSTANT0("webrtc", "VideoSendStream::Start"); |
sprang | 1a646ee | 2016-12-01 06:34:11 -0800 | [diff] [blame] | 977 | payload_router_.SetActive(true); |
Seth Hampson | cc7125f | 2018-02-02 08:46:16 -0800 | [diff] [blame] | 978 | StartupVideoSendStream(); |
| 979 | } |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 980 | |
Seth Hampson | cc7125f | 2018-02-02 08:46:16 -0800 | [diff] [blame] | 981 | void VideoSendStreamImpl::StartupVideoSendStream() { |
| 982 | RTC_DCHECK_RUN_ON(worker_queue_); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 983 | bitrate_allocator_->AddObserver( |
| 984 | this, encoder_min_bitrate_bps_, encoder_max_bitrate_bps_, |
Alex Narest | b3944f0 | 2017-10-13 14:56:18 +0200 | [diff] [blame] | 985 | max_padding_bitrate_, !config_->suspend_below_min_bitrate, |
Seth Hampson | 24722b3 | 2017-12-22 09:36:42 -0800 | [diff] [blame] | 986 | config_->track_id, encoder_bitrate_priority_); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 987 | // Start monitoring encoder activity. |
perkj | 57c21f9 | 2016-06-17 07:27:16 -0700 | [diff] [blame] | 988 | { |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 989 | rtc::CritScope lock(&encoder_activity_crit_sect_); |
| 990 | RTC_DCHECK(!check_encoder_activity_task_); |
perkj | 8ff860a | 2016-10-03 00:30:04 -0700 | [diff] [blame] | 991 | check_encoder_activity_task_ = new CheckEncoderActivityTask(weak_ptr_); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 992 | worker_queue_->PostDelayedTask( |
| 993 | std::unique_ptr<rtc::QueuedTask>(check_encoder_activity_task_), |
| 994 | CheckEncoderActivityTask::kEncoderTimeOutMs); |
perkj | 57c21f9 | 2016-06-17 07:27:16 -0700 | [diff] [blame] | 995 | } |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 996 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 997 | video_stream_encoder_->SendKeyFrame(); |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 998 | } |
| 999 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 1000 | void VideoSendStreamImpl::Stop() { |
| 1001 | RTC_DCHECK_RUN_ON(worker_queue_); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1002 | RTC_LOG(LS_INFO) << "VideoSendStream::Stop"; |
sprang | 1a646ee | 2016-12-01 06:34:11 -0800 | [diff] [blame] | 1003 | if (!payload_router_.IsActive()) |
Peter Boström | 0a9fc05 | 2016-03-02 16:24:10 +0100 | [diff] [blame] | 1004 | return; |
Peter Boström | dabc944 | 2016-04-11 11:45:14 +0200 | [diff] [blame] | 1005 | TRACE_EVENT_INSTANT0("webrtc", "VideoSendStream::Stop"); |
sprang | 1a646ee | 2016-12-01 06:34:11 -0800 | [diff] [blame] | 1006 | payload_router_.SetActive(false); |
Seth Hampson | cc7125f | 2018-02-02 08:46:16 -0800 | [diff] [blame] | 1007 | StopVideoSendStream(); |
| 1008 | } |
| 1009 | |
| 1010 | void VideoSendStreamImpl::StopVideoSendStream() { |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 1011 | bitrate_allocator_->RemoveObserver(this); |
perkj | 57c21f9 | 2016-06-17 07:27:16 -0700 | [diff] [blame] | 1012 | { |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 1013 | rtc::CritScope lock(&encoder_activity_crit_sect_); |
| 1014 | check_encoder_activity_task_->Stop(); |
| 1015 | check_encoder_activity_task_ = nullptr; |
perkj | 57c21f9 | 2016-06-17 07:27:16 -0700 | [diff] [blame] | 1016 | } |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1017 | video_stream_encoder_->OnBitrateUpdated(0, 0, 0); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 1018 | stats_proxy_->OnSetEncoderTargetRate(0); |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 1019 | } |
| 1020 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 1021 | void VideoSendStreamImpl::SignalEncoderTimedOut() { |
| 1022 | RTC_DCHECK_RUN_ON(worker_queue_); |
| 1023 | // If the encoder has not produced anything the last kEncoderTimeOutMs and it |
| 1024 | // is supposed to, deregister as BitrateAllocatorObserver. This can happen |
| 1025 | // if a camera stops producing frames. |
| 1026 | if (encoder_target_rate_bps_ > 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1027 | RTC_LOG(LS_INFO) << "SignalEncoderTimedOut, Encoder timed out."; |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 1028 | bitrate_allocator_->RemoveObserver(this); |
Peter Boström | a4c7688 | 2016-03-03 16:29:02 +0100 | [diff] [blame] | 1029 | } |
| 1030 | } |
| 1031 | |
sprang | 1a646ee | 2016-12-01 06:34:11 -0800 | [diff] [blame] | 1032 | void VideoSendStreamImpl::OnBitrateAllocationUpdated( |
| 1033 | const BitrateAllocation& allocation) { |
| 1034 | payload_router_.OnBitrateAllocationUpdated(allocation); |
| 1035 | } |
| 1036 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 1037 | void VideoSendStreamImpl::SignalEncoderActive() { |
| 1038 | RTC_DCHECK_RUN_ON(worker_queue_); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1039 | RTC_LOG(LS_INFO) << "SignalEncoderActive, Encoder is active."; |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 1040 | bitrate_allocator_->AddObserver( |
| 1041 | this, encoder_min_bitrate_bps_, encoder_max_bitrate_bps_, |
Alex Narest | b3944f0 | 2017-10-13 14:56:18 +0200 | [diff] [blame] | 1042 | max_padding_bitrate_, !config_->suspend_below_min_bitrate, |
Seth Hampson | 24722b3 | 2017-12-22 09:36:42 -0800 | [diff] [blame] | 1043 | config_->track_id, encoder_bitrate_priority_); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 1044 | } |
| 1045 | |
Per | 512ecb3 | 2016-09-23 15:52:06 +0200 | [diff] [blame] | 1046 | void VideoSendStreamImpl::OnEncoderConfigurationChanged( |
| 1047 | std::vector<VideoStream> streams, |
| 1048 | int min_transmit_bitrate_bps) { |
| 1049 | if (!worker_queue_->IsCurrent()) { |
Per | 512ecb3 | 2016-09-23 15:52:06 +0200 | [diff] [blame] | 1050 | worker_queue_->PostTask( |
| 1051 | std::unique_ptr<rtc::QueuedTask>(new EncoderReconfiguredTask( |
perkj | 8ff860a | 2016-10-03 00:30:04 -0700 | [diff] [blame] | 1052 | weak_ptr_, std::move(streams), min_transmit_bitrate_bps))); |
Per | 512ecb3 | 2016-09-23 15:52:06 +0200 | [diff] [blame] | 1053 | return; |
| 1054 | } |
| 1055 | RTC_DCHECK_GE(config_->rtp.ssrcs.size(), streams.size()); |
| 1056 | TRACE_EVENT0("webrtc", "VideoSendStream::OnEncoderConfigurationChanged"); |
| 1057 | RTC_DCHECK_GE(config_->rtp.ssrcs.size(), streams.size()); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 1058 | RTC_DCHECK_RUN_ON(worker_queue_); |
| 1059 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 1060 | encoder_min_bitrate_bps_ = |
Ã…sa Persson | 45bbc8a | 2017-11-13 10:16:47 +0100 | [diff] [blame] | 1061 | std::max(streams[0].min_bitrate_bps, GetEncoderMinBitrateBps()); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 1062 | encoder_max_bitrate_bps_ = 0; |
Seth Hampson | 24722b3 | 2017-12-22 09:36:42 -0800 | [diff] [blame] | 1063 | double stream_bitrate_priority_sum = 0; |
| 1064 | for (const auto& stream : streams) { |
Seth Hampson | 46e31ba | 2018-01-18 10:39:54 -0800 | [diff] [blame] | 1065 | // We don't want to allocate more bitrate than needed to inactive streams. |
| 1066 | encoder_max_bitrate_bps_ += stream.active ? stream.max_bitrate_bps : 0; |
Seth Hampson | 24722b3 | 2017-12-22 09:36:42 -0800 | [diff] [blame] | 1067 | if (stream.bitrate_priority) { |
| 1068 | RTC_DCHECK_GT(*stream.bitrate_priority, 0); |
| 1069 | stream_bitrate_priority_sum += *stream.bitrate_priority; |
| 1070 | } |
| 1071 | } |
| 1072 | RTC_DCHECK_GT(stream_bitrate_priority_sum, 0); |
| 1073 | encoder_bitrate_priority_ = stream_bitrate_priority_sum; |
Seth Hampson | 46e31ba | 2018-01-18 10:39:54 -0800 | [diff] [blame] | 1074 | encoder_max_bitrate_bps_ = |
| 1075 | std::max(static_cast<uint32_t>(encoder_min_bitrate_bps_), |
| 1076 | encoder_max_bitrate_bps_); |
Per | 512ecb3 | 2016-09-23 15:52:06 +0200 | [diff] [blame] | 1077 | max_padding_bitrate_ = CalculateMaxPadBitrateBps( |
| 1078 | streams, min_transmit_bitrate_bps, config_->suspend_below_min_bitrate); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 1079 | |
| 1080 | // Clear stats for disabled layers. |
Per | 512ecb3 | 2016-09-23 15:52:06 +0200 | [diff] [blame] | 1081 | for (size_t i = streams.size(); i < config_->rtp.ssrcs.size(); ++i) { |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 1082 | stats_proxy_->OnInactiveSsrc(config_->rtp.ssrcs[i]); |
pbos@webrtc.org | bbe0a85 | 2014-09-19 12:30:25 +0000 | [diff] [blame] | 1083 | } |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 1084 | |
| 1085 | size_t number_of_temporal_layers = |
Per | 512ecb3 | 2016-09-23 15:52:06 +0200 | [diff] [blame] | 1086 | streams.back().temporal_layer_thresholds_bps.size() + 1; |
Ying Wang | 3b790f3 | 2018-01-19 17:58:57 +0100 | [diff] [blame] | 1087 | fec_controller_->SetEncodingData(streams[0].width, streams[0].height, |
| 1088 | number_of_temporal_layers, |
| 1089 | config_->rtp.max_packet_size); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 1090 | |
sprang | 1a646ee | 2016-12-01 06:34:11 -0800 | [diff] [blame] | 1091 | if (payload_router_.IsActive()) { |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 1092 | // The send stream is started already. Update the allocator with new bitrate |
| 1093 | // limits. |
| 1094 | bitrate_allocator_->AddObserver( |
| 1095 | this, encoder_min_bitrate_bps_, encoder_max_bitrate_bps_, |
Alex Narest | b3944f0 | 2017-10-13 14:56:18 +0200 | [diff] [blame] | 1096 | max_padding_bitrate_, !config_->suspend_below_min_bitrate, |
Seth Hampson | 24722b3 | 2017-12-22 09:36:42 -0800 | [diff] [blame] | 1097 | config_->track_id, encoder_bitrate_priority_); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 1098 | } |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 1099 | } |
| 1100 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 1101 | EncodedImageCallback::Result VideoSendStreamImpl::OnEncodedImage( |
Sergey Ulanov | 525df3f | 2016-08-02 17:46:41 -0700 | [diff] [blame] | 1102 | const EncodedImage& encoded_image, |
| 1103 | const CodecSpecificInfo* codec_specific_info, |
| 1104 | const RTPFragmentationHeader* fragmentation) { |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 1105 | // Encoded is called on whatever thread the real encoder implementation run |
| 1106 | // on. In the case of hardware encoders, there might be several encoders |
| 1107 | // running in parallel on different threads. |
ilnik | cb8c146 | 2017-03-09 09:23:30 -0800 | [diff] [blame] | 1108 | size_t simulcast_idx = 0; |
| 1109 | if (codec_specific_info->codecType == kVideoCodecVP8) { |
| 1110 | simulcast_idx = codec_specific_info->codecSpecific.VP8.simulcastIdx; |
| 1111 | } |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 1112 | if (config_->post_encode_callback) { |
ilnik | cb8c146 | 2017-03-09 09:23:30 -0800 | [diff] [blame] | 1113 | config_->post_encode_callback->EncodedFrameCallback(EncodedFrame( |
| 1114 | encoded_image._buffer, encoded_image._length, encoded_image._frameType, |
| 1115 | simulcast_idx, encoded_image._timeStamp)); |
sergeyu | 37ad337 | 2016-06-14 15:29:37 -0700 | [diff] [blame] | 1116 | } |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 1117 | { |
| 1118 | rtc::CritScope lock(&encoder_activity_crit_sect_); |
| 1119 | if (check_encoder_activity_task_) |
| 1120 | check_encoder_activity_task_->UpdateEncoderActivity(); |
| 1121 | } |
sergeyu | 37ad337 | 2016-06-14 15:29:37 -0700 | [diff] [blame] | 1122 | |
Ying Wang | 3b790f3 | 2018-01-19 17:58:57 +0100 | [diff] [blame] | 1123 | fec_controller_->UpdateWithEncodedData(encoded_image); |
Sergey Ulanov | 525df3f | 2016-08-02 17:46:41 -0700 | [diff] [blame] | 1124 | EncodedImageCallback::Result result = payload_router_.OnEncodedImage( |
perkj | 600246e | 2016-05-04 11:26:51 -0700 | [diff] [blame] | 1125 | encoded_image, codec_specific_info, fragmentation); |
| 1126 | |
palmkvist | e75f204 | 2016-09-28 06:19:48 -0700 | [diff] [blame] | 1127 | RTC_DCHECK(codec_specific_info); |
| 1128 | |
| 1129 | int layer = codec_specific_info->codecType == kVideoCodecVP8 |
| 1130 | ? codec_specific_info->codecSpecific.VP8.simulcastIdx |
| 1131 | : 0; |
| 1132 | { |
| 1133 | rtc::CritScope lock(&ivf_writers_crit_); |
| 1134 | if (file_writers_[layer].get()) { |
| 1135 | bool ok = file_writers_[layer]->WriteFrame( |
| 1136 | encoded_image, codec_specific_info->codecType); |
perkj | 600246e | 2016-05-04 11:26:51 -0700 | [diff] [blame] | 1137 | RTC_DCHECK(ok); |
| 1138 | } |
| 1139 | } |
| 1140 | |
Sergey Ulanov | 525df3f | 2016-08-02 17:46:41 -0700 | [diff] [blame] | 1141 | return result; |
perkj | bc75d97 | 2016-05-02 06:31:25 -0700 | [diff] [blame] | 1142 | } |
| 1143 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 1144 | void VideoSendStreamImpl::ConfigureProtection() { |
| 1145 | RTC_DCHECK_RUN_ON(worker_queue_); |
brandtr | e6f98c7 | 2016-11-11 03:28:30 -0800 | [diff] [blame] | 1146 | |
brandtr | e950cad | 2016-11-15 05:25:41 -0800 | [diff] [blame] | 1147 | // Consistency of FlexFEC parameters is checked in MaybeCreateFlexfecSender. |
| 1148 | const bool flexfec_enabled = (flexfec_sender_ != nullptr); |
| 1149 | |
| 1150 | // Consistency of NACK and RED+ULPFEC parameters is checked in this function. |
brandtr | e6f98c7 | 2016-11-11 03:28:30 -0800 | [diff] [blame] | 1151 | const bool nack_enabled = config_->rtp.nack.rtp_history_ms > 0; |
| 1152 | int red_payload_type = config_->rtp.ulpfec.red_payload_type; |
brandtr | f1bb476 | 2016-11-07 03:05:06 -0800 | [diff] [blame] | 1153 | int ulpfec_payload_type = config_->rtp.ulpfec.ulpfec_payload_type; |
brandtr | e6f98c7 | 2016-11-11 03:28:30 -0800 | [diff] [blame] | 1154 | |
| 1155 | // Shorthands. |
| 1156 | auto IsRedEnabled = [&]() { return red_payload_type >= 0; }; |
brandtr | e950cad | 2016-11-15 05:25:41 -0800 | [diff] [blame] | 1157 | auto DisableRed = [&]() { red_payload_type = -1; }; |
brandtr | e6f98c7 | 2016-11-11 03:28:30 -0800 | [diff] [blame] | 1158 | auto IsUlpfecEnabled = [&]() { return ulpfec_payload_type >= 0; }; |
| 1159 | auto DisableUlpfec = [&]() { ulpfec_payload_type = -1; }; |
| 1160 | |
stefan | 60e10c7 | 2017-08-23 10:40:00 -0700 | [diff] [blame] | 1161 | if (webrtc::field_trial::IsEnabled("WebRTC-DisableUlpFecExperiment")) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1162 | RTC_LOG(LS_INFO) << "Experiment to disable sending ULPFEC is enabled."; |
stefan | 60e10c7 | 2017-08-23 10:40:00 -0700 | [diff] [blame] | 1163 | DisableUlpfec(); |
| 1164 | } |
| 1165 | |
brandtr | e950cad | 2016-11-15 05:25:41 -0800 | [diff] [blame] | 1166 | // If enabled, FlexFEC takes priority over RED+ULPFEC. |
| 1167 | if (flexfec_enabled) { |
| 1168 | // We can safely disable RED here, because if the remote supports FlexFEC, |
| 1169 | // we know that it has a receiver without the RED/RTX workaround. |
| 1170 | // See http://crbug.com/webrtc/6650 for more information. |
| 1171 | if (IsRedEnabled()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1172 | RTC_LOG(LS_INFO) << "Both FlexFEC and RED are configured. Disabling RED."; |
brandtr | e950cad | 2016-11-15 05:25:41 -0800 | [diff] [blame] | 1173 | DisableRed(); |
| 1174 | } |
| 1175 | if (IsUlpfecEnabled()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1176 | RTC_LOG(LS_INFO) |
brandtr | e950cad | 2016-11-15 05:25:41 -0800 | [diff] [blame] | 1177 | << "Both FlexFEC and ULPFEC are configured. Disabling ULPFEC."; |
| 1178 | DisableUlpfec(); |
| 1179 | } |
| 1180 | } |
| 1181 | |
Per | 83d0910 | 2016-04-15 14:59:13 +0200 | [diff] [blame] | 1182 | // Payload types without picture ID cannot determine that a stream is complete |
brandtr | e6f98c7 | 2016-11-11 03:28:30 -0800 | [diff] [blame] | 1183 | // without retransmitting FEC, so using ULPFEC + NACK for H.264 (for instance) |
| 1184 | // is a waste of bandwidth since FEC packets still have to be transmitted. |
| 1185 | // Note that this is not the case with FlexFEC. |
| 1186 | if (nack_enabled && IsUlpfecEnabled() && |
Per | 83d0910 | 2016-04-15 14:59:13 +0200 | [diff] [blame] | 1187 | !PayloadTypeSupportsSkippingFecPackets( |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 1188 | config_->encoder_settings.payload_name)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1189 | RTC_LOG(LS_WARNING) |
brandtr | e6f98c7 | 2016-11-11 03:28:30 -0800 | [diff] [blame] | 1190 | << "Transmitting payload type without picture ID using " |
| 1191 | "NACK+ULPFEC is a waste of bandwidth since ULPFEC packets " |
| 1192 | "also have to be retransmitted. Disabling ULPFEC."; |
| 1193 | DisableUlpfec(); |
Per | 83d0910 | 2016-04-15 14:59:13 +0200 | [diff] [blame] | 1194 | } |
| 1195 | |
brandtr | e6f98c7 | 2016-11-11 03:28:30 -0800 | [diff] [blame] | 1196 | // Verify payload types. |
brandtr | d804895 | 2016-11-07 02:08:51 -0800 | [diff] [blame] | 1197 | // |
brandtr | e6f98c7 | 2016-11-11 03:28:30 -0800 | [diff] [blame] | 1198 | // Due to how old receivers work, we need to always send RED if it has been |
| 1199 | // negotiated. This is a remnant of an old RED/RTX workaround, see |
| 1200 | // https://codereview.webrtc.org/2469093003. |
| 1201 | // TODO(brandtr): This change went into M56, so we can remove it in ~M59. |
| 1202 | // At that time, we can disable RED whenever ULPFEC is disabled, as there is |
| 1203 | // no point in using RED without ULPFEC. |
| 1204 | if (IsRedEnabled()) { |
brandtr | f1bb476 | 2016-11-07 03:05:06 -0800 | [diff] [blame] | 1205 | RTC_DCHECK_GE(red_payload_type, 0); |
| 1206 | RTC_DCHECK_LE(red_payload_type, 127); |
stefan | 8f4c77f | 2016-06-03 00:16:45 -0700 | [diff] [blame] | 1207 | } |
brandtr | e6f98c7 | 2016-11-11 03:28:30 -0800 | [diff] [blame] | 1208 | if (IsUlpfecEnabled()) { |
brandtr | f1bb476 | 2016-11-07 03:05:06 -0800 | [diff] [blame] | 1209 | RTC_DCHECK_GE(ulpfec_payload_type, 0); |
| 1210 | RTC_DCHECK_LE(ulpfec_payload_type, 127); |
brandtr | e6f98c7 | 2016-11-11 03:28:30 -0800 | [diff] [blame] | 1211 | if (!IsRedEnabled()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1212 | RTC_LOG(LS_WARNING) |
brandtr | e6f98c7 | 2016-11-11 03:28:30 -0800 | [diff] [blame] | 1213 | << "ULPFEC is enabled but RED is disabled. Disabling ULPFEC."; |
| 1214 | DisableUlpfec(); |
| 1215 | } |
Per | 83d0910 | 2016-04-15 14:59:13 +0200 | [diff] [blame] | 1216 | } |
| 1217 | |
| 1218 | for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) { |
| 1219 | // Set NACK. |
| 1220 | rtp_rtcp->SetStorePacketsStatus( |
nisse | 10daf86 | 2016-12-08 06:24:28 -0800 | [diff] [blame] | 1221 | true, |
Per | 83d0910 | 2016-04-15 14:59:13 +0200 | [diff] [blame] | 1222 | kMinSendSidePacketHistorySize); |
brandtr | e6f98c7 | 2016-11-11 03:28:30 -0800 | [diff] [blame] | 1223 | // Set RED/ULPFEC information. |
stefan | 60e10c7 | 2017-08-23 10:40:00 -0700 | [diff] [blame] | 1224 | rtp_rtcp->SetUlpfecConfig(red_payload_type, ulpfec_payload_type); |
Per | 83d0910 | 2016-04-15 14:59:13 +0200 | [diff] [blame] | 1225 | } |
| 1226 | |
brandtr | e950cad | 2016-11-15 05:25:41 -0800 | [diff] [blame] | 1227 | // Currently, both ULPFEC and FlexFEC use the same FEC rate calculation logic, |
| 1228 | // so enable that logic if either of those FEC schemes are enabled. |
Ying Wang | 3b790f3 | 2018-01-19 17:58:57 +0100 | [diff] [blame] | 1229 | fec_controller_->SetProtectionMethod(flexfec_enabled || IsUlpfecEnabled(), |
| 1230 | nack_enabled); |
Per | 83d0910 | 2016-04-15 14:59:13 +0200 | [diff] [blame] | 1231 | } |
| 1232 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 1233 | void VideoSendStreamImpl::ConfigureSsrcs() { |
| 1234 | RTC_DCHECK_RUN_ON(worker_queue_); |
Peter Boström | 723ead8 | 2016-02-22 15:14:01 +0100 | [diff] [blame] | 1235 | // Configure regular SSRCs. |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 1236 | for (size_t i = 0; i < config_->rtp.ssrcs.size(); ++i) { |
| 1237 | uint32_t ssrc = config_->rtp.ssrcs[i]; |
Peter Boström | 723ead8 | 2016-02-22 15:14:01 +0100 | [diff] [blame] | 1238 | RtpRtcp* const rtp_rtcp = rtp_rtcp_modules_[i]; |
| 1239 | rtp_rtcp->SetSSRC(ssrc); |
| 1240 | |
| 1241 | // Restore RTP state if previous existed. |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 1242 | VideoSendStream::RtpStateMap::iterator it = suspended_ssrcs_.find(ssrc); |
pbos@webrtc.org | 2bb1bda | 2014-07-07 13:06:48 +0000 | [diff] [blame] | 1243 | if (it != suspended_ssrcs_.end()) |
Per | 83d0910 | 2016-04-15 14:59:13 +0200 | [diff] [blame] | 1244 | rtp_rtcp->SetRtpState(it->second); |
pbos@webrtc.org | be9d2a4 | 2014-06-30 13:19:09 +0000 | [diff] [blame] | 1245 | } |
| 1246 | |
Peter Boström | 723ead8 | 2016-02-22 15:14:01 +0100 | [diff] [blame] | 1247 | // Set up RTX if available. |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 1248 | if (config_->rtp.rtx.ssrcs.empty()) |
pbos@webrtc.org | be9d2a4 | 2014-06-30 13:19:09 +0000 | [diff] [blame] | 1249 | return; |
pbos@webrtc.org | be9d2a4 | 2014-06-30 13:19:09 +0000 | [diff] [blame] | 1250 | |
Peter Boström | 723ead8 | 2016-02-22 15:14:01 +0100 | [diff] [blame] | 1251 | // Configure RTX SSRCs. |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 1252 | RTC_DCHECK_EQ(config_->rtp.rtx.ssrcs.size(), config_->rtp.ssrcs.size()); |
| 1253 | for (size_t i = 0; i < config_->rtp.rtx.ssrcs.size(); ++i) { |
| 1254 | uint32_t ssrc = config_->rtp.rtx.ssrcs[i]; |
Peter Boström | 723ead8 | 2016-02-22 15:14:01 +0100 | [diff] [blame] | 1255 | RtpRtcp* const rtp_rtcp = rtp_rtcp_modules_[i]; |
| 1256 | rtp_rtcp->SetRtxSsrc(ssrc); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 1257 | VideoSendStream::RtpStateMap::iterator it = suspended_ssrcs_.find(ssrc); |
pbos@webrtc.org | 2bb1bda | 2014-07-07 13:06:48 +0000 | [diff] [blame] | 1258 | if (it != suspended_ssrcs_.end()) |
Per | 83d0910 | 2016-04-15 14:59:13 +0200 | [diff] [blame] | 1259 | rtp_rtcp->SetRtxState(it->second); |
pbos@webrtc.org | be9d2a4 | 2014-06-30 13:19:09 +0000 | [diff] [blame] | 1260 | } |
| 1261 | |
Peter Boström | 723ead8 | 2016-02-22 15:14:01 +0100 | [diff] [blame] | 1262 | // Configure RTX payload types. |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 1263 | RTC_DCHECK_GE(config_->rtp.rtx.payload_type, 0); |
Peter Boström | 723ead8 | 2016-02-22 15:14:01 +0100 | [diff] [blame] | 1264 | for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) { |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 1265 | rtp_rtcp->SetRtxSendPayloadType(config_->rtp.rtx.payload_type, |
| 1266 | config_->encoder_settings.payload_type); |
Peter Boström | 723ead8 | 2016-02-22 15:14:01 +0100 | [diff] [blame] | 1267 | rtp_rtcp->SetRtxSendStatus(kRtxRetransmitted | kRtxRedundantPayloads); |
| 1268 | } |
brandtr | b5f2c3f | 2016-10-04 23:28:39 -0700 | [diff] [blame] | 1269 | if (config_->rtp.ulpfec.red_payload_type != -1 && |
| 1270 | config_->rtp.ulpfec.red_rtx_payload_type != -1) { |
Peter Boström | 723ead8 | 2016-02-22 15:14:01 +0100 | [diff] [blame] | 1271 | for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) { |
brandtr | b5f2c3f | 2016-10-04 23:28:39 -0700 | [diff] [blame] | 1272 | rtp_rtcp->SetRtxSendPayloadType(config_->rtp.ulpfec.red_rtx_payload_type, |
| 1273 | config_->rtp.ulpfec.red_payload_type); |
Peter Boström | 723ead8 | 2016-02-22 15:14:01 +0100 | [diff] [blame] | 1274 | } |
Stefan Holmer | 1088001 | 2016-02-03 13:29:59 +0100 | [diff] [blame] | 1275 | } |
pbos@webrtc.org | be9d2a4 | 2014-06-30 13:19:09 +0000 | [diff] [blame] | 1276 | } |
| 1277 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 1278 | std::map<uint32_t, RtpState> VideoSendStreamImpl::GetRtpStates() const { |
| 1279 | RTC_DCHECK_RUN_ON(worker_queue_); |
pbos@webrtc.org | 2bb1bda | 2014-07-07 13:06:48 +0000 | [diff] [blame] | 1280 | std::map<uint32_t, RtpState> rtp_states; |
brandtr | 48d21a2 | 2017-05-30 02:32:12 -0700 | [diff] [blame] | 1281 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 1282 | for (size_t i = 0; i < config_->rtp.ssrcs.size(); ++i) { |
| 1283 | uint32_t ssrc = config_->rtp.ssrcs[i]; |
Per | 83d0910 | 2016-04-15 14:59:13 +0200 | [diff] [blame] | 1284 | RTC_DCHECK_EQ(ssrc, rtp_rtcp_modules_[i]->SSRC()); |
| 1285 | rtp_states[ssrc] = rtp_rtcp_modules_[i]->GetRtpState(); |
pbos@webrtc.org | 2bb1bda | 2014-07-07 13:06:48 +0000 | [diff] [blame] | 1286 | } |
| 1287 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 1288 | for (size_t i = 0; i < config_->rtp.rtx.ssrcs.size(); ++i) { |
| 1289 | uint32_t ssrc = config_->rtp.rtx.ssrcs[i]; |
Per | 83d0910 | 2016-04-15 14:59:13 +0200 | [diff] [blame] | 1290 | rtp_states[ssrc] = rtp_rtcp_modules_[i]->GetRtxState(); |
pbos@webrtc.org | 2bb1bda | 2014-07-07 13:06:48 +0000 | [diff] [blame] | 1291 | } |
| 1292 | |
brandtr | 48d21a2 | 2017-05-30 02:32:12 -0700 | [diff] [blame] | 1293 | if (flexfec_sender_) { |
| 1294 | uint32_t ssrc = config_->rtp.flexfec.ssrc; |
| 1295 | rtp_states[ssrc] = flexfec_sender_->GetRtpState(); |
| 1296 | } |
| 1297 | |
pbos@webrtc.org | 2bb1bda | 2014-07-07 13:06:48 +0000 | [diff] [blame] | 1298 | return rtp_states; |
| 1299 | } |
| 1300 | |
Ã…sa Persson | 4bece9a | 2017-10-06 10:04:04 +0200 | [diff] [blame] | 1301 | std::map<uint32_t, RtpPayloadState> VideoSendStreamImpl::GetRtpPayloadStates() |
| 1302 | const { |
| 1303 | RTC_DCHECK_RUN_ON(worker_queue_); |
| 1304 | return payload_router_.GetRtpPayloadStates(); |
| 1305 | } |
| 1306 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 1307 | void VideoSendStreamImpl::SignalNetworkState(NetworkState state) { |
| 1308 | RTC_DCHECK_RUN_ON(worker_queue_); |
perkj | fea9309 | 2016-05-14 00:58:48 -0700 | [diff] [blame] | 1309 | for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) { |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 1310 | rtp_rtcp->SetRTCPStatus(state == kNetworkUp ? config_->rtp.rtcp_mode |
perkj | fea9309 | 2016-05-14 00:58:48 -0700 | [diff] [blame] | 1311 | : RtcpMode::kOff); |
| 1312 | } |
| 1313 | } |
| 1314 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 1315 | uint32_t VideoSendStreamImpl::OnBitrateUpdated(uint32_t bitrate_bps, |
| 1316 | uint8_t fraction_loss, |
minyue | 78b4d56 | 2016-11-30 04:47:39 -0800 | [diff] [blame] | 1317 | int64_t rtt, |
| 1318 | int64_t probing_interval_ms) { |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 1319 | RTC_DCHECK_RUN_ON(worker_queue_); |
sprang | 1a646ee | 2016-12-01 06:34:11 -0800 | [diff] [blame] | 1320 | RTC_DCHECK(payload_router_.IsActive()) |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 1321 | << "VideoSendStream::Start has not been called."; |
michaelt | a332877 | 2016-11-29 09:25:03 -0800 | [diff] [blame] | 1322 | |
michaelt | 192132e | 2017-01-26 09:05:27 -0800 | [diff] [blame] | 1323 | // Substract overhead from bitrate. |
| 1324 | rtc::CritScope lock(&overhead_bytes_per_packet_crit_); |
elad.alon | 0fe1216 | 2017-01-31 05:48:37 -0800 | [diff] [blame] | 1325 | uint32_t payload_bitrate_bps = bitrate_bps; |
| 1326 | if (send_side_bwe_with_overhead_) { |
| 1327 | payload_bitrate_bps -= CalculateOverheadRateBps( |
| 1328 | CalculatePacketRate(bitrate_bps, |
| 1329 | config_->rtp.max_packet_size + |
| 1330 | transport_overhead_bytes_per_packet_), |
| 1331 | overhead_bytes_per_packet_ + transport_overhead_bytes_per_packet_, |
| 1332 | bitrate_bps); |
| 1333 | } |
michaelt | a332877 | 2016-11-29 09:25:03 -0800 | [diff] [blame] | 1334 | |
Per | 69b332d | 2016-06-02 15:45:42 +0200 | [diff] [blame] | 1335 | // Get the encoder target rate. It is the estimated network rate - |
| 1336 | // protection overhead. |
Ying Wang | 3b790f3 | 2018-01-19 17:58:57 +0100 | [diff] [blame] | 1337 | encoder_target_rate_bps_ = fec_controller_->UpdateFecRates( |
michaelt | 192132e | 2017-01-26 09:05:27 -0800 | [diff] [blame] | 1338 | payload_bitrate_bps, stats_proxy_->GetSendFrameRate(), fraction_loss, |
Ying Wang | 3b790f3 | 2018-01-19 17:58:57 +0100 | [diff] [blame] | 1339 | loss_mask_vector_, rtt); |
| 1340 | loss_mask_vector_.clear(); |
michaelt | 192132e | 2017-01-26 09:05:27 -0800 | [diff] [blame] | 1341 | |
elad.alon | 0fe1216 | 2017-01-31 05:48:37 -0800 | [diff] [blame] | 1342 | uint32_t encoder_overhead_rate_bps = |
| 1343 | send_side_bwe_with_overhead_ |
| 1344 | ? CalculateOverheadRateBps( |
| 1345 | CalculatePacketRate(encoder_target_rate_bps_, |
| 1346 | config_->rtp.max_packet_size + |
| 1347 | transport_overhead_bytes_per_packet_ - |
| 1348 | overhead_bytes_per_packet_), |
| 1349 | overhead_bytes_per_packet_ + |
| 1350 | transport_overhead_bytes_per_packet_, |
| 1351 | bitrate_bps - encoder_target_rate_bps_) |
| 1352 | : 0; |
michaelt | 192132e | 2017-01-26 09:05:27 -0800 | [diff] [blame] | 1353 | |
| 1354 | // When the field trial "WebRTC-SendSideBwe-WithOverhead" is enabled |
| 1355 | // protection_bitrate includes overhead. |
| 1356 | uint32_t protection_bitrate = |
| 1357 | bitrate_bps - (encoder_target_rate_bps_ + encoder_overhead_rate_bps); |
mflodman | 48a4beb | 2016-07-01 13:03:59 +0200 | [diff] [blame] | 1358 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 1359 | encoder_target_rate_bps_ = |
| 1360 | std::min(encoder_max_bitrate_bps_, encoder_target_rate_bps_); |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1361 | video_stream_encoder_->OnBitrateUpdated(encoder_target_rate_bps_, |
| 1362 | fraction_loss, rtt); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 1363 | stats_proxy_->OnSetEncoderTargetRate(encoder_target_rate_bps_); |
perkj | f5b2e51 | 2016-07-05 08:34:04 -0700 | [diff] [blame] | 1364 | return protection_bitrate; |
mflodman | 86aabb2 | 2016-03-11 15:44:32 +0100 | [diff] [blame] | 1365 | } |
| 1366 | |
palmkvist | e75f204 | 2016-09-28 06:19:48 -0700 | [diff] [blame] | 1367 | void VideoSendStreamImpl::EnableEncodedFrameRecording( |
| 1368 | const std::vector<rtc::PlatformFile>& files, |
| 1369 | size_t byte_limit) { |
| 1370 | { |
| 1371 | rtc::CritScope lock(&ivf_writers_crit_); |
| 1372 | for (unsigned int i = 0; i < kMaxSimulcastStreams; ++i) { |
| 1373 | if (i < files.size()) { |
| 1374 | file_writers_[i] = IvfFileWriter::Wrap(rtc::File(files[i]), byte_limit); |
| 1375 | } else { |
| 1376 | file_writers_[i].reset(); |
| 1377 | } |
| 1378 | } |
| 1379 | } |
| 1380 | |
| 1381 | if (!files.empty()) { |
| 1382 | // Make a keyframe appear as early as possible in the logs, to give actually |
| 1383 | // decodable output. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1384 | video_stream_encoder_->SendKeyFrame(); |
palmkvist | e75f204 | 2016-09-28 06:19:48 -0700 | [diff] [blame] | 1385 | } |
| 1386 | } |
| 1387 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 1388 | int VideoSendStreamImpl::ProtectionRequest( |
| 1389 | const FecProtectionParams* delta_params, |
| 1390 | const FecProtectionParams* key_params, |
| 1391 | uint32_t* sent_video_rate_bps, |
| 1392 | uint32_t* sent_nack_rate_bps, |
| 1393 | uint32_t* sent_fec_rate_bps) { |
| 1394 | RTC_DCHECK_RUN_ON(worker_queue_); |
Per | 83d0910 | 2016-04-15 14:59:13 +0200 | [diff] [blame] | 1395 | *sent_video_rate_bps = 0; |
| 1396 | *sent_nack_rate_bps = 0; |
| 1397 | *sent_fec_rate_bps = 0; |
| 1398 | for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) { |
| 1399 | uint32_t not_used = 0; |
| 1400 | uint32_t module_video_rate = 0; |
| 1401 | uint32_t module_fec_rate = 0; |
| 1402 | uint32_t module_nack_rate = 0; |
brandtr | 1743a19 | 2016-11-07 03:36:05 -0800 | [diff] [blame] | 1403 | rtp_rtcp->SetFecParameters(*delta_params, *key_params); |
Per | 83d0910 | 2016-04-15 14:59:13 +0200 | [diff] [blame] | 1404 | rtp_rtcp->BitrateSent(¬_used, &module_video_rate, &module_fec_rate, |
| 1405 | &module_nack_rate); |
| 1406 | *sent_video_rate_bps += module_video_rate; |
| 1407 | *sent_nack_rate_bps += module_nack_rate; |
| 1408 | *sent_fec_rate_bps += module_fec_rate; |
| 1409 | } |
| 1410 | return 0; |
| 1411 | } |
| 1412 | |
michaelt | a332877 | 2016-11-29 09:25:03 -0800 | [diff] [blame] | 1413 | void VideoSendStreamImpl::OnOverheadChanged(size_t overhead_bytes_per_packet) { |
| 1414 | rtc::CritScope lock(&overhead_bytes_per_packet_crit_); |
| 1415 | overhead_bytes_per_packet_ = overhead_bytes_per_packet; |
| 1416 | } |
| 1417 | |
michaelt | 79e0588 | 2016-11-08 02:50:09 -0800 | [diff] [blame] | 1418 | void VideoSendStreamImpl::SetTransportOverhead( |
nisse | 284542b | 2017-01-10 08:58:32 -0800 | [diff] [blame] | 1419 | size_t transport_overhead_bytes_per_packet) { |
| 1420 | if (transport_overhead_bytes_per_packet >= static_cast<int>(kPathMTU)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1421 | RTC_LOG(LS_ERROR) << "Transport overhead exceeds size of ethernet frame"; |
nisse | 284542b | 2017-01-10 08:58:32 -0800 | [diff] [blame] | 1422 | return; |
| 1423 | } |
| 1424 | |
michaelt | a332877 | 2016-11-29 09:25:03 -0800 | [diff] [blame] | 1425 | transport_overhead_bytes_per_packet_ = transport_overhead_bytes_per_packet; |
nisse | 284542b | 2017-01-10 08:58:32 -0800 | [diff] [blame] | 1426 | |
nisse | b8f9a32 | 2017-03-27 05:36:15 -0700 | [diff] [blame] | 1427 | transport_->send_side_cc()->SetTransportOverhead( |
| 1428 | transport_overhead_bytes_per_packet_); |
nisse | 284542b | 2017-01-10 08:58:32 -0800 | [diff] [blame] | 1429 | |
| 1430 | size_t rtp_packet_size = |
| 1431 | std::min(config_->rtp.max_packet_size, |
| 1432 | kPathMTU - transport_overhead_bytes_per_packet_); |
| 1433 | |
michaelt | a332877 | 2016-11-29 09:25:03 -0800 | [diff] [blame] | 1434 | for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) { |
nisse | 284542b | 2017-01-10 08:58:32 -0800 | [diff] [blame] | 1435 | rtp_rtcp->SetMaxRtpPacketSize(rtp_packet_size); |
michaelt | a332877 | 2016-11-29 09:25:03 -0800 | [diff] [blame] | 1436 | } |
michaelt | 79e0588 | 2016-11-08 02:50:09 -0800 | [diff] [blame] | 1437 | } |
| 1438 | |
Ying Wang | 3b790f3 | 2018-01-19 17:58:57 +0100 | [diff] [blame] | 1439 | void VideoSendStreamImpl::OnPacketAdded(uint32_t ssrc, uint16_t seq_num) { |
| 1440 | if (!worker_queue_->IsCurrent()) { |
| 1441 | auto ptr = weak_ptr_; |
| 1442 | worker_queue_->PostTask([=] { |
| 1443 | if (!ptr.get()) |
| 1444 | return; |
| 1445 | ptr->OnPacketAdded(ssrc, seq_num); |
| 1446 | }); |
| 1447 | return; |
| 1448 | } |
| 1449 | const auto ssrcs = config_->rtp.ssrcs; |
| 1450 | if (std::find(ssrcs.begin(), ssrcs.end(), ssrc) != ssrcs.end()) { |
| 1451 | feedback_packet_seq_num_set_.insert(seq_num); |
| 1452 | if (feedback_packet_seq_num_set_.size() > kSendSideSeqNumSetMaxSize) { |
| 1453 | RTC_LOG(LS_WARNING) << "Feedback packet sequence number set exceed it's " |
| 1454 | "max size', will get reset."; |
| 1455 | feedback_packet_seq_num_set_.clear(); |
| 1456 | } |
| 1457 | } |
| 1458 | } |
| 1459 | |
| 1460 | void VideoSendStreamImpl::OnPacketFeedbackVector( |
| 1461 | const std::vector<PacketFeedback>& packet_feedback_vector) { |
| 1462 | if (!worker_queue_->IsCurrent()) { |
| 1463 | auto ptr = weak_ptr_; |
| 1464 | worker_queue_->PostTask([=] { |
| 1465 | if (!ptr.get()) |
| 1466 | return; |
| 1467 | ptr->OnPacketFeedbackVector(packet_feedback_vector); |
| 1468 | }); |
| 1469 | return; |
| 1470 | } |
| 1471 | // Lost feedbacks are not considered to be lost packets. |
| 1472 | for (const PacketFeedback& packet : packet_feedback_vector) { |
| 1473 | if (auto it = feedback_packet_seq_num_set_.find(packet.sequence_number) != |
| 1474 | feedback_packet_seq_num_set_.end()) { |
| 1475 | const bool lost = packet.arrival_time_ms == PacketFeedback::kNotReceived; |
| 1476 | loss_mask_vector_.push_back(lost); |
| 1477 | feedback_packet_seq_num_set_.erase(it); |
| 1478 | } |
| 1479 | } |
| 1480 | } |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 1481 | } // namespace internal |
| 1482 | } // namespace webrtc |