add shared media control allowing jellyfin to control LMS by pointing to files on shared storage
This commit is contained in:
@@ -172,14 +172,17 @@ public class LmsSessionController : ISessionController, IDisposable
|
||||
var itemId = _playlist[index];
|
||||
_playlistIndex = index;
|
||||
|
||||
// Build stream URL with start position - LMS can't seek on HTTP streams,
|
||||
// so we need to use Jellyfin's startTimeTicks parameter for transcoding
|
||||
var streamUrl = BuildStreamUrlWithPosition(itemId, startPositionTicks);
|
||||
// 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: {Url}",
|
||||
"Playing item {Index}/{Total} from position {Position}s (direct={Direct}): {Url}",
|
||||
index + 1,
|
||||
_playlist.Length,
|
||||
startPositionTicks / TimeSpan.TicksPerSecond,
|
||||
useDirectPath,
|
||||
streamUrl);
|
||||
|
||||
await _lmsClient.PlayUrlAsync(_player.MacAddress, streamUrl).ConfigureAwait(false);
|
||||
@@ -190,12 +193,9 @@ public class LmsSessionController : ISessionController, IDisposable
|
||||
IsPaused = false;
|
||||
|
||||
// Track the seek offset so we report the correct position
|
||||
// When starting from a position, the transcoded stream starts at 0,
|
||||
// but we need to report the actual track position
|
||||
_seekOffsetTicks = startPositionTicks;
|
||||
|
||||
// Look up the item for duration info
|
||||
_currentItem = _libraryManager.GetItemById(itemId);
|
||||
// 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);
|
||||
@@ -419,21 +419,42 @@ public class LmsSessionController : ISessionController, IDisposable
|
||||
if (playstateRequest.SeekPositionTicks.HasValue && CurrentItemId.HasValue)
|
||||
{
|
||||
var positionTicks = playstateRequest.SeekPositionTicks.Value;
|
||||
var positionSeconds = positionTicks / TimeSpan.TicksPerSecond;
|
||||
|
||||
// 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);
|
||||
// Check if we're using direct file path mode - if so, LMS can seek natively
|
||||
var config = Plugin.Instance?.Configuration;
|
||||
var canSeekNatively = config?.UseDirectFilePath == true
|
||||
&& !string.IsNullOrEmpty(config.JellyfinMediaPath)
|
||||
&& !string.IsNullOrEmpty(config.LmsMediaPath)
|
||||
&& _currentItem?.Path != null
|
||||
&& _currentItem.Path.StartsWith(config.JellyfinMediaPath, StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
await _lmsClient.PlayUrlAsync(_player.MacAddress, streamUrl).ConfigureAwait(false);
|
||||
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);
|
||||
// No seek offset needed - LMS handles position tracking natively
|
||||
_seekOffsetTicks = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
// 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}",
|
||||
positionSeconds,
|
||||
streamUrl);
|
||||
|
||||
// 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);
|
||||
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("Seek offset is now {Ticks} ticks ({Seconds}s)", _seekOffsetTicks, _seekOffsetTicks / TimeSpan.TicksPerSecond);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -536,6 +557,59 @@ public class LmsSessionController : ISessionController, IDisposable
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Builds the playback URL or file path for the given item.
|
||||
/// Returns a tuple of (url/path, isDirectFilePath).
|
||||
/// </summary>
|
||||
private (string Url, bool IsDirectPath) BuildPlaybackUrl(Guid itemId, long startPositionTicks)
|
||||
{
|
||||
var config = Plugin.Instance?.Configuration;
|
||||
|
||||
// Check if direct file path mode is enabled and configured
|
||||
if (config?.UseDirectFilePath == true
|
||||
&& !string.IsNullOrEmpty(config.JellyfinMediaPath)
|
||||
&& !string.IsNullOrEmpty(config.LmsMediaPath)
|
||||
&& _currentItem?.Path != null)
|
||||
{
|
||||
var directPath = BuildDirectFilePath(_currentItem.Path, config.JellyfinMediaPath, config.LmsMediaPath);
|
||||
if (directPath != null)
|
||||
{
|
||||
_logger.LogDebug(
|
||||
"Using direct file path: {OriginalPath} -> {MappedPath}",
|
||||
_currentItem.Path,
|
||||
directPath);
|
||||
return (directPath, true);
|
||||
}
|
||||
|
||||
_logger.LogWarning(
|
||||
"Direct file path mode enabled but path mapping failed for: {Path}",
|
||||
_currentItem.Path);
|
||||
}
|
||||
|
||||
// Fall back to HTTP streaming
|
||||
return (BuildStreamUrlWithPosition(itemId, startPositionTicks), false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Maps a Jellyfin file path to an LMS file path using the configured path prefixes.
|
||||
/// </summary>
|
||||
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);
|
||||
@@ -553,7 +627,9 @@ public class LmsSessionController : ISessionController, IDisposable
|
||||
{
|
||||
// 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}";
|
||||
// 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
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user