jellyfin/Jellyfin.Api/Controllers/SyncPlayController.cs

206 lines
9.0 KiB
C#
Raw Normal View History

2020-07-15 07:15:17 -07:00
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Threading;
using Jellyfin.Api.Constants;
using Jellyfin.Api.Helpers;
using MediaBrowser.Controller.Net;
using MediaBrowser.Controller.Session;
using MediaBrowser.Controller.SyncPlay;
using MediaBrowser.Model.SyncPlay;
using Microsoft.AspNetCore.Authorization;
2020-07-22 01:20:51 -07:00
using Microsoft.AspNetCore.Http;
2020-07-15 07:15:17 -07:00
using Microsoft.AspNetCore.Mvc;
namespace Jellyfin.Api.Controllers
{
/// <summary>
/// The sync play controller.
/// </summary>
[Authorize(Policy = Policies.DefaultAuthorization)]
public class SyncPlayController : BaseJellyfinApiController
{
private readonly ISessionManager _sessionManager;
private readonly IAuthorizationContext _authorizationContext;
private readonly ISyncPlayManager _syncPlayManager;
/// <summary>
/// Initializes a new instance of the <see cref="SyncPlayController"/> class.
/// </summary>
/// <param name="sessionManager">Instance of the <see cref="ISessionManager"/> interface.</param>
/// <param name="authorizationContext">Instance of the <see cref="IAuthorizationContext"/> interface.</param>
/// <param name="syncPlayManager">Instance of the <see cref="ISyncPlayManager"/> interface.</param>
public SyncPlayController(
ISessionManager sessionManager,
IAuthorizationContext authorizationContext,
ISyncPlayManager syncPlayManager)
{
_sessionManager = sessionManager;
_authorizationContext = authorizationContext;
_syncPlayManager = syncPlayManager;
}
/// <summary>
/// Create a new SyncPlay group.
/// </summary>
2020-07-22 01:20:51 -07:00
/// <response code="204">New group created.</response>
2020-07-15 07:15:17 -07:00
/// <returns>A <see cref="NoContentResult"/> indicating success.</returns>
[HttpPost("New")]
2020-07-22 01:20:51 -07:00
[ProducesResponseType(StatusCodes.Status204NoContent)]
2020-08-03 13:38:51 -07:00
public ActionResult SyncPlayCreateGroup()
2020-07-15 07:15:17 -07:00
{
var currentSession = RequestHelpers.GetSession(_sessionManager, _authorizationContext, Request);
_syncPlayManager.NewGroup(currentSession, CancellationToken.None);
return NoContent();
}
/// <summary>
/// Join an existing SyncPlay group.
/// </summary>
/// <param name="groupId">The sync play group id.</param>
2020-07-22 01:20:51 -07:00
/// <response code="204">Group join successful.</response>
2020-07-15 07:15:17 -07:00
/// <returns>A <see cref="NoContentResult"/> indicating success.</returns>
[HttpPost("Join")]
2020-07-22 01:20:51 -07:00
[ProducesResponseType(StatusCodes.Status204NoContent)]
2020-08-03 13:38:51 -07:00
public ActionResult SyncPlayJoinGroup([FromQuery, Required] Guid groupId)
2020-07-15 07:15:17 -07:00
{
var currentSession = RequestHelpers.GetSession(_sessionManager, _authorizationContext, Request);
var joinRequest = new JoinGroupRequest()
{
GroupId = groupId
};
_syncPlayManager.JoinGroup(currentSession, groupId, joinRequest, CancellationToken.None);
return NoContent();
}
/// <summary>
/// Leave the joined SyncPlay group.
/// </summary>
2020-07-22 01:20:51 -07:00
/// <response code="204">Group leave successful.</response>
2020-07-15 07:15:17 -07:00
/// <returns>A <see cref="NoContentResult"/> indicating success.</returns>
[HttpPost("Leave")]
2020-07-22 01:20:51 -07:00
[ProducesResponseType(StatusCodes.Status204NoContent)]
2020-08-03 13:38:51 -07:00
public ActionResult SyncPlayLeaveGroup()
2020-07-15 07:15:17 -07:00
{
var currentSession = RequestHelpers.GetSession(_sessionManager, _authorizationContext, Request);
_syncPlayManager.LeaveGroup(currentSession, CancellationToken.None);
return NoContent();
}
/// <summary>
/// Gets all SyncPlay groups.
/// </summary>
/// <param name="filterItemId">Optional. Filter by item id.</param>
2020-07-22 01:20:51 -07:00
/// <response code="200">Groups returned.</response>
2020-08-06 07:17:45 -07:00
/// <returns>An <see cref="IEnumerable{GroupInfoView}"/> containing the available SyncPlay groups.</returns>
2020-07-15 07:15:17 -07:00
[HttpGet("List")]
2020-07-22 01:20:51 -07:00
[ProducesResponseType(StatusCodes.Status200OK)]
2020-08-03 13:38:51 -07:00
public ActionResult<IEnumerable<GroupInfoView>> SyncPlayGetGroups([FromQuery] Guid? filterItemId)
2020-07-15 07:15:17 -07:00
{
var currentSession = RequestHelpers.GetSession(_sessionManager, _authorizationContext, Request);
return Ok(_syncPlayManager.ListGroups(currentSession, filterItemId.HasValue ? filterItemId.Value : Guid.Empty));
}
/// <summary>
/// Request play in SyncPlay group.
/// </summary>
2020-07-22 01:20:51 -07:00
/// <response code="204">Play request sent to all group members.</response>
2020-07-15 07:15:17 -07:00
/// <returns>A <see cref="NoContentResult"/> indicating success.</returns>
[HttpPost("Play")]
2020-07-22 01:20:51 -07:00
[ProducesResponseType(StatusCodes.Status204NoContent)]
2020-08-03 13:38:51 -07:00
public ActionResult SyncPlayPlay()
2020-07-15 07:15:17 -07:00
{
var currentSession = RequestHelpers.GetSession(_sessionManager, _authorizationContext, Request);
var syncPlayRequest = new PlaybackRequest()
{
Type = PlaybackRequestType.Play
};
_syncPlayManager.HandleRequest(currentSession, syncPlayRequest, CancellationToken.None);
return NoContent();
}
/// <summary>
/// Request pause in SyncPlay group.
/// </summary>
2020-07-22 01:20:51 -07:00
/// <response code="204">Pause request sent to all group members.</response>
2020-07-15 07:15:17 -07:00
/// <returns>A <see cref="NoContentResult"/> indicating success.</returns>
[HttpPost("Pause")]
2020-07-22 01:20:51 -07:00
[ProducesResponseType(StatusCodes.Status204NoContent)]
2020-08-03 13:38:51 -07:00
public ActionResult SyncPlayPause()
2020-07-15 07:15:17 -07:00
{
var currentSession = RequestHelpers.GetSession(_sessionManager, _authorizationContext, Request);
var syncPlayRequest = new PlaybackRequest()
{
Type = PlaybackRequestType.Pause
};
_syncPlayManager.HandleRequest(currentSession, syncPlayRequest, CancellationToken.None);
return NoContent();
}
/// <summary>
/// Request seek in SyncPlay group.
/// </summary>
/// <param name="positionTicks">The playback position in ticks.</param>
2020-07-22 01:20:51 -07:00
/// <response code="204">Seek request sent to all group members.</response>
2020-07-15 07:15:17 -07:00
/// <returns>A <see cref="NoContentResult"/> indicating success.</returns>
[HttpPost("Seek")]
2020-07-22 01:20:51 -07:00
[ProducesResponseType(StatusCodes.Status204NoContent)]
2020-08-03 13:38:51 -07:00
public ActionResult SyncPlaySeek([FromQuery] long positionTicks)
2020-07-15 07:15:17 -07:00
{
var currentSession = RequestHelpers.GetSession(_sessionManager, _authorizationContext, Request);
var syncPlayRequest = new PlaybackRequest()
{
Type = PlaybackRequestType.Seek,
PositionTicks = positionTicks
};
_syncPlayManager.HandleRequest(currentSession, syncPlayRequest, CancellationToken.None);
return NoContent();
}
/// <summary>
/// Request group wait in SyncPlay group while buffering.
/// </summary>
/// <param name="when">When the request has been made by the client.</param>
/// <param name="positionTicks">The playback position in ticks.</param>
/// <param name="bufferingDone">Whether the buffering is done.</param>
2020-07-22 01:20:51 -07:00
/// <response code="204">Buffering request sent to all group members.</response>
2020-07-15 07:15:17 -07:00
/// <returns>A <see cref="NoContentResult"/> indicating success.</returns>
[HttpPost("Buffering")]
2020-07-22 01:20:51 -07:00
[ProducesResponseType(StatusCodes.Status204NoContent)]
2020-08-03 13:38:51 -07:00
public ActionResult SyncPlayBuffering([FromQuery] DateTime when, [FromQuery] long positionTicks, [FromQuery] bool bufferingDone)
2020-07-15 07:15:17 -07:00
{
var currentSession = RequestHelpers.GetSession(_sessionManager, _authorizationContext, Request);
var syncPlayRequest = new PlaybackRequest()
{
2020-07-21 13:06:07 -07:00
Type = bufferingDone ? PlaybackRequestType.Ready : PlaybackRequestType.Buffer,
2020-07-15 07:15:17 -07:00
When = when,
PositionTicks = positionTicks
};
_syncPlayManager.HandleRequest(currentSession, syncPlayRequest, CancellationToken.None);
return NoContent();
}
/// <summary>
/// Update session ping.
/// </summary>
/// <param name="ping">The ping.</param>
2020-07-22 01:20:51 -07:00
/// <response code="204">Ping updated.</response>
2020-07-15 07:15:17 -07:00
/// <returns>A <see cref="NoContentResult"/> indicating success.</returns>
[HttpPost("Ping")]
2020-07-22 01:20:51 -07:00
[ProducesResponseType(StatusCodes.Status204NoContent)]
2020-08-03 13:38:51 -07:00
public ActionResult SyncPlayPing([FromQuery] double ping)
2020-07-15 07:15:17 -07:00
{
var currentSession = RequestHelpers.GetSession(_sessionManager, _authorizationContext, Request);
var syncPlayRequest = new PlaybackRequest()
{
2020-07-21 13:06:07 -07:00
Type = PlaybackRequestType.Ping,
2020-07-15 07:15:17 -07:00
Ping = Convert.ToInt64(ping)
};
_syncPlayManager.HandleRequest(currentSession, syncPlayRequest, CancellationToken.None);
return NoContent();
}
}
}