Skip to content

Commit

Permalink
Added Episode Play function to Web version
Browse files Browse the repository at this point in the history
  • Loading branch information
Namo2 committed Jun 9, 2024
1 parent 711d23a commit 4c8e7f6
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System.ComponentModel.DataAnnotations;
using System.Net.Mime;
using System.Reflection;
using MediaBrowser.Controller.Dto;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Persistence;
using MediaBrowser.Controller.Session;
using MediaBrowser.Model.Session;
using Namo.Plugin.InPlayerEpisodePreview.Configuration;

namespace Namo.Plugin.InPlayerEpisodePreview.Api;
Expand All @@ -35,6 +34,7 @@ public class InPlayerPreviewController : ControllerBase
private readonly ILibraryMonitor _libraryMonitor;
private readonly IMediaEncoder _mediaEncoder;
private readonly IServerConfigurationManager _configurationManager;
private readonly ISessionManager _sessionManager;
private readonly EncodingHelper _encodingHelper;

private readonly PluginConfiguration _config;
Expand All @@ -52,6 +52,7 @@ public InPlayerPreviewController(
ILibraryMonitor libraryMonitor,
IMediaEncoder mediaEncoder,
IServerConfigurationManager configurationManager,
ISessionManager sessionManager,
EncodingHelper encodingHelper)
{
_assembly = Assembly.GetExecutingAssembly();
Expand All @@ -66,6 +67,7 @@ public InPlayerPreviewController(
_libraryMonitor = libraryMonitor;
_mediaEncoder = mediaEncoder;
_configurationManager = configurationManager;
_sessionManager = sessionManager;
_encodingHelper = encodingHelper;

_config = InPlayerEpisodePreviewPlugin.Instance!.Configuration;
Expand All @@ -90,4 +92,34 @@ public ActionResult GetClientScript()

return File(scriptStream, "application/javascript");
}

[HttpGet("Users/{userId}/Items/{id}/Play/{ticks}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public ActionResult StartMedia([FromRoute] Guid userId, [FromRoute] Guid id, [FromRoute] long ticks = 0)
{
SessionInfo? session = _sessionManager.Sessions.FirstOrDefault(session => session.UserId.Equals(userId));
if (session is null)
{
_logger.LogInformation("Couldn't find a valid session for this user");
return NotFound("Couldn't find a valid session for this user");
}

BaseItem? item = _libraryManager.GetItemById(id);
if (item is null)
{
_logger.LogInformation("Couldn't find item to play");
return NotFound("Couldn't find item to play");
}

_sessionManager.SendPlayCommand(session.Id, session.Id,
new PlayRequest
{
ItemIds = [item.Id],
StartPositionTicks = ticks,
PlayCommand = PlayCommand.PlayNow,
}, CancellationToken.None);

return Ok();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export class EpisodeElementTemplate extends BaseTemplate {
</div>
</div>
</button>
<div class="cardOverlayContainer itemAction ${!this.isJMPClient ? "hide" : ""}"
<div class="cardOverlayContainer itemAction"
data-action="link">
<button id="start-episode-${this.episode.IndexNumber}"
is="paper-icon-button-light"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export abstract class PlaybackHandler {
abstract play(episodeId: string, startPositionTicks: number, serverId: string): Function;
abstract play(episodeId: string, startPositionTicks: number, serverId: string): Function | Promise<void | Response>;

protected getPlaybackData(episodeId: string, startPositionTicks: number, serverId: string) {
return {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import {PlaybackHandler} from "./PlaybackHandler";
import {ProgramDataStore} from "../ProgramDataStore";
import {DataLoader} from "../DataLoader/DataLoader";

export class WebPlaybackHandler extends PlaybackHandler {
constructor() {
constructor(private dataLoader: DataLoader, private programDataStore: ProgramDataStore) {
super();
}

play(episodeId: string, startPositionTicks: number, serverId: string): Function {
// @ts-ignore
// return this.playbackManager.play(this.getPlaybackData(episodeId, serverId));

return () => {};
play(episodeId: string, startPositionTicks: number, serverId: string): Promise<void | Response> {
return fetch(`${this.dataLoader.getBaseUrl()}/InPlayerPreview/Users/${this.programDataStore.userId}/Items/${episodeId}/Play/${startPositionTicks}`).catch(err => console.log('[IPEP]' + err));
}
}
4 changes: 2 additions & 2 deletions Namo.Plugin.InPlayerEpisodePreview/Web/inPlayerPreview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ inPlayerPreviewStyle.textContent += '.previewEpisodeContainer {width: 100%;}';
inPlayerPreviewStyle.textContent += '.previewEpisodeTitle {pointer-events: none;}';
inPlayerPreviewStyle.textContent += '.previewEpisodeImageCard {width: 12vw; height: 15vh; left: 1em;}';
inPlayerPreviewStyle.textContent += '.previewEpisodeDescription {position: absolute; right: 1em; left: 13.5vw; display: block; overflow: auto;}';
document.body.appendChild(inPlayerPreviewStyle);
document?.head?.appendChild(inPlayerPreviewStyle);
// const cssInjector: CssInjector = new CssInjector();
// cssInjector.injectCss('/Web/inPlayerPreviewStyle.css', document.body);

Expand All @@ -54,7 +54,7 @@ const dataLoader: DataLoader = isJMPClient ? new JMPDataLoader(authService, prog
isJMPClient ? new JMPDataFetcher(programDataStore, dataLoader, events, playbackManager) : new WebDataFetcher(programDataStore, dataLoader, authService, logger)

// @ts-ignore
let playbackHandler: PlaybackHandler = isJMPClient ? new JMPPlaybackHandler(playbackManager) : new WebPlaybackHandler();
let playbackHandler: PlaybackHandler = isJMPClient ? new JMPPlaybackHandler(playbackManager) : new WebPlaybackHandler(dataLoader, programDataStore);

const videoPaths = ['playback/video/index.html', '/video'];
let previousRoutePath = null;
Expand Down

0 comments on commit 4c8e7f6

Please sign in to comment.