Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef MODULES_PACING_PACKET_ROUTER_H_ |
| 12 | #define MODULES_PACING_PACKET_ROUTER_H_ |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 13 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 14 | #include <stddef.h> |
| 15 | #include <stdint.h> |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 16 | #include <list> |
Erik Språng | 58ee187 | 2019-06-18 16:20:11 +0200 | [diff] [blame^] | 17 | #include <memory> |
nisse | fdbfdc9 | 2017-03-31 05:44:52 -0700 | [diff] [blame] | 18 | #include <vector> |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 19 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 20 | #include "api/transport/network_types.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 21 | #include "modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h" |
| 22 | #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h" |
Erik Språng | 58ee187 | 2019-06-18 16:20:11 +0200 | [diff] [blame^] | 23 | #include "modules/rtp_rtcp/source/rtp_packet_to_send.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 24 | #include "rtc_base/constructor_magic.h" |
| 25 | #include "rtc_base/critical_section.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 26 | #include "rtc_base/thread_annotations.h" |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 27 | |
| 28 | namespace webrtc { |
| 29 | |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 30 | class RtpRtcp; |
sprang | ebbf8a8 | 2015-09-21 15:11:14 -0700 | [diff] [blame] | 31 | namespace rtcp { |
| 32 | class TransportFeedback; |
| 33 | } // namespace rtcp |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 34 | |
Erik Språng | 58ee187 | 2019-06-18 16:20:11 +0200 | [diff] [blame^] | 35 | // PacketRouter keeps track of rtp send modules to support the pacer. |
nisse | fdbfdc9 | 2017-03-31 05:44:52 -0700 | [diff] [blame] | 36 | // In addition, it handles feedback messages, which are sent on a send |
| 37 | // module if possible (sender report), otherwise on receive module |
| 38 | // (receiver report). For the latter case, we also keep track of the |
| 39 | // receive modules. |
Erik Språng | e794243 | 2019-06-12 13:30:02 +0200 | [diff] [blame] | 40 | class PacketRouter : public TransportSequenceNumberAllocator, |
Danil Chapovalov | 599df85 | 2017-09-25 15:19:35 +0200 | [diff] [blame] | 41 | public RemoteBitrateObserver, |
| 42 | public TransportFeedbackSenderInterface { |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 43 | public: |
| 44 | PacketRouter(); |
nisse | 76e62b0 | 2017-05-31 02:24:52 -0700 | [diff] [blame] | 45 | ~PacketRouter() override; |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 46 | |
eladalon | 822ff2b | 2017-08-01 06:30:28 -0700 | [diff] [blame] | 47 | void AddSendRtpModule(RtpRtcp* rtp_module, bool remb_candidate); |
| 48 | void RemoveSendRtpModule(RtpRtcp* rtp_module); |
eladalon | 822ff2b | 2017-08-01 06:30:28 -0700 | [diff] [blame] | 49 | |
Danil Chapovalov | eb0edd8 | 2017-12-14 16:02:31 +0100 | [diff] [blame] | 50 | void AddReceiveRtpModule(RtcpFeedbackSenderInterface* rtcp_sender, |
| 51 | bool remb_candidate); |
| 52 | void RemoveReceiveRtpModule(RtcpFeedbackSenderInterface* rtcp_sender); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 53 | |
Erik Språng | e794243 | 2019-06-12 13:30:02 +0200 | [diff] [blame] | 54 | virtual RtpPacketSendResult TimeToSendPacket( |
Erik Språng | d287962 | 2019-05-10 08:29:01 -0700 | [diff] [blame] | 55 | uint32_t ssrc, |
| 56 | uint16_t sequence_number, |
| 57 | int64_t capture_timestamp, |
| 58 | bool retransmission, |
Erik Språng | e794243 | 2019-06-12 13:30:02 +0200 | [diff] [blame] | 59 | const PacedPacketInfo& packet_info); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 60 | |
Erik Språng | 58ee187 | 2019-06-18 16:20:11 +0200 | [diff] [blame^] | 61 | virtual void SendPacket(std::unique_ptr<RtpPacketToSend> packet, |
| 62 | const PacedPacketInfo& cluster_info); |
| 63 | |
Erik Språng | e794243 | 2019-06-12 13:30:02 +0200 | [diff] [blame] | 64 | virtual size_t TimeToSendPadding(size_t bytes, |
| 65 | const PacedPacketInfo& packet_info); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 66 | |
sprang | 867fb52 | 2015-08-03 04:38:41 -0700 | [diff] [blame] | 67 | void SetTransportWideSequenceNumber(uint16_t sequence_number); |
sprang | ebbf8a8 | 2015-09-21 15:11:14 -0700 | [diff] [blame] | 68 | uint16_t AllocateSequenceNumber() override; |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 69 | |
nisse | 0584331 | 2017-04-18 23:38:35 -0700 | [diff] [blame] | 70 | // Called every time there is a new bitrate estimate for a receive channel |
| 71 | // group. This call will trigger a new RTCP REMB packet if the bitrate |
| 72 | // estimate has decreased or if no RTCP REMB packet has been sent for |
| 73 | // a certain time interval. |
| 74 | // Implements RtpReceiveBitrateUpdate. |
| 75 | void OnReceiveBitrateChanged(const std::vector<uint32_t>& ssrcs, |
| 76 | uint32_t bitrate_bps) override; |
| 77 | |
danilchap | 4708537 | 2017-08-10 06:03:57 -0700 | [diff] [blame] | 78 | // Ensures remote party notified of the receive bitrate limit no larger than |
| 79 | // |bitrate_bps|. |
Danil Chapovalov | 1de4b62 | 2017-12-13 13:35:10 +0100 | [diff] [blame] | 80 | void SetMaxDesiredReceiveBitrate(int64_t bitrate_bps); |
danilchap | 4708537 | 2017-08-10 06:03:57 -0700 | [diff] [blame] | 81 | |
nisse | 0584331 | 2017-04-18 23:38:35 -0700 | [diff] [blame] | 82 | // Send REMB feedback. |
Danil Chapovalov | 1de4b62 | 2017-12-13 13:35:10 +0100 | [diff] [blame] | 83 | bool SendRemb(int64_t bitrate_bps, const std::vector<uint32_t>& ssrcs); |
nisse | 0584331 | 2017-04-18 23:38:35 -0700 | [diff] [blame] | 84 | |
sprang | 233bd87 | 2015-09-08 13:25:16 -0700 | [diff] [blame] | 85 | // Send transport feedback packet to send-side. |
Danil Chapovalov | 599df85 | 2017-09-25 15:19:35 +0200 | [diff] [blame] | 86 | bool SendTransportFeedback(rtcp::TransportFeedback* packet) override; |
sprang | 233bd87 | 2015-09-08 13:25:16 -0700 | [diff] [blame] | 87 | |
sprang | 867fb52 | 2015-08-03 04:38:41 -0700 | [diff] [blame] | 88 | private: |
Danil Chapovalov | eb0edd8 | 2017-12-14 16:02:31 +0100 | [diff] [blame] | 89 | void AddRembModuleCandidate(RtcpFeedbackSenderInterface* candidate_module, |
| 90 | bool media_sender) |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 91 | RTC_EXCLUSIVE_LOCKS_REQUIRED(modules_crit_); |
Danil Chapovalov | eb0edd8 | 2017-12-14 16:02:31 +0100 | [diff] [blame] | 92 | void MaybeRemoveRembModuleCandidate( |
| 93 | RtcpFeedbackSenderInterface* candidate_module, |
| 94 | bool media_sender) RTC_EXCLUSIVE_LOCKS_REQUIRED(modules_crit_); |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 95 | void UnsetActiveRembModule() RTC_EXCLUSIVE_LOCKS_REQUIRED(modules_crit_); |
| 96 | void DetermineActiveRembModule() RTC_EXCLUSIVE_LOCKS_REQUIRED(modules_crit_); |
eladalon | 822ff2b | 2017-08-01 06:30:28 -0700 | [diff] [blame] | 97 | |
stefan | bba9dec | 2016-02-01 04:39:55 -0800 | [diff] [blame] | 98 | rtc::CriticalSection modules_crit_; |
Danil Chapovalov | eb0edd8 | 2017-12-14 16:02:31 +0100 | [diff] [blame] | 99 | // Rtp and Rtcp modules of the rtp senders. |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 100 | std::list<RtpRtcp*> rtp_send_modules_ RTC_GUARDED_BY(modules_crit_); |
Erik Språng | 8b7ca4a | 2018-05-17 13:43:35 +0200 | [diff] [blame] | 101 | // The last module used to send media. |
| 102 | RtpRtcp* last_send_module_ RTC_GUARDED_BY(modules_crit_); |
Danil Chapovalov | eb0edd8 | 2017-12-14 16:02:31 +0100 | [diff] [blame] | 103 | // Rtcp modules of the rtp receivers. |
| 104 | std::vector<RtcpFeedbackSenderInterface*> rtcp_feedback_senders_ |
| 105 | RTC_GUARDED_BY(modules_crit_); |
sprang | 867fb52 | 2015-08-03 04:38:41 -0700 | [diff] [blame] | 106 | |
eladalon | 822ff2b | 2017-08-01 06:30:28 -0700 | [diff] [blame] | 107 | // TODO(eladalon): remb_crit_ only ever held from one function, and it's not |
| 108 | // clear if that function can actually be called from more than one thread. |
nisse | 0584331 | 2017-04-18 23:38:35 -0700 | [diff] [blame] | 109 | rtc::CriticalSection remb_crit_; |
| 110 | // The last time a REMB was sent. |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 111 | int64_t last_remb_time_ms_ RTC_GUARDED_BY(remb_crit_); |
Danil Chapovalov | 1de4b62 | 2017-12-13 13:35:10 +0100 | [diff] [blame] | 112 | int64_t last_send_bitrate_bps_ RTC_GUARDED_BY(remb_crit_); |
nisse | 0584331 | 2017-04-18 23:38:35 -0700 | [diff] [blame] | 113 | // The last bitrate update. |
Danil Chapovalov | 1de4b62 | 2017-12-13 13:35:10 +0100 | [diff] [blame] | 114 | int64_t bitrate_bps_ RTC_GUARDED_BY(remb_crit_); |
| 115 | int64_t max_bitrate_bps_ RTC_GUARDED_BY(remb_crit_); |
nisse | 0584331 | 2017-04-18 23:38:35 -0700 | [diff] [blame] | 116 | |
eladalon | 822ff2b | 2017-08-01 06:30:28 -0700 | [diff] [blame] | 117 | // Candidates for the REMB module can be RTP sender/receiver modules, with |
| 118 | // the sender modules taking precedence. |
Danil Chapovalov | eb0edd8 | 2017-12-14 16:02:31 +0100 | [diff] [blame] | 119 | std::vector<RtcpFeedbackSenderInterface*> sender_remb_candidates_ |
| 120 | RTC_GUARDED_BY(modules_crit_); |
| 121 | std::vector<RtcpFeedbackSenderInterface*> receiver_remb_candidates_ |
| 122 | RTC_GUARDED_BY(modules_crit_); |
| 123 | RtcpFeedbackSenderInterface* active_remb_module_ |
| 124 | RTC_GUARDED_BY(modules_crit_); |
eladalon | 822ff2b | 2017-08-01 06:30:28 -0700 | [diff] [blame] | 125 | |
pbos | 46ad542 | 2015-12-07 14:29:14 -0800 | [diff] [blame] | 126 | volatile int transport_seq_; |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 127 | |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 128 | RTC_DISALLOW_COPY_AND_ASSIGN(PacketRouter); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 129 | }; |
| 130 | } // namespace webrtc |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 131 | #endif // MODULES_PACING_PACKET_ROUTER_H_ |