blob: e5abdeb2ab18e2dec47ec4734aa44720bc4f65a1 [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
Henrik Kjellander0b9e29c2015-11-16 11:12:24 +010011#ifndef WEBRTC_MODULES_PACING_PACKET_ROUTER_H_
12#define WEBRTC_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
Stefan Holmere5904162015-03-26 11:11:06 +010017#include "webrtc/common_types.h"
Henrik Kjellander0b9e29c2015-11-16 11:12:24 +010018#include "webrtc/modules/pacing/paced_sender.h"
nisse05843312017-04-18 23:38:35 -070019#include "webrtc/modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010020#include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020021#include "webrtc/rtc_base/constructormagic.h"
22#include "webrtc/rtc_base/criticalsection.h"
23#include "webrtc/rtc_base/race_checker.h"
24#include "webrtc/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
86 // Send REMB feedback.
87 virtual bool SendRemb(uint32_t bitrate_bps,
88 const std::vector<uint32_t>& ssrcs);
89
sprang233bd872015-09-08 13:25:16 -070090 // Send transport feedback packet to send-side.
nisse05843312017-04-18 23:38:35 -070091 virtual bool SendTransportFeedback(rtcp::TransportFeedback* packet);
sprang233bd872015-09-08 13:25:16 -070092
sprang867fb522015-08-03 04:38:41 -070093 private:
eladalon822ff2b2017-08-01 06:30:28 -070094 void AddRembModuleCandidate(RtpRtcp* candidate_module, bool sender)
95 EXCLUSIVE_LOCKS_REQUIRED(modules_crit_);
96 void MaybeRemoveRembModuleCandidate(RtpRtcp* candidate_module, bool sender)
97 EXCLUSIVE_LOCKS_REQUIRED(modules_crit_);
98 void UnsetActiveRembModule() EXCLUSIVE_LOCKS_REQUIRED(modules_crit_);
99 void DetermineActiveRembModule() EXCLUSIVE_LOCKS_REQUIRED(modules_crit_);
100
erikvargabf5a2fc2017-06-16 05:02:05 -0700101 rtc::RaceChecker pacer_race_;
stefanbba9dec2016-02-01 04:39:55 -0800102 rtc::CriticalSection modules_crit_;
nissefdbfdc92017-03-31 05:44:52 -0700103 std::list<RtpRtcp*> rtp_send_modules_ GUARDED_BY(modules_crit_);
104 std::vector<RtpRtcp*> rtp_receive_modules_ GUARDED_BY(modules_crit_);
sprang867fb522015-08-03 04:38:41 -0700105
eladalon822ff2b2017-08-01 06:30:28 -0700106 // TODO(eladalon): remb_crit_ only ever held from one function, and it's not
107 // clear if that function can actually be called from more than one thread.
nisse05843312017-04-18 23:38:35 -0700108 rtc::CriticalSection remb_crit_;
109 // The last time a REMB was sent.
110 int64_t last_remb_time_ms_ GUARDED_BY(remb_crit_);
111 uint32_t last_send_bitrate_bps_ GUARDED_BY(remb_crit_);
112 // The last bitrate update.
113 uint32_t bitrate_bps_ GUARDED_BY(remb_crit_);
114
eladalon822ff2b2017-08-01 06:30:28 -0700115 // Candidates for the REMB module can be RTP sender/receiver modules, with
116 // the sender modules taking precedence.
117 std::vector<RtpRtcp*> sender_remb_candidates_ GUARDED_BY(modules_crit_);
118 std::vector<RtpRtcp*> receiver_remb_candidates_ GUARDED_BY(modules_crit_);
119 RtpRtcp* active_remb_module_ GUARDED_BY(modules_crit_);
120
pbos46ad5422015-12-07 14:29:14 -0800121 volatile int transport_seq_;
Stefan Holmere5904162015-03-26 11:11:06 +0100122
henrikg3c089d72015-09-16 05:37:44 -0700123 RTC_DISALLOW_COPY_AND_ASSIGN(PacketRouter);
Stefan Holmere5904162015-03-26 11:11:06 +0100124};
125} // namespace webrtc
Henrik Kjellander0b9e29c2015-11-16 11:12:24 +0100126#endif // WEBRTC_MODULES_PACING_PACKET_ROUTER_H_