Browse code

added contacts list and call recording and upgraded SIP.js library

DoubleBastionAdmin authored on 21/05/2024 02:08:44
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,564 @@
1
+/**
2
+ * @copyright 2024 Double Bastion LLC <www.doublebastion.com>
3
+ *
4
+ * @author Double Bastion LLC
5
+ *
6
+ * @license GNU AGPL version 3 or any later version
7
+ *
8
+ * This program is free software; you can redistribute it and/or
9
+ * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
10
+ * License as published by the Free Software Foundation; either
11
+ * version 3 of the License, or any later version.
12
+ *
13
+ * This program is distributed in the hope that it will be useful,
14
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
+ * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
17
+ *
18
+ * You should have received a copy of the GNU Affero General Public
19
+ * License along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
+ *
21
+ *
22
+ *  The code in this file is identical with that of the
23
+ *  original file "fix-webm-duration.js" released under the MIT License:
24
+ *  https://github.com/yusitnikov/fix-webm-duration/blob/master/LICENSE.
25
+ *  The copyright notice for the original file follows:
26
+ */
27
+/**
28
+ * The MIT license
29
+ *
30
+ * Copyright (c) 2018 Yury Sitnikov
31
+ *
32
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
33
+ * of this software and associated documentation files (the "Software"), to deal
34
+ * in the Software without restriction, including without limitation the rights
35
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
36
+ * copies of the Software, and to permit persons to whom the Software is
37
+ * furnished to do so, subject to the following conditions:
38
+ *
39
+ * The above copyright notice and this permission notice shall be included in
40
+ * all copies or substantial portions of the Software.
41
+ *
42
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
43
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
44
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
45
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
46
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
47
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
48
+ * THE SOFTWARE.
49
+ */
50
+
51
+
52
+(function (name, definition) {
53
+    if (typeof define === 'function' && define.amd) { // RequireJS / AMD
54
+        define(definition);
55
+    } else if (typeof module !== 'undefined' && module.exports) { // CommonJS / Node.js
56
+        module.exports = definition();
57
+    } else { // Direct include
58
+        window.ysFixWebmDuration = definition();
59
+    }
60
+})('fix-webm-duration', function () {
61
+    /*
62
+     * This is the list of possible WEBM file sections by their IDs.
63
+     * Possible types: Container, Binary, Uint, Int, String, Float, Date
64
+     */
65
+    var sections = {
66
+        0xa45dfa3: { name: 'EBML', type: 'Container' },
67
+        0x286: { name: 'EBMLVersion', type: 'Uint' },
68
+        0x2f7: { name: 'EBMLReadVersion', type: 'Uint' },
69
+        0x2f2: { name: 'EBMLMaxIDLength', type: 'Uint' },
70
+        0x2f3: { name: 'EBMLMaxSizeLength', type: 'Uint' },
71
+        0x282: { name: 'DocType', type: 'String' },
72
+        0x287: { name: 'DocTypeVersion', type: 'Uint' },
73
+        0x285: { name: 'DocTypeReadVersion', type: 'Uint' },
74
+        0x6c: { name: 'Void', type: 'Binary' },
75
+        0x3f: { name: 'CRC-32', type: 'Binary' },
76
+        0xb538667: { name: 'SignatureSlot', type: 'Container' },
77
+        0x3e8a: { name: 'SignatureAlgo', type: 'Uint' },
78
+        0x3e9a: { name: 'SignatureHash', type: 'Uint' },
79
+        0x3ea5: { name: 'SignaturePublicKey', type: 'Binary' },
80
+        0x3eb5: { name: 'Signature', type: 'Binary' },
81
+        0x3e5b: { name: 'SignatureElements', type: 'Container' },
82
+        0x3e7b: { name: 'SignatureElementList', type: 'Container' },
83
+        0x2532: { name: 'SignedElement', type: 'Binary' },
84
+        0x8538067: { name: 'Segment', type: 'Container' },
85
+        0x14d9b74: { name: 'SeekHead', type: 'Container' },
86
+        0xdbb: { name: 'Seek', type: 'Container' },
87
+        0x13ab: { name: 'SeekID', type: 'Binary' },
88
+        0x13ac: { name: 'SeekPosition', type: 'Uint' },
89
+        0x549a966: { name: 'Info', type: 'Container' },
90
+        0x33a4: { name: 'SegmentUID', type: 'Binary' },
91
+        0x3384: { name: 'SegmentFilename', type: 'String' },
92
+        0x1cb923: { name: 'PrevUID', type: 'Binary' },
93
+        0x1c83ab: { name: 'PrevFilename', type: 'String' },
94
+        0x1eb923: { name: 'NextUID', type: 'Binary' },
95
+        0x1e83bb: { name: 'NextFilename', type: 'String' },
96
+        0x444: { name: 'SegmentFamily', type: 'Binary' },
97
+        0x2924: { name: 'ChapterTranslate', type: 'Container' },
98
+        0x29fc: { name: 'ChapterTranslateEditionUID', type: 'Uint' },
99
+        0x29bf: { name: 'ChapterTranslateCodec', type: 'Uint' },
100
+        0x29a5: { name: 'ChapterTranslateID', type: 'Binary' },
101
+        0xad7b1: { name: 'TimecodeScale', type: 'Uint' },
102
+        0x489: { name: 'Duration', type: 'Float' },
103
+        0x461: { name: 'DateUTC', type: 'Date' },
104
+        0x3ba9: { name: 'Title', type: 'String' },
105
+        0xd80: { name: 'MuxingApp', type: 'String' },
106
+        0x1741: { name: 'WritingApp', type: 'String' },
107
+        // 0xf43b675: { name: 'Cluster', type: 'Container' },
108
+        0x67: { name: 'Timecode', type: 'Uint' },
109
+        0x1854: { name: 'SilentTracks', type: 'Container' },
110
+        0x18d7: { name: 'SilentTrackNumber', type: 'Uint' },
111
+        0x27: { name: 'Position', type: 'Uint' },
112
+        0x2b: { name: 'PrevSize', type: 'Uint' },
113
+        0x23: { name: 'SimpleBlock', type: 'Binary' },
114
+        0x20: { name: 'BlockGroup', type: 'Container' },
115
+        0x21: { name: 'Block', type: 'Binary' },
116
+        0x22: { name: 'BlockVirtual', type: 'Binary' },
117
+        0x35a1: { name: 'BlockAdditions', type: 'Container' },
118
+        0x26: { name: 'BlockMore', type: 'Container' },
119
+        0x6e: { name: 'BlockAddID', type: 'Uint' },
120
+        0x25: { name: 'BlockAdditional', type: 'Binary' },
121
+        0x1b: { name: 'BlockDuration', type: 'Uint' },
122
+        0x7a: { name: 'ReferencePriority', type: 'Uint' },
123
+        0x7b: { name: 'ReferenceBlock', type: 'Int' },
124
+        0x7d: { name: 'ReferenceVirtual', type: 'Int' },
125
+        0x24: { name: 'CodecState', type: 'Binary' },
126
+        0x35a2: { name: 'DiscardPadding', type: 'Int' },
127
+        0xe: { name: 'Slices', type: 'Container' },
128
+        0x68: { name: 'TimeSlice', type: 'Container' },
129
+        0x4c: { name: 'LaceNumber', type: 'Uint' },
130
+        0x4d: { name: 'FrameNumber', type: 'Uint' },
131
+        0x4b: { name: 'BlockAdditionID', type: 'Uint' },
132
+        0x4e: { name: 'Delay', type: 'Uint' },
133
+        0x4f: { name: 'SliceDuration', type: 'Uint' },
134
+        0x48: { name: 'ReferenceFrame', type: 'Container' },
135
+        0x49: { name: 'ReferenceOffset', type: 'Uint' },
136
+        0x4a: { name: 'ReferenceTimeCode', type: 'Uint' },
137
+        0x2f: { name: 'EncryptedBlock', type: 'Binary' },
138
+        0x654ae6b: { name: 'Tracks', type: 'Container' },
139
+        0x2e: { name: 'TrackEntry', type: 'Container' },
140
+        0x57: { name: 'TrackNumber', type: 'Uint' },
141
+        0x33c5: { name: 'TrackUID', type: 'Uint' },
142
+        0x3: { name: 'TrackType', type: 'Uint' },
143
+        0x39: { name: 'FlagEnabled', type: 'Uint' },
144
+        0x8: { name: 'FlagDefault', type: 'Uint' },
145
+        0x15aa: { name: 'FlagForced', type: 'Uint' },
146
+        0x1c: { name: 'FlagLacing', type: 'Uint' },
147
+        0x2de7: { name: 'MinCache', type: 'Uint' },
148
+        0x2df8: { name: 'MaxCache', type: 'Uint' },
149
+        0x3e383: { name: 'DefaultDuration', type: 'Uint' },
150
+        0x34e7a: { name: 'DefaultDecodedFieldDuration', type: 'Uint' },
151
+        0x3314f: { name: 'TrackTimecodeScale', type: 'Float' },
152
+        0x137f: { name: 'TrackOffset', type: 'Int' },
153
+        0x15ee: { name: 'MaxBlockAdditionID', type: 'Uint' },
154
+        0x136e: { name: 'Name', type: 'String' },
155
+        0x2b59c: { name: 'Language', type: 'String' },
156
+        0x6: { name: 'CodecID', type: 'String' },
157
+        0x23a2: { name: 'CodecPrivate', type: 'Binary' },
158
+        0x58688: { name: 'CodecName', type: 'String' },
159
+        0x3446: { name: 'AttachmentLink', type: 'Uint' },
160
+        0x1a9697: { name: 'CodecSettings', type: 'String' },
161
+        0x1b4040: { name: 'CodecInfoURL', type: 'String' },
162
+        0x6b240: { name: 'CodecDownloadURL', type: 'String' },
163
+        0x2a: { name: 'CodecDecodeAll', type: 'Uint' },
164
+        0x2fab: { name: 'TrackOverlay', type: 'Uint' },
165
+        0x16aa: { name: 'CodecDelay', type: 'Uint' },
166
+        0x16bb: { name: 'SeekPreRoll', type: 'Uint' },
167
+        0x2624: { name: 'TrackTranslate', type: 'Container' },
168
+        0x26fc: { name: 'TrackTranslateEditionUID', type: 'Uint' },
169
+        0x26bf: { name: 'TrackTranslateCodec', type: 'Uint' },
170
+        0x26a5: { name: 'TrackTranslateTrackID', type: 'Binary' },
171
+        0x60: { name: 'Video', type: 'Container' },
172
+        0x1a: { name: 'FlagInterlaced', type: 'Uint' },
173
+        0x13b8: { name: 'StereoMode', type: 'Uint' },
174
+        0x13c0: { name: 'AlphaMode', type: 'Uint' },
175
+        0x13b9: { name: 'OldStereoMode', type: 'Uint' },
176
+        0x30: { name: 'PixelWidth', type: 'Uint' },
177
+        0x3a: { name: 'PixelHeight', type: 'Uint' },
178
+        0x14aa: { name: 'PixelCropBottom', type: 'Uint' },
179
+        0x14bb: { name: 'PixelCropTop', type: 'Uint' },
180
+        0x14cc: { name: 'PixelCropLeft', type: 'Uint' },
181
+        0x14dd: { name: 'PixelCropRight', type: 'Uint' },
182
+        0x14b0: { name: 'DisplayWidth', type: 'Uint' },
183
+        0x14ba: { name: 'DisplayHeight', type: 'Uint' },
184
+        0x14b2: { name: 'DisplayUnit', type: 'Uint' },
185
+        0x14b3: { name: 'AspectRatioType', type: 'Uint' },
186
+        0xeb524: { name: 'ColourSpace', type: 'Binary' },
187
+        0xfb523: { name: 'GammaValue', type: 'Float' },
188
+        0x383e3: { name: 'FrameRate', type: 'Float' },
189
+        0x61: { name: 'Audio', type: 'Container' },
190
+        0x35: { name: 'SamplingFrequency', type: 'Float' },
191
+        0x38b5: { name: 'OutputSamplingFrequency', type: 'Float' },
192
+        0x1f: { name: 'Channels', type: 'Uint' },
193
+        0x3d7b: { name: 'ChannelPositions', type: 'Binary' },
194
+        0x2264: { name: 'BitDepth', type: 'Uint' },
195
+        0x62: { name: 'TrackOperation', type: 'Container' },
196
+        0x63: { name: 'TrackCombinePlanes', type: 'Container' },
197
+        0x64: { name: 'TrackPlane', type: 'Container' },
198
+        0x65: { name: 'TrackPlaneUID', type: 'Uint' },
199
+        0x66: { name: 'TrackPlaneType', type: 'Uint' },
200
+        0x69: { name: 'TrackJoinBlocks', type: 'Container' },
201
+        0x6d: { name: 'TrackJoinUID', type: 'Uint' },
202
+        0x40: { name: 'TrickTrackUID', type: 'Uint' },
203
+        0x41: { name: 'TrickTrackSegmentUID', type: 'Binary' },
204
+        0x46: { name: 'TrickTrackFlag', type: 'Uint' },
205
+        0x47: { name: 'TrickMasterTrackUID', type: 'Uint' },
206
+        0x44: { name: 'TrickMasterTrackSegmentUID', type: 'Binary' },
207
+        0x2d80: { name: 'ContentEncodings', type: 'Container' },
208
+        0x2240: { name: 'ContentEncoding', type: 'Container' },
209
+        0x1031: { name: 'ContentEncodingOrder', type: 'Uint' },
210
+        0x1032: { name: 'ContentEncodingScope', type: 'Uint' },
211
+        0x1033: { name: 'ContentEncodingType', type: 'Uint' },
212
+        0x1034: { name: 'ContentCompression', type: 'Container' },
213
+        0x254: { name: 'ContentCompAlgo', type: 'Uint' },
214
+        0x255: { name: 'ContentCompSettings', type: 'Binary' },
215
+        0x1035: { name: 'ContentEncryption', type: 'Container' },
216
+        0x7e1: { name: 'ContentEncAlgo', type: 'Uint' },
217
+        0x7e2: { name: 'ContentEncKeyID', type: 'Binary' },
218
+        0x7e3: { name: 'ContentSignature', type: 'Binary' },
219
+        0x7e4: { name: 'ContentSigKeyID', type: 'Binary' },
220
+        0x7e5: { name: 'ContentSigAlgo', type: 'Uint' },
221
+        0x7e6: { name: 'ContentSigHashAlgo', type: 'Uint' },
222
+        0xc53bb6b: { name: 'Cues', type: 'Container' },
223
+        0x3b: { name: 'CuePoint', type: 'Container' },
224
+        0x33: { name: 'CueTime', type: 'Uint' },
225
+        0x37: { name: 'CueTrackPositions', type: 'Container' },
226
+        0x77: { name: 'CueTrack', type: 'Uint' },
227
+        0x71: { name: 'CueClusterPosition', type: 'Uint' },
228
+        0x70: { name: 'CueRelativePosition', type: 'Uint' },
229
+        0x32: { name: 'CueDuration', type: 'Uint' },
230
+        0x1378: { name: 'CueBlockNumber', type: 'Uint' },
231
+        0x6a: { name: 'CueCodecState', type: 'Uint' },
232
+        0x5b: { name: 'CueReference', type: 'Container' },
233
+        0x16: { name: 'CueRefTime', type: 'Uint' },
234
+        0x17: { name: 'CueRefCluster', type: 'Uint' },
235
+        0x135f: { name: 'CueRefNumber', type: 'Uint' },
236
+        0x6b: { name: 'CueRefCodecState', type: 'Uint' },
237
+        0x941a469: { name: 'Attachments', type: 'Container' },
238
+        0x21a7: { name: 'AttachedFile', type: 'Container' },
239
+        0x67e: { name: 'FileDescription', type: 'String' },
240
+        0x66e: { name: 'FileName', type: 'String' },
241
+        0x660: { name: 'FileMimeType', type: 'String' },
242
+        0x65c: { name: 'FileData', type: 'Binary' },
243
+        0x6ae: { name: 'FileUID', type: 'Uint' },
244
+        0x675: { name: 'FileReferral', type: 'Binary' },
245
+        0x661: { name: 'FileUsedStartTime', type: 'Uint' },
246
+        0x662: { name: 'FileUsedEndTime', type: 'Uint' },
247
+        0x43a770: { name: 'Chapters', type: 'Container' },
248
+        0x5b9: { name: 'EditionEntry', type: 'Container' },
249
+        0x5bc: { name: 'EditionUID', type: 'Uint' },
250
+        0x5bd: { name: 'EditionFlagHidden', type: 'Uint' },
251
+        0x5db: { name: 'EditionFlagDefault', type: 'Uint' },
252
+        0x5dd: { name: 'EditionFlagOrdered', type: 'Uint' },
253
+        0x36: { name: 'ChapterAtom', type: 'Container' },
254
+        0x33c4: { name: 'ChapterUID', type: 'Uint' },
255
+        0x1654: { name: 'ChapterStringUID', type: 'String' },
256
+        0x11: { name: 'ChapterTimeStart', type: 'Uint' },
257
+        0x12: { name: 'ChapterTimeEnd', type: 'Uint' },
258
+        0x18: { name: 'ChapterFlagHidden', type: 'Uint' },
259
+        0x598: { name: 'ChapterFlagEnabled', type: 'Uint' },
260
+        0x2e67: { name: 'ChapterSegmentUID', type: 'Binary' },
261
+        0x2ebc: { name: 'ChapterSegmentEditionUID', type: 'Uint' },
262
+        0x23c3: { name: 'ChapterPhysicalEquiv', type: 'Uint' },
263
+        0xf: { name: 'ChapterTrack', type: 'Container' },
264
+        0x9: { name: 'ChapterTrackNumber', type: 'Uint' },
265
+        0x0: { name: 'ChapterDisplay', type: 'Container' },
266
+        0x5: { name: 'ChapString', type: 'String' },
267
+        0x37c: { name: 'ChapLanguage', type: 'String' },
268
+        0x37e: { name: 'ChapCountry', type: 'String' },
269
+        0x2944: { name: 'ChapProcess', type: 'Container' },
270
+        0x2955: { name: 'ChapProcessCodecID', type: 'Uint' },
271
+        0x50d: { name: 'ChapProcessPrivate', type: 'Binary' },
272
+        0x2911: { name: 'ChapProcessCommand', type: 'Container' },
273
+        0x2922: { name: 'ChapProcessTime', type: 'Uint' },
274
+        0x2933: { name: 'ChapProcessData', type: 'Binary' },
275
+        0x254c367: { name: 'Tags', type: 'Container' },
276
+        0x3373: { name: 'Tag', type: 'Container' },
277
+        0x23c0: { name: 'Targets', type: 'Container' },
278
+        0x28ca: { name: 'TargetTypeValue', type: 'Uint' },
279
+        0x23ca: { name: 'TargetType', type: 'String' },
280
+        0x23c5: { name: 'TagTrackUID', type: 'Uint' },
281
+        0x23c9: { name: 'TagEditionUID', type: 'Uint' },
282
+        0x23c4: { name: 'TagChapterUID', type: 'Uint' },
283
+        0x23c6: { name: 'TagAttachmentUID', type: 'Uint' },
284
+        0x27c8: { name: 'SimpleTag', type: 'Container' },
285
+        0x5a3: { name: 'TagName', type: 'String' },
286
+        0x47a: { name: 'TagLanguage', type: 'String' },
287
+        0x484: { name: 'TagDefault', type: 'Uint' },
288
+        0x487: { name: 'TagString', type: 'String' },
289
+        0x485: { name: 'TagBinary', type: 'Binary' }
290
+    };
291
+
292
+    function doInherit(newClass, baseClass) {
293
+        newClass.prototype = Object.create(baseClass.prototype);
294
+        newClass.prototype.constructor = newClass;
295
+    }
296
+
297
+    function WebmBase(name, type) {
298
+        this.name = name || 'Unknown';
299
+        this.type = type || 'Unknown';
300
+    }
301
+    WebmBase.prototype.updateBySource = function() { };
302
+    WebmBase.prototype.setSource = function(source) {
303
+        this.source = source;
304
+        this.updateBySource();
305
+    };
306
+    WebmBase.prototype.updateByData = function() { };
307
+    WebmBase.prototype.setData = function(data) {
308
+        this.data = data;
309
+        this.updateByData();
310
+    };
311
+
312
+    function WebmUint(name, type) {
313
+        WebmBase.call(this, name, type || 'Uint');
314
+    }
315
+    doInherit(WebmUint, WebmBase);
316
+    function padHex(hex) {
317
+        return hex.length % 2 === 1 ? '0' + hex : hex;
318
+    }
319
+    WebmUint.prototype.updateBySource = function() {
320
+        // use hex representation of a number instead of number value
321
+        this.data = '';
322
+        for (var i = 0; i < this.source.length; i++) {
323
+            var hex = this.source[i].toString(16);
324
+            this.data += padHex(hex);
325
+        }
326
+    };
327
+    WebmUint.prototype.updateByData = function() {
328
+        var length = this.data.length / 2;
329
+        this.source = new Uint8Array(length);
330
+        for (var i = 0; i < length; i++) {
331
+            var hex = this.data.substr(i * 2, 2);
332
+            this.source[i] = parseInt(hex, 16);
333
+        }
334
+    };
335
+    WebmUint.prototype.getValue = function() {
336
+        return parseInt(this.data, 16);
337
+    };
338
+    WebmUint.prototype.setValue = function(value) {
339
+        this.setData(padHex(value.toString(16)));
340
+    };
341
+
342
+    function WebmFloat(name, type) {
343
+        WebmBase.call(this, name, type || 'Float');
344
+    }
345
+    doInherit(WebmFloat, WebmBase);
346
+    WebmFloat.prototype.getFloatArrayType = function() {
347
+        return this.source && this.source.length === 4 ? Float32Array : Float64Array;
348
+    };
349
+    WebmFloat.prototype.updateBySource = function() {
350
+        var byteArray = this.source.reverse();
351
+        var floatArrayType = this.getFloatArrayType();
352
+        var floatArray = new floatArrayType(byteArray.buffer);
353
+        this.data = floatArray[0];
354
+    };
355
+    WebmFloat.prototype.updateByData = function() {
356
+        var floatArrayType = this.getFloatArrayType();
357
+        var floatArray = new floatArrayType([ this.data ]);
358
+        var byteArray = new Uint8Array(floatArray.buffer);
359
+        this.source = byteArray.reverse();
360
+    };
361
+    WebmFloat.prototype.getValue = function() {
362
+        return this.data;
363
+    };
364
+    WebmFloat.prototype.setValue = function(value) {
365
+        this.setData(value);
366
+    };
367
+
368
+    function WebmContainer(name, type) {
369
+        WebmBase.call(this, name, type || 'Container');
370
+    }
371
+    doInherit(WebmContainer, WebmBase);
372
+    WebmContainer.prototype.readByte = function() {
373
+        return this.source[this.offset++];
374
+    };
375
+    WebmContainer.prototype.readUint = function() {
376
+        var firstByte = this.readByte();
377
+        var bytes = 8 - firstByte.toString(2).length;
378
+        var value = firstByte - (1 << (7 - bytes));
379
+        for (var i = 0; i < bytes; i++) {
380
+            // don't use bit operators to support x86
381
+            value *= 256;
382
+            value += this.readByte();
383
+        }
384
+        return value;
385
+    };
386
+    WebmContainer.prototype.updateBySource = function() {
387
+        this.data = [];
388
+        for (this.offset = 0; this.offset < this.source.length; this.offset = end) {
389
+            var id = this.readUint();
390
+            var len = this.readUint();
391
+            var end = Math.min(this.offset + len, this.source.length);
392
+            var data = this.source.slice(this.offset, end);
393
+
394
+            var info = sections[id] || { name: 'Unknown', type: 'Unknown' };
395
+            var ctr = WebmBase;
396
+            switch (info.type) {
397
+                case 'Container':
398
+                    ctr = WebmContainer;
399
+                    break;
400
+                case 'Uint':
401
+                    ctr = WebmUint;
402
+                    break;
403
+                case 'Float':
404
+                    ctr = WebmFloat;
405
+                    break;
406
+            }
407
+            var section = new ctr(info.name, info.type);
408
+            section.setSource(data);
409
+            this.data.push({
410
+                id: id,
411
+                idHex: id.toString(16),
412
+                data: section
413
+            });
414
+        }
415
+    };
416
+    WebmContainer.prototype.writeUint = function(x, draft) {
417
+        for (var bytes = 1, flag = 0x80; x >= flag && bytes < 8; bytes++, flag *= 0x80) { }
418
+
419
+        if (!draft) {
420
+            var value = flag + x;
421
+            for (var i = bytes - 1; i >= 0; i--) {
422
+                // don't use bit operators to support x86
423
+                var c = value % 256;
424
+                this.source[this.offset + i] = c;
425
+                value = (value - c) / 256;
426
+            }
427
+        }
428
+
429
+        this.offset += bytes;
430
+    };
431
+    WebmContainer.prototype.writeSections = function(draft) {
432
+        this.offset = 0;
433
+        for (var i = 0; i < this.data.length; i++) {
434
+            var section = this.data[i],
435
+                content = section.data.source,
436
+                contentLength = content.length;
437
+            this.writeUint(section.id, draft);
438
+            this.writeUint(contentLength, draft);
439
+            if (!draft) {
440
+                this.source.set(content, this.offset);
441
+            }
442
+            this.offset += contentLength;
443
+        }
444
+        return this.offset;
445
+    };
446
+    WebmContainer.prototype.updateByData = function() {
447
+        // run without accessing this.source to determine total length - need to know it to create Uint8Array
448
+        var length = this.writeSections('draft');
449
+        this.source = new Uint8Array(length);
450
+        // now really write data
451
+        this.writeSections();
452
+    };
453
+    WebmContainer.prototype.getSectionById = function(id) {
454
+        for (var i = 0; i < this.data.length; i++) {
455
+            var section = this.data[i];
456
+            if (section.id === id) {
457
+                return section.data;
458
+            }
459
+        }
460
+        return null;
461
+    };
462
+
463
+    function WebmFile(source) {
464
+        WebmContainer.call(this, 'File', 'File');
465
+        this.setSource(source);
466
+    }
467
+    doInherit(WebmFile, WebmContainer);
468
+    WebmFile.prototype.fixDuration = function(duration, options) {
469
+        var logger = options && options.logger;
470
+        if (logger === undefined) {
471
+            logger = function(message) {
472
+                console.log(message);
473
+            };
474
+        } else if (!logger) {
475
+            logger = function() { };
476
+        }
477
+
478
+        var segmentSection = this.getSectionById(0x8538067);
479
+        if (!segmentSection) {
480
+            logger('[fix-webm-duration] Segment section is missing');
481
+            return false;
482
+        }
483
+
484
+        var infoSection = segmentSection.getSectionById(0x549a966);
485
+        if (!infoSection) {
486
+            logger('[fix-webm-duration] Info section is missing');
487
+            return false;
488
+        }
489
+
490
+        var timeScaleSection = infoSection.getSectionById(0xad7b1);
491
+        if (!timeScaleSection) {
492
+            logger('[fix-webm-duration] TimecodeScale section is missing');
493
+            return false;
494
+        }
495
+
496
+        var durationSection = infoSection.getSectionById(0x489);
497
+        if (durationSection) {
498
+            if (durationSection.getValue() <= 0) {
499
+                logger('[fix-webm-duration] Duration section is present, but the value is empty');
500
+                durationSection.setValue(duration);
501
+            } else {
502
+                logger('[fix-webm-duration] Duration section is present');
503
+                return false;
504
+            }
505
+        } else {
506
+            logger('[fix-webm-duration] Duration section is missing');
507
+            // append Duration section
508
+            durationSection = new WebmFloat('Duration', 'Float');
509
+            durationSection.setValue(duration);
510
+            infoSection.data.push({
511
+                id: 0x489,
512
+                data: durationSection
513
+            });
514
+        }
515
+
516
+        // set default time scale to 1 millisecond (1000000 nanoseconds)
517
+        timeScaleSection.setValue(1000000);
518
+        infoSection.updateByData();
519
+        segmentSection.updateByData();
520
+        this.updateByData();
521
+
522
+        return true;
523
+    };
524
+    WebmFile.prototype.toBlob = function(mimeType) {
525
+        return new Blob([ this.source.buffer ], { type: mimeType || 'video/webm' });
526
+    };
527
+
528
+    function fixWebmDuration(blob, duration, callback, options) {
529
+        // The callback may be omitted - then the third argument is options
530
+        if (typeof callback === "object") {
531
+            options = callback;
532
+            callback = undefined;
533
+        }
534
+
535
+        if (!callback) {
536
+            return new Promise(function(resolve) {
537
+                fixWebmDuration(blob, duration, resolve, options);
538
+            });
539
+        }
540
+
541
+        try {
542
+            var reader = new FileReader();
543
+            reader.onloadend = function() {
544
+                try {
545
+                    var file = new WebmFile(new Uint8Array(reader.result));
546
+                    if (file.fixDuration(duration, options)) {
547
+                        blob = file.toBlob(blob.type);
548
+                    }
549
+                } catch (ex) {
550
+                    // ignore
551
+                }
552
+                callback(blob);
553
+            };
554
+            reader.readAsArrayBuffer(blob);
555
+        } catch (ex) {
556
+            callback(blob);
557
+        }
558
+    }
559
+
560
+    // Support AMD import default
561
+    fixWebmDuration.default = fixWebmDuration;
562
+
563
+    return fixWebmDuration;
564
+});