jellyfin-web/dashboard-ui/cordova/generaldevice.js

675 lines
18 KiB
JavaScript
Raw Normal View History

2015-05-19 12:15:40 -07:00
(function () {
2015-06-15 21:52:01 -07:00
function connectSDKPlayer() {
2015-05-19 12:15:40 -07:00
var self = this;
2015-06-15 21:52:01 -07:00
var PlayerName = "ConnectSDK";
var currentDevice;
var currentDeviceId;
2015-06-17 18:41:22 -07:00
var currentMediaControl;
2015-06-15 21:52:01 -07:00
2015-05-19 12:15:40 -07:00
// 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("playbackstart", function (e, data) {
2015-06-26 20:27:38 -07:00
Logger.log('cc: playbackstart');
2015-05-19 12:15:40 -07:00
var state = self.getPlayerStateInternal(data);
$(self).trigger("playbackstart", [state]);
});
$(castPlayer).on("playbackstop", function (e, data) {
2015-06-26 20:27:38 -07:00
Logger.log('cc: playbackstop');
2015-05-19 12:15:40 -07:00
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) {
2015-06-26 20:27:38 -07:00
Logger.log('cc: positionchange');
2015-05-19 12:15:40 -07:00
var state = self.getPlayerStateInternal(data);
$(self).trigger("positionchange", [state]);
});
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;
}
playItemInternal(items[0], null, serverAddress);
2015-06-15 21:52:01 -07:00
};
2015-05-19 12:15:40 -07:00
2015-06-17 18:41:22 -07:00
function validatePlaybackInfoResult(result) {
if (result.ErrorCode) {
MediaController.showPlaybackInfoErrorMessage(result.ErrorCode);
return false;
}
return true;
}
function getOptimalMediaSource(mediaType, versions) {
var optimalVersion = versions.filter(function (v) {
v.enableDirectPlay = MediaController.supportsDirectPlay(v);
return v.enableDirectPlay;
})[0];
if (!optimalVersion) {
optimalVersion = versions.filter(function (v) {
return v.SupportsDirectStream;
})[0];
}
return optimalVersion || versions.filter(function (s) {
return s.SupportsTranscoding;
})[0];
}
function playItemInternal(item, startPosition) {
2015-05-19 12:15:40 -07:00
if (item == null) {
throw new Error("item cannot be null");
}
if (item.MediaType !== 'Audio' && item.MediaType !== 'Video') {
throw new Error("Unrecognized media type");
}
if (item.IsPlaceHolder) {
MediaController.showPlaybackInfoErrorMessage('PlaceHolder');
return;
}
var deviceProfile = self.getDeviceProfile();
if (item.MediaType === "Video") {
Dashboard.showModalLoadingMsg();
}
2015-06-17 18:41:22 -07:00
MediaController.getPlaybackInfo(item.Id, deviceProfile, startPosition).done(function (playbackInfoResult) {
2015-05-19 12:15:40 -07:00
2015-06-17 18:41:22 -07:00
if (validatePlaybackInfoResult(playbackInfoResult)) {
2015-05-19 12:15:40 -07:00
2015-06-17 18:41:22 -07:00
var mediaSource = getOptimalMediaSource(item.MediaType, playbackInfoResult.MediaSources);
2015-05-19 12:15:40 -07:00
2015-06-17 18:41:22 -07:00
if (mediaSource) {
2015-05-19 12:15:40 -07:00
2015-06-17 18:41:22 -07:00
if (mediaSource.RequiresOpening) {
2015-05-19 12:15:40 -07:00
2015-06-17 18:41:22 -07:00
getLiveStream(item.Id, playbackInfoResult.PlaySessionId, deviceProfile, startPosition, mediaSource, null, null).done(function (openLiveStreamResult) {
2015-05-19 12:15:40 -07:00
2015-06-17 18:41:22 -07:00
openLiveStreamResult.MediaSource.enableDirectPlay = supportsDirectPlay(openLiveStreamResult.MediaSource);
2015-05-19 12:15:40 -07:00
2015-06-17 18:41:22 -07:00
playInternalPostMediaSourceSelection(item, openLiveStreamResult.MediaSource, startPosition, callback);
});
2015-05-19 12:15:40 -07:00
2015-06-17 18:41:22 -07:00
} else {
playInternalPostMediaSourceSelection(item, mediaSource, startPosition, callback);
}
} else {
Dashboard.hideModalLoadingMsg();
MediaController.showPlaybackInfoErrorMessage('NoCompatibleStream');
}
}
2015-05-19 12:15:40 -07:00
2015-06-17 18:41:22 -07:00
});
}
function playInternalPostMediaSourceSelection(item, mediaSource, startPosition, deferred) {
Dashboard.hideModalLoadingMsg();
var streamInfo = MediaPlayer.createStreamInfo('Video', item, mediaSource, startPosition);
currentDevice.getMediaPlayer().playMedia(
streamInfo.url,
streamInfo.MimeType,
{
title: item.Name,
description: item.Overview || '',
shouldLoop: false
}
).success(function (launchSession, mediaControl) {
2015-06-26 20:27:38 -07:00
Logger.log("Video launch successful");
2015-06-17 18:41:22 -07:00
currentMediaControl = mediaControl && mediaControl.acquire();
}).error(function (err) {
2015-06-26 20:27:38 -07:00
Logger.log("error: " + err.message);
2015-06-17 18:41:22 -07:00
});
deferred.resolveWith(null, [streamInfo]);
2015-05-19 12:15:40 -07:00
}
2015-06-17 18:41:22 -07:00
2015-05-19 12:15:40 -07:00
self.unpause = function () {
2015-06-17 18:41:22 -07:00
if (currentMediaControl) {
currentMediaControl.pause();
2015-06-15 21:52:01 -07:00
}
2015-05-19 12:15:40 -07:00
};
self.pause = function () {
2015-06-17 18:41:22 -07:00
if (currentMediaControl) {
currentMediaControl.pause();
2015-06-15 21:52:01 -07:00
}
2015-05-19 12:15:40 -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) {
2015-06-15 21:52:01 -07:00
return mediaType == "Audio";
2015-05-19 12:15:40 -07:00
};
self.queue = function (options) {
2015-06-15 21:52:01 -07:00
self.playWithCommnd(options, 'PlayLast');
2015-05-19 12:15:40 -07:00
};
self.queueNext = function (options) {
2015-06-15 21:52:01 -07:00
self.playWithCommand(options, 'PlayNext');
2015-05-19 12:15:40 -07:00
};
self.stop = function () {
2015-06-17 18:41:22 -07:00
if (currentMediaControl) {
currentMediaControl.stop();
2015-06-15 21:52:01 -07:00
}
2015-05-19 12:15:40 -07:00
};
self.displayContent = function (options) {
2015-06-15 21:52:01 -07:00
// TODO
2015-05-19 12:15:40 -07:00
};
self.mute = function () {
2015-06-15 21:52:01 -07:00
if (currentDevice) {
currentDevice.getVolumeControl().setMute(true);
}
2015-05-19 12:15:40 -07:00
};
self.unMute = function () {
2015-06-15 21:52:01 -07:00
self.setVolume(getCurrentVolume() + 2);
2015-05-19 12:15:40 -07:00
};
self.toggleMute = function () {
2015-06-15 21:52:01 -07:00
var state = self.lastPlayerData || {};
state = state.PlayState || {};
2015-05-19 12:15:40 -07:00
2015-06-15 21:52:01 -07:00
if (state.IsMuted) {
self.unMute();
} else {
self.mute();
}
2015-05-19 12:15:40 -07:00
};
self.getDeviceProfile = function () {
var qualityOption = self.getVideoQualityOptions().filter(function (q) {
return q.selected;
})[0];
var bitrateSetting = AppSettings.maxStreamingBitrate();
var profile = {};
profile.MaxStreamingBitrate = bitrateSetting;
profile.MaxStaticBitrate = 40000000;
profile.MusicStreamingTranscodingBitrate = Math.min(bitrateSetting, 192000);
profile.DirectPlayProfiles = [];
profile.DirectPlayProfiles.push({
Container: 'mp4,m4v',
Type: 'Video',
VideoCodec: 'h264',
AudioCodec: 'aac,mp3,ac3'
});
profile.DirectPlayProfiles.push({
Container: 'mov',
Type: 'Video'
});
profile.DirectPlayProfiles.push({
Container: 'mp3',
Type: 'Audio'
});
profile.DirectPlayProfiles.push({
Container: 'aac',
Type: 'Audio'
});
profile.TranscodingProfiles = [];
profile.TranscodingProfiles.push({
Container: 'mp3',
Type: 'Audio',
AudioCodec: 'mp3',
Context: 'Streaming',
Protocol: 'http'
});
if (self.canPlayHls()) {
profile.TranscodingProfiles.push({
Container: 'ts',
Type: 'Video',
AudioCodec: 'aac',
VideoCodec: 'h264',
Context: 'Streaming',
Protocol: 'hls'
});
}
profile.TranscodingProfiles.push({
Container: 'mp4',
Type: 'Video',
AudioCodec: 'aac',
VideoCodec: 'h264',
Context: 'Streaming',
Protocol: 'http'
});
profile.ContainerProfiles = [];
profile.CodecProfiles = [];
profile.CodecProfiles.push({
Type: 'Audio',
Conditions: [{
Condition: 'LessThanEqual',
Property: 'AudioChannels',
Value: '2'
}]
});
profile.CodecProfiles.push({
Type: 'VideoAudio',
Codec: 'aac,mp3',
Conditions: [
{
Condition: 'LessThanEqual',
Property: 'AudioChannels',
Value: '6'
}
]
});
profile.CodecProfiles.push({
Type: 'Video',
Codec: 'h264',
Conditions: [
{
Condition: 'NotEquals',
Property: 'IsAnamorphic',
Value: 'true',
IsRequired: false
},
{
Condition: 'EqualsAny',
Property: 'VideoProfile',
Value: 'high|main|baseline|constrained baseline'
},
{
Condition: 'LessThanEqual',
Property: 'VideoLevel',
Value: '41'
},
{
Condition: 'LessThanEqual',
Property: 'Width',
Value: qualityOption.maxWidth
}]
});
// Subtitle profiles
profile.SubtitleProfiles = [];
profile.ResponseProfiles = [];
profile.ResponseProfiles.push({
Type: 'Video',
Container: 'm4v',
MimeType: 'video/mp4'
});
//profile.ResponseProfiles.push({
// Type: 'Video',
// Container: 'mkv',
// MimeType: 'video/webm'
//});
return profile;
};
function getBaseTargetInfo() {
var target = {};
target.playableMediaTypes = ["Audio", "Video"];
target.isLocalPlayer = false;
target.supportedCommands = [
"VolumeUp",
"VolumeDown",
"Mute",
"Unmute",
"ToggleMute",
"SetVolume"
];
return target;
}
function convertDeviceToTarget(device) {
var target = getBaseTargetInfo();
target.appName = target.name = target.deviceName = device.getFriendlyName();
target.playerName = PlayerName;
target.id = device.getId();
return target;
}
function isValid(device) {
var validTokens = ['AirPlay', 'Airplay', 'airplay'];
return validTokens.filter(function (t) {
return device.hasService(t);
}).length > 0;
}
self.getTargets = function () {
2015-06-12 20:42:19 -07:00
return ConnectSDKHelper.getDeviceList().filter(function (d) {
2015-05-19 12:15:40 -07:00
return isValid(d);
}).map(convertDeviceToTarget);
};
self.seek = function (position) {
2015-06-15 21:52:01 -07:00
position = parseInt(position);
position = position / 10000000;
// TODO
2015-05-19 12:15:40 -07:00
};
self.setAudioStreamIndex = function (index) {
2015-06-15 21:52:01 -07:00
// TODO
2015-05-19 12:15:40 -07:00
};
self.setSubtitleStreamIndex = function (index) {
2015-06-15 21:52:01 -07:00
// TODO
2015-05-19 12:15:40 -07:00
};
self.nextTrack = function () {
2015-06-15 21:52:01 -07:00
// TODO
2015-05-19 12:15:40 -07:00
};
self.previousTrack = function () {
2015-06-15 21:52:01 -07:00
// TODO
2015-05-19 12:15:40 -07:00
};
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 () {
2015-06-15 21:52:01 -07:00
if (currentDevice) {
currentDevice.getVolumeControl().volumeDown();
}
2015-05-19 12:15:40 -07:00
};
self.volumeUp = function () {
2015-06-15 21:52:01 -07:00
if (currentDevice) {
currentDevice.getVolumeControl().volumeUp();
}
2015-05-19 12:15:40 -07:00
};
self.setVolume = function (vol) {
vol = Math.min(vol, 100);
vol = Math.max(vol, 0);
2015-06-15 21:52:01 -07:00
if (currentDevice) {
currentDevice.getVolumeControl().setVolume(vol / 100);
}
2015-05-19 12:15:40 -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;
2015-06-26 20:27:38 -07:00
Logger.log(JSON.stringify(data));
2015-05-19 12:15:40 -07:00
return data;
};
2015-06-15 21:52:01 -07:00
function handleSessionDisconnect() {
2015-06-26 20:27:38 -07:00
Logger.log("session disconnected");
2015-06-15 21:52:01 -07:00
cleanupSession();
MediaController.removeActivePlayer(PlayerName);
}
2015-06-04 22:32:14 -07:00
function cleanupSession() {
2015-05-19 12:15:40 -07:00
2015-06-15 21:52:01 -07:00
}
2015-06-04 22:32:14 -07:00
2015-06-15 21:52:01 -07:00
function launchWebApp(device) {
if (currentDevice) {
cleanupSession();
2015-05-19 12:15:40 -07:00
}
2015-06-04 22:32:14 -07:00
2015-06-26 20:27:38 -07:00
Logger.log('session.connect succeeded');
2015-06-15 21:52:01 -07:00
MediaController.setActivePlayer(PlayerName, convertDeviceToTarget(device));
currentDevice = device;
currentDeviceId = device.getId();
2015-05-19 12:15:40 -07:00
}
2015-06-15 21:52:01 -07:00
function onDeviceReady(device) {
2015-05-19 12:15:40 -07:00
2015-06-15 21:52:01 -07:00
device.off("ready");
2015-06-26 20:27:38 -07:00
Logger.log('creating webAppSession');
2015-05-19 12:15:40 -07:00
2015-06-15 21:52:01 -07:00
launchWebApp(device);
2015-05-19 12:15:40 -07:00
}
self.tryPair = function (target) {
var deferred = $.Deferred();
2015-06-12 20:42:19 -07:00
var device = ConnectSDKHelper.getDeviceList().filter(function (d) {
2015-05-19 12:15:40 -07:00
return d.getId() == target.id;
})[0];
if (device) {
2015-06-04 22:32:14 -07:00
self.tryPairWithDevice(device, deferred);
2015-05-19 12:15:40 -07:00
2015-06-04 22:32:14 -07:00
} else {
deferred.reject();
}
2015-05-19 12:15:40 -07:00
2015-06-04 22:32:14 -07:00
return deferred.promise();
};
2015-05-19 12:15:40 -07:00
2015-06-04 22:32:14 -07:00
self.tryPairWithDevice = function (device, deferred) {
2015-05-19 12:15:40 -07:00
2015-06-26 20:27:38 -07:00
Logger.log('Will attempt to connect to Connect Device');
2015-05-19 12:15:40 -07:00
2015-06-15 21:52:01 -07:00
device.on("disconnect", function () {
device.off("ready");
device.off("disconnect");
});
2015-05-19 12:15:40 -07:00
2015-06-04 22:32:14 -07:00
if (device.isReady()) {
2015-06-26 20:27:38 -07:00
Logger.log('Device is already ready, calling onDeviceReady');
2015-06-15 21:52:01 -07:00
onDeviceReady(device);
2015-05-19 12:15:40 -07:00
} else {
2015-06-26 20:27:38 -07:00
Logger.log('Binding device ready handler');
2015-06-04 22:32:14 -07:00
device.on("ready", function () {
2015-06-26 20:27:38 -07:00
Logger.log('device.ready fired');
2015-06-15 21:52:01 -07:00
onDeviceReady(device);
2015-06-04 22:32:14 -07:00
});
2015-06-26 20:27:38 -07:00
Logger.log('Calling device.connect');
2015-06-04 22:32:14 -07:00
device.connect();
}
2015-05-19 12:15:40 -07:00
};
$(MediaController).on('playerchange', function (e, newPlayer, newTarget) {
2015-06-15 21:52:01 -07:00
if (currentDevice) {
if (newTarget.id != currentDeviceId) {
2015-06-04 22:32:14 -07:00
if (currentDevice) {
2015-06-26 20:27:38 -07:00
Logger.log('Disconnecting from connect device');
2015-06-15 21:52:01 -07:00
//currentDevice.disconnect();
2015-06-18 21:23:55 -07:00
cleanupSession();
2015-06-15 21:52:01 -07:00
currentDevice = null;
currentDeviceId = null;
2015-06-17 18:41:22 -07:00
currentMediaControl = null;
2015-06-04 22:32:14 -07:00
}
}
2015-05-19 12:15:40 -07:00
}
});
2015-06-15 21:52:01 -07:00
function onResume() {
var deviceId = currentDeviceId;
if (deviceId) {
self.tryPair({
id: deviceId
});
}
}
2015-05-19 12:15:40 -07:00
2015-06-15 21:52:01 -07:00
document.addEventListener("resume", onResume, false);
2015-05-19 12:15:40 -07:00
}
2015-06-15 21:52:01 -07:00
MediaController.registerPlayer(new connectSDKPlayer());
2015-05-19 12:15:40 -07:00
})();