blob: 14553e532b182ef88db4471bf8d55194b7847258 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "call/rtp_packet_sink_interface.h"
21#include "call/video_receive_stream.h"
22#include "modules/include/module_common_types.h"
23#include "modules/rtp_rtcp/include/receive_statistics.h"
24#include "modules/rtp_rtcp/include/remote_ntp_time_estimator.h"
Niels Möllerb0573bc2017-09-25 10:47:00 +020025#include "modules/rtp_rtcp/include/rtp_header_extension_map.h"
Bjorn Tereliusa194e582017-10-25 13:07:09 +020026#include "modules/rtp_rtcp/include/rtp_payload_registry.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "modules/rtp_rtcp/include/rtp_rtcp.h"
28#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
29#include "modules/video_coding/h264_sps_pps_tracker.h"
30#include "modules/video_coding/include/video_coding_defines.h"
31#include "modules/video_coding/packet_buffer.h"
32#include "modules/video_coding/rtp_frame_reference_finder.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020033#include "rtc_base/constructormagic.h"
34#include "rtc_base/criticalsection.h"
Bjorn Tereliusa194e582017-10-25 13:07:09 +020035#include "rtc_base/numerics/sequence_number_util.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020036#include "rtc_base/sequenced_task_checker.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020037#include "typedefs.h" // NOLINT(build/include)
niklase@google.com470e71d2011-07-07 08:21:25 +000038
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +000039namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000040
philipelfd5a20f2016-11-15 00:57:57 -080041class NackModule;
mflodmanc0e58a32016-04-25 01:26:26 -070042class PacedSender;
43class PacketRouter;
mflodmandc7d0d22016-05-06 05:32:22 -070044class ProcessThread;
wu@webrtc.org822fbd82013-08-15 23:38:54 +000045class ReceiveStatistics;
mflodmancfc8e3b2016-05-03 21:22:04 -070046class ReceiveStatisticsProxy;
philipelfd5a20f2016-11-15 00:57:57 -080047class RemoteNtpTimeEstimator;
mflodmanc0e58a32016-04-25 01:26:26 -070048class RtcpRttStats;
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000049class RtpHeaderParser;
nisse38cc1d62017-02-13 05:59:46 -080050class RtpPacketReceived;
wu@webrtc.org822fbd82013-08-15 23:38:54 +000051class RTPPayloadRegistry;
52class RtpReceiver;
mflodmanc0e58a32016-04-25 01:26:26 -070053class Transport;
brandtrd55c3f62016-10-31 04:51:33 -070054class UlpfecReceiver;
philipelfd5a20f2016-11-15 00:57:57 -080055class VCMTiming;
Peter Boström0b250722016-04-22 18:23:15 +020056
57namespace vcm {
58class VideoReceiver;
59} // namespace vcm
niklase@google.com470e71d2011-07-07 08:21:25 +000060
nisseb1f2ff92017-06-09 04:01:55 -070061class RtpVideoStreamReceiver : public RtpData,
62 public RecoveredPacketReceiver,
63 public RtpFeedback,
nisse0f15f922017-06-21 01:05:22 -070064 public RtpPacketSinkInterface,
nisseb1f2ff92017-06-09 04:01:55 -070065 public VCMFrameTypeCallback,
66 public VCMPacketRequestCallback,
67 public video_coding::OnReceivedFrameCallback,
68 public video_coding::OnCompleteFrameCallback,
69 public CallStatsObserver {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +000070 public:
nisseb1f2ff92017-06-09 04:01:55 -070071 RtpVideoStreamReceiver(
philipelfd5a20f2016-11-15 00:57:57 -080072 Transport* transport,
73 RtcpRttStats* rtt_stats,
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,
80 KeyFrameRequestSender* keyframe_request_sender,
81 video_coding::OnCompleteFrameCallback* complete_frame_callback,
82 VCMTiming* timing);
nisseb1f2ff92017-06-09 04:01:55 -070083 ~RtpVideoStreamReceiver();
niklase@google.com470e71d2011-07-07 08:21:25 +000084
philipel022b54e2016-12-20 04:15:59 -080085 bool AddReceiveCodec(const VideoCodec& video_codec,
86 const std::map<std::string, std::string>& codec_params);
wu@webrtc.org822fbd82013-08-15 23:38:54 +000087 uint32_t GetRemoteSsrc() const;
88 int GetCsrcs(uint32_t* csrcs) const;
89
wu@webrtc.org822fbd82013-08-15 23:38:54 +000090 RtpReceiver* GetRtpReceiver() const;
mflodmanc0e58a32016-04-25 01:26:26 -070091 RtpRtcp* rtp_rtcp() const { return rtp_rtcp_.get(); }
wu@webrtc.org822fbd82013-08-15 23:38:54 +000092
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +000093 void StartReceive();
94 void StopReceive();
niklase@google.com470e71d2011-07-07 08:21:25 +000095
Peter Boströmd1d66ba2016-02-08 14:07:14 +010096 bool DeliverRtcp(const uint8_t* rtcp_packet, size_t rtcp_packet_length);
niklase@google.com470e71d2011-07-07 08:21:25 +000097
philipeld4fac692017-09-04 07:03:46 -070098 void FrameContinuous(int64_t seq_num);
philipelfd5a20f2016-11-15 00:57:57 -080099
philipeld4fac692017-09-04 07:03:46 -0700100 void FrameDecoded(int64_t seq_num);
philipelfd5a20f2016-11-15 00:57:57 -0800101
mflodmandc7d0d22016-05-06 05:32:22 -0700102 void SignalNetworkState(NetworkState state);
103
nisse0f15f922017-06-21 01:05:22 -0700104 // Implements RtpPacketSinkInterface.
105 void OnRtpPacket(const RtpPacketReceived& packet) override;
nisse38cc1d62017-02-13 05:59:46 -0800106
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000107 // Implements RtpData.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000108 int32_t OnReceivedPayloadData(const uint8_t* payload_data,
Peter Boström02083222016-06-14 12:52:54 +0200109 size_t payload_size,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000110 const WebRtcRTPHeader* rtp_header) override;
nisse30e89312017-05-29 08:16:37 -0700111 // Implements RecoveredPacketReceiver.
112 void OnRecoveredPacket(const uint8_t* packet, size_t packet_length) override;
niklase@google.com470e71d2011-07-07 08:21:25 +0000113
mflodmanfa666592016-04-28 23:15:33 -0700114 // Implements RtpFeedback.
Karl Wibergc62f6c72017-10-04 12:38:53 +0200115 int32_t OnInitializeDecoder(int payload_type,
116 const SdpAudioFormat& audio_format,
Sergey Ulanovec4f0682016-07-28 15:19:10 -0700117 uint32_t rate) override;
stefanb4ab3812017-06-09 06:12:11 -0700118 void OnIncomingSSRCChanged(uint32_t ssrc) override {}
Sergey Ulanovec4f0682016-07-28 15:19:10 -0700119 void OnIncomingCSRCChanged(uint32_t CSRC, bool added) override {}
mflodmanfa666592016-04-28 23:15:33 -0700120
mflodmancfc8e3b2016-05-03 21:22:04 -0700121 // Implements VCMFrameTypeCallback.
122 int32_t RequestKeyFrame() override;
mflodmancfc8e3b2016-05-03 21:22:04 -0700123
brandtrf7c6d722016-12-08 08:25:47 -0800124 bool IsUlpfecEnabled() const;
mflodmandc7d0d22016-05-06 05:32:22 -0700125 bool IsRetransmissionsEnabled() const;
126 // Don't use, still experimental.
127 void RequestPacketRetransmit(const std::vector<uint16_t>& sequence_numbers);
128
mflodmancfc8e3b2016-05-03 21:22:04 -0700129 // Implements VCMPacketRequestCallback.
130 int32_t ResendPackets(const uint16_t* sequenceNumbers,
131 uint16_t length) override;
132
philipelfd5a20f2016-11-15 00:57:57 -0800133 // Implements OnReceivedFrameCallback.
134 void OnReceivedFrame(
135 std::unique_ptr<video_coding::RtpFrameObject> frame) override;
136
137 // Implements OnCompleteFrameCallback.
138 void OnCompleteFrame(
139 std::unique_ptr<video_coding::FrameObject> frame) override;
140
141 void OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) override;
142
philipel3184f8e2017-05-18 08:08:53 -0700143 rtc::Optional<int64_t> LastReceivedPacketMs() const;
144 rtc::Optional<int64_t> LastReceivedKeyframePacketMs() const;
145
eladalonc0d481a2017-08-02 07:39:07 -0700146 // RtpDemuxer only forwards a given RTP packet to one sink. However, some
147 // sinks, such as FlexFEC, might wish to be informed of all of the packets
148 // a given sink receives (or any set of sinks). They may do so by registering
149 // themselves as secondary sinks.
150 void AddSecondarySink(RtpPacketSinkInterface* sink);
151 void RemoveSecondarySink(const RtpPacketSinkInterface* sink);
152
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000153 private:
johanbfb11b22017-01-25 07:37:27 -0800154 bool AddReceiveCodec(const VideoCodec& video_codec);
nisse30e89312017-05-29 08:16:37 -0700155 void ReceivePacket(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000156 size_t packet_length,
Niels Möller22ec9522017-10-05 08:39:15 +0200157 const RTPHeader& header);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000158 // Parses and handles for instance RTX and RED headers.
159 // This function assumes that it's being called from only one thread.
nisse30e89312017-05-29 08:16:37 -0700160 void ParseAndHandleEncapsulatingHeader(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000161 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000162 const RTPHeader& header);
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000163 void NotifyReceiverOfFecPacket(const RTPHeader& header);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000164 bool IsPacketInOrder(const RTPHeader& header) const;
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000165 bool IsPacketRetransmitted(const RTPHeader& header, bool in_order) const;
asapersson@webrtc.org0800db72015-01-15 07:40:20 +0000166 void UpdateHistograms();
brandtre6f98c72016-11-11 03:28:30 -0800167 bool IsRedEnabled() const;
philipel022b54e2016-12-20 04:15:59 -0800168 void InsertSpsPpsIntoTracker(uint8_t payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +0000169
Peter Boström4fa7eca2016-03-02 15:05:53 +0100170 Clock* const clock_;
Tommi733b5472016-06-10 17:58:01 +0200171 // Ownership of this object lies with VideoReceiveStream, which owns |this|.
172 const VideoReceiveStream::Config& config_;
mflodmanc0e58a32016-04-25 01:26:26 -0700173 PacketRouter* const packet_router_;
mflodmandc7d0d22016-05-06 05:32:22 -0700174 ProcessThread* const process_thread_;
Peter Boström4fa7eca2016-03-02 15:05:53 +0100175
176 RemoteNtpTimeEstimator ntp_estimator_;
177 RTPPayloadRegistry rtp_payload_registry_;
178
Niels Möllerb0573bc2017-09-25 10:47:00 +0200179 RtpHeaderExtensionMap rtp_header_extensions_;
Peter Boström4fa7eca2016-03-02 15:05:53 +0100180 const std::unique_ptr<RtpReceiver> rtp_receiver_;
nisseca5706d2017-09-11 02:32:16 -0700181 ReceiveStatistics* const rtp_receive_statistics_;
brandtrd55c3f62016-10-31 04:51:33 -0700182 std::unique_ptr<UlpfecReceiver> ulpfec_receiver_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000183
eladalon8b073052017-08-25 00:49:08 -0700184 rtc::SequencedTaskChecker worker_task_checker_;
danilchapa37de392017-09-09 04:17:22 -0700185 bool receiving_ RTC_GUARDED_BY(worker_task_checker_);
danilchapa37de392017-09-09 04:17:22 -0700186 int64_t last_packet_log_ms_ RTC_GUARDED_BY(worker_task_checker_);
mflodmanc0e58a32016-04-25 01:26:26 -0700187
188 const std::unique_ptr<RtpRtcp> rtp_rtcp_;
philipelfd5a20f2016-11-15 00:57:57 -0800189
190 // Members for the new jitter buffer experiment.
philipelfd5a20f2016-11-15 00:57:57 -0800191 video_coding::OnCompleteFrameCallback* complete_frame_callback_;
192 KeyFrameRequestSender* keyframe_request_sender_;
193 VCMTiming* timing_;
194 std::unique_ptr<NackModule> nack_module_;
195 rtc::scoped_refptr<video_coding::PacketBuffer> packet_buffer_;
196 std::unique_ptr<video_coding::RtpFrameReferenceFinder> reference_finder_;
197 rtc::CriticalSection last_seq_num_cs_;
philipeld4fac692017-09-04 07:03:46 -0700198 std::map<int64_t, uint16_t> last_seq_num_for_pic_id_
danilchapa37de392017-09-09 04:17:22 -0700199 RTC_GUARDED_BY(last_seq_num_cs_);
philipelfd5a20f2016-11-15 00:57:57 -0800200 video_coding::H264SpsPpsTracker tracker_;
philipel022b54e2016-12-20 04:15:59 -0800201 // TODO(johan): Remove pt_codec_params_ once
202 // https://bugs.chromium.org/p/webrtc/issues/detail?id=6883 is resolved.
203 // Maps a payload type to a map of out-of-band supplied codec parameters.
204 std::map<uint8_t, std::map<std::string, std::string>> pt_codec_params_;
205 int16_t last_payload_type_ = -1;
philipel2c53b132017-05-16 08:06:30 -0700206
207 bool has_received_frame_;
eladalonc0d481a2017-08-02 07:39:07 -0700208
eladalon8b073052017-08-25 00:49:08 -0700209 std::vector<RtpPacketSinkInterface*> secondary_sinks_
danilchapa37de392017-09-09 04:17:22 -0700210 RTC_GUARDED_BY(worker_task_checker_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000211};
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000212
kjellander@webrtc.org0fcaf992015-11-26 15:24:52 +0100213} // namespace webrtc
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000214
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200215#endif // VIDEO_RTP_VIDEO_STREAM_RECEIVER_H_