-
Notifications
You must be signed in to change notification settings - Fork 0
/
DefaultRtmpProtocol.cs
368 lines (317 loc) · 13.2 KB
/
DefaultRtmpProtocol.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
namespace System.Net.Rtmp
{
public class DefaultRtmpProtocol : RtmpProtocol
{
private Framer _framer;
private Chunker _chunker;
private volatile bool _isRunning = false;
public override byte ProtocolVersion
{
get => 3;
}
public DefaultRtmpProtocol()
{
_framer = new Framer();
_chunker = new Chunker();
_framer.ChunkReceived += (s, e) =>
{
_chunker.AddChunk(e.Chunk);
};
_chunker.MessageReceived += (s, e) =>
{
this.OnMessageReceived(s, e);
};
}
public override void Start(Stream stream)
{
_isRunning = true;
// TODO: add cancellations
Task.Factory.StartNew(() =>
{
byte[] readBuffer = new byte[1024];
using (BinaryReader reader = new BinaryReader(stream))
//using (StreamWriter writer = new StreamWriter(stream))
{
while (_isRunning)
{
int amtRead = reader.Read(readBuffer, 0, readBuffer.Length);
_framer.AppendData(ref readBuffer, amtRead);
}
}
});
}
public override void Stop()
{
_isRunning = false;
}
private class FramerEventArgs : EventArgs
{
public Chunk Chunk { get; set; }
}
private class Framer
{
public event EventHandler<FramerEventArgs> ChunkReceived;
private byte[] _buffer;
private int _currentInsertIndex;
private int _maxBufferSize;
private Chunk _lastReadChunk;
public Framer(int initialBufferSize = 4 * 1024, int maxAllowedBufferSize = 1 * 1024 * 1024)
{
_buffer = new byte[initialBufferSize];
_currentInsertIndex = 0;
_maxBufferSize = maxAllowedBufferSize;
}
protected virtual void OnChunkReceived(Chunk c)
{
ChunkReceived?.Invoke(this, new FramerEventArgs { Chunk = c });
}
public void AppendData(ref byte[] data, int length)
{
// check to see if the buffer is too small, then resize of necessary
if (length + _currentInsertIndex > _buffer.Length)
{
ResizeBuffer();
}
Array.Copy(data, 0, _buffer, _currentInsertIndex, length);
_currentInsertIndex += length;
Chunk chunk = null;
var amtRead = ReadChunk(out chunk);
_lastReadChunk = chunk;
if (amtRead > 0 && chunk != null)
{
OnChunkReceived(chunk);
}
}
private uint ReadChunk(out Chunk c)
{
// must have at least 3 bytes. In practice you always need > 3
if (_currentInsertIndex < 3)
{
c = null;
return 0;
}
uint amtRead = 0;
c = null;
// read basic header
var chunkHeaderFormat = ((uint)_buffer[0] & 0b11000000) >> 6;
if (chunkHeaderFormat > 3)
{
throw new Exception("BUG: chunk header format is invalid (Code: L83)");
}
var chunkStreamId = (uint)_buffer[0] & 0b00111111;
amtRead = 1;
if (chunkStreamId == 0)
{
// id of 0 is a two byte header
chunkStreamId = ((uint)_buffer[1]) + 64;
amtRead = 2;
}
else if (chunkStreamId == 1)
{
// id of 1 is a three byte header
chunkStreamId = ((uint)_buffer[3] * 256) + _buffer[1] + 64;
amtRead = 3;
}
// NOTE: Chunk Stream ID of 2 is an internal message for the protocol
if (_lastReadChunk == null && (chunkHeaderFormat > 0))
{
throw new Exception("Received a chunk type greater than 0 without received any prior chunks.");
}
c = new Chunk
{
ChunkStreamId = chunkStreamId
};
// read chunk header
if (chunkHeaderFormat == 0)
{
c.Delta = BitConverter.ToUInt32(new byte[] { 0x00, _buffer[amtRead++], _buffer[amtRead++], _buffer[amtRead++] }, 0);
c.Length = BitConverter.ToUInt32(new byte[] { 0x00, _buffer[amtRead++], _buffer[amtRead++], _buffer[amtRead++] }, 0);
c.ChunkType = _buffer[amtRead++];
c.StreamId = BitConverter.ToUInt32(new byte[] { _buffer[amtRead++], _buffer[amtRead++], _buffer[amtRead++], _buffer[amtRead++] }, 0);
}
else if (chunkHeaderFormat == 1)
{
c.Delta = BitConverter.ToUInt32(new byte[] { 0x00, _buffer[amtRead++], _buffer[amtRead++], _buffer[amtRead++] }, 0);
c.Length = BitConverter.ToUInt32(new byte[] { 0x00, _buffer[amtRead++], _buffer[amtRead++], _buffer[amtRead++] }, 0);
c.ChunkType = _buffer[amtRead++];
c.StreamId = _lastReadChunk.StreamId;
}
else if (chunkHeaderFormat == 2)
{
c.Delta = BitConverter.ToUInt32(new byte[] { 0x00, _buffer[amtRead++], _buffer[amtRead++], _buffer[amtRead++] }, 0);
c.ChunkType = _lastReadChunk.ChunkType;
c.Length = _lastReadChunk.Length;
c.StreamId = _lastReadChunk.StreamId;
}
else if (chunkHeaderFormat == 3)
{
c.Delta = _lastReadChunk.Delta;
c.ChunkType = _lastReadChunk.ChunkType;
c.Length = _lastReadChunk.Length;
c.StreamId = _lastReadChunk.StreamId;
}
// Read data
if (c.Length + amtRead >= _currentInsertIndex)
{
c = null;
return 0;
}
byte[] data = new byte[c.Length];
Array.Copy(_buffer, amtRead, data, 0, c.Length);
amtRead += c.Length;
return amtRead;
}
private void ShiftBuffer(int newStartIndex)
{
if (newStartIndex == 0)
{
return;
}
for (int i = 0; newStartIndex + i < _buffer.Length; i++)
{
_buffer[i] = _buffer[newStartIndex + i];
}
_currentInsertIndex = 0;
}
private void ResizeBuffer()
{
if (_buffer.Length >= _maxBufferSize)
{
throw new Exception("Maximum buffer size reached. Either a HUGE 1MB chunk was sent (unlikely), or there is a bug on the remote server.");
}
Array.Resize(ref _buffer, Math.Min(_buffer.Length * 2, _maxBufferSize));
}
}
private class Chunker
{
public event EventHandler<RtmpProtocolEventArgs> MessageReceived;
private IDictionary<int, List<Chunk>> _store;
private uint _lastReceviedTime;
private uint _lastSentTime;
public Chunker()
{
_store = new Dictionary<int, List<Chunk>>();
}
public void AddChunk(Chunk chunk)
{
if (chunk == null || chunk.Data == null)
{
throw new ArgumentNullException("chunk");
}
Trace.TraceInformation($"Received chunk for chunk stream {chunk.ChunkStreamId}");
if (!_store.ContainsKey((int)chunk.ChunkStreamId))
{
_store[(int)chunk.ChunkStreamId] = new List<Chunk>();
}
var currentChunkList = _store[(int)chunk.ChunkStreamId];
Chunk lastReceivedChunk = null;
if (currentChunkList.Count > 0)
{
lastReceivedChunk = currentChunkList[currentChunkList.Count - 1];
}
if (chunk.ChunkType != 0 && lastReceivedChunk == null)
{
throw new Exception("There are no chunks in the current chunk stream. The first chunk in the chunk stream must be of type 0.");
}
// We shouldn't need this (this might actually break things)
// will decide after testing
switch (chunk.ChunkType)
{
case 0:
break;
case 1:
chunk.StreamId = lastReceivedChunk.StreamId;
break;
case 2:
chunk.StreamId = lastReceivedChunk.StreamId;
chunk.Length = lastReceivedChunk.Length;
break;
case 3:
chunk.StreamId = lastReceivedChunk.StreamId;
chunk.Length = lastReceivedChunk.Length;
chunk.Delta = lastReceivedChunk.Delta;
break;
default:
throw new Exception("Invalid chunk type");
}
Trace.TraceInformation($"Determined chunk to be for message stream {chunk.StreamId}");
currentChunkList.Add(chunk);
// Do we have any messages?
foreach (var key in _store.Keys)
{
// if we have a list that has nothing in it
// delete it to free up memory
if (_store[key].Count > 0)
{
var firstChunk = _store[key][0];
var msgLength = firstChunk.Length;
var receivedLength = firstChunk.Data.Length;
// find out how much data we have received
for (int i = 1; i < _store[key].Count; i++)
{
receivedLength += _store[key][i].Data.Length;
}
// did we receive more than we were expecting?
if (receivedLength > msgLength)
{
throw new Exception("Received more data then was expecting.");
}
// we received the amount of data we need to make a message
if (msgLength == receivedLength)
{
var chunks = _store[key];
Trace.TraceInformation($"Creating message from {chunks.Count} chunks.");
_store[key] = null;
OnMessageReceived(ChunksToMessage(chunks));
}
}
}
}
protected virtual void OnMessageReceived(Message msg)
{
MessageReceived?.Invoke(this, new RtmpProtocolEventArgs { Message = msg });
}
public Message ChunksToMessage(List<Chunk> chunks)
{
// this could be better
// do we need to make a new array, maybe create a linked list type thing
byte[] messageData = new byte[chunks[0].Length];
var msg = new Message
{
Data = messageData,
Length = chunks[0].Length,
MessageStreamId = chunks[0].StreamId,
MessageType = chunks[0].ChunkType,
Time = chunks[0].Delta
};
int messageCopyOffset = 0;
foreach (var chunk in chunks)
{
Array.Copy(chunk.Data, 0, messageData, messageCopyOffset, chunk.Data.Length);
messageCopyOffset += chunk.Data.Length;
}
// Clean up memory (all chunks except last, since we copy the master info to all chunks)
// we need the last one for retieving chunk stream info
for (int i = 0; i < chunks.Count - 1; i++)
{
chunks.RemoveAt(0);
}
return msg;
}
}
private class Chunk
{
public uint Delta { get; set; }
public uint ChunkStreamId { get; set; }
public uint StreamId { get; set; }
public uint ChunkType { get; set; }
public uint Length { get; set; }
public byte[] Data { get; set; }
}
}
}