%PDF- %PDF-
| Direktori : /home/tjamichg/cursos.tjamich.gob.mx/web/assets/vrview/src/embed/ |
| Current File : /home/tjamichg/cursos.tjamich.gob.mx/web/assets/vrview/src/embed/adaptive-player.js |
/*
* Copyright 2016 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var EventEmitter = require('eventemitter3');
var shaka = require('shaka-player');
var Types = require('../video-type');
var Util = require('../util');
var DEFAULT_BITS_PER_SECOND = 1000000;
/**
* Supports regular video URLs (eg. mp4), as well as adaptive manifests like
* DASH (.mpd) and soon HLS (.m3u8).
*
* Events:
* load(video): When the video is loaded.
* error(message): If an error occurs.
*
* To play/pause/seek/etc, please use the underlying video element.
*/
function AdaptivePlayer(params) {
this.video = document.createElement('video');
// Loop by default.
if (params.loop === true) {
this.video.setAttribute('loop', true);
}
if (params.volume !== undefined) {
// XXX: .setAttribute('volume', params.volume) doesn't work for some reason.
this.video.volume = params.volume;
}
// Not muted by default.
if (params.muted === true) {
this.video.muted = params.muted;
}
// For FF, make sure we enable preload.
this.video.setAttribute('preload', 'auto');
// Enable inline video playback in iOS 10+.
this.video.setAttribute('playsinline', true);
this.video.setAttribute('crossorigin', 'anonymous');
}
AdaptivePlayer.prototype = new EventEmitter();
AdaptivePlayer.prototype.load = function(url) {
var self = this;
// TODO(smus): Investigate whether or not differentiation is best done by
// mimeType after all. Cursory research suggests that adaptive streaming
// manifest mime types aren't properly supported.
//
// For now, make determination based on extension.
var extension = Util.getExtension(url);
switch (extension) {
case 'm3u8': // HLS
this.type = Types.HLS;
if (Util.isSafari()) {
this.loadVideo_(url).then(function() {
self.emit('load', self.video, self.type);
}).catch(this.onError_.bind(this));
} else {
self.onError_('HLS is only supported on Safari.');
}
break;
case 'mpd': // MPEG-DASH
this.type = Types.DASH;
this.loadShakaVideo_(url).then(function() {
console.log('The video has now been loaded!');
self.emit('load', self.video, self.type);
}).catch(this.onError_.bind(this));
break;
default: // A regular video, not an adaptive manifest.
this.type = Types.VIDEO;
this.loadVideo_(url).then(function() {
self.emit('load', self.video, self.type);
}).catch(this.onError_.bind(this));
break;
}
};
AdaptivePlayer.prototype.destroy = function() {
this.video.pause();
this.video.src = '';
this.video = null;
};
/*** PRIVATE API ***/
AdaptivePlayer.prototype.onError_ = function(e) {
console.error(e);
this.emit('error', e);
};
AdaptivePlayer.prototype.loadVideo_ = function(url) {
var self = this, video = self.video;
return new Promise(function(resolve, reject) {
video.src = url;
video.addEventListener('canplaythrough', resolve);
video.addEventListener('loadedmetadata', function() {
self.emit('timeupdate', {
currentTime: video.currentTime,
duration: video.duration
});
});
video.addEventListener('error', reject);
video.load();
});
};
AdaptivePlayer.prototype.initShaka_ = function() {
this.player = new shaka.Player(this.video);
this.player.configure({
abr: { defaultBandwidthEstimate: DEFAULT_BITS_PER_SECOND }
});
// Listen for error events.
this.player.addEventListener('error', this.onError_);
};
AdaptivePlayer.prototype.loadShakaVideo_ = function(url) {
// Install built-in polyfills to patch browser incompatibilities.
shaka.polyfill.installAll();
if (!shaka.Player.isBrowserSupported()) {
console.error('Shaka is not supported on this browser.');
return;
}
this.initShaka_();
return this.player.load(url);
};
module.exports = AdaptivePlayer;