Tommi | d3807da | 2020-05-22 17:36:36 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 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 | */ |
| 10 | |
| 11 | #ifndef VIDEO_RTP_VIDEO_STREAM_RECEIVER2_H_ |
| 12 | #define VIDEO_RTP_VIDEO_STREAM_RECEIVER2_H_ |
| 13 | |
| 14 | #include <atomic> |
| 15 | #include <list> |
| 16 | #include <map> |
| 17 | #include <memory> |
| 18 | #include <string> |
| 19 | #include <vector> |
| 20 | |
| 21 | #include "absl/types/optional.h" |
| 22 | #include "api/array_view.h" |
| 23 | #include "api/crypto/frame_decryptor_interface.h" |
| 24 | #include "api/video/color_space.h" |
| 25 | #include "api/video_codecs/video_codec.h" |
| 26 | #include "call/rtp_packet_sink_interface.h" |
| 27 | #include "call/syncable.h" |
| 28 | #include "call/video_receive_stream.h" |
| 29 | #include "modules/rtp_rtcp/include/receive_statistics.h" |
| 30 | #include "modules/rtp_rtcp/include/remote_ntp_time_estimator.h" |
| 31 | #include "modules/rtp_rtcp/include/rtp_header_extension_map.h" |
| 32 | #include "modules/rtp_rtcp/include/rtp_rtcp.h" |
| 33 | #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h" |
| 34 | #include "modules/rtp_rtcp/source/absolute_capture_time_receiver.h" |
| 35 | #include "modules/rtp_rtcp/source/rtp_dependency_descriptor_extension.h" |
| 36 | #include "modules/rtp_rtcp/source/rtp_packet_received.h" |
| 37 | #include "modules/rtp_rtcp/source/rtp_video_header.h" |
| 38 | #include "modules/rtp_rtcp/source/video_rtp_depacketizer.h" |
| 39 | #include "modules/video_coding/h264_sps_pps_tracker.h" |
| 40 | #include "modules/video_coding/loss_notification_controller.h" |
| 41 | #include "modules/video_coding/packet_buffer.h" |
| 42 | #include "modules/video_coding/rtp_frame_reference_finder.h" |
| 43 | #include "modules/video_coding/unique_timestamp_counter.h" |
| 44 | #include "rtc_base/constructor_magic.h" |
| 45 | #include "rtc_base/critical_section.h" |
| 46 | #include "rtc_base/experiments/field_trial_parser.h" |
| 47 | #include "rtc_base/numerics/sequence_number_util.h" |
| 48 | #include "rtc_base/synchronization/sequence_checker.h" |
| 49 | #include "rtc_base/thread_annotations.h" |
| 50 | #include "rtc_base/thread_checker.h" |
| 51 | #include "video/buffered_frame_decryptor.h" |
| 52 | #include "video/rtp_video_stream_receiver_frame_transformer_delegate.h" |
| 53 | |
| 54 | namespace webrtc { |
| 55 | |
| 56 | class NackModule2; |
| 57 | class PacketRouter; |
| 58 | class ProcessThread; |
| 59 | class ReceiveStatistics; |
| 60 | class RtcpRttStats; |
| 61 | class RtpPacketReceived; |
| 62 | class Transport; |
| 63 | class UlpfecReceiver; |
| 64 | |
| 65 | class RtpVideoStreamReceiver2 : public LossNotificationSender, |
| 66 | public RecoveredPacketReceiver, |
| 67 | public RtpPacketSinkInterface, |
| 68 | public KeyFrameRequestSender, |
| 69 | public video_coding::OnCompleteFrameCallback, |
| 70 | public OnDecryptedFrameCallback, |
| 71 | public OnDecryptionStatusChangeCallback, |
| 72 | public RtpVideoFrameReceiver { |
| 73 | public: |
| 74 | RtpVideoStreamReceiver2( |
| 75 | Clock* clock, |
| 76 | Transport* transport, |
| 77 | RtcpRttStats* rtt_stats, |
| 78 | // The packet router is optional; if provided, the RtpRtcp module for this |
| 79 | // stream is registered as a candidate for sending REMB and transport |
| 80 | // feedback. |
| 81 | PacketRouter* packet_router, |
| 82 | const VideoReceiveStream::Config* config, |
| 83 | ReceiveStatistics* rtp_receive_statistics, |
| 84 | RtcpPacketTypeCounterObserver* rtcp_packet_type_counter_observer, |
| 85 | RtcpCnameCallback* rtcp_cname_callback, |
| 86 | ProcessThread* process_thread, |
| 87 | NackSender* nack_sender, |
| 88 | // The KeyFrameRequestSender is optional; if not provided, key frame |
| 89 | // requests are sent via the internal RtpRtcp module. |
| 90 | KeyFrameRequestSender* keyframe_request_sender, |
| 91 | video_coding::OnCompleteFrameCallback* complete_frame_callback, |
| 92 | rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor, |
| 93 | rtc::scoped_refptr<FrameTransformerInterface> frame_transformer); |
| 94 | ~RtpVideoStreamReceiver2() override; |
| 95 | |
| 96 | void AddReceiveCodec(const VideoCodec& video_codec, |
| 97 | const std::map<std::string, std::string>& codec_params, |
| 98 | bool raw_payload); |
| 99 | |
| 100 | void StartReceive(); |
| 101 | void StopReceive(); |
| 102 | |
| 103 | // Produces the transport-related timestamps; current_delay_ms is left unset. |
| 104 | absl::optional<Syncable::Info> GetSyncInfo() const; |
| 105 | |
| 106 | bool DeliverRtcp(const uint8_t* rtcp_packet, size_t rtcp_packet_length); |
| 107 | |
| 108 | void FrameContinuous(int64_t seq_num); |
| 109 | |
| 110 | void FrameDecoded(int64_t seq_num); |
| 111 | |
| 112 | void SignalNetworkState(NetworkState state); |
| 113 | |
| 114 | // Returns number of different frames seen. |
| 115 | int GetUniqueFramesSeen() const { |
| 116 | RTC_DCHECK_RUN_ON(&worker_task_checker_); |
| 117 | return frame_counter_.GetUniqueSeen(); |
| 118 | } |
| 119 | |
| 120 | // Implements RtpPacketSinkInterface. |
| 121 | void OnRtpPacket(const RtpPacketReceived& packet) override; |
| 122 | |
| 123 | // TODO(philipel): Stop using VCMPacket in the new jitter buffer and then |
| 124 | // remove this function. Public only for tests. |
| 125 | void OnReceivedPayloadData(rtc::CopyOnWriteBuffer codec_payload, |
| 126 | const RtpPacketReceived& rtp_packet, |
| 127 | const RTPVideoHeader& video); |
| 128 | |
| 129 | // Implements RecoveredPacketReceiver. |
| 130 | void OnRecoveredPacket(const uint8_t* packet, size_t packet_length) override; |
| 131 | |
| 132 | // Send an RTCP keyframe request. |
| 133 | void RequestKeyFrame() override; |
| 134 | |
| 135 | // Implements LossNotificationSender. |
| 136 | void SendLossNotification(uint16_t last_decoded_seq_num, |
| 137 | uint16_t last_received_seq_num, |
| 138 | bool decodability_flag, |
| 139 | bool buffering_allowed) override; |
| 140 | |
| 141 | bool IsUlpfecEnabled() const; |
| 142 | bool IsRetransmissionsEnabled() const; |
| 143 | |
| 144 | // Returns true if a decryptor is attached and frames can be decrypted. |
| 145 | // Updated by OnDecryptionStatusChangeCallback. Note this refers to Frame |
| 146 | // Decryption not SRTP. |
| 147 | bool IsDecryptable() const; |
| 148 | |
| 149 | // Don't use, still experimental. |
| 150 | void RequestPacketRetransmit(const std::vector<uint16_t>& sequence_numbers); |
| 151 | |
| 152 | // Implements OnCompleteFrameCallback. |
| 153 | void OnCompleteFrame( |
| 154 | std::unique_ptr<video_coding::EncodedFrame> frame) override; |
| 155 | |
| 156 | // Implements OnDecryptedFrameCallback. |
| 157 | void OnDecryptedFrame( |
| 158 | std::unique_ptr<video_coding::RtpFrameObject> frame) override; |
| 159 | |
| 160 | // Implements OnDecryptionStatusChangeCallback. |
| 161 | void OnDecryptionStatusChange( |
| 162 | FrameDecryptorInterface::Status status) override; |
| 163 | |
| 164 | // Optionally set a frame decryptor after a stream has started. This will not |
| 165 | // reset the decoder state. |
| 166 | void SetFrameDecryptor( |
| 167 | rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor); |
| 168 | |
| 169 | // Sets a frame transformer after a stream has started, if no transformer |
| 170 | // has previously been set. Does not reset the decoder state. |
| 171 | void SetDepacketizerToDecoderFrameTransformer( |
| 172 | rtc::scoped_refptr<FrameTransformerInterface> frame_transformer); |
| 173 | |
| 174 | // Called by VideoReceiveStream when stats are updated. |
| 175 | void UpdateRtt(int64_t max_rtt_ms); |
| 176 | |
| 177 | absl::optional<int64_t> LastReceivedPacketMs() const; |
| 178 | absl::optional<int64_t> LastReceivedKeyframePacketMs() const; |
| 179 | |
| 180 | // RtpDemuxer only forwards a given RTP packet to one sink. However, some |
| 181 | // sinks, such as FlexFEC, might wish to be informed of all of the packets |
| 182 | // a given sink receives (or any set of sinks). They may do so by registering |
| 183 | // themselves as secondary sinks. |
| 184 | void AddSecondarySink(RtpPacketSinkInterface* sink); |
| 185 | void RemoveSecondarySink(const RtpPacketSinkInterface* sink); |
| 186 | |
| 187 | private: |
| 188 | // Implements RtpVideoFrameReceiver. |
| 189 | void ManageFrame( |
| 190 | std::unique_ptr<video_coding::RtpFrameObject> frame) override; |
| 191 | |
| 192 | // Used for buffering RTCP feedback messages and sending them all together. |
| 193 | // Note: |
| 194 | // 1. Key frame requests and NACKs are mutually exclusive, with the |
| 195 | // former taking precedence over the latter. |
| 196 | // 2. Loss notifications are orthogonal to either. (That is, may be sent |
| 197 | // alongside either.) |
| 198 | class RtcpFeedbackBuffer : public KeyFrameRequestSender, |
| 199 | public NackSender, |
| 200 | public LossNotificationSender { |
| 201 | public: |
| 202 | RtcpFeedbackBuffer(KeyFrameRequestSender* key_frame_request_sender, |
| 203 | NackSender* nack_sender, |
| 204 | LossNotificationSender* loss_notification_sender); |
| 205 | |
| 206 | ~RtcpFeedbackBuffer() override = default; |
| 207 | |
| 208 | // KeyFrameRequestSender implementation. |
| 209 | void RequestKeyFrame() RTC_LOCKS_EXCLUDED(cs_) override; |
| 210 | |
| 211 | // NackSender implementation. |
| 212 | void SendNack(const std::vector<uint16_t>& sequence_numbers, |
| 213 | bool buffering_allowed) RTC_LOCKS_EXCLUDED(cs_) override; |
| 214 | |
| 215 | // LossNotificationSender implementation. |
| 216 | void SendLossNotification(uint16_t last_decoded_seq_num, |
| 217 | uint16_t last_received_seq_num, |
| 218 | bool decodability_flag, |
| 219 | bool buffering_allowed) |
| 220 | RTC_LOCKS_EXCLUDED(cs_) override; |
| 221 | |
| 222 | // Send all RTCP feedback messages buffered thus far. |
| 223 | void SendBufferedRtcpFeedback() RTC_LOCKS_EXCLUDED(cs_); |
| 224 | |
| 225 | private: |
| 226 | // LNTF-related state. |
| 227 | struct LossNotificationState { |
| 228 | LossNotificationState(uint16_t last_decoded_seq_num, |
| 229 | uint16_t last_received_seq_num, |
| 230 | bool decodability_flag) |
| 231 | : last_decoded_seq_num(last_decoded_seq_num), |
| 232 | last_received_seq_num(last_received_seq_num), |
| 233 | decodability_flag(decodability_flag) {} |
| 234 | |
| 235 | uint16_t last_decoded_seq_num; |
| 236 | uint16_t last_received_seq_num; |
| 237 | bool decodability_flag; |
| 238 | }; |
| 239 | struct ConsumedRtcpFeedback { |
| 240 | bool request_key_frame = false; |
| 241 | std::vector<uint16_t> nack_sequence_numbers; |
| 242 | absl::optional<LossNotificationState> lntf_state; |
| 243 | }; |
| 244 | |
| 245 | ConsumedRtcpFeedback ConsumeRtcpFeedback() RTC_LOCKS_EXCLUDED(cs_); |
| 246 | ConsumedRtcpFeedback ConsumeRtcpFeedbackLocked() |
| 247 | RTC_EXCLUSIVE_LOCKS_REQUIRED(cs_); |
| 248 | // This method is called both with and without cs_ held. |
| 249 | void SendRtcpFeedback(ConsumedRtcpFeedback feedback); |
| 250 | |
| 251 | KeyFrameRequestSender* const key_frame_request_sender_; |
| 252 | NackSender* const nack_sender_; |
| 253 | LossNotificationSender* const loss_notification_sender_; |
| 254 | |
| 255 | // NACKs are accessible from two threads due to nack_module_ being a module. |
| 256 | rtc::CriticalSection cs_; |
| 257 | |
| 258 | // Key-frame-request-related state. |
| 259 | bool request_key_frame_ RTC_GUARDED_BY(cs_); |
| 260 | |
| 261 | // NACK-related state. |
| 262 | std::vector<uint16_t> nack_sequence_numbers_ RTC_GUARDED_BY(cs_); |
| 263 | |
| 264 | absl::optional<LossNotificationState> lntf_state_ RTC_GUARDED_BY(cs_); |
| 265 | }; |
| 266 | enum ParseGenericDependenciesResult { |
| 267 | kDropPacket, |
| 268 | kHasGenericDescriptor, |
| 269 | kNoGenericDescriptor |
| 270 | }; |
| 271 | |
| 272 | // Entry point doing non-stats work for a received packet. Called |
| 273 | // for the same packet both before and after RED decapsulation. |
| 274 | void ReceivePacket(const RtpPacketReceived& packet); |
| 275 | // Parses and handles RED headers. |
| 276 | // This function assumes that it's being called from only one thread. |
| 277 | void ParseAndHandleEncapsulatingHeader(const RtpPacketReceived& packet); |
| 278 | void NotifyReceiverOfEmptyPacket(uint16_t seq_num); |
| 279 | void UpdateHistograms(); |
| 280 | bool IsRedEnabled() const; |
| 281 | void InsertSpsPpsIntoTracker(uint8_t payload_type); |
| 282 | void OnInsertedPacket(video_coding::PacketBuffer::InsertResult result); |
| 283 | ParseGenericDependenciesResult ParseGenericDependenciesExtension( |
| 284 | const RtpPacketReceived& rtp_packet, |
| 285 | RTPVideoHeader* video_header) RTC_RUN_ON(worker_task_checker_); |
| 286 | void OnAssembledFrame(std::unique_ptr<video_coding::RtpFrameObject> frame); |
| 287 | |
| 288 | Clock* const clock_; |
| 289 | // Ownership of this object lies with VideoReceiveStream, which owns |this|. |
| 290 | const VideoReceiveStream::Config& config_; |
| 291 | PacketRouter* const packet_router_; |
| 292 | ProcessThread* const process_thread_; |
| 293 | |
| 294 | RemoteNtpTimeEstimator ntp_estimator_; |
| 295 | |
| 296 | RtpHeaderExtensionMap rtp_header_extensions_; |
| 297 | // Set by the field trial WebRTC-ForcePlayoutDelay to override any playout |
| 298 | // delay that is specified in the received packets. |
| 299 | FieldTrialOptional<int> forced_playout_delay_max_ms_; |
| 300 | FieldTrialOptional<int> forced_playout_delay_min_ms_; |
| 301 | ReceiveStatistics* const rtp_receive_statistics_; |
| 302 | std::unique_ptr<UlpfecReceiver> ulpfec_receiver_; |
| 303 | |
| 304 | SequenceChecker worker_task_checker_; |
| 305 | bool receiving_ RTC_GUARDED_BY(worker_task_checker_); |
| 306 | int64_t last_packet_log_ms_ RTC_GUARDED_BY(worker_task_checker_); |
| 307 | |
| 308 | const std::unique_ptr<RtpRtcp> rtp_rtcp_; |
| 309 | |
| 310 | video_coding::OnCompleteFrameCallback* complete_frame_callback_; |
| 311 | KeyFrameRequestSender* const keyframe_request_sender_; |
| 312 | |
| 313 | RtcpFeedbackBuffer rtcp_feedback_buffer_; |
| 314 | std::unique_ptr<NackModule2> nack_module_; |
| 315 | std::unique_ptr<LossNotificationController> loss_notification_controller_; |
| 316 | |
| 317 | video_coding::PacketBuffer packet_buffer_; |
| 318 | UniqueTimestampCounter frame_counter_ RTC_GUARDED_BY(worker_task_checker_); |
| 319 | SeqNumUnwrapper<uint16_t> frame_id_unwrapper_ |
| 320 | RTC_GUARDED_BY(worker_task_checker_); |
| 321 | |
| 322 | // Video structure provided in the dependency descriptor in a first packet |
| 323 | // of a key frame. It is required to parse dependency descriptor in the |
| 324 | // following delta packets. |
| 325 | std::unique_ptr<FrameDependencyStructure> video_structure_ |
| 326 | RTC_GUARDED_BY(worker_task_checker_); |
| 327 | // Frame id of the last frame with the attached video structure. |
| 328 | // absl::nullopt when `video_structure_ == nullptr`; |
| 329 | absl::optional<int64_t> video_structure_frame_id_ |
| 330 | RTC_GUARDED_BY(worker_task_checker_); |
| 331 | |
| 332 | rtc::CriticalSection reference_finder_lock_; |
| 333 | std::unique_ptr<video_coding::RtpFrameReferenceFinder> reference_finder_ |
| 334 | RTC_GUARDED_BY(reference_finder_lock_); |
| 335 | absl::optional<VideoCodecType> current_codec_; |
| 336 | uint32_t last_assembled_frame_rtp_timestamp_; |
| 337 | |
| 338 | rtc::CriticalSection last_seq_num_cs_; |
| 339 | std::map<int64_t, uint16_t> last_seq_num_for_pic_id_ |
| 340 | RTC_GUARDED_BY(last_seq_num_cs_); |
| 341 | video_coding::H264SpsPpsTracker tracker_; |
| 342 | |
| 343 | // Maps payload id to the depacketizer. |
| 344 | std::map<uint8_t, std::unique_ptr<VideoRtpDepacketizer>> payload_type_map_; |
| 345 | |
| 346 | // TODO(johan): Remove pt_codec_params_ once |
| 347 | // https://bugs.chromium.org/p/webrtc/issues/detail?id=6883 is resolved. |
| 348 | // Maps a payload type to a map of out-of-band supplied codec parameters. |
| 349 | std::map<uint8_t, std::map<std::string, std::string>> pt_codec_params_; |
| 350 | int16_t last_payload_type_ = -1; |
| 351 | |
| 352 | bool has_received_frame_; |
| 353 | |
| 354 | std::vector<RtpPacketSinkInterface*> secondary_sinks_ |
| 355 | RTC_GUARDED_BY(worker_task_checker_); |
| 356 | |
| 357 | // Info for GetSyncInfo is updated on network or worker thread, and queried on |
| 358 | // the worker thread. |
| 359 | rtc::CriticalSection sync_info_lock_; |
| 360 | absl::optional<uint32_t> last_received_rtp_timestamp_ |
| 361 | RTC_GUARDED_BY(sync_info_lock_); |
| 362 | absl::optional<int64_t> last_received_rtp_system_time_ms_ |
| 363 | RTC_GUARDED_BY(sync_info_lock_); |
| 364 | |
| 365 | // Used to validate the buffered frame decryptor is always run on the correct |
| 366 | // thread. |
| 367 | rtc::ThreadChecker network_tc_; |
| 368 | // Handles incoming encrypted frames and forwards them to the |
| 369 | // rtp_reference_finder if they are decryptable. |
| 370 | std::unique_ptr<BufferedFrameDecryptor> buffered_frame_decryptor_ |
| 371 | RTC_PT_GUARDED_BY(network_tc_); |
| 372 | std::atomic<bool> frames_decryptable_; |
| 373 | absl::optional<ColorSpace> last_color_space_; |
| 374 | |
| 375 | AbsoluteCaptureTimeReceiver absolute_capture_time_receiver_ |
| 376 | RTC_GUARDED_BY(worker_task_checker_); |
| 377 | |
| 378 | int64_t last_completed_picture_id_ = 0; |
| 379 | |
| 380 | rtc::scoped_refptr<RtpVideoStreamReceiverFrameTransformerDelegate> |
| 381 | frame_transformer_delegate_; |
| 382 | }; |
| 383 | |
| 384 | } // namespace webrtc |
| 385 | |
| 386 | #endif // VIDEO_RTP_VIDEO_STREAM_RECEIVER2_H_ |