From a5cd11735cc0320e576605410b68e4dace6caa05 Mon Sep 17 00:00:00 2001 From: Bond-009 Date: Thu, 19 Dec 2019 15:50:25 +0100 Subject: [PATCH 1/4] Minor changes to MediaInfoService --- MediaBrowser.Api/Playback/MediaInfoService.cs | 54 +++++++++---------- 1 file changed, 24 insertions(+), 30 deletions(-) diff --git a/MediaBrowser.Api/Playback/MediaInfoService.cs b/MediaBrowser.Api/Playback/MediaInfoService.cs index c3032416b8..8caa033039 100644 --- a/MediaBrowser.Api/Playback/MediaInfoService.cs +++ b/MediaBrowser.Api/Playback/MediaInfoService.cs @@ -102,12 +102,6 @@ namespace MediaBrowser.Api.Playback public object Get(GetBitrateTestBytes request) { var bytes = new byte[request.Size]; - - for (var i = 0; i < bytes.Length; i++) - { - bytes[i] = 0; - } - return ResultFactory.GetResult(null, bytes, "application/octet-stream"); } @@ -166,8 +160,7 @@ namespace MediaBrowser.Api.Playback public void Post(CloseMediaSource request) { - var task = _mediaSourceManager.CloseLiveStream(request.LiveStreamId); - Task.WaitAll(task); + _mediaSourceManager.CloseLiveStream(request.LiveStreamId).GetAwaiter().GetResult(); } public async Task GetPlaybackInfo(GetPostedPlaybackInfo request) @@ -176,7 +169,7 @@ namespace MediaBrowser.Api.Playback var profile = request.DeviceProfile; - //Logger.LogInformation("GetPostedPlaybackInfo profile: {profile}", _json.SerializeToString(profile)); + Logger.LogInformation("GetPostedPlaybackInfo profile: {profile}", _json.SerializeToString(profile)); if (profile == null) { @@ -215,9 +208,7 @@ namespace MediaBrowser.Api.Playback StartTimeTicks = request.StartTimeTicks, SubtitleStreamIndex = request.SubtitleStreamIndex, UserId = request.UserId, - OpenToken = mediaSource.OpenToken, - //EnableMediaProbe = request.EnableMediaProbe - + OpenToken = mediaSource.OpenToken }).ConfigureAwait(false); info.MediaSources = new MediaSourceInfo[] { openStreamResult.MediaSource }; @@ -311,7 +302,8 @@ namespace MediaBrowser.Api.Playback return result; } - private void SetDeviceSpecificData(Guid itemId, + private void SetDeviceSpecificData( + Guid itemId, PlaybackInfoResponse result, DeviceProfile profile, AuthorizationInfo auth, @@ -339,7 +331,8 @@ namespace MediaBrowser.Api.Playback SortMediaSources(result, maxBitrate); } - private void SetDeviceSpecificData(BaseItem item, + private void SetDeviceSpecificData( + BaseItem item, MediaSourceInfo mediaSource, DeviceProfile profile, AuthorizationInfo auth, @@ -383,10 +376,12 @@ namespace MediaBrowser.Api.Playback { mediaSource.SupportsDirectPlay = false; } + if (!enableDirectStream) { mediaSource.SupportsDirectStream = false; } + if (!enableTranscoding) { mediaSource.SupportsTranscoding = false; @@ -434,9 +429,9 @@ namespace MediaBrowser.Api.Playback } // 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); + var streamInfo = string.Equals(item.MediaType, MediaType.Audio, StringComparison.OrdinalIgnoreCase) + ? streamBuilder.BuildAudioItem(options) + : streamBuilder.BuildVideoItem(options); if (streamInfo == null || !streamInfo.IsDirectStream) { @@ -473,9 +468,9 @@ namespace MediaBrowser.Api.Playback } // 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); + var streamInfo = string.Equals(item.MediaType, MediaType.Audio, StringComparison.OrdinalIgnoreCase) + ? streamBuilder.BuildAudioItem(options) + : streamBuilder.BuildVideoItem(options); if (streamInfo == null || !streamInfo.IsDirectStream) { @@ -493,9 +488,9 @@ namespace MediaBrowser.Api.Playback options.MaxBitrate = GetMaxBitrate(maxBitrate, user); // 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); + var streamInfo = string.Equals(item.MediaType, MediaType.Audio, StringComparison.OrdinalIgnoreCase) + ? streamBuilder.BuildAudioItem(options) + : streamBuilder.BuildVideoItem(options); if (streamInfo != null) { @@ -510,10 +505,12 @@ namespace MediaBrowser.Api.Playback { mediaSource.TranscodingUrl += "&allowVideoStreamCopy=false"; } + if (!allowAudioStreamCopy) { mediaSource.TranscodingUrl += "&allowAudioStreamCopy=false"; } + mediaSource.TranscodingContainer = streamInfo.Container; mediaSource.TranscodingSubProtocol = streamInfo.SubProtocol; } @@ -599,14 +596,11 @@ namespace MediaBrowser.Api.Playback }).ThenBy(i => { - switch (i.Protocol) + return i.Protocol switch { - case MediaProtocol.File: - return 0; - default: - return 1; - } - + MediaProtocol.File => 0, + _ => 1, + }; }).ThenBy(i => { if (maxBitrate.HasValue) From bb62dd14c2fee3f7a6fa8cb3d8592500046ed3c9 Mon Sep 17 00:00:00 2001 From: Bond-009 Date: Thu, 19 Dec 2019 17:46:17 +0100 Subject: [PATCH 2/4] Limit size for playbacktest --- MediaBrowser.Api/Playback/MediaInfoService.cs | 30 +++++++++++++++++-- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/MediaBrowser.Api/Playback/MediaInfoService.cs b/MediaBrowser.Api/Playback/MediaInfoService.cs index 8caa033039..ac1cba09ab 100644 --- a/MediaBrowser.Api/Playback/MediaInfoService.cs +++ b/MediaBrowser.Api/Playback/MediaInfoService.cs @@ -1,4 +1,5 @@ using System; +using System.Buffers; using System.Collections.Generic; using System.Globalization; using System.Linq; @@ -54,7 +55,7 @@ namespace MediaBrowser.Api.Playback public class GetBitrateTestBytes { [ApiMember(Name = "Size", Description = "Size", IsRequired = true, DataType = "int", ParameterType = "query", Verb = "GET")] - public long Size { get; set; } + public int Size { get; set; } public GetBitrateTestBytes() { @@ -101,8 +102,31 @@ namespace MediaBrowser.Api.Playback public object Get(GetBitrateTestBytes request) { - var bytes = new byte[request.Size]; - return ResultFactory.GetResult(null, bytes, "application/octet-stream"); + const int MaxSize = 10_000_000; + + var size = request.Size; + + if (size <= 0) + { + throw new ArgumentException($"The requested size can't be equal or smaller than 0.", nameof(request)); + } + + if (size > MaxSize) + { + throw new ArgumentException($"The requested size can't be larger than the max allowed value ({MaxSize}).", nameof(request)); + } + + byte[] buffer = ArrayPool.Shared.Rent(size); + try + { + // ArrayPool.Shared.Rent doesn't guarantee that the returned buffer is zeroed + Array.Fill(buffer, 0); + return ResultFactory.GetResult(null, buffer, "application/octet-stream"); + } + finally + { + ArrayPool.Shared.Return(buffer); + } } public async Task Get(GetPlaybackInfo request) From 5751d865363606e2ee1a773adaaf58c2864b08f8 Mon Sep 17 00:00:00 2001 From: Bond_009 Date: Fri, 20 Dec 2019 21:49:16 +0100 Subject: [PATCH 3/4] Fix warnings and move to System.Text.Json --- MediaBrowser.Api/Playback/MediaInfoService.cs | 18 ++++++++++-------- .../Playback/UniversalAudioService.cs | 2 -- .../MediaInfo/PlaybackInfoResponse.cs | 12 ++++++++++-- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/MediaBrowser.Api/Playback/MediaInfoService.cs b/MediaBrowser.Api/Playback/MediaInfoService.cs index ac1cba09ab..80acdc4554 100644 --- a/MediaBrowser.Api/Playback/MediaInfoService.cs +++ b/MediaBrowser.Api/Playback/MediaInfoService.cs @@ -1,7 +1,13 @@ +#pragma warning disable CS1591 +#pragma warning disable SA1402 +#pragma warning disable SA1600 +#pragma warning disable SA1649 + using System; using System.Buffers; using System.Collections.Generic; using System.Globalization; +using System.Text.Json; using System.Linq; using System.Threading; using System.Threading.Tasks; @@ -73,7 +79,6 @@ namespace MediaBrowser.Api.Playback private readonly INetworkManager _networkManager; private readonly IMediaEncoder _mediaEncoder; private readonly IUserManager _userManager; - private readonly IJsonSerializer _json; private readonly IAuthorizationContext _authContext; public MediaInfoService( @@ -86,7 +91,6 @@ namespace MediaBrowser.Api.Playback INetworkManager networkManager, IMediaEncoder mediaEncoder, IUserManager userManager, - IJsonSerializer json, IAuthorizationContext authContext) : base(logger, serverConfigurationManager, httpResultFactory) { @@ -96,7 +100,6 @@ namespace MediaBrowser.Api.Playback _networkManager = networkManager; _mediaEncoder = mediaEncoder; _userManager = userManager; - _json = json; _authContext = authContext; } @@ -193,7 +196,7 @@ namespace MediaBrowser.Api.Playback var profile = request.DeviceProfile; - Logger.LogInformation("GetPostedPlaybackInfo profile: {profile}", _json.SerializeToString(profile)); + Logger.LogInformation("GetPostedPlaybackInfo profile: {@Profile}", profile); if (profile == null) { @@ -266,9 +269,8 @@ namespace MediaBrowser.Api.Playback { // 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? - - var json = _json.SerializeToString(obj); - return _json.DeserializeFromString(json); + var json = JsonSerializer.SerializeToUtf8Bytes(obj); + return JsonSerializer.Deserialize(json); } private async Task GetPlaybackInfo(Guid id, Guid userId, string[] supportedLiveMediaTypes, string mediaSourceId = null, string liveStreamId = null) @@ -309,7 +311,7 @@ namespace MediaBrowser.Api.Playback result.MediaSources = new MediaSourceInfo[] { mediaSource }; } - if (result.MediaSources.Length == 0) + if (result.MediaSources.Count == 0) { if (!result.ErrorCode.HasValue) { diff --git a/MediaBrowser.Api/Playback/UniversalAudioService.cs b/MediaBrowser.Api/Playback/UniversalAudioService.cs index 9cba9df139..bcac5093e0 100644 --- a/MediaBrowser.Api/Playback/UniversalAudioService.cs +++ b/MediaBrowser.Api/Playback/UniversalAudioService.cs @@ -74,7 +74,6 @@ namespace MediaBrowser.Api.Playback [Authenticated] public class UniversalAudioService : BaseApiService { - private readonly ILoggerFactory _loggerFactory; private readonly EncodingHelper _encodingHelper; public UniversalAudioService( @@ -243,7 +242,6 @@ namespace MediaBrowser.Api.Playback NetworkManager, MediaEncoder, UserManager, - JsonSerializer, AuthorizationContext) { Request = Request diff --git a/MediaBrowser.Model/MediaInfo/PlaybackInfoResponse.cs b/MediaBrowser.Model/MediaInfo/PlaybackInfoResponse.cs index 38638af42c..440818c3ef 100644 --- a/MediaBrowser.Model/MediaInfo/PlaybackInfoResponse.cs +++ b/MediaBrowser.Model/MediaInfo/PlaybackInfoResponse.cs @@ -1,15 +1,20 @@ +using System; +using System.Collections.Generic; using MediaBrowser.Model.Dlna; using MediaBrowser.Model.Dto; namespace MediaBrowser.Model.MediaInfo { + /// + /// Class PlaybackInfoResponse. + /// public class PlaybackInfoResponse { /// /// Gets or sets the media sources. /// /// The media sources. - public MediaSourceInfo[] MediaSources { get; set; } + public IReadOnlyList MediaSources { get; set; } /// /// Gets or sets the play session identifier. @@ -23,9 +28,12 @@ namespace MediaBrowser.Model.MediaInfo /// The error code. public PlaybackErrorCode? ErrorCode { get; set; } + /// + /// Initializes a new instance of the class. + /// public PlaybackInfoResponse() { - MediaSources = new MediaSourceInfo[] { }; + MediaSources = Array.Empty(); } } } From d9ec502ff985918cf94326780b41c6382b9e8937 Mon Sep 17 00:00:00 2001 From: Bond_009 Date: Fri, 10 Jan 2020 21:25:45 +0100 Subject: [PATCH 4/4] Address comments --- MediaBrowser.Api/Playback/MediaInfoService.cs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/MediaBrowser.Api/Playback/MediaInfoService.cs b/MediaBrowser.Api/Playback/MediaInfoService.cs index 80acdc4554..3d8231a2ab 100644 --- a/MediaBrowser.Api/Playback/MediaInfoService.cs +++ b/MediaBrowser.Api/Playback/MediaInfoService.cs @@ -111,19 +111,18 @@ namespace MediaBrowser.Api.Playback if (size <= 0) { - throw new ArgumentException($"The requested size can't be equal or smaller than 0.", nameof(request)); + throw new ArgumentException($"The requested size ({size}) is equal to or smaller than 0.", nameof(request)); } if (size > MaxSize) { - throw new ArgumentException($"The requested size can't be larger than the max allowed value ({MaxSize}).", nameof(request)); + throw new ArgumentException($"The requested size ({size}) is larger than the max allowed value ({MaxSize}).", nameof(request)); } byte[] buffer = ArrayPool.Shared.Rent(size); try { - // ArrayPool.Shared.Rent doesn't guarantee that the returned buffer is zeroed - Array.Fill(buffer, 0); + new Random().NextBytes(buffer); return ResultFactory.GetResult(null, buffer, "application/octet-stream"); } finally