-
Notifications
You must be signed in to change notification settings - Fork 0
/
VideoDecoder.cs
124 lines (111 loc) · 4.71 KB
/
VideoDecoder.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
using System;
using System.Drawing;
using System.IO;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
namespace myCamViewer
{
class VideoDecoder
{
// JPEG markers
const byte picMarker = 0xFF;
const byte picStart = 0xD8;
const byte picEnd = 0xD9;
/// <summary>
/// Start a MJPEG on a http stream
/// </summary>
/// <param name="action">Delegate to run at each frame</param>
/// <param name="url">url of the http stream (only basic auth is implemented)</param>
/// <param name="token">cancellation token used to cancel the stream parsing</param>
/// <param name="chunkMaxSize">Max chunk byte size when reading stream</param>
/// <param name="frameBufferSize">Maximum frame byte size</param>
/// <returns></returns>
public async static Task StartAsync(Action<Image> action, string url, CancellationToken? token = null, int chunkMaxSize = 1024, int frameBufferSize = 1024 * 1024)
{
var tok = token ?? CancellationToken.None;
using (var cli = new HttpClient())
{
using (var stream = await cli.GetStreamAsync(url).ConfigureAwait(false))
{
int frameIndex = 0; // index of current frame byte
bool inPicture = false;
byte current = 0x00;
byte previous = 0x00;
var chunk = new byte[chunkMaxSize];
// byte array that will contain picture eventually
var frameBytes = new byte[frameBufferSize];
// Filling the stream until CancellationToken is used
while (true)
{
var streamLength = await stream.ReadAsync(chunk, 0, chunkMaxSize, tok).ConfigureAwait(false);
parseStreamBuffer(action, frameBytes, ref frameIndex, streamLength, chunk, ref inPicture, ref previous, ref current);
};
}
}
}
// Depending on the position on buffer decide either to try parse image or search for JPEG start
static void parseStreamBuffer(Action<Image> action, byte[] frameBytes, ref int frameIndex, int streamLength, byte[] chunk, ref bool inPicture, ref byte previous, ref byte current)
{
var idx = 0;
while (idx < streamLength)
{
if (inPicture)
{
parsePicture(action, frameBytes, ref frameIndex, ref streamLength, chunk, ref idx, ref inPicture, ref previous, ref current);
}
else
{
searchPicture(frameBytes, ref frameIndex, ref streamLength, chunk, ref idx, ref inPicture, ref previous, ref current);
}
}
}
// Look for JPEG start byte sequence
static void searchPicture(byte[] frameBytes, ref int frameIndex, ref int streamLength, byte[] chunk, ref int idx, ref bool inPicture, ref byte previous, ref byte current)
{
do
{
previous = current;
current = chunk[idx++];
// JPEG picture start
if (previous == picMarker && current == picStart)
{
frameIndex = 2;
frameBytes[0] = picMarker;
frameBytes[1] = picStart;
inPicture = true;
return;
}
} while (idx < streamLength);
}
// Fill image bytes until a JPEG end is reach.
static void parsePicture(Action<Image> action, byte[] frameBytes, ref int frameIndex, ref int streamLength, byte[] chunk, ref int idx, ref bool inPicture, ref byte previous, ref byte current)
{
do
{
previous = current;
current = chunk[idx++];
frameBytes[frameIndex++] = current;
// JPEG end
if (previous == picMarker && current == picEnd)
{
Image img = null;
using (var s = new MemoryStream(frameBytes, 0, frameIndex))
{
try
{
img = Image.FromStream(s);
}
catch
{
// Ignore wrongly parsed images
}
}
Task.Run(() => action(img));
inPicture = false;
return;
}
} while (idx < streamLength);
}
}
}