Blame view

static/plugins/spice-html5/webm.js 17.3 KB
831eac332   zhuzhenchao   add file
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
  "use strict";
  /*
     Copyright (C) 2014 by Jeremy P. White <jwhite@codeweavers.com>
  
     This file is part of spice-html5.
  
     spice-html5 is free software: you can redistribute it and/or modify
     it under the terms of the GNU Lesser General Public License as published by
     the Free Software Foundation, either version 3 of the License, or
     (at your option) any later version.
  
     spice-html5 is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     GNU Lesser General Public License for more details.
  
     You should have received a copy of the GNU Lesser General Public License
     along with spice-html5.  If not, see <http://www.gnu.org/licenses/>.
  */
  
  
  /*----------------------------------------------------------------------------
  **  EBML identifiers
  **--------------------------------------------------------------------------*/
  var EBML_HEADER =                           [ 0x1a, 0x45, 0xdf, 0xa3 ];
  var EBML_HEADER_VERSION =                   [ 0x42, 0x86 ];
  var EBML_HEADER_READ_VERSION =              [ 0x42, 0xf7 ];
  var EBML_HEADER_MAX_ID_LENGTH =             [ 0x42, 0xf2 ];
  var EBML_HEADER_MAX_SIZE_LENGTH =           [ 0x42, 0xf3 ];
  var EBML_HEADER_DOC_TYPE =                  [ 0x42, 0x82 ];
  var EBML_HEADER_DOC_TYPE_VERSION =          [ 0x42, 0x87 ];
  var EBML_HEADER_DOC_TYPE_READ_VERSION =     [ 0x42, 0x85 ];
  
  var WEBM_SEGMENT_HEADER =                   [ 0x18, 0x53, 0x80, 0x67 ];
  var WEBM_SEGMENT_INFORMATION =              [ 0x15, 0x49, 0xA9, 0x66 ];
  
  var WEBM_TIMECODE_SCALE =                   [ 0x2A, 0xD7, 0xB1 ];
  var WEBM_MUXING_APP =                       [ 0x4D, 0x80 ];
  var WEBM_WRITING_APP =                      [ 0x57, 0x41 ];
  
  var WEBM_SEEK_HEAD =                        [ 0x11, 0x4D, 0x9B, 0x74 ];
  var WEBM_SEEK =                             [ 0x4D, 0xBB ];
  var WEBM_SEEK_ID =                          [ 0x53, 0xAB ];
  var WEBM_SEEK_POSITION =                    [ 0x53, 0xAC ];
  
  var WEBM_TRACKS =                           [ 0x16, 0x54, 0xAE, 0x6B ];
  var WEBM_TRACK_ENTRY =                      [ 0xAE ];
  var WEBM_TRACK_NUMBER =                     [ 0xD7 ];
  var WEBM_TRACK_UID =                        [ 0x73, 0xC5 ];
  var WEBM_TRACK_TYPE =                       [ 0x83 ];
  var WEBM_FLAG_ENABLED =                     [ 0xB9 ];
  var WEBM_FLAG_DEFAULT =                     [ 0x88 ];
  var WEBM_FLAG_FORCED =                      [ 0x55, 0xAA ];
  var WEBM_FLAG_LACING =                      [ 0x9C ];
  var WEBM_MIN_CACHE =                        [ 0x6D, 0xE7 ];
  
  var WEBM_MAX_BLOCK_ADDITION_ID =            [ 0x55, 0xEE ];
  var WEBM_CODEC_DECODE_ALL =                 [ 0xAA ];
  var WEBM_SEEK_PRE_ROLL =                    [ 0x56, 0xBB ];
  var WEBM_CODEC_DELAY =                      [ 0x56, 0xAA ];
  var WEBM_CODEC_PRIVATE =                    [ 0x63, 0xA2 ];
  var WEBM_CODEC_ID =                         [ 0x86 ];
  
  var WEBM_AUDIO =                            [ 0xE1 ] ;
  var WEBM_SAMPLING_FREQUENCY =               [ 0xB5 ] ;
  var WEBM_CHANNELS =                         [ 0x9F ] ;
  
  var WEBM_CLUSTER =                          [ 0x1F, 0x43, 0xB6, 0x75 ];
  var WEBM_TIME_CODE =                        [ 0xE7 ] ;
  var WEBM_SIMPLE_BLOCK =                     [ 0xA3 ] ;
  
  /*----------------------------------------------------------------------------
  **  Various OPUS / Webm constants
  **--------------------------------------------------------------------------*/
  var CLUSTER_SIMPLEBLOCK_FLAG_KEYFRAME       = 1 << 7;
  
  var OPUS_FREQUENCY                          = 48000;
  var OPUS_CHANNELS                           = 2;
  
  var SPICE_PLAYBACK_CODEC                    = 'audio/webm; codecs="opus"';
  var MAX_CLUSTER_TIME                        = 1000;
  
  var GAP_DETECTION_THRESHOLD                 = 50;
  
  /*----------------------------------------------------------------------------
  **  EBML utility functions
  **      These classes can create the binary representation of a webm file
  **--------------------------------------------------------------------------*/
  function EBML_write_u1_data_len(len, dv, at)
  {
      var b = 0x80 | len;
      dv.setUint8(at, b);
      return at + 1;
  }
  
  function EBML_write_u8_value(id, val, dv, at)
  {
      at = EBML_write_array(id, dv, at);
      at = EBML_write_u1_data_len(1, dv, at);
      dv.setUint8(at, val);
      return at + 1;
  }
  
  function EBML_write_u32_value(id, val, dv, at)
  {
      at = EBML_write_array(id, dv, at);
      at = EBML_write_u1_data_len(4, dv, at);
      dv.setUint32(at, val);
      return at + 4;
  }
  
  function EBML_write_u16_value(id, val, dv, at)
  {
      at = EBML_write_array(id, dv, at);
      at = EBML_write_u1_data_len(2, dv, at);
      dv.setUint16(at, val);
      return at + 2;
  }
  
  function EBML_write_float_value(id, val, dv, at)
  {
      at = EBML_write_array(id, dv, at);
      at = EBML_write_u1_data_len(4, dv, at);
      dv.setFloat32(at, val);
      return at + 4;
  }
  
  
  
  function EBML_write_u64_data_len(len, dv, at)
  {
      /* Javascript doesn't do 64 bit ints, so this cheats and
          just has a max of 32 bits.  Fine for our purposes */
      dv.setUint8(at++, 0x01);
      dv.setUint8(at++, 0x00);
      dv.setUint8(at++, 0x00);
      dv.setUint8(at++, 0x00);
      var val = len & 0xFFFFFFFF;
      for (var shift = 24; shift >= 0; shift -= 8)
          dv.setUint8(at++, val >> shift);
      return at;
  }
  
  function EBML_write_array(arr, dv, at)
  {
      for (var i = 0; i < arr.length; i++)
          dv.setUint8(at + i, arr[i]);
      return at + arr.length;
  }
  
  function EBML_write_string(str, dv, at)
  {
      for (var i = 0; i < str.length; i++)
          dv.setUint8(at + i, str.charCodeAt(i));
      return at + str.length;
  }
  
  function EBML_write_data(id, data, dv, at)
  {
      at = EBML_write_array(id, dv, at);
      if (data.length < 127)
          at = EBML_write_u1_data_len(data.length, dv, at);
      else
          at = EBML_write_u64_data_len(data.length, dv, at);
      if ((typeof data) == "string")
          at = EBML_write_string(data, dv, at);
      else
          at = EBML_write_array(data, dv, at);
      return at;
  }
  
  /*----------------------------------------------------------------------------
  **  Webm objects
  **      These classes can create the binary representation of a webm file
  **--------------------------------------------------------------------------*/
  function EBMLHeader()
  {
      this.id = EBML_HEADER;
      this.Version = 1;
      this.ReadVersion = 1;
      this.MaxIDLength = 4;
      this.MaxSizeLength = 8;
      this.DocType = "webm";
      this.DocTypeVersion = 2;  /* Not well specified by the WebM guys, but functionally required for Firefox */
      this.DocTypeReadVersion = 2;
  }
  
  EBMLHeader.prototype =
  {
      to_buffer: function(a, at)
      {
          at = at || 0;
          var dv = new DataView(a);
  
          at = EBML_write_array(this.id, dv, at);
          at = EBML_write_u64_data_len(0x1f, dv, at);
          at = EBML_write_u8_value(EBML_HEADER_VERSION, this.Version, dv, at);
          at = EBML_write_u8_value(EBML_HEADER_READ_VERSION, this.ReadVersion, dv, at);
          at = EBML_write_u8_value(EBML_HEADER_MAX_ID_LENGTH, this.MaxIDLength, dv, at);
          at = EBML_write_u8_value(EBML_HEADER_MAX_SIZE_LENGTH, this.MaxSizeLength, dv, at);
          at = EBML_write_data(EBML_HEADER_DOC_TYPE, this.DocType, dv, at);
          at = EBML_write_u8_value(EBML_HEADER_DOC_TYPE_VERSION, this.DocTypeVersion, dv, at);
          at = EBML_write_u8_value(EBML_HEADER_DOC_TYPE_READ_VERSION, this.DocTypeReadVersion, dv, at);
  
          return at;
      },
      buffer_size: function()
      {
          return 0x1f + 8 + this.id.length;
      },
  }
  
  function webm_Segment()
  {
      this.id = WEBM_SEGMENT_HEADER;
  }
  
  webm_Segment.prototype =
  {
      to_buffer: function(a, at)
      {
          at = at || 0;
          var dv = new DataView(a);
  
          at = EBML_write_array(this.id, dv, at);
          dv.setUint8(at++, 0xff);
          return at;
      },
      buffer_size: function()
      {
          return this.id.length + 1;
      },
  }
  
  function webm_SegmentInformation()
  {
      this.id = WEBM_SEGMENT_INFORMATION;
      this.timecode_scale = 1000000; /* 1 ms */
      this.muxing_app = "spice";
      this.writing_app = "spice-html5";
  
  }
  
  webm_SegmentInformation.prototype =
  {
      to_buffer: function(a, at)
      {
          at = at || 0;
          var dv = new DataView(a);
  
          at = EBML_write_array(this.id, dv, at);
          at = EBML_write_u64_data_len(this.buffer_size() - 8 - this.id.length, dv, at);
          at = EBML_write_u32_value(WEBM_TIMECODE_SCALE, this.timecode_scale, dv, at);
          at = EBML_write_data(WEBM_MUXING_APP, this.muxing_app, dv, at);
          at = EBML_write_data(WEBM_WRITING_APP, this.writing_app, dv, at);
          return at;
      },
      buffer_size: function()
      {
          return this.id.length + 8 +
                   WEBM_TIMECODE_SCALE.length + 1 + 4 +
                   WEBM_MUXING_APP.length + 1 + this.muxing_app.length +
                   WEBM_WRITING_APP.length + 1 + this.writing_app.length;
      },
  }
  
  function webm_Audio(frequency)
  {
      this.id = WEBM_AUDIO;
      this.sampling_frequency = frequency;
      this.channels = OPUS_CHANNELS;
  }
  
  webm_Audio.prototype =
  {
      to_buffer: function(a, at)
      {
          at = at || 0;
          var dv = new DataView(a);
          at = EBML_write_array(this.id, dv, at);
          at = EBML_write_u64_data_len(this.buffer_size() - 8 - this.id.length, dv, at);
          at = EBML_write_u8_value(WEBM_CHANNELS, this.channels, dv, at);
          at = EBML_write_float_value(WEBM_SAMPLING_FREQUENCY, this.sampling_frequency, dv, at);
          return at;
      },
      buffer_size: function()
      {
          return this.id.length + 8 +
              WEBM_SAMPLING_FREQUENCY.length + 1 + 4 +
              WEBM_CHANNELS.length + 1 + 1;
      },
  }
  
  
  /* ---------------------------
     SeekHead not currently used.  Hopefully not needed.
  */
  function webm_Seek(seekid, pos)
  {
      this.id = WEBM_SEEK;
      this.pos = pos;
      this.seekid = seekid;
  }
  
  webm_Seek.prototype =
  {
      to_buffer: function(a, at)
      {
          at = at || 0;
          var dv = new DataView(a);
          at = EBML_write_array(this.id, dv, at);
          at = EBML_write_u1_data_len(this.buffer_size() - 1 - this.id.length, dv, at);
  
          at = EBML_write_data(WEBM_SEEK_ID, this.seekid, dv, at)
          at = EBML_write_u16_value(WEBM_SEEK_POSITION, this.pos, dv, at)
  
          return at;
      },
      buffer_size: function()
      {
          return this.id.length + 1 +
                  WEBM_SEEK_ID.length + 1 + this.seekid.length +
                  WEBM_SEEK_POSITION.length + 1 + 2;
      },
  }
  function webm_SeekHead(info_pos, track_pos)
  {
      this.id = WEBM_SEEK_HEAD;
      this.info = new webm_Seek(WEBM_SEGMENT_INFORMATION, info_pos);
      this.track = new webm_Seek(WEBM_TRACKS, track_pos);
  }
  
  webm_SeekHead.prototype =
  {
      to_buffer: function(a, at)
      {
          at = at || 0;
          var dv = new DataView(a);
          at = EBML_write_array(this.id, dv, at);
          at = EBML_write_u64_data_len(this.buffer_size() - 8 - this.id.length, dv, at);
  
          at = this.info.to_buffer(a, at);
          at = this.track.to_buffer(a, at);
  
          return at;
      },
      buffer_size: function()
      {
          return this.id.length + 8 +
                  this.info.buffer_size() +
                  this.track.buffer_size();
      },
  }
  
  /* -------------------------------
     End of Seek Head
  */
  
  function webm_TrackEntry()
  {
      this.id = WEBM_TRACK_ENTRY;
      this.number = 1;
      this.uid = 1;
      this.type = 2; // Audio
      this.flag_enabled = 1;
      this.flag_default = 1;
      this.flag_forced = 1;
      this.flag_lacing = 0;
      this.min_cache = 0; // fixme - check
      this.max_block_addition_id = 0;
      this.codec_decode_all = 0; // fixme - check
      this.seek_pre_roll = 0; // 80000000; // fixme - check
      this.codec_delay =   80000000; // Must match codec_private.preskip
      this.codec_id = "A_OPUS";
      this.audio = new webm_Audio(OPUS_FREQUENCY);
  
      // See:  http://tools.ietf.org/html/draft-terriberry-oggopus-01
      this.codec_private = [ 0x4f, 0x70, 0x75, 0x73, 0x48, 0x65, 0x61, 0x64,  // OpusHead
                             0x01, // Version
                             OPUS_CHANNELS,
                             0x00, 0x0F, // Preskip - 3840 samples - should be 8ms at 48kHz
                             0x80, 0xbb, 0x00, 0x00,  // 48000
                             0x00, 0x00, // Output gain
                             0x00  // Channel mapping family
                             ];
  }
  
  webm_TrackEntry.prototype =
  {
      to_buffer: function(a, at)
      {
          at = at || 0;
          var dv = new DataView(a);
          at = EBML_write_array(this.id, dv, at);
          at = EBML_write_u64_data_len(this.buffer_size() - 8 - this.id.length, dv, at);
          at = EBML_write_u8_value(WEBM_TRACK_NUMBER, this.number, dv, at);
          at = EBML_write_u8_value(WEBM_TRACK_UID, this.uid, dv, at);
          at = EBML_write_u8_value(WEBM_FLAG_ENABLED, this.flag_enabled, dv, at);
          at = EBML_write_u8_value(WEBM_FLAG_DEFAULT, this.flag_default, dv, at);
          at = EBML_write_u8_value(WEBM_FLAG_FORCED, this.flag_forced, dv, at);
          at = EBML_write_u8_value(WEBM_FLAG_LACING, this.flag_lacing, dv, at);
          at = EBML_write_data(WEBM_CODEC_ID, this.codec_id, dv, at);
          at = EBML_write_u8_value(WEBM_MIN_CACHE, this.min_cache, dv, at);
          at = EBML_write_u8_value(WEBM_MAX_BLOCK_ADDITION_ID, this.max_block_addition_id, dv, at);
          at = EBML_write_u8_value(WEBM_CODEC_DECODE_ALL, this.codec_decode_all, dv, at);
          at = EBML_write_u32_value(WEBM_CODEC_DELAY, this.codec_delay, dv, at);
          at = EBML_write_u32_value(WEBM_SEEK_PRE_ROLL, this.seek_pre_roll, dv, at);
          at = EBML_write_u8_value(WEBM_TRACK_TYPE, this.type, dv, at);
          at = EBML_write_data(WEBM_CODEC_PRIVATE, this.codec_private, dv, at);
  
          at = this.audio.to_buffer(a, at);
          return at;
      },
      buffer_size: function()
      {
          return this.id.length + 8 +
              WEBM_TRACK_NUMBER.length + 1 + 1 +
              WEBM_TRACK_UID.length + 1 + 1 +
              WEBM_TRACK_TYPE.length + 1 + 1 +
              WEBM_FLAG_ENABLED.length + 1 + 1 +
              WEBM_FLAG_DEFAULT.length + 1 + 1 +
              WEBM_FLAG_FORCED.length + 1 + 1 +
              WEBM_FLAG_LACING.length + 1 + 1 +
              WEBM_MIN_CACHE.length + 1 + 1 +
              WEBM_MAX_BLOCK_ADDITION_ID.length + 1 + 1 +
              WEBM_CODEC_DECODE_ALL.length + 1 + 1 +
              WEBM_SEEK_PRE_ROLL.length + 1 + 4 +
              WEBM_CODEC_DELAY.length + 1 + 4 +
              WEBM_CODEC_ID.length + this.codec_id.length + 1 +
              WEBM_CODEC_PRIVATE.length + 1 + this.codec_private.length +
              this.audio.buffer_size();
      },
  }
  function webm_Tracks(entry)
  {
      this.id = WEBM_TRACKS;
      this.track_entry = entry;
  }
  
  webm_Tracks.prototype =
  {
      to_buffer: function(a, at)
      {
          at = at || 0;
          var dv = new DataView(a);
          at = EBML_write_array(this.id, dv, at);
          at = EBML_write_u64_data_len(this.buffer_size() - 8 - this.id.length, dv, at);
          at = this.track_entry.to_buffer(a, at);
          return at;
      },
      buffer_size: function()
      {
          return this.id.length + 8 +
                   this.track_entry.buffer_size();
      },
  }
  
  function webm_Cluster(timecode, data)
  {
      this.id = WEBM_CLUSTER;
      this.timecode = timecode;
      this.data = data;
  }
  
  webm_Cluster.prototype =
  {
      to_buffer: function(a, at)
      {
          at = at || 0;
          var dv = new DataView(a);
          at = EBML_write_array(this.id, dv, at);
          dv.setUint8(at++, 0xff);
          at = EBML_write_u32_value(WEBM_TIME_CODE, this.timecode, dv, at);
          return at;
      },
      buffer_size: function()
      {
          return this.id.length + 1 +
                   WEBM_TIME_CODE.length + 1 + 4;
      },
  }
  
  function webm_SimpleBlock(timecode, data, keyframe)
  {
      this.id = WEBM_SIMPLE_BLOCK;
      this.timecode = timecode;
      this.data = data;
      this.keyframe = keyframe;
  }
  
  webm_SimpleBlock.prototype =
  {
      to_buffer: function(a, at)
      {
          at = at || 0;
          var dv = new DataView(a);
          at = EBML_write_array(this.id, dv, at);
          at = EBML_write_u64_data_len(this.data.byteLength + 4, dv, at);
          at = EBML_write_u1_data_len(1, dv, at); // Track #
          dv.setUint16(at, this.timecode); at += 2; // timecode - relative to cluster
          dv.setUint8(at, this.keyframe ? CLUSTER_SIMPLEBLOCK_FLAG_KEYFRAME : 0); at += 1;  // flags
  
          // FIXME - There should be a better way to copy
          var u8 = new Uint8Array(this.data);
          for (var i = 0; i < this.data.byteLength; i++)
              dv.setUint8(at++, u8[i]);
  
          return at;
      },
      buffer_size: function()
      {
          return this.id.length + 8 +
                   1 + 2 + 1 +
                   this.data.byteLength;
      },
  }
  
  function webm_Header()
  {
      this.ebml = new EBMLHeader;
      this.segment = new webm_Segment;
      this.seek_head = new webm_SeekHead(0, 0);
  
      this.seek_head.info.pos = this.segment.buffer_size() + this.seek_head.buffer_size();
  
      this.info = new webm_SegmentInformation;
  
      this.seek_head.track.pos = this.seek_head.info.pos + this.info.buffer_size();
  
      this.track_entry = new webm_TrackEntry;
      this.tracks = new webm_Tracks(this.track_entry);
  }
  
  webm_Header.prototype =
  {
      to_buffer: function(a, at)
      {
          at = at || 0;
          at = this.ebml.to_buffer(a, at);
          at = this.segment.to_buffer(a, at);
          at = this.info.to_buffer(a, at);
          at = this.tracks.to_buffer(a, at);
  
          return at;
      },
      buffer_size: function()
      {
          return this.ebml.buffer_size() +
                 this.segment.buffer_size() +
                 this.info.buffer_size() +
                 this.tracks.buffer_size();
      },
  }