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