jellyLMS/Jellyfin.Plugin.JellyLMS/Services/LmsSessionController.cs
Duncan Tourolle c967b062a2
Some checks failed
Build Plugin / build (push) Successful in 2m5s
Release Plugin / build-and-release (push) Failing after 8s
update readme
improve scrubbing
improve syncing gui
2025-12-14 16:16:33 +01:00

594 lines
21 KiB
C#

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;
/// <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, 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;
private Guid[] _playlist = [];
private int _playlistIndex;
private long _seekOffsetTicks; // Offset from transcoded stream start position
/// <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>
/// <param name="sessionManager">The session manager for reporting playback events.</param>
/// <param name="libraryManager">The library manager for item lookups.</param>
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;
}
/// <summary>
/// Gets or sets the currently playing item ID.
/// </summary>
public Guid? CurrentItemId { get; set; }
/// <summary>
/// Gets or sets a value indicating whether playback is currently active.
/// </summary>
public bool IsPlaying { get; set; }
/// <summary>
/// Gets or sets a value indicating whether playback is paused.
/// </summary>
public bool IsPaused { get; set; }
/// <inheritdoc />
public bool IsSessionActive => _player.IsConnected;
/// <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.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>(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;
var streamUrl = BuildStreamUrl(itemId);
_logger.LogInformation("Playing item {Index}/{Total}: {Url}", index + 1, _playlist.Length, streamUrl);
await _lmsClient.PlayUrlAsync(_player.MacAddress, streamUrl).ConfigureAwait(false);
// Track current playback state
CurrentItemId = itemId;
IsPlaying = true;
IsPaused = false;
_seekOffsetTicks = 0; // Reset seek offset when starting a new track
// Look up the item for duration info
_currentItem = _libraryManager.GetItemById(itemId);
// Seek to start position if specified
if (startPositionTicks > 0)
{
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;
}
// 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 isPaused = status.Mode == "pause";
// Update our local state from LMS
IsPaused = isPaused;
// Check if playback has stopped on LMS side (track ended)
// Only advance if we're not paused - LMS can briefly report "stop" during transitions
if (status.Mode == "stop" && !IsPaused)
{
// Double-check by getting status again after a brief delay to avoid false positives
await Task.Delay(500).ConfigureAwait(false);
var confirmStatus = await _lmsClient.GetPlayerStatusAsync(_player.MacAddress).ConfigureAwait(false);
if (confirmStatus?.Mode != "stop")
{
_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 = 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>(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:
_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;
// For HTTP streams, LMS can't seek directly - we need to restart with startTimeTicks
// Build a new URL with the seek position and restart playback
var streamUrl = BuildStreamUrlWithPosition(CurrentItemId.Value, positionTicks);
_logger.LogInformation(
"Seeking by restarting stream at position {Seconds}s: {Url}",
positionTicks / TimeSpan.TicksPerSecond,
streamUrl);
await _lmsClient.PlayUrlAsync(_player.MacAddress, streamUrl).ConfigureAwait(false);
// Track the seek offset so we report the correct position
// The transcoded stream starts at 0, but we need to report the actual track position
_seekOffsetTicks = positionTicks;
_logger.LogInformation("Set seek offset to {Ticks} ticks ({Seconds}s)", positionTicks, positionTicks / 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>(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)
{
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
url = $"{jellyfinUrl}/Audio/{itemId}/stream.mp3?audioCodec=mp3&audioBitRate=320000&startTimeTicks={startPositionTicks}";
}
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;
}
/// <inheritdoc />
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Disposes managed resources.
/// </summary>
/// <param name="disposing">Whether to dispose managed resources.</param>
protected virtual void Dispose(bool disposing)
{
if (_disposed)
{
return;
}
if (disposing)
{
StopProgressTimer();
}
_disposed = true;
}
}