Skip to content

Add still watching feature #6737

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Actions that are triggered for still watching.
*/
export enum StillWatchingAction {
Short = 'Short',
Default = 'Default',
Long = 'Long',
VeryLong = 'VeryLong',
Disabled = 'Disabled'
}
102 changes: 102 additions & 0 deletions src/components/playback/idlemanager.js
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, new files should be in TypeScript (.ts) if possible

Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { MILLISECONDS_PER_MINUTE, TICKS_PER_MILLISECOND, TICKS_PER_MINUTE } from '../../constants/time';
import { StillWatchingAction } from 'apps/stable/features/playback/constants/stillWatchingAction';
import { stillWatchingBehavior } from '../../scripts/settings/userSettings';

function itemIsEpisode (item) {
return item.Type === 'Episode';
}

function getStillWatchingConfig (setting) {
switch (setting) {
case StillWatchingAction.Default: return { episodeCount: 3, minMinutes: 90 };
case StillWatchingAction.Short: return { episodeCount: 2, minMinutes: 60 };
case StillWatchingAction.Long: return { episodeCount: 5, minMinutes: 150 };
case StillWatchingAction.VeryLong: return { episodeCount: 8, minMinutes: 240 };
default: return null;
}
}

class IdleManager {
constructor(playbackManager) {
this.playbackManager = playbackManager;
this.episodeCount = 0;
this.watchTicks = 0;
this.lastUpdateTicks = 0;
this.isWatchingEpisodes = false;
this.episodeWasInterrupted = false;
this.showStillWatching = false;
this.stillWatchingShowing = false;
this.stillWatchingSetting = null;
}

notifyStartSession (item, items) {
// No need to track when only watching 1 episode
if (itemIsEpisode(item) && items.length > 1) {
this.resetSession();
this.episodeWasInterrupted = false;
this.stillWatchingSetting = getStillWatchingConfig(stillWatchingBehavior());
this.stillWatchingShowing = false;
}
}

notifyStart (item) {
if (itemIsEpisode(item)) {
this.isWatchingEpisodes = true;
}
}

onEpisodeWatched () {
if (!this.episodeWasInterrupted) {
this.episodeCount++;
}
this.calculateWatchTime();
this.episodeWasInterrupted = false;
}

notifyInteraction (item) {
if (itemIsEpisode(item)) {
this.resetSession();
this.lastUpdateTicks = this.playbackManager.currentTime() * TICKS_PER_MILLISECOND;
this.episodeWasInterrupted = true;
}
}

notifyPlay (item) {
if (itemIsEpisode(item)) {
this.calculateWatchTime();
this.stillWatchingShowing = false;
}
}

async #checkStillWatchingStatus () {
// User has disabled the Still Watching feature
if (!this.stillWatchingSetting) {
return;
}

const minMinutesInMs = this.stillWatchingSetting.minMinutes * MILLISECONDS_PER_MINUTE;

const episodeRequirementMet = this.episodeCount >= this.stillWatchingSetting.episodeCount - 1;
const watchTimeRequirementMet = (this.watchTicks / TICKS_PER_MINUTE) >= minMinutesInMs;

if (episodeRequirementMet || watchTimeRequirementMet) {
this.showStillWatching = true;
}
}

calculateWatchTime () {
const currentTimeTicks = this.playbackManager.currentTime() * TICKS_PER_MILLISECOND;
this.watchTicks += currentTimeTicks - this.lastUpdateTicks;
this.lastUpdateTicks = currentTimeTicks;
this.#checkStillWatchingStatus();
}

resetSession () {
this.watchTicks = 0;
this.episodeCount = 0;
this.lastUpdateTicks = 0;
this.showStillWatching = false;
}
}

export default IdleManager;
11 changes: 9 additions & 2 deletions src/components/playback/playbackmanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import appSettings from '../../scripts/settings/appSettings';
import itemHelper from '../itemHelper';
import { pluginManager } from '../pluginManager';
import PlayQueueManager from './playqueuemanager';
import IdleManager from './idlemanager';
import * as userSettings from '../../scripts/settings/userSettings';
import globalize from '../../lib/globalize';
import loading from '../loading/loading';
Expand Down Expand Up @@ -715,6 +716,7 @@ export class PlaybackManager {
const playerStates = {};

this._playQueueManager = new PlayQueueManager();
this._idleManager = new IdleManager(this);

self.currentItem = function (player) {
if (!player) {
Expand Down Expand Up @@ -3267,6 +3269,8 @@ export class PlaybackManager {
Events.trigger(player, 'playbackstart', [state]);
Events.trigger(self, 'playbackstart', [player, state]);

if (self.getCurrentPlaylistIndex(self._currentPlayer) === 0) self._idleManager.notifyStartSession(streamInfo.item, self._playQueueManager.getPlaylist());

// only used internally as a safeguard to avoid reporting other events to the server before playback start
streamInfo.started = true;

Expand Down Expand Up @@ -3464,14 +3468,17 @@ export class PlaybackManager {

if (errorOccurred) {
showPlaybackInfoErrorMessage(self, 'PlaybackError' + displayErrorCode);
} else if (nextItem) {
} else if (nextItem && !self._idleManager.stillWatchingShowing) {
const apiClient = ServerConnections.getApiClient(nextItem.item.ServerId);

apiClient.getCurrentUser().then(function (user) {
apiClient.getUser(apiClient.getCurrentUserId()).then(function (user) {
if (user.Configuration.EnableNextEpisodeAutoPlay || nextMediaType !== MediaType.Video) {
self._idleManager.onEpisodeWatched();
self.nextTrack();
}
});
} else if (self._idleManager.stillWatchingShowing) {
self._idleManager.stillWatchingShowing = false;
}
}

Expand Down
43 changes: 42 additions & 1 deletion src/components/playbackSettings/playbackSettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import escapeHTML from 'escape-html';
import { MediaSegmentAction } from 'apps/stable/features/playback/constants/mediaSegmentAction';
import { getId, getMediaSegmentAction } from 'apps/stable/features/playback/utils/mediaSegmentSettings';

import { StillWatchingAction } from 'apps/stable/features/playback/constants/stillWatchingAction';

import appSettings from '../../scripts/settings/appSettings';
import { appHost } from '../apphost';
import browser from '../../scripts/browser';
Expand Down Expand Up @@ -82,6 +84,19 @@ function populateMediaSegments(container, userSettings) {
});
}

function populateStillWatching (container) {
const actionOptions = Object.values(StillWatchingAction)
.map(action => {
const actionLabel = globalize.translate(`StillWatchingAction.${action}`);
return `<option value='${action}'>${actionLabel}</option>`;
})
.join('');

container.innerHTML = `<div class="selectContainer">
<select is="emby-select" class="selectStillWatchingBehavior" label="${globalize.translate('StillWatchingLabel')}">${actionOptions}</select>
</div>`;
}

function fillQuality(select, isInNetwork, mediatype, maxVideoWidth) {
const options = mediatype === 'Audio' ? qualityoptions.getAudioQualityOptions({

Expand Down Expand Up @@ -196,7 +211,32 @@ function loadForm(context, user, userSettings, systemInfo, apiClient) {
populateLanguages(context.querySelector('#selectAudioLanguage'), allCultures);

context.querySelector('#selectAudioLanguage', context).value = user.Configuration.AudioLanguagePreference || '';
context.querySelector('.chkEpisodeAutoPlay').checked = user.Configuration.EnableNextEpisodeAutoPlay || false;
const enableNextEpisodeAutoPlay = context.querySelector('.chkEpisodeAutoPlay');
enableNextEpisodeAutoPlay.checked = user.Configuration.EnableNextEpisodeAutoPlay || false;

const stillWatchingContainer = context.querySelector('.stillWatchingSelectContainer');
populateStillWatching(stillWatchingContainer);
const stillWatchingSelect = context.querySelector('.selectStillWatchingBehavior');
stillWatchingSelect.value = userSettings.stillWatchingBehavior();

if (user.Configuration.EnableNextEpisodeAutoPlay) {
requestAnimationFrame(() => {
stillWatchingContainer.style.maxHeight = stillWatchingContainer.scrollHeight + 'px';
stillWatchingContainer.style.opacity = 1;
});
}

enableNextEpisodeAutoPlay.addEventListener('change', function () {
if (stillWatchingContainer.style.maxHeight) {
stillWatchingContainer.style.maxHeight = null;
stillWatchingContainer.style.opacity = 0;
stillWatchingSelect.value = StillWatchingAction.Disabled;
} else {
stillWatchingSelect.value = userSettings.stillWatchingBehavior();
stillWatchingContainer.style.maxHeight = stillWatchingContainer.scrollHeight + 'px';
stillWatchingContainer.style.opacity = 1;
}
});
});

if (appHost.supports('externalplayerintent') && userId === loggedInUserId) {
Expand Down Expand Up @@ -292,6 +332,7 @@ function saveUser(context, user, userSettingsInstance, apiClient) {
user.Configuration.AudioLanguagePreference = context.querySelector('#selectAudioLanguage').value;
user.Configuration.PlayDefaultAudioTrack = context.querySelector('.chkPlayDefaultAudioTrack').checked;
user.Configuration.EnableNextEpisodeAutoPlay = context.querySelector('.chkEpisodeAutoPlay').checked;
userSettingsInstance.stillWatchingBehavior(context.querySelector('.selectStillWatchingBehavior').value);
userSettingsInstance.preferFmp4HlsContainer(context.querySelector('.chkPreferFmp4HlsContainer').checked);
userSettingsInstance.enableCinemaMode(context.querySelector('.chkEnableCinemaMode').checked);
userSettingsInstance.selectAudioNormalization(context.querySelector('#selectAudioNormalization').value);
Expand Down
11 changes: 11 additions & 0 deletions src/components/playbackSettings/playbackSettings.template.html
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ <h2 class="sectionTitle">
</label>
</div>

<div class="stillWatchingSelectContainer"></div>

<div class="checkboxContainer checkboxContainer-withDescription">
<label>
<input type="checkbox" is="emby-checkbox" class="chkRememberAudioSelections" />
Expand Down Expand Up @@ -249,3 +251,12 @@ <h2 class="sectionTitle">
<span>${Save}</span>
</button>
</form>

<style>
.stillWatchingSelectContainer {
transition: max-height 1s ease, opacity 0.9s ease;
overflow: hidden;
max-height: 0;
opacity: 0;
}
</style>
Loading
Loading