blob: 8041e080578e7d51884a76c381d0cddec3849b77 [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
Sebastian Janssonb5374962018-02-07 13:26:38 +010019#include "modules/pacing/packet_queue_interface.h"
philipel9981bd92017-09-26 17:16:06 +020020#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
21
22namespace webrtc {
23
Sebastian Janssonb5374962018-02-07 13:26:38 +010024class PacketQueue : public PacketQueueInterface {
philipel9981bd92017-09-26 17:16:06 +020025 public:
26 explicit PacketQueue(const Clock* clock);
Sebastian Janssonb5374962018-02-07 13:26:38 +010027 ~PacketQueue() override;
philipel9981bd92017-09-26 17:16:06 +020028
Sebastian Janssonb5374962018-02-07 13:26:38 +010029 using Packet = PacketQueueInterface::Packet;
philipel9981bd92017-09-26 17:16:06 +020030
Sebastian Janssonb5374962018-02-07 13:26:38 +010031 void Push(const Packet& packet) override;
32 const Packet& BeginPop() override;
33 void CancelPop(const Packet& packet) override;
34 void FinalizePop(const Packet& packet) override;
35 bool Empty() const override;
36 size_t SizeInPackets() const override;
37 uint64_t SizeInBytes() const override;
38 int64_t OldestEnqueueTimeMs() const override;
39 void UpdateQueueTime(int64_t timestamp_ms) override;
40 void SetPauseState(bool paused, int64_t timestamp_ms) override;
41 int64_t AverageQueueTimeMs() const override;
philipel9981bd92017-09-26 17:16:06 +020042
43 private:
44 // Try to add a packet to the set of ssrc/seqno identifiers currently in the
45 // queue. Return true if inserted, false if this is a duplicate.
46 bool AddToDupeSet(const Packet& packet);
47
48 void RemoveFromDupeSet(const Packet& packet);
49
50 // Used by priority queue to sort packets.
51 struct Comparator {
52 bool operator()(const Packet* first, const Packet* second) {
53 // Highest prio = 0.
54 if (first->priority != second->priority)
55 return first->priority > second->priority;
56
57 // Retransmissions go first.
58 if (second->retransmission != first->retransmission)
59 return second->retransmission;
60
61 // Older frames have higher prio.
62 if (first->capture_time_ms != second->capture_time_ms)
63 return first->capture_time_ms > second->capture_time_ms;
64
65 return first->enqueue_order > second->enqueue_order;
66 }
67 };
68
69 // List of packets, in the order the were enqueued. Since dequeueing may
70 // occur out of order, use list instead of vector.
71 std::list<Packet> packet_list_;
72 // Priority queue of the packets, sorted according to Comparator.
73 // Use pointers into list, to avodi moving whole struct within heap.
74 std::priority_queue<Packet*, std::vector<Packet*>, Comparator> prio_queue_;
75 // Total number of bytes in the queue.
76 uint64_t bytes_;
philipel9981bd92017-09-26 17:16:06 +020077 const Clock* const clock_;
78 int64_t queue_time_sum_;
79 int64_t time_last_updated_;
80 bool paused_;
81};
82} // namespace webrtc
83
84#endif // MODULES_PACING_PACKET_QUEUE_H_