Skip to content

Commit

Permalink
Refactor maincontroller to use PlaybackState
Browse files Browse the repository at this point in the history
  • Loading branch information
YouKnowBlom committed Aug 1, 2021
1 parent c541135 commit a7f31a2
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 59 deletions.
145 changes: 91 additions & 54 deletions src/components/maincontroller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
getCurrentPositionTicks,
getReportingParams,
resetPlaybackScope,
getMetadata,
createStreamInfo,
getStreamByIndex,
Expand All @@ -21,7 +20,7 @@ import {
} from './jellyfinActions';
import { getDeviceProfile } from './deviceprofileBuilder';
import { JellyfinApi } from './jellyfinApi';
import { playbackManager } from './playbackManager';
import { playbackManager, PlaybackState } from './playbackManager';
import { CommandHandler } from './commandHandler';
import { getMaxBitrateSupport } from './codecSupportHelper';
import { DocumentManager } from './documentManager';
Expand All @@ -36,7 +35,7 @@ const playbackMgr = new playbackManager(window.playerManager);

CommandHandler.configure(window.playerManager, playbackMgr);

resetPlaybackScope($scope);
playbackMgr.resetPlaybackScope();

let broadcastToServer = new Date();

Expand All @@ -46,29 +45,37 @@ let hasReportedCapabilities = false;
*
*/
export function onMediaElementTimeUpdate(): void {
if ($scope.isChangingStream) {
if (playbackMgr.playbackState.isChangingStream) {
return;
}

const now = new Date();

const elapsed = now.valueOf() - broadcastToServer.valueOf();
const playbackState = playbackMgr.playbackState;

if (elapsed > 5000) {
// TODO use status as input
reportPlaybackProgress($scope, getReportingParams($scope));
reportPlaybackProgress(
playbackState,
getReportingParams(playbackState)
);
broadcastToServer = now;
} else if (elapsed > 1500) {
// TODO use status as input
reportPlaybackProgress($scope, getReportingParams($scope), false);
reportPlaybackProgress(
playbackState,
getReportingParams(playbackState),
false
);
}
}

/**
*
*/
export function onMediaElementPause(): void {
if ($scope.isChangingStream) {
if (playbackMgr.playbackState.isChangingStream) {
return;
}

Expand All @@ -79,7 +86,7 @@ export function onMediaElementPause(): void {
*
*/
export function onMediaElementPlaying(): void {
if ($scope.isChangingStream) {
if (playbackMgr.playbackState.isChangingStream) {
return;
}

Expand Down Expand Up @@ -146,22 +153,34 @@ enableTimeUpdateListener();

window.addEventListener('beforeunload', () => {
// Try to cleanup after ourselves before the page closes
const playbackState = playbackMgr.playbackState;

disableTimeUpdateListener();
reportPlaybackStopped($scope, getReportingParams($scope));
reportPlaybackStopped(playbackState, getReportingParams(playbackState));
});

window.playerManager.addEventListener(
cast.framework.events.EventType.PLAY,
(): void => {
play($scope);
reportPlaybackProgress($scope, getReportingParams($scope));
const playbackState = playbackMgr.playbackState;

play(playbackState);
reportPlaybackProgress(
playbackState,
getReportingParams(playbackState)
);
}
);

window.playerManager.addEventListener(
cast.framework.events.EventType.PAUSE,
(): void => {
reportPlaybackProgress($scope, getReportingParams($scope));
const playbackState = playbackMgr.playbackState;

reportPlaybackProgress(
playbackState,
getReportingParams(playbackState)
);
}
);

Expand All @@ -184,13 +203,15 @@ window.playerManager.addEventListener(
window.playerManager.addEventListener(
cast.framework.events.EventType.ENDED,
(): void => {
const playbackState = playbackMgr.playbackState;

// If we're changing streams, do not report playback ended.
if ($scope.isChangingStream) {
if (playbackState.isChangingStream) {
return;
}

reportPlaybackStopped($scope, getReportingParams($scope));
resetPlaybackScope($scope);
reportPlaybackStopped(playbackState, getReportingParams(playbackState));
playbackMgr.resetPlaybackScope();

if (!playbackMgr.playNextItem()) {
window.playlist = [];
Expand Down Expand Up @@ -286,15 +307,22 @@ export function processMessage(data: any): void {
CommandHandler.processMessage(data, data.command);

if (window.reportEventType) {
const report = (): Promise<void> =>
reportPlaybackProgress($scope, getReportingParams($scope));
const playbackState = playbackMgr.playbackState;

const report = (): void => {
reportPlaybackProgress(
playbackState,
getReportingParams(playbackState)
);
};

reportPlaybackProgress(
$scope,
getReportingParams($scope),
playbackState,
getReportingParams(playbackState),
true,
window.reportEventType
);

setTimeout(report, 100);
setTimeout(report, 500);
}
Expand All @@ -308,33 +336,35 @@ export function reportEvent(
name: string,
reportToServer: boolean
): Promise<void> {
const playbackState = playbackMgr.playbackState;

return reportPlaybackProgress(
$scope,
getReportingParams($scope),
playbackState,
getReportingParams(playbackState),
reportToServer,
name
);
}

/**
* @param $scope
* @param state - playback state.
* @param index
*/
export function setSubtitleStreamIndex(
$scope: GlobalScope,
state: PlaybackState,
index: number
): void {
console.log(`setSubtitleStreamIndex. index: ${index}`);

let positionTicks;

const currentSubtitleStream = $scope.mediaSource.MediaStreams.filter(
// FIXME: Possible index error when MediaStreams is undefined.
const currentSubtitleStream = state.mediaSource?.MediaStreams?.filter(
(m: any) => {
return (
m.Index == $scope.subtitleStreamIndex && m.Type == 'Subtitle'
);
return m.Index == state.subtitleStreamIndex && m.Type == 'Subtitle';
}
)[0];

const currentDeliveryMethod = currentSubtitleStream
? currentSubtitleStream.DeliveryMethod
: null;
Expand All @@ -343,21 +373,25 @@ export function setSubtitleStreamIndex(
// Need to change the stream to turn off the subs
if (currentDeliveryMethod == 'Encode') {
console.log('setSubtitleStreamIndex video url change required');
positionTicks = getCurrentPositionTicks($scope);
changeStream(positionTicks, {
positionTicks = getCurrentPositionTicks(state);
changeStream(state, positionTicks, {
SubtitleStreamIndex: -1
});
} else {
$scope.subtitleStreamIndex = -1;
state.subtitleStreamIndex = -1;
setTextTrack(null);
}

return;
}

const mediaStreams = $scope.PlaybackMediaSource.MediaStreams;
const mediaStreams = state.PlaybackMediaSource?.MediaStreams;

const subtitleStream = getStreamByIndex(mediaStreams, 'Subtitle', index);
const subtitleStream = getStreamByIndex(
<any>mediaStreams,
'Subtitle',
index
);

if (!subtitleStream) {
console.log(
Expand All @@ -381,45 +415,48 @@ export function setSubtitleStreamIndex(

console.log(`Subtitle url: ${textStreamUrl}`);
setTextTrack(index);
$scope.subtitleStreamIndex = subtitleStream.Index;
state.subtitleStreamIndex = subtitleStream.Index;

return;
} else {
console.log('setSubtitleStreamIndex video url change required');
positionTicks = getCurrentPositionTicks($scope);
changeStream(positionTicks, {
positionTicks = getCurrentPositionTicks(state);
changeStream(state, positionTicks, {
SubtitleStreamIndex: index
});
}
}

/**
* @param $scope
* @param state - playback state.
* @param index
*/
export function setAudioStreamIndex(
$scope: GlobalScope,
state: PlaybackState,
index: number
): Promise<void> {
const positionTicks = getCurrentPositionTicks($scope);
const positionTicks = getCurrentPositionTicks(state);

return changeStream(positionTicks, {
return changeStream(state, positionTicks, {
AudioStreamIndex: index
});
}

/**
* @param state - playback state.
* @param ticks
*/
export function seek(ticks: number): Promise<void> {
return changeStream(ticks);
export function seek(state: PlaybackState, ticks: number): Promise<void> {
return changeStream(state, ticks);
}

/**
* @param state - playback state.
* @param ticks
* @param params
*/
export async function changeStream(
state: PlaybackState,
ticks: number,
params: any = undefined
): Promise<void> {
Expand All @@ -428,17 +465,17 @@ export async function changeStream(
params == null
) {
window.playerManager.seek(ticks / 10000000);
reportPlaybackProgress($scope, getReportingParams($scope));
reportPlaybackProgress(state, getReportingParams(state));

return Promise.resolve();
}

params = params || {};

const playSessionId = $scope.playSessionId;
const liveStreamId = $scope.liveStreamId;
const playSessionId = state.playSessionId;
const liveStreamId = state.liveStreamId;

const item = $scope.item;
const item = state.item;
const maxBitrate = await getMaxBitrate();

const deviceProfile = getDeviceProfile({
Expand All @@ -447,19 +484,19 @@ export async function changeStream(
});
const audioStreamIndex =
params.AudioStreamIndex == null
? $scope.audioStreamIndex
? state.audioStreamIndex
: params.AudioStreamIndex;
const subtitleStreamIndex =
params.SubtitleStreamIndex == null
? $scope.subtitleStreamIndex
? state.subtitleStreamIndex
: params.SubtitleStreamIndex;

const playbackInformation = await getPlaybackInfo(
item,
<BaseItemDto>item,
maxBitrate,
deviceProfile,
ticks,
$scope.mediaSourceId,
state.mediaSourceId,
audioStreamIndex,
subtitleStreamIndex,
liveStreamId
Expand All @@ -470,7 +507,7 @@ export async function changeStream(
}

const mediaSource = playbackInformation.MediaSources[0];
const streamInfo = createStreamInfo(item, mediaSource, ticks);
const streamInfo = createStreamInfo(<BaseItemDto>item, mediaSource, ticks);

if (!streamInfo.url) {
showPlaybackInfoErrorMessage('NoCompatibleStream');
Expand All @@ -480,7 +517,7 @@ export async function changeStream(

const mediaInformation = createMediaInformation(
playSessionId,
item,
<BaseItemDto>item,
streamInfo
);
const loadRequest = new cast.framework.messages.LoadRequestData();
Expand All @@ -493,13 +530,13 @@ export async function changeStream(

if (requiresStoppingTranscoding) {
window.playerManager.pause();
await stopActiveEncodings(playSessionId);
await stopActiveEncodings(state);
}

window.playerManager.load(loadRequest);
window.playerManager.play();
$scope.subtitleStreamIndex = subtitleStreamIndex;
$scope.audioStreamIndex = audioStreamIndex;
state.subtitleStreamIndex = subtitleStreamIndex;
state.audioStreamIndex = audioStreamIndex;
}

// Create a message handler for the custome namespace channel
Expand Down
Loading

0 comments on commit a7f31a2

Please sign in to comment.