alltube/js/cast.js

108 lines
3.6 KiB
JavaScript
Raw Normal View History

2014-04-04 14:38:36 +00:00
/*global chrome*/
2016-07-22 16:27:33 +00:00
/*jslint browser: true, nomen: true */
2016-06-22 10:53:16 +00:00
var castModule = (function () {
2014-04-04 14:38:36 +00:00
'use strict';
2016-06-22 10:53:16 +00:00
var launchBtn, disabledBtn, stopBtn, session;
2014-04-04 14:38:36 +00:00
2016-06-22 10:53:16 +00:00
function receiverListener(e) {
return (e === chrome.cast.ReceiverAvailability.AVAILABLE);
2014-04-04 21:57:41 +00:00
}
2014-04-04 14:38:36 +00:00
2016-06-22 10:53:16 +00:00
function onMediaDiscovered() {
if (launchBtn) {
stopBtn.classList.remove('cast_hidden');
launchBtn.classList.add('cast_hidden');
}
}
2016-06-22 10:50:11 +00:00
2016-06-22 10:53:16 +00:00
function onStopCast() {
stopBtn.classList.add('cast_hidden');
launchBtn.classList.remove('cast_hidden');
2016-06-22 10:50:11 +00:00
}
2016-06-22 10:56:02 +00:00
function onStopCastError(e) {
onStopCast();
throw e.description;
}
2016-06-22 10:53:16 +00:00
function updateListener() {
if (session.status !== chrome.cast.SessionStatus.CONNECTED) {
onStopCast();
}
2014-04-04 21:57:41 +00:00
}
2014-04-04 14:38:36 +00:00
2016-06-22 10:53:16 +00:00
function sessionListener(e) {
session = e;
2017-05-09 17:32:39 +00:00
session.addMediaListener(onMediaDiscovered);
session.addUpdateListener(updateListener);
2016-06-22 10:53:16 +00:00
if (session.media.length !== 0) {
2017-05-09 17:32:39 +00:00
onMediaDiscovered();
2016-06-22 10:53:16 +00:00
}
}
2014-04-04 14:38:36 +00:00
2016-06-22 10:53:16 +00:00
function stopCast() {
2016-06-22 10:56:02 +00:00
session.stop(onStopCast, onStopCastError);
2016-06-22 10:53:16 +00:00
}
2014-04-04 14:38:36 +00:00
2016-06-22 10:58:54 +00:00
function onMediaError(e) {
2016-06-22 10:53:16 +00:00
stopCast();
2016-06-22 10:58:54 +00:00
throw e.description;
2016-06-22 10:53:16 +00:00
}
2014-04-04 14:38:36 +00:00
2016-06-22 10:53:16 +00:00
function onRequestSessionSuccess(e) {
session = e;
var videoLink = document.getElementById('video_link'), videoURL = videoLink.dataset.video, mediaInfo = new chrome.cast.media.MediaInfo(videoURL, 'video/' + videoLink.dataset.ext), request = new chrome.cast.media.LoadRequest(mediaInfo);
2017-05-09 17:32:39 +00:00
session.loadMedia(request, onMediaDiscovered, onMediaError);
2016-06-22 10:53:16 +00:00
}
2014-04-04 14:38:36 +00:00
2016-06-22 10:53:16 +00:00
function onLaunchError(e) {
throw e.description;
}
2014-04-04 14:38:36 +00:00
2016-06-22 10:53:16 +00:00
function launchCast() {
chrome.cast.requestSession(onRequestSessionSuccess, onLaunchError);
2014-04-04 14:38:36 +00:00
}
2016-06-22 10:53:16 +00:00
function onInitSuccess() {
launchBtn = document.getElementById('cast_btn_launch');
disabledBtn = document.getElementById('cast_disabled');
stopBtn = document.getElementById('cast_btn_stop');
if (launchBtn) {
disabledBtn.classList.add('cast_hidden');
launchBtn.classList.remove('cast_hidden');
launchBtn.addEventListener('click', launchCast, false);
stopBtn.addEventListener('click', stopCast, false);
}
}
2014-04-04 21:57:41 +00:00
2016-06-22 10:53:16 +00:00
function onError(e) {
throw e.code;
}
2014-04-04 21:57:41 +00:00
2016-06-22 10:53:16 +00:00
function initializeCastApi() {
2017-05-09 17:32:39 +00:00
var sessionRequest = new chrome.cast.SessionRequest(chrome.cast.media.DEFAULT_MEDIA_RECEIVER_APP_ID),
apiConfig = new chrome.cast.ApiConfig(sessionRequest, sessionListener, receiverListener, chrome.cast.AutoJoinPolicy.ORIGIN_SCOPED);
2016-06-22 10:53:16 +00:00
chrome.cast.initialize(apiConfig, onInitSuccess, onError);
2014-04-04 14:38:36 +00:00
}
2016-06-22 10:53:16 +00:00
function loadCastApi(loaded, errorInfo) {
if (loaded) {
initializeCastApi();
} else {
throw errorInfo;
}
2016-06-22 10:32:57 +00:00
}
2016-06-22 10:53:16 +00:00
return {
init: function () {
var intro = document.getElementById('download_intro');
if (intro) {
intro.insertAdjacentHTML('beforeend', '<img class="cast_icon" id="cast_disabled" src="img/ic_media_route_disabled_holo_light.png" alt="" title="Google Cast is not supported on this browser." /> <img class="cast_btn cast_hidden cast_icon" id="cast_btn_launch" src="img/ic_media_route_off_holo_light.png" title="Cast to ChromeCast" alt="Google Cast™" /> <img src="img/ic_media_route_on_holo_light.png" alt="Casting to ChromeCast…" title="Stop casting" id="cast_btn_stop" class="cast_btn cast_hidden cast_icon" />');
window.__onGCastApiAvailable = loadCastApi;
}
}
};
}());
window.addEventListener('load', castModule.init, false);