blob: 5e970fb16dd22da50f83777d866b24c5fa06ddc8 [file] [log] [blame]
philipel9981bd92017-09-26 17:16:06 +02001/*
2 * Copyright (c) 2017 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 MODULES_PACING_PACKET_QUEUE_H_
12#define MODULES_PACING_PACKET_QUEUE_H_
13
14#include <list>
philipel9981bd92017-09-26 17:16:06 +020015#include <queue>
philipel9981bd92017-09-26 17:16:06 +020016#include <vector>
17
18#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
19
20namespace webrtc {
21
22class PacketQueue {
23 public:
24 explicit PacketQueue(const Clock* clock);
25 virtual ~PacketQueue();
26
27 struct Packet {
28 Packet(RtpPacketSender::Priority priority,
29 uint32_t ssrc,
30 uint16_t seq_number,
31 int64_t capture_time_ms,
32 int64_t enqueue_time_ms,
33 size_t length_in_bytes,
34 bool retransmission,
35 uint64_t enqueue_order);
36
37 virtual ~Packet();
38
39 RtpPacketSender::Priority priority;
40 uint32_t ssrc;
41 uint16_t sequence_number;
42 int64_t capture_time_ms; // Absolute time of frame capture.
43 int64_t enqueue_time_ms; // Absolute time of pacer queue entry.
44 int64_t sum_paused_ms; // Sum of time spent in queue while pacer is paused.
45 size_t bytes;
46 bool retransmission;
47 uint64_t enqueue_order;
48 std::list<Packet>::iterator this_it;
49 };
50
51 void Push(const Packet& packet);
52 const Packet& BeginPop();
53 void CancelPop(const Packet& packet);
54 void FinalizePop(const Packet& packet);
55 bool Empty() const;
56 size_t SizeInPackets() const;
57 uint64_t SizeInBytes() const;
58 int64_t OldestEnqueueTimeMs() const;
59 void UpdateQueueTime(int64_t timestamp_ms);
60 void SetPauseState(bool paused, int64_t timestamp_ms);
61 int64_t AverageQueueTimeMs() const;
62
63 private:
64 // Try to add a packet to the set of ssrc/seqno identifiers currently in the
65 // queue. Return true if inserted, false if this is a duplicate.
66 bool AddToDupeSet(const Packet& packet);
67
68 void RemoveFromDupeSet(const Packet& packet);
69
70 // Used by priority queue to sort packets.
71 struct Comparator {
72 bool operator()(const Packet* first, const Packet* second) {
73 // Highest prio = 0.
74 if (first->priority != second->priority)
75 return first->priority > second->priority;
76
77 // Retransmissions go first.
78 if (second->retransmission != first->retransmission)
79 return second->retransmission;
80
81 // Older frames have higher prio.
82 if (first->capture_time_ms != second->capture_time_ms)
83 return first->capture_time_ms > second->capture_time_ms;
84
85 return first->enqueue_order > second->enqueue_order;
86 }
87 };
88
89 // List of packets, in the order the were enqueued. Since dequeueing may
90 // occur out of order, use list instead of vector.
91 std::list<Packet> packet_list_;
92 // Priority queue of the packets, sorted according to Comparator.
93 // Use pointers into list, to avodi moving whole struct within heap.
94 std::priority_queue<Packet*, std::vector<Packet*>, Comparator> prio_queue_;
95 // Total number of bytes in the queue.
96 uint64_t bytes_;
philipel9981bd92017-09-26 17:16:06 +020097 const Clock* const clock_;
98 int64_t queue_time_sum_;
99 int64_t time_last_updated_;
100 bool paused_;
101};
102} // namespace webrtc
103
104#endif // MODULES_PACING_PACKET_QUEUE_H_