Skip to content

Commit

Permalink
@imbcmdth fixed async currentSrc behavior. closes videojs#2256
Browse files Browse the repository at this point in the history
  • Loading branch information
jrivera authored and gkatsev committed Jun 15, 2015
1 parent 20b46b9 commit 463ba4e
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ CHANGELOG

## HEAD (Unreleased)
* @imbcmdth updated currentSrc to return src instead of blob urls in html5 tech. Fixes #2232 ([view](https://github.com/videojs/video.js/pull/2232))
* @imbcmdth fixed async currentSrc behavior ([view](https://github.com/videojs/video.js/pull/2256))

--------------------

Expand Down
34 changes: 27 additions & 7 deletions src/js/media/media.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ vjs.MediaTechController = vjs.Component.extend({
this.emulateTextTracks();
}

this.on('loadstart', this.updateCurrentSource_);

this.initTextTrackListeners();
}
});
Expand Down Expand Up @@ -168,6 +170,24 @@ vjs.MediaTechController.prototype.onTap = function(){
this.player().userActive(!this.player().userActive());
};

/**
* Set currentSource_ asynchronously to simulate the media element's
* asynchronous execution of the `resource selection algorithm`
*
* currentSource_ is set either as the first loadstart event OR
* in a timeout to make sure it is set asynchronously before anything else
* but before other loadstart handlers have had a chance to execute
*/
vjs.MediaTechController.prototype.updateCurrentSource_ = function () {
// We could have been called with a 0-ms setTimeout OR via loadstart (which ever
// happens first) so we should clear the timeout to be a good citizen
this.clearTimeout(this.updateSourceTimer_);

if (this.pendingSource_) {
this.currentSource_ = this.pendingSource_;
}
};

/* Fallbacks for unsupported event types
================================================================================ */
// Manually trigger progress events based on changes to the buffered amount
Expand Down Expand Up @@ -426,6 +446,8 @@ vjs.MediaTechController.prototype['featuresNativeTextTracks'] = false;
*
*/
vjs.MediaTechController.withSourceHandlers = function(Tech){
Tech.prototype.currentSource_ = {src: ''};

/**
* Register a source handler
* Source handlers are scripts for handling specific formats.
Expand Down Expand Up @@ -510,13 +532,11 @@ vjs.MediaTechController.withSourceHandlers = function(Tech){
this.disposeSourceHandler();
this.off('dispose', this.disposeSourceHandler);

// Set currentSource_ asynchronously to simulate the media element's
// asynchronous execution of the `resource selection algorithm`
this.setTimeout(vjs.bind(this, function () {
if (source && source.src !== '') {
this.currentSource_ = source;
}
}), 0);
// Schedule currentSource_ to be set asynchronously
if (source && source.src !== '') {
this.pendingSource_ = source;
this.updateSourceTimer_ = this.setTimeout(vjs.bind(this, this.updateCurrentSource_), 0);
}

this.sourceHandler_ = sh.handleSource(source, this);
this.on('dispose', this.disposeSourceHandler);
Expand Down
7 changes: 6 additions & 1 deletion src/js/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -1228,7 +1228,12 @@ vjs.Player.prototype.load = function(){
* @return {String} The current source
*/
vjs.Player.prototype.currentSrc = function(){
return this.techGet('currentSrc') || this.cache_.src || '';
var techSrc = this.techGet('currentSrc');

if (techSrc === undefined) {
return this.cache_.src || '';
}
return techSrc;
};

/**
Expand Down
2 changes: 1 addition & 1 deletion test/unit/media.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ test('should emulate the video element\'s behavior for currentSrc when src is se
tech.setSource(sourceA);

// Test that currentSource_ is not immediately specified
strictEqual(tech.currentSource_, undefined, 'sourceA was not stored immediately');
deepEqual(tech.currentSource_, {src:''}, 'sourceA was not stored immediately');

this.clock.tick(1);

Expand Down

0 comments on commit 463ba4e

Please # to comment.