blob: 1359e04332f6461c1a50c61405720ae753420d31 [file] [log] [blame]
Stefan Holmere5904162015-03-26 11:11:06 +01001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_PACING_PACKET_ROUTER_H_
12#define MODULES_PACING_PACKET_ROUTER_H_
Stefan Holmere5904162015-03-26 11:11:06 +010013
Yves Gerey988cc082018-10-23 12:03:01 +020014#include <stddef.h>
15#include <stdint.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020016
Stefan Holmere5904162015-03-26 11:11:06 +010017#include <list>
Erik Språng58ee1872019-06-18 16:20:11 +020018#include <memory>
Danil Chapovalovdb59de32019-06-26 11:06:41 +020019#include <unordered_map>
Erik Språng96f3de02019-10-17 13:02:27 +020020#include <utility>
nissefdbfdc92017-03-31 05:44:52 -070021#include <vector>
Stefan Holmere5904162015-03-26 11:11:06 +010022
Yves Gerey988cc082018-10-23 12:03:01 +020023#include "api/transport/network_types.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
25#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
Per Kjellanderee153c92019-10-10 16:43:46 +020026#include "modules/rtp_rtcp/source/rtcp_packet.h"
Erik Språng58ee1872019-06-18 16:20:11 +020027#include "modules/rtp_rtcp/source/rtp_packet_to_send.h"
Steve Anton10542f22019-01-11 09:11:00 -080028#include "rtc_base/constructor_magic.h"
29#include "rtc_base/critical_section.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020030#include "rtc_base/thread_annotations.h"
Stefan Holmere5904162015-03-26 11:11:06 +010031
32namespace webrtc {
33
Stefan Holmere5904162015-03-26 11:11:06 +010034class RtpRtcp;
Stefan Holmere5904162015-03-26 11:11:06 +010035
Erik Språng58ee1872019-06-18 16:20:11 +020036// PacketRouter keeps track of rtp send modules to support the pacer.
nissefdbfdc92017-03-31 05:44:52 -070037// 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ångfac7e312019-08-27 16:47:00 +020041class PacketRouter : public RemoteBitrateObserver,
Danil Chapovalov599df852017-09-25 15:19:35 +020042 public TransportFeedbackSenderInterface {
Stefan Holmere5904162015-03-26 11:11:06 +010043 public:
44 PacketRouter();
Erik Språng5f01bf62019-10-16 17:06:34 +020045 explicit PacketRouter(uint16_t start_transport_seq);
nisse76e62b02017-05-31 02:24:52 -070046 ~PacketRouter() override;
Stefan Holmere5904162015-03-26 11:11:06 +010047
eladalon822ff2b2017-08-01 06:30:28 -070048 void AddSendRtpModule(RtpRtcp* rtp_module, bool remb_candidate);
49 void RemoveSendRtpModule(RtpRtcp* rtp_module);
eladalon822ff2b2017-08-01 06:30:28 -070050
Danil Chapovaloveb0edd82017-12-14 16:02:31 +010051 void AddReceiveRtpModule(RtcpFeedbackSenderInterface* rtcp_sender,
52 bool remb_candidate);
53 void RemoveReceiveRtpModule(RtcpFeedbackSenderInterface* rtcp_sender);
Stefan Holmere5904162015-03-26 11:11:06 +010054
Erik Språng58ee1872019-06-18 16:20:11 +020055 virtual void SendPacket(std::unique_ptr<RtpPacketToSend> packet,
56 const PacedPacketInfo& cluster_info);
57
Erik Språngf6468d22019-07-05 16:53:43 +020058 virtual std::vector<std::unique_ptr<RtpPacketToSend>> GeneratePadding(
59 size_t target_size_bytes);
Erik Språng478cb462019-06-26 15:49:27 +020060
Erik Språng5f01bf62019-10-16 17:06:34 +020061 // TODO(bugs.webrtc.org/11036): Remove when downstream usage is gone.
sprang867fb522015-08-03 04:38:41 -070062 void SetTransportWideSequenceNumber(uint16_t sequence_number);
Erik Språng5f01bf62019-10-16 17:06:34 +020063 // TODO(bugs.webrtc.org/11036): Make private when downstream usage is gone.
Erik Språngfac7e312019-08-27 16:47:00 +020064 uint16_t AllocateSequenceNumber();
Stefan Holmere5904162015-03-26 11:11:06 +010065
Erik Språng5f01bf62019-10-16 17:06:34 +020066 uint16_t CurrentTransportSequenceNumber() const;
67
nisse05843312017-04-18 23:38:35 -070068 // Called every time there is a new bitrate estimate for a receive channel
69 // group. This call will trigger a new RTCP REMB packet if the bitrate
70 // estimate has decreased or if no RTCP REMB packet has been sent for
71 // a certain time interval.
72 // Implements RtpReceiveBitrateUpdate.
73 void OnReceiveBitrateChanged(const std::vector<uint32_t>& ssrcs,
74 uint32_t bitrate_bps) override;
75
danilchap47085372017-08-10 06:03:57 -070076 // Ensures remote party notified of the receive bitrate limit no larger than
77 // |bitrate_bps|.
Danil Chapovalov1de4b622017-12-13 13:35:10 +010078 void SetMaxDesiredReceiveBitrate(int64_t bitrate_bps);
danilchap47085372017-08-10 06:03:57 -070079
nisse05843312017-04-18 23:38:35 -070080 // Send REMB feedback.
Danil Chapovalov1de4b622017-12-13 13:35:10 +010081 bool SendRemb(int64_t bitrate_bps, const std::vector<uint32_t>& ssrcs);
nisse05843312017-04-18 23:38:35 -070082
Per Kjellanderee153c92019-10-10 16:43:46 +020083 // Sends |packets| in one or more IP packets.
84 bool SendCombinedRtcpPacket(
85 std::vector<std::unique_ptr<rtcp::RtcpPacket>> packets) override;
sprang233bd872015-09-08 13:25:16 -070086
sprang867fb522015-08-03 04:38:41 -070087 private:
Danil Chapovaloveb0edd82017-12-14 16:02:31 +010088 void AddRembModuleCandidate(RtcpFeedbackSenderInterface* candidate_module,
89 bool media_sender)
danilchap56359be2017-09-07 07:53:45 -070090 RTC_EXCLUSIVE_LOCKS_REQUIRED(modules_crit_);
Danil Chapovaloveb0edd82017-12-14 16:02:31 +010091 void MaybeRemoveRembModuleCandidate(
92 RtcpFeedbackSenderInterface* candidate_module,
93 bool media_sender) RTC_EXCLUSIVE_LOCKS_REQUIRED(modules_crit_);
danilchap56359be2017-09-07 07:53:45 -070094 void UnsetActiveRembModule() RTC_EXCLUSIVE_LOCKS_REQUIRED(modules_crit_);
95 void DetermineActiveRembModule() RTC_EXCLUSIVE_LOCKS_REQUIRED(modules_crit_);
Erik Språng96f3de02019-10-17 13:02:27 +020096 void AddSendRtpModuleToMap(RtpRtcp* rtp_module, uint32_t ssrc)
97 RTC_EXCLUSIVE_LOCKS_REQUIRED(modules_crit_);
98 void RemoveSendRtpModuleFromMap(uint32_t ssrc)
Mirko Bonadei999a72a2019-07-12 17:33:46 +000099 RTC_EXCLUSIVE_LOCKS_REQUIRED(modules_crit_);
eladalon822ff2b2017-08-01 06:30:28 -0700100
stefanbba9dec2016-02-01 04:39:55 -0800101 rtc::CriticalSection modules_crit_;
Erik Språng96f3de02019-10-17 13:02:27 +0200102 // Ssrc to RtpRtcp module and iterator into |send_modules_list_|;
103 std::unordered_map<uint32_t,
104 std::pair<RtpRtcp*, std::list<RtpRtcp*>::iterator>>
105 send_modules_map_ RTC_GUARDED_BY(modules_crit_);
106 std::list<RtpRtcp*> send_modules_list_ RTC_GUARDED_BY(modules_crit_);
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200107 // The last module used to send media.
108 RtpRtcp* last_send_module_ RTC_GUARDED_BY(modules_crit_);
Danil Chapovaloveb0edd82017-12-14 16:02:31 +0100109 // Rtcp modules of the rtp receivers.
110 std::vector<RtcpFeedbackSenderInterface*> rtcp_feedback_senders_
111 RTC_GUARDED_BY(modules_crit_);
sprang867fb522015-08-03 04:38:41 -0700112
eladalon822ff2b2017-08-01 06:30:28 -0700113 // TODO(eladalon): remb_crit_ only ever held from one function, and it's not
114 // clear if that function can actually be called from more than one thread.
nisse05843312017-04-18 23:38:35 -0700115 rtc::CriticalSection remb_crit_;
116 // The last time a REMB was sent.
danilchap56359be2017-09-07 07:53:45 -0700117 int64_t last_remb_time_ms_ RTC_GUARDED_BY(remb_crit_);
Danil Chapovalov1de4b622017-12-13 13:35:10 +0100118 int64_t last_send_bitrate_bps_ RTC_GUARDED_BY(remb_crit_);
nisse05843312017-04-18 23:38:35 -0700119 // The last bitrate update.
Danil Chapovalov1de4b622017-12-13 13:35:10 +0100120 int64_t bitrate_bps_ RTC_GUARDED_BY(remb_crit_);
121 int64_t max_bitrate_bps_ RTC_GUARDED_BY(remb_crit_);
nisse05843312017-04-18 23:38:35 -0700122
eladalon822ff2b2017-08-01 06:30:28 -0700123 // Candidates for the REMB module can be RTP sender/receiver modules, with
124 // the sender modules taking precedence.
Danil Chapovaloveb0edd82017-12-14 16:02:31 +0100125 std::vector<RtcpFeedbackSenderInterface*> sender_remb_candidates_
126 RTC_GUARDED_BY(modules_crit_);
127 std::vector<RtcpFeedbackSenderInterface*> receiver_remb_candidates_
128 RTC_GUARDED_BY(modules_crit_);
129 RtcpFeedbackSenderInterface* active_remb_module_
130 RTC_GUARDED_BY(modules_crit_);
eladalon822ff2b2017-08-01 06:30:28 -0700131
Erik Språng5f01bf62019-10-16 17:06:34 +0200132 int transport_seq_ RTC_GUARDED_BY(modules_crit_);
Stefan Holmere5904162015-03-26 11:11:06 +0100133
henrikg3c089d72015-09-16 05:37:44 -0700134 RTC_DISALLOW_COPY_AND_ASSIGN(PacketRouter);
Stefan Holmere5904162015-03-26 11:11:06 +0100135};
136} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200137#endif // MODULES_PACING_PACKET_ROUTER_H_