-
Notifications
You must be signed in to change notification settings - Fork 8
/
PacketQueue.cs
254 lines (204 loc) · 7.7 KB
/
PacketQueue.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
namespace MonoGame.Extended.VideoPlayback;
/// <inheritdoc />
/// <summary>
/// A simple packet queue for <see cref="Packet"/>s.
/// The packet order satisfies that: 1. the packet with smaller primary key stays in the front;
/// 2. if two primary keys are equal, secondary keys apply; 3. otherwise, first-in-first-out (FIFO).
/// </summary>
internal sealed class PacketQueue : IEnumerable<Packet>
{
/// <inheritdoc />
/// <summary>
/// Creates a new <see cref="PacketQueue" /> instance using the default comparing method and default initial capacity.
/// </summary>
internal PacketQueue()
: this(DefaultComparison)
{
}
/// <summary>
/// Creates a new <see cref="PacketQueue"/> instance using specified comparing method and default initial capacity.
/// </summary>
/// <param name="comparison">Comparing method.</param>
internal PacketQueue(PacketQueueComparison comparison)
{
Comparison = comparison;
_list = new LinkedList<Packet>();
_comparer = CreateComparer(comparison);
}
/// <summary>
/// Creates a new <see cref="PacketQueue"/> instance using the default comparing method and specified initial capacity.
/// </summary>
/// <param name="capacity">Initial capacity.</param>
internal PacketQueue(int capacity)
: this(DefaultComparison, capacity)
{
}
/// <summary>
/// Creates a new <see cref="PacketQueue"/> instance using the specified comparing method and initial capacity.
/// </summary>
/// <param name="comparison">Comparing method.</param>
/// <param name="capacity">Initial capacity. Not used.</param>
internal PacketQueue(PacketQueueComparison comparison, int capacity)
{
Comparison = comparison;
_list = new LinkedList<Packet>();
_comparer = CreateComparer(comparison);
}
/// <summary>
/// Default comparing method.
/// </summary>
internal const PacketQueueComparison DefaultComparison = PacketQueueComparison.FirstDtsThenPts;
public IEnumerator<Packet> GetEnumerator()
{
return _list.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public override string ToString()
{
var baseString = base.ToString();
return $"{baseString} (Count = {Count.ToString()})";
}
/// <summary>
/// Enqueues a <see cref="Packet"/>.
/// </summary>
/// <param name="packetToInsert">The packet to enqueue.</param>
internal void Enqueue(Packet packetToInsert)
{
var list = _list;
var originalListCount = list.Count;
// If there is nothing in the queue, then welcome our first guest!
if (originalListCount == 0)
{
list.AddLast(packetToInsert);
return;
}
var comparer = _comparer;
var lastPacket = list.First!.Value;
var compareResult = comparer.Compare(packetToInsert, lastPacket);
// If there is only one packet in the queue, we only need to do one comparison.
if (originalListCount == 1)
{
if (compareResult >= 0)
{
list.AddLast(packetToInsert);
}
else
{
list.AddFirst(packetToInsert);
}
return;
}
// Handling boundary situation: the packet is "smaller" than the first packet in this queue.
if (compareResult < 0)
{
list.AddFirst(packetToInsert);
return;
}
// Video streams sometimes contain packets with the same PTS or DTS. That's why there is a "secondary key" and "list of same primary keys".
for (var groupStartNode = list.First; groupStartNode != null;)
{
var groupStartPacket = groupStartNode.Value;
var groupEndNode = groupStartNode;
var groupSize = 1;
// Scan the whole group, mark its start and end.
while (groupEndNode.Next != null && comparer.AreInSameGroup(groupEndNode.Value, groupEndNode.Next.Value))
{
groupEndNode = groupEndNode.Next;
++groupSize;
}
// If the packet belongs to the group, find a place for it to insert.
// Possible locations: before the first item in the group; between two group items; after the whole group.
if (comparer.AreInSameGroup(packetToInsert, groupStartPacket))
{
var currentNode = groupStartNode;
var inserted = false;
for (var i = 0; i < groupSize; ++i)
{
compareResult = comparer.CompareInSameGroup(packetToInsert, currentNode.Value);
Debug.Assert(compareResult != 0);
if (compareResult < 0)
{
list.AddBefore(currentNode, packetToInsert);
inserted = true;
break;
}
currentNode = currentNode.Next;
Debug.Assert(currentNode != null);
}
if (!inserted)
{
list.AddAfter(groupEndNode, packetToInsert);
}
return;
}
else
{
// Otherwise it should fall before (smaller than) or after (larger than) the whole group.
compareResult = comparer.Compare(packetToInsert, groupStartPacket);
Debug.Assert(compareResult != 0);
// If the item is smaller than the whole group, just insert it before the group start.
if (compareResult < 0)
{
list.AddBefore(groupStartNode, packetToInsert);
return;
}
// Otherwise, fall through and move to the next group.
}
groupStartNode = groupEndNode.Next;
}
// Handle boundary situation: the packet is "greater" than all packages in the queue.
list.AddLast(packetToInsert);
}
/// <summary>
/// Dequeues a <see cref="Packet"/> and returns it.
/// </summary>
/// <returns>The packet dequeued.</returns>
/// <exception cref="InvalidOperationException">Thrown when the packet queue is empty.</exception>
internal Packet Dequeue()
{
var firstNode = _list.First;
if (firstNode == null)
{
throw new InvalidOperationException("The packet queue is empty.");
}
var item = firstNode.Value;
_list.RemoveFirst();
return item;
}
/// <summary>
/// Clears the <see cref="PacketQueue"/>.
/// </summary>
internal void Clear()
{
_list.Clear();
}
/// <summary>
/// Gets the number of packets in the <see cref="PacketQueue"/>.
/// </summary>
internal int Count => _list.Count;
/// <summary>
/// The comparing method of this <see cref="PacketQueue"/>.
/// </summary>
internal PacketQueueComparison Comparison { get; }
private static PacketComparer CreateComparer(PacketQueueComparison comparison)
{
switch (comparison)
{
case PacketQueueComparison.FirstDtsThenPts:
return PacketComparer.FirstDtsThenPts;
case PacketQueueComparison.FirstPtsThenDts:
return PacketComparer.FirstPtsThenDts;
default:
throw new ArgumentOutOfRangeException(nameof(comparison), comparison, null);
}
}
private readonly LinkedList<Packet> _list;
private readonly PacketComparer _comparer;
}