blob: 77d46d77172757e85793e9fbe209ad734c79a329 [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
14#include <list>
nissefdbfdc92017-03-31 05:44:52 -070015#include <vector>
Stefan Holmere5904162015-03-26 11:11:06 +010016
Mirko Bonadei71207422017-09-15 13:58:09 +020017#include "common_types.h" // NOLINT(build/include)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "modules/pacing/paced_sender.h"
19#include "modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
20#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
21#include "rtc_base/constructormagic.h"
22#include "rtc_base/criticalsection.h"
23#include "rtc_base/race_checker.h"
24#include "rtc_base/thread_annotations.h"
Stefan Holmere5904162015-03-26 11:11:06 +010025
26namespace webrtc {
27
Stefan Holmere5904162015-03-26 11:11:06 +010028class RtpRtcp;
sprangebbf8a82015-09-21 15:11:14 -070029namespace rtcp {
30class TransportFeedback;
31} // namespace rtcp
Stefan Holmere5904162015-03-26 11:11:06 +010032
nissefdbfdc92017-03-31 05:44:52 -070033// PacketRouter keeps track of rtp send modules to support the pacer.
34// In addition, it handles feedback messages, which are sent on a send
35// module if possible (sender report), otherwise on receive module
36// (receiver report). For the latter case, we also keep track of the
37// receive modules.
perkjec81bcd2016-05-11 06:01:13 -070038class PacketRouter : public PacedSender::PacketSender,
nisse05843312017-04-18 23:38:35 -070039 public TransportSequenceNumberAllocator,
Danil Chapovalov599df852017-09-25 15:19:35 +020040 public RemoteBitrateObserver,
41 public TransportFeedbackSenderInterface {
Stefan Holmere5904162015-03-26 11:11:06 +010042 public:
43 PacketRouter();
nisse76e62b02017-05-31 02:24:52 -070044 ~PacketRouter() override;
Stefan Holmere5904162015-03-26 11:11:06 +010045
eladalon822ff2b2017-08-01 06:30:28 -070046 void AddSendRtpModule(RtpRtcp* rtp_module, bool remb_candidate);
47 void RemoveSendRtpModule(RtpRtcp* rtp_module);
eladalon822ff2b2017-08-01 06:30:28 -070048
49 void AddReceiveRtpModule(RtpRtcp* rtp_module, bool remb_candidate);
nissefdbfdc92017-03-31 05:44:52 -070050 void RemoveReceiveRtpModule(RtpRtcp* rtp_module);
Stefan Holmere5904162015-03-26 11:11:06 +010051
52 // Implements PacedSender::Callback.
53 bool TimeToSendPacket(uint32_t ssrc,
54 uint16_t sequence_number,
55 int64_t capture_timestamp,
philipel29dca2c2016-05-13 11:13:05 +020056 bool retransmission,
philipelc7bf32a2017-02-17 03:59:43 -080057 const PacedPacketInfo& packet_info) override;
Stefan Holmere5904162015-03-26 11:11:06 +010058
philipelc7bf32a2017-02-17 03:59:43 -080059 size_t TimeToSendPadding(size_t bytes,
60 const PacedPacketInfo& packet_info) override;
Stefan Holmere5904162015-03-26 11:11:06 +010061
sprang867fb522015-08-03 04:38:41 -070062 void SetTransportWideSequenceNumber(uint16_t sequence_number);
sprangebbf8a82015-09-21 15:11:14 -070063 uint16_t AllocateSequenceNumber() override;
Stefan Holmere5904162015-03-26 11:11:06 +010064
nisse05843312017-04-18 23:38:35 -070065 // Called every time there is a new bitrate estimate for a receive channel
66 // group. This call will trigger a new RTCP REMB packet if the bitrate
67 // estimate has decreased or if no RTCP REMB packet has been sent for
68 // a certain time interval.
69 // Implements RtpReceiveBitrateUpdate.
70 void OnReceiveBitrateChanged(const std::vector<uint32_t>& ssrcs,
71 uint32_t bitrate_bps) override;
72
danilchap47085372017-08-10 06:03:57 -070073 // Ensures remote party notified of the receive bitrate limit no larger than
74 // |bitrate_bps|.
Danil Chapovalov1de4b622017-12-13 13:35:10 +010075 void SetMaxDesiredReceiveBitrate(int64_t bitrate_bps);
danilchap47085372017-08-10 06:03:57 -070076
nisse05843312017-04-18 23:38:35 -070077 // Send REMB feedback.
Danil Chapovalov1de4b622017-12-13 13:35:10 +010078 bool SendRemb(int64_t bitrate_bps, const std::vector<uint32_t>& ssrcs);
nisse05843312017-04-18 23:38:35 -070079
sprang233bd872015-09-08 13:25:16 -070080 // Send transport feedback packet to send-side.
Danil Chapovalov599df852017-09-25 15:19:35 +020081 bool SendTransportFeedback(rtcp::TransportFeedback* packet) override;
sprang233bd872015-09-08 13:25:16 -070082
sprang867fb522015-08-03 04:38:41 -070083 private:
eladalon822ff2b2017-08-01 06:30:28 -070084 void AddRembModuleCandidate(RtpRtcp* candidate_module, bool sender)
danilchap56359be2017-09-07 07:53:45 -070085 RTC_EXCLUSIVE_LOCKS_REQUIRED(modules_crit_);
eladalon822ff2b2017-08-01 06:30:28 -070086 void MaybeRemoveRembModuleCandidate(RtpRtcp* candidate_module, bool sender)
danilchap56359be2017-09-07 07:53:45 -070087 RTC_EXCLUSIVE_LOCKS_REQUIRED(modules_crit_);
88 void UnsetActiveRembModule() RTC_EXCLUSIVE_LOCKS_REQUIRED(modules_crit_);
89 void DetermineActiveRembModule() RTC_EXCLUSIVE_LOCKS_REQUIRED(modules_crit_);
eladalon822ff2b2017-08-01 06:30:28 -070090
erikvargabf5a2fc2017-06-16 05:02:05 -070091 rtc::RaceChecker pacer_race_;
stefanbba9dec2016-02-01 04:39:55 -080092 rtc::CriticalSection modules_crit_;
danilchap56359be2017-09-07 07:53:45 -070093 std::list<RtpRtcp*> rtp_send_modules_ RTC_GUARDED_BY(modules_crit_);
94 std::vector<RtpRtcp*> rtp_receive_modules_ RTC_GUARDED_BY(modules_crit_);
sprang867fb522015-08-03 04:38:41 -070095
eladalon822ff2b2017-08-01 06:30:28 -070096 // TODO(eladalon): remb_crit_ only ever held from one function, and it's not
97 // clear if that function can actually be called from more than one thread.
nisse05843312017-04-18 23:38:35 -070098 rtc::CriticalSection remb_crit_;
99 // The last time a REMB was sent.
danilchap56359be2017-09-07 07:53:45 -0700100 int64_t last_remb_time_ms_ RTC_GUARDED_BY(remb_crit_);
Danil Chapovalov1de4b622017-12-13 13:35:10 +0100101 int64_t last_send_bitrate_bps_ RTC_GUARDED_BY(remb_crit_);
nisse05843312017-04-18 23:38:35 -0700102 // The last bitrate update.
Danil Chapovalov1de4b622017-12-13 13:35:10 +0100103 int64_t bitrate_bps_ RTC_GUARDED_BY(remb_crit_);
104 int64_t max_bitrate_bps_ RTC_GUARDED_BY(remb_crit_);
nisse05843312017-04-18 23:38:35 -0700105
eladalon822ff2b2017-08-01 06:30:28 -0700106 // Candidates for the REMB module can be RTP sender/receiver modules, with
107 // the sender modules taking precedence.
danilchap56359be2017-09-07 07:53:45 -0700108 std::vector<RtpRtcp*> sender_remb_candidates_ RTC_GUARDED_BY(modules_crit_);
109 std::vector<RtpRtcp*> receiver_remb_candidates_ RTC_GUARDED_BY(modules_crit_);
110 RtpRtcp* active_remb_module_ RTC_GUARDED_BY(modules_crit_);
eladalon822ff2b2017-08-01 06:30:28 -0700111
pbos46ad5422015-12-07 14:29:14 -0800112 volatile int transport_seq_;
Stefan Holmere5904162015-03-26 11:11:06 +0100113
henrikg3c089d72015-09-16 05:37:44 -0700114 RTC_DISALLOW_COPY_AND_ASSIGN(PacketRouter);
Stefan Holmere5904162015-03-26 11:11:06 +0100115};
116} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200117#endif // MODULES_PACING_PACKET_ROUTER_H_