| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 |
| 10 #include "webrtc/video/video_send_stream.h" | 11 #include "webrtc/video/video_send_stream.h" |
| 11 | 12 |
| 12 #include <algorithm> | 13 #include <algorithm> |
| 13 #include <sstream> | 14 #include <sstream> |
| 14 #include <string> | 15 #include <string> |
| 15 #include <utility> | 16 #include <utility> |
| 16 #include <vector> | 17 #include <vector> |
| 17 | 18 |
| 18 #include "webrtc/base/checks.h" | 19 #include "webrtc/base/checks.h" |
| 19 #include "webrtc/base/logging.h" | 20 #include "webrtc/base/logging.h" |
| 20 #include "webrtc/base/trace_event.h" | 21 #include "webrtc/base/trace_event.h" |
| 22 #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h" |
| 21 #include "webrtc/modules/bitrate_controller/include/bitrate_controller.h" | 23 #include "webrtc/modules/bitrate_controller/include/bitrate_controller.h" |
| 22 #include "webrtc/modules/congestion_controller/include/congestion_controller.h" | 24 #include "webrtc/modules/congestion_controller/include/congestion_controller.h" |
| 23 #include "webrtc/modules/pacing/packet_router.h" | 25 #include "webrtc/modules/pacing/packet_router.h" |
| 24 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp.h" | 26 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp.h" |
| 25 #include "webrtc/modules/utility/include/process_thread.h" | 27 #include "webrtc/modules/utility/include/process_thread.h" |
| 26 #include "webrtc/modules/video_coding/utility/ivf_file_writer.h" | 28 #include "webrtc/modules/video_coding/utility/ivf_file_writer.h" |
| 27 #include "webrtc/video/call_stats.h" | 29 #include "webrtc/video/call_stats.h" |
| 30 #include "webrtc/video/video_capture_input.h" |
| 28 #include "webrtc/video/vie_remb.h" | 31 #include "webrtc/video/vie_remb.h" |
| 29 #include "webrtc/video_send_stream.h" | 32 #include "webrtc/video_send_stream.h" |
| 30 | 33 |
| 31 namespace webrtc { | 34 namespace webrtc { |
| 32 | 35 |
| 36 class RtcpIntraFrameObserver; |
| 37 class TransportFeedbackObserver; |
| 38 |
| 33 static const int kMinSendSidePacketHistorySize = 600; | 39 static const int kMinSendSidePacketHistorySize = 600; |
| 40 static const int kEncoderTimeOutMs = 2000; |
| 41 |
| 34 namespace { | 42 namespace { |
| 35 | 43 |
| 36 std::vector<RtpRtcp*> CreateRtpRtcpModules( | 44 std::vector<RtpRtcp*> CreateRtpRtcpModules( |
| 37 Transport* outgoing_transport, | 45 Transport* outgoing_transport, |
| 38 RtcpIntraFrameObserver* intra_frame_callback, | 46 RtcpIntraFrameObserver* intra_frame_callback, |
| 39 RtcpBandwidthObserver* bandwidth_callback, | 47 RtcpBandwidthObserver* bandwidth_callback, |
| 40 TransportFeedbackObserver* transport_feedback_callback, | 48 TransportFeedbackObserver* transport_feedback_callback, |
| 41 RtcpRttStats* rtt_stats, | 49 RtcpRttStats* rtt_stats, |
| 42 RtpPacketSender* paced_sender, | 50 RtpPacketSender* paced_sender, |
| 43 TransportSequenceNumberAllocator* transport_sequence_number_allocator, | 51 TransportSequenceNumberAllocator* transport_sequence_number_allocator, |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 } | 145 } |
| 138 | 146 |
| 139 std::string VideoSendStream::Config::ToString() const { | 147 std::string VideoSendStream::Config::ToString() const { |
| 140 std::stringstream ss; | 148 std::stringstream ss; |
| 141 ss << "{encoder_settings: " << encoder_settings.ToString(); | 149 ss << "{encoder_settings: " << encoder_settings.ToString(); |
| 142 ss << ", rtp: " << rtp.ToString(); | 150 ss << ", rtp: " << rtp.ToString(); |
| 143 ss << ", pre_encode_callback: " | 151 ss << ", pre_encode_callback: " |
| 144 << (pre_encode_callback ? "(I420FrameCallback)" : "nullptr"); | 152 << (pre_encode_callback ? "(I420FrameCallback)" : "nullptr"); |
| 145 ss << ", post_encode_callback: " | 153 ss << ", post_encode_callback: " |
| 146 << (post_encode_callback ? "(EncodedFrameObserver)" : "nullptr"); | 154 << (post_encode_callback ? "(EncodedFrameObserver)" : "nullptr"); |
| 155 ss << ", local_renderer: " |
| 156 << (local_renderer ? "(VideoRenderer)" : "nullptr"); |
| 147 ss << ", render_delay_ms: " << render_delay_ms; | 157 ss << ", render_delay_ms: " << render_delay_ms; |
| 148 ss << ", target_delay_ms: " << target_delay_ms; | 158 ss << ", target_delay_ms: " << target_delay_ms; |
| 149 ss << ", suspend_below_min_bitrate: " << (suspend_below_min_bitrate ? "on" | 159 ss << ", suspend_below_min_bitrate: " << (suspend_below_min_bitrate ? "on" |
| 150 : "off"); | 160 : "off"); |
| 151 ss << '}'; | 161 ss << '}'; |
| 152 return ss.str(); | 162 return ss.str(); |
| 153 } | 163 } |
| 154 | 164 |
| 155 std::string VideoSendStream::Stats::ToString(int64_t time_ms) const { | 165 std::string VideoSendStream::Stats::ToString(int64_t time_ms) const { |
| 156 std::stringstream ss; | 166 std::stringstream ss; |
| (...skipping 30 matching lines...) Expand all Loading... |
| 187 ss << "cum_loss: " << rtcp_stats.cumulative_lost << ", "; | 197 ss << "cum_loss: " << rtcp_stats.cumulative_lost << ", "; |
| 188 ss << "max_ext_seq: " << rtcp_stats.extended_max_sequence_number << ", "; | 198 ss << "max_ext_seq: " << rtcp_stats.extended_max_sequence_number << ", "; |
| 189 ss << "nack: " << rtcp_packet_type_counts.nack_packets << ", "; | 199 ss << "nack: " << rtcp_packet_type_counts.nack_packets << ", "; |
| 190 ss << "fir: " << rtcp_packet_type_counts.fir_packets << ", "; | 200 ss << "fir: " << rtcp_packet_type_counts.fir_packets << ", "; |
| 191 ss << "pli: " << rtcp_packet_type_counts.pli_packets; | 201 ss << "pli: " << rtcp_packet_type_counts.pli_packets; |
| 192 return ss.str(); | 202 return ss.str(); |
| 193 } | 203 } |
| 194 | 204 |
| 195 namespace { | 205 namespace { |
| 196 | 206 |
| 207 VideoCodecType PayloadNameToCodecType(const std::string& payload_name) { |
| 208 if (payload_name == "VP8") |
| 209 return kVideoCodecVP8; |
| 210 if (payload_name == "VP9") |
| 211 return kVideoCodecVP9; |
| 212 if (payload_name == "H264") |
| 213 return kVideoCodecH264; |
| 214 return kVideoCodecGeneric; |
| 215 } |
| 216 |
| 197 bool PayloadTypeSupportsSkippingFecPackets(const std::string& payload_name) { | 217 bool PayloadTypeSupportsSkippingFecPackets(const std::string& payload_name) { |
| 198 if (payload_name == "VP8" || payload_name == "VP9") | 218 switch (PayloadNameToCodecType(payload_name)) { |
| 199 return true; | 219 case kVideoCodecVP8: |
| 200 RTC_DCHECK(payload_name == "H264" || payload_name == "FAKE") | 220 case kVideoCodecVP9: |
| 201 << "unknown payload_name " << payload_name; | 221 return true; |
| 222 case kVideoCodecH264: |
| 223 case kVideoCodecGeneric: |
| 224 return false; |
| 225 case kVideoCodecI420: |
| 226 case kVideoCodecRED: |
| 227 case kVideoCodecULPFEC: |
| 228 case kVideoCodecUnknown: |
| 229 RTC_NOTREACHED(); |
| 230 return false; |
| 231 } |
| 232 RTC_NOTREACHED(); |
| 202 return false; | 233 return false; |
| 203 } | 234 } |
| 204 | 235 |
| 205 int CalculateMaxPadBitrateBps(const VideoEncoderConfig& config, | 236 // TODO(pbos): Lower these thresholds (to closer to 100%) when we handle |
| 237 // pipelining encoders better (multiple input frames before something comes |
| 238 // out). This should effectively turn off CPU adaptations for systems that |
| 239 // remotely cope with the load right now. |
| 240 CpuOveruseOptions GetCpuOveruseOptions(bool full_overuse_time) { |
| 241 CpuOveruseOptions options; |
| 242 if (full_overuse_time) { |
| 243 options.low_encode_usage_threshold_percent = 150; |
| 244 options.high_encode_usage_threshold_percent = 200; |
| 245 } |
| 246 return options; |
| 247 } |
| 248 |
| 249 VideoCodec VideoEncoderConfigToVideoCodec(const VideoEncoderConfig& config, |
| 250 const std::string& payload_name, |
| 251 int payload_type) { |
| 252 const std::vector<VideoStream>& streams = config.streams; |
| 253 static const int kEncoderMinBitrateKbps = 30; |
| 254 RTC_DCHECK(!streams.empty()); |
| 255 RTC_DCHECK_GE(config.min_transmit_bitrate_bps, 0); |
| 256 |
| 257 VideoCodec video_codec; |
| 258 memset(&video_codec, 0, sizeof(video_codec)); |
| 259 video_codec.codecType = PayloadNameToCodecType(payload_name); |
| 260 |
| 261 switch (config.content_type) { |
| 262 case VideoEncoderConfig::ContentType::kRealtimeVideo: |
| 263 video_codec.mode = kRealtimeVideo; |
| 264 break; |
| 265 case VideoEncoderConfig::ContentType::kScreen: |
| 266 video_codec.mode = kScreensharing; |
| 267 if (config.streams.size() == 1 && |
| 268 config.streams[0].temporal_layer_thresholds_bps.size() == 1) { |
| 269 video_codec.targetBitrate = |
| 270 config.streams[0].temporal_layer_thresholds_bps[0] / 1000; |
| 271 } |
| 272 break; |
| 273 } |
| 274 |
| 275 switch (video_codec.codecType) { |
| 276 case kVideoCodecVP8: { |
| 277 if (config.encoder_specific_settings) { |
| 278 video_codec.codecSpecific.VP8 = *reinterpret_cast<const VideoCodecVP8*>( |
| 279 config.encoder_specific_settings); |
| 280 } else { |
| 281 video_codec.codecSpecific.VP8 = VideoEncoder::GetDefaultVp8Settings(); |
| 282 } |
| 283 video_codec.codecSpecific.VP8.numberOfTemporalLayers = |
| 284 static_cast<unsigned char>( |
| 285 streams.back().temporal_layer_thresholds_bps.size() + 1); |
| 286 break; |
| 287 } |
| 288 case kVideoCodecVP9: { |
| 289 if (config.encoder_specific_settings) { |
| 290 video_codec.codecSpecific.VP9 = *reinterpret_cast<const VideoCodecVP9*>( |
| 291 config.encoder_specific_settings); |
| 292 if (video_codec.mode == kScreensharing) { |
| 293 video_codec.codecSpecific.VP9.flexibleMode = true; |
| 294 // For now VP9 screensharing use 1 temporal and 2 spatial layers. |
| 295 RTC_DCHECK_EQ(video_codec.codecSpecific.VP9.numberOfTemporalLayers, |
| 296 1); |
| 297 RTC_DCHECK_EQ(video_codec.codecSpecific.VP9.numberOfSpatialLayers, 2); |
| 298 } |
| 299 } else { |
| 300 video_codec.codecSpecific.VP9 = VideoEncoder::GetDefaultVp9Settings(); |
| 301 } |
| 302 video_codec.codecSpecific.VP9.numberOfTemporalLayers = |
| 303 static_cast<unsigned char>( |
| 304 streams.back().temporal_layer_thresholds_bps.size() + 1); |
| 305 break; |
| 306 } |
| 307 case kVideoCodecH264: { |
| 308 if (config.encoder_specific_settings) { |
| 309 video_codec.codecSpecific.H264 = |
| 310 *reinterpret_cast<const VideoCodecH264*>( |
| 311 config.encoder_specific_settings); |
| 312 } else { |
| 313 video_codec.codecSpecific.H264 = VideoEncoder::GetDefaultH264Settings(); |
| 314 } |
| 315 break; |
| 316 } |
| 317 default: |
| 318 // TODO(pbos): Support encoder_settings codec-agnostically. |
| 319 RTC_DCHECK(!config.encoder_specific_settings) |
| 320 << "Encoder-specific settings for codec type not wired up."; |
| 321 break; |
| 322 } |
| 323 |
| 324 strncpy(video_codec.plName, payload_name.c_str(), kPayloadNameSize - 1); |
| 325 video_codec.plName[kPayloadNameSize - 1] = '\0'; |
| 326 video_codec.plType = payload_type; |
| 327 video_codec.numberOfSimulcastStreams = |
| 328 static_cast<unsigned char>(streams.size()); |
| 329 video_codec.minBitrate = streams[0].min_bitrate_bps / 1000; |
| 330 if (video_codec.minBitrate < kEncoderMinBitrateKbps) |
| 331 video_codec.minBitrate = kEncoderMinBitrateKbps; |
| 332 RTC_DCHECK_LE(streams.size(), static_cast<size_t>(kMaxSimulcastStreams)); |
| 333 if (video_codec.codecType == kVideoCodecVP9) { |
| 334 // If the vector is empty, bitrates will be configured automatically. |
| 335 RTC_DCHECK(config.spatial_layers.empty() || |
| 336 config.spatial_layers.size() == |
| 337 video_codec.codecSpecific.VP9.numberOfSpatialLayers); |
| 338 RTC_DCHECK_LE(video_codec.codecSpecific.VP9.numberOfSpatialLayers, |
| 339 kMaxSimulcastStreams); |
| 340 for (size_t i = 0; i < config.spatial_layers.size(); ++i) |
| 341 video_codec.spatialLayers[i] = config.spatial_layers[i]; |
| 342 } |
| 343 for (size_t i = 0; i < streams.size(); ++i) { |
| 344 SimulcastStream* sim_stream = &video_codec.simulcastStream[i]; |
| 345 RTC_DCHECK_GT(streams[i].width, 0u); |
| 346 RTC_DCHECK_GT(streams[i].height, 0u); |
| 347 RTC_DCHECK_GT(streams[i].max_framerate, 0); |
| 348 // Different framerates not supported per stream at the moment. |
| 349 RTC_DCHECK_EQ(streams[i].max_framerate, streams[0].max_framerate); |
| 350 RTC_DCHECK_GE(streams[i].min_bitrate_bps, 0); |
| 351 RTC_DCHECK_GE(streams[i].target_bitrate_bps, streams[i].min_bitrate_bps); |
| 352 RTC_DCHECK_GE(streams[i].max_bitrate_bps, streams[i].target_bitrate_bps); |
| 353 RTC_DCHECK_GE(streams[i].max_qp, 0); |
| 354 |
| 355 sim_stream->width = static_cast<uint16_t>(streams[i].width); |
| 356 sim_stream->height = static_cast<uint16_t>(streams[i].height); |
| 357 sim_stream->minBitrate = streams[i].min_bitrate_bps / 1000; |
| 358 sim_stream->targetBitrate = streams[i].target_bitrate_bps / 1000; |
| 359 sim_stream->maxBitrate = streams[i].max_bitrate_bps / 1000; |
| 360 sim_stream->qpMax = streams[i].max_qp; |
| 361 sim_stream->numberOfTemporalLayers = static_cast<unsigned char>( |
| 362 streams[i].temporal_layer_thresholds_bps.size() + 1); |
| 363 |
| 364 video_codec.width = std::max(video_codec.width, |
| 365 static_cast<uint16_t>(streams[i].width)); |
| 366 video_codec.height = std::max( |
| 367 video_codec.height, static_cast<uint16_t>(streams[i].height)); |
| 368 video_codec.minBitrate = |
| 369 std::min(static_cast<uint16_t>(video_codec.minBitrate), |
| 370 static_cast<uint16_t>(streams[i].min_bitrate_bps / 1000)); |
| 371 video_codec.maxBitrate += streams[i].max_bitrate_bps / 1000; |
| 372 video_codec.qpMax = std::max(video_codec.qpMax, |
| 373 static_cast<unsigned int>(streams[i].max_qp)); |
| 374 } |
| 375 |
| 376 if (video_codec.maxBitrate == 0) { |
| 377 // Unset max bitrate -> cap to one bit per pixel. |
| 378 video_codec.maxBitrate = |
| 379 (video_codec.width * video_codec.height * video_codec.maxFramerate) / |
| 380 1000; |
| 381 } |
| 382 if (video_codec.maxBitrate < kEncoderMinBitrateKbps) |
| 383 video_codec.maxBitrate = kEncoderMinBitrateKbps; |
| 384 |
| 385 RTC_DCHECK_GT(streams[0].max_framerate, 0); |
| 386 video_codec.maxFramerate = streams[0].max_framerate; |
| 387 video_codec.expect_encode_from_texture = config.expect_encode_from_texture; |
| 388 |
| 389 return video_codec; |
| 390 } |
| 391 |
| 392 int CalulcateMaxPadBitrateBps(const VideoEncoderConfig& config, |
| 206 bool pad_to_min_bitrate) { | 393 bool pad_to_min_bitrate) { |
| 207 int pad_up_to_bitrate_bps = 0; | 394 int pad_up_to_bitrate_bps = 0; |
| 208 // Calculate max padding bitrate for a multi layer codec. | 395 // Calculate max padding bitrate for a multi layer codec. |
| 209 if (config.streams.size() > 1) { | 396 if (config.streams.size() > 1) { |
| 210 // Pad to min bitrate of the highest layer. | 397 // Pad to min bitrate of the highest layer. |
| 211 pad_up_to_bitrate_bps = | 398 pad_up_to_bitrate_bps = |
| 212 config.streams[config.streams.size() - 1].min_bitrate_bps; | 399 config.streams[config.streams.size() - 1].min_bitrate_bps; |
| 213 // Add target_bitrate_bps of the lower layers. | 400 // Add target_bitrate_bps of the lower layers. |
| 214 for (size_t i = 0; i < config.streams.size() - 1; ++i) | 401 for (size_t i = 0; i < config.streams.size() - 1; ++i) |
| 215 pad_up_to_bitrate_bps += config.streams[i].target_bitrate_bps; | 402 pad_up_to_bitrate_bps += config.streams[i].target_bitrate_bps; |
| 216 } else if (pad_to_min_bitrate) { | 403 } else if (pad_to_min_bitrate) { |
| 217 pad_up_to_bitrate_bps = config.streams[0].min_bitrate_bps; | 404 pad_up_to_bitrate_bps = config.streams[0].min_bitrate_bps; |
| 218 } | 405 } |
| 219 | 406 |
| 220 pad_up_to_bitrate_bps = | 407 pad_up_to_bitrate_bps = |
| 221 std::max(pad_up_to_bitrate_bps, config.min_transmit_bitrate_bps); | 408 std::max(pad_up_to_bitrate_bps, config.min_transmit_bitrate_bps); |
| 222 | 409 |
| 223 return pad_up_to_bitrate_bps; | 410 return pad_up_to_bitrate_bps; |
| 224 } | 411 } |
| 225 | 412 |
| 226 } // namespace | 413 } // namespace |
| 227 | 414 |
| 228 namespace internal { | 415 namespace internal { |
| 229 | |
| 230 // VideoSendStreamImpl implements internal::VideoSendStream. | |
| 231 // It is created and destroyed on |worker_queue|. The intent is to decrease the | |
| 232 // need for locking and to ensure methods are called in sequence. | |
| 233 // Public methods except |DeliverRtcp| must be called on |worker_queue|. | |
| 234 // DeliverRtcp is called on the libjingle worker thread or a network thread. | |
| 235 // An encoder may deliver frames through the EncodedImageCallback on an | |
| 236 // arbitrary thread. | |
| 237 class VideoSendStreamImpl : public webrtc::BitrateAllocatorObserver, | |
| 238 public webrtc::VCMProtectionCallback, | |
| 239 public EncodedImageCallback { | |
| 240 public: | |
| 241 VideoSendStreamImpl(SendStatisticsProxy* stats_proxy, | |
| 242 rtc::TaskQueue* worker_queue, | |
| 243 CallStats* call_stats, | |
| 244 CongestionController* congestion_controller, | |
| 245 BitrateAllocator* bitrate_allocator, | |
| 246 SendDelayStats* send_delay_stats, | |
| 247 VieRemb* remb, | |
| 248 ViEEncoder* vie_encoder, | |
| 249 RtcEventLog* event_log, | |
| 250 const VideoSendStream::Config* config, | |
| 251 std::map<uint32_t, RtpState> suspended_ssrcs); | |
| 252 ~VideoSendStreamImpl() override; | |
| 253 | |
| 254 // RegisterProcessThread register |module_process_thread| with those objects | |
| 255 // that use it. Registration has to happen on the thread were | |
| 256 // |module_process_thread| was created (libjingle's worker thread). | |
| 257 // TODO(perkj): Replace the use of |module_process_thread| with a TaskQueue, | |
| 258 // maybe |worker_queue|. | |
| 259 void RegisterProcessThread(ProcessThread* module_process_thread); | |
| 260 void DeRegisterProcessThread(); | |
| 261 | |
| 262 void SignalNetworkState(NetworkState state); | |
| 263 bool DeliverRtcp(const uint8_t* packet, size_t length); | |
| 264 void Start(); | |
| 265 void Stop(); | |
| 266 | |
| 267 void SignalEncoderConfigurationChanged(const VideoEncoderConfig& config); | |
| 268 VideoSendStream::RtpStateMap GetRtpStates() const; | |
| 269 | |
| 270 private: | |
| 271 class CheckEncoderActivityTask; | |
| 272 | |
| 273 // Implements BitrateAllocatorObserver. | |
| 274 uint32_t OnBitrateUpdated(uint32_t bitrate_bps, | |
| 275 uint8_t fraction_loss, | |
| 276 int64_t rtt) override; | |
| 277 | |
| 278 // Implements webrtc::VCMProtectionCallback. | |
| 279 int ProtectionRequest(const FecProtectionParams* delta_params, | |
| 280 const FecProtectionParams* key_params, | |
| 281 uint32_t* sent_video_rate_bps, | |
| 282 uint32_t* sent_nack_rate_bps, | |
| 283 uint32_t* sent_fec_rate_bps) override; | |
| 284 | |
| 285 // Implements EncodedImageCallback. The implementation routes encoded frames | |
| 286 // to the |payload_router_| and |config.pre_encode_callback| if set. | |
| 287 // Called on an arbitrary encoder callback thread. | |
| 288 EncodedImageCallback::Result OnEncodedImage( | |
| 289 const EncodedImage& encoded_image, | |
| 290 const CodecSpecificInfo* codec_specific_info, | |
| 291 const RTPFragmentationHeader* fragmentation) override; | |
| 292 | |
| 293 void ConfigureProtection(); | |
| 294 void ConfigureSsrcs(); | |
| 295 void SignalEncoderTimedOut(); | |
| 296 void SignalEncoderActive(); | |
| 297 | |
| 298 SendStatisticsProxy* const stats_proxy_; | |
| 299 const VideoSendStream::Config* const config_; | |
| 300 std::map<uint32_t, RtpState> suspended_ssrcs_; | |
| 301 | |
| 302 ProcessThread* module_process_thread_; | |
| 303 rtc::ThreadChecker module_process_thread_checker_; | |
| 304 rtc::TaskQueue* const worker_queue_; | |
| 305 | |
| 306 rtc::CriticalSection encoder_activity_crit_sect_; | |
| 307 CheckEncoderActivityTask* check_encoder_activity_task_ | |
| 308 GUARDED_BY(encoder_activity_crit_sect_); | |
| 309 CallStats* const call_stats_; | |
| 310 CongestionController* const congestion_controller_; | |
| 311 BitrateAllocator* const bitrate_allocator_; | |
| 312 VieRemb* const remb_; | |
| 313 | |
| 314 static const bool kEnableFrameRecording = false; | |
| 315 static const int kMaxLayers = 3; | |
| 316 std::unique_ptr<IvfFileWriter> file_writers_[kMaxLayers]; | |
| 317 | |
| 318 int max_padding_bitrate_; | |
| 319 int encoder_min_bitrate_bps_; | |
| 320 uint32_t encoder_max_bitrate_bps_; | |
| 321 uint32_t encoder_target_rate_bps_; | |
| 322 | |
| 323 ViEEncoder* const vie_encoder_; | |
| 324 EncoderStateFeedback encoder_feedback_; | |
| 325 ProtectionBitrateCalculator protection_bitrate_calculator_; | |
| 326 | |
| 327 const std::unique_ptr<RtcpBandwidthObserver> bandwidth_observer_; | |
| 328 // RtpRtcp modules, declared here as they use other members on construction. | |
| 329 const std::vector<RtpRtcp*> rtp_rtcp_modules_; | |
| 330 PayloadRouter payload_router_; | |
| 331 }; | |
| 332 | |
| 333 // TODO(tommi): See if there's a more elegant way to create a task that creates | |
| 334 // an object on the correct task queue. | |
| 335 class VideoSendStream::ConstructionTask : public rtc::QueuedTask { | |
| 336 public: | |
| 337 ConstructionTask(std::unique_ptr<VideoSendStreamImpl>* send_stream, | |
| 338 rtc::Event* done_event, | |
| 339 SendStatisticsProxy* stats_proxy, | |
| 340 ViEEncoder* vie_encoder, | |
| 341 ProcessThread* module_process_thread, | |
| 342 CallStats* call_stats, | |
| 343 CongestionController* congestion_controller, | |
| 344 BitrateAllocator* bitrate_allocator, | |
| 345 SendDelayStats* send_delay_stats, | |
| 346 VieRemb* remb, | |
| 347 RtcEventLog* event_log, | |
| 348 const VideoSendStream::Config* config, | |
| 349 const std::map<uint32_t, RtpState>& suspended_ssrcs) | |
| 350 : send_stream_(send_stream), | |
| 351 done_event_(done_event), | |
| 352 stats_proxy_(stats_proxy), | |
| 353 vie_encoder_(vie_encoder), | |
| 354 call_stats_(call_stats), | |
| 355 congestion_controller_(congestion_controller), | |
| 356 bitrate_allocator_(bitrate_allocator), | |
| 357 send_delay_stats_(send_delay_stats), | |
| 358 remb_(remb), | |
| 359 event_log_(event_log), | |
| 360 config_(config), | |
| 361 suspended_ssrcs_(suspended_ssrcs) {} | |
| 362 | |
| 363 ~ConstructionTask() override { done_event_->Set(); } | |
| 364 | |
| 365 private: | |
| 366 bool Run() override { | |
| 367 send_stream_->reset(new VideoSendStreamImpl( | |
| 368 stats_proxy_, rtc::TaskQueue::Current(), call_stats_, | |
| 369 congestion_controller_, bitrate_allocator_, send_delay_stats_, remb_, | |
| 370 vie_encoder_, event_log_, config_, std::move(suspended_ssrcs_))); | |
| 371 return true; | |
| 372 } | |
| 373 | |
| 374 std::unique_ptr<VideoSendStreamImpl>* const send_stream_; | |
| 375 rtc::Event* const done_event_; | |
| 376 SendStatisticsProxy* const stats_proxy_; | |
| 377 ViEEncoder* const vie_encoder_; | |
| 378 CallStats* const call_stats_; | |
| 379 CongestionController* const congestion_controller_; | |
| 380 BitrateAllocator* const bitrate_allocator_; | |
| 381 SendDelayStats* const send_delay_stats_; | |
| 382 VieRemb* const remb_; | |
| 383 RtcEventLog* const event_log_; | |
| 384 const VideoSendStream::Config* config_; | |
| 385 std::map<uint32_t, RtpState> suspended_ssrcs_; | |
| 386 }; | |
| 387 | |
| 388 class VideoSendStream::DestructAndGetRtpStateTask : public rtc::QueuedTask { | |
| 389 public: | |
| 390 DestructAndGetRtpStateTask(VideoSendStream::RtpStateMap* state_map, | |
| 391 std::unique_ptr<VideoSendStreamImpl> send_stream, | |
| 392 rtc::Event* done_event) | |
| 393 : state_map_(state_map), | |
| 394 send_stream_(std::move(send_stream)), | |
| 395 done_event_(done_event) {} | |
| 396 | |
| 397 ~DestructAndGetRtpStateTask() override { RTC_CHECK(!send_stream_); } | |
| 398 | |
| 399 private: | |
| 400 bool Run() override { | |
| 401 send_stream_->Stop(); | |
| 402 *state_map_ = send_stream_->GetRtpStates(); | |
| 403 send_stream_.reset(); | |
| 404 done_event_->Set(); | |
| 405 return true; | |
| 406 } | |
| 407 | |
| 408 VideoSendStream::RtpStateMap* state_map_; | |
| 409 std::unique_ptr<VideoSendStreamImpl> send_stream_; | |
| 410 rtc::Event* done_event_; | |
| 411 }; | |
| 412 | |
| 413 // CheckEncoderActivityTask is used for tracking when the encoder last produced | |
| 414 // and encoded video frame. If the encoder has not produced anything the last | |
| 415 // kEncoderTimeOutMs we also want to stop sending padding. | |
| 416 class VideoSendStreamImpl::CheckEncoderActivityTask : public rtc::QueuedTask { | |
| 417 public: | |
| 418 static const int kEncoderTimeOutMs = 2000; | |
| 419 explicit CheckEncoderActivityTask(VideoSendStreamImpl* send_stream) | |
| 420 : activity_(0), send_stream_(send_stream), timed_out_(false) {} | |
| 421 | |
| 422 void Stop() { | |
| 423 RTC_CHECK(task_checker_.CalledSequentially()); | |
| 424 send_stream_ = nullptr; | |
| 425 } | |
| 426 | |
| 427 void UpdateEncoderActivity() { | |
| 428 // UpdateEncoderActivity is called from VideoSendStreamImpl::Encoded on | |
| 429 // whatever thread the real encoder implementation run on. In the case of | |
| 430 // hardware encoders, there might be several encoders | |
| 431 // running in parallel on different threads. | |
| 432 rtc::AtomicOps::ReleaseStore(&activity_, 1); | |
| 433 } | |
| 434 | |
| 435 private: | |
| 436 bool Run() override { | |
| 437 RTC_CHECK(task_checker_.CalledSequentially()); | |
| 438 if (!send_stream_) | |
| 439 return true; | |
| 440 if (!rtc::AtomicOps::AcquireLoad(&activity_)) { | |
| 441 if (!timed_out_) { | |
| 442 send_stream_->SignalEncoderTimedOut(); | |
| 443 } | |
| 444 timed_out_ = true; | |
| 445 } else if (timed_out_) { | |
| 446 send_stream_->SignalEncoderActive(); | |
| 447 timed_out_ = false; | |
| 448 } | |
| 449 rtc::AtomicOps::ReleaseStore(&activity_, 0); | |
| 450 | |
| 451 rtc::TaskQueue::Current()->PostDelayedTask( | |
| 452 std::unique_ptr<rtc::QueuedTask>(this), kEncoderTimeOutMs); | |
| 453 // Return false to prevent this task from being deleted. Ownership has been | |
| 454 // transferred to the task queue when PostDelayedTask was called. | |
| 455 return false; | |
| 456 } | |
| 457 volatile int activity_; | |
| 458 | |
| 459 rtc::SequencedTaskChecker task_checker_; | |
| 460 VideoSendStreamImpl* send_stream_; | |
| 461 bool timed_out_; | |
| 462 }; | |
| 463 | |
| 464 class ReconfigureVideoEncoderTask : public rtc::QueuedTask { | |
| 465 public: | |
| 466 ReconfigureVideoEncoderTask(VideoSendStreamImpl* send_stream, | |
| 467 VideoEncoderConfig config) | |
| 468 : send_stream_(send_stream), config_(std::move(config)) {} | |
| 469 | |
| 470 private: | |
| 471 bool Run() override { | |
| 472 send_stream_->SignalEncoderConfigurationChanged(std::move(config_)); | |
| 473 return true; | |
| 474 } | |
| 475 | |
| 476 VideoSendStreamImpl* send_stream_; | |
| 477 VideoEncoderConfig config_; | |
| 478 }; | |
| 479 | |
| 480 VideoSendStream::VideoSendStream( | 416 VideoSendStream::VideoSendStream( |
| 481 int num_cpu_cores, | 417 int num_cpu_cores, |
| 482 ProcessThread* module_process_thread, | 418 ProcessThread* module_process_thread, |
| 483 rtc::TaskQueue* worker_queue, | |
| 484 CallStats* call_stats, | 419 CallStats* call_stats, |
| 485 CongestionController* congestion_controller, | 420 CongestionController* congestion_controller, |
| 486 BitrateAllocator* bitrate_allocator, | 421 BitrateAllocator* bitrate_allocator, |
| 487 SendDelayStats* send_delay_stats, | 422 SendDelayStats* send_delay_stats, |
| 488 VieRemb* remb, | 423 VieRemb* remb, |
| 489 RtcEventLog* event_log, | 424 RtcEventLog* event_log, |
| 490 VideoSendStream::Config config, | 425 const VideoSendStream::Config& config, |
| 491 VideoEncoderConfig encoder_config, | 426 const VideoEncoderConfig& encoder_config, |
| 492 const std::map<uint32_t, RtpState>& suspended_ssrcs) | 427 const std::map<uint32_t, RtpState>& suspended_ssrcs) |
| 493 : worker_queue_(worker_queue), | 428 : stats_proxy_(Clock::GetRealTimeClock(), |
| 494 thread_sync_event_(false /* manual_reset */, false), | |
| 495 stats_proxy_(Clock::GetRealTimeClock(), | |
| 496 config, | 429 config, |
| 497 encoder_config.content_type), | 430 encoder_config.content_type), |
| 498 config_(std::move(config)) { | |
| 499 vie_encoder_.reset( | |
| 500 new ViEEncoder(num_cpu_cores, &stats_proxy_, config_.encoder_settings, | |
| 501 config_.pre_encode_callback, config_.overuse_callback, | |
| 502 config_.post_encode_callback)); | |
| 503 | |
| 504 worker_queue_->PostTask(std::unique_ptr<rtc::QueuedTask>(new ConstructionTask( | |
| 505 &send_stream_, &thread_sync_event_, &stats_proxy_, vie_encoder_.get(), | |
| 506 module_process_thread, call_stats, congestion_controller, | |
| 507 bitrate_allocator, send_delay_stats, remb, event_log, &config_, | |
| 508 suspended_ssrcs))); | |
| 509 | |
| 510 // Wait for ConstructionTask to complete so that |send_stream_| can be used. | |
| 511 // |module_process_thread| must be registered and deregistered on the thread | |
| 512 // it was created on. | |
| 513 thread_sync_event_.Wait(rtc::Event::kForever); | |
| 514 send_stream_->RegisterProcessThread(module_process_thread); | |
| 515 | |
| 516 vie_encoder_->RegisterProcessThread(module_process_thread); | |
| 517 | |
| 518 ReconfigureVideoEncoder(std::move(encoder_config)); | |
| 519 } | |
| 520 | |
| 521 VideoSendStream::~VideoSendStream() { | |
| 522 RTC_DCHECK_RUN_ON(&thread_checker_); | |
| 523 RTC_DCHECK(!send_stream_); | |
| 524 } | |
| 525 | |
| 526 void VideoSendStream::Start() { | |
| 527 RTC_DCHECK_RUN_ON(&thread_checker_); | |
| 528 LOG(LS_INFO) << "VideoSendStream::Start"; | |
| 529 VideoSendStreamImpl* send_stream = send_stream_.get(); | |
| 530 worker_queue_->PostTask([this, send_stream] { | |
| 531 send_stream->Start(); | |
| 532 thread_sync_event_.Set(); | |
| 533 }); | |
| 534 | |
| 535 // It is expected that after VideoSendStream::Start has been called, incoming | |
| 536 // frames are not dropped in ViEEncoder. To ensure this, Start has to be | |
| 537 // synchronized. | |
| 538 thread_sync_event_.Wait(rtc::Event::kForever); | |
| 539 } | |
| 540 | |
| 541 void VideoSendStream::Stop() { | |
| 542 RTC_DCHECK_RUN_ON(&thread_checker_); | |
| 543 LOG(LS_INFO) << "VideoSendStream::Stop"; | |
| 544 VideoSendStreamImpl* send_stream = send_stream_.get(); | |
| 545 worker_queue_->PostTask([send_stream] { send_stream->Stop(); }); | |
| 546 } | |
| 547 | |
| 548 VideoCaptureInput* VideoSendStream::Input() { | |
| 549 // Input() will be called on the thread that deliverers video frames from | |
| 550 // libjingle. | |
| 551 // TODO(perkj): Refactor ViEEncoder to register directly as a VideoSink to the | |
| 552 // VideoSource. | |
| 553 return vie_encoder_.get(); | |
| 554 } | |
| 555 | |
| 556 void VideoSendStream::ReconfigureVideoEncoder(VideoEncoderConfig config) { | |
| 557 // ReconfigureVideoEncoder will be called on the thread that deliverers video | |
| 558 // frames. We must change the encoder settings immediately so that | |
| 559 // the codec settings matches the next frame. | |
| 560 // TODO(perkj): Move logic for reconfiguration the encoder due to frame size | |
| 561 // change from WebRtcVideoChannel2::WebRtcVideoSendStream::OnFrame to | |
| 562 // be internally handled by ViEEncoder. | |
| 563 vie_encoder_->ConfigureEncoder(config, config_.rtp.max_packet_size); | |
| 564 | |
| 565 worker_queue_->PostTask(std::unique_ptr<rtc::QueuedTask>( | |
| 566 new ReconfigureVideoEncoderTask(send_stream_.get(), std::move(config)))); | |
| 567 } | |
| 568 | |
| 569 VideoSendStream::Stats VideoSendStream::GetStats() { | |
| 570 // TODO(perkj, solenberg): Some test cases in EndToEndTest call GetStats from | |
| 571 // a network thread. See comment in Call::GetStats(). | |
| 572 // RTC_DCHECK_RUN_ON(&thread_checker_); | |
| 573 return stats_proxy_.GetStats(); | |
| 574 } | |
| 575 | |
| 576 void VideoSendStream::SignalNetworkState(NetworkState state) { | |
| 577 RTC_DCHECK_RUN_ON(&thread_checker_); | |
| 578 VideoSendStreamImpl* send_stream = send_stream_.get(); | |
| 579 worker_queue_->PostTask( | |
| 580 [send_stream, state] { send_stream->SignalNetworkState(state); }); | |
| 581 } | |
| 582 | |
| 583 VideoSendStream::RtpStateMap VideoSendStream::StopPermanentlyAndGetRtpStates() { | |
| 584 RTC_DCHECK_RUN_ON(&thread_checker_); | |
| 585 vie_encoder_->Stop(); | |
| 586 vie_encoder_->DeRegisterProcessThread(); | |
| 587 VideoSendStream::RtpStateMap state_map; | |
| 588 send_stream_->DeRegisterProcessThread(); | |
| 589 worker_queue_->PostTask( | |
| 590 std::unique_ptr<rtc::QueuedTask>(new DestructAndGetRtpStateTask( | |
| 591 &state_map, std::move(send_stream_), &thread_sync_event_))); | |
| 592 thread_sync_event_.Wait(rtc::Event::kForever); | |
| 593 return state_map; | |
| 594 } | |
| 595 | |
| 596 bool VideoSendStream::DeliverRtcp(const uint8_t* packet, size_t length) { | |
| 597 // Called on a network thread. | |
| 598 return send_stream_->DeliverRtcp(packet, length); | |
| 599 } | |
| 600 | |
| 601 VideoSendStreamImpl::VideoSendStreamImpl( | |
| 602 SendStatisticsProxy* stats_proxy, | |
| 603 rtc::TaskQueue* worker_queue, | |
| 604 CallStats* call_stats, | |
| 605 CongestionController* congestion_controller, | |
| 606 BitrateAllocator* bitrate_allocator, | |
| 607 SendDelayStats* send_delay_stats, | |
| 608 VieRemb* remb, | |
| 609 ViEEncoder* vie_encoder, | |
| 610 RtcEventLog* event_log, | |
| 611 const VideoSendStream::Config* config, | |
| 612 std::map<uint32_t, RtpState> suspended_ssrcs) | |
| 613 : stats_proxy_(stats_proxy), | |
| 614 config_(config), | 431 config_(config), |
| 615 suspended_ssrcs_(std::move(suspended_ssrcs)), | 432 suspended_ssrcs_(suspended_ssrcs), |
| 616 module_process_thread_(nullptr), | 433 module_process_thread_(module_process_thread), |
| 617 worker_queue_(worker_queue), | |
| 618 check_encoder_activity_task_(nullptr), | |
| 619 call_stats_(call_stats), | 434 call_stats_(call_stats), |
| 620 congestion_controller_(congestion_controller), | 435 congestion_controller_(congestion_controller), |
| 621 bitrate_allocator_(bitrate_allocator), | 436 bitrate_allocator_(bitrate_allocator), |
| 622 remb_(remb), | 437 remb_(remb), |
| 623 max_padding_bitrate_(0), | 438 encoder_thread_(EncoderThreadFunction, this, "EncoderThread"), |
| 624 encoder_min_bitrate_bps_(0), | 439 encoder_wakeup_event_(false, false), |
| 440 stop_encoder_thread_(0), |
| 625 encoder_max_bitrate_bps_(0), | 441 encoder_max_bitrate_bps_(0), |
| 626 encoder_target_rate_bps_(0), | 442 encoder_target_rate_bps_(0), |
| 627 vie_encoder_(vie_encoder), | 443 state_(State::kStopped), |
| 444 overuse_detector_( |
| 445 Clock::GetRealTimeClock(), |
| 446 GetCpuOveruseOptions(config.encoder_settings.full_overuse_time), |
| 447 this, |
| 448 config.post_encode_callback, |
| 449 &stats_proxy_), |
| 450 vie_encoder_(num_cpu_cores, |
| 451 module_process_thread_, |
| 452 &stats_proxy_, |
| 453 &overuse_detector_, |
| 454 this), |
| 628 encoder_feedback_(Clock::GetRealTimeClock(), | 455 encoder_feedback_(Clock::GetRealTimeClock(), |
| 629 config_->rtp.ssrcs, | 456 config.rtp.ssrcs, |
| 630 vie_encoder), | 457 &vie_encoder_), |
| 631 protection_bitrate_calculator_(Clock::GetRealTimeClock(), this), | 458 protection_bitrate_calculator_(Clock::GetRealTimeClock(), this), |
| 459 video_sender_(vie_encoder_.video_sender()), |
| 632 bandwidth_observer_(congestion_controller_->GetBitrateController() | 460 bandwidth_observer_(congestion_controller_->GetBitrateController() |
| 633 ->CreateRtcpBandwidthObserver()), | 461 ->CreateRtcpBandwidthObserver()), |
| 634 rtp_rtcp_modules_(CreateRtpRtcpModules( | 462 rtp_rtcp_modules_(CreateRtpRtcpModules( |
| 635 config_->send_transport, | 463 config.send_transport, |
| 636 &encoder_feedback_, | 464 &encoder_feedback_, |
| 637 bandwidth_observer_.get(), | 465 bandwidth_observer_.get(), |
| 638 congestion_controller_->GetTransportFeedbackObserver(), | 466 congestion_controller_->GetTransportFeedbackObserver(), |
| 639 call_stats_->rtcp_rtt_stats(), | 467 call_stats_->rtcp_rtt_stats(), |
| 640 congestion_controller_->pacer(), | 468 congestion_controller_->pacer(), |
| 641 congestion_controller_->packet_router(), | 469 congestion_controller_->packet_router(), |
| 642 stats_proxy_, | 470 &stats_proxy_, |
| 643 send_delay_stats, | 471 send_delay_stats, |
| 644 event_log, | 472 event_log, |
| 645 congestion_controller_->GetRetransmissionRateLimiter(), | 473 congestion_controller_->GetRetransmissionRateLimiter(), |
| 646 config_->rtp.ssrcs.size())), | 474 config_.rtp.ssrcs.size())), |
| 647 payload_router_(rtp_rtcp_modules_, | 475 payload_router_(rtp_rtcp_modules_, config.encoder_settings.payload_type), |
| 648 config_->encoder_settings.payload_type) { | 476 input_(&encoder_wakeup_event_, |
| 649 RTC_DCHECK_RUN_ON(worker_queue_); | 477 config_.local_renderer, |
| 650 LOG(LS_INFO) << "VideoSendStreamInternal: " << config_->ToString(); | 478 &stats_proxy_, |
| 651 module_process_thread_checker_.DetachFromThread(); | 479 &overuse_detector_) { |
| 480 LOG(LS_INFO) << "VideoSendStream: " << config_.ToString(); |
| 652 | 481 |
| 653 RTC_DCHECK(!config_->rtp.ssrcs.empty()); | 482 RTC_DCHECK(!config_.rtp.ssrcs.empty()); |
| 483 RTC_DCHECK(module_process_thread_); |
| 654 RTC_DCHECK(call_stats_); | 484 RTC_DCHECK(call_stats_); |
| 655 RTC_DCHECK(congestion_controller_); | 485 RTC_DCHECK(congestion_controller_); |
| 656 RTC_DCHECK(remb_); | 486 RTC_DCHECK(remb_); |
| 657 | 487 |
| 658 // RTP/RTCP initialization. | 488 // RTP/RTCP initialization. |
| 659 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) { | 489 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) { |
| 490 module_process_thread_->RegisterModule(rtp_rtcp); |
| 660 congestion_controller_->packet_router()->AddRtpModule(rtp_rtcp); | 491 congestion_controller_->packet_router()->AddRtpModule(rtp_rtcp); |
| 661 } | 492 } |
| 662 | 493 |
| 663 for (size_t i = 0; i < config_->rtp.extensions.size(); ++i) { | 494 for (size_t i = 0; i < config_.rtp.extensions.size(); ++i) { |
| 664 const std::string& extension = config_->rtp.extensions[i].uri; | 495 const std::string& extension = config_.rtp.extensions[i].uri; |
| 665 int id = config_->rtp.extensions[i].id; | 496 int id = config_.rtp.extensions[i].id; |
| 666 // One-byte-extension local identifiers are in the range 1-14 inclusive. | 497 // One-byte-extension local identifiers are in the range 1-14 inclusive. |
| 667 RTC_DCHECK_GE(id, 1); | 498 RTC_DCHECK_GE(id, 1); |
| 668 RTC_DCHECK_LE(id, 14); | 499 RTC_DCHECK_LE(id, 14); |
| 669 RTC_DCHECK(RtpExtension::IsSupportedForVideo(extension)); | 500 RTC_DCHECK(RtpExtension::IsSupportedForVideo(extension)); |
| 670 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) { | 501 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) { |
| 671 RTC_CHECK_EQ(0, rtp_rtcp->RegisterSendRtpHeaderExtension( | 502 RTC_CHECK_EQ(0, rtp_rtcp->RegisterSendRtpHeaderExtension( |
| 672 StringToRtpExtensionType(extension), id)); | 503 StringToRtpExtensionType(extension), id)); |
| 673 } | 504 } |
| 674 } | 505 } |
| 675 | 506 |
| 676 remb_->AddRembSender(rtp_rtcp_modules_[0]); | 507 remb_->AddRembSender(rtp_rtcp_modules_[0]); |
| 677 rtp_rtcp_modules_[0]->SetREMBStatus(true); | 508 rtp_rtcp_modules_[0]->SetREMBStatus(true); |
| 678 | 509 |
| 679 ConfigureProtection(); | 510 ConfigureProtection(); |
| 680 ConfigureSsrcs(); | 511 ConfigureSsrcs(); |
| 681 | 512 |
| 682 // TODO(pbos): Should we set CNAME on all RTP modules? | 513 // TODO(pbos): Should we set CNAME on all RTP modules? |
| 683 rtp_rtcp_modules_.front()->SetCNAME(config_->rtp.c_name.c_str()); | 514 rtp_rtcp_modules_.front()->SetCNAME(config_.rtp.c_name.c_str()); |
| 684 // 28 to match packet overhead in ModuleRtpRtcpImpl. | 515 // 28 to match packet overhead in ModuleRtpRtcpImpl. |
| 685 static const size_t kRtpPacketSizeOverhead = 28; | 516 static const size_t kRtpPacketSizeOverhead = 28; |
| 686 RTC_DCHECK_LE(config_->rtp.max_packet_size, 0xFFFFu + kRtpPacketSizeOverhead); | 517 RTC_DCHECK_LE(config_.rtp.max_packet_size, 0xFFFFu + kRtpPacketSizeOverhead); |
| 687 const uint16_t mtu = static_cast<uint16_t>(config_->rtp.max_packet_size + | 518 const uint16_t mtu = static_cast<uint16_t>(config_.rtp.max_packet_size + |
| 688 kRtpPacketSizeOverhead); | 519 kRtpPacketSizeOverhead); |
| 689 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) { | 520 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) { |
| 690 rtp_rtcp->RegisterRtcpStatisticsCallback(stats_proxy_); | 521 rtp_rtcp->RegisterRtcpStatisticsCallback(&stats_proxy_); |
| 691 rtp_rtcp->RegisterSendChannelRtpStatisticsCallback(stats_proxy_); | 522 rtp_rtcp->RegisterSendChannelRtpStatisticsCallback(&stats_proxy_); |
| 692 rtp_rtcp->SetMaxTransferUnit(mtu); | 523 rtp_rtcp->SetMaxTransferUnit(mtu); |
| 693 rtp_rtcp->RegisterVideoSendPayload( | 524 rtp_rtcp->RegisterVideoSendPayload( |
| 694 config_->encoder_settings.payload_type, | 525 config_.encoder_settings.payload_type, |
| 695 config_->encoder_settings.payload_name.c_str()); | 526 config_.encoder_settings.payload_name.c_str()); |
| 696 } | 527 } |
| 697 | 528 |
| 698 RTC_DCHECK(config_->encoder_settings.encoder); | 529 RTC_DCHECK(config.encoder_settings.encoder); |
| 699 RTC_DCHECK_GE(config_->encoder_settings.payload_type, 0); | 530 RTC_DCHECK_GE(config.encoder_settings.payload_type, 0); |
| 700 RTC_DCHECK_LE(config_->encoder_settings.payload_type, 127); | 531 RTC_DCHECK_LE(config.encoder_settings.payload_type, 127); |
| 701 | 532 ReconfigureVideoEncoder(encoder_config); |
| 702 vie_encoder_->SetStartBitrate(bitrate_allocator_->GetStartBitrate(this)); | 533 |
| 703 vie_encoder_->SetSink(this); | 534 module_process_thread_->RegisterModule(&overuse_detector_); |
| 704 } | 535 |
| 705 | 536 encoder_thread_checker_.DetachFromThread(); |
| 706 void VideoSendStreamImpl::RegisterProcessThread( | 537 encoder_thread_.Start(); |
| 707 ProcessThread* module_process_thread) { | 538 encoder_thread_.SetPriority(rtc::kHighPriority); |
| 708 RTC_DCHECK_RUN_ON(&module_process_thread_checker_); | 539 } |
| 709 RTC_DCHECK(!module_process_thread_); | 540 |
| 710 module_process_thread_ = module_process_thread; | 541 VideoSendStream::~VideoSendStream() { |
| 711 | 542 LOG(LS_INFO) << "~VideoSendStream: " << config_.ToString(); |
| 712 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) | 543 |
| 713 module_process_thread_->RegisterModule(rtp_rtcp); | 544 Stop(); |
| 714 } | 545 |
| 715 | 546 // Stop the encoder thread permanently. |
| 716 void VideoSendStreamImpl::DeRegisterProcessThread() { | 547 rtc::AtomicOps::ReleaseStore(&stop_encoder_thread_, 1); |
| 717 RTC_DCHECK_RUN_ON(&module_process_thread_checker_); | 548 encoder_wakeup_event_.Set(); |
| 718 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) | 549 encoder_thread_.Stop(); |
| 719 module_process_thread_->DeRegisterModule(rtp_rtcp); | 550 |
| 720 } | 551 // This needs to happen after stopping the encoder thread, |
| 721 | 552 // since the encoder thread calls AddObserver. |
| 722 VideoSendStreamImpl::~VideoSendStreamImpl() { | 553 bitrate_allocator_->RemoveObserver(this); |
| 723 RTC_DCHECK_RUN_ON(worker_queue_); | 554 |
| 724 RTC_DCHECK(!payload_router_.active()) | 555 module_process_thread_->DeRegisterModule(&overuse_detector_); |
| 725 << "VideoSendStreamImpl::Stop not called"; | |
| 726 LOG(LS_INFO) << "~VideoSendStreamInternal: " << config_->ToString(); | |
| 727 | 556 |
| 728 rtp_rtcp_modules_[0]->SetREMBStatus(false); | 557 rtp_rtcp_modules_[0]->SetREMBStatus(false); |
| 729 remb_->RemoveRembSender(rtp_rtcp_modules_[0]); | 558 remb_->RemoveRembSender(rtp_rtcp_modules_[0]); |
| 730 | 559 |
| 731 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) { | 560 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) { |
| 732 congestion_controller_->packet_router()->RemoveRtpModule(rtp_rtcp); | 561 congestion_controller_->packet_router()->RemoveRtpModule(rtp_rtcp); |
| 562 module_process_thread_->DeRegisterModule(rtp_rtcp); |
| 733 delete rtp_rtcp; | 563 delete rtp_rtcp; |
| 734 } | 564 } |
| 735 } | 565 } |
| 736 | 566 |
| 737 bool VideoSendStreamImpl::DeliverRtcp(const uint8_t* packet, size_t length) { | 567 bool VideoSendStream::DeliverRtcp(const uint8_t* packet, size_t length) { |
| 738 // Runs on a network thread. | |
| 739 RTC_DCHECK(!worker_queue_->IsCurrent()); | |
| 740 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) | 568 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) |
| 741 rtp_rtcp->IncomingRtcpPacket(packet, length); | 569 rtp_rtcp->IncomingRtcpPacket(packet, length); |
| 742 return true; | 570 return true; |
| 743 } | 571 } |
| 744 | 572 |
| 745 void VideoSendStreamImpl::Start() { | 573 void VideoSendStream::Start() { |
| 746 RTC_DCHECK_RUN_ON(worker_queue_); | |
| 747 LOG(LS_INFO) << "VideoSendStream::Start"; | 574 LOG(LS_INFO) << "VideoSendStream::Start"; |
| 748 if (payload_router_.active()) | 575 if (payload_router_.active()) |
| 749 return; | 576 return; |
| 750 TRACE_EVENT_INSTANT0("webrtc", "VideoSendStream::Start"); | 577 TRACE_EVENT_INSTANT0("webrtc", "VideoSendStream::Start"); |
| 751 payload_router_.set_active(true); | 578 payload_router_.set_active(true); |
| 752 | |
| 753 bitrate_allocator_->AddObserver( | |
| 754 this, encoder_min_bitrate_bps_, encoder_max_bitrate_bps_, | |
| 755 max_padding_bitrate_, !config_->suspend_below_min_bitrate); | |
| 756 | |
| 757 // Start monitoring encoder activity. | |
| 758 { | 579 { |
| 759 rtc::CritScope lock(&encoder_activity_crit_sect_); | 580 rtc::CritScope lock(&encoder_settings_crit_); |
| 760 RTC_DCHECK(!check_encoder_activity_task_); | 581 pending_state_change_ = rtc::Optional<State>(State::kStarted); |
| 761 check_encoder_activity_task_ = new CheckEncoderActivityTask(this); | 582 } |
| 762 worker_queue_->PostDelayedTask( | 583 encoder_wakeup_event_.Set(); |
| 763 std::unique_ptr<rtc::QueuedTask>(check_encoder_activity_task_), | 584 } |
| 764 CheckEncoderActivityTask::kEncoderTimeOutMs); | 585 |
| 765 } | 586 void VideoSendStream::Stop() { |
| 766 | |
| 767 vie_encoder_->SendKeyFrame(); | |
| 768 } | |
| 769 | |
| 770 void VideoSendStreamImpl::Stop() { | |
| 771 RTC_DCHECK_RUN_ON(worker_queue_); | |
| 772 LOG(LS_INFO) << "VideoSendStream::Stop"; | 587 LOG(LS_INFO) << "VideoSendStream::Stop"; |
| 773 if (!payload_router_.active()) | 588 if (!payload_router_.active()) |
| 774 return; | 589 return; |
| 775 TRACE_EVENT_INSTANT0("webrtc", "VideoSendStream::Stop"); | 590 TRACE_EVENT_INSTANT0("webrtc", "VideoSendStream::Stop"); |
| 776 payload_router_.set_active(false); | 591 payload_router_.set_active(false); |
| 777 bitrate_allocator_->RemoveObserver(this); | |
| 778 { | 592 { |
| 779 rtc::CritScope lock(&encoder_activity_crit_sect_); | 593 rtc::CritScope lock(&encoder_settings_crit_); |
| 780 check_encoder_activity_task_->Stop(); | 594 pending_state_change_ = rtc::Optional<State>(State::kStopped); |
| 781 check_encoder_activity_task_ = nullptr; | 595 } |
| 782 } | 596 encoder_wakeup_event_.Set(); |
| 783 vie_encoder_->OnBitrateUpdated(0, 0, 0); | 597 } |
| 784 stats_proxy_->OnSetEncoderTargetRate(0); | 598 |
| 785 } | 599 VideoCaptureInput* VideoSendStream::Input() { |
| 786 | 600 return &input_; |
| 787 void VideoSendStreamImpl::SignalEncoderTimedOut() { | 601 } |
| 788 RTC_DCHECK_RUN_ON(worker_queue_); | 602 |
| 789 // If the encoder has not produced anything the last kEncoderTimeOutMs and it | 603 bool VideoSendStream::EncoderThreadFunction(void* obj) { |
| 790 // is supposed to, deregister as BitrateAllocatorObserver. This can happen | 604 static_cast<VideoSendStream*>(obj)->EncoderProcess(); |
| 791 // if a camera stops producing frames. | 605 // We're done, return false to abort. |
| 792 if (encoder_target_rate_bps_ > 0) { | 606 return false; |
| 793 LOG(LS_INFO) << "SignalEncoderTimedOut, Encoder timed out."; | 607 } |
| 794 bitrate_allocator_->RemoveObserver(this); | 608 |
| 795 } | 609 void VideoSendStream::EncoderProcess() { |
| 796 } | 610 RTC_CHECK_EQ(0, vie_encoder_.RegisterExternalEncoder( |
| 797 | 611 config_.encoder_settings.encoder, |
| 798 void VideoSendStreamImpl::SignalEncoderActive() { | 612 config_.encoder_settings.payload_type, |
| 799 RTC_DCHECK_RUN_ON(worker_queue_); | 613 config_.encoder_settings.internal_source)); |
| 800 LOG(LS_INFO) << "SignalEncoderActive, Encoder is active."; | 614 RTC_DCHECK_RUN_ON(&encoder_thread_checker_); |
| 801 bitrate_allocator_->AddObserver( | 615 while (true) { |
| 802 this, encoder_min_bitrate_bps_, encoder_max_bitrate_bps_, | 616 // Wake up every kEncodeCheckForActivityPeriodMs to check if the encoder is |
| 803 max_padding_bitrate_, !config_->suspend_below_min_bitrate); | 617 // active. If not, deregister as BitrateAllocatorObserver. |
| 804 } | 618 const int kEncodeCheckForActivityPeriodMs = 1000; |
| 805 | 619 encoder_wakeup_event_.Wait(kEncodeCheckForActivityPeriodMs); |
| 806 void VideoSendStreamImpl::SignalEncoderConfigurationChanged( | 620 if (rtc::AtomicOps::AcquireLoad(&stop_encoder_thread_)) |
| 621 break; |
| 622 bool change_settings = false; |
| 623 rtc::Optional<State> pending_state_change; |
| 624 { |
| 625 rtc::CritScope lock(&encoder_settings_crit_); |
| 626 if (pending_encoder_settings_) { |
| 627 std::swap(current_encoder_settings_, pending_encoder_settings_); |
| 628 pending_encoder_settings_.reset(); |
| 629 change_settings = true; |
| 630 } else if (pending_state_change_) { |
| 631 swap(pending_state_change, pending_state_change_); |
| 632 } |
| 633 } |
| 634 if (change_settings) { |
| 635 current_encoder_settings_->video_codec.startBitrate = std::max( |
| 636 bitrate_allocator_->GetStartBitrate(this) / 1000, |
| 637 static_cast<int>(current_encoder_settings_->video_codec.minBitrate)); |
| 638 |
| 639 if (state_ == State::kStarted) { |
| 640 bitrate_allocator_->AddObserver( |
| 641 this, current_encoder_settings_->video_codec.minBitrate * 1000, |
| 642 current_encoder_settings_->video_codec.maxBitrate * 1000, |
| 643 CalulcateMaxPadBitrateBps(current_encoder_settings_->config, |
| 644 config_.suspend_below_min_bitrate), |
| 645 !config_.suspend_below_min_bitrate); |
| 646 } |
| 647 |
| 648 payload_router_.SetSendStreams(current_encoder_settings_->config.streams); |
| 649 vie_encoder_.SetEncoder(current_encoder_settings_->video_codec, |
| 650 payload_router_.MaxPayloadLength()); |
| 651 |
| 652 // Clear stats for disabled layers. |
| 653 for (size_t i = current_encoder_settings_->config.streams.size(); |
| 654 i < config_.rtp.ssrcs.size(); ++i) { |
| 655 stats_proxy_.OnInactiveSsrc(config_.rtp.ssrcs[i]); |
| 656 } |
| 657 |
| 658 size_t number_of_temporal_layers = |
| 659 current_encoder_settings_->config.streams.back() |
| 660 .temporal_layer_thresholds_bps.size() + |
| 661 1; |
| 662 protection_bitrate_calculator_.SetEncodingData( |
| 663 current_encoder_settings_->video_codec.width, |
| 664 current_encoder_settings_->video_codec.height, |
| 665 number_of_temporal_layers, payload_router_.MaxPayloadLength()); |
| 666 |
| 667 // We might've gotten new settings while configuring the encoder settings, |
| 668 // restart from the top to see if that's the case before trying to encode |
| 669 // a frame (which might correspond to the last frame size). |
| 670 encoder_wakeup_event_.Set(); |
| 671 continue; |
| 672 } |
| 673 |
| 674 if (pending_state_change) { |
| 675 if (*pending_state_change == State::kStarted && |
| 676 state_ == State::kStopped) { |
| 677 bitrate_allocator_->AddObserver( |
| 678 this, current_encoder_settings_->video_codec.minBitrate * 1000, |
| 679 current_encoder_settings_->video_codec.maxBitrate * 1000, |
| 680 CalulcateMaxPadBitrateBps(current_encoder_settings_->config, |
| 681 config_.suspend_below_min_bitrate), |
| 682 !config_.suspend_below_min_bitrate); |
| 683 vie_encoder_.SendKeyFrame(); |
| 684 state_ = State::kStarted; |
| 685 LOG_F(LS_INFO) << "Encoder started."; |
| 686 } else if (*pending_state_change == State::kStopped) { |
| 687 bitrate_allocator_->RemoveObserver(this); |
| 688 vie_encoder_.OnBitrateUpdated(0, 0, 0); |
| 689 stats_proxy_.OnSetEncoderTargetRate(0); |
| 690 state_ = State::kStopped; |
| 691 LOG_F(LS_INFO) << "Encoder stopped."; |
| 692 } |
| 693 encoder_wakeup_event_.Set(); |
| 694 continue; |
| 695 } |
| 696 |
| 697 // Check if the encoder has produced anything the last kEncoderTimeOutMs. |
| 698 // If not, deregister as BitrateAllocatorObserver. |
| 699 if (state_ == State::kStarted && |
| 700 vie_encoder_.time_of_last_frame_activity_ms() < |
| 701 rtc::TimeMillis() - kEncoderTimeOutMs) { |
| 702 // The encoder has timed out. |
| 703 LOG_F(LS_INFO) << "Encoder timed out."; |
| 704 bitrate_allocator_->RemoveObserver(this); |
| 705 state_ = State::kEncoderTimedOut; |
| 706 } |
| 707 if (state_ == State::kEncoderTimedOut && |
| 708 vie_encoder_.time_of_last_frame_activity_ms() > |
| 709 rtc::TimeMillis() - kEncoderTimeOutMs) { |
| 710 LOG_F(LS_INFO) << "Encoder is active."; |
| 711 bitrate_allocator_->AddObserver( |
| 712 this, current_encoder_settings_->video_codec.minBitrate * 1000, |
| 713 current_encoder_settings_->video_codec.maxBitrate * 1000, |
| 714 CalulcateMaxPadBitrateBps(current_encoder_settings_->config, |
| 715 config_.suspend_below_min_bitrate), |
| 716 !config_.suspend_below_min_bitrate); |
| 717 state_ = State::kStarted; |
| 718 } |
| 719 |
| 720 VideoFrame frame; |
| 721 if (input_.GetVideoFrame(&frame)) { |
| 722 // TODO(perkj): |pre_encode_callback| is only used by tests. Tests should |
| 723 // register as a sink to the VideoSource instead. |
| 724 if (config_.pre_encode_callback) { |
| 725 config_.pre_encode_callback->OnFrame(frame); |
| 726 } |
| 727 vie_encoder_.EncodeVideoFrame(frame); |
| 728 } |
| 729 } |
| 730 vie_encoder_.DeRegisterExternalEncoder(config_.encoder_settings.payload_type); |
| 731 } |
| 732 |
| 733 void VideoSendStream::ReconfigureVideoEncoder( |
| 807 const VideoEncoderConfig& config) { | 734 const VideoEncoderConfig& config) { |
| 808 RTC_DCHECK_GE(config_->rtp.ssrcs.size(), config.streams.size()); | 735 TRACE_EVENT0("webrtc", "VideoSendStream::(Re)configureVideoEncoder"); |
| 809 TRACE_EVENT0("webrtc", "VideoSendStream::SignalEncoderConfigurationChanged"); | 736 LOG(LS_INFO) << "(Re)configureVideoEncoder: " << config.ToString(); |
| 810 LOG(LS_INFO) << "SignalEncoderConfigurationChanged: " << config.ToString(); | 737 RTC_DCHECK_GE(config_.rtp.ssrcs.size(), config.streams.size()); |
| 811 RTC_DCHECK_GE(config_->rtp.ssrcs.size(), config.streams.size()); | 738 VideoCodec video_codec = VideoEncoderConfigToVideoCodec( |
| 812 RTC_DCHECK_RUN_ON(worker_queue_); | 739 config, config_.encoder_settings.payload_name, |
| 813 | 740 config_.encoder_settings.payload_type); |
| 814 const int kEncoderMinBitrateBps = 30000; | 741 { |
| 815 encoder_min_bitrate_bps_ = | 742 rtc::CritScope lock(&encoder_settings_crit_); |
| 816 std::max(config.streams[0].min_bitrate_bps, kEncoderMinBitrateBps); | 743 encoder_max_bitrate_bps_ = video_codec.maxBitrate * 1000; |
| 817 encoder_max_bitrate_bps_ = 0; | 744 pending_encoder_settings_.reset(new EncoderSettings({video_codec, config})); |
| 818 for (const auto& stream : config.streams) | 745 } |
| 819 encoder_max_bitrate_bps_ += stream.max_bitrate_bps; | 746 encoder_wakeup_event_.Set(); |
| 820 max_padding_bitrate_ = | 747 } |
| 821 CalculateMaxPadBitrateBps(config, config_->suspend_below_min_bitrate); | 748 |
| 822 | 749 VideoSendStream::Stats VideoSendStream::GetStats() { |
| 823 payload_router_.SetSendStreams(config.streams); | 750 return stats_proxy_.GetStats(); |
| 824 | 751 } |
| 825 // Clear stats for disabled layers. | 752 |
| 826 for (size_t i = config.streams.size(); i < config_->rtp.ssrcs.size(); ++i) { | 753 void VideoSendStream::OveruseDetected() { |
| 827 stats_proxy_->OnInactiveSsrc(config_->rtp.ssrcs[i]); | 754 if (config_.overuse_callback) |
| 828 } | 755 config_.overuse_callback->OnLoadUpdate(LoadObserver::kOveruse); |
| 829 | 756 } |
| 830 size_t number_of_temporal_layers = | 757 |
| 831 config.streams.back().temporal_layer_thresholds_bps.size() + 1; | 758 void VideoSendStream::NormalUsage() { |
| 832 protection_bitrate_calculator_.SetEncodingData( | 759 if (config_.overuse_callback) |
| 833 config.streams[0].width, config.streams[0].height, | 760 config_.overuse_callback->OnLoadUpdate(LoadObserver::kUnderuse); |
| 834 number_of_temporal_layers, config_->rtp.max_packet_size); | 761 } |
| 835 | 762 |
| 836 if (payload_router_.active()) { | 763 EncodedImageCallback::Result VideoSendStream::OnEncodedImage( |
| 837 // The send stream is started already. Update the allocator with new bitrate | |
| 838 // limits. | |
| 839 bitrate_allocator_->AddObserver( | |
| 840 this, encoder_min_bitrate_bps_, encoder_max_bitrate_bps_, | |
| 841 max_padding_bitrate_, !config_->suspend_below_min_bitrate); | |
| 842 } | |
| 843 } | |
| 844 | |
| 845 EncodedImageCallback::Result VideoSendStreamImpl::OnEncodedImage( | |
| 846 const EncodedImage& encoded_image, | 764 const EncodedImage& encoded_image, |
| 847 const CodecSpecificInfo* codec_specific_info, | 765 const CodecSpecificInfo* codec_specific_info, |
| 848 const RTPFragmentationHeader* fragmentation) { | 766 const RTPFragmentationHeader* fragmentation) { |
| 849 // Encoded is called on whatever thread the real encoder implementation run | 767 if (config_.post_encode_callback) { |
| 850 // on. In the case of hardware encoders, there might be several encoders | 768 config_.post_encode_callback->EncodedFrameCallback( |
| 851 // running in parallel on different threads. | |
| 852 if (config_->post_encode_callback) { | |
| 853 config_->post_encode_callback->EncodedFrameCallback( | |
| 854 EncodedFrame(encoded_image._buffer, encoded_image._length, | 769 EncodedFrame(encoded_image._buffer, encoded_image._length, |
| 855 encoded_image._frameType)); | 770 encoded_image._frameType)); |
| 856 } | 771 } |
| 857 { | |
| 858 rtc::CritScope lock(&encoder_activity_crit_sect_); | |
| 859 if (check_encoder_activity_task_) | |
| 860 check_encoder_activity_task_->UpdateEncoderActivity(); | |
| 861 } | |
| 862 | 772 |
| 863 protection_bitrate_calculator_.UpdateWithEncodedData(encoded_image); | 773 protection_bitrate_calculator_.UpdateWithEncodedData(encoded_image); |
| 864 EncodedImageCallback::Result result = payload_router_.OnEncodedImage( | 774 EncodedImageCallback::Result result = payload_router_.OnEncodedImage( |
| 865 encoded_image, codec_specific_info, fragmentation); | 775 encoded_image, codec_specific_info, fragmentation); |
| 866 | 776 |
| 867 if (kEnableFrameRecording) { | 777 if (kEnableFrameRecording) { |
| 868 int layer = codec_specific_info->codecType == kVideoCodecVP8 | 778 int layer = codec_specific_info->codecType == kVideoCodecVP8 |
| 869 ? codec_specific_info->codecSpecific.VP8.simulcastIdx | 779 ? codec_specific_info->codecSpecific.VP8.simulcastIdx |
| 870 : 0; | 780 : 0; |
| 871 IvfFileWriter* file_writer; | 781 IvfFileWriter* file_writer; |
| 872 { | 782 { |
| 873 if (file_writers_[layer] == nullptr) { | 783 if (file_writers_[layer] == nullptr) { |
| 874 std::ostringstream oss; | 784 std::ostringstream oss; |
| 875 oss << "send_bitstream_ssrc"; | 785 oss << "send_bitstream_ssrc"; |
| 876 for (uint32_t ssrc : config_->rtp.ssrcs) | 786 for (uint32_t ssrc : config_.rtp.ssrcs) |
| 877 oss << "_" << ssrc; | 787 oss << "_" << ssrc; |
| 878 oss << "_layer" << layer << ".ivf"; | 788 oss << "_layer" << layer << ".ivf"; |
| 879 file_writers_[layer] = | 789 file_writers_[layer] = |
| 880 IvfFileWriter::Open(oss.str(), codec_specific_info->codecType); | 790 IvfFileWriter::Open(oss.str(), codec_specific_info->codecType); |
| 881 } | 791 } |
| 882 file_writer = file_writers_[layer].get(); | 792 file_writer = file_writers_[layer].get(); |
| 883 } | 793 } |
| 884 if (file_writer) { | 794 if (file_writer) { |
| 885 bool ok = file_writer->WriteFrame(encoded_image); | 795 bool ok = file_writer->WriteFrame(encoded_image); |
| 886 RTC_DCHECK(ok); | 796 RTC_DCHECK(ok); |
| 887 } | 797 } |
| 888 } | 798 } |
| 889 | 799 |
| 890 return result; | 800 return result; |
| 891 } | 801 } |
| 892 | 802 |
| 893 void VideoSendStreamImpl::ConfigureProtection() { | 803 void VideoSendStream::ConfigureProtection() { |
| 894 RTC_DCHECK_RUN_ON(worker_queue_); | |
| 895 // Enable NACK, FEC or both. | 804 // Enable NACK, FEC or both. |
| 896 const bool enable_protection_nack = config_->rtp.nack.rtp_history_ms > 0; | 805 const bool enable_protection_nack = config_.rtp.nack.rtp_history_ms > 0; |
| 897 bool enable_protection_fec = config_->rtp.fec.ulpfec_payload_type != -1; | 806 bool enable_protection_fec = config_.rtp.fec.ulpfec_payload_type != -1; |
| 898 // Payload types without picture ID cannot determine that a stream is complete | 807 // Payload types without picture ID cannot determine that a stream is complete |
| 899 // without retransmitting FEC, so using FEC + NACK for H.264 (for instance) is | 808 // without retransmitting FEC, so using FEC + NACK for H.264 (for instance) is |
| 900 // a waste of bandwidth since FEC packets still have to be transmitted. Note | 809 // a waste of bandwidth since FEC packets still have to be transmitted. Note |
| 901 // that this is not the case with FLEXFEC. | 810 // that this is not the case with FLEXFEC. |
| 902 if (enable_protection_nack && | 811 if (enable_protection_nack && |
| 903 !PayloadTypeSupportsSkippingFecPackets( | 812 !PayloadTypeSupportsSkippingFecPackets( |
| 904 config_->encoder_settings.payload_name)) { | 813 config_.encoder_settings.payload_name)) { |
| 905 LOG(LS_WARNING) << "Transmitting payload type without picture ID using" | 814 LOG(LS_WARNING) << "Transmitting payload type without picture ID using" |
| 906 "NACK+FEC is a waste of bandwidth since FEC packets " | 815 "NACK+FEC is a waste of bandwidth since FEC packets " |
| 907 "also have to be retransmitted. Disabling FEC."; | 816 "also have to be retransmitted. Disabling FEC."; |
| 908 enable_protection_fec = false; | 817 enable_protection_fec = false; |
| 909 } | 818 } |
| 910 | 819 |
| 911 // Set to valid uint8_ts to be castable later without signed overflows. | 820 // Set to valid uint8_ts to be castable later without signed overflows. |
| 912 uint8_t payload_type_red = 0; | 821 uint8_t payload_type_red = 0; |
| 913 uint8_t payload_type_fec = 0; | 822 uint8_t payload_type_fec = 0; |
| 914 | 823 |
| 915 // TODO(changbin): Should set RTX for RED mapping in RTP sender in future. | 824 // TODO(changbin): Should set RTX for RED mapping in RTP sender in future. |
| 916 // Validate payload types. If either RED or FEC payload types are set then | 825 // Validate payload types. If either RED or FEC payload types are set then |
| 917 // both should be. If FEC is enabled then they both have to be set. | 826 // both should be. If FEC is enabled then they both have to be set. |
| 918 if (config_->rtp.fec.red_payload_type != -1) { | 827 if (config_.rtp.fec.red_payload_type != -1) { |
| 919 RTC_DCHECK_GE(config_->rtp.fec.red_payload_type, 0); | 828 RTC_DCHECK_GE(config_.rtp.fec.red_payload_type, 0); |
| 920 RTC_DCHECK_LE(config_->rtp.fec.red_payload_type, 127); | 829 RTC_DCHECK_LE(config_.rtp.fec.red_payload_type, 127); |
| 921 // TODO(holmer): We should only enable red if ulpfec is also enabled, but | 830 // TODO(holmer): We should only enable red if ulpfec is also enabled, but |
| 922 // but due to an incompatibility issue with previous versions the receiver | 831 // but due to an incompatibility issue with previous versions the receiver |
| 923 // assumes rtx packets are containing red if it has been configured to | 832 // assumes rtx packets are containing red if it has been configured to |
| 924 // receive red. Remove this in a few versions once the incompatibility | 833 // receive red. Remove this in a few versions once the incompatibility |
| 925 // issue is resolved (M53 timeframe). | 834 // issue is resolved (M53 timeframe). |
| 926 payload_type_red = static_cast<uint8_t>(config_->rtp.fec.red_payload_type); | 835 payload_type_red = static_cast<uint8_t>(config_.rtp.fec.red_payload_type); |
| 927 } | 836 } |
| 928 if (config_->rtp.fec.ulpfec_payload_type != -1) { | 837 if (config_.rtp.fec.ulpfec_payload_type != -1) { |
| 929 RTC_DCHECK_GE(config_->rtp.fec.ulpfec_payload_type, 0); | 838 RTC_DCHECK_GE(config_.rtp.fec.ulpfec_payload_type, 0); |
| 930 RTC_DCHECK_LE(config_->rtp.fec.ulpfec_payload_type, 127); | 839 RTC_DCHECK_LE(config_.rtp.fec.ulpfec_payload_type, 127); |
| 931 payload_type_fec = | 840 payload_type_fec = |
| 932 static_cast<uint8_t>(config_->rtp.fec.ulpfec_payload_type); | 841 static_cast<uint8_t>(config_.rtp.fec.ulpfec_payload_type); |
| 933 } | 842 } |
| 934 | 843 |
| 935 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) { | 844 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) { |
| 936 // Set NACK. | 845 // Set NACK. |
| 937 rtp_rtcp->SetStorePacketsStatus( | 846 rtp_rtcp->SetStorePacketsStatus( |
| 938 enable_protection_nack || congestion_controller_->pacer(), | 847 enable_protection_nack || congestion_controller_->pacer(), |
| 939 kMinSendSidePacketHistorySize); | 848 kMinSendSidePacketHistorySize); |
| 940 // Set FEC. | 849 // Set FEC. |
| 941 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) { | 850 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) { |
| 942 rtp_rtcp->SetGenericFECStatus(enable_protection_fec, payload_type_red, | 851 rtp_rtcp->SetGenericFECStatus(enable_protection_fec, payload_type_red, |
| 943 payload_type_fec); | 852 payload_type_fec); |
| 944 } | 853 } |
| 945 } | 854 } |
| 946 | 855 |
| 947 protection_bitrate_calculator_.SetProtectionMethod(enable_protection_fec, | 856 protection_bitrate_calculator_.SetProtectionMethod(enable_protection_fec, |
| 948 enable_protection_nack); | 857 enable_protection_nack); |
| 949 } | 858 } |
| 950 | 859 |
| 951 void VideoSendStreamImpl::ConfigureSsrcs() { | 860 void VideoSendStream::ConfigureSsrcs() { |
| 952 RTC_DCHECK_RUN_ON(worker_queue_); | |
| 953 // Configure regular SSRCs. | 861 // Configure regular SSRCs. |
| 954 for (size_t i = 0; i < config_->rtp.ssrcs.size(); ++i) { | 862 for (size_t i = 0; i < config_.rtp.ssrcs.size(); ++i) { |
| 955 uint32_t ssrc = config_->rtp.ssrcs[i]; | 863 uint32_t ssrc = config_.rtp.ssrcs[i]; |
| 956 RtpRtcp* const rtp_rtcp = rtp_rtcp_modules_[i]; | 864 RtpRtcp* const rtp_rtcp = rtp_rtcp_modules_[i]; |
| 957 rtp_rtcp->SetSSRC(ssrc); | 865 rtp_rtcp->SetSSRC(ssrc); |
| 958 | 866 |
| 959 // Restore RTP state if previous existed. | 867 // Restore RTP state if previous existed. |
| 960 VideoSendStream::RtpStateMap::iterator it = suspended_ssrcs_.find(ssrc); | 868 RtpStateMap::iterator it = suspended_ssrcs_.find(ssrc); |
| 961 if (it != suspended_ssrcs_.end()) | 869 if (it != suspended_ssrcs_.end()) |
| 962 rtp_rtcp->SetRtpState(it->second); | 870 rtp_rtcp->SetRtpState(it->second); |
| 963 } | 871 } |
| 964 | 872 |
| 965 // Set up RTX if available. | 873 // Set up RTX if available. |
| 966 if (config_->rtp.rtx.ssrcs.empty()) | 874 if (config_.rtp.rtx.ssrcs.empty()) |
| 967 return; | 875 return; |
| 968 | 876 |
| 969 // Configure RTX SSRCs. | 877 // Configure RTX SSRCs. |
| 970 RTC_DCHECK_EQ(config_->rtp.rtx.ssrcs.size(), config_->rtp.ssrcs.size()); | 878 RTC_DCHECK_EQ(config_.rtp.rtx.ssrcs.size(), config_.rtp.ssrcs.size()); |
| 971 for (size_t i = 0; i < config_->rtp.rtx.ssrcs.size(); ++i) { | 879 for (size_t i = 0; i < config_.rtp.rtx.ssrcs.size(); ++i) { |
| 972 uint32_t ssrc = config_->rtp.rtx.ssrcs[i]; | 880 uint32_t ssrc = config_.rtp.rtx.ssrcs[i]; |
| 973 RtpRtcp* const rtp_rtcp = rtp_rtcp_modules_[i]; | 881 RtpRtcp* const rtp_rtcp = rtp_rtcp_modules_[i]; |
| 974 rtp_rtcp->SetRtxSsrc(ssrc); | 882 rtp_rtcp->SetRtxSsrc(ssrc); |
| 975 VideoSendStream::RtpStateMap::iterator it = suspended_ssrcs_.find(ssrc); | 883 RtpStateMap::iterator it = suspended_ssrcs_.find(ssrc); |
| 976 if (it != suspended_ssrcs_.end()) | 884 if (it != suspended_ssrcs_.end()) |
| 977 rtp_rtcp->SetRtxState(it->second); | 885 rtp_rtcp->SetRtxState(it->second); |
| 978 } | 886 } |
| 979 | 887 |
| 980 // Configure RTX payload types. | 888 // Configure RTX payload types. |
| 981 RTC_DCHECK_GE(config_->rtp.rtx.payload_type, 0); | 889 RTC_DCHECK_GE(config_.rtp.rtx.payload_type, 0); |
| 982 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) { | 890 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) { |
| 983 rtp_rtcp->SetRtxSendPayloadType(config_->rtp.rtx.payload_type, | 891 rtp_rtcp->SetRtxSendPayloadType(config_.rtp.rtx.payload_type, |
| 984 config_->encoder_settings.payload_type); | 892 config_.encoder_settings.payload_type); |
| 985 rtp_rtcp->SetRtxSendStatus(kRtxRetransmitted | kRtxRedundantPayloads); | 893 rtp_rtcp->SetRtxSendStatus(kRtxRetransmitted | kRtxRedundantPayloads); |
| 986 } | 894 } |
| 987 if (config_->rtp.fec.red_payload_type != -1 && | 895 if (config_.rtp.fec.red_payload_type != -1 && |
| 988 config_->rtp.fec.red_rtx_payload_type != -1) { | 896 config_.rtp.fec.red_rtx_payload_type != -1) { |
| 989 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) { | 897 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) { |
| 990 rtp_rtcp->SetRtxSendPayloadType(config_->rtp.fec.red_rtx_payload_type, | 898 rtp_rtcp->SetRtxSendPayloadType(config_.rtp.fec.red_rtx_payload_type, |
| 991 config_->rtp.fec.red_payload_type); | 899 config_.rtp.fec.red_payload_type); |
| 992 } | 900 } |
| 993 } | 901 } |
| 994 } | 902 } |
| 995 | 903 |
| 996 std::map<uint32_t, RtpState> VideoSendStreamImpl::GetRtpStates() const { | 904 std::map<uint32_t, RtpState> VideoSendStream::GetRtpStates() const { |
| 997 RTC_DCHECK_RUN_ON(worker_queue_); | |
| 998 std::map<uint32_t, RtpState> rtp_states; | 905 std::map<uint32_t, RtpState> rtp_states; |
| 999 for (size_t i = 0; i < config_->rtp.ssrcs.size(); ++i) { | 906 for (size_t i = 0; i < config_.rtp.ssrcs.size(); ++i) { |
| 1000 uint32_t ssrc = config_->rtp.ssrcs[i]; | 907 uint32_t ssrc = config_.rtp.ssrcs[i]; |
| 1001 RTC_DCHECK_EQ(ssrc, rtp_rtcp_modules_[i]->SSRC()); | 908 RTC_DCHECK_EQ(ssrc, rtp_rtcp_modules_[i]->SSRC()); |
| 1002 rtp_states[ssrc] = rtp_rtcp_modules_[i]->GetRtpState(); | 909 rtp_states[ssrc] = rtp_rtcp_modules_[i]->GetRtpState(); |
| 1003 } | 910 } |
| 1004 | 911 |
| 1005 for (size_t i = 0; i < config_->rtp.rtx.ssrcs.size(); ++i) { | 912 for (size_t i = 0; i < config_.rtp.rtx.ssrcs.size(); ++i) { |
| 1006 uint32_t ssrc = config_->rtp.rtx.ssrcs[i]; | 913 uint32_t ssrc = config_.rtp.rtx.ssrcs[i]; |
| 1007 rtp_states[ssrc] = rtp_rtcp_modules_[i]->GetRtxState(); | 914 rtp_states[ssrc] = rtp_rtcp_modules_[i]->GetRtxState(); |
| 1008 } | 915 } |
| 1009 | 916 |
| 1010 return rtp_states; | 917 return rtp_states; |
| 1011 } | 918 } |
| 1012 | 919 |
| 1013 void VideoSendStreamImpl::SignalNetworkState(NetworkState state) { | 920 void VideoSendStream::SignalNetworkState(NetworkState state) { |
| 1014 RTC_DCHECK_RUN_ON(worker_queue_); | |
| 1015 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) { | 921 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) { |
| 1016 rtp_rtcp->SetRTCPStatus(state == kNetworkUp ? config_->rtp.rtcp_mode | 922 rtp_rtcp->SetRTCPStatus(state == kNetworkUp ? config_.rtp.rtcp_mode |
| 1017 : RtcpMode::kOff); | 923 : RtcpMode::kOff); |
| 1018 } | 924 } |
| 1019 } | 925 } |
| 1020 | 926 |
| 1021 uint32_t VideoSendStreamImpl::OnBitrateUpdated(uint32_t bitrate_bps, | 927 uint32_t VideoSendStream::OnBitrateUpdated(uint32_t bitrate_bps, |
| 1022 uint8_t fraction_loss, | 928 uint8_t fraction_loss, |
| 1023 int64_t rtt) { | 929 int64_t rtt) { |
| 1024 RTC_DCHECK_RUN_ON(worker_queue_); | |
| 1025 RTC_DCHECK(payload_router_.active()) | |
| 1026 << "VideoSendStream::Start has not been called."; | |
| 1027 // Get the encoder target rate. It is the estimated network rate - | 930 // Get the encoder target rate. It is the estimated network rate - |
| 1028 // protection overhead. | 931 // protection overhead. |
| 1029 encoder_target_rate_bps_ = protection_bitrate_calculator_.SetTargetRates( | 932 uint32_t encoder_target_rate_bps = |
| 1030 bitrate_bps, stats_proxy_->GetSendFrameRate(), fraction_loss, rtt); | 933 protection_bitrate_calculator_.SetTargetRates( |
| 1031 uint32_t protection_bitrate = bitrate_bps - encoder_target_rate_bps_; | 934 bitrate_bps, stats_proxy_.GetSendFrameRate(), fraction_loss, rtt); |
| 1032 | 935 |
| 1033 encoder_target_rate_bps_ = | 936 uint32_t protection_bitrate = bitrate_bps - encoder_target_rate_bps; |
| 1034 std::min(encoder_max_bitrate_bps_, encoder_target_rate_bps_); | 937 { |
| 1035 vie_encoder_->OnBitrateUpdated(encoder_target_rate_bps_, fraction_loss, rtt); | 938 // Limit the target bitrate to the configured max bitrate. |
| 1036 stats_proxy_->OnSetEncoderTargetRate(encoder_target_rate_bps_); | 939 rtc::CritScope lock(&encoder_settings_crit_); |
| 940 encoder_target_rate_bps = |
| 941 std::min(encoder_max_bitrate_bps_, encoder_target_rate_bps); |
| 942 if ((encoder_target_rate_bps_ == 0 && encoder_target_rate_bps > 0) || |
| 943 (encoder_target_rate_bps_ > 0 && encoder_target_rate_bps == 0)) { |
| 944 LOG(LS_INFO) |
| 945 << "OnBitrateUpdated: Encoder state changed, target bitrate " |
| 946 << encoder_target_rate_bps << " bps."; |
| 947 } |
| 948 encoder_target_rate_bps_ = encoder_target_rate_bps; |
| 949 } |
| 950 vie_encoder_.OnBitrateUpdated(encoder_target_rate_bps, fraction_loss, rtt); |
| 951 stats_proxy_.OnSetEncoderTargetRate(encoder_target_rate_bps); |
| 952 |
| 1037 return protection_bitrate; | 953 return protection_bitrate; |
| 1038 } | 954 } |
| 1039 | 955 |
| 1040 int VideoSendStreamImpl::ProtectionRequest( | 956 int VideoSendStream::ProtectionRequest(const FecProtectionParams* delta_params, |
| 1041 const FecProtectionParams* delta_params, | 957 const FecProtectionParams* key_params, |
| 1042 const FecProtectionParams* key_params, | 958 uint32_t* sent_video_rate_bps, |
| 1043 uint32_t* sent_video_rate_bps, | 959 uint32_t* sent_nack_rate_bps, |
| 1044 uint32_t* sent_nack_rate_bps, | 960 uint32_t* sent_fec_rate_bps) { |
| 1045 uint32_t* sent_fec_rate_bps) { | |
| 1046 RTC_DCHECK_RUN_ON(worker_queue_); | |
| 1047 *sent_video_rate_bps = 0; | 961 *sent_video_rate_bps = 0; |
| 1048 *sent_nack_rate_bps = 0; | 962 *sent_nack_rate_bps = 0; |
| 1049 *sent_fec_rate_bps = 0; | 963 *sent_fec_rate_bps = 0; |
| 1050 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) { | 964 for (RtpRtcp* rtp_rtcp : rtp_rtcp_modules_) { |
| 1051 uint32_t not_used = 0; | 965 uint32_t not_used = 0; |
| 1052 uint32_t module_video_rate = 0; | 966 uint32_t module_video_rate = 0; |
| 1053 uint32_t module_fec_rate = 0; | 967 uint32_t module_fec_rate = 0; |
| 1054 uint32_t module_nack_rate = 0; | 968 uint32_t module_nack_rate = 0; |
| 1055 rtp_rtcp->SetFecParameters(delta_params, key_params); | 969 rtp_rtcp->SetFecParameters(delta_params, key_params); |
| 1056 rtp_rtcp->BitrateSent(¬_used, &module_video_rate, &module_fec_rate, | 970 rtp_rtcp->BitrateSent(¬_used, &module_video_rate, &module_fec_rate, |
| 1057 &module_nack_rate); | 971 &module_nack_rate); |
| 1058 *sent_video_rate_bps += module_video_rate; | 972 *sent_video_rate_bps += module_video_rate; |
| 1059 *sent_nack_rate_bps += module_nack_rate; | 973 *sent_nack_rate_bps += module_nack_rate; |
| 1060 *sent_fec_rate_bps += module_fec_rate; | 974 *sent_fec_rate_bps += module_fec_rate; |
| 1061 } | 975 } |
| 1062 return 0; | 976 return 0; |
| 1063 } | 977 } |
| 1064 | 978 |
| 1065 } // namespace internal | 979 } // namespace internal |
| 1066 } // namespace webrtc | 980 } // namespace webrtc |
| OLD | NEW |