Menu

[r29]: / libs / OSMF / org / osmf / net / httpstreaming / f4f / BoxParser.as  Maximize  Restore  History

Download this file

636 lines (559 with data), 18.8 kB

  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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
/*****************************************************
*
* Copyright 2009 Adobe Systems Incorporated. All Rights Reserved.
*
*****************************************************
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
*
*
* The Initial Developer of the Original Code is Adobe Systems Incorporated.
* Portions created by Adobe Systems Incorporated are Copyright (C) 2009 Adobe Systems
* Incorporated. All Rights Reserved.
*
*****************************************************/
package org.osmf.net.httpstreaming.f4f
{
import __AS3__.vec.Vector;
import flash.errors.IllegalOperationError;
import flash.events.EventDispatcher;
import flash.utils.ByteArray;
CONFIG::LOGGING
{
import org.osmf.logging.Logger;
import org.osmf.logging.Log;
}
[ExcludeClass]
/**
* @private
*
* The parser that takes byte array and converts into a list of boxes in sequence.
*/
internal class BoxParser extends EventDispatcher
{
/**
* Constructor
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function BoxParser()
{
super();
_ba = null;
}
/**
* Set bytes to be parsed upon.
*
* @param ba The byte array to be parsed upon.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function init(ba:ByteArray):void
{
_ba = ba;
_ba.position = 0;
}
/**
* Read a 4 byte unsigned integer and a 4 byte string from the byte array, construct
* and return a BoxInfo object
*
* @return a BoxInfo object
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function getNextBoxInfo():BoxInfo
{
if (_ba == null || _ba.bytesAvailable < F4FConstants.FIELD_SIZE_LENGTH + F4FConstants.FIELD_TYPE_LENGTH)
{
return null;
}
var size:Number = _ba.readUnsignedInt();
var type:String = _ba.readUTFBytes(F4FConstants.FIELD_TYPE_LENGTH);
return new BoxInfo(size, type);
}
/**
* Parse and return a list of root level boxes in the order
* of the boxes in the byte array.
*
* @return the list of root level boxes.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function getBoxes():Vector.<Box>
{
var boxes:Vector.<Box> = new Vector.<Box>();
var bi:BoxInfo = getNextBoxInfo();
while (bi != null)
{
if (bi.type == F4FConstants.BOX_TYPE_ABST)
{
var abst:AdobeBootstrapBox = new AdobeBootstrapBox();
parseAdobeBootstrapBox(bi, abst);
boxes.push(abst);
}
else if (bi.type == F4FConstants.BOX_TYPE_AFRA)
{
var afra:AdobeFragmentRandomAccessBox = new AdobeFragmentRandomAccessBox();
parseAdobeFragmentRandomAccessBox(bi, afra);
boxes.push(afra);
}
else if (bi.type == F4FConstants.BOX_TYPE_MDAT)
{
var mdat:MediaDataBox = new MediaDataBox();
parseMediaDataBox(bi, mdat);
boxes.push(mdat);
}
else
{
_ba.position = _ba.position + bi.size - (F4FConstants.FIELD_SIZE_LENGTH + F4FConstants.FIELD_TYPE_LENGTH);
}
bi = getNextBoxInfo();
if (bi != null && bi.size <= 0)
{
break;
}
}
return boxes;
}
/**
* Parse and returns AFRA
*
* @return AFRA if the current byte array from the current position is indeed an AFRA, null otherwise.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function readFragmentRandomAccessBox(bi:BoxInfo):AdobeFragmentRandomAccessBox
{
var afra:AdobeFragmentRandomAccessBox = new AdobeFragmentRandomAccessBox();
parseAdobeFragmentRandomAccessBox(bi, afra);
return afra;
}
/**
* Parse and returns ABST
*
* @return ABST if the current byte array from the current position is indeed an ABST, null otherwise.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function readAdobeBootstrapBox(bi:BoxInfo):AdobeBootstrapBox
{
var abst:AdobeBootstrapBox = new AdobeBootstrapBox();
this.parseAdobeBootstrapBox(bi, abst);
return abst;
}
// Internals
//
/**
* Reads 8 bytes from the byte array, treats the 8 bytes as an long integer and pack the values
* into a Number. This is a little tricky since ActionScript does not have a long integer type.
* The solution is to read an unsigned integer which is 4 bytes long, times the unsigned integer
* by 2^32, and assign the value to a Number. Next, read the next unsigned integer, and then
* add the new unsigned integer to the existing number.
*
* @return the number that is created from the 8 bytes read from the byte array at the current position.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
internal function readLongUIntToNumber():Number
{
if (_ba == null || _ba.bytesAvailable < 8)
{
throw new IllegalOperationError("not enough length for readLongUIntToNumer");
}
var result:Number = _ba.readUnsignedInt();
result *= 4294967296.0;
result += _ba.readUnsignedInt();
return result;
}
/**
* Reads and returns an unsigned integer from the byte array at the current position.
*
* @return an unsigned integer.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
private function readUnsignedInt():uint
{
if (_ba == null || _ba.bytesAvailable < 4)
{
throw new IllegalOperationError("not enough length for readUnsignedInt");
}
return _ba.readUnsignedInt();
}
/**
* Reads the number of data bytes, specified by the length parameter, from the byte stream.
* The bytes are read into the ByteArray object specified by the bytes parameter, and the
* bytes are written into the destination ByteArray starting at the position specified by offset.
*
* @param bytes The ByteArray object to read data into.
* @param offset The offset (position) in bytes at which the read data should be written.
* @param length The number of bytes to read. The default value of 0 causes all available data to be read.
*
* @throws IllegalOperaitonError There is not sufficient data available to read.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
private function readBytes(bytes:ByteArray, offset:uint = 0, length:uint = 0):void
{
if (_ba == null || _ba.bytesAvailable < length)
{
throw new IllegalOperationError("not enough length for readBytes: " + length);
}
return _ba.readBytes(bytes, offset, length);
}
/**
* Reads an unsigned byte from the byte stream. The returned value is in the range 0 to 255.
*
* @return A 32-bit unsigned integer between 0 and 255.
*
* @throws IllegalOperaitonError There is not sufficient data available to read.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
private function readUnsignedByte():uint
{
if (_ba == null || _ba.bytesAvailable < 1)
{
throw new IllegalOperationError("not enough length for readUnsingedByte");
}
return _ba.readUnsignedByte();
}
/**
* Read a number of bytes, treat them as bytes of an unsigned integer and return the uint.
* The number of bytes must be between 0 and 4.
*
* @return A 32-bit unsigned integer converted from the list of bytes.
*
* @throws IllegalOperaitonError There is not sufficient data available to read.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
private function readBytesToUint(length:uint):uint
{
if (_ba == null || _ba.bytesAvailable < length)
{
throw new IllegalOperationError("not enough length for readUnsingedByte");
}
if (length > 4)
{
throw new IllegalOperationError("number of bytes to read must be equal or less than 4");
}
var result:uint = 0;
for (var i:uint = 0; i < length; i++)
{
result = (result << 8);
var byte:uint = _ba.readUnsignedByte();
result += byte;
}
return result;
}
/**
* Read a null terminated string.
*
* @return the null terminated string.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
private function readString():String
{
var pos:uint = _ba.position;
while (_ba.position < _ba.length)
{
var c:uint = _ba.readByte();
if (c == 0)
{
break;
}
}
var length:uint = _ba.position - pos;
_ba.position = pos;
return _ba.readUTFBytes(length);
}
// Internal
//
private function parseBox(boxInfo:BoxInfo, box:Box):void
{
var size:Number = boxInfo.size;
var boxLength:uint = F4FConstants.FIELD_SIZE_LENGTH + F4FConstants.FIELD_TYPE_LENGTH;
if (boxInfo.size == F4FConstants.FLAG_USE_LARGE_SIZE)
{
size = readLongUIntToNumber();
boxLength += F4FConstants.FIELD_LARGE_SIZE_LENGTH;
}
if (boxInfo.type == F4FConstants.EXTENDED_TYPE)
{
// Read past the extended type.
var extendedType:ByteArray = new ByteArray();
readBytes(extendedType, 0, F4FConstants.FIELD_EXTENDED_TYPE_LENGTH);
boxLength += F4FConstants.FIELD_EXTENDED_TYPE_LENGTH;
}
box.size = size;
box.type = boxInfo.type;
box.boxLength = boxLength;
}
private function parseFullBox(boxInfo:BoxInfo, fullBox:FullBox):void
{
parseBox(boxInfo, fullBox);
fullBox.version = readUnsignedByte();
fullBox.flags = readBytesToUint(FULL_BOX_FIELD_FLAGS_LENGTH);
}
private function parseAdobeBootstrapBox(boxInfo:BoxInfo, abst:AdobeBootstrapBox):void
{
parseFullBox(boxInfo, abst);
abst.bootstrapVersion = readUnsignedInt();
var temp:uint = readUnsignedByte();
abst.profile = (temp >> 6);
abst.live = ((temp & 0x20) == 0x20);
abst.update = ((temp & 0x1) == 0x1);
abst.timeScale = readUnsignedInt();
abst.currentMediaTime = readLongUIntToNumber();
abst.smpteTimeCodeOffset = readLongUIntToNumber();
abst.movieIdentifier = readString();
var serverEntryCount:uint = readUnsignedByte();
var serverBaseURLs:Vector.<String> = new Vector.<String>();
for (var i:int = 0; i < serverEntryCount; i++)
{
serverBaseURLs.push(readString());
}
abst.serverBaseURLs = serverBaseURLs;
var qualityEntryCount:uint = readUnsignedByte();
var qualitySegmentURLModifiers:Vector.<String> = new Vector.<String>();
for (i = 0; i < qualityEntryCount; i++)
{
qualitySegmentURLModifiers.push(readString());
}
abst.qualitySegmentURLModifiers = qualitySegmentURLModifiers;
abst.drmData = readString();
abst.metadata = readString();
var segmentRunTableCount:uint = readUnsignedByte();
var segmentRunTables:Vector.<AdobeSegmentRunTable> = new Vector.<AdobeSegmentRunTable>();
for (i = 0; i < segmentRunTableCount; i++)
{
boxInfo = getNextBoxInfo();
if (boxInfo.type == F4FConstants.BOX_TYPE_ASRT)
{
var asrt:AdobeSegmentRunTable = new AdobeSegmentRunTable();
parseAdobeSegmentRunTable(boxInfo, asrt);
segmentRunTables.push(asrt);
}
else
{
throw new IllegalOperationError("Unexpected data structure: " + boxInfo.type);
}
}
abst.segmentRunTables = segmentRunTables;
var fragmentRunTableCount:uint = readUnsignedByte();
var fragmentRunTables:Vector.<AdobeFragmentRunTable> = new Vector.<AdobeFragmentRunTable>();
for (i = 0; i < fragmentRunTableCount; i++)
{
boxInfo = getNextBoxInfo();
if (boxInfo.type == F4FConstants.BOX_TYPE_AFRT)
{
var afrt:AdobeFragmentRunTable = new AdobeFragmentRunTable();
parseAdobeFragmentRunTable(boxInfo, afrt);
fragmentRunTables.push(afrt);
}
else
{
throw new IllegalOperationError("Unexpected data structure: " + boxInfo.type);
}
}
abst.fragmentRunTables = fragmentRunTables;
}
private function parseAdobeSegmentRunTable(boxInfo:BoxInfo, asrt:AdobeSegmentRunTable):void
{
parseFullBox(boxInfo, asrt);
var qualityEntryCount:uint = readUnsignedByte();
var qualitySegmentURLModifiers:Vector.<String> = new Vector.<String>();
for (var i:uint = 0; i < qualityEntryCount; i++)
{
qualitySegmentURLModifiers.push(readString());
}
asrt.qualitySegmentURLModifiers = qualitySegmentURLModifiers;
var entryCount:uint = readUnsignedInt();
for (i = 0; i < entryCount; i++)
{
asrt.addSegmentFragmentPair(new SegmentFragmentPair(readUnsignedInt(), readUnsignedInt()));
}
}
private function parseAdobeFragmentRunTable(boxInfo:BoxInfo, afrt:AdobeFragmentRunTable):void
{
parseFullBox(boxInfo, afrt);
afrt.timeScale = readUnsignedInt();
var qualityEntryCount:uint = readUnsignedByte();
var qualitySegmentURLModifiers:Vector.<String> = new Vector.<String>();
for (var i:uint = 0; i < qualityEntryCount; i++)
{
qualitySegmentURLModifiers.push(readString());
}
afrt.qualitySegmentURLModifiers = qualitySegmentURLModifiers;
var entryCount:uint = readUnsignedInt();
for (i = 0; i < entryCount; i++)
{
var fdp:FragmentDurationPair = new FragmentDurationPair();
parseFragmentDurationPair(fdp);
afrt.addFragmentDurationPair(fdp);
}
}
private function parseFragmentDurationPair(fdp:FragmentDurationPair):void
{
fdp.firstFragment = readUnsignedInt();
fdp.durationAccrued = readLongUIntToNumber();
fdp.duration = readUnsignedInt();
if (fdp.duration == 0)
{
fdp.discontinuityIndicator = readUnsignedByte();
}
CONFIG::LOGGING
{
logger.debug(" firstFragment=" + fdp.firstFragment +
" duration=" + fdp.duration +
" durationAccrued=" + fdp.durationAccrued +
" discontinuityIndicator=" + fdp.discontinuityIndicator);
}
}
private function parseAdobeFragmentRandomAccessBox(boxInfo:BoxInfo, afra:AdobeFragmentRandomAccessBox):void
{
parseFullBox(boxInfo, afra);
var sizes:uint = readBytesToUint(1);
var longIdFields:Boolean = ((sizes & AFRA_MASK_LONG_ID) > 0);
var longOffsetFields:Boolean = ((sizes & AFRA_MASK_LONG_OFFSET) > 0);
var globalEntries:Boolean = ((sizes & AFRA_MASK_GLOBAL_ENTRIES) > 0);
afra.timeScale = readUnsignedInt();
CONFIG::LOGGING
{
logger.debug("------------------- AFRA -------------------");
logger.debug(" timeScale=" + afra.timeScale);
}
var entryCount:uint = readUnsignedInt();
var localRandomAccessEntries:Vector.<LocalRandomAccessEntry> = new Vector.<LocalRandomAccessEntry>();
for (var i:uint = 0; i < entryCount; i++)
{
var lrae:LocalRandomAccessEntry = new LocalRandomAccessEntry();
parseLocalRandomAccessEntry(lrae, longOffsetFields);
localRandomAccessEntries.push(lrae);
}
afra.localRandomAccessEntries = localRandomAccessEntries;
var globalRandomAccessEntries:Vector.<GlobalRandomAccessEntry> = new Vector.<GlobalRandomAccessEntry>();
if (globalEntries)
{
entryCount = readUnsignedInt();
for (i = 0; i < entryCount; i++)
{
var grae:GlobalRandomAccessEntry = new GlobalRandomAccessEntry();
parseGlobalRandomAccessEntry(grae, longIdFields, longOffsetFields);
globalRandomAccessEntries.push(grae);
}
}
afra.globalRandomAccessEntries = globalRandomAccessEntries;
}
private function parseLocalRandomAccessEntry(lrae:LocalRandomAccessEntry, longOffsetFields:Boolean):void
{
lrae.time = readLongUIntToNumber();
if (longOffsetFields)
{
lrae.offset = readLongUIntToNumber();
}
else
{
lrae.offset = readUnsignedInt();
}
CONFIG::LOGGING
{
logger.debug(" time=" + lrae.time + " offset=" + lrae.offset);
}
}
private function parseGlobalRandomAccessEntry(grae:GlobalRandomAccessEntry, longIdFields:Boolean, longOffsetFields:Boolean):void
{
grae.time = readLongUIntToNumber();
if (longIdFields)
{
grae.segment = readUnsignedInt();
grae.fragment = readUnsignedInt();
}
else
{
grae.segment = readBytesToUint(2);
grae.fragment = readBytesToUint(2);
}
if (longOffsetFields)
{
grae.afraOffset = readLongUIntToNumber();
grae.offsetFromAfra = readLongUIntToNumber();
}
else
{
grae.afraOffset = readUnsignedInt();
grae.offsetFromAfra = readUnsignedInt();
}
CONFIG::LOGGING
{
logger.debug(
" time=" + grae.time +
" segment=" + grae.segment +
" fragment=" + grae.fragment +
" afraOffset=" + grae.afraOffset +
" offsetFromAfra=" + grae.offsetFromAfra);
}
}
private function parseMediaDataBox(boxInfo:BoxInfo, mdat:MediaDataBox):void
{
parseBox(boxInfo, mdat);
var data:ByteArray = new ByteArray();
readBytes(data, 0, mdat.size - mdat.boxLength);
mdat.data = data;
}
private var _ba:ByteArray;
private static const FULL_BOX_FIELD_FLAGS_LENGTH:uint = 3;
private static const AFRA_MASK_LONG_ID:uint = 128;
private static const AFRA_MASK_LONG_OFFSET:uint = 64;
private static const AFRA_MASK_GLOBAL_ENTRIES:uint = 32;
CONFIG::LOGGING
{
private static const logger:Logger = Log.getLogger("org.osmf.net.httpstreaming.f4f.BoxParser");;
}
}
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.