2019-12-20 20:49:16 +00:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
#pragma warning disable SA1402
|
|
|
|
#pragma warning disable SA1649
|
|
|
|
|
2019-01-13 20:01:16 +00:00
|
|
|
using System;
|
2019-12-19 16:46:17 +00:00
|
|
|
using System.Buffers;
|
2019-02-28 22:22:57 +00:00
|
|
|
using System.Globalization;
|
2019-01-13 19:24:58 +00:00
|
|
|
using System.Linq;
|
2020-03-24 15:12:06 +00:00
|
|
|
using System.Text.Json;
|
2019-01-13 19:24:58 +00:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using MediaBrowser.Common.Net;
|
2018-12-14 09:40:55 +00:00
|
|
|
using MediaBrowser.Controller.Configuration;
|
|
|
|
using MediaBrowser.Controller.Devices;
|
|
|
|
using MediaBrowser.Controller.Entities;
|
2019-01-13 19:24:58 +00:00
|
|
|
using MediaBrowser.Controller.Entities.Audio;
|
2018-12-14 09:40:55 +00:00
|
|
|
using MediaBrowser.Controller.Library;
|
2019-01-13 19:24:58 +00:00
|
|
|
using MediaBrowser.Controller.MediaEncoding;
|
2018-12-14 09:40:55 +00:00
|
|
|
using MediaBrowser.Controller.Net;
|
|
|
|
using MediaBrowser.Model.Dlna;
|
|
|
|
using MediaBrowser.Model.Dto;
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
using MediaBrowser.Model.MediaInfo;
|
|
|
|
using MediaBrowser.Model.Services;
|
2019-01-13 19:24:58 +00:00
|
|
|
using MediaBrowser.Model.Session;
|
2018-12-14 23:48:06 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
2018-12-14 09:40:55 +00:00
|
|
|
|
|
|
|
namespace MediaBrowser.Api.Playback
|
|
|
|
{
|
|
|
|
[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 Guid Id { get; set; }
|
|
|
|
|
|
|
|
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")]
|
|
|
|
public Guid UserId { get; set; }
|
|
|
|
}
|
|
|
|
|
|
|
|
[Route("/Items/{Id}/PlaybackInfo", "POST", Summary = "Gets live playback media info for an item")]
|
|
|
|
public class GetPostedPlaybackInfo : PlaybackInfoRequest, IReturn<PlaybackInfoResponse>
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
[Route("/LiveStreams/Open", "POST", Summary = "Opens a media source")]
|
|
|
|
public class OpenMediaSource : LiveStreamRequest, IReturn<LiveStreamResponse>
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
[Route("/LiveStreams/Close", "POST", Summary = "Closes a media source")]
|
|
|
|
public class CloseMediaSource : IReturnVoid
|
|
|
|
{
|
|
|
|
[ApiMember(Name = "LiveStreamId", Description = "LiveStreamId", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
|
|
|
|
public string LiveStreamId { get; set; }
|
|
|
|
}
|
|
|
|
|
|
|
|
[Route("/Playback/BitrateTest", "GET")]
|
|
|
|
public class GetBitrateTestBytes
|
|
|
|
{
|
|
|
|
[ApiMember(Name = "Size", Description = "Size", IsRequired = true, DataType = "int", ParameterType = "query", Verb = "GET")]
|
2019-12-19 16:46:17 +00:00
|
|
|
public int Size { get; set; }
|
2018-12-14 09:40:55 +00:00
|
|
|
|
|
|
|
public GetBitrateTestBytes()
|
|
|
|
{
|
|
|
|
// 100k
|
|
|
|
Size = 102400;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[Authenticated]
|
|
|
|
public class MediaInfoService : BaseApiService
|
|
|
|
{
|
|
|
|
private readonly IMediaSourceManager _mediaSourceManager;
|
|
|
|
private readonly IDeviceManager _deviceManager;
|
|
|
|
private readonly ILibraryManager _libraryManager;
|
|
|
|
private readonly INetworkManager _networkManager;
|
|
|
|
private readonly IMediaEncoder _mediaEncoder;
|
|
|
|
private readonly IUserManager _userManager;
|
|
|
|
private readonly IAuthorizationContext _authContext;
|
2019-01-21 17:52:49 +00:00
|
|
|
|
|
|
|
public MediaInfoService(
|
2020-05-15 00:11:34 +00:00
|
|
|
ILogger<MediaInfoService> logger,
|
2019-11-17 22:05:39 +00:00
|
|
|
IServerConfigurationManager serverConfigurationManager,
|
|
|
|
IHttpResultFactory httpResultFactory,
|
2019-01-21 17:52:49 +00:00
|
|
|
IMediaSourceManager mediaSourceManager,
|
|
|
|
IDeviceManager deviceManager,
|
|
|
|
ILibraryManager libraryManager,
|
|
|
|
INetworkManager networkManager,
|
|
|
|
IMediaEncoder mediaEncoder,
|
|
|
|
IUserManager userManager,
|
2019-11-17 22:05:39 +00:00
|
|
|
IAuthorizationContext authContext)
|
|
|
|
: base(logger, serverConfigurationManager, httpResultFactory)
|
2018-12-14 09:40:55 +00:00
|
|
|
{
|
|
|
|
_mediaSourceManager = mediaSourceManager;
|
|
|
|
_deviceManager = deviceManager;
|
|
|
|
_libraryManager = libraryManager;
|
|
|
|
_networkManager = networkManager;
|
|
|
|
_mediaEncoder = mediaEncoder;
|
|
|
|
_userManager = userManager;
|
|
|
|
_authContext = authContext;
|
|
|
|
}
|
|
|
|
|
|
|
|
public object Get(GetBitrateTestBytes request)
|
|
|
|
{
|
2019-12-19 16:46:17 +00:00
|
|
|
const int MaxSize = 10_000_000;
|
|
|
|
|
|
|
|
var size = request.Size;
|
|
|
|
|
|
|
|
if (size <= 0)
|
|
|
|
{
|
2020-01-10 20:25:45 +00:00
|
|
|
throw new ArgumentException($"The requested size ({size}) is equal to or smaller than 0.", nameof(request));
|
2019-12-19 16:46:17 +00:00
|
|
|
}
|
2018-12-14 09:40:55 +00:00
|
|
|
|
2019-12-19 16:46:17 +00:00
|
|
|
if (size > MaxSize)
|
2018-12-14 09:40:55 +00:00
|
|
|
{
|
2020-01-10 20:25:45 +00:00
|
|
|
throw new ArgumentException($"The requested size ({size}) is larger than the max allowed value ({MaxSize}).", nameof(request));
|
2018-12-14 09:40:55 +00:00
|
|
|
}
|
|
|
|
|
2019-12-19 16:46:17 +00:00
|
|
|
byte[] buffer = ArrayPool<byte>.Shared.Rent(size);
|
|
|
|
try
|
|
|
|
{
|
2020-01-10 20:25:45 +00:00
|
|
|
new Random().NextBytes(buffer);
|
2019-12-19 16:46:17 +00:00
|
|
|
return ResultFactory.GetResult(null, buffer, "application/octet-stream");
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
ArrayPool<byte>.Shared.Return(buffer);
|
|
|
|
}
|
2018-12-14 09:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<object> Get(GetPlaybackInfo request)
|
|
|
|
{
|
|
|
|
var result = await GetPlaybackInfo(request.Id, request.UserId, new[] { MediaType.Audio, MediaType.Video }).ConfigureAwait(false);
|
|
|
|
return ToOptimizedResult(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<object> Post(OpenMediaSource request)
|
|
|
|
{
|
|
|
|
var result = await OpenMediaSource(request).ConfigureAwait(false);
|
|
|
|
|
|
|
|
return ToOptimizedResult(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
private async Task<LiveStreamResponse> OpenMediaSource(OpenMediaSource request)
|
|
|
|
{
|
|
|
|
var authInfo = _authContext.GetAuthorizationInfo(Request);
|
|
|
|
|
|
|
|
var result = await _mediaSourceManager.OpenLiveStream(request, CancellationToken.None).ConfigureAwait(false);
|
2019-01-07 23:27:46 +00:00
|
|
|
|
2018-12-14 09:40:55 +00:00
|
|
|
var profile = request.DeviceProfile;
|
|
|
|
if (profile == null)
|
|
|
|
{
|
|
|
|
var caps = _deviceManager.GetCapabilities(authInfo.DeviceId);
|
|
|
|
if (caps != null)
|
|
|
|
{
|
|
|
|
profile = caps.DeviceProfile;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (profile != null)
|
|
|
|
{
|
|
|
|
var item = _libraryManager.GetItemById(request.ItemId);
|
|
|
|
|
|
|
|
SetDeviceSpecificData(item, result.MediaSource, profile, authInfo, request.MaxStreamingBitrate,
|
|
|
|
request.StartTimeTicks ?? 0, result.MediaSource.Id, request.AudioStreamIndex,
|
|
|
|
request.SubtitleStreamIndex, request.MaxAudioChannels, request.PlaySessionId, request.UserId, request.EnableDirectPlay, true, request.EnableDirectStream, true, true, true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!string.IsNullOrWhiteSpace(result.MediaSource.TranscodingUrl))
|
|
|
|
{
|
|
|
|
result.MediaSource.TranscodingUrl += "&LiveStreamId=" + result.MediaSource.LiveStreamId;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result.MediaSource != null)
|
|
|
|
{
|
|
|
|
NormalizeMediaSourceContainer(result.MediaSource, profile, DlnaProfileType.Video);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Post(CloseMediaSource request)
|
|
|
|
{
|
2019-12-19 14:50:25 +00:00
|
|
|
_mediaSourceManager.CloseLiveStream(request.LiveStreamId).GetAwaiter().GetResult();
|
2018-12-14 09:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<PlaybackInfoResponse> GetPlaybackInfo(GetPostedPlaybackInfo request)
|
|
|
|
{
|
|
|
|
var authInfo = _authContext.GetAuthorizationInfo(Request);
|
|
|
|
|
|
|
|
var profile = request.DeviceProfile;
|
|
|
|
|
2019-12-20 20:49:16 +00:00
|
|
|
Logger.LogInformation("GetPostedPlaybackInfo profile: {@Profile}", profile);
|
2018-12-14 09:40:55 +00:00
|
|
|
|
|
|
|
if (profile == null)
|
|
|
|
{
|
|
|
|
var caps = _deviceManager.GetCapabilities(authInfo.DeviceId);
|
|
|
|
if (caps != null)
|
|
|
|
{
|
|
|
|
profile = caps.DeviceProfile;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var info = await GetPlaybackInfo(request.Id, request.UserId, new[] { MediaType.Audio, MediaType.Video }, request.MediaSourceId, request.LiveStreamId).ConfigureAwait(false);
|
|
|
|
|
|
|
|
if (profile != null)
|
|
|
|
{
|
|
|
|
var mediaSourceId = request.MediaSourceId;
|
|
|
|
|
|
|
|
SetDeviceSpecificData(request.Id, info, profile, authInfo, request.MaxStreamingBitrate ?? profile.MaxStreamingBitrate, request.StartTimeTicks ?? 0, mediaSourceId, request.AudioStreamIndex, request.SubtitleStreamIndex, request.MaxAudioChannels, request.UserId, request.EnableDirectPlay, true, request.EnableDirectStream, request.EnableTranscoding, request.AllowVideoStreamCopy, request.AllowAudioStreamCopy);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (request.AutoOpenLiveStream)
|
|
|
|
{
|
|
|
|
var mediaSource = string.IsNullOrWhiteSpace(request.MediaSourceId) ? info.MediaSources.FirstOrDefault() : info.MediaSources.FirstOrDefault(i => string.Equals(i.Id, request.MediaSourceId, StringComparison.Ordinal));
|
|
|
|
|
|
|
|
if (mediaSource != null && mediaSource.RequiresOpening && string.IsNullOrWhiteSpace(mediaSource.LiveStreamId))
|
|
|
|
{
|
|
|
|
var openStreamResult = await OpenMediaSource(new OpenMediaSource
|
|
|
|
{
|
|
|
|
AudioStreamIndex = request.AudioStreamIndex,
|
|
|
|
DeviceProfile = request.DeviceProfile,
|
|
|
|
EnableDirectPlay = request.EnableDirectPlay,
|
|
|
|
EnableDirectStream = request.EnableDirectStream,
|
|
|
|
ItemId = request.Id,
|
|
|
|
MaxAudioChannels = request.MaxAudioChannels,
|
|
|
|
MaxStreamingBitrate = request.MaxStreamingBitrate,
|
|
|
|
PlaySessionId = info.PlaySessionId,
|
|
|
|
StartTimeTicks = request.StartTimeTicks,
|
|
|
|
SubtitleStreamIndex = request.SubtitleStreamIndex,
|
|
|
|
UserId = request.UserId,
|
2019-12-19 14:50:25 +00:00
|
|
|
OpenToken = mediaSource.OpenToken
|
2018-12-14 09:40:55 +00:00
|
|
|
}).ConfigureAwait(false);
|
|
|
|
|
2020-04-05 17:00:35 +00:00
|
|
|
info.MediaSources = new[] { openStreamResult.MediaSource };
|
2018-12-14 09:40:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (info.MediaSources != null)
|
|
|
|
{
|
|
|
|
foreach (var mediaSource in info.MediaSources)
|
|
|
|
{
|
|
|
|
NormalizeMediaSourceContainer(mediaSource, profile, DlnaProfileType.Video);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void NormalizeMediaSourceContainer(MediaSourceInfo mediaSource, DeviceProfile profile, DlnaProfileType type)
|
|
|
|
{
|
|
|
|
mediaSource.Container = StreamBuilder.NormalizeMediaSourceFormatIntoSingleContainer(mediaSource.Container, mediaSource.Path, profile, type);
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<object> Post(GetPostedPlaybackInfo request)
|
|
|
|
{
|
|
|
|
var result = await GetPlaybackInfo(request).ConfigureAwait(false);
|
|
|
|
|
|
|
|
return ToOptimizedResult(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
private async Task<PlaybackInfoResponse> GetPlaybackInfo(Guid id, Guid userId, string[] supportedLiveMediaTypes, string mediaSourceId = null, string liveStreamId = null)
|
|
|
|
{
|
|
|
|
var user = _userManager.GetUserById(userId);
|
|
|
|
var item = _libraryManager.GetItemById(id);
|
|
|
|
var result = new PlaybackInfoResponse();
|
|
|
|
|
2020-01-12 20:21:20 +00:00
|
|
|
MediaSourceInfo[] mediaSources;
|
2018-12-14 09:40:55 +00:00
|
|
|
if (string.IsNullOrWhiteSpace(liveStreamId))
|
|
|
|
{
|
|
|
|
|
2020-01-13 08:01:14 +00:00
|
|
|
// TODO handle supportedLiveMediaTypes?
|
2020-01-17 19:48:28 +00:00
|
|
|
var mediaSourcesList = await _mediaSourceManager.GetPlaybackMediaSources(item, user, true, true, CancellationToken.None).ConfigureAwait(false);
|
2018-12-14 09:40:55 +00:00
|
|
|
|
2020-01-12 20:21:20 +00:00
|
|
|
if (string.IsNullOrWhiteSpace(mediaSourceId))
|
|
|
|
{
|
|
|
|
mediaSources = mediaSourcesList.ToArray();
|
|
|
|
}
|
|
|
|
else
|
2018-12-14 09:40:55 +00:00
|
|
|
{
|
2020-01-12 20:21:20 +00:00
|
|
|
mediaSources = mediaSourcesList
|
2018-12-14 09:40:55 +00:00
|
|
|
.Where(i => string.Equals(i.Id, mediaSourceId, StringComparison.OrdinalIgnoreCase))
|
|
|
|
.ToArray();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var mediaSource = await _mediaSourceManager.GetLiveStream(liveStreamId, CancellationToken.None).ConfigureAwait(false);
|
|
|
|
|
2020-04-05 17:00:35 +00:00
|
|
|
mediaSources = new[] { mediaSource };
|
2018-12-14 09:40:55 +00:00
|
|
|
}
|
|
|
|
|
2020-01-12 20:21:20 +00:00
|
|
|
if (mediaSources.Length == 0)
|
2018-12-14 09:40:55 +00:00
|
|
|
{
|
2020-01-12 20:21:20 +00:00
|
|
|
result.MediaSources = Array.Empty<MediaSourceInfo>();
|
|
|
|
|
2018-12-14 09:40:55 +00:00
|
|
|
if (!result.ErrorCode.HasValue)
|
|
|
|
{
|
|
|
|
result.ErrorCode = PlaybackErrorCode.NoCompatibleStream;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-01-12 20:21:20 +00:00
|
|
|
// Since we're going to be setting properties on MediaSourceInfos that come out of _mediaSourceManager, we should clone it
|
|
|
|
// Should we move this directly into MediaSourceManager?
|
|
|
|
result.MediaSources = JsonSerializer.Deserialize<MediaSourceInfo[]>(JsonSerializer.SerializeToUtf8Bytes(mediaSources));
|
2018-12-14 09:40:55 +00:00
|
|
|
|
2019-02-28 22:22:57 +00:00
|
|
|
result.PlaySessionId = Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture);
|
2018-12-14 09:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-12-19 14:50:25 +00:00
|
|
|
private void SetDeviceSpecificData(
|
|
|
|
Guid itemId,
|
2018-12-14 09:40:55 +00:00
|
|
|
PlaybackInfoResponse result,
|
|
|
|
DeviceProfile profile,
|
|
|
|
AuthorizationInfo auth,
|
|
|
|
long? maxBitrate,
|
|
|
|
long startTimeTicks,
|
|
|
|
string mediaSourceId,
|
|
|
|
int? audioStreamIndex,
|
|
|
|
int? subtitleStreamIndex,
|
|
|
|
int? maxAudioChannels,
|
|
|
|
Guid userId,
|
|
|
|
bool enableDirectPlay,
|
|
|
|
bool forceDirectPlayRemoteMediaSource,
|
|
|
|
bool enableDirectStream,
|
|
|
|
bool enableTranscoding,
|
|
|
|
bool allowVideoStreamCopy,
|
|
|
|
bool allowAudioStreamCopy)
|
|
|
|
{
|
|
|
|
var item = _libraryManager.GetItemById(itemId);
|
|
|
|
|
|
|
|
foreach (var mediaSource in result.MediaSources)
|
|
|
|
{
|
|
|
|
SetDeviceSpecificData(item, mediaSource, profile, auth, maxBitrate, startTimeTicks, mediaSourceId, audioStreamIndex, subtitleStreamIndex, maxAudioChannels, result.PlaySessionId, userId, enableDirectPlay, forceDirectPlayRemoteMediaSource, enableDirectStream, enableTranscoding, allowVideoStreamCopy, allowAudioStreamCopy);
|
|
|
|
}
|
|
|
|
|
|
|
|
SortMediaSources(result, maxBitrate);
|
|
|
|
}
|
|
|
|
|
2019-12-19 14:50:25 +00:00
|
|
|
private void SetDeviceSpecificData(
|
|
|
|
BaseItem item,
|
2018-12-14 09:40:55 +00:00
|
|
|
MediaSourceInfo mediaSource,
|
|
|
|
DeviceProfile profile,
|
|
|
|
AuthorizationInfo auth,
|
|
|
|
long? maxBitrate,
|
|
|
|
long startTimeTicks,
|
|
|
|
string mediaSourceId,
|
|
|
|
int? audioStreamIndex,
|
|
|
|
int? subtitleStreamIndex,
|
|
|
|
int? maxAudioChannels,
|
|
|
|
string playSessionId,
|
|
|
|
Guid userId,
|
|
|
|
bool enableDirectPlay,
|
|
|
|
bool forceDirectPlayRemoteMediaSource,
|
|
|
|
bool enableDirectStream,
|
|
|
|
bool enableTranscoding,
|
|
|
|
bool allowVideoStreamCopy,
|
|
|
|
bool allowAudioStreamCopy)
|
|
|
|
{
|
|
|
|
var streamBuilder = new StreamBuilder(_mediaEncoder, Logger);
|
|
|
|
|
|
|
|
var options = new VideoOptions
|
|
|
|
{
|
2020-04-05 17:00:35 +00:00
|
|
|
MediaSources = new[] { mediaSource },
|
2018-12-14 09:40:55 +00:00
|
|
|
Context = EncodingContext.Streaming,
|
|
|
|
DeviceId = auth.DeviceId,
|
|
|
|
ItemId = item.Id,
|
|
|
|
Profile = profile,
|
|
|
|
MaxAudioChannels = maxAudioChannels
|
|
|
|
};
|
|
|
|
|
|
|
|
if (string.Equals(mediaSourceId, mediaSource.Id, StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
options.MediaSourceId = mediaSourceId;
|
|
|
|
options.AudioStreamIndex = audioStreamIndex;
|
|
|
|
options.SubtitleStreamIndex = subtitleStreamIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
var user = _userManager.GetUserById(userId);
|
|
|
|
|
|
|
|
if (!enableDirectPlay)
|
|
|
|
{
|
|
|
|
mediaSource.SupportsDirectPlay = false;
|
|
|
|
}
|
2019-12-19 14:50:25 +00:00
|
|
|
|
2018-12-14 09:40:55 +00:00
|
|
|
if (!enableDirectStream)
|
|
|
|
{
|
|
|
|
mediaSource.SupportsDirectStream = false;
|
|
|
|
}
|
2019-12-19 14:50:25 +00:00
|
|
|
|
2018-12-14 09:40:55 +00:00
|
|
|
if (!enableTranscoding)
|
|
|
|
{
|
|
|
|
mediaSource.SupportsTranscoding = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (item is Audio)
|
|
|
|
{
|
2018-12-14 23:48:06 +00:00
|
|
|
Logger.LogInformation("User policy for {0}. EnableAudioPlaybackTranscoding: {1}", user.Name, user.Policy.EnableAudioPlaybackTranscoding);
|
2018-12-14 09:40:55 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-01-07 23:27:46 +00:00
|
|
|
Logger.LogInformation("User policy for {0}. EnablePlaybackRemuxing: {1} EnableVideoPlaybackTranscoding: {2} EnableAudioPlaybackTranscoding: {3}",
|
2018-12-14 09:40:55 +00:00
|
|
|
user.Name,
|
|
|
|
user.Policy.EnablePlaybackRemuxing,
|
2019-01-07 23:27:46 +00:00
|
|
|
user.Policy.EnableVideoPlaybackTranscoding,
|
2018-12-14 09:40:55 +00:00
|
|
|
user.Policy.EnableAudioPlaybackTranscoding);
|
|
|
|
}
|
|
|
|
|
2019-12-21 04:17:01 +00:00
|
|
|
// Beginning of Playback Determination: Attempt DirectPlay first
|
2018-12-14 09:40:55 +00:00
|
|
|
if (mediaSource.SupportsDirectPlay)
|
|
|
|
{
|
2020-02-06 22:21:10 +00:00
|
|
|
if (mediaSource.IsRemote && user.Policy.ForceRemoteSourceTranscoding)
|
2018-12-14 09:40:55 +00:00
|
|
|
{
|
2019-12-16 20:22:18 +00:00
|
|
|
mediaSource.SupportsDirectPlay = false;
|
2018-12-14 09:40:55 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var supportsDirectStream = mediaSource.SupportsDirectStream;
|
|
|
|
|
|
|
|
// Dummy this up to fool StreamBuilder
|
|
|
|
mediaSource.SupportsDirectStream = true;
|
|
|
|
options.MaxBitrate = maxBitrate;
|
|
|
|
|
|
|
|
if (item is Audio)
|
|
|
|
{
|
|
|
|
if (!user.Policy.EnableAudioPlaybackTranscoding)
|
|
|
|
{
|
|
|
|
options.ForceDirectPlay = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (item is Video)
|
|
|
|
{
|
|
|
|
if (!user.Policy.EnableAudioPlaybackTranscoding && !user.Policy.EnableVideoPlaybackTranscoding && !user.Policy.EnablePlaybackRemuxing)
|
|
|
|
{
|
|
|
|
options.ForceDirectPlay = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The MediaSource supports direct stream, now test to see if the client supports it
|
2019-12-19 14:50:25 +00:00
|
|
|
var streamInfo = string.Equals(item.MediaType, MediaType.Audio, StringComparison.OrdinalIgnoreCase)
|
|
|
|
? streamBuilder.BuildAudioItem(options)
|
|
|
|
: streamBuilder.BuildVideoItem(options);
|
2018-12-14 09:40:55 +00:00
|
|
|
|
|
|
|
if (streamInfo == null || !streamInfo.IsDirectStream)
|
|
|
|
{
|
|
|
|
mediaSource.SupportsDirectPlay = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set this back to what it was
|
|
|
|
mediaSource.SupportsDirectStream = supportsDirectStream;
|
|
|
|
|
|
|
|
if (streamInfo != null)
|
|
|
|
{
|
|
|
|
SetDeviceSpecificSubtitleInfo(streamInfo, mediaSource, auth.Token);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mediaSource.SupportsDirectStream)
|
|
|
|
{
|
2020-02-06 22:21:10 +00:00
|
|
|
if (mediaSource.IsRemote && user.Policy.ForceRemoteSourceTranscoding)
|
2018-12-14 09:40:55 +00:00
|
|
|
{
|
2019-12-21 04:40:36 +00:00
|
|
|
mediaSource.SupportsDirectStream = false;
|
2018-12-14 09:40:55 +00:00
|
|
|
}
|
2019-12-16 20:22:18 +00:00
|
|
|
else
|
2018-12-14 09:40:55 +00:00
|
|
|
{
|
2019-12-16 20:22:18 +00:00
|
|
|
options.MaxBitrate = GetMaxBitrate(maxBitrate, user);
|
2020-03-24 15:12:06 +00:00
|
|
|
|
2019-12-16 20:22:18 +00:00
|
|
|
if (item is Audio)
|
2018-12-14 09:40:55 +00:00
|
|
|
{
|
2019-12-16 20:22:18 +00:00
|
|
|
if (!user.Policy.EnableAudioPlaybackTranscoding)
|
|
|
|
{
|
|
|
|
options.ForceDirectStream = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (item is Video)
|
|
|
|
{
|
|
|
|
if (!user.Policy.EnableAudioPlaybackTranscoding && !user.Policy.EnableVideoPlaybackTranscoding && !user.Policy.EnablePlaybackRemuxing)
|
|
|
|
{
|
|
|
|
options.ForceDirectStream = true;
|
|
|
|
}
|
2018-12-14 09:40:55 +00:00
|
|
|
}
|
|
|
|
|
2020-03-24 15:12:06 +00:00
|
|
|
// The MediaSource supports direct stream, now test to see if the client supports it
|
|
|
|
var streamInfo = string.Equals(item.MediaType, MediaType.Audio, StringComparison.OrdinalIgnoreCase)
|
|
|
|
? streamBuilder.BuildAudioItem(options)
|
|
|
|
: streamBuilder.BuildVideoItem(options);
|
2018-12-14 09:40:55 +00:00
|
|
|
|
2019-12-16 20:22:18 +00:00
|
|
|
if (streamInfo == null || !streamInfo.IsDirectStream)
|
|
|
|
{
|
|
|
|
mediaSource.SupportsDirectStream = false;
|
|
|
|
}
|
2018-12-14 09:40:55 +00:00
|
|
|
|
2019-12-16 20:22:18 +00:00
|
|
|
if (streamInfo != null)
|
|
|
|
{
|
|
|
|
SetDeviceSpecificSubtitleInfo(streamInfo, mediaSource, auth.Token);
|
|
|
|
}
|
2018-12-14 09:40:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mediaSource.SupportsTranscoding)
|
|
|
|
{
|
|
|
|
options.MaxBitrate = GetMaxBitrate(maxBitrate, user);
|
|
|
|
|
|
|
|
// The MediaSource supports direct stream, now test to see if the client supports it
|
2019-12-19 14:50:25 +00:00
|
|
|
var streamInfo = string.Equals(item.MediaType, MediaType.Audio, StringComparison.OrdinalIgnoreCase)
|
|
|
|
? streamBuilder.BuildAudioItem(options)
|
|
|
|
: streamBuilder.BuildVideoItem(options);
|
2018-12-14 09:40:55 +00:00
|
|
|
|
2020-02-19 11:15:55 +00:00
|
|
|
if (mediaSource.IsRemote && user.Policy.ForceRemoteSourceTranscoding)
|
2018-12-14 09:40:55 +00:00
|
|
|
{
|
2019-12-16 20:22:18 +00:00
|
|
|
if (streamInfo != null)
|
2018-12-14 09:40:55 +00:00
|
|
|
{
|
2020-03-24 15:12:06 +00:00
|
|
|
streamInfo.PlaySessionId = playSessionId;
|
2018-12-14 09:40:55 +00:00
|
|
|
streamInfo.StartPositionTicks = startTimeTicks;
|
|
|
|
mediaSource.TranscodingUrl = streamInfo.ToUrl("-", auth.Token).TrimStart('-');
|
2019-12-16 20:22:18 +00:00
|
|
|
mediaSource.TranscodingUrl += "&allowVideoStreamCopy=false";
|
2020-04-02 18:14:57 +00:00
|
|
|
mediaSource.TranscodingUrl += "&allowAudioStreamCopy=false";
|
2019-12-16 20:22:18 +00:00
|
|
|
mediaSource.TranscodingContainer = streamInfo.Container;
|
|
|
|
mediaSource.TranscodingSubProtocol = streamInfo.SubProtocol;
|
2020-03-24 15:12:06 +00:00
|
|
|
|
2019-12-16 20:22:18 +00:00
|
|
|
// Do this after the above so that StartPositionTicks is set
|
|
|
|
SetDeviceSpecificSubtitleInfo(streamInfo, mediaSource, auth.Token);
|
2020-03-24 15:12:06 +00:00
|
|
|
}
|
2020-02-19 11:15:55 +00:00
|
|
|
}
|
2019-12-16 20:22:18 +00:00
|
|
|
else
|
2020-02-19 11:15:55 +00:00
|
|
|
{
|
2019-12-16 20:22:18 +00:00
|
|
|
if (streamInfo != null)
|
|
|
|
{
|
|
|
|
streamInfo.PlaySessionId = playSessionId;
|
2018-12-14 09:40:55 +00:00
|
|
|
|
2019-12-16 20:22:18 +00:00
|
|
|
if (streamInfo.PlayMethod == PlayMethod.Transcode)
|
2018-12-14 09:40:55 +00:00
|
|
|
{
|
2019-12-16 20:22:18 +00:00
|
|
|
streamInfo.StartPositionTicks = startTimeTicks;
|
|
|
|
mediaSource.TranscodingUrl = streamInfo.ToUrl("-", auth.Token).TrimStart('-');
|
|
|
|
|
|
|
|
if (!allowVideoStreamCopy)
|
|
|
|
{
|
|
|
|
mediaSource.TranscodingUrl += "&allowVideoStreamCopy=false";
|
|
|
|
}
|
|
|
|
if (!allowAudioStreamCopy)
|
|
|
|
{
|
|
|
|
mediaSource.TranscodingUrl += "&allowAudioStreamCopy=false";
|
|
|
|
}
|
|
|
|
mediaSource.TranscodingContainer = streamInfo.Container;
|
|
|
|
mediaSource.TranscodingSubProtocol = streamInfo.SubProtocol;
|
2018-12-14 09:40:55 +00:00
|
|
|
}
|
2019-12-19 14:50:25 +00:00
|
|
|
|
2018-12-14 09:40:55 +00:00
|
|
|
if (!allowAudioStreamCopy)
|
|
|
|
{
|
|
|
|
mediaSource.TranscodingUrl += "&allowAudioStreamCopy=false";
|
|
|
|
}
|
2019-12-19 14:50:25 +00:00
|
|
|
|
2018-12-14 09:40:55 +00:00
|
|
|
mediaSource.TranscodingContainer = streamInfo.Container;
|
|
|
|
mediaSource.TranscodingSubProtocol = streamInfo.SubProtocol;
|
|
|
|
|
2019-12-16 20:22:18 +00:00
|
|
|
// Do this after the above so that StartPositionTicks is set
|
|
|
|
SetDeviceSpecificSubtitleInfo(streamInfo, mediaSource, auth.Token);
|
|
|
|
}
|
2018-12-14 09:40:55 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-18 11:59:03 +00:00
|
|
|
|
|
|
|
foreach (var attachment in mediaSource.MediaAttachments)
|
|
|
|
{
|
2019-11-05 13:21:06 +00:00
|
|
|
attachment.DeliveryUrl = string.Format(
|
|
|
|
CultureInfo.InvariantCulture,
|
2020-04-06 11:06:42 +00:00
|
|
|
"/Videos/{0}/{1}/Attachments/{2}",
|
2019-10-18 11:59:03 +00:00
|
|
|
item.Id,
|
|
|
|
mediaSource.Id,
|
2019-11-04 16:31:41 +00:00
|
|
|
attachment.Index);
|
2019-10-18 11:59:03 +00:00
|
|
|
}
|
2018-12-14 09:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private long? GetMaxBitrate(long? clientMaxBitrate, User user)
|
|
|
|
{
|
|
|
|
var maxBitrate = clientMaxBitrate;
|
2020-04-05 16:45:01 +00:00
|
|
|
var remoteClientMaxBitrate = user?.Policy.RemoteClientBitrateLimit ?? 0;
|
2018-12-14 09:40:55 +00:00
|
|
|
|
|
|
|
if (remoteClientMaxBitrate <= 0)
|
|
|
|
{
|
2019-11-17 22:05:39 +00:00
|
|
|
remoteClientMaxBitrate = ServerConfigurationManager.Configuration.RemoteClientBitrateLimit;
|
2018-12-14 09:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (remoteClientMaxBitrate > 0)
|
|
|
|
{
|
|
|
|
var isInLocalNetwork = _networkManager.IsInLocalNetwork(Request.RemoteIp);
|
|
|
|
|
2018-12-14 23:48:06 +00:00
|
|
|
Logger.LogInformation("RemoteClientBitrateLimit: {0}, RemoteIp: {1}, IsInLocalNetwork: {2}", remoteClientMaxBitrate, Request.RemoteIp, isInLocalNetwork);
|
2018-12-14 09:40:55 +00:00
|
|
|
if (!isInLocalNetwork)
|
|
|
|
{
|
|
|
|
maxBitrate = Math.Min(maxBitrate ?? remoteClientMaxBitrate, remoteClientMaxBitrate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return maxBitrate;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void SetDeviceSpecificSubtitleInfo(StreamInfo info, MediaSourceInfo mediaSource, string accessToken)
|
|
|
|
{
|
|
|
|
var profiles = info.GetSubtitleProfiles(_mediaEncoder, false, "-", accessToken);
|
|
|
|
mediaSource.DefaultSubtitleStreamIndex = info.SubtitleStreamIndex;
|
|
|
|
|
|
|
|
mediaSource.TranscodeReasons = info.TranscodeReasons;
|
|
|
|
|
|
|
|
foreach (var profile in profiles)
|
|
|
|
{
|
|
|
|
foreach (var stream in mediaSource.MediaStreams)
|
|
|
|
{
|
|
|
|
if (stream.Type == MediaStreamType.Subtitle && stream.Index == profile.Index)
|
|
|
|
{
|
|
|
|
stream.DeliveryMethod = profile.DeliveryMethod;
|
|
|
|
|
|
|
|
if (profile.DeliveryMethod == SubtitleDeliveryMethod.External)
|
|
|
|
{
|
|
|
|
stream.DeliveryUrl = profile.Url.TrimStart('-');
|
|
|
|
stream.IsExternalUrl = profile.IsExternalUrl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void SortMediaSources(PlaybackInfoResponse result, long? maxBitrate)
|
|
|
|
{
|
|
|
|
var originalList = result.MediaSources.ToList();
|
|
|
|
|
|
|
|
result.MediaSources = result.MediaSources.OrderBy(i =>
|
|
|
|
{
|
|
|
|
// Nothing beats direct playing a file
|
|
|
|
if (i.SupportsDirectPlay && i.Protocol == MediaProtocol.File)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
}).ThenBy(i =>
|
|
|
|
{
|
|
|
|
// Let's assume direct streaming a file is just as desirable as direct playing a remote url
|
|
|
|
if (i.SupportsDirectPlay || i.SupportsDirectStream)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
}).ThenBy(i =>
|
|
|
|
{
|
2019-12-19 14:50:25 +00:00
|
|
|
return i.Protocol switch
|
2018-12-14 09:40:55 +00:00
|
|
|
{
|
2019-12-19 14:50:25 +00:00
|
|
|
MediaProtocol.File => 0,
|
|
|
|
_ => 1,
|
|
|
|
};
|
2018-12-14 09:40:55 +00:00
|
|
|
}).ThenBy(i =>
|
|
|
|
{
|
2020-04-05 18:44:14 +00:00
|
|
|
if (maxBitrate.HasValue && i.Bitrate.HasValue)
|
2018-12-14 09:40:55 +00:00
|
|
|
{
|
2020-04-05 18:44:14 +00:00
|
|
|
return i.Bitrate.Value <= maxBitrate.Value ? 0 : 2;
|
2018-12-14 09:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
}).ThenBy(originalList.IndexOf)
|
|
|
|
.ToArray();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|