Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delay the JSMpeg.CreateVideoElements call 'till after the script has loaded #193

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
4 changes: 3 additions & 1 deletion src/jsmpeg.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ var JSMpeg = {

// Automatically create players for all found <div class="jsmpeg"/> elements.
if (document.readyState === 'complete') {
JSMpeg.CreateVideoElements();
setTimeout(function() {
JSMpeg.CreateVideoElements();
});
}
else {
document.addEventListener('DOMContentLoaded', JSMpeg.CreateVideoElements);
Expand Down
4 changes: 4 additions & 0 deletions src/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
63 changes: 63 additions & 0 deletions src/stream.js
Original file line number Diff line number Diff line change
@@ -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;

})();