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; } /// public bool IsSessionActive => _player.IsConnected && _player.IsPoweredOn; /// 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.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 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 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 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; } }