blob: e57a32d01d01cd3f54ca6acd807355e819e98e50 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
leozwang@webrtc.org39e96592012-03-01 18:22:48 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef VIDEO_RTP_VIDEO_STREAM_RECEIVER_H_
12#define VIDEO_RTP_VIDEO_STREAM_RECEIVER_H_
niklase@google.com470e71d2011-07-07 08:21:25 +000013
Benjamin Wright52426ed2019-03-01 11:01:59 -080014#include <atomic>
pwestin@webrtc.org1da1ce02011-10-13 15:19:55 +000015#include <list>
philipelfd5a20f2016-11-15 00:57:57 -080016#include <map>
kwiberg27f982b2016-03-01 11:52:33 -080017#include <memory>
Peter Boström9c017252016-02-26 16:26:20 +010018#include <string>
kjellander@webrtc.org0fcaf992015-11-26 15:24:52 +010019#include <vector>
pwestin@webrtc.org1da1ce02011-10-13 15:19:55 +000020
Niels Möller2ff1f2a2018-08-09 16:16:34 +020021#include "absl/types/optional.h"
Steve Anton10542f22019-01-11 09:11:00 -080022#include "api/crypto/frame_decryptor_interface.h"
Johannes Krond0b69a82018-12-03 14:18:53 +010023#include "api/video/color_space.h"
Niels Möller2ff1f2a2018-08-09 16:16:34 +020024#include "api/video_codecs/video_codec.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "call/rtp_packet_sink_interface.h"
Niels Möllerdf9e9ae2018-07-31 08:29:53 +020026#include "call/syncable.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "call/video_receive_stream.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "modules/rtp_rtcp/include/receive_statistics.h"
29#include "modules/rtp_rtcp/include/remote_ntp_time_estimator.h"
Niels Möllerb0573bc2017-09-25 10:47:00 +020030#include "modules/rtp_rtcp/include/rtp_header_extension_map.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020031#include "modules/rtp_rtcp/include/rtp_rtcp.h"
32#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
Jonas Oreland49ac5952018-09-26 16:04:32 +020033#include "modules/rtp_rtcp/source/contributing_sources.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020034#include "modules/video_coding/h264_sps_pps_tracker.h"
Elad Alon7d6a4c02019-02-25 13:00:51 +010035#include "modules/video_coding/loss_notification_controller.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020036#include "modules/video_coding/packet_buffer.h"
37#include "modules/video_coding/rtp_frame_reference_finder.h"
Steve Anton10542f22019-01-11 09:11:00 -080038#include "rtc_base/constructor_magic.h"
39#include "rtc_base/critical_section.h"
Bjorn Tereliusa194e582017-10-25 13:07:09 +020040#include "rtc_base/numerics/sequence_number_util.h"
Sebastian Janssonb55015e2019-04-09 13:44:04 +020041#include "rtc_base/synchronization/sequence_checker.h"
Benjamin Wright00765292018-11-30 16:18:26 -080042#include "rtc_base/thread_annotations.h"
43#include "rtc_base/thread_checker.h"
44#include "video/buffered_frame_decryptor.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000045
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +000046namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000047
philipelfd5a20f2016-11-15 00:57:57 -080048class NackModule;
mflodmanc0e58a32016-04-25 01:26:26 -070049class PacketRouter;
mflodmandc7d0d22016-05-06 05:32:22 -070050class ProcessThread;
wu@webrtc.org822fbd82013-08-15 23:38:54 +000051class ReceiveStatistics;
mflodmancfc8e3b2016-05-03 21:22:04 -070052class ReceiveStatisticsProxy;
mflodmanc0e58a32016-04-25 01:26:26 -070053class RtcpRttStats;
nisse38cc1d62017-02-13 05:59:46 -080054class RtpPacketReceived;
mflodmanc0e58a32016-04-25 01:26:26 -070055class Transport;
brandtrd55c3f62016-10-31 04:51:33 -070056class UlpfecReceiver;
Peter Boström0b250722016-04-22 18:23:15 +020057
Elad Alon7d6a4c02019-02-25 13:00:51 +010058class RtpVideoStreamReceiver : public LossNotificationSender,
59 public RecoveredPacketReceiver,
nisse0f15f922017-06-21 01:05:22 -070060 public RtpPacketSinkInterface,
Niels Möller479c0552019-05-23 16:57:01 +020061 public KeyFrameRequestSender,
Elad Alonb4643ad2019-02-22 11:19:50 +010062 public video_coding::OnAssembledFrameCallback,
Benjamin Wright00765292018-11-30 16:18:26 -080063 public video_coding::OnCompleteFrameCallback,
Benjamin Wright52426ed2019-03-01 11:01:59 -080064 public OnDecryptedFrameCallback,
65 public OnDecryptionStatusChangeCallback {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +000066 public:
nisseb1f2ff92017-06-09 04:01:55 -070067 RtpVideoStreamReceiver(
Sebastian Jansson8026d602019-03-04 19:39:01 +010068 Clock* clock,
philipelfd5a20f2016-11-15 00:57:57 -080069 Transport* transport,
70 RtcpRttStats* rtt_stats,
Niels Möller60f4e292019-05-20 11:06:33 +020071 // The packet router is optional; if provided, the RtpRtcp module for this
72 // stream is registered as a candidate for sending REMB and transport
73 // feedback.
philipelfd5a20f2016-11-15 00:57:57 -080074 PacketRouter* packet_router,
philipelfd5a20f2016-11-15 00:57:57 -080075 const VideoReceiveStream::Config* config,
nisseca5706d2017-09-11 02:32:16 -070076 ReceiveStatistics* rtp_receive_statistics,
philipelfd5a20f2016-11-15 00:57:57 -080077 ReceiveStatisticsProxy* receive_stats_proxy,
78 ProcessThread* process_thread,
philipelfd5a20f2016-11-15 00:57:57 -080079 NackSender* nack_sender,
Benjamin Wright192eeec2018-10-17 17:27:25 -070080 video_coding::OnCompleteFrameCallback* complete_frame_callback,
81 rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor);
Niels Möller479c0552019-05-23 16:57:01 +020082
83 // Constructor with injected RtpRtcp. Intended for tests only!
84 RtpVideoStreamReceiver(
85 Clock* clock,
86 std::unique_ptr<RtpRtcp> rtp_rtcp,
87 // The packet router is optional; if provided, the RtpRtcp module for this
88 // stream is registered as a candidate for sending REMB and transport
89 // feedback.
90 PacketRouter* packet_router,
91 const VideoReceiveStream::Config* config,
92 ReceiveStatistics* rtp_receive_statistics,
93 ReceiveStatisticsProxy* receive_stats_proxy,
94 ProcessThread* process_thread,
95 NackSender* nack_sender,
96 video_coding::OnCompleteFrameCallback* complete_frame_callback,
97 rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor);
98
Mirko Bonadei8fdcac32018-08-28 16:30:18 +020099 ~RtpVideoStreamReceiver() override;
niklase@google.com470e71d2011-07-07 08:21:25 +0000100
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200101 void AddReceiveCodec(const VideoCodec& video_codec,
Mirta Dvornicicfe68daa2019-05-23 13:21:12 +0200102 const std::map<std::string, std::string>& codec_params,
103 bool raw_payload);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000104
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000105 void StartReceive();
106 void StopReceive();
niklase@google.com470e71d2011-07-07 08:21:25 +0000107
Niels Möllerdf9e9ae2018-07-31 08:29:53 +0200108 // Produces the transport-related timestamps; current_delay_ms is left unset.
109 absl::optional<Syncable::Info> GetSyncInfo() const;
110
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100111 bool DeliverRtcp(const uint8_t* rtcp_packet, size_t rtcp_packet_length);
niklase@google.com470e71d2011-07-07 08:21:25 +0000112
philipeld4fac692017-09-04 07:03:46 -0700113 void FrameContinuous(int64_t seq_num);
philipelfd5a20f2016-11-15 00:57:57 -0800114
philipeld4fac692017-09-04 07:03:46 -0700115 void FrameDecoded(int64_t seq_num);
philipelfd5a20f2016-11-15 00:57:57 -0800116
mflodmandc7d0d22016-05-06 05:32:22 -0700117 void SignalNetworkState(NetworkState state);
118
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +0100119 // Returns number of different frames seen in the packet buffer.
120 int GetUniqueFramesSeen() const;
121
nisse0f15f922017-06-21 01:05:22 -0700122 // Implements RtpPacketSinkInterface.
123 void OnRtpPacket(const RtpPacketReceived& packet) override;
nisse38cc1d62017-02-13 05:59:46 -0800124
philipel2837edc2018-10-02 13:55:47 +0200125 // TODO(philipel): Stop using VCMPacket in the new jitter buffer and then
Niels Möller125b5d62019-03-11 16:11:07 +0100126 // remove this function. Public only for tests.
philipel2837edc2018-10-02 13:55:47 +0200127 int32_t OnReceivedPayloadData(
128 const uint8_t* payload_data,
129 size_t payload_size,
Niels Möller125b5d62019-03-11 16:11:07 +0100130 const RTPHeader& rtp_header,
131 const RTPVideoHeader& video_header,
Ying Wangb32bb952018-10-31 10:12:27 +0100132 const absl::optional<RtpGenericFrameDescriptor>& generic_descriptor,
133 bool is_recovered);
philipel2837edc2018-10-02 13:55:47 +0200134
nisse30e89312017-05-29 08:16:37 -0700135 // Implements RecoveredPacketReceiver.
136 void OnRecoveredPacket(const uint8_t* packet, size_t packet_length) override;
niklase@google.com470e71d2011-07-07 08:21:25 +0000137
Niels Möller41684372019-03-25 15:51:03 +0100138 // Send an RTCP keyframe request.
Niels Möller479c0552019-05-23 16:57:01 +0200139 void RequestKeyFrame() override;
mflodmancfc8e3b2016-05-03 21:22:04 -0700140
Elad Alon7d6a4c02019-02-25 13:00:51 +0100141 // Implements LossNotificationSender.
142 void SendLossNotification(uint16_t last_decoded_seq_num,
143 uint16_t last_received_seq_num,
144 bool decodability_flag) override;
145
brandtrf7c6d722016-12-08 08:25:47 -0800146 bool IsUlpfecEnabled() const;
mflodmandc7d0d22016-05-06 05:32:22 -0700147 bool IsRetransmissionsEnabled() const;
Benjamin Wright52426ed2019-03-01 11:01:59 -0800148
149 // Returns true if a decryptor is attached and frames can be decrypted.
150 // Updated by OnDecryptionStatusChangeCallback. Note this refers to Frame
151 // Decryption not SRTP.
152 bool IsDecryptable() const;
153
mflodmandc7d0d22016-05-06 05:32:22 -0700154 // Don't use, still experimental.
155 void RequestPacketRetransmit(const std::vector<uint16_t>& sequence_numbers);
156
Elad Alonb4643ad2019-02-22 11:19:50 +0100157 // Implements OnAssembledFrameCallback.
158 void OnAssembledFrame(
philipelfd5a20f2016-11-15 00:57:57 -0800159 std::unique_ptr<video_coding::RtpFrameObject> frame) override;
160
161 // Implements OnCompleteFrameCallback.
162 void OnCompleteFrame(
philipele7c891f2018-02-22 14:35:06 +0100163 std::unique_ptr<video_coding::EncodedFrame> frame) override;
philipelfd5a20f2016-11-15 00:57:57 -0800164
Benjamin Wright00765292018-11-30 16:18:26 -0800165 // Implements OnDecryptedFrameCallback.
166 void OnDecryptedFrame(
167 std::unique_ptr<video_coding::RtpFrameObject> frame) override;
168
Benjamin Wright52426ed2019-03-01 11:01:59 -0800169 // Implements OnDecryptionStatusChangeCallback.
Benjamin Wright2af5dcb2019-04-09 20:08:41 +0000170 void OnDecryptionStatusChange(
171 FrameDecryptorInterface::Status status) override;
Benjamin Wright52426ed2019-03-01 11:01:59 -0800172
Benjamin Wrighta5564482019-04-03 10:44:18 -0700173 // Optionally set a frame decryptor after a stream has started. This will not
174 // reset the decoder state.
175 void SetFrameDecryptor(
176 rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor);
177
Tommi81de14f2018-03-25 22:19:25 +0200178 // Called by VideoReceiveStream when stats are updated.
179 void UpdateRtt(int64_t max_rtt_ms);
philipelfd5a20f2016-11-15 00:57:57 -0800180
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200181 absl::optional<int64_t> LastReceivedPacketMs() const;
182 absl::optional<int64_t> LastReceivedKeyframePacketMs() const;
philipel3184f8e2017-05-18 08:08:53 -0700183
eladalonc0d481a2017-08-02 07:39:07 -0700184 // RtpDemuxer only forwards a given RTP packet to one sink. However, some
185 // sinks, such as FlexFEC, might wish to be informed of all of the packets
186 // a given sink receives (or any set of sinks). They may do so by registering
187 // themselves as secondary sinks.
188 void AddSecondarySink(RtpPacketSinkInterface* sink);
189 void RemoveSecondarySink(const RtpPacketSinkInterface* sink);
190
Jonas Oreland49ac5952018-09-26 16:04:32 +0200191 std::vector<webrtc::RtpSource> GetSources() const;
192
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000193 private:
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200194 // Entry point doing non-stats work for a received packet. Called
195 // for the same packet both before and after RED decapsulation.
196 void ReceivePacket(const RtpPacketReceived& packet);
197 // Parses and handles RED headers.
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000198 // This function assumes that it's being called from only one thread.
Niels Möller1f3206c2018-09-14 08:26:32 +0200199 void ParseAndHandleEncapsulatingHeader(const RtpPacketReceived& packet);
Niels Möllerbc010472018-03-23 13:22:29 +0100200 void NotifyReceiverOfEmptyPacket(uint16_t seq_num);
asapersson@webrtc.org0800db72015-01-15 07:40:20 +0000201 void UpdateHistograms();
brandtre6f98c72016-11-11 03:28:30 -0800202 bool IsRedEnabled() const;
philipel022b54e2016-12-20 04:15:59 -0800203 void InsertSpsPpsIntoTracker(uint8_t payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +0000204
Peter Boström4fa7eca2016-03-02 15:05:53 +0100205 Clock* const clock_;
Tommi733b5472016-06-10 17:58:01 +0200206 // Ownership of this object lies with VideoReceiveStream, which owns |this|.
207 const VideoReceiveStream::Config& config_;
mflodmanc0e58a32016-04-25 01:26:26 -0700208 PacketRouter* const packet_router_;
mflodmandc7d0d22016-05-06 05:32:22 -0700209 ProcessThread* const process_thread_;
Peter Boström4fa7eca2016-03-02 15:05:53 +0100210
211 RemoteNtpTimeEstimator ntp_estimator_;
Peter Boström4fa7eca2016-03-02 15:05:53 +0100212
Niels Möllerb0573bc2017-09-25 10:47:00 +0200213 RtpHeaderExtensionMap rtp_header_extensions_;
nisseca5706d2017-09-11 02:32:16 -0700214 ReceiveStatistics* const rtp_receive_statistics_;
brandtrd55c3f62016-10-31 04:51:33 -0700215 std::unique_ptr<UlpfecReceiver> ulpfec_receiver_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000216
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200217 SequenceChecker worker_task_checker_;
danilchapa37de392017-09-09 04:17:22 -0700218 bool receiving_ RTC_GUARDED_BY(worker_task_checker_);
danilchapa37de392017-09-09 04:17:22 -0700219 int64_t last_packet_log_ms_ RTC_GUARDED_BY(worker_task_checker_);
mflodmanc0e58a32016-04-25 01:26:26 -0700220
221 const std::unique_ptr<RtpRtcp> rtp_rtcp_;
philipelfd5a20f2016-11-15 00:57:57 -0800222
223 // Members for the new jitter buffer experiment.
philipelfd5a20f2016-11-15 00:57:57 -0800224 video_coding::OnCompleteFrameCallback* complete_frame_callback_;
philipelfd5a20f2016-11-15 00:57:57 -0800225 std::unique_ptr<NackModule> nack_module_;
Elad Alon7d6a4c02019-02-25 13:00:51 +0100226 std::unique_ptr<LossNotificationController> loss_notification_controller_;
philipelfd5a20f2016-11-15 00:57:57 -0800227 rtc::scoped_refptr<video_coding::PacketBuffer> packet_buffer_;
228 std::unique_ptr<video_coding::RtpFrameReferenceFinder> reference_finder_;
229 rtc::CriticalSection last_seq_num_cs_;
philipeld4fac692017-09-04 07:03:46 -0700230 std::map<int64_t, uint16_t> last_seq_num_for_pic_id_
danilchapa37de392017-09-09 04:17:22 -0700231 RTC_GUARDED_BY(last_seq_num_cs_);
philipelfd5a20f2016-11-15 00:57:57 -0800232 video_coding::H264SpsPpsTracker tracker_;
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200233
Mirta Dvornicicfe68daa2019-05-23 13:21:12 +0200234 // Maps payload type to codec type, for packetization.
235 std::map<uint8_t, absl::optional<VideoCodecType>> payload_type_map_;
236
philipel022b54e2016-12-20 04:15:59 -0800237 // TODO(johan): Remove pt_codec_params_ once
238 // https://bugs.chromium.org/p/webrtc/issues/detail?id=6883 is resolved.
239 // Maps a payload type to a map of out-of-band supplied codec parameters.
240 std::map<uint8_t, std::map<std::string, std::string>> pt_codec_params_;
241 int16_t last_payload_type_ = -1;
philipel2c53b132017-05-16 08:06:30 -0700242
243 bool has_received_frame_;
eladalonc0d481a2017-08-02 07:39:07 -0700244
eladalon8b073052017-08-25 00:49:08 -0700245 std::vector<RtpPacketSinkInterface*> secondary_sinks_
danilchapa37de392017-09-09 04:17:22 -0700246 RTC_GUARDED_BY(worker_task_checker_);
Jonas Oreland49ac5952018-09-26 16:04:32 +0200247
248 // Info for GetSources and GetSyncInfo is updated on network or worker thread,
249 // queried on the worker thread.
250 rtc::CriticalSection rtp_sources_lock_;
251 ContributingSources contributing_sources_ RTC_GUARDED_BY(&rtp_sources_lock_);
252 absl::optional<uint32_t> last_received_rtp_timestamp_
253 RTC_GUARDED_BY(rtp_sources_lock_);
254 absl::optional<int64_t> last_received_rtp_system_time_ms_
255 RTC_GUARDED_BY(rtp_sources_lock_);
Benjamin Wright192eeec2018-10-17 17:27:25 -0700256
Benjamin Wright00765292018-11-30 16:18:26 -0800257 // Used to validate the buffered frame decryptor is always run on the correct
258 // thread.
259 rtc::ThreadChecker network_tc_;
260 // Handles incoming encrypted frames and forwards them to the
261 // rtp_reference_finder if they are decryptable.
262 std::unique_ptr<BufferedFrameDecryptor> buffered_frame_decryptor_
263 RTC_PT_GUARDED_BY(network_tc_);
Benjamin Wright52426ed2019-03-01 11:01:59 -0800264 std::atomic<bool> frames_decryptable_;
Johannes Krond0b69a82018-12-03 14:18:53 +0100265 absl::optional<ColorSpace> last_color_space_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000266};
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000267
kjellander@webrtc.org0fcaf992015-11-26 15:24:52 +0100268} // namespace webrtc
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000269
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200270#endif // VIDEO_RTP_VIDEO_STREAM_RECEIVER_H_