mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 1 | /* |
| 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 Holmer | 9416ef8 | 2018-07-19 10:34:38 +0200 | [diff] [blame] | 11 | #ifndef CALL_RTP_VIDEO_SENDER_H_ |
| 12 | #define CALL_RTP_VIDEO_SENDER_H_ |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 13 | |
Åsa Persson | 4bece9a | 2017-10-06 10:04:04 +0200 | [diff] [blame] | 14 | #include <map> |
Stefan Holmer | dbdb3a0 | 2018-07-17 16:03:46 +0200 | [diff] [blame] | 15 | #include <memory> |
Stefan Holmer | 64be7fa | 2018-10-04 15:21:55 +0200 | [diff] [blame] | 16 | #include <unordered_set> |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 17 | #include <vector> |
| 18 | |
Elad Alon | 8b60e8b | 2019-04-08 14:14:05 +0200 | [diff] [blame] | 19 | #include "absl/types/optional.h" |
Elad Alon | 898395d | 2019-04-10 15:55:00 +0200 | [diff] [blame] | 20 | #include "api/array_view.h" |
Stefan Holmer | dbdb3a0 | 2018-07-17 16:03:46 +0200 | [diff] [blame] | 21 | #include "api/call/transport.h" |
Stefan Holmer | 64be7fa | 2018-10-04 15:21:55 +0200 | [diff] [blame] | 22 | #include "api/fec_controller.h" |
Elad Alon | 8f01c4e | 2019-06-28 15:19:43 +0200 | [diff] [blame] | 23 | #include "api/fec_controller_override.h" |
Danil Chapovalov | 83bbe91 | 2019-08-07 12:24:53 +0200 | [diff] [blame] | 24 | #include "api/rtc_event_log/rtc_event_log.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 25 | #include "api/video_codecs/video_encoder.h" |
Stefan Holmer | dbdb3a0 | 2018-07-17 16:03:46 +0200 | [diff] [blame] | 26 | #include "call/rtp_config.h" |
Stefan Holmer | f704468 | 2018-07-17 10:16:41 +0200 | [diff] [blame] | 27 | #include "call/rtp_payload_params.h" |
Stefan Holmer | dbdb3a0 | 2018-07-17 16:03:46 +0200 | [diff] [blame] | 28 | #include "call/rtp_transport_controller_send_interface.h" |
Stefan Holmer | 9416ef8 | 2018-07-19 10:34:38 +0200 | [diff] [blame] | 29 | #include "call/rtp_video_sender_interface.h" |
Stefan Holmer | dbdb3a0 | 2018-07-17 16:03:46 +0200 | [diff] [blame] | 30 | #include "modules/rtp_rtcp/include/flexfec_sender.h" |
Erik Språng | 845c6aa | 2019-05-29 13:02:24 +0200 | [diff] [blame] | 31 | #include "modules/rtp_rtcp/source/rtp_sender.h" |
Niels Möller | 5fe9510 | 2019-03-04 16:49:25 +0100 | [diff] [blame] | 32 | #include "modules/rtp_rtcp/source/rtp_sender_video.h" |
Elad Alon | 8b60e8b | 2019-04-08 14:14:05 +0200 | [diff] [blame] | 33 | #include "modules/rtp_rtcp/source/rtp_sequence_number_map.h" |
philipel | 1a4746a | 2018-07-09 15:52:29 +0200 | [diff] [blame] | 34 | #include "modules/rtp_rtcp/source/rtp_video_header.h" |
Stefan Holmer | dbdb3a0 | 2018-07-17 16:03:46 +0200 | [diff] [blame] | 35 | #include "modules/utility/include/process_thread.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 36 | #include "rtc_base/constructor_magic.h" |
| 37 | #include "rtc_base/critical_section.h" |
Stefan Holmer | dbdb3a0 | 2018-07-17 16:03:46 +0200 | [diff] [blame] | 38 | #include "rtc_base/rate_limiter.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 39 | #include "rtc_base/thread_annotations.h" |
Stefan Holmer | dbdb3a0 | 2018-07-17 16:03:46 +0200 | [diff] [blame] | 40 | #include "rtc_base/thread_checker.h" |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 41 | |
| 42 | namespace webrtc { |
| 43 | |
Benjamin Wright | 192eeec | 2018-10-17 17:27:25 -0700 | [diff] [blame] | 44 | class FrameEncryptorInterface; |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 45 | class RTPFragmentationHeader; |
| 46 | class RtpRtcp; |
Stefan Holmer | dbdb3a0 | 2018-07-17 16:03:46 +0200 | [diff] [blame] | 47 | class RtpTransportControllerSendInterface; |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 48 | |
Niels Möller | 5fe9510 | 2019-03-04 16:49:25 +0100 | [diff] [blame] | 49 | namespace webrtc_internal_rtp_video_sender { |
| 50 | // RTP state for a single simulcast stream. Internal to the implementation of |
| 51 | // RtpVideoSender. |
| 52 | struct RtpStreamSender { |
| 53 | RtpStreamSender(std::unique_ptr<PlayoutDelayOracle> playout_delay_oracle, |
| 54 | std::unique_ptr<RtpRtcp> rtp_rtcp, |
| 55 | std::unique_ptr<RTPSenderVideo> sender_video); |
| 56 | ~RtpStreamSender(); |
| 57 | |
| 58 | RtpStreamSender(RtpStreamSender&&) = default; |
| 59 | RtpStreamSender& operator=(RtpStreamSender&&) = default; |
| 60 | |
| 61 | // Note: Needs pointer stability. |
| 62 | std::unique_ptr<PlayoutDelayOracle> playout_delay_oracle; |
| 63 | std::unique_ptr<RtpRtcp> rtp_rtcp; |
| 64 | std::unique_ptr<RTPSenderVideo> sender_video; |
| 65 | }; |
| 66 | |
| 67 | } // namespace webrtc_internal_rtp_video_sender |
| 68 | |
Stefan Holmer | 9416ef8 | 2018-07-19 10:34:38 +0200 | [diff] [blame] | 69 | // RtpVideoSender routes outgoing data to the correct sending RTP module, based |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 70 | // on the simulcast layer in RTPVideoHeader. |
Stefan Holmer | 64be7fa | 2018-10-04 15:21:55 +0200 | [diff] [blame] | 71 | class RtpVideoSender : public RtpVideoSenderInterface, |
| 72 | public OverheadObserver, |
| 73 | public VCMProtectionCallback, |
Sebastian Jansson | f298855 | 2019-10-29 17:18:51 +0100 | [diff] [blame] | 74 | public StreamFeedbackObserver { |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 75 | public: |
Per | 83d0910 | 2016-04-15 14:59:13 +0200 | [diff] [blame] | 76 | // Rtp modules are assumed to be sorted in simulcast index order. |
Stefan Holmer | 9416ef8 | 2018-07-19 10:34:38 +0200 | [diff] [blame] | 77 | RtpVideoSender( |
Sebastian Jansson | 572c60f | 2019-03-04 18:30:41 +0100 | [diff] [blame] | 78 | Clock* clock, |
Stefan Holmer | dbdb3a0 | 2018-07-17 16:03:46 +0200 | [diff] [blame] | 79 | std::map<uint32_t, RtpState> suspended_ssrcs, |
| 80 | const std::map<uint32_t, RtpPayloadState>& states, |
| 81 | const RtpConfig& rtp_config, |
Jiawei Ou | 5571812 | 2018-11-09 13:17:39 -0800 | [diff] [blame] | 82 | int rtcp_report_interval_ms, |
Stefan Holmer | dbdb3a0 | 2018-07-17 16:03:46 +0200 | [diff] [blame] | 83 | Transport* send_transport, |
| 84 | const RtpSenderObservers& observers, |
| 85 | RtpTransportControllerSendInterface* transport, |
| 86 | RtcEventLog* event_log, |
Stefan Holmer | 64be7fa | 2018-10-04 15:21:55 +0200 | [diff] [blame] | 87 | RateLimiter* retransmission_limiter, // move inside RtpTransport |
Benjamin Wright | 192eeec | 2018-10-17 17:27:25 -0700 | [diff] [blame] | 88 | std::unique_ptr<FecController> fec_controller, |
| 89 | FrameEncryptorInterface* frame_encryptor, |
| 90 | const CryptoOptions& crypto_options); // move inside RtpTransport |
Stefan Holmer | 9416ef8 | 2018-07-19 10:34:38 +0200 | [diff] [blame] | 91 | ~RtpVideoSender() override; |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 92 | |
Stefan Holmer | dbdb3a0 | 2018-07-17 16:03:46 +0200 | [diff] [blame] | 93 | // RegisterProcessThread register |module_process_thread| with those objects |
| 94 | // that use it. Registration has to happen on the thread were |
| 95 | // |module_process_thread| was created (libjingle's worker thread). |
| 96 | // TODO(perkj): Replace the use of |module_process_thread| with a TaskQueue, |
| 97 | // maybe |worker_queue|. |
| 98 | void RegisterProcessThread(ProcessThread* module_process_thread) override; |
| 99 | void DeRegisterProcessThread() override; |
| 100 | |
Stefan Holmer | 9416ef8 | 2018-07-19 10:34:38 +0200 | [diff] [blame] | 101 | // RtpVideoSender will only route packets if being active, all packets will be |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 102 | // dropped otherwise. |
Stefan Holmer | dbdb3a0 | 2018-07-17 16:03:46 +0200 | [diff] [blame] | 103 | void SetActive(bool active) override; |
Seth Hampson | cc7125f | 2018-02-02 08:46:16 -0800 | [diff] [blame] | 104 | // Sets the sending status of the rtp modules and appropriately sets the |
| 105 | // payload router to active if any rtp modules are active. |
Stefan Holmer | dbdb3a0 | 2018-07-17 16:03:46 +0200 | [diff] [blame] | 106 | void SetActiveModules(const std::vector<bool> active_modules) override; |
| 107 | bool IsActive() override; |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 108 | |
Stefan Holmer | dbdb3a0 | 2018-07-17 16:03:46 +0200 | [diff] [blame] | 109 | void OnNetworkAvailability(bool network_available) override; |
| 110 | std::map<uint32_t, RtpState> GetRtpStates() const override; |
| 111 | std::map<uint32_t, RtpPayloadState> GetRtpPayloadStates() const override; |
| 112 | |
Stefan Holmer | dbdb3a0 | 2018-07-17 16:03:46 +0200 | [diff] [blame] | 113 | void DeliverRtcp(const uint8_t* packet, size_t length) override; |
| 114 | |
Stefan Holmer | 64be7fa | 2018-10-04 15:21:55 +0200 | [diff] [blame] | 115 | // Implements webrtc::VCMProtectionCallback. |
| 116 | int ProtectionRequest(const FecProtectionParams* delta_params, |
| 117 | const FecProtectionParams* key_params, |
| 118 | uint32_t* sent_video_rate_bps, |
| 119 | uint32_t* sent_nack_rate_bps, |
| 120 | uint32_t* sent_fec_rate_bps) override; |
Åsa Persson | 4bece9a | 2017-10-06 10:04:04 +0200 | [diff] [blame] | 121 | |
Elad Alon | 8f01c4e | 2019-06-28 15:19:43 +0200 | [diff] [blame] | 122 | // Implements FecControllerOverride. |
| 123 | void SetFecAllowed(bool fec_allowed) override; |
| 124 | |
kjellander | 02b3d27 | 2016-04-20 05:05:54 -0700 | [diff] [blame] | 125 | // Implements EncodedImageCallback. |
| 126 | // Returns 0 if the packet was routed / sent, -1 otherwise. |
Sergey Ulanov | 525df3f | 2016-08-02 17:46:41 -0700 | [diff] [blame] | 127 | EncodedImageCallback::Result OnEncodedImage( |
| 128 | const EncodedImage& encoded_image, |
| 129 | const CodecSpecificInfo* codec_specific_info, |
| 130 | const RTPFragmentationHeader* fragmentation) override; |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 131 | |
Stefan Holmer | dbdb3a0 | 2018-07-17 16:03:46 +0200 | [diff] [blame] | 132 | void OnBitrateAllocationUpdated( |
| 133 | const VideoBitrateAllocation& bitrate) override; |
sprang | 1a646ee | 2016-12-01 06:34:11 -0800 | [diff] [blame] | 134 | |
Stefan Holmer | 64be7fa | 2018-10-04 15:21:55 +0200 | [diff] [blame] | 135 | void OnTransportOverheadChanged( |
| 136 | size_t transport_overhead_bytes_per_packet) override; |
| 137 | // Implements OverheadObserver. |
| 138 | void OnOverheadChanged(size_t overhead_bytes_per_packet) override; |
Sebastian Jansson | 82ed2e8 | 2019-10-15 15:58:37 +0200 | [diff] [blame] | 139 | void OnBitrateUpdated(BitrateAllocationUpdate update, int framerate) override; |
Stefan Holmer | 64be7fa | 2018-10-04 15:21:55 +0200 | [diff] [blame] | 140 | uint32_t GetPayloadBitrateBps() const override; |
| 141 | uint32_t GetProtectionBitrateBps() const override; |
| 142 | void SetEncodingData(size_t width, |
| 143 | size_t height, |
| 144 | size_t num_temporal_layers) override; |
| 145 | |
Elad Alon | 898395d | 2019-04-10 15:55:00 +0200 | [diff] [blame] | 146 | std::vector<RtpSequenceNumberMap::Info> GetSentRtpPacketInfos( |
Elad Alon | 8b60e8b | 2019-04-08 14:14:05 +0200 | [diff] [blame] | 147 | uint32_t ssrc, |
Elad Alon | 898395d | 2019-04-10 15:55:00 +0200 | [diff] [blame] | 148 | rtc::ArrayView<const uint16_t> sequence_numbers) const override; |
Elad Alon | 8b60e8b | 2019-04-08 14:14:05 +0200 | [diff] [blame] | 149 | |
Sebastian Jansson | f298855 | 2019-10-29 17:18:51 +0100 | [diff] [blame] | 150 | // From StreamFeedbackObserver. |
Stefan Holmer | 64be7fa | 2018-10-04 15:21:55 +0200 | [diff] [blame] | 151 | void OnPacketFeedbackVector( |
Sebastian Jansson | f298855 | 2019-10-29 17:18:51 +0100 | [diff] [blame] | 152 | std::vector<StreamPacketInfo> packet_feedback_vector) override; |
Stefan Holmer | 64be7fa | 2018-10-04 15:21:55 +0200 | [diff] [blame] | 153 | |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 154 | private: |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 155 | void UpdateModuleSendingState() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_); |
Elad Alon | 62ce035 | 2019-05-23 16:58:53 +0200 | [diff] [blame] | 156 | void ConfigureProtection(); |
| 157 | void ConfigureSsrcs(); |
| 158 | void ConfigureRids(); |
Stefan Holmer | 64be7fa | 2018-10-04 15:21:55 +0200 | [diff] [blame] | 159 | bool FecEnabled() const; |
| 160 | bool NackEnabled() const; |
Erik Språng | 482b3ef | 2019-01-08 16:19:11 +0100 | [diff] [blame] | 161 | uint32_t GetPacketizationOverheadRate() const; |
Peter Boström | 8b79b07 | 2016-02-26 16:31:37 +0100 | [diff] [blame] | 162 | |
Stefan Holmer | 64be7fa | 2018-10-04 15:21:55 +0200 | [diff] [blame] | 163 | const bool send_side_bwe_with_overhead_; |
Erik Språng | c12d41b | 2019-01-09 09:55:31 +0100 | [diff] [blame] | 164 | const bool account_for_packetization_overhead_; |
Erik Språng | 845c6aa | 2019-05-29 13:02:24 +0200 | [diff] [blame] | 165 | const bool use_early_loss_detection_; |
Sebastian Jansson | c3eb9fd | 2020-01-29 17:42:52 +0100 | [diff] [blame] | 166 | const bool has_packet_feedback_; |
Stefan Holmer | 64be7fa | 2018-10-04 15:21:55 +0200 | [diff] [blame] | 167 | |
| 168 | // TODO(holmer): Remove crit_ once RtpVideoSender runs on the |
| 169 | // transport task queue. |
pbos | d8de115 | 2016-02-01 09:00:51 -0800 | [diff] [blame] | 170 | rtc::CriticalSection crit_; |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 171 | bool active_ RTC_GUARDED_BY(crit_); |
mflodman@webrtc.org | 7ac374a | 2015-02-20 12:45:40 +0000 | [diff] [blame] | 172 | |
Stefan Holmer | dbdb3a0 | 2018-07-17 16:03:46 +0200 | [diff] [blame] | 173 | ProcessThread* module_process_thread_; |
| 174 | rtc::ThreadChecker module_process_thread_checker_; |
| 175 | std::map<uint32_t, RtpState> suspended_ssrcs_; |
| 176 | |
| 177 | std::unique_ptr<FlexfecSender> flexfec_sender_; |
Elad Alon | 67daf71 | 2019-06-28 18:14:36 +0200 | [diff] [blame] | 178 | |
Erik Språng | 490d76c | 2019-05-07 09:29:15 -0700 | [diff] [blame] | 179 | const std::unique_ptr<FecController> fec_controller_; |
Elad Alon | 67daf71 | 2019-06-28 18:14:36 +0200 | [diff] [blame] | 180 | bool fec_allowed_ RTC_GUARDED_BY(crit_); |
| 181 | |
Niels Möller | 2a15267 | 2018-08-08 12:03:00 +0200 | [diff] [blame] | 182 | // Rtp modules are assumed to be sorted in simulcast index order. |
Niels Möller | 5fe9510 | 2019-03-04 16:49:25 +0100 | [diff] [blame] | 183 | const std::vector<webrtc_internal_rtp_video_sender::RtpStreamSender> |
| 184 | rtp_streams_; |
Stefan Holmer | dbdb3a0 | 2018-07-17 16:03:46 +0200 | [diff] [blame] | 185 | const RtpConfig rtp_config_; |
Erik Språng | 6cf554e | 2019-10-02 20:55:39 +0200 | [diff] [blame] | 186 | const absl::optional<VideoCodecType> codec_type_; |
Stefan Holmer | dbdb3a0 | 2018-07-17 16:03:46 +0200 | [diff] [blame] | 187 | RtpTransportControllerSendInterface* const transport_; |
Per | 83d0910 | 2016-04-15 14:59:13 +0200 | [diff] [blame] | 188 | |
philipel | 25d31ec | 2018-08-08 16:33:01 +0200 | [diff] [blame] | 189 | // When using the generic descriptor we want all simulcast streams to share |
| 190 | // one frame id space (so that the SFU can switch stream without having to |
| 191 | // rewrite the frame id), therefore |shared_frame_id| has to live in a place |
| 192 | // where we are aware of all the different streams. |
| 193 | int64_t shared_frame_id_ = 0; |
Åsa Persson | 4bece9a | 2017-10-06 10:04:04 +0200 | [diff] [blame] | 194 | std::vector<RtpPayloadParams> params_ RTC_GUARDED_BY(crit_); |
| 195 | |
Stefan Holmer | 64be7fa | 2018-10-04 15:21:55 +0200 | [diff] [blame] | 196 | size_t transport_overhead_bytes_per_packet_ RTC_GUARDED_BY(crit_); |
| 197 | size_t overhead_bytes_per_packet_ RTC_GUARDED_BY(crit_); |
| 198 | uint32_t protection_bitrate_bps_; |
| 199 | uint32_t encoder_target_rate_bps_; |
| 200 | |
Stefan Holmer | 64be7fa | 2018-10-04 15:21:55 +0200 | [diff] [blame] | 201 | std::vector<bool> loss_mask_vector_ RTC_GUARDED_BY(crit_); |
| 202 | |
Niels Möller | 949f0fd | 2019-01-29 09:44:24 +0100 | [diff] [blame] | 203 | std::vector<FrameCounts> frame_counts_ RTC_GUARDED_BY(crit_); |
| 204 | FrameCountObserver* const frame_count_observer_; |
| 205 | |
Erik Språng | a922904 | 2019-10-24 12:39:32 +0200 | [diff] [blame] | 206 | // Effectively const map from SSRC to RtpRtcp, for all media SSRCs. |
Erik Språng | 845c6aa | 2019-05-29 13:02:24 +0200 | [diff] [blame] | 207 | // This map is set at construction time and never changed, but it's |
Erik Språng | 490d76c | 2019-05-07 09:29:15 -0700 | [diff] [blame] | 208 | // non-trivial to make it properly const. |
Erik Språng | a922904 | 2019-10-24 12:39:32 +0200 | [diff] [blame] | 209 | std::map<uint32_t, RtpRtcp*> ssrc_to_rtp_module_; |
Erik Språng | 490d76c | 2019-05-07 09:29:15 -0700 | [diff] [blame] | 210 | |
Stefan Holmer | 9416ef8 | 2018-07-19 10:34:38 +0200 | [diff] [blame] | 211 | RTC_DISALLOW_COPY_AND_ASSIGN(RtpVideoSender); |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 212 | }; |
| 213 | |
| 214 | } // namespace webrtc |
| 215 | |
Stefan Holmer | 9416ef8 | 2018-07-19 10:34:38 +0200 | [diff] [blame] | 216 | #endif // CALL_RTP_VIDEO_SENDER_H_ |