blob: 0a3b3159a1b1d7e90306b6fbf1c5fd24a08889a1 [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>
kwiberg27f982b2016-03-01 11:52:33 -080015#include <memory>
Peter Boström9c017252016-02-26 16:26:20 +010016#include <string>
kjellander@webrtc.org0fcaf992015-11-26 15:24:52 +010017#include <vector>
pwestin@webrtc.org1da1ce02011-10-13 15:19:55 +000018
kwiberg4485ffb2016-04-26 08:14:39 -070019#include "webrtc/base/constructormagic.h"
Tommi97888bd2016-01-21 23:24:59 +010020#include "webrtc/base/criticalsection.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010021#include "webrtc/modules/rtp_rtcp/include/receive_statistics.h"
Peter Boström4fa7eca2016-03-02 15:05:53 +010022#include "webrtc/modules/rtp_rtcp/include/remote_ntp_time_estimator.h"
23#include "webrtc/modules/rtp_rtcp/include/rtp_payload_registry.h"
mflodmanc0e58a32016-04-25 01:26:26 -070024#include "webrtc/modules/rtp_rtcp/include/rtp_rtcp.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010025#include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h"
mflodmancfc8e3b2016-05-03 21:22:04 -070026#include "webrtc/modules/video_coding/include/video_coding_defines.h"
pwestin@webrtc.org82dcc9f2013-04-02 20:37:14 +000027#include "webrtc/typedefs.h"
mflodmancfc8e3b2016-05-03 21:22:04 -070028#include "webrtc/video_receive_stream.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000029
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +000030namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000031
mflodmanc0e58a32016-04-25 01:26:26 -070032class PacedSender;
33class PacketRouter;
mflodmandc7d0d22016-05-06 05:32:22 -070034class ProcessThread;
wu@webrtc.org88abf112014-05-14 16:53:51 +000035class RemoteNtpTimeEstimator;
wu@webrtc.org822fbd82013-08-15 23:38:54 +000036class ReceiveStatistics;
mflodmancfc8e3b2016-05-03 21:22:04 -070037class ReceiveStatisticsProxy;
stefan@webrtc.org976a7e62012-09-21 13:20:21 +000038class RemoteBitrateEstimator;
mflodmanc0e58a32016-04-25 01:26:26 -070039class RtcpRttStats;
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000040class RtpHeaderParser;
wu@webrtc.org822fbd82013-08-15 23:38:54 +000041class RTPPayloadRegistry;
42class RtpReceiver;
mflodmanc0e58a32016-04-25 01:26:26 -070043class Transport;
brandtrd55c3f62016-10-31 04:51:33 -070044class UlpfecReceiver;
mflodmandc7d0d22016-05-06 05:32:22 -070045class VieRemb;
Peter Boström0b250722016-04-22 18:23:15 +020046
47namespace vcm {
48class VideoReceiver;
49} // namespace vcm
niklase@google.com470e71d2011-07-07 08:21:25 +000050
mflodmancfc8e3b2016-05-03 21:22:04 -070051class RtpStreamReceiver : public RtpData, public RtpFeedback,
52 public VCMFrameTypeCallback,
53 public VCMPacketRequestCallback {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +000054 public:
mflodmanfa666592016-04-28 23:15:33 -070055 RtpStreamReceiver(vcm::VideoReceiver* video_receiver,
56 RemoteBitrateEstimator* remote_bitrate_estimator,
57 Transport* transport,
58 RtcpRttStats* rtt_stats,
59 PacedSender* paced_sender,
mflodmancfc8e3b2016-05-03 21:22:04 -070060 PacketRouter* packet_router,
mflodmandc7d0d22016-05-06 05:32:22 -070061 VieRemb* remb,
Tommi733b5472016-06-10 17:58:01 +020062 const VideoReceiveStream::Config* config,
mflodmandc7d0d22016-05-06 05:32:22 -070063 ReceiveStatisticsProxy* receive_stats_proxy,
Erik Språng737336d2016-07-29 12:59:36 +020064 ProcessThread* process_thread,
65 RateLimiter* retransmission_rate_limiter);
mflodmanfa666592016-04-28 23:15:33 -070066 ~RtpStreamReceiver();
niklase@google.com470e71d2011-07-07 08:21:25 +000067
wu@webrtc.org822fbd82013-08-15 23:38:54 +000068 bool SetReceiveCodec(const VideoCodec& video_codec);
wu@webrtc.org822fbd82013-08-15 23:38:54 +000069
wu@webrtc.org822fbd82013-08-15 23:38:54 +000070 uint32_t GetRemoteSsrc() const;
71 int GetCsrcs(uint32_t* csrcs) const;
72
wu@webrtc.org822fbd82013-08-15 23:38:54 +000073 RtpReceiver* GetRtpReceiver() const;
mflodmanc0e58a32016-04-25 01:26:26 -070074 RtpRtcp* rtp_rtcp() const { return rtp_rtcp_.get(); }
wu@webrtc.org822fbd82013-08-15 23:38:54 +000075
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +000076 void StartReceive();
77 void StopReceive();
niklase@google.com470e71d2011-07-07 08:21:25 +000078
Peter Boströmd1d66ba2016-02-08 14:07:14 +010079 bool DeliverRtp(const uint8_t* rtp_packet,
80 size_t rtp_packet_length,
81 const PacketTime& packet_time);
82 bool DeliverRtcp(const uint8_t* rtcp_packet, size_t rtcp_packet_length);
niklase@google.com470e71d2011-07-07 08:21:25 +000083
mflodmandc7d0d22016-05-06 05:32:22 -070084 void SignalNetworkState(NetworkState state);
85
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +000086 // Implements RtpData.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000087 int32_t OnReceivedPayloadData(const uint8_t* payload_data,
Peter Boström02083222016-06-14 12:52:54 +020088 size_t payload_size,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000089 const WebRtcRTPHeader* rtp_header) override;
90 bool OnRecoveredPacket(const uint8_t* packet, size_t packet_length) override;
niklase@google.com470e71d2011-07-07 08:21:25 +000091
mflodmanfa666592016-04-28 23:15:33 -070092 // Implements RtpFeedback.
Sergey Ulanovec4f0682016-07-28 15:19:10 -070093 int32_t OnInitializeDecoder(int8_t payload_type,
mflodmanfa666592016-04-28 23:15:33 -070094 const char payload_name[RTP_PAYLOAD_NAME_SIZE],
Sergey Ulanovec4f0682016-07-28 15:19:10 -070095 int frequency,
96 size_t channels,
97 uint32_t rate) override;
98 void OnIncomingSSRCChanged(uint32_t ssrc) override;
99 void OnIncomingCSRCChanged(uint32_t CSRC, bool added) override {}
mflodmanfa666592016-04-28 23:15:33 -0700100
mflodmancfc8e3b2016-05-03 21:22:04 -0700101 // Implements VCMFrameTypeCallback.
102 int32_t RequestKeyFrame() override;
103 int32_t SliceLossIndicationRequest(const uint64_t picture_id) override;
104
mflodmandc7d0d22016-05-06 05:32:22 -0700105 bool IsFecEnabled() const;
106 bool IsRetransmissionsEnabled() const;
107 // Don't use, still experimental.
108 void RequestPacketRetransmit(const std::vector<uint16_t>& sequence_numbers);
109
mflodmancfc8e3b2016-05-03 21:22:04 -0700110 // Implements VCMPacketRequestCallback.
111 int32_t ResendPackets(const uint16_t* sequenceNumbers,
112 uint16_t length) override;
113
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000114 private:
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000115 bool ReceivePacket(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000116 size_t packet_length,
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000117 const RTPHeader& header,
118 bool in_order);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000119 // Parses and handles for instance RTX and RED headers.
120 // This function assumes that it's being called from only one thread.
121 bool ParseAndHandleEncapsulatingHeader(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000122 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000123 const RTPHeader& header);
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000124 void NotifyReceiverOfFecPacket(const RTPHeader& header);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000125 bool IsPacketInOrder(const RTPHeader& header) const;
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000126 bool IsPacketRetransmitted(const RTPHeader& header, bool in_order) const;
asapersson@webrtc.org0800db72015-01-15 07:40:20 +0000127 void UpdateHistograms();
mflodmandc7d0d22016-05-06 05:32:22 -0700128 void EnableReceiveRtpHeaderExtension(const std::string& extension, int id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000129
Peter Boström4fa7eca2016-03-02 15:05:53 +0100130 Clock* const clock_;
Tommi733b5472016-06-10 17:58:01 +0200131 // Ownership of this object lies with VideoReceiveStream, which owns |this|.
132 const VideoReceiveStream::Config& config_;
Peter Boström0b250722016-04-22 18:23:15 +0200133 vcm::VideoReceiver* const video_receiver_;
Peter Boström4fa7eca2016-03-02 15:05:53 +0100134 RemoteBitrateEstimator* const remote_bitrate_estimator_;
mflodmanc0e58a32016-04-25 01:26:26 -0700135 PacketRouter* const packet_router_;
mflodmandc7d0d22016-05-06 05:32:22 -0700136 VieRemb* const remb_;
137 ProcessThread* const process_thread_;
Peter Boström4fa7eca2016-03-02 15:05:53 +0100138
139 RemoteNtpTimeEstimator ntp_estimator_;
140 RTPPayloadRegistry rtp_payload_registry_;
141
142 const std::unique_ptr<RtpHeaderParser> rtp_header_parser_;
143 const std::unique_ptr<RtpReceiver> rtp_receiver_;
kwiberg27f982b2016-03-01 11:52:33 -0800144 const std::unique_ptr<ReceiveStatistics> rtp_receive_statistics_;
brandtrd55c3f62016-10-31 04:51:33 -0700145 std::unique_ptr<UlpfecReceiver> ulpfec_receiver_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000146
Peter Boström4fa7eca2016-03-02 15:05:53 +0100147 rtc::CriticalSection receive_cs_;
148 bool receiving_ GUARDED_BY(receive_cs_);
149 uint8_t restored_packet_[IP_PACKET_SIZE] GUARDED_BY(receive_cs_);
150 bool restored_packet_in_use_ GUARDED_BY(receive_cs_);
151 int64_t last_packet_log_ms_ GUARDED_BY(receive_cs_);
mflodmanc0e58a32016-04-25 01:26:26 -0700152
153 const std::unique_ptr<RtpRtcp> rtp_rtcp_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000154};
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000155
kjellander@webrtc.org0fcaf992015-11-26 15:24:52 +0100156} // namespace webrtc
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000157
mflodmanfa666592016-04-28 23:15:33 -0700158#endif // WEBRTC_VIDEO_RTP_STREAM_RECEIVER_H_