blob: 6d394c26eb19ad44645d634641142d3e70676b6e [file] [log] [blame]
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +00001/*
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_PACED_SENDER_H_
12#define WEBRTC_MODULES_PACED_SENDER_H_
13
14#include <list>
15
16#include "webrtc/modules/interface/module.h"
17#include "webrtc/system_wrappers/interface/scoped_ptr.h"
18#include "webrtc/system_wrappers/interface/tick_util.h"
19#include "webrtc/typedefs.h"
20
21namespace webrtc {
22class CriticalSectionWrapper;
23
24class PacedSender : public Module {
25 public:
26 enum Priority {
27 kHighPriority = 0, // Pass through; will be sent immediately.
28 kNormalPriority = 2, // Put in back of the line.
29 kLowPriority = 3, // Put in back of the low priority line.
30 };
31 class Callback {
32 public:
33 // Note: packets sent as a result of a callback should not pass by this
34 // module again.
35 // Called when it's time to send a queued packet.
36 virtual void TimeToSendPacket(uint32_t ssrc, uint16_t sequence_number,
37 int64_t capture_time_ms) = 0;
38 // Called when it's a good time to send a padding data.
39 virtual void TimeToSendPadding(int bytes) = 0;
40 protected:
41 virtual ~Callback() {}
42 };
43 PacedSender(Callback* callback, int target_bitrate_kbps);
44
45 virtual ~PacedSender();
46
47 // Enable/disable pacing.
48 void SetStatus(bool enable);
49
50 // Current total estimated bitrate.
51 void UpdateBitrate(int target_bitrate_kbps);
52
53 // Returns true if we send the packet now, else it will add the packet
54 // information to the queue and call TimeToSendPacket when it's time to send.
55 bool SendPacket(Priority priority, uint32_t ssrc, uint16_t sequence_number,
56 int64_t capture_time_ms, int bytes);
57
58 // Returns the number of milliseconds until the module want a worker thread
59 // to call Process.
60 virtual int32_t TimeUntilNextProcess();
61
62 // Process any pending packets in the queue(s).
63 virtual int32_t Process();
64
65 private:
66 struct Packet {
67 Packet(uint32_t ssrc, uint16_t seq_number, int64_t capture_time_ms,
68 int length_in_bytes)
69 : ssrc_(ssrc),
70 sequence_number_(seq_number),
71 capture_time_ms_(capture_time_ms),
72 bytes_(length_in_bytes) {
73 }
74 uint32_t ssrc_;
75 uint16_t sequence_number_;
76 int64_t capture_time_ms_;
77 int bytes_;
78 };
79 // Checks if next packet in line can be transmitted. Returns true on success.
80 bool GetNextPacket(uint32_t* ssrc, uint16_t* sequence_number,
81 int64_t* capture_time_ms);
82
83 // Updates the number of bytes that can be sent for the next time interval.
84 void UpdateBytesPerInterval(uint32_t delta_time_in_ms);
85
86 // Updates the buffers with the number of bytes that we sent.
87 void UpdateState(int num_bytes);
88
89 Callback* callback_;
90 bool enable_;
91 scoped_ptr<CriticalSectionWrapper> critsect_;
92 int target_bitrate_kbytes_per_s_;
93 int bytes_remaining_interval_;
94 int padding_bytes_remaining_interval_;
95 TickTime time_last_update_;
96 TickTime time_last_send_;
97
98 std::list<Packet> normal_priority_packets_;
99 std::list<Packet> low_priority_packets_;
100};
101} // namespace webrtc
102#endif // WEBRTC_MODULES_PACED_SENDER_H_