blob: 36689848eb47a74f36dc22e781867d169d49a7a2 [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
wu@webrtc.org822fbd82013-08-15 23:38:54 +000085 bool SetReceiveCodec(const VideoCodec& video_codec);
wu@webrtc.org822fbd82013-08-15 23:38:54 +000086
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 DeliverRtp(const uint8_t* rtp_packet,
97 size_t rtp_packet_length,
98 const PacketTime& packet_time);
99 bool DeliverRtcp(const uint8_t* rtcp_packet, size_t rtcp_packet_length);
niklase@google.com470e71d2011-07-07 08:21:25 +0000100
philipelfd5a20f2016-11-15 00:57:57 -0800101 void FrameContinuous(uint16_t seq_num);
102
103 void FrameDecoded(uint16_t seq_num);
104
mflodmandc7d0d22016-05-06 05:32:22 -0700105 void SignalNetworkState(NetworkState state);
106
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;
111 bool 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;
119 void OnIncomingSSRCChanged(uint32_t ssrc) override;
120 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;
124 int32_t SliceLossIndicationRequest(const uint64_t picture_id) override;
125
mflodmandc7d0d22016-05-06 05:32:22 -0700126 bool IsFecEnabled() const;
127 bool IsRetransmissionsEnabled() const;
128 // Don't use, still experimental.
129 void RequestPacketRetransmit(const std::vector<uint16_t>& sequence_numbers);
130
mflodmancfc8e3b2016-05-03 21:22:04 -0700131 // Implements VCMPacketRequestCallback.
132 int32_t ResendPackets(const uint16_t* sequenceNumbers,
133 uint16_t length) override;
134
philipelfd5a20f2016-11-15 00:57:57 -0800135 // Implements OnReceivedFrameCallback.
136 void OnReceivedFrame(
137 std::unique_ptr<video_coding::RtpFrameObject> frame) override;
138
139 // Implements OnCompleteFrameCallback.
140 void OnCompleteFrame(
141 std::unique_ptr<video_coding::FrameObject> frame) override;
142
143 void OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) override;
144
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000145 private:
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000146 bool ReceivePacket(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000147 size_t packet_length,
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000148 const RTPHeader& header,
149 bool in_order);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000150 // Parses and handles for instance RTX and RED headers.
151 // This function assumes that it's being called from only one thread.
152 bool ParseAndHandleEncapsulatingHeader(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000153 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000154 const RTPHeader& header);
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000155 void NotifyReceiverOfFecPacket(const RTPHeader& header);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000156 bool IsPacketInOrder(const RTPHeader& header) const;
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000157 bool IsPacketRetransmitted(const RTPHeader& header, bool in_order) const;
asapersson@webrtc.org0800db72015-01-15 07:40:20 +0000158 void UpdateHistograms();
mflodmandc7d0d22016-05-06 05:32:22 -0700159 void EnableReceiveRtpHeaderExtension(const std::string& extension, int id);
brandtre6f98c72016-11-11 03:28:30 -0800160 bool IsRedEnabled() const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000161
Peter Boström4fa7eca2016-03-02 15:05:53 +0100162 Clock* const clock_;
Tommi733b5472016-06-10 17:58:01 +0200163 // Ownership of this object lies with VideoReceiveStream, which owns |this|.
164 const VideoReceiveStream::Config& config_;
Peter Boström0b250722016-04-22 18:23:15 +0200165 vcm::VideoReceiver* const video_receiver_;
Peter Boström4fa7eca2016-03-02 15:05:53 +0100166 RemoteBitrateEstimator* const remote_bitrate_estimator_;
mflodmanc0e58a32016-04-25 01:26:26 -0700167 PacketRouter* const packet_router_;
mflodmandc7d0d22016-05-06 05:32:22 -0700168 VieRemb* const remb_;
169 ProcessThread* const process_thread_;
Peter Boström4fa7eca2016-03-02 15:05:53 +0100170
171 RemoteNtpTimeEstimator ntp_estimator_;
172 RTPPayloadRegistry rtp_payload_registry_;
173
174 const std::unique_ptr<RtpHeaderParser> rtp_header_parser_;
175 const std::unique_ptr<RtpReceiver> rtp_receiver_;
kwiberg27f982b2016-03-01 11:52:33 -0800176 const std::unique_ptr<ReceiveStatistics> rtp_receive_statistics_;
brandtrd55c3f62016-10-31 04:51:33 -0700177 std::unique_ptr<UlpfecReceiver> ulpfec_receiver_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000178
Peter Boström4fa7eca2016-03-02 15:05:53 +0100179 rtc::CriticalSection receive_cs_;
180 bool receiving_ GUARDED_BY(receive_cs_);
181 uint8_t restored_packet_[IP_PACKET_SIZE] GUARDED_BY(receive_cs_);
182 bool restored_packet_in_use_ GUARDED_BY(receive_cs_);
183 int64_t last_packet_log_ms_ GUARDED_BY(receive_cs_);
mflodmanc0e58a32016-04-25 01:26:26 -0700184
185 const std::unique_ptr<RtpRtcp> rtp_rtcp_;
philipelfd5a20f2016-11-15 00:57:57 -0800186
187 // Members for the new jitter buffer experiment.
188 bool jitter_buffer_experiment_;
189 video_coding::OnCompleteFrameCallback* complete_frame_callback_;
190 KeyFrameRequestSender* keyframe_request_sender_;
191 VCMTiming* timing_;
192 std::unique_ptr<NackModule> nack_module_;
193 rtc::scoped_refptr<video_coding::PacketBuffer> packet_buffer_;
194 std::unique_ptr<video_coding::RtpFrameReferenceFinder> reference_finder_;
195 rtc::CriticalSection last_seq_num_cs_;
196 std::map<uint16_t, uint16_t, DescendingSeqNumComp<uint16_t>>
197 last_seq_num_for_pic_id_ GUARDED_BY(last_seq_num_cs_);
198 video_coding::H264SpsPpsTracker tracker_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000199};
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000200
kjellander@webrtc.org0fcaf992015-11-26 15:24:52 +0100201} // namespace webrtc
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000202
mflodmanfa666592016-04-28 23:15:33 -0700203#endif // WEBRTC_VIDEO_RTP_STREAM_RECEIVER_H_