diff options
| author | LukePulverenti <luke.pulverenti@gmail.com> | 2013-02-26 23:19:05 -0500 |
|---|---|---|
| committer | LukePulverenti <luke.pulverenti@gmail.com> | 2013-02-26 23:19:05 -0500 |
| commit | 3751e14eb1b7a0815b6ab7c2164c262e4723c52e (patch) | |
| tree | 4acac5381745de703ff0ea65bee049cf6e0e4f5b /MediaBrowser.Api/Streaming | |
| parent | a6596042a67e2d846f74542d72e81b87d1521a5d (diff) | |
restored audio
Diffstat (limited to 'MediaBrowser.Api/Streaming')
| -rw-r--r-- | MediaBrowser.Api/Streaming/AudioHandler.cs | 110 | ||||
| -rw-r--r-- | MediaBrowser.Api/Streaming/BaseProgressiveStreamingHandler.cs | 152 | ||||
| -rw-r--r-- | MediaBrowser.Api/Streaming/VideoHandler.cs | 179 |
3 files changed, 0 insertions, 441 deletions
diff --git a/MediaBrowser.Api/Streaming/AudioHandler.cs b/MediaBrowser.Api/Streaming/AudioHandler.cs deleted file mode 100644 index 2332f29d3f..0000000000 --- a/MediaBrowser.Api/Streaming/AudioHandler.cs +++ /dev/null @@ -1,110 +0,0 @@ -using MediaBrowser.Common.IO; -using MediaBrowser.Common.Net.Handlers; -using MediaBrowser.Controller.Entities.Audio; -using MediaBrowser.Controller.Resolvers; -using MediaBrowser.Model.Dto; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Net; - -namespace MediaBrowser.Api.Streaming -{ - /// <summary> - /// Providers a progressive streaming audio api - /// </summary> - public class AudioHandler : BaseProgressiveStreamingHandler<Audio> - { - /// <summary> - /// Handleses the request. - /// </summary> - /// <param name="request">The request.</param> - /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns> - public override bool HandlesRequest(HttpListenerRequest request) - { - return EntityResolutionHelper.AudioFileExtensions.Any(a => ApiService.IsApiUrlMatch("audio" + a, request)); - } - - /// <summary> - /// Gets the audio codec. - /// </summary> - /// <value>The audio codec.</value> - /// <exception cref="InvalidOperationException"></exception> - protected override AudioCodecs? AudioCodec - { - get - { - var ext = OutputFileExtension; - - if (ext.Equals(".aac", StringComparison.OrdinalIgnoreCase) || ext.Equals(".m4a", StringComparison.OrdinalIgnoreCase)) - { - return AudioCodecs.Aac; - } - if (ext.Equals(".mp3", StringComparison.OrdinalIgnoreCase)) - { - return AudioCodecs.Mp3; - } - if (ext.Equals(".wma", StringComparison.OrdinalIgnoreCase)) - { - return AudioCodecs.Wma; - } - if (ext.Equals(".oga", StringComparison.OrdinalIgnoreCase) || ext.Equals(".ogg", StringComparison.OrdinalIgnoreCase)) - { - return AudioCodecs.Vorbis; - } - - throw new InvalidOperationException(); - } - } - - /// <summary> - /// Creates arguments to pass to ffmpeg - /// </summary> - /// <param name="outputPath">The output path.</param> - /// <param name="isoMount">The iso mount.</param> - /// <returns>System.String.</returns> - /// <exception cref="System.InvalidOperationException">Only aac and mp3 audio codecs are supported.</exception> - /// <exception cref="InvalidOperationException">Only aac and mp3 audio codecs are supported.</exception> - /// <exception cref="ArgumentException">Only aac and mp3 audio codecs are supported.</exception> - protected override string GetCommandLineArguments(string outputPath, IIsoMount isoMount) - { - var audioTranscodeParams = new List<string>(); - - var outputFormat = AudioCodec; - - if (outputFormat != AudioCodecs.Aac && outputFormat != AudioCodecs.Mp3) - { - throw new InvalidOperationException("Only aac and mp3 audio codecs are supported."); - } - - if (AudioBitRate.HasValue) - { - audioTranscodeParams.Add("-ab " + AudioBitRate.Value); - } - - var channels = GetNumAudioChannelsParam(); - - if (channels.HasValue) - { - audioTranscodeParams.Add("-ac " + channels.Value); - } - - var sampleRate = GetSampleRateParam(); - - if (sampleRate.HasValue) - { - audioTranscodeParams.Add("-ar " + sampleRate.Value); - } - - const string vn = " -vn"; - - return string.Format("{0} -i {1}{2} -threads 0{5} {3} -id3v2_version 3 -write_id3v1 1 \"{4}\"", - FastSeekCommandLineParameter, - GetInputArgument(isoMount), - SlowSeekCommandLineParameter, - string.Join(" ", audioTranscodeParams.ToArray()), - outputPath, - vn).Trim(); - } - } -} diff --git a/MediaBrowser.Api/Streaming/BaseProgressiveStreamingHandler.cs b/MediaBrowser.Api/Streaming/BaseProgressiveStreamingHandler.cs deleted file mode 100644 index 91dab31d95..0000000000 --- a/MediaBrowser.Api/Streaming/BaseProgressiveStreamingHandler.cs +++ /dev/null @@ -1,152 +0,0 @@ -using MediaBrowser.Common.Extensions; -using MediaBrowser.Common.IO; -using MediaBrowser.Common.Net.Handlers; -using MediaBrowser.Controller.Entities; -using MediaBrowser.Model.Entities; -using System; -using System.IO; -using System.Net; -using System.Threading.Tasks; - -namespace MediaBrowser.Api.Streaming -{ - /// <summary> - /// Class BaseMediaHandler - /// </summary> - /// <typeparam name="TBaseItemType">The type of the T base item type.</typeparam> - public abstract class BaseProgressiveStreamingHandler<TBaseItemType> : BaseStreamingHandler<TBaseItemType> - where TBaseItemType : BaseItem, IHasMediaStreams, new() - { - /// <summary> - /// Gets the type of the transcoding job. - /// </summary> - /// <value>The type of the transcoding job.</value> - protected override TranscodingJobType TranscodingJobType - { - get { return TranscodingJobType.Progressive; } - } - - /// <summary> - /// Processes the request. - /// </summary> - /// <param name="ctx">The CTX.</param> - /// <returns>Task.</returns> - public override Task ProcessRequest(HttpListenerContext ctx) - { - HttpListenerContext = ctx; - - if (!RequiresConversion()) - { - return new StaticFileHandler(Kernel) - { - Path = LibraryItem.Path - - }.ProcessRequest(ctx); - } - - var outputPath = OutputFilePath; - - if (File.Exists(outputPath) && !Plugin.Instance.HasActiveTranscodingJob(outputPath, TranscodingJobType.Progressive)) - { - return new StaticFileHandler(Kernel) - { - Path = outputPath - - }.ProcessRequest(ctx); - } - - return base.ProcessRequest(ctx); - } - - /// <summary> - /// Requireses the conversion. - /// </summary> - /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns> - protected bool RequiresConversion() - { - return !GetBoolQueryStringParam("static"); - } - - /// <summary> - /// Writes the response to output stream. - /// </summary> - /// <param name="stream">The stream.</param> - /// <param name="responseInfo">The response info.</param> - /// <param name="contentLength">Length of the content.</param> - /// <returns>Task.</returns> - protected override async Task WriteResponseToOutputStream(Stream stream, ResponseInfo responseInfo, long? contentLength) - { - // Use the command line args with a dummy playlist path - var outputPath = OutputFilePath; - - if (!File.Exists(outputPath)) - { - await StartFFMpeg(outputPath).ConfigureAwait(false); - } - else - { - Plugin.Instance.OnTranscodeBeginRequest(outputPath, TranscodingJobType.Progressive); - } - - try - { - await StreamFile(outputPath, stream).ConfigureAwait(false); - } - catch (Exception ex) - { - //Logger.ErrorException("Error streaming media", ex); - } - finally - { - Plugin.Instance.OnTranscodeEndRequest(outputPath, TranscodingJobType.Progressive); - } - } - - /// <summary> - /// Streams the file. - /// </summary> - /// <param name="path">The path.</param> - /// <param name="outputStream">The output stream.</param> - /// <returns>Task{System.Boolean}.</returns> - private async Task StreamFile(string path, Stream outputStream) - { - var eofCount = 0; - long position = 0; - - using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, StreamDefaults.DefaultFileStreamBufferSize, FileOptions.Asynchronous)) - { - while (eofCount < 15) - { - await fs.CopyToAsync(outputStream).ConfigureAwait(false); - - var fsPosition = fs.Position; - - var bytesRead = fsPosition - position; - - //Logger.LogInfo("Streamed {0} bytes from file {1}", bytesRead, path); - - if (bytesRead == 0) - { - eofCount++; - await Task.Delay(100).ConfigureAwait(false); - } - else - { - eofCount = 0; - } - - position = fsPosition; - } - } - } - - /// <summary> - /// Deletes the partial stream files. - /// </summary> - /// <param name="outputFilePath">The output file path.</param> - protected override void DeletePartialStreamFiles(string outputFilePath) - { - File.Delete(outputFilePath); - } - } -} diff --git a/MediaBrowser.Api/Streaming/VideoHandler.cs b/MediaBrowser.Api/Streaming/VideoHandler.cs deleted file mode 100644 index da60297f23..0000000000 --- a/MediaBrowser.Api/Streaming/VideoHandler.cs +++ /dev/null @@ -1,179 +0,0 @@ -using MediaBrowser.Common.IO; -using MediaBrowser.Controller.Entities; -using MediaBrowser.Controller.Resolvers; -using System; -using System.Linq; -using System.Net; - -namespace MediaBrowser.Api.Streaming -{ - /// <summary> - /// Providers a progressive streaming video api - /// </summary> - class VideoHandler : BaseProgressiveStreamingHandler<Video> - { - /// <summary> - /// Handleses the request. - /// </summary> - /// <param name="request">The request.</param> - /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns> - public override bool HandlesRequest(HttpListenerRequest request) - { - return EntityResolutionHelper.VideoFileExtensions.Any(a => ApiService.IsApiUrlMatch("video" + a, request)); - } - - /// <summary> - /// Creates arguments to pass to ffmpeg - /// </summary> - /// <param name="outputPath">The output path.</param> - /// <param name="isoMount">The iso mount.</param> - /// <returns>System.String.</returns> - protected override string GetCommandLineArguments(string outputPath, IIsoMount isoMount) - { - var probeSize = Kernel.FFMpegManager.GetProbeSizeArgument(LibraryItem.VideoType, LibraryItem.IsoType); - - // Get the output codec name - var videoCodec = GetVideoCodec(); - - var graphicalSubtitleParam = string.Empty; - - if (SubtitleStream != null) - { - // This is for internal graphical subs - if (!SubtitleStream.IsExternal && (SubtitleStream.Codec.IndexOf("pgs", StringComparison.OrdinalIgnoreCase) != -1 || SubtitleStream.Codec.IndexOf("dvd", StringComparison.OrdinalIgnoreCase) != -1)) - { - graphicalSubtitleParam = GetInternalGraphicalSubtitleParam(SubtitleStream, videoCodec); - } - } - - return string.Format("{0} {1} -i {2}{3} -threads 0 {4} {5}{6} {7} \"{8}\"", - probeSize, - FastSeekCommandLineParameter, - GetInputArgument(isoMount), - SlowSeekCommandLineParameter, - MapArgs, - GetVideoArguments(videoCodec), - graphicalSubtitleParam, - GetAudioArguments(), - outputPath - ).Trim(); - } - - /// <summary> - /// Gets video arguments to pass to ffmpeg - /// </summary> - /// <returns>System.String.</returns> - private string GetVideoArguments(string videoCodec) - { - var args = "-vcodec " + videoCodec; - - // If we're encoding video, add additional params - if (!videoCodec.Equals("copy", StringComparison.OrdinalIgnoreCase)) - { - // Add resolution params, if specified - if (Width.HasValue || Height.HasValue || MaxHeight.HasValue || MaxWidth.HasValue) - { - args += GetOutputSizeParam(videoCodec); - } - - if (FrameRate.HasValue) - { - args += string.Format(" -r {0}", FrameRate.Value); - } - - // Add the audio bitrate - var qualityParam = GetVideoQualityParam(videoCodec); - - if (!string.IsNullOrEmpty(qualityParam)) - { - args += " " + qualityParam; - } - } - else if (IsH264(VideoStream)) - { - args += " -bsf h264_mp4toannexb"; - } - - return args; - } - - /// <summary> - /// Gets audio arguments to pass to ffmpeg - /// </summary> - /// <returns>System.String.</returns> - private string GetAudioArguments() - { - // If the video doesn't have an audio stream, return a default. - if (AudioStream == null) - { - return string.Empty; - } - - // Get the output codec name - var codec = GetAudioCodec(); - - var args = "-acodec " + codec; - - // If we're encoding audio, add additional params - if (!codec.Equals("copy", StringComparison.OrdinalIgnoreCase)) - { - // Add the number of audio channels - var channels = GetNumAudioChannelsParam(); - - if (channels.HasValue) - { - args += " -ac " + channels.Value; - } - - // Add the audio sample rate - var sampleRate = GetSampleRateParam(); - - if (sampleRate.HasValue) - { - args += " -ar " + sampleRate.Value; - } - - if (AudioBitRate.HasValue) - { - args += " -ab " + AudioBitRate.Value; - } - } - - return args; - } - - /// <summary> - /// Gets the video bitrate to specify on the command line - /// </summary> - /// <param name="videoCodec">The video codec.</param> - /// <returns>System.String.</returns> - private string GetVideoQualityParam(string videoCodec) - { - var args = string.Empty; - - // webm - if (videoCodec.Equals("libvpx", StringComparison.OrdinalIgnoreCase)) - { - args = "-g 120 -cpu-used 1 -lag-in-frames 16 -deadline realtime -slices 4 -vprofile 0"; - } - - // asf/wmv - else if (videoCodec.Equals("wmv2", StringComparison.OrdinalIgnoreCase)) - { - args = "-g 100 -qmax 15"; - } - - else if (videoCodec.Equals("libx264", StringComparison.OrdinalIgnoreCase)) - { - args = "-preset superfast"; - } - - if (VideoBitRate.HasValue) - { - args += " -b:v " + VideoBitRate; - } - - return args.Trim(); - } - } -} |
