/
FlvMetadata.cs
327 lines (287 loc) · 11.7 KB
/
FlvMetadata.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
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace BililiveRecorder.FlvProcessor
{
public class FlvMetadata : IFlvMetadata
{
private IDictionary<string, object> Meta { get; set; } = new Dictionary<string, object>();
public ICollection<string> Keys => Meta.Keys;
public ICollection<object> Values => Meta.Values;
public int Count => Meta.Count;
public bool IsReadOnly => false;
public object this[string key] { get => Meta[key]; set => Meta[key] = value; }
public FlvMetadata()
{
Meta["duration"] = 0.0;
Meta["lasttimestamp"] = 0.0;
}
public FlvMetadata(byte[] data)
{
// Meta = _Decode(data);
int readHead = 0;
string name = DecodeScriptDataValue(data, ref readHead, AMFTypes.String) as string;
if (name != "onMetaData")
{
throw new Exception("Isn't onMetadata");
}
Meta = DecodeScriptDataValue(data, ref readHead) as Dictionary<string, object>;
if (!Meta.ContainsKey("duration"))
{
Meta["duration"] = 0d;
}
if (!Meta.ContainsKey("lasttimestamp"))
{
Meta["lasttimestamp"] = 0d;
}
Meta.Remove("");
foreach (var item in Meta.ToArray())
{
if (item.Value is string text)
{
Meta[item.Key] = text.Replace("\0", "");
}
}
}
public byte[] ToBytes()
{
using (var ms = new MemoryStream())
{
EncodeScriptDataValue(ms, "onMetaData");
EncodeScriptDataValue(ms, Meta);
return ms.ToArray();
}
}
#region Static
private static void EncodeScriptDataValue(MemoryStream ms, object value)
{
switch (value)
{
case double number:
{
ms.WriteByte((byte)AMFTypes.Number);
ms.Write(BitConverter.GetBytes(number).ToBE(), 0, sizeof(double));
break;
}
case bool boolean:
{
ms.WriteByte((byte)AMFTypes.Boolean);
if (boolean)
{
ms.WriteByte(1);
}
else
{
ms.WriteByte(0);
}
break;
}
case string text:
{
var b = Encoding.UTF8.GetBytes(text);
if (b.Length >= ushort.MaxValue)
{
ms.WriteByte((byte)AMFTypes.LongString);
ms.Write(BitConverter.GetBytes((uint)b.Length).ToBE(), 0, sizeof(uint));
}
else
{
ms.WriteByte((byte)AMFTypes.String);
ms.Write(BitConverter.GetBytes((ushort)b.Length).ToBE(), 0, sizeof(ushort));
}
ms.Write(b, 0, b.Length);
break;
}
case Dictionary<string, object> d:
{
ms.WriteByte((byte)AMFTypes.Object);
foreach (var item in d)
{
var b = Encoding.UTF8.GetBytes(item.Key);
ms.Write(BitConverter.GetBytes((ushort)b.Length).ToBE(), 0, sizeof(ushort));
ms.Write(b, 0, b.Length);
EncodeScriptDataValue(ms, item.Value);
}
ms.WriteByte(0);
ms.WriteByte(0);
ms.WriteByte(9);
break;
}
case List<object> l:
{
ms.WriteByte((byte)AMFTypes.StrictArray);
ms.Write(BitConverter.GetBytes((uint)l.Count), 0, sizeof(uint));
foreach (var item in l)
{
EncodeScriptDataValue(ms, item);
}
break;
}
case DateTime dateTime:
{
ms.WriteByte((byte)AMFTypes.Date);
ms.Write(BitConverter.GetBytes(dateTime.ToUniversalTime().Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds).ToBE(), 0, sizeof(double));
break;
}
default:
throw new NotSupportedException("Type " + value.GetType().FullName + " is not supported");
}
}
private static object DecodeScriptDataValue(byte[] buff, ref int _readHead, AMFTypes expectType = AMFTypes.Any)
{
AMFTypes type = (AMFTypes)buff[_readHead++];
if (expectType != AMFTypes.Any && expectType != type)
{
throw new Exception("AMF Decode type error");
}
switch (type)
{
case AMFTypes.Number:
{
const int SIZEOF_DOUBLE = sizeof(double);
byte[] bytes = new byte[SIZEOF_DOUBLE];
Buffer.BlockCopy(buff, _readHead, bytes, 0, bytes.Length);
_readHead += SIZEOF_DOUBLE;
return BitConverter.ToDouble(bytes.ToBE(), 0);
}
case AMFTypes.Boolean:
return buff[_readHead++] != 0;
case AMFTypes.String:
{
byte[] bytes = new byte[sizeof(ushort)];
bytes[0] = buff[_readHead++];
bytes[1] = buff[_readHead++];
ushort text_size = BitConverter.ToUInt16(bytes.ToBE(), 0);
string text = Encoding.UTF8.GetString(buff, _readHead, text_size);
_readHead += text_size;
return text;
}
case AMFTypes.Object:
{
var d = new Dictionary<string, object>();
while (!(buff[_readHead] == 0 && buff[_readHead + 1] == 0 && buff[_readHead + 2] == 9))
{
string key;
{
byte[] bytes = new byte[sizeof(ushort)];
bytes[0] = buff[_readHead++];
bytes[1] = buff[_readHead++];
ushort text_size = BitConverter.ToUInt16(bytes.ToBE(), 0);
key = Encoding.UTF8.GetString(buff, _readHead, text_size);
_readHead += text_size;
}
object value = DecodeScriptDataValue(buff, ref _readHead);
d.Add(key, value);
}
_readHead += 3;
return d;
}
case AMFTypes.Null:
case AMFTypes.Undefined:
return null;
case AMFTypes.ECMAArray:
_readHead += 4;
goto case AMFTypes.Object;
case AMFTypes.StrictArray:
{
byte[] bytes = new byte[sizeof(uint)];
bytes[0] = buff[_readHead++];
bytes[1] = buff[_readHead++];
bytes[2] = buff[_readHead++];
bytes[3] = buff[_readHead++];
uint array_size = BitConverter.ToUInt32(bytes.ToBE(), 0);
var d = new List<object>();
while (d.Count < array_size)
{
d.Add(DecodeScriptDataValue(buff, ref _readHead));
}
return d;
}
case AMFTypes.Date:
{
const int SIZEOF_DOUBLE = sizeof(double);
const int SIZEOF_SI16 = sizeof(short);
byte[] datetime_bytes = new byte[SIZEOF_DOUBLE];
Buffer.BlockCopy(buff, _readHead, datetime_bytes, 0, datetime_bytes.Length);
_readHead += SIZEOF_DOUBLE;
var datetime = BitConverter.ToDouble(datetime_bytes.ToBE(), 0);
byte[] offset_bytes = new byte[SIZEOF_SI16];
Buffer.BlockCopy(buff, _readHead, offset_bytes, 0, offset_bytes.Length);
_readHead += SIZEOF_SI16;
var offset = BitConverter.ToDouble(offset_bytes.ToBE(), 0);
return new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds(datetime).AddMinutes(-offset);
}
case AMFTypes.LongString:
unchecked
{
byte[] bytes = new byte[sizeof(uint)];
bytes[0] = buff[_readHead++];
bytes[1] = buff[_readHead++];
bytes[2] = buff[_readHead++];
bytes[3] = buff[_readHead++];
int text_size = (int)BitConverter.ToUInt32(bytes.ToBE(), 0);
if (text_size <= 0)
{
throw new Exception("LongString longer than " + int.MaxValue + " is not supported");
}
string text = Encoding.UTF8.GetString(buff, _readHead, text_size);
_readHead += text_size;
return text;
}
default:
case AMFTypes.MovieClip:
case AMFTypes.Reference:
case AMFTypes.ObjectEndMarker:
throw new NotSupportedException("AMF type not supported");
}
}
#endregion
public void Add(string key, object value)
{
Meta.Add(key, value);
}
public bool ContainsKey(string key)
{
return Meta.ContainsKey(key);
}
public bool Remove(string key)
{
return Meta.Remove(key);
}
public bool TryGetValue(string key, out object value)
{
return Meta.TryGetValue(key, out value);
}
public void Add(KeyValuePair<string, object> item)
{
Meta.Add(item);
}
public void Clear()
{
Meta.Clear();
}
public bool Contains(KeyValuePair<string, object> item)
{
return Meta.Contains(item);
}
public void CopyTo(KeyValuePair<string, object>[] array, int arrayIndex)
{
Meta.CopyTo(array, arrayIndex);
}
public bool Remove(KeyValuePair<string, object> item)
{
return Meta.Remove(item);
}
public IEnumerator<KeyValuePair<string, object>> GetEnumerator()
{
return Meta.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return Meta.GetEnumerator();
}
}
}