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