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;
///
/// Session controller for LMS player devices.
/// Enables Jellyfin to send playback commands to LMS players via the cast interface.
///
public class LmsSessionController : ISessionController
{
private readonly ILogger _logger;
private readonly ILmsApiClient _lmsClient;
private readonly LmsPlayer _player;
private readonly SessionInfo _session;
///
/// Initializes a new instance of the class.
///
/// The logger instance.
/// The LMS API client.
/// The LMS player this controller manages.
/// The Jellyfin session associated with this controller.
public LmsSessionController(
ILogger logger,
ILmsApiClient lmsClient,
LmsPlayer player,
SessionInfo session)
{
_logger = logger;
_lmsClient = lmsClient;
_player = player;
_session = session;
}
///
/// Gets or sets the currently playing item ID.
///
public Guid? CurrentItemId { get; set; }
///
/// Gets or sets a value indicating whether playback is currently active.
///
public bool IsPlaying { get; set; }
///
/// Gets or sets a value indicating whether playback is paused.
///
public bool IsPaused { get; set; }
///
public bool IsSessionActive => _player.IsConnected;
///
public bool SupportsMediaControl => true;
///
/// Gets the MAC address of the LMS player.
///
public string PlayerMac => _player.MacAddress;
///
public async Task SendMessage(
SessionMessageType name,
Guid messageId,
T data,
CancellationToken cancellationToken)
{
_logger.LogInformation(
"LMS Session Controller received message {MessageType} for player {PlayerName} ({Mac})",
name,
_player.Name,
_player.MacAddress);
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 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);
// Track current playback state
CurrentItemId = itemId;
IsPlaying = true;
IsPaused = 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 data, CancellationToken cancellationToken)
{
if (data is not PlaystateRequest playstateRequest)
{
_logger.LogWarning("Expected PlaystateRequest but got {Type}", data?.GetType().Name);
return;
}
_logger.LogInformation(
"Playstate command {Command} for player {PlayerName} ({Mac})",
playstateRequest.Command,
_player.Name,
_player.MacAddress);
switch (playstateRequest.Command)
{
case PlaystateCommand.Stop:
var stopResult = await _lmsClient.StopAsync(_player.MacAddress).ConfigureAwait(false);
_logger.LogInformation("Stop command result: {Result}", stopResult);
IsPlaying = false;
IsPaused = false;
CurrentItemId = null;
break;
case PlaystateCommand.Pause:
var pauseResult = await _lmsClient.PauseAsync(_player.MacAddress).ConfigureAwait(false);
_logger.LogInformation("Pause command result: {Result}", pauseResult);
IsPaused = true;
break;
case PlaystateCommand.Unpause:
var playResult = await _lmsClient.PlayAsync(_player.MacAddress).ConfigureAwait(false);
_logger.LogInformation("Unpause/Play command result: {Result}", playResult);
IsPaused = false;
break;
case PlaystateCommand.PlayPause:
// Toggle play/pause - check current state first
var currentState = await _lmsClient.GetPlayerStatusAsync(_player.MacAddress).ConfigureAwait(false);
if (currentState?.Mode == "play")
{
var togglePauseResult = await _lmsClient.PauseAsync(_player.MacAddress).ConfigureAwait(false);
_logger.LogInformation("PlayPause toggle (pause) result: {Result}", togglePauseResult);
IsPaused = true;
}
else
{
var togglePlayResult = await _lmsClient.PlayAsync(_player.MacAddress).ConfigureAwait(false);
_logger.LogInformation("PlayPause toggle (play) result: {Result}", togglePlayResult);
IsPaused = false;
}
break;
case PlaystateCommand.Seek:
if (playstateRequest.SeekPositionTicks.HasValue)
{
var positionSeconds = playstateRequest.SeekPositionTicks.Value / TimeSpan.TicksPerSecond;
_logger.LogInformation(
"Seeking player {PlayerName} to {Seconds} seconds",
_player.Name,
positionSeconds);
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 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;
}
}