blob: e17bb49a366b11f00abc8879698a663daae826b1 [file] [log] [blame]
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +00001/*
2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
3 *
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
Stefan Holmer9416ef82018-07-19 10:34:38 +020011#ifndef CALL_RTP_VIDEO_SENDER_H_
12#define CALL_RTP_VIDEO_SENDER_H_
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000013
Åsa Persson4bece9a2017-10-06 10:04:04 +020014#include <map>
Stefan Holmerdbdb3a02018-07-17 16:03:46 +020015#include <memory>
Stefan Holmer64be7fa2018-10-04 15:21:55 +020016#include <unordered_set>
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000017#include <vector>
18
Elad Alon8b60e8b2019-04-08 14:14:05 +020019#include "absl/types/optional.h"
Elad Alon898395d2019-04-10 15:55:00 +020020#include "api/array_view.h"
Stefan Holmerdbdb3a02018-07-17 16:03:46 +020021#include "api/call/transport.h"
Stefan Holmer64be7fa2018-10-04 15:21:55 +020022#include "api/fec_controller.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "api/video_codecs/video_encoder.h"
Stefan Holmerdbdb3a02018-07-17 16:03:46 +020024#include "call/rtp_config.h"
Stefan Holmerf7044682018-07-17 10:16:41 +020025#include "call/rtp_payload_params.h"
Stefan Holmerdbdb3a02018-07-17 16:03:46 +020026#include "call/rtp_transport_controller_send_interface.h"
Stefan Holmer9416ef82018-07-19 10:34:38 +020027#include "call/rtp_video_sender_interface.h"
Stefan Holmerdbdb3a02018-07-17 16:03:46 +020028#include "logging/rtc_event_log/rtc_event_log.h"
29#include "modules/rtp_rtcp/include/flexfec_sender.h"
Niels Möller5fe95102019-03-04 16:49:25 +010030#include "modules/rtp_rtcp/source/rtp_sender_video.h"
Elad Alon8b60e8b2019-04-08 14:14:05 +020031#include "modules/rtp_rtcp/source/rtp_sequence_number_map.h"
philipel1a4746a2018-07-09 15:52:29 +020032#include "modules/rtp_rtcp/source/rtp_video_header.h"
Stefan Holmerdbdb3a02018-07-17 16:03:46 +020033#include "modules/utility/include/process_thread.h"
Steve Anton10542f22019-01-11 09:11:00 -080034#include "rtc_base/constructor_magic.h"
35#include "rtc_base/critical_section.h"
Stefan Holmerdbdb3a02018-07-17 16:03:46 +020036#include "rtc_base/rate_limiter.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020037#include "rtc_base/thread_annotations.h"
Stefan Holmerdbdb3a02018-07-17 16:03:46 +020038#include "rtc_base/thread_checker.h"
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000039
40namespace webrtc {
41
Benjamin Wright192eeec2018-10-17 17:27:25 -070042class FrameEncryptorInterface;
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000043class RTPFragmentationHeader;
44class RtpRtcp;
Stefan Holmerdbdb3a02018-07-17 16:03:46 +020045class RtpTransportControllerSendInterface;
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000046
Niels Möller5fe95102019-03-04 16:49:25 +010047namespace webrtc_internal_rtp_video_sender {
48// RTP state for a single simulcast stream. Internal to the implementation of
49// RtpVideoSender.
50struct RtpStreamSender {
51 RtpStreamSender(std::unique_ptr<PlayoutDelayOracle> playout_delay_oracle,
52 std::unique_ptr<RtpRtcp> rtp_rtcp,
53 std::unique_ptr<RTPSenderVideo> sender_video);
54 ~RtpStreamSender();
55
56 RtpStreamSender(RtpStreamSender&&) = default;
57 RtpStreamSender& operator=(RtpStreamSender&&) = default;
58
59 // Note: Needs pointer stability.
60 std::unique_ptr<PlayoutDelayOracle> playout_delay_oracle;
61 std::unique_ptr<RtpRtcp> rtp_rtcp;
62 std::unique_ptr<RTPSenderVideo> sender_video;
63};
64
65} // namespace webrtc_internal_rtp_video_sender
66
Stefan Holmer9416ef82018-07-19 10:34:38 +020067// RtpVideoSender routes outgoing data to the correct sending RTP module, based
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000068// on the simulcast layer in RTPVideoHeader.
Stefan Holmer64be7fa2018-10-04 15:21:55 +020069class RtpVideoSender : public RtpVideoSenderInterface,
70 public OverheadObserver,
71 public VCMProtectionCallback,
72 public PacketFeedbackObserver {
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000073 public:
Per83d09102016-04-15 14:59:13 +020074 // Rtp modules are assumed to be sorted in simulcast index order.
Stefan Holmer9416ef82018-07-19 10:34:38 +020075 RtpVideoSender(
Sebastian Jansson572c60f2019-03-04 18:30:41 +010076 Clock* clock,
Stefan Holmerdbdb3a02018-07-17 16:03:46 +020077 std::map<uint32_t, RtpState> suspended_ssrcs,
78 const std::map<uint32_t, RtpPayloadState>& states,
79 const RtpConfig& rtp_config,
Jiawei Ou55718122018-11-09 13:17:39 -080080 int rtcp_report_interval_ms,
Stefan Holmerdbdb3a02018-07-17 16:03:46 +020081 Transport* send_transport,
82 const RtpSenderObservers& observers,
83 RtpTransportControllerSendInterface* transport,
84 RtcEventLog* event_log,
Stefan Holmer64be7fa2018-10-04 15:21:55 +020085 RateLimiter* retransmission_limiter, // move inside RtpTransport
Benjamin Wright192eeec2018-10-17 17:27:25 -070086 std::unique_ptr<FecController> fec_controller,
87 FrameEncryptorInterface* frame_encryptor,
88 const CryptoOptions& crypto_options); // move inside RtpTransport
Stefan Holmer9416ef82018-07-19 10:34:38 +020089 ~RtpVideoSender() override;
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000090
Stefan Holmerdbdb3a02018-07-17 16:03:46 +020091 // RegisterProcessThread register |module_process_thread| with those objects
92 // that use it. Registration has to happen on the thread were
93 // |module_process_thread| was created (libjingle's worker thread).
94 // TODO(perkj): Replace the use of |module_process_thread| with a TaskQueue,
95 // maybe |worker_queue|.
96 void RegisterProcessThread(ProcessThread* module_process_thread) override;
97 void DeRegisterProcessThread() override;
98
Stefan Holmer9416ef82018-07-19 10:34:38 +020099 // RtpVideoSender will only route packets if being active, all packets will be
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000100 // dropped otherwise.
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200101 void SetActive(bool active) override;
Seth Hampsoncc7125f2018-02-02 08:46:16 -0800102 // Sets the sending status of the rtp modules and appropriately sets the
103 // payload router to active if any rtp modules are active.
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200104 void SetActiveModules(const std::vector<bool> active_modules) override;
105 bool IsActive() override;
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000106
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200107 void OnNetworkAvailability(bool network_available) override;
108 std::map<uint32_t, RtpState> GetRtpStates() const override;
109 std::map<uint32_t, RtpPayloadState> GetRtpPayloadStates() const override;
110
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200111 void DeliverRtcp(const uint8_t* packet, size_t length) override;
112
Stefan Holmer64be7fa2018-10-04 15:21:55 +0200113 // Implements webrtc::VCMProtectionCallback.
114 int ProtectionRequest(const FecProtectionParams* delta_params,
115 const FecProtectionParams* key_params,
116 uint32_t* sent_video_rate_bps,
117 uint32_t* sent_nack_rate_bps,
118 uint32_t* sent_fec_rate_bps) override;
Åsa Persson4bece9a2017-10-06 10:04:04 +0200119
kjellander02b3d272016-04-20 05:05:54 -0700120 // Implements EncodedImageCallback.
121 // Returns 0 if the packet was routed / sent, -1 otherwise.
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700122 EncodedImageCallback::Result OnEncodedImage(
123 const EncodedImage& encoded_image,
124 const CodecSpecificInfo* codec_specific_info,
125 const RTPFragmentationHeader* fragmentation) override;
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000126
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200127 void OnBitrateAllocationUpdated(
128 const VideoBitrateAllocation& bitrate) override;
sprang1a646ee2016-12-01 06:34:11 -0800129
Stefan Holmer64be7fa2018-10-04 15:21:55 +0200130 void OnTransportOverheadChanged(
131 size_t transport_overhead_bytes_per_packet) override;
132 // Implements OverheadObserver.
133 void OnOverheadChanged(size_t overhead_bytes_per_packet) override;
134 void OnBitrateUpdated(uint32_t bitrate_bps,
135 uint8_t fraction_loss,
136 int64_t rtt,
137 int framerate) override;
138 uint32_t GetPayloadBitrateBps() const override;
139 uint32_t GetProtectionBitrateBps() const override;
140 void SetEncodingData(size_t width,
141 size_t height,
142 size_t num_temporal_layers) override;
143
Elad Alon898395d2019-04-10 15:55:00 +0200144 std::vector<RtpSequenceNumberMap::Info> GetSentRtpPacketInfos(
Elad Alon8b60e8b2019-04-08 14:14:05 +0200145 uint32_t ssrc,
Elad Alon898395d2019-04-10 15:55:00 +0200146 rtc::ArrayView<const uint16_t> sequence_numbers) const override;
Elad Alon8b60e8b2019-04-08 14:14:05 +0200147
Stefan Holmer64be7fa2018-10-04 15:21:55 +0200148 // From PacketFeedbackObserver.
149 void OnPacketAdded(uint32_t ssrc, uint16_t seq_num) override;
150 void OnPacketFeedbackVector(
151 const std::vector<PacketFeedback>& packet_feedback_vector) override;
152
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000153 private:
danilchapa37de392017-09-09 04:17:22 -0700154 void UpdateModuleSendingState() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200155 void ConfigureProtection(const RtpConfig& rtp_config);
156 void ConfigureSsrcs(const RtpConfig& rtp_config);
Amit Hilbuch77938e62018-12-21 09:23:38 -0800157 void ConfigureRids(const RtpConfig& rtp_config);
Stefan Holmer64be7fa2018-10-04 15:21:55 +0200158 bool FecEnabled() const;
159 bool NackEnabled() const;
Erik Språng482b3ef2019-01-08 16:19:11 +0100160 uint32_t GetPacketizationOverheadRate() const;
Peter Boström8b79b072016-02-26 16:31:37 +0100161
Stefan Holmer64be7fa2018-10-04 15:21:55 +0200162 const bool send_side_bwe_with_overhead_;
Erik Språngc12d41b2019-01-09 09:55:31 +0100163 const bool account_for_packetization_overhead_;
Stefan Holmer64be7fa2018-10-04 15:21:55 +0200164
165 // TODO(holmer): Remove crit_ once RtpVideoSender runs on the
166 // transport task queue.
pbosd8de1152016-02-01 09:00:51 -0800167 rtc::CriticalSection crit_;
danilchapa37de392017-09-09 04:17:22 -0700168 bool active_ RTC_GUARDED_BY(crit_);
mflodman@webrtc.org7ac374a2015-02-20 12:45:40 +0000169
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200170 ProcessThread* module_process_thread_;
171 rtc::ThreadChecker module_process_thread_checker_;
172 std::map<uint32_t, RtpState> suspended_ssrcs_;
173
174 std::unique_ptr<FlexfecSender> flexfec_sender_;
Stefan Holmer64be7fa2018-10-04 15:21:55 +0200175 std::unique_ptr<FecController> fec_controller_;
Niels Möller2a152672018-08-08 12:03:00 +0200176 // Rtp modules are assumed to be sorted in simulcast index order.
Niels Möller5fe95102019-03-04 16:49:25 +0100177 const std::vector<webrtc_internal_rtp_video_sender::RtpStreamSender>
178 rtp_streams_;
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200179 const RtpConfig rtp_config_;
180 RtpTransportControllerSendInterface* const transport_;
Per83d09102016-04-15 14:59:13 +0200181
philipel25d31ec2018-08-08 16:33:01 +0200182 // When using the generic descriptor we want all simulcast streams to share
183 // one frame id space (so that the SFU can switch stream without having to
184 // rewrite the frame id), therefore |shared_frame_id| has to live in a place
185 // where we are aware of all the different streams.
186 int64_t shared_frame_id_ = 0;
Åsa Persson4bece9a2017-10-06 10:04:04 +0200187 std::vector<RtpPayloadParams> params_ RTC_GUARDED_BY(crit_);
188
Stefan Holmer64be7fa2018-10-04 15:21:55 +0200189 size_t transport_overhead_bytes_per_packet_ RTC_GUARDED_BY(crit_);
190 size_t overhead_bytes_per_packet_ RTC_GUARDED_BY(crit_);
191 uint32_t protection_bitrate_bps_;
192 uint32_t encoder_target_rate_bps_;
193
194 std::unordered_set<uint16_t> feedback_packet_seq_num_set_;
195 std::vector<bool> loss_mask_vector_ RTC_GUARDED_BY(crit_);
196
Niels Möller949f0fd2019-01-29 09:44:24 +0100197 std::vector<FrameCounts> frame_counts_ RTC_GUARDED_BY(crit_);
198 FrameCountObserver* const frame_count_observer_;
199
Stefan Holmer9416ef82018-07-19 10:34:38 +0200200 RTC_DISALLOW_COPY_AND_ASSIGN(RtpVideoSender);
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000201};
202
203} // namespace webrtc
204
Stefan Holmer9416ef82018-07-19 10:34:38 +0200205#endif // CALL_RTP_VIDEO_SENDER_H_