blob: ec3f354e260c511c9c4c4bf4fa45e33dc87127fe [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
pwestin@webrtc.org1da1ce02011-10-13 15:19:55 +000014#include <list>
philipelfd5a20f2016-11-15 00:57:57 -080015#include <map>
kwiberg27f982b2016-03-01 11:52:33 -080016#include <memory>
Peter Boström9c017252016-02-26 16:26:20 +010017#include <string>
kjellander@webrtc.org0fcaf992015-11-26 15:24:52 +010018#include <vector>
pwestin@webrtc.org1da1ce02011-10-13 15:19:55 +000019
Niels Möller2ff1f2a2018-08-09 16:16:34 +020020#include "absl/types/optional.h"
21
Benjamin Wright192eeec2018-10-17 17:27:25 -070022#include "api/crypto/framedecryptorinterface.h"
Niels Möller2ff1f2a2018-08-09 16:16:34 +020023#include "api/video_codecs/video_codec.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "call/rtp_packet_sink_interface.h"
Niels Möllerdf9e9ae2018-07-31 08:29:53 +020025#include "call/syncable.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "call/video_receive_stream.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "modules/rtp_rtcp/include/receive_statistics.h"
28#include "modules/rtp_rtcp/include/remote_ntp_time_estimator.h"
Niels Möllerb0573bc2017-09-25 10:47:00 +020029#include "modules/rtp_rtcp/include/rtp_header_extension_map.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020030#include "modules/rtp_rtcp/include/rtp_rtcp.h"
31#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
Jonas Oreland49ac5952018-09-26 16:04:32 +020032#include "modules/rtp_rtcp/source/contributing_sources.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020033#include "modules/video_coding/h264_sps_pps_tracker.h"
34#include "modules/video_coding/include/video_coding_defines.h"
35#include "modules/video_coding/packet_buffer.h"
36#include "modules/video_coding/rtp_frame_reference_finder.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020037#include "rtc_base/constructormagic.h"
38#include "rtc_base/criticalsection.h"
Bjorn Tereliusa194e582017-10-25 13:07:09 +020039#include "rtc_base/numerics/sequence_number_util.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020040#include "rtc_base/sequenced_task_checker.h"
Benjamin Wright00765292018-11-30 16:18:26 -080041#include "rtc_base/thread_annotations.h"
42#include "rtc_base/thread_checker.h"
43#include "video/buffered_frame_decryptor.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000044
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +000045namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000046
philipelfd5a20f2016-11-15 00:57:57 -080047class NackModule;
mflodmanc0e58a32016-04-25 01:26:26 -070048class PacketRouter;
mflodmandc7d0d22016-05-06 05:32:22 -070049class ProcessThread;
wu@webrtc.org822fbd82013-08-15 23:38:54 +000050class ReceiveStatistics;
mflodmancfc8e3b2016-05-03 21:22:04 -070051class ReceiveStatisticsProxy;
mflodmanc0e58a32016-04-25 01:26:26 -070052class RtcpRttStats;
nisse38cc1d62017-02-13 05:59:46 -080053class RtpPacketReceived;
mflodmanc0e58a32016-04-25 01:26:26 -070054class Transport;
brandtrd55c3f62016-10-31 04:51:33 -070055class UlpfecReceiver;
Peter Boström0b250722016-04-22 18:23:15 +020056
philipel2837edc2018-10-02 13:55:47 +020057class RtpVideoStreamReceiver : public RecoveredPacketReceiver,
nisse0f15f922017-06-21 01:05:22 -070058 public RtpPacketSinkInterface,
nisseb1f2ff92017-06-09 04:01:55 -070059 public VCMFrameTypeCallback,
60 public VCMPacketRequestCallback,
61 public video_coding::OnReceivedFrameCallback,
Benjamin Wright00765292018-11-30 16:18:26 -080062 public video_coding::OnCompleteFrameCallback,
63 public OnDecryptedFrameCallback {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +000064 public:
nisseb1f2ff92017-06-09 04:01:55 -070065 RtpVideoStreamReceiver(
philipelfd5a20f2016-11-15 00:57:57 -080066 Transport* transport,
67 RtcpRttStats* rtt_stats,
philipelfd5a20f2016-11-15 00:57:57 -080068 PacketRouter* packet_router,
philipelfd5a20f2016-11-15 00:57:57 -080069 const VideoReceiveStream::Config* config,
nisseca5706d2017-09-11 02:32:16 -070070 ReceiveStatistics* rtp_receive_statistics,
philipelfd5a20f2016-11-15 00:57:57 -080071 ReceiveStatisticsProxy* receive_stats_proxy,
72 ProcessThread* process_thread,
philipelfd5a20f2016-11-15 00:57:57 -080073 NackSender* nack_sender,
74 KeyFrameRequestSender* keyframe_request_sender,
Benjamin Wright192eeec2018-10-17 17:27:25 -070075 video_coding::OnCompleteFrameCallback* complete_frame_callback,
76 rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor);
Mirko Bonadei8fdcac32018-08-28 16:30:18 +020077 ~RtpVideoStreamReceiver() override;
niklase@google.com470e71d2011-07-07 08:21:25 +000078
Niels Möller2ff1f2a2018-08-09 16:16:34 +020079 void AddReceiveCodec(const VideoCodec& video_codec,
philipel022b54e2016-12-20 04:15:59 -080080 const std::map<std::string, std::string>& codec_params);
wu@webrtc.org822fbd82013-08-15 23:38:54 +000081
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +000082 void StartReceive();
83 void StopReceive();
niklase@google.com470e71d2011-07-07 08:21:25 +000084
Niels Möllerdf9e9ae2018-07-31 08:29:53 +020085 // Produces the transport-related timestamps; current_delay_ms is left unset.
86 absl::optional<Syncable::Info> GetSyncInfo() const;
87
Peter Boströmd1d66ba2016-02-08 14:07:14 +010088 bool DeliverRtcp(const uint8_t* rtcp_packet, size_t rtcp_packet_length);
niklase@google.com470e71d2011-07-07 08:21:25 +000089
philipeld4fac692017-09-04 07:03:46 -070090 void FrameContinuous(int64_t seq_num);
philipelfd5a20f2016-11-15 00:57:57 -080091
philipeld4fac692017-09-04 07:03:46 -070092 void FrameDecoded(int64_t seq_num);
philipelfd5a20f2016-11-15 00:57:57 -080093
mflodmandc7d0d22016-05-06 05:32:22 -070094 void SignalNetworkState(NetworkState state);
95
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +010096 // Returns number of different frames seen in the packet buffer.
97 int GetUniqueFramesSeen() const;
98
nisse0f15f922017-06-21 01:05:22 -070099 // Implements RtpPacketSinkInterface.
100 void OnRtpPacket(const RtpPacketReceived& packet) override;
nisse38cc1d62017-02-13 05:59:46 -0800101
philipel2837edc2018-10-02 13:55:47 +0200102 // TODO(philipel): Stop using VCMPacket in the new jitter buffer and then
103 // remove this function.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000104 int32_t OnReceivedPayloadData(const uint8_t* payload_data,
Peter Boström02083222016-06-14 12:52:54 +0200105 size_t payload_size,
philipel2837edc2018-10-02 13:55:47 +0200106 const WebRtcRTPHeader* rtp_header);
107 int32_t OnReceivedPayloadData(
108 const uint8_t* payload_data,
109 size_t payload_size,
110 const WebRtcRTPHeader* rtp_header,
Ying Wangb32bb952018-10-31 10:12:27 +0100111 const absl::optional<RtpGenericFrameDescriptor>& generic_descriptor,
112 bool is_recovered);
philipel2837edc2018-10-02 13:55:47 +0200113
nisse30e89312017-05-29 08:16:37 -0700114 // Implements RecoveredPacketReceiver.
115 void OnRecoveredPacket(const uint8_t* packet, size_t packet_length) override;
niklase@google.com470e71d2011-07-07 08:21:25 +0000116
mflodmancfc8e3b2016-05-03 21:22:04 -0700117 // Implements VCMFrameTypeCallback.
118 int32_t RequestKeyFrame() override;
mflodmancfc8e3b2016-05-03 21:22:04 -0700119
brandtrf7c6d722016-12-08 08:25:47 -0800120 bool IsUlpfecEnabled() const;
mflodmandc7d0d22016-05-06 05:32:22 -0700121 bool IsRetransmissionsEnabled() const;
122 // Don't use, still experimental.
123 void RequestPacketRetransmit(const std::vector<uint16_t>& sequence_numbers);
124
mflodmancfc8e3b2016-05-03 21:22:04 -0700125 // Implements VCMPacketRequestCallback.
126 int32_t ResendPackets(const uint16_t* sequenceNumbers,
127 uint16_t length) override;
128
philipelfd5a20f2016-11-15 00:57:57 -0800129 // Implements OnReceivedFrameCallback.
130 void OnReceivedFrame(
131 std::unique_ptr<video_coding::RtpFrameObject> frame) override;
132
133 // Implements OnCompleteFrameCallback.
134 void OnCompleteFrame(
philipele7c891f2018-02-22 14:35:06 +0100135 std::unique_ptr<video_coding::EncodedFrame> frame) override;
philipelfd5a20f2016-11-15 00:57:57 -0800136
Benjamin Wright00765292018-11-30 16:18:26 -0800137 // Implements OnDecryptedFrameCallback.
138 void OnDecryptedFrame(
139 std::unique_ptr<video_coding::RtpFrameObject> frame) override;
140
Tommi81de14f2018-03-25 22:19:25 +0200141 // Called by VideoReceiveStream when stats are updated.
142 void UpdateRtt(int64_t max_rtt_ms);
philipelfd5a20f2016-11-15 00:57:57 -0800143
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200144 absl::optional<int64_t> LastReceivedPacketMs() const;
145 absl::optional<int64_t> LastReceivedKeyframePacketMs() const;
philipel3184f8e2017-05-18 08:08:53 -0700146
eladalonc0d481a2017-08-02 07:39:07 -0700147 // RtpDemuxer only forwards a given RTP packet to one sink. However, some
148 // sinks, such as FlexFEC, might wish to be informed of all of the packets
149 // a given sink receives (or any set of sinks). They may do so by registering
150 // themselves as secondary sinks.
151 void AddSecondarySink(RtpPacketSinkInterface* sink);
152 void RemoveSecondarySink(const RtpPacketSinkInterface* sink);
153
Jonas Oreland49ac5952018-09-26 16:04:32 +0200154 std::vector<webrtc::RtpSource> GetSources() const;
155
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000156 private:
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200157 // Entry point doing non-stats work for a received packet. Called
158 // for the same packet both before and after RED decapsulation.
159 void ReceivePacket(const RtpPacketReceived& packet);
160 // Parses and handles RED headers.
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000161 // This function assumes that it's being called from only one thread.
Niels Möller1f3206c2018-09-14 08:26:32 +0200162 void ParseAndHandleEncapsulatingHeader(const RtpPacketReceived& packet);
Niels Möllerbc010472018-03-23 13:22:29 +0100163 void NotifyReceiverOfEmptyPacket(uint16_t seq_num);
asapersson@webrtc.org0800db72015-01-15 07:40:20 +0000164 void UpdateHistograms();
brandtre6f98c72016-11-11 03:28:30 -0800165 bool IsRedEnabled() const;
philipel022b54e2016-12-20 04:15:59 -0800166 void InsertSpsPpsIntoTracker(uint8_t payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +0000167
Peter Boström4fa7eca2016-03-02 15:05:53 +0100168 Clock* const clock_;
Tommi733b5472016-06-10 17:58:01 +0200169 // Ownership of this object lies with VideoReceiveStream, which owns |this|.
170 const VideoReceiveStream::Config& config_;
mflodmanc0e58a32016-04-25 01:26:26 -0700171 PacketRouter* const packet_router_;
mflodmandc7d0d22016-05-06 05:32:22 -0700172 ProcessThread* const process_thread_;
Peter Boström4fa7eca2016-03-02 15:05:53 +0100173
174 RemoteNtpTimeEstimator ntp_estimator_;
Peter Boström4fa7eca2016-03-02 15:05:53 +0100175
Niels Möllerb0573bc2017-09-25 10:47:00 +0200176 RtpHeaderExtensionMap rtp_header_extensions_;
nisseca5706d2017-09-11 02:32:16 -0700177 ReceiveStatistics* const rtp_receive_statistics_;
brandtrd55c3f62016-10-31 04:51:33 -0700178 std::unique_ptr<UlpfecReceiver> ulpfec_receiver_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000179
eladalon8b073052017-08-25 00:49:08 -0700180 rtc::SequencedTaskChecker worker_task_checker_;
danilchapa37de392017-09-09 04:17:22 -0700181 bool receiving_ RTC_GUARDED_BY(worker_task_checker_);
danilchapa37de392017-09-09 04:17:22 -0700182 int64_t last_packet_log_ms_ RTC_GUARDED_BY(worker_task_checker_);
mflodmanc0e58a32016-04-25 01:26:26 -0700183
184 const std::unique_ptr<RtpRtcp> rtp_rtcp_;
philipelfd5a20f2016-11-15 00:57:57 -0800185
186 // Members for the new jitter buffer experiment.
philipelfd5a20f2016-11-15 00:57:57 -0800187 video_coding::OnCompleteFrameCallback* complete_frame_callback_;
188 KeyFrameRequestSender* keyframe_request_sender_;
philipelfd5a20f2016-11-15 00:57:57 -0800189 std::unique_ptr<NackModule> nack_module_;
190 rtc::scoped_refptr<video_coding::PacketBuffer> packet_buffer_;
191 std::unique_ptr<video_coding::RtpFrameReferenceFinder> reference_finder_;
192 rtc::CriticalSection last_seq_num_cs_;
philipeld4fac692017-09-04 07:03:46 -0700193 std::map<int64_t, uint16_t> last_seq_num_for_pic_id_
danilchapa37de392017-09-09 04:17:22 -0700194 RTC_GUARDED_BY(last_seq_num_cs_);
philipelfd5a20f2016-11-15 00:57:57 -0800195 video_coding::H264SpsPpsTracker tracker_;
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200196
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200197 std::map<uint8_t, VideoCodecType> pt_codec_type_;
philipel022b54e2016-12-20 04:15:59 -0800198 // TODO(johan): Remove pt_codec_params_ once
199 // https://bugs.chromium.org/p/webrtc/issues/detail?id=6883 is resolved.
200 // Maps a payload type to a map of out-of-band supplied codec parameters.
201 std::map<uint8_t, std::map<std::string, std::string>> pt_codec_params_;
202 int16_t last_payload_type_ = -1;
philipel2c53b132017-05-16 08:06:30 -0700203
204 bool has_received_frame_;
eladalonc0d481a2017-08-02 07:39:07 -0700205
eladalon8b073052017-08-25 00:49:08 -0700206 std::vector<RtpPacketSinkInterface*> secondary_sinks_
danilchapa37de392017-09-09 04:17:22 -0700207 RTC_GUARDED_BY(worker_task_checker_);
Jonas Oreland49ac5952018-09-26 16:04:32 +0200208
209 // Info for GetSources and GetSyncInfo is updated on network or worker thread,
210 // queried on the worker thread.
211 rtc::CriticalSection rtp_sources_lock_;
212 ContributingSources contributing_sources_ RTC_GUARDED_BY(&rtp_sources_lock_);
213 absl::optional<uint32_t> last_received_rtp_timestamp_
214 RTC_GUARDED_BY(rtp_sources_lock_);
215 absl::optional<int64_t> last_received_rtp_system_time_ms_
216 RTC_GUARDED_BY(rtp_sources_lock_);
Benjamin Wright192eeec2018-10-17 17:27:25 -0700217
Benjamin Wright00765292018-11-30 16:18:26 -0800218 // Used to validate the buffered frame decryptor is always run on the correct
219 // thread.
220 rtc::ThreadChecker network_tc_;
221 // Handles incoming encrypted frames and forwards them to the
222 // rtp_reference_finder if they are decryptable.
223 std::unique_ptr<BufferedFrameDecryptor> buffered_frame_decryptor_
224 RTC_PT_GUARDED_BY(network_tc_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000225};
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000226
kjellander@webrtc.org0fcaf992015-11-26 15:24:52 +0100227} // namespace webrtc
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000228
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200229#endif // VIDEO_RTP_VIDEO_STREAM_RECEIVER_H_