blob: 20463b9fe80f40b4f702f12436b26e3e16b00bee [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
22#include "api/video_codecs/video_codec.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "call/rtp_packet_sink_interface.h"
Niels Möllerdf9e9ae2018-07-31 08:29:53 +020024#include "call/syncable.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "call/video_receive_stream.h"
26#include "modules/include/module_common_types.h"
27#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"
32#include "modules/video_coding/h264_sps_pps_tracker.h"
33#include "modules/video_coding/include/video_coding_defines.h"
34#include "modules/video_coding/packet_buffer.h"
35#include "modules/video_coding/rtp_frame_reference_finder.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020036#include "rtc_base/constructormagic.h"
37#include "rtc_base/criticalsection.h"
Bjorn Tereliusa194e582017-10-25 13:07:09 +020038#include "rtc_base/numerics/sequence_number_util.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020039#include "rtc_base/sequenced_task_checker.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000040
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +000041namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000042
philipelfd5a20f2016-11-15 00:57:57 -080043class NackModule;
mflodmanc0e58a32016-04-25 01:26:26 -070044class PacketRouter;
mflodmandc7d0d22016-05-06 05:32:22 -070045class ProcessThread;
wu@webrtc.org822fbd82013-08-15 23:38:54 +000046class ReceiveStatistics;
mflodmancfc8e3b2016-05-03 21:22:04 -070047class ReceiveStatisticsProxy;
mflodmanc0e58a32016-04-25 01:26:26 -070048class RtcpRttStats;
nisse38cc1d62017-02-13 05:59:46 -080049class RtpPacketReceived;
mflodmanc0e58a32016-04-25 01:26:26 -070050class Transport;
brandtrd55c3f62016-10-31 04:51:33 -070051class UlpfecReceiver;
Peter Boström0b250722016-04-22 18:23:15 +020052
nisseb1f2ff92017-06-09 04:01:55 -070053class RtpVideoStreamReceiver : public RtpData,
54 public RecoveredPacketReceiver,
nisse0f15f922017-06-21 01:05:22 -070055 public RtpPacketSinkInterface,
nisseb1f2ff92017-06-09 04:01:55 -070056 public VCMFrameTypeCallback,
57 public VCMPacketRequestCallback,
58 public video_coding::OnReceivedFrameCallback,
Tommi81de14f2018-03-25 22:19:25 +020059 public video_coding::OnCompleteFrameCallback {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +000060 public:
nisseb1f2ff92017-06-09 04:01:55 -070061 RtpVideoStreamReceiver(
philipelfd5a20f2016-11-15 00:57:57 -080062 Transport* transport,
63 RtcpRttStats* rtt_stats,
philipelfd5a20f2016-11-15 00:57:57 -080064 PacketRouter* packet_router,
philipelfd5a20f2016-11-15 00:57:57 -080065 const VideoReceiveStream::Config* config,
nisseca5706d2017-09-11 02:32:16 -070066 ReceiveStatistics* rtp_receive_statistics,
philipelfd5a20f2016-11-15 00:57:57 -080067 ReceiveStatisticsProxy* receive_stats_proxy,
68 ProcessThread* process_thread,
philipelfd5a20f2016-11-15 00:57:57 -080069 NackSender* nack_sender,
70 KeyFrameRequestSender* keyframe_request_sender,
philipel0a9f6de2018-02-28 11:29:47 +010071 video_coding::OnCompleteFrameCallback* complete_frame_callback);
Mirko Bonadei8fdcac32018-08-28 16:30:18 +020072 ~RtpVideoStreamReceiver() override;
niklase@google.com470e71d2011-07-07 08:21:25 +000073
Niels Möller2ff1f2a2018-08-09 16:16:34 +020074 void AddReceiveCodec(const VideoCodec& video_codec,
philipel022b54e2016-12-20 04:15:59 -080075 const std::map<std::string, std::string>& codec_params);
wu@webrtc.org822fbd82013-08-15 23:38:54 +000076
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +000077 void StartReceive();
78 void StopReceive();
niklase@google.com470e71d2011-07-07 08:21:25 +000079
Niels Möllerdf9e9ae2018-07-31 08:29:53 +020080 // Produces the transport-related timestamps; current_delay_ms is left unset.
81 absl::optional<Syncable::Info> GetSyncInfo() const;
82
Peter Boströmd1d66ba2016-02-08 14:07:14 +010083 bool DeliverRtcp(const uint8_t* rtcp_packet, size_t rtcp_packet_length);
niklase@google.com470e71d2011-07-07 08:21:25 +000084
philipeld4fac692017-09-04 07:03:46 -070085 void FrameContinuous(int64_t seq_num);
philipelfd5a20f2016-11-15 00:57:57 -080086
philipeld4fac692017-09-04 07:03:46 -070087 void FrameDecoded(int64_t seq_num);
philipelfd5a20f2016-11-15 00:57:57 -080088
mflodmandc7d0d22016-05-06 05:32:22 -070089 void SignalNetworkState(NetworkState state);
90
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +010091 // Returns number of different frames seen in the packet buffer.
92 int GetUniqueFramesSeen() const;
93
nisse0f15f922017-06-21 01:05:22 -070094 // Implements RtpPacketSinkInterface.
95 void OnRtpPacket(const RtpPacketReceived& packet) override;
nisse38cc1d62017-02-13 05:59:46 -080096
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +000097 // Implements RtpData.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000098 int32_t OnReceivedPayloadData(const uint8_t* payload_data,
Peter Boström02083222016-06-14 12:52:54 +020099 size_t payload_size,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000100 const WebRtcRTPHeader* rtp_header) override;
nisse30e89312017-05-29 08:16:37 -0700101 // Implements RecoveredPacketReceiver.
102 void OnRecoveredPacket(const uint8_t* packet, size_t packet_length) override;
niklase@google.com470e71d2011-07-07 08:21:25 +0000103
mflodmancfc8e3b2016-05-03 21:22:04 -0700104 // Implements VCMFrameTypeCallback.
105 int32_t RequestKeyFrame() override;
mflodmancfc8e3b2016-05-03 21:22:04 -0700106
brandtrf7c6d722016-12-08 08:25:47 -0800107 bool IsUlpfecEnabled() const;
mflodmandc7d0d22016-05-06 05:32:22 -0700108 bool IsRetransmissionsEnabled() const;
109 // Don't use, still experimental.
110 void RequestPacketRetransmit(const std::vector<uint16_t>& sequence_numbers);
111
mflodmancfc8e3b2016-05-03 21:22:04 -0700112 // Implements VCMPacketRequestCallback.
113 int32_t ResendPackets(const uint16_t* sequenceNumbers,
114 uint16_t length) override;
115
philipelfd5a20f2016-11-15 00:57:57 -0800116 // Implements OnReceivedFrameCallback.
117 void OnReceivedFrame(
118 std::unique_ptr<video_coding::RtpFrameObject> frame) override;
119
120 // Implements OnCompleteFrameCallback.
121 void OnCompleteFrame(
philipele7c891f2018-02-22 14:35:06 +0100122 std::unique_ptr<video_coding::EncodedFrame> frame) override;
philipelfd5a20f2016-11-15 00:57:57 -0800123
Tommi81de14f2018-03-25 22:19:25 +0200124 // Called by VideoReceiveStream when stats are updated.
125 void UpdateRtt(int64_t max_rtt_ms);
philipelfd5a20f2016-11-15 00:57:57 -0800126
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200127 absl::optional<int64_t> LastReceivedPacketMs() const;
128 absl::optional<int64_t> LastReceivedKeyframePacketMs() const;
philipel3184f8e2017-05-18 08:08:53 -0700129
eladalonc0d481a2017-08-02 07:39:07 -0700130 // RtpDemuxer only forwards a given RTP packet to one sink. However, some
131 // sinks, such as FlexFEC, might wish to be informed of all of the packets
132 // a given sink receives (or any set of sinks). They may do so by registering
133 // themselves as secondary sinks.
134 void AddSecondarySink(RtpPacketSinkInterface* sink);
135 void RemoveSecondarySink(const RtpPacketSinkInterface* sink);
136
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000137 private:
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200138 // Entry point doing non-stats work for a received packet. Called
139 // for the same packet both before and after RED decapsulation.
140 void ReceivePacket(const RtpPacketReceived& packet);
141 // Parses and handles RED headers.
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000142 // This function assumes that it's being called from only one thread.
nisse30e89312017-05-29 08:16:37 -0700143 void ParseAndHandleEncapsulatingHeader(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000144 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000145 const RTPHeader& header);
Niels Möllerbc010472018-03-23 13:22:29 +0100146 void NotifyReceiverOfEmptyPacket(uint16_t seq_num);
Danil Chapovalov64b17c22018-06-25 16:58:54 +0200147 bool IsPacketRetransmitted(const RTPHeader& header) const;
asapersson@webrtc.org0800db72015-01-15 07:40:20 +0000148 void UpdateHistograms();
brandtre6f98c72016-11-11 03:28:30 -0800149 bool IsRedEnabled() const;
philipel022b54e2016-12-20 04:15:59 -0800150 void InsertSpsPpsIntoTracker(uint8_t payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +0000151
Peter Boström4fa7eca2016-03-02 15:05:53 +0100152 Clock* const clock_;
Tommi733b5472016-06-10 17:58:01 +0200153 // Ownership of this object lies with VideoReceiveStream, which owns |this|.
154 const VideoReceiveStream::Config& config_;
mflodmanc0e58a32016-04-25 01:26:26 -0700155 PacketRouter* const packet_router_;
mflodmandc7d0d22016-05-06 05:32:22 -0700156 ProcessThread* const process_thread_;
Peter Boström4fa7eca2016-03-02 15:05:53 +0100157
158 RemoteNtpTimeEstimator ntp_estimator_;
Peter Boström4fa7eca2016-03-02 15:05:53 +0100159
Niels Möllerb0573bc2017-09-25 10:47:00 +0200160 RtpHeaderExtensionMap rtp_header_extensions_;
nisseca5706d2017-09-11 02:32:16 -0700161 ReceiveStatistics* const rtp_receive_statistics_;
brandtrd55c3f62016-10-31 04:51:33 -0700162 std::unique_ptr<UlpfecReceiver> ulpfec_receiver_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000163
eladalon8b073052017-08-25 00:49:08 -0700164 rtc::SequencedTaskChecker worker_task_checker_;
danilchapa37de392017-09-09 04:17:22 -0700165 bool receiving_ RTC_GUARDED_BY(worker_task_checker_);
danilchapa37de392017-09-09 04:17:22 -0700166 int64_t last_packet_log_ms_ RTC_GUARDED_BY(worker_task_checker_);
mflodmanc0e58a32016-04-25 01:26:26 -0700167
168 const std::unique_ptr<RtpRtcp> rtp_rtcp_;
philipelfd5a20f2016-11-15 00:57:57 -0800169
170 // Members for the new jitter buffer experiment.
philipelfd5a20f2016-11-15 00:57:57 -0800171 video_coding::OnCompleteFrameCallback* complete_frame_callback_;
172 KeyFrameRequestSender* keyframe_request_sender_;
philipelfd5a20f2016-11-15 00:57:57 -0800173 std::unique_ptr<NackModule> nack_module_;
174 rtc::scoped_refptr<video_coding::PacketBuffer> packet_buffer_;
175 std::unique_ptr<video_coding::RtpFrameReferenceFinder> reference_finder_;
176 rtc::CriticalSection last_seq_num_cs_;
philipeld4fac692017-09-04 07:03:46 -0700177 std::map<int64_t, uint16_t> last_seq_num_for_pic_id_
danilchapa37de392017-09-09 04:17:22 -0700178 RTC_GUARDED_BY(last_seq_num_cs_);
philipelfd5a20f2016-11-15 00:57:57 -0800179 video_coding::H264SpsPpsTracker tracker_;
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200180
181 absl::optional<uint32_t> last_received_rtp_timestamp_;
182 absl::optional<int64_t> last_received_rtp_system_time_ms_;
183 std::map<uint8_t, VideoCodecType> pt_codec_type_;
philipel022b54e2016-12-20 04:15:59 -0800184 // TODO(johan): Remove pt_codec_params_ once
185 // https://bugs.chromium.org/p/webrtc/issues/detail?id=6883 is resolved.
186 // Maps a payload type to a map of out-of-band supplied codec parameters.
187 std::map<uint8_t, std::map<std::string, std::string>> pt_codec_params_;
188 int16_t last_payload_type_ = -1;
philipel2c53b132017-05-16 08:06:30 -0700189
190 bool has_received_frame_;
eladalonc0d481a2017-08-02 07:39:07 -0700191
eladalon8b073052017-08-25 00:49:08 -0700192 std::vector<RtpPacketSinkInterface*> secondary_sinks_
danilchapa37de392017-09-09 04:17:22 -0700193 RTC_GUARDED_BY(worker_task_checker_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000194};
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000195
kjellander@webrtc.org0fcaf992015-11-26 15:24:52 +0100196} // namespace webrtc
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000197
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200198#endif // VIDEO_RTP_VIDEO_STREAM_RECEIVER_H_