blob: ae328283f398ffe09c73577916ee3d5607138302 [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,
40 public RemoteBitrateObserver {
Stefan Holmere5904162015-03-26 11:11:06 +010041 public:
42 PacketRouter();
nisse76e62b02017-05-31 02:24:52 -070043 ~PacketRouter() override;
Stefan Holmere5904162015-03-26 11:11:06 +010044
nissefdbfdc92017-03-31 05:44:52 -070045 // TODO(nisse): Delete, as soon as downstream app is updated.
46 RTC_DEPRECATED void AddRtpModule(RtpRtcp* rtp_module) {
47 AddReceiveRtpModule(rtp_module);
48 }
49 RTC_DEPRECATED void RemoveRtpModule(RtpRtcp* rtp_module) {
50 RemoveReceiveRtpModule(rtp_module);
51 }
nissefdbfdc92017-03-31 05:44:52 -070052
eladalon822ff2b2017-08-01 06:30:28 -070053 void AddSendRtpModule(RtpRtcp* rtp_module, bool remb_candidate);
54 void RemoveSendRtpModule(RtpRtcp* rtp_module);
55 RTC_DEPRECATED void AddSendRtpModule(RtpRtcp* rtp_module) {
56 AddSendRtpModule(rtp_module, true);
57 }
58
59 void AddReceiveRtpModule(RtpRtcp* rtp_module, bool remb_candidate);
nissefdbfdc92017-03-31 05:44:52 -070060 void RemoveReceiveRtpModule(RtpRtcp* rtp_module);
eladalon822ff2b2017-08-01 06:30:28 -070061 RTC_DEPRECATED void AddReceiveRtpModule(RtpRtcp* rtp_module) {
62 AddReceiveRtpModule(rtp_module, true);
63 }
Stefan Holmere5904162015-03-26 11:11:06 +010064
65 // Implements PacedSender::Callback.
66 bool TimeToSendPacket(uint32_t ssrc,
67 uint16_t sequence_number,
68 int64_t capture_timestamp,
philipel29dca2c2016-05-13 11:13:05 +020069 bool retransmission,
philipelc7bf32a2017-02-17 03:59:43 -080070 const PacedPacketInfo& packet_info) override;
Stefan Holmere5904162015-03-26 11:11:06 +010071
philipelc7bf32a2017-02-17 03:59:43 -080072 size_t TimeToSendPadding(size_t bytes,
73 const PacedPacketInfo& packet_info) override;
Stefan Holmere5904162015-03-26 11:11:06 +010074
sprang867fb522015-08-03 04:38:41 -070075 void SetTransportWideSequenceNumber(uint16_t sequence_number);
sprangebbf8a82015-09-21 15:11:14 -070076 uint16_t AllocateSequenceNumber() override;
Stefan Holmere5904162015-03-26 11:11:06 +010077
nisse05843312017-04-18 23:38:35 -070078 // Called every time there is a new bitrate estimate for a receive channel
79 // group. This call will trigger a new RTCP REMB packet if the bitrate
80 // estimate has decreased or if no RTCP REMB packet has been sent for
81 // a certain time interval.
82 // Implements RtpReceiveBitrateUpdate.
83 void OnReceiveBitrateChanged(const std::vector<uint32_t>& ssrcs,
84 uint32_t bitrate_bps) override;
85
danilchap47085372017-08-10 06:03:57 -070086 // Ensures remote party notified of the receive bitrate limit no larger than
87 // |bitrate_bps|.
88 void SetMaxDesiredReceiveBitrate(uint32_t bitrate_bps);
89
nisse05843312017-04-18 23:38:35 -070090 // Send REMB feedback.
91 virtual bool SendRemb(uint32_t bitrate_bps,
92 const std::vector<uint32_t>& ssrcs);
93
sprang233bd872015-09-08 13:25:16 -070094 // Send transport feedback packet to send-side.
nisse05843312017-04-18 23:38:35 -070095 virtual bool SendTransportFeedback(rtcp::TransportFeedback* packet);
sprang233bd872015-09-08 13:25:16 -070096
sprang867fb522015-08-03 04:38:41 -070097 private:
eladalon822ff2b2017-08-01 06:30:28 -070098 void AddRembModuleCandidate(RtpRtcp* candidate_module, bool sender)
danilchap56359be2017-09-07 07:53:45 -070099 RTC_EXCLUSIVE_LOCKS_REQUIRED(modules_crit_);
eladalon822ff2b2017-08-01 06:30:28 -0700100 void MaybeRemoveRembModuleCandidate(RtpRtcp* candidate_module, bool sender)
danilchap56359be2017-09-07 07:53:45 -0700101 RTC_EXCLUSIVE_LOCKS_REQUIRED(modules_crit_);
102 void UnsetActiveRembModule() RTC_EXCLUSIVE_LOCKS_REQUIRED(modules_crit_);
103 void DetermineActiveRembModule() RTC_EXCLUSIVE_LOCKS_REQUIRED(modules_crit_);
eladalon822ff2b2017-08-01 06:30:28 -0700104
erikvargabf5a2fc2017-06-16 05:02:05 -0700105 rtc::RaceChecker pacer_race_;
stefanbba9dec2016-02-01 04:39:55 -0800106 rtc::CriticalSection modules_crit_;
danilchap56359be2017-09-07 07:53:45 -0700107 std::list<RtpRtcp*> rtp_send_modules_ RTC_GUARDED_BY(modules_crit_);
108 std::vector<RtpRtcp*> rtp_receive_modules_ RTC_GUARDED_BY(modules_crit_);
sprang867fb522015-08-03 04:38:41 -0700109
eladalon822ff2b2017-08-01 06:30:28 -0700110 // TODO(eladalon): remb_crit_ only ever held from one function, and it's not
111 // clear if that function can actually be called from more than one thread.
nisse05843312017-04-18 23:38:35 -0700112 rtc::CriticalSection remb_crit_;
113 // The last time a REMB was sent.
danilchap56359be2017-09-07 07:53:45 -0700114 int64_t last_remb_time_ms_ RTC_GUARDED_BY(remb_crit_);
115 uint32_t last_send_bitrate_bps_ RTC_GUARDED_BY(remb_crit_);
nisse05843312017-04-18 23:38:35 -0700116 // The last bitrate update.
danilchap56359be2017-09-07 07:53:45 -0700117 uint32_t bitrate_bps_ RTC_GUARDED_BY(remb_crit_);
118 uint32_t max_bitrate_bps_ RTC_GUARDED_BY(remb_crit_);
nisse05843312017-04-18 23:38:35 -0700119
eladalon822ff2b2017-08-01 06:30:28 -0700120 // Candidates for the REMB module can be RTP sender/receiver modules, with
121 // the sender modules taking precedence.
danilchap56359be2017-09-07 07:53:45 -0700122 std::vector<RtpRtcp*> sender_remb_candidates_ RTC_GUARDED_BY(modules_crit_);
123 std::vector<RtpRtcp*> receiver_remb_candidates_ RTC_GUARDED_BY(modules_crit_);
124 RtpRtcp* active_remb_module_ RTC_GUARDED_BY(modules_crit_);
eladalon822ff2b2017-08-01 06:30:28 -0700125
pbos46ad5422015-12-07 14:29:14 -0800126 volatile int transport_seq_;
Stefan Holmere5904162015-03-26 11:11:06 +0100127
henrikg3c089d72015-09-16 05:37:44 -0700128 RTC_DISALLOW_COPY_AND_ASSIGN(PacketRouter);
Stefan Holmere5904162015-03-26 11:11:06 +0100129};
130} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200131#endif // MODULES_PACING_PACKET_ROUTER_H_