blob: b1e1db4b14475c6d591afc4d0c4ac22f3b88cd36 [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
mflodmanfa666592016-04-28 23:15:33 -070011#ifndef WEBRTC_VIDEO_RTP_STREAM_RECEIVER_H_
12#define WEBRTC_VIDEO_RTP_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
kwiberg4485ffb2016-04-26 08:14:39 -070020#include "webrtc/base/constructormagic.h"
Tommi97888bd2016-01-21 23:24:59 +010021#include "webrtc/base/criticalsection.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"
pwestin@webrtc.org82dcc9f2013-04-02 20:37:14 +000033#include "webrtc/typedefs.h"
mflodmancfc8e3b2016-05-03 21:22:04 -070034#include "webrtc/video_receive_stream.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000035
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +000036namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000037
philipelfd5a20f2016-11-15 00:57:57 -080038class NackModule;
mflodmanc0e58a32016-04-25 01:26:26 -070039class PacedSender;
40class PacketRouter;
mflodmandc7d0d22016-05-06 05:32:22 -070041class ProcessThread;
wu@webrtc.org822fbd82013-08-15 23:38:54 +000042class ReceiveStatistics;
mflodmancfc8e3b2016-05-03 21:22:04 -070043class ReceiveStatisticsProxy;
stefan@webrtc.org976a7e62012-09-21 13:20:21 +000044class RemoteBitrateEstimator;
philipelfd5a20f2016-11-15 00:57:57 -080045class RemoteNtpTimeEstimator;
mflodmanc0e58a32016-04-25 01:26:26 -070046class RtcpRttStats;
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000047class RtpHeaderParser;
wu@webrtc.org822fbd82013-08-15 23:38:54 +000048class RTPPayloadRegistry;
49class RtpReceiver;
mflodmanc0e58a32016-04-25 01:26:26 -070050class Transport;
brandtrd55c3f62016-10-31 04:51:33 -070051class UlpfecReceiver;
philipelfd5a20f2016-11-15 00:57:57 -080052class VCMTiming;
mflodmandc7d0d22016-05-06 05:32:22 -070053class VieRemb;
Peter Boström0b250722016-04-22 18:23:15 +020054
55namespace vcm {
56class VideoReceiver;
57} // namespace vcm
niklase@google.com470e71d2011-07-07 08:21:25 +000058
philipelfd5a20f2016-11-15 00:57:57 -080059class RtpStreamReceiver : public RtpData,
60 public RtpFeedback,
mflodmancfc8e3b2016-05-03 21:22:04 -070061 public VCMFrameTypeCallback,
philipelfd5a20f2016-11-15 00:57:57 -080062 public VCMPacketRequestCallback,
63 public video_coding::OnReceivedFrameCallback,
64 public video_coding::OnCompleteFrameCallback,
65 public CallStatsObserver {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +000066 public:
philipelfd5a20f2016-11-15 00:57:57 -080067 RtpStreamReceiver(
68 vcm::VideoReceiver* video_receiver,
69 RemoteBitrateEstimator* remote_bitrate_estimator,
70 Transport* transport,
71 RtcpRttStats* rtt_stats,
72 PacedSender* paced_sender,
73 PacketRouter* packet_router,
74 VieRemb* remb,
75 const VideoReceiveStream::Config* config,
76 ReceiveStatisticsProxy* receive_stats_proxy,
77 ProcessThread* process_thread,
78 RateLimiter* retransmission_rate_limiter,
79 NackSender* nack_sender,
80 KeyFrameRequestSender* keyframe_request_sender,
81 video_coding::OnCompleteFrameCallback* complete_frame_callback,
82 VCMTiming* timing);
mflodmanfa666592016-04-28 23:15:33 -070083 ~RtpStreamReceiver();
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);
87
johan07e276c2016-12-13 02:23:03 -080088 bool AddReceiveCodec(const VideoCodec& video_codec);
wu@webrtc.org822fbd82013-08-15 23:38:54 +000089
wu@webrtc.org822fbd82013-08-15 23:38:54 +000090 uint32_t GetRemoteSsrc() const;
91 int GetCsrcs(uint32_t* csrcs) const;
92
wu@webrtc.org822fbd82013-08-15 23:38:54 +000093 RtpReceiver* GetRtpReceiver() const;
mflodmanc0e58a32016-04-25 01:26:26 -070094 RtpRtcp* rtp_rtcp() const { return rtp_rtcp_.get(); }
wu@webrtc.org822fbd82013-08-15 23:38:54 +000095
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +000096 void StartReceive();
97 void StopReceive();
niklase@google.com470e71d2011-07-07 08:21:25 +000098
Peter Boströmd1d66ba2016-02-08 14:07:14 +010099 bool DeliverRtp(const uint8_t* rtp_packet,
100 size_t rtp_packet_length,
101 const PacketTime& packet_time);
102 bool DeliverRtcp(const uint8_t* rtcp_packet, size_t rtcp_packet_length);
niklase@google.com470e71d2011-07-07 08:21:25 +0000103
philipelfd5a20f2016-11-15 00:57:57 -0800104 void FrameContinuous(uint16_t seq_num);
105
106 void FrameDecoded(uint16_t seq_num);
107
mflodmandc7d0d22016-05-06 05:32:22 -0700108 void SignalNetworkState(NetworkState state);
109
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000110 // Implements RtpData.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000111 int32_t OnReceivedPayloadData(const uint8_t* payload_data,
Peter Boström02083222016-06-14 12:52:54 +0200112 size_t payload_size,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000113 const WebRtcRTPHeader* rtp_header) override;
114 bool OnRecoveredPacket(const uint8_t* packet, size_t packet_length) override;
niklase@google.com470e71d2011-07-07 08:21:25 +0000115
mflodmanfa666592016-04-28 23:15:33 -0700116 // Implements RtpFeedback.
Sergey Ulanovec4f0682016-07-28 15:19:10 -0700117 int32_t OnInitializeDecoder(int8_t payload_type,
mflodmanfa666592016-04-28 23:15:33 -0700118 const char payload_name[RTP_PAYLOAD_NAME_SIZE],
Sergey Ulanovec4f0682016-07-28 15:19:10 -0700119 int frequency,
120 size_t channels,
121 uint32_t rate) override;
122 void OnIncomingSSRCChanged(uint32_t ssrc) override;
123 void OnIncomingCSRCChanged(uint32_t CSRC, bool added) override {}
mflodmanfa666592016-04-28 23:15:33 -0700124
mflodmancfc8e3b2016-05-03 21:22:04 -0700125 // Implements VCMFrameTypeCallback.
126 int32_t RequestKeyFrame() override;
127 int32_t SliceLossIndicationRequest(const uint64_t picture_id) override;
128
brandtrf7c6d722016-12-08 08:25:47 -0800129 bool IsUlpfecEnabled() const;
mflodmandc7d0d22016-05-06 05:32:22 -0700130 bool IsRetransmissionsEnabled() const;
131 // Don't use, still experimental.
132 void RequestPacketRetransmit(const std::vector<uint16_t>& sequence_numbers);
133
mflodmancfc8e3b2016-05-03 21:22:04 -0700134 // Implements VCMPacketRequestCallback.
135 int32_t ResendPackets(const uint16_t* sequenceNumbers,
136 uint16_t length) override;
137
philipelfd5a20f2016-11-15 00:57:57 -0800138 // Implements OnReceivedFrameCallback.
139 void OnReceivedFrame(
140 std::unique_ptr<video_coding::RtpFrameObject> frame) override;
141
142 // Implements OnCompleteFrameCallback.
143 void OnCompleteFrame(
144 std::unique_ptr<video_coding::FrameObject> frame) override;
145
146 void OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) override;
147
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000148 private:
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000149 bool ReceivePacket(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000150 size_t packet_length,
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000151 const RTPHeader& header,
152 bool in_order);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000153 // Parses and handles for instance RTX and RED headers.
154 // This function assumes that it's being called from only one thread.
155 bool ParseAndHandleEncapsulatingHeader(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000156 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000157 const RTPHeader& header);
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000158 void NotifyReceiverOfFecPacket(const RTPHeader& header);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000159 bool IsPacketInOrder(const RTPHeader& header) const;
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000160 bool IsPacketRetransmitted(const RTPHeader& header, bool in_order) const;
asapersson@webrtc.org0800db72015-01-15 07:40:20 +0000161 void UpdateHistograms();
mflodmandc7d0d22016-05-06 05:32:22 -0700162 void EnableReceiveRtpHeaderExtension(const std::string& extension, int id);
brandtre6f98c72016-11-11 03:28:30 -0800163 bool IsRedEnabled() const;
philipel022b54e2016-12-20 04:15:59 -0800164 void InsertSpsPpsIntoTracker(uint8_t payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +0000165
Peter Boström4fa7eca2016-03-02 15:05:53 +0100166 Clock* const clock_;
Tommi733b5472016-06-10 17:58:01 +0200167 // Ownership of this object lies with VideoReceiveStream, which owns |this|.
168 const VideoReceiveStream::Config& config_;
Peter Boström0b250722016-04-22 18:23:15 +0200169 vcm::VideoReceiver* const video_receiver_;
Peter Boström4fa7eca2016-03-02 15:05:53 +0100170 RemoteBitrateEstimator* const remote_bitrate_estimator_;
mflodmanc0e58a32016-04-25 01:26:26 -0700171 PacketRouter* const packet_router_;
mflodmandc7d0d22016-05-06 05:32:22 -0700172 VieRemb* const remb_;
173 ProcessThread* const process_thread_;
Peter Boström4fa7eca2016-03-02 15:05:53 +0100174
175 RemoteNtpTimeEstimator ntp_estimator_;
176 RTPPayloadRegistry rtp_payload_registry_;
177
178 const std::unique_ptr<RtpHeaderParser> rtp_header_parser_;
179 const std::unique_ptr<RtpReceiver> rtp_receiver_;
kwiberg27f982b2016-03-01 11:52:33 -0800180 const std::unique_ptr<ReceiveStatistics> rtp_receive_statistics_;
brandtrd55c3f62016-10-31 04:51:33 -0700181 std::unique_ptr<UlpfecReceiver> ulpfec_receiver_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000182
Peter Boström4fa7eca2016-03-02 15:05:53 +0100183 rtc::CriticalSection receive_cs_;
184 bool receiving_ GUARDED_BY(receive_cs_);
185 uint8_t restored_packet_[IP_PACKET_SIZE] GUARDED_BY(receive_cs_);
186 bool restored_packet_in_use_ GUARDED_BY(receive_cs_);
187 int64_t last_packet_log_ms_ GUARDED_BY(receive_cs_);
mflodmanc0e58a32016-04-25 01:26:26 -0700188
189 const std::unique_ptr<RtpRtcp> rtp_rtcp_;
philipelfd5a20f2016-11-15 00:57:57 -0800190
191 // Members for the new jitter buffer experiment.
192 bool jitter_buffer_experiment_;
193 video_coding::OnCompleteFrameCallback* complete_frame_callback_;
194 KeyFrameRequestSender* keyframe_request_sender_;
195 VCMTiming* timing_;
196 std::unique_ptr<NackModule> nack_module_;
197 rtc::scoped_refptr<video_coding::PacketBuffer> packet_buffer_;
198 std::unique_ptr<video_coding::RtpFrameReferenceFinder> reference_finder_;
199 rtc::CriticalSection last_seq_num_cs_;
200 std::map<uint16_t, uint16_t, DescendingSeqNumComp<uint16_t>>
201 last_seq_num_for_pic_id_ GUARDED_BY(last_seq_num_cs_);
202 video_coding::H264SpsPpsTracker tracker_;
philipel022b54e2016-12-20 04:15:59 -0800203 // TODO(johan): Remove pt_codec_params_ once
204 // https://bugs.chromium.org/p/webrtc/issues/detail?id=6883 is resolved.
205 // Maps a payload type to a map of out-of-band supplied codec parameters.
206 std::map<uint8_t, std::map<std::string, std::string>> pt_codec_params_;
207 int16_t last_payload_type_ = -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000208};
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000209
kjellander@webrtc.org0fcaf992015-11-26 15:24:52 +0100210} // namespace webrtc
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000211
mflodmanfa666592016-04-28 23:15:33 -0700212#endif // WEBRTC_VIDEO_RTP_STREAM_RECEIVER_H_