blob: 994ef55e893f2a96a7086d239d272c9cbdebe87c [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
nisseb1f2ff92017-06-09 04:01:55 -070011#ifndef WEBRTC_VIDEO_RTP_VIDEO_STREAM_RECEIVER_H_
12#define WEBRTC_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
nisse0f15f922017-06-21 01:05:22 -070020#include "webrtc/call/rtp_packet_sink_interface.h"
aleloi440b6d92017-08-22 05:43:23 -070021#include "webrtc/call/video_receive_stream.h"
philipelfd5a20f2016-11-15 00:57:57 -080022#include "webrtc/modules/include/module_common_types.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010023#include "webrtc/modules/rtp_rtcp/include/receive_statistics.h"
Peter Boström4fa7eca2016-03-02 15:05:53 +010024#include "webrtc/modules/rtp_rtcp/include/remote_ntp_time_estimator.h"
25#include "webrtc/modules/rtp_rtcp/include/rtp_payload_registry.h"
mflodmanc0e58a32016-04-25 01:26:26 -070026#include "webrtc/modules/rtp_rtcp/include/rtp_rtcp.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010027#include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h"
philipelfd5a20f2016-11-15 00:57:57 -080028#include "webrtc/modules/video_coding/h264_sps_pps_tracker.h"
mflodmancfc8e3b2016-05-03 21:22:04 -070029#include "webrtc/modules/video_coding/include/video_coding_defines.h"
philipelfd5a20f2016-11-15 00:57:57 -080030#include "webrtc/modules/video_coding/packet_buffer.h"
31#include "webrtc/modules/video_coding/rtp_frame_reference_finder.h"
32#include "webrtc/modules/video_coding/sequence_number_util.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020033#include "webrtc/rtc_base/constructormagic.h"
34#include "webrtc/rtc_base/criticalsection.h"
eladalon8b073052017-08-25 00:49:08 -070035#include "webrtc/rtc_base/sequenced_task_checker.h"
pwestin@webrtc.org82dcc9f2013-04-02 20:37:14 +000036#include "webrtc/typedefs.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000037
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +000038namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000039
philipelfd5a20f2016-11-15 00:57:57 -080040class NackModule;
mflodmanc0e58a32016-04-25 01:26:26 -070041class PacedSender;
42class PacketRouter;
mflodmandc7d0d22016-05-06 05:32:22 -070043class ProcessThread;
wu@webrtc.org822fbd82013-08-15 23:38:54 +000044class ReceiveStatistics;
mflodmancfc8e3b2016-05-03 21:22:04 -070045class ReceiveStatisticsProxy;
philipelfd5a20f2016-11-15 00:57:57 -080046class RemoteNtpTimeEstimator;
mflodmanc0e58a32016-04-25 01:26:26 -070047class RtcpRttStats;
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000048class RtpHeaderParser;
nisse38cc1d62017-02-13 05:59:46 -080049class RtpPacketReceived;
wu@webrtc.org822fbd82013-08-15 23:38:54 +000050class RTPPayloadRegistry;
51class RtpReceiver;
mflodmanc0e58a32016-04-25 01:26:26 -070052class Transport;
brandtrd55c3f62016-10-31 04:51:33 -070053class UlpfecReceiver;
philipelfd5a20f2016-11-15 00:57:57 -080054class VCMTiming;
Peter Boström0b250722016-04-22 18:23:15 +020055
56namespace vcm {
57class VideoReceiver;
58} // namespace vcm
niklase@google.com470e71d2011-07-07 08:21:25 +000059
nisseb1f2ff92017-06-09 04:01:55 -070060class RtpVideoStreamReceiver : public RtpData,
61 public RecoveredPacketReceiver,
62 public RtpFeedback,
nisse0f15f922017-06-21 01:05:22 -070063 public RtpPacketSinkInterface,
nisseb1f2ff92017-06-09 04:01:55 -070064 public VCMFrameTypeCallback,
65 public VCMPacketRequestCallback,
66 public video_coding::OnReceivedFrameCallback,
67 public video_coding::OnCompleteFrameCallback,
68 public CallStatsObserver {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +000069 public:
nisseb1f2ff92017-06-09 04:01:55 -070070 RtpVideoStreamReceiver(
philipelfd5a20f2016-11-15 00:57:57 -080071 Transport* transport,
72 RtcpRttStats* rtt_stats,
philipelfd5a20f2016-11-15 00:57:57 -080073 PacketRouter* packet_router,
philipelfd5a20f2016-11-15 00:57:57 -080074 const VideoReceiveStream::Config* config,
nisseca5706d2017-09-11 02:32:16 -070075 ReceiveStatistics* rtp_receive_statistics,
philipelfd5a20f2016-11-15 00:57:57 -080076 ReceiveStatisticsProxy* receive_stats_proxy,
77 ProcessThread* process_thread,
philipelfd5a20f2016-11-15 00:57:57 -080078 NackSender* nack_sender,
79 KeyFrameRequestSender* keyframe_request_sender,
80 video_coding::OnCompleteFrameCallback* complete_frame_callback,
81 VCMTiming* timing);
nisseb1f2ff92017-06-09 04:01:55 -070082 ~RtpVideoStreamReceiver();
niklase@google.com470e71d2011-07-07 08:21:25 +000083
philipel022b54e2016-12-20 04:15:59 -080084 bool AddReceiveCodec(const VideoCodec& video_codec,
85 const std::map<std::string, std::string>& codec_params);
wu@webrtc.org822fbd82013-08-15 23:38:54 +000086 uint32_t GetRemoteSsrc() const;
87 int GetCsrcs(uint32_t* csrcs) const;
88
wu@webrtc.org822fbd82013-08-15 23:38:54 +000089 RtpReceiver* GetRtpReceiver() const;
mflodmanc0e58a32016-04-25 01:26:26 -070090 RtpRtcp* rtp_rtcp() const { return rtp_rtcp_.get(); }
wu@webrtc.org822fbd82013-08-15 23:38:54 +000091
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +000092 void StartReceive();
93 void StopReceive();
niklase@google.com470e71d2011-07-07 08:21:25 +000094
Peter Boströmd1d66ba2016-02-08 14:07:14 +010095 bool DeliverRtcp(const uint8_t* rtcp_packet, size_t rtcp_packet_length);
niklase@google.com470e71d2011-07-07 08:21:25 +000096
philipeld4fac692017-09-04 07:03:46 -070097 void FrameContinuous(int64_t seq_num);
philipelfd5a20f2016-11-15 00:57:57 -080098
philipeld4fac692017-09-04 07:03:46 -070099 void FrameDecoded(int64_t seq_num);
philipelfd5a20f2016-11-15 00:57:57 -0800100
mflodmandc7d0d22016-05-06 05:32:22 -0700101 void SignalNetworkState(NetworkState state);
102
nisse0f15f922017-06-21 01:05:22 -0700103 // Implements RtpPacketSinkInterface.
104 void OnRtpPacket(const RtpPacketReceived& packet) override;
nisse38cc1d62017-02-13 05:59:46 -0800105
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000106 // Implements RtpData.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000107 int32_t OnReceivedPayloadData(const uint8_t* payload_data,
Peter Boström02083222016-06-14 12:52:54 +0200108 size_t payload_size,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000109 const WebRtcRTPHeader* rtp_header) override;
nisse30e89312017-05-29 08:16:37 -0700110 // Implements RecoveredPacketReceiver.
111 void OnRecoveredPacket(const uint8_t* packet, size_t packet_length) override;
niklase@google.com470e71d2011-07-07 08:21:25 +0000112
mflodmanfa666592016-04-28 23:15:33 -0700113 // Implements RtpFeedback.
Sergey Ulanovec4f0682016-07-28 15:19:10 -0700114 int32_t OnInitializeDecoder(int8_t payload_type,
mflodmanfa666592016-04-28 23:15:33 -0700115 const char payload_name[RTP_PAYLOAD_NAME_SIZE],
Sergey Ulanovec4f0682016-07-28 15:19:10 -0700116 int frequency,
117 size_t channels,
118 uint32_t rate) override;
stefanb4ab3812017-06-09 06:12:11 -0700119 void OnIncomingSSRCChanged(uint32_t ssrc) override {}
Sergey Ulanovec4f0682016-07-28 15:19:10 -0700120 void OnIncomingCSRCChanged(uint32_t CSRC, bool added) override {}
mflodmanfa666592016-04-28 23:15:33 -0700121
mflodmancfc8e3b2016-05-03 21:22:04 -0700122 // Implements VCMFrameTypeCallback.
123 int32_t RequestKeyFrame() override;
mflodmancfc8e3b2016-05-03 21:22:04 -0700124
brandtrf7c6d722016-12-08 08:25:47 -0800125 bool IsUlpfecEnabled() const;
mflodmandc7d0d22016-05-06 05:32:22 -0700126 bool IsRetransmissionsEnabled() const;
127 // Don't use, still experimental.
128 void RequestPacketRetransmit(const std::vector<uint16_t>& sequence_numbers);
129
mflodmancfc8e3b2016-05-03 21:22:04 -0700130 // Implements VCMPacketRequestCallback.
131 int32_t ResendPackets(const uint16_t* sequenceNumbers,
132 uint16_t length) override;
133
philipelfd5a20f2016-11-15 00:57:57 -0800134 // Implements OnReceivedFrameCallback.
135 void OnReceivedFrame(
136 std::unique_ptr<video_coding::RtpFrameObject> frame) override;
137
138 // Implements OnCompleteFrameCallback.
139 void OnCompleteFrame(
140 std::unique_ptr<video_coding::FrameObject> frame) override;
141
142 void OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) override;
143
philipel3184f8e2017-05-18 08:08:53 -0700144 rtc::Optional<int64_t> LastReceivedPacketMs() const;
145 rtc::Optional<int64_t> LastReceivedKeyframePacketMs() const;
146
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
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000154 private:
johanbfb11b22017-01-25 07:37:27 -0800155 bool AddReceiveCodec(const VideoCodec& video_codec);
nisse30e89312017-05-29 08:16:37 -0700156 void ReceivePacket(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000157 size_t packet_length,
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000158 const RTPHeader& header,
159 bool in_order);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000160 // Parses and handles for instance RTX and RED headers.
161 // This function assumes that it's being called from only one thread.
nisse30e89312017-05-29 08:16:37 -0700162 void ParseAndHandleEncapsulatingHeader(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000163 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000164 const RTPHeader& header);
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000165 void NotifyReceiverOfFecPacket(const RTPHeader& header);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000166 bool IsPacketInOrder(const RTPHeader& header) const;
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000167 bool IsPacketRetransmitted(const RTPHeader& header, bool in_order) const;
asapersson@webrtc.org0800db72015-01-15 07:40:20 +0000168 void UpdateHistograms();
mflodmandc7d0d22016-05-06 05:32:22 -0700169 void EnableReceiveRtpHeaderExtension(const std::string& extension, int id);
brandtre6f98c72016-11-11 03:28:30 -0800170 bool IsRedEnabled() const;
philipel022b54e2016-12-20 04:15:59 -0800171 void InsertSpsPpsIntoTracker(uint8_t payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +0000172
Peter Boström4fa7eca2016-03-02 15:05:53 +0100173 Clock* const clock_;
Tommi733b5472016-06-10 17:58:01 +0200174 // Ownership of this object lies with VideoReceiveStream, which owns |this|.
175 const VideoReceiveStream::Config& config_;
mflodmanc0e58a32016-04-25 01:26:26 -0700176 PacketRouter* const packet_router_;
mflodmandc7d0d22016-05-06 05:32:22 -0700177 ProcessThread* const process_thread_;
Peter Boström4fa7eca2016-03-02 15:05:53 +0100178
179 RemoteNtpTimeEstimator ntp_estimator_;
180 RTPPayloadRegistry rtp_payload_registry_;
181
182 const std::unique_ptr<RtpHeaderParser> rtp_header_parser_;
183 const std::unique_ptr<RtpReceiver> rtp_receiver_;
nisseca5706d2017-09-11 02:32:16 -0700184 ReceiveStatistics* const rtp_receive_statistics_;
brandtrd55c3f62016-10-31 04:51:33 -0700185 std::unique_ptr<UlpfecReceiver> ulpfec_receiver_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000186
eladalon8b073052017-08-25 00:49:08 -0700187 rtc::SequencedTaskChecker worker_task_checker_;
danilchapa37de392017-09-09 04:17:22 -0700188 bool receiving_ RTC_GUARDED_BY(worker_task_checker_);
danilchapa37de392017-09-09 04:17:22 -0700189 int64_t last_packet_log_ms_ RTC_GUARDED_BY(worker_task_checker_);
mflodmanc0e58a32016-04-25 01:26:26 -0700190
191 const std::unique_ptr<RtpRtcp> rtp_rtcp_;
philipelfd5a20f2016-11-15 00:57:57 -0800192
193 // Members for the new jitter buffer experiment.
philipelfd5a20f2016-11-15 00:57:57 -0800194 video_coding::OnCompleteFrameCallback* complete_frame_callback_;
195 KeyFrameRequestSender* keyframe_request_sender_;
196 VCMTiming* timing_;
197 std::unique_ptr<NackModule> nack_module_;
198 rtc::scoped_refptr<video_coding::PacketBuffer> packet_buffer_;
199 std::unique_ptr<video_coding::RtpFrameReferenceFinder> reference_finder_;
200 rtc::CriticalSection last_seq_num_cs_;
philipeld4fac692017-09-04 07:03:46 -0700201 std::map<int64_t, uint16_t> last_seq_num_for_pic_id_
danilchapa37de392017-09-09 04:17:22 -0700202 RTC_GUARDED_BY(last_seq_num_cs_);
philipelfd5a20f2016-11-15 00:57:57 -0800203 video_coding::H264SpsPpsTracker tracker_;
philipel022b54e2016-12-20 04:15:59 -0800204 // TODO(johan): Remove pt_codec_params_ once
205 // https://bugs.chromium.org/p/webrtc/issues/detail?id=6883 is resolved.
206 // Maps a payload type to a map of out-of-band supplied codec parameters.
207 std::map<uint8_t, std::map<std::string, std::string>> pt_codec_params_;
208 int16_t last_payload_type_ = -1;
philipel2c53b132017-05-16 08:06:30 -0700209
210 bool has_received_frame_;
eladalonc0d481a2017-08-02 07:39:07 -0700211
eladalon8b073052017-08-25 00:49:08 -0700212 std::vector<RtpPacketSinkInterface*> secondary_sinks_
danilchapa37de392017-09-09 04:17:22 -0700213 RTC_GUARDED_BY(worker_task_checker_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000214};
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000215
kjellander@webrtc.org0fcaf992015-11-26 15:24:52 +0100216} // namespace webrtc
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000217
nisseb1f2ff92017-06-09 04:01:55 -0700218#endif // WEBRTC_VIDEO_RTP_VIDEO_STREAM_RECEIVER_H_