| | | 1 | | #nullable disable |
| | | 2 | | |
| | | 3 | | using System; |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.Linq; |
| | | 6 | | using System.Text.Json.Serialization; |
| | | 7 | | using System.Threading; |
| | | 8 | | using System.Threading.Tasks; |
| | | 9 | | using Jellyfin.Data.Enums; |
| | | 10 | | using MediaBrowser.Controller.Entities; |
| | | 11 | | using MediaBrowser.Model.Dto; |
| | | 12 | | using MediaBrowser.Model.Session; |
| | | 13 | | using Microsoft.Extensions.Logging; |
| | | 14 | | |
| | | 15 | | namespace MediaBrowser.Controller.Session |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// Class SessionInfo. |
| | | 19 | | /// </summary> |
| | | 20 | | public sealed class SessionInfo : IAsyncDisposable |
| | | 21 | | { |
| | | 22 | | // 1 second |
| | | 23 | | private const long ProgressIncrement = 10000000; |
| | | 24 | | |
| | | 25 | | private readonly ISessionManager _sessionManager; |
| | | 26 | | private readonly ILogger _logger; |
| | | 27 | | |
| | 94 | 28 | | private readonly Lock _progressLock = new(); |
| | | 29 | | private Timer _progressTimer; |
| | | 30 | | private PlaybackProgressInfo _lastProgressInfo; |
| | | 31 | | private long? _lastPlaybackCheckInPositionTicks; |
| | | 32 | | |
| | | 33 | | private bool _disposed; |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Initializes a new instance of the <see cref="SessionInfo"/> class. |
| | | 37 | | /// </summary> |
| | | 38 | | /// <param name="sessionManager">Instance of <see cref="ISessionManager"/> interface.</param> |
| | | 39 | | /// <param name="logger">Instance of <see cref="ILogger"/> interface.</param> |
| | | 40 | | public SessionInfo(ISessionManager sessionManager, ILogger logger) |
| | | 41 | | { |
| | 94 | 42 | | _sessionManager = sessionManager; |
| | 94 | 43 | | _logger = logger; |
| | | 44 | | |
| | 94 | 45 | | AdditionalUsers = []; |
| | 94 | 46 | | PlayState = new PlayerStateInfo(); |
| | 94 | 47 | | SessionControllers = []; |
| | 94 | 48 | | NowPlayingQueue = []; |
| | 94 | 49 | | } |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Gets or sets the play state. |
| | | 53 | | /// </summary> |
| | | 54 | | /// <value>The play state.</value> |
| | | 55 | | public PlayerStateInfo PlayState { get; set; } |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Gets or sets the additional users. |
| | | 59 | | /// </summary> |
| | | 60 | | /// <value>The additional users.</value> |
| | | 61 | | public IReadOnlyList<SessionUserInfo> AdditionalUsers { get; set; } |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// Gets or sets the client capabilities. |
| | | 65 | | /// </summary> |
| | | 66 | | /// <value>The client capabilities.</value> |
| | | 67 | | public ClientCapabilities Capabilities { get; set; } |
| | | 68 | | |
| | | 69 | | /// <summary> |
| | | 70 | | /// Gets or sets the remote end point. |
| | | 71 | | /// </summary> |
| | | 72 | | /// <value>The remote end point.</value> |
| | | 73 | | public string RemoteEndPoint { get; set; } |
| | | 74 | | |
| | | 75 | | /// <summary> |
| | | 76 | | /// Gets the playable media types. |
| | | 77 | | /// </summary> |
| | | 78 | | /// <value>The playable media types.</value> |
| | | 79 | | public IReadOnlyList<MediaType> PlayableMediaTypes |
| | | 80 | | { |
| | | 81 | | get |
| | | 82 | | { |
| | 16 | 83 | | if (Capabilities is null) |
| | | 84 | | { |
| | 0 | 85 | | return []; |
| | | 86 | | } |
| | | 87 | | |
| | 16 | 88 | | return Capabilities.PlayableMediaTypes; |
| | | 89 | | } |
| | | 90 | | } |
| | | 91 | | |
| | | 92 | | /// <summary> |
| | | 93 | | /// Gets or sets the id. |
| | | 94 | | /// </summary> |
| | | 95 | | /// <value>The id.</value> |
| | | 96 | | public string Id { get; set; } |
| | | 97 | | |
| | | 98 | | /// <summary> |
| | | 99 | | /// Gets or sets the user id. |
| | | 100 | | /// </summary> |
| | | 101 | | /// <value>The user id.</value> |
| | | 102 | | public Guid UserId { get; set; } |
| | | 103 | | |
| | | 104 | | /// <summary> |
| | | 105 | | /// Gets or sets the username. |
| | | 106 | | /// </summary> |
| | | 107 | | /// <value>The username.</value> |
| | | 108 | | public string UserName { get; set; } |
| | | 109 | | |
| | | 110 | | /// <summary> |
| | | 111 | | /// Gets or sets the type of the client. |
| | | 112 | | /// </summary> |
| | | 113 | | /// <value>The type of the client.</value> |
| | | 114 | | public string Client { get; set; } |
| | | 115 | | |
| | | 116 | | /// <summary> |
| | | 117 | | /// Gets or sets the last activity date. |
| | | 118 | | /// </summary> |
| | | 119 | | /// <value>The last activity date.</value> |
| | | 120 | | public DateTime LastActivityDate { get; set; } |
| | | 121 | | |
| | | 122 | | /// <summary> |
| | | 123 | | /// Gets or sets the last playback check in. |
| | | 124 | | /// </summary> |
| | | 125 | | /// <value>The last playback check in.</value> |
| | | 126 | | public DateTime LastPlaybackCheckIn { get; set; } |
| | | 127 | | |
| | | 128 | | /// <summary> |
| | | 129 | | /// Gets the position reported by the client at the last playback check-in. |
| | | 130 | | /// </summary> |
| | | 131 | | /// <value>The position ticks, or <see langword="null"/> if the client did not report a position.</value> |
| | | 132 | | [JsonIgnore] |
| | | 133 | | public long? LastPlaybackCheckInPositionTicks |
| | | 134 | | { |
| | | 135 | | get |
| | 6 | 136 | | { |
| | | 137 | | lock (_progressLock) |
| | | 138 | | { |
| | 6 | 139 | | return _lastPlaybackCheckInPositionTicks; |
| | | 140 | | } |
| | 6 | 141 | | } |
| | | 142 | | } |
| | | 143 | | |
| | | 144 | | /// <summary> |
| | | 145 | | /// Gets or sets the last paused date. |
| | | 146 | | /// </summary> |
| | | 147 | | /// <value>The last paused date.</value> |
| | | 148 | | public DateTime? LastPausedDate { get; set; } |
| | | 149 | | |
| | | 150 | | /// <summary> |
| | | 151 | | /// Gets or sets the name of the device. |
| | | 152 | | /// </summary> |
| | | 153 | | /// <value>The name of the device.</value> |
| | | 154 | | public string DeviceName { get; set; } |
| | | 155 | | |
| | | 156 | | /// <summary> |
| | | 157 | | /// Gets or sets the type of the device. |
| | | 158 | | /// </summary> |
| | | 159 | | /// <value>The type of the device.</value> |
| | | 160 | | public string DeviceType { get; set; } |
| | | 161 | | |
| | | 162 | | /// <summary> |
| | | 163 | | /// Gets or sets the now playing item. |
| | | 164 | | /// </summary> |
| | | 165 | | /// <value>The now playing item.</value> |
| | | 166 | | public BaseItemDto NowPlayingItem { get; set; } |
| | | 167 | | |
| | | 168 | | /// <summary> |
| | | 169 | | /// Gets or sets the now playing queue full items. |
| | | 170 | | /// </summary> |
| | | 171 | | /// <value>The now playing queue full items.</value> |
| | | 172 | | [JsonIgnore] |
| | | 173 | | public BaseItem FullNowPlayingItem { get; set; } |
| | | 174 | | |
| | | 175 | | /// <summary> |
| | | 176 | | /// Gets or sets the now viewing item. |
| | | 177 | | /// </summary> |
| | | 178 | | /// <value>The now viewing item.</value> |
| | | 179 | | public BaseItemDto NowViewingItem { get; set; } |
| | | 180 | | |
| | | 181 | | /// <summary> |
| | | 182 | | /// Gets or sets the device id. |
| | | 183 | | /// </summary> |
| | | 184 | | /// <value>The device id.</value> |
| | | 185 | | public string DeviceId { get; set; } |
| | | 186 | | |
| | | 187 | | /// <summary> |
| | | 188 | | /// Gets or sets the application version. |
| | | 189 | | /// </summary> |
| | | 190 | | /// <value>The application version.</value> |
| | | 191 | | public string ApplicationVersion { get; set; } |
| | | 192 | | |
| | | 193 | | /// <summary> |
| | | 194 | | /// Gets or sets the session controller. |
| | | 195 | | /// </summary> |
| | | 196 | | /// <value>The session controller.</value> |
| | | 197 | | [JsonIgnore] |
| | | 198 | | public IReadOnlyList<ISessionController> SessionControllers { get; set; } |
| | | 199 | | |
| | | 200 | | /// <summary> |
| | | 201 | | /// Gets or sets the transcoding info. |
| | | 202 | | /// </summary> |
| | | 203 | | /// <value>The transcoding info.</value> |
| | | 204 | | public TranscodingInfo TranscodingInfo { get; set; } |
| | | 205 | | |
| | | 206 | | /// <summary> |
| | | 207 | | /// Gets a value indicating whether this instance is active. |
| | | 208 | | /// </summary> |
| | | 209 | | /// <value><c>true</c> if this instance is active; otherwise, <c>false</c>.</value> |
| | | 210 | | public bool IsActive |
| | | 211 | | { |
| | | 212 | | get |
| | | 213 | | { |
| | 16 | 214 | | var controllers = SessionControllers; |
| | 32 | 215 | | foreach (var controller in controllers) |
| | | 216 | | { |
| | 0 | 217 | | if (controller.IsSessionActive) |
| | | 218 | | { |
| | 0 | 219 | | return true; |
| | | 220 | | } |
| | | 221 | | } |
| | | 222 | | |
| | 16 | 223 | | if (controllers.Count > 0) |
| | | 224 | | { |
| | 0 | 225 | | return false; |
| | | 226 | | } |
| | | 227 | | |
| | 16 | 228 | | return true; |
| | 0 | 229 | | } |
| | | 230 | | } |
| | | 231 | | |
| | | 232 | | /// <summary> |
| | | 233 | | /// Gets a value indicating whether the session supports media control. |
| | | 234 | | /// </summary> |
| | | 235 | | /// <value><c>true</c> if this session supports media control; otherwise, <c>false</c>.</value> |
| | | 236 | | public bool SupportsMediaControl |
| | | 237 | | { |
| | | 238 | | get |
| | | 239 | | { |
| | 16 | 240 | | if (Capabilities is null || !Capabilities.SupportsMediaControl) |
| | | 241 | | { |
| | 16 | 242 | | return false; |
| | | 243 | | } |
| | | 244 | | |
| | 0 | 245 | | var controllers = SessionControllers; |
| | 0 | 246 | | foreach (var controller in controllers) |
| | | 247 | | { |
| | 0 | 248 | | if (controller.SupportsMediaControl) |
| | | 249 | | { |
| | 0 | 250 | | return true; |
| | | 251 | | } |
| | | 252 | | } |
| | | 253 | | |
| | 0 | 254 | | return false; |
| | 0 | 255 | | } |
| | | 256 | | } |
| | | 257 | | |
| | | 258 | | /// <summary> |
| | | 259 | | /// Gets a value indicating whether the session supports remote control. |
| | | 260 | | /// </summary> |
| | | 261 | | /// <value><c>true</c> if this session supports remote control; otherwise, <c>false</c>.</value> |
| | | 262 | | public bool SupportsRemoteControl |
| | | 263 | | { |
| | | 264 | | get |
| | | 265 | | { |
| | 16 | 266 | | if (Capabilities is null || !Capabilities.SupportsMediaControl) |
| | | 267 | | { |
| | 16 | 268 | | return false; |
| | | 269 | | } |
| | | 270 | | |
| | 0 | 271 | | var controllers = SessionControllers; |
| | 0 | 272 | | foreach (var controller in controllers) |
| | | 273 | | { |
| | 0 | 274 | | if (controller.SupportsMediaControl) |
| | | 275 | | { |
| | 0 | 276 | | return true; |
| | | 277 | | } |
| | | 278 | | } |
| | | 279 | | |
| | 0 | 280 | | return false; |
| | 0 | 281 | | } |
| | | 282 | | } |
| | | 283 | | |
| | | 284 | | /// <summary> |
| | | 285 | | /// Gets or sets the now playing queue. |
| | | 286 | | /// </summary> |
| | | 287 | | /// <value>The now playing queue.</value> |
| | | 288 | | public IReadOnlyList<QueueItem> NowPlayingQueue { get; set; } |
| | | 289 | | |
| | | 290 | | /// <summary> |
| | | 291 | | /// Gets or sets a value indicating whether the session has a custom device name. |
| | | 292 | | /// </summary> |
| | | 293 | | /// <value><c>true</c> if the session has a custom device name; otherwise, <c>false</c>.</value> |
| | | 294 | | public bool HasCustomDeviceName { get; set; } |
| | | 295 | | |
| | | 296 | | /// <summary> |
| | | 297 | | /// Gets or sets the playlist item id. |
| | | 298 | | /// </summary> |
| | | 299 | | /// <value>The playlist item id.</value> |
| | | 300 | | public string PlaylistItemId { get; set; } |
| | | 301 | | |
| | | 302 | | /// <summary> |
| | | 303 | | /// Gets or sets the server id. |
| | | 304 | | /// </summary> |
| | | 305 | | /// <value>The server id.</value> |
| | | 306 | | public string ServerId { get; set; } |
| | | 307 | | |
| | | 308 | | /// <summary> |
| | | 309 | | /// Gets or sets the user primary image tag. |
| | | 310 | | /// </summary> |
| | | 311 | | /// <value>The user primary image tag.</value> |
| | | 312 | | public string UserPrimaryImageTag { get; set; } |
| | | 313 | | |
| | | 314 | | /// <summary> |
| | | 315 | | /// Gets the supported commands. |
| | | 316 | | /// </summary> |
| | | 317 | | /// <value>The supported commands.</value> |
| | | 318 | | public IReadOnlyList<GeneralCommandType> SupportedCommands |
| | 16 | 319 | | => Capabilities is null ? [] : Capabilities.SupportedCommands; |
| | | 320 | | |
| | | 321 | | /// <summary> |
| | | 322 | | /// Ensures a controller of type exists. |
| | | 323 | | /// </summary> |
| | | 324 | | /// <typeparam name="T">Class to register.</typeparam> |
| | | 325 | | /// <param name="factory">The factory.</param> |
| | | 326 | | /// <returns>Tuple{ISessionController, bool}.</returns> |
| | | 327 | | public Tuple<ISessionController, bool> EnsureController<T>(Func<SessionInfo, ISessionController> factory) |
| | | 328 | | { |
| | 1 | 329 | | var controllers = SessionControllers.ToList(); |
| | 2 | 330 | | foreach (var controller in controllers) |
| | | 331 | | { |
| | 0 | 332 | | if (controller is T) |
| | | 333 | | { |
| | 0 | 334 | | return new Tuple<ISessionController, bool>(controller, false); |
| | | 335 | | } |
| | | 336 | | } |
| | | 337 | | |
| | 1 | 338 | | var newController = factory(this); |
| | 1 | 339 | | _logger.LogDebug("Creating new {Factory}", newController.GetType().Name); |
| | 1 | 340 | | controllers.Add(newController); |
| | | 341 | | |
| | 1 | 342 | | SessionControllers = [.. controllers]; |
| | 1 | 343 | | return new Tuple<ISessionController, bool>(newController, true); |
| | 0 | 344 | | } |
| | | 345 | | |
| | | 346 | | /// <summary> |
| | | 347 | | /// Adds a controller to the session. |
| | | 348 | | /// </summary> |
| | | 349 | | /// <param name="controller">The controller.</param> |
| | | 350 | | public void AddController(ISessionController controller) |
| | | 351 | | { |
| | 0 | 352 | | SessionControllers = [.. SessionControllers, controller]; |
| | 0 | 353 | | } |
| | | 354 | | |
| | | 355 | | /// <summary> |
| | | 356 | | /// Gets a value indicating whether the session contains a user. |
| | | 357 | | /// </summary> |
| | | 358 | | /// <param name="userId">The user id to check.</param> |
| | | 359 | | /// <returns><c>true</c> if this session contains the user; otherwise, <c>false</c>.</returns> |
| | | 360 | | public bool ContainsUser(Guid userId) |
| | | 361 | | { |
| | 7 | 362 | | if (UserId.Equals(userId)) |
| | | 363 | | { |
| | 2 | 364 | | return true; |
| | | 365 | | } |
| | | 366 | | |
| | 10 | 367 | | foreach (var additionalUser in AdditionalUsers) |
| | | 368 | | { |
| | 0 | 369 | | if (additionalUser.UserId.Equals(userId)) |
| | | 370 | | { |
| | 0 | 371 | | return true; |
| | | 372 | | } |
| | | 373 | | } |
| | | 374 | | |
| | 5 | 375 | | return false; |
| | 0 | 376 | | } |
| | | 377 | | |
| | | 378 | | /// <summary> |
| | | 379 | | /// Starts automatic progressing. |
| | | 380 | | /// </summary> |
| | | 381 | | /// <param name="progressInfo">The playback progress info.</param> |
| | | 382 | | /// <value>The supported commands.</value> |
| | | 383 | | public void StartAutomaticProgress(PlaybackProgressInfo progressInfo) |
| | | 384 | | { |
| | 6 | 385 | | if (_disposed) |
| | | 386 | | { |
| | 0 | 387 | | return; |
| | | 388 | | } |
| | | 389 | | |
| | | 390 | | lock (_progressLock) |
| | | 391 | | { |
| | 6 | 392 | | _lastPlaybackCheckInPositionTicks = progressInfo.PositionTicks; |
| | 6 | 393 | | _lastProgressInfo = progressInfo; |
| | | 394 | | |
| | 6 | 395 | | if (_progressTimer is null) |
| | | 396 | | { |
| | 5 | 397 | | _progressTimer = new Timer(OnProgressTimerCallback, null, 1000, 1000); |
| | | 398 | | } |
| | | 399 | | else |
| | | 400 | | { |
| | 1 | 401 | | _progressTimer.Change(1000, 1000); |
| | | 402 | | } |
| | 1 | 403 | | } |
| | 6 | 404 | | } |
| | | 405 | | |
| | | 406 | | private async void OnProgressTimerCallback(object state) |
| | | 407 | | { |
| | 1 | 408 | | if (_disposed) |
| | | 409 | | { |
| | 0 | 410 | | return; |
| | | 411 | | } |
| | | 412 | | |
| | 1 | 413 | | var progressInfo = _lastProgressInfo; |
| | 1 | 414 | | if (progressInfo is null) |
| | | 415 | | { |
| | 0 | 416 | | return; |
| | | 417 | | } |
| | | 418 | | |
| | 1 | 419 | | if (progressInfo.IsPaused) |
| | | 420 | | { |
| | 0 | 421 | | return; |
| | | 422 | | } |
| | | 423 | | |
| | 1 | 424 | | var positionTicks = progressInfo.PositionTicks ?? 0; |
| | 1 | 425 | | if (positionTicks < 0) |
| | | 426 | | { |
| | 0 | 427 | | positionTicks = 0; |
| | | 428 | | } |
| | | 429 | | |
| | 1 | 430 | | var newPositionTicks = positionTicks + ProgressIncrement; |
| | 1 | 431 | | var item = progressInfo.Item; |
| | 1 | 432 | | long? runtimeTicks = item?.RunTimeTicks; |
| | | 433 | | |
| | | 434 | | // Don't report beyond the runtime |
| | 1 | 435 | | if (runtimeTicks.HasValue && newPositionTicks >= runtimeTicks.Value) |
| | | 436 | | { |
| | 0 | 437 | | return; |
| | | 438 | | } |
| | | 439 | | |
| | 1 | 440 | | progressInfo.PositionTicks = newPositionTicks; |
| | | 441 | | |
| | | 442 | | try |
| | | 443 | | { |
| | 1 | 444 | | await _sessionManager.OnPlaybackProgress(progressInfo, true).ConfigureAwait(false); |
| | 1 | 445 | | } |
| | 0 | 446 | | catch (Exception ex) |
| | | 447 | | { |
| | 0 | 448 | | _logger.LogError(ex, "Error reporting playback progress"); |
| | 0 | 449 | | } |
| | 1 | 450 | | } |
| | | 451 | | |
| | | 452 | | /// <summary> |
| | | 453 | | /// Stops automatic progressing. |
| | | 454 | | /// </summary> |
| | | 455 | | public void StopAutomaticProgress() |
| | 40 | 456 | | { |
| | | 457 | | lock (_progressLock) |
| | | 458 | | { |
| | 40 | 459 | | if (_progressTimer is not null) |
| | | 460 | | { |
| | 5 | 461 | | _progressTimer.Dispose(); |
| | 5 | 462 | | _progressTimer = null; |
| | | 463 | | } |
| | | 464 | | |
| | 40 | 465 | | _lastProgressInfo = null; |
| | 40 | 466 | | } |
| | 40 | 467 | | } |
| | | 468 | | |
| | | 469 | | /// <summary> |
| | | 470 | | /// Disposes the instance async. |
| | | 471 | | /// </summary> |
| | | 472 | | /// <returns>ValueTask.</returns> |
| | | 473 | | public async ValueTask DisposeAsync() |
| | | 474 | | { |
| | 36 | 475 | | _disposed = true; |
| | | 476 | | |
| | 36 | 477 | | StopAutomaticProgress(); |
| | | 478 | | |
| | 36 | 479 | | var controllers = SessionControllers.ToList(); |
| | 36 | 480 | | SessionControllers = []; |
| | | 481 | | |
| | 74 | 482 | | foreach (var controller in controllers) |
| | | 483 | | { |
| | 1 | 484 | | if (controller is IAsyncDisposable disposableAsync) |
| | | 485 | | { |
| | 1 | 486 | | _logger.LogDebug("Disposing session controller asynchronously {TypeName}", disposableAsync.GetType() |
| | 1 | 487 | | await disposableAsync.DisposeAsync().ConfigureAwait(false); |
| | | 488 | | } |
| | 0 | 489 | | else if (controller is IDisposable disposable) |
| | | 490 | | { |
| | 0 | 491 | | _logger.LogDebug("Disposing session controller synchronously {TypeName}", disposable.GetType().Name) |
| | 0 | 492 | | disposable.Dispose(); |
| | | 493 | | } |
| | | 494 | | } |
| | 36 | 495 | | } |
| | | 496 | | } |
| | | 497 | | } |