Skip to content

Commit

Permalink
Changing the text track now fires AdaptationEvent.
Browse files Browse the repository at this point in the history
Closes #147

Change-Id: Idd57ada8155273db50f422096f2d40b8bce38230
  • Loading branch information
TheModMaker authored and Gerrit Code Review committed Aug 17, 2015
1 parent 9070446 commit e0e6621
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 28 deletions.
15 changes: 15 additions & 0 deletions lib/media/i_stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,21 @@ goog.require('shaka.media.StreamInfo');
shaka.media.IStream = function() {};


/**
* @event shaka.media.IStream.AdaptationEvent
* @description Fired when video, audio, or text tracks change.
* Bubbles up through the Player.
* @property {string} type 'adaptation'
* @property {boolean} bubbles true
* @property {string} contentType 'video', 'audio', or 'text'
* @property {?{width: number, height: number}} size The resolution chosen, if
* the stream is a video stream.
* @property {number} bandwidth The stream's bandwidth requirement in bits per
* second.
* @export
*/


/**
* Configures the Stream options. Options are set via key-value pairs.
*
Expand Down
53 changes: 26 additions & 27 deletions lib/media/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,6 @@ goog.require('shaka.util.TypedBind');



/**
* @event shaka.media.Stream.AdaptationEvent
* @description Fired when video or audio tracks change.
* Bubbles up through the Player.
* @property {string} type 'adaptation'
* @property {boolean} bubbles true
* @property {string} contentType 'video' or 'audio'
* @property {?{width: number, height: number}} size The resolution chosen, if
* the stream is a video stream.
* @property {number} bandwidth The stream's bandwidth requirement in bits per
* second.
* @export
*/

/**
* @event shaka.media.Stream.EndedEvent
* @description Fired when the stream ends.
Expand Down Expand Up @@ -78,7 +64,7 @@ goog.require('shaka.util.TypedBind');
* @param {!shaka.util.IBandwidthEstimator} estimator A bandwidth estimator to
* attach to all data requests.
*
* @fires shaka.media.Stream.AdaptationEvent
* @fires shaka.media.IStream.AdaptationEvent
* @fires shaka.media.Stream.EndedEvent
* @fires shaka.media.Stream.StartedEvent
* @fires shaka.player.Player.ErrorEvent
Expand Down Expand Up @@ -598,18 +584,7 @@ shaka.media.Stream.prototype.fireStartedEvent_ = function(timestampCorrection) {
* @private
*/
shaka.media.Stream.prototype.fireAdaptationEvent_ = function(streamInfo) {
var contentType = streamInfo.mimeType.split('/')[0];
var size = (contentType != 'video') ? null : {
'width': streamInfo.width,
'height': streamInfo.height
};
var event = shaka.util.FakeEvent.create({
'type': 'adaptation',
'bubbles': true,
'contentType': contentType,
'size': size,
'bandwidth': streamInfo.bandwidth
});
var event = shaka.media.Stream.createAdaptationEvent_(streamInfo);
this.dispatchEvent(event);
};

Expand Down Expand Up @@ -650,3 +625,27 @@ shaka.media.Stream.prototype.cancelUpdateTimer_ = function() {
}
};


/**
* Creates an event object for an AdaptationEvent using the given StreamInfo.
*
* @param {!shaka.media.StreamInfo} streamInfo
* @return {!Event}
* @private
*/
shaka.media.Stream.createAdaptationEvent_ = function(streamInfo) {
var contentType = streamInfo.mimeType.split('/')[0];
var size = (contentType != 'video') ? null : {
'width': streamInfo.width,
'height': streamInfo.height
};
var event = shaka.util.FakeEvent.create({
'type': 'adaptation',
'bubbles': true,
'contentType': contentType,
'size': size,
'bandwidth': streamInfo.bandwidth
});
return event;
};

22 changes: 22 additions & 0 deletions lib/media/text_stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ shaka.media.TextStream.prototype.switch = function(

// NOTE: mode must be set after appending to the DOM.
this.setEnabled(enabled);

var event = shaka.media.TextStream.createAdaptationEvent_(streamInfo);
this.dispatchEvent(event);
}));
};

Expand All @@ -176,3 +179,22 @@ shaka.media.TextStream.prototype.getEnabled = function() {
return this.enabled_;
};


/**
* Creates an event object for an AdaptationEvent using the given StreamInfo.
*
* @param {!shaka.media.StreamInfo} streamInfo
* @return {!Event}
* @private
*/
shaka.media.TextStream.createAdaptationEvent_ = function(streamInfo) {
var event = shaka.util.FakeEvent.create({
'type': 'adaptation',
'bubbles': true,
'contentType': 'text',
'size': null,
'bandwidth': streamInfo.bandwidth
});
return event;
};

2 changes: 1 addition & 1 deletion lib/player/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ goog.require('shaka.util.MapUtils');
*
* @param {!HTMLVideoElement} video The video element.
*
* @fires shaka.media.Stream.AdaptationEvent
* @fires shaka.media.IStream.AdaptationEvent
* @fires shaka.player.StreamVideoSource.SeekRangeChangedEvent
* @fires shaka.player.Player.BufferingEvent
* @fires shaka.player.Player.ErrorEvent
Expand Down
28 changes: 28 additions & 0 deletions spec/player_integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,34 @@ describe('Player', function() {
});
});

it('fires an adaptation event', function(done) {
var onAdaptation = jasmine.createSpy('onAdaptation');
player.load(newSource(languagesManifest)).then(function() {
player.addEventListener('adaptation', onAdaptation, false);

tracks = player.getTextTracks();
player.selectTextTrack(tracks[1].id);
return delay(0.1);
}).then(function() {
activeTrack = getActiveTextTrack();
expect(activeTrack.id).toBe(tracks[1].id);

var found = false;
var max = onAdaptation.calls.count();
for (var i = 0; i < max; i++) {
if (onAdaptation.calls.argsFor(i)[0].contentType === 'text') {
found = true;
}
}
expect(found).toBe(true);
done();
}).catch(function(error) {
fail(error);
done();
});
});


it('does not disable subtitles', function(done) {
var tracks;
player.load(newSource(languagesManifest)).then(function() {
Expand Down

0 comments on commit e0e6621

Please sign in to comment.