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