df9faa3f by David LaPalomento

Whitespace and formatting cleanup

Remove a remaining tabs. Standardize whitespace usage around declarations and keywords. Lift and unify `var` declarations.
1 parent e95cfe5a
(function (window) {
window.videojs.hls.HLSPlaybackController = function (vjsPlayerReference) {
var ManifestController = window.videojs.hls.ManifestController;
var SegmentController = window.videojs.hls.SegmentController;
var MediaSource = window.videojs.MediaSource;
var SegmentParser = window.videojs.hls.SegmentParser;
var M3U8 = window.videojs.hls.M3U8;
(function(window) {
var
ManifestController = window.videojs.hls.ManifestController,
SegmentController = window.videojs.hls.SegmentController,
MediaSource = window.videojs.MediaSource,
SegmentParser = window.videojs.hls.SegmentParser,
M3U8 = window.videojs.hls.M3U8;
window.videojs.hls.HLSPlaybackController = function(player) {
var self = this;
self.player = vjsPlayerReference;
self.player = player;
self.mediaSource = new MediaSource();
self.parser = new SegmentParser();
self.manifestController = null;
self.segmentController = null;
self.manifestLoaded = false;
self.currentSegment = 0;
self.currentManifest = null;
self.currentPlaylist = null;
self.currentRendition = null;
// Register Externall Callbacks
self.manifestLoadCompleteCallback;
self.player.on('timeupdate', function () {
console.log(self.player.currentTime());
});
self.player.on('onsrcchange', function () {
console.log('src change', self.player.currentSrc());
//if src.url.m3u8 -- loadManifest.url
});
self.rendition = function (rendition) {
// register external callbacks
self.rendition = function(rendition) {
self.currentRendition = rendition;
self.loadManifest(self.currentRendition.url, self.onM3U8LoadComplete, self.onM3U8LoadError, self.onM3U8Update);
};
self.loadManifest = function (manifestUrl, onDataCallback, onErrorCallback, onUpdateCallback) {
self.mediaSource.addEventListener('sourceopen', function (event) {
console.log('source open here');
self.loadManifest = function(manifestUrl, onDataCallback, onErrorCallback, onUpdateCallback) {
self.mediaSource.addEventListener('sourceopen', function(event) {
// feed parsed bytes into the player
self.sourceBuffer = self.mediaSource.addSourceBuffer('video/flv; codecs="vp6,aac"');
......@@ -47,8 +32,7 @@
self.sourceBuffer.appendBuffer(self.parser.getFlvHeader(), video);
if( onDataCallback )
{
if (onDataCallback) {
self.manifestLoadCompleteCallback = onDataCallback;
}
......@@ -63,10 +47,9 @@
});
};
self.onM3U8LoadComplete = function (m3u8) {
if (m3u8.invalidReasons.length == 0) {
if(m3u8.isPlaylist)
{
self.onM3U8LoadComplete = function(m3u8) {
if (m3u8.invalidReasons.length === 0) {
if (m3u8.isPlaylist) {
self.currentPlaylist = m3u8;
self.rendition(self.currentPlaylist.playlistItems[0]);
} else {
......@@ -75,52 +58,39 @@
self.loadSegment(self.currentManifest.mediaItems[0]);
if(self.manifestLoadCompleteCallback)
{
if (self.manifestLoadCompleteCallback) {
self.manifestLoadCompleteCallback(m3u8);
}
}
}
};
self.onM3U8LoadError = function (error) {
};
self.onM3U8Update = function (m3u8) {
};
self.onM3U8LoadError = function(error) {};
self.onM3U8Update = function(m3u8) {};
self.loadSegment = function(segment) {
self.segmentController = new SegmentController();
self.segmentController.loadSegment(segment.url, self.onSegmentLoadComplete, self.onSegmentLoadError);
};
self.onSegmentLoadComplete = function (segment) {
self.onSegmentLoadComplete = function(segment) {
self.parser.parseSegmentBinaryData(segment.binaryData);
while (self.parser.tagsAvailable()) {
self.sourceBuffer.appendBuffer(self.parser.getNextTag().bytes, self.player);
};
console.log('load another',self.currentSegment,self.currentManifest.mediaItems.length);
}
if(self.currentSegment < self.currentManifest.mediaItems.length-1)
{
console.log('load another');
if (self.currentSegment < self.currentManifest.mediaItems.length-1) {
self.loadNextSegment();
}
};
self.loadNextSegment = function () {
self.loadNextSegment = function() {
self.currentSegment++;
self.loadSegment(self.currentManifest.mediaItems[self.currentSegment]);
}
self.onSegmentLoadError = function (error) {
};
self.onSegmentLoadError = function(error) {};
};
})(this);
......
......@@ -2,169 +2,150 @@
var M3U8 = window.videojs.hls.M3U8;
window.videojs.hls.M3U8Parser = function() {
var self = this;
self.directory;
var tagTypes = window.videojs.hls.m3u8TagType;
var lines = [];
var data;
self.getTagType = function( lineData ) {
for ( var s in tagTypes )
{
if (lineData.indexOf(tagTypes[s]) == 0)
{
return tagTypes[s];
}
var
self = this,
tagTypes = window.videojs.hls.m3u8TagType,
lines = [],
data;
self.getTagType = function(lineData) {
for (var s in tagTypes) {
if (lineData.indexOf(tagTypes[s]) === 0) {
return tagTypes[s];
}
}
}
self.getTagValue = function ( lineData ) {
for ( var s in tagTypes )
{
if (lineData.indexOf(tagTypes[s]) == 0)
{
return lineData.substr(tagTypes[s].length);
}
};
self.getTagValue = function(lineData) {
for (var s in tagTypes) {
if (lineData.indexOf(tagTypes[s]) === 0) {
return lineData.substr(tagTypes[s].length);
}
}
}
};
self.parse = function( rawDataString ) {
self.parse = function(rawDataString) {
data = new M3U8();
if(self.directory)
{
data.directory = self.directory;
if (self.directory) {
data.directory = self.directory;
}
if( rawDataString != undefined && rawDataString.toString().length > 0 )
{
lines = rawDataString.split('\n');
lines.forEach(
function(value,index) {
switch( self.getTagType(value) )
{
case tagTypes.EXTM3U:
data.hasValidM3UTag = (index == 0);
if(!data.hasValidM3UTag)
{
data.invalidReasons.push("Invalid EXTM3U Tag");
}
break;
case tagTypes.DISCONTINUITY:
break;
case tagTypes.PLAYLIST_TYPE:
if(self.getTagValue(value) == "VOD" || self.getTagValue(value) == "EVENT")
{
data.playlistType = self.getTagValue(value);
data.isPlaylist = true;
} else {
data.invalidReasons.push("Invalid Playlist Type Value");
}
break;
case tagTypes.EXTINF:
var segment = {url: "unknown", byterange: -1, targetDuration: data.targetDuration };
if( self.getTagType(lines[index+1]) == tagTypes.BYTERANGE )
{
segment.byterange = self.getTagValue(lines[index+1]).split('@');
segment.url = lines[index+2];
} else
{
segment.url = lines[index+1];
}
if(segment.url.indexOf("http")===-1 && self.directory)
{
if(data.directory[data.directory.length-1] === segment.url[0] && segment.url[0] === "/")
{
segment.url = segment.url.substr(1);
}
segment.url = self.directory + segment.url;
}
data.mediaItems.push(segment);
break;
case tagTypes.STREAM_INF:
var rendition = {};
var attributes = value.substr(tagTypes.STREAM_INF.length).split(',');
attributes.forEach(function(attr_value,attr_index) {
if(isNaN(attr_value.split('=')[1])){
rendition[attr_value.split('=')[0].toLowerCase()] = attr_value.split('=')[1];
if(rendition[attr_value.split('=')[0].toLowerCase()].split('x').length = 2)
{
rendition.resolution = {
width: Number(rendition[attr_value.split('=')[0].toLowerCase()].split('x')[0]),
height: Number(rendition[attr_value.split('=')[0].toLowerCase()].split('x')[1])
}
}
} else {
rendition[attr_value.split('=')[0].toLowerCase()] = Number(attr_value.split('=')[1]);
}
});
if( self.getTagType(lines[index+1]) == tagTypes.BYTERANGE )
{
rendition.byterange = self.getTagValue(lines[index+1]).split('@');
rendition.url = lines[index+2];
} else
{
rendition.url = lines[index+1];
}
data.isPlaylist = true;
data.playlistItems.push(rendition);
break;
case tagTypes.TARGETDURATION:
data.targetDuration = Number(self.getTagValue(value).split(',')[0]);
break;
case tagTypes.ZEN_TOTAL_DURATION:
data.totalDuration = Number(self.getTagValue(value));
break;
case tagTypes.VERSION:
data.version = Number(self.getTagValue(value));
break;
case tagTypes.MEDIA_SEQUENCE:
data.mediaSequence = parseInt(self.getTagValue(value));
break;
case tagTypes.ALLOW_CACHE:
if(self.getTagValue(value) == "YES" || self.getTagValue(value) == "NO")
{
data.allowCache = self.getTagValue(value);
} else {
data.invalidReasons.push("Invalid ALLOW_CACHE Value");
}
break;
case tagTypes.ENDLIST:
data.hasEndTag = true;
break;
}
}
)
} else {
data.invalidReasons.push("Empty Manifest");
if (rawDataString === undefined || rawDataString.length <= 0) {
data.invalidReasons.push("Empty Manifest");
return;
}
lines = rawDataString.split('\n');
lines.forEach(function(value,index) {
var segment, rendition, attribute;
switch (self.getTagType(value)) {
case tagTypes.EXTM3U:
data.hasValidM3UTag = (index == 0);
if (!data.hasValidM3UTag) {
data.invalidReasons.push("Invalid EXTM3U Tag");
}
break;
case tagTypes.DISCONTINUITY:
break;
case tagTypes.PLAYLIST_TYPE:
if (self.getTagValue(value) === "VOD" ||
self.getTagValue(value) === "EVENT") {
data.playlistType = self.getTagValue(value);
data.isPlaylist = true;
} else {
data.invalidReasons.push("Invalid Playlist Type Value");
}
break;
case tagTypes.EXTINF:
segment = {
url: "unknown",
byterange: -1,
targetDuration: data.targetDuration
};
if (self.getTagType(lines[index + 1]) === tagTypes.BYTERANGE) {
segment.byterange = self.getTagValue(lines[index + 1]).split('@');
segment.url = lines[index + 2];
} else {
segment.url = lines[index + 1];
}
if (segment.url.indexOf("http") === -1 && self.directory) {
if (data.directory[data.directory.length-1] === segment.url[0] &&
segment.url[0] === "/") {
segment.url = segment.url.substr(1);
}
segment.url = self.directory + segment.url;
}
data.mediaItems.push(segment);
break;
case tagTypes.STREAM_INF:
rendition = {};
attributes = value.substr(tagTypes.STREAM_INF.length).split(',');
attributes.forEach(function(attrValue) {
if (isNaN(attrValue.split('=')[1])) {
rendition[attrValue.split('=')[0].toLowerCase()] = attrValue.split('=')[1];
if (rendition[attrValue.split('=')[0].toLowerCase()].split('x').length === 2) {
rendition.resolution = {
width: parseInt(rendition[attrValue.split('=')[0].toLowerCase()].split('x')[0]),
height: parseInt(rendition[attrValue.split('=')[0].toLowerCase()].split('x')[1])
}
}
} else {
rendition[attrValue.split('=')[0].toLowerCase()] = parseInt(attrValue.split('=')[1]);
}
});
if (self.getTagType(lines[index + 1]) === tagTypes.BYTERANGE) {
rendition.byterange = self.getTagValue(lines[index + 1]).split('@');
rendition.url = lines[index + 2];
} else {
rendition.url = lines[index + 1];
}
data.isPlaylist = true;
data.playlistItems.push(rendition);
break;
case tagTypes.TARGETDURATION:
data.targetDuration = parseFloat(self.getTagValue(value).split(',')[0]);
break;
case tagTypes.ZEN_TOTAL_DURATION:
data.totalDuration = parseFloat(self.getTagValue(value));
break;
case tagTypes.VERSION:
data.version = parseFloat(self.getTagValue(value));
break;
case tagTypes.MEDIA_SEQUENCE:
data.mediaSequence = parseInt(self.getTagValue(value));
break;
case tagTypes.ALLOW_CACHE:
if (self.getTagValue(value) === "YES" || self.getTagValue(value) === "NO") {
data.allowCache = self.getTagValue(value);
} else {
data.invalidReasons.push("Invalid ALLOW_CACHE Value");
}
break;
case tagTypes.ENDLIST:
data.hasEndTag = true;
break;
}
});
return data;
};
};
})(this);
......
(function(window) {
window.videojs.hls.m3u8TagType = {
/*
* Derived from V8: http://tools.ietf.org/html/draft-pantos-http-live-streaming-08
* Derived from the HTTP Live Streaming Spec V8
* http://tools.ietf.org/html/draft-pantos-http-live-streaming-08
*/
/**
......
......@@ -14,5 +14,5 @@
this.playlistType = "";
this.mediaSequence = -1;
this.version = -1;
}
};
})(this);
......
(function (window) {
var M3U8 = window.videojs.hls.M3U8;
var M3U8Parser = window.videojs.hls.M3U8Parser;
var
M3U8 = window.videojs.hls.M3U8,
M3U8Parser = window.videojs.hls.M3U8Parser;
window.videojs.hls.ManifestController = function () {
window.videojs.hls.ManifestController = function() {
var self = this;
self.parser;
......@@ -13,7 +14,7 @@
self.onErrorCallback;
self.onUpdateCallback;
self.loadManifest = function (manifestUrl, onDataCallback, onErrorCallback, onUpdateCallback) {
self.loadManifest = function(manifestUrl, onDataCallback, onErrorCallback, onUpdateCallback) {
self.url = manifestUrl;
if (onDataCallback) {
......@@ -30,7 +31,7 @@
vjs.get(manifestUrl, self.onManifestLoadComplete, self.onManifestLoadError);
};
self.parseManifest = function (dataAsString) {
self.parseManifest = function(dataAsString) {
self.parser = new M3U8Parser();
self.parser.directory = /^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/.exec(self.url).slice(1)[1];
self.data = self.parser.parse(dataAsString);
......@@ -38,7 +39,7 @@
return self.data;
};
self.onManifestLoadComplete = function (response) {
self.onManifestLoadComplete = function(response) {
var output = self.parseManifest(response);
if (self.onDataCallback != undefined) {
......@@ -46,7 +47,7 @@
}
};
self.onManifestLoadError = function (err) {
self.onManifestLoadError = function(err) {
if (self.onErrorCallback != undefined) {
self.onErrorCallback((err != undefined) ? err : null);
}
......
(function(window) {
window.videojs.hls.SegmentController = function(){
window.videojs.hls.SegmentController = function() {
var self = this;
self.url;
self.requestTimestamp;
self.responseTimestamp;
self.data;
self.onDataCallback;
self.onErrorCallback;
self.onUpdateCallback;
self.loadSegment = function(segmentUrl, onDataCallback, onErrorCallback, onUpdateCallback) {
var request = new XMLHttpRequest();
self.loadSegment = function ( segmentUrl, onDataCallback, onErrorCallback, onUpdateCallback ) {
self.url = segmentUrl;
self.onDataCallback = onDataCallback;
self.onErrorCallback = onErrorCallback;
self.onUpdateCallback = onUpdateCallback;
self.requestTimestamp = new Date().getTime();
self.requestTimestamp = +new Date();
var req = new XMLHttpRequest();
req.open('GET', segmentUrl, true);
req.responseType = 'arraybuffer';
req.onload = function(response) {
self.onSegmentLoadComplete(new Uint8Array(req.response));
request.open('GET', segmentUrl, true);
request.responseType = 'arraybuffer';
request.onload = function(response) {
self.onSegmentLoadComplete(new Uint8Array(request.response));
};
req.send(null);
//vjs.get(segmentUrl, self.onSegmentLoadComplete, self.onSegmentLoadError);
request.send(null);
};
self.parseSegment = function ( incomingData ) {
// Add David's code later //
console.log('got segment data', incomingData.byteLength);
self.parseSegment = function(incomingData) {
self.data = {};
self.data.binaryData = incomingData;
self.data.url = self.url;
......@@ -44,36 +29,35 @@
self.data.requestTimestamp = self.requestTimestamp;
self.data.responseTimestamp = self.responseTimestamp;
self.data.byteLength = incomingData.byteLength;
self.data.isCached = ( parseInt(self.responseTimestamp - self.requestTimestamp) < 75 );
self.data.isCached = parseInt(self.responseTimestamp - self.requestTimestamp) < 75;
self.data.throughput = self.calculateThroughput(self.data.byteLength, self.requestTimestamp ,self.responseTimestamp);
return self.data;
};
self.calculateThroughput = function(dataAmount, startTime, endTime) {
return Math.round(dataAmount/(endTime-startTime)*1000)*8;
return Math.round(dataAmount / (endTime - startTime) * 1000) * 8;
}
self.onSegmentLoadComplete = function(response) {
self.responseTimestamp = new Date().getTime();
var output;
self.responseTimestamp = +new Date();
var output = self.parseSegment(response);
output = self.parseSegment(response);
if(self.onDataCallback != undefined)
{
if (self.onDataCallback !== undefined) {
self.onDataCallback(output);
}
};
self.onSegmentLoadError = function(err) {
if(err)
{
console.log(err.message);
self.onSegmentLoadError = function(error) {
if (error) {
console.log(error.message);
}
if(self.onErrorCallback != undefined)
{
onErrorCallback((err != undefined) ? err : null);
if (self.onErrorCallback !== undefined) {
onErrorCallback(error);
}
};
}
......
......@@ -249,14 +249,12 @@
test('should create my parser', function () {
ok(m3u8parser != undefined);
}
);
});
test('should successfully parse manifest data', function () {
var parsedData = m3u8parser.parse(window.playlistData);
ok(parsedData);
}
);
});
test('test for expected results', function () {
var data = m3u8parser.parse(window.playlistData);
......@@ -272,8 +270,7 @@
equal(data.mediaSequence, 0, 'MEDIA SEQUENCE is correct');
equal(data.totalDuration, -1, "ZEN TOTAL DURATION is unknown as expected");
equal(data.hasEndTag, true, 'should have ENDLIST tag');
}
);
});
module('brightcove playlist', {
setup: function () {
......@@ -299,7 +296,6 @@
manifestController = new window.videojs.hls.ManifestController();
this.vjsget = vjs.get;
vjs.get = function (url, success, error) {
console.log(url);
success(window.brightcove_playlist_data);
};
},
......@@ -316,29 +312,22 @@
var data = manifestController.parseManifest(window.brightcove_playlist_data);
ok(data);
equal(data.playlistItems.length, 4, 'Has correct rendition count');
equal(data.playlistItems[0].bandwidth, 240000, 'First rendition index bandwidth is correct');
equal(data.playlistItems[0]["program-id"], 1, 'First rendition index program-id is correct');
equal(data.playlistItems[0].resolution.width, 396, 'First rendition index resolution width is correct');
equal(data.playlistItems[0].resolution.height, 224, 'First rendition index resolution height is correct');
})
});
test('should get a manifest from hermes', function () {
var hermesUrl = "http://localhost:7070/test/basic-playback/brightcove/16x9-master.m3u8";
manifestController.loadManifest(
hermesUrl,
function (responseData) {
ok(true);
},
function (errorData) {
console.log('got error data');
},
function (updateData) {
console.log('got update data');
}
)
manifestController.loadManifest('http://example.com/16x9-master.m3u8',
function(responseData) {
ok(responseData);
},
function(errorData) {
ok(false, 'does not error');
},
function(updateData) {});
});
module('segment controller', {
......@@ -355,33 +344,11 @@
}
});
test('should get a segment data', function () {
ok(true);
var hermesUrl = "http://localhost:7070/test/ts-files/brightcove/s-1.ts";
segmentController.loadSegment(
hermesUrl,
function (responseData) {
console.log('got response from segment controller');
ok(true);
},
function (errorData) {
console.log('got error data');
},
function (updateData) {
console.log('got update data');
}
)
}
)
test('bandwidth calulation test', function () {
var multiSecondData = segmentController.calculateThroughput(10000, 1000, 2000);
var subSecondData = segmentController.calculateThroughput(10000, 1000, 1500);
var
multiSecondData = segmentController.calculateThroughput(10000, 1000, 2000),
subSecondData = segmentController.calculateThroughput(10000, 1000, 1500);
equal(multiSecondData, 80000, 'MULTI-Second bits per second calculation');
equal(subSecondData, 160000, 'SUB-Second bits per second calculation');
})
});
})(this);
......