| | | 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 | | |
| | 15 | 28 | | private readonly Lock _progressLock = new(); |
| | | 29 | | private Timer _progressTimer; |
| | | 30 | | private PlaybackProgressInfo _lastProgressInfo; |
| | | 31 | | |
| | | 32 | | private bool _disposed; |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Initializes a new instance of the <see cref="SessionInfo"/> class. |
| | | 36 | | /// </summary> |
| | | 37 | | /// <param name="sessionManager">Instance of <see cref="ISessionManager"/> interface.</param> |
| | | 38 | | /// <param name="logger">Instance of <see cref="ILogger"/> interface.</param> |
| | | 39 | | public SessionInfo(ISessionManager sessionManager, ILogger logger) |
| | | 40 | | { |
| | 15 | 41 | | _sessionManager = sessionManager; |
| | 15 | 42 | | _logger = logger; |
| | | 43 | | |
| | 15 | 44 | | AdditionalUsers = []; |
| | 15 | 45 | | PlayState = new PlayerStateInfo(); |
| | 15 | 46 | | SessionControllers = []; |
| | 15 | 47 | | NowPlayingQueue = []; |
| | 15 | 48 | | NowPlayingQueueFullItems = []; |
| | 15 | 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 | | { |
| | 15 | 83 | | if (Capabilities is null) |
| | | 84 | | { |
| | 0 | 85 | | return []; |
| | | 86 | | } |
| | | 87 | | |
| | 15 | 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 or sets the last paused date. |
| | | 130 | | /// </summary> |
| | | 131 | | /// <value>The last paused date.</value> |
| | | 132 | | public DateTime? LastPausedDate { get; set; } |
| | | 133 | | |
| | | 134 | | /// <summary> |
| | | 135 | | /// Gets or sets the name of the device. |
| | | 136 | | /// </summary> |
| | | 137 | | /// <value>The name of the device.</value> |
| | | 138 | | public string DeviceName { get; set; } |
| | | 139 | | |
| | | 140 | | /// <summary> |
| | | 141 | | /// Gets or sets the type of the device. |
| | | 142 | | /// </summary> |
| | | 143 | | /// <value>The type of the device.</value> |
| | | 144 | | public string DeviceType { get; set; } |
| | | 145 | | |
| | | 146 | | /// <summary> |
| | | 147 | | /// Gets or sets the now playing item. |
| | | 148 | | /// </summary> |
| | | 149 | | /// <value>The now playing item.</value> |
| | | 150 | | public BaseItemDto NowPlayingItem { get; set; } |
| | | 151 | | |
| | | 152 | | /// <summary> |
| | | 153 | | /// Gets or sets the now playing queue full items. |
| | | 154 | | /// </summary> |
| | | 155 | | /// <value>The now playing queue full items.</value> |
| | | 156 | | [JsonIgnore] |
| | | 157 | | public BaseItem FullNowPlayingItem { get; set; } |
| | | 158 | | |
| | | 159 | | /// <summary> |
| | | 160 | | /// Gets or sets the now viewing item. |
| | | 161 | | /// </summary> |
| | | 162 | | /// <value>The now viewing item.</value> |
| | | 163 | | public BaseItemDto NowViewingItem { get; set; } |
| | | 164 | | |
| | | 165 | | /// <summary> |
| | | 166 | | /// Gets or sets the device id. |
| | | 167 | | /// </summary> |
| | | 168 | | /// <value>The device id.</value> |
| | | 169 | | public string DeviceId { get; set; } |
| | | 170 | | |
| | | 171 | | /// <summary> |
| | | 172 | | /// Gets or sets the application version. |
| | | 173 | | /// </summary> |
| | | 174 | | /// <value>The application version.</value> |
| | | 175 | | public string ApplicationVersion { get; set; } |
| | | 176 | | |
| | | 177 | | /// <summary> |
| | | 178 | | /// Gets or sets the session controller. |
| | | 179 | | /// </summary> |
| | | 180 | | /// <value>The session controller.</value> |
| | | 181 | | [JsonIgnore] |
| | | 182 | | public IReadOnlyList<ISessionController> SessionControllers { get; set; } |
| | | 183 | | |
| | | 184 | | /// <summary> |
| | | 185 | | /// Gets or sets the transcoding info. |
| | | 186 | | /// </summary> |
| | | 187 | | /// <value>The transcoding info.</value> |
| | | 188 | | public TranscodingInfo TranscodingInfo { get; set; } |
| | | 189 | | |
| | | 190 | | /// <summary> |
| | | 191 | | /// Gets a value indicating whether this instance is active. |
| | | 192 | | /// </summary> |
| | | 193 | | /// <value><c>true</c> if this instance is active; otherwise, <c>false</c>.</value> |
| | | 194 | | public bool IsActive |
| | | 195 | | { |
| | | 196 | | get |
| | | 197 | | { |
| | 15 | 198 | | var controllers = SessionControllers; |
| | 30 | 199 | | foreach (var controller in controllers) |
| | | 200 | | { |
| | 0 | 201 | | if (controller.IsSessionActive) |
| | | 202 | | { |
| | 0 | 203 | | return true; |
| | | 204 | | } |
| | | 205 | | } |
| | | 206 | | |
| | 15 | 207 | | if (controllers.Count > 0) |
| | | 208 | | { |
| | 0 | 209 | | return false; |
| | | 210 | | } |
| | | 211 | | |
| | 15 | 212 | | return true; |
| | 0 | 213 | | } |
| | | 214 | | } |
| | | 215 | | |
| | | 216 | | /// <summary> |
| | | 217 | | /// Gets a value indicating whether the session supports media control. |
| | | 218 | | /// </summary> |
| | | 219 | | /// <value><c>true</c> if this session supports media control; otherwise, <c>false</c>.</value> |
| | | 220 | | public bool SupportsMediaControl |
| | | 221 | | { |
| | | 222 | | get |
| | | 223 | | { |
| | 15 | 224 | | if (Capabilities is null || !Capabilities.SupportsMediaControl) |
| | | 225 | | { |
| | 15 | 226 | | return false; |
| | | 227 | | } |
| | | 228 | | |
| | 0 | 229 | | var controllers = SessionControllers; |
| | 0 | 230 | | foreach (var controller in controllers) |
| | | 231 | | { |
| | 0 | 232 | | if (controller.SupportsMediaControl) |
| | | 233 | | { |
| | 0 | 234 | | return true; |
| | | 235 | | } |
| | | 236 | | } |
| | | 237 | | |
| | 0 | 238 | | return false; |
| | 0 | 239 | | } |
| | | 240 | | } |
| | | 241 | | |
| | | 242 | | /// <summary> |
| | | 243 | | /// Gets a value indicating whether the session supports remote control. |
| | | 244 | | /// </summary> |
| | | 245 | | /// <value><c>true</c> if this session supports remote control; otherwise, <c>false</c>.</value> |
| | | 246 | | public bool SupportsRemoteControl |
| | | 247 | | { |
| | | 248 | | get |
| | | 249 | | { |
| | 15 | 250 | | if (Capabilities is null || !Capabilities.SupportsMediaControl) |
| | | 251 | | { |
| | 15 | 252 | | return false; |
| | | 253 | | } |
| | | 254 | | |
| | 0 | 255 | | var controllers = SessionControllers; |
| | 0 | 256 | | foreach (var controller in controllers) |
| | | 257 | | { |
| | 0 | 258 | | if (controller.SupportsMediaControl) |
| | | 259 | | { |
| | 0 | 260 | | return true; |
| | | 261 | | } |
| | | 262 | | } |
| | | 263 | | |
| | 0 | 264 | | return false; |
| | 0 | 265 | | } |
| | | 266 | | } |
| | | 267 | | |
| | | 268 | | /// <summary> |
| | | 269 | | /// Gets or sets the now playing queue. |
| | | 270 | | /// </summary> |
| | | 271 | | /// <value>The now playing queue.</value> |
| | | 272 | | public IReadOnlyList<QueueItem> NowPlayingQueue { get; set; } |
| | | 273 | | |
| | | 274 | | /// <summary> |
| | | 275 | | /// Gets or sets the now playing queue full items. |
| | | 276 | | /// </summary> |
| | | 277 | | /// <value>The now playing queue full items.</value> |
| | | 278 | | public IReadOnlyList<BaseItemDto> NowPlayingQueueFullItems { get; set; } |
| | | 279 | | |
| | | 280 | | /// <summary> |
| | | 281 | | /// Gets or sets a value indicating whether the session has a custom device name. |
| | | 282 | | /// </summary> |
| | | 283 | | /// <value><c>true</c> if this session has a custom device name; otherwise, <c>false</c>.</value> |
| | | 284 | | public bool HasCustomDeviceName { get; set; } |
| | | 285 | | |
| | | 286 | | /// <summary> |
| | | 287 | | /// Gets or sets the playlist item id. |
| | | 288 | | /// </summary> |
| | | 289 | | /// <value>The playlist item id.</value> |
| | | 290 | | public string PlaylistItemId { get; set; } |
| | | 291 | | |
| | | 292 | | /// <summary> |
| | | 293 | | /// Gets or sets the server id. |
| | | 294 | | /// </summary> |
| | | 295 | | /// <value>The server id.</value> |
| | | 296 | | public string ServerId { get; set; } |
| | | 297 | | |
| | | 298 | | /// <summary> |
| | | 299 | | /// Gets or sets the user primary image tag. |
| | | 300 | | /// </summary> |
| | | 301 | | /// <value>The user primary image tag.</value> |
| | | 302 | | public string UserPrimaryImageTag { get; set; } |
| | | 303 | | |
| | | 304 | | /// <summary> |
| | | 305 | | /// Gets the supported commands. |
| | | 306 | | /// </summary> |
| | | 307 | | /// <value>The supported commands.</value> |
| | | 308 | | public IReadOnlyList<GeneralCommandType> SupportedCommands |
| | 15 | 309 | | => Capabilities is null ? [] : Capabilities.SupportedCommands; |
| | | 310 | | |
| | | 311 | | /// <summary> |
| | | 312 | | /// Ensures a controller of type exists. |
| | | 313 | | /// </summary> |
| | | 314 | | /// <typeparam name="T">Class to register.</typeparam> |
| | | 315 | | /// <param name="factory">The factory.</param> |
| | | 316 | | /// <returns>Tuple{ISessionController, bool}.</returns> |
| | | 317 | | public Tuple<ISessionController, bool> EnsureController<T>(Func<SessionInfo, ISessionController> factory) |
| | | 318 | | { |
| | 0 | 319 | | var controllers = SessionControllers.ToList(); |
| | 0 | 320 | | foreach (var controller in controllers) |
| | | 321 | | { |
| | 0 | 322 | | if (controller is T) |
| | | 323 | | { |
| | 0 | 324 | | return new Tuple<ISessionController, bool>(controller, false); |
| | | 325 | | } |
| | | 326 | | } |
| | | 327 | | |
| | 0 | 328 | | var newController = factory(this); |
| | 0 | 329 | | _logger.LogDebug("Creating new {Factory}", newController.GetType().Name); |
| | 0 | 330 | | controllers.Add(newController); |
| | | 331 | | |
| | 0 | 332 | | SessionControllers = [.. controllers]; |
| | 0 | 333 | | return new Tuple<ISessionController, bool>(newController, true); |
| | 0 | 334 | | } |
| | | 335 | | |
| | | 336 | | /// <summary> |
| | | 337 | | /// Adds a controller to the session. |
| | | 338 | | /// </summary> |
| | | 339 | | /// <param name="controller">The controller.</param> |
| | | 340 | | public void AddController(ISessionController controller) |
| | | 341 | | { |
| | 0 | 342 | | SessionControllers = [.. SessionControllers, controller]; |
| | 0 | 343 | | } |
| | | 344 | | |
| | | 345 | | /// <summary> |
| | | 346 | | /// Gets a value indicating whether the session contains a user. |
| | | 347 | | /// </summary> |
| | | 348 | | /// <param name="userId">The user id to check.</param> |
| | | 349 | | /// <returns><c>true</c> if this session contains the user; otherwise, <c>false</c>.</returns> |
| | | 350 | | public bool ContainsUser(Guid userId) |
| | | 351 | | { |
| | 0 | 352 | | if (UserId.Equals(userId)) |
| | | 353 | | { |
| | 0 | 354 | | return true; |
| | | 355 | | } |
| | | 356 | | |
| | 0 | 357 | | foreach (var additionalUser in AdditionalUsers) |
| | | 358 | | { |
| | 0 | 359 | | if (additionalUser.UserId.Equals(userId)) |
| | | 360 | | { |
| | 0 | 361 | | return true; |
| | | 362 | | } |
| | | 363 | | } |
| | | 364 | | |
| | 0 | 365 | | return false; |
| | 0 | 366 | | } |
| | | 367 | | |
| | | 368 | | /// <summary> |
| | | 369 | | /// Starts automatic progressing. |
| | | 370 | | /// </summary> |
| | | 371 | | /// <param name="progressInfo">The playback progress info.</param> |
| | | 372 | | /// <value>The supported commands.</value> |
| | | 373 | | public void StartAutomaticProgress(PlaybackProgressInfo progressInfo) |
| | | 374 | | { |
| | 0 | 375 | | if (_disposed) |
| | | 376 | | { |
| | 0 | 377 | | return; |
| | | 378 | | } |
| | | 379 | | |
| | | 380 | | lock (_progressLock) |
| | | 381 | | { |
| | 0 | 382 | | _lastProgressInfo = progressInfo; |
| | | 383 | | |
| | 0 | 384 | | if (_progressTimer is null) |
| | | 385 | | { |
| | 0 | 386 | | _progressTimer = new Timer(OnProgressTimerCallback, null, 1000, 1000); |
| | | 387 | | } |
| | | 388 | | else |
| | | 389 | | { |
| | 0 | 390 | | _progressTimer.Change(1000, 1000); |
| | | 391 | | } |
| | 0 | 392 | | } |
| | 0 | 393 | | } |
| | | 394 | | |
| | | 395 | | private async void OnProgressTimerCallback(object state) |
| | | 396 | | { |
| | | 397 | | if (_disposed) |
| | | 398 | | { |
| | | 399 | | return; |
| | | 400 | | } |
| | | 401 | | |
| | | 402 | | var progressInfo = _lastProgressInfo; |
| | | 403 | | if (progressInfo is null) |
| | | 404 | | { |
| | | 405 | | return; |
| | | 406 | | } |
| | | 407 | | |
| | | 408 | | if (progressInfo.IsPaused) |
| | | 409 | | { |
| | | 410 | | return; |
| | | 411 | | } |
| | | 412 | | |
| | | 413 | | var positionTicks = progressInfo.PositionTicks ?? 0; |
| | | 414 | | if (positionTicks < 0) |
| | | 415 | | { |
| | | 416 | | positionTicks = 0; |
| | | 417 | | } |
| | | 418 | | |
| | | 419 | | var newPositionTicks = positionTicks + ProgressIncrement; |
| | | 420 | | var item = progressInfo.Item; |
| | | 421 | | long? runtimeTicks = item?.RunTimeTicks; |
| | | 422 | | |
| | | 423 | | // Don't report beyond the runtime |
| | | 424 | | if (runtimeTicks.HasValue && newPositionTicks >= runtimeTicks.Value) |
| | | 425 | | { |
| | | 426 | | return; |
| | | 427 | | } |
| | | 428 | | |
| | | 429 | | progressInfo.PositionTicks = newPositionTicks; |
| | | 430 | | |
| | | 431 | | try |
| | | 432 | | { |
| | | 433 | | await _sessionManager.OnPlaybackProgress(progressInfo, true).ConfigureAwait(false); |
| | | 434 | | } |
| | | 435 | | catch (Exception ex) |
| | | 436 | | { |
| | | 437 | | _logger.LogError(ex, "Error reporting playback progress"); |
| | | 438 | | } |
| | | 439 | | } |
| | | 440 | | |
| | | 441 | | /// <summary> |
| | | 442 | | /// Stops automatic progressing. |
| | | 443 | | /// </summary> |
| | | 444 | | public void StopAutomaticProgress() |
| | 15 | 445 | | { |
| | | 446 | | lock (_progressLock) |
| | | 447 | | { |
| | 15 | 448 | | if (_progressTimer is not null) |
| | | 449 | | { |
| | 0 | 450 | | _progressTimer.Dispose(); |
| | 0 | 451 | | _progressTimer = null; |
| | | 452 | | } |
| | | 453 | | |
| | 15 | 454 | | _lastProgressInfo = null; |
| | 15 | 455 | | } |
| | 15 | 456 | | } |
| | | 457 | | |
| | | 458 | | /// <summary> |
| | | 459 | | /// Disposes the instance async. |
| | | 460 | | /// </summary> |
| | | 461 | | /// <returns>ValueTask.</returns> |
| | | 462 | | public async ValueTask DisposeAsync() |
| | | 463 | | { |
| | | 464 | | _disposed = true; |
| | | 465 | | |
| | | 466 | | StopAutomaticProgress(); |
| | | 467 | | |
| | | 468 | | var controllers = SessionControllers.ToList(); |
| | | 469 | | SessionControllers = []; |
| | | 470 | | |
| | | 471 | | foreach (var controller in controllers) |
| | | 472 | | { |
| | | 473 | | if (controller is IAsyncDisposable disposableAsync) |
| | | 474 | | { |
| | | 475 | | _logger.LogDebug("Disposing session controller asynchronously {TypeName}", disposableAsync.GetType() |
| | | 476 | | await disposableAsync.DisposeAsync().ConfigureAwait(false); |
| | | 477 | | } |
| | | 478 | | else if (controller is IDisposable disposable) |
| | | 479 | | { |
| | | 480 | | _logger.LogDebug("Disposing session controller synchronously {TypeName}", disposable.GetType().Name) |
| | | 481 | | disposable.Dispose(); |
| | | 482 | | } |
| | | 483 | | } |
| | | 484 | | } |
| | | 485 | | } |
| | | 486 | | } |