2015-05-18 09:40:20 -07:00
|
|
|
|
(function () {
|
|
|
|
|
|
|
|
|
|
var PlayerName = "Chromecast";
|
|
|
|
|
var ApplicationID = "F4EB2E8E";
|
2015-05-18 15:23:03 -07:00
|
|
|
|
var currentPairingDeviceId;
|
|
|
|
|
var currentPairedDeviceId;
|
|
|
|
|
var currentDeviceFriendlyName;
|
|
|
|
|
var currentWebAppSession;
|
2015-05-18 09:40:20 -07:00
|
|
|
|
|
|
|
|
|
function chromecastPlayer() {
|
|
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
|
|
// MediaController needs this
|
|
|
|
|
self.name = PlayerName;
|
|
|
|
|
|
|
|
|
|
self.getItemsForPlayback = function (query) {
|
|
|
|
|
|
|
|
|
|
var userId = Dashboard.getCurrentUserId();
|
|
|
|
|
|
|
|
|
|
query.Limit = query.Limit || 100;
|
|
|
|
|
query.ExcludeLocationTypes = "Virtual";
|
|
|
|
|
|
|
|
|
|
return ApiClient.getItems(userId, query);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var castPlayer = {};
|
|
|
|
|
|
|
|
|
|
$(castPlayer).on("connect", function (e) {
|
|
|
|
|
|
|
|
|
|
console.log('cc: connect');
|
|
|
|
|
// Reset this so the next query doesn't make it appear like content is playing.
|
|
|
|
|
self.lastPlayerData = {};
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$(castPlayer).on("playbackstart", function (e, data) {
|
|
|
|
|
|
|
|
|
|
console.log('cc: playbackstart');
|
|
|
|
|
|
|
|
|
|
var state = self.getPlayerStateInternal(data);
|
|
|
|
|
$(self).trigger("playbackstart", [state]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$(castPlayer).on("playbackstop", function (e, data) {
|
|
|
|
|
|
|
|
|
|
console.log('cc: playbackstop');
|
|
|
|
|
var state = self.getPlayerStateInternal(data);
|
|
|
|
|
|
|
|
|
|
$(self).trigger("playbackstop", [state]);
|
|
|
|
|
|
|
|
|
|
// Reset this so the next query doesn't make it appear like content is playing.
|
|
|
|
|
self.lastPlayerData = {};
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$(castPlayer).on("playbackprogress", function (e, data) {
|
|
|
|
|
|
|
|
|
|
console.log('cc: positionchange');
|
|
|
|
|
var state = self.getPlayerStateInternal(data);
|
|
|
|
|
|
|
|
|
|
$(self).trigger("positionchange", [state]);
|
|
|
|
|
});
|
|
|
|
|
|
2015-05-18 15:23:03 -07:00
|
|
|
|
var endpointInfo;
|
|
|
|
|
function getEndpointInfo() {
|
|
|
|
|
|
|
|
|
|
if (endpointInfo) {
|
|
|
|
|
|
|
|
|
|
var deferred = $.Deferred();
|
|
|
|
|
deferred.resolveWith(null, [endpointInfo]);
|
|
|
|
|
return deferred.promise();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ApiClient.getJSON(ApiClient.getUrl('System/Endpoint')).done(function (info) {
|
|
|
|
|
|
|
|
|
|
endpointInfo = info;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function sendMessageToDevice(message) {
|
|
|
|
|
|
|
|
|
|
var bitrateSetting = AppSettings.maxChromecastBitrate();
|
|
|
|
|
|
|
|
|
|
message = $.extend(message, {
|
|
|
|
|
userId: Dashboard.getCurrentUserId(),
|
|
|
|
|
deviceId: ApiClient.deviceId(),
|
|
|
|
|
accessToken: ApiClient.accessToken(),
|
|
|
|
|
serverAddress: ApiClient.serverAddress(),
|
|
|
|
|
maxBitrate: bitrateSetting,
|
|
|
|
|
receiverName: currentDeviceFriendlyName
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
getEndpointInfo().done(function (endpoint) {
|
|
|
|
|
|
|
|
|
|
if (endpoint.IsLocal || endpoint.IsInNetwork) {
|
|
|
|
|
ApiClient.getSystemInfo().done(function (info) {
|
|
|
|
|
|
|
|
|
|
message.serverAddress = info.LocalAddress;
|
|
|
|
|
sendMessageInternal(message);
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
sendMessageInternal(message);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function sendMessageInternal(message) {
|
|
|
|
|
currentWebAppSession.sendText(JSON.stringify(message));
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-18 09:40:20 -07:00
|
|
|
|
self.play = function (options) {
|
|
|
|
|
|
|
|
|
|
Dashboard.getCurrentUser().done(function (user) {
|
|
|
|
|
|
|
|
|
|
if (options.items) {
|
|
|
|
|
|
|
|
|
|
self.playWithCommand(options, 'PlayNow');
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
self.getItemsForPlayback({
|
|
|
|
|
|
|
|
|
|
Ids: options.ids.join(',')
|
|
|
|
|
|
|
|
|
|
}).done(function (result) {
|
|
|
|
|
|
|
|
|
|
options.items = result.Items;
|
|
|
|
|
self.playWithCommand(options, 'PlayNow');
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
self.playWithCommand = function (options, command) {
|
|
|
|
|
|
|
|
|
|
if (!options.items) {
|
|
|
|
|
ApiClient.getItem(Dashboard.getCurrentUserId(), options.ids[0]).done(function (item) {
|
|
|
|
|
|
|
|
|
|
options.items = [item];
|
|
|
|
|
self.playWithCommand(options, command);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-18 15:23:03 -07:00
|
|
|
|
// Convert the items to smaller stubs to send the minimal amount of information
|
|
|
|
|
options.items = options.items.map(function (i) {
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
Id: i.Id,
|
|
|
|
|
Name: i.Name,
|
|
|
|
|
Type: i.Type,
|
|
|
|
|
MediaType: i.MediaType,
|
|
|
|
|
IsFolder: i.IsFolder
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
sendMessageToDevice({
|
|
|
|
|
options: options,
|
|
|
|
|
command: command
|
|
|
|
|
});
|
2015-05-18 09:40:20 -07:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
self.unpause = function () {
|
2015-05-18 15:23:03 -07:00
|
|
|
|
sendMessageToDevice({
|
2015-05-18 18:46:31 -07:00
|
|
|
|
command: 'Unpause'
|
2015-05-18 15:23:03 -07:00
|
|
|
|
});
|
2015-05-18 09:40:20 -07:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
self.pause = function () {
|
2015-05-18 15:23:03 -07:00
|
|
|
|
sendMessageToDevice({
|
2015-05-18 18:46:31 -07:00
|
|
|
|
command: 'Pause'
|
2015-05-18 15:23:03 -07:00
|
|
|
|
});
|
2015-05-18 09:40:20 -07:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
self.shuffle = function (id) {
|
|
|
|
|
|
|
|
|
|
var userId = Dashboard.getCurrentUserId();
|
|
|
|
|
|
|
|
|
|
ApiClient.getItem(userId, id).done(function (item) {
|
|
|
|
|
|
|
|
|
|
self.playWithCommand({
|
|
|
|
|
|
|
|
|
|
items: [item]
|
|
|
|
|
|
|
|
|
|
}, 'Shuffle');
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
self.instantMix = function (id) {
|
|
|
|
|
|
|
|
|
|
var userId = Dashboard.getCurrentUserId();
|
|
|
|
|
|
|
|
|
|
ApiClient.getItem(userId, id).done(function (item) {
|
|
|
|
|
|
|
|
|
|
self.playWithCommand({
|
|
|
|
|
|
|
|
|
|
items: [item]
|
|
|
|
|
|
|
|
|
|
}, 'InstantMix');
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
self.canQueueMediaType = function (mediaType) {
|
|
|
|
|
return mediaType == "Audio";
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
self.queue = function (options) {
|
|
|
|
|
self.playWithCommnd(options, 'PlayLast');
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
self.queueNext = function (options) {
|
|
|
|
|
self.playWithCommand(options, 'PlayNext');
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
self.stop = function () {
|
2015-05-18 15:23:03 -07:00
|
|
|
|
sendMessageToDevice({
|
2015-05-18 18:46:31 -07:00
|
|
|
|
command: 'Stop'
|
2015-05-18 15:23:03 -07:00
|
|
|
|
});
|
2015-05-18 09:40:20 -07:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
self.displayContent = function (options) {
|
|
|
|
|
|
2015-05-18 15:23:03 -07:00
|
|
|
|
sendMessageToDevice({
|
2015-05-18 09:40:20 -07:00
|
|
|
|
options: options,
|
|
|
|
|
command: 'DisplayContent'
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
self.mute = function () {
|
2015-05-18 15:23:03 -07:00
|
|
|
|
sendMessageToDevice({
|
2015-05-18 18:46:31 -07:00
|
|
|
|
command: 'Mute'
|
2015-05-18 15:23:03 -07:00
|
|
|
|
});
|
2015-05-18 09:40:20 -07:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
self.unMute = function () {
|
|
|
|
|
self.setVolume(getCurrentVolume() + 2);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
self.toggleMute = function () {
|
|
|
|
|
|
|
|
|
|
var state = self.lastPlayerData || {};
|
|
|
|
|
state = state.PlayState || {};
|
|
|
|
|
|
|
|
|
|
if (state.IsMuted) {
|
|
|
|
|
self.unMute();
|
|
|
|
|
} else {
|
|
|
|
|
self.mute();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function getBaseTargetInfo() {
|
|
|
|
|
var target = {};
|
|
|
|
|
|
|
|
|
|
target.playerName = PlayerName;
|
|
|
|
|
target.playableMediaTypes = ["Audio", "Video"];
|
|
|
|
|
target.isLocalPlayer = false;
|
|
|
|
|
target.appName = PlayerName;
|
|
|
|
|
target.supportedCommands = [
|
|
|
|
|
"VolumeUp",
|
|
|
|
|
"VolumeDown",
|
|
|
|
|
"Mute",
|
|
|
|
|
"Unmute",
|
|
|
|
|
"ToggleMute",
|
|
|
|
|
"SetVolume",
|
|
|
|
|
"SetAudioStreamIndex",
|
|
|
|
|
"SetSubtitleStreamIndex",
|
|
|
|
|
"DisplayContent"
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
return target;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function convertDeviceToTarget(device) {
|
|
|
|
|
|
|
|
|
|
var target = getBaseTargetInfo();
|
|
|
|
|
|
|
|
|
|
target.name = target.deviceName = device.getFriendlyName();
|
|
|
|
|
target.id = device.getId();
|
|
|
|
|
|
|
|
|
|
return target;
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-18 15:23:03 -07:00
|
|
|
|
function isChromecast(name) {
|
|
|
|
|
|
2015-05-19 12:15:40 -07:00
|
|
|
|
name = (name || '').toLowerCase();
|
2015-05-18 15:23:03 -07:00
|
|
|
|
var validTokens = ['nexusplayer', 'chromecast', 'eurekadongle'];
|
|
|
|
|
|
|
|
|
|
return validTokens.filter(function (t) {
|
|
|
|
|
|
2015-05-19 12:15:40 -07:00
|
|
|
|
return name.replace(' ', '').indexOf(t) != -1;
|
2015-05-18 15:23:03 -07:00
|
|
|
|
|
|
|
|
|
}).length > 0;
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-18 09:40:20 -07:00
|
|
|
|
self.getTargets = function () {
|
|
|
|
|
|
|
|
|
|
var manager = ConnectSDK.discoveryManager;
|
|
|
|
|
|
2015-05-18 15:23:03 -07:00
|
|
|
|
return manager.getDeviceList().filter(function (d) {
|
|
|
|
|
|
|
|
|
|
return isChromecast(d.getModelName()) || isChromecast(d.getFriendlyName());
|
|
|
|
|
|
|
|
|
|
}).map(convertDeviceToTarget);
|
2015-05-18 09:40:20 -07:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
self.seek = function (position) {
|
2015-05-18 15:23:03 -07:00
|
|
|
|
sendMessageToDevice({
|
|
|
|
|
options: {
|
|
|
|
|
position: position
|
|
|
|
|
},
|
|
|
|
|
command: 'Seek'
|
|
|
|
|
});
|
2015-05-18 09:40:20 -07:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
self.setAudioStreamIndex = function (index) {
|
2015-05-18 15:23:03 -07:00
|
|
|
|
sendMessageToDevice({
|
2015-05-18 09:40:20 -07:00
|
|
|
|
options: {
|
|
|
|
|
index: index
|
|
|
|
|
},
|
|
|
|
|
command: 'SetAudioStreamIndex'
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
self.setSubtitleStreamIndex = function (index) {
|
2015-05-18 15:23:03 -07:00
|
|
|
|
sendMessageToDevice({
|
2015-05-18 09:40:20 -07:00
|
|
|
|
options: {
|
|
|
|
|
index: index
|
|
|
|
|
},
|
|
|
|
|
command: 'SetSubtitleStreamIndex'
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
self.nextTrack = function () {
|
2015-05-18 15:23:03 -07:00
|
|
|
|
sendMessageToDevice({
|
2015-05-18 09:40:20 -07:00
|
|
|
|
options: {},
|
|
|
|
|
command: 'NextTrack'
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
self.previousTrack = function () {
|
2015-05-18 15:23:03 -07:00
|
|
|
|
sendMessageToDevice({
|
2015-05-18 09:40:20 -07:00
|
|
|
|
options: {},
|
|
|
|
|
command: 'PreviousTrack'
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
self.beginPlayerUpdates = function () {
|
|
|
|
|
// Setup polling here
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
self.endPlayerUpdates = function () {
|
|
|
|
|
// Stop polling here
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function getCurrentVolume() {
|
|
|
|
|
var state = self.lastPlayerData || {};
|
|
|
|
|
state = state.PlayState || {};
|
|
|
|
|
|
|
|
|
|
return state.VolumeLevel == null ? 100 : state.VolumeLevel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.volumeDown = function () {
|
|
|
|
|
|
|
|
|
|
self.setVolume(getCurrentVolume() - 2);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
self.volumeUp = function () {
|
|
|
|
|
|
|
|
|
|
self.setVolume(getCurrentVolume() + 2);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
self.setVolume = function (vol) {
|
|
|
|
|
|
|
|
|
|
vol = Math.min(vol, 100);
|
|
|
|
|
vol = Math.max(vol, 0);
|
|
|
|
|
|
2015-05-18 15:23:03 -07:00
|
|
|
|
sendMessageToDevice({
|
|
|
|
|
options: {
|
2015-05-18 18:46:31 -07:00
|
|
|
|
volume: vol
|
2015-05-18 15:23:03 -07:00
|
|
|
|
},
|
|
|
|
|
command: 'SetVolume'
|
|
|
|
|
});
|
2015-05-18 09:40:20 -07:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
self.getPlayerState = function () {
|
|
|
|
|
|
|
|
|
|
var deferred = $.Deferred();
|
|
|
|
|
|
|
|
|
|
var result = self.getPlayerStateInternal();
|
|
|
|
|
|
|
|
|
|
deferred.resolveWith(null, [result]);
|
|
|
|
|
|
|
|
|
|
return deferred.promise();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
self.lastPlayerData = {};
|
|
|
|
|
|
|
|
|
|
self.getPlayerStateInternal = function (data) {
|
|
|
|
|
|
|
|
|
|
data = data || self.lastPlayerData;
|
|
|
|
|
self.lastPlayerData = data;
|
|
|
|
|
|
|
|
|
|
console.log(JSON.stringify(data));
|
|
|
|
|
return data;
|
|
|
|
|
};
|
|
|
|
|
|
2015-05-18 15:23:03 -07:00
|
|
|
|
function onMessage(message) {
|
|
|
|
|
|
|
|
|
|
if (message.type == 'playbackerror') {
|
|
|
|
|
|
|
|
|
|
var errorCode = message.data;
|
|
|
|
|
|
|
|
|
|
setTimeout(function () {
|
|
|
|
|
Dashboard.alert({
|
|
|
|
|
message: Globalize.translate('MessagePlaybackError' + errorCode),
|
|
|
|
|
title: Globalize.translate('HeaderPlaybackError')
|
|
|
|
|
});
|
|
|
|
|
}, 300);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else if (message.type == 'connectionerror') {
|
|
|
|
|
|
|
|
|
|
setTimeout(function () {
|
|
|
|
|
Dashboard.alert({
|
|
|
|
|
message: Globalize.translate('MessageChromecastConnectionError'),
|
|
|
|
|
title: Globalize.translate('HeaderError')
|
|
|
|
|
});
|
|
|
|
|
}, 300);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else if (message.type && message.type.indexOf('playback') == 0) {
|
|
|
|
|
$(castPlayer).trigger(message.type, [message.data]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function onSessionConnected(device, session) {
|
|
|
|
|
|
|
|
|
|
// hold on to a reference
|
|
|
|
|
currentWebAppSession = session.acquire();
|
|
|
|
|
|
|
|
|
|
session.connect().success(function () {
|
|
|
|
|
|
|
|
|
|
console.log('session.connect succeeded');
|
|
|
|
|
|
|
|
|
|
MediaController.setActivePlayer(PlayerName, convertDeviceToTarget(device));
|
|
|
|
|
currentDeviceFriendlyName = device.getFriendlyName();
|
|
|
|
|
currentPairedDeviceId = device.getId();
|
|
|
|
|
|
|
|
|
|
$(castPlayer).trigger('connect');
|
|
|
|
|
|
|
|
|
|
sendMessageToDevice({
|
|
|
|
|
options: {},
|
|
|
|
|
command: 'Identify'
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
session.on('message', function (message) {
|
|
|
|
|
// message could be either a string or an object
|
|
|
|
|
if (typeof message === 'string') {
|
|
|
|
|
onMessage(JSON.parse(message));
|
|
|
|
|
} else {
|
|
|
|
|
onMessage(message);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
session.on('disconnect', function () {
|
|
|
|
|
|
|
|
|
|
console.log("session disconnected");
|
|
|
|
|
|
|
|
|
|
if (currentPairedDeviceId == device.getId()) {
|
|
|
|
|
onDisconnected();
|
|
|
|
|
MediaController.removeActivePlayer(PlayerName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function onDisconnected() {
|
|
|
|
|
currentWebAppSession = null;
|
|
|
|
|
currentPairedDeviceId = null;
|
|
|
|
|
currentDeviceFriendlyName = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function launchWebApp(device) {
|
|
|
|
|
device.getWebAppLauncher().launchWebApp(ApplicationID).success(function (session) {
|
|
|
|
|
|
|
|
|
|
console.log('launchWebApp success. calling onSessionConnected');
|
|
|
|
|
onSessionConnected(device, session);
|
|
|
|
|
|
|
|
|
|
}).error(function (err) {
|
|
|
|
|
|
|
|
|
|
console.log('launchWebApp error: ' + JSON.stringify(err) + '. calling joinWebApp');
|
|
|
|
|
|
|
|
|
|
device.getWebAppLauncher().joinWebApp(ApplicationID).success(function (session) {
|
|
|
|
|
|
|
|
|
|
console.log('joinWebApp success. calling onSessionConnected');
|
|
|
|
|
onSessionConnected(device, session);
|
|
|
|
|
|
|
|
|
|
}).error(function (err1) {
|
|
|
|
|
|
|
|
|
|
console.log('joinWebApp error:' + JSON.stringify(err1));
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function onDeviceReady(device) {
|
|
|
|
|
|
|
|
|
|
if (currentPairingDeviceId != device.getId()) {
|
|
|
|
|
console.log('device ready fired for a different device. ignoring.');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.log('calling launchWebApp');
|
|
|
|
|
|
|
|
|
|
setTimeout(function () {
|
|
|
|
|
|
|
|
|
|
launchWebApp(device);
|
|
|
|
|
|
|
|
|
|
}, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var boundHandlers = [];
|
|
|
|
|
|
2015-05-18 09:40:20 -07:00
|
|
|
|
self.tryPair = function (target) {
|
|
|
|
|
|
|
|
|
|
var deferred = $.Deferred();
|
|
|
|
|
|
2015-05-18 15:23:03 -07:00
|
|
|
|
var manager = ConnectSDK.discoveryManager;
|
2015-05-18 09:40:20 -07:00
|
|
|
|
|
2015-05-18 15:23:03 -07:00
|
|
|
|
var device = manager.getDeviceList().filter(function (d) {
|
2015-05-18 09:40:20 -07:00
|
|
|
|
|
2015-05-18 15:23:03 -07:00
|
|
|
|
return d.getId() == target.id;
|
|
|
|
|
})[0];
|
2015-05-18 09:40:20 -07:00
|
|
|
|
|
2015-05-18 15:23:03 -07:00
|
|
|
|
if (device) {
|
2015-05-18 09:40:20 -07:00
|
|
|
|
|
2015-05-18 15:23:03 -07:00
|
|
|
|
var deviceId = device.getId();
|
|
|
|
|
currentPairingDeviceId = deviceId;
|
2015-05-18 09:40:20 -07:00
|
|
|
|
|
2015-05-18 15:23:03 -07:00
|
|
|
|
console.log('Will attempt to connect to Chromecast');
|
|
|
|
|
|
|
|
|
|
if (device.isReady()) {
|
|
|
|
|
console.log('Device is already ready, calling onDeviceReady');
|
|
|
|
|
onDeviceReady(device);
|
|
|
|
|
} else {
|
2015-05-18 09:40:20 -07:00
|
|
|
|
|
2015-05-18 15:23:03 -07:00
|
|
|
|
console.log('Binding device ready handler');
|
2015-05-18 09:40:20 -07:00
|
|
|
|
|
2015-05-18 15:23:03 -07:00
|
|
|
|
if (boundHandlers.indexOf(deviceId) == -1) {
|
2015-05-18 09:40:20 -07:00
|
|
|
|
|
2015-05-18 15:23:03 -07:00
|
|
|
|
boundHandlers.push(deviceId);
|
|
|
|
|
device.on("ready", function () {
|
|
|
|
|
console.log('device.ready fired');
|
|
|
|
|
onDeviceReady(device);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.log('Calling device.connect');
|
|
|
|
|
device.connect();
|
|
|
|
|
}
|
|
|
|
|
//deferred.resolve();
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
deferred.reject();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return deferred.promise();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$(MediaController).on('playerchange', function (e, newPlayer, newTarget) {
|
|
|
|
|
|
|
|
|
|
if (newPlayer.name != PlayerName || newTarget.id != currentPairedDeviceId) {
|
|
|
|
|
if (currentWebAppSession) {
|
|
|
|
|
currentWebAppSession.disconnect();
|
|
|
|
|
onDisconnected();
|
|
|
|
|
}
|
2015-05-18 09:40:20 -07:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-18 15:23:03 -07:00
|
|
|
|
function initSdk() {
|
|
|
|
|
|
|
|
|
|
MediaController.registerPlayer(new chromecastPlayer());
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-18 09:40:20 -07:00
|
|
|
|
initSdk();
|
|
|
|
|
|
|
|
|
|
})();
|