PlaystateController.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. using System;
  2. using System.ComponentModel.DataAnnotations;
  3. using System.Diagnostics.CodeAnalysis;
  4. using System.Threading.Tasks;
  5. using Jellyfin.Api.Extensions;
  6. using Jellyfin.Api.Helpers;
  7. using Jellyfin.Api.ModelBinders;
  8. using Jellyfin.Data.Entities;
  9. using Jellyfin.Extensions;
  10. using MediaBrowser.Controller.Entities;
  11. using MediaBrowser.Controller.Library;
  12. using MediaBrowser.Controller.MediaEncoding;
  13. using MediaBrowser.Controller.Session;
  14. using MediaBrowser.Model.Dto;
  15. using MediaBrowser.Model.Session;
  16. using Microsoft.AspNetCore.Authorization;
  17. using Microsoft.AspNetCore.Http;
  18. using Microsoft.AspNetCore.Mvc;
  19. using Microsoft.Extensions.Logging;
  20. namespace Jellyfin.Api.Controllers;
  21. /// <summary>
  22. /// Playstate controller.
  23. /// </summary>
  24. [Route("")]
  25. [Authorize]
  26. public class PlaystateController : BaseJellyfinApiController
  27. {
  28. private readonly IUserManager _userManager;
  29. private readonly IUserDataManager _userDataRepository;
  30. private readonly ILibraryManager _libraryManager;
  31. private readonly ISessionManager _sessionManager;
  32. private readonly ILogger<PlaystateController> _logger;
  33. private readonly ITranscodeManager _transcodeManager;
  34. /// <summary>
  35. /// Initializes a new instance of the <see cref="PlaystateController"/> class.
  36. /// </summary>
  37. /// <param name="userManager">Instance of the <see cref="IUserManager"/> interface.</param>
  38. /// <param name="userDataRepository">Instance of the <see cref="IUserDataManager"/> interface.</param>
  39. /// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param>
  40. /// <param name="sessionManager">Instance of the <see cref="ISessionManager"/> interface.</param>
  41. /// <param name="loggerFactory">Instance of the <see cref="ILoggerFactory"/> interface.</param>
  42. /// <param name="transcodeManager">Instance of the <see cref="ITranscodeManager"/> interface.</param>
  43. public PlaystateController(
  44. IUserManager userManager,
  45. IUserDataManager userDataRepository,
  46. ILibraryManager libraryManager,
  47. ISessionManager sessionManager,
  48. ILoggerFactory loggerFactory,
  49. ITranscodeManager transcodeManager)
  50. {
  51. _userManager = userManager;
  52. _userDataRepository = userDataRepository;
  53. _libraryManager = libraryManager;
  54. _sessionManager = sessionManager;
  55. _logger = loggerFactory.CreateLogger<PlaystateController>();
  56. _transcodeManager = transcodeManager;
  57. }
  58. /// <summary>
  59. /// Marks an item as played for user.
  60. /// </summary>
  61. /// <param name="userId">User id.</param>
  62. /// <param name="itemId">Item id.</param>
  63. /// <param name="datePlayed">Optional. The date the item was played.</param>
  64. /// <response code="200">Item marked as played.</response>
  65. /// <response code="404">Item not found.</response>
  66. /// <returns>An <see cref="OkResult"/> containing the <see cref="UserItemDataDto"/>, or a <see cref="NotFoundResult"/> if item was not found.</returns>
  67. [HttpPost("UserPlayedItems/{itemId}")]
  68. [ProducesResponseType(StatusCodes.Status200OK)]
  69. [ProducesResponseType(StatusCodes.Status404NotFound)]
  70. public async Task<ActionResult<UserItemDataDto?>> MarkPlayedItem(
  71. [FromQuery] Guid? userId,
  72. [FromRoute, Required] Guid itemId,
  73. [FromQuery, ModelBinder(typeof(LegacyDateTimeModelBinder))] DateTime? datePlayed)
  74. {
  75. userId = RequestHelpers.GetUserId(User, userId);
  76. var user = _userManager.GetUserById(userId.Value);
  77. if (user is null)
  78. {
  79. return NotFound();
  80. }
  81. var item = _libraryManager.GetItemById<BaseItem>(itemId, user);
  82. if (item is null)
  83. {
  84. return NotFound();
  85. }
  86. var session = await RequestHelpers.GetSession(_sessionManager, _userManager, HttpContext, userId).ConfigureAwait(false);
  87. var dto = UpdatePlayedStatus(user, item, true, datePlayed);
  88. foreach (var additionalUserInfo in session.AdditionalUsers)
  89. {
  90. var additionalUser = _userManager.GetUserById(additionalUserInfo.UserId);
  91. if (additionalUser is null)
  92. {
  93. return NotFound();
  94. }
  95. UpdatePlayedStatus(additionalUser, item, true, datePlayed);
  96. }
  97. return dto;
  98. }
  99. /// <summary>
  100. /// Marks an item as played for user.
  101. /// </summary>
  102. /// <param name="userId">User id.</param>
  103. /// <param name="itemId">Item id.</param>
  104. /// <param name="datePlayed">Optional. The date the item was played.</param>
  105. /// <response code="200">Item marked as played.</response>
  106. /// <response code="404">Item not found.</response>
  107. /// <returns>An <see cref="OkResult"/> containing the <see cref="UserItemDataDto"/>, or a <see cref="NotFoundResult"/> if item was not found.</returns>
  108. [HttpPost("Users/{userId}/PlayedItems/{itemId}")]
  109. [ProducesResponseType(StatusCodes.Status200OK)]
  110. [ProducesResponseType(StatusCodes.Status404NotFound)]
  111. [Obsolete("Kept for backwards compatibility")]
  112. [ApiExplorerSettings(IgnoreApi = true)]
  113. public Task<ActionResult<UserItemDataDto?>> MarkPlayedItemLegacy(
  114. [FromRoute, Required] Guid userId,
  115. [FromRoute, Required] Guid itemId,
  116. [FromQuery, ModelBinder(typeof(LegacyDateTimeModelBinder))] DateTime? datePlayed)
  117. => MarkPlayedItem(userId, itemId, datePlayed);
  118. /// <summary>
  119. /// Marks an item as unplayed for user.
  120. /// </summary>
  121. /// <param name="userId">User id.</param>
  122. /// <param name="itemId">Item id.</param>
  123. /// <response code="200">Item marked as unplayed.</response>
  124. /// <response code="404">Item not found.</response>
  125. /// <returns>A <see cref="OkResult"/> containing the <see cref="UserItemDataDto"/>, or a <see cref="NotFoundResult"/> if item was not found.</returns>
  126. [HttpDelete("UserPlayedItems/{itemId}")]
  127. [ProducesResponseType(StatusCodes.Status200OK)]
  128. [ProducesResponseType(StatusCodes.Status404NotFound)]
  129. public async Task<ActionResult<UserItemDataDto?>> MarkUnplayedItem(
  130. [FromQuery] Guid? userId,
  131. [FromRoute, Required] Guid itemId)
  132. {
  133. userId = RequestHelpers.GetUserId(User, userId);
  134. var user = _userManager.GetUserById(userId.Value);
  135. if (user is null)
  136. {
  137. return NotFound();
  138. }
  139. var item = _libraryManager.GetItemById<BaseItem>(itemId, user);
  140. if (item is null)
  141. {
  142. return NotFound();
  143. }
  144. var session = await RequestHelpers.GetSession(_sessionManager, _userManager, HttpContext, userId).ConfigureAwait(false);
  145. var dto = UpdatePlayedStatus(user, item, false, null);
  146. foreach (var additionalUserInfo in session.AdditionalUsers)
  147. {
  148. var additionalUser = _userManager.GetUserById(additionalUserInfo.UserId);
  149. if (additionalUser is null)
  150. {
  151. return NotFound();
  152. }
  153. UpdatePlayedStatus(additionalUser, item, false, null);
  154. }
  155. return dto;
  156. }
  157. /// <summary>
  158. /// Marks an item as unplayed for user.
  159. /// </summary>
  160. /// <param name="userId">User id.</param>
  161. /// <param name="itemId">Item id.</param>
  162. /// <response code="200">Item marked as unplayed.</response>
  163. /// <response code="404">Item not found.</response>
  164. /// <returns>A <see cref="OkResult"/> containing the <see cref="UserItemDataDto"/>, or a <see cref="NotFoundResult"/> if item was not found.</returns>
  165. [HttpDelete("Users/{userId}/PlayedItems/{itemId}")]
  166. [ProducesResponseType(StatusCodes.Status200OK)]
  167. [ProducesResponseType(StatusCodes.Status404NotFound)]
  168. [Obsolete("Kept for backwards compatibility")]
  169. [ApiExplorerSettings(IgnoreApi = true)]
  170. public Task<ActionResult<UserItemDataDto?>> MarkUnplayedItemLegacy(
  171. [FromRoute, Required] Guid userId,
  172. [FromRoute, Required] Guid itemId)
  173. => MarkUnplayedItem(userId, itemId);
  174. /// <summary>
  175. /// Reports playback has started within a session.
  176. /// </summary>
  177. /// <param name="playbackStartInfo">The playback start info.</param>
  178. /// <response code="204">Playback start recorded.</response>
  179. /// <returns>A <see cref="NoContentResult"/>.</returns>
  180. [HttpPost("Sessions/Playing")]
  181. [ProducesResponseType(StatusCodes.Status204NoContent)]
  182. public async Task<ActionResult> ReportPlaybackStart([FromBody] PlaybackStartInfo playbackStartInfo)
  183. {
  184. playbackStartInfo.PlayMethod = ValidatePlayMethod(playbackStartInfo.PlayMethod, playbackStartInfo.PlaySessionId);
  185. playbackStartInfo.SessionId = await RequestHelpers.GetSessionId(_sessionManager, _userManager, HttpContext).ConfigureAwait(false);
  186. await _sessionManager.OnPlaybackStart(playbackStartInfo).ConfigureAwait(false);
  187. return NoContent();
  188. }
  189. /// <summary>
  190. /// Reports playback progress within a session.
  191. /// </summary>
  192. /// <param name="playbackProgressInfo">The playback progress info.</param>
  193. /// <response code="204">Playback progress recorded.</response>
  194. /// <returns>A <see cref="NoContentResult"/>.</returns>
  195. [HttpPost("Sessions/Playing/Progress")]
  196. [ProducesResponseType(StatusCodes.Status204NoContent)]
  197. public async Task<ActionResult> ReportPlaybackProgress([FromBody] PlaybackProgressInfo playbackProgressInfo)
  198. {
  199. playbackProgressInfo.PlayMethod = ValidatePlayMethod(playbackProgressInfo.PlayMethod, playbackProgressInfo.PlaySessionId);
  200. playbackProgressInfo.SessionId = await RequestHelpers.GetSessionId(_sessionManager, _userManager, HttpContext).ConfigureAwait(false);
  201. await _sessionManager.OnPlaybackProgress(playbackProgressInfo).ConfigureAwait(false);
  202. return NoContent();
  203. }
  204. /// <summary>
  205. /// Pings a playback session.
  206. /// </summary>
  207. /// <param name="playSessionId">Playback session id.</param>
  208. /// <response code="204">Playback session pinged.</response>
  209. /// <returns>A <see cref="NoContentResult"/>.</returns>
  210. [HttpPost("Sessions/Playing/Ping")]
  211. [ProducesResponseType(StatusCodes.Status204NoContent)]
  212. public ActionResult PingPlaybackSession([FromQuery, Required] string playSessionId)
  213. {
  214. _transcodeManager.PingTranscodingJob(playSessionId, null);
  215. return NoContent();
  216. }
  217. /// <summary>
  218. /// Reports playback has stopped within a session.
  219. /// </summary>
  220. /// <param name="playbackStopInfo">The playback stop info.</param>
  221. /// <response code="204">Playback stop recorded.</response>
  222. /// <returns>A <see cref="NoContentResult"/>.</returns>
  223. [HttpPost("Sessions/Playing/Stopped")]
  224. [ProducesResponseType(StatusCodes.Status204NoContent)]
  225. public async Task<ActionResult> ReportPlaybackStopped([FromBody] PlaybackStopInfo playbackStopInfo)
  226. {
  227. _logger.LogDebug("ReportPlaybackStopped PlaySessionId: {0}", playbackStopInfo.PlaySessionId ?? string.Empty);
  228. if (!string.IsNullOrWhiteSpace(playbackStopInfo.PlaySessionId))
  229. {
  230. await _transcodeManager.KillTranscodingJobs(User.GetDeviceId()!, playbackStopInfo.PlaySessionId, s => true).ConfigureAwait(false);
  231. }
  232. playbackStopInfo.SessionId = await RequestHelpers.GetSessionId(_sessionManager, _userManager, HttpContext).ConfigureAwait(false);
  233. await _sessionManager.OnPlaybackStopped(playbackStopInfo).ConfigureAwait(false);
  234. return NoContent();
  235. }
  236. /// <summary>
  237. /// Reports that a session has begun playing an item.
  238. /// </summary>
  239. /// <param name="itemId">Item id.</param>
  240. /// <param name="mediaSourceId">The id of the MediaSource.</param>
  241. /// <param name="audioStreamIndex">The audio stream index.</param>
  242. /// <param name="subtitleStreamIndex">The subtitle stream index.</param>
  243. /// <param name="playMethod">The play method.</param>
  244. /// <param name="liveStreamId">The live stream id.</param>
  245. /// <param name="playSessionId">The play session id.</param>
  246. /// <param name="canSeek">Indicates if the client can seek.</param>
  247. /// <response code="204">Play start recorded.</response>
  248. /// <returns>A <see cref="NoContentResult"/>.</returns>
  249. [HttpPost("PlayingItems/{itemId}")]
  250. [ProducesResponseType(StatusCodes.Status204NoContent)]
  251. public async Task<ActionResult> OnPlaybackStart(
  252. [FromRoute, Required] Guid itemId,
  253. [FromQuery] string? mediaSourceId,
  254. [FromQuery] int? audioStreamIndex,
  255. [FromQuery] int? subtitleStreamIndex,
  256. [FromQuery] PlayMethod? playMethod,
  257. [FromQuery] string? liveStreamId,
  258. [FromQuery] string? playSessionId,
  259. [FromQuery] bool canSeek = false)
  260. {
  261. var playbackStartInfo = new PlaybackStartInfo
  262. {
  263. CanSeek = canSeek,
  264. ItemId = itemId,
  265. MediaSourceId = mediaSourceId,
  266. AudioStreamIndex = audioStreamIndex,
  267. SubtitleStreamIndex = subtitleStreamIndex,
  268. PlayMethod = playMethod ?? PlayMethod.Transcode,
  269. PlaySessionId = playSessionId,
  270. LiveStreamId = liveStreamId
  271. };
  272. playbackStartInfo.PlayMethod = ValidatePlayMethod(playbackStartInfo.PlayMethod, playbackStartInfo.PlaySessionId);
  273. playbackStartInfo.SessionId = await RequestHelpers.GetSessionId(_sessionManager, _userManager, HttpContext).ConfigureAwait(false);
  274. await _sessionManager.OnPlaybackStart(playbackStartInfo).ConfigureAwait(false);
  275. return NoContent();
  276. }
  277. /// <summary>
  278. /// Reports that a user has begun playing an item.
  279. /// </summary>
  280. /// <param name="userId">User id.</param>
  281. /// <param name="itemId">Item id.</param>
  282. /// <param name="mediaSourceId">The id of the MediaSource.</param>
  283. /// <param name="audioStreamIndex">The audio stream index.</param>
  284. /// <param name="subtitleStreamIndex">The subtitle stream index.</param>
  285. /// <param name="playMethod">The play method.</param>
  286. /// <param name="liveStreamId">The live stream id.</param>
  287. /// <param name="playSessionId">The play session id.</param>
  288. /// <param name="canSeek">Indicates if the client can seek.</param>
  289. /// <response code="204">Play start recorded.</response>
  290. /// <returns>A <see cref="NoContentResult"/>.</returns>
  291. [HttpPost("Users/{userId}/PlayingItems/{itemId}")]
  292. [ProducesResponseType(StatusCodes.Status204NoContent)]
  293. [Obsolete("Kept for backwards compatibility")]
  294. [ApiExplorerSettings(IgnoreApi = true)]
  295. [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "userId", Justification = "Required for ServiceStack")]
  296. public Task<ActionResult> OnPlaybackStartLegacy(
  297. [FromRoute, Required] Guid userId,
  298. [FromRoute, Required] Guid itemId,
  299. [FromQuery] string? mediaSourceId,
  300. [FromQuery] int? audioStreamIndex,
  301. [FromQuery] int? subtitleStreamIndex,
  302. [FromQuery] PlayMethod? playMethod,
  303. [FromQuery] string? liveStreamId,
  304. [FromQuery] string? playSessionId,
  305. [FromQuery] bool canSeek = false)
  306. => OnPlaybackStart(itemId, mediaSourceId, audioStreamIndex, subtitleStreamIndex, playMethod, liveStreamId, playSessionId, canSeek);
  307. /// <summary>
  308. /// Reports a session's playback progress.
  309. /// </summary>
  310. /// <param name="itemId">Item id.</param>
  311. /// <param name="mediaSourceId">The id of the MediaSource.</param>
  312. /// <param name="positionTicks">Optional. The current position, in ticks. 1 tick = 10000 ms.</param>
  313. /// <param name="audioStreamIndex">The audio stream index.</param>
  314. /// <param name="subtitleStreamIndex">The subtitle stream index.</param>
  315. /// <param name="volumeLevel">Scale of 0-100.</param>
  316. /// <param name="playMethod">The play method.</param>
  317. /// <param name="liveStreamId">The live stream id.</param>
  318. /// <param name="playSessionId">The play session id.</param>
  319. /// <param name="repeatMode">The repeat mode.</param>
  320. /// <param name="isPaused">Indicates if the player is paused.</param>
  321. /// <param name="isMuted">Indicates if the player is muted.</param>
  322. /// <response code="204">Play progress recorded.</response>
  323. /// <returns>A <see cref="NoContentResult"/>.</returns>
  324. [HttpPost("PlayingItems/{itemId}/Progress")]
  325. [ProducesResponseType(StatusCodes.Status204NoContent)]
  326. public async Task<ActionResult> OnPlaybackProgress(
  327. [FromRoute, Required] Guid itemId,
  328. [FromQuery] string? mediaSourceId,
  329. [FromQuery] long? positionTicks,
  330. [FromQuery] int? audioStreamIndex,
  331. [FromQuery] int? subtitleStreamIndex,
  332. [FromQuery] int? volumeLevel,
  333. [FromQuery] PlayMethod? playMethod,
  334. [FromQuery] string? liveStreamId,
  335. [FromQuery] string? playSessionId,
  336. [FromQuery] RepeatMode? repeatMode,
  337. [FromQuery] bool isPaused = false,
  338. [FromQuery] bool isMuted = false)
  339. {
  340. var playbackProgressInfo = new PlaybackProgressInfo
  341. {
  342. ItemId = itemId,
  343. PositionTicks = positionTicks,
  344. IsMuted = isMuted,
  345. IsPaused = isPaused,
  346. MediaSourceId = mediaSourceId,
  347. AudioStreamIndex = audioStreamIndex,
  348. SubtitleStreamIndex = subtitleStreamIndex,
  349. VolumeLevel = volumeLevel,
  350. PlayMethod = playMethod ?? PlayMethod.Transcode,
  351. PlaySessionId = playSessionId,
  352. LiveStreamId = liveStreamId,
  353. RepeatMode = repeatMode ?? RepeatMode.RepeatNone
  354. };
  355. playbackProgressInfo.PlayMethod = ValidatePlayMethod(playbackProgressInfo.PlayMethod, playbackProgressInfo.PlaySessionId);
  356. playbackProgressInfo.SessionId = await RequestHelpers.GetSessionId(_sessionManager, _userManager, HttpContext).ConfigureAwait(false);
  357. await _sessionManager.OnPlaybackProgress(playbackProgressInfo).ConfigureAwait(false);
  358. return NoContent();
  359. }
  360. /// <summary>
  361. /// Reports a user's playback progress.
  362. /// </summary>
  363. /// <param name="userId">User id.</param>
  364. /// <param name="itemId">Item id.</param>
  365. /// <param name="mediaSourceId">The id of the MediaSource.</param>
  366. /// <param name="positionTicks">Optional. The current position, in ticks. 1 tick = 10000 ms.</param>
  367. /// <param name="audioStreamIndex">The audio stream index.</param>
  368. /// <param name="subtitleStreamIndex">The subtitle stream index.</param>
  369. /// <param name="volumeLevel">Scale of 0-100.</param>
  370. /// <param name="playMethod">The play method.</param>
  371. /// <param name="liveStreamId">The live stream id.</param>
  372. /// <param name="playSessionId">The play session id.</param>
  373. /// <param name="repeatMode">The repeat mode.</param>
  374. /// <param name="isPaused">Indicates if the player is paused.</param>
  375. /// <param name="isMuted">Indicates if the player is muted.</param>
  376. /// <response code="204">Play progress recorded.</response>
  377. /// <returns>A <see cref="NoContentResult"/>.</returns>
  378. [HttpPost("Users/{userId}/PlayingItems/{itemId}/Progress")]
  379. [ProducesResponseType(StatusCodes.Status204NoContent)]
  380. [Obsolete("Kept for backwards compatibility")]
  381. [ApiExplorerSettings(IgnoreApi = true)]
  382. [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "userId", Justification = "Required for ServiceStack")]
  383. public Task<ActionResult> OnPlaybackProgressLegacy(
  384. [FromRoute, Required] Guid userId,
  385. [FromRoute, Required] Guid itemId,
  386. [FromQuery] string? mediaSourceId,
  387. [FromQuery] long? positionTicks,
  388. [FromQuery] int? audioStreamIndex,
  389. [FromQuery] int? subtitleStreamIndex,
  390. [FromQuery] int? volumeLevel,
  391. [FromQuery] PlayMethod? playMethod,
  392. [FromQuery] string? liveStreamId,
  393. [FromQuery] string? playSessionId,
  394. [FromQuery] RepeatMode? repeatMode,
  395. [FromQuery] bool isPaused = false,
  396. [FromQuery] bool isMuted = false)
  397. => OnPlaybackProgress(itemId, mediaSourceId, positionTicks, audioStreamIndex, subtitleStreamIndex, volumeLevel, playMethod, liveStreamId, playSessionId, repeatMode, isPaused, isMuted);
  398. /// <summary>
  399. /// Reports that a session has stopped playing an item.
  400. /// </summary>
  401. /// <param name="itemId">Item id.</param>
  402. /// <param name="mediaSourceId">The id of the MediaSource.</param>
  403. /// <param name="nextMediaType">The next media type that will play.</param>
  404. /// <param name="positionTicks">Optional. The position, in ticks, where playback stopped. 1 tick = 10000 ms.</param>
  405. /// <param name="liveStreamId">The live stream id.</param>
  406. /// <param name="playSessionId">The play session id.</param>
  407. /// <response code="204">Playback stop recorded.</response>
  408. /// <returns>A <see cref="NoContentResult"/>.</returns>
  409. [HttpDelete("PlayingItems/{itemId}")]
  410. [ProducesResponseType(StatusCodes.Status204NoContent)]
  411. public async Task<ActionResult> OnPlaybackStopped(
  412. [FromRoute, Required] Guid itemId,
  413. [FromQuery] string? mediaSourceId,
  414. [FromQuery] string? nextMediaType,
  415. [FromQuery] long? positionTicks,
  416. [FromQuery] string? liveStreamId,
  417. [FromQuery] string? playSessionId)
  418. {
  419. var playbackStopInfo = new PlaybackStopInfo
  420. {
  421. ItemId = itemId,
  422. PositionTicks = positionTicks,
  423. MediaSourceId = mediaSourceId,
  424. PlaySessionId = playSessionId,
  425. LiveStreamId = liveStreamId,
  426. NextMediaType = nextMediaType
  427. };
  428. _logger.LogDebug("ReportPlaybackStopped PlaySessionId: {0}", playbackStopInfo.PlaySessionId ?? string.Empty);
  429. if (!string.IsNullOrWhiteSpace(playbackStopInfo.PlaySessionId))
  430. {
  431. await _transcodeManager.KillTranscodingJobs(User.GetDeviceId()!, playbackStopInfo.PlaySessionId, s => true).ConfigureAwait(false);
  432. }
  433. playbackStopInfo.SessionId = await RequestHelpers.GetSessionId(_sessionManager, _userManager, HttpContext).ConfigureAwait(false);
  434. await _sessionManager.OnPlaybackStopped(playbackStopInfo).ConfigureAwait(false);
  435. return NoContent();
  436. }
  437. /// <summary>
  438. /// Reports that a user has stopped playing an item.
  439. /// </summary>
  440. /// <param name="userId">User id.</param>
  441. /// <param name="itemId">Item id.</param>
  442. /// <param name="mediaSourceId">The id of the MediaSource.</param>
  443. /// <param name="nextMediaType">The next media type that will play.</param>
  444. /// <param name="positionTicks">Optional. The position, in ticks, where playback stopped. 1 tick = 10000 ms.</param>
  445. /// <param name="liveStreamId">The live stream id.</param>
  446. /// <param name="playSessionId">The play session id.</param>
  447. /// <response code="204">Playback stop recorded.</response>
  448. /// <returns>A <see cref="NoContentResult"/>.</returns>
  449. [HttpDelete("Users/{userId}/PlayingItems/{itemId}")]
  450. [ProducesResponseType(StatusCodes.Status204NoContent)]
  451. [Obsolete("Kept for backwards compatibility")]
  452. [ApiExplorerSettings(IgnoreApi = true)]
  453. [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "userId", Justification = "Required for ServiceStack")]
  454. public Task<ActionResult> OnPlaybackStoppedLegacy(
  455. [FromRoute, Required] Guid userId,
  456. [FromRoute, Required] Guid itemId,
  457. [FromQuery] string? mediaSourceId,
  458. [FromQuery] string? nextMediaType,
  459. [FromQuery] long? positionTicks,
  460. [FromQuery] string? liveStreamId,
  461. [FromQuery] string? playSessionId)
  462. => OnPlaybackStopped(itemId, mediaSourceId, nextMediaType, positionTicks, liveStreamId, playSessionId);
  463. /// <summary>
  464. /// Updates the played status.
  465. /// </summary>
  466. /// <param name="user">The user.</param>
  467. /// <param name="item">The item.</param>
  468. /// <param name="wasPlayed">if set to <c>true</c> [was played].</param>
  469. /// <param name="datePlayed">The date played.</param>
  470. /// <returns>Task.</returns>
  471. private UserItemDataDto? UpdatePlayedStatus(User user, BaseItem item, bool wasPlayed, DateTime? datePlayed)
  472. {
  473. if (wasPlayed)
  474. {
  475. item.MarkPlayed(user, datePlayed, true);
  476. }
  477. else
  478. {
  479. item.MarkUnplayed(user);
  480. }
  481. return _userDataRepository.GetUserDataDto(item, user);
  482. }
  483. private PlayMethod ValidatePlayMethod(PlayMethod method, string? playSessionId)
  484. {
  485. if (method == PlayMethod.Transcode)
  486. {
  487. var job = string.IsNullOrWhiteSpace(playSessionId) ? null : _transcodeManager.GetTranscodingJob(playSessionId);
  488. if (job is null)
  489. {
  490. return PlayMethod.DirectPlay;
  491. }
  492. }
  493. return method;
  494. }
  495. }