jellyfin/Jellyfin.Api/Controllers/SyncPlayController.cs

206 lines
8.9 KiB
C#
Raw Normal View History

2020-07-15 14:15:17 +00: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 08:20:51 +00:00
using Microsoft.AspNetCore.Http;
2020-07-15 14:15:17 +00: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 08:20:51 +00:00
/// <response code="204">New group created.</response>
2020-07-15 14:15:17 +00:00
/// <returns>A <see cref="NoContentResult"/> indicating success.</returns>
[HttpPost("New")]
2020-07-22 08:20:51 +00:00
[ProducesResponseType(StatusCodes.Status204NoContent)]
2020-07-15 14:15:17 +00:00
public ActionResult CreateNewGroup()
{
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 08:20:51 +00:00
/// <response code="204">Group join successful.</response>
2020-07-15 14:15:17 +00:00
/// <returns>A <see cref="NoContentResult"/> indicating success.</returns>
[HttpPost("Join")]
2020-07-22 08:20:51 +00:00
[ProducesResponseType(StatusCodes.Status204NoContent)]
2020-07-15 14:15:17 +00:00
public ActionResult JoinGroup([FromQuery, Required] Guid groupId)
{
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 08:20:51 +00:00
/// <response code="204">Group leave successful.</response>
2020-07-15 14:15:17 +00:00
/// <returns>A <see cref="NoContentResult"/> indicating success.</returns>
[HttpPost("Leave")]
2020-07-22 08:20:51 +00:00
[ProducesResponseType(StatusCodes.Status204NoContent)]
2020-07-15 14:15:17 +00:00
public ActionResult LeaveGroup()
{
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 08:20:51 +00:00
/// <response code="200">Groups returned.</response>
2020-07-15 14:15:17 +00:00
/// <returns>An <see cref="IEnumerable{GrouüInfoView}"/> containing the available SyncPlay groups.</returns>
[HttpGet("List")]
2020-07-22 08:20:51 +00:00
[ProducesResponseType(StatusCodes.Status200OK)]
2020-07-15 14:15:17 +00:00
public ActionResult<IEnumerable<GroupInfoView>> GetSyncPlayGroups([FromQuery] Guid? filterItemId)
{
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 08:20:51 +00:00
/// <response code="204">Play request sent to all group members.</response>
2020-07-15 14:15:17 +00:00
/// <returns>A <see cref="NoContentResult"/> indicating success.</returns>
[HttpPost("Play")]
2020-07-22 08:20:51 +00:00
[ProducesResponseType(StatusCodes.Status204NoContent)]
2020-07-15 14:15:17 +00:00
public ActionResult Play()
{
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 08:20:51 +00:00
/// <response code="204">Pause request sent to all group members.</response>
2020-07-15 14:15:17 +00:00
/// <returns>A <see cref="NoContentResult"/> indicating success.</returns>
[HttpPost("Pause")]
2020-07-22 08:20:51 +00:00
[ProducesResponseType(StatusCodes.Status204NoContent)]
2020-07-15 14:15:17 +00:00
public ActionResult Pause()
{
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 08:20:51 +00:00
/// <response code="204">Seek request sent to all group members.</response>
2020-07-15 14:15:17 +00:00
/// <returns>A <see cref="NoContentResult"/> indicating success.</returns>
[HttpPost("Seek")]
2020-07-22 08:20:51 +00:00
[ProducesResponseType(StatusCodes.Status204NoContent)]
2020-07-15 14:15:17 +00:00
public ActionResult Seek([FromQuery] long positionTicks)
{
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 08:20:51 +00:00
/// <response code="204">Buffering request sent to all group members.</response>
2020-07-15 14:15:17 +00:00
/// <returns>A <see cref="NoContentResult"/> indicating success.</returns>
[HttpPost("Buffering")]
2020-07-22 08:20:51 +00:00
[ProducesResponseType(StatusCodes.Status204NoContent)]
2020-07-15 14:15:17 +00:00
public ActionResult Buffering([FromQuery] DateTime when, [FromQuery] long positionTicks, [FromQuery] bool bufferingDone)
{
var currentSession = RequestHelpers.GetSession(_sessionManager, _authorizationContext, Request);
var syncPlayRequest = new PlaybackRequest()
{
2020-07-21 20:06:07 +00:00
Type = bufferingDone ? PlaybackRequestType.Ready : PlaybackRequestType.Buffer,
2020-07-15 14:15:17 +00: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 08:20:51 +00:00
/// <response code="204">Ping updated.</response>
2020-07-15 14:15:17 +00:00
/// <returns>A <see cref="NoContentResult"/> indicating success.</returns>
[HttpPost("Ping")]
2020-07-22 08:20:51 +00:00
[ProducesResponseType(StatusCodes.Status204NoContent)]
2020-07-15 14:15:17 +00:00
public ActionResult Ping([FromQuery] double ping)
{
var currentSession = RequestHelpers.GetSession(_sessionManager, _authorizationContext, Request);
var syncPlayRequest = new PlaybackRequest()
{
2020-07-21 20:06:07 +00:00
Type = PlaybackRequestType.Ping,
2020-07-15 14:15:17 +00:00
Ping = Convert.ToInt64(ping)
};
_syncPlayManager.HandleRequest(currentSession, syncPlayRequest, CancellationToken.None);
return NoContent();
}
}
}