17dd1e68 by David LaPalomento

Create test harness and start porting segment parser

Create a qunit test to verify the flv header. Start work on parsing the m2ts packets. The test harness is using a hard-coded Uint8Array which is the first segment of the "bipbop" video. Currently the segment parser is consuming bytes and passing them off to the internal packet parsing function but that isn't yet implemented.
1 parent 7f0a59b2
......@@ -53,7 +53,7 @@ module.exports = function(grunt) {
options: {
jshintrc: 'test/.jshintrc'
},
src: ['test/**/*.js']
src: ['test/**/*.js', '!test/tsSegment.js']
},
},
watch: {
......
{
"curly": true,
"eqeqeq": true,
"globals": {
"console": true
},
"immed": true,
"latedef": true,
"newcap": true,
......
/*
* aac-stream
*
*
* Copyright (c) 2013 Brightcove
* All rights reserved.
*/
(function(window) {
window.videojs.hls.AacStream = function(){
this.tags = [];
};
})(this);
(function(window) {
window.videojs.hls.FlvTag = function() {};
/*
package com.videojs.providers.hls.utils{
import flash.utils.ByteArray;
import flash.utils.Endian;
public class FlvTag extends ByteArray
{
public static const AUDIO_TAG:uint = 0x08;
public static const VIDEO_TAG:uint = 0x09;
public static const METADATA_TAG:uint = 0x12;
public var keyFrame:Boolean = false;
private var extraData:Boolean = false;
private var adHoc:uint = 0; // Counter if this is a metadata tag, nal start marker if this is a video tag. unused if this is an audio tag
public var pts:uint;
public var dts:uint;
public static function isAudioFrame(tag:ByteArray):Boolean
{
return AUDIO_TAG == tag[0];
}
public static function isVideoFrame(tag:ByteArray):Boolean
{
return VIDEO_TAG == tag[0];
}
public static function isMetaData(tag:ByteArray):Boolean
{
return METADATA_TAG == tag[0];
}
public static function isKeyFrame(tag:ByteArray):Boolean
{
if ( isVideoFrame(tag) )
return tag[11] == 0x17;
if( isAudioFrame(tag) )
return true;
if( isMetaData(tag) )
return true;
return false;
}
public static function frameTime(tag:ByteArray):uint
{
var pts:uint = tag[ 4] << 16;
pts |= tag[ 5] << 8;
pts |= tag[ 6] << 0;
pts |= tag[ 7] << 24;
return pts;
}
public function FlvTag(type:uint, ed:Boolean = false)
{
super();
extraData = ed;
this.endian = Endian.BIG_ENDIAN;
switch(type)
{
case VIDEO_TAG: this.length = 16; break;
case AUDIO_TAG: this.length = 13; keyFrame = true; break;
case METADATA_TAG: this.length = 29; keyFrame = true; break;
default: throw("Error Unknown TagType");
}
this[0] = type
this.position = this.length;
keyFrame = extraData; // Defaults to false
pts = dts = 0;
}
// Negative index into array
public function negIndex(pos:uint):int
{
return this[this.length - pos];
}
// The functions below ONLY work when this[0] == VIDEO_TAG.
// We are not going to check for that because we dont want the overhead
public function nalUnitSize(nal:ByteArray = null):int
{
if( 0 == adHoc )
return 0;
return this.length - ( adHoc + 4 );
}
public function startNalUnit():void
{ // remember position and add 4 bytes
if ( 0 < adHoc )
{
throw new Error("Attempted to create new NAL wihout closing the old one");
}
// reserve 4 bytes for nal unit size
adHoc = this.length;
this.length += 4;
this.position = this.length;
}
public function endNalUnit(nal:ByteArray = null):void
{ // Rewind to the marker and write the size
if ( this.length == adHoc + 4 )
{
this.length -= 4; // we started a nal unit, but didnt write one, so roll back the 4 byte size value
}
else
if ( 0 < adHoc )
{
var nalStart:uint = adHoc + 4;
var nalLength:uint = this.length - nalStart;
this.position = adHoc;
this.writeUnsignedInt( nalLength );
this.position = this.length;
if ( null != nal ) // If the user pass in a ByteArray, copy the NAL to it.
nal.writeBytes( this, nalStart, nalLength );
}
adHoc = 0;
}
public function writeMetaDataDouble(key:String, val:Number):void
{
writeShort ( key.length );
writeUTFBytes ( key );
writeByte ( 0x00 );
writeDouble ( val );
++adHoc;
}
public function writeMetaDataBoolean(key:String, val:Boolean):void
{
writeShort ( key.length );
writeUTFBytes ( key );
writeByte ( 0x01 );
writeByte ( true == val ? 0x01 : 0x00 );
++adHoc;
}
public function finalize():ByteArray
{
switch(this[0])
{ // Video Data
case VIDEO_TAG:
this[11] = ( ( keyFrame || extraData ) ? 0x10 : 0x20 ) | 0x07; // We only support AVC, 1 = key frame (for AVC, a seekable frame), 2 = inter frame (for AVC, a non-seekable frame)
this[12] = extraData ? 0x00 : 0x01;
var dtsDelta:int = pts - dts;
this[13] = ( dtsDelta & 0x00FF0000 ) >>> 16;
this[14] = ( dtsDelta & 0x0000FF00 ) >>> 8;
this[15] = ( dtsDelta & 0x000000FF ) >>> 0;
break;
case AUDIO_TAG:
this[11] = 0xAF;
this[12] = extraData ? 0x00 : 0x01;
break;
case METADATA_TAG:
this.position = 11;
writeByte(0x02); // String type
writeShort(0x0A); // 10 Bytes
writeUTFBytes("onMetaData");
writeByte(0x08); // Array type
writeUnsignedInt( adHoc );
this.position = this.length;
writeUnsignedInt( 0x09 ); // End Data Tag
break;
}
var len:int = this.length - 11;
this[ 1] = ( len & 0x00FF0000 ) >>> 16;
this[ 2] = ( len & 0x0000FF00 ) >>> 8;
this[ 3] = ( len & 0x000000FF ) >>> 0;
this[ 4] = ( pts & 0x00FF0000 ) >>> 16;
this[ 5] = ( pts & 0x0000FF00 ) >>> 8;
this[ 6] = ( pts & 0x000000FF ) >>> 0;
this[ 7] = ( pts & 0xFF000000 ) >>> 24;
this[ 8] = 0;
this[ 9] = 0;
this[10] = 0;
this.writeUnsignedInt( this.length );
return this;
}
}
}
*/
})(this);
/*
* h264-stream
*
*
* Copyright (c) 2013 Brightcove
* All rights reserved.
*/
(function(window) {
window.videojs.hls.H264Stream = function(){
this.tags = [];
};
})(this);
(function(window) {
var
FlvTag = window.videojs.hls.FlvTag,
H264Stream = window.videojs.hls.H264Stream,
AacStream = window.videojs.hls.AacStream,
m2tsPacketSize = 188;
console.assert(H264Stream);
console.assert(AacStream);
window.videojs.hls.SegmentParser = function() {
var
self = this,
parseTSPacket,
pmtPid,
streamBuffer = new Uint8Array(m2tsPacketSize),
streamBufferByteCount = 0,
videoPid,
h264Stream = new H264Stream(),
audioPid,
aacStream = new AacStream(),
seekToKeyFrame = false;
// For information on the FLV format, see
// http://download.macromedia.com/f4v/video_file_format_spec_v10_1.pdf.
// Technically, this function returns the header and a metadata FLV tag
// if duration is greater than zero
// duration in seconds
self.getFlvHeader = function(duration, audio, video) { // :ByteArray {
var
headBytes = new Uint8Array(3 + 1 + 1 + 4),
head = new DataView(headBytes.buffer),
metadata,
result;
// default arguments
duration = duration || 0;
audio = audio === undefined? true : audio;
video = video === undefined? true : video;
// signature
head.setUint8(0, 0x46); // 'F'
head.setUint8(1, 0x4c); // 'L'
head.setUint8(2, 0x56); // 'V'
// version
head.setUint8(3, 0x01);
// flags
head.setUint8(4, (audio ? 0x04 : 0x00) | (video ? 0x01 : 0x00));
// data offset, should be 9 for FLV v1
head.setUint32(5, headBytes.byteLength);
// init the first FLV tag
if (duration <= 0) {
// no duration available so just write the first field of the first
// FLV tag
result = new Uint8Array(headBytes.byteLength + 4);
result.set(headBytes);
result.set([0, 0, 0, 0], headBytes.byteLength);
return result;
}
// write out the duration metadata tag
metadata = new FlvTag(FlvTag.METADATA_TAG);
metadata.pts = metadata.dts = 0;
metadata.writeMetaDataDouble("duration", duration);
result = new Uint8Array(headBytes.byteLength + metadata.byteLength);
result.set(head);
result.set(head.bytesLength, metadata.finalize());
return result;
};
self.flushTags = function() {
h264Stream.finishFrame();
};
self.doSeek = function() {
self.flushTags();
aacStream.tags.length = 0;
h264Stream.tags.length = 0;
seekToKeyFrame = true;
};
self.tagsAvailable = function() { // :int {
var i, pts; // :uint
if (seekToKeyFrame) {
for (i = 0 ; i < h264Stream.tags.length && seekToKeyFrame; ++i) {
if (h264Stream.tags[i].keyFrame) {
seekToKeyFrame = false; // We found, a keyframe, stop seeking
}
}
if (seekToKeyFrame) {
// we didnt find a keyframe. yet
h264Stream.tags.length = 0;
return 0;
}
// TODO we MAY need to use dts, not pts
h264Stream.tags = h264Stream.tags.slice(i);
pts = h264Stream.tags[0].pts;
// Remove any audio before the found keyframe
while( 0 < aacStream.tags.length && pts > aacStream.tags[0].pts ) {
aacStream.tags.shift();
}
}
return h264Stream.tags.length + aacStream.tags.length;
};
self.getNextTag = function() { // :ByteArray {
var tag; // :FlvTag; // return tags in approximate dts order
if (0 === self.tagsAvailable()) {
throw new Error("getNextTag() called when 0 == tagsAvailable()");
}
if (0 < h264Stream.tags.length) {
if (0 < aacStream.tags.length && aacStream.tags[0].dts < h264Stream.tags[0].dts) {
tag = aacStream.tags.shift();
} else {
tag = h264Stream.tags.shift();
}
} else if ( 0 < aacStream.tags.length ) {
tag = aacStream.tags.shift();
} else {
// We dont have any tags available to return
return new Uint8Array();
}
return tag.finalize();
};
self.parseSegmentBinaryData = function(data) { // :ByteArray) {
var
dataPosition = 0,
dataSlice;
// To avoid an extra copy, we will stash overflow data, and only
// reconstruct the first packet. The rest of the packets will be
// parsed directly from data
if (streamBufferByteCount > 0) {
if (data.byteLength + streamBufferByteCount < m2tsPacketSize) {
// the current data is less than a single m2ts packet, so stash it
// until we receive more
// ?? this seems to append streamBuffer onto data and then just give up. I'm not sure why that would be interesting.
console.log('data.length + streamBuffer.length < m2tsPacketSize ??');
streamBuffer.readBytes(data, data.length, streamBuffer.length);
return;
} else {
// we have enough data for an m2ts packet
// process it immediately
dataSlice = data.subarray(0, m2tsPacketSize - streamBufferByteCount);
streamBuffer.set(dataSlice, streamBufferByteCount);
parseTSPacket(streamBuffer);
// reset the buffer
streamBuffer = new Uint8Array(m2tsPacketSize);
streamBufferByteCount = 0;
}
}
while (true) {
// Make sure we are TS aligned
while(dataPosition < data.byteLength && data[dataPosition] !== 0x47) {
// If there is no sync byte skip forward until we find one
// TODO if we find a sync byte, look 188 bytes in the future (if
// possible). If there is not a sync byte there, keep looking
dataPosition++;
}
// base case: not enough data to parse a m2ts packet
if (data.byteLength - dataPosition < m2tsPacketSize) {
if (data.byteLength - dataPosition > 0) {
// there are bytes remaining, save them for next time
streamBuffer.set(data.subarray(dataPosition),
streamBufferByteCount);
streamBufferByteCount += data.byteLength - dataPosition;
}
return;
}
// attempt to parse a m2ts packet
if (parseTSPacket(data.subarray(dataPosition, m2tsPacketSize))) {
dataPosition += m2tsPacketSize;
} else {
// If there was an error parsing a TS packet. it could be
// because we are not TS packet aligned. Step one forward by
// one byte and allow the code above to find the next
console.log('error parsing m2ts packet, attempting to re-align');
dataPosition++;
}
}
};
// TODO add more testing to make sure we dont walk past the end of a TS
// packet!
parseTSPacket = function(data) { // :ByteArray):Boolean {
var
s = data.position, //:uint
o = s, // :uint
e = o + m2tsPacketSize, // :uint
// Don't look for a sync byte. We handle that in
// parseSegmentBinaryData()
// Payload Unit Start Indicator
pusi = !!(data[o+1] & 0x40),
// PacketId
pid = (data[o + 1] & 0x1F) << 8 | data[o+2],
afflag = (data[o + 3] & 0x30 ) >>> 4,
aflen, // :uint
patTableId, // :int
patCurrentNextIndicator, // Boolean
patSectionLength, // :uint
pesPacketSize, // :int,
dataAlignmentIndicator, // :Boolean,
ptsDtsIndicator, // :int
pesHeaderLength, // :int
pts, // :uint
dts, // :uint
pmtTableId, // :int
pmtCurrentNextIndicator, // :Boolean
pmtSectionLength, // :uint
streamType, // :int
elementaryPID, // :int
ESInfolength; // :int
// Continuity Counter we could use this for sanity check, and
// corrupt stream detection
// cc = (data[o + 3] & 0x0F);
// Done with TS header
o += 4;
if (afflag > 0x01) { // skip most of the adaption field
aflen = data[o];
o += aflen + 1;
}
if (0x0000 === pid) {
// always test for PMT first! (becuse other variables default to 0)
// if pusi is set we must skip X bytes (PSI pointer field)
o += pusi ? 1 + data[o] : 0;
patTableId = data[o];
console.assert(0x00 === patTableId, 'patTableId should be 0x00');
patCurrentNextIndicator = !!(data[o+5] & 0x01);
if (patCurrentNextIndicator) {
patSectionLength = (data[o + 1] & 0x0F) << 8 | data[o + 2];
o += 8; // skip past PSI header
// We currently only support streams with 1 program
patSectionLength = (patSectionLength - 9) / 4;
if (1 !== patSectionLength) {
throw new Error("TS has more that 1 program");
}
// if we ever support more that 1 program (unlikely) loop over them here
// var programNumber = data[o + 0] << 8 | data[o + 1];
// var programId = (data[o+2] & 0x1F) << 8 | data[o + 3];
pmtPid = (data[o + 2] & 0x1F) << 8 | data[o + 3];
}
// We could test the CRC here to detect corruption with extra CPU cost
} else if (videoPid === pid || audioPid === pid) {
if (pusi) {
// comment out for speed
// if( 0x00 != data[o+0] || 0x00 != data[o+1] || 0x01 != data[o+2] )
// {// look for PES start code
// throw new Error("PES did not begin with start code");
// }
// var sid:int = data[o+3]; // StreamID
pesPacketSize = (data[o + 4] << 8) | data[o + 5];
dataAlignmentIndicator = !!((data[o + 6] & 0x04) >>> 2);
ptsDtsIndicator = (data[o + 7] & 0xC0) >>> 6;
pesHeaderLength = data[o + 8]; // TODO sanity check header length
o += 9; // Skip past PES header
// PTS and DTS are normially stored as a 33 bit number.
// JavaScript does not have a integer type larger than 32 bit
// BUT, we need to convert from 90ns to 1ms time scale anyway.
// so what we are going to do instead, is drop the least
// significant bit (the same as dividing by two) then we can
// divide by 45 (45 * 2 = 90) to get ms.
if (ptsDtsIndicator & 0x03) {
pts = (data[o + 0] & 0x0E) << 28
| (data[o + 1] & 0xFF) << 21
| (data[o + 2] & 0xFE) << 13
| (data[o + 3] & 0xFF) << 6
| (data[o + 4] & 0xFE) >>> 2;
pts /= 45;
if (ptsDtsIndicator & 0x01) {// DTS
dts = (data[o + 5] & 0x0E ) << 28
| (data[o + 6] & 0xFF ) << 21
| (data[o + 7] & 0xFE ) << 13
| (data[o + 8] & 0xFF ) << 6
| (data[o + 9] & 0xFE ) >>> 2;
dts /= 45;
} else {
dts = pts;
}
}
// Skip past "optional" portion of PTS header
o += pesHeaderLength;
if (videoPid === pid) {
// Stash this frame for future use.
// console.assert(videoFrames.length < 3);
h264Stream.setNextTimeStamp(pts,
dts,
dataAlignmentIndicator);
} else if (audioPid === pid) {
aacStream.setNextTimeStamp(pts,
pesPacketSize,
dataAlignmentIndicator);
}
}
if (audioPid === pid) {
aacStream.writeBytes(data,o,e-o);
} else if (videoPid === pid) {
h264Stream.writeBytes(data,o,e-o);
}
} else if (pmtPid === pid) {
// TODO sanity check data[o]
// if pusi is set we must skip X bytes (PSI pointer field)
o += ( pusi ? 1 + data[o] : 0 );
pmtTableId = data[o];
console.assert(0x02 === pmtTableId);
pmtCurrentNextIndicator = !!(data[o + 5] & 0x01);
if (pmtCurrentNextIndicator) {
audioPid = videoPid = 0;
pmtSectionLength = (data[o + 1] & 0x0F ) << 8 | data[o + 2];
// skip CRC and PSI data we dont care about
pmtSectionLength -= 13;
o += 12; // skip past PSI header and some PMT data
while (0 < pmtSectionLength) {
streamType = data[o + 0];
elementaryPID = (data[o + 1] & 0x1F) << 8 | data[o + 2];
ESInfolength = (data[o + 3] & 0x0F ) << 8 | data[o + 4];
o += 5 + ESInfolength;
pmtSectionLength -= 5 + ESInfolength;
if (0x1B === streamType) {
if (0 !== videoPid) {
throw new Error("Program has more than 1 video stream");
}
videoPid = elementaryPID;
} else if (0x0F === streamType) {
if (0 !== audioPid) {
throw new Error("Program has more than 1 audio Stream");
}
audioPid = elementaryPID;
}
// TODO add support for MP3 audio
}
}
// We could test the CRC here to detect corruption with extra CPU cost
} else if (0x0011 === pid) {
// Service Description Table
} else if (0x1FFF === pid) {
// NULL packet
} else {
console.log("Unknown PID " + pid);
}
return true;
};
};
})(this);
......@@ -6,6 +6,8 @@
* All rights reserved.
*/
(function() {
(function(window) {
})();
window.videojs.hls = {};
})(this);
......
This diff could not be displayed because it is too large.
......@@ -6,8 +6,46 @@
<!-- Load local QUnit. -->
<link rel="stylesheet" href="../libs/qunit/qunit.css" media="screen">
<script src="../libs/qunit/qunit.js"></script>
<!-- Load local lib and tests. -->
<!-- video.js -->
<script src="../libs/video-js/src/js/core.js"></script>
<script src="../libs/video-js/src/js/core-object.js"></script>
<script src="../libs/video-js/src/js/events.js"></script>
<script src="../libs/video-js/src/js/lib.js"></script>
<script src="../libs/video-js/src/js/component.js"></script>
<script src="../libs/video-js/src/js/button.js"></script>
<script src="../libs/video-js/src/js/slider.js"></script>
<script src="../libs/video-js/src/js/menu.js"></script>
<script src="../libs/video-js/src/js/player.js"></script>
<script src="../libs/video-js/src/js/control-bar/control-bar.js"></script>
<script src="../libs/video-js/src/js/control-bar/play-toggle.js"></script>
<script src="../libs/video-js/src/js/control-bar/time-display.js"></script>
<script src="../libs/video-js/src/js/control-bar/fullscreen-toggle.js"></script>
<script src="../libs/video-js/src/js/control-bar/progress-control.js"></script>
<script src="../libs/video-js/src/js/control-bar/volume-control.js"></script>
<script src="../libs/video-js/src/js/control-bar/mute-toggle.js"></script>
<script src="../libs/video-js/src/js/control-bar/volume-menu-button.js"></script>
<script src="../libs/video-js/src/js/poster.js"></script>
<script src="../libs/video-js/src/js/loading-spinner.js"></script>
<script src="../libs/video-js/src/js/big-play-button.js"></script>
<script src="../libs/video-js/src/js/media/media.js"></script>
<script src="../libs/video-js/src/js/media/html5.js"></script>
<script src="../libs/video-js/src/js/media/flash.js"></script>
<script src="../libs/video-js/src/js/media/loader.js"></script>
<script src="../libs/video-js/src/js/tracks.js"></script>
<script src="../libs/video-js/src/js/json.js"></script>
<script src="../libs/video-js/src/js/setup.js"></script>
<script src="../libs/video-js/src/js/plugins.js"></script>
<!-- HLS plugin -->
<script src="../src/video-js-hls.js"></script>
<script src="../src/h264-stream.js"></script>
<script src="../src/aac-stream.js"></script>
<script src="../src/flv-tag.js"></script>
<script src="../src/segment-parser.js"></script>
<!-- an example MPEG2-TS segment -->
<script src="tsSegment.js"></script>
<script src="video-js-hls_test.js"></script>
</head>
<body>
......
(function() {
(function(window) {
/*
======== A Handy Little QUnit Reference ========
http://api.qunitjs.com/
......@@ -19,15 +19,33 @@
notStrictEqual(actual, expected, [message])
throws(block, [expected], [message])
*/
var parser;
module('environment', {
// This will run before each test in this module.
setup: function() {
}
});
module('environment');
test('is sane', function() {
expect(1);
ok(true);
});
}());
module('segment parser', {
setup: function() {
parser = new window.videojs.hls.SegmentParser();
}
});
test('creates an flv header', function() {
var header = parser.getFlvHeader();
ok(header, 'the header is truthy');
equal(9 + 4, header.byteLength, 'the header length is correct');
equal(header[0], 'F'.charCodeAt(0), 'the signature is correct');
equal(header[1], 'L'.charCodeAt(0), 'the signature is correct');
equal(header[2], 'V'.charCodeAt(0), 'the signature is correct');
});
test('parses the first bipbop segment', function() {
parser.parseSegmentBinaryData(window.testSegment);
ok(parser.tagsAvailable(), 'tags should be available');
});
})(this);
......