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