diff --git a/allstarr/Controllers/JellyfinController.cs b/allstarr/Controllers/JellyfinController.cs index 9fce43c..808c9c2 100644 --- a/allstarr/Controllers/JellyfinController.cs +++ b/allstarr/Controllers/JellyfinController.cs @@ -1789,7 +1789,22 @@ public class JellyfinController : ControllerBase var (deviceId, client, device, version) = ExtractDeviceInfo(Request.Headers); if (!string.IsNullOrEmpty(deviceId)) { - await _sessionManager.EnsureSessionAsync(deviceId, client ?? "Unknown", device ?? "Unknown", version ?? "1.0", Request.Headers); + var sessionCreated = await _sessionManager.EnsureSessionAsync(deviceId, client ?? "Unknown", device ?? "Unknown", version ?? "1.0", Request.Headers); + if (sessionCreated) + { + _logger.LogInformation("✓ Session ensured for device {DeviceId}", deviceId); + + // Give Jellyfin a moment to process the session creation + await Task.Delay(100); + } + else + { + _logger.LogWarning("⚠️ Failed to ensure session for device {DeviceId}", deviceId); + } + } + else + { + _logger.LogWarning("⚠️ No device ID found in headers - session cannot be created"); } // Parse the body to check if it's an external track diff --git a/allstarr/Services/Jellyfin/JellyfinSessionManager.cs b/allstarr/Services/Jellyfin/JellyfinSessionManager.cs index 5fad725..3388d72 100644 --- a/allstarr/Services/Jellyfin/JellyfinSessionManager.cs +++ b/allstarr/Services/Jellyfin/JellyfinSessionManager.cs @@ -42,6 +42,9 @@ public class JellyfinSessionManager { existingSession.LastActivity = DateTime.UtcNow; _logger.LogDebug("Session already exists for device {DeviceId}", deviceId); + + // Refresh capabilities to keep session alive + await PostCapabilitiesAsync(headers); return true; } @@ -50,45 +53,22 @@ public class JellyfinSessionManager try { // Post session capabilities to Jellyfin - this creates the session - var capabilities = new + await PostCapabilitiesAsync(headers); + + _logger.LogInformation("✓ Session created for {DeviceId}", deviceId); + + // Track this session + _sessions[deviceId] = new SessionInfo { - PlayableMediaTypes = new[] { "Audio" }, - SupportedCommands = new[] - { - "Play", - "Playstate", - "PlayNext" - }, - SupportsMediaControl = true, - SupportsPersistentIdentifier = true, - SupportsSync = false + DeviceId = deviceId, + Client = client, + Device = device, + Version = version, + LastActivity = DateTime.UtcNow, + Headers = CloneHeaders(headers) }; - var json = JsonSerializer.Serialize(capabilities); - var (result, statusCode) = await _proxyService.PostJsonAsync("Sessions/Capabilities/Full", json, headers); - - if (statusCode == 204 || statusCode == 200) - { - _logger.LogInformation("✓ Session created for {DeviceId}", deviceId); - - // Track this session - _sessions[deviceId] = new SessionInfo - { - DeviceId = deviceId, - Client = client, - Device = device, - Version = version, - LastActivity = DateTime.UtcNow, - Headers = CloneHeaders(headers) - }; - - return true; - } - else - { - _logger.LogWarning("Failed to create session for {DeviceId} - status {StatusCode}", deviceId, statusCode); - return false; - } + return true; } catch (Exception ex) { @@ -97,6 +77,34 @@ public class JellyfinSessionManager } } + /// + /// Posts session capabilities to Jellyfin. + /// + private async Task PostCapabilitiesAsync(IHeaderDictionary headers) + { + var capabilities = new + { + PlayableMediaTypes = new[] { "Audio" }, + SupportedCommands = new[] + { + "Play", + "Playstate", + "PlayNext" + }, + SupportsMediaControl = true, + SupportsPersistentIdentifier = true, + SupportsSync = false + }; + + var json = JsonSerializer.Serialize(capabilities); + var (result, statusCode) = await _proxyService.PostJsonAsync("Sessions/Capabilities/Full", json, headers); + + if (statusCode != 204 && statusCode != 200) + { + _logger.LogWarning("Failed to post capabilities - status {StatusCode}", statusCode); + } + } + /// /// Updates session activity timestamp. /// @@ -150,18 +158,7 @@ public class JellyfinSessionManager try { // Post capabilities again to keep session alive - var capabilities = new - { - PlayableMediaTypes = new[] { "Audio" }, - SupportedCommands = new[] { "Play", "Playstate", "PlayNext" }, - SupportsMediaControl = true, - SupportsPersistentIdentifier = true, - SupportsSync = false - }; - - var json = JsonSerializer.Serialize(capabilities); - await _proxyService.PostJsonAsync("Sessions/Capabilities/Full", json, session.Headers); - + await PostCapabilitiesAsync(session.Headers); _logger.LogDebug("✓ Kept session alive for {DeviceId}", session.DeviceId); } catch (Exception ex)