Skip to content

Commit

Permalink
Merge pull request #59 from YouKnowBlom/refactor-play
Browse files Browse the repository at this point in the history
Refactor play functions
  • Loading branch information
YouKnowBlom authored Oct 3, 2020
2 parents 928226e + 5919dcf commit 780705b
Showing 1 changed file with 47 additions and 55 deletions.
102 changes: 47 additions & 55 deletions src/components/playbackManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
getMaxBitrate,
getDeviceProfile,
getOptimalMediaSource,
validatePlaybackInfoResult,
showPlaybackInfoErrorMessage,
supportsDirectPlay,
createMediaInformation
Expand All @@ -36,30 +35,26 @@ export class playbackManager {
return this.playerManager.getPlayerState() === cast.framework.messages.PlayerState.PLAYING;
}

playFromOptions(options) {
var firstItem = options.items[0];
async playFromOptions(options) {
const firstItem = options.items[0];

if (options.startPositionTicks || firstItem.MediaType !== 'Video') {
this.playFromOptionsInternal(options);
return;
}

getIntros(firstItem.serverAddress, firstItem.accessToken, firstItem.userId, firstItem).then(intros => {

tagItems(intros.Items, {
userId: firstItem.userId,
accessToken: firstItem.accessToken,
serverAddress: firstItem.serverAddress
});

options.items = intros.Items.concat(options.items);
this.playFromOptionsInternal(options);
let intros = await getIntros(firstItem.serverAddress, firstItem.accessToken, firstItem.userId, firstItem);
tagItems(intros.Items, {
userId: firstItem.userId,
accessToken: firstItem.accessToken,
serverAddress: firstItem.serverAddress
});
options.items = intros.Items.concat(options.items);
this.playFromOptionsInternal(options);
}

playFromOptionsInternal(options) {

var stopPlayer = this.activePlaylist && this.activePlaylist.length > 0;
const stopPlayer = this.activePlaylist && this.activePlaylist.length > 0;

this.activePlaylist = options.items;
this.activePlaylist.currentPlaylistIndex = -1;
Expand All @@ -68,7 +63,6 @@ export class playbackManager {
this.playNextItem(options, stopPlayer);
}

// Plays the next item in the list
playNextItem(options, stopPlayer) {

var nextItemInfo = getNextPlaybackItemInfo();
Expand Down Expand Up @@ -97,55 +91,53 @@ export class playbackManager {
return false;
}

playItem(item, options, stopPlayer) {

var callback = function () {
onStopPlayerBeforePlaybackDone(item, options);
};

async playItem(item, options, stopPlayer) {
if (stopPlayer) {

this.stop("none").then(callback);
} else {
callback();
await this.stop("none");
}
}

playItemInternal(item, options) {
onStopPlayerBeforePlaybackDone(item, options);
}

async playItemInternal(item, options) {
$scope.isChangingStream = false;
setAppStatus('loading');

getMaxBitrate(item.MediaType).then(maxBitrate => {

var deviceProfile = getDeviceProfile(maxBitrate);

jellyfinActions.getPlaybackInfo(item, maxBitrate, deviceProfile, options.startPositionTicks, options.mediaSourceId, options.audioStreamIndex, options.subtitleStreamIndex).then(result => {

if (validatePlaybackInfoResult(result)) {

var mediaSource = getOptimalMediaSource(result.MediaSources);

if (mediaSource) {

if (mediaSource.RequiresOpening) {

jellyfinActions.getLiveStream(item, result.PlaySessionId, maxBitrate, deviceProfile, options.startPositionTicks, mediaSource, null, null).then(openLiveStreamResult => {
const maxBitrate = await getMaxBitrate(item.MediaType);
const deviceProfile = await getDeviceProfile(maxBitrate);
const playbackInfo = await jellyfinActions.getPlaybackInfo(
item,
maxBitrate,
deviceProfile,
options.startPositionTicks,
options.mediaSourceId,
options.audioStreamIndex,
options.subtitleStreamIndex)
.catch(broadcastConnectionErrorMessage);

if (playbackInfo.ErrorCode) {
return showPlaybackInfoErrorMessage(playbackInfo.ErrorCode);
}

openLiveStreamResult.MediaSource.enableDirectPlay = supportsDirectPlay(openLiveStreamResult.MediaSource);
this.playMediaSource(result.PlaySessionId, item, openLiveStreamResult.MediaSource, options);
});
const mediaSource = await getOptimalMediaSource(playbackInfo.MediaSources);
if (!mediaSource) {
return showPlaybackInfoErrorMessage('NoCompatibleStream');
}

} else {
this.playMediaSource(result.PlaySessionId, item, mediaSource, options);
}
} else {
showPlaybackInfoErrorMessage('NoCompatibleStream');
}
}
let itemToPlay = mediaSource;
if (mediaSource.RequiresOpening) {
const openLiveStreamResult = await jellyfinActions.getLiveStream(item,
playbackInfo.PlaySessionId,
maxBitrate,
deviceProfile,
options.startPositionTicks,
mediaSource,
null, null);
openLiveStreamResult.MediaSource.enableDirectPlay = supportsDirectPlay(openLiveStreamResult.MediaSource);
itemToPlay = openLiveStreamResult.MediaSource;
}

}, broadcastConnectionErrorMessage);
});
this.playMediaSource(playbackInfo.PlaySessionId, item, itemToPlay, options);
}

playMediaSource(playSessionId, item, mediaSource, options) {
Expand Down

0 comments on commit 780705b

Please sign in to comment.