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 | |
Sebastian Jansson | b537496 | 2018-02-07 13:26:38 +0100 | [diff] [blame^] | 19 | #include "modules/pacing/packet_queue_interface.h" |
philipel | 9981bd9 | 2017-09-26 17:16:06 +0200 | [diff] [blame] | 20 | #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h" |
| 21 | |
| 22 | namespace webrtc { |
| 23 | |
Sebastian Jansson | b537496 | 2018-02-07 13:26:38 +0100 | [diff] [blame^] | 24 | class PacketQueue : public PacketQueueInterface { |
philipel | 9981bd9 | 2017-09-26 17:16:06 +0200 | [diff] [blame] | 25 | public: |
| 26 | explicit PacketQueue(const Clock* clock); |
Sebastian Jansson | b537496 | 2018-02-07 13:26:38 +0100 | [diff] [blame^] | 27 | ~PacketQueue() override; |
philipel | 9981bd9 | 2017-09-26 17:16:06 +0200 | [diff] [blame] | 28 | |
Sebastian Jansson | b537496 | 2018-02-07 13:26:38 +0100 | [diff] [blame^] | 29 | using Packet = PacketQueueInterface::Packet; |
philipel | 9981bd9 | 2017-09-26 17:16:06 +0200 | [diff] [blame] | 30 | |
Sebastian Jansson | b537496 | 2018-02-07 13:26:38 +0100 | [diff] [blame^] | 31 | 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; |
philipel | 9981bd9 | 2017-09-26 17:16:06 +0200 | [diff] [blame] | 42 | |
| 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_; |
philipel | 9981bd9 | 2017-09-26 17:16:06 +0200 | [diff] [blame] | 77 | 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_ |