jellyfin-server/MediaBrowser.Api/SyncPlay/SyncPlayService.cs

263 lines
9.4 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-07-07 13:25:46 +00:00
[Route("/SyncPlay/New", "POST", Summary = "Create a new SyncPlay group")]
2020-04-01 15:52:42 +00:00
[Authenticated]
2020-07-07 13:25:46 +00:00
public class SyncPlayNew : IReturnVoid
2020-04-01 15:52:42 +00:00
{
}
2020-07-07 13:25:46 +00:00
[Route("/SyncPlay/Join", "POST", Summary = "Join an existing SyncPlay group")]
2020-04-01 15:52:42 +00:00
[Authenticated]
2020-07-07 13:25:46 +00:00
public class SyncPlayJoin : IReturnVoid
2020-04-01 15:52:42 +00:00
{
/// <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; }
}
2020-07-07 13:25:46 +00:00
[Route("/SyncPlay/Leave", "POST", Summary = "Leave joined SyncPlay group")]
2020-04-01 15:52:42 +00:00
[Authenticated]
2020-07-07 13:25:46 +00:00
public class SyncPlayLeave : IReturnVoid
2020-04-01 15:52:42 +00:00
{
}
2020-07-07 13:25:46 +00:00
[Route("/SyncPlay/List", "GET", Summary = "List SyncPlay groups")]
2020-04-01 15:52:42 +00:00
[Authenticated]
2020-07-07 13:25:46 +00:00
public class SyncPlayList : IReturnVoid
2020-04-01 15:52:42 +00:00
{
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 = "GET")]
2020-05-04 17:46:02 +00:00
public string FilterItemId { get; set; }
2020-04-01 15:52:42 +00:00
}
2020-07-07 13:25:46 +00:00
[Route("/SyncPlay/Play", "POST", Summary = "Request play in SyncPlay group")]
2020-04-01 15:52:42 +00:00
[Authenticated]
2020-07-07 13:25:46 +00:00
public class SyncPlayPlay : IReturnVoid
2020-04-01 15:52:42 +00:00
{
}
2020-07-07 13:25:46 +00:00
[Route("/SyncPlay/Pause", "POST", Summary = "Request pause in SyncPlay group")]
2020-04-01 15:52:42 +00:00
[Authenticated]
2020-07-07 13:25:46 +00:00
public class SyncPlayPause : IReturnVoid
2020-04-01 15:52:42 +00:00
{
}
2020-07-07 13:25:46 +00:00
[Route("/SyncPlay/Seek", "POST", Summary = "Request seek in SyncPlay group")]
2020-04-01 15:52:42 +00:00
[Authenticated]
2020-07-07 13:25:46 +00:00
public class SyncPlaySeek : IReturnVoid
2020-04-01 15:52:42 +00:00
{
[ApiMember(Name = "PositionTicks", IsRequired = true, DataType = "long", ParameterType = "query", Verb = "POST")]
public long PositionTicks { get; set; }
}
2020-07-07 13:25:46 +00:00
[Route("/SyncPlay/Buffering", "POST", Summary = "Request group wait in SyncPlay group while buffering")]
2020-04-01 15:52:42 +00:00
[Authenticated]
2020-07-07 13:25:46 +00:00
public class SyncPlayBuffering : IReturnVoid
2020-04-01 15:52:42 +00:00
{
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 ready request.
2020-04-21 21:37:37 +00:00
/// </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-07-07 13:25:46 +00:00
[Route("/SyncPlay/Ping", "POST", Summary = "Update session ping")]
2020-04-01 15:52:42 +00:00
[Authenticated]
2020-07-07 13:25:46 +00:00
public class SyncPlayPing : IReturnVoid
2020-04-01 15:52:42 +00:00
{
[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-07-07 13:25:46 +00:00
public void Post(SyncPlayNew 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-07-07 13:25:46 +00:00
public void Post(SyncPlayJoin request)
2020-04-01 15:52:42 +00:00
{
var currentSession = GetSession(_sessionContext);
2020-05-09 12:34:07 +00:00
Guid groupId;
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
2020-05-09 12:34:07 +00:00
var joinRequest = new JoinGroupRequest()
{
GroupId = groupId
2020-05-09 12:34:07 +00:00
};
_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-07-07 13:25:46 +00:00
public void Post(SyncPlayLeave 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-07-11 16:26:01 +00:00
public List<GroupInfoView> Get(SyncPlayList 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-07-07 13:25:46 +00:00
public void Post(SyncPlayPlay 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-07-07 13:25:46 +00:00
public void Post(SyncPlayPause 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-07-07 13:25:46 +00:00
public void Post(SyncPlaySeek 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-07-07 13:25:46 +00:00
public void Post(SyncPlayBuffering 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 = request.BufferingDone ? PlaybackRequestType.Ready : PlaybackRequestType.Buffer,
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-07-07 13:25:46 +00:00
public void Post(SyncPlayPing 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.Ping,
2020-04-21 21:37:37 +00:00
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
}
}
}