using System; using System.Text.Json; using System.Threading; using System.Threading.Tasks; using Jellyfin.Plugin.JellyLMS.Models; using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Library; 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, IDisposable { private readonly ILogger _logger; private readonly ILmsApiClient _lmsClient; private readonly LmsPlayer _player; private readonly SessionInfo _session; private readonly ISessionManager _sessionManager; private readonly ILibraryManager _libraryManager; private Timer? _progressTimer; private bool _disposed; private BaseItem? _currentItem; /// /// 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. /// The session manager for reporting playback events. /// The library manager for item lookups. public LmsSessionController( ILogger logger, ILmsApiClient lmsClient, LmsPlayer player, SessionInfo session, ISessionManager sessionManager, ILibraryManager libraryManager) { _logger = logger; _lmsClient = lmsClient; _player = player; _session = session; _sessionManager = sessionManager; _libraryManager = libraryManager; } /// /// 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; // Look up the item for duration info _currentItem = _libraryManager.GetItemById(itemId); // Seek to start position if specified var startPositionTicks = 0L; if (playRequest.StartPositionTicks.HasValue && playRequest.StartPositionTicks.Value > 0) { startPositionTicks = playRequest.StartPositionTicks.Value; var positionSeconds = startPositionTicks / TimeSpan.TicksPerSecond; await _lmsClient.SeekAsync(_player.MacAddress, positionSeconds).ConfigureAwait(false); } // Report playback start to Jellyfin await ReportPlaybackStartAsync(itemId, startPositionTicks).ConfigureAwait(false); // Start progress reporting timer (every 2 seconds) StartProgressTimer(); } } private async Task ReportPlaybackStartAsync(Guid itemId, long positionTicks) { try { var startInfo = new PlaybackStartInfo { ItemId = itemId, SessionId = _session.Id, PositionTicks = positionTicks, PlayMethod = PlayMethod.DirectStream, CanSeek = true, IsPaused = false, IsMuted = false }; _logger.LogInformation( "Reporting playback start for item {ItemId}, duration: {Duration}", itemId, _currentItem?.RunTimeTicks); await _sessionManager.OnPlaybackStart(startInfo).ConfigureAwait(false); } catch (Exception ex) { _logger.LogError(ex, "Failed to report playback start"); } } private void StartProgressTimer() { // Stop any existing timer _progressTimer?.Dispose(); // Report progress every 2 seconds _progressTimer = new Timer( async _ => await ReportPlaybackProgressAsync().ConfigureAwait(false), null, TimeSpan.FromSeconds(2), TimeSpan.FromSeconds(2)); } private void StopProgressTimer() { _progressTimer?.Dispose(); _progressTimer = null; } private async Task ReportPlaybackProgressAsync() { if (!IsPlaying || !CurrentItemId.HasValue) { return; } try { var status = await _lmsClient.GetPlayerStatusAsync(_player.MacAddress).ConfigureAwait(false); if (status == null) { return; } var positionTicks = (long)(status.Time * TimeSpan.TicksPerSecond); var isPaused = status.Mode == "pause"; // Update our local state from LMS IsPaused = isPaused; // Check if playback has stopped on LMS side if (status.Mode == "stop") { _logger.LogInformation("LMS playback stopped, reporting to Jellyfin"); await ReportPlaybackStoppedAsync().ConfigureAwait(false); return; } var progressInfo = new PlaybackProgressInfo { ItemId = CurrentItemId.Value, SessionId = _session.Id, IsPaused = isPaused, PositionTicks = positionTicks, PlayMethod = PlayMethod.DirectStream, CanSeek = true, IsMuted = status.Volume == 0, VolumeLevel = status.Volume }; await _sessionManager.OnPlaybackProgress(progressInfo).ConfigureAwait(false); } catch (Exception ex) { _logger.LogDebug(ex, "Error reporting playback progress"); } } private async Task ReportPlaybackStoppedAsync() { if (!CurrentItemId.HasValue) { return; } try { StopProgressTimer(); var stopInfo = new PlaybackStopInfo { ItemId = CurrentItemId.Value, SessionId = _session.Id }; _logger.LogInformation("Reporting playback stopped for item {ItemId}", CurrentItemId.Value); await _sessionManager.OnPlaybackStopped(stopInfo).ConfigureAwait(false); IsPlaying = false; IsPaused = false; CurrentItemId = null; } catch (Exception ex) { _logger.LogError(ex, "Failed to report playback stopped"); } } 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); await ReportPlaybackStoppedAsync().ConfigureAwait(false); 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; } /// public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } /// /// Disposes managed resources. /// /// Whether to dispose managed resources. protected virtual void Dispose(bool disposing) { if (_disposed) { return; } if (disposing) { StopProgressTimer(); } _disposed = true; } }