jellyfin/MediaBrowser.Api/SyncPlay/SyncPlayService.cs

303 lines
12 KiB
C#
Raw Normal View History

2020-05-04 17:46:02 +00:00
using System.Threading;
2020-04-01 15:52:42 +00:00
using System;
using System.Collections.Generic;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Net;
using MediaBrowser.Controller.Session;
2020-05-06 21:42:53 +00:00
using MediaBrowser.Controller.SyncPlay;
2020-04-01 15:52:42 +00:00
using MediaBrowser.Model.Services;
2020-05-06 21:42:53 +00:00
using MediaBrowser.Model.SyncPlay;
2020-04-01 15:52:42 +00:00
using Microsoft.Extensions.Logging;
2020-05-06 21:42:53 +00:00
namespace MediaBrowser.Api.SyncPlay
2020-04-01 15:52:42 +00:00
{
2020-05-06 21:42:53 +00:00
[Route("/SyncPlay/{SessionId}/NewGroup", "POST", Summary = "Create a new SyncPlay group")]
2020-04-01 15:52:42 +00:00
[Authenticated]
2020-05-06 21:42:53 +00:00
public class SyncPlayNewGroup : IReturnVoid
2020-04-01 15:52:42 +00:00
{
[ApiMember(Name = "SessionId", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
public string SessionId { get; set; }
}
2020-05-06 21:42:53 +00:00
[Route("/SyncPlay/{SessionId}/JoinGroup", "POST", Summary = "Join an existing SyncPlay group")]
2020-04-01 15:52:42 +00:00
[Authenticated]
2020-05-06 21:42:53 +00:00
public class SyncPlayJoinGroup : IReturnVoid
2020-04-01 15:52:42 +00:00
{
[ApiMember(Name = "SessionId", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
public string SessionId { get; set; }
/// <summary>
/// Gets or sets the Group id.
/// </summary>
/// <value>The Group id to join.</value>
[ApiMember(Name = "GroupId", Description = "Group Id", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
public string GroupId { get; set; }
/// <summary>
/// Gets or sets the playing item id.
/// </summary>
/// <value>The client's currently playing item id.</value>
[ApiMember(Name = "PlayingItemId", Description = "Client's playing item id", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
public string PlayingItemId { get; set; }
2020-04-01 15:52:42 +00:00
}
2020-05-06 21:42:53 +00:00
[Route("/SyncPlay/{SessionId}/LeaveGroup", "POST", Summary = "Leave joined SyncPlay group")]
2020-04-01 15:52:42 +00:00
[Authenticated]
2020-05-06 21:42:53 +00:00
public class SyncPlayLeaveGroup : IReturnVoid
2020-04-01 15:52:42 +00:00
{
[ApiMember(Name = "SessionId", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
public string SessionId { get; set; }
}
2020-05-06 21:42:53 +00:00
[Route("/SyncPlay/{SessionId}/ListGroups", "POST", Summary = "List SyncPlay groups")]
2020-04-01 15:52:42 +00:00
[Authenticated]
2020-05-06 21:42:53 +00:00
public class SyncPlayListGroups : IReturnVoid
2020-04-01 15:52:42 +00:00
{
[ApiMember(Name = "SessionId", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
public string SessionId { get; set; }
2020-05-04 17:46:02 +00:00
/// <summary>
/// Gets or sets the filter item id.
/// </summary>
/// <value>The filter item id.</value>
[ApiMember(Name = "FilterItemId", Description = "Filter by item id", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
public string FilterItemId { get; set; }
2020-04-01 15:52:42 +00:00
}
2020-05-06 21:42:53 +00:00
[Route("/SyncPlay/{SessionId}/PlayRequest", "POST", Summary = "Request play in SyncPlay group")]
2020-04-01 15:52:42 +00:00
[Authenticated]
2020-05-06 21:42:53 +00:00
public class SyncPlayPlayRequest : IReturnVoid
2020-04-01 15:52:42 +00:00
{
[ApiMember(Name = "SessionId", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
public string SessionId { get; set; }
}
2020-05-06 21:42:53 +00:00
[Route("/SyncPlay/{SessionId}/PauseRequest", "POST", Summary = "Request pause in SyncPlay group")]
2020-04-01 15:52:42 +00:00
[Authenticated]
2020-05-06 21:42:53 +00:00
public class SyncPlayPauseRequest : IReturnVoid
2020-04-01 15:52:42 +00:00
{
[ApiMember(Name = "SessionId", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
public string SessionId { get; set; }
}
2020-05-06 21:42:53 +00:00
[Route("/SyncPlay/{SessionId}/SeekRequest", "POST", Summary = "Request seek in SyncPlay group")]
2020-04-01 15:52:42 +00:00
[Authenticated]
2020-05-06 21:42:53 +00:00
public class SyncPlaySeekRequest : IReturnVoid
2020-04-01 15:52:42 +00:00
{
[ApiMember(Name = "SessionId", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
public string SessionId { get; set; }
[ApiMember(Name = "PositionTicks", IsRequired = true, DataType = "long", ParameterType = "query", Verb = "POST")]
public long PositionTicks { get; set; }
}
2020-05-06 21:42:53 +00:00
[Route("/SyncPlay/{SessionId}/BufferingRequest", "POST", Summary = "Request group wait in SyncPlay group while buffering")]
2020-04-01 15:52:42 +00:00
[Authenticated]
2020-05-06 21:42:53 +00:00
public class SyncPlayBufferingRequest : IReturnVoid
2020-04-01 15:52:42 +00:00
{
[ApiMember(Name = "SessionId", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
public string SessionId { get; set; }
2020-04-21 21:37:37 +00:00
/// <summary>
/// Gets or sets the date used to pin PositionTicks in time.
/// </summary>
/// <value>The date related to PositionTicks.</value>
2020-04-01 15:52:42 +00:00
[ApiMember(Name = "When", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
public string When { get; set; }
[ApiMember(Name = "PositionTicks", IsRequired = true, DataType = "long", ParameterType = "query", Verb = "POST")]
public long PositionTicks { get; set; }
2020-04-21 21:37:37 +00:00
/// <summary>
/// Gets or sets whether this is a buffering or a buffering-done request.
/// </summary>
/// <value><c>true</c> if buffering is complete; <c>false</c> otherwise.</value>
2020-05-04 17:46:02 +00:00
[ApiMember(Name = "BufferingDone", IsRequired = true, DataType = "bool", ParameterType = "query", Verb = "POST")]
public bool BufferingDone { get; set; }
2020-04-01 15:52:42 +00:00
}
2020-05-06 21:42:53 +00:00
[Route("/SyncPlay/{SessionId}/UpdatePing", "POST", Summary = "Update session ping")]
2020-04-01 15:52:42 +00:00
[Authenticated]
2020-05-06 21:42:53 +00:00
public class SyncPlayUpdatePing : IReturnVoid
2020-04-01 15:52:42 +00:00
{
[ApiMember(Name = "SessionId", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
public string SessionId { get; set; }
[ApiMember(Name = "Ping", IsRequired = true, DataType = "double", ParameterType = "query", Verb = "POST")]
public double Ping { get; set; }
}
/// <summary>
2020-05-06 21:42:53 +00:00
/// Class SyncPlayService.
2020-04-01 15:52:42 +00:00
/// </summary>
2020-05-06 21:42:53 +00:00
public class SyncPlayService : BaseApiService
2020-04-01 15:52:42 +00:00
{
2020-04-16 14:02:52 +00:00
/// <summary>
/// The session context.
/// </summary>
2020-04-01 15:52:42 +00:00
private readonly ISessionContext _sessionContext;
/// <summary>
2020-05-06 21:42:53 +00:00
/// The SyncPlay manager.
2020-04-01 15:52:42 +00:00
/// </summary>
2020-05-06 21:42:53 +00:00
private readonly ISyncPlayManager _syncPlayManager;
2020-04-01 15:52:42 +00:00
2020-05-06 21:42:53 +00:00
public SyncPlayService(
ILogger<SyncPlayService> logger,
2020-04-01 15:52:42 +00:00
IServerConfigurationManager serverConfigurationManager,
IHttpResultFactory httpResultFactory,
ISessionContext sessionContext,
2020-05-06 21:42:53 +00:00
ISyncPlayManager syncPlayManager)
2020-04-01 15:52:42 +00:00
: base(logger, serverConfigurationManager, httpResultFactory)
{
_sessionContext = sessionContext;
2020-05-06 21:42:53 +00:00
_syncPlayManager = syncPlayManager;
2020-04-01 15:52:42 +00:00
}
/// <summary>
/// Handles the specified request.
/// </summary>
/// <param name="request">The request.</param>
2020-05-06 21:42:53 +00:00
public void Post(SyncPlayNewGroup request)
2020-04-01 15:52:42 +00:00
{
var currentSession = GetSession(_sessionContext);
2020-05-06 21:42:53 +00:00
_syncPlayManager.NewGroup(currentSession, CancellationToken.None);
2020-04-01 15:52:42 +00:00
}
/// <summary>
/// Handles the specified request.
/// </summary>
/// <param name="request">The request.</param>
2020-05-06 21:42:53 +00:00
public void Post(SyncPlayJoinGroup request)
2020-04-01 15:52:42 +00:00
{
var currentSession = GetSession(_sessionContext);
2020-05-09 12:34:07 +00:00
Guid groupId;
Guid playingItemId = Guid.Empty;
2020-05-15 16:47:16 +00:00
if (!Guid.TryParse(request.GroupId, out groupId))
2020-04-21 21:37:37 +00:00
{
2020-05-09 12:34:07 +00:00
Logger.LogError("JoinGroup: {0} is not a valid format for GroupId. Ignoring request.", request.GroupId);
return;
}
2020-05-04 17:46:02 +00:00
// Both null and empty strings mean that client isn't playing anything
2020-05-26 09:37:52 +00:00
if (!string.IsNullOrEmpty(request.PlayingItemId) && !Guid.TryParse(request.PlayingItemId, out playingItemId))
{
2020-05-15 18:06:41 +00:00
Logger.LogError("JoinGroup: {0} is not a valid format for PlayingItemId. Ignoring request.", request.PlayingItemId);
return;
}
2020-05-09 12:34:07 +00:00
var joinRequest = new JoinGroupRequest()
{
GroupId = groupId,
PlayingItemId = playingItemId
};
_syncPlayManager.JoinGroup(currentSession, groupId, joinRequest, CancellationToken.None);
2020-04-01 15:52:42 +00:00
}
/// <summary>
/// Handles the specified request.
/// </summary>
/// <param name="request">The request.</param>
2020-05-06 21:42:53 +00:00
public void Post(SyncPlayLeaveGroup request)
2020-04-01 15:52:42 +00:00
{
var currentSession = GetSession(_sessionContext);
2020-05-06 21:42:53 +00:00
_syncPlayManager.LeaveGroup(currentSession, CancellationToken.None);
2020-04-01 15:52:42 +00:00
}
/// <summary>
/// Handles the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <value>The requested list of groups.</value>
2020-05-06 21:42:53 +00:00
public List<GroupInfoView> Post(SyncPlayListGroups request)
2020-04-01 15:52:42 +00:00
{
var currentSession = GetSession(_sessionContext);
2020-05-04 17:46:02 +00:00
var filterItemId = Guid.Empty;
2020-05-09 12:34:07 +00:00
2020-05-26 09:37:52 +00:00
if (!string.IsNullOrEmpty(request.FilterItemId) && !Guid.TryParse(request.FilterItemId, out filterItemId))
2020-05-04 17:46:02 +00:00
{
2020-05-15 18:06:41 +00:00
Logger.LogWarning("ListGroups: {0} is not a valid format for FilterItemId. Ignoring filter.", request.FilterItemId);
2020-05-04 17:46:02 +00:00
}
2020-05-09 12:34:07 +00:00
2020-05-06 21:42:53 +00:00
return _syncPlayManager.ListGroups(currentSession, filterItemId);
2020-04-01 15:52:42 +00:00
}
/// <summary>
/// Handles the specified request.
/// </summary>
/// <param name="request">The request.</param>
2020-05-06 21:42:53 +00:00
public void Post(SyncPlayPlayRequest request)
2020-04-01 15:52:42 +00:00
{
var currentSession = GetSession(_sessionContext);
2020-05-06 21:42:53 +00:00
var syncPlayRequest = new PlaybackRequest()
2020-04-21 21:37:37 +00:00
{
Type = PlaybackRequestType.Play
};
2020-05-06 21:42:53 +00:00
_syncPlayManager.HandleRequest(currentSession, syncPlayRequest, CancellationToken.None);
2020-04-01 15:52:42 +00:00
}
/// <summary>
/// Handles the specified request.
/// </summary>
/// <param name="request">The request.</param>
2020-05-06 21:42:53 +00:00
public void Post(SyncPlayPauseRequest request)
2020-04-01 15:52:42 +00:00
{
var currentSession = GetSession(_sessionContext);
2020-05-06 21:42:53 +00:00
var syncPlayRequest = new PlaybackRequest()
2020-04-21 21:37:37 +00:00
{
Type = PlaybackRequestType.Pause
};
2020-05-06 21:42:53 +00:00
_syncPlayManager.HandleRequest(currentSession, syncPlayRequest, CancellationToken.None);
2020-04-01 15:52:42 +00:00
}
/// <summary>
/// Handles the specified request.
/// </summary>
/// <param name="request">The request.</param>
2020-05-06 21:42:53 +00:00
public void Post(SyncPlaySeekRequest request)
2020-04-01 15:52:42 +00:00
{
var currentSession = GetSession(_sessionContext);
2020-05-06 21:42:53 +00:00
var syncPlayRequest = new PlaybackRequest()
2020-04-21 21:37:37 +00:00
{
Type = PlaybackRequestType.Seek,
PositionTicks = request.PositionTicks
};
2020-05-06 21:42:53 +00:00
_syncPlayManager.HandleRequest(currentSession, syncPlayRequest, CancellationToken.None);
2020-04-01 15:52:42 +00:00
}
/// <summary>
/// Handles the specified request.
/// </summary>
/// <param name="request">The request.</param>
2020-05-06 21:42:53 +00:00
public void Post(SyncPlayBufferingRequest request)
2020-04-01 15:52:42 +00:00
{
var currentSession = GetSession(_sessionContext);
2020-05-06 21:42:53 +00:00
var syncPlayRequest = new PlaybackRequest()
2020-04-21 21:37:37 +00:00
{
2020-05-04 17:46:02 +00:00
Type = request.BufferingDone ? PlaybackRequestType.BufferingDone : PlaybackRequestType.Buffering,
2020-04-21 21:37:37 +00:00
When = DateTime.Parse(request.When),
PositionTicks = request.PositionTicks
};
2020-05-06 21:42:53 +00:00
_syncPlayManager.HandleRequest(currentSession, syncPlayRequest, CancellationToken.None);
2020-04-01 15:52:42 +00:00
}
/// <summary>
/// Handles the specified request.
/// </summary>
/// <param name="request">The request.</param>
2020-05-06 21:42:53 +00:00
public void Post(SyncPlayUpdatePing request)
2020-04-01 15:52:42 +00:00
{
var currentSession = GetSession(_sessionContext);
2020-05-06 21:42:53 +00:00
var syncPlayRequest = new PlaybackRequest()
2020-04-21 21:37:37 +00:00
{
Type = PlaybackRequestType.UpdatePing,
Ping = Convert.ToInt64(request.Ping)
};
2020-05-06 21:42:53 +00:00
_syncPlayManager.HandleRequest(currentSession, syncPlayRequest, CancellationToken.None);
2020-04-01 15:52:42 +00:00
}
}
}