diff --git a/build.sh b/build.sh index 0bba680a..77d04ef0 100755 --- a/build.sh +++ b/build.sh @@ -7,6 +7,7 @@ uglifyjs \ src/ajax.js \ src/ajax-progressive.js \ src/websocket.js \ + src/stream.js \ src/ts.js \ src/decoder.js \ src/mpeg1.js \ diff --git a/src/player.js b/src/player.js index fa9a33e6..d5ceaede 100644 --- a/src/player.js +++ b/src/player.js @@ -7,6 +7,10 @@ var Player = function(url, options) { this.source = new options.source(url, options); options.streaming = !!this.source.streaming; } + else if (url && typeof url.pipe == 'function') { + this.source = new JSMpeg.Source.Stream(url, options); + options.streaming = true; + } else if (url.match(/^wss?:\/\//)) { this.source = new JSMpeg.Source.WebSocket(url, options); options.streaming = true; diff --git a/src/stream.js b/src/stream.js new file mode 100644 index 00000000..cad1043a --- /dev/null +++ b/src/stream.js @@ -0,0 +1,63 @@ +JSMpeg.Source.Stream = (function(){ "use strict"; + +var StreamSource = function(stream, options) { + this.stream = stream; + this.destination = null; + + this.completed = false; + this.established = false; + this.progress = 0; + + // Streaming is obiously true when using a stream + this.streaming = true; +}; + +StreamSource.prototype.connect = function(destination) { + this.destination = destination; +}; + +StreamSource.prototype.setStream = function(stream) { + if (this.stream) { + this.stream.destroy(); + } + + this.stream = stream; + this.start(); +}; + +StreamSource.prototype.start = function() { + + var that = this; + + if (!this.stream) { + return; + } + + this.stream.on('data', function onData(chunk) { + that.onLoad(chunk); + }); +}; + +StreamSource.prototype.resume = function(secondsHeadroom) { + // Nothing to do here +}; + +StreamSource.prototype.destroy = function() { + if (this.stream) { + this.stream.destroy(); + } +}; + +StreamSource.prototype.onLoad = function(data) { + this.established = true; + this.completed = true; + this.progress = 1; + + if (this.destination) { + this.destination.write(data); + } +}; + +return StreamSource; + +})(); \ No newline at end of file