blob: 33bccb7407199fd9084e6963a1a6383af280aa0c [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
stefan@webrtc.org9c84b0d2012-02-09 13:14:04 +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 MODULES_RTP_RTCP_SOURCE_RTP_SENDER_VIDEO_H_
12#define MODULES_RTP_RTCP_SOURCE_RTP_SENDER_VIDEO_H_
niklase@google.com470e71d2011-07-07 08:21:25 +000013
spranga8ae6f22017-09-04 07:23:56 -070014#include <map>
danilchap74110612016-10-02 10:54:29 -070015#include <memory>
pwestin@webrtc.org95cf4792012-01-20 06:59:06 +000016
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "api/optional.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020018#include "common_types.h" // NOLINT(build/include)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "modules/rtp_rtcp/include/flexfec_sender.h"
20#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
21#include "modules/rtp_rtcp/source/rtp_rtcp_config.h"
22#include "modules/rtp_rtcp/source/rtp_sender.h"
23#include "modules/rtp_rtcp/source/rtp_utility.h"
24#include "modules/rtp_rtcp/source/ulpfec_generator.h"
25#include "modules/rtp_rtcp/source/video_codec_information.h"
26#include "rtc_base/criticalsection.h"
27#include "rtc_base/onetimeevent.h"
28#include "rtc_base/rate_statistics.h"
29#include "rtc_base/sequenced_task_checker.h"
30#include "rtc_base/thread_annotations.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020031#include "typedefs.h" // NOLINT(build/include)
niklase@google.com470e71d2011-07-07 08:21:25 +000032
33namespace webrtc {
spranga8ae6f22017-09-04 07:23:56 -070034class RtpPacketizer;
danilchap74110612016-10-02 10:54:29 -070035class RtpPacketToSend;
pwestin@webrtc.org95cf4792012-01-20 06:59:06 +000036
pbos@webrtc.orgb5e6bfc2014-09-12 11:05:55 +000037class RTPSenderVideo {
38 public:
spranga8ae6f22017-09-04 07:23:56 -070039 static constexpr int64_t kTLRateWindowSizeMs = 2500;
40
brandtrdbdb3f12016-11-10 05:04:48 -080041 RTPSenderVideo(Clock* clock,
42 RTPSender* rtpSender,
43 FlexfecSender* flexfec_sender);
pbos@webrtc.orgb5e6bfc2014-09-12 11:05:55 +000044 virtual ~RTPSenderVideo();
niklase@google.com470e71d2011-07-07 08:21:25 +000045
pbos@webrtc.orgb5e6bfc2014-09-12 11:05:55 +000046 virtual RtpVideoCodecTypes VideoCodecType() const;
niklase@google.com470e71d2011-07-07 08:21:25 +000047
mflodmanfcf54bd2015-04-14 21:28:08 +020048 static RtpUtility::Payload* CreateVideoPayload(
Sergey Ulanovec4f0682016-07-28 15:19:10 -070049 const char payload_name[RTP_PAYLOAD_NAME_SIZE],
50 int8_t payload_type);
niklase@google.com470e71d2011-07-07 08:21:25 +000051
Sergey Ulanov525df3f2016-08-02 17:46:41 -070052 bool SendVideo(RtpVideoCodecTypes video_type,
53 FrameType frame_type,
54 int8_t payload_type,
55 uint32_t capture_timestamp,
56 int64_t capture_time_ms,
57 const uint8_t* payload_data,
58 size_t payload_size,
59 const RTPFragmentationHeader* fragmentation,
spranga8ae6f22017-09-04 07:23:56 -070060 const RTPVideoHeader* video_header,
61 int64_t expected_retransmission_time_ms);
niklase@google.com470e71d2011-07-07 08:21:25 +000062
pbos@webrtc.orgb5e6bfc2014-09-12 11:05:55 +000063 void SetVideoCodecType(RtpVideoCodecTypes type);
niklase@google.com470e71d2011-07-07 08:21:25 +000064
brandtrf1bb4762016-11-07 03:05:06 -080065 // ULPFEC.
66 void SetUlpfecConfig(int red_payload_type, int ulpfec_payload_type);
67 void GetUlpfecConfig(int* red_payload_type, int* ulpfec_payload_type) const;
pbos@webrtc.orgb5e6bfc2014-09-12 11:05:55 +000068
brandtr9dfff292016-11-14 05:14:50 -080069 // FlexFEC/ULPFEC.
brandtr1743a192016-11-07 03:36:05 -080070 void SetFecParameters(const FecProtectionParams& delta_params,
71 const FecProtectionParams& key_params);
pbos@webrtc.orgb5e6bfc2014-09-12 11:05:55 +000072
brandtr9dfff292016-11-14 05:14:50 -080073 // FlexFEC.
74 rtc::Optional<uint32_t> FlexfecSsrc() const;
75
pbos@webrtc.orgb5e6bfc2014-09-12 11:05:55 +000076 uint32_t VideoBitrateSent() const;
77 uint32_t FecOverheadRate() const;
78
79 int SelectiveRetransmissions() const;
mflodmanfcf54bd2015-04-14 21:28:08 +020080 void SetSelectiveRetransmissions(uint8_t settings);
pbos@webrtc.orgb5e6bfc2014-09-12 11:05:55 +000081
spranga8ae6f22017-09-04 07:23:56 -070082 protected:
83 static uint8_t GetTemporalId(const RTPVideoHeader& header);
84 StorageType GetStorageType(uint8_t temporal_id,
85 int32_t retransmission_settings,
86 int64_t expected_retransmission_time_ms);
87
danilchap162abd32015-12-10 02:39:40 -080088 private:
spranga8ae6f22017-09-04 07:23:56 -070089 struct TemporalLayerStats {
90 TemporalLayerStats()
91 : frame_rate_fp1000s(kTLRateWindowSizeMs, 1000 * 1000),
92 last_frame_time_ms(0) {}
93 // Frame rate, in frames per 1000 seconds. This essentially turns the fps
94 // value into a fixed point value with three decimals. Improves precision at
95 // low frame rates.
96 RateStatistics frame_rate_fp1000s;
97 int64_t last_frame_time_ms;
98 };
99
danilchap56359be2017-09-07 07:53:45 -0700100 size_t CalculateFecPacketOverhead() const RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
danilchap2a615fc2016-11-11 02:27:35 -0800101
danilchap74110612016-10-02 10:54:29 -0700102 void SendVideoPacket(std::unique_ptr<RtpPacketToSend> packet,
mflodmanfcf54bd2015-04-14 21:28:08 +0200103 StorageType storage);
pbos@webrtc.orgb5e6bfc2014-09-12 11:05:55 +0000104
brandtr131bc492016-11-10 05:01:11 -0800105 void SendVideoPacketAsRedMaybeWithUlpfec(
106 std::unique_ptr<RtpPacketToSend> media_packet,
107 StorageType media_packet_storage,
108 bool protect_media_packet);
109
110 // TODO(brandtr): Remove the FlexFEC functions when FlexfecSender has been
111 // moved to PacedSender.
112 void SendVideoPacketWithFlexfec(std::unique_ptr<RtpPacketToSend> media_packet,
113 StorageType media_packet_storage,
114 bool protect_media_packet);
pbos@webrtc.orgb5e6bfc2014-09-12 11:05:55 +0000115
danilchap56359be2017-09-07 07:53:45 -0700116 bool red_enabled() const RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_) {
brandtrf1bb4762016-11-07 03:05:06 -0800117 return red_payload_type_ >= 0;
118 }
119
danilchap56359be2017-09-07 07:53:45 -0700120 bool ulpfec_enabled() const RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_) {
brandtrf1bb4762016-11-07 03:05:06 -0800121 return ulpfec_payload_type_ >= 0;
122 }
123
brandtr131bc492016-11-10 05:01:11 -0800124 bool flexfec_enabled() const { return flexfec_sender_ != nullptr; }
125
spranga8ae6f22017-09-04 07:23:56 -0700126 bool UpdateConditionalRetransmit(uint8_t temporal_id,
127 int64_t expected_retransmission_time_ms)
danilchap56359be2017-09-07 07:53:45 -0700128 RTC_EXCLUSIVE_LOCKS_REQUIRED(stats_crit_);
spranga8ae6f22017-09-04 07:23:56 -0700129
danilchap5fb291a2016-08-09 07:43:25 -0700130 RTPSender* const rtp_sender_;
sprangcd349d92016-07-13 09:11:28 -0700131 Clock* const clock_;
pbos@webrtc.orgb5e6bfc2014-09-12 11:05:55 +0000132
mflodmanfcf54bd2015-04-14 21:28:08 +0200133 // Should never be held when calling out of this class.
sprangcd349d92016-07-13 09:11:28 -0700134 rtc::CriticalSection crit_;
mflodmanfcf54bd2015-04-14 21:28:08 +0200135
brandtrd8048952016-11-07 02:08:51 -0800136 RtpVideoCodecTypes video_type_;
danilchap56359be2017-09-07 07:53:45 -0700137 int32_t retransmission_settings_ RTC_GUARDED_BY(crit_);
138 VideoRotation last_rotation_ RTC_GUARDED_BY(crit_);
pbos@webrtc.orgb5e6bfc2014-09-12 11:05:55 +0000139
brandtrf1bb4762016-11-07 03:05:06 -0800140 // RED/ULPFEC.
danilchap56359be2017-09-07 07:53:45 -0700141 int red_payload_type_ RTC_GUARDED_BY(crit_);
142 int ulpfec_payload_type_ RTC_GUARDED_BY(crit_);
143 UlpfecGenerator ulpfec_generator_ RTC_GUARDED_BY(crit_);
brandtr131bc492016-11-10 05:01:11 -0800144
145 // FlexFEC.
146 FlexfecSender* const flexfec_sender_;
147
148 // FEC parameters, applicable to either ULPFEC or FlexFEC.
danilchap56359be2017-09-07 07:53:45 -0700149 FecProtectionParams delta_fec_params_ RTC_GUARDED_BY(crit_);
150 FecProtectionParams key_fec_params_ RTC_GUARDED_BY(crit_);
pbos@webrtc.orgb5e6bfc2014-09-12 11:05:55 +0000151
sprangcd349d92016-07-13 09:11:28 -0700152 rtc::CriticalSection stats_crit_;
pbos@webrtc.orgb5e6bfc2014-09-12 11:05:55 +0000153 // Bitrate used for FEC payload, RED headers, RTP headers for FEC packets
154 // and any padding overhead.
danilchap56359be2017-09-07 07:53:45 -0700155 RateStatistics fec_bitrate_ RTC_GUARDED_BY(stats_crit_);
sprangcd349d92016-07-13 09:11:28 -0700156 // Bitrate used for video payload and RTP headers.
danilchap56359be2017-09-07 07:53:45 -0700157 RateStatistics video_bitrate_ RTC_GUARDED_BY(stats_crit_);
spranga8ae6f22017-09-04 07:23:56 -0700158
159 std::map<int, TemporalLayerStats> frame_stats_by_temporal_layer_
danilchap56359be2017-09-07 07:53:45 -0700160 RTC_GUARDED_BY(stats_crit_);
spranga8ae6f22017-09-04 07:23:56 -0700161
skvlad98bb6642016-04-07 15:36:45 -0700162 OneTimeEvent first_frame_sent_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000163};
Sergey Ulanovec4f0682016-07-28 15:19:10 -0700164
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000165} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +0000166
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200167#endif // MODULES_RTP_RTCP_SOURCE_RTP_SENDER_VIDEO_H_