alltube/js/cast.js

111 lines
2.7 KiB
JavaScript
Raw Normal View History

2014-04-04 14:38:36 +00:00
/*global chrome*/
/*jslint devel: true, browser: true */
var launchBtn, disabledBtn, stopBtn;
var session, currentMedia;
2016-03-29 23:49:08 +00:00
function receiverListener(e)
{
2014-04-04 14:38:36 +00:00
'use strict';
2014-04-04 21:57:41 +00:00
console.log('receiverListener', e);
2014-04-04 14:38:36 +00:00
}
2016-03-29 23:49:08 +00:00
function onMediaDiscovered(how, media)
{
2014-04-04 14:38:36 +00:00
'use strict';
2014-04-04 21:57:41 +00:00
console.log('onMediaDiscovered', how);
2014-04-04 14:38:36 +00:00
currentMedia = media;
2014-04-04 21:57:41 +00:00
if (launchBtn) {
stopBtn.classList.remove('cast_hidden');
launchBtn.classList.add('cast_hidden');
}
2014-04-04 14:38:36 +00:00
}
2016-03-29 23:49:08 +00:00
function sessionListener(e)
{
2014-04-04 14:38:36 +00:00
'use strict';
session = e;
2014-04-04 21:57:41 +00:00
session.addMediaListener(onMediaDiscovered.bind(this, 'addMediaListener'));
if (session.media.length !== 0) {
onMediaDiscovered('onRequestSessionSuccess', session.media[0]);
}
2014-04-04 14:38:36 +00:00
}
2016-03-29 23:49:08 +00:00
function onStopCast()
{
2014-04-04 14:38:36 +00:00
'use strict';
2014-04-04 21:57:41 +00:00
stopBtn.classList.add('cast_hidden');
launchBtn.classList.remove('cast_hidden');
2014-04-04 14:38:36 +00:00
}
2016-03-29 23:49:08 +00:00
function stopCast()
{
2014-04-04 14:38:36 +00:00
'use strict';
2014-04-04 21:57:41 +00:00
session.stop(onStopCast);
2014-04-04 14:38:36 +00:00
}
2016-03-29 23:49:08 +00:00
function onMediaError()
{
2014-04-04 14:38:36 +00:00
'use strict';
2014-04-04 21:57:41 +00:00
console.log('onMediaError');
stopCast();
2014-04-04 14:38:36 +00:00
}
2016-03-29 23:49:08 +00:00
function onRequestSessionSuccess(e)
{
2014-04-04 14:38:36 +00:00
'use strict';
2014-04-04 21:57:41 +00:00
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);
session.loadMedia(request, onMediaDiscovered.bind(this, 'loadMedia'), onMediaError);
2014-04-04 14:38:36 +00:00
}
2016-03-29 23:49:08 +00:00
function onLaunchError(e)
{
2014-04-04 14:38:36 +00:00
'use strict';
2014-04-04 21:57:41 +00:00
console.log('onLaunchError', e.description);
2014-04-04 14:38:36 +00:00
}
2016-03-29 23:49:08 +00:00
function launchCast()
{
2014-04-04 14:38:36 +00:00
'use strict';
2014-04-04 21:57:41 +00:00
chrome.cast.requestSession(onRequestSessionSuccess, onLaunchError);
2014-04-04 14:38:36 +00:00
}
2016-03-29 23:49:08 +00:00
function onInitSuccess()
{
2014-04-04 14:38:36 +00:00
'use strict';
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);
}
}
2016-03-29 23:49:08 +00:00
function onError()
{
2014-04-04 21:57:41 +00:00
'use strict';
console.log('onError');
}
2016-03-29 23:49:08 +00:00
function initializeCastApi()
{
2014-04-04 21:57:41 +00:00
'use strict';
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);
chrome.cast.initialize(apiConfig, onInitSuccess, onError);
}
2016-03-29 23:49:08 +00:00
function loadCastApi(loaded, errorInfo)
{
2014-04-04 14:38:36 +00:00
'use strict';
if (loaded) {
initializeCastApi();
} else {
console.log(errorInfo);
}
}
window['__onGCastApiAvailable'] = loadCastApi;