using System; using System.Threading; using System.Threading.Tasks; using Jellyfin.Plugin.JellyLMS.Configuration; 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 readonly PlaybackStateMachine _stateMachine; private readonly LmsStatusPoller _statusPoller; private readonly CancellationTokenSource _cancellationTokenSource = new(); private Timer? _progressTimer; private bool _disposed; private BaseItem? _currentItem; private Guid[] _playlist = []; private int _playlistIndex; private long _seekOffsetTicks; // Offset from transcoded stream start position private PlaybackErrorInfo? _lastError; /// /// 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; _stateMachine = new PlaybackStateMachine(logger); _statusPoller = new LmsStatusPoller(lmsClient, logger); } private static PluginConfiguration Config => Plugin.Instance?.Configuration ?? new PluginConfiguration(); /// /// Gets or sets the currently playing item ID. /// public Guid? CurrentItemId { get; set; } /// /// Gets a value indicating whether playback is currently active. /// public bool IsPlaying => _stateMachine.CurrentState == PlaybackState.Playing || _stateMachine.CurrentState == PlaybackState.Loading || _stateMachine.CurrentState == PlaybackState.Seeking; /// /// Gets a value indicating whether playback is paused. /// public bool IsPaused => _stateMachine.CurrentState == PlaybackState.Paused; /// /// Gets the current playback state. /// public PlaybackState State => _stateMachine.CurrentState; /// /// Gets the last error that occurred during playback. /// public PlaybackErrorInfo? LastError => _lastError; /// 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}), data type: {DataType}", name, _player.Name, _player.MacAddress, data?.GetType().Name ?? "null"); // Log the data for debugging if (data is PlaystateRequest psr) { _logger.LogInformation( "PlaystateRequest: Command={Command}, SeekPositionTicks={Ticks}", psr.Command, psr.SeekPositionTicks); } 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); } if (playRequest.ItemIds.Length > 0) { // Store the full playlist _playlist = playRequest.ItemIds; _playlistIndex = playRequest.StartIndex ?? 0; // Play the item at the start index await PlayItemAtIndexAsync(_playlistIndex, playRequest.StartPositionTicks ?? 0).ConfigureAwait(false); } } private async Task PlayItemAtIndexAsync(int index, long startPositionTicks = 0) { if (index < 0 || index >= _playlist.Length) { _logger.LogWarning("Invalid playlist index {Index}, playlist has {Count} items", index, _playlist.Length); return; } var itemId = _playlist[index]; _playlistIndex = index; // Look up the item first - we need it for file path if using direct mode _currentItem = _libraryManager.GetItemById(itemId); // Build stream URL/path with start position var (streamUrl, useDirectPath) = BuildPlaybackUrl(itemId, startPositionTicks); _logger.LogInformation( "Playing item {Index}/{Total} from position {Position}s (direct={Direct}): {Url}", index + 1, _playlist.Length, startPositionTicks / TimeSpan.TicksPerSecond, useDirectPath, streamUrl); // Transition to Loading state before sending command _stateMachine.TryTransition(PlaybackState.Loading, "Starting playback"); // Send play command with retry logic var playSuccess = await _statusPoller.ExecuteWithRetryAsync( async () => await _lmsClient.PlayUrlAsync(_player.MacAddress, streamUrl).ConfigureAwait(false), Config.MaxAutoRetries, retry => _logger.LogInformation("Retrying play command (attempt {Retry})", retry), _cancellationTokenSource.Token).ConfigureAwait(false); if (!playSuccess) { _lastError = new PlaybackErrorInfo { ErrorType = PlaybackErrorType.LmsError, Message = "Failed to send play command to LMS after retries", OccurredAt = DateTime.UtcNow, RetryCount = Config.MaxAutoRetries }; _stateMachine.TryTransition(PlaybackState.Error, "Play command failed"); return; } // Wait for LMS to confirm playback started var loadingTimeout = TimeSpan.FromSeconds(Config.LoadingTimeoutSeconds); var started = await _statusPoller.WaitForPlaybackStartAsync( _player.MacAddress, loadingTimeout, _cancellationTokenSource.Token).ConfigureAwait(false); if (!started) { _lastError = new PlaybackErrorInfo { ErrorType = PlaybackErrorType.Timeout, Message = $"LMS did not start playing within {loadingTimeout.TotalSeconds}s", OccurredAt = DateTime.UtcNow }; _stateMachine.TryTransition(PlaybackState.Error, "Loading timeout"); return; } // Track current playback state CurrentItemId = itemId; _stateMachine.TryTransition(PlaybackState.Playing, "LMS confirmed playback"); // Track the seek offset so we report the correct position // When using direct file paths, LMS handles seeking natively so no offset needed // When using HTTP streaming with startTimeTicks, the stream starts at 0 but we need to report actual position _seekOffsetTicks = useDirectPath ? 0 : startPositionTicks; // 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() { // Don't report during Loading, Seeking, or Error states var currentState = _stateMachine.CurrentState; if (currentState == PlaybackState.Loading || currentState == PlaybackState.Seeking || currentState == PlaybackState.Error || currentState == PlaybackState.Stopped || currentState == PlaybackState.Idle || !CurrentItemId.HasValue) { return; } try { var status = await _lmsClient.GetPlayerStatusAsync(_player.MacAddress).ConfigureAwait(false); if (status == null) { return; } // LMS reports time relative to the current stream, but after seeking // we're playing a transcoded stream that starts at the seek position. // Add the seek offset to get the actual track position. var positionTicks = (long)(status.Time * TimeSpan.TicksPerSecond) + _seekOffsetTicks; var lmsIsPaused = status.Mode == "pause"; // Sync state machine with LMS state (handles external pause/play) if (lmsIsPaused && currentState == PlaybackState.Playing) { _stateMachine.TryTransition(PlaybackState.Paused, "LMS reported pause"); } else if (!lmsIsPaused && status.Mode == "play" && currentState == PlaybackState.Paused) { _stateMachine.TryTransition(PlaybackState.Playing, "LMS reported play"); } // Check if playback has stopped on LMS side (track ended) if (status.Mode == "stop" && currentState == PlaybackState.Playing) { // Confirm stop with a quick poll instead of fixed delay var stillStopped = await _statusPoller.WaitForModeAsync( _player.MacAddress, "stop", TimeSpan.FromMilliseconds(500), _cancellationTokenSource.Token).ConfigureAwait(false); if (!stillStopped) { _logger.LogDebug("LMS mode changed from stop, ignoring"); return; } _logger.LogInformation("LMS playback stopped, checking if we should advance to next track"); // Check if there are more tracks in the playlist if (_playlistIndex < _playlist.Length - 1) { _logger.LogInformation( "Track ended, advancing to next track (index {Index}/{Total})", _playlistIndex + 2, _playlist.Length); await PlayItemAtIndexAsync(_playlistIndex + 1).ConfigureAwait(false); } else { _logger.LogInformation("Playlist finished, reporting playback stopped"); await ReportPlaybackStoppedAsync().ConfigureAwait(false); } return; } var progressInfo = new PlaybackProgressInfo { ItemId = CurrentItemId.Value, SessionId = _session.Id, IsPaused = _stateMachine.CurrentState == PlaybackState.Paused, 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); _stateMachine.TryTransition(PlaybackState.Stopped, "Playback stopped"); 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); _stateMachine.TryTransition(PlaybackState.Paused, "Pause command"); break; case PlaystateCommand.Unpause: var playResult = await _lmsClient.PlayAsync(_player.MacAddress).ConfigureAwait(false); _logger.LogInformation("Unpause/Play command result: {Result}", playResult); _stateMachine.TryTransition(PlaybackState.Playing, "Unpause command"); break; case PlaystateCommand.PlayPause: // Toggle play/pause based on current state if (_stateMachine.CurrentState == PlaybackState.Playing) { var togglePauseResult = await _lmsClient.PauseAsync(_player.MacAddress).ConfigureAwait(false); _logger.LogInformation("PlayPause toggle (pause) result: {Result}", togglePauseResult); _stateMachine.TryTransition(PlaybackState.Paused, "PlayPause toggle to pause"); } else { var togglePlayResult = await _lmsClient.PlayAsync(_player.MacAddress).ConfigureAwait(false); _logger.LogInformation("PlayPause toggle (play) result: {Result}", togglePlayResult); _stateMachine.TryTransition(PlaybackState.Playing, "PlayPause toggle to play"); } break; case PlaystateCommand.Seek: _logger.LogInformation( "Seek command received for player {PlayerName}, SeekPositionTicks: {Ticks}, CurrentItemId: {ItemId}, CurrentSeekOffset: {Offset}", _player.Name, playstateRequest.SeekPositionTicks, CurrentItemId, _seekOffsetTicks); if (playstateRequest.SeekPositionTicks.HasValue && CurrentItemId.HasValue) { var positionTicks = playstateRequest.SeekPositionTicks.Value; var positionSeconds = (double)(positionTicks / TimeSpan.TicksPerSecond); // Transition to Seeking state (state machine remembers previous state) _stateMachine.TryTransition(PlaybackState.Seeking, "Seek command"); // Check if we're using direct file path mode - if so, LMS can seek natively if (CanSeekNatively()) { // Use native LMS seeking - much smoother! _logger.LogInformation("Seeking natively to {Seconds}s using LMS time command", positionSeconds); await _lmsClient.SeekAsync(_player.MacAddress, positionSeconds).ConfigureAwait(false); // Wait for seek to complete var seekTimeout = TimeSpan.FromSeconds(Config.SeekTimeoutSeconds); var seekComplete = await _statusPoller.WaitForSeekCompleteAsync( _player.MacAddress, positionSeconds, toleranceSeconds: 2.0, seekTimeout, _cancellationTokenSource.Token).ConfigureAwait(false); // No seek offset needed - LMS handles position tracking natively _seekOffsetTicks = 0; // Restore previous state var previousState = _stateMachine.StateBeforeSeek; if (seekComplete) { _stateMachine.TryTransition(previousState, "Seek completed"); } else { _logger.LogWarning("Seek may not have completed within timeout, restoring state anyway"); _stateMachine.TryTransition(previousState, "Seek timeout - restoring state"); } } else { // For HTTP streams, LMS can't seek directly - we need to restart with startTimeTicks // This is essentially a new playback, so transition to Loading _stateMachine.TryTransition(PlaybackState.Loading, "HTTP stream seek - restarting"); var streamUrl = BuildStreamUrlWithPosition(CurrentItemId.Value, positionTicks); _logger.LogInformation( "Seeking by restarting stream at position {Seconds}s: {Url}", positionSeconds, streamUrl); await _lmsClient.PlayUrlAsync(_player.MacAddress, streamUrl).ConfigureAwait(false); // Wait for playback to start var loadingTimeout = TimeSpan.FromSeconds(Config.LoadingTimeoutSeconds); var started = await _statusPoller.WaitForPlaybackStartAsync( _player.MacAddress, loadingTimeout, _cancellationTokenSource.Token).ConfigureAwait(false); // Track the seek offset so we report the correct position _seekOffsetTicks = positionTicks; if (started) { _stateMachine.TryTransition(PlaybackState.Playing, "HTTP stream seek completed"); } else { _lastError = new PlaybackErrorInfo { ErrorType = PlaybackErrorType.Timeout, Message = "Stream restart after seek failed to start", OccurredAt = DateTime.UtcNow }; _stateMachine.TryTransition(PlaybackState.Error, "HTTP stream seek failed"); } } _logger.LogInformation("Seek offset is now {Ticks} ticks ({Seconds}s)", _seekOffsetTicks, _seekOffsetTicks / TimeSpan.TicksPerSecond); } else { _logger.LogWarning("Seek command received but SeekPositionTicks or CurrentItemId is null"); } break; case PlaystateCommand.NextTrack: if (_playlistIndex < _playlist.Length - 1) { _logger.LogInformation("Skipping to next track (index {Index})", _playlistIndex + 1); await PlayItemAtIndexAsync(_playlistIndex + 1).ConfigureAwait(false); } else { _logger.LogInformation("Already at last track, stopping playback"); await _lmsClient.StopAsync(_player.MacAddress).ConfigureAwait(false); await ReportPlaybackStoppedAsync().ConfigureAwait(false); } break; case PlaystateCommand.PreviousTrack: if (_playlistIndex > 0) { _logger.LogInformation("Skipping to previous track (index {Index})", _playlistIndex - 1); await PlayItemAtIndexAsync(_playlistIndex - 1).ConfigureAwait(false); } else { // At first track, restart from beginning _logger.LogInformation("At first track, restarting from beginning"); await _lmsClient.SeekAsync(_player.MacAddress, 0).ConfigureAwait(false); } 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; } } /// /// Builds the playback URL or file path for the given item. /// Returns a tuple of (url/path, isDirectFilePath). /// private (string Url, bool IsDirectPath) BuildPlaybackUrl(Guid itemId, long startPositionTicks) { var config = Plugin.Instance?.Configuration; // Check if direct file path mode is enabled if (config?.UseDirectFilePath == true && _currentItem?.Path != null) { // Try each path mapping until one matches foreach (var mapping in config.GetAllPathMappings()) { var directPath = BuildDirectFilePath(_currentItem.Path, mapping.JellyfinPath, mapping.LmsPath); if (directPath != null) { _logger.LogDebug( "Using direct file path with mapping '{JellyfinPath}' -> '{LmsPath}': {OriginalPath} -> {MappedPath}", mapping.JellyfinPath, mapping.LmsPath, _currentItem.Path, directPath); return (directPath, true); } } _logger.LogWarning( "Direct file path mode enabled but no path mapping matched for: {Path}", _currentItem.Path); } // Fall back to HTTP streaming return (BuildStreamUrlWithPosition(itemId, startPositionTicks), false); } /// /// Checks if the current item can use native LMS seeking (direct file path mode). /// private bool CanSeekNatively() { var config = Plugin.Instance?.Configuration; if (config?.UseDirectFilePath != true || _currentItem?.Path == null) { return false; } // Check if any mapping matches the current item's path foreach (var mapping in config.GetAllPathMappings()) { var normalizedPath = _currentItem.Path.Replace('\\', '/'); var normalizedPrefix = mapping.JellyfinPath.TrimEnd('/', '\\').Replace('\\', '/'); if (normalizedPath.StartsWith(normalizedPrefix, StringComparison.OrdinalIgnoreCase)) { return true; } } return false; } /// /// Maps a Jellyfin file path to an LMS file path using the configured path prefixes. /// private static string? BuildDirectFilePath(string jellyfinPath, string jellyfinPrefix, string lmsPrefix) { // Normalize path separators for comparison var normalizedPath = jellyfinPath.Replace('\\', '/'); var normalizedJellyfinPrefix = jellyfinPrefix.TrimEnd('/', '\\').Replace('\\', '/'); var normalizedLmsPrefix = lmsPrefix.TrimEnd('/', '\\').Replace('\\', '/'); if (!normalizedPath.StartsWith(normalizedJellyfinPrefix, StringComparison.OrdinalIgnoreCase)) { return null; } // Replace the prefix var relativePath = normalizedPath[normalizedJellyfinPrefix.Length..]; return normalizedLmsPrefix + relativePath; } private string BuildStreamUrl(Guid itemId) { return BuildStreamUrlWithPosition(itemId, 0); } private string BuildStreamUrlWithPosition(Guid itemId, long startPositionTicks) { var config = Plugin.Instance?.Configuration; var jellyfinUrl = config?.JellyfinServerUrl?.TrimEnd('/') ?? "http://localhost:8096"; var apiKey = config?.JellyfinApiKey ?? string.Empty; string url; if (startPositionTicks > 0) { // For seeking, we need to use transcoding (static=true doesn't support startTimeTicks) // Use MP3 transcoding with the start position // Add a cache-busting parameter to ensure we get a fresh stream on each seek var cacheBuster = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); url = $"{jellyfinUrl}/Audio/{itemId}/stream.mp3?audioCodec=mp3&audioBitRate=320000&startTimeTicks={startPositionTicks}&_={cacheBuster}"; } else { // For normal playback from start, use static streaming (better quality, no transcoding) url = $"{jellyfinUrl}/Audio/{itemId}/stream.mp3?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) { // Cancel any pending operations _cancellationTokenSource.Cancel(); _cancellationTokenSource.Dispose(); StopProgressTimer(); // Reset state machine _stateMachine.Reset(); } _disposed = true; } }