Skip to content

Commit

Permalink
@chemoish add the ability to obtain current source object + current s…
Browse files Browse the repository at this point in the history
…ource objects—when using `<source>` or `src()`.

This is needed for source retrival for two use cases:
- preroll
- resolution switch (kind of)

Currently there is no way of accessing the source object for any DRM specified content. Plugin access only has API methods to `currentSrc` and `currentType`.

This will allow for `currentSource` and `currentSources` to return unverified source object(s).

# Conflicts:
#	src/js/player.js
#	test/unit/player.js
  • Loading branch information
Carey Hinoki committed Aug 26, 2016
1 parent 3a859f9 commit be8341d
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/js/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -614,10 +614,12 @@ class Player extends Component {

if (source) {
this.currentType_ = source.type;

if (source.src === this.cache_.src && this.cache_.currentTime > 0) {
techOptions.startTime = this.cache_.currentTime;
}

this.cache_.source = source;
this.cache_.src = source.src;
}

Expand Down Expand Up @@ -1975,6 +1977,7 @@ class Player extends Component {
// the tech loop to check for a compatible technology
this.sourceList_([source]);
} else {
this.cache_.source = source;
this.cache_.src = source.src;
this.currentType_ = source.type || '';

Expand Down Expand Up @@ -2025,6 +2028,8 @@ class Player extends Component {
// load this technology with the chosen source
this.loadTech_(sourceTech.tech, sourceTech.source);
}

this.cache_.sources = sources;
} else {
// We need to wrap this in a timeout to give folks a chance to add error event handlers
this.setTimeout(function() {
Expand Down Expand Up @@ -2061,6 +2066,26 @@ class Player extends Component {
return this;
}

/**
* Returns the current source objects.
*
* @return {Object[]} The current source objects
* @method currentSources
*/
currentSources() {
return this.cache_.sources || [this.currentSource()];
}

/**
* Returns the current source object.
*
* @return {Object} The current source object
* @method currentSource
*/
currentSource() {
return this.cache_.source || {};
}

/**
* Returns the fully qualified URL of the current source value e.g. http://mysite.com/video.mp4
* Can be used in conjuction with `currentType` to assist in rebuilding the current source object.
Expand Down
112 changes: 112 additions & 0 deletions test/unit/player.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,118 @@ QUnit.test('should get tag, source, and track settings', function(assert) {
assert.equal(player.el(), null, 'player el killed');
});

QUnit.test('should get current source from source tag', function(assert) {
const fixture = document.getElementById('qunit-fixture');

const html = [
'<video id="example_1" class="video-js" autoplay preload="none">',
'<source src="http://google.com" type="video/mp4">',
'<source src="http://hugo.com" type="video/webm">',
'</video>'
].join('');

fixture.innerHTML += html;

const tag = document.getElementById('example_1');
const player = TestHelpers.makePlayer({}, tag);

assert.ok(player.currentSource().src === 'http://google.com');
assert.ok(player.currentSource().type === 'video/mp4');
});

QUnit.test('should get current sources from source tag', function(assert) {
const fixture = document.getElementById('qunit-fixture');

const html = [
'<video id="example_1" class="video-js" autoplay preload="none">',
'<source src="http://google.com" type="video/mp4">',
'<source src="http://hugo.com" type="video/webm">',
'</video>'
].join('');

fixture.innerHTML += html;

const tag = document.getElementById('example_1');
const player = TestHelpers.makePlayer({}, tag);

assert.ok(player.currentSources()[0].src === 'http://google.com');
assert.ok(player.currentSources()[0].type === 'video/mp4');
assert.ok(player.currentSources()[1].src === 'http://hugo.com');
assert.ok(player.currentSources()[1].type === 'video/webm');
});

QUnit.test('should get current source from src set', function(assert) {
const fixture = document.getElementById('qunit-fixture');

const html = '<video id="example_1" class="video-js" autoplay preload="none"></video>';

fixture.innerHTML += html;

const tag = document.getElementById('example_1');
const player = TestHelpers.makePlayer({}, tag);

// check for empty object
assert.ok(Object.keys(player.currentSource()).length === 0);

player.src('http://google.com');

assert.ok(player.currentSource().src === 'http://google.com');
assert.ok(player.currentSource().type === undefined);

player.src({
src: 'http://google.com'
});

assert.ok(player.currentSource().src === 'http://google.com');
assert.ok(player.currentSource().type === undefined);

player.src({
src: 'http://google.com',
type: 'video/mp4'
});

assert.ok(player.currentSource().src === 'http://google.com');
assert.ok(player.currentSource().type === 'video/mp4');
});

QUnit.test('should get current sources from src set', function(assert) {
const fixture = document.getElementById('qunit-fixture');

const html = '<video id="example_1" class="video-js" autoplay preload="none"></video>';

fixture.innerHTML += html;

const tag = document.getElementById('example_1');
const player = TestHelpers.makePlayer({}, tag);

// check for empty object
assert.ok(Object.keys(player.currentSources()[0]).length === 0);

player.src([{
src: 'http://google.com'
}, {
src: 'http://hugo.com'
}]);

assert.ok(player.currentSources()[0].src === 'http://google.com');
assert.ok(player.currentSources()[0].type === undefined);
assert.ok(player.currentSources()[1].src === 'http://hugo.com');
assert.ok(player.currentSources()[1].type === undefined);

player.src([{
src: 'http://google.com',
type: 'video/mp4'
}, {
src: 'http://hugo.com',
type: 'video/webm'
}]);

assert.ok(player.currentSources()[0].src === 'http://google.com');
assert.ok(player.currentSources()[0].type === 'video/mp4');
assert.ok(player.currentSources()[1].src === 'http://hugo.com');
assert.ok(player.currentSources()[1].type === 'video/webm');
});

QUnit.test('should asynchronously fire error events during source selection', function(assert) {
assert.expect(2);

Expand Down

0 comments on commit be8341d

Please sign in to comment.