From cce120d8d325a9186d4e79efdf7009ad72638e81 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Sun, 12 Oct 2014 13:31:41 -0400 Subject: [PATCH] refresh connect authorizations --- MediaBrowser.Api/ApiEntryPoint.cs | 6 +- MediaBrowser.Api/MediaBrowser.Api.csproj | 1 + .../Playback/Hls/DynamicHlsService.cs | 8 - .../Playback/Hls/MpegDashService.cs | 271 ++++++++++++++++++ MediaBrowser.Common/Net/MimeTypes.cs | 4 + MediaBrowser.Controller/Entities/BaseItem.cs | 4 +- MediaBrowser.Model/Entities/ChapterInfo.cs | 12 - .../Connect/ConnectManager.cs | 47 ++- 8 files changed, 327 insertions(+), 26 deletions(-) create mode 100644 MediaBrowser.Api/Playback/Hls/MpegDashService.cs diff --git a/MediaBrowser.Api/ApiEntryPoint.cs b/MediaBrowser.Api/ApiEntryPoint.cs index 2e9323e03..7c655ef88 100644 --- a/MediaBrowser.Api/ApiEntryPoint.cs +++ b/MediaBrowser.Api/ApiEntryPoint.cs @@ -580,6 +580,10 @@ namespace MediaBrowser.Api /// /// The HLS /// - Hls + Hls, + /// + /// The dash + /// + Dash } } diff --git a/MediaBrowser.Api/MediaBrowser.Api.csproj b/MediaBrowser.Api/MediaBrowser.Api.csproj index 732d602c6..df9569e28 100644 --- a/MediaBrowser.Api/MediaBrowser.Api.csproj +++ b/MediaBrowser.Api/MediaBrowser.Api.csproj @@ -75,6 +75,7 @@ + diff --git a/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs b/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs index b99657c30..aa28ee90a 100644 --- a/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs +++ b/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs @@ -695,13 +695,5 @@ namespace MediaBrowser.Api.Playback.Hls { return ".ts"; } - - protected override TranscodingJobType TranscodingJobType - { - get - { - return TranscodingJobType.Hls; - } - } } } diff --git a/MediaBrowser.Api/Playback/Hls/MpegDashService.cs b/MediaBrowser.Api/Playback/Hls/MpegDashService.cs new file mode 100644 index 000000000..caddbd9a1 --- /dev/null +++ b/MediaBrowser.Api/Playback/Hls/MpegDashService.cs @@ -0,0 +1,271 @@ +using MediaBrowser.Common.IO; +using MediaBrowser.Common.Net; +using MediaBrowser.Controller.Channels; +using MediaBrowser.Controller.Configuration; +using MediaBrowser.Controller.Dlna; +using MediaBrowser.Controller.Library; +using MediaBrowser.Controller.LiveTv; +using MediaBrowser.Controller.MediaEncoding; +using MediaBrowser.Model.IO; +using ServiceStack; +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace MediaBrowser.Api.Playback.Hls +{ + /// + /// Options is needed for chromecast. Threw Head in there since it's related + /// + [Route("/Videos/{Id}/master.mpd", "GET", Summary = "Gets a video stream using Mpeg dash.")] + [Route("/Videos/{Id}/master.mpd", "HEAD", Summary = "Gets a video stream using Mpeg dash.")] + public class GetMasterManifest : VideoStreamRequest + { + public bool EnableAdaptiveBitrateStreaming { get; set; } + + public GetMasterManifest() + { + EnableAdaptiveBitrateStreaming = true; + } + } + + [Route("/Videos/{Id}/dash/{SegmentId}.ts", "GET")] + public class GetDashSegment : VideoStreamRequest + { + /// + /// Gets or sets the segment id. + /// + /// The segment id. + public string SegmentId { get; set; } + } + + public class MpegDashService : BaseHlsService + { + protected INetworkManager NetworkManager { get; private set; } + + public MpegDashService(IServerConfigurationManager serverConfig, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager, IMediaEncoder mediaEncoder, IFileSystem fileSystem, ILiveTvManager liveTvManager, IDlnaManager dlnaManager, IChannelManager channelManager, ISubtitleEncoder subtitleEncoder, INetworkManager networkManager) + : base(serverConfig, userManager, libraryManager, isoManager, mediaEncoder, fileSystem, liveTvManager, dlnaManager, channelManager, subtitleEncoder) + { + NetworkManager = networkManager; + } + + public object Get(GetMasterManifest request) + { + var result = GetAsync(request, "GET").Result; + + return result; + } + + public object Head(GetMasterManifest request) + { + var result = GetAsync(request, "HEAD").Result; + + return result; + } + + private async Task GetAsync(GetMasterManifest request, string method) + { + if (string.Equals(request.AudioCodec, "copy", StringComparison.OrdinalIgnoreCase)) + { + throw new ArgumentException("Audio codec copy is not allowed here."); + } + + if (string.Equals(request.VideoCodec, "copy", StringComparison.OrdinalIgnoreCase)) + { + throw new ArgumentException("Video codec copy is not allowed here."); + } + + if (string.IsNullOrEmpty(request.MediaSourceId)) + { + throw new ArgumentException("MediaSourceId is required"); + } + + var state = await GetState(request, CancellationToken.None).ConfigureAwait(false); + + var playlistText = string.Empty; + + if (string.Equals(method, "GET", StringComparison.OrdinalIgnoreCase)) + { + playlistText = GetManifestText(state); + } + + return ResultFactory.GetResult(playlistText, Common.Net.MimeTypes.GetMimeType("playlist.mpd"), new Dictionary()); + } + + private string GetManifestText(StreamState state) + { + var audioBitrate = state.OutputAudioBitrate ?? 0; + var videoBitrate = state.OutputVideoBitrate ?? 0; + + var builder = new StringBuilder(); + + var duration = "PT0H02M11.00S"; + + builder.AppendFormat( + "", + duration, + state.SegmentLength.ToString(CultureInfo.InvariantCulture)); + + builder.Append(""); + builder.Append(""); + + builder.AppendFormat("", duration); + builder.Append(""); + + builder.Append(""); + builder.Append(""); + + builder.Append(GetRepresentationOpenElement(state)); + + AppendSegmentList(state, builder); + + builder.Append(""); + builder.Append(""); + builder.Append(""); + + builder.Append(""); + + return builder.ToString(); + } + + private string GetRepresentationOpenElement(StreamState state) + { + return + ""; + } + + public object Get(GetDashSegment request) + { + return null; + } + + private void AppendSegmentList(StreamState state, StringBuilder builder) + { + var seconds = TimeSpan.FromTicks(state.RunTimeTicks ?? 0).TotalSeconds; + + builder.Append(""); + + var queryStringIndex = Request.RawUrl.IndexOf('?'); + var queryString = queryStringIndex == -1 ? string.Empty : Request.RawUrl.Substring(queryStringIndex); + + var index = 0; + + while (seconds > 0) + { + builder.AppendFormat("", index.ToString(UsCulture), queryString); + + seconds -= state.SegmentLength; + index++; + } + builder.Append(""); + } + + protected override string GetAudioArguments(StreamState state) + { + var codec = state.OutputAudioCodec; + + if (codec.Equals("copy", StringComparison.OrdinalIgnoreCase)) + { + return "-codec:a:0 copy"; + } + + var args = "-codec:a:0 " + codec; + + var channels = state.OutputAudioChannels; + + if (channels.HasValue) + { + args += " -ac " + channels.Value; + } + + var bitrate = state.OutputAudioBitrate; + + if (bitrate.HasValue) + { + args += " -ab " + bitrate.Value.ToString(UsCulture); + } + + args += " " + GetAudioFilterParam(state, true); + + return args; + } + + protected override string GetVideoArguments(StreamState state) + { + var codec = state.OutputVideoCodec; + + // See if we can save come cpu cycles by avoiding encoding + if (codec.Equals("copy", StringComparison.OrdinalIgnoreCase)) + { + return IsH264(state.VideoStream) ? "-codec:v:0 copy -bsf:v h264_mp4toannexb" : "-codec:v:0 copy"; + } + + var keyFrameArg = string.Format(" -force_key_frames expr:gte(t,n_forced*{0})", + state.SegmentLength.ToString(UsCulture)); + + var hasGraphicalSubs = state.SubtitleStream != null && !state.SubtitleStream.IsTextSubtitleStream; + + var args = "-codec:v:0 " + codec + " " + GetVideoQualityParam(state, "libx264", true) + keyFrameArg; + + // Add resolution params, if specified + if (!hasGraphicalSubs) + { + args += GetOutputSizeParam(state, codec, false); + } + + // This is for internal graphical subs + if (hasGraphicalSubs) + { + args += GetGraphicalSubtitleParam(state, codec); + } + + return args; + } + + protected override string GetCommandLineArguments(string outputPath, string transcodingJobId, StreamState state, bool isEncoding) + { + var threads = GetNumberOfThreads(state, false); + + var inputModifier = GetInputModifier(state); + + // If isEncoding is true we're actually starting ffmpeg + var startNumberParam = isEncoding ? GetStartNumber(state).ToString(UsCulture) : "0"; + + var args = string.Format("{0} -i {1} -map_metadata -1 -threads {2} {3} {4} -copyts -flags -global_header {5} -hls_time {6} -start_number {7} -hls_list_size {8} -y \"{9}\"", + inputModifier, + GetInputArgument(transcodingJobId, state), + threads, + GetMapArgs(state), + GetVideoArguments(state), + GetAudioArguments(state), + state.SegmentLength.ToString(UsCulture), + startNumberParam, + state.HlsListSize.ToString(UsCulture), + outputPath + ).Trim(); + + return args; + } + + /// + /// Gets the segment file extension. + /// + /// The state. + /// System.String. + protected override string GetSegmentFileExtension(StreamState state) + { + return ".ts"; + } + + protected override TranscodingJobType TranscodingJobType + { + get + { + return TranscodingJobType.Dash; + } + } + } +} diff --git a/MediaBrowser.Common/Net/MimeTypes.cs b/MediaBrowser.Common/Net/MimeTypes.cs index a135dfa30..14052e759 100644 --- a/MediaBrowser.Common/Net/MimeTypes.cs +++ b/MediaBrowser.Common/Net/MimeTypes.cs @@ -146,6 +146,10 @@ namespace MediaBrowser.Common.Net { return "video/mp2t"; } + if (ext.Equals(".mpd", StringComparison.OrdinalIgnoreCase)) + { + return "video/vnd.mpeg.dash.mpd"; + } // Catch-all for all video types that don't require specific mime types if (VideoFileExtensionsDictionary.ContainsKey(ext)) diff --git a/MediaBrowser.Controller/Entities/BaseItem.cs b/MediaBrowser.Controller/Entities/BaseItem.cs index 97564f563..d5296d7dc 100644 --- a/MediaBrowser.Controller/Entities/BaseItem.cs +++ b/MediaBrowser.Controller/Entities/BaseItem.cs @@ -1575,9 +1575,7 @@ namespace MediaBrowser.Controller.Entities { Path = path, DateModified = FileSystem.GetLastWriteTimeUtc(path), - Type = imageType, - Width = chapter.ImageWidth, - Height = chapter.ImageHeight + Type = imageType }; } diff --git a/MediaBrowser.Model/Entities/ChapterInfo.cs b/MediaBrowser.Model/Entities/ChapterInfo.cs index 9d0bbc0d9..9da7a9caa 100644 --- a/MediaBrowser.Model/Entities/ChapterInfo.cs +++ b/MediaBrowser.Model/Entities/ChapterInfo.cs @@ -23,17 +23,5 @@ namespace MediaBrowser.Model.Entities /// /// The image path. public string ImagePath { get; set; } - - /// - /// Gets or sets the height of the image. - /// - /// The height of the image. - public int? ImageHeight { get; set; } - - /// - /// Gets or sets the width of the image. - /// - /// The width of the image. - public int? ImageWidth { get; set; } } } diff --git a/MediaBrowser.Server.Implementations/Connect/ConnectManager.cs b/MediaBrowser.Server.Implementations/Connect/ConnectManager.cs index 10fe6736e..b3234aae5 100644 --- a/MediaBrowser.Server.Implementations/Connect/ConnectManager.cs +++ b/MediaBrowser.Server.Implementations/Connect/ConnectManager.cs @@ -14,6 +14,7 @@ using System; using System.Collections.Generic; using System.Globalization; using System.IO; +using System.Linq; using System.Net; using System.Text; using System.Threading; @@ -133,8 +134,7 @@ namespace MediaBrowser.Server.Implementations.Connect } catch (HttpException ex) { - if (!ex.StatusCode.HasValue || ex.StatusCode.Value != HttpStatusCode.NotFound || - ex.StatusCode.Value != HttpStatusCode.Unauthorized) + if (!ex.StatusCode.HasValue || !new[] { HttpStatusCode.NotFound, HttpStatusCode.Unauthorized }.Contains(ex.StatusCode.Value)) { throw; } @@ -147,6 +147,8 @@ namespace MediaBrowser.Server.Implementations.Connect { await CreateServerRegistration(wanApiAddress).ConfigureAwait(false); } + + await RefreshAuthorizations(CancellationToken.None).ConfigureAwait(false); } catch (Exception ex) { @@ -435,5 +437,46 @@ namespace MediaBrowser.Server.Implementations.Connect { options.RequestHeaders.Add("X-Connect-Token", ConnectAccessKey); } + + public async Task RefreshAuthorizations(CancellationToken cancellationToken) + { + var url = GetConnectUrl("ServerAuthorizations"); + + var options = new HttpRequestOptions + { + Url = url, + CancellationToken = cancellationToken + }; + + var postData = new Dictionary + { + {"serverId", ConnectServerId} + }; + + options.SetPostData(postData); + + SetServerAccessToken(options); + + try + { + // No need to examine the response + using (var stream = (await _httpClient.SendAsync(options, "POST").ConfigureAwait(false)).Content) + { + var list = _json.DeserializeFromStream>(stream); + + RefreshAuthorizations(list); + } + } + catch (Exception ex) + { + _logger.ErrorException("Error refreshing server authorizations.", ex); + } + + } + + private void RefreshAuthorizations(List list) + { + + } } }