diff options
Diffstat (limited to 'MediaBrowser.Api/Playback/BaseStreamingService.cs')
| -rw-r--r-- | MediaBrowser.Api/Playback/BaseStreamingService.cs | 291 |
1 files changed, 205 insertions, 86 deletions
diff --git a/MediaBrowser.Api/Playback/BaseStreamingService.cs b/MediaBrowser.Api/Playback/BaseStreamingService.cs index 90996296d..c04648c37 100644 --- a/MediaBrowser.Api/Playback/BaseStreamingService.cs +++ b/MediaBrowser.Api/Playback/BaseStreamingService.cs @@ -1,13 +1,14 @@ using MediaBrowser.Common.Extensions; using MediaBrowser.Common.IO; using MediaBrowser.Common.MediaInfo; -using MediaBrowser.Controller; +using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.Dto; using MediaBrowser.Controller.Entities; -using MediaBrowser.Controller.Entities.Audio; using MediaBrowser.Controller.Library; +using MediaBrowser.Controller.LiveTv; using MediaBrowser.Controller.MediaInfo; using MediaBrowser.Controller.Persistence; +using MediaBrowser.Model.Configuration; using MediaBrowser.Model.Drawing; using MediaBrowser.Model.Dto; using MediaBrowser.Model.Entities; @@ -32,7 +33,7 @@ namespace MediaBrowser.Api.Playback /// Gets or sets the application paths. /// </summary> /// <value>The application paths.</value> - protected IServerApplicationPaths ApplicationPaths { get; private set; } + protected IServerConfigurationManager ServerConfigurationManager { get; private set; } /// <summary> /// Gets or sets the user manager. @@ -62,21 +63,26 @@ namespace MediaBrowser.Api.Playback protected IFileSystem FileSystem { get; private set; } protected IItemRepository ItemRepository { get; private set; } + protected ILiveTvManager LiveTvManager { get; private set; } /// <summary> /// Initializes a new instance of the <see cref="BaseStreamingService" /> class. /// </summary> - /// <param name="appPaths">The app paths.</param> + /// <param name="serverConfig">The server configuration.</param> /// <param name="userManager">The user manager.</param> /// <param name="libraryManager">The library manager.</param> /// <param name="isoManager">The iso manager.</param> /// <param name="mediaEncoder">The media encoder.</param> - protected BaseStreamingService(IServerApplicationPaths appPaths, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager, IMediaEncoder mediaEncoder, IDtoService dtoService, IFileSystem fileSystem, IItemRepository itemRepository) + /// <param name="dtoService">The dto service.</param> + /// <param name="fileSystem">The file system.</param> + /// <param name="itemRepository">The item repository.</param> + protected BaseStreamingService(IServerConfigurationManager serverConfig, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager, IMediaEncoder mediaEncoder, IDtoService dtoService, IFileSystem fileSystem, IItemRepository itemRepository, ILiveTvManager liveTvManager) { + LiveTvManager = liveTvManager; ItemRepository = itemRepository; FileSystem = fileSystem; DtoService = dtoService; - ApplicationPaths = appPaths; + ServerConfigurationManager = serverConfig; UserManager = userManager; LibraryManager = libraryManager; IsoManager = isoManager; @@ -105,7 +111,7 @@ namespace MediaBrowser.Api.Playback /// <returns>System.String.</returns> protected virtual string GetOutputFileExtension(StreamState state) { - return Path.GetExtension(state.Url); + return Path.GetExtension(state.RequestedUrl); } /// <summary> @@ -115,7 +121,7 @@ namespace MediaBrowser.Api.Playback /// <returns>System.String.</returns> protected virtual string GetOutputFilePath(StreamState state) { - var folder = ApplicationPaths.EncodedMediaCachePath; + var folder = ServerConfigurationManager.ApplicationPaths.EncodedMediaCachePath; var outputFileExtension = GetOutputFileExtension(state); @@ -182,7 +188,7 @@ namespace MediaBrowser.Api.Playback { var args = string.Empty; - if (state.Item.LocationType == LocationType.Remote) + if (state.IsRemote || !state.HasMediaStreams) { return string.Empty; } @@ -191,6 +197,10 @@ namespace MediaBrowser.Api.Playback { args += string.Format("-map 0:{0}", state.VideoStream.Index); } + else if (!state.HasMediaStreams) + { + args += string.Format("-map 0:{0}", 0); + } else { args += "-map -0:v"; @@ -200,6 +210,10 @@ namespace MediaBrowser.Api.Playback { args += string.Format(" -map 0:{0}", state.AudioStream.Index); } + else if (!state.HasMediaStreams) + { + args += string.Format(" -map 0:{0}", 1); + } else { @@ -247,6 +261,64 @@ namespace MediaBrowser.Api.Playback } /// <summary> + /// Gets the number of threads. + /// </summary> + /// <returns>System.Int32.</returns> + /// <exception cref="System.Exception">Unrecognized EncodingQuality value.</exception> + protected int GetNumberOfThreads() + { + var quality = ServerConfigurationManager.Configuration.EncodingQuality; + + switch (quality) + { + case EncodingQuality.Auto: + return 0; + case EncodingQuality.HighSpeed: + return 2; + case EncodingQuality.HighQuality: + return 2; + case EncodingQuality.MaxQuality: + return 0; + default: + throw new Exception("Unrecognized EncodingQuality value."); + } + } + + /// <summary> + /// Gets the video bitrate to specify on the command line + /// </summary> + /// <param name="state">The state.</param> + /// <param name="videoCodec">The video codec.</param> + /// <returns>System.String.</returns> + protected string GetVideoQualityParam(StreamState state, string videoCodec) + { + var args = string.Empty; + + // webm + if (videoCodec.Equals("libvpx", StringComparison.OrdinalIgnoreCase)) + { + args = "-speed 16 -quality good -profile:v 0 -slices 8"; + } + + // asf/wmv + else if (videoCodec.Equals("wmv2", StringComparison.OrdinalIgnoreCase)) + { + args = "-g 100 -qmax 15"; + } + + else if (videoCodec.Equals("libx264", StringComparison.OrdinalIgnoreCase)) + { + args = "-preset superfast"; + } + else if (videoCodec.Equals("mpeg4", StringComparison.OrdinalIgnoreCase)) + { + args = "-mbd rd -flags +mv4+aic -trellis 2 -cmp 2 -subcmp 2 -bf 2"; + } + + return args.Trim(); + } + + /// <summary> /// If we're going to put a fixed size on the command line, this will calculate it /// </summary> /// <param name="state">The state.</param> @@ -268,14 +340,17 @@ namespace MediaBrowser.Api.Playback string.Equals(state.SubtitleStream.Codec, "ass", StringComparison.OrdinalIgnoreCase) || string.Equals(state.SubtitleStream.Codec, "ssa", StringComparison.OrdinalIgnoreCase)) { - assSubtitleParam = GetTextSubtitleParam((Video)state.Item, state.SubtitleStream, request.StartTimeTicks, performTextSubtitleConversion); + assSubtitleParam = GetTextSubtitleParam(state, request.StartTimeTicks, performTextSubtitleConversion); } } // If fixed dimensions were supplied if (request.Width.HasValue && request.Height.HasValue) { - return string.Format(" -vf \"scale=trunc({0}/2)*2:trunc({1}/2)*2{2}\"", request.Width.Value, request.Height.Value, assSubtitleParam); + var widthParam = request.Width.Value.ToString(UsCulture); + var heightParam = request.Height.Value.ToString(UsCulture); + + return string.Format(" -vf \"scale=trunc({0}/2)*2:trunc({1}/2)*2{2}\"", widthParam, heightParam, assSubtitleParam); } var isH264Output = outputVideoCodec.Equals("libx264", StringComparison.OrdinalIgnoreCase); @@ -283,33 +358,41 @@ namespace MediaBrowser.Api.Playback // If a fixed width was requested if (request.Width.HasValue) { + var widthParam = request.Width.Value.ToString(UsCulture); + return isH264Output ? - string.Format(" -vf \"scale={0}:trunc(ow/a/2)*2{1}\"", request.Width.Value, assSubtitleParam) : - string.Format(" -vf \"scale={0}:-1{1}\"", request.Width.Value, assSubtitleParam); + string.Format(" -vf \"scale={0}:trunc(ow/a/2)*2{1}\"", widthParam, assSubtitleParam) : + string.Format(" -vf \"scale={0}:-1{1}\"", widthParam, assSubtitleParam); } // If a fixed height was requested if (request.Height.HasValue) { + var heightParam = request.Height.Value.ToString(UsCulture); + return isH264Output ? - string.Format(" -vf \"scale=trunc(oh*a*2)/2:{0}{1}\"", request.Height.Value, assSubtitleParam) : - string.Format(" -vf \"scale=-1:{0}{1}\"", request.Height.Value, assSubtitleParam); + string.Format(" -vf \"scale=trunc(oh*a*2)/2:{0}{1}\"", heightParam, assSubtitleParam) : + string.Format(" -vf \"scale=-1:{0}{1}\"", heightParam, assSubtitleParam); } // If a max width was requested if (request.MaxWidth.HasValue && (!request.MaxHeight.HasValue || state.VideoStream == null)) { + var maxWidthParam = request.MaxWidth.Value.ToString(UsCulture); + return isH264Output ? - string.Format(" -vf \"scale=min(iw\\,{0}):trunc(ow/a/2)*2{1}\"", request.MaxWidth.Value, assSubtitleParam) : - string.Format(" -vf \"scale=min(iw\\,{0}):-1{1}\"", request.MaxWidth.Value, assSubtitleParam); + string.Format(" -vf \"scale=min(iw\\,{0}):trunc(ow/a/2)*2{1}\"", maxWidthParam, assSubtitleParam) : + string.Format(" -vf \"scale=min(iw\\,{0}):-1{1}\"", maxWidthParam, assSubtitleParam); } // If a max height was requested if (request.MaxHeight.HasValue && (!request.MaxWidth.HasValue || state.VideoStream == null)) { + var maxHeightParam = request.MaxHeight.Value.ToString(UsCulture); + return isH264Output ? - string.Format(" -vf \"scale=trunc(oh*a*2)/2:min(ih\\,{0}){1}\"", request.MaxHeight.Value, assSubtitleParam) : - string.Format(" -vf \"scale=-1:min(ih\\,{0}){1}\"", request.MaxHeight.Value, assSubtitleParam); + string.Format(" -vf \"scale=trunc(oh*a*2)/2:min(ih\\,{0}){1}\"", maxHeightParam, assSubtitleParam) : + string.Format(" -vf \"scale=-1:min(ih\\,{0}){1}\"", maxHeightParam, assSubtitleParam); } if (state.VideoStream == null) @@ -329,7 +412,10 @@ namespace MediaBrowser.Api.Playback // If we're encoding with libx264, it can't handle odd numbered widths or heights, so we'll have to fix that if (isH264Output) { - return string.Format(" -vf \"scale=trunc({0}/2)*2:trunc({1}/2)*2{2}\"", outputSize.Width, outputSize.Height, assSubtitleParam); + var widthParam = outputSize.Width.ToString(UsCulture); + var heightParam = outputSize.Height.ToString(UsCulture); + + return string.Format(" -vf \"scale=trunc({0}/2)*2:trunc({1}/2)*2{2}\"", widthParam, heightParam, assSubtitleParam); } // Otherwise use -vf scale since ffmpeg will ensure internally that the aspect ratio is preserved @@ -339,14 +425,14 @@ namespace MediaBrowser.Api.Playback /// <summary> /// Gets the text subtitle param. /// </summary> - /// <param name="video">The video.</param> - /// <param name="subtitleStream">The subtitle stream.</param> + /// <param name="state">The state.</param> /// <param name="startTimeTicks">The start time ticks.</param> /// <param name="performConversion">if set to <c>true</c> [perform conversion].</param> /// <returns>System.String.</returns> - protected string GetTextSubtitleParam(Video video, MediaStream subtitleStream, long? startTimeTicks, bool performConversion) + protected string GetTextSubtitleParam(StreamState state, long? startTimeTicks, bool performConversion) { - var path = subtitleStream.IsExternal ? GetConvertedAssPath(video, subtitleStream, startTimeTicks, performConversion) : GetExtractedAssPath(video, subtitleStream, startTimeTicks, performConversion); + var path = state.SubtitleStream.IsExternal ? GetConvertedAssPath(state.MediaPath, state.SubtitleStream, startTimeTicks, performConversion) : + GetExtractedAssPath(state, startTimeTicks, performConversion); if (string.IsNullOrEmpty(path)) { @@ -359,22 +445,21 @@ namespace MediaBrowser.Api.Playback /// <summary> /// Gets the extracted ass path. /// </summary> - /// <param name="video">The video.</param> - /// <param name="subtitleStream">The subtitle stream.</param> + /// <param name="state">The state.</param> /// <param name="startTimeTicks">The start time ticks.</param> /// <param name="performConversion">if set to <c>true</c> [perform conversion].</param> /// <returns>System.String.</returns> - private string GetExtractedAssPath(Video video, MediaStream subtitleStream, long? startTimeTicks, bool performConversion) + private string GetExtractedAssPath(StreamState state, long? startTimeTicks, bool performConversion) { var offset = TimeSpan.FromTicks(startTimeTicks ?? 0); - var path = Kernel.Instance.FFMpegManager.GetSubtitleCachePath(video, subtitleStream.Index, offset, ".ass"); + var path = FFMpegManager.Instance.GetSubtitleCachePath(state.MediaPath, state.SubtitleStream, offset, ".ass"); if (performConversion) { InputType type; - var inputPath = MediaEncoderHelpers.GetInputArgument(video, null, out type); + var inputPath = MediaEncoderHelpers.GetInputArgument(state.MediaPath, state.IsRemote, state.VideoType, state.IsoType, null, state.PlayableStreamFileNames, out type); try { @@ -382,7 +467,7 @@ namespace MediaBrowser.Api.Playback Directory.CreateDirectory(parentPath); - var task = MediaEncoder.ExtractTextSubtitle(inputPath, type, subtitleStream.Index, offset, path, CancellationToken.None); + var task = MediaEncoder.ExtractTextSubtitle(inputPath, type, state.SubtitleStream.Index, offset, path, CancellationToken.None); Task.WaitAll(task); } @@ -398,22 +483,16 @@ namespace MediaBrowser.Api.Playback /// <summary> /// Gets the converted ass path. /// </summary> - /// <param name="video">The video.</param> + /// <param name="mediaPath">The media path.</param> /// <param name="subtitleStream">The subtitle stream.</param> /// <param name="startTimeTicks">The start time ticks.</param> /// <param name="performConversion">if set to <c>true</c> [perform conversion].</param> /// <returns>System.String.</returns> - private string GetConvertedAssPath(Video video, MediaStream subtitleStream, long? startTimeTicks, bool performConversion) + private string GetConvertedAssPath(string mediaPath, MediaStream subtitleStream, long? startTimeTicks, bool performConversion) { - // If it's already ass, no conversion neccessary - //if (string.Equals(Path.GetExtension(subtitleStream.Path), ".ass", StringComparison.OrdinalIgnoreCase)) - //{ - // return subtitleStream.Path; - //} - var offset = TimeSpan.FromTicks(startTimeTicks ?? 0); - var path = Kernel.Instance.FFMpegManager.GetSubtitleCachePath(video, subtitleStream.Index, offset, ".ass"); + var path = FFMpegManager.Instance.GetSubtitleCachePath(mediaPath, subtitleStream, offset, ".ass"); if (performConversion) { @@ -461,25 +540,15 @@ namespace MediaBrowser.Api.Playback /// <summary> /// Gets the probe size argument. /// </summary> - /// <param name="item">The item.</param> + /// <param name="mediaPath">The media path.</param> + /// <param name="isVideo">if set to <c>true</c> [is video].</param> + /// <param name="videoType">Type of the video.</param> + /// <param name="isoType">Type of the iso.</param> /// <returns>System.String.</returns> - protected string GetProbeSizeArgument(BaseItem item) + protected string GetProbeSizeArgument(string mediaPath, bool isVideo, VideoType? videoType, IsoType? isoType) { - var type = InputType.AudioFile; - - if (item is Audio) - { - type = MediaEncoderHelpers.GetInputType(item.Path, null, null); - } - else - { - var video = item as Video; - - if (video != null) - { - type = MediaEncoderHelpers.GetInputType(item.Path, video.VideoType, video.IsoType); - } - } + var type = !isVideo ? MediaEncoderHelpers.GetInputType(mediaPath, null, null) : + MediaEncoderHelpers.GetInputType(mediaPath, videoType, isoType); return MediaEncoder.GetProbeSizeArgument(type); } @@ -589,22 +658,19 @@ namespace MediaBrowser.Api.Playback /// <summary> /// Gets the input argument. /// </summary> - /// <param name="item">The item.</param> - /// <param name="isoMount">The iso mount.</param> + /// <param name="state">The state.</param> /// <returns>System.String.</returns> - protected string GetInputArgument(BaseItem item, IIsoMount isoMount) + protected string GetInputArgument(StreamState state) { var type = InputType.AudioFile; - var inputPath = new[] { item.Path }; - - var video = item as Video; + var inputPath = new[] { state.MediaPath }; - if (video != null) + if (state.IsInputVideo) { - if (!(video.VideoType == VideoType.Iso && isoMount == null)) + if (!(state.VideoType == VideoType.Iso && state.IsoMount == null)) { - inputPath = MediaEncoderHelpers.GetInputArgument(video, isoMount, out type); + inputPath = MediaEncoderHelpers.GetInputArgument(state.MediaPath, state.IsRemote, state.VideoType, state.IsoType, state.IsoMount, state.PlayableStreamFileNames, out type); } } @@ -623,11 +689,9 @@ namespace MediaBrowser.Api.Playback Directory.CreateDirectory(parentPath); - var video = state.Item as Video; - - if (video != null && video.VideoType == VideoType.Iso && video.IsoType.HasValue && IsoManager.CanMount(video.Path)) + if (state.IsInputVideo && state.VideoType == VideoType.Iso && state.IsoType.HasValue && IsoManager.CanMount(state.MediaPath)) { - state.IsoMount = await IsoManager.Mount(video.Path, CancellationToken.None).ConfigureAwait(false); + state.IsoMount = await IsoManager.Mount(state.MediaPath, CancellationToken.None).ConfigureAwait(false); } var process = new Process @@ -652,11 +716,11 @@ namespace MediaBrowser.Api.Playback EnableRaisingEvents = true }; - ApiEntryPoint.Instance.OnTranscodeBeginning(outputPath, TranscodingJobType, process, video != null, state.Request.StartTimeTicks, state.Item.Path, state.Request.DeviceId); + ApiEntryPoint.Instance.OnTranscodeBeginning(outputPath, TranscodingJobType, process, state.IsInputVideo, state.Request.StartTimeTicks, state.MediaPath, state.Request.DeviceId); Logger.Info(process.StartInfo.FileName + " " + process.StartInfo.Arguments); - var logFilePath = Path.Combine(ApplicationPaths.LogDirectoryPath, "ffmpeg-" + Guid.NewGuid() + ".txt"); + var logFilePath = Path.Combine(ServerConfigurationManager.ApplicationPaths.LogDirectoryPath, "ffmpeg-" + Guid.NewGuid() + ".txt"); // FFMpeg writes debug/error info to stderr. This is useful when debugging so let's put it in the log directory. state.LogFileStream = FileSystem.GetFileStream(logFilePath, FileMode.Create, FileAccess.Write, FileShare.Read, true); @@ -691,13 +755,13 @@ namespace MediaBrowser.Api.Playback } // Allow a small amount of time to buffer a little - if (state.Item is Video) + if (state.IsInputVideo) { await Task.Delay(500).ConfigureAwait(false); } // This is arbitrary, but add a little buffer time when internet streaming - if (state.Item.LocationType == LocationType.Remote) + if (state.IsRemote) { await Task.Delay(4000).ConfigureAwait(false); } @@ -724,11 +788,11 @@ namespace MediaBrowser.Api.Playback /// <summary> /// Gets the user agent param. /// </summary> - /// <param name="item">The item.</param> + /// <param name="path">The path.</param> /// <returns>System.String.</returns> - protected string GetUserAgentParam(BaseItem item) + protected string GetUserAgentParam(string path) { - var useragent = GetUserAgent(item); + var useragent = GetUserAgent(path); if (!string.IsNullOrEmpty(useragent)) { @@ -741,11 +805,16 @@ namespace MediaBrowser.Api.Playback /// <summary> /// Gets the user agent. /// </summary> - /// <param name="item">The item.</param> + /// <param name="path">The path.</param> /// <returns>System.String.</returns> - protected string GetUserAgent(BaseItem item) + protected string GetUserAgent(string path) { - if (item.Path.IndexOf("apple.com", StringComparison.OrdinalIgnoreCase) != -1) + if (string.IsNullOrEmpty(path)) + { + throw new ArgumentNullException("path"); + + } + if (path.IndexOf("apple.com", StringComparison.OrdinalIgnoreCase) != -1) { return "QuickTime/7.7.4"; } @@ -784,13 +853,10 @@ namespace MediaBrowser.Api.Playback /// Gets the state. /// </summary> /// <param name="request">The request.</param> + /// <param name="cancellationToken">The cancellation token.</param> /// <returns>StreamState.</returns> - protected StreamState GetState(StreamRequest request) + protected async Task<StreamState> GetState(StreamRequest request, CancellationToken cancellationToken) { - var item = DtoService.GetItemByDtoId(request.Id); - - var media = (IHasMediaStreams)item; - var url = Request.PathInfo; if (!request.AudioCodec.HasValue) @@ -800,11 +866,62 @@ namespace MediaBrowser.Api.Playback var state = new StreamState { - Item = item, Request = request, - Url = url + RequestedUrl = url }; + BaseItem item; + + if (string.Equals(request.Type, "Recording", StringComparison.OrdinalIgnoreCase)) + { + var recording = await LiveTvManager.GetInternalRecording(request.Id, cancellationToken).ConfigureAwait(false); + + state.VideoType = VideoType.VideoFile; + state.IsInputVideo = string.Equals(recording.MediaType, MediaType.Video, StringComparison.OrdinalIgnoreCase); + state.PlayableStreamFileNames = new List<string>(); + + if (!string.IsNullOrEmpty(recording.RecordingInfo.Path) && File.Exists(recording.RecordingInfo.Path)) + { + state.MediaPath = recording.RecordingInfo.Path; + state.IsRemote = false; + } + else if (!string.IsNullOrEmpty(recording.RecordingInfo.Url)) + { + state.MediaPath = recording.RecordingInfo.Url; + state.IsRemote = true; + } + else + { + state.MediaPath = string.Format("http://localhost:{0}/mediabrowser/LiveTv/Recordings/{1}/Stream", + ServerConfigurationManager.Configuration.HttpServerPortNumber, + request.Id); + + state.IsRemote = true; + } + + item = recording; + } + else + { + item = DtoService.GetItemByDtoId(request.Id); + + state.MediaPath = item.Path; + state.IsRemote = item.LocationType == LocationType.Remote; + + var video = item as Video; + + if (video != null) + { + state.IsInputVideo = true; + state.VideoType = video.VideoType; + state.IsoType = video.IsoType; + + state.PlayableStreamFileNames = video.PlayableStreamFileNames == null + ? new List<string>() + : video.PlayableStreamFileNames.ToList(); + } + } + var videoRequest = request as VideoStreamRequest; var mediaStreams = ItemRepository.GetMediaStreams(new MediaStreamQuery @@ -829,6 +946,8 @@ namespace MediaBrowser.Api.Playback state.AudioStream = GetMediaStream(mediaStreams, null, MediaStreamType.Audio, true); } + state.HasMediaStreams = mediaStreams.Count > 0; + return state; } |
