jellyfin/MediaBrowser.Api/Playback/MediaInfoService.cs

233 lines
9.6 KiB
C#
Raw Normal View History

using MediaBrowser.Controller.Devices;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
2014-11-19 02:45:12 +00:00
using MediaBrowser.Controller.Net;
2015-03-16 16:47:14 +00:00
using MediaBrowser.Model.Dlna;
using MediaBrowser.Model.Dto;
2014-11-19 02:45:12 +00:00
using MediaBrowser.Model.MediaInfo;
using MediaBrowser.Model.Session;
2014-11-19 02:45:12 +00:00
using ServiceStack;
2015-03-23 17:19:21 +00:00
using System;
2015-03-16 16:47:14 +00:00
using System.Collections.Generic;
2014-11-19 02:45:12 +00:00
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Api.Playback
{
2015-03-15 19:22:04 +00:00
[Route("/Items/{Id}/MediaInfo", "GET", Summary = "Gets live playback media info for an item")]
public class GetLiveMediaInfo : IReturn<PlaybackInfoResponse>
2014-11-19 02:45:12 +00:00
{
[ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
public string Id { get; set; }
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")]
public string UserId { get; set; }
}
2015-03-07 22:43:53 +00:00
[Route("/Items/{Id}/PlaybackInfo", "GET", Summary = "Gets live playback media info for an item")]
public class GetPlaybackInfo : IReturn<PlaybackInfoResponse>
{
[ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
public string Id { get; set; }
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")]
public string UserId { get; set; }
}
[Route("/Items/{Id}/PlaybackInfo", "POST", Summary = "Gets live playback media info for an item")]
public class GetPostedPlaybackInfo : PlaybackInfoRequest, IReturn<PlaybackInfoResponse>
2015-03-07 22:43:53 +00:00
{
[ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
public string Id { get; set; }
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")]
public string UserId { get; set; }
2015-03-26 19:18:21 +00:00
[ApiMember(Name = "StartTimeTicks", Description = "Optional. Specify a starting offset, in ticks. 1 tick = 10000 ms", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
public long? StartTimeTicks { get; set; }
[ApiMember(Name = "AudioStreamIndex", Description = "Optional. The index of the audio stream to use. If omitted the first audio stream will be used.", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
public int? AudioStreamIndex { get; set; }
[ApiMember(Name = "SubtitleStreamIndex", Description = "Optional. The index of the subtitle stream to use. If omitted no subtitles will be used.", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
public int? SubtitleStreamIndex { get; set; }
2015-03-07 22:43:53 +00:00
}
2014-11-19 02:45:12 +00:00
[Authenticated]
public class MediaInfoService : BaseApiService
{
2015-03-07 22:43:53 +00:00
private readonly IMediaSourceManager _mediaSourceManager;
private readonly IDeviceManager _deviceManager;
private readonly ILibraryManager _libraryManager;
2014-11-19 02:45:12 +00:00
public MediaInfoService(IMediaSourceManager mediaSourceManager, IDeviceManager deviceManager, ILibraryManager libraryManager)
2014-11-19 02:45:12 +00:00
{
2015-03-07 22:43:53 +00:00
_mediaSourceManager = mediaSourceManager;
_deviceManager = deviceManager;
_libraryManager = libraryManager;
}
public async Task<object> Get(GetPlaybackInfo request)
{
var result = await GetPlaybackInfo(request.Id, request.UserId).ConfigureAwait(false);
return ToOptimizedResult(result);
2014-11-19 02:45:12 +00:00
}
public async Task<object> Get(GetLiveMediaInfo request)
2014-11-19 02:45:12 +00:00
{
var result = await GetPlaybackInfo(request.Id, request.UserId).ConfigureAwait(false);
return ToOptimizedResult(result);
2015-03-16 16:47:14 +00:00
}
2014-11-19 02:45:12 +00:00
public async Task<object> Post(GetPostedPlaybackInfo request)
2015-03-16 16:47:14 +00:00
{
2015-03-26 19:18:21 +00:00
var info = await GetPlaybackInfo(request.Id, request.UserId, request.MediaSource).ConfigureAwait(false);
var authInfo = AuthorizationContext.GetAuthorizationInfo(Request);
var profile = request.DeviceProfile;
2015-03-26 23:10:34 +00:00
if (profile == null)
{
var caps = _deviceManager.GetCapabilities(authInfo.DeviceId);
if (caps != null)
{
profile = caps.DeviceProfile;
}
}
if (profile != null)
{
2015-03-26 19:18:21 +00:00
var mediaSourceId = request.MediaSource == null ? null : request.MediaSource.Id;
SetDeviceSpecificData(request.Id, info, profile, authInfo, null, request.StartTimeTicks ?? 0, mediaSourceId, request.AudioStreamIndex, request.SubtitleStreamIndex);
}
return ToOptimizedResult(info);
2015-03-07 22:43:53 +00:00
}
2014-11-26 04:12:29 +00:00
2015-03-26 19:18:21 +00:00
private async Task<PlaybackInfoResponse> GetPlaybackInfo(string id, string userId, MediaSourceInfo mediaSource = null)
2015-03-07 22:43:53 +00:00
{
var result = new PlaybackInfoResponse();
2014-11-19 02:45:12 +00:00
2015-03-26 19:18:21 +00:00
if (mediaSource == null)
2014-11-19 02:45:12 +00:00
{
2015-03-26 19:18:21 +00:00
IEnumerable<MediaSourceInfo> mediaSources;
try
{
mediaSources = await _mediaSourceManager.GetPlayackMediaSources(id, userId, true, CancellationToken.None).ConfigureAwait(false);
}
catch (PlaybackException ex)
{
mediaSources = new List<MediaSourceInfo>();
result.ErrorCode = ex.ErrorCode;
}
result.MediaSources = mediaSources.ToList();
2015-03-16 16:47:14 +00:00
}
2015-03-26 19:18:21 +00:00
else
2015-03-16 16:47:14 +00:00
{
2015-03-26 19:18:21 +00:00
result.MediaSources = new List<MediaSourceInfo> { mediaSource };
2015-03-16 16:47:14 +00:00
}
if (result.MediaSources.Count == 0)
{
if (!result.ErrorCode.HasValue)
{
result.ErrorCode = PlaybackErrorCode.NoCompatibleStream;
}
}
else
{
result.StreamId = Guid.NewGuid().ToString("N");
}
return result;
}
2015-03-26 19:18:21 +00:00
private void SetDeviceSpecificData(string itemId,
PlaybackInfoResponse result,
DeviceProfile profile,
AuthorizationInfo auth,
int? maxBitrate,
long startTimeTicks,
string mediaSourceId,
int? audioStreamIndex,
int? subtitleStreamIndex)
{
var streamBuilder = new StreamBuilder();
var item = _libraryManager.GetItemById(itemId);
foreach (var mediaSource in result.MediaSources)
{
var options = new VideoOptions
{
MediaSources = new List<MediaSourceInfo> { mediaSource },
Context = EncodingContext.Streaming,
DeviceId = auth.DeviceId,
ItemId = item.Id.ToString("N"),
Profile = profile,
MaxBitrate = maxBitrate
};
2015-03-26 19:18:21 +00:00
if (string.Equals(mediaSourceId, mediaSource.Id, StringComparison.OrdinalIgnoreCase))
{
options.MediaSourceId = mediaSourceId;
options.AudioStreamIndex = audioStreamIndex;
options.SubtitleStreamIndex = subtitleStreamIndex;
}
if (mediaSource.SupportsDirectPlay)
{
var supportsDirectStream = mediaSource.SupportsDirectStream;
// Dummy this up to fool StreamBuilder
mediaSource.SupportsDirectStream = true;
2015-03-26 19:18:21 +00:00
// The MediaSource supports direct stream, now test to see if the client supports it
var streamInfo = item is Video ?
streamBuilder.BuildVideoItem(options) :
streamBuilder.BuildAudioItem(options);
if (streamInfo == null || !streamInfo.IsDirectStream)
{
mediaSource.SupportsDirectPlay = false;
}
// Set this back to what it was
mediaSource.SupportsDirectStream = supportsDirectStream;
}
if (mediaSource.SupportsDirectStream)
{
// The MediaSource supports direct stream, now test to see if the client supports it
var streamInfo = item is Video ?
streamBuilder.BuildVideoItem(options) :
streamBuilder.BuildAudioItem(options);
if (streamInfo == null || !streamInfo.IsDirectStream)
{
mediaSource.SupportsDirectStream = false;
}
}
if (mediaSource.SupportsTranscoding)
{
// The MediaSource supports direct stream, now test to see if the client supports it
var streamInfo = item is Video ?
streamBuilder.BuildVideoItem(options) :
streamBuilder.BuildAudioItem(options);
if (streamInfo != null && streamInfo.PlayMethod == PlayMethod.Transcode)
{
2015-03-26 19:18:21 +00:00
streamInfo.StartPositionTicks = startTimeTicks;
mediaSource.TranscodingUrl = streamInfo.ToUrl("-", auth.Token).Substring(1);
mediaSource.TranscodingContainer = streamInfo.Container;
mediaSource.TranscodingSubProtocol = streamInfo.SubProtocol;
}
}
}
2014-11-19 02:45:12 +00:00
}
}
}