| 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 | */ |
| 10 | #include "video/video_send_stream.h" |
| 11 |
| 12 | #include <utility> |
| 13 |
| 14 | #include "api/array_view.h" |
| 15 | #include "api/task_queue/task_queue_base.h" |
| 16 | #include "api/video/video_stream_encoder_settings.h" |
| 17 | #include "modules/rtp_rtcp/include/rtp_header_extension_map.h" |
| 18 | #include "modules/rtp_rtcp/source/rtp_header_extension_size.h" |
| 19 | #include "modules/rtp_rtcp/source/rtp_sender.h" |
| 20 | #include "rtc_base/checks.h" |
| 21 | #include "rtc_base/logging.h" |
| 22 | #include "rtc_base/strings/string_builder.h" |
| 23 | #include "system_wrappers/include/clock.h" |
| 24 | #include "video/adaptation/overuse_frame_detector.h" |
| 25 | #include "video/frame_cadence_adapter.h" |
| 26 | #include "video/video_stream_encoder.h" |
| 27 |
| 28 | namespace webrtc { |
| 29 |
| 30 | namespace { |
| 31 |
| 32 | size_t CalculateMaxHeaderSize(const RtpConfig& config) { |
| 33 | size_t header_size = kRtpHeaderSize; |
| 34 | size_t extensions_size = 0; |
| 35 | size_t fec_extensions_size = 0; |
| 36 | if (!config.extensions.empty()) { |
| 37 | RtpHeaderExtensionMap extensions_map(config.extensions); |
| 38 | extensions_size = RtpHeaderExtensionSize(RTPSender::VideoExtensionSizes(), |
| 39 | extensions_map); |
| 40 | fec_extensions_size = |
| 41 | RtpHeaderExtensionSize(RTPSender::FecExtensionSizes(), extensions_map); |
| 42 | } |
| 43 | header_size += extensions_size; |
| 44 | if (config.flexfec.payload_type >= 0) { |
| 45 | // All FEC extensions again plus maximum FlexFec overhead. |
| 46 | header_size += fec_extensions_size + 32; |
| 47 | } else { |
| 48 | if (config.ulpfec.ulpfec_payload_type >= 0) { |
| 49 | // Header with all the FEC extensions will be repeated plus maximum |
| 50 | // UlpFec overhead. |
| 51 | header_size += fec_extensions_size + 18; |
| 52 | } |
| 53 | if (config.ulpfec.red_payload_type >= 0) { |
| 54 | header_size += 1; // RED header. |
| 55 | } |
| 56 | } |
| 57 | // Additional room for Rtx. |
| 58 | if (config.rtx.payload_type >= 0) |
| 59 | header_size += kRtxHeaderSize; |
| 60 | return header_size; |
| 61 | } |
| 62 |
| 63 | VideoStreamEncoder::BitrateAllocationCallbackType |
| 64 | GetBitrateAllocationCallbackType(const VideoSendStream::Config& config, |
| 65 | const FieldTrialsView& field_trials) { |
| 66 | if (webrtc::RtpExtension::FindHeaderExtensionByUri( |
| 67 | config.rtp.extensions, |
| 68 | webrtc::RtpExtension::kVideoLayersAllocationUri, |
| 69 | config.crypto_options.srtp.enable_encrypted_rtp_header_extensions |
| 70 | ? RtpExtension::Filter::kPreferEncryptedExtension |
| 71 | : RtpExtension::Filter::kDiscardEncryptedExtension)) { |
| 72 | return VideoStreamEncoder::BitrateAllocationCallbackType:: |
| 73 | kVideoLayersAllocation; |
| 74 | } |
| 75 | if (field_trials.IsEnabled("WebRTC-Target-Bitrate-Rtcp")) { |
| 76 | return VideoStreamEncoder::BitrateAllocationCallbackType:: |
| 77 | kVideoBitrateAllocation; |
| 78 | } |
| 79 | return VideoStreamEncoder::BitrateAllocationCallbackType:: |
| 80 | kVideoBitrateAllocationWhenScreenSharing; |
| 81 | } |
| 82 |
| 83 | RtpSenderFrameEncryptionConfig CreateFrameEncryptionConfig( |
| 84 | const VideoSendStream::Config* config) { |
| 85 | RtpSenderFrameEncryptionConfig frame_encryption_config; |
| 86 | frame_encryption_config.frame_encryptor = config->frame_encryptor.get(); |
| 87 | frame_encryption_config.crypto_options = config->crypto_options; |
| 88 | return frame_encryption_config; |
| 89 | } |
| 90 |
| 91 | RtpSenderObservers CreateObservers(RtcpRttStats* call_stats, |
| 92 | EncoderRtcpFeedback* encoder_feedback, |
| 93 | SendStatisticsProxy* stats_proxy, |
| 94 | SendDelayStats* send_delay_stats) { |
| 95 | RtpSenderObservers observers; |
| 96 | observers.rtcp_rtt_stats = call_stats; |
| 97 | observers.intra_frame_callback = encoder_feedback; |
| 98 | observers.rtcp_loss_notification_observer = encoder_feedback; |
| 99 | observers.report_block_data_observer = stats_proxy; |
| 100 | observers.rtp_stats = stats_proxy; |
| 101 | observers.bitrate_observer = stats_proxy; |
| 102 | observers.frame_count_observer = stats_proxy; |
| 103 | observers.rtcp_type_observer = stats_proxy; |
| 104 | observers.send_delay_observer = stats_proxy; |
| 105 | observers.send_packet_observer = send_delay_stats; |
| 106 | return observers; |
| 107 | } |
| 108 |
| 109 | std::unique_ptr<VideoStreamEncoder> CreateVideoStreamEncoder( |
| 110 | Clock* clock, |
| 111 | int num_cpu_cores, |
| 112 | TaskQueueFactory* task_queue_factory, |
| 113 | SendStatisticsProxy* stats_proxy, |
| 114 | const VideoStreamEncoderSettings& encoder_settings, |
| 115 | VideoStreamEncoder::BitrateAllocationCallbackType |
| 116 | bitrate_allocation_callback_type, |
| 117 | const FieldTrialsView& field_trials, |
| 118 | webrtc::VideoEncoderFactory::EncoderSelectorInterface* encoder_selector) { |
| 119 | std::unique_ptr<TaskQueueBase, TaskQueueDeleter> encoder_queue = |
| 120 | task_queue_factory->CreateTaskQueue("EncoderQueue", |
| 121 | TaskQueueFactory::Priority::NORMAL); |
| 122 | TaskQueueBase* encoder_queue_ptr = encoder_queue.get(); |
| 123 | return std::make_unique<VideoStreamEncoder>( |
| 124 | clock, num_cpu_cores, stats_proxy, encoder_settings, |
| 125 | std::make_unique<OveruseFrameDetector>(stats_proxy, field_trials), |
| 126 | FrameCadenceAdapterInterface::Create(clock, encoder_queue_ptr, |
| 127 | field_trials), |
| 128 | std::move(encoder_queue), bitrate_allocation_callback_type, field_trials, |
| 129 | encoder_selector); |
| 130 | } |
| 131 |
| 132 | } // namespace |
| 133 |
| 134 | namespace internal { |
| 135 |
| 136 | VideoSendStream::VideoSendStream( |
| 137 | Clock* clock, |
| 138 | int num_cpu_cores, |
| 139 | TaskQueueFactory* task_queue_factory, |
| 140 | TaskQueueBase* network_queue, |
| 141 | RtcpRttStats* call_stats, |
| 142 | RtpTransportControllerSendInterface* transport, |
| 143 | BitrateAllocatorInterface* bitrate_allocator, |
| 144 | SendDelayStats* send_delay_stats, |
| 145 | RtcEventLog* event_log, |
| 146 | VideoSendStream::Config config, |
| 147 | VideoEncoderConfig encoder_config, |
| 148 | const std::map<uint32_t, RtpState>& suspended_ssrcs, |
| 149 | const std::map<uint32_t, RtpPayloadState>& suspended_payload_states, |
| 150 | std::unique_ptr<FecController> fec_controller, |
| 151 | const FieldTrialsView& field_trials) |
| 152 | : rtp_transport_queue_(transport->GetWorkerQueue()), |
| 153 | transport_(transport), |
| 154 | stats_proxy_(clock, config, encoder_config.content_type, field_trials), |
| 155 | config_(std::move(config)), |
| 156 | content_type_(encoder_config.content_type), |
| 157 | video_stream_encoder_(CreateVideoStreamEncoder( |
| 158 | clock, |
| 159 | num_cpu_cores, |
| 160 | task_queue_factory, |
| 161 | &stats_proxy_, |
| 162 | config_.encoder_settings, |
| 163 | GetBitrateAllocationCallbackType(config_, field_trials), |
| 164 | field_trials, |
| 165 | config_.encoder_selector)), |
| 166 | encoder_feedback_( |
| 167 | clock, |
| 168 | config_.rtp.ssrcs, |
| 169 | video_stream_encoder_.get(), |
| 170 | [this](uint32_t ssrc, const std::vector<uint16_t>& seq_nums) { |
| 171 | return rtp_video_sender_->GetSentRtpPacketInfos(ssrc, seq_nums); |
| 172 | }), |
| 173 | rtp_video_sender_( |
| 174 | transport->CreateRtpVideoSender(suspended_ssrcs, |
| 175 | suspended_payload_states, |
| 176 | config_.rtp, |
| 177 | config_.rtcp_report_interval_ms, |
| 178 | config_.send_transport, |
| 179 | CreateObservers(call_stats, |
| 180 | &encoder_feedback_, |
| 181 | &stats_proxy_, |
| 182 | send_delay_stats), |
| 183 | event_log, |
| 184 | std::move(fec_controller), |
| 185 | CreateFrameEncryptionConfig(&config_), |
| 186 | config_.frame_transformer)), |
| 187 | send_stream_(clock, |
| 188 | &stats_proxy_, |
| 189 | transport, |
| 190 | bitrate_allocator, |
| 191 | video_stream_encoder_.get(), |
| 192 | &config_, |
| 193 | encoder_config.max_bitrate_bps, |
| 194 | encoder_config.bitrate_priority, |
| 195 | encoder_config.content_type, |
| 196 | rtp_video_sender_, |
| 197 | field_trials) { |
| 198 | RTC_DCHECK(config_.encoder_settings.encoder_factory); |
| 199 | RTC_DCHECK(config_.encoder_settings.bitrate_allocator_factory); |
| 200 |
| 201 | video_stream_encoder_->SetFecControllerOverride(rtp_video_sender_); |
| 202 |
| 203 | ReconfigureVideoEncoder(std::move(encoder_config)); |
| 204 | } |
| 205 |
| 206 | VideoSendStream::~VideoSendStream() { |
| 207 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 208 | RTC_DCHECK(!running_); |
| 209 | transport_->DestroyRtpVideoSender(rtp_video_sender_); |
| 210 | } |
| 211 |
| 212 | void VideoSendStream::UpdateActiveSimulcastLayers( |
| 213 | const std::vector<bool> active_layers) { |
| 214 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 215 |
| 216 | // Keep our `running_` flag expected state in sync with active layers since |
| 217 | // the `send_stream_` will be implicitly stopped/started depending on the |
| 218 | // state of the layers. |
| 219 | bool running = false; |
| 220 |
| 221 | rtc::StringBuilder active_layers_string; |
| 222 | active_layers_string << "{"; |
| 223 | for (size_t i = 0; i < active_layers.size(); ++i) { |
| 224 | if (active_layers[i]) { |
| 225 | running = true; |
| 226 | active_layers_string << "1"; |
| 227 | } else { |
| 228 | active_layers_string << "0"; |
| 229 | } |
| 230 | if (i < active_layers.size() - 1) { |
| 231 | active_layers_string << ", "; |
| 232 | } |
| 233 | } |
| 234 | active_layers_string << "}"; |
| 235 | RTC_LOG(LS_INFO) << "UpdateActiveSimulcastLayers: " |
| 236 | << active_layers_string.str(); |
| 237 |
| 238 | rtp_transport_queue_->RunOrPost( |
| 239 | SafeTask(transport_queue_safety_, [this, active_layers] { |
| 240 | send_stream_.UpdateActiveSimulcastLayers(active_layers); |
| 241 | })); |
| 242 |
| 243 | running_ = running; |
| 244 | } |
| 245 |
| 246 | void VideoSendStream::Start() { |
| 247 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 248 | RTC_DLOG(LS_INFO) << "VideoSendStream::Start"; |
| 249 | if (running_) |
| 250 | return; |
| 251 |
| 252 | running_ = true; |
| 253 |
| 254 | // It is expected that after VideoSendStream::Start has been called, incoming |
| 255 | // frames are not dropped in VideoStreamEncoder. To ensure this, Start has to |
| 256 | // be synchronized. |
| 257 | // TODO(tommi): ^^^ Validate if this still holds. |
| 258 | rtp_transport_queue_->RunSynchronous([this] { |
| 259 | transport_queue_safety_->SetAlive(); |
| 260 | send_stream_.Start(); |
| 261 | }); |
| 262 | } |
| 263 |
| 264 | void VideoSendStream::Stop() { |
| 265 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 266 | if (!running_) |
| 267 | return; |
| 268 | RTC_DLOG(LS_INFO) << "VideoSendStream::Stop"; |
| 269 | running_ = false; |
| 270 | rtp_transport_queue_->RunOrPost(SafeTask(transport_queue_safety_, [this] { |
| 271 | // As the stream can get re-used and implicitly restarted via changing |
| 272 | // the state of the active layers, we do not mark the |
| 273 | // `transport_queue_safety_` flag with `SetNotAlive()` here. That's only |
| 274 | // done when we stop permanently via `StopPermanentlyAndGetRtpStates()`. |
| 275 | send_stream_.Stop(); |
| 276 | })); |
| 277 | } |
| 278 |
| 279 | bool VideoSendStream::started() { |
| 280 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 281 | return running_; |
| 282 | } |
| 283 |
| 284 | void VideoSendStream::AddAdaptationResource( |
| 285 | rtc::scoped_refptr<Resource> resource) { |
| 286 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 287 | video_stream_encoder_->AddAdaptationResource(resource); |
| 288 | } |
| 289 |
| 290 | std::vector<rtc::scoped_refptr<Resource>> |
| 291 | VideoSendStream::GetAdaptationResources() { |
| 292 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 293 | return video_stream_encoder_->GetAdaptationResources(); |
| 294 | } |
| 295 |
| 296 | void VideoSendStream::SetSource( |
| 297 | rtc::VideoSourceInterface<webrtc::VideoFrame>* source, |
| 298 | const DegradationPreference& degradation_preference) { |
| 299 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 300 | video_stream_encoder_->SetSource(source, degradation_preference); |
| 301 | } |
| 302 |
| 303 | void VideoSendStream::ReconfigureVideoEncoder(VideoEncoderConfig config) { |
| 304 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 305 | RTC_DCHECK_EQ(content_type_, config.content_type); |
| 306 | video_stream_encoder_->ConfigureEncoder( |
| 307 | std::move(config), |
| 308 | config_.rtp.max_packet_size - CalculateMaxHeaderSize(config_.rtp)); |
| 309 | } |
| 310 |
| 311 | VideoSendStream::Stats VideoSendStream::GetStats() { |
| 312 | // TODO(perkj, solenberg): Some test cases in EndToEndTest call GetStats from |
| 313 | // a network thread. See comment in Call::GetStats(). |
| 314 | // RTC_DCHECK_RUN_ON(&thread_checker_); |
| 315 | return stats_proxy_.GetStats(); |
| 316 | } |
| 317 |
| 318 | absl::optional<float> VideoSendStream::GetPacingFactorOverride() const { |
| 319 | return send_stream_.configured_pacing_factor(); |
| 320 | } |
| 321 |
| 322 | void VideoSendStream::StopPermanentlyAndGetRtpStates( |
| 323 | VideoSendStream::RtpStateMap* rtp_state_map, |
| 324 | VideoSendStream::RtpPayloadStateMap* payload_state_map) { |
| 325 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 326 | video_stream_encoder_->Stop(); |
| 327 |
| 328 | running_ = false; |
| 329 | // Always run these cleanup steps regardless of whether running_ was set |
| 330 | // or not. This will unregister callbacks before destruction. |
| 331 | // See `VideoSendStreamImpl::StopVideoSendStream` for more. |
| 332 | rtp_transport_queue_->RunSynchronous( |
| 333 | [this, rtp_state_map, payload_state_map]() { |
| 334 | transport_queue_safety_->SetNotAlive(); |
| 335 | send_stream_.Stop(); |
| 336 | *rtp_state_map = send_stream_.GetRtpStates(); |
| 337 | *payload_state_map = send_stream_.GetRtpPayloadStates(); |
| 338 | }); |
| 339 | } |
| 340 |
| 341 | void VideoSendStream::DeliverRtcp(const uint8_t* packet, size_t length) { |
| 342 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 343 | send_stream_.DeliverRtcp(packet, length); |
| 344 | } |
| 345 |
| 346 | void VideoSendStream::GenerateKeyFrame() { |
| 347 | if (video_stream_encoder_) { |
| 348 | video_stream_encoder_->SendKeyFrame(); |
| 349 | } |
| 350 | } |
| 351 |
| 352 | } // namespace internal |
| 353 | } // namespace webrtc |
| 354 |