/
InputStream.cs
112 lines (98 loc) · 3.68 KB
/
InputStream.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
using System;
using System.Collections.Concurrent;
using System.Threading;
using System.Threading.Tasks;
namespace Discord.Audio.Streams
{
///<summary> Reads the payload from an RTP frame </summary>
public class InputStream : AudioInStream
{
private const int MaxFrames = 100; //1-2 Seconds
private ConcurrentQueue<RTPFrame> _frames;
private SemaphoreSlim _signal;
private ushort _nextSeq;
private uint _nextTimestamp;
private bool _nextMissed;
private bool _hasHeader;
private bool _isDisposed;
public override bool CanRead => !_isDisposed;
public override bool CanSeek => false;
public override bool CanWrite => false;
public override int AvailableFrames => _signal.CurrentCount;
public InputStream()
{
_frames = new ConcurrentQueue<RTPFrame>();
_signal = new SemaphoreSlim(0, MaxFrames);
}
public override bool TryReadFrame(CancellationToken cancelToken, out RTPFrame frame)
{
cancelToken.ThrowIfCancellationRequested();
if (_signal.Wait(0))
{
_frames.TryDequeue(out frame);
return true;
}
frame = default(RTPFrame);
return false;
}
public override async Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancelToken)
{
cancelToken.ThrowIfCancellationRequested();
var frame = await ReadFrameAsync(cancelToken).ConfigureAwait(false);
if (count < frame.Payload.Length)
throw new InvalidOperationException("Buffer is too small.");
Buffer.BlockCopy(frame.Payload, 0, buffer, offset, frame.Payload.Length);
return frame.Payload.Length;
}
public override async Task<RTPFrame> ReadFrameAsync(CancellationToken cancelToken)
{
cancelToken.ThrowIfCancellationRequested();
await _signal.WaitAsync(cancelToken).ConfigureAwait(false);
_frames.TryDequeue(out RTPFrame frame);
return frame;
}
public override void WriteHeader(ushort seq, uint timestamp, bool missed)
{
if (_hasHeader)
throw new InvalidOperationException("Header received with no payload");
_hasHeader = true;
_nextSeq = seq;
_nextTimestamp = timestamp;
_nextMissed = missed;
}
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancelToken)
{
cancelToken.ThrowIfCancellationRequested();
if (_signal.CurrentCount >= MaxFrames) //1-2 seconds
{
_hasHeader = false;
return Task.Delay(0); //Buffer overloaded
}
if (!_hasHeader)
throw new InvalidOperationException("Received payload without an RTP header");
_hasHeader = false;
byte[] payload = new byte[count];
Buffer.BlockCopy(buffer, offset, payload, 0, count);
_frames.Enqueue(new RTPFrame(
sequence: _nextSeq,
timestamp: _nextTimestamp,
missed: _nextMissed,
payload: payload
));
_signal.Release();
return Task.Delay(0);
}
protected override void Dispose(bool isDisposing)
{
if (!_isDisposed)
{
if (isDisposing)
{
_signal?.Dispose();
}
_isDisposed = true;
}
base.Dispose(isDisposing);
}
}
}