blob: 117d681f6c38fde9851fbc37503bc5a0a207ba10 [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ångc06aef22019-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 uint16_t CurrentTransportSequenceNumber() const;
62
nisse05843312017-04-18 23:38:35 -070063 // Called every time there is a new bitrate estimate for a receive channel
64 // group. This call will trigger a new RTCP REMB packet if the bitrate
65 // estimate has decreased or if no RTCP REMB packet has been sent for
66 // a certain time interval.
67 // Implements RtpReceiveBitrateUpdate.
68 void OnReceiveBitrateChanged(const std::vector<uint32_t>& ssrcs,
69 uint32_t bitrate_bps) override;
70
danilchap47085372017-08-10 06:03:57 -070071 // Ensures remote party notified of the receive bitrate limit no larger than
72 // |bitrate_bps|.
Danil Chapovalov1de4b622017-12-13 13:35:10 +010073 void SetMaxDesiredReceiveBitrate(int64_t bitrate_bps);
danilchap47085372017-08-10 06:03:57 -070074
nisse05843312017-04-18 23:38:35 -070075 // Send REMB feedback.
Danil Chapovalov1de4b622017-12-13 13:35:10 +010076 bool SendRemb(int64_t bitrate_bps, const std::vector<uint32_t>& ssrcs);
nisse05843312017-04-18 23:38:35 -070077
Per Kjellanderee153c92019-10-10 16:43:46 +020078 // Sends |packets| in one or more IP packets.
79 bool SendCombinedRtcpPacket(
80 std::vector<std::unique_ptr<rtcp::RtcpPacket>> packets) override;
sprang233bd872015-09-08 13:25:16 -070081
sprang867fb522015-08-03 04:38:41 -070082 private:
Danil Chapovaloveb0edd82017-12-14 16:02:31 +010083 void AddRembModuleCandidate(RtcpFeedbackSenderInterface* candidate_module,
84 bool media_sender)
danilchap56359be2017-09-07 07:53:45 -070085 RTC_EXCLUSIVE_LOCKS_REQUIRED(modules_crit_);
Danil Chapovaloveb0edd82017-12-14 16:02:31 +010086 void MaybeRemoveRembModuleCandidate(
87 RtcpFeedbackSenderInterface* candidate_module,
88 bool media_sender) RTC_EXCLUSIVE_LOCKS_REQUIRED(modules_crit_);
danilchap56359be2017-09-07 07:53:45 -070089 void UnsetActiveRembModule() RTC_EXCLUSIVE_LOCKS_REQUIRED(modules_crit_);
90 void DetermineActiveRembModule() RTC_EXCLUSIVE_LOCKS_REQUIRED(modules_crit_);
Erik Språngc06aef22019-10-17 13:02:27 +020091 void AddSendRtpModuleToMap(RtpRtcp* rtp_module, uint32_t ssrc)
92 RTC_EXCLUSIVE_LOCKS_REQUIRED(modules_crit_);
93 void RemoveSendRtpModuleFromMap(uint32_t ssrc)
Mirko Bonadei999a72a2019-07-12 17:33:46 +000094 RTC_EXCLUSIVE_LOCKS_REQUIRED(modules_crit_);
eladalon822ff2b2017-08-01 06:30:28 -070095
stefanbba9dec2016-02-01 04:39:55 -080096 rtc::CriticalSection modules_crit_;
Erik Språngc06aef22019-10-17 13:02:27 +020097 // Ssrc to RtpRtcp module and iterator into |send_modules_list_|;
98 std::unordered_map<uint32_t,
99 std::pair<RtpRtcp*, std::list<RtpRtcp*>::iterator>>
100 send_modules_map_ RTC_GUARDED_BY(modules_crit_);
101 std::list<RtpRtcp*> send_modules_list_ RTC_GUARDED_BY(modules_crit_);
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200102 // The last module used to send media.
103 RtpRtcp* last_send_module_ RTC_GUARDED_BY(modules_crit_);
Danil Chapovaloveb0edd82017-12-14 16:02:31 +0100104 // Rtcp modules of the rtp receivers.
105 std::vector<RtcpFeedbackSenderInterface*> rtcp_feedback_senders_
106 RTC_GUARDED_BY(modules_crit_);
sprang867fb522015-08-03 04:38:41 -0700107
eladalon822ff2b2017-08-01 06:30:28 -0700108 // TODO(eladalon): remb_crit_ only ever held from one function, and it's not
109 // clear if that function can actually be called from more than one thread.
nisse05843312017-04-18 23:38:35 -0700110 rtc::CriticalSection remb_crit_;
111 // The last time a REMB was sent.
danilchap56359be2017-09-07 07:53:45 -0700112 int64_t last_remb_time_ms_ RTC_GUARDED_BY(remb_crit_);
Danil Chapovalov1de4b622017-12-13 13:35:10 +0100113 int64_t last_send_bitrate_bps_ RTC_GUARDED_BY(remb_crit_);
nisse05843312017-04-18 23:38:35 -0700114 // The last bitrate update.
Danil Chapovalov1de4b622017-12-13 13:35:10 +0100115 int64_t bitrate_bps_ RTC_GUARDED_BY(remb_crit_);
116 int64_t max_bitrate_bps_ RTC_GUARDED_BY(remb_crit_);
nisse05843312017-04-18 23:38:35 -0700117
eladalon822ff2b2017-08-01 06:30:28 -0700118 // Candidates for the REMB module can be RTP sender/receiver modules, with
119 // the sender modules taking precedence.
Danil Chapovaloveb0edd82017-12-14 16:02:31 +0100120 std::vector<RtcpFeedbackSenderInterface*> sender_remb_candidates_
121 RTC_GUARDED_BY(modules_crit_);
122 std::vector<RtcpFeedbackSenderInterface*> receiver_remb_candidates_
123 RTC_GUARDED_BY(modules_crit_);
124 RtcpFeedbackSenderInterface* active_remb_module_
125 RTC_GUARDED_BY(modules_crit_);
eladalon822ff2b2017-08-01 06:30:28 -0700126
Erik Språng13a8e162019-10-21 19:39:57 +0200127 uint64_t transport_seq_ RTC_GUARDED_BY(modules_crit_);
Stefan Holmere5904162015-03-26 11:11:06 +0100128
henrikg3c089d72015-09-16 05:37:44 -0700129 RTC_DISALLOW_COPY_AND_ASSIGN(PacketRouter);
Stefan Holmere5904162015-03-26 11:11:06 +0100130};
131} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200132#endif // MODULES_PACING_PACKET_ROUTER_H_