Line data Source code
1 : /*
2 : * Copyright (c) 2012 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 :
11 : #ifndef WEBRTC_MODULES_PACING_PACED_SENDER_H_
12 : #define WEBRTC_MODULES_PACING_PACED_SENDER_H_
13 :
14 : #include <list>
15 : #include <memory>
16 : #include <set>
17 :
18 : #include "webrtc/base/optional.h"
19 : #include "webrtc/base/thread_annotations.h"
20 : #include "webrtc/modules/include/module.h"
21 : #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h"
22 : #include "webrtc/typedefs.h"
23 :
24 : namespace webrtc {
25 : class AlrDetector;
26 : class BitrateProber;
27 : class Clock;
28 : class CriticalSectionWrapper;
29 : class ProbeClusterCreatedObserver;
30 :
31 : namespace paced_sender {
32 : class IntervalBudget;
33 : struct Packet;
34 : class PacketQueue;
35 : } // namespace paced_sender
36 :
37 : class PacedSender : public Module, public RtpPacketSender {
38 : public:
39 0 : class PacketSender {
40 : public:
41 : // Note: packets sent as a result of a callback should not pass by this
42 : // module again.
43 : // Called when it's time to send a queued packet.
44 : // Returns false if packet cannot be sent.
45 : virtual bool TimeToSendPacket(uint32_t ssrc,
46 : uint16_t sequence_number,
47 : int64_t capture_time_ms,
48 : bool retransmission,
49 : int probe_cluster_id) = 0;
50 : // Called when it's a good time to send a padding data.
51 : // Returns the number of bytes sent.
52 : virtual size_t TimeToSendPadding(size_t bytes, int probe_cluster_id) = 0;
53 :
54 : protected:
55 0 : virtual ~PacketSender() {}
56 : };
57 :
58 : // Expected max pacer delay in ms. If ExpectedQueueTimeMs() is higher than
59 : // this value, the packet producers should wait (eg drop frames rather than
60 : // encoding them). Bitrate sent may temporarily exceed target set by
61 : // UpdateBitrate() so that this limit will be upheld.
62 : static const int64_t kMaxQueueLengthMs;
63 : // Pacing-rate relative to our target send rate.
64 : // Multiplicative factor that is applied to the target bitrate to calculate
65 : // the number of bytes that can be transmitted per interval.
66 : // Increasing this factor will result in lower delays in cases of bitrate
67 : // overshoots from the encoder.
68 : static const float kDefaultPaceMultiplier;
69 :
70 : static const size_t kMinProbePacketSize = 200;
71 :
72 : PacedSender(Clock* clock, PacketSender* packet_sender);
73 :
74 : virtual ~PacedSender();
75 :
76 : virtual void CreateProbeCluster(int bitrate_bps);
77 :
78 : // Temporarily pause all sending.
79 : void Pause();
80 :
81 : // Resume sending packets.
82 : void Resume();
83 :
84 : // Enable bitrate probing. Enabled by default, mostly here to simplify
85 : // testing. Must be called before any packets are being sent to have an
86 : // effect.
87 : void SetProbingEnabled(bool enabled);
88 :
89 : // Sets the estimated capacity of the network. Must be called once before
90 : // packets can be sent.
91 : // |bitrate_bps| is our estimate of what we are allowed to send on average.
92 : // We will pace out bursts of packets at a bitrate of
93 : // |bitrate_bps| * kDefaultPaceMultiplier.
94 : virtual void SetEstimatedBitrate(uint32_t bitrate_bps);
95 :
96 : // Sets the minimum send bitrate and maximum padding bitrate requested by send
97 : // streams.
98 : // |min_send_bitrate_bps| might be higher that the estimated available network
99 : // bitrate and if so, the pacer will send with |min_send_bitrate_bps|.
100 : // |max_padding_bitrate_bps| might be higher than the estimate available
101 : // network bitrate and if so, the pacer will send padding packets to reach
102 : // the min of the estimated available bitrate and |max_padding_bitrate_bps|.
103 : void SetSendBitrateLimits(int min_send_bitrate_bps,
104 : int max_padding_bitrate_bps);
105 :
106 : // Returns true if we send the packet now, else it will add the packet
107 : // information to the queue and call TimeToSendPacket when it's time to send.
108 : void InsertPacket(RtpPacketSender::Priority priority,
109 : uint32_t ssrc,
110 : uint16_t sequence_number,
111 : int64_t capture_time_ms,
112 : size_t bytes,
113 : bool retransmission) override;
114 :
115 : // Returns the time since the oldest queued packet was enqueued.
116 : virtual int64_t QueueInMs() const;
117 :
118 : virtual size_t QueueSizePackets() const;
119 :
120 : // Returns the number of milliseconds it will take to send the current
121 : // packets in the queue, given the current size and bitrate, ignoring prio.
122 : virtual int64_t ExpectedQueueTimeMs() const;
123 :
124 : // Returns time in milliseconds when the current application-limited region
125 : // started or empty result if the sender is currently not application-limited.
126 : //
127 : // Application Limited Region (ALR) refers to operating in a state where the
128 : // traffic on network is limited due to application not having enough
129 : // traffic to meet the current channel capacity.
130 : virtual rtc::Optional<int64_t> GetApplicationLimitedRegionStartTime() const;
131 :
132 : // Returns the average time since being enqueued, in milliseconds, for all
133 : // packets currently in the pacer queue, or 0 if queue is empty.
134 : virtual int64_t AverageQueueTimeMs();
135 :
136 : // Returns the number of milliseconds until the module want a worker thread
137 : // to call Process.
138 : int64_t TimeUntilNextProcess() override;
139 :
140 : // Process any pending packets in the queue(s).
141 : void Process() override;
142 :
143 : private:
144 : // Updates the number of bytes that can be sent for the next time interval.
145 : void UpdateBudgetWithElapsedTime(int64_t delta_time_in_ms)
146 : EXCLUSIVE_LOCKS_REQUIRED(critsect_);
147 : void UpdateBudgetWithBytesSent(size_t bytes)
148 : EXCLUSIVE_LOCKS_REQUIRED(critsect_);
149 :
150 : bool SendPacket(const paced_sender::Packet& packet, int probe_cluster_id)
151 : EXCLUSIVE_LOCKS_REQUIRED(critsect_);
152 : size_t SendPadding(size_t padding_needed, int probe_cluster_id)
153 : EXCLUSIVE_LOCKS_REQUIRED(critsect_);
154 :
155 : Clock* const clock_;
156 : PacketSender* const packet_sender_;
157 : std::unique_ptr<AlrDetector> alr_detector_ GUARDED_BY(critsect_);
158 :
159 : std::unique_ptr<CriticalSectionWrapper> critsect_;
160 : bool paused_ GUARDED_BY(critsect_);
161 : // This is the media budget, keeping track of how many bits of media
162 : // we can pace out during the current interval.
163 : std::unique_ptr<paced_sender::IntervalBudget> media_budget_
164 : GUARDED_BY(critsect_);
165 : // This is the padding budget, keeping track of how many bits of padding we're
166 : // allowed to send out during the current interval. This budget will be
167 : // utilized when there's no media to send.
168 : std::unique_ptr<paced_sender::IntervalBudget> padding_budget_
169 : GUARDED_BY(critsect_);
170 :
171 : std::unique_ptr<BitrateProber> prober_ GUARDED_BY(critsect_);
172 : // Actual configured bitrates (media_budget_ may temporarily be higher in
173 : // order to meet pace time constraint).
174 : uint32_t estimated_bitrate_bps_ GUARDED_BY(critsect_);
175 : uint32_t min_send_bitrate_kbps_ GUARDED_BY(critsect_);
176 : uint32_t max_padding_bitrate_kbps_ GUARDED_BY(critsect_);
177 : uint32_t pacing_bitrate_kbps_ GUARDED_BY(critsect_);
178 :
179 : int64_t time_last_update_us_ GUARDED_BY(critsect_);
180 :
181 : std::unique_ptr<paced_sender::PacketQueue> packets_ GUARDED_BY(critsect_);
182 : uint64_t packet_counter_;
183 : };
184 : } // namespace webrtc
185 : #endif // WEBRTC_MODULES_PACING_PACED_SENDER_H_
|