First POC with working playback
This commit is contained in:
@@ -0,0 +1,253 @@
|
||||
using System;
|
||||
using System.Text.Json;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Jellyfin.Plugin.JellyLMS.Models;
|
||||
using MediaBrowser.Controller.Session;
|
||||
using MediaBrowser.Model.Session;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Jellyfin.Plugin.JellyLMS.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Session controller for LMS player devices.
|
||||
/// Enables Jellyfin to send playback commands to LMS players via the cast interface.
|
||||
/// </summary>
|
||||
public class LmsSessionController : ISessionController
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
private readonly ILmsApiClient _lmsClient;
|
||||
private readonly LmsPlayer _player;
|
||||
private readonly SessionInfo _session;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="LmsSessionController"/> class.
|
||||
/// </summary>
|
||||
/// <param name="logger">The logger instance.</param>
|
||||
/// <param name="lmsClient">The LMS API client.</param>
|
||||
/// <param name="player">The LMS player this controller manages.</param>
|
||||
/// <param name="session">The Jellyfin session associated with this controller.</param>
|
||||
public LmsSessionController(
|
||||
ILogger logger,
|
||||
ILmsApiClient lmsClient,
|
||||
LmsPlayer player,
|
||||
SessionInfo session)
|
||||
{
|
||||
_logger = logger;
|
||||
_lmsClient = lmsClient;
|
||||
_player = player;
|
||||
_session = session;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool IsSessionActive => _player.IsConnected && _player.IsPoweredOn;
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool SupportsMediaControl => true;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the MAC address of the LMS player.
|
||||
/// </summary>
|
||||
public string PlayerMac => _player.MacAddress;
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task SendMessage<T>(
|
||||
SessionMessageType name,
|
||||
Guid messageId,
|
||||
T data,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.LogDebug(
|
||||
"LMS Session Controller received message {MessageType} for player {PlayerName}",
|
||||
name,
|
||||
_player.Name);
|
||||
|
||||
try
|
||||
{
|
||||
switch (name)
|
||||
{
|
||||
case SessionMessageType.Play:
|
||||
await HandlePlayCommandAsync(data, cancellationToken).ConfigureAwait(false);
|
||||
break;
|
||||
|
||||
case SessionMessageType.Playstate:
|
||||
await HandlePlaystateCommandAsync(data, cancellationToken).ConfigureAwait(false);
|
||||
break;
|
||||
|
||||
case SessionMessageType.GeneralCommand:
|
||||
await HandleGeneralCommandAsync(data, cancellationToken).ConfigureAwait(false);
|
||||
break;
|
||||
|
||||
default:
|
||||
_logger.LogDebug("Unhandled message type: {MessageType}", name);
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error handling message {MessageType} for player {PlayerName}", name, _player.Name);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task HandlePlayCommandAsync<T>(T data, CancellationToken cancellationToken)
|
||||
{
|
||||
if (data is not PlayRequest playRequest)
|
||||
{
|
||||
_logger.LogWarning("Expected PlayRequest but got {Type}", data?.GetType().Name);
|
||||
return;
|
||||
}
|
||||
|
||||
_logger.LogInformation(
|
||||
"Play command received for player {PlayerName}: {ItemCount} items",
|
||||
_player.Name,
|
||||
playRequest.ItemIds.Length);
|
||||
|
||||
// Power on the player if needed
|
||||
if (!_player.IsPoweredOn)
|
||||
{
|
||||
await _lmsClient.PowerOnAsync(_player.MacAddress).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
// For now, play the first item
|
||||
// TODO: Support playlists/queues
|
||||
if (playRequest.ItemIds.Length > 0)
|
||||
{
|
||||
var itemId = playRequest.ItemIds[0];
|
||||
var streamUrl = BuildStreamUrl(itemId);
|
||||
|
||||
_logger.LogInformation("Playing stream URL: {Url}", streamUrl);
|
||||
await _lmsClient.PlayUrlAsync(_player.MacAddress, streamUrl).ConfigureAwait(false);
|
||||
|
||||
// Seek to start position if specified
|
||||
if (playRequest.StartPositionTicks.HasValue && playRequest.StartPositionTicks.Value > 0)
|
||||
{
|
||||
var positionSeconds = playRequest.StartPositionTicks.Value / TimeSpan.TicksPerSecond;
|
||||
await _lmsClient.SeekAsync(_player.MacAddress, positionSeconds).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async Task HandlePlaystateCommandAsync<T>(T data, CancellationToken cancellationToken)
|
||||
{
|
||||
if (data is not PlaystateRequest playstateRequest)
|
||||
{
|
||||
_logger.LogWarning("Expected PlaystateRequest but got {Type}", data?.GetType().Name);
|
||||
return;
|
||||
}
|
||||
|
||||
_logger.LogDebug(
|
||||
"Playstate command {Command} for player {PlayerName}",
|
||||
playstateRequest.Command,
|
||||
_player.Name);
|
||||
|
||||
switch (playstateRequest.Command)
|
||||
{
|
||||
case PlaystateCommand.Stop:
|
||||
await _lmsClient.StopAsync(_player.MacAddress).ConfigureAwait(false);
|
||||
break;
|
||||
|
||||
case PlaystateCommand.Pause:
|
||||
await _lmsClient.PauseAsync(_player.MacAddress).ConfigureAwait(false);
|
||||
break;
|
||||
|
||||
case PlaystateCommand.Unpause:
|
||||
await _lmsClient.PlayAsync(_player.MacAddress).ConfigureAwait(false);
|
||||
break;
|
||||
|
||||
case PlaystateCommand.Seek:
|
||||
if (playstateRequest.SeekPositionTicks.HasValue)
|
||||
{
|
||||
var positionSeconds = playstateRequest.SeekPositionTicks.Value / TimeSpan.TicksPerSecond;
|
||||
await _lmsClient.SeekAsync(_player.MacAddress, positionSeconds).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case PlaystateCommand.NextTrack:
|
||||
case PlaystateCommand.PreviousTrack:
|
||||
// TODO: Implement playlist navigation
|
||||
_logger.LogDebug("Track navigation not yet implemented");
|
||||
break;
|
||||
|
||||
default:
|
||||
_logger.LogDebug("Unhandled playstate command: {Command}", playstateRequest.Command);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task HandleGeneralCommandAsync<T>(T data, CancellationToken cancellationToken)
|
||||
{
|
||||
if (data is not GeneralCommand command)
|
||||
{
|
||||
_logger.LogWarning("Expected GeneralCommand but got {Type}", data?.GetType().Name);
|
||||
return;
|
||||
}
|
||||
|
||||
_logger.LogDebug(
|
||||
"General command {CommandName} for player {PlayerName}",
|
||||
command.Name,
|
||||
_player.Name);
|
||||
|
||||
switch (command.Name)
|
||||
{
|
||||
case GeneralCommandType.SetVolume:
|
||||
if (command.Arguments.TryGetValue("Volume", out var volumeStr) &&
|
||||
int.TryParse(volumeStr, out var volume))
|
||||
{
|
||||
await _lmsClient.SetVolumeAsync(_player.MacAddress, volume).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case GeneralCommandType.VolumeUp:
|
||||
var currentStatus = await _lmsClient.GetPlayerStatusAsync(_player.MacAddress).ConfigureAwait(false);
|
||||
if (currentStatus != null)
|
||||
{
|
||||
var newVolume = Math.Min(100, currentStatus.Volume + 5);
|
||||
await _lmsClient.SetVolumeAsync(_player.MacAddress, newVolume).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case GeneralCommandType.VolumeDown:
|
||||
var status = await _lmsClient.GetPlayerStatusAsync(_player.MacAddress).ConfigureAwait(false);
|
||||
if (status != null)
|
||||
{
|
||||
var newVolume = Math.Max(0, status.Volume - 5);
|
||||
await _lmsClient.SetVolumeAsync(_player.MacAddress, newVolume).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case GeneralCommandType.Mute:
|
||||
await _lmsClient.SetVolumeAsync(_player.MacAddress, 0).ConfigureAwait(false);
|
||||
break;
|
||||
|
||||
case GeneralCommandType.ToggleMute:
|
||||
// TODO: Track mute state to toggle properly
|
||||
break;
|
||||
|
||||
default:
|
||||
_logger.LogDebug("Unhandled general command: {Command}", command.Name);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private string BuildStreamUrl(Guid itemId)
|
||||
{
|
||||
var config = Plugin.Instance?.Configuration;
|
||||
var jellyfinUrl = config?.JellyfinServerUrl?.TrimEnd('/') ?? "http://localhost:8096";
|
||||
var apiKey = config?.JellyfinApiKey ?? string.Empty;
|
||||
|
||||
// Build audio stream URL that LMS can fetch
|
||||
// Use direct stream endpoint - simpler and more compatible
|
||||
var url = $"{jellyfinUrl}/Audio/{itemId}/stream?static=true";
|
||||
|
||||
if (!string.IsNullOrEmpty(apiKey))
|
||||
{
|
||||
url += $"&api_key={apiKey}";
|
||||
}
|
||||
|
||||
return url;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user