Skip to content

Commit

Permalink
Background load next item JW8-30
Browse files Browse the repository at this point in the history
  • Loading branch information
John Bartos committed Mar 5, 2018
1 parent 9de1855 commit e5085f5
Show file tree
Hide file tree
Showing 9 changed files with 239 additions and 89 deletions.
2 changes: 1 addition & 1 deletion src/js/api/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -1005,5 +1005,5 @@ Object.assign(Api.prototype, /** @lends Api.prototype */ {
* @param {boolean} toggle - Specifies whether ad playback should be paused or resumed.
* @returns {void}
*/
pauseAd(toggle) {}, // eslint-disable-line
pauseAd(toggle) {}, // eslint-disable-line,
});
7 changes: 7 additions & 0 deletions src/js/controller/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { PLAYER_STATE, STATE_BUFFERING, STATE_IDLE, STATE_COMPLETE, STATE_PAUSED
CAPTIONS_LIST, CONTROLS, RESIZE, MEDIA_VISUAL_QUALITY } from 'events/events';
import ProgramController from 'program/program-controller';
import initQoe from 'controller/qoe';
import { BACKGROUND_LOAD_OFFSET } from '../program/program-constants';

// The model stores a different state than the provider
function normalizeState(newstate) {
Expand Down Expand Up @@ -162,6 +163,12 @@ Object.assign(Controller.prototype, {
const type = streamType(duration, minDvrWindow);
model.setStreamType(type);
});
mediaModel.on('change:position', function (changedMediaModel, position) {
if (!_programController.backgroundLoading
&& (position >= mediaModel.get('duration') - BACKGROUND_LOAD_OFFSET)) {
_programController.backgroundLoad(_model.get('item') + 1);
}
});
});

// Ensure captionsList event is raised after playlistItem
Expand Down
42 changes: 42 additions & 0 deletions src/js/program/background-media.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* A simple data structure for containing both of the background loading objects.
* loadedMedia is the currently active item which has been put into the background during ad playback.
* loadingMedia is an item which is preloading in the background which may be selected in the future. It is usually the
* next item in the playlist, or the next up item in a recommendations feed.
* @returns {BackgroundMedia}
*/
/**
* @typedef {Object} BackgroundMedia
* @property {MediaController} loadingItem - The mediaController which has been placed into the background during playback.
* @property {Object} loadingItem - The playlist item loading in the background.
* @property {Promise} loadingMedia - A promise representing the media loading in the background. Resolves with the mediaController.
* @constructor
*/
export default function BackgroundMedia() {
let loadedMedia = null;
let loadingMedia = null;

return Object.defineProperties(Object.create(null), {
loadingItem: {
get() {
return loadingMedia ? loadingMedia.item : null;
}
},
loadingMedia: {
get() {
return loadingMedia ? loadingMedia.loadPromise : null;
},
set(loadObject) {
loadingMedia = loadObject;
}
},
loadedMedia: {
get() {
return loadedMedia;
},
set(mediaController) {
loadedMedia = mediaController;
}
}
});
}
1 change: 1 addition & 0 deletions src/js/program/media-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ export default class MediaController extends Eventable {
mediaModelState.duration = duration;

this.item = item;
this.provider.init(item);
}

set audioTrack(index) {
Expand Down
Loading

0 comments on commit e5085f5

Please sign in to comment.