jellyfin-server/MediaBrowser.Api/Syncplay/SyncplayService.cs

313 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;
using MediaBrowser.Controller.Syncplay;
using MediaBrowser.Model.Services;
using MediaBrowser.Model.Syncplay;
using Microsoft.Extensions.Logging;
namespace MediaBrowser.Api.Syncplay
{
[Route("/Syncplay/{SessionId}/NewGroup", "POST", Summary = "Create a new Syncplay group")]
[Authenticated]
public class SyncplayNewGroup : IReturnVoid
{
[ApiMember(Name = "SessionId", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
public string SessionId { get; set; }
}
[Route("/Syncplay/{SessionId}/JoinGroup", "POST", Summary = "Join an existing Syncplay group")]
[Authenticated]
public class SyncplayJoinGroup : IReturnVoid
{
[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
}
[Route("/Syncplay/{SessionId}/LeaveGroup", "POST", Summary = "Leave joined Syncplay group")]
[Authenticated]
public class SyncplayLeaveGroup : IReturnVoid
{
[ApiMember(Name = "SessionId", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
public string SessionId { get; set; }
}
2020-05-04 17:46:02 +00:00
[Route("/Syncplay/{SessionId}/ListGroups", "POST", Summary = "List Syncplay groups")]
2020-04-01 15:52:42 +00:00
[Authenticated]
public class SyncplayListGroups : IReturnVoid
{
[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
}
[Route("/Syncplay/{SessionId}/PlayRequest", "POST", Summary = "Request play in Syncplay group")]
[Authenticated]
public class SyncplayPlayRequest : IReturnVoid
{
[ApiMember(Name = "SessionId", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
public string SessionId { get; set; }
}
[Route("/Syncplay/{SessionId}/PauseRequest", "POST", Summary = "Request pause in Syncplay group")]
[Authenticated]
public class SyncplayPauseRequest : IReturnVoid
{
[ApiMember(Name = "SessionId", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
public string SessionId { get; set; }
}
[Route("/Syncplay/{SessionId}/SeekRequest", "POST", Summary = "Request seek in Syncplay group")]
[Authenticated]
public class SyncplaySeekRequest : IReturnVoid
{
[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; }
}
[Route("/Syncplay/{SessionId}/BufferingRequest", "POST", Summary = "Request group wait in Syncplay group while buffering")]
[Authenticated]
public class SyncplayBufferingRequest : IReturnVoid
{
[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-04-17 10:57:36 +00:00
[Route("/Syncplay/{SessionId}/UpdatePing", "POST", Summary = "Update session ping")]
2020-04-01 15:52:42 +00:00
[Authenticated]
2020-04-17 10:57:36 +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>
/// Class SyncplayService.
/// </summary>
public class SyncplayService : BaseApiService
{
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>
/// The Syncplay manager.
/// </summary>
private readonly ISyncplayManager _syncplayManager;
public SyncplayService(
ILogger<SyncplayService> logger,
IServerConfigurationManager serverConfigurationManager,
IHttpResultFactory httpResultFactory,
ISessionContext sessionContext,
ISyncplayManager syncplayManager)
: base(logger, serverConfigurationManager, httpResultFactory)
{
_sessionContext = sessionContext;
_syncplayManager = syncplayManager;
}
/// <summary>
/// Handles the specified request.
/// </summary>
/// <param name="request">The request.</param>
public void Post(SyncplayNewGroup request)
{
var currentSession = GetSession(_sessionContext);
2020-05-04 17:46:02 +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>
public void Post(SyncplayJoinGroup request)
{
var currentSession = GetSession(_sessionContext);
2020-04-21 21:37:37 +00:00
var joinRequest = new JoinGroupRequest()
{
GroupId = Guid.Parse(request.GroupId)
};
2020-05-04 17:46:02 +00:00
// Both null and empty strings mean that client isn't playing anything
if (!String.IsNullOrEmpty(request.PlayingItemId))
{
2020-05-04 17:46:02 +00:00
try
{
joinRequest.PlayingItemId = Guid.Parse(request.PlayingItemId);
}
catch (ArgumentNullException)
{
// Should never happen, but just in case
Logger.LogError("JoinGroup: null value for PlayingItemId. Ignoring request.");
return;
}
catch (FormatException)
{
Logger.LogError("JoinGroup: {0} is not a valid format for PlayingItemId. Ignoring request.", request.PlayingItemId);
return;
}
}
2020-05-04 17:46:02 +00:00
_syncplayManager.JoinGroup(currentSession, request.GroupId, joinRequest, CancellationToken.None);
2020-04-01 15:52:42 +00:00
}
/// <summary>
/// Handles the specified request.
/// </summary>
/// <param name="request">The request.</param>
public void Post(SyncplayLeaveGroup request)
{
var currentSession = GetSession(_sessionContext);
2020-05-04 17:46:02 +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>
public List<GroupInfoView> Post(SyncplayListGroups request)
{
var currentSession = GetSession(_sessionContext);
2020-05-04 17:46:02 +00:00
var filterItemId = Guid.Empty;
if (!String.IsNullOrEmpty(request.FilterItemId))
{
try
{
filterItemId = Guid.Parse(request.FilterItemId);
}
catch (ArgumentNullException)
{
Logger.LogWarning("ListGroups: null value for FilterItemId. Ignoring filter.");
}
catch (FormatException)
{
Logger.LogWarning("ListGroups: {0} is not a valid format for FilterItemId. Ignoring filter.", request.FilterItemId);
}
}
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>
public void Post(SyncplayPlayRequest request)
{
var currentSession = GetSession(_sessionContext);
2020-04-21 21:37:37 +00:00
var syncplayRequest = new PlaybackRequest()
{
Type = PlaybackRequestType.Play
};
2020-05-04 17:46:02 +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>
public void Post(SyncplayPauseRequest request)
{
var currentSession = GetSession(_sessionContext);
2020-04-21 21:37:37 +00:00
var syncplayRequest = new PlaybackRequest()
{
Type = PlaybackRequestType.Pause
};
2020-05-04 17:46:02 +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>
public void Post(SyncplaySeekRequest request)
{
var currentSession = GetSession(_sessionContext);
2020-04-21 21:37:37 +00:00
var syncplayRequest = new PlaybackRequest()
{
Type = PlaybackRequestType.Seek,
PositionTicks = request.PositionTicks
};
2020-05-04 17:46:02 +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>
public void Post(SyncplayBufferingRequest request)
{
var currentSession = GetSession(_sessionContext);
2020-04-21 21:37:37 +00:00
var syncplayRequest = new PlaybackRequest()
{
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-04 17:46:02 +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-04-17 10:57:36 +00:00
public void Post(SyncplayUpdatePing request)
2020-04-01 15:52:42 +00:00
{
var currentSession = GetSession(_sessionContext);
2020-04-21 21:37:37 +00:00
var syncplayRequest = new PlaybackRequest()
{
Type = PlaybackRequestType.UpdatePing,
Ping = Convert.ToInt64(request.Ping)
};
2020-05-04 17:46:02 +00:00
_syncplayManager.HandleRequest(currentSession, syncplayRequest, CancellationToken.None);
2020-04-01 15:52:42 +00:00
}
}
}