philipel | 9981bd9 | 2017-09-26 17:16:06 +0200 | [diff] [blame] | 1 | /* |
| 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> |
philipel | 9981bd9 | 2017-09-26 17:16:06 +0200 | [diff] [blame] | 15 | #include <queue> |
philipel | ccdfcca | 2017-10-23 12:42:17 +0200 | [diff] [blame^] | 16 | #include <set> |
philipel | 9981bd9 | 2017-09-26 17:16:06 +0200 | [diff] [blame] | 17 | #include <vector> |
| 18 | |
| 19 | #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h" |
| 20 | |
| 21 | namespace webrtc { |
| 22 | |
| 23 | class 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 | |
philipel | ccdfcca | 2017-10-23 12:42:17 +0200 | [diff] [blame^] | 38 | Packet(const Packet& other); |
| 39 | |
philipel | 9981bd9 | 2017-09-26 17:16:06 +0200 | [diff] [blame] | 40 | virtual ~Packet(); |
| 41 | |
philipel | ccdfcca | 2017-10-23 12:42:17 +0200 | [diff] [blame^] | 42 | 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 | |
philipel | 9981bd9 | 2017-09-26 17:16:06 +0200 | [diff] [blame] | 51 | 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. |
philipel | ccdfcca | 2017-10-23 12:42:17 +0200 | [diff] [blame^] | 56 | int64_t sum_paused_ms; |
philipel | 9981bd9 | 2017-09-26 17:16:06 +0200 | [diff] [blame] | 57 | size_t bytes; |
| 58 | bool retransmission; |
| 59 | uint64_t enqueue_order; |
| 60 | std::list<Packet>::iterator this_it; |
philipel | ccdfcca | 2017-10-23 12:42:17 +0200 | [diff] [blame^] | 61 | std::multiset<int64_t>::iterator enqueue_time_it; |
philipel | 9981bd9 | 2017-09-26 17:16:06 +0200 | [diff] [blame] | 62 | }; |
| 63 | |
philipel | ccdfcca | 2017-10-23 12:42:17 +0200 | [diff] [blame^] | 64 | 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; |
philipel | 9981bd9 | 2017-09-26 17:16:06 +0200 | [diff] [blame] | 75 | |
| 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_; |
philipel | 9981bd9 | 2017-09-26 17:16:06 +0200 | [diff] [blame] | 110 | 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_ |