From 1d7d52ff9e42c3efb4bb2c65e82a4a82faf9decb Mon Sep 17 00:00:00 2001 From: Mathieu Velten Date: Fri, 14 Dec 2018 10:40:55 +0100 Subject: [PATCH] Port MediaEncoding and Api.Playback from 10e57ce8d21b4516733894075001819f3cd6db6b --- .../ApplicationHost.cs | 16 +- .../Emby.Server.Implementations.csproj | 6 +- MediaBrowser.Api/ApiEntryPoint.cs | 746 ++++++++- .../Playback/BaseStreamingService.cs | 1025 ++++++++++++ .../Playback/Hls/BaseHlsService.cs | 333 ++++ .../Playback/Hls/DynamicHlsService.cs | 971 ++++++++++++ .../Playback/Hls/HlsSegmentService.cs | 163 ++ .../Playback/Hls/VideoHlsService.cs | 137 ++ MediaBrowser.Api/Playback/MediaInfoService.cs | 618 ++++++++ .../Playback/Progressive/AudioService.cs | 67 + .../BaseProgressiveStreamingService.cs | 429 +++++ .../Progressive/ProgressiveStreamWriter.cs | 193 +++ .../Playback/Progressive/VideoService.cs | 100 ++ .../Playback/StaticRemoteStreamWriter.cs | 47 + MediaBrowser.Api/Playback/StreamRequest.cs | 64 + MediaBrowser.Api/Playback/StreamState.cs | 259 +++ .../Playback/TranscodingThrottler.cs | 176 +++ .../Playback/UniversalAudioService.cs | 349 +++++ .../BdInfo/BdInfoExaminer.cs | 201 +++ .../EncodingConfigurationFactory.cs | 58 + .../Encoder/AudioEncoder.cs | 62 + .../Encoder/BaseEncoder.cs | 375 +++++ .../Encoder/EncoderValidator.cs | 219 +++ .../Encoder/EncodingJob.cs | 197 +++ .../Encoder/EncodingJobFactory.cs | 309 ++++ .../Encoder/EncodingUtils.cs | 70 + .../Encoder/FontConfigLoader.cs | 182 +++ .../Encoder/MediaEncoder.cs | 1148 ++++++++++++++ .../Encoder/VideoEncoder.cs | 66 + .../MediaBrowser.MediaEncoding.csproj | 33 + .../MediaBrowser.MediaEncoding.nuget.targets | 6 + .../Probing/FFProbeHelpers.cs | 117 ++ .../Probing/InternalMediaInfoResult.cs | 341 ++++ .../Probing/ProbeResultNormalizer.cs | 1392 +++++++++++++++++ .../Properties/AssemblyInfo.cs | 33 + .../Subtitles/AssParser.cs | 122 ++ .../Subtitles/ConfigurationExtension.cs | 29 + .../Subtitles/ISubtitleParser.cs | 17 + .../Subtitles/ISubtitleWriter.cs | 20 + .../Subtitles/JsonWriter.cs | 28 + .../Subtitles/OpenSubtitleDownloader.cs | 349 +++++ .../Subtitles/ParserValues.cs | 7 + .../Subtitles/SrtParser.cs | 92 ++ .../Subtitles/SrtWriter.cs | 39 + .../Subtitles/SsaParser.cs | 397 +++++ .../Subtitles/SubtitleEncoder.cs | 733 +++++++++ .../Subtitles/TtmlWriter.cs | 60 + .../Subtitles/VttWriter.cs | 44 + MediaBrowser.MediaEncoding/packages.config | 3 + 49 files changed, 12431 insertions(+), 17 deletions(-) create mode 100644 MediaBrowser.Api/Playback/BaseStreamingService.cs create mode 100644 MediaBrowser.Api/Playback/Hls/BaseHlsService.cs create mode 100644 MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs create mode 100644 MediaBrowser.Api/Playback/Hls/HlsSegmentService.cs create mode 100644 MediaBrowser.Api/Playback/Hls/VideoHlsService.cs create mode 100644 MediaBrowser.Api/Playback/MediaInfoService.cs create mode 100644 MediaBrowser.Api/Playback/Progressive/AudioService.cs create mode 100644 MediaBrowser.Api/Playback/Progressive/BaseProgressiveStreamingService.cs create mode 100644 MediaBrowser.Api/Playback/Progressive/ProgressiveStreamWriter.cs create mode 100644 MediaBrowser.Api/Playback/Progressive/VideoService.cs create mode 100644 MediaBrowser.Api/Playback/StaticRemoteStreamWriter.cs create mode 100644 MediaBrowser.Api/Playback/StreamRequest.cs create mode 100644 MediaBrowser.Api/Playback/StreamState.cs create mode 100644 MediaBrowser.Api/Playback/TranscodingThrottler.cs create mode 100644 MediaBrowser.Api/Playback/UniversalAudioService.cs create mode 100644 MediaBrowser.MediaEncoding/BdInfo/BdInfoExaminer.cs create mode 100644 MediaBrowser.MediaEncoding/Configuration/EncodingConfigurationFactory.cs create mode 100644 MediaBrowser.MediaEncoding/Encoder/AudioEncoder.cs create mode 100644 MediaBrowser.MediaEncoding/Encoder/BaseEncoder.cs create mode 100644 MediaBrowser.MediaEncoding/Encoder/EncoderValidator.cs create mode 100644 MediaBrowser.MediaEncoding/Encoder/EncodingJob.cs create mode 100644 MediaBrowser.MediaEncoding/Encoder/EncodingJobFactory.cs create mode 100644 MediaBrowser.MediaEncoding/Encoder/EncodingUtils.cs create mode 100644 MediaBrowser.MediaEncoding/Encoder/FontConfigLoader.cs create mode 100644 MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs create mode 100644 MediaBrowser.MediaEncoding/Encoder/VideoEncoder.cs create mode 100644 MediaBrowser.MediaEncoding/MediaBrowser.MediaEncoding.csproj create mode 100644 MediaBrowser.MediaEncoding/MediaBrowser.MediaEncoding.nuget.targets create mode 100644 MediaBrowser.MediaEncoding/Probing/FFProbeHelpers.cs create mode 100644 MediaBrowser.MediaEncoding/Probing/InternalMediaInfoResult.cs create mode 100644 MediaBrowser.MediaEncoding/Probing/ProbeResultNormalizer.cs create mode 100644 MediaBrowser.MediaEncoding/Properties/AssemblyInfo.cs create mode 100644 MediaBrowser.MediaEncoding/Subtitles/AssParser.cs create mode 100644 MediaBrowser.MediaEncoding/Subtitles/ConfigurationExtension.cs create mode 100644 MediaBrowser.MediaEncoding/Subtitles/ISubtitleParser.cs create mode 100644 MediaBrowser.MediaEncoding/Subtitles/ISubtitleWriter.cs create mode 100644 MediaBrowser.MediaEncoding/Subtitles/JsonWriter.cs create mode 100644 MediaBrowser.MediaEncoding/Subtitles/OpenSubtitleDownloader.cs create mode 100644 MediaBrowser.MediaEncoding/Subtitles/ParserValues.cs create mode 100644 MediaBrowser.MediaEncoding/Subtitles/SrtParser.cs create mode 100644 MediaBrowser.MediaEncoding/Subtitles/SrtWriter.cs create mode 100644 MediaBrowser.MediaEncoding/Subtitles/SsaParser.cs create mode 100644 MediaBrowser.MediaEncoding/Subtitles/SubtitleEncoder.cs create mode 100644 MediaBrowser.MediaEncoding/Subtitles/TtmlWriter.cs create mode 100644 MediaBrowser.MediaEncoding/Subtitles/VttWriter.cs create mode 100644 MediaBrowser.MediaEncoding/packages.config diff --git a/Emby.Server.Implementations/ApplicationHost.cs b/Emby.Server.Implementations/ApplicationHost.cs index de1a1dfcde..db716894ee 100644 --- a/Emby.Server.Implementations/ApplicationHost.cs +++ b/Emby.Server.Implementations/ApplicationHost.cs @@ -21,7 +21,6 @@ using Emby.Server.Implementations.IO; using Emby.Server.Implementations.Library; using Emby.Server.Implementations.LiveTv; using Emby.Server.Implementations.Localization; -using Emby.Server.Implementations.MediaEncoder; using Emby.Server.Implementations.Net; using Emby.Notifications; using Emby.Server.Implementations.Playlists; @@ -34,7 +33,6 @@ using Emby.Server.Implementations.Threading; using Emby.Server.Implementations.TV; using Emby.Server.Implementations.Updates; using Emby.Server.Implementations.Xml; -using Emby.Server.MediaEncoding.Subtitles; using MediaBrowser.Api; using MediaBrowser.Common; using MediaBrowser.Common.Configuration; @@ -1040,7 +1038,7 @@ namespace Emby.Server.Implementations RegisterMediaEncoder(assemblyInfo); - EncodingManager = new EncodingManager(FileSystemManager, Logger, MediaEncoder, ChapterManager, LibraryManager); + EncodingManager = new Emby.Server.Implementations.MediaEncoder.EncodingManager(FileSystemManager, Logger, MediaEncoder, ChapterManager, LibraryManager); RegisterSingleInstance(EncodingManager); var activityLogRepo = GetActivityLogRepository(); @@ -1054,7 +1052,7 @@ namespace Emby.Server.Implementations AuthService = new AuthService(UserManager, authContext, ServerConfigurationManager, ConnectManager, SessionManager, NetworkManager); RegisterSingleInstance(AuthService); - SubtitleEncoder = new SubtitleEncoder(LibraryManager, LogManager.GetLogger("SubtitleEncoder"), ApplicationPaths, FileSystemManager, MediaEncoder, JsonSerializer, HttpClient, MediaSourceManager, ProcessFactory, TextEncoding); + SubtitleEncoder = new MediaBrowser.MediaEncoding.Subtitles.SubtitleEncoder(LibraryManager, LogManager.GetLogger("SubtitleEncoder"), ApplicationPaths, FileSystemManager, MediaEncoder, JsonSerializer, HttpClient, MediaSourceManager, ProcessFactory, TextEncoding); RegisterSingleInstance(SubtitleEncoder); RegisterSingleInstance(CreateResourceFileManager()); @@ -1270,7 +1268,7 @@ namespace Emby.Server.Implementations probePath = info.ProbePath; var hasExternalEncoder = string.Equals(info.Version, "external", StringComparison.OrdinalIgnoreCase); - var mediaEncoder = new MediaEncoding.Encoder.MediaEncoder(LogManager.GetLogger("MediaEncoder"), + var mediaEncoder = new MediaBrowser.MediaEncoding.Encoder.MediaEncoder(LogManager.GetLogger("MediaEncoder"), JsonSerializer, encoderPath, probePath, @@ -1287,10 +1285,8 @@ namespace Emby.Server.Implementations HttpClient, ZipClient, ProcessFactory, - EnvironmentInfo, - BlurayExaminer, - assemblyInfo, - this); + 5000, false, + EnvironmentInfo); MediaEncoder = mediaEncoder; RegisterSingleInstance(MediaEncoder); @@ -1777,7 +1773,7 @@ namespace Emby.Server.Implementations list.Add(GetAssembly(typeof(InstallationManager))); // MediaEncoding - list.Add(GetAssembly(typeof(MediaEncoding.Encoder.MediaEncoder))); + list.Add(GetAssembly(typeof(MediaBrowser.MediaEncoding.Encoder.MediaEncoder))); // Dlna list.Add(GetAssembly(typeof(DlnaEntryPoint))); diff --git a/Emby.Server.Implementations/Emby.Server.Implementations.csproj b/Emby.Server.Implementations/Emby.Server.Implementations.csproj index b3b4769073..97909fd18f 100644 --- a/Emby.Server.Implementations/Emby.Server.Implementations.csproj +++ b/Emby.Server.Implementations/Emby.Server.Implementations.csproj @@ -18,6 +18,7 @@ + @@ -148,10 +149,5 @@ - - - ..\ThirdParty\emby\Emby.Server.MediaEncoding.dll - - diff --git a/MediaBrowser.Api/ApiEntryPoint.cs b/MediaBrowser.Api/ApiEntryPoint.cs index 9676a2c2af..5aa803b9b6 100644 --- a/MediaBrowser.Api/ApiEntryPoint.cs +++ b/MediaBrowser.Api/ApiEntryPoint.cs @@ -10,6 +10,15 @@ using MediaBrowser.Model.Logging; using MediaBrowser.Model.Threading; using System.Collections.Generic; using System.Threading; +using MediaBrowser.Controller.MediaEncoding; +using MediaBrowser.Api.Playback; +using System.IO; +using MediaBrowser.Model.Session; +using System.Linq; +using System.Threading.Tasks; +using MediaBrowser.Model.Dto; +using MediaBrowser.Model.Configuration; +using MediaBrowser.Common.Configuration; namespace MediaBrowser.Api { @@ -41,6 +50,11 @@ namespace MediaBrowser.Api public readonly ITimerFactory TimerFactory; public readonly IProcessFactory ProcessFactory; + /// + /// The active transcoding jobs + /// + private readonly List _activeTranscodingJobs = new List(); + private readonly Dictionary _transcodingLocks = new Dictionary(); @@ -64,6 +78,8 @@ namespace MediaBrowser.Api ResultFactory = resultFactory; Instance = this; + _sessionManager.PlaybackProgress += _sessionManager_PlaybackProgress; + _sessionManager.PlaybackStart += _sessionManager_PlaybackStart; } public static string[] Split(string value, char separator, bool removeEmpty) @@ -81,13 +97,739 @@ namespace MediaBrowser.Api return value.Split(separator); } - public void Run() + public SemaphoreSlim GetTranscodingLock(string outputPath) { - + lock (_transcodingLocks) + { + SemaphoreSlim result; + if (!_transcodingLocks.TryGetValue(outputPath, out result)) + { + result = new SemaphoreSlim(1, 1); + _transcodingLocks[outputPath] = result; + } + + return result; + } } + private void _sessionManager_PlaybackStart(object sender, PlaybackProgressEventArgs e) + { + if (!string.IsNullOrWhiteSpace(e.PlaySessionId)) + { + PingTranscodingJob(e.PlaySessionId, e.IsPaused); + } + } + + void _sessionManager_PlaybackProgress(object sender, PlaybackProgressEventArgs e) + { + if (!string.IsNullOrWhiteSpace(e.PlaySessionId)) + { + PingTranscodingJob(e.PlaySessionId, e.IsPaused); + } + } + + /// + /// Runs this instance. + /// + public void Run() + { + try + { + DeleteEncodedMediaCache(); + } + catch (FileNotFoundException) + { + // Don't clutter the log + } + catch (IOException) + { + // Don't clutter the log + } + catch (Exception ex) + { + Logger.ErrorException("Error deleting encoded media cache", ex); + } + } + + public EncodingOptions GetEncodingOptions() + { + return ConfigurationManagerExtensions.GetConfiguration(_config, "encoding"); + } + + /// + /// Deletes the encoded media cache. + /// + private void DeleteEncodedMediaCache() + { + var path = _config.ApplicationPaths.TranscodingTempPath; + + foreach (var file in _fileSystem.GetFilePaths(path, true) + .ToList()) + { + _fileSystem.DeleteFile(file); + } + } + + /// + /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. + /// public void Dispose() { + Dispose(true); + GC.SuppressFinalize(this); + } + + /// + /// Releases unmanaged and - optionally - managed resources. + /// + /// true to release both managed and unmanaged resources; false to release only unmanaged resources. + protected virtual void Dispose(bool dispose) + { + var list = _activeTranscodingJobs.ToList(); + var jobCount = list.Count; + + Parallel.ForEach(list, j => KillTranscodingJob(j, false, path => true)); + + // Try to allow for some time to kill the ffmpeg processes and delete the partial stream files + if (jobCount > 0) + { + var task = Task.Delay(1000); + Task.WaitAll(task); + } + } + + + /// + /// Called when [transcode beginning]. + /// + /// The path. + /// The play session identifier. + /// The live stream identifier. + /// The transcoding job identifier. + /// The type. + /// The process. + /// The device id. + /// The state. + /// The cancellation token source. + /// TranscodingJob. + public TranscodingJob OnTranscodeBeginning(string path, + string playSessionId, + string liveStreamId, + string transcodingJobId, + TranscodingJobType type, + IProcess process, + string deviceId, + StreamState state, + CancellationTokenSource cancellationTokenSource) + { + lock (_activeTranscodingJobs) + { + var job = new TranscodingJob(Logger, TimerFactory) + { + Type = type, + Path = path, + Process = process, + ActiveRequestCount = 1, + DeviceId = deviceId, + CancellationTokenSource = cancellationTokenSource, + Id = transcodingJobId, + PlaySessionId = playSessionId, + LiveStreamId = liveStreamId, + MediaSource = state.MediaSource + }; + + _activeTranscodingJobs.Add(job); + + ReportTranscodingProgress(job, state, null, null, null, null, null); + + return job; + } + } + + public void ReportTranscodingProgress(TranscodingJob job, StreamState state, TimeSpan? transcodingPosition, float? framerate, double? percentComplete, long? bytesTranscoded, int? bitRate) + { + var ticks = transcodingPosition.HasValue ? transcodingPosition.Value.Ticks : (long?)null; + + if (job != null) + { + job.Framerate = framerate; + job.CompletionPercentage = percentComplete; + job.TranscodingPositionTicks = ticks; + job.BytesTranscoded = bytesTranscoded; + job.BitRate = bitRate; + } + + var deviceId = state.Request.DeviceId; + + if (!string.IsNullOrWhiteSpace(deviceId)) + { + var audioCodec = state.ActualOutputAudioCodec; + var videoCodec = state.ActualOutputVideoCodec; + + _sessionManager.ReportTranscodingInfo(deviceId, new TranscodingInfo + { + Bitrate = bitRate ?? state.TotalOutputBitrate, + AudioCodec = audioCodec, + VideoCodec = videoCodec, + Container = state.OutputContainer, + Framerate = framerate, + CompletionPercentage = percentComplete, + Width = state.OutputWidth, + Height = state.OutputHeight, + AudioChannels = state.OutputAudioChannels, + IsAudioDirect = string.Equals(state.OutputAudioCodec, "copy", StringComparison.OrdinalIgnoreCase), + IsVideoDirect = string.Equals(state.OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase), + TranscodeReasons = state.TranscodeReasons + }); + } + } + + /// + /// + /// The progressive + /// + /// Called when [transcode failed to start]. + /// + /// The path. + /// The type. + /// The state. + public void OnTranscodeFailedToStart(string path, TranscodingJobType type, StreamState state) + { + lock (_activeTranscodingJobs) + { + var job = _activeTranscodingJobs.FirstOrDefault(j => j.Type == type && string.Equals(j.Path, path, StringComparison.OrdinalIgnoreCase)); + + if (job != null) + { + _activeTranscodingJobs.Remove(job); + } + } + + lock (_transcodingLocks) + { + _transcodingLocks.Remove(path); + } + + if (!string.IsNullOrWhiteSpace(state.Request.DeviceId)) + { + _sessionManager.ClearTranscodingInfo(state.Request.DeviceId); + } + } + + /// + /// Determines whether [has active transcoding job] [the specified path]. + /// + /// The path. + /// The type. + /// true if [has active transcoding job] [the specified path]; otherwise, false. + public bool HasActiveTranscodingJob(string path, TranscodingJobType type) + { + return GetTranscodingJob(path, type) != null; + } + + public TranscodingJob GetTranscodingJob(string path, TranscodingJobType type) + { + lock (_activeTranscodingJobs) + { + return _activeTranscodingJobs.FirstOrDefault(j => j.Type == type && string.Equals(j.Path, path, StringComparison.OrdinalIgnoreCase)); + } + } + + public TranscodingJob GetTranscodingJob(string playSessionId) + { + lock (_activeTranscodingJobs) + { + return _activeTranscodingJobs.FirstOrDefault(j => string.Equals(j.PlaySessionId, playSessionId, StringComparison.OrdinalIgnoreCase)); + } + } + + /// + /// Called when [transcode begin request]. + /// + /// The path. + /// The type. + public TranscodingJob OnTranscodeBeginRequest(string path, TranscodingJobType type) + { + lock (_activeTranscodingJobs) + { + var job = _activeTranscodingJobs.FirstOrDefault(j => j.Type == type && string.Equals(j.Path, path, StringComparison.OrdinalIgnoreCase)); + + if (job == null) + { + return null; + } + + OnTranscodeBeginRequest(job); + + return job; + } + } + + public void OnTranscodeBeginRequest(TranscodingJob job) + { + job.ActiveRequestCount++; + + if (string.IsNullOrWhiteSpace(job.PlaySessionId) || job.Type == TranscodingJobType.Progressive) + { + job.StopKillTimer(); + } + } + + public void OnTranscodeEndRequest(TranscodingJob job) + { + job.ActiveRequestCount--; + //Logger.Debug("OnTranscodeEndRequest job.ActiveRequestCount={0}", job.ActiveRequestCount); + if (job.ActiveRequestCount <= 0) + { + PingTimer(job, false); + } + } + internal void PingTranscodingJob(string playSessionId, bool? isUserPaused) + { + if (string.IsNullOrEmpty(playSessionId)) + { + throw new ArgumentNullException("playSessionId"); + } + + //Logger.Debug("PingTranscodingJob PlaySessionId={0} isUsedPaused: {1}", playSessionId, isUserPaused); + + List jobs; + + lock (_activeTranscodingJobs) + { + // This is really only needed for HLS. + // Progressive streams can stop on their own reliably + jobs = _activeTranscodingJobs.Where(j => string.Equals(playSessionId, j.PlaySessionId, StringComparison.OrdinalIgnoreCase)).ToList(); + } + + foreach (var job in jobs) + { + if (isUserPaused.HasValue) + { + //Logger.Debug("Setting job.IsUserPaused to {0}. jobId: {1}", isUserPaused, job.Id); + job.IsUserPaused = isUserPaused.Value; + } + PingTimer(job, true); + } + } + + private void PingTimer(TranscodingJob job, bool isProgressCheckIn) + { + if (job.HasExited) + { + job.StopKillTimer(); + return; + } + + var timerDuration = 10000; + + if (job.Type != TranscodingJobType.Progressive) + { + timerDuration = 60000; + } + + job.PingTimeout = timerDuration; + job.LastPingDate = DateTime.UtcNow; + + // Don't start the timer for playback checkins with progressive streaming + if (job.Type != TranscodingJobType.Progressive || !isProgressCheckIn) + { + job.StartKillTimer(OnTranscodeKillTimerStopped); + } + else + { + job.ChangeKillTimerIfStarted(); + } + } + + /// + /// Called when [transcode kill timer stopped]. + /// + /// The state. + private void OnTranscodeKillTimerStopped(object state) + { + var job = (TranscodingJob)state; + + if (!job.HasExited && job.Type != TranscodingJobType.Progressive) + { + var timeSinceLastPing = (DateTime.UtcNow - job.LastPingDate).TotalMilliseconds; + + if (timeSinceLastPing < job.PingTimeout) + { + job.StartKillTimer(OnTranscodeKillTimerStopped, job.PingTimeout); + return; + } + } + + Logger.Info("Transcoding kill timer stopped for JobId {0} PlaySessionId {1}. Killing transcoding", job.Id, job.PlaySessionId); + + KillTranscodingJob(job, true, path => true); + } + + /// + /// Kills the single transcoding job. + /// + /// The device id. + /// The play session identifier. + /// The delete files. + /// Task. + internal void KillTranscodingJobs(string deviceId, string playSessionId, Func deleteFiles) + { + KillTranscodingJobs(j => + { + if (!string.IsNullOrWhiteSpace(playSessionId)) + { + return string.Equals(playSessionId, j.PlaySessionId, StringComparison.OrdinalIgnoreCase); + } + + return string.Equals(deviceId, j.DeviceId, StringComparison.OrdinalIgnoreCase); + + }, deleteFiles); + } + + /// + /// Kills the transcoding jobs. + /// + /// The kill job. + /// The delete files. + /// Task. + private void KillTranscodingJobs(Func killJob, Func deleteFiles) + { + var jobs = new List(); + + lock (_activeTranscodingJobs) + { + // This is really only needed for HLS. + // Progressive streams can stop on their own reliably + jobs.AddRange(_activeTranscodingJobs.Where(killJob)); + } + + if (jobs.Count == 0) + { + return; + } + + foreach (var job in jobs) + { + KillTranscodingJob(job, false, deleteFiles); + } + } + + /// + /// Kills the transcoding job. + /// + /// The job. + /// if set to true [close live stream]. + /// The delete. + private async void KillTranscodingJob(TranscodingJob job, bool closeLiveStream, Func delete) + { + job.DisposeKillTimer(); + + Logger.Debug("KillTranscodingJob - JobId {0} PlaySessionId {1}. Killing transcoding", job.Id, job.PlaySessionId); + + lock (_activeTranscodingJobs) + { + _activeTranscodingJobs.Remove(job); + + if (!job.CancellationTokenSource.IsCancellationRequested) + { + job.CancellationTokenSource.Cancel(); + } + } + + lock (_transcodingLocks) + { + _transcodingLocks.Remove(job.Path); + } + + lock (job.ProcessLock) + { + if (job.TranscodingThrottler != null) + { + job.TranscodingThrottler.Stop(); + } + + var process = job.Process; + + var hasExited = job.HasExited; + + if (!hasExited) + { + try + { + Logger.Info("Stopping ffmpeg process with q command for {0}", job.Path); + + //process.Kill(); + process.StandardInput.WriteLine("q"); + + // Need to wait because killing is asynchronous + if (!process.WaitForExit(5000)) + { + Logger.Info("Killing ffmpeg process for {0}", job.Path); + process.Kill(); + } + } + catch (Exception ex) + { + Logger.ErrorException("Error killing transcoding job for {0}", ex, job.Path); + } + } + } + + if (delete(job.Path)) + { + DeletePartialStreamFiles(job.Path, job.Type, 0, 1500); + } + + if (closeLiveStream && !string.IsNullOrWhiteSpace(job.LiveStreamId)) + { + try + { + await _mediaSourceManager.CloseLiveStream(job.LiveStreamId).ConfigureAwait(false); + } + catch (Exception ex) + { + Logger.ErrorException("Error closing live stream for {0}", ex, job.Path); + } + } + } + + private async void DeletePartialStreamFiles(string path, TranscodingJobType jobType, int retryCount, int delayMs) + { + if (retryCount >= 10) + { + return; + } + + Logger.Info("Deleting partial stream file(s) {0}", path); + + await Task.Delay(delayMs).ConfigureAwait(false); + + try + { + if (jobType == TranscodingJobType.Progressive) + { + DeleteProgressivePartialStreamFiles(path); + } + else + { + DeleteHlsPartialStreamFiles(path); + } + } + catch (FileNotFoundException) + { + + } + catch (IOException) + { + //Logger.ErrorException("Error deleting partial stream file(s) {0}", ex, path); + + DeletePartialStreamFiles(path, jobType, retryCount + 1, 500); + } + catch + { + //Logger.ErrorException("Error deleting partial stream file(s) {0}", ex, path); + } + } + + /// + /// Deletes the progressive partial stream files. + /// + /// The output file path. + private void DeleteProgressivePartialStreamFiles(string outputFilePath) + { + _fileSystem.DeleteFile(outputFilePath); + } + + /// + /// Deletes the HLS partial stream files. + /// + /// The output file path. + private void DeleteHlsPartialStreamFiles(string outputFilePath) + { + var directory = _fileSystem.GetDirectoryName(outputFilePath); + var name = Path.GetFileNameWithoutExtension(outputFilePath); + + var filesToDelete = _fileSystem.GetFilePaths(directory) + .Where(f => f.IndexOf(name, StringComparison.OrdinalIgnoreCase) != -1) + .ToList(); + + Exception e = null; + + foreach (var file in filesToDelete) + { + try + { + //Logger.Debug("Deleting HLS file {0}", file); + _fileSystem.DeleteFile(file); + } + catch (FileNotFoundException) + { + + } + catch (IOException ex) + { + e = ex; + //Logger.ErrorException("Error deleting HLS file {0}", ex, file); + } + } + + if (e != null) + { + throw e; + } + } + } + + /// + /// Class TranscodingJob + /// + public class TranscodingJob + { + /// + /// Gets or sets the play session identifier. + /// + /// The play session identifier. + public string PlaySessionId { get; set; } + /// + /// Gets or sets the live stream identifier. + /// + /// The live stream identifier. + public string LiveStreamId { get; set; } + + public bool IsLiveOutput { get; set; } + + /// + /// Gets or sets the path. + /// + /// The path. + public MediaSourceInfo MediaSource { get; set; } + public string Path { get; set; } + /// + /// Gets or sets the type. + /// + /// The type. + public TranscodingJobType Type { get; set; } + /// + /// Gets or sets the process. + /// + /// The process. + public IProcess Process { get; set; } + public ILogger Logger { get; private set; } + /// + /// Gets or sets the active request count. + /// + /// The active request count. + public int ActiveRequestCount { get; set; } + /// + /// Gets or sets the kill timer. + /// + /// The kill timer. + private ITimer KillTimer { get; set; } + + private readonly ITimerFactory _timerFactory; + + public string DeviceId { get; set; } + + public CancellationTokenSource CancellationTokenSource { get; set; } + + public object ProcessLock = new object(); + + public bool HasExited { get; set; } + public bool IsUserPaused { get; set; } + + public string Id { get; set; } + + public float? Framerate { get; set; } + public double? CompletionPercentage { get; set; } + + public long? BytesDownloaded { get; set; } + public long? BytesTranscoded { get; set; } + public int? BitRate { get; set; } + + public long? TranscodingPositionTicks { get; set; } + public long? DownloadPositionTicks { get; set; } + + public TranscodingThrottler TranscodingThrottler { get; set; } + + private readonly object _timerLock = new object(); + + public DateTime LastPingDate { get; set; } + public int PingTimeout { get; set; } + + public TranscodingJob(ILogger logger, ITimerFactory timerFactory) + { + Logger = logger; + _timerFactory = timerFactory; + } + + public void StopKillTimer() + { + lock (_timerLock) + { + if (KillTimer != null) + { + KillTimer.Change(Timeout.Infinite, Timeout.Infinite); + } + } + } + + public void DisposeKillTimer() + { + lock (_timerLock) + { + if (KillTimer != null) + { + KillTimer.Dispose(); + KillTimer = null; + } + } + } + + public void StartKillTimer(Action callback) + { + StartKillTimer(callback, PingTimeout); + } + + public void StartKillTimer(Action callback, int intervalMs) + { + if (HasExited) + { + return; + } + + lock (_timerLock) + { + if (KillTimer == null) + { + //Logger.Debug("Starting kill timer at {0}ms. JobId {1} PlaySessionId {2}", intervalMs, Id, PlaySessionId); + KillTimer = _timerFactory.Create(callback, this, intervalMs, Timeout.Infinite); + } + else + { + //Logger.Debug("Changing kill timer to {0}ms. JobId {1} PlaySessionId {2}", intervalMs, Id, PlaySessionId); + KillTimer.Change(intervalMs, Timeout.Infinite); + } + } + } + + public void ChangeKillTimerIfStarted() + { + if (HasExited) + { + return; + } + + lock (_timerLock) + { + if (KillTimer != null) + { + var intervalMs = PingTimeout; + + //Logger.Debug("Changing kill timer to {0}ms. JobId {1} PlaySessionId {2}", intervalMs, Id, PlaySessionId); + KillTimer.Change(intervalMs, Timeout.Infinite); + } + } } } } diff --git a/MediaBrowser.Api/Playback/BaseStreamingService.cs b/MediaBrowser.Api/Playback/BaseStreamingService.cs new file mode 100644 index 0000000000..9c2e0e9d8f --- /dev/null +++ b/MediaBrowser.Api/Playback/BaseStreamingService.cs @@ -0,0 +1,1025 @@ +using MediaBrowser.Common.Extensions; +using MediaBrowser.Controller.Configuration; +using MediaBrowser.Controller.Devices; +using MediaBrowser.Controller.Dlna; +using MediaBrowser.Controller.Library; +using MediaBrowser.Controller.MediaEncoding; +using MediaBrowser.Model.Dlna; +using MediaBrowser.Model.Dto; +using MediaBrowser.Model.Entities; +using MediaBrowser.Model.Extensions; +using MediaBrowser.Model.IO; +using MediaBrowser.Model.MediaInfo; +using MediaBrowser.Model.Serialization; +using System; +using System.Collections.Generic; +using System.Globalization; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using MediaBrowser.Common.Net; +using MediaBrowser.Controller; +using MediaBrowser.Controller.Net; +using MediaBrowser.Model.Configuration; +using MediaBrowser.Model.Diagnostics; + +namespace MediaBrowser.Api.Playback +{ + /// + /// Class BaseStreamingService + /// + public abstract class BaseStreamingService : BaseApiService + { + /// + /// Gets or sets the application paths. + /// + /// The application paths. + protected IServerConfigurationManager ServerConfigurationManager { get; private set; } + + /// + /// Gets or sets the user manager. + /// + /// The user manager. + protected IUserManager UserManager { get; private set; } + + /// + /// Gets or sets the library manager. + /// + /// The library manager. + protected ILibraryManager LibraryManager { get; private set; } + + /// + /// Gets or sets the iso manager. + /// + /// The iso manager. + protected IIsoManager IsoManager { get; private set; } + + /// + /// Gets or sets the media encoder. + /// + /// The media encoder. + protected IMediaEncoder MediaEncoder { get; private set; } + + protected IFileSystem FileSystem { get; private set; } + + protected IDlnaManager DlnaManager { get; private set; } + protected IDeviceManager DeviceManager { get; private set; } + protected ISubtitleEncoder SubtitleEncoder { get; private set; } + protected IMediaSourceManager MediaSourceManager { get; private set; } + protected IZipClient ZipClient { get; private set; } + protected IJsonSerializer JsonSerializer { get; private set; } + + public static IServerApplicationHost AppHost; + public static IHttpClient HttpClient; + protected IAuthorizationContext AuthorizationContext { get; private set; } + + protected EncodingHelper EncodingHelper { get; set; } + + /// + /// Initializes a new instance of the class. + /// + protected BaseStreamingService(IServerConfigurationManager serverConfig, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager, IMediaEncoder mediaEncoder, IFileSystem fileSystem, IDlnaManager dlnaManager, ISubtitleEncoder subtitleEncoder, IDeviceManager deviceManager, IMediaSourceManager mediaSourceManager, IZipClient zipClient, IJsonSerializer jsonSerializer, IAuthorizationContext authorizationContext) + { + JsonSerializer = jsonSerializer; + AuthorizationContext = authorizationContext; + ZipClient = zipClient; + MediaSourceManager = mediaSourceManager; + DeviceManager = deviceManager; + SubtitleEncoder = subtitleEncoder; + DlnaManager = dlnaManager; + FileSystem = fileSystem; + ServerConfigurationManager = serverConfig; + UserManager = userManager; + LibraryManager = libraryManager; + IsoManager = isoManager; + MediaEncoder = mediaEncoder; + EncodingHelper = new EncodingHelper(MediaEncoder, FileSystem, SubtitleEncoder); + } + + /// + /// Gets the command line arguments. + /// + protected abstract string GetCommandLineArguments(string outputPath, EncodingOptions encodingOptions, StreamState state, bool isEncoding); + + /// + /// Gets the type of the transcoding job. + /// + /// The type of the transcoding job. + protected abstract TranscodingJobType TranscodingJobType { get; } + + /// + /// Gets the output file extension. + /// + /// The state. + /// System.String. + protected virtual string GetOutputFileExtension(StreamState state) + { + return Path.GetExtension(state.RequestedUrl); + } + + /// + /// Gets the output file path. + /// + private string GetOutputFilePath(StreamState state, EncodingOptions encodingOptions, string outputFileExtension) + { + var folder = ServerConfigurationManager.ApplicationPaths.TranscodingTempPath; + + var data = GetCommandLineArguments("dummy\\dummy", encodingOptions, state, false); + + data += "-" + (state.Request.DeviceId ?? string.Empty); + data += "-" + (state.Request.PlaySessionId ?? string.Empty); + + var dataHash = data.GetMD5().ToString("N"); + + if (EnableOutputInSubFolder) + { + return Path.Combine(folder, dataHash, dataHash + (outputFileExtension ?? string.Empty).ToLower()); + } + + return Path.Combine(folder, dataHash + (outputFileExtension ?? string.Empty).ToLower()); + } + + protected virtual bool EnableOutputInSubFolder + { + get { return false; } + } + + protected readonly CultureInfo UsCulture = new CultureInfo("en-US"); + + protected virtual string GetDefaultH264Preset() + { + return "superfast"; + } + + private async Task AcquireResources(StreamState state, CancellationTokenSource cancellationTokenSource) + { + if (state.VideoType == VideoType.Iso && state.IsoType.HasValue && IsoManager.CanMount(state.MediaPath)) + { + state.IsoMount = await IsoManager.Mount(state.MediaPath, cancellationTokenSource.Token).ConfigureAwait(false); + } + + if (state.MediaSource.RequiresOpening && string.IsNullOrWhiteSpace(state.Request.LiveStreamId)) + { + var liveStreamResponse = await MediaSourceManager.OpenLiveStream(new LiveStreamRequest + { + OpenToken = state.MediaSource.OpenToken + + }, cancellationTokenSource.Token).ConfigureAwait(false); + + EncodingHelper.AttachMediaSourceInfo(state, liveStreamResponse.MediaSource, state.RequestedUrl); + + if (state.VideoRequest != null) + { + EncodingHelper.TryStreamCopy(state); + } + } + + if (state.MediaSource.BufferMs.HasValue) + { + await Task.Delay(state.MediaSource.BufferMs.Value, cancellationTokenSource.Token).ConfigureAwait(false); + } + } + + /// + /// Starts the FFMPEG. + /// + /// The state. + /// The output path. + /// The cancellation token source. + /// The working directory. + /// Task. + protected async Task StartFfMpeg(StreamState state, + string outputPath, + CancellationTokenSource cancellationTokenSource, + string workingDirectory = null) + { + FileSystem.CreateDirectory(FileSystem.GetDirectoryName(outputPath)); + + await AcquireResources(state, cancellationTokenSource).ConfigureAwait(false); + + if (state.VideoRequest != null && !string.Equals(state.OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase)) + { + var auth = AuthorizationContext.GetAuthorizationInfo(Request); + if (auth.User != null) + { + if (!auth.User.Policy.EnableVideoPlaybackTranscoding) + { + ApiEntryPoint.Instance.OnTranscodeFailedToStart(outputPath, TranscodingJobType, state); + + throw new ArgumentException("User does not have access to video transcoding"); + } + } + } + + var encodingOptions = ApiEntryPoint.Instance.GetEncodingOptions(); + + var transcodingId = Guid.NewGuid().ToString("N"); + var commandLineArgs = GetCommandLineArguments(outputPath, encodingOptions, state, true); + + var process = ApiEntryPoint.Instance.ProcessFactory.Create(new ProcessOptions + { + CreateNoWindow = true, + UseShellExecute = false, + + // Must consume both stdout and stderr or deadlocks may occur + //RedirectStandardOutput = true, + RedirectStandardError = true, + RedirectStandardInput = true, + + FileName = MediaEncoder.EncoderPath, + Arguments = commandLineArgs, + + IsHidden = true, + ErrorDialog = false, + EnableRaisingEvents = true, + WorkingDirectory = !string.IsNullOrWhiteSpace(workingDirectory) ? workingDirectory : null + }); + + var transcodingJob = ApiEntryPoint.Instance.OnTranscodeBeginning(outputPath, + state.Request.PlaySessionId, + state.MediaSource.LiveStreamId, + transcodingId, + TranscodingJobType, + process, + state.Request.DeviceId, + state, + cancellationTokenSource); + + var commandLineLogMessage = process.StartInfo.FileName + " " + process.StartInfo.Arguments; + Logger.Info(commandLineLogMessage); + + var logFilePrefix = "ffmpeg-transcode"; + if (state.VideoRequest != null && string.Equals(state.OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase) && string.Equals(state.OutputAudioCodec, "copy", StringComparison.OrdinalIgnoreCase)) + { + logFilePrefix = "ffmpeg-directstream"; + } + else if (state.VideoRequest != null && string.Equals(state.OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase)) + { + logFilePrefix = "ffmpeg-remux"; + } + + var logFilePath = Path.Combine(ServerConfigurationManager.ApplicationPaths.LogDirectoryPath, logFilePrefix + "-" + Guid.NewGuid() + ".txt"); + FileSystem.CreateDirectory(FileSystem.GetDirectoryName(logFilePath)); + + // 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, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.Read, true); + + var commandLineLogMessageBytes = Encoding.UTF8.GetBytes(Request.AbsoluteUri + Environment.NewLine + Environment.NewLine + JsonSerializer.SerializeToString(state.MediaSource) + Environment.NewLine + Environment.NewLine + commandLineLogMessage + Environment.NewLine + Environment.NewLine); + await state.LogFileStream.WriteAsync(commandLineLogMessageBytes, 0, commandLineLogMessageBytes.Length, cancellationTokenSource.Token).ConfigureAwait(false); + + process.Exited += (sender, args) => OnFfMpegProcessExited(process, transcodingJob, state); + + try + { + process.Start(); + } + catch (Exception ex) + { + Logger.ErrorException("Error starting ffmpeg", ex); + + ApiEntryPoint.Instance.OnTranscodeFailedToStart(outputPath, TranscodingJobType, state); + + throw; + } + + // MUST read both stdout and stderr asynchronously or a deadlock may occurr + //process.BeginOutputReadLine(); + + state.TranscodingJob = transcodingJob; + + // Important - don't await the log task or we won't be able to kill ffmpeg when the user stops playback + new JobLogger(Logger).StartStreamingLog(state, process.StandardError.BaseStream, state.LogFileStream); + + // Wait for the file to exist before proceeeding + while (!FileSystem.FileExists(state.WaitForPath ?? outputPath) && !transcodingJob.HasExited) + { + await Task.Delay(100, cancellationTokenSource.Token).ConfigureAwait(false); + } + + if (state.IsInputVideo && transcodingJob.Type == TranscodingJobType.Progressive && !transcodingJob.HasExited) + { + await Task.Delay(1000, cancellationTokenSource.Token).ConfigureAwait(false); + + if (state.ReadInputAtNativeFramerate && !transcodingJob.HasExited) + { + await Task.Delay(1500, cancellationTokenSource.Token).ConfigureAwait(false); + } + } + + if (!transcodingJob.HasExited) + { + StartThrottler(state, transcodingJob); + } + + return transcodingJob; + } + + private void StartThrottler(StreamState state, TranscodingJob transcodingJob) + { + if (EnableThrottling(state)) + { + transcodingJob.TranscodingThrottler = state.TranscodingThrottler = new TranscodingThrottler(transcodingJob, Logger, ServerConfigurationManager, ApiEntryPoint.Instance.TimerFactory, FileSystem); + state.TranscodingThrottler.Start(); + } + } + + private bool EnableThrottling(StreamState state) + { + return false; + //// do not use throttling with hardware encoders + //return state.InputProtocol == MediaProtocol.File && + // state.RunTimeTicks.HasValue && + // state.RunTimeTicks.Value >= TimeSpan.FromMinutes(5).Ticks && + // state.IsInputVideo && + // state.VideoType == VideoType.VideoFile && + // !string.Equals(state.OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase) && + // string.Equals(GetVideoEncoder(state), "libx264", StringComparison.OrdinalIgnoreCase); + } + + /// + /// Processes the exited. + /// + /// The process. + /// The job. + /// The state. + private void OnFfMpegProcessExited(IProcess process, TranscodingJob job, StreamState state) + { + if (job != null) + { + job.HasExited = true; + } + + Logger.Debug("Disposing stream resources"); + state.Dispose(); + + try + { + Logger.Info("FFMpeg exited with code {0}", process.ExitCode); + } + catch + { + Logger.Error("FFMpeg exited with an error."); + } + + // This causes on exited to be called twice: + //try + //{ + // // Dispose the process + // process.Dispose(); + //} + //catch (Exception ex) + //{ + // Logger.ErrorException("Error disposing ffmpeg.", ex); + //} + } + + /// + /// Parses the parameters. + /// + /// The request. + private void ParseParams(StreamRequest request) + { + var vals = request.Params.Split(';'); + + var videoRequest = request as VideoStreamRequest; + + for (var i = 0; i < vals.Length; i++) + { + var val = vals[i]; + + if (string.IsNullOrWhiteSpace(val)) + { + continue; + } + + if (i == 0) + { + request.DeviceProfileId = val; + } + else if (i == 1) + { + request.DeviceId = val; + } + else if (i == 2) + { + request.MediaSourceId = val; + } + else if (i == 3) + { + request.Static = string.Equals("true", val, StringComparison.OrdinalIgnoreCase); + } + else if (i == 4) + { + if (videoRequest != null) + { + videoRequest.VideoCodec = val; + } + } + else if (i == 5) + { + request.AudioCodec = val; + } + else if (i == 6) + { + if (videoRequest != null) + { + videoRequest.AudioStreamIndex = int.Parse(val, UsCulture); + } + } + else if (i == 7) + { + if (videoRequest != null) + { + videoRequest.SubtitleStreamIndex = int.Parse(val, UsCulture); + } + } + else if (i == 8) + { + if (videoRequest != null) + { + videoRequest.VideoBitRate = int.Parse(val, UsCulture); + } + } + else if (i == 9) + { + request.AudioBitRate = int.Parse(val, UsCulture); + } + else if (i == 10) + { + request.MaxAudioChannels = int.Parse(val, UsCulture); + } + else if (i == 11) + { + if (videoRequest != null) + { + videoRequest.MaxFramerate = float.Parse(val, UsCulture); + } + } + else if (i == 12) + { + if (videoRequest != null) + { + videoRequest.MaxWidth = int.Parse(val, UsCulture); + } + } + else if (i == 13) + { + if (videoRequest != null) + { + videoRequest.MaxHeight = int.Parse(val, UsCulture); + } + } + else if (i == 14) + { + request.StartTimeTicks = long.Parse(val, UsCulture); + } + else if (i == 15) + { + if (videoRequest != null) + { + videoRequest.Level = val; + } + } + else if (i == 16) + { + if (videoRequest != null) + { + videoRequest.MaxRefFrames = int.Parse(val, UsCulture); + } + } + else if (i == 17) + { + if (videoRequest != null) + { + videoRequest.MaxVideoBitDepth = int.Parse(val, UsCulture); + } + } + else if (i == 18) + { + if (videoRequest != null) + { + videoRequest.Profile = val; + } + } + else if (i == 19) + { + // cabac no longer used + } + else if (i == 20) + { + request.PlaySessionId = val; + } + else if (i == 21) + { + // api_key + } + else if (i == 22) + { + request.LiveStreamId = val; + } + else if (i == 23) + { + // Duplicating ItemId because of MediaMonkey + } + else if (i == 24) + { + if (videoRequest != null) + { + videoRequest.CopyTimestamps = string.Equals("true", val, StringComparison.OrdinalIgnoreCase); + } + } + else if (i == 25) + { + if (!string.IsNullOrWhiteSpace(val) && videoRequest != null) + { + SubtitleDeliveryMethod method; + if (Enum.TryParse(val, out method)) + { + videoRequest.SubtitleMethod = method; + } + } + } + else if (i == 26) + { + request.TranscodingMaxAudioChannels = int.Parse(val, UsCulture); + } + else if (i == 27) + { + if (videoRequest != null) + { + videoRequest.EnableSubtitlesInManifest = string.Equals("true", val, StringComparison.OrdinalIgnoreCase); + } + } + else if (i == 28) + { + request.Tag = val; + } + else if (i == 29) + { + if (videoRequest != null) + { + videoRequest.RequireAvc = string.Equals("true", val, StringComparison.OrdinalIgnoreCase); + } + } + else if (i == 30) + { + request.SubtitleCodec = val; + } + else if (i == 31) + { + if (videoRequest != null) + { + videoRequest.RequireNonAnamorphic = string.Equals("true", val, StringComparison.OrdinalIgnoreCase); + } + } + else if (i == 32) + { + if (videoRequest != null) + { + videoRequest.DeInterlace = string.Equals("true", val, StringComparison.OrdinalIgnoreCase); + } + } + else if (i == 33) + { + request.TranscodeReasons = val; + } + } + } + + /// + /// Parses the dlna headers. + /// + /// The request. + private void ParseDlnaHeaders(StreamRequest request) + { + if (!request.StartTimeTicks.HasValue) + { + var timeSeek = GetHeader("TimeSeekRange.dlna.org"); + + request.StartTimeTicks = ParseTimeSeekHeader(timeSeek); + } + } + + /// + /// Parses the time seek header. + /// + private long? ParseTimeSeekHeader(string value) + { + if (string.IsNullOrWhiteSpace(value)) + { + return null; + } + + if (value.IndexOf("npt=", StringComparison.OrdinalIgnoreCase) != 0) + { + throw new ArgumentException("Invalid timeseek header"); + } + value = value.Substring(4).Split(new[] { '-' }, 2)[0]; + + if (value.IndexOf(':') == -1) + { + // Parses npt times in the format of '417.33' + double seconds; + if (double.TryParse(value, NumberStyles.Any, UsCulture, out seconds)) + { + return TimeSpan.FromSeconds(seconds).Ticks; + } + + throw new ArgumentException("Invalid timeseek header"); + } + + // Parses npt times in the format of '10:19:25.7' + var tokens = value.Split(new[] { ':' }, 3); + double secondsSum = 0; + var timeFactor = 3600; + + foreach (var time in tokens) + { + double digit; + if (double.TryParse(time, NumberStyles.Any, UsCulture, out digit)) + { + secondsSum += digit * timeFactor; + } + else + { + throw new ArgumentException("Invalid timeseek header"); + } + timeFactor /= 60; + } + return TimeSpan.FromSeconds(secondsSum).Ticks; + } + + /// + /// Gets the state. + /// + /// The request. + /// The cancellation token. + /// StreamState. + protected async Task GetState(StreamRequest request, CancellationToken cancellationToken) + { + ParseDlnaHeaders(request); + + if (!string.IsNullOrWhiteSpace(request.Params)) + { + ParseParams(request); + } + + var url = Request.PathInfo; + + if (string.IsNullOrEmpty(request.AudioCodec)) + { + request.AudioCodec = EncodingHelper.InferAudioCodec(url); + } + + var enableDlnaHeaders = !string.IsNullOrWhiteSpace(request.Params) /*|| + string.Equals(Request.Headers.Get("GetContentFeatures.DLNA.ORG"), "1", StringComparison.OrdinalIgnoreCase)*/; + + var state = new StreamState(MediaSourceManager, Logger, TranscodingJobType) + { + Request = request, + RequestedUrl = url, + UserAgent = Request.UserAgent, + EnableDlnaHeaders = enableDlnaHeaders + }; + + var auth = AuthorizationContext.GetAuthorizationInfo(Request); + if (auth.UserId != null) + { + state.User = UserManager.GetUserById(auth.UserId); + } + + //if ((Request.UserAgent ?? string.Empty).IndexOf("iphone", StringComparison.OrdinalIgnoreCase) != -1 || + // (Request.UserAgent ?? string.Empty).IndexOf("ipad", StringComparison.OrdinalIgnoreCase) != -1 || + // (Request.UserAgent ?? string.Empty).IndexOf("ipod", StringComparison.OrdinalIgnoreCase) != -1) + //{ + // state.SegmentLength = 6; + //} + + if (state.VideoRequest != null) + { + if (!string.IsNullOrWhiteSpace(state.VideoRequest.VideoCodec)) + { + state.SupportedVideoCodecs = state.VideoRequest.VideoCodec.Split(',').Where(i => !string.IsNullOrWhiteSpace(i)).ToArray(); + state.VideoRequest.VideoCodec = state.SupportedVideoCodecs.FirstOrDefault(); + } + } + + if (!string.IsNullOrWhiteSpace(request.AudioCodec)) + { + state.SupportedAudioCodecs = request.AudioCodec.Split(',').Where(i => !string.IsNullOrWhiteSpace(i)).ToArray(); + state.Request.AudioCodec = state.SupportedAudioCodecs.FirstOrDefault(i => MediaEncoder.CanEncodeToAudioCodec(i)) + ?? state.SupportedAudioCodecs.FirstOrDefault(); + } + + if (!string.IsNullOrWhiteSpace(request.SubtitleCodec)) + { + state.SupportedSubtitleCodecs = request.SubtitleCodec.Split(',').Where(i => !string.IsNullOrWhiteSpace(i)).ToArray(); + state.Request.SubtitleCodec = state.SupportedSubtitleCodecs.FirstOrDefault(i => MediaEncoder.CanEncodeToSubtitleCodec(i)) + ?? state.SupportedSubtitleCodecs.FirstOrDefault(); + } + + var item = LibraryManager.GetItemById(request.Id); + + state.IsInputVideo = string.Equals(item.MediaType, MediaType.Video, StringComparison.OrdinalIgnoreCase); + + //var primaryImage = item.GetImageInfo(ImageType.Primary, 0) ?? + // item.Parents.Select(i => i.GetImageInfo(ImageType.Primary, 0)).FirstOrDefault(i => i != null); + //if (primaryImage != null) + //{ + // state.AlbumCoverPath = primaryImage.Path; + //} + + MediaSourceInfo mediaSource = null; + if (string.IsNullOrWhiteSpace(request.LiveStreamId)) + { + TranscodingJob currentJob = !string.IsNullOrWhiteSpace(request.PlaySessionId) ? + ApiEntryPoint.Instance.GetTranscodingJob(request.PlaySessionId) + : null; + + if (currentJob != null) + { + mediaSource = currentJob.MediaSource; + } + + if (mediaSource == null) + { + var mediaSources = (await MediaSourceManager.GetPlayackMediaSources(LibraryManager.GetItemById(request.Id), null, false, false, cancellationToken).ConfigureAwait(false)).ToList(); + + mediaSource = string.IsNullOrEmpty(request.MediaSourceId) + ? mediaSources.First() + : mediaSources.FirstOrDefault(i => string.Equals(i.Id, request.MediaSourceId)); + + if (mediaSource == null && request.MediaSourceId.Equals(request.Id)) + { + mediaSource = mediaSources.First(); + } + } + } + else + { + var liveStreamInfo = await MediaSourceManager.GetLiveStreamWithDirectStreamProvider(request.LiveStreamId, cancellationToken).ConfigureAwait(false); + mediaSource = liveStreamInfo.Item1; + state.DirectStreamProvider = liveStreamInfo.Item2; + } + + var videoRequest = request as VideoStreamRequest; + + EncodingHelper.AttachMediaSourceInfo(state, mediaSource, url); + + var container = Path.GetExtension(state.RequestedUrl); + + if (string.IsNullOrEmpty(container)) + { + container = request.Container; + } + + if (string.IsNullOrEmpty(container)) + { + container = request.Static ? + StreamBuilder.NormalizeMediaSourceFormatIntoSingleContainer(state.InputContainer, state.MediaPath, null, DlnaProfileType.Audio) : + GetOutputFileExtension(state); + } + + state.OutputContainer = (container ?? string.Empty).TrimStart('.'); + + state.OutputAudioBitrate = EncodingHelper.GetAudioBitrateParam(state.Request, state.AudioStream); + + state.OutputAudioCodec = state.Request.AudioCodec; + + state.OutputAudioChannels = EncodingHelper.GetNumAudioChannelsParam(state, state.AudioStream, state.OutputAudioCodec); + + if (videoRequest != null) + { + state.OutputVideoCodec = state.VideoRequest.VideoCodec; + state.OutputVideoBitrate = EncodingHelper.GetVideoBitrateParamValue(state.VideoRequest, state.VideoStream, state.OutputVideoCodec); + + if (videoRequest != null) + { + EncodingHelper.TryStreamCopy(state); + } + + if (state.OutputVideoBitrate.HasValue && !string.Equals(state.OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase)) + { + var resolution = ResolutionNormalizer.Normalize( + state.VideoStream == null ? (int?)null : state.VideoStream.BitRate, + state.VideoStream == null ? (int?)null : state.VideoStream.Width, + state.VideoStream == null ? (int?)null : state.VideoStream.Height, + state.OutputVideoBitrate.Value, + state.VideoStream == null ? null : state.VideoStream.Codec, + state.OutputVideoCodec, + videoRequest.MaxWidth, + videoRequest.MaxHeight); + + videoRequest.MaxWidth = resolution.MaxWidth; + videoRequest.MaxHeight = resolution.MaxHeight; + } + + ApplyDeviceProfileSettings(state); + } + else + { + ApplyDeviceProfileSettings(state); + } + + var ext = string.IsNullOrWhiteSpace(state.OutputContainer) + ? GetOutputFileExtension(state) + : ("." + state.OutputContainer); + + var encodingOptions = ApiEntryPoint.Instance.GetEncodingOptions(); + + state.OutputFilePath = GetOutputFilePath(state, encodingOptions, ext); + + return state; + } + + private void ApplyDeviceProfileSettings(StreamState state) + { + var headers = Request.Headers.ToDictionary(); + + if (!string.IsNullOrWhiteSpace(state.Request.DeviceProfileId)) + { + state.DeviceProfile = DlnaManager.GetProfile(state.Request.DeviceProfileId); + } + else + { + if (!string.IsNullOrWhiteSpace(state.Request.DeviceId)) + { + var caps = DeviceManager.GetCapabilities(state.Request.DeviceId); + + if (caps != null) + { + state.DeviceProfile = caps.DeviceProfile; + } + else + { + state.DeviceProfile = DlnaManager.GetProfile(headers); + } + } + } + + var profile = state.DeviceProfile; + + if (profile == null) + { + // Don't use settings from the default profile. + // Only use a specific profile if it was requested. + return; + } + + var audioCodec = state.ActualOutputAudioCodec; + var videoCodec = state.ActualOutputVideoCodec; + + var mediaProfile = state.VideoRequest == null ? + profile.GetAudioMediaProfile(state.OutputContainer, audioCodec, state.OutputAudioChannels, state.OutputAudioBitrate, state.OutputAudioSampleRate, state.OutputAudioBitDepth) : + profile.GetVideoMediaProfile(state.OutputContainer, + audioCodec, + videoCodec, + state.OutputWidth, + state.OutputHeight, + state.TargetVideoBitDepth, + state.OutputVideoBitrate, + state.TargetVideoProfile, + state.TargetVideoLevel, + state.TargetFramerate, + state.TargetPacketLength, + state.TargetTimestamp, + state.IsTargetAnamorphic, + state.IsTargetInterlaced, + state.TargetRefFrames, + state.TargetVideoStreamCount, + state.TargetAudioStreamCount, + state.TargetVideoCodecTag, + state.IsTargetAVC); + + if (mediaProfile != null) + { + state.MimeType = mediaProfile.MimeType; + } + + if (!state.Request.Static) + { + var transcodingProfile = state.VideoRequest == null ? + profile.GetAudioTranscodingProfile(state.OutputContainer, audioCodec) : + profile.GetVideoTranscodingProfile(state.OutputContainer, audioCodec, videoCodec); + + if (transcodingProfile != null) + { + state.EstimateContentLength = transcodingProfile.EstimateContentLength; + //state.EnableMpegtsM2TsMode = transcodingProfile.EnableMpegtsM2TsMode; + state.TranscodeSeekInfo = transcodingProfile.TranscodeSeekInfo; + + if (state.VideoRequest != null) + { + state.VideoRequest.CopyTimestamps = transcodingProfile.CopyTimestamps; + state.VideoRequest.EnableSubtitlesInManifest = transcodingProfile.EnableSubtitlesInManifest; + } + } + } + } + + /// + /// Adds the dlna headers. + /// + /// The state. + /// The response headers. + /// if set to true [is statically streamed]. + /// true if XXXX, false otherwise + protected void AddDlnaHeaders(StreamState state, IDictionary responseHeaders, bool isStaticallyStreamed) + { + if (!state.EnableDlnaHeaders) + { + return; + } + + var profile = state.DeviceProfile; + + var transferMode = GetHeader("transferMode.dlna.org"); + responseHeaders["transferMode.dlna.org"] = string.IsNullOrEmpty(transferMode) ? "Streaming" : transferMode; + responseHeaders["realTimeInfo.dlna.org"] = "DLNA.ORG_TLAG=*"; + + if (string.Equals(GetHeader("getMediaInfo.sec"), "1", StringComparison.OrdinalIgnoreCase)) + { + if (state.RunTimeTicks.HasValue) + { + var ms = TimeSpan.FromTicks(state.RunTimeTicks.Value).TotalMilliseconds; + responseHeaders["MediaInfo.sec"] = string.Format("SEC_Duration={0};", Convert.ToInt32(ms).ToString(CultureInfo.InvariantCulture)); + } + } + + if (state.RunTimeTicks.HasValue && !isStaticallyStreamed && profile != null) + { + AddTimeSeekResponseHeaders(state, responseHeaders); + } + + if (profile == null) + { + profile = DlnaManager.GetDefaultProfile(); + } + + var audioCodec = state.ActualOutputAudioCodec; + + if (state.VideoRequest == null) + { + responseHeaders["contentFeatures.dlna.org"] = new ContentFeatureBuilder(profile) + .BuildAudioHeader( + state.OutputContainer, + audioCodec, + state.OutputAudioBitrate, + state.OutputAudioSampleRate, + state.OutputAudioChannels, + state.OutputAudioBitDepth, + isStaticallyStreamed, + state.RunTimeTicks, + state.TranscodeSeekInfo + ); + } + else + { + var videoCodec = state.ActualOutputVideoCodec; + + responseHeaders["contentFeatures.dlna.org"] = new ContentFeatureBuilder(profile) + .BuildVideoHeader( + state.OutputContainer, + videoCodec, + audioCodec, + state.OutputWidth, + state.OutputHeight, + state.TargetVideoBitDepth, + state.OutputVideoBitrate, + state.TargetTimestamp, + isStaticallyStreamed, + state.RunTimeTicks, + state.TargetVideoProfile, + state.TargetVideoLevel, + state.TargetFramerate, + state.TargetPacketLength, + state.TranscodeSeekInfo, + state.IsTargetAnamorphic, + state.IsTargetInterlaced, + state.TargetRefFrames, + state.TargetVideoStreamCount, + state.TargetAudioStreamCount, + state.TargetVideoCodecTag, + state.IsTargetAVC + + ).FirstOrDefault() ?? string.Empty; + } + + foreach (var item in responseHeaders) + { + Request.Response.AddHeader(item.Key, item.Value); + } + } + + private void AddTimeSeekResponseHeaders(StreamState state, IDictionary responseHeaders) + { + var runtimeSeconds = TimeSpan.FromTicks(state.RunTimeTicks.Value).TotalSeconds.ToString(UsCulture); + var startSeconds = TimeSpan.FromTicks(state.Request.StartTimeTicks ?? 0).TotalSeconds.ToString(UsCulture); + + responseHeaders["TimeSeekRange.dlna.org"] = string.Format("npt={0}-{1}/{1}", startSeconds, runtimeSeconds); + responseHeaders["X-AvailableSeekRange"] = string.Format("1 npt={0}-{1}", startSeconds, runtimeSeconds); + } + } +} diff --git a/MediaBrowser.Api/Playback/Hls/BaseHlsService.cs b/MediaBrowser.Api/Playback/Hls/BaseHlsService.cs new file mode 100644 index 0000000000..a0f4a2e715 --- /dev/null +++ b/MediaBrowser.Api/Playback/Hls/BaseHlsService.cs @@ -0,0 +1,333 @@ +using MediaBrowser.Controller.Configuration; +using MediaBrowser.Controller.Devices; +using MediaBrowser.Controller.Dlna; +using MediaBrowser.Controller.Library; +using MediaBrowser.Controller.MediaEncoding; +using MediaBrowser.Model.Extensions; +using MediaBrowser.Model.IO; +using MediaBrowser.Model.Net; +using MediaBrowser.Model.Serialization; +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using MediaBrowser.Controller.Net; +using MediaBrowser.Model.Configuration; + +namespace MediaBrowser.Api.Playback.Hls +{ + /// + /// Class BaseHlsService + /// + public abstract class BaseHlsService : BaseStreamingService + { + /// + /// Gets the audio arguments. + /// + protected abstract string GetAudioArguments(StreamState state, EncodingOptions encodingOptions); + + /// + /// Gets the video arguments. + /// + protected abstract string GetVideoArguments(StreamState state, EncodingOptions encodingOptions); + + /// + /// Gets the segment file extension. + /// + protected string GetSegmentFileExtension(StreamRequest request) + { + var segmentContainer = request.SegmentContainer; + if (!string.IsNullOrWhiteSpace(segmentContainer)) + { + return "." + segmentContainer; + } + + return ".ts"; + } + + /// + /// Gets the type of the transcoding job. + /// + /// The type of the transcoding job. + protected override TranscodingJobType TranscodingJobType + { + get { return TranscodingJobType.Hls; } + } + + /// + /// Processes the request. + /// + /// The request. + /// if set to true [is live]. + /// System.Object. + protected async Task ProcessRequest(StreamRequest request, bool isLive) + { + return await ProcessRequestAsync(request, isLive).ConfigureAwait(false); + } + + /// + /// Processes the request async. + /// + /// The request. + /// if set to true [is live]. + /// Task{System.Object}. + /// A video bitrate is required + /// or + /// An audio bitrate is required + private async Task ProcessRequestAsync(StreamRequest request, bool isLive) + { + var cancellationTokenSource = new CancellationTokenSource(); + + var state = await GetState(request, cancellationTokenSource.Token).ConfigureAwait(false); + + TranscodingJob job = null; + var playlist = state.OutputFilePath; + + if (!FileSystem.FileExists(playlist)) + { + var transcodingLock = ApiEntryPoint.Instance.GetTranscodingLock(playlist); + await transcodingLock.WaitAsync(cancellationTokenSource.Token).ConfigureAwait(false); + try + { + if (!FileSystem.FileExists(playlist)) + { + // If the playlist doesn't already exist, startup ffmpeg + try + { + job = await StartFfMpeg(state, playlist, cancellationTokenSource).ConfigureAwait(false); + job.IsLiveOutput = isLive; + } + catch + { + state.Dispose(); + throw; + } + + var minSegments = state.MinSegments; + if (minSegments > 0) + { + await WaitForMinimumSegmentCount(playlist, minSegments, cancellationTokenSource.Token).ConfigureAwait(false); + } + } + } + finally + { + transcodingLock.Release(); + } + } + + if (isLive) + { + job = job ?? ApiEntryPoint.Instance.OnTranscodeBeginRequest(playlist, TranscodingJobType); + + if (job != null) + { + ApiEntryPoint.Instance.OnTranscodeEndRequest(job); + } + return ResultFactory.GetResult(GetLivePlaylistText(playlist, state.SegmentLength), MimeTypes.GetMimeType("playlist.m3u8"), new Dictionary()); + } + + var audioBitrate = state.OutputAudioBitrate ?? 0; + var videoBitrate = state.OutputVideoBitrate ?? 0; + + var baselineStreamBitrate = 64000; + + var playlistText = GetMasterPlaylistFileText(playlist, videoBitrate + audioBitrate, baselineStreamBitrate); + + job = job ?? ApiEntryPoint.Instance.OnTranscodeBeginRequest(playlist, TranscodingJobType); + + if (job != null) + { + ApiEntryPoint.Instance.OnTranscodeEndRequest(job); + } + + return ResultFactory.GetResult(playlistText, MimeTypes.GetMimeType("playlist.m3u8"), new Dictionary()); + } + + private string GetLivePlaylistText(string path, int segmentLength) + { + using (var stream = FileSystem.GetFileStream(path, FileOpenMode.Open, FileAccessMode.Read, FileShareMode.ReadWrite)) + { + using (var reader = new StreamReader(stream)) + { + var text = reader.ReadToEnd(); + + text = text.Replace("#EXTM3U", "#EXTM3U\n#EXT-X-PLAYLIST-TYPE:EVENT"); + + var newDuration = "#EXT-X-TARGETDURATION:" + segmentLength.ToString(UsCulture); + + text = text.Replace("#EXT-X-TARGETDURATION:" + (segmentLength - 1).ToString(UsCulture), newDuration, StringComparison.OrdinalIgnoreCase); + //text = text.Replace("#EXT-X-TARGETDURATION:" + (segmentLength + 1).ToString(UsCulture), newDuration, StringComparison.OrdinalIgnoreCase); + + return text; + } + } + } + + private string GetMasterPlaylistFileText(string firstPlaylist, int bitrate, int baselineStreamBitrate) + { + var builder = new StringBuilder(); + + builder.AppendLine("#EXTM3U"); + + // Pad a little to satisfy the apple hls validator + var paddedBitrate = Convert.ToInt32(bitrate * 1.15); + + // Main stream + builder.AppendLine("#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=" + paddedBitrate.ToString(UsCulture)); + var playlistUrl = "hls/" + Path.GetFileName(firstPlaylist).Replace(".m3u8", "/stream.m3u8"); + builder.AppendLine(playlistUrl); + + return builder.ToString(); + } + + protected virtual async Task WaitForMinimumSegmentCount(string playlist, int segmentCount, CancellationToken cancellationToken) + { + Logger.Debug("Waiting for {0} segments in {1}", segmentCount, playlist); + + while (!cancellationToken.IsCancellationRequested) + { + try + { + // Need to use FileShareMode.ReadWrite because we're reading the file at the same time it's being written + using (var fileStream = GetPlaylistFileStream(playlist)) + { + using (var reader = new StreamReader(fileStream)) + { + var count = 0; + + while (!reader.EndOfStream) + { + var line = reader.ReadLine(); + + if (line.IndexOf("#EXTINF:", StringComparison.OrdinalIgnoreCase) != -1) + { + count++; + if (count >= segmentCount) + { + Logger.Debug("Finished waiting for {0} segments in {1}", segmentCount, playlist); + return; + } + } + } + await Task.Delay(100, cancellationToken).ConfigureAwait(false); + } + } + } + catch (IOException) + { + // May get an error if the file is locked + } + + await Task.Delay(50, cancellationToken).ConfigureAwait(false); + } + } + + protected Stream GetPlaylistFileStream(string path) + { + var tmpPath = path + ".tmp"; + tmpPath = path; + + try + { + return FileSystem.GetFileStream(tmpPath, FileOpenMode.Open, FileAccessMode.Read, FileShareMode.ReadWrite, FileOpenOptions.SequentialScan); + } + catch (IOException) + { + return FileSystem.GetFileStream(path, FileOpenMode.Open, FileAccessMode.Read, FileShareMode.ReadWrite, FileOpenOptions.SequentialScan); + } + } + + protected override string GetCommandLineArguments(string outputPath, EncodingOptions encodingOptions, StreamState state, bool isEncoding) + { + var itsOffsetMs = 0; + + var itsOffset = itsOffsetMs == 0 ? string.Empty : string.Format("-itsoffset {0} ", TimeSpan.FromMilliseconds(itsOffsetMs).TotalSeconds.ToString(UsCulture)); + + var videoCodec = EncodingHelper.GetVideoEncoder(state, encodingOptions); + + var threads = EncodingHelper.GetNumberOfThreads(state, encodingOptions, videoCodec); + + var inputModifier = EncodingHelper.GetInputModifier(state, encodingOptions); + + // If isEncoding is true we're actually starting ffmpeg + var startNumberParam = isEncoding ? GetStartNumber(state).ToString(UsCulture) : "0"; + + var baseUrlParam = string.Empty; + + if (state.Request is GetLiveHlsStream) + { + baseUrlParam = string.Format(" -hls_base_url \"{0}/\"", + "hls/" + Path.GetFileNameWithoutExtension(outputPath)); + } + + var useGenericSegmenter = true; + if (useGenericSegmenter) + { + var outputTsArg = Path.Combine(FileSystem.GetDirectoryName(outputPath), Path.GetFileNameWithoutExtension(outputPath)) + "%d" + GetSegmentFileExtension(state.Request); + + var timeDeltaParam = String.Empty; + + var segmentFormat = GetSegmentFileExtension(state.Request).TrimStart('.'); + if (string.Equals(segmentFormat, "ts", StringComparison.OrdinalIgnoreCase)) + { + segmentFormat = "mpegts"; + } + + baseUrlParam = string.Format("\"{0}/\"", "hls/" + Path.GetFileNameWithoutExtension(outputPath)); + + return string.Format("{0} {1} -map_metadata -1 -map_chapters -1 -threads {2} {3} {4} {5} -f segment -max_delay 5000000 -avoid_negative_ts disabled -start_at_zero -segment_time {6} {10} -individual_header_trailer 0 -segment_format {11} -segment_list_entry_prefix {12} -segment_list_type m3u8 -segment_start_number {7} -segment_list \"{8}\" -y \"{9}\"", + inputModifier, + EncodingHelper.GetInputArgument(state, encodingOptions), + threads, + EncodingHelper.GetMapArgs(state), + GetVideoArguments(state, encodingOptions), + GetAudioArguments(state, encodingOptions), + state.SegmentLength.ToString(UsCulture), + startNumberParam, + outputPath, + outputTsArg, + timeDeltaParam, + segmentFormat, + baseUrlParam + ).Trim(); + } + + // add when stream copying? + // -avoid_negative_ts make_zero -fflags +genpts + + var args = string.Format("{0} {1} {2} -map_metadata -1 -map_chapters -1 -threads {3} {4} {5} -max_delay 5000000 -avoid_negative_ts disabled -start_at_zero {6} -hls_time {7} -individual_header_trailer 0 -start_number {8} -hls_list_size {9}{10} -y \"{11}\"", + itsOffset, + inputModifier, + EncodingHelper.GetInputArgument(state, encodingOptions), + threads, + EncodingHelper.GetMapArgs(state), + GetVideoArguments(state, encodingOptions), + GetAudioArguments(state, encodingOptions), + state.SegmentLength.ToString(UsCulture), + startNumberParam, + state.HlsListSize.ToString(UsCulture), + baseUrlParam, + outputPath + ).Trim(); + + return args; + } + + protected override string GetDefaultH264Preset() + { + return "veryfast"; + } + + protected virtual int GetStartNumber(StreamState state) + { + return 0; + } + + public BaseHlsService(IServerConfigurationManager serverConfig, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager, IMediaEncoder mediaEncoder, IFileSystem fileSystem, IDlnaManager dlnaManager, ISubtitleEncoder subtitleEncoder, IDeviceManager deviceManager, IMediaSourceManager mediaSourceManager, IZipClient zipClient, IJsonSerializer jsonSerializer, IAuthorizationContext authorizationContext) : base(serverConfig, userManager, libraryManager, isoManager, mediaEncoder, fileSystem, dlnaManager, subtitleEncoder, deviceManager, mediaSourceManager, zipClient, jsonSerializer, authorizationContext) + { + } + } +} \ No newline at end of file diff --git a/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs b/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs new file mode 100644 index 0000000000..0525c8cc48 --- /dev/null +++ b/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs @@ -0,0 +1,971 @@ +using MediaBrowser.Common.Net; +using MediaBrowser.Controller.Configuration; +using MediaBrowser.Controller.Devices; +using MediaBrowser.Controller.Dlna; +using MediaBrowser.Controller.Library; +using MediaBrowser.Controller.MediaEncoding; +using MediaBrowser.Controller.Net; +using MediaBrowser.Model.Dlna; +using MediaBrowser.Model.Entities; +using MediaBrowser.Model.Extensions; +using MediaBrowser.Model.IO; +using MediaBrowser.Model.Serialization; +using System; +using System.Collections.Generic; +using System.Globalization; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using MediaBrowser.Model.Configuration; +using MediaBrowser.Model.Services; +using MimeTypes = MediaBrowser.Model.Net.MimeTypes; + +namespace MediaBrowser.Api.Playback.Hls +{ + /// + /// Options is needed for chromecast. Threw Head in there since it's related + /// + [Route("/Videos/{Id}/master.m3u8", "GET", Summary = "Gets a video stream using HTTP live streaming.")] + [Route("/Videos/{Id}/master.m3u8", "HEAD", Summary = "Gets a video stream using HTTP live streaming.")] + public class GetMasterHlsVideoPlaylist : VideoStreamRequest, IMasterHlsRequest + { + public bool EnableAdaptiveBitrateStreaming { get; set; } + + public GetMasterHlsVideoPlaylist() + { + EnableAdaptiveBitrateStreaming = true; + } + } + + [Route("/Audio/{Id}/master.m3u8", "GET", Summary = "Gets an audio stream using HTTP live streaming.")] + [Route("/Audio/{Id}/master.m3u8", "HEAD", Summary = "Gets an audio stream using HTTP live streaming.")] + public class GetMasterHlsAudioPlaylist : StreamRequest, IMasterHlsRequest + { + public bool EnableAdaptiveBitrateStreaming { get; set; } + + public GetMasterHlsAudioPlaylist() + { + EnableAdaptiveBitrateStreaming = true; + } + } + + public interface IMasterHlsRequest + { + bool EnableAdaptiveBitrateStreaming { get; set; } + } + + [Route("/Videos/{Id}/main.m3u8", "GET", Summary = "Gets a video stream using HTTP live streaming.")] + public class GetVariantHlsVideoPlaylist : VideoStreamRequest + { + } + + [Route("/Audio/{Id}/main.m3u8", "GET", Summary = "Gets an audio stream using HTTP live streaming.")] + public class GetVariantHlsAudioPlaylist : StreamRequest + { + } + + [Route("/Videos/{Id}/hls1/{PlaylistId}/{SegmentId}.{SegmentContainer}", "GET")] + public class GetHlsVideoSegment : VideoStreamRequest + { + public string PlaylistId { get; set; } + + /// + /// Gets or sets the segment id. + /// + /// The segment id. + public string SegmentId { get; set; } + } + + [Route("/Audio/{Id}/hls1/{PlaylistId}/{SegmentId}.{SegmentContainer}", "GET")] + public class GetHlsAudioSegment : StreamRequest + { + public string PlaylistId { get; set; } + + /// + /// Gets or sets the segment id. + /// + /// The segment id. + public string SegmentId { get; set; } + } + + [Authenticated] + public class DynamicHlsService : BaseHlsService + { + + public DynamicHlsService(IServerConfigurationManager serverConfig, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager, IMediaEncoder mediaEncoder, IFileSystem fileSystem, IDlnaManager dlnaManager, ISubtitleEncoder subtitleEncoder, IDeviceManager deviceManager, IMediaSourceManager mediaSourceManager, IZipClient zipClient, IJsonSerializer jsonSerializer, IAuthorizationContext authorizationContext, INetworkManager networkManager) : base(serverConfig, userManager, libraryManager, isoManager, mediaEncoder, fileSystem, dlnaManager, subtitleEncoder, deviceManager, mediaSourceManager, zipClient, jsonSerializer, authorizationContext) + { + NetworkManager = networkManager; + } + + protected INetworkManager NetworkManager { get; private set; } + + public Task Get(GetMasterHlsVideoPlaylist request) + { + return GetMasterPlaylistInternal(request, "GET"); + } + + public Task Head(GetMasterHlsVideoPlaylist request) + { + return GetMasterPlaylistInternal(request, "HEAD"); + } + + public Task Get(GetMasterHlsAudioPlaylist request) + { + return GetMasterPlaylistInternal(request, "GET"); + } + + public Task Head(GetMasterHlsAudioPlaylist request) + { + return GetMasterPlaylistInternal(request, "HEAD"); + } + + public Task Get(GetVariantHlsVideoPlaylist request) + { + return GetVariantPlaylistInternal(request, true, "main"); + } + + public Task Get(GetVariantHlsAudioPlaylist request) + { + return GetVariantPlaylistInternal(request, false, "main"); + } + + public Task Get(GetHlsVideoSegment request) + { + return GetDynamicSegment(request, request.SegmentId); + } + + public Task Get(GetHlsAudioSegment request) + { + return GetDynamicSegment(request, request.SegmentId); + } + + private async Task GetDynamicSegment(StreamRequest request, string segmentId) + { + if ((request.StartTimeTicks ?? 0) > 0) + { + throw new ArgumentException("StartTimeTicks is not allowed."); + } + + var cancellationTokenSource = new CancellationTokenSource(); + var cancellationToken = cancellationTokenSource.Token; + + var requestedIndex = int.Parse(segmentId, NumberStyles.Integer, UsCulture); + + var state = await GetState(request, cancellationToken).ConfigureAwait(false); + + var playlistPath = Path.ChangeExtension(state.OutputFilePath, ".m3u8"); + + var segmentPath = GetSegmentPath(state, playlistPath, requestedIndex); + + var segmentExtension = GetSegmentFileExtension(state.Request); + + TranscodingJob job = null; + + if (FileSystem.FileExists(segmentPath)) + { + job = ApiEntryPoint.Instance.OnTranscodeBeginRequest(playlistPath, TranscodingJobType); + return await GetSegmentResult(state, playlistPath, segmentPath, segmentExtension, requestedIndex, job, cancellationToken).ConfigureAwait(false); + } + + var transcodingLock = ApiEntryPoint.Instance.GetTranscodingLock(playlistPath); + await transcodingLock.WaitAsync(cancellationTokenSource.Token).ConfigureAwait(false); + var released = false; + var startTranscoding = false; + + try + { + if (FileSystem.FileExists(segmentPath)) + { + job = ApiEntryPoint.Instance.OnTranscodeBeginRequest(playlistPath, TranscodingJobType); + transcodingLock.Release(); + released = true; + return await GetSegmentResult(state, playlistPath, segmentPath, segmentExtension, requestedIndex, job, cancellationToken).ConfigureAwait(false); + } + else + { + var currentTranscodingIndex = GetCurrentTranscodingIndex(playlistPath, segmentExtension); + var segmentGapRequiringTranscodingChange = 24 / state.SegmentLength; + + if (currentTranscodingIndex == null) + { + Logger.Debug("Starting transcoding because currentTranscodingIndex=null"); + startTranscoding = true; + } + else if (requestedIndex < currentTranscodingIndex.Value) + { + Logger.Debug("Starting transcoding because requestedIndex={0} and currentTranscodingIndex={1}", requestedIndex, currentTranscodingIndex); + startTranscoding = true; + } + else if (requestedIndex - currentTranscodingIndex.Value > segmentGapRequiringTranscodingChange) + { + Logger.Debug("Starting transcoding because segmentGap is {0} and max allowed gap is {1}. requestedIndex={2}", requestedIndex - currentTranscodingIndex.Value, segmentGapRequiringTranscodingChange, requestedIndex); + startTranscoding = true; + } + if (startTranscoding) + { + // If the playlist doesn't already exist, startup ffmpeg + try + { + ApiEntryPoint.Instance.KillTranscodingJobs(request.DeviceId, request.PlaySessionId, p => false); + + if (currentTranscodingIndex.HasValue) + { + DeleteLastFile(playlistPath, segmentExtension, 0); + } + + request.StartTimeTicks = GetStartPositionTicks(state, requestedIndex); + + job = await StartFfMpeg(state, playlistPath, cancellationTokenSource).ConfigureAwait(false); + } + catch + { + state.Dispose(); + throw; + } + + //await WaitForMinimumSegmentCount(playlistPath, 1, cancellationTokenSource.Token).ConfigureAwait(false); + } + else + { + job = ApiEntryPoint.Instance.OnTranscodeBeginRequest(playlistPath, TranscodingJobType); + if (job.TranscodingThrottler != null) + { + job.TranscodingThrottler.UnpauseTranscoding(); + } + } + } + } + finally + { + if (!released) + { + transcodingLock.Release(); + } + } + + //Logger.Info("waiting for {0}", segmentPath); + //while (!File.Exists(segmentPath)) + //{ + // await Task.Delay(50, cancellationToken).ConfigureAwait(false); + //} + + Logger.Info("returning {0}", segmentPath); + job = job ?? ApiEntryPoint.Instance.OnTranscodeBeginRequest(playlistPath, TranscodingJobType); + return await GetSegmentResult(state, playlistPath, segmentPath, segmentExtension, requestedIndex, job, cancellationToken).ConfigureAwait(false); + } + + private const int BufferSize = 81920; + + private long GetStartPositionTicks(StreamState state, int requestedIndex) + { + double startSeconds = 0; + var lengths = GetSegmentLengths(state); + + if (requestedIndex >= lengths.Length) + { + var msg = string.Format("Invalid segment index requested: {0} - Segment count: {1}", requestedIndex, lengths.Length); + throw new ArgumentException(msg); + } + + for (var i = 0; i < requestedIndex; i++) + { + startSeconds += lengths[i]; + } + + var position = TimeSpan.FromSeconds(startSeconds).Ticks; + return position; + } + + private long GetEndPositionTicks(StreamState state, int requestedIndex) + { + double startSeconds = 0; + var lengths = GetSegmentLengths(state); + + if (requestedIndex >= lengths.Length) + { + var msg = string.Format("Invalid segment index requested: {0} - Segment count: {1}", requestedIndex, lengths.Length); + throw new ArgumentException(msg); + } + + for (var i = 0; i <= requestedIndex; i++) + { + startSeconds += lengths[i]; + } + + var position = TimeSpan.FromSeconds(startSeconds).Ticks; + return position; + } + + private double[] GetSegmentLengths(StreamState state) + { + var result = new List(); + + var ticks = state.RunTimeTicks ?? 0; + + var segmentLengthTicks = TimeSpan.FromSeconds(state.SegmentLength).Ticks; + + while (ticks > 0) + { + var length = ticks >= segmentLengthTicks ? segmentLengthTicks : ticks; + + result.Add(TimeSpan.FromTicks(length).TotalSeconds); + + ticks -= length; + } + + return result.ToArray(); + } + + public int? GetCurrentTranscodingIndex(string playlist, string segmentExtension) + { + var job = ApiEntryPoint.Instance.GetTranscodingJob(playlist, TranscodingJobType); + + if (job == null || job.HasExited) + { + return null; + } + + var file = GetLastTranscodingFile(playlist, segmentExtension, FileSystem); + + if (file == null) + { + return null; + } + + var playlistFilename = Path.GetFileNameWithoutExtension(playlist); + + var indexString = Path.GetFileNameWithoutExtension(file.Name).Substring(playlistFilename.Length); + + return int.Parse(indexString, NumberStyles.Integer, UsCulture); + } + + private void DeleteLastFile(string playlistPath, string segmentExtension, int retryCount) + { + var file = GetLastTranscodingFile(playlistPath, segmentExtension, FileSystem); + + if (file != null) + { + DeleteFile(file.FullName, retryCount); + } + } + + private void DeleteFile(string path, int retryCount) + { + if (retryCount >= 5) + { + return; + } + + Logger.Debug("Deleting partial HLS file {0}", path); + + try + { + FileSystem.DeleteFile(path); + } + catch (IOException ex) + { + Logger.ErrorException("Error deleting partial stream file(s) {0}", ex, path); + + var task = Task.Delay(100); + Task.WaitAll(task); + DeleteFile(path, retryCount + 1); + } + catch (Exception ex) + { + Logger.ErrorException("Error deleting partial stream file(s) {0}", ex, path); + } + } + + private static FileSystemMetadata GetLastTranscodingFile(string playlist, string segmentExtension, IFileSystem fileSystem) + { + var folder = fileSystem.GetDirectoryName(playlist); + + var filePrefix = Path.GetFileNameWithoutExtension(playlist) ?? string.Empty; + + try + { + return fileSystem.GetFiles(folder, new[] { segmentExtension }, true, false) + .Where(i => Path.GetFileNameWithoutExtension(i.Name).StartsWith(filePrefix, StringComparison.OrdinalIgnoreCase)) + .OrderByDescending(fileSystem.GetLastWriteTimeUtc) + .FirstOrDefault(); + } + catch (IOException) + { + return null; + } + } + + protected override int GetStartNumber(StreamState state) + { + return GetStartNumber(state.VideoRequest); + } + + private int GetStartNumber(VideoStreamRequest request) + { + var segmentId = "0"; + + var segmentRequest = request as GetHlsVideoSegment; + if (segmentRequest != null) + { + segmentId = segmentRequest.SegmentId; + } + + return int.Parse(segmentId, NumberStyles.Integer, UsCulture); + } + + private string GetSegmentPath(StreamState state, string playlist, int index) + { + var folder = FileSystem.GetDirectoryName(playlist); + + var filename = Path.GetFileNameWithoutExtension(playlist); + + return Path.Combine(folder, filename + index.ToString(UsCulture) + GetSegmentFileExtension(state.Request)); + } + + private async Task GetSegmentResult(StreamState state, + string playlistPath, + string segmentPath, + string segmentExtension, + int segmentIndex, + TranscodingJob transcodingJob, + CancellationToken cancellationToken) + { + var segmentFileExists = FileSystem.FileExists(segmentPath); + + // If all transcoding has completed, just return immediately + if (transcodingJob != null && transcodingJob.HasExited && segmentFileExists) + { + return await GetSegmentResult(state, segmentPath, segmentIndex, transcodingJob).ConfigureAwait(false); + } + + if (segmentFileExists) + { + var currentTranscodingIndex = GetCurrentTranscodingIndex(playlistPath, segmentExtension); + + // If requested segment is less than transcoding position, we can't transcode backwards, so assume it's ready + if (segmentIndex < currentTranscodingIndex) + { + return await GetSegmentResult(state, segmentPath, segmentIndex, transcodingJob).ConfigureAwait(false); + } + } + + var segmentFilename = Path.GetFileName(segmentPath); + + while (!cancellationToken.IsCancellationRequested) + { + try + { + var text = FileSystem.ReadAllText(playlistPath, Encoding.UTF8); + + // If it appears in the playlist, it's done + if (text.IndexOf(segmentFilename, StringComparison.OrdinalIgnoreCase) != -1) + { + if (!segmentFileExists) + { + segmentFileExists = FileSystem.FileExists(segmentPath); + } + if (segmentFileExists) + { + return await GetSegmentResult(state, segmentPath, segmentIndex, transcodingJob).ConfigureAwait(false); + } + //break; + } + } + catch (IOException) + { + // May get an error if the file is locked + } + + await Task.Delay(100, cancellationToken).ConfigureAwait(false); + } + + cancellationToken.ThrowIfCancellationRequested(); + return await GetSegmentResult(state, segmentPath, segmentIndex, transcodingJob).ConfigureAwait(false); + } + + private Task GetSegmentResult(StreamState state, string segmentPath, int index, TranscodingJob transcodingJob) + { + var segmentEndingPositionTicks = GetEndPositionTicks(state, index); + + return ResultFactory.GetStaticFileResult(Request, new StaticFileResultOptions + { + Path = segmentPath, + FileShare = FileShareMode.ReadWrite, + OnComplete = () => + { + if (transcodingJob != null) + { + transcodingJob.DownloadPositionTicks = Math.Max(transcodingJob.DownloadPositionTicks ?? segmentEndingPositionTicks, segmentEndingPositionTicks); + ApiEntryPoint.Instance.OnTranscodeEndRequest(transcodingJob); + } + } + }); + } + + private async Task GetMasterPlaylistInternal(StreamRequest request, string method) + { + var state = await GetState(request, CancellationToken.None).ConfigureAwait(false); + + if (string.IsNullOrEmpty(request.MediaSourceId)) + { + throw new ArgumentException("MediaSourceId is required"); + } + + var playlistText = string.Empty; + + if (string.Equals(method, "GET", StringComparison.OrdinalIgnoreCase)) + { + var audioBitrate = state.OutputAudioBitrate ?? 0; + var videoBitrate = state.OutputVideoBitrate ?? 0; + + playlistText = GetMasterPlaylistFileText(state, videoBitrate + audioBitrate); + } + + return ResultFactory.GetResult(playlistText, MimeTypes.GetMimeType("playlist.m3u8"), new Dictionary()); + } + + private string GetMasterPlaylistFileText(StreamState state, int totalBitrate) + { + var builder = new StringBuilder(); + + builder.AppendLine("#EXTM3U"); + + var isLiveStream = state.IsSegmentedLiveStream; + + var queryStringIndex = Request.RawUrl.IndexOf('?'); + var queryString = queryStringIndex == -1 ? string.Empty : Request.RawUrl.Substring(queryStringIndex); + + // from universal audio service + if (queryString.IndexOf("SegmentContainer", StringComparison.OrdinalIgnoreCase) == -1 && !string.IsNullOrWhiteSpace(state.Request.SegmentContainer)) + { + queryString += "&SegmentContainer=" + state.Request.SegmentContainer; + } + // from universal audio service + if (!string.IsNullOrWhiteSpace(state.Request.TranscodeReasons) && queryString.IndexOf("TranscodeReasons=", StringComparison.OrdinalIgnoreCase) == -1) + { + queryString += "&TranscodeReasons=" + state.Request.TranscodeReasons; + } + + // Main stream + var playlistUrl = isLiveStream ? "live.m3u8" : "main.m3u8"; + + playlistUrl += queryString; + + var request = state.Request; + + var subtitleStreams = state.MediaSource + .MediaStreams + .Where(i => i.IsTextSubtitleStream) + .ToList(); + + var subtitleGroup = subtitleStreams.Count > 0 && + request is GetMasterHlsVideoPlaylist && + (state.SubtitleDeliveryMethod == SubtitleDeliveryMethod.Hls || state.VideoRequest.EnableSubtitlesInManifest) ? + "subs" : + null; + + // If we're burning in subtitles then don't add additional subs to the manifest + if (state.SubtitleStream != null && state.SubtitleDeliveryMethod == SubtitleDeliveryMethod.Encode) + { + subtitleGroup = null; + } + + if (!string.IsNullOrWhiteSpace(subtitleGroup)) + { + AddSubtitles(state, subtitleStreams, builder); + } + + AppendPlaylist(builder, state, playlistUrl, totalBitrate, subtitleGroup); + + if (EnableAdaptiveBitrateStreaming(state, isLiveStream)) + { + var requestedVideoBitrate = state.VideoRequest == null ? 0 : state.VideoRequest.VideoBitRate ?? 0; + + // By default, vary by just 200k + var variation = GetBitrateVariation(totalBitrate); + + var newBitrate = totalBitrate - variation; + var variantUrl = ReplaceBitrate(playlistUrl, requestedVideoBitrate, requestedVideoBitrate - variation); + AppendPlaylist(builder, state, variantUrl, newBitrate, subtitleGroup); + + variation *= 2; + newBitrate = totalBitrate - variation; + variantUrl = ReplaceBitrate(playlistUrl, requestedVideoBitrate, requestedVideoBitrate - variation); + AppendPlaylist(builder, state, variantUrl, newBitrate, subtitleGroup); + } + + return builder.ToString(); + } + + private string ReplaceBitrate(string url, int oldValue, int newValue) + { + return url.Replace( + "videobitrate=" + oldValue.ToString(UsCulture), + "videobitrate=" + newValue.ToString(UsCulture), + StringComparison.OrdinalIgnoreCase); + } + + private void AddSubtitles(StreamState state, IEnumerable subtitles, StringBuilder builder) + { + var selectedIndex = state.SubtitleStream == null || state.SubtitleDeliveryMethod != SubtitleDeliveryMethod.Hls ? (int?)null : state.SubtitleStream.Index; + + foreach (var stream in subtitles) + { + const string format = "#EXT-X-MEDIA:TYPE=SUBTITLES,GROUP-ID=\"subs\",NAME=\"{0}\",DEFAULT={1},FORCED={2},AUTOSELECT=YES,URI=\"{3}\",LANGUAGE=\"{4}\""; + + var name = stream.DisplayTitle; + + var isDefault = selectedIndex.HasValue && selectedIndex.Value == stream.Index; + var isForced = stream.IsForced; + + var url = string.Format("{0}/Subtitles/{1}/subtitles.m3u8?SegmentLength={2}&api_key={3}", + state.Request.MediaSourceId, + stream.Index.ToString(UsCulture), + 30.ToString(UsCulture), + AuthorizationContext.GetAuthorizationInfo(Request).Token); + + var line = string.Format(format, + name, + isDefault ? "YES" : "NO", + isForced ? "YES" : "NO", + url, + stream.Language ?? "Unknown"); + + builder.AppendLine(line); + } + } + + private bool EnableAdaptiveBitrateStreaming(StreamState state, bool isLiveStream) + { + // Within the local network this will likely do more harm than good. + if (Request.IsLocal || NetworkManager.IsInLocalNetwork(Request.RemoteIp)) + { + return false; + } + + var request = state.Request as IMasterHlsRequest; + if (request != null && !request.EnableAdaptiveBitrateStreaming) + { + return false; + } + + if (isLiveStream || string.IsNullOrWhiteSpace(state.MediaPath)) + { + // Opening live streams is so slow it's not even worth it + return false; + } + + if (string.Equals(state.OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase)) + { + return false; + } + + if (string.Equals(state.OutputAudioCodec, "copy", StringComparison.OrdinalIgnoreCase)) + { + return false; + } + + if (!state.IsOutputVideo) + { + return false; + } + + // Having problems in android + return false; + //return state.VideoRequest.VideoBitRate.HasValue; + } + + private void AppendPlaylist(StringBuilder builder, StreamState state, string url, int bitrate, string subtitleGroup) + { + var header = "#EXT-X-STREAM-INF:BANDWIDTH=" + bitrate.ToString(UsCulture) + ",AVERAGE-BANDWIDTH=" + bitrate.ToString(UsCulture); + + // tvos wants resolution, codecs, framerate + //if (state.TargetFramerate.HasValue) + //{ + // header += string.Format(",FRAME-RATE=\"{0}\"", state.TargetFramerate.Value.ToString(CultureInfo.InvariantCulture)); + //} + + if (!string.IsNullOrWhiteSpace(subtitleGroup)) + { + header += string.Format(",SUBTITLES=\"{0}\"", subtitleGroup); + } + + builder.AppendLine(header); + builder.AppendLine(url); + } + + private int GetBitrateVariation(int bitrate) + { + // By default, vary by just 50k + var variation = 50000; + + if (bitrate >= 10000000) + { + variation = 2000000; + } + else if (bitrate >= 5000000) + { + variation = 1500000; + } + else if (bitrate >= 3000000) + { + variation = 1000000; + } + else if (bitrate >= 2000000) + { + variation = 500000; + } + else if (bitrate >= 1000000) + { + variation = 300000; + } + else if (bitrate >= 600000) + { + variation = 200000; + } + else if (bitrate >= 400000) + { + variation = 100000; + } + + return variation; + } + + private async Task GetVariantPlaylistInternal(StreamRequest request, bool isOutputVideo, string name) + { + var state = await GetState(request, CancellationToken.None).ConfigureAwait(false); + + var segmentLengths = GetSegmentLengths(state); + + var builder = new StringBuilder(); + + builder.AppendLine("#EXTM3U"); + builder.AppendLine("#EXT-X-PLAYLIST-TYPE:VOD"); + builder.AppendLine("#EXT-X-VERSION:3"); + builder.AppendLine("#EXT-X-TARGETDURATION:" + Math.Ceiling(segmentLengths.Length > 0 ? segmentLengths.Max() : state.SegmentLength).ToString(UsCulture)); + builder.AppendLine("#EXT-X-MEDIA-SEQUENCE:0"); + + var queryStringIndex = Request.RawUrl.IndexOf('?'); + var queryString = queryStringIndex == -1 ? string.Empty : Request.RawUrl.Substring(queryStringIndex); + + //if ((Request.UserAgent ?? string.Empty).IndexOf("roku", StringComparison.OrdinalIgnoreCase) != -1) + //{ + // queryString = string.Empty; + //} + + var index = 0; + + foreach (var length in segmentLengths) + { + builder.AppendLine("#EXTINF:" + length.ToString("0.0000", UsCulture) + ", nodesc"); + + builder.AppendLine(string.Format("hls1/{0}/{1}{2}{3}", + + name, + index.ToString(UsCulture), + GetSegmentFileExtension(request), + queryString)); + + index++; + } + + builder.AppendLine("#EXT-X-ENDLIST"); + + var playlistText = builder.ToString(); + + return ResultFactory.GetResult(playlistText, MimeTypes.GetMimeType("playlist.m3u8"), new Dictionary()); + } + + protected override string GetAudioArguments(StreamState state, EncodingOptions encodingOptions) + { + var audioCodec = EncodingHelper.GetAudioEncoder(state); + + if (!state.IsOutputVideo) + { + if (string.Equals(audioCodec, "copy", StringComparison.OrdinalIgnoreCase)) + { + return "-acodec copy"; + } + + var audioTranscodeParams = new List(); + + audioTranscodeParams.Add("-acodec " + audioCodec); + + if (state.OutputAudioBitrate.HasValue) + { + audioTranscodeParams.Add("-ab " + state.OutputAudioBitrate.Value.ToString(UsCulture)); + } + + if (state.OutputAudioChannels.HasValue) + { + audioTranscodeParams.Add("-ac " + state.OutputAudioChannels.Value.ToString(UsCulture)); + } + + if (state.OutputAudioSampleRate.HasValue) + { + audioTranscodeParams.Add("-ar " + state.OutputAudioSampleRate.Value.ToString(UsCulture)); + } + + audioTranscodeParams.Add("-vn"); + return string.Join(" ", audioTranscodeParams.ToArray()); + } + + if (string.Equals(audioCodec, "copy", StringComparison.OrdinalIgnoreCase)) + { + var videoCodec = EncodingHelper.GetVideoEncoder(state, encodingOptions); + + if (string.Equals(videoCodec, "copy", StringComparison.OrdinalIgnoreCase) && state.EnableBreakOnNonKeyFrames(videoCodec)) + { + return "-codec:a:0 copy -copypriorss:a:0 0"; + } + + return "-codec:a:0 copy"; + } + + var args = "-codec:a:0 " + audioCodec; + + var channels = state.OutputAudioChannels; + + if (channels.HasValue) + { + args += " -ac " + channels.Value; + } + + var bitrate = state.OutputAudioBitrate; + + if (bitrate.HasValue) + { + args += " -ab " + bitrate.Value.ToString(UsCulture); + } + + if (state.OutputAudioSampleRate.HasValue) + { + args += " -ar " + state.OutputAudioSampleRate.Value.ToString(UsCulture); + } + + args += " " + EncodingHelper.GetAudioFilterParam(state, encodingOptions, true); + + return args; + } + + protected override string GetVideoArguments(StreamState state, EncodingOptions encodingOptions) + { + if (!state.IsOutputVideo) + { + return string.Empty; + } + + var codec = EncodingHelper.GetVideoEncoder(state, encodingOptions); + + var args = "-codec:v:0 " + codec; + + // if (state.EnableMpegtsM2TsMode) + // { + // args += " -mpegts_m2ts_mode 1"; + // } + + // See if we can save come cpu cycles by avoiding encoding + if (string.Equals(codec, "copy", StringComparison.OrdinalIgnoreCase)) + { + if (state.VideoStream != null && EncodingHelper.IsH264(state.VideoStream) && !string.Equals(state.VideoStream.NalLengthSize, "0", StringComparison.OrdinalIgnoreCase)) + { + args += " -bsf:v h264_mp4toannexb"; + } + + //args += " -flags -global_header"; + } + else + { + 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 && state.SubtitleDeliveryMethod == SubtitleDeliveryMethod.Encode; + + args += " " + EncodingHelper.GetVideoQualityParam(state, codec, encodingOptions, GetDefaultH264Preset()) + keyFrameArg; + + //args += " -mixed-refs 0 -refs 3 -x264opts b_pyramid=0:weightb=0:weightp=0"; + + // Add resolution params, if specified + if (!hasGraphicalSubs) + { + args += EncodingHelper.GetOutputSizeParam(state, encodingOptions, codec, true); + } + + // This is for internal graphical subs + if (hasGraphicalSubs) + { + args += EncodingHelper.GetGraphicalSubtitleParam(state, encodingOptions, codec); + } + + //args += " -flags -global_header"; + } + + if (args.IndexOf("-copyts", StringComparison.OrdinalIgnoreCase) == -1) + { + args += " -copyts"; + } + + if (!string.IsNullOrEmpty(state.OutputVideoSync)) + { + args += " -vsync " + state.OutputVideoSync; + } + + args += EncodingHelper.GetOutputFFlags(state); + + return args; + } + + protected override string GetCommandLineArguments(string outputPath, EncodingOptions encodingOptions, StreamState state, bool isEncoding) + { + var videoCodec = EncodingHelper.GetVideoEncoder(state, encodingOptions); + + var threads = EncodingHelper.GetNumberOfThreads(state, encodingOptions, videoCodec); + + var inputModifier = EncodingHelper.GetInputModifier(state, encodingOptions); + + // If isEncoding is true we're actually starting ffmpeg + var startNumber = GetStartNumber(state); + var startNumberParam = isEncoding ? startNumber.ToString(UsCulture) : "0"; + + var mapArgs = state.IsOutputVideo ? EncodingHelper.GetMapArgs(state) : string.Empty; + + var outputTsArg = Path.Combine(FileSystem.GetDirectoryName(outputPath), Path.GetFileNameWithoutExtension(outputPath)) + "%d" + GetSegmentFileExtension(state.Request); + + var timeDeltaParam = String.Empty; + + if (isEncoding && startNumber > 0) + { + var startTime = state.SegmentLength * startNumber; + timeDeltaParam = string.Format("-segment_time_delta -{0}", startTime); + } + + var segmentFormat = GetSegmentFileExtension(state.Request).TrimStart('.'); + if (string.Equals(segmentFormat, "ts", StringComparison.OrdinalIgnoreCase)) + { + segmentFormat = "mpegts"; + } + + var breakOnNonKeyFrames = state.EnableBreakOnNonKeyFrames(videoCodec); + + var breakOnNonKeyFramesArg = breakOnNonKeyFrames ? " -break_non_keyframes 1" : ""; + + return string.Format("{0} {1} -map_metadata -1 -map_chapters -1 -threads {2} {3} {4} {5} -f segment -max_delay 5000000 -avoid_negative_ts disabled -start_at_zero -segment_time {6} {10} -individual_header_trailer 0{12} -segment_format {11} -segment_list_type m3u8 -segment_start_number {7} -segment_list \"{8}\" -y \"{9}\"", + inputModifier, + EncodingHelper.GetInputArgument(state, encodingOptions), + threads, + mapArgs, + GetVideoArguments(state, encodingOptions), + GetAudioArguments(state, encodingOptions), + state.SegmentLength.ToString(UsCulture), + startNumberParam, + outputPath, + outputTsArg, + timeDeltaParam, + segmentFormat, + breakOnNonKeyFramesArg + ).Trim(); + } + } +} \ No newline at end of file diff --git a/MediaBrowser.Api/Playback/Hls/HlsSegmentService.cs b/MediaBrowser.Api/Playback/Hls/HlsSegmentService.cs new file mode 100644 index 0000000000..52cc025283 --- /dev/null +++ b/MediaBrowser.Api/Playback/Hls/HlsSegmentService.cs @@ -0,0 +1,163 @@ +using MediaBrowser.Controller; +using MediaBrowser.Controller.Configuration; +using MediaBrowser.Controller.Net; +using System; +using System.IO; +using System.Linq; +using System.Threading.Tasks; + +using MediaBrowser.Controller.MediaEncoding; +using MediaBrowser.Model.IO; +using MediaBrowser.Model.Services; + +namespace MediaBrowser.Api.Playback.Hls +{ + /// + /// Class GetHlsAudioSegment + /// + // Can't require authentication just yet due to seeing some requests come from Chrome without full query string + //[Authenticated] + [Route("/Audio/{Id}/hls/{SegmentId}/stream.mp3", "GET")] + [Route("/Audio/{Id}/hls/{SegmentId}/stream.aac", "GET")] + public class GetHlsAudioSegmentLegacy + { + // TODO: Deprecate with new iOS app + + /// + /// Gets or sets the id. + /// + /// The id. + public string Id { get; set; } + + /// + /// Gets or sets the segment id. + /// + /// The segment id. + public string SegmentId { get; set; } + } + + /// + /// Class GetHlsVideoSegment + /// + [Route("/Videos/{Id}/hls/{PlaylistId}/stream.m3u8", "GET")] + [Authenticated] + public class GetHlsPlaylistLegacy + { + // TODO: Deprecate with new iOS app + + /// + /// Gets or sets the id. + /// + /// The id. + public string Id { get; set; } + + public string PlaylistId { get; set; } + } + + [Route("/Videos/ActiveEncodings", "DELETE")] + [Authenticated] + public class StopEncodingProcess + { + [ApiMember(Name = "DeviceId", Description = "The device id of the client requesting. Used to stop encoding processes when needed.", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "DELETE")] + public string DeviceId { get; set; } + + [ApiMember(Name = "PlaySessionId", Description = "The play session id", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "DELETE")] + public string PlaySessionId { get; set; } + } + + /// + /// Class GetHlsVideoSegment + /// + // Can't require authentication just yet due to seeing some requests come from Chrome without full query string + //[Authenticated] + [Route("/Videos/{Id}/hls/{PlaylistId}/{SegmentId}.{SegmentContainer}", "GET")] + public class GetHlsVideoSegmentLegacy : VideoStreamRequest + { + public string PlaylistId { get; set; } + + /// + /// Gets or sets the segment id. + /// + /// The segment id. + public string SegmentId { get; set; } + } + + public class HlsSegmentService : BaseApiService + { + private readonly IServerApplicationPaths _appPaths; + private readonly IServerConfigurationManager _config; + private readonly IFileSystem _fileSystem; + + public HlsSegmentService(IServerApplicationPaths appPaths, IServerConfigurationManager config, IFileSystem fileSystem) + { + _appPaths = appPaths; + _config = config; + _fileSystem = fileSystem; + } + + public Task Get(GetHlsPlaylistLegacy request) + { + var file = request.PlaylistId + Path.GetExtension(Request.PathInfo); + file = Path.Combine(_appPaths.TranscodingTempPath, file); + + return GetFileResult(file, file); + } + + public void Delete(StopEncodingProcess request) + { + ApiEntryPoint.Instance.KillTranscodingJobs(request.DeviceId, request.PlaySessionId, path => true); + } + + /// + /// Gets the specified request. + /// + /// The request. + /// System.Object. + public Task Get(GetHlsVideoSegmentLegacy request) + { + var file = request.SegmentId + Path.GetExtension(Request.PathInfo); + + var transcodeFolderPath = _config.ApplicationPaths.TranscodingTempPath; + file = Path.Combine(transcodeFolderPath, file); + + var normalizedPlaylistId = request.PlaylistId; + + var playlistPath = _fileSystem.GetFilePaths(transcodeFolderPath) + .FirstOrDefault(i => string.Equals(Path.GetExtension(i), ".m3u8", StringComparison.OrdinalIgnoreCase) && i.IndexOf(normalizedPlaylistId, StringComparison.OrdinalIgnoreCase) != -1); + + return GetFileResult(file, playlistPath); + } + + /// + /// Gets the specified request. + /// + /// The request. + /// System.Object. + public Task Get(GetHlsAudioSegmentLegacy request) + { + // TODO: Deprecate with new iOS app + var file = request.SegmentId + Path.GetExtension(Request.PathInfo); + file = Path.Combine(_appPaths.TranscodingTempPath, file); + + return ResultFactory.GetStaticFileResult(Request, file, FileShareMode.ReadWrite); + } + + private Task GetFileResult(string path, string playlistPath) + { + var transcodingJob = ApiEntryPoint.Instance.OnTranscodeBeginRequest(playlistPath, TranscodingJobType.Hls); + + return ResultFactory.GetStaticFileResult(Request, new StaticFileResultOptions + { + Path = path, + FileShare = FileShareMode.ReadWrite, + OnComplete = () => + { + if (transcodingJob != null) + { + ApiEntryPoint.Instance.OnTranscodeEndRequest(transcodingJob); + } + } + }); + } + } +} diff --git a/MediaBrowser.Api/Playback/Hls/VideoHlsService.cs b/MediaBrowser.Api/Playback/Hls/VideoHlsService.cs new file mode 100644 index 0000000000..1ae7ea3a84 --- /dev/null +++ b/MediaBrowser.Api/Playback/Hls/VideoHlsService.cs @@ -0,0 +1,137 @@ +using MediaBrowser.Controller.Configuration; +using MediaBrowser.Controller.Devices; +using MediaBrowser.Controller.Dlna; +using MediaBrowser.Controller.Library; +using MediaBrowser.Controller.MediaEncoding; +using MediaBrowser.Model.IO; +using MediaBrowser.Model.Serialization; +using System; +using MediaBrowser.Controller.Net; +using MediaBrowser.Model.Configuration; +using MediaBrowser.Model.Dlna; +using MediaBrowser.Model.Services; + +namespace MediaBrowser.Api.Playback.Hls +{ + [Route("/Videos/{Id}/live.m3u8", "GET")] + public class GetLiveHlsStream : VideoStreamRequest + { + } + + /// + /// Class VideoHlsService + /// + [Authenticated] + public class VideoHlsService : BaseHlsService + { + public object Get(GetLiveHlsStream request) + { + return ProcessRequest(request, true); + } + + /// + /// Gets the audio arguments. + /// + protected override string GetAudioArguments(StreamState state, EncodingOptions encodingOptions) + { + var codec = EncodingHelper.GetAudioEncoder(state); + + if (string.Equals(codec, "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); + } + + if (state.OutputAudioSampleRate.HasValue) + { + args += " -ar " + state.OutputAudioSampleRate.Value.ToString(UsCulture); + } + + args += " " + EncodingHelper.GetAudioFilterParam(state, encodingOptions, true); + + return args; + } + + /// + /// Gets the video arguments. + /// + protected override string GetVideoArguments(StreamState state, EncodingOptions encodingOptions) + { + if (!state.IsOutputVideo) + { + return string.Empty; + } + + var codec = EncodingHelper.GetVideoEncoder(state, encodingOptions); + + var args = "-codec:v:0 " + codec; + + // if (state.EnableMpegtsM2TsMode) + // { + // args += " -mpegts_m2ts_mode 1"; + // } + + // See if we can save come cpu cycles by avoiding encoding + if (codec.Equals("copy", StringComparison.OrdinalIgnoreCase)) + { + // if h264_mp4toannexb is ever added, do not use it for live tv + if (state.VideoStream != null && EncodingHelper.IsH264(state.VideoStream) && + !string.Equals(state.VideoStream.NalLengthSize, "0", StringComparison.OrdinalIgnoreCase)) + { + args += " -bsf:v h264_mp4toannexb"; + } + } + else + { + 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 && state.SubtitleDeliveryMethod == SubtitleDeliveryMethod.Encode; + + args += " " + EncodingHelper.GetVideoQualityParam(state, codec, encodingOptions, GetDefaultH264Preset()) + keyFrameArg; + + // Add resolution params, if specified + if (!hasGraphicalSubs) + { + args += EncodingHelper.GetOutputSizeParam(state, encodingOptions, codec); + } + + // This is for internal graphical subs + if (hasGraphicalSubs) + { + args += EncodingHelper.GetGraphicalSubtitleParam(state, encodingOptions, codec); + } + } + + args += " -flags -global_header"; + + if (!string.IsNullOrEmpty(state.OutputVideoSync)) + { + args += " -vsync " + state.OutputVideoSync; + } + + args += EncodingHelper.GetOutputFFlags(state); + + return args; + } + + public VideoHlsService(IServerConfigurationManager serverConfig, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager, IMediaEncoder mediaEncoder, IFileSystem fileSystem, IDlnaManager dlnaManager, ISubtitleEncoder subtitleEncoder, IDeviceManager deviceManager, IMediaSourceManager mediaSourceManager, IZipClient zipClient, IJsonSerializer jsonSerializer, IAuthorizationContext authorizationContext) : base(serverConfig, userManager, libraryManager, isoManager, mediaEncoder, fileSystem, dlnaManager, subtitleEncoder, deviceManager, mediaSourceManager, zipClient, jsonSerializer, authorizationContext) + { + } + } +} diff --git a/MediaBrowser.Api/Playback/MediaInfoService.cs b/MediaBrowser.Api/Playback/MediaInfoService.cs new file mode 100644 index 0000000000..2db0f8f419 --- /dev/null +++ b/MediaBrowser.Api/Playback/MediaInfoService.cs @@ -0,0 +1,618 @@ +using MediaBrowser.Common.Net; +using MediaBrowser.Controller.Configuration; +using MediaBrowser.Controller.Devices; +using MediaBrowser.Controller.Entities; +using MediaBrowser.Controller.Library; +using MediaBrowser.Controller.Net; +using MediaBrowser.Model.Dlna; +using MediaBrowser.Model.Dto; +using MediaBrowser.Model.Entities; +using MediaBrowser.Model.MediaInfo; +using MediaBrowser.Model.Session; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using MediaBrowser.Controller.Entities.Audio; +using MediaBrowser.Controller.MediaEncoding; +using MediaBrowser.Model.Serialization; +using MediaBrowser.Model.Services; + +namespace MediaBrowser.Api.Playback +{ + [Route("/Items/{Id}/PlaybackInfo", "GET", Summary = "Gets live playback media info for an item")] + public class GetPlaybackInfo : IReturn + { + [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 + { + } + + [Route("/LiveStreams/Open", "POST", Summary = "Opens a media source")] + public class OpenMediaSource : LiveStreamRequest, IReturn + { + } + + [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")] + public long Size { get; set; } + + public GetBitrateTestBytes() + { + // 100k + Size = 102400; + } + } + + [Authenticated] + public class MediaInfoService : BaseApiService + { + private readonly IMediaSourceManager _mediaSourceManager; + private readonly IDeviceManager _deviceManager; + private readonly ILibraryManager _libraryManager; + private readonly IServerConfigurationManager _config; + private readonly INetworkManager _networkManager; + private readonly IMediaEncoder _mediaEncoder; + private readonly IUserManager _userManager; + private readonly IJsonSerializer _json; + private readonly IAuthorizationContext _authContext; + + public MediaInfoService(IMediaSourceManager mediaSourceManager, IDeviceManager deviceManager, ILibraryManager libraryManager, IServerConfigurationManager config, INetworkManager networkManager, IMediaEncoder mediaEncoder, IUserManager userManager, IJsonSerializer json, IAuthorizationContext authContext) + { + _mediaSourceManager = mediaSourceManager; + _deviceManager = deviceManager; + _libraryManager = libraryManager; + _config = config; + _networkManager = networkManager; + _mediaEncoder = mediaEncoder; + _userManager = userManager; + _json = json; + _authContext = authContext; + } + + 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"); + } + + public async Task Get(GetPlaybackInfo request) + { + var result = await GetPlaybackInfo(request.Id, request.UserId, new[] { MediaType.Audio, MediaType.Video }).ConfigureAwait(false); + return ToOptimizedResult(result); + } + + public async Task Post(OpenMediaSource request) + { + var result = await OpenMediaSource(request).ConfigureAwait(false); + + return ToOptimizedResult(result); + } + + private async Task OpenMediaSource(OpenMediaSource request) + { + var authInfo = _authContext.GetAuthorizationInfo(Request); + + var result = await _mediaSourceManager.OpenLiveStream(request, CancellationToken.None).ConfigureAwait(false); + + 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) + { + var task = _mediaSourceManager.CloseLiveStream(request.LiveStreamId); + Task.WaitAll(task); + } + + public async Task GetPlaybackInfo(GetPostedPlaybackInfo request) + { + var authInfo = _authContext.GetAuthorizationInfo(Request); + + var profile = request.DeviceProfile; + + //Logger.Info("GetPostedPlaybackInfo profile: {0}", _json.SerializeToString(profile)); + + 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, + OpenToken = mediaSource.OpenToken, + //EnableMediaProbe = request.EnableMediaProbe + + }).ConfigureAwait(false); + + info.MediaSources = new MediaSourceInfo[] { openStreamResult.MediaSource }; + } + } + + 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 Post(GetPostedPlaybackInfo request) + { + var result = await GetPlaybackInfo(request).ConfigureAwait(false); + + return ToOptimizedResult(result); + } + + private T Clone(T obj) + { + // 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); + } + + private async Task 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(); + + if (string.IsNullOrWhiteSpace(liveStreamId)) + { + IEnumerable mediaSources; + try + { + // TODO handle supportedLiveMediaTypes ? + mediaSources = await _mediaSourceManager.GetPlayackMediaSources(item, user, true, false, CancellationToken.None).ConfigureAwait(false); + } + catch (Exception ex) + { + mediaSources = new List(); + // TODO PlaybackException ?? + //result.ErrorCode = ex.ErrorCode; + } + + result.MediaSources = mediaSources.ToArray(); + + if (!string.IsNullOrWhiteSpace(mediaSourceId)) + { + result.MediaSources = result.MediaSources + .Where(i => string.Equals(i.Id, mediaSourceId, StringComparison.OrdinalIgnoreCase)) + .ToArray(); + } + } + else + { + var mediaSource = await _mediaSourceManager.GetLiveStream(liveStreamId, CancellationToken.None).ConfigureAwait(false); + + result.MediaSources = new MediaSourceInfo[] { mediaSource }; + } + + if (result.MediaSources.Length == 0) + { + if (!result.ErrorCode.HasValue) + { + result.ErrorCode = PlaybackErrorCode.NoCompatibleStream; + } + } + else + { + result.MediaSources = Clone(result.MediaSources); + + result.PlaySessionId = Guid.NewGuid().ToString("N"); + } + + return result; + } + + private void SetDeviceSpecificData(Guid itemId, + 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); + } + + private void SetDeviceSpecificData(BaseItem item, + 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 + { + MediaSources = new MediaSourceInfo[] { mediaSource }, + 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; + } + if (!enableDirectStream) + { + mediaSource.SupportsDirectStream = false; + } + if (!enableTranscoding) + { + mediaSource.SupportsTranscoding = false; + } + + if (item is Audio) + { + Logger.Info("User policy for {0}. EnableAudioPlaybackTranscoding: {1}", user.Name, user.Policy.EnableAudioPlaybackTranscoding); + } + else + { + Logger.Info("User policy for {0}. EnablePlaybackRemuxing: {1} EnableVideoPlaybackTranscoding: {2} EnableAudioPlaybackTranscoding: {3}", + user.Name, + user.Policy.EnablePlaybackRemuxing, + user.Policy.EnableVideoPlaybackTranscoding, + user.Policy.EnableAudioPlaybackTranscoding); + } + + if (mediaSource.SupportsDirectPlay) + { + if (mediaSource.IsRemote && forceDirectPlayRemoteMediaSource) + { + } + 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 + var streamInfo = string.Equals(item.MediaType, MediaType.Audio, StringComparison.OrdinalIgnoreCase) ? + streamBuilder.BuildAudioItem(options) : + streamBuilder.BuildVideoItem(options); + + 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) + { + options.MaxBitrate = GetMaxBitrate(maxBitrate, user); + + if (item is Audio) + { + 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; + } + } + + // 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); + + if (streamInfo == null || !streamInfo.IsDirectStream) + { + mediaSource.SupportsDirectStream = false; + } + + if (streamInfo != null) + { + SetDeviceSpecificSubtitleInfo(streamInfo, mediaSource, auth.Token); + } + } + + if (mediaSource.SupportsTranscoding) + { + 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); + + if (streamInfo != null) + { + streamInfo.PlaySessionId = playSessionId; + + if (streamInfo.PlayMethod == PlayMethod.Transcode) + { + 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; + } + + // Do this after the above so that StartPositionTicks is set + SetDeviceSpecificSubtitleInfo(streamInfo, mediaSource, auth.Token); + } + } + } + + private long? GetMaxBitrate(long? clientMaxBitrate, User user) + { + var maxBitrate = clientMaxBitrate; + var remoteClientMaxBitrate = user == null ? 0 : user.Policy.RemoteClientBitrateLimit; + + if (remoteClientMaxBitrate <= 0) + { + remoteClientMaxBitrate = _config.Configuration.RemoteClientBitrateLimit; + } + + if (remoteClientMaxBitrate > 0) + { + var isInLocalNetwork = _networkManager.IsInLocalNetwork(Request.RemoteIp); + + Logger.Info("RemoteClientBitrateLimit: {0}, RemoteIp: {1}, IsInLocalNetwork: {2}", remoteClientMaxBitrate, Request.RemoteIp, isInLocalNetwork); + 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 => + { + switch (i.Protocol) + { + case MediaProtocol.File: + return 0; + default: + return 1; + } + + }).ThenBy(i => + { + if (maxBitrate.HasValue) + { + if (i.Bitrate.HasValue) + { + if (i.Bitrate.Value <= maxBitrate.Value) + { + return 0; + } + + return 2; + } + } + + return 1; + + }).ThenBy(originalList.IndexOf) + .ToArray(); + } + } +} diff --git a/MediaBrowser.Api/Playback/Progressive/AudioService.cs b/MediaBrowser.Api/Playback/Progressive/AudioService.cs new file mode 100644 index 0000000000..44e096dd7f --- /dev/null +++ b/MediaBrowser.Api/Playback/Progressive/AudioService.cs @@ -0,0 +1,67 @@ +using MediaBrowser.Common.Net; +using MediaBrowser.Controller.Configuration; +using MediaBrowser.Controller.Devices; +using MediaBrowser.Controller.Dlna; +using MediaBrowser.Controller.Drawing; +using MediaBrowser.Controller.Library; +using MediaBrowser.Controller.MediaEncoding; +using MediaBrowser.Model.IO; +using MediaBrowser.Model.Serialization; +using System.Collections.Generic; +using System.Threading.Tasks; +using MediaBrowser.Controller.Net; +using MediaBrowser.Model.Configuration; +using MediaBrowser.Model.IO; +using MediaBrowser.Model.Services; +using MediaBrowser.Model.System; + +namespace MediaBrowser.Api.Playback.Progressive +{ + /// + /// Class GetAudioStream + /// + [Route("/Audio/{Id}/stream.{Container}", "GET", Summary = "Gets an audio stream")] + [Route("/Audio/{Id}/stream", "GET", Summary = "Gets an audio stream")] + [Route("/Audio/{Id}/stream.{Container}", "HEAD", Summary = "Gets an audio stream")] + [Route("/Audio/{Id}/stream", "HEAD", Summary = "Gets an audio stream")] + public class GetAudioStream : StreamRequest + { + } + + /// + /// Class AudioService + /// + // TODO: In order to autheneticate this in the future, Dlna playback will require updating + //[Authenticated] + public class AudioService : BaseProgressiveStreamingService + { + public AudioService(IServerConfigurationManager serverConfig, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager, IMediaEncoder mediaEncoder, IFileSystem fileSystem, IDlnaManager dlnaManager, ISubtitleEncoder subtitleEncoder, IDeviceManager deviceManager, IMediaSourceManager mediaSourceManager, IZipClient zipClient, IJsonSerializer jsonSerializer, IAuthorizationContext authorizationContext, IImageProcessor imageProcessor, IEnvironmentInfo environmentInfo) : base(serverConfig, userManager, libraryManager, isoManager, mediaEncoder, fileSystem, dlnaManager, subtitleEncoder, deviceManager, mediaSourceManager, zipClient, jsonSerializer, authorizationContext, imageProcessor, environmentInfo) + { + } + + /// + /// Gets the specified request. + /// + /// The request. + /// System.Object. + public Task Get(GetAudioStream request) + { + return ProcessRequest(request, false); + } + + /// + /// Gets the specified request. + /// + /// The request. + /// System.Object. + public Task Head(GetAudioStream request) + { + return ProcessRequest(request, true); + } + + protected override string GetCommandLineArguments(string outputPath, EncodingOptions encodingOptions, StreamState state, bool isEncoding) + { + return EncodingHelper.GetProgressiveAudioFullCommandLine(state, encodingOptions, outputPath); + } + } +} diff --git a/MediaBrowser.Api/Playback/Progressive/BaseProgressiveStreamingService.cs b/MediaBrowser.Api/Playback/Progressive/BaseProgressiveStreamingService.cs new file mode 100644 index 0000000000..44261f2d55 --- /dev/null +++ b/MediaBrowser.Api/Playback/Progressive/BaseProgressiveStreamingService.cs @@ -0,0 +1,429 @@ +using MediaBrowser.Common.Net; +using MediaBrowser.Controller.Configuration; +using MediaBrowser.Controller.Devices; +using MediaBrowser.Controller.Dlna; +using MediaBrowser.Controller.Drawing; +using MediaBrowser.Controller.Library; +using MediaBrowser.Controller.MediaEncoding; +using MediaBrowser.Controller.Net; +using MediaBrowser.Model.IO; +using MediaBrowser.Model.MediaInfo; +using MediaBrowser.Model.Serialization; +using System; +using System.Collections.Generic; +using System.Globalization; +using System.IO; +using System.Threading; +using System.Threading.Tasks; +using MediaBrowser.Model.Services; +using MediaBrowser.Model.System; + +namespace MediaBrowser.Api.Playback.Progressive +{ + /// + /// Class BaseProgressiveStreamingService + /// + public abstract class BaseProgressiveStreamingService : BaseStreamingService + { + protected readonly IImageProcessor ImageProcessor; + protected readonly IEnvironmentInfo EnvironmentInfo; + + public BaseProgressiveStreamingService(IServerConfigurationManager serverConfig, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager, IMediaEncoder mediaEncoder, IFileSystem fileSystem, IDlnaManager dlnaManager, ISubtitleEncoder subtitleEncoder, IDeviceManager deviceManager, IMediaSourceManager mediaSourceManager, IZipClient zipClient, IJsonSerializer jsonSerializer, IAuthorizationContext authorizationContext, IImageProcessor imageProcessor, IEnvironmentInfo environmentInfo) : base(serverConfig, userManager, libraryManager, isoManager, mediaEncoder, fileSystem, dlnaManager, subtitleEncoder, deviceManager, mediaSourceManager, zipClient, jsonSerializer, authorizationContext) + { + ImageProcessor = imageProcessor; + EnvironmentInfo = environmentInfo; + } + + /// + /// Gets the output file extension. + /// + /// The state. + /// System.String. + protected override string GetOutputFileExtension(StreamState state) + { + var ext = base.GetOutputFileExtension(state); + + if (!string.IsNullOrEmpty(ext)) + { + return ext; + } + + var isVideoRequest = state.VideoRequest != null; + + // Try to infer based on the desired video codec + if (isVideoRequest) + { + var videoCodec = state.VideoRequest.VideoCodec; + + if (string.Equals(videoCodec, "h264", StringComparison.OrdinalIgnoreCase)) + { + return ".ts"; + } + if (string.Equals(videoCodec, "theora", StringComparison.OrdinalIgnoreCase)) + { + return ".ogv"; + } + if (string.Equals(videoCodec, "vpx", StringComparison.OrdinalIgnoreCase)) + { + return ".webm"; + } + if (string.Equals(videoCodec, "wmv", StringComparison.OrdinalIgnoreCase)) + { + return ".asf"; + } + } + + // Try to infer based on the desired audio codec + if (!isVideoRequest) + { + var audioCodec = state.Request.AudioCodec; + + if (string.Equals("aac", audioCodec, StringComparison.OrdinalIgnoreCase)) + { + return ".aac"; + } + if (string.Equals("mp3", audioCodec, StringComparison.OrdinalIgnoreCase)) + { + return ".mp3"; + } + if (string.Equals("vorbis", audioCodec, StringComparison.OrdinalIgnoreCase)) + { + return ".ogg"; + } + if (string.Equals("wma", audioCodec, StringComparison.OrdinalIgnoreCase)) + { + return ".wma"; + } + } + + return null; + } + + /// + /// Gets the type of the transcoding job. + /// + /// The type of the transcoding job. + protected override TranscodingJobType TranscodingJobType + { + get { return TranscodingJobType.Progressive; } + } + + /// + /// Processes the request. + /// + /// The request. + /// if set to true [is head request]. + /// Task. + protected async Task ProcessRequest(StreamRequest request, bool isHeadRequest) + { + var cancellationTokenSource = new CancellationTokenSource(); + + var state = await GetState(request, cancellationTokenSource.Token).ConfigureAwait(false); + + var responseHeaders = new Dictionary(); + + if (request.Static && state.DirectStreamProvider != null) + { + AddDlnaHeaders(state, responseHeaders, true); + + using (state) + { + var outputHeaders = new Dictionary(StringComparer.OrdinalIgnoreCase); + + // TODO: Don't hardcode this + outputHeaders["Content-Type"] = MediaBrowser.Model.Net.MimeTypes.GetMimeType("file.ts"); + + return new ProgressiveFileCopier(state.DirectStreamProvider, outputHeaders, null, Logger, EnvironmentInfo, CancellationToken.None) + { + AllowEndOfFile = false + }; + } + } + + // Static remote stream + if (request.Static && state.InputProtocol == MediaProtocol.Http) + { + AddDlnaHeaders(state, responseHeaders, true); + + using (state) + { + return await GetStaticRemoteStreamResult(state, responseHeaders, isHeadRequest, cancellationTokenSource).ConfigureAwait(false); + } + } + + if (request.Static && state.InputProtocol != MediaProtocol.File) + { + throw new ArgumentException(string.Format("Input protocol {0} cannot be streamed statically.", state.InputProtocol)); + } + + var outputPath = state.OutputFilePath; + var outputPathExists = FileSystem.FileExists(outputPath); + + var transcodingJob = ApiEntryPoint.Instance.GetTranscodingJob(outputPath, TranscodingJobType.Progressive); + var isTranscodeCached = outputPathExists && transcodingJob != null; + + AddDlnaHeaders(state, responseHeaders, request.Static || isTranscodeCached); + + // Static stream + if (request.Static) + { + var contentType = state.GetMimeType("." + state.OutputContainer, false) ?? state.GetMimeType(state.MediaPath); + + using (state) + { + if (state.MediaSource.IsInfiniteStream) + { + var outputHeaders = new Dictionary(StringComparer.OrdinalIgnoreCase); + + outputHeaders["Content-Type"] = contentType; + + return new ProgressiveFileCopier(FileSystem, state.MediaPath, outputHeaders, null, Logger, EnvironmentInfo, CancellationToken.None) + { + AllowEndOfFile = false + }; + } + + TimeSpan? cacheDuration = null; + + if (!string.IsNullOrEmpty(request.Tag)) + { + cacheDuration = TimeSpan.FromDays(365); + } + + return await ResultFactory.GetStaticFileResult(Request, new StaticFileResultOptions + { + ResponseHeaders = responseHeaders, + ContentType = contentType, + IsHeadRequest = isHeadRequest, + Path = state.MediaPath, + CacheDuration = cacheDuration + + }).ConfigureAwait(false); + } + } + + //// Not static but transcode cache file exists + //if (isTranscodeCached && state.VideoRequest == null) + //{ + // var contentType = state.GetMimeType(outputPath); + + // try + // { + // if (transcodingJob != null) + // { + // ApiEntryPoint.Instance.OnTranscodeBeginRequest(transcodingJob); + // } + + // return await ResultFactory.GetStaticFileResult(Request, new StaticFileResultOptions + // { + // ResponseHeaders = responseHeaders, + // ContentType = contentType, + // IsHeadRequest = isHeadRequest, + // Path = outputPath, + // FileShare = FileShareMode.ReadWrite, + // OnComplete = () => + // { + // if (transcodingJob != null) + // { + // ApiEntryPoint.Instance.OnTranscodeEndRequest(transcodingJob); + // } + // } + + // }).ConfigureAwait(false); + // } + // finally + // { + // state.Dispose(); + // } + //} + + // Need to start ffmpeg + try + { + return await GetStreamResult(request, state, responseHeaders, isHeadRequest, cancellationTokenSource).ConfigureAwait(false); + } + catch + { + state.Dispose(); + + throw; + } + } + + /// + /// Gets the static remote stream result. + /// + /// The state. + /// The response headers. + /// if set to true [is head request]. + /// The cancellation token source. + /// Task{System.Object}. + private async Task GetStaticRemoteStreamResult(StreamState state, Dictionary responseHeaders, bool isHeadRequest, CancellationTokenSource cancellationTokenSource) + { + string useragent = null; + state.RemoteHttpHeaders.TryGetValue("User-Agent", out useragent); + + var trySupportSeek = false; + + var options = new HttpRequestOptions + { + Url = state.MediaPath, + UserAgent = useragent, + BufferContent = false, + CancellationToken = cancellationTokenSource.Token + }; + + if (trySupportSeek) + { + if (!string.IsNullOrWhiteSpace(Request.QueryString["Range"])) + { + options.RequestHeaders["Range"] = Request.QueryString["Range"]; + } + } + var response = await HttpClient.GetResponse(options).ConfigureAwait(false); + + if (trySupportSeek) + { + foreach (var name in new[] { "Content-Range", "Accept-Ranges" }) + { + var val = response.Headers[name]; + if (!string.IsNullOrWhiteSpace(val)) + { + responseHeaders[name] = val; + } + } + } + else + { + responseHeaders["Accept-Ranges"] = "none"; + } + + // Seeing cases of -1 here + if (response.ContentLength.HasValue && response.ContentLength.Value >= 0) + { + responseHeaders["Content-Length"] = response.ContentLength.Value.ToString(UsCulture); + } + + if (isHeadRequest) + { + using (response) + { + return ResultFactory.GetResult(null, new byte[] { }, response.ContentType, responseHeaders); + } + } + + var result = new StaticRemoteStreamWriter(response); + + result.Headers["Content-Type"] = response.ContentType; + + // Add the response headers to the result object + foreach (var header in responseHeaders) + { + result.Headers[header.Key] = header.Value; + } + + return result; + } + + /// + /// Gets the stream result. + /// + /// The state. + /// The response headers. + /// if set to true [is head request]. + /// The cancellation token source. + /// Task{System.Object}. + private async Task GetStreamResult(StreamRequest request, StreamState state, IDictionary responseHeaders, bool isHeadRequest, CancellationTokenSource cancellationTokenSource) + { + // Use the command line args with a dummy playlist path + var outputPath = state.OutputFilePath; + + responseHeaders["Accept-Ranges"] = "none"; + + var contentType = state.GetMimeType(outputPath); + + // TODO: The isHeadRequest is only here because ServiceStack will add Content-Length=0 to the response + // What we really want to do is hunt that down and remove that + var contentLength = state.EstimateContentLength || isHeadRequest ? GetEstimatedContentLength(state) : null; + + if (contentLength.HasValue) + { + responseHeaders["Content-Length"] = contentLength.Value.ToString(UsCulture); + } + + // Headers only + if (isHeadRequest) + { + var streamResult = ResultFactory.GetResult(null, new byte[] { }, contentType, responseHeaders); + + var hasHeaders = streamResult as IHasHeaders; + if (hasHeaders != null) + { + if (contentLength.HasValue) + { + hasHeaders.Headers["Content-Length"] = contentLength.Value.ToString(CultureInfo.InvariantCulture); + } + else + { + if (hasHeaders.Headers.ContainsKey("Content-Length")) + { + hasHeaders.Headers.Remove("Content-Length"); + } + } + } + + return streamResult; + } + + var transcodingLock = ApiEntryPoint.Instance.GetTranscodingLock(outputPath); + await transcodingLock.WaitAsync(cancellationTokenSource.Token).ConfigureAwait(false); + try + { + TranscodingJob job; + + if (!FileSystem.FileExists(outputPath)) + { + job = await StartFfMpeg(state, outputPath, cancellationTokenSource).ConfigureAwait(false); + } + else + { + job = ApiEntryPoint.Instance.OnTranscodeBeginRequest(outputPath, TranscodingJobType.Progressive); + state.Dispose(); + } + + var outputHeaders = new Dictionary(StringComparer.OrdinalIgnoreCase); + + outputHeaders["Content-Type"] = contentType; + + // Add the response headers to the result object + foreach (var item in responseHeaders) + { + outputHeaders[item.Key] = item.Value; + } + + return new ProgressiveFileCopier(FileSystem, outputPath, outputHeaders, job, Logger, EnvironmentInfo, CancellationToken.None); + } + finally + { + transcodingLock.Release(); + } + } + + /// + /// Gets the length of the estimated content. + /// + /// The state. + /// System.Nullable{System.Int64}. + private long? GetEstimatedContentLength(StreamState state) + { + var totalBitrate = state.TotalOutputBitrate ?? 0; + + if (totalBitrate > 0 && state.RunTimeTicks.HasValue) + { + return Convert.ToInt64(totalBitrate * TimeSpan.FromTicks(state.RunTimeTicks.Value).TotalSeconds / 8); + } + + return null; + } + } +} diff --git a/MediaBrowser.Api/Playback/Progressive/ProgressiveStreamWriter.cs b/MediaBrowser.Api/Playback/Progressive/ProgressiveStreamWriter.cs new file mode 100644 index 0000000000..9839a7a9aa --- /dev/null +++ b/MediaBrowser.Api/Playback/Progressive/ProgressiveStreamWriter.cs @@ -0,0 +1,193 @@ +using MediaBrowser.Model.Logging; +using System; +using System.IO; +using System.Threading; +using System.Threading.Tasks; +using MediaBrowser.Model.IO; +using MediaBrowser.Controller.Net; +using System.Collections.Generic; + +using MediaBrowser.Controller.IO; +using MediaBrowser.Controller.Library; +using MediaBrowser.Model.Services; +using MediaBrowser.Model.System; + +namespace MediaBrowser.Api.Playback.Progressive +{ + public class ProgressiveFileCopier : IAsyncStreamWriter, IHasHeaders + { + private readonly IFileSystem _fileSystem; + private readonly TranscodingJob _job; + private readonly ILogger _logger; + private readonly string _path; + private readonly CancellationToken _cancellationToken; + private readonly Dictionary _outputHeaders; + + const int StreamCopyToBufferSize = 81920; + + private long _bytesWritten = 0; + public long StartPosition { get; set; } + public bool AllowEndOfFile = true; + + private readonly IDirectStreamProvider _directStreamProvider; + private readonly IEnvironmentInfo _environment; + + public ProgressiveFileCopier(IFileSystem fileSystem, string path, Dictionary outputHeaders, TranscodingJob job, ILogger logger, IEnvironmentInfo environment, CancellationToken cancellationToken) + { + _fileSystem = fileSystem; + _path = path; + _outputHeaders = outputHeaders; + _job = job; + _logger = logger; + _cancellationToken = cancellationToken; + _environment = environment; + } + + public ProgressiveFileCopier(IDirectStreamProvider directStreamProvider, Dictionary outputHeaders, TranscodingJob job, ILogger logger, IEnvironmentInfo environment, CancellationToken cancellationToken) + { + _directStreamProvider = directStreamProvider; + _outputHeaders = outputHeaders; + _job = job; + _logger = logger; + _cancellationToken = cancellationToken; + _environment = environment; + } + + public IDictionary Headers + { + get + { + return _outputHeaders; + } + } + + private Stream GetInputStream(bool allowAsyncFileRead) + { + var fileOpenOptions = FileOpenOptions.SequentialScan; + + if (allowAsyncFileRead) + { + fileOpenOptions |= FileOpenOptions.Asynchronous; + } + + return _fileSystem.GetFileStream(_path, FileOpenMode.Open, FileAccessMode.Read, FileShareMode.ReadWrite, fileOpenOptions); + } + + public async Task WriteToAsync(Stream outputStream, CancellationToken cancellationToken) + { + cancellationToken = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, _cancellationToken).Token; + + try + { + if (_directStreamProvider != null) + { + await _directStreamProvider.CopyToAsync(outputStream, cancellationToken).ConfigureAwait(false); + return; + } + + var eofCount = 0; + + // use non-async filestream along with read due to https://github.com/dotnet/corefx/issues/6039 + var allowAsyncFileRead = _environment.OperatingSystem != Model.System.OperatingSystem.Windows; + + using (var inputStream = GetInputStream(allowAsyncFileRead)) + { + if (StartPosition > 0) + { + inputStream.Position = StartPosition; + } + + while (eofCount < 20 || !AllowEndOfFile) + { + int bytesRead; + if (allowAsyncFileRead) + { + bytesRead = await CopyToInternalAsync(inputStream, outputStream, cancellationToken).ConfigureAwait(false); + } + else + { + bytesRead = await CopyToInternalAsyncWithSyncRead(inputStream, outputStream, cancellationToken).ConfigureAwait(false); + } + + //var position = fs.Position; + //_logger.Debug("Streamed {0} bytes to position {1} from file {2}", bytesRead, position, path); + + if (bytesRead == 0) + { + if (_job == null || _job.HasExited) + { + eofCount++; + } + await Task.Delay(100, cancellationToken).ConfigureAwait(false); + } + else + { + eofCount = 0; + } + } + } + } + finally + { + if (_job != null) + { + ApiEntryPoint.Instance.OnTranscodeEndRequest(_job); + } + } + } + + private async Task CopyToInternalAsyncWithSyncRead(Stream source, Stream destination, CancellationToken cancellationToken) + { + var array = new byte[StreamCopyToBufferSize]; + int bytesRead; + int totalBytesRead = 0; + + while ((bytesRead = source.Read(array, 0, array.Length)) != 0) + { + var bytesToWrite = bytesRead; + + if (bytesToWrite > 0) + { + await destination.WriteAsync(array, 0, Convert.ToInt32(bytesToWrite), cancellationToken).ConfigureAwait(false); + + _bytesWritten += bytesRead; + totalBytesRead += bytesRead; + + if (_job != null) + { + _job.BytesDownloaded = Math.Max(_job.BytesDownloaded ?? _bytesWritten, _bytesWritten); + } + } + } + + return totalBytesRead; + } + + private async Task CopyToInternalAsync(Stream source, Stream destination, CancellationToken cancellationToken) + { + var array = new byte[StreamCopyToBufferSize]; + int bytesRead; + int totalBytesRead = 0; + + while ((bytesRead = await source.ReadAsync(array, 0, array.Length, cancellationToken).ConfigureAwait(false)) != 0) + { + var bytesToWrite = bytesRead; + + if (bytesToWrite > 0) + { + await destination.WriteAsync(array, 0, Convert.ToInt32(bytesToWrite), cancellationToken).ConfigureAwait(false); + + _bytesWritten += bytesRead; + totalBytesRead += bytesRead; + + if (_job != null) + { + _job.BytesDownloaded = Math.Max(_job.BytesDownloaded ?? _bytesWritten, _bytesWritten); + } + } + } + + return totalBytesRead; + } + } +} diff --git a/MediaBrowser.Api/Playback/Progressive/VideoService.cs b/MediaBrowser.Api/Playback/Progressive/VideoService.cs new file mode 100644 index 0000000000..a41b4cbf54 --- /dev/null +++ b/MediaBrowser.Api/Playback/Progressive/VideoService.cs @@ -0,0 +1,100 @@ +using MediaBrowser.Controller.Configuration; +using MediaBrowser.Controller.Devices; +using MediaBrowser.Controller.Dlna; +using MediaBrowser.Controller.Drawing; +using MediaBrowser.Controller.Library; +using MediaBrowser.Controller.MediaEncoding; +using MediaBrowser.Model.IO; +using MediaBrowser.Model.Serialization; +using System.Threading.Tasks; +using MediaBrowser.Controller.Net; +using MediaBrowser.Model.Configuration; +using MediaBrowser.Model.Services; +using MediaBrowser.Model.System; + +namespace MediaBrowser.Api.Playback.Progressive +{ + /// + /// Class GetVideoStream + /// + [Route("/Videos/{Id}/stream.mpegts", "GET")] + [Route("/Videos/{Id}/stream.ts", "GET")] + [Route("/Videos/{Id}/stream.webm", "GET")] + [Route("/Videos/{Id}/stream.asf", "GET")] + [Route("/Videos/{Id}/stream.wmv", "GET")] + [Route("/Videos/{Id}/stream.ogv", "GET")] + [Route("/Videos/{Id}/stream.mp4", "GET")] + [Route("/Videos/{Id}/stream.m4v", "GET")] + [Route("/Videos/{Id}/stream.mkv", "GET")] + [Route("/Videos/{Id}/stream.mpeg", "GET")] + [Route("/Videos/{Id}/stream.mpg", "GET")] + [Route("/Videos/{Id}/stream.avi", "GET")] + [Route("/Videos/{Id}/stream.m2ts", "GET")] + [Route("/Videos/{Id}/stream.3gp", "GET")] + [Route("/Videos/{Id}/stream.wmv", "GET")] + [Route("/Videos/{Id}/stream.wtv", "GET")] + [Route("/Videos/{Id}/stream.mov", "GET")] + [Route("/Videos/{Id}/stream.iso", "GET")] + [Route("/Videos/{Id}/stream.flv", "GET")] + [Route("/Videos/{Id}/stream", "GET")] + [Route("/Videos/{Id}/stream.ts", "HEAD")] + [Route("/Videos/{Id}/stream.webm", "HEAD")] + [Route("/Videos/{Id}/stream.asf", "HEAD")] + [Route("/Videos/{Id}/stream.wmv", "HEAD")] + [Route("/Videos/{Id}/stream.ogv", "HEAD")] + [Route("/Videos/{Id}/stream.mp4", "HEAD")] + [Route("/Videos/{Id}/stream.m4v", "HEAD")] + [Route("/Videos/{Id}/stream.mkv", "HEAD")] + [Route("/Videos/{Id}/stream.mpeg", "HEAD")] + [Route("/Videos/{Id}/stream.mpg", "HEAD")] + [Route("/Videos/{Id}/stream.avi", "HEAD")] + [Route("/Videos/{Id}/stream.3gp", "HEAD")] + [Route("/Videos/{Id}/stream.wmv", "HEAD")] + [Route("/Videos/{Id}/stream.wtv", "HEAD")] + [Route("/Videos/{Id}/stream.m2ts", "HEAD")] + [Route("/Videos/{Id}/stream.mov", "HEAD")] + [Route("/Videos/{Id}/stream.iso", "HEAD")] + [Route("/Videos/{Id}/stream.flv", "HEAD")] + [Route("/Videos/{Id}/stream", "HEAD")] + public class GetVideoStream : VideoStreamRequest + { + + } + + /// + /// Class VideoService + /// + // TODO: In order to autheneticate this in the future, Dlna playback will require updating + //[Authenticated] + public class VideoService : BaseProgressiveStreamingService + { + public VideoService(IServerConfigurationManager serverConfig, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager, IMediaEncoder mediaEncoder, IFileSystem fileSystem, IDlnaManager dlnaManager, ISubtitleEncoder subtitleEncoder, IDeviceManager deviceManager, IMediaSourceManager mediaSourceManager, IZipClient zipClient, IJsonSerializer jsonSerializer, IAuthorizationContext authorizationContext, IImageProcessor imageProcessor, IEnvironmentInfo environmentInfo) : base(serverConfig, userManager, libraryManager, isoManager, mediaEncoder, fileSystem, dlnaManager, subtitleEncoder, deviceManager, mediaSourceManager, zipClient, jsonSerializer, authorizationContext, imageProcessor, environmentInfo) + { + } + + /// + /// Gets the specified request. + /// + /// The request. + /// System.Object. + public Task Get(GetVideoStream request) + { + return ProcessRequest(request, false); + } + + /// + /// Heads the specified request. + /// + /// The request. + /// System.Object. + public Task Head(GetVideoStream request) + { + return ProcessRequest(request, true); + } + + protected override string GetCommandLineArguments(string outputPath, EncodingOptions encodingOptions, StreamState state, bool isEncoding) + { + return EncodingHelper.GetProgressiveVideoFullCommandLine(state, encodingOptions, outputPath, GetDefaultH264Preset()); + } + } +} \ No newline at end of file diff --git a/MediaBrowser.Api/Playback/StaticRemoteStreamWriter.cs b/MediaBrowser.Api/Playback/StaticRemoteStreamWriter.cs new file mode 100644 index 0000000000..6bb3b6b804 --- /dev/null +++ b/MediaBrowser.Api/Playback/StaticRemoteStreamWriter.cs @@ -0,0 +1,47 @@ +using MediaBrowser.Common.Net; +using System.Collections.Generic; +using System.IO; +using System.Threading; +using System.Threading.Tasks; +using MediaBrowser.Model.Services; + +namespace MediaBrowser.Api.Playback +{ + /// + /// Class StaticRemoteStreamWriter + /// + public class StaticRemoteStreamWriter : IAsyncStreamWriter, IHasHeaders + { + /// + /// The _input stream + /// + private readonly HttpResponseInfo _response; + + /// + /// The _options + /// + private readonly IDictionary _options = new Dictionary(); + + public StaticRemoteStreamWriter(HttpResponseInfo response) + { + _response = response; + } + + /// + /// Gets the options. + /// + /// The options. + public IDictionary Headers + { + get { return _options; } + } + + public async Task WriteToAsync(Stream responseStream, CancellationToken cancellationToken) + { + using (_response) + { + await _response.Content.CopyToAsync(responseStream, 81920, cancellationToken).ConfigureAwait(false); + } + } + } +} diff --git a/MediaBrowser.Api/Playback/StreamRequest.cs b/MediaBrowser.Api/Playback/StreamRequest.cs new file mode 100644 index 0000000000..d95c30d657 --- /dev/null +++ b/MediaBrowser.Api/Playback/StreamRequest.cs @@ -0,0 +1,64 @@ +using System; +using MediaBrowser.Controller.MediaEncoding; +using MediaBrowser.Model.Dlna; +using MediaBrowser.Model.Services; + +namespace MediaBrowser.Api.Playback +{ + /// + /// Class StreamRequest + /// + public class StreamRequest : BaseEncodingJobOptions + { + /// + /// Gets or sets the id. + /// + /// The id. + [ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")] + public Guid Id { get; set; } + + [ApiMember(Name = "MediaSourceId", Description = "The media version id, if playing an alternate version", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")] + public string MediaSourceId { get; set; } + + [ApiMember(Name = "DeviceId", Description = "The device id of the client requesting. Used to stop encoding processes when needed.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")] + public string DeviceId { get; set; } + + [ApiMember(Name = "Container", Description = "Container", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")] + public string Container { get; set; } + + /// + /// Gets or sets the audio codec. + /// + /// The audio codec. + [ApiMember(Name = "AudioCodec", Description = "Optional. Specify a audio codec to encode to, e.g. mp3. If omitted the server will auto-select using the url's extension. Options: aac, mp3, vorbis, wma.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")] + public string AudioCodec { get; set; } + + [ApiMember(Name = "DeviceProfileId", Description = "Optional. The dlna device profile id to utilize.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")] + public string DeviceProfileId { get; set; } + + public string Params { get; set; } + public string PlaySessionId { get; set; } + public string Tag { get; set; } + public string SegmentContainer { get; set; } + + public int? SegmentLength { get; set; } + public int? MinSegments { get; set; } + } + + public class VideoStreamRequest : StreamRequest + { + /// + /// Gets a value indicating whether this instance has fixed resolution. + /// + /// true if this instance has fixed resolution; otherwise, false. + public bool HasFixedResolution + { + get + { + return Width.HasValue || Height.HasValue; + } + } + + public bool EnableSubtitlesInManifest { get; set; } + } +} diff --git a/MediaBrowser.Api/Playback/StreamState.cs b/MediaBrowser.Api/Playback/StreamState.cs new file mode 100644 index 0000000000..ac20b5e337 --- /dev/null +++ b/MediaBrowser.Api/Playback/StreamState.cs @@ -0,0 +1,259 @@ +using MediaBrowser.Controller.Library; +using MediaBrowser.Model.Dlna; +using MediaBrowser.Model.Drawing; +using MediaBrowser.Model.Dto; +using MediaBrowser.Model.Entities; +using MediaBrowser.Model.IO; +using MediaBrowser.Model.Logging; +using MediaBrowser.Model.MediaInfo; +using MediaBrowser.Model.Net; +using System; +using System.Collections.Generic; +using System.Globalization; +using System.IO; +using System.Linq; +using System.Threading; +using MediaBrowser.Controller.MediaEncoding; + +namespace MediaBrowser.Api.Playback +{ + public class StreamState : EncodingJobInfo, IDisposable + { + private readonly ILogger _logger; + private readonly IMediaSourceManager _mediaSourceManager; + + public string RequestedUrl { get; set; } + + public StreamRequest Request + { + get { return (StreamRequest)BaseRequest; } + set + { + BaseRequest = value; + + IsVideoRequest = VideoRequest != null; + } + } + + public TranscodingThrottler TranscodingThrottler { get; set; } + + public VideoStreamRequest VideoRequest + { + get { return Request as VideoStreamRequest; } + } + + /// + /// Gets or sets the log file stream. + /// + /// The log file stream. + public Stream LogFileStream { get; set; } + public IDirectStreamProvider DirectStreamProvider { get; set; } + + public string WaitForPath { get; set; } + + public bool IsOutputVideo + { + get { return Request is VideoStreamRequest; } + } + + public int SegmentLength + { + get + { + if (Request.SegmentLength.HasValue) + { + return Request.SegmentLength.Value; + } + + if (string.Equals(OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase)) + { + var userAgent = UserAgent ?? string.Empty; + + if (userAgent.IndexOf("AppleTV", StringComparison.OrdinalIgnoreCase) != -1 || + userAgent.IndexOf("cfnetwork", StringComparison.OrdinalIgnoreCase) != -1 || + userAgent.IndexOf("ipad", StringComparison.OrdinalIgnoreCase) != -1 || + userAgent.IndexOf("iphone", StringComparison.OrdinalIgnoreCase) != -1 || + userAgent.IndexOf("ipod", StringComparison.OrdinalIgnoreCase) != -1) + { + if (IsSegmentedLiveStream) + { + return 6; + } + + return 6; + } + + if (IsSegmentedLiveStream) + { + return 3; + } + return 6; + } + + return 3; + } + } + + public int MinSegments + { + get + { + if (Request.MinSegments.HasValue) + { + return Request.MinSegments.Value; + } + + return SegmentLength >= 10 ? 2 : 3; + } + } + + public int HlsListSize + { + get + { + return 0; + } + } + + public string UserAgent { get; set; } + + public StreamState(IMediaSourceManager mediaSourceManager, ILogger logger, TranscodingJobType transcodingType) + : base(logger, mediaSourceManager, transcodingType) + { + _mediaSourceManager = mediaSourceManager; + _logger = logger; + } + + public string MimeType { get; set; } + + public bool EstimateContentLength { get; set; } + public TranscodeSeekInfo TranscodeSeekInfo { get; set; } + + public long? EncodingDurationTicks { get; set; } + + public string GetMimeType(string outputPath, bool enableStreamDefault = true) + { + if (!string.IsNullOrEmpty(MimeType)) + { + return MimeType; + } + + return MimeTypes.GetMimeType(outputPath, enableStreamDefault); + } + + public bool EnableDlnaHeaders { get; set; } + + public void Dispose() + { + DisposeTranscodingThrottler(); + DisposeLiveStream(); + DisposeLogStream(); + DisposeIsoMount(); + + TranscodingJob = null; + } + + private void DisposeLogStream() + { + if (LogFileStream != null) + { + try + { + LogFileStream.Dispose(); + } + catch (Exception ex) + { + _logger.ErrorException("Error disposing log stream", ex); + } + + LogFileStream = null; + } + } + + private void DisposeTranscodingThrottler() + { + if (TranscodingThrottler != null) + { + try + { + TranscodingThrottler.Dispose(); + } + catch (Exception ex) + { + _logger.ErrorException("Error disposing TranscodingThrottler", ex); + } + + TranscodingThrottler = null; + } + } + + private async void DisposeLiveStream() + { + if (MediaSource.RequiresClosing && string.IsNullOrWhiteSpace(Request.LiveStreamId) && !string.IsNullOrWhiteSpace(MediaSource.LiveStreamId)) + { + try + { + await _mediaSourceManager.CloseLiveStream(MediaSource.LiveStreamId).ConfigureAwait(false); + } + catch (Exception ex) + { + _logger.ErrorException("Error closing media source", ex); + } + } + } + + public string OutputFilePath { get; set; } + + public string ActualOutputVideoCodec + { + get + { + var codec = OutputVideoCodec; + + if (string.Equals(codec, "copy", StringComparison.OrdinalIgnoreCase)) + { + var stream = VideoStream; + + if (stream != null) + { + return stream.Codec; + } + + return null; + } + + return codec; + } + } + + public string ActualOutputAudioCodec + { + get + { + var codec = OutputAudioCodec; + + if (string.Equals(codec, "copy", StringComparison.OrdinalIgnoreCase)) + { + var stream = AudioStream; + + if (stream != null) + { + return stream.Codec; + } + + return null; + } + + return codec; + } + } + + public DeviceProfile DeviceProfile { get; set; } + + public TranscodingJob TranscodingJob; + public override void ReportTranscodingProgress(TimeSpan? transcodingPosition, float? framerate, double? percentComplete, long? bytesTranscoded, int? bitRate) + { + ApiEntryPoint.Instance.ReportTranscodingProgress(TranscodingJob, this, transcodingPosition, framerate, percentComplete, bytesTranscoded, bitRate); + } + } +} diff --git a/MediaBrowser.Api/Playback/TranscodingThrottler.cs b/MediaBrowser.Api/Playback/TranscodingThrottler.cs new file mode 100644 index 0000000000..c42d0c3e4c --- /dev/null +++ b/MediaBrowser.Api/Playback/TranscodingThrottler.cs @@ -0,0 +1,176 @@ +using MediaBrowser.Common.Configuration; +using MediaBrowser.Model.Configuration; +using MediaBrowser.Model.Logging; +using System; +using MediaBrowser.Model.IO; +using MediaBrowser.Model.Threading; + +namespace MediaBrowser.Api.Playback +{ + public class TranscodingThrottler : IDisposable + { + private readonly TranscodingJob _job; + private readonly ILogger _logger; + private ITimer _timer; + private bool _isPaused; + private readonly IConfigurationManager _config; + private readonly ITimerFactory _timerFactory; + private readonly IFileSystem _fileSystem; + + public TranscodingThrottler(TranscodingJob job, ILogger logger, IConfigurationManager config, ITimerFactory timerFactory, IFileSystem fileSystem) + { + _job = job; + _logger = logger; + _config = config; + _timerFactory = timerFactory; + _fileSystem = fileSystem; + } + + private EncodingOptions GetOptions() + { + return _config.GetConfiguration("encoding"); + } + + public void Start() + { + _timer = _timerFactory.Create(TimerCallback, null, 5000, 5000); + } + + private void TimerCallback(object state) + { + if (_job.HasExited) + { + DisposeTimer(); + return; + } + + var options = GetOptions(); + + if (options.EnableThrottling && IsThrottleAllowed(_job, options.ThrottleDelaySeconds)) + { + PauseTranscoding(); + } + else + { + UnpauseTranscoding(); + } + } + + private void PauseTranscoding() + { + if (!_isPaused) + { + _logger.Debug("Sending pause command to ffmpeg"); + + try + { + _job.Process.StandardInput.Write("c"); + _isPaused = true; + } + catch (Exception ex) + { + _logger.ErrorException("Error pausing transcoding", ex); + } + } + } + + public void UnpauseTranscoding() + { + if (_isPaused) + { + _logger.Debug("Sending unpause command to ffmpeg"); + + try + { + _job.Process.StandardInput.WriteLine(); + _isPaused = false; + } + catch (Exception ex) + { + _logger.ErrorException("Error unpausing transcoding", ex); + } + } + } + + private bool IsThrottleAllowed(TranscodingJob job, int thresholdSeconds) + { + var bytesDownloaded = job.BytesDownloaded ?? 0; + var transcodingPositionTicks = job.TranscodingPositionTicks ?? 0; + var downloadPositionTicks = job.DownloadPositionTicks ?? 0; + + var path = job.Path; + var gapLengthInTicks = TimeSpan.FromSeconds(thresholdSeconds).Ticks; + + if (downloadPositionTicks > 0 && transcodingPositionTicks > 0) + { + // HLS - time-based consideration + + var targetGap = gapLengthInTicks; + var gap = transcodingPositionTicks - downloadPositionTicks; + + if (gap < targetGap) + { + //_logger.Debug("Not throttling transcoder gap {0} target gap {1}", gap, targetGap); + return false; + } + + //_logger.Debug("Throttling transcoder gap {0} target gap {1}", gap, targetGap); + return true; + } + + if (bytesDownloaded > 0 && transcodingPositionTicks > 0) + { + // Progressive Streaming - byte-based consideration + + try + { + var bytesTranscoded = job.BytesTranscoded ?? _fileSystem.GetFileInfo(path).Length; + + // Estimate the bytes the transcoder should be ahead + double gapFactor = gapLengthInTicks; + gapFactor /= transcodingPositionTicks; + var targetGap = bytesTranscoded * gapFactor; + + var gap = bytesTranscoded - bytesDownloaded; + + if (gap < targetGap) + { + //_logger.Debug("Not throttling transcoder gap {0} target gap {1} bytes downloaded {2}", gap, targetGap, bytesDownloaded); + return false; + } + + //_logger.Debug("Throttling transcoder gap {0} target gap {1} bytes downloaded {2}", gap, targetGap, bytesDownloaded); + return true; + } + catch + { + //_logger.Error("Error getting output size"); + return false; + } + } + + //_logger.Debug("No throttle data for " + path); + return false; + } + + public void Stop() + { + DisposeTimer(); + UnpauseTranscoding(); + } + + public void Dispose() + { + DisposeTimer(); + } + + private void DisposeTimer() + { + if (_timer != null) + { + _timer.Dispose(); + _timer = null; + } + } + } +} diff --git a/MediaBrowser.Api/Playback/UniversalAudioService.cs b/MediaBrowser.Api/Playback/UniversalAudioService.cs new file mode 100644 index 0000000000..6f928000a6 --- /dev/null +++ b/MediaBrowser.Api/Playback/UniversalAudioService.cs @@ -0,0 +1,349 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Threading.Tasks; +using MediaBrowser.Api.Playback.Hls; +using MediaBrowser.Api.Playback.Progressive; +using MediaBrowser.Common.Net; +using MediaBrowser.Controller.Configuration; +using MediaBrowser.Controller.Devices; +using MediaBrowser.Controller.Dlna; +using MediaBrowser.Controller.Drawing; +using MediaBrowser.Controller.Library; +using MediaBrowser.Controller.MediaEncoding; +using MediaBrowser.Controller.Net; +using MediaBrowser.Model.Dlna; +using MediaBrowser.Model.IO; +using MediaBrowser.Model.MediaInfo; +using MediaBrowser.Model.Serialization; +using MediaBrowser.Model.Services; +using MediaBrowser.Model.System; + +namespace MediaBrowser.Api.Playback +{ + public class BaseUniversalRequest + { + /// + /// Gets or sets the id. + /// + /// The id. + [ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")] + public Guid Id { get; set; } + + [ApiMember(Name = "MediaSourceId", Description = "The media version id, if playing an alternate version", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")] + public string MediaSourceId { get; set; } + + [ApiMember(Name = "DeviceId", Description = "The device id of the client requesting. Used to stop encoding processes when needed.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")] + public string DeviceId { get; set; } + + public Guid UserId { get; set; } + public string AudioCodec { get; set; } + public string Container { get; set; } + + public int? MaxAudioChannels { get; set; } + public int? TranscodingAudioChannels { get; set; } + + public long? MaxStreamingBitrate { get; set; } + + [ApiMember(Name = "StartTimeTicks", Description = "Optional. Specify a starting offset, in ticks. 1 tick = 10000 ms", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")] + public long? StartTimeTicks { get; set; } + + public string TranscodingContainer { get; set; } + public string TranscodingProtocol { get; set; } + public int? MaxAudioSampleRate { get; set; } + public int? MaxAudioBitDepth { get; set; } + + public bool EnableRedirection { get; set; } + public bool EnableRemoteMedia { get; set; } + public bool BreakOnNonKeyFrames { get; set; } + + public BaseUniversalRequest() + { + EnableRedirection = true; + } + } + + [Route("/Audio/{Id}/universal.{Container}", "GET", Summary = "Gets an audio stream")] + [Route("/Audio/{Id}/universal", "GET", Summary = "Gets an audio stream")] + [Route("/Audio/{Id}/universal.{Container}", "HEAD", Summary = "Gets an audio stream")] + [Route("/Audio/{Id}/universal", "HEAD", Summary = "Gets an audio stream")] + public class GetUniversalAudioStream : BaseUniversalRequest + { + } + + [Authenticated] + public class UniversalAudioService : BaseApiService + { + public UniversalAudioService(IServerConfigurationManager serverConfigurationManager, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager, IMediaEncoder mediaEncoder, IFileSystem fileSystem, IDlnaManager dlnaManager, IDeviceManager deviceManager, ISubtitleEncoder subtitleEncoder, IMediaSourceManager mediaSourceManager, IZipClient zipClient, IJsonSerializer jsonSerializer, IAuthorizationContext authorizationContext, IImageProcessor imageProcessor, INetworkManager networkManager, IEnvironmentInfo environmentInfo) + { + ServerConfigurationManager = serverConfigurationManager; + UserManager = userManager; + LibraryManager = libraryManager; + IsoManager = isoManager; + MediaEncoder = mediaEncoder; + FileSystem = fileSystem; + DlnaManager = dlnaManager; + DeviceManager = deviceManager; + SubtitleEncoder = subtitleEncoder; + MediaSourceManager = mediaSourceManager; + ZipClient = zipClient; + JsonSerializer = jsonSerializer; + AuthorizationContext = authorizationContext; + ImageProcessor = imageProcessor; + NetworkManager = networkManager; + EnvironmentInfo = environmentInfo; + } + + protected IServerConfigurationManager ServerConfigurationManager { get; private set; } + protected IUserManager UserManager { get; private set; } + protected ILibraryManager LibraryManager { get; private set; } + protected IIsoManager IsoManager { get; private set; } + protected IMediaEncoder MediaEncoder { get; private set; } + protected IFileSystem FileSystem { get; private set; } + protected IDlnaManager DlnaManager { get; private set; } + protected IDeviceManager DeviceManager { get; private set; } + protected ISubtitleEncoder SubtitleEncoder { get; private set; } + protected IMediaSourceManager MediaSourceManager { get; private set; } + protected IZipClient ZipClient { get; private set; } + protected IJsonSerializer JsonSerializer { get; private set; } + protected IAuthorizationContext AuthorizationContext { get; private set; } + protected IImageProcessor ImageProcessor { get; private set; } + protected INetworkManager NetworkManager { get; private set; } + protected IEnvironmentInfo EnvironmentInfo { get; private set; } + + public Task Get(GetUniversalAudioStream request) + { + return GetUniversalStream(request, false); + } + + public Task Head(GetUniversalAudioStream request) + { + return GetUniversalStream(request, true); + } + + private DeviceProfile GetDeviceProfile(GetUniversalAudioStream request) + { + var deviceProfile = new DeviceProfile(); + + var directPlayProfiles = new List(); + + var containers = (request.Container ?? string.Empty).Split(new [] { ',' }, StringSplitOptions.RemoveEmptyEntries); + + foreach (var container in containers) + { + var parts = container.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries); + + var audioCodecs = parts.Length == 1 ? null : string.Join(",", parts.Skip(1).ToArray()); + + directPlayProfiles.Add(new DirectPlayProfile + { + Type = DlnaProfileType.Audio, + Container = parts[0], + AudioCodec = audioCodecs + }); + } + + deviceProfile.DirectPlayProfiles = directPlayProfiles.ToArray(); + + deviceProfile.TranscodingProfiles = new[] + { + new TranscodingProfile + { + Type = DlnaProfileType.Audio, + Context = EncodingContext.Streaming, + Container = request.TranscodingContainer, + AudioCodec = request.AudioCodec, + Protocol = request.TranscodingProtocol, + BreakOnNonKeyFrames = request.BreakOnNonKeyFrames, + MaxAudioChannels = request.TranscodingAudioChannels.HasValue ? request.TranscodingAudioChannels.Value.ToString(CultureInfo.InvariantCulture) : null + } + }; + + var codecProfiles = new List(); + var conditions = new List(); + + if (request.MaxAudioSampleRate.HasValue) + { + // codec profile + conditions.Add(new ProfileCondition + { + Condition = ProfileConditionType.LessThanEqual, + IsRequired = false, + Property = ProfileConditionValue.AudioSampleRate, + Value = request.MaxAudioSampleRate.Value.ToString(CultureInfo.InvariantCulture) + }); + } + + if (request.MaxAudioBitDepth.HasValue) + { + // codec profile + conditions.Add(new ProfileCondition + { + Condition = ProfileConditionType.LessThanEqual, + IsRequired = false, + Property = ProfileConditionValue.AudioBitDepth, + Value = request.MaxAudioBitDepth.Value.ToString(CultureInfo.InvariantCulture) + }); + } + + if (request.MaxAudioChannels.HasValue) + { + // codec profile + conditions.Add(new ProfileCondition + { + Condition = ProfileConditionType.LessThanEqual, + IsRequired = false, + Property = ProfileConditionValue.AudioChannels, + Value = request.MaxAudioChannels.Value.ToString(CultureInfo.InvariantCulture) + }); + } + + if (conditions.Count > 0) + { + // codec profile + codecProfiles.Add(new CodecProfile + { + Type = CodecType.Audio, + Container = request.Container, + Conditions = conditions.ToArray() + }); + } + + deviceProfile.CodecProfiles = codecProfiles.ToArray(); + + return deviceProfile; + } + + private async Task GetUniversalStream(GetUniversalAudioStream request, bool isHeadRequest) + { + var deviceProfile = GetDeviceProfile(request); + + AuthorizationContext.GetAuthorizationInfo(Request).DeviceId = request.DeviceId; + + var mediaInfoService = new MediaInfoService(MediaSourceManager, DeviceManager, LibraryManager, ServerConfigurationManager, NetworkManager, MediaEncoder, UserManager, JsonSerializer, AuthorizationContext) + { + Request = Request + }; + + var playbackInfoResult = await mediaInfoService.GetPlaybackInfo(new GetPostedPlaybackInfo + { + Id = request.Id, + MaxAudioChannels = request.MaxAudioChannels, + MaxStreamingBitrate = request.MaxStreamingBitrate, + StartTimeTicks = request.StartTimeTicks, + UserId = request.UserId, + DeviceProfile = deviceProfile, + MediaSourceId = request.MediaSourceId + + }).ConfigureAwait(false); + + var mediaSource = playbackInfoResult.MediaSources[0]; + + if (mediaSource.SupportsDirectPlay && mediaSource.Protocol == MediaProtocol.Http) + { + if (request.EnableRedirection) + { + if (mediaSource.IsRemote && request.EnableRemoteMedia) + { + return ResultFactory.GetRedirectResult(mediaSource.Path); + } + } + } + + var isStatic = mediaSource.SupportsDirectStream; + + if (!isStatic && string.Equals(mediaSource.TranscodingSubProtocol, "hls", StringComparison.OrdinalIgnoreCase)) + { + var service = new DynamicHlsService(ServerConfigurationManager, + UserManager, + LibraryManager, + IsoManager, + MediaEncoder, + FileSystem, + DlnaManager, + SubtitleEncoder, + DeviceManager, + MediaSourceManager, + ZipClient, + JsonSerializer, + AuthorizationContext, + NetworkManager) + { + Request = Request + }; + + var transcodingProfile = deviceProfile.TranscodingProfiles[0]; + + var newRequest = new GetMasterHlsAudioPlaylist + { + AudioBitRate = isStatic ? (int?)null : Convert.ToInt32(Math.Min(request.MaxStreamingBitrate ?? 192000, int.MaxValue)), + AudioCodec = transcodingProfile.AudioCodec, + Container = ".m3u8", + DeviceId = request.DeviceId, + Id = request.Id, + MaxAudioChannels = request.MaxAudioChannels, + MediaSourceId = mediaSource.Id, + PlaySessionId = playbackInfoResult.PlaySessionId, + StartTimeTicks = request.StartTimeTicks, + Static = isStatic, + SegmentContainer = request.TranscodingContainer, + AudioSampleRate = request.MaxAudioSampleRate, + MaxAudioBitDepth = request.MaxAudioBitDepth, + BreakOnNonKeyFrames = transcodingProfile.BreakOnNonKeyFrames, + TranscodeReasons = mediaSource.TranscodeReasons == null ? null : string.Join(",", mediaSource.TranscodeReasons.Select(i => i.ToString()).ToArray()) + }; + + if (isHeadRequest) + { + return await service.Head(newRequest).ConfigureAwait(false); + } + return await service.Get(newRequest).ConfigureAwait(false); + } + else + { + var service = new AudioService(ServerConfigurationManager, + UserManager, + LibraryManager, + IsoManager, + MediaEncoder, + FileSystem, + DlnaManager, + SubtitleEncoder, + DeviceManager, + MediaSourceManager, + ZipClient, + JsonSerializer, + AuthorizationContext, + ImageProcessor, + EnvironmentInfo) + { + Request = Request + }; + + var newRequest = new GetAudioStream + { + AudioBitRate = isStatic ? (int?)null : Convert.ToInt32(Math.Min(request.MaxStreamingBitrate ?? 192000, int.MaxValue)), + AudioCodec = request.AudioCodec, + Container = isStatic ? null : ("." + mediaSource.TranscodingContainer), + DeviceId = request.DeviceId, + Id = request.Id, + MaxAudioChannels = request.MaxAudioChannels, + MediaSourceId = mediaSource.Id, + PlaySessionId = playbackInfoResult.PlaySessionId, + StartTimeTicks = request.StartTimeTicks, + Static = isStatic, + AudioSampleRate = request.MaxAudioSampleRate, + MaxAudioBitDepth = request.MaxAudioBitDepth, + TranscodeReasons = mediaSource.TranscodeReasons == null ? null : string.Join(",", mediaSource.TranscodeReasons.Select(i => i.ToString()).ToArray()) + }; + + if (isHeadRequest) + { + return await service.Head(newRequest).ConfigureAwait(false); + } + return await service.Get(newRequest).ConfigureAwait(false); + } + } + } +} diff --git a/MediaBrowser.MediaEncoding/BdInfo/BdInfoExaminer.cs b/MediaBrowser.MediaEncoding/BdInfo/BdInfoExaminer.cs new file mode 100644 index 0000000000..557f4d6374 --- /dev/null +++ b/MediaBrowser.MediaEncoding/BdInfo/BdInfoExaminer.cs @@ -0,0 +1,201 @@ +using BDInfo; +using MediaBrowser.Model.Entities; +using MediaBrowser.Model.MediaInfo; +using System; +using System.Collections.Generic; +using System.Linq; +using MediaBrowser.Model.IO; +using MediaBrowser.Model.Text; + +namespace MediaBrowser.MediaEncoding.BdInfo +{ + /// + /// Class BdInfoExaminer + /// + public class BdInfoExaminer : IBlurayExaminer + { + private readonly IFileSystem _fileSystem; + private readonly ITextEncoding _textEncoding; + + public BdInfoExaminer(IFileSystem fileSystem, ITextEncoding textEncoding) + { + _fileSystem = fileSystem; + _textEncoding = textEncoding; + } + + /// + /// Gets the disc info. + /// + /// The path. + /// BlurayDiscInfo. + public BlurayDiscInfo GetDiscInfo(string path) + { + if (string.IsNullOrWhiteSpace(path)) + { + throw new ArgumentNullException("path"); + } + + var bdrom = new BDROM(path, _fileSystem, _textEncoding); + + bdrom.Scan(); + + // Get the longest playlist + var playlist = bdrom.PlaylistFiles.Values.OrderByDescending(p => p.TotalLength).FirstOrDefault(p => p.IsValid); + + var outputStream = new BlurayDiscInfo + { + MediaStreams = new MediaStream[] {} + }; + + if (playlist == null) + { + return outputStream; + } + + outputStream.Chapters = playlist.Chapters.ToArray(); + + outputStream.RunTimeTicks = TimeSpan.FromSeconds(playlist.TotalLength).Ticks; + + var mediaStreams = new List(); + + foreach (var stream in playlist.SortedStreams) + { + var videoStream = stream as TSVideoStream; + + if (videoStream != null) + { + AddVideoStream(mediaStreams, videoStream); + continue; + } + + var audioStream = stream as TSAudioStream; + + if (audioStream != null) + { + AddAudioStream(mediaStreams, audioStream); + continue; + } + + var textStream = stream as TSTextStream; + + if (textStream != null) + { + AddSubtitleStream(mediaStreams, textStream); + continue; + } + + var graphicsStream = stream as TSGraphicsStream; + + if (graphicsStream != null) + { + AddSubtitleStream(mediaStreams, graphicsStream); + } + } + + outputStream.MediaStreams = mediaStreams.ToArray(); + + outputStream.PlaylistName = playlist.Name; + + if (playlist.StreamClips != null && playlist.StreamClips.Any()) + { + // Get the files in the playlist + outputStream.Files = playlist.StreamClips.Select(i => i.StreamFile.Name).ToArray(); + } + + return outputStream; + } + + /// + /// Adds the video stream. + /// + /// The streams. + /// The video stream. + private void AddVideoStream(List streams, TSVideoStream videoStream) + { + var mediaStream = new MediaStream + { + BitRate = Convert.ToInt32(videoStream.BitRate), + Width = videoStream.Width, + Height = videoStream.Height, + Codec = videoStream.CodecShortName, + IsInterlaced = videoStream.IsInterlaced, + Type = MediaStreamType.Video, + Index = streams.Count + }; + + if (videoStream.FrameRateDenominator > 0) + { + float frameRateEnumerator = videoStream.FrameRateEnumerator; + float frameRateDenominator = videoStream.FrameRateDenominator; + + mediaStream.AverageFrameRate = mediaStream.RealFrameRate = frameRateEnumerator / frameRateDenominator; + } + + streams.Add(mediaStream); + } + + /// + /// Adds the audio stream. + /// + /// The streams. + /// The audio stream. + private void AddAudioStream(List streams, TSAudioStream audioStream) + { + var stream = new MediaStream + { + Codec = audioStream.CodecShortName, + Language = audioStream.LanguageCode, + Channels = audioStream.ChannelCount, + SampleRate = audioStream.SampleRate, + Type = MediaStreamType.Audio, + Index = streams.Count + }; + + var bitrate = Convert.ToInt32(audioStream.BitRate); + + if (bitrate > 0) + { + stream.BitRate = bitrate; + } + + if (audioStream.LFE > 0) + { + stream.Channels = audioStream.ChannelCount + 1; + } + + streams.Add(stream); + } + + /// + /// Adds the subtitle stream. + /// + /// The streams. + /// The text stream. + private void AddSubtitleStream(List streams, TSTextStream textStream) + { + streams.Add(new MediaStream + { + Language = textStream.LanguageCode, + Codec = textStream.CodecShortName, + Type = MediaStreamType.Subtitle, + Index = streams.Count + }); + } + + /// + /// Adds the subtitle stream. + /// + /// The streams. + /// The text stream. + private void AddSubtitleStream(List streams, TSGraphicsStream textStream) + { + streams.Add(new MediaStream + { + Language = textStream.LanguageCode, + Codec = textStream.CodecShortName, + Type = MediaStreamType.Subtitle, + Index = streams.Count + }); + } + } +} diff --git a/MediaBrowser.MediaEncoding/Configuration/EncodingConfigurationFactory.cs b/MediaBrowser.MediaEncoding/Configuration/EncodingConfigurationFactory.cs new file mode 100644 index 0000000000..16c67655ad --- /dev/null +++ b/MediaBrowser.MediaEncoding/Configuration/EncodingConfigurationFactory.cs @@ -0,0 +1,58 @@ +using MediaBrowser.Common.Configuration; +using MediaBrowser.Model.Configuration; +using System.Collections.Generic; +using System.IO; + +using MediaBrowser.Controller.IO; +using MediaBrowser.Model.IO; + +namespace MediaBrowser.MediaEncoding.Configuration +{ + public class EncodingConfigurationFactory : IConfigurationFactory + { + private readonly IFileSystem _fileSystem; + + public EncodingConfigurationFactory(IFileSystem fileSystem) + { + _fileSystem = fileSystem; + } + + public IEnumerable GetConfigurations() + { + return new[] + { + new EncodingConfigurationStore(_fileSystem) + }; + } + } + + public class EncodingConfigurationStore : ConfigurationStore, IValidatingConfiguration + { + private readonly IFileSystem _fileSystem; + + public EncodingConfigurationStore(IFileSystem fileSystem) + { + ConfigurationType = typeof(EncodingOptions); + Key = "encoding"; + _fileSystem = fileSystem; + } + + public void Validate(object oldConfig, object newConfig) + { + var oldEncodingConfig = (EncodingOptions)oldConfig; + var newEncodingConfig = (EncodingOptions)newConfig; + + var newPath = newEncodingConfig.TranscodingTempPath; + + if (!string.IsNullOrWhiteSpace(newPath) + && !string.Equals(oldEncodingConfig.TranscodingTempPath ?? string.Empty, newPath)) + { + // Validate + if (!_fileSystem.DirectoryExists(newPath)) + { + throw new FileNotFoundException(string.Format("{0} does not exist.", newPath)); + } + } + } + } +} diff --git a/MediaBrowser.MediaEncoding/Encoder/AudioEncoder.cs b/MediaBrowser.MediaEncoding/Encoder/AudioEncoder.cs new file mode 100644 index 0000000000..566e7946d3 --- /dev/null +++ b/MediaBrowser.MediaEncoding/Encoder/AudioEncoder.cs @@ -0,0 +1,62 @@ +using MediaBrowser.Controller.Configuration; +using MediaBrowser.Controller.Library; +using MediaBrowser.Controller.MediaEncoding; +using MediaBrowser.Controller.Session; +using MediaBrowser.Model.IO; +using MediaBrowser.Model.Logging; +using System; +using MediaBrowser.Model.Diagnostics; + +namespace MediaBrowser.MediaEncoding.Encoder +{ + public class AudioEncoder : BaseEncoder + { + public AudioEncoder(MediaEncoder mediaEncoder, ILogger logger, IServerConfigurationManager configurationManager, IFileSystem fileSystem, IIsoManager isoManager, ILibraryManager libraryManager, ISessionManager sessionManager, ISubtitleEncoder subtitleEncoder, IMediaSourceManager mediaSourceManager, IProcessFactory processFactory) : base(mediaEncoder, logger, configurationManager, fileSystem, isoManager, libraryManager, sessionManager, subtitleEncoder, mediaSourceManager, processFactory) + { + } + + protected override string GetCommandLineArguments(EncodingJob state) + { + var encodingOptions = GetEncodingOptions(); + + return EncodingHelper.GetProgressiveAudioFullCommandLine(state, encodingOptions, state.OutputFilePath); + } + + protected override string GetOutputFileExtension(EncodingJob state) + { + var ext = base.GetOutputFileExtension(state); + + if (!string.IsNullOrEmpty(ext)) + { + return ext; + } + + var audioCodec = state.Options.AudioCodec; + + if (string.Equals("aac", audioCodec, StringComparison.OrdinalIgnoreCase)) + { + return ".aac"; + } + if (string.Equals("mp3", audioCodec, StringComparison.OrdinalIgnoreCase)) + { + return ".mp3"; + } + if (string.Equals("vorbis", audioCodec, StringComparison.OrdinalIgnoreCase)) + { + return ".ogg"; + } + if (string.Equals("wma", audioCodec, StringComparison.OrdinalIgnoreCase)) + { + return ".wma"; + } + + return null; + } + + protected override bool IsVideoEncoder + { + get { return false; } + } + + } +} diff --git a/MediaBrowser.MediaEncoding/Encoder/BaseEncoder.cs b/MediaBrowser.MediaEncoding/Encoder/BaseEncoder.cs new file mode 100644 index 0000000000..3383276bcb --- /dev/null +++ b/MediaBrowser.MediaEncoding/Encoder/BaseEncoder.cs @@ -0,0 +1,375 @@ +using MediaBrowser.Common.Configuration; +using MediaBrowser.Controller.Configuration; +using MediaBrowser.Controller.Library; +using MediaBrowser.Controller.MediaEncoding; +using MediaBrowser.Controller.Session; +using MediaBrowser.Model.Configuration; +using MediaBrowser.Model.Dto; +using MediaBrowser.Model.Entities; +using MediaBrowser.Model.IO; +using MediaBrowser.Model.Logging; +using MediaBrowser.Model.MediaInfo; +using System; +using System.Collections.Generic; +using System.Globalization; +using System.IO; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using MediaBrowser.Model.Diagnostics; +using MediaBrowser.Model.Dlna; + +namespace MediaBrowser.MediaEncoding.Encoder +{ + public abstract class BaseEncoder + { + protected readonly MediaEncoder MediaEncoder; + protected readonly ILogger Logger; + protected readonly IServerConfigurationManager ConfigurationManager; + protected readonly IFileSystem FileSystem; + protected readonly IIsoManager IsoManager; + protected readonly ILibraryManager LibraryManager; + protected readonly ISessionManager SessionManager; + protected readonly ISubtitleEncoder SubtitleEncoder; + protected readonly IMediaSourceManager MediaSourceManager; + protected IProcessFactory ProcessFactory; + + protected readonly CultureInfo UsCulture = new CultureInfo("en-US"); + + protected EncodingHelper EncodingHelper; + + protected BaseEncoder(MediaEncoder mediaEncoder, + ILogger logger, + IServerConfigurationManager configurationManager, + IFileSystem fileSystem, + IIsoManager isoManager, + ILibraryManager libraryManager, + ISessionManager sessionManager, + ISubtitleEncoder subtitleEncoder, + IMediaSourceManager mediaSourceManager, IProcessFactory processFactory) + { + MediaEncoder = mediaEncoder; + Logger = logger; + ConfigurationManager = configurationManager; + FileSystem = fileSystem; + IsoManager = isoManager; + LibraryManager = libraryManager; + SessionManager = sessionManager; + SubtitleEncoder = subtitleEncoder; + MediaSourceManager = mediaSourceManager; + ProcessFactory = processFactory; + + EncodingHelper = new EncodingHelper(MediaEncoder, FileSystem, SubtitleEncoder); + } + + public async Task Start(EncodingJobOptions options, + IProgress progress, + CancellationToken cancellationToken) + { + var encodingJob = await new EncodingJobFactory(Logger, LibraryManager, MediaSourceManager, ConfigurationManager, MediaEncoder) + .CreateJob(options, EncodingHelper, IsVideoEncoder, progress, cancellationToken).ConfigureAwait(false); + + encodingJob.OutputFilePath = GetOutputFilePath(encodingJob); + FileSystem.CreateDirectory(FileSystem.GetDirectoryName(encodingJob.OutputFilePath)); + + encodingJob.ReadInputAtNativeFramerate = options.ReadInputAtNativeFramerate; + + await AcquireResources(encodingJob, cancellationToken).ConfigureAwait(false); + + var commandLineArgs = GetCommandLineArguments(encodingJob); + + var process = ProcessFactory.Create(new ProcessOptions + { + CreateNoWindow = true, + UseShellExecute = false, + + // Must consume both stdout and stderr or deadlocks may occur + //RedirectStandardOutput = true, + RedirectStandardError = true, + RedirectStandardInput = true, + + FileName = MediaEncoder.EncoderPath, + Arguments = commandLineArgs, + + IsHidden = true, + ErrorDialog = false, + EnableRaisingEvents = true + }); + + var workingDirectory = GetWorkingDirectory(options); + if (!string.IsNullOrWhiteSpace(workingDirectory)) + { + process.StartInfo.WorkingDirectory = workingDirectory; + } + + OnTranscodeBeginning(encodingJob); + + var commandLineLogMessage = process.StartInfo.FileName + " " + process.StartInfo.Arguments; + Logger.Info(commandLineLogMessage); + + var logFilePath = Path.Combine(ConfigurationManager.CommonApplicationPaths.LogDirectoryPath, "transcode-" + Guid.NewGuid() + ".txt"); + FileSystem.CreateDirectory(FileSystem.GetDirectoryName(logFilePath)); + + // FFMpeg writes debug/error info to stderr. This is useful when debugging so let's put it in the log directory. + encodingJob.LogFileStream = FileSystem.GetFileStream(logFilePath, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.Read, true); + + var commandLineLogMessageBytes = Encoding.UTF8.GetBytes(commandLineLogMessage + Environment.NewLine + Environment.NewLine); + await encodingJob.LogFileStream.WriteAsync(commandLineLogMessageBytes, 0, commandLineLogMessageBytes.Length, cancellationToken).ConfigureAwait(false); + + process.Exited += (sender, args) => OnFfMpegProcessExited(process, encodingJob); + + try + { + process.Start(); + } + catch (Exception ex) + { + Logger.ErrorException("Error starting ffmpeg", ex); + + OnTranscodeFailedToStart(encodingJob.OutputFilePath, encodingJob); + + throw; + } + + cancellationToken.Register(() => Cancel(process, encodingJob)); + + // MUST read both stdout and stderr asynchronously or a deadlock may occurr + //process.BeginOutputReadLine(); + + // Important - don't await the log task or we won't be able to kill ffmpeg when the user stops playback + new JobLogger(Logger).StartStreamingLog(encodingJob, process.StandardError.BaseStream, encodingJob.LogFileStream); + + // Wait for the file to exist before proceeeding + while (!FileSystem.FileExists(encodingJob.OutputFilePath) && !encodingJob.HasExited) + { + await Task.Delay(100, cancellationToken).ConfigureAwait(false); + } + + return encodingJob; + } + + private void Cancel(IProcess process, EncodingJob job) + { + Logger.Info("Killing ffmpeg process for {0}", job.OutputFilePath); + + //process.Kill(); + process.StandardInput.WriteLine("q"); + + job.IsCancelled = true; + } + + /// + /// Processes the exited. + /// + /// The process. + /// The job. + private void OnFfMpegProcessExited(IProcess process, EncodingJob job) + { + job.HasExited = true; + + Logger.Debug("Disposing stream resources"); + job.Dispose(); + + var isSuccesful = false; + + try + { + var exitCode = process.ExitCode; + Logger.Info("FFMpeg exited with code {0}", exitCode); + + isSuccesful = exitCode == 0; + } + catch + { + Logger.Error("FFMpeg exited with an error."); + } + + if (isSuccesful && !job.IsCancelled) + { + job.TaskCompletionSource.TrySetResult(true); + } + else if (job.IsCancelled) + { + try + { + DeleteFiles(job); + } + catch + { + } + try + { + job.TaskCompletionSource.TrySetException(new OperationCanceledException()); + } + catch + { + } + } + else + { + try + { + DeleteFiles(job); + } + catch + { + } + try + { + job.TaskCompletionSource.TrySetException(new Exception("Encoding failed")); + } + catch + { + } + } + + // This causes on exited to be called twice: + //try + //{ + // // Dispose the process + // process.Dispose(); + //} + //catch (Exception ex) + //{ + // Logger.ErrorException("Error disposing ffmpeg.", ex); + //} + } + + protected virtual void DeleteFiles(EncodingJob job) + { + FileSystem.DeleteFile(job.OutputFilePath); + } + + private void OnTranscodeBeginning(EncodingJob job) + { + job.ReportTranscodingProgress(null, null, null, null, null); + } + + private void OnTranscodeFailedToStart(string path, EncodingJob job) + { + if (!string.IsNullOrWhiteSpace(job.Options.DeviceId)) + { + SessionManager.ClearTranscodingInfo(job.Options.DeviceId); + } + } + + protected abstract bool IsVideoEncoder { get; } + + protected virtual string GetWorkingDirectory(EncodingJobOptions options) + { + return null; + } + + protected EncodingOptions GetEncodingOptions() + { + return ConfigurationManager.GetConfiguration("encoding"); + } + + protected abstract string GetCommandLineArguments(EncodingJob job); + + private string GetOutputFilePath(EncodingJob state) + { + var folder = string.IsNullOrWhiteSpace(state.Options.TempDirectory) ? + ConfigurationManager.ApplicationPaths.TranscodingTempPath : + state.Options.TempDirectory; + + var outputFileExtension = GetOutputFileExtension(state); + + var filename = state.Id + (outputFileExtension ?? string.Empty).ToLower(); + return Path.Combine(folder, filename); + } + + protected virtual string GetOutputFileExtension(EncodingJob state) + { + if (!string.IsNullOrWhiteSpace(state.Options.Container)) + { + return "." + state.Options.Container; + } + + return null; + } + + /// + /// Gets the name of the output video codec + /// + /// The state. + /// System.String. + protected string GetVideoDecoder(EncodingJob state) + { + if (string.Equals(state.OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase)) + { + return null; + } + + // Only use alternative encoders for video files. + // When using concat with folder rips, if the mfx session fails to initialize, ffmpeg will be stuck retrying and will not exit gracefully + // Since transcoding of folder rips is expiremental anyway, it's not worth adding additional variables such as this. + if (state.VideoType != VideoType.VideoFile) + { + return null; + } + + if (state.VideoStream != null && !string.IsNullOrWhiteSpace(state.VideoStream.Codec)) + { + if (string.Equals(GetEncodingOptions().HardwareAccelerationType, "qsv", StringComparison.OrdinalIgnoreCase)) + { + switch (state.MediaSource.VideoStream.Codec.ToLower()) + { + case "avc": + case "h264": + if (MediaEncoder.SupportsDecoder("h264_qsv")) + { + // Seeing stalls and failures with decoding. Not worth it compared to encoding. + return "-c:v h264_qsv "; + } + break; + case "mpeg2video": + if (MediaEncoder.SupportsDecoder("mpeg2_qsv")) + { + return "-c:v mpeg2_qsv "; + } + break; + case "vc1": + if (MediaEncoder.SupportsDecoder("vc1_qsv")) + { + return "-c:v vc1_qsv "; + } + break; + } + } + } + + // leave blank so ffmpeg will decide + return null; + } + + private async Task AcquireResources(EncodingJob state, CancellationToken cancellationToken) + { + if (state.VideoType == VideoType.Iso && state.IsoType.HasValue && IsoManager.CanMount(state.MediaPath)) + { + state.IsoMount = await IsoManager.Mount(state.MediaPath, cancellationToken).ConfigureAwait(false); + } + + if (state.MediaSource.RequiresOpening && string.IsNullOrWhiteSpace(state.Options.LiveStreamId)) + { + var liveStreamResponse = await MediaSourceManager.OpenLiveStream(new LiveStreamRequest + { + OpenToken = state.MediaSource.OpenToken + + }, cancellationToken).ConfigureAwait(false); + + EncodingHelper.AttachMediaSourceInfo(state, liveStreamResponse.MediaSource, null); + + if (state.IsVideoRequest) + { + EncodingHelper.TryStreamCopy(state); + } + } + + if (state.MediaSource.BufferMs.HasValue) + { + await Task.Delay(state.MediaSource.BufferMs.Value, cancellationToken).ConfigureAwait(false); + } + } + } +} diff --git a/MediaBrowser.MediaEncoding/Encoder/EncoderValidator.cs b/MediaBrowser.MediaEncoding/Encoder/EncoderValidator.cs new file mode 100644 index 0000000000..59f3576ec8 --- /dev/null +++ b/MediaBrowser.MediaEncoding/Encoder/EncoderValidator.cs @@ -0,0 +1,219 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Globalization; +using MediaBrowser.Model.Diagnostics; +using MediaBrowser.Model.Logging; + +namespace MediaBrowser.MediaEncoding.Encoder +{ + public class EncoderValidator + { + private readonly ILogger _logger; + private readonly IProcessFactory _processFactory; + + public EncoderValidator(ILogger logger, IProcessFactory processFactory) + { + _logger = logger; + _processFactory = processFactory; + } + + public Tuple, List> Validate(string encoderPath) + { + _logger.Info("Validating media encoder at {0}", encoderPath); + + var decoders = GetDecoders(encoderPath); + var encoders = GetEncoders(encoderPath); + + _logger.Info("Encoder validation complete"); + + return new Tuple, List>(decoders, encoders); + } + + public bool ValidateVersion(string encoderAppPath, bool logOutput) + { + string output = string.Empty; + try + { + output = GetProcessOutput(encoderAppPath, "-version"); + } + catch (Exception ex) + { + if (logOutput) + { + _logger.ErrorException("Error validating encoder", ex); + } + } + + if (string.IsNullOrWhiteSpace(output)) + { + return false; + } + + _logger.Info("ffmpeg info: {0}", output); + + if (output.IndexOf("Libav developers", StringComparison.OrdinalIgnoreCase) != -1) + { + return false; + } + + output = " " + output + " "; + + for (var i = 2013; i <= 2015; i++) + { + var yearString = i.ToString(CultureInfo.InvariantCulture); + if (output.IndexOf(" " + yearString + " ", StringComparison.OrdinalIgnoreCase) != -1) + { + return false; + } + } + + return true; + } + + private List GetDecoders(string encoderAppPath) + { + string output = string.Empty; + try + { + output = GetProcessOutput(encoderAppPath, "-decoders"); + } + catch (Exception ) + { + //_logger.ErrorException("Error detecting available decoders", ex); + } + + var found = new List(); + var required = new[] + { + "mpeg2video", + "h264_qsv", + "hevc_qsv", + "mpeg2_qsv", + "vc1_qsv", + "h264_cuvid", + "hevc_cuvid", + "dts", + "ac3", + "aac", + "mp3", + "h264", + "hevc" + }; + + foreach (var codec in required) + { + var srch = " " + codec + " "; + + if (output.IndexOf(srch, StringComparison.OrdinalIgnoreCase) != -1) + { + _logger.Info("Decoder available: " + codec); + found.Add(codec); + } + } + + return found; + } + + private List GetEncoders(string encoderAppPath) + { + string output = null; + try + { + output = GetProcessOutput(encoderAppPath, "-encoders"); + } + catch + { + } + + var found = new List(); + var required = new[] + { + "libx264", + "libx265", + "mpeg4", + "msmpeg4", + "libvpx", + "libvpx-vp9", + "aac", + "libmp3lame", + "libopus", + "libvorbis", + "srt", + "h264_nvenc", + "hevc_nvenc", + "h264_qsv", + "hevc_qsv", + "h264_omx", + "hevc_omx", + "h264_vaapi", + "hevc_vaapi", + "ac3" + }; + + output = output ?? string.Empty; + + var index = 0; + + foreach (var codec in required) + { + var srch = " " + codec + " "; + + if (output.IndexOf(srch, StringComparison.OrdinalIgnoreCase) != -1) + { + if (index < required.Length - 1) + { + _logger.Info("Encoder available: " + codec); + } + + found.Add(codec); + } + index++; + } + + return found; + } + + private string GetProcessOutput(string path, string arguments) + { + var process = _processFactory.Create(new ProcessOptions + { + CreateNoWindow = true, + UseShellExecute = false, + FileName = path, + Arguments = arguments, + IsHidden = true, + ErrorDialog = false, + RedirectStandardOutput = true + }); + + _logger.Info("Running {0} {1}", path, arguments); + + using (process) + { + process.Start(); + + try + { + return process.StandardOutput.ReadToEnd(); + } + catch + { + _logger.Info("Killing process {0} {1}", path, arguments); + + // Hate having to do this + try + { + process.Kill(); + } + catch (Exception ex1) + { + _logger.ErrorException("Error killing process", ex1); + } + + throw; + } + } + } + } +} diff --git a/MediaBrowser.MediaEncoding/Encoder/EncodingJob.cs b/MediaBrowser.MediaEncoding/Encoder/EncodingJob.cs new file mode 100644 index 0000000000..945e20dd0e --- /dev/null +++ b/MediaBrowser.MediaEncoding/Encoder/EncodingJob.cs @@ -0,0 +1,197 @@ +using MediaBrowser.Controller.Library; +using MediaBrowser.Controller.MediaEncoding; +using MediaBrowser.Model.Dlna; +using MediaBrowser.Model.Drawing; +using MediaBrowser.Model.Dto; +using MediaBrowser.Model.Entities; +using MediaBrowser.Model.IO; +using MediaBrowser.Model.Logging; +using MediaBrowser.Model.MediaInfo; +using MediaBrowser.Model.Net; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; + +namespace MediaBrowser.MediaEncoding.Encoder +{ + public class EncodingJob : EncodingJobInfo, IDisposable + { + public bool HasExited { get; internal set; } + public bool IsCancelled { get; internal set; } + + public Stream LogFileStream { get; set; } + public IProgress Progress { get; set; } + public TaskCompletionSource TaskCompletionSource; + + public EncodingJobOptions Options + { + get { return (EncodingJobOptions) BaseRequest; } + set { BaseRequest = value; } + } + + public string Id { get; set; } + + public string MimeType { get; set; } + public bool EstimateContentLength { get; set; } + public TranscodeSeekInfo TranscodeSeekInfo { get; set; } + public long? EncodingDurationTicks { get; set; } + + public string ItemType { get; set; } + + public string GetMimeType(string outputPath) + { + if (!string.IsNullOrEmpty(MimeType)) + { + return MimeType; + } + + return MimeTypes.GetMimeType(outputPath); + } + + private readonly ILogger _logger; + private readonly IMediaSourceManager _mediaSourceManager; + + public EncodingJob(ILogger logger, IMediaSourceManager mediaSourceManager) : + base(logger, mediaSourceManager, TranscodingJobType.Progressive) + { + _logger = logger; + _mediaSourceManager = mediaSourceManager; + Id = Guid.NewGuid().ToString("N"); + + _logger = logger; + TaskCompletionSource = new TaskCompletionSource(); + } + + public override void Dispose() + { + DisposeLiveStream(); + DisposeLogStream(); + DisposeIsoMount(); + } + + private void DisposeLogStream() + { + if (LogFileStream != null) + { + try + { + LogFileStream.Dispose(); + } + catch (Exception ex) + { + _logger.ErrorException("Error disposing log stream", ex); + } + + LogFileStream = null; + } + } + + private async void DisposeLiveStream() + { + if (MediaSource.RequiresClosing && string.IsNullOrWhiteSpace(Options.LiveStreamId) && !string.IsNullOrWhiteSpace(MediaSource.LiveStreamId)) + { + try + { + await _mediaSourceManager.CloseLiveStream(MediaSource.LiveStreamId).ConfigureAwait(false); + } + catch (Exception ex) + { + _logger.ErrorException("Error closing media source", ex); + } + } + } + + public string OutputFilePath { get; set; } + + public string ActualOutputVideoCodec + { + get + { + var codec = OutputVideoCodec; + + if (string.Equals(codec, "copy", StringComparison.OrdinalIgnoreCase)) + { + var stream = VideoStream; + + if (stream != null) + { + return stream.Codec; + } + + return null; + } + + return codec; + } + } + + public string ActualOutputAudioCodec + { + get + { + var codec = OutputAudioCodec; + + if (string.Equals(codec, "copy", StringComparison.OrdinalIgnoreCase)) + { + var stream = AudioStream; + + if (stream != null) + { + return stream.Codec; + } + + return null; + } + + return codec; + } + } + + public void ReportTranscodingProgress(TimeSpan? transcodingPosition, float? framerate, double? percentComplete, long? bytesTranscoded, int? bitRate) + { + var ticks = transcodingPosition.HasValue ? transcodingPosition.Value.Ticks : (long?)null; + + // job.Framerate = framerate; + + if (!percentComplete.HasValue && ticks.HasValue && RunTimeTicks.HasValue) + { + var pct = ticks.Value / RunTimeTicks.Value; + percentComplete = pct * 100; + } + + if (percentComplete.HasValue) + { + Progress.Report(percentComplete.Value); + } + + // job.TranscodingPositionTicks = ticks; + // job.BytesTranscoded = bytesTranscoded; + + var deviceId = Options.DeviceId; + + if (!string.IsNullOrWhiteSpace(deviceId)) + { + var audioCodec = ActualOutputVideoCodec; + var videoCodec = ActualOutputVideoCodec; + + // SessionManager.ReportTranscodingInfo(deviceId, new TranscodingInfo + // { + // Bitrate = job.TotalOutputBitrate, + // AudioCodec = audioCodec, + // VideoCodec = videoCodec, + // Container = job.Options.OutputContainer, + // Framerate = framerate, + // CompletionPercentage = percentComplete, + // Width = job.OutputWidth, + // Height = job.OutputHeight, + // AudioChannels = job.OutputAudioChannels, + // IsAudioDirect = string.Equals(job.OutputAudioCodec, "copy", StringComparison.OrdinalIgnoreCase), + // IsVideoDirect = string.Equals(job.OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase) + // }); + } + } + } +} diff --git a/MediaBrowser.MediaEncoding/Encoder/EncodingJobFactory.cs b/MediaBrowser.MediaEncoding/Encoder/EncodingJobFactory.cs new file mode 100644 index 0000000000..8d8d05a16e --- /dev/null +++ b/MediaBrowser.MediaEncoding/Encoder/EncodingJobFactory.cs @@ -0,0 +1,309 @@ +using MediaBrowser.Common.Configuration; +using MediaBrowser.Controller.Library; +using MediaBrowser.Controller.MediaEncoding; +using MediaBrowser.Model.Configuration; +using MediaBrowser.Model.Dlna; +using MediaBrowser.Model.Dto; +using MediaBrowser.Model.Entities; +using MediaBrowser.Model.Logging; +using MediaBrowser.Model.MediaInfo; +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; + +namespace MediaBrowser.MediaEncoding.Encoder +{ + public class EncodingJobFactory + { + private readonly ILogger _logger; + private readonly ILibraryManager _libraryManager; + private readonly IMediaSourceManager _mediaSourceManager; + private readonly IConfigurationManager _config; + private readonly IMediaEncoder _mediaEncoder; + + protected static readonly CultureInfo UsCulture = new CultureInfo("en-US"); + + public EncodingJobFactory(ILogger logger, ILibraryManager libraryManager, IMediaSourceManager mediaSourceManager, IConfigurationManager config, IMediaEncoder mediaEncoder) + { + _logger = logger; + _libraryManager = libraryManager; + _mediaSourceManager = mediaSourceManager; + _config = config; + _mediaEncoder = mediaEncoder; + } + + public async Task CreateJob(EncodingJobOptions options, EncodingHelper encodingHelper, bool isVideoRequest, IProgress progress, CancellationToken cancellationToken) + { + var request = options; + + if (string.IsNullOrEmpty(request.AudioCodec)) + { + request.AudioCodec = InferAudioCodec(request.Container); + } + + var state = new EncodingJob(_logger, _mediaSourceManager) + { + Options = options, + IsVideoRequest = isVideoRequest, + Progress = progress + }; + + if (!string.IsNullOrWhiteSpace(request.VideoCodec)) + { + state.SupportedVideoCodecs = request.VideoCodec.Split(',').Where(i => !string.IsNullOrWhiteSpace(i)).ToArray(); + request.VideoCodec = state.SupportedVideoCodecs.FirstOrDefault(); + } + + if (!string.IsNullOrWhiteSpace(request.AudioCodec)) + { + state.SupportedAudioCodecs = request.AudioCodec.Split(',').Where(i => !string.IsNullOrWhiteSpace(i)).ToArray(); + request.AudioCodec = state.SupportedAudioCodecs.FirstOrDefault(); + } + + if (!string.IsNullOrWhiteSpace(request.SubtitleCodec)) + { + state.SupportedSubtitleCodecs = request.SubtitleCodec.Split(',').Where(i => !string.IsNullOrWhiteSpace(i)).ToArray(); + request.SubtitleCodec = state.SupportedSubtitleCodecs.FirstOrDefault(i => _mediaEncoder.CanEncodeToSubtitleCodec(i)) + ?? state.SupportedSubtitleCodecs.FirstOrDefault(); + } + + var item = _libraryManager.GetItemById(request.Id); + state.ItemType = item.GetType().Name; + + state.IsInputVideo = string.Equals(item.MediaType, MediaType.Video, StringComparison.OrdinalIgnoreCase); + + // TODO + // var primaryImage = item.GetImageInfo(ImageType.Primary, 0) ?? + // item.Parents.Select(i => i.GetImageInfo(ImageType.Primary, 0)).FirstOrDefault(i => i != null); + + // if (primaryImage != null) + // { + // state.AlbumCoverPath = primaryImage.Path; + // } + + // TODO network path substition useful ? + var mediaSources = await _mediaSourceManager.GetPlayackMediaSources(item, null, true, true, cancellationToken).ConfigureAwait(false); + + var mediaSource = string.IsNullOrEmpty(request.MediaSourceId) + ? mediaSources.First() + : mediaSources.First(i => string.Equals(i.Id, request.MediaSourceId)); + + var videoRequest = state.Options; + + encodingHelper.AttachMediaSourceInfo(state, mediaSource, null); + + //var container = Path.GetExtension(state.RequestedUrl); + + //if (string.IsNullOrEmpty(container)) + //{ + // container = request.Static ? + // state.InputContainer : + // (Path.GetExtension(GetOutputFilePath(state)) ?? string.Empty).TrimStart('.'); + //} + + //state.OutputContainer = (container ?? string.Empty).TrimStart('.'); + + state.OutputAudioBitrate = encodingHelper.GetAudioBitrateParam(state.Options, state.AudioStream); + + state.OutputAudioCodec = state.Options.AudioCodec; + + state.OutputAudioChannels = encodingHelper.GetNumAudioChannelsParam(state, state.AudioStream, state.OutputAudioCodec); + + if (videoRequest != null) + { + state.OutputVideoCodec = state.Options.VideoCodec; + state.OutputVideoBitrate = encodingHelper.GetVideoBitrateParamValue(state.Options, state.VideoStream, state.OutputVideoCodec); + + if (state.OutputVideoBitrate.HasValue) + { + var resolution = ResolutionNormalizer.Normalize( + state.VideoStream == null ? (int?)null : state.VideoStream.BitRate, + state.VideoStream == null ? (int?)null : state.VideoStream.Width, + state.VideoStream == null ? (int?)null : state.VideoStream.Height, + state.OutputVideoBitrate.Value, + state.VideoStream == null ? null : state.VideoStream.Codec, + state.OutputVideoCodec, + videoRequest.MaxWidth, + videoRequest.MaxHeight); + + videoRequest.MaxWidth = resolution.MaxWidth; + videoRequest.MaxHeight = resolution.MaxHeight; + } + } + + ApplyDeviceProfileSettings(state); + + if (videoRequest != null) + { + encodingHelper.TryStreamCopy(state); + } + + //state.OutputFilePath = GetOutputFilePath(state); + + return state; + } + + protected EncodingOptions GetEncodingOptions() + { + return _config.GetConfiguration("encoding"); + } + + /// + /// Infers the video codec. + /// + /// The container. + /// System.Nullable{VideoCodecs}. + private static string InferVideoCodec(string container) + { + var ext = "." + (container ?? string.Empty); + + if (string.Equals(ext, ".asf", StringComparison.OrdinalIgnoreCase)) + { + return "wmv"; + } + if (string.Equals(ext, ".webm", StringComparison.OrdinalIgnoreCase)) + { + return "vpx"; + } + if (string.Equals(ext, ".ogg", StringComparison.OrdinalIgnoreCase) || string.Equals(ext, ".ogv", StringComparison.OrdinalIgnoreCase)) + { + return "theora"; + } + if (string.Equals(ext, ".m3u8", StringComparison.OrdinalIgnoreCase) || string.Equals(ext, ".ts", StringComparison.OrdinalIgnoreCase)) + { + return "h264"; + } + + return "copy"; + } + + private string InferAudioCodec(string container) + { + var ext = "." + (container ?? string.Empty); + + if (string.Equals(ext, ".mp3", StringComparison.OrdinalIgnoreCase)) + { + return "mp3"; + } + if (string.Equals(ext, ".aac", StringComparison.OrdinalIgnoreCase)) + { + return "aac"; + } + if (string.Equals(ext, ".wma", StringComparison.OrdinalIgnoreCase)) + { + return "wma"; + } + if (string.Equals(ext, ".ogg", StringComparison.OrdinalIgnoreCase)) + { + return "vorbis"; + } + if (string.Equals(ext, ".oga", StringComparison.OrdinalIgnoreCase)) + { + return "vorbis"; + } + if (string.Equals(ext, ".ogv", StringComparison.OrdinalIgnoreCase)) + { + return "vorbis"; + } + if (string.Equals(ext, ".webm", StringComparison.OrdinalIgnoreCase)) + { + return "vorbis"; + } + if (string.Equals(ext, ".webma", StringComparison.OrdinalIgnoreCase)) + { + return "vorbis"; + } + + return "copy"; + } + + /// + /// Determines whether the specified stream is H264. + /// + /// The stream. + /// true if the specified stream is H264; otherwise, false. + protected bool IsH264(MediaStream stream) + { + var codec = stream.Codec ?? string.Empty; + + return codec.IndexOf("264", StringComparison.OrdinalIgnoreCase) != -1 || + codec.IndexOf("avc", StringComparison.OrdinalIgnoreCase) != -1; + } + + private static int GetVideoProfileScore(string profile) + { + var list = new List + { + "Constrained Baseline", + "Baseline", + "Extended", + "Main", + "High", + "Progressive High", + "Constrained High" + }; + + return Array.FindIndex(list.ToArray(), t => string.Equals(t, profile, StringComparison.OrdinalIgnoreCase)); + } + + private void ApplyDeviceProfileSettings(EncodingJob state) + { + var profile = state.Options.DeviceProfile; + + if (profile == null) + { + // Don't use settings from the default profile. + // Only use a specific profile if it was requested. + return; + } + + var audioCodec = state.ActualOutputAudioCodec; + + var videoCodec = state.ActualOutputVideoCodec; + var outputContainer = state.Options.Container; + + var mediaProfile = state.IsVideoRequest ? + profile.GetAudioMediaProfile(outputContainer, audioCodec, state.OutputAudioChannels, state.OutputAudioBitrate, state.OutputAudioSampleRate, state.OutputAudioBitDepth) : + profile.GetVideoMediaProfile(outputContainer, + audioCodec, + videoCodec, + state.OutputWidth, + state.OutputHeight, + state.TargetVideoBitDepth, + state.OutputVideoBitrate, + state.TargetVideoProfile, + state.TargetVideoLevel, + state.TargetFramerate, + state.TargetPacketLength, + state.TargetTimestamp, + state.IsTargetAnamorphic, + state.IsTargetInterlaced, + state.TargetRefFrames, + state.TargetVideoStreamCount, + state.TargetAudioStreamCount, + state.TargetVideoCodecTag, + state.IsTargetAVC); + + if (mediaProfile != null) + { + state.MimeType = mediaProfile.MimeType; + } + + var transcodingProfile = state.IsVideoRequest ? + profile.GetAudioTranscodingProfile(outputContainer, audioCodec) : + profile.GetVideoTranscodingProfile(outputContainer, audioCodec, videoCodec); + + if (transcodingProfile != null) + { + state.EstimateContentLength = transcodingProfile.EstimateContentLength; + //state.EnableMpegtsM2TsMode = transcodingProfile.EnableMpegtsM2TsMode; + state.TranscodeSeekInfo = transcodingProfile.TranscodeSeekInfo; + + state.Options.CopyTimestamps = transcodingProfile.CopyTimestamps; + } + } + } +} diff --git a/MediaBrowser.MediaEncoding/Encoder/EncodingUtils.cs b/MediaBrowser.MediaEncoding/Encoder/EncodingUtils.cs new file mode 100644 index 0000000000..dc3cb5f5e1 --- /dev/null +++ b/MediaBrowser.MediaEncoding/Encoder/EncodingUtils.cs @@ -0,0 +1,70 @@ +using MediaBrowser.Model.MediaInfo; +using System.Collections.Generic; +using System.Linq; + +namespace MediaBrowser.MediaEncoding.Encoder +{ + public static class EncodingUtils + { + public static string GetInputArgument(List inputFiles, MediaProtocol protocol) + { + if (protocol != MediaProtocol.File) + { + var url = inputFiles.First(); + + return string.Format("\"{0}\"", url); + } + + return GetConcatInputArgument(inputFiles); + } + + /// + /// Gets the concat input argument. + /// + /// The input files. + /// System.String. + private static string GetConcatInputArgument(IReadOnlyList inputFiles) + { + // Get all streams + // If there's more than one we'll need to use the concat command + if (inputFiles.Count > 1) + { + var files = string.Join("|", inputFiles.Select(NormalizePath).ToArray()); + + return string.Format("concat:\"{0}\"", files); + } + + // Determine the input path for video files + return GetFileInputArgument(inputFiles[0]); + } + + /// + /// Gets the file input argument. + /// + /// The path. + /// System.String. + private static string GetFileInputArgument(string path) + { + if (path.IndexOf("://") != -1) + { + return string.Format("\"{0}\"", path); + } + + // Quotes are valid path characters in linux and they need to be escaped here with a leading \ + path = NormalizePath(path); + + return string.Format("file:\"{0}\"", path); + } + + /// + /// Normalizes the path. + /// + /// The path. + /// System.String. + private static string NormalizePath(string path) + { + // Quotes are valid path characters in linux and they need to be escaped here with a leading \ + return path.Replace("\"", "\\\""); + } + } +} diff --git a/MediaBrowser.MediaEncoding/Encoder/FontConfigLoader.cs b/MediaBrowser.MediaEncoding/Encoder/FontConfigLoader.cs new file mode 100644 index 0000000000..e21292cbd7 --- /dev/null +++ b/MediaBrowser.MediaEncoding/Encoder/FontConfigLoader.cs @@ -0,0 +1,182 @@ +using System; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using MediaBrowser.Model.IO; +using MediaBrowser.Common.Configuration; + +using MediaBrowser.Common.Net; +using MediaBrowser.Common.Progress; +using MediaBrowser.Controller.IO; +using MediaBrowser.Model.IO; +using MediaBrowser.Model.Logging; +using MediaBrowser.Model.Net; + +namespace MediaBrowser.MediaEncoding.Encoder +{ + public class FontConfigLoader + { + private readonly IHttpClient _httpClient; + private readonly IApplicationPaths _appPaths; + private readonly ILogger _logger; + private readonly IZipClient _zipClient; + private readonly IFileSystem _fileSystem; + + private readonly string[] _fontUrls = + { + "https://github.com/MediaBrowser/MediaBrowser.Resources/raw/master/ffmpeg/ARIALUNI.7z" + }; + + public FontConfigLoader(IHttpClient httpClient, IApplicationPaths appPaths, ILogger logger, IZipClient zipClient, IFileSystem fileSystem) + { + _httpClient = httpClient; + _appPaths = appPaths; + _logger = logger; + _zipClient = zipClient; + _fileSystem = fileSystem; + } + + /// + /// Extracts the fonts. + /// + /// The target path. + /// Task. + public async Task DownloadFonts(string targetPath) + { + try + { + var fontsDirectory = Path.Combine(targetPath, "fonts"); + + _fileSystem.CreateDirectory(fontsDirectory); + + const string fontFilename = "ARIALUNI.TTF"; + + var fontFile = Path.Combine(fontsDirectory, fontFilename); + + if (_fileSystem.FileExists(fontFile)) + { + await WriteFontConfigFile(fontsDirectory).ConfigureAwait(false); + } + else + { + // Kick this off, but no need to wait on it + var task = Task.Run(async () => + { + await DownloadFontFile(fontsDirectory, fontFilename, new SimpleProgress()).ConfigureAwait(false); + + await WriteFontConfigFile(fontsDirectory).ConfigureAwait(false); + }); + } + } + catch (HttpException ex) + { + // Don't let the server crash because of this + _logger.ErrorException("Error downloading ffmpeg font files", ex); + } + catch (Exception ex) + { + // Don't let the server crash because of this + _logger.ErrorException("Error writing ffmpeg font files", ex); + } + } + + /// + /// Downloads the font file. + /// + /// The fonts directory. + /// The font filename. + /// Task. + private async Task DownloadFontFile(string fontsDirectory, string fontFilename, IProgress progress) + { + var existingFile = _fileSystem + .GetFilePaths(_appPaths.ProgramDataPath, true) + .FirstOrDefault(i => string.Equals(fontFilename, Path.GetFileName(i), StringComparison.OrdinalIgnoreCase)); + + if (existingFile != null) + { + try + { + _fileSystem.CopyFile(existingFile, Path.Combine(fontsDirectory, fontFilename), true); + return; + } + catch (IOException ex) + { + // Log this, but don't let it fail the operation + _logger.ErrorException("Error copying file", ex); + } + } + + string tempFile = null; + + foreach (var url in _fontUrls) + { + progress.Report(0); + + try + { + tempFile = await _httpClient.GetTempFile(new HttpRequestOptions + { + Url = url, + Progress = progress + + }).ConfigureAwait(false); + + break; + } + catch (Exception ex) + { + // The core can function without the font file, so handle this + _logger.ErrorException("Failed to download ffmpeg font file from {0}", ex, url); + } + } + + if (string.IsNullOrEmpty(tempFile)) + { + return; + } + + Extract7zArchive(tempFile, fontsDirectory); + + try + { + _fileSystem.DeleteFile(tempFile); + } + catch (IOException ex) + { + // Log this, but don't let it fail the operation + _logger.ErrorException("Error deleting temp file {0}", ex, tempFile); + } + } + private void Extract7zArchive(string archivePath, string targetPath) + { + _logger.Info("Extracting {0} to {1}", archivePath, targetPath); + + _zipClient.ExtractAllFrom7z(archivePath, targetPath, true); + } + + /// + /// Writes the font config file. + /// + /// The fonts directory. + /// Task. + private async Task WriteFontConfigFile(string fontsDirectory) + { + const string fontConfigFilename = "fonts.conf"; + var fontConfigFile = Path.Combine(fontsDirectory, fontConfigFilename); + + if (!_fileSystem.FileExists(fontConfigFile)) + { + var contents = string.Format("{0}ArialArial Unicode MS", fontsDirectory); + + var bytes = Encoding.UTF8.GetBytes(contents); + + using (var fileStream = _fileSystem.GetFileStream(fontConfigFile, FileOpenMode.Create, FileAccessMode.Write, + FileShareMode.Read, true)) + { + await fileStream.WriteAsync(bytes, 0, bytes.Length); + } + } + } + } +} diff --git a/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs b/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs new file mode 100644 index 0000000000..31c7d20b2a --- /dev/null +++ b/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs @@ -0,0 +1,1148 @@ +using MediaBrowser.Controller.Channels; +using MediaBrowser.Controller.Configuration; +using MediaBrowser.Controller.Library; +using MediaBrowser.Controller.LiveTv; +using MediaBrowser.Controller.MediaEncoding; +using MediaBrowser.Controller.Session; +using MediaBrowser.MediaEncoding.Probing; +using MediaBrowser.Model.Dlna; +using MediaBrowser.Model.Dto; +using MediaBrowser.Model.Entities; +using MediaBrowser.Model.IO; +using MediaBrowser.Model.Logging; +using MediaBrowser.Model.MediaInfo; +using MediaBrowser.Model.Serialization; +using System; +using System.Collections.Generic; +using System.Globalization; +using System.IO; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using MediaBrowser.Model.Configuration; +using MediaBrowser.Common.Configuration; +using MediaBrowser.Common.Extensions; +using MediaBrowser.Common.Net; +using MediaBrowser.Model.Diagnostics; +using MediaBrowser.Model.System; + +namespace MediaBrowser.MediaEncoding.Encoder +{ + /// + /// Class MediaEncoder + /// + public class MediaEncoder : IMediaEncoder, IDisposable + { + /// + /// The _logger + /// + private readonly ILogger _logger; + + /// + /// Gets the json serializer. + /// + /// The json serializer. + private readonly IJsonSerializer _jsonSerializer; + + /// + /// The _thumbnail resource pool + /// + private readonly SemaphoreSlim _thumbnailResourcePool = new SemaphoreSlim(1, 1); + + public string FFMpegPath { get; private set; } + + public string FFProbePath { get; private set; } + + protected readonly IServerConfigurationManager ConfigurationManager; + protected readonly IFileSystem FileSystem; + protected readonly ILiveTvManager LiveTvManager; + protected readonly IIsoManager IsoManager; + protected readonly ILibraryManager LibraryManager; + protected readonly IChannelManager ChannelManager; + protected readonly ISessionManager SessionManager; + protected readonly Func SubtitleEncoder; + protected readonly Func MediaSourceManager; + private readonly IHttpClient _httpClient; + private readonly IZipClient _zipClient; + private readonly IProcessFactory _processFactory; + + private readonly List _runningProcesses = new List(); + private readonly bool _hasExternalEncoder; + private readonly string _originalFFMpegPath; + private readonly string _originalFFProbePath; + private readonly int DefaultImageExtractionTimeoutMs; + private readonly bool EnableEncoderFontFile; + + private readonly IEnvironmentInfo _environmentInfo; + + public MediaEncoder(ILogger logger, IJsonSerializer jsonSerializer, string ffMpegPath, string ffProbePath, bool hasExternalEncoder, IServerConfigurationManager configurationManager, IFileSystem fileSystem, ILiveTvManager liveTvManager, IIsoManager isoManager, ILibraryManager libraryManager, IChannelManager channelManager, ISessionManager sessionManager, Func subtitleEncoder, Func mediaSourceManager, IHttpClient httpClient, IZipClient zipClient, IProcessFactory processFactory, + int defaultImageExtractionTimeoutMs, + bool enableEncoderFontFile, IEnvironmentInfo environmentInfo) + { + _logger = logger; + _jsonSerializer = jsonSerializer; + ConfigurationManager = configurationManager; + FileSystem = fileSystem; + LiveTvManager = liveTvManager; + IsoManager = isoManager; + LibraryManager = libraryManager; + ChannelManager = channelManager; + SessionManager = sessionManager; + SubtitleEncoder = subtitleEncoder; + MediaSourceManager = mediaSourceManager; + _httpClient = httpClient; + _zipClient = zipClient; + _processFactory = processFactory; + DefaultImageExtractionTimeoutMs = defaultImageExtractionTimeoutMs; + EnableEncoderFontFile = enableEncoderFontFile; + _environmentInfo = environmentInfo; + FFProbePath = ffProbePath; + FFMpegPath = ffMpegPath; + _originalFFProbePath = ffProbePath; + _originalFFMpegPath = ffMpegPath; + + _hasExternalEncoder = hasExternalEncoder; + + SetEnvironmentVariable(); + } + + private readonly object _logLock = new object(); + public void SetLogFilename(string name) + { + lock (_logLock) + { + try + { + _environmentInfo.SetProcessEnvironmentVariable("FFREPORT", "file=" + name + ":level=32"); + } + catch (Exception ex) + { + _logger.ErrorException("Error setting FFREPORT environment variable", ex); + } + } + } + + public void ClearLogFilename() + { + lock (_logLock) + { + try + { + _environmentInfo.SetProcessEnvironmentVariable("FFREPORT", null); + } + catch (Exception ex) + { + //_logger.ErrorException("Error setting FFREPORT environment variable", ex); + } + } + } + + private void SetEnvironmentVariable() + { + try + { + //_environmentInfo.SetProcessEnvironmentVariable("FFREPORT", "file=program-YYYYMMDD-HHMMSS.txt:level=32"); + } + catch (Exception ex) + { + _logger.ErrorException("Error setting FFREPORT environment variable", ex); + } + try + { + //_environmentInfo.SetUserEnvironmentVariable("FFREPORT", "file=program-YYYYMMDD-HHMMSS.txt:level=32"); + } + catch (Exception ex) + { + _logger.ErrorException("Error setting FFREPORT environment variable", ex); + } + } + + public string EncoderLocationType + { + get + { + if (_hasExternalEncoder) + { + return "External"; + } + + if (string.IsNullOrWhiteSpace(FFMpegPath)) + { + return null; + } + + if (IsSystemInstalledPath(FFMpegPath)) + { + return "System"; + } + + return "Custom"; + } + } + + private bool IsSystemInstalledPath(string path) + { + if (path.IndexOf("/", StringComparison.Ordinal) == -1 && path.IndexOf("\\", StringComparison.Ordinal) == -1) + { + return true; + } + + return false; + } + + public void Init() + { + InitPaths(); + + if (!string.IsNullOrWhiteSpace(FFMpegPath)) + { + var result = new EncoderValidator(_logger, _processFactory).Validate(FFMpegPath); + + SetAvailableDecoders(result.Item1); + SetAvailableEncoders(result.Item2); + + if (EnableEncoderFontFile) + { + var directory = FileSystem.GetDirectoryName(FFMpegPath); + + if (!string.IsNullOrWhiteSpace(directory) && FileSystem.ContainsSubPath(ConfigurationManager.ApplicationPaths.ProgramDataPath, directory)) + { + new FontConfigLoader(_httpClient, ConfigurationManager.ApplicationPaths, _logger, _zipClient, FileSystem).DownloadFonts(directory).ConfigureAwait(false); + } + } + } + } + + private void InitPaths() + { + ConfigureEncoderPaths(); + + if (_hasExternalEncoder) + { + LogPaths(); + return; + } + + // If the path was passed in, save it into config now. + var encodingOptions = GetEncodingOptions(); + var appPath = encodingOptions.EncoderAppPath; + + var valueToSave = FFMpegPath; + + if (!string.IsNullOrWhiteSpace(valueToSave)) + { + // if using system variable, don't save this. + if (IsSystemInstalledPath(valueToSave) || _hasExternalEncoder) + { + valueToSave = null; + } + } + + if (!string.Equals(valueToSave, appPath, StringComparison.Ordinal)) + { + encodingOptions.EncoderAppPath = valueToSave; + ConfigurationManager.SaveConfiguration("encoding", encodingOptions); + } + } + + public void UpdateEncoderPath(string path, string pathType) + { + if (_hasExternalEncoder) + { + return; + } + + _logger.Info("Attempting to update encoder path to {0}. pathType: {1}", path ?? string.Empty, pathType ?? string.Empty); + + Tuple newPaths; + + if (string.Equals(pathType, "system", StringComparison.OrdinalIgnoreCase)) + { + path = "ffmpeg"; + + newPaths = TestForInstalledVersions(); + } + else if (string.Equals(pathType, "custom", StringComparison.OrdinalIgnoreCase)) + { + if (string.IsNullOrWhiteSpace(path)) + { + throw new ArgumentNullException("path"); + } + + if (!FileSystem.FileExists(path) && !FileSystem.DirectoryExists(path)) + { + throw new ResourceNotFoundException(); + } + newPaths = GetEncoderPaths(path); + } + else + { + throw new ArgumentException("Unexpected pathType value"); + } + + if (string.IsNullOrWhiteSpace(newPaths.Item1)) + { + throw new ResourceNotFoundException("ffmpeg not found"); + } + if (string.IsNullOrWhiteSpace(newPaths.Item2)) + { + throw new ResourceNotFoundException("ffprobe not found"); + } + + path = newPaths.Item1; + + if (!ValidateVersion(path, true)) + { + throw new ResourceNotFoundException("ffmpeg version 3.0 or greater is required."); + } + + var config = GetEncodingOptions(); + config.EncoderAppPath = path; + ConfigurationManager.SaveConfiguration("encoding", config); + + Init(); + } + + private bool ValidateVersion(string path, bool logOutput) + { + return new EncoderValidator(_logger, _processFactory).ValidateVersion(path, logOutput); + } + + private void ConfigureEncoderPaths() + { + if (_hasExternalEncoder) + { + return; + } + + var appPath = GetEncodingOptions().EncoderAppPath; + + if (string.IsNullOrWhiteSpace(appPath)) + { + appPath = Path.Combine(ConfigurationManager.ApplicationPaths.ProgramDataPath, "ffmpeg"); + } + + var newPaths = GetEncoderPaths(appPath); + if (string.IsNullOrWhiteSpace(newPaths.Item1) || string.IsNullOrWhiteSpace(newPaths.Item2) || IsSystemInstalledPath(appPath)) + { + newPaths = TestForInstalledVersions(); + } + + if (!string.IsNullOrWhiteSpace(newPaths.Item1) && !string.IsNullOrWhiteSpace(newPaths.Item2)) + { + FFMpegPath = newPaths.Item1; + FFProbePath = newPaths.Item2; + } + + LogPaths(); + } + + private Tuple GetEncoderPaths(string configuredPath) + { + var appPath = configuredPath; + + if (!string.IsNullOrWhiteSpace(appPath)) + { + if (FileSystem.DirectoryExists(appPath)) + { + return GetPathsFromDirectory(appPath); + } + + if (FileSystem.FileExists(appPath)) + { + return new Tuple(appPath, GetProbePathFromEncoderPath(appPath)); + } + } + + return new Tuple(null, null); + } + + private Tuple TestForInstalledVersions() + { + string encoderPath = null; + string probePath = null; + + if (_hasExternalEncoder && ValidateVersion(_originalFFMpegPath, true)) + { + encoderPath = _originalFFMpegPath; + probePath = _originalFFProbePath; + } + + if (string.IsNullOrWhiteSpace(encoderPath)) + { + if (ValidateVersion("ffmpeg", true) && ValidateVersion("ffprobe", false)) + { + encoderPath = "ffmpeg"; + probePath = "ffprobe"; + } + } + + return new Tuple(encoderPath, probePath); + } + + private Tuple GetPathsFromDirectory(string path) + { + // Since we can't predict the file extension, first try directly within the folder + // If that doesn't pan out, then do a recursive search + var files = FileSystem.GetFilePaths(path); + + var excludeExtensions = new[] { ".c" }; + + var ffmpegPath = files.FirstOrDefault(i => string.Equals(Path.GetFileNameWithoutExtension(i), "ffmpeg", StringComparison.OrdinalIgnoreCase) && !excludeExtensions.Contains(Path.GetExtension(i) ?? string.Empty)); + var ffprobePath = files.FirstOrDefault(i => string.Equals(Path.GetFileNameWithoutExtension(i), "ffprobe", StringComparison.OrdinalIgnoreCase) && !excludeExtensions.Contains(Path.GetExtension(i) ?? string.Empty)); + + if (string.IsNullOrWhiteSpace(ffmpegPath) || !FileSystem.FileExists(ffmpegPath)) + { + files = FileSystem.GetFilePaths(path, true); + + ffmpegPath = files.FirstOrDefault(i => string.Equals(Path.GetFileNameWithoutExtension(i), "ffmpeg", StringComparison.OrdinalIgnoreCase) && !excludeExtensions.Contains(Path.GetExtension(i) ?? string.Empty)); + + if (!string.IsNullOrWhiteSpace(ffmpegPath)) + { + ffprobePath = GetProbePathFromEncoderPath(ffmpegPath); + } + } + + return new Tuple(ffmpegPath, ffprobePath); + } + + private string GetProbePathFromEncoderPath(string appPath) + { + return FileSystem.GetFilePaths(FileSystem.GetDirectoryName(appPath)) + .FirstOrDefault(i => string.Equals(Path.GetFileNameWithoutExtension(i), "ffprobe", StringComparison.OrdinalIgnoreCase)); + } + + private void LogPaths() + { + _logger.Info("FFMpeg: {0}", FFMpegPath ?? "not found"); + _logger.Info("FFProbe: {0}", FFProbePath ?? "not found"); + } + + private EncodingOptions GetEncodingOptions() + { + return ConfigurationManager.GetConfiguration("encoding"); + } + + private List _encoders = new List(); + public void SetAvailableEncoders(List list) + { + _encoders = list.ToList(); + //_logger.Info("Supported encoders: {0}", string.Join(",", list.ToArray())); + } + + private List _decoders = new List(); + public void SetAvailableDecoders(List list) + { + _decoders = list.ToList(); + //_logger.Info("Supported decoders: {0}", string.Join(",", list.ToArray())); + } + + public bool SupportsEncoder(string encoder) + { + return _encoders.Contains(encoder, StringComparer.OrdinalIgnoreCase); + } + + public bool SupportsDecoder(string decoder) + { + return _decoders.Contains(decoder, StringComparer.OrdinalIgnoreCase); + } + + public bool CanEncodeToAudioCodec(string codec) + { + if (string.Equals(codec, "opus", StringComparison.OrdinalIgnoreCase)) + { + codec = "libopus"; + } + else if (string.Equals(codec, "mp3", StringComparison.OrdinalIgnoreCase)) + { + codec = "libmp3lame"; + } + + return SupportsEncoder(codec); + } + + public bool CanEncodeToSubtitleCodec(string codec) + { + // TODO + return true; + } + + /// + /// Gets the encoder path. + /// + /// The encoder path. + public string EncoderPath + { + get { return FFMpegPath; } + } + + /// + /// Gets the media info. + /// + /// The request. + /// The cancellation token. + /// Task. + public Task GetMediaInfo(MediaInfoRequest request, CancellationToken cancellationToken) + { + var extractChapters = request.MediaType == DlnaProfileType.Video && request.ExtractChapters; + + var inputFiles = MediaEncoderHelpers.GetInputArgument(FileSystem, request.MediaSource.Path, request.MediaSource.Protocol, request.MountedIso, request.PlayableStreamFileNames); + + var probeSize = EncodingHelper.GetProbeSizeArgument(inputFiles.Length); + string analyzeDuration; + + if (request.MediaSource.AnalyzeDurationMs > 0) + { + analyzeDuration = "-analyzeduration " + + (request.MediaSource.AnalyzeDurationMs * 1000).ToString(); + } + else + { + analyzeDuration = EncodingHelper.GetAnalyzeDurationArgument(inputFiles.Length); + } + + probeSize = probeSize + " " + analyzeDuration; + probeSize = probeSize.Trim(); + + var forceEnableLogging = request.MediaSource.Protocol != MediaProtocol.File; + + return GetMediaInfoInternal(GetInputArgument(inputFiles, request.MediaSource.Protocol), request.MediaSource.Path, request.MediaSource.Protocol, extractChapters, + probeSize, request.MediaType == DlnaProfileType.Audio, request.MediaSource.VideoType, forceEnableLogging, cancellationToken); + } + + /// + /// Gets the input argument. + /// + /// The input files. + /// The protocol. + /// System.String. + /// Unrecognized InputType + public string GetInputArgument(string[] inputFiles, MediaProtocol protocol) + { + return EncodingUtils.GetInputArgument(inputFiles.ToList(), protocol); + } + + /// + /// Gets the media info internal. + /// + /// Task{MediaInfoResult}. + private async Task GetMediaInfoInternal(string inputPath, + string primaryPath, + MediaProtocol protocol, + bool extractChapters, + string probeSizeArgument, + bool isAudio, + VideoType? videoType, + bool forceEnableLogging, + CancellationToken cancellationToken) + { + var args = extractChapters + ? "{0} -i {1} -threads 0 -v info -print_format json -show_streams -show_chapters -show_format" + : "{0} -i {1} -threads 0 -v info -print_format json -show_streams -show_format"; + + var process = _processFactory.Create(new ProcessOptions + { + CreateNoWindow = true, + UseShellExecute = false, + + // Must consume both or ffmpeg may hang due to deadlocks. See comments below. + RedirectStandardOutput = true, + FileName = FFProbePath, + Arguments = string.Format(args, probeSizeArgument, inputPath).Trim(), + + IsHidden = true, + ErrorDialog = false, + EnableRaisingEvents = true + }); + + if (forceEnableLogging) + { + _logger.Info("{0} {1}", process.StartInfo.FileName, process.StartInfo.Arguments); + } + else + { + _logger.Debug("{0} {1}", process.StartInfo.FileName, process.StartInfo.Arguments); + } + + using (var processWrapper = new ProcessWrapper(process, this, _logger)) + { + StartProcess(processWrapper); + + try + { + //process.BeginErrorReadLine(); + + var result = _jsonSerializer.DeserializeFromStream(process.StandardOutput.BaseStream); + + if (result == null || (result.streams == null && result.format == null)) + { + throw new Exception("ffprobe failed - streams and format are both null."); + } + + if (result.streams != null) + { + // Normalize aspect ratio if invalid + foreach (var stream in result.streams) + { + if (string.Equals(stream.display_aspect_ratio, "0:1", StringComparison.OrdinalIgnoreCase)) + { + stream.display_aspect_ratio = string.Empty; + } + if (string.Equals(stream.sample_aspect_ratio, "0:1", StringComparison.OrdinalIgnoreCase)) + { + stream.sample_aspect_ratio = string.Empty; + } + } + } + + return new ProbeResultNormalizer(_logger, FileSystem).GetMediaInfo(result, videoType, isAudio, primaryPath, protocol); + } + catch + { + StopProcess(processWrapper, 100); + + throw; + } + } + } + + /// + /// The us culture + /// + protected readonly CultureInfo UsCulture = new CultureInfo("en-US"); + + public Task ExtractAudioImage(string path, int? imageStreamIndex, CancellationToken cancellationToken) + { + return ExtractImage(new[] { path }, null, null, imageStreamIndex, MediaProtocol.File, true, null, null, cancellationToken); + } + + public Task ExtractVideoImage(string[] inputFiles, string container, MediaProtocol protocol, MediaStream videoStream, Video3DFormat? threedFormat, TimeSpan? offset, CancellationToken cancellationToken) + { + return ExtractImage(inputFiles, container, videoStream, null, protocol, false, threedFormat, offset, cancellationToken); + } + + public Task ExtractVideoImage(string[] inputFiles, string container, MediaProtocol protocol, MediaStream imageStream, int? imageStreamIndex, CancellationToken cancellationToken) + { + return ExtractImage(inputFiles, container, imageStream, imageStreamIndex, protocol, false, null, null, cancellationToken); + } + + private async Task ExtractImage(string[] inputFiles, string container, MediaStream videoStream, int? imageStreamIndex, MediaProtocol protocol, bool isAudio, + Video3DFormat? threedFormat, TimeSpan? offset, CancellationToken cancellationToken) + { + var inputArgument = GetInputArgument(inputFiles, protocol); + + if (isAudio) + { + if (imageStreamIndex.HasValue && imageStreamIndex.Value > 0) + { + // It seems for audio files we need to subtract 1 (for the audio stream??) + imageStreamIndex = imageStreamIndex.Value - 1; + } + } + else + { + try + { + return await ExtractImageInternal(inputArgument, container, videoStream, imageStreamIndex, threedFormat, offset, true, cancellationToken).ConfigureAwait(false); + } + catch (ArgumentException) + { + throw; + } + catch + { + _logger.Error("I-frame image extraction failed, will attempt standard way. Input: {0}", inputArgument); + } + } + + return await ExtractImageInternal(inputArgument, container, videoStream, imageStreamIndex, threedFormat, offset, false, cancellationToken).ConfigureAwait(false); + } + + private async Task ExtractImageInternal(string inputPath, string container, MediaStream videoStream, int? imageStreamIndex, Video3DFormat? threedFormat, TimeSpan? offset, bool useIFrame, CancellationToken cancellationToken) + { + if (string.IsNullOrEmpty(inputPath)) + { + throw new ArgumentNullException("inputPath"); + } + + var tempExtractPath = Path.Combine(ConfigurationManager.ApplicationPaths.TempDirectory, Guid.NewGuid() + ".jpg"); + FileSystem.CreateDirectory(FileSystem.GetDirectoryName(tempExtractPath)); + + // apply some filters to thumbnail extracted below (below) crop any black lines that we made and get the correct ar then scale to width 600. + // This filter chain may have adverse effects on recorded tv thumbnails if ar changes during presentation ex. commercials @ diff ar + var vf = "scale=600:trunc(600/dar/2)*2"; + + if (threedFormat.HasValue) + { + switch (threedFormat.Value) + { + case Video3DFormat.HalfSideBySide: + vf = "crop=iw/2:ih:0:0,scale=(iw*2):ih,setdar=dar=a,crop=min(iw\\,ih*dar):min(ih\\,iw/dar):(iw-min(iw\\,iw*sar))/2:(ih - min (ih\\,ih/sar))/2,setsar=sar=1,scale=600:trunc(600/dar/2)*2"; + // hsbs crop width in half,scale to correct size, set the display aspect,crop out any black bars we may have made the scale width to 600. Work out the correct height based on the display aspect it will maintain the aspect where -1 in this case (3d) may not. + break; + case Video3DFormat.FullSideBySide: + vf = "crop=iw/2:ih:0:0,setdar=dar=a,crop=min(iw\\,ih*dar):min(ih\\,iw/dar):(iw-min(iw\\,iw*sar))/2:(ih - min (ih\\,ih/sar))/2,setsar=sar=1,scale=600:trunc(600/dar/2)*2"; + //fsbs crop width in half,set the display aspect,crop out any black bars we may have made the scale width to 600. + break; + case Video3DFormat.HalfTopAndBottom: + vf = "crop=iw:ih/2:0:0,scale=(iw*2):ih),setdar=dar=a,crop=min(iw\\,ih*dar):min(ih\\,iw/dar):(iw-min(iw\\,iw*sar))/2:(ih - min (ih\\,ih/sar))/2,setsar=sar=1,scale=600:trunc(600/dar/2)*2"; + //htab crop heigh in half,scale to correct size, set the display aspect,crop out any black bars we may have made the scale width to 600 + break; + case Video3DFormat.FullTopAndBottom: + vf = "crop=iw:ih/2:0:0,setdar=dar=a,crop=min(iw\\,ih*dar):min(ih\\,iw/dar):(iw-min(iw\\,iw*sar))/2:(ih - min (ih\\,ih/sar))/2,setsar=sar=1,scale=600:trunc(600/dar/2)*2"; + // ftab crop heigt in half, set the display aspect,crop out any black bars we may have made the scale width to 600 + break; + default: + break; + } + } + + var mapArg = imageStreamIndex.HasValue ? (" -map 0:v:" + imageStreamIndex.Value.ToString(CultureInfo.InvariantCulture)) : string.Empty; + + var enableThumbnail = !new List { "wtv" }.Contains(container ?? string.Empty, StringComparer.OrdinalIgnoreCase); + // Use ffmpeg to sample 100 (we can drop this if required using thumbnail=50 for 50 frames) frames and pick the best thumbnail. Have a fall back just in case. + var thumbnail = enableThumbnail ? ",thumbnail=24" : string.Empty; + + var args = useIFrame ? string.Format("-i {0}{3} -threads 0 -v quiet -vframes 1 -vf \"{2}{4}\" -f image2 \"{1}\"", inputPath, tempExtractPath, vf, mapArg, thumbnail) : + string.Format("-i {0}{3} -threads 0 -v quiet -vframes 1 -vf \"{2}\" -f image2 \"{1}\"", inputPath, tempExtractPath, vf, mapArg); + + var probeSizeArgument = EncodingHelper.GetProbeSizeArgument(1); + var analyzeDurationArgument = EncodingHelper.GetAnalyzeDurationArgument(1); + + if (!string.IsNullOrWhiteSpace(probeSizeArgument)) + { + args = probeSizeArgument + " " + args; + } + + if (!string.IsNullOrWhiteSpace(analyzeDurationArgument)) + { + args = analyzeDurationArgument + " " + args; + } + + if (offset.HasValue) + { + args = string.Format("-ss {0} ", GetTimeParameter(offset.Value)) + args; + } + + var encodinghelper = new EncodingHelper(this, FileSystem, SubtitleEncoder()); + if (videoStream != null) + { + var decoder = encodinghelper.GetHardwareAcceleratedVideoDecoder(VideoType.VideoFile, videoStream, GetEncodingOptions()); + if (!string.IsNullOrWhiteSpace(decoder)) + { + args = decoder + " " + args; + } + } + + if (!string.IsNullOrWhiteSpace(container)) + { + var inputFormat = encodinghelper.GetInputFormat(container); + if (!string.IsNullOrWhiteSpace(inputFormat)) + { + args = "-f " + inputFormat + " " + args; + } + } + + var process = _processFactory.Create(new ProcessOptions + { + CreateNoWindow = true, + UseShellExecute = false, + FileName = FFMpegPath, + Arguments = args, + IsHidden = true, + ErrorDialog = false + }); + + _logger.Debug("{0} {1}", process.StartInfo.FileName, process.StartInfo.Arguments); + + using (var processWrapper = new ProcessWrapper(process, this, _logger)) + { + bool ranToCompletion; + + StartProcess(processWrapper); + + var timeoutMs = ConfigurationManager.Configuration.ImageExtractionTimeoutMs; + if (timeoutMs <= 0) + { + timeoutMs = DefaultImageExtractionTimeoutMs; + } + + ranToCompletion = await process.WaitForExitAsync(timeoutMs).ConfigureAwait(false); + + if (!ranToCompletion) + { + StopProcess(processWrapper, 1000); + } + + var exitCode = ranToCompletion ? processWrapper.ExitCode ?? 0 : -1; + var file = FileSystem.GetFileInfo(tempExtractPath); + + if (exitCode == -1 || !file.Exists || file.Length == 0) + { + var msg = string.Format("ffmpeg image extraction failed for {0}", inputPath); + + _logger.Error(msg); + + throw new Exception(msg); + } + + return tempExtractPath; + } + } + + public string GetTimeParameter(long ticks) + { + var time = TimeSpan.FromTicks(ticks); + + return GetTimeParameter(time); + } + + public string GetTimeParameter(TimeSpan time) + { + return time.ToString(@"hh\:mm\:ss\.fff", UsCulture); + } + + public async Task ExtractVideoImagesOnInterval(string[] inputFiles, + string container, + MediaStream videoStream, + MediaProtocol protocol, + Video3DFormat? threedFormat, + TimeSpan interval, + string targetDirectory, + string filenamePrefix, + int? maxWidth, + CancellationToken cancellationToken) + { + var resourcePool = _thumbnailResourcePool; + + var inputArgument = GetInputArgument(inputFiles, protocol); + + var vf = "fps=fps=1/" + interval.TotalSeconds.ToString(UsCulture); + + if (maxWidth.HasValue) + { + var maxWidthParam = maxWidth.Value.ToString(UsCulture); + + vf += string.Format(",scale=min(iw\\,{0}):trunc(ow/dar/2)*2", maxWidthParam); + } + + FileSystem.CreateDirectory(targetDirectory); + var outputPath = Path.Combine(targetDirectory, filenamePrefix + "%05d.jpg"); + + var args = string.Format("-i {0} -threads 0 -v quiet -vf \"{2}\" -f image2 \"{1}\"", inputArgument, outputPath, vf); + + var probeSizeArgument = EncodingHelper.GetProbeSizeArgument(1); + var analyzeDurationArgument = EncodingHelper.GetAnalyzeDurationArgument(1); + + if (!string.IsNullOrWhiteSpace(probeSizeArgument)) + { + args = probeSizeArgument + " " + args; + } + + if (!string.IsNullOrWhiteSpace(analyzeDurationArgument)) + { + args = analyzeDurationArgument + " " + args; + } + + var encodinghelper = new EncodingHelper(this, FileSystem, SubtitleEncoder()); + if (videoStream != null) + { + var decoder = encodinghelper.GetHardwareAcceleratedVideoDecoder(VideoType.VideoFile, videoStream, GetEncodingOptions()); + if (!string.IsNullOrWhiteSpace(decoder)) + { + args = decoder + " " + args; + } + } + + if (!string.IsNullOrWhiteSpace(container)) + { + var inputFormat = encodinghelper.GetInputFormat(container); + if (!string.IsNullOrWhiteSpace(inputFormat)) + { + args = "-f " + inputFormat + " " + args; + } + } + + var process = _processFactory.Create(new ProcessOptions + { + CreateNoWindow = true, + UseShellExecute = false, + FileName = FFMpegPath, + Arguments = args, + IsHidden = true, + ErrorDialog = false + }); + + _logger.Info(process.StartInfo.FileName + " " + process.StartInfo.Arguments); + + await resourcePool.WaitAsync(cancellationToken).ConfigureAwait(false); + + bool ranToCompletion = false; + + using (var processWrapper = new ProcessWrapper(process, this, _logger)) + { + try + { + StartProcess(processWrapper); + + // Need to give ffmpeg enough time to make all the thumbnails, which could be a while, + // but we still need to detect if the process hangs. + // Making the assumption that as long as new jpegs are showing up, everything is good. + + bool isResponsive = true; + int lastCount = 0; + + while (isResponsive) + { + if (await process.WaitForExitAsync(30000).ConfigureAwait(false)) + { + ranToCompletion = true; + break; + } + + cancellationToken.ThrowIfCancellationRequested(); + + var jpegCount = FileSystem.GetFilePaths(targetDirectory) + .Count(i => string.Equals(Path.GetExtension(i), ".jpg", StringComparison.OrdinalIgnoreCase)); + + isResponsive = (jpegCount > lastCount); + lastCount = jpegCount; + } + + if (!ranToCompletion) + { + StopProcess(processWrapper, 1000); + } + } + finally + { + resourcePool.Release(); + } + + var exitCode = ranToCompletion ? processWrapper.ExitCode ?? 0 : -1; + + if (exitCode == -1) + { + var msg = string.Format("ffmpeg image extraction failed for {0}", inputArgument); + + _logger.Error(msg); + + throw new Exception(msg); + } + } + } + + public async Task EncodeAudio(EncodingJobOptions options, + IProgress progress, + CancellationToken cancellationToken) + { + var job = await new AudioEncoder(this, + _logger, + ConfigurationManager, + FileSystem, + IsoManager, + LibraryManager, + SessionManager, + SubtitleEncoder(), + MediaSourceManager(), + _processFactory) + .Start(options, progress, cancellationToken).ConfigureAwait(false); + + await job.TaskCompletionSource.Task.ConfigureAwait(false); + + return job.OutputFilePath; + } + + public async Task EncodeVideo(EncodingJobOptions options, + IProgress progress, + CancellationToken cancellationToken) + { + _logger.Error("EncodeVideo"); + var job = await new VideoEncoder(this, + _logger, + ConfigurationManager, + FileSystem, + IsoManager, + LibraryManager, + SessionManager, + SubtitleEncoder(), + MediaSourceManager(), + _processFactory) + .Start(options, progress, cancellationToken).ConfigureAwait(false); + + await job.TaskCompletionSource.Task.ConfigureAwait(false); + + return job.OutputFilePath; + } + + private void StartProcess(ProcessWrapper process) + { + process.Process.Start(); + + lock (_runningProcesses) + { + _runningProcesses.Add(process); + } + } + private void StopProcess(ProcessWrapper process, int waitTimeMs) + { + try + { + if (process.Process.WaitForExit(waitTimeMs)) + { + return; + } + } + catch (Exception ex) + { + _logger.Error("Error in WaitForExit", ex); + } + + try + { + _logger.Info("Killing ffmpeg process"); + + process.Process.Kill(); + } + catch (Exception ex) + { + _logger.ErrorException("Error killing process", ex); + } + } + + private void StopProcesses() + { + List proceses; + lock (_runningProcesses) + { + proceses = _runningProcesses.ToList(); + _runningProcesses.Clear(); + } + + foreach (var process in proceses) + { + if (!process.HasExited) + { + StopProcess(process, 500); + } + } + } + + public string EscapeSubtitleFilterPath(string path) + { + // https://ffmpeg.org/ffmpeg-filters.html#Notes-on-filtergraph-escaping + // We need to double escape + + return path.Replace('\\', '/').Replace(":", "\\:").Replace("'", "'\\\\\\''"); + } + + /// + /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. + /// + public void Dispose() + { + Dispose(true); + } + + /// + /// Releases unmanaged and - optionally - managed resources. + /// + /// true to release both managed and unmanaged resources; false to release only unmanaged resources. + protected virtual void Dispose(bool dispose) + { + if (dispose) + { + StopProcesses(); + } + } + + public Task ConvertImage(string inputPath, string outputPath) + { + throw new NotImplementedException(); + } + + public string[] GetPlayableStreamFileNames(string path, VideoType videoType) + { + throw new NotImplementedException(); + } + + public IEnumerable GetPrimaryPlaylistVobFiles(string path, IIsoMount isoMount, uint? titleNumber) + { + throw new NotImplementedException(); + } + + public bool CanExtractSubtitles(string codec) + { + return false; + } + + private class ProcessWrapper : IDisposable + { + public readonly IProcess Process; + public bool HasExited; + public int? ExitCode; + private readonly MediaEncoder _mediaEncoder; + private readonly ILogger _logger; + + public ProcessWrapper(IProcess process, MediaEncoder mediaEncoder, ILogger logger) + { + Process = process; + _mediaEncoder = mediaEncoder; + _logger = logger; + Process.Exited += Process_Exited; + } + + void Process_Exited(object sender, EventArgs e) + { + var process = (IProcess)sender; + + HasExited = true; + + try + { + ExitCode = process.ExitCode; + } + catch + { + } + + DisposeProcess(process); + } + + private void DisposeProcess(IProcess process) + { + lock (_mediaEncoder._runningProcesses) + { + _mediaEncoder._runningProcesses.Remove(this); + } + + try + { + process.Dispose(); + } + catch + { + } + } + + private bool _disposed; + private readonly object _syncLock = new object(); + public void Dispose() + { + lock (_syncLock) + { + if (!_disposed) + { + if (Process != null) + { + Process.Exited -= Process_Exited; + DisposeProcess(Process); + } + } + + _disposed = true; + } + } + } + } +} \ No newline at end of file diff --git a/MediaBrowser.MediaEncoding/Encoder/VideoEncoder.cs b/MediaBrowser.MediaEncoding/Encoder/VideoEncoder.cs new file mode 100644 index 0000000000..96c1269236 --- /dev/null +++ b/MediaBrowser.MediaEncoding/Encoder/VideoEncoder.cs @@ -0,0 +1,66 @@ +using MediaBrowser.Controller.Configuration; +using MediaBrowser.Controller.Library; +using MediaBrowser.Controller.MediaEncoding; +using MediaBrowser.Controller.Session; +using MediaBrowser.Model.Dlna; +using MediaBrowser.Model.IO; +using MediaBrowser.Model.Logging; +using System; +using System.IO; +using System.Threading.Tasks; +using MediaBrowser.Model.Diagnostics; + +namespace MediaBrowser.MediaEncoding.Encoder +{ + public class VideoEncoder : BaseEncoder + { + public VideoEncoder(MediaEncoder mediaEncoder, ILogger logger, IServerConfigurationManager configurationManager, IFileSystem fileSystem, IIsoManager isoManager, ILibraryManager libraryManager, ISessionManager sessionManager, ISubtitleEncoder subtitleEncoder, IMediaSourceManager mediaSourceManager, IProcessFactory processFactory) : base(mediaEncoder, logger, configurationManager, fileSystem, isoManager, libraryManager, sessionManager, subtitleEncoder, mediaSourceManager, processFactory) + { + } + + protected override string GetCommandLineArguments(EncodingJob state) + { + // Get the output codec name + var encodingOptions = GetEncodingOptions(); + + return EncodingHelper.GetProgressiveVideoFullCommandLine(state, encodingOptions, state.OutputFilePath, "superfast"); + } + + protected override string GetOutputFileExtension(EncodingJob state) + { + var ext = base.GetOutputFileExtension(state); + + if (!string.IsNullOrEmpty(ext)) + { + return ext; + } + + var videoCodec = state.Options.VideoCodec; + + if (string.Equals(videoCodec, "h264", StringComparison.OrdinalIgnoreCase)) + { + return ".ts"; + } + if (string.Equals(videoCodec, "theora", StringComparison.OrdinalIgnoreCase)) + { + return ".ogv"; + } + if (string.Equals(videoCodec, "vpx", StringComparison.OrdinalIgnoreCase)) + { + return ".webm"; + } + if (string.Equals(videoCodec, "wmv", StringComparison.OrdinalIgnoreCase)) + { + return ".asf"; + } + + return null; + } + + protected override bool IsVideoEncoder + { + get { return true; } + } + + } +} \ No newline at end of file diff --git a/MediaBrowser.MediaEncoding/MediaBrowser.MediaEncoding.csproj b/MediaBrowser.MediaEncoding/MediaBrowser.MediaEncoding.csproj new file mode 100644 index 0000000000..64ab6831c5 --- /dev/null +++ b/MediaBrowser.MediaEncoding/MediaBrowser.MediaEncoding.csproj @@ -0,0 +1,33 @@ + + + netstandard2.0 + false + + + + Properties\SharedVersion.cs + + + + + {88ae38df-19d7-406f-a6a9-09527719a21e} + BDInfo + + + {9142eefa-7570-41e1-bfcc-468bb571af2f} + MediaBrowser.Common + + + {17e1f4e6-8abd-4fe5-9ecf-43d4b6087ba2} + MediaBrowser.Controller + + + {7eeeb4bb-f3e8-48fc-b4c5-70f0fff8329b} + MediaBrowser.Model + + + {4a4402d4-e910-443b-b8fc-2c18286a2ca0} + OpenSubtitlesHandler + + + diff --git a/MediaBrowser.MediaEncoding/MediaBrowser.MediaEncoding.nuget.targets b/MediaBrowser.MediaEncoding/MediaBrowser.MediaEncoding.nuget.targets new file mode 100644 index 0000000000..e69ce0e64f --- /dev/null +++ b/MediaBrowser.MediaEncoding/MediaBrowser.MediaEncoding.nuget.targets @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/MediaBrowser.MediaEncoding/Probing/FFProbeHelpers.cs b/MediaBrowser.MediaEncoding/Probing/FFProbeHelpers.cs new file mode 100644 index 0000000000..396c85e210 --- /dev/null +++ b/MediaBrowser.MediaEncoding/Probing/FFProbeHelpers.cs @@ -0,0 +1,117 @@ +using System; +using System.Collections.Generic; + +namespace MediaBrowser.MediaEncoding.Probing +{ + public static class FFProbeHelpers + { + /// + /// Normalizes the FF probe result. + /// + /// The result. + public static void NormalizeFFProbeResult(InternalMediaInfoResult result) + { + if (result == null) + { + throw new ArgumentNullException("result"); + } + + if (result.format != null && result.format.tags != null) + { + result.format.tags = ConvertDictionaryToCaseInSensitive(result.format.tags); + } + + if (result.streams != null) + { + // Convert all dictionaries to case insensitive + foreach (var stream in result.streams) + { + if (stream.tags != null) + { + stream.tags = ConvertDictionaryToCaseInSensitive(stream.tags); + } + + if (stream.disposition != null) + { + stream.disposition = ConvertDictionaryToCaseInSensitive(stream.disposition); + } + } + } + } + + /// + /// Gets a string from an FFProbeResult tags dictionary + /// + /// The tags. + /// The key. + /// System.String. + public static string GetDictionaryValue(Dictionary tags, string key) + { + if (tags == null) + { + return null; + } + + string val; + + tags.TryGetValue(key, out val); + return val; + } + + /// + /// Gets an int from an FFProbeResult tags dictionary + /// + /// The tags. + /// The key. + /// System.Nullable{System.Int32}. + public static int? GetDictionaryNumericValue(Dictionary tags, string key) + { + var val = GetDictionaryValue(tags, key); + + if (!string.IsNullOrEmpty(val)) + { + int i; + + if (int.TryParse(val, out i)) + { + return i; + } + } + + return null; + } + + /// + /// Gets a DateTime from an FFProbeResult tags dictionary + /// + /// The tags. + /// The key. + /// System.Nullable{DateTime}. + public static DateTime? GetDictionaryDateTime(Dictionary tags, string key) + { + var val = GetDictionaryValue(tags, key); + + if (!string.IsNullOrEmpty(val)) + { + DateTime i; + + if (DateTime.TryParse(val, out i)) + { + return i.ToUniversalTime(); + } + } + + return null; + } + + /// + /// Converts a dictionary to case insensitive + /// + /// The dict. + /// Dictionary{System.StringSystem.String}. + private static Dictionary ConvertDictionaryToCaseInSensitive(Dictionary dict) + { + return new Dictionary(dict, StringComparer.OrdinalIgnoreCase); + } + } +} diff --git a/MediaBrowser.MediaEncoding/Probing/InternalMediaInfoResult.cs b/MediaBrowser.MediaEncoding/Probing/InternalMediaInfoResult.cs new file mode 100644 index 0000000000..eef2732509 --- /dev/null +++ b/MediaBrowser.MediaEncoding/Probing/InternalMediaInfoResult.cs @@ -0,0 +1,341 @@ +using System.Collections.Generic; + +namespace MediaBrowser.MediaEncoding.Probing +{ + /// + /// Class MediaInfoResult + /// + public class InternalMediaInfoResult + { + /// + /// Gets or sets the streams. + /// + /// The streams. + public MediaStreamInfo[] streams { get; set; } + + /// + /// Gets or sets the format. + /// + /// The format. + public MediaFormatInfo format { get; set; } + + /// + /// Gets or sets the chapters. + /// + /// The chapters. + public MediaChapter[] Chapters { get; set; } + } + + public class MediaChapter + { + public int id { get; set; } + public string time_base { get; set; } + public long start { get; set; } + public string start_time { get; set; } + public long end { get; set; } + public string end_time { get; set; } + public Dictionary tags { get; set; } + } + + /// + /// Represents a stream within the output + /// + public class MediaStreamInfo + { + /// + /// Gets or sets the index. + /// + /// The index. + public int index { get; set; } + + /// + /// Gets or sets the profile. + /// + /// The profile. + public string profile { get; set; } + + /// + /// Gets or sets the codec_name. + /// + /// The codec_name. + public string codec_name { get; set; } + + /// + /// Gets or sets the codec_long_name. + /// + /// The codec_long_name. + public string codec_long_name { get; set; } + + /// + /// Gets or sets the codec_type. + /// + /// The codec_type. + public string codec_type { get; set; } + + /// + /// Gets or sets the sample_rate. + /// + /// The sample_rate. + public string sample_rate { get; set; } + + /// + /// Gets or sets the channels. + /// + /// The channels. + public int channels { get; set; } + + /// + /// Gets or sets the channel_layout. + /// + /// The channel_layout. + public string channel_layout { get; set; } + + /// + /// Gets or sets the avg_frame_rate. + /// + /// The avg_frame_rate. + public string avg_frame_rate { get; set; } + + /// + /// Gets or sets the duration. + /// + /// The duration. + public string duration { get; set; } + + /// + /// Gets or sets the bit_rate. + /// + /// The bit_rate. + public string bit_rate { get; set; } + + /// + /// Gets or sets the width. + /// + /// The width. + public int width { get; set; } + + /// + /// Gets or sets the refs. + /// + /// The refs. + public int refs { get; set; } + + /// + /// Gets or sets the height. + /// + /// The height. + public int height { get; set; } + + /// + /// Gets or sets the display_aspect_ratio. + /// + /// The display_aspect_ratio. + public string display_aspect_ratio { get; set; } + + /// + /// Gets or sets the tags. + /// + /// The tags. + public Dictionary tags { get; set; } + + /// + /// Gets or sets the bits_per_sample. + /// + /// The bits_per_sample. + public int bits_per_sample { get; set; } + + /// + /// Gets or sets the bits_per_raw_sample. + /// + /// The bits_per_raw_sample. + public int bits_per_raw_sample { get; set; } + + /// + /// Gets or sets the r_frame_rate. + /// + /// The r_frame_rate. + public string r_frame_rate { get; set; } + + /// + /// Gets or sets the has_b_frames. + /// + /// The has_b_frames. + public int has_b_frames { get; set; } + + /// + /// Gets or sets the sample_aspect_ratio. + /// + /// The sample_aspect_ratio. + public string sample_aspect_ratio { get; set; } + + /// + /// Gets or sets the pix_fmt. + /// + /// The pix_fmt. + public string pix_fmt { get; set; } + + /// + /// Gets or sets the level. + /// + /// The level. + public int level { get; set; } + + /// + /// Gets or sets the time_base. + /// + /// The time_base. + public string time_base { get; set; } + + /// + /// Gets or sets the start_time. + /// + /// The start_time. + public string start_time { get; set; } + + /// + /// Gets or sets the codec_time_base. + /// + /// The codec_time_base. + public string codec_time_base { get; set; } + + /// + /// Gets or sets the codec_tag. + /// + /// The codec_tag. + public string codec_tag { get; set; } + + /// + /// Gets or sets the codec_tag_string. + /// + /// The codec_tag_string. + public string codec_tag_string { get; set; } + + /// + /// Gets or sets the sample_fmt. + /// + /// The sample_fmt. + public string sample_fmt { get; set; } + + /// + /// Gets or sets the dmix_mode. + /// + /// The dmix_mode. + public string dmix_mode { get; set; } + + /// + /// Gets or sets the start_pts. + /// + /// The start_pts. + public string start_pts { get; set; } + + /// + /// Gets or sets the is_avc. + /// + /// The is_avc. + public string is_avc { get; set; } + + /// + /// Gets or sets the nal_length_size. + /// + /// The nal_length_size. + public string nal_length_size { get; set; } + + /// + /// Gets or sets the ltrt_cmixlev. + /// + /// The ltrt_cmixlev. + public string ltrt_cmixlev { get; set; } + + /// + /// Gets or sets the ltrt_surmixlev. + /// + /// The ltrt_surmixlev. + public string ltrt_surmixlev { get; set; } + + /// + /// Gets or sets the loro_cmixlev. + /// + /// The loro_cmixlev. + public string loro_cmixlev { get; set; } + + /// + /// Gets or sets the loro_surmixlev. + /// + /// The loro_surmixlev. + public string loro_surmixlev { get; set; } + + public string field_order { get; set; } + + /// + /// Gets or sets the disposition. + /// + /// The disposition. + public Dictionary disposition { get; set; } + } + + /// + /// Class MediaFormat + /// + public class MediaFormatInfo + { + /// + /// Gets or sets the filename. + /// + /// The filename. + public string filename { get; set; } + + /// + /// Gets or sets the nb_streams. + /// + /// The nb_streams. + public int nb_streams { get; set; } + + /// + /// Gets or sets the format_name. + /// + /// The format_name. + public string format_name { get; set; } + + /// + /// Gets or sets the format_long_name. + /// + /// The format_long_name. + public string format_long_name { get; set; } + + /// + /// Gets or sets the start_time. + /// + /// The start_time. + public string start_time { get; set; } + + /// + /// Gets or sets the duration. + /// + /// The duration. + public string duration { get; set; } + + /// + /// Gets or sets the size. + /// + /// The size. + public string size { get; set; } + + /// + /// Gets or sets the bit_rate. + /// + /// The bit_rate. + public string bit_rate { get; set; } + + /// + /// Gets or sets the probe_score. + /// + /// The probe_score. + public int probe_score { get; set; } + + /// + /// Gets or sets the tags. + /// + /// The tags. + public Dictionary tags { get; set; } + } +} diff --git a/MediaBrowser.MediaEncoding/Probing/ProbeResultNormalizer.cs b/MediaBrowser.MediaEncoding/Probing/ProbeResultNormalizer.cs new file mode 100644 index 0000000000..0a3532b8cf --- /dev/null +++ b/MediaBrowser.MediaEncoding/Probing/ProbeResultNormalizer.cs @@ -0,0 +1,1392 @@ +using MediaBrowser.Model.Dto; +using MediaBrowser.Model.Entities; +using MediaBrowser.Model.Extensions; +using System; +using System.Collections.Generic; +using System.Globalization; +using System.IO; +using System.Linq; +using System.Text; +using System.Xml; +using MediaBrowser.Model.IO; + +using MediaBrowser.Controller.IO; +using MediaBrowser.Controller.Library; +using MediaBrowser.Model.IO; +using MediaBrowser.Model.Logging; +using MediaBrowser.Model.MediaInfo; + +namespace MediaBrowser.MediaEncoding.Probing +{ + public class ProbeResultNormalizer + { + private readonly CultureInfo _usCulture = new CultureInfo("en-US"); + private readonly ILogger _logger; + private readonly IFileSystem _fileSystem; + + public ProbeResultNormalizer(ILogger logger, IFileSystem fileSystem) + { + _logger = logger; + _fileSystem = fileSystem; + } + + public MediaInfo GetMediaInfo(InternalMediaInfoResult data, VideoType? videoType, bool isAudio, string path, MediaProtocol protocol) + { + var info = new MediaInfo + { + Path = path, + Protocol = protocol + }; + + FFProbeHelpers.NormalizeFFProbeResult(data); + SetSize(data, info); + + var internalStreams = data.streams ?? new MediaStreamInfo[] { }; + + info.MediaStreams = internalStreams.Select(s => GetMediaStream(isAudio, s, data.format)) + .Where(i => i != null) + // Drop subtitle streams if we don't know the codec because it will just cause failures if we don't know how to handle them + .Where(i => i.Type != MediaStreamType.Subtitle || !string.IsNullOrWhiteSpace(i.Codec)) + .ToList(); + + if (data.format != null) + { + info.Container = NormalizeFormat(data.format.format_name); + + if (!string.IsNullOrEmpty(data.format.bit_rate)) + { + int value; + if (int.TryParse(data.format.bit_rate, NumberStyles.Any, _usCulture, out value)) + { + info.Bitrate = value; + } + } + } + + var tags = new Dictionary(StringComparer.OrdinalIgnoreCase); + var tagStreamType = isAudio ? "audio" : "video"; + + if (data.streams != null) + { + var tagStream = data.streams.FirstOrDefault(i => string.Equals(i.codec_type, tagStreamType, StringComparison.OrdinalIgnoreCase)); + + if (tagStream != null && tagStream.tags != null) + { + foreach (var pair in tagStream.tags) + { + tags[pair.Key] = pair.Value; + } + } + } + + if (data.format != null && data.format.tags != null) + { + foreach (var pair in data.format.tags) + { + tags[pair.Key] = pair.Value; + } + } + + FetchGenres(info, tags); + var overview = FFProbeHelpers.GetDictionaryValue(tags, "synopsis"); + + if (string.IsNullOrWhiteSpace(overview)) + { + overview = FFProbeHelpers.GetDictionaryValue(tags, "description"); + } + if (string.IsNullOrWhiteSpace(overview)) + { + overview = FFProbeHelpers.GetDictionaryValue(tags, "desc"); + } + + if (!string.IsNullOrWhiteSpace(overview)) + { + info.Overview = overview; + } + + var title = FFProbeHelpers.GetDictionaryValue(tags, "title"); + if (!string.IsNullOrWhiteSpace(title)) + { + info.Name = title; + } + + info.ProductionYear = FFProbeHelpers.GetDictionaryNumericValue(tags, "date"); + + // Several different forms of retaildate + info.PremiereDate = FFProbeHelpers.GetDictionaryDateTime(tags, "retaildate") ?? + FFProbeHelpers.GetDictionaryDateTime(tags, "retail date") ?? + FFProbeHelpers.GetDictionaryDateTime(tags, "retail_date") ?? + FFProbeHelpers.GetDictionaryDateTime(tags, "date"); + + if (isAudio) + { + SetAudioRuntimeTicks(data, info); + + // tags are normally located under data.format, but we've seen some cases with ogg where they're part of the info stream + // so let's create a combined list of both + + SetAudioInfoFromTags(info, tags); + } + else + { + FetchStudios(info, tags, "copyright"); + + var iTunEXTC = FFProbeHelpers.GetDictionaryValue(tags, "iTunEXTC"); + if (!string.IsNullOrWhiteSpace(iTunEXTC)) + { + var parts = iTunEXTC.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries); + // Example + // mpaa|G|100|For crude humor + if (parts.Length > 1) + { + info.OfficialRating = parts[1]; + + if (parts.Length > 3) + { + info.OfficialRatingDescription = parts[3]; + } + } + } + + var itunesXml = FFProbeHelpers.GetDictionaryValue(tags, "iTunMOVI"); + if (!string.IsNullOrWhiteSpace(itunesXml)) + { + FetchFromItunesInfo(itunesXml, info); + } + + if (data.format != null && !string.IsNullOrEmpty(data.format.duration)) + { + info.RunTimeTicks = TimeSpan.FromSeconds(double.Parse(data.format.duration, _usCulture)).Ticks; + } + + FetchWtvInfo(info, data); + + if (data.Chapters != null) + { + info.Chapters = data.Chapters.Select(GetChapterInfo).ToArray(); + } + + ExtractTimestamp(info); + + var stereoMode = GetDictionaryValue(tags, "stereo_mode"); + if (string.Equals(stereoMode, "left_right", StringComparison.OrdinalIgnoreCase)) + { + info.Video3DFormat = Video3DFormat.FullSideBySide; + } + + foreach (var mediaStream in info.MediaStreams) + { + if (mediaStream.Type == MediaStreamType.Audio && !mediaStream.BitRate.HasValue) + { + mediaStream.BitRate = GetEstimatedAudioBitrate(mediaStream.Codec, mediaStream.Channels); + } + } + + var videoStreamsBitrate = info.MediaStreams.Where(i => i.Type == MediaStreamType.Video).Select(i => i.BitRate ?? 0).Sum(); + // If ffprobe reported the container bitrate as being the same as the video stream bitrate, then it's wrong + if (videoStreamsBitrate == (info.Bitrate ?? 0)) + { + info.InferTotalBitrate(true); + } + } + + return info; + } + + private string NormalizeFormat(string format) + { + if (string.IsNullOrWhiteSpace(format)) + { + return null; + } + + if (string.Equals(format, "mpegvideo", StringComparison.OrdinalIgnoreCase)) + { + return "mpeg"; + } + + format = format.Replace("matroska", "mkv", StringComparison.OrdinalIgnoreCase); + + return format; + } + + private int? GetEstimatedAudioBitrate(string codec, int? channels) + { + if (!channels.HasValue) + { + return null; + } + + var channelsValue = channels.Value; + + if (string.Equals(codec, "aac", StringComparison.OrdinalIgnoreCase) || + string.Equals(codec, "mp3", StringComparison.OrdinalIgnoreCase)) + { + if (channelsValue <= 2) + { + return 192000; + } + + if (channelsValue >= 5) + { + return 320000; + } + } + + return null; + } + + private void FetchFromItunesInfo(string xml, MediaInfo info) + { + // Make things simpler and strip out the dtd + var plistIndex = xml.IndexOf("" + xml; + + // \n\n\n\n\tcast\n\t\n\t\t\n\t\t\tname\n\t\t\tBlender Foundation\n\t\t\n\t\t\n\t\t\tname\n\t\t\tJanus Bager Kristensen\n\t\t\n\t\n\tdirectors\n\t\n\t\t\n\t\t\tname\n\t\t\tSacha Goedegebure\n\t\t\n\t\n\tstudio\n\tBlender Foundation\n\n\n + using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(xml))) + { + using (var streamReader = new StreamReader(stream)) + { + try + { + // Use XmlReader for best performance + using (var reader = XmlReader.Create(streamReader)) + { + reader.MoveToContent(); + reader.Read(); + + // Loop through each element + while (!reader.EOF && reader.ReadState == ReadState.Interactive) + { + if (reader.NodeType == XmlNodeType.Element) + { + switch (reader.Name) + { + case "dict": + if (reader.IsEmptyElement) + { + reader.Read(); + continue; + } + using (var subtree = reader.ReadSubtree()) + { + ReadFromDictNode(subtree, info); + } + break; + default: + reader.Skip(); + break; + } + } + else + { + reader.Read(); + } + } + } + } + catch (XmlException) + { + // I've seen probe examples where the iTunMOVI value is just "<" + // So we should not allow this to fail the entire probing operation + } + } + } + } + + private void ReadFromDictNode(XmlReader reader, MediaInfo info) + { + string currentKey = null; + List pairs = new List(); + + reader.MoveToContent(); + reader.Read(); + + // Loop through each element + while (!reader.EOF && reader.ReadState == ReadState.Interactive) + { + if (reader.NodeType == XmlNodeType.Element) + { + switch (reader.Name) + { + case "key": + if (!string.IsNullOrWhiteSpace(currentKey)) + { + ProcessPairs(currentKey, pairs, info); + } + currentKey = reader.ReadElementContentAsString(); + pairs = new List(); + break; + case "string": + var value = reader.ReadElementContentAsString(); + if (!string.IsNullOrWhiteSpace(value)) + { + pairs.Add(new NameValuePair + { + Name = value, + Value = value + }); + } + break; + case "array": + if (reader.IsEmptyElement) + { + reader.Read(); + continue; + } + using (var subtree = reader.ReadSubtree()) + { + if (!string.IsNullOrWhiteSpace(currentKey)) + { + pairs.AddRange(ReadValueArray(subtree)); + } + } + break; + default: + reader.Skip(); + break; + } + } + else + { + reader.Read(); + } + } + } + + private List ReadValueArray(XmlReader reader) + { + + List pairs = new List(); + + reader.MoveToContent(); + reader.Read(); + + // Loop through each element + while (!reader.EOF && reader.ReadState == ReadState.Interactive) + { + if (reader.NodeType == XmlNodeType.Element) + { + switch (reader.Name) + { + case "dict": + + if (reader.IsEmptyElement) + { + reader.Read(); + continue; + } + using (var subtree = reader.ReadSubtree()) + { + var dict = GetNameValuePair(subtree); + if (dict != null) + { + pairs.Add(dict); + } + } + break; + default: + reader.Skip(); + break; + } + } + else + { + reader.Read(); + } + } + + return pairs; + } + + private void ProcessPairs(string key, List pairs, MediaInfo info) + { + IList peoples = new List(); + if (string.Equals(key, "studio", StringComparison.OrdinalIgnoreCase)) + { + info.Studios = pairs.Select(p => p.Value) + .Where(i => !string.IsNullOrWhiteSpace(i)) + .Distinct(StringComparer.OrdinalIgnoreCase) + .ToArray(); + + } + else if (string.Equals(key, "screenwriters", StringComparison.OrdinalIgnoreCase)) + { + foreach (var pair in pairs) + { + peoples.Add(new BaseItemPerson + { + Name = pair.Value, + Type = PersonType.Writer + }); + } + + } + else if (string.Equals(key, "producers", StringComparison.OrdinalIgnoreCase)) + { + foreach (var pair in pairs) + { + peoples.Add(new BaseItemPerson + { + Name = pair.Value, + Type = PersonType.Producer + }); + } + } + else if (string.Equals(key, "directors", StringComparison.OrdinalIgnoreCase)) + { + foreach (var pair in pairs) + { + peoples.Add(new BaseItemPerson + { + Name = pair.Value, + Type = PersonType.Director + }); + } + } + + info.People = peoples.ToArray(); + } + + private NameValuePair GetNameValuePair(XmlReader reader) + { + string name = null; + string value = null; + + reader.MoveToContent(); + reader.Read(); + + // Loop through each element + while (!reader.EOF && reader.ReadState == ReadState.Interactive) + { + if (reader.NodeType == XmlNodeType.Element) + { + switch (reader.Name) + { + case "key": + name = reader.ReadElementContentAsString(); + break; + case "string": + value = reader.ReadElementContentAsString(); + break; + default: + reader.Skip(); + break; + } + } + else + { + reader.Read(); + } + } + + if (string.IsNullOrWhiteSpace(name) || + string.IsNullOrWhiteSpace(value)) + { + return null; + } + + return new NameValuePair + { + Name = name, + Value = value + }; + } + + private string NormalizeSubtitleCodec(string codec) + { + if (string.Equals(codec, "dvb_subtitle", StringComparison.OrdinalIgnoreCase)) + { + codec = "dvbsub"; + } + else if ((codec ?? string.Empty).IndexOf("PGS", StringComparison.OrdinalIgnoreCase) != -1) + { + codec = "PGSSUB"; + } + else if ((codec ?? string.Empty).IndexOf("DVD", StringComparison.OrdinalIgnoreCase) != -1) + { + codec = "DVDSUB"; + } + + return codec; + } + + /// + /// Converts ffprobe stream info to our MediaStream class + /// + /// if set to true [is info]. + /// The stream info. + /// The format info. + /// MediaStream. + private MediaStream GetMediaStream(bool isAudio, MediaStreamInfo streamInfo, MediaFormatInfo formatInfo) + { + // These are mp4 chapters + if (string.Equals(streamInfo.codec_name, "mov_text", StringComparison.OrdinalIgnoreCase)) + { + // Edit: but these are also sometimes subtitles? + //return null; + } + + var stream = new MediaStream + { + Codec = streamInfo.codec_name, + Profile = streamInfo.profile, + Level = streamInfo.level, + Index = streamInfo.index, + PixelFormat = streamInfo.pix_fmt, + NalLengthSize = streamInfo.nal_length_size, + TimeBase = streamInfo.time_base, + CodecTimeBase = streamInfo.codec_time_base + }; + + if (string.Equals(streamInfo.is_avc, "true", StringComparison.OrdinalIgnoreCase) || + string.Equals(streamInfo.is_avc, "1", StringComparison.OrdinalIgnoreCase)) + { + stream.IsAVC = true; + } + else if (string.Equals(streamInfo.is_avc, "false", StringComparison.OrdinalIgnoreCase) || + string.Equals(streamInfo.is_avc, "0", StringComparison.OrdinalIgnoreCase)) + { + stream.IsAVC = false; + } + + if (!string.IsNullOrWhiteSpace(streamInfo.field_order) && !string.Equals(streamInfo.field_order, "progressive", StringComparison.OrdinalIgnoreCase)) + { + stream.IsInterlaced = true; + } + + // Filter out junk + if (!string.IsNullOrWhiteSpace(streamInfo.codec_tag_string) && streamInfo.codec_tag_string.IndexOf("[0]", StringComparison.OrdinalIgnoreCase) == -1) + { + stream.CodecTag = streamInfo.codec_tag_string; + } + + if (streamInfo.tags != null) + { + stream.Language = GetDictionaryValue(streamInfo.tags, "language"); + stream.Comment = GetDictionaryValue(streamInfo.tags, "comment"); + stream.Title = GetDictionaryValue(streamInfo.tags, "title"); + } + + if (string.Equals(streamInfo.codec_type, "audio", StringComparison.OrdinalIgnoreCase)) + { + stream.Type = MediaStreamType.Audio; + + stream.Channels = streamInfo.channels; + + if (!string.IsNullOrEmpty(streamInfo.sample_rate)) + { + int value; + if (int.TryParse(streamInfo.sample_rate, NumberStyles.Any, _usCulture, out value)) + { + stream.SampleRate = value; + } + } + + stream.ChannelLayout = ParseChannelLayout(streamInfo.channel_layout); + + if (streamInfo.bits_per_sample > 0) + { + stream.BitDepth = streamInfo.bits_per_sample; + } + else if (streamInfo.bits_per_raw_sample > 0) + { + stream.BitDepth = streamInfo.bits_per_raw_sample; + } + } + else if (string.Equals(streamInfo.codec_type, "subtitle", StringComparison.OrdinalIgnoreCase)) + { + stream.Type = MediaStreamType.Subtitle; + stream.Codec = NormalizeSubtitleCodec(stream.Codec); + } + else if (string.Equals(streamInfo.codec_type, "video", StringComparison.OrdinalIgnoreCase)) + { + stream.Type = isAudio || string.Equals(stream.Codec, "mjpeg", StringComparison.OrdinalIgnoreCase) || string.Equals(stream.Codec, "gif", StringComparison.OrdinalIgnoreCase) || string.Equals(stream.Codec, "png", StringComparison.OrdinalIgnoreCase) + ? MediaStreamType.EmbeddedImage + : MediaStreamType.Video; + + stream.AverageFrameRate = GetFrameRate(streamInfo.avg_frame_rate); + stream.RealFrameRate = GetFrameRate(streamInfo.r_frame_rate); + + if (isAudio || string.Equals(stream.Codec, "gif", StringComparison.OrdinalIgnoreCase) || + string.Equals(stream.Codec, "png", StringComparison.OrdinalIgnoreCase)) + { + stream.Type = MediaStreamType.EmbeddedImage; + } + else if (string.Equals(stream.Codec, "mjpeg", StringComparison.OrdinalIgnoreCase)) + { + // How to differentiate between video and embedded image? + // The only difference I've seen thus far is presence of codec tag, also embedded images have high (unusual) framerates + if (!string.IsNullOrWhiteSpace(stream.CodecTag)) + { + stream.Type = MediaStreamType.Video; + } + else + { + stream.Type = MediaStreamType.EmbeddedImage; + } + } + else + { + stream.Type = MediaStreamType.Video; + } + + stream.Width = streamInfo.width; + stream.Height = streamInfo.height; + stream.AspectRatio = GetAspectRatio(streamInfo); + + if (streamInfo.bits_per_sample > 0) + { + stream.BitDepth = streamInfo.bits_per_sample; + } + else if (streamInfo.bits_per_raw_sample > 0) + { + stream.BitDepth = streamInfo.bits_per_raw_sample; + } + + //stream.IsAnamorphic = string.Equals(streamInfo.sample_aspect_ratio, "0:1", StringComparison.OrdinalIgnoreCase) || + // string.Equals(stream.AspectRatio, "2.35:1", StringComparison.OrdinalIgnoreCase) || + // string.Equals(stream.AspectRatio, "2.40:1", StringComparison.OrdinalIgnoreCase); + + // http://stackoverflow.com/questions/17353387/how-to-detect-anamorphic-video-with-ffprobe + stream.IsAnamorphic = string.Equals(streamInfo.sample_aspect_ratio, "0:1", StringComparison.OrdinalIgnoreCase); + + if (streamInfo.refs > 0) + { + stream.RefFrames = streamInfo.refs; + } + } + else + { + return null; + } + + // Get stream bitrate + var bitrate = 0; + + if (!string.IsNullOrEmpty(streamInfo.bit_rate)) + { + int value; + if (int.TryParse(streamInfo.bit_rate, NumberStyles.Any, _usCulture, out value)) + { + bitrate = value; + } + } + + if (bitrate == 0 && formatInfo != null && !string.IsNullOrEmpty(formatInfo.bit_rate) && stream.Type == MediaStreamType.Video) + { + // If the stream info doesn't have a bitrate get the value from the media format info + int value; + if (int.TryParse(formatInfo.bit_rate, NumberStyles.Any, _usCulture, out value)) + { + bitrate = value; + } + } + + if (bitrate > 0) + { + stream.BitRate = bitrate; + } + + if (streamInfo.disposition != null) + { + var isDefault = GetDictionaryValue(streamInfo.disposition, "default"); + var isForced = GetDictionaryValue(streamInfo.disposition, "forced"); + + stream.IsDefault = string.Equals(isDefault, "1", StringComparison.OrdinalIgnoreCase); + + stream.IsForced = string.Equals(isForced, "1", StringComparison.OrdinalIgnoreCase); + } + + NormalizeStreamTitle(stream); + + return stream; + } + + private void NormalizeStreamTitle(MediaStream stream) + { + if (string.Equals(stream.Title, "cc", StringComparison.OrdinalIgnoreCase)) + { + stream.Title = null; + } + + if (stream.Type == MediaStreamType.EmbeddedImage) + { + stream.Title = null; + } + } + + /// + /// Gets a string from an FFProbeResult tags dictionary + /// + /// The tags. + /// The key. + /// System.String. + private string GetDictionaryValue(Dictionary tags, string key) + { + if (tags == null) + { + return null; + } + + string val; + + tags.TryGetValue(key, out val); + return val; + } + + private string ParseChannelLayout(string input) + { + if (string.IsNullOrEmpty(input)) + { + return input; + } + + return input.Split('(').FirstOrDefault(); + } + + private string GetAspectRatio(MediaStreamInfo info) + { + var original = info.display_aspect_ratio; + + int height; + int width; + + var parts = (original ?? string.Empty).Split(':'); + if (!(parts.Length == 2 && + int.TryParse(parts[0], NumberStyles.Any, _usCulture, out width) && + int.TryParse(parts[1], NumberStyles.Any, _usCulture, out height) && + width > 0 && + height > 0)) + { + width = info.width; + height = info.height; + } + + if (width > 0 && height > 0) + { + double ratio = width; + ratio /= height; + + if (IsClose(ratio, 1.777777778, .03)) + { + return "16:9"; + } + + if (IsClose(ratio, 1.3333333333, .05)) + { + return "4:3"; + } + + if (IsClose(ratio, 1.41)) + { + return "1.41:1"; + } + + if (IsClose(ratio, 1.5)) + { + return "1.5:1"; + } + + if (IsClose(ratio, 1.6)) + { + return "1.6:1"; + } + + if (IsClose(ratio, 1.66666666667)) + { + return "5:3"; + } + + if (IsClose(ratio, 1.85, .02)) + { + return "1.85:1"; + } + + if (IsClose(ratio, 2.35, .025)) + { + return "2.35:1"; + } + + if (IsClose(ratio, 2.4, .025)) + { + return "2.40:1"; + } + } + + return original; + } + + private bool IsClose(double d1, double d2, double variance = .005) + { + return Math.Abs(d1 - d2) <= variance; + } + + /// + /// Gets a frame rate from a string value in ffprobe output + /// This could be a number or in the format of 2997/125. + /// + /// The value. + /// System.Nullable{System.Single}. + private float? GetFrameRate(string value) + { + if (!string.IsNullOrEmpty(value)) + { + var parts = value.Split('/'); + + float result; + + if (parts.Length == 2) + { + result = float.Parse(parts[0], _usCulture) / float.Parse(parts[1], _usCulture); + } + else + { + result = float.Parse(parts[0], _usCulture); + } + + return float.IsNaN(result) ? (float?)null : result; + } + + return null; + } + + private void SetAudioRuntimeTicks(InternalMediaInfoResult result, MediaInfo data) + { + if (result.streams != null) + { + // Get the first info stream + var stream = result.streams.FirstOrDefault(s => string.Equals(s.codec_type, "audio", StringComparison.OrdinalIgnoreCase)); + + if (stream != null) + { + // Get duration from stream properties + var duration = stream.duration; + + // If it's not there go into format properties + if (string.IsNullOrEmpty(duration)) + { + duration = result.format.duration; + } + + // If we got something, parse it + if (!string.IsNullOrEmpty(duration)) + { + data.RunTimeTicks = TimeSpan.FromSeconds(double.Parse(duration, _usCulture)).Ticks; + } + } + } + } + + private void SetSize(InternalMediaInfoResult data, Model.MediaInfo.MediaInfo info) + { + if (data.format != null) + { + if (!string.IsNullOrEmpty(data.format.size)) + { + info.Size = long.Parse(data.format.size, _usCulture); + } + else + { + info.Size = null; + } + } + } + + private void SetAudioInfoFromTags(MediaInfo audio, Dictionary tags) + { + var composer = FFProbeHelpers.GetDictionaryValue(tags, "composer"); + if (!string.IsNullOrWhiteSpace(composer)) + { + List peoples = new List(); + foreach (var person in Split(composer, false)) + { + peoples.Add(new BaseItemPerson { Name = person, Type = PersonType.Composer }); + } + audio.People = peoples.ToArray(); + } + + //var conductor = FFProbeHelpers.GetDictionaryValue(tags, "conductor"); + //if (!string.IsNullOrWhiteSpace(conductor)) + //{ + // foreach (var person in Split(conductor, false)) + // { + // audio.People.Add(new BaseItemPerson { Name = person, Type = PersonType.Conductor }); + // } + //} + + //var lyricist = FFProbeHelpers.GetDictionaryValue(tags, "lyricist"); + //if (!string.IsNullOrWhiteSpace(lyricist)) + //{ + // foreach (var person in Split(lyricist, false)) + // { + // audio.People.Add(new BaseItemPerson { Name = person, Type = PersonType.Lyricist }); + // } + //} + + // Check for writer some music is tagged that way as alternative to composer/lyricist + var writer = FFProbeHelpers.GetDictionaryValue(tags, "writer"); + + if (!string.IsNullOrWhiteSpace(writer)) + { + List peoples = new List(); + foreach (var person in Split(writer, false)) + { + peoples.Add(new BaseItemPerson { Name = person, Type = PersonType.Writer }); + } + audio.People = peoples.ToArray(); + } + + audio.Album = FFProbeHelpers.GetDictionaryValue(tags, "album"); + + var artists = FFProbeHelpers.GetDictionaryValue(tags, "artists"); + + if (!string.IsNullOrWhiteSpace(artists)) + { + audio.Artists = SplitArtists(artists, new[] { '/', ';' }, false) + .DistinctNames() + .ToArray(); + } + else + { + var artist = FFProbeHelpers.GetDictionaryValue(tags, "artist"); + if (string.IsNullOrWhiteSpace(artist)) + { + audio.Artists = new string[] {}; + } + else + { + audio.Artists = SplitArtists(artist, _nameDelimiters, true) + .DistinctNames() + .ToArray(); + } + } + + var albumArtist = FFProbeHelpers.GetDictionaryValue(tags, "albumartist"); + if (string.IsNullOrWhiteSpace(albumArtist)) + { + albumArtist = FFProbeHelpers.GetDictionaryValue(tags, "album artist"); + } + if (string.IsNullOrWhiteSpace(albumArtist)) + { + albumArtist = FFProbeHelpers.GetDictionaryValue(tags, "album_artist"); + } + + if (string.IsNullOrWhiteSpace(albumArtist)) + { + audio.AlbumArtists = new string[] {}; + } + else + { + audio.AlbumArtists = SplitArtists(albumArtist, _nameDelimiters, true) + .DistinctNames() + .ToArray(); + + } + + if (audio.AlbumArtists.Length == 0) + { + audio.AlbumArtists = audio.Artists; + } + + // Track number + audio.IndexNumber = GetDictionaryDiscValue(tags, "track"); + + // Disc number + audio.ParentIndexNumber = GetDictionaryDiscValue(tags, "disc"); + + // If we don't have a ProductionYear try and get it from PremiereDate + if (audio.PremiereDate.HasValue && !audio.ProductionYear.HasValue) + { + audio.ProductionYear = audio.PremiereDate.Value.ToLocalTime().Year; + } + + // There's several values in tags may or may not be present + FetchStudios(audio, tags, "organization"); + FetchStudios(audio, tags, "ensemble"); + FetchStudios(audio, tags, "publisher"); + FetchStudios(audio, tags, "label"); + + // These support mulitple values, but for now we only store the first. + var mb = GetMultipleMusicBrainzId(FFProbeHelpers.GetDictionaryValue(tags, "MusicBrainz Album Artist Id")); + if (mb == null) mb = GetMultipleMusicBrainzId(FFProbeHelpers.GetDictionaryValue(tags, "MUSICBRAINZ_ALBUMARTISTID")); + audio.SetProviderId(MetadataProviders.MusicBrainzAlbumArtist, mb); + + mb = GetMultipleMusicBrainzId(FFProbeHelpers.GetDictionaryValue(tags, "MusicBrainz Artist Id")); + if (mb == null) mb = GetMultipleMusicBrainzId(FFProbeHelpers.GetDictionaryValue(tags, "MUSICBRAINZ_ARTISTID")); + audio.SetProviderId(MetadataProviders.MusicBrainzArtist, mb); + + mb = GetMultipleMusicBrainzId(FFProbeHelpers.GetDictionaryValue(tags, "MusicBrainz Album Id")); + if (mb == null) mb = GetMultipleMusicBrainzId(FFProbeHelpers.GetDictionaryValue(tags, "MUSICBRAINZ_ALBUMID")); + audio.SetProviderId(MetadataProviders.MusicBrainzAlbum, mb); + + mb = GetMultipleMusicBrainzId(FFProbeHelpers.GetDictionaryValue(tags, "MusicBrainz Release Group Id")); + if (mb == null) mb = GetMultipleMusicBrainzId(FFProbeHelpers.GetDictionaryValue(tags, "MUSICBRAINZ_RELEASEGROUPID")); + audio.SetProviderId(MetadataProviders.MusicBrainzReleaseGroup, mb); + + mb = GetMultipleMusicBrainzId(FFProbeHelpers.GetDictionaryValue(tags, "MusicBrainz Release Track Id")); + if (mb == null) mb = GetMultipleMusicBrainzId(FFProbeHelpers.GetDictionaryValue(tags, "MUSICBRAINZ_RELEASETRACKID")); + audio.SetProviderId(MetadataProviders.MusicBrainzTrack, mb); + } + + private string GetMultipleMusicBrainzId(string value) + { + if (string.IsNullOrWhiteSpace(value)) + { + return null; + } + + return value.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries) + .Select(i => i.Trim()) + .FirstOrDefault(i => !string.IsNullOrWhiteSpace(i)); + } + + private readonly char[] _nameDelimiters = { '/', '|', ';', '\\' }; + + /// + /// Splits the specified val. + /// + /// The val. + /// if set to true [allow comma delimiter]. + /// System.String[][]. + private IEnumerable Split(string val, bool allowCommaDelimiter) + { + // Only use the comma as a delimeter if there are no slashes or pipes. + // We want to be careful not to split names that have commas in them + var delimeter = !allowCommaDelimiter || _nameDelimiters.Any(i => val.IndexOf(i) != -1) ? + _nameDelimiters : + new[] { ',' }; + + return val.Split(delimeter, StringSplitOptions.RemoveEmptyEntries) + .Where(i => !string.IsNullOrWhiteSpace(i)) + .Select(i => i.Trim()); + } + + private const string ArtistReplaceValue = " | "; + + private IEnumerable SplitArtists(string val, char[] delimiters, bool splitFeaturing) + { + if (splitFeaturing) + { + val = val.Replace(" featuring ", ArtistReplaceValue, StringComparison.OrdinalIgnoreCase) + .Replace(" feat. ", ArtistReplaceValue, StringComparison.OrdinalIgnoreCase); + } + + var artistsFound = new List(); + + foreach (var whitelistArtist in GetSplitWhitelist()) + { + var originalVal = val; + val = val.Replace(whitelistArtist, "|", StringComparison.OrdinalIgnoreCase); + + if (!string.Equals(originalVal, val, StringComparison.OrdinalIgnoreCase)) + { + artistsFound.Add(whitelistArtist); + } + } + + var artists = val.Split(delimiters, StringSplitOptions.RemoveEmptyEntries) + .Where(i => !string.IsNullOrWhiteSpace(i)) + .Select(i => i.Trim()); + + artistsFound.AddRange(artists); + return artistsFound; + } + + + private List _splitWhiteList = null; + + private IEnumerable GetSplitWhitelist() + { + if (_splitWhiteList == null) + { + _splitWhiteList = new List + { + "AC/DC" + }; + } + + return _splitWhiteList; + } + + /// + /// Gets the studios from the tags collection + /// + /// The info. + /// The tags. + /// Name of the tag. + private void FetchStudios(MediaInfo info, Dictionary tags, string tagName) + { + var val = FFProbeHelpers.GetDictionaryValue(tags, tagName); + + if (!string.IsNullOrEmpty(val)) + { + var studios = Split(val, true); + List studioList = new List(); + + foreach (var studio in studios) + { + // Sometimes the artist name is listed here, account for that + if (info.Artists.Contains(studio, StringComparer.OrdinalIgnoreCase)) + { + continue; + } + if (info.AlbumArtists.Contains(studio, StringComparer.OrdinalIgnoreCase)) + { + continue; + } + + studioList.Add(studio); + } + + info.Studios = studioList + .Where(i => !string.IsNullOrWhiteSpace(i)) + .Distinct(StringComparer.OrdinalIgnoreCase) + .ToArray(); + } + } + + /// + /// Gets the genres from the tags collection + /// + /// The information. + /// The tags. + private void FetchGenres(MediaInfo info, Dictionary tags) + { + var val = FFProbeHelpers.GetDictionaryValue(tags, "genre"); + + if (!string.IsNullOrEmpty(val)) + { + List genres = new List(info.Genres); + foreach (var genre in Split(val, true)) + { + genres.Add(genre); + } + + info.Genres = genres + .Where(i => !string.IsNullOrWhiteSpace(i)) + .Distinct(StringComparer.OrdinalIgnoreCase) + .ToArray(); + } + } + + /// + /// Gets the disc number, which is sometimes can be in the form of '1', or '1/3' + /// + /// The tags. + /// Name of the tag. + /// System.Nullable{System.Int32}. + private int? GetDictionaryDiscValue(Dictionary tags, string tagName) + { + var disc = FFProbeHelpers.GetDictionaryValue(tags, tagName); + + if (!string.IsNullOrEmpty(disc)) + { + disc = disc.Split('/')[0]; + + int num; + + if (int.TryParse(disc, out num)) + { + return num; + } + } + + return null; + } + + private ChapterInfo GetChapterInfo(MediaChapter chapter) + { + var info = new ChapterInfo(); + + if (chapter.tags != null) + { + string name; + if (chapter.tags.TryGetValue("title", out name)) + { + info.Name = name; + } + } + + // Limit accuracy to milliseconds to match xml saving + var secondsString = chapter.start_time; + double seconds; + + if (double.TryParse(secondsString, NumberStyles.Any, CultureInfo.InvariantCulture, out seconds)) + { + var ms = Math.Round(TimeSpan.FromSeconds(seconds).TotalMilliseconds); + info.StartPositionTicks = TimeSpan.FromMilliseconds(ms).Ticks; + } + + return info; + } + + private const int MaxSubtitleDescriptionExtractionLength = 100; // When extracting subtitles, the maximum length to consider (to avoid invalid filenames) + + private void FetchWtvInfo(MediaInfo video, InternalMediaInfoResult data) + { + if (data.format == null || data.format.tags == null) + { + return; + } + + var genres = FFProbeHelpers.GetDictionaryValue(data.format.tags, "WM/Genre"); + + if (!string.IsNullOrWhiteSpace(genres)) + { + var genreList = genres.Split(new[] { ';', '/', ',' }, StringSplitOptions.RemoveEmptyEntries) + .Where(i => !string.IsNullOrWhiteSpace(i)) + .Select(i => i.Trim()) + .ToList(); + + // If this is empty then don't overwrite genres that might have been fetched earlier + if (genreList.Count > 0) + { + video.Genres = genreList.ToArray(); + } + } + + var officialRating = FFProbeHelpers.GetDictionaryValue(data.format.tags, "WM/ParentalRating"); + + if (!string.IsNullOrWhiteSpace(officialRating)) + { + video.OfficialRating = officialRating; + } + + var people = FFProbeHelpers.GetDictionaryValue(data.format.tags, "WM/MediaCredits"); + + if (!string.IsNullOrEmpty(people)) + { + video.People = people.Split(new[] { ';', '/' }, StringSplitOptions.RemoveEmptyEntries) + .Where(i => !string.IsNullOrWhiteSpace(i)) + .Select(i => new BaseItemPerson { Name = i.Trim(), Type = PersonType.Actor }) + .ToArray(); + } + + var year = FFProbeHelpers.GetDictionaryValue(data.format.tags, "WM/OriginalReleaseTime"); + if (!string.IsNullOrWhiteSpace(year)) + { + int val; + + if (int.TryParse(year, NumberStyles.Integer, _usCulture, out val)) + { + video.ProductionYear = val; + } + } + + var premiereDateString = FFProbeHelpers.GetDictionaryValue(data.format.tags, "WM/MediaOriginalBroadcastDateTime"); + if (!string.IsNullOrWhiteSpace(premiereDateString)) + { + DateTime val; + + // Credit to MCEBuddy: https://mcebuddy2x.codeplex.com/ + // DateTime is reported along with timezone info (typically Z i.e. UTC hence assume None) + if (DateTime.TryParse(year, null, DateTimeStyles.None, out val)) + { + video.PremiereDate = val.ToUniversalTime(); + } + } + + var description = FFProbeHelpers.GetDictionaryValue(data.format.tags, "WM/SubTitleDescription"); + + var subTitle = FFProbeHelpers.GetDictionaryValue(data.format.tags, "WM/SubTitle"); + + // For below code, credit to MCEBuddy: https://mcebuddy2x.codeplex.com/ + + // Sometimes for TV Shows the Subtitle field is empty and the subtitle description contains the subtitle, extract if possible. See ticket https://mcebuddy2x.codeplex.com/workitem/1910 + // The format is -> EPISODE/TOTAL_EPISODES_IN_SEASON. SUBTITLE: DESCRIPTION + // OR -> COMMENT. SUBTITLE: DESCRIPTION + // e.g. -> 4/13. The Doctor's Wife: Science fiction drama. When he follows a Time Lord distress signal, the Doctor puts Amy, Rory and his beloved TARDIS in grave danger. Also in HD. [AD,S] + // e.g. -> CBeebies Bedtime Hour. The Mystery: Animated adventures of two friends who live on an island in the middle of the big city. Some of Abney and Teal's favourite objects are missing. [S] + if (String.IsNullOrWhiteSpace(subTitle) && !String.IsNullOrWhiteSpace(description) && description.Substring(0, Math.Min(description.Length, MaxSubtitleDescriptionExtractionLength)).Contains(":")) // Check within the Subtitle size limit, otherwise from description it can get too long creating an invalid filename + { + string[] parts = description.Split(':'); + if (parts.Length > 0) + { + string subtitle = parts[0]; + try + { + if (subtitle.Contains("/")) // It contains a episode number and season number + { + string[] numbers = subtitle.Split(' '); + video.IndexNumber = int.Parse(numbers[0].Replace(".", "").Split('/')[0]); + int totalEpisodesInSeason = int.Parse(numbers[0].Replace(".", "").Split('/')[1]); + + description = String.Join(" ", numbers, 1, numbers.Length - 1).Trim(); // Skip the first, concatenate the rest, clean up spaces and save it + } + else + throw new Exception(); // Switch to default parsing + } + catch // Default parsing + { + if (subtitle.Contains(".")) // skip the comment, keep the subtitle + description = String.Join(".", subtitle.Split('.'), 1, subtitle.Split('.').Length - 1).Trim(); // skip the first + else + description = subtitle.Trim(); // Clean up whitespaces and save it + } + } + } + + if (!string.IsNullOrWhiteSpace(description)) + { + video.Overview = description; + } + } + + private void ExtractTimestamp(MediaInfo video) + { + if (video.VideoType == VideoType.VideoFile) + { + if (string.Equals(video.Container, "mpeg2ts", StringComparison.OrdinalIgnoreCase) || + string.Equals(video.Container, "m2ts", StringComparison.OrdinalIgnoreCase) || + string.Equals(video.Container, "ts", StringComparison.OrdinalIgnoreCase)) + { + try + { + video.Timestamp = GetMpegTimestamp(video.Path); + + _logger.Debug("Video has {0} timestamp", video.Timestamp); + } + catch (Exception ex) + { + _logger.ErrorException("Error extracting timestamp info from {0}", ex, video.Path); + video.Timestamp = null; + } + } + } + } + + private TransportStreamTimestamp GetMpegTimestamp(string path) + { + var packetBuffer = new byte['Å']; + + using (var fs = _fileSystem.GetFileStream(path, FileOpenMode.Open, FileAccessMode.Read, FileShareMode.Read)) + { + fs.Read(packetBuffer, 0, packetBuffer.Length); + } + + if (packetBuffer[0] == 71) + { + return TransportStreamTimestamp.None; + } + + if ((packetBuffer[4] == 71) && (packetBuffer['Ä'] == 71)) + { + if ((packetBuffer[0] == 0) && (packetBuffer[1] == 0) && (packetBuffer[2] == 0) && (packetBuffer[3] == 0)) + { + return TransportStreamTimestamp.Zero; + } + + return TransportStreamTimestamp.Valid; + } + + return TransportStreamTimestamp.None; + } + } +} diff --git a/MediaBrowser.MediaEncoding/Properties/AssemblyInfo.cs b/MediaBrowser.MediaEncoding/Properties/AssemblyInfo.cs new file mode 100644 index 0000000000..53f4eb4035 --- /dev/null +++ b/MediaBrowser.MediaEncoding/Properties/AssemblyInfo.cs @@ -0,0 +1,33 @@ +using System.Reflection; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("MediaBrowser.MediaEncoding")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("MediaBrowser.MediaEncoding")] +[assembly: AssemblyCopyright("Copyright © 2014")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("05f49ab9-2a90-4332-9d41-7817a9cccd90")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] \ No newline at end of file diff --git a/MediaBrowser.MediaEncoding/Subtitles/AssParser.cs b/MediaBrowser.MediaEncoding/Subtitles/AssParser.cs new file mode 100644 index 0000000000..71fefba44a --- /dev/null +++ b/MediaBrowser.MediaEncoding/Subtitles/AssParser.cs @@ -0,0 +1,122 @@ +using MediaBrowser.Model.Extensions; +using System; +using System.Collections.Generic; +using System.Globalization; +using System.IO; +using System.Linq; +using System.Text.RegularExpressions; +using System.Threading; +using MediaBrowser.Model.MediaInfo; + +namespace MediaBrowser.MediaEncoding.Subtitles +{ + public class AssParser : ISubtitleParser + { + private readonly CultureInfo _usCulture = new CultureInfo("en-US"); + + public SubtitleTrackInfo Parse(Stream stream, CancellationToken cancellationToken) + { + var trackInfo = new SubtitleTrackInfo(); + List trackEvents = new List(); + var eventIndex = 1; + using (var reader = new StreamReader(stream)) + { + string line; + while (reader.ReadLine() != "[Events]") + {} + var headers = ParseFieldHeaders(reader.ReadLine()); + + while ((line = reader.ReadLine()) != null) + { + cancellationToken.ThrowIfCancellationRequested(); + + if (string.IsNullOrWhiteSpace(line)) + { + continue; + } + if(line.StartsWith("[")) + break; + if(string.IsNullOrEmpty(line)) + continue; + var subEvent = new SubtitleTrackEvent { Id = eventIndex.ToString(_usCulture) }; + eventIndex++; + var sections = line.Substring(10).Split(','); + + subEvent.StartPositionTicks = GetTicks(sections[headers["Start"]]); + subEvent.EndPositionTicks = GetTicks(sections[headers["End"]]); + + subEvent.Text = string.Join(",", sections.Skip(headers["Text"])); + RemoteNativeFormatting(subEvent); + + subEvent.Text = subEvent.Text.Replace("\\n", ParserValues.NewLine, StringComparison.OrdinalIgnoreCase); + + subEvent.Text = Regex.Replace(subEvent.Text, @"\{(\\[\w]+\(?([\w\d]+,?)+\)?)+\}", string.Empty, RegexOptions.IgnoreCase); + + trackEvents.Add(subEvent); + } + } + trackInfo.TrackEvents = trackEvents.ToArray(); + return trackInfo; + } + + long GetTicks(string time) + { + TimeSpan span; + return TimeSpan.TryParseExact(time, @"h\:mm\:ss\.ff", _usCulture, out span) + ? span.Ticks: 0; + } + + private Dictionary ParseFieldHeaders(string line) { + var fields = line.Substring(8).Split(',').Select(x=>x.Trim()).ToList(); + + var result = new Dictionary { + {"Start", fields.IndexOf("Start")}, + {"End", fields.IndexOf("End")}, + {"Text", fields.IndexOf("Text")} + }; + return result; + } + + /// + /// Credit: https://github.com/SubtitleEdit/subtitleedit/blob/master/src/Logic/SubtitleFormats/AdvancedSubStationAlpha.cs + /// + private void RemoteNativeFormatting(SubtitleTrackEvent p) + { + int indexOfBegin = p.Text.IndexOf('{'); + string pre = string.Empty; + while (indexOfBegin >= 0 && p.Text.IndexOf('}') > indexOfBegin) + { + string s = p.Text.Substring(indexOfBegin); + if (s.StartsWith("{\\an1}", StringComparison.Ordinal) || + s.StartsWith("{\\an2}", StringComparison.Ordinal) || + s.StartsWith("{\\an3}", StringComparison.Ordinal) || + s.StartsWith("{\\an4}", StringComparison.Ordinal) || + s.StartsWith("{\\an5}", StringComparison.Ordinal) || + s.StartsWith("{\\an6}", StringComparison.Ordinal) || + s.StartsWith("{\\an7}", StringComparison.Ordinal) || + s.StartsWith("{\\an8}", StringComparison.Ordinal) || + s.StartsWith("{\\an9}", StringComparison.Ordinal)) + { + pre = s.Substring(0, 6); + } + else if (s.StartsWith("{\\an1\\", StringComparison.Ordinal) || + s.StartsWith("{\\an2\\", StringComparison.Ordinal) || + s.StartsWith("{\\an3\\", StringComparison.Ordinal) || + s.StartsWith("{\\an4\\", StringComparison.Ordinal) || + s.StartsWith("{\\an5\\", StringComparison.Ordinal) || + s.StartsWith("{\\an6\\", StringComparison.Ordinal) || + s.StartsWith("{\\an7\\", StringComparison.Ordinal) || + s.StartsWith("{\\an8\\", StringComparison.Ordinal) || + s.StartsWith("{\\an9\\", StringComparison.Ordinal)) + { + pre = s.Substring(0, 5) + "}"; + } + int indexOfEnd = p.Text.IndexOf('}'); + p.Text = p.Text.Remove(indexOfBegin, (indexOfEnd - indexOfBegin) + 1); + + indexOfBegin = p.Text.IndexOf('{'); + } + p.Text = pre + p.Text; + } + } +} diff --git a/MediaBrowser.MediaEncoding/Subtitles/ConfigurationExtension.cs b/MediaBrowser.MediaEncoding/Subtitles/ConfigurationExtension.cs new file mode 100644 index 0000000000..973c653a47 --- /dev/null +++ b/MediaBrowser.MediaEncoding/Subtitles/ConfigurationExtension.cs @@ -0,0 +1,29 @@ +using System.Collections.Generic; +using MediaBrowser.Common.Configuration; +using MediaBrowser.Model.Providers; + +namespace MediaBrowser.MediaEncoding.Subtitles +{ + public static class ConfigurationExtension + { + public static SubtitleOptions GetSubtitleConfiguration(this IConfigurationManager manager) + { + return manager.GetConfiguration("subtitles"); + } + } + + public class SubtitleConfigurationFactory : IConfigurationFactory + { + public IEnumerable GetConfigurations() + { + return new List + { + new ConfigurationStore + { + Key = "subtitles", + ConfigurationType = typeof (SubtitleOptions) + } + }; + } + } +} diff --git a/MediaBrowser.MediaEncoding/Subtitles/ISubtitleParser.cs b/MediaBrowser.MediaEncoding/Subtitles/ISubtitleParser.cs new file mode 100644 index 0000000000..75de81f461 --- /dev/null +++ b/MediaBrowser.MediaEncoding/Subtitles/ISubtitleParser.cs @@ -0,0 +1,17 @@ +using System.IO; +using System.Threading; +using MediaBrowser.Model.MediaInfo; + +namespace MediaBrowser.MediaEncoding.Subtitles +{ + public interface ISubtitleParser + { + /// + /// Parses the specified stream. + /// + /// The stream. + /// The cancellation token. + /// SubtitleTrackInfo. + SubtitleTrackInfo Parse(Stream stream, CancellationToken cancellationToken); + } +} diff --git a/MediaBrowser.MediaEncoding/Subtitles/ISubtitleWriter.cs b/MediaBrowser.MediaEncoding/Subtitles/ISubtitleWriter.cs new file mode 100644 index 0000000000..e28da91857 --- /dev/null +++ b/MediaBrowser.MediaEncoding/Subtitles/ISubtitleWriter.cs @@ -0,0 +1,20 @@ +using System.IO; +using System.Threading; +using MediaBrowser.Model.MediaInfo; + +namespace MediaBrowser.MediaEncoding.Subtitles +{ + /// + /// Interface ISubtitleWriter + /// + public interface ISubtitleWriter + { + /// + /// Writes the specified information. + /// + /// The information. + /// The stream. + /// The cancellation token. + void Write(SubtitleTrackInfo info, Stream stream, CancellationToken cancellationToken); + } +} diff --git a/MediaBrowser.MediaEncoding/Subtitles/JsonWriter.cs b/MediaBrowser.MediaEncoding/Subtitles/JsonWriter.cs new file mode 100644 index 0000000000..474f712f9e --- /dev/null +++ b/MediaBrowser.MediaEncoding/Subtitles/JsonWriter.cs @@ -0,0 +1,28 @@ +using MediaBrowser.Model.MediaInfo; +using MediaBrowser.Model.Serialization; +using System.IO; +using System.Text; +using System.Threading; + +namespace MediaBrowser.MediaEncoding.Subtitles +{ + public class JsonWriter : ISubtitleWriter + { + private readonly IJsonSerializer _json; + + public JsonWriter(IJsonSerializer json) + { + _json = json; + } + + public void Write(SubtitleTrackInfo info, Stream stream, CancellationToken cancellationToken) + { + using (var writer = new StreamWriter(stream, Encoding.UTF8, 1024, true)) + { + var json = _json.SerializeToString(info); + + writer.Write(json); + } + } + } +} diff --git a/MediaBrowser.MediaEncoding/Subtitles/OpenSubtitleDownloader.cs b/MediaBrowser.MediaEncoding/Subtitles/OpenSubtitleDownloader.cs new file mode 100644 index 0000000000..3954897cac --- /dev/null +++ b/MediaBrowser.MediaEncoding/Subtitles/OpenSubtitleDownloader.cs @@ -0,0 +1,349 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.IO; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using MediaBrowser.Common.Configuration; +using MediaBrowser.Common.Extensions; +using MediaBrowser.Common.Net; +using MediaBrowser.Controller.Configuration; +using MediaBrowser.Controller.Providers; +using MediaBrowser.Controller.Security; +using MediaBrowser.Controller.Subtitles; +using MediaBrowser.Model.Dto; +using MediaBrowser.Model.Entities; +using MediaBrowser.Model.IO; +using MediaBrowser.Model.Logging; +using MediaBrowser.Model.Providers; +using MediaBrowser.Model.Serialization; +using OpenSubtitlesHandler; + +namespace MediaBrowser.MediaEncoding.Subtitles +{ + public class OpenSubtitleDownloader : ISubtitleProvider, IDisposable + { + private readonly ILogger _logger; + private readonly IHttpClient _httpClient; + private readonly CultureInfo _usCulture = new CultureInfo("en-US"); + + private readonly IServerConfigurationManager _config; + private readonly IEncryptionManager _encryption; + + private readonly IJsonSerializer _json; + private readonly IFileSystem _fileSystem; + + public OpenSubtitleDownloader(ILogManager logManager, IHttpClient httpClient, IServerConfigurationManager config, IEncryptionManager encryption, IJsonSerializer json, IFileSystem fileSystem) + { + _logger = logManager.GetLogger(GetType().Name); + _httpClient = httpClient; + _config = config; + _encryption = encryption; + _json = json; + _fileSystem = fileSystem; + + _config.NamedConfigurationUpdating += _config_NamedConfigurationUpdating; + + Utilities.HttpClient = httpClient; + OpenSubtitles.SetUserAgent("mediabrowser.tv"); + } + + private const string PasswordHashPrefix = "h:"; + void _config_NamedConfigurationUpdating(object sender, ConfigurationUpdateEventArgs e) + { + if (!string.Equals(e.Key, "subtitles", StringComparison.OrdinalIgnoreCase)) + { + return; + } + + var options = (SubtitleOptions)e.NewConfiguration; + + if (options != null && + !string.IsNullOrWhiteSpace(options.OpenSubtitlesPasswordHash) && + !options.OpenSubtitlesPasswordHash.StartsWith(PasswordHashPrefix, StringComparison.OrdinalIgnoreCase)) + { + options.OpenSubtitlesPasswordHash = EncryptPassword(options.OpenSubtitlesPasswordHash); + } + } + + private string EncryptPassword(string password) + { + return PasswordHashPrefix + _encryption.EncryptString(password); + } + + private string DecryptPassword(string password) + { + if (password == null || + !password.StartsWith(PasswordHashPrefix, StringComparison.OrdinalIgnoreCase)) + { + return string.Empty; + } + + return _encryption.DecryptString(password.Substring(2)); + } + + public string Name + { + get { return "Open Subtitles"; } + } + + private SubtitleOptions GetOptions() + { + return _config.GetSubtitleConfiguration(); + } + + public IEnumerable SupportedMediaTypes + { + get + { + var options = GetOptions(); + + if (string.IsNullOrWhiteSpace(options.OpenSubtitlesUsername) || + string.IsNullOrWhiteSpace(options.OpenSubtitlesPasswordHash)) + { + return new VideoContentType[] { }; + } + + return new[] { VideoContentType.Episode, VideoContentType.Movie }; + } + } + + public Task GetSubtitles(string id, CancellationToken cancellationToken) + { + return GetSubtitlesInternal(id, GetOptions(), cancellationToken); + } + + private DateTime _lastRateLimitException; + private async Task GetSubtitlesInternal(string id, + SubtitleOptions options, + CancellationToken cancellationToken) + { + if (string.IsNullOrWhiteSpace(id)) + { + throw new ArgumentNullException("id"); + } + + var idParts = id.Split(new[] { '-' }, 3); + + var format = idParts[0]; + var language = idParts[1]; + var ossId = idParts[2]; + + var downloadsList = new[] { int.Parse(ossId, _usCulture) }; + + await Login(cancellationToken).ConfigureAwait(false); + + if ((DateTime.UtcNow - _lastRateLimitException).TotalHours < 1) + { + throw new RateLimitExceededException("OpenSubtitles rate limit reached"); + } + + var resultDownLoad = await OpenSubtitles.DownloadSubtitlesAsync(downloadsList, cancellationToken).ConfigureAwait(false); + + if ((resultDownLoad.Status ?? string.Empty).IndexOf("407", StringComparison.OrdinalIgnoreCase) != -1) + { + _lastRateLimitException = DateTime.UtcNow; + throw new RateLimitExceededException("OpenSubtitles rate limit reached"); + } + + if (!(resultDownLoad is MethodResponseSubtitleDownload)) + { + throw new Exception("Invalid response type"); + } + + var results = ((MethodResponseSubtitleDownload)resultDownLoad).Results; + + _lastRateLimitException = DateTime.MinValue; + + if (results.Count == 0) + { + var msg = string.Format("Subtitle with Id {0} was not found. Name: {1}. Status: {2}. Message: {3}", + ossId, + resultDownLoad.Name ?? string.Empty, + resultDownLoad.Status ?? string.Empty, + resultDownLoad.Message ?? string.Empty); + + throw new ResourceNotFoundException(msg); + } + + var data = Convert.FromBase64String(results.First().Data); + + return new SubtitleResponse + { + Format = format, + Language = language, + + Stream = new MemoryStream(Utilities.Decompress(new MemoryStream(data))) + }; + } + + private DateTime _lastLogin; + private async Task Login(CancellationToken cancellationToken) + { + if ((DateTime.UtcNow - _lastLogin).TotalSeconds < 60) + { + return; + } + + var options = GetOptions(); + + var user = options.OpenSubtitlesUsername ?? string.Empty; + var password = DecryptPassword(options.OpenSubtitlesPasswordHash); + + var loginResponse = await OpenSubtitles.LogInAsync(user, password, "en", cancellationToken).ConfigureAwait(false); + + if (!(loginResponse is MethodResponseLogIn)) + { + throw new Exception("Authentication to OpenSubtitles failed."); + } + + _lastLogin = DateTime.UtcNow; + } + + public async Task> GetSupportedLanguages(CancellationToken cancellationToken) + { + await Login(cancellationToken).ConfigureAwait(false); + + var result = OpenSubtitles.GetSubLanguages("en"); + if (!(result is MethodResponseGetSubLanguages)) + { + _logger.Error("Invalid response type"); + return new List(); + } + + var results = ((MethodResponseGetSubLanguages)result).Languages; + + return results.Select(i => new NameIdPair + { + Name = i.LanguageName, + Id = i.SubLanguageID + }); + } + + private string NormalizeLanguage(string language) + { + // Problem with Greek subtitle download #1349 + if (string.Equals(language, "gre", StringComparison.OrdinalIgnoreCase)) + { + + return "ell"; + } + + return language; + } + + public async Task> Search(SubtitleSearchRequest request, CancellationToken cancellationToken) + { + var imdbIdText = request.GetProviderId(MetadataProviders.Imdb); + long imdbId = 0; + + switch (request.ContentType) + { + case VideoContentType.Episode: + if (!request.IndexNumber.HasValue || !request.ParentIndexNumber.HasValue || string.IsNullOrEmpty(request.SeriesName)) + { + _logger.Debug("Episode information missing"); + return new List(); + } + break; + case VideoContentType.Movie: + if (string.IsNullOrEmpty(request.Name)) + { + _logger.Debug("Movie name missing"); + return new List(); + } + if (string.IsNullOrWhiteSpace(imdbIdText) || !long.TryParse(imdbIdText.TrimStart('t'), NumberStyles.Any, _usCulture, out imdbId)) + { + _logger.Debug("Imdb id missing"); + return new List(); + } + break; + } + + if (string.IsNullOrEmpty(request.MediaPath)) + { + _logger.Debug("Path Missing"); + return new List(); + } + + await Login(cancellationToken).ConfigureAwait(false); + + var subLanguageId = NormalizeLanguage(request.Language); + string hash; + + using (var fileStream = _fileSystem.OpenRead(request.MediaPath)) + { + hash = Utilities.ComputeHash(fileStream); + } + var fileInfo = _fileSystem.GetFileInfo(request.MediaPath); + var movieByteSize = fileInfo.Length; + var searchImdbId = request.ContentType == VideoContentType.Movie ? imdbId.ToString(_usCulture) : ""; + var subtitleSearchParameters = request.ContentType == VideoContentType.Episode + ? new List { + new SubtitleSearchParameters(subLanguageId, + query: request.SeriesName, + season: request.ParentIndexNumber.Value.ToString(_usCulture), + episode: request.IndexNumber.Value.ToString(_usCulture)) + } + : new List { + new SubtitleSearchParameters(subLanguageId, imdbid: searchImdbId), + new SubtitleSearchParameters(subLanguageId, query: request.Name, imdbid: searchImdbId) + }; + var parms = new List { + new SubtitleSearchParameters( subLanguageId, + movieHash: hash, + movieByteSize: movieByteSize, + imdbid: searchImdbId ), + }; + parms.AddRange(subtitleSearchParameters); + var result = await OpenSubtitles.SearchSubtitlesAsync(parms.ToArray(), cancellationToken).ConfigureAwait(false); + if (!(result is MethodResponseSubtitleSearch)) + { + _logger.Error("Invalid response type"); + return new List(); + } + + Predicate mediaFilter = + x => + request.ContentType == VideoContentType.Episode + ? !string.IsNullOrEmpty(x.SeriesSeason) && !string.IsNullOrEmpty(x.SeriesEpisode) && + int.Parse(x.SeriesSeason, _usCulture) == request.ParentIndexNumber && + int.Parse(x.SeriesEpisode, _usCulture) == request.IndexNumber + : !string.IsNullOrEmpty(x.IDMovieImdb) && long.Parse(x.IDMovieImdb, _usCulture) == imdbId; + + var results = ((MethodResponseSubtitleSearch)result).Results; + + // Avoid implicitly captured closure + var hasCopy = hash; + + return results.Where(x => x.SubBad == "0" && mediaFilter(x) && (!request.IsPerfectMatch || string.Equals(x.MovieHash, hash, StringComparison.OrdinalIgnoreCase))) + .OrderBy(x => (string.Equals(x.MovieHash, hash, StringComparison.OrdinalIgnoreCase) ? 0 : 1)) + .ThenBy(x => Math.Abs(long.Parse(x.MovieByteSize, _usCulture) - movieByteSize)) + .ThenByDescending(x => int.Parse(x.SubDownloadsCnt, _usCulture)) + .ThenByDescending(x => double.Parse(x.SubRating, _usCulture)) + .Select(i => new RemoteSubtitleInfo + { + Author = i.UserNickName, + Comment = i.SubAuthorComment, + CommunityRating = float.Parse(i.SubRating, _usCulture), + DownloadCount = int.Parse(i.SubDownloadsCnt, _usCulture), + Format = i.SubFormat, + ProviderName = Name, + ThreeLetterISOLanguageName = i.SubLanguageID, + + Id = i.SubFormat + "-" + i.SubLanguageID + "-" + i.IDSubtitleFile, + + Name = i.SubFileName, + DateCreated = DateTime.Parse(i.SubAddDate, _usCulture), + IsHashMatch = i.MovieHash == hasCopy + + }).Where(i => !string.Equals(i.Format, "sub", StringComparison.OrdinalIgnoreCase) && !string.Equals(i.Format, "idx", StringComparison.OrdinalIgnoreCase)); + } + + public void Dispose() + { + _config.NamedConfigurationUpdating -= _config_NamedConfigurationUpdating; + } + } +} diff --git a/MediaBrowser.MediaEncoding/Subtitles/ParserValues.cs b/MediaBrowser.MediaEncoding/Subtitles/ParserValues.cs new file mode 100644 index 0000000000..b8c2fef1e4 --- /dev/null +++ b/MediaBrowser.MediaEncoding/Subtitles/ParserValues.cs @@ -0,0 +1,7 @@ +namespace MediaBrowser.MediaEncoding.Subtitles +{ + public class ParserValues + { + public const string NewLine = "\r\n"; + } +} diff --git a/MediaBrowser.MediaEncoding/Subtitles/SrtParser.cs b/MediaBrowser.MediaEncoding/Subtitles/SrtParser.cs new file mode 100644 index 0000000000..de6d7bc725 --- /dev/null +++ b/MediaBrowser.MediaEncoding/Subtitles/SrtParser.cs @@ -0,0 +1,92 @@ +using MediaBrowser.Model.Extensions; +using MediaBrowser.Model.Logging; +using System; +using System.Collections.Generic; +using System.Globalization; +using System.IO; +using System.Text.RegularExpressions; +using System.Threading; +using MediaBrowser.Model.MediaInfo; + +namespace MediaBrowser.MediaEncoding.Subtitles +{ + public class SrtParser : ISubtitleParser + { + private readonly ILogger _logger; + + private readonly CultureInfo _usCulture = new CultureInfo("en-US"); + + public SrtParser(ILogger logger) + { + _logger = logger; + } + + public SubtitleTrackInfo Parse(Stream stream, CancellationToken cancellationToken) + { + var trackInfo = new SubtitleTrackInfo(); + List trackEvents = new List(); + using ( var reader = new StreamReader(stream)) + { + string line; + while ((line = reader.ReadLine()) != null) + { + cancellationToken.ThrowIfCancellationRequested(); + + if (string.IsNullOrWhiteSpace(line)) + { + continue; + } + var subEvent = new SubtitleTrackEvent {Id = line}; + line = reader.ReadLine(); + + if (string.IsNullOrWhiteSpace(line)) + { + continue; + } + + var time = Regex.Split(line, @"[\t ]*-->[\t ]*"); + + if (time.Length < 2) + { + // This occurs when subtitle text has an empty line as part of the text. + // Need to adjust the break statement below to resolve this. + _logger.Warn("Unrecognized line in srt: {0}", line); + continue; + } + subEvent.StartPositionTicks = GetTicks(time[0]); + var endTime = time[1]; + var idx = endTime.IndexOf(" ", StringComparison.Ordinal); + if (idx > 0) + endTime = endTime.Substring(0, idx); + subEvent.EndPositionTicks = GetTicks(endTime); + var multiline = new List(); + while ((line = reader.ReadLine()) != null) + { + if (string.IsNullOrEmpty(line)) + { + break; + } + multiline.Add(line); + } + subEvent.Text = string.Join(ParserValues.NewLine, multiline); + subEvent.Text = subEvent.Text.Replace(@"\N", ParserValues.NewLine, StringComparison.OrdinalIgnoreCase); + subEvent.Text = Regex.Replace(subEvent.Text, @"\{(?:\\\d?[\w.-]+(?:\([^\)]*\)|&H?[0-9A-Fa-f]+&|))+\}", string.Empty, RegexOptions.IgnoreCase); + subEvent.Text = Regex.Replace(subEvent.Text, "<", "<", RegexOptions.IgnoreCase); + subEvent.Text = Regex.Replace(subEvent.Text, ">", ">", RegexOptions.IgnoreCase); + subEvent.Text = Regex.Replace(subEvent.Text, "<(\\/?(font|b|u|i|s))((\\s+(\\w|\\w[\\w\\-]*\\w)(\\s*=\\s*(?:\\\".*?\\\"|'.*?'|[^'\\\">\\s]+))?)+\\s*|\\s*)(\\/?)>", "<$1$3$7>", RegexOptions.IgnoreCase); + trackEvents.Add(subEvent); + } + } + trackInfo.TrackEvents = trackEvents.ToArray(); + return trackInfo; + } + + long GetTicks(string time) { + TimeSpan span; + return TimeSpan.TryParseExact(time, @"hh\:mm\:ss\.fff", _usCulture, out span) + ? span.Ticks + : (TimeSpan.TryParseExact(time, @"hh\:mm\:ss\,fff", _usCulture, out span) + ? span.Ticks : 0); + } + } +} diff --git a/MediaBrowser.MediaEncoding/Subtitles/SrtWriter.cs b/MediaBrowser.MediaEncoding/Subtitles/SrtWriter.cs new file mode 100644 index 0000000000..c05929fdeb --- /dev/null +++ b/MediaBrowser.MediaEncoding/Subtitles/SrtWriter.cs @@ -0,0 +1,39 @@ +using System; +using System.Globalization; +using System.IO; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading; +using MediaBrowser.Model.MediaInfo; + +namespace MediaBrowser.MediaEncoding.Subtitles +{ + public class SrtWriter : ISubtitleWriter + { + public void Write(SubtitleTrackInfo info, Stream stream, CancellationToken cancellationToken) + { + using (var writer = new StreamWriter(stream, Encoding.UTF8, 1024, true)) + { + var index = 1; + + foreach (var trackEvent in info.TrackEvents) + { + cancellationToken.ThrowIfCancellationRequested(); + + writer.WriteLine(index.ToString(CultureInfo.InvariantCulture)); + writer.WriteLine(@"{0:hh\:mm\:ss\,fff} --> {1:hh\:mm\:ss\,fff}", TimeSpan.FromTicks(trackEvent.StartPositionTicks), TimeSpan.FromTicks(trackEvent.EndPositionTicks)); + + var text = trackEvent.Text; + + // TODO: Not sure how to handle these + text = Regex.Replace(text, @"\\n", " ", RegexOptions.IgnoreCase); + + writer.WriteLine(text); + writer.WriteLine(string.Empty); + + index++; + } + } + } + } +} diff --git a/MediaBrowser.MediaEncoding/Subtitles/SsaParser.cs b/MediaBrowser.MediaEncoding/Subtitles/SsaParser.cs new file mode 100644 index 0000000000..a2cee77935 --- /dev/null +++ b/MediaBrowser.MediaEncoding/Subtitles/SsaParser.cs @@ -0,0 +1,397 @@ +using MediaBrowser.Model.Extensions; +using System; +using System.IO; +using System.Text; +using System.Threading; +using MediaBrowser.Model.MediaInfo; +using System.Collections.Generic; + +namespace MediaBrowser.MediaEncoding.Subtitles +{ + /// + /// Credit to https://github.com/SubtitleEdit/subtitleedit/blob/a299dc4407a31796364cc6ad83f0d3786194ba22/src/Logic/SubtitleFormats/SubStationAlpha.cs + /// + public class SsaParser : ISubtitleParser + { + public SubtitleTrackInfo Parse(Stream stream, CancellationToken cancellationToken) + { + var trackInfo = new SubtitleTrackInfo(); + List trackEvents = new List(); + + using (var reader = new StreamReader(stream)) + { + bool eventsStarted = false; + + string[] format = "Marked, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text".Split(','); + int indexLayer = 0; + int indexStart = 1; + int indexEnd = 2; + int indexStyle = 3; + int indexName = 4; + int indexEffect = 8; + int indexText = 9; + int lineNumber = 0; + + var header = new StringBuilder(); + + string line; + + while ((line = reader.ReadLine()) != null) + { + cancellationToken.ThrowIfCancellationRequested(); + + lineNumber++; + if (!eventsStarted) + header.AppendLine(line); + + if (line.Trim().ToLower() == "[events]") + { + eventsStarted = true; + } + else if (!string.IsNullOrEmpty(line) && line.Trim().StartsWith(";")) + { + // skip comment lines + } + else if (eventsStarted && line.Trim().Length > 0) + { + string s = line.Trim().ToLower(); + if (s.StartsWith("format:")) + { + if (line.Length > 10) + { + format = line.ToLower().Substring(8).Split(','); + for (int i = 0; i < format.Length; i++) + { + if (format[i].Trim().ToLower() == "layer") + indexLayer = i; + else if (format[i].Trim().ToLower() == "start") + indexStart = i; + else if (format[i].Trim().ToLower() == "end") + indexEnd = i; + else if (format[i].Trim().ToLower() == "text") + indexText = i; + else if (format[i].Trim().ToLower() == "effect") + indexEffect = i; + else if (format[i].Trim().ToLower() == "style") + indexStyle = i; + } + } + } + else if (!string.IsNullOrEmpty(s)) + { + string text = string.Empty; + string start = string.Empty; + string end = string.Empty; + string style = string.Empty; + string layer = string.Empty; + string effect = string.Empty; + string name = string.Empty; + + string[] splittedLine; + + if (s.StartsWith("dialogue:")) + splittedLine = line.Substring(10).Split(','); + else + splittedLine = line.Split(','); + + for (int i = 0; i < splittedLine.Length; i++) + { + if (i == indexStart) + start = splittedLine[i].Trim(); + else if (i == indexEnd) + end = splittedLine[i].Trim(); + else if (i == indexLayer) + layer = splittedLine[i]; + else if (i == indexEffect) + effect = splittedLine[i]; + else if (i == indexText) + text = splittedLine[i]; + else if (i == indexStyle) + style = splittedLine[i]; + else if (i == indexName) + name = splittedLine[i]; + else if (i > indexText) + text += "," + splittedLine[i]; + } + + try + { + var p = new SubtitleTrackEvent(); + + p.StartPositionTicks = GetTimeCodeFromString(start); + p.EndPositionTicks = GetTimeCodeFromString(end); + p.Text = GetFormattedText(text); + + trackEvents.Add(p); + } + catch + { + } + } + } + } + + //if (header.Length > 0) + //subtitle.Header = header.ToString(); + + //subtitle.Renumber(1); + } + trackInfo.TrackEvents = trackEvents.ToArray(); + return trackInfo; + } + + private static long GetTimeCodeFromString(string time) + { + // h:mm:ss.cc + string[] timeCode = time.Split(':', '.'); + return new TimeSpan(0, int.Parse(timeCode[0]), + int.Parse(timeCode[1]), + int.Parse(timeCode[2]), + int.Parse(timeCode[3]) * 10).Ticks; + } + + public static string GetFormattedText(string text) + { + text = text.Replace("\\n", ParserValues.NewLine, StringComparison.OrdinalIgnoreCase); + + bool italic = false; + + for (int i = 0; i < 10; i++) // just look ten times... + { + if (text.Contains(@"{\fn")) + { + int start = text.IndexOf(@"{\fn"); + int end = text.IndexOf('}', start); + if (end > 0 && !text.Substring(start).StartsWith("{\\fn}")) + { + string fontName = text.Substring(start + 4, end - (start + 4)); + string extraTags = string.Empty; + CheckAndAddSubTags(ref fontName, ref extraTags, out italic); + text = text.Remove(start, end - start + 1); + if (italic) + text = text.Insert(start, ""); + else + text = text.Insert(start, ""); + + int indexOfEndTag = text.IndexOf("{\\fn}", start); + if (indexOfEndTag > 0) + text = text.Remove(indexOfEndTag, "{\\fn}".Length).Insert(indexOfEndTag, ""); + else + text += ""; + } + } + + if (text.Contains(@"{\fs")) + { + int start = text.IndexOf(@"{\fs"); + int end = text.IndexOf('}', start); + if (end > 0 && !text.Substring(start).StartsWith("{\\fs}")) + { + string fontSize = text.Substring(start + 4, end - (start + 4)); + string extraTags = string.Empty; + CheckAndAddSubTags(ref fontSize, ref extraTags, out italic); + if (IsInteger(fontSize)) + { + text = text.Remove(start, end - start + 1); + if (italic) + text = text.Insert(start, ""); + else + text = text.Insert(start, ""); + + int indexOfEndTag = text.IndexOf("{\\fs}", start); + if (indexOfEndTag > 0) + text = text.Remove(indexOfEndTag, "{\\fs}".Length).Insert(indexOfEndTag, ""); + else + text += ""; + } + } + } + + if (text.Contains(@"{\c")) + { + int start = text.IndexOf(@"{\c"); + int end = text.IndexOf('}', start); + if (end > 0 && !text.Substring(start).StartsWith("{\\c}")) + { + string color = text.Substring(start + 4, end - (start + 4)); + string extraTags = string.Empty; + CheckAndAddSubTags(ref color, ref extraTags, out italic); + + color = color.Replace("&", string.Empty).TrimStart('H'); + color = color.PadLeft(6, '0'); + + // switch to rrggbb from bbggrr + color = "#" + color.Remove(color.Length - 6) + color.Substring(color.Length - 2, 2) + color.Substring(color.Length - 4, 2) + color.Substring(color.Length - 6, 2); + color = color.ToLower(); + + text = text.Remove(start, end - start + 1); + if (italic) + text = text.Insert(start, ""); + else + text = text.Insert(start, ""); + int indexOfEndTag = text.IndexOf("{\\c}", start); + if (indexOfEndTag > 0) + text = text.Remove(indexOfEndTag, "{\\c}".Length).Insert(indexOfEndTag, ""); + else + text += ""; + } + } + + if (text.Contains(@"{\1c")) // "1" specifices primary color + { + int start = text.IndexOf(@"{\1c"); + int end = text.IndexOf('}', start); + if (end > 0 && !text.Substring(start).StartsWith("{\\1c}")) + { + string color = text.Substring(start + 5, end - (start + 5)); + string extraTags = string.Empty; + CheckAndAddSubTags(ref color, ref extraTags, out italic); + + color = color.Replace("&", string.Empty).TrimStart('H'); + color = color.PadLeft(6, '0'); + + // switch to rrggbb from bbggrr + color = "#" + color.Remove(color.Length - 6) + color.Substring(color.Length - 2, 2) + color.Substring(color.Length - 4, 2) + color.Substring(color.Length - 6, 2); + color = color.ToLower(); + + text = text.Remove(start, end - start + 1); + if (italic) + text = text.Insert(start, ""); + else + text = text.Insert(start, ""); + text += ""; + } + } + + } + + text = text.Replace(@"{\i1}", ""); + text = text.Replace(@"{\i0}", ""); + text = text.Replace(@"{\i}", ""); + if (CountTagInText(text, "") > CountTagInText(text, "")) + text += ""; + + text = text.Replace(@"{\u1}", ""); + text = text.Replace(@"{\u0}", ""); + text = text.Replace(@"{\u}", ""); + if (CountTagInText(text, "") > CountTagInText(text, "")) + text += ""; + + text = text.Replace(@"{\b1}", ""); + text = text.Replace(@"{\b0}", ""); + text = text.Replace(@"{\b}", ""); + if (CountTagInText(text, "") > CountTagInText(text, "")) + text += ""; + + return text; + } + + private static bool IsInteger(string s) + { + int i; + if (int.TryParse(s, out i)) + return true; + return false; + } + + private static int CountTagInText(string text, string tag) + { + int count = 0; + int index = text.IndexOf(tag); + while (index >= 0) + { + count++; + if (index == text.Length) + return count; + index = text.IndexOf(tag, index + 1); + } + return count; + } + + private static void CheckAndAddSubTags(ref string tagName, ref string extraTags, out bool italic) + { + italic = false; + int indexOfSPlit = tagName.IndexOf(@"\"); + if (indexOfSPlit > 0) + { + string rest = tagName.Substring(indexOfSPlit).TrimStart('\\'); + tagName = tagName.Remove(indexOfSPlit); + + for (int i = 0; i < 10; i++) + { + if (rest.StartsWith("fs") && rest.Length > 2) + { + indexOfSPlit = rest.IndexOf(@"\"); + string fontSize = rest; + if (indexOfSPlit > 0) + { + fontSize = rest.Substring(0, indexOfSPlit); + rest = rest.Substring(indexOfSPlit).TrimStart('\\'); + } + else + { + rest = string.Empty; + } + extraTags += " size=\"" + fontSize.Substring(2) + "\""; + } + else if (rest.StartsWith("fn") && rest.Length > 2) + { + indexOfSPlit = rest.IndexOf(@"\"); + string fontName = rest; + if (indexOfSPlit > 0) + { + fontName = rest.Substring(0, indexOfSPlit); + rest = rest.Substring(indexOfSPlit).TrimStart('\\'); + } + else + { + rest = string.Empty; + } + extraTags += " face=\"" + fontName.Substring(2) + "\""; + } + else if (rest.StartsWith("c") && rest.Length > 2) + { + indexOfSPlit = rest.IndexOf(@"\"); + string fontColor = rest; + if (indexOfSPlit > 0) + { + fontColor = rest.Substring(0, indexOfSPlit); + rest = rest.Substring(indexOfSPlit).TrimStart('\\'); + } + else + { + rest = string.Empty; + } + + string color = fontColor.Substring(2); + color = color.Replace("&", string.Empty).TrimStart('H'); + color = color.PadLeft(6, '0'); + // switch to rrggbb from bbggrr + color = "#" + color.Remove(color.Length - 6) + color.Substring(color.Length - 2, 2) + color.Substring(color.Length - 4, 2) + color.Substring(color.Length - 6, 2); + color = color.ToLower(); + + extraTags += " color=\"" + color + "\""; + } + else if (rest.StartsWith("i1") && rest.Length > 1) + { + indexOfSPlit = rest.IndexOf(@"\"); + italic = true; + if (indexOfSPlit > 0) + { + rest = rest.Substring(indexOfSPlit).TrimStart('\\'); + } + else + { + rest = string.Empty; + } + } + else if (rest.Length > 0 && rest.Contains("\\")) + { + indexOfSPlit = rest.IndexOf(@"\"); + rest = rest.Substring(indexOfSPlit).TrimStart('\\'); + } + } + } + } + } +} diff --git a/MediaBrowser.MediaEncoding/Subtitles/SubtitleEncoder.cs b/MediaBrowser.MediaEncoding/Subtitles/SubtitleEncoder.cs new file mode 100644 index 0000000000..d565ff3e20 --- /dev/null +++ b/MediaBrowser.MediaEncoding/Subtitles/SubtitleEncoder.cs @@ -0,0 +1,733 @@ +using MediaBrowser.Common.Configuration; +using MediaBrowser.Common.Extensions; +using MediaBrowser.Common.Net; +using MediaBrowser.Controller.Entities; +using MediaBrowser.Controller.Library; +using MediaBrowser.Controller.MediaEncoding; +using MediaBrowser.Model.Entities; +using MediaBrowser.Model.Logging; +using MediaBrowser.Model.MediaInfo; +using MediaBrowser.Model.Serialization; +using System; +using System.Collections.Concurrent; +using System.Diagnostics; +using System.Globalization; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using MediaBrowser.Model.IO; +using MediaBrowser.Model.Diagnostics; +using MediaBrowser.Model.Dto; +using MediaBrowser.Model.Text; + +namespace MediaBrowser.MediaEncoding.Subtitles +{ + public class SubtitleEncoder : ISubtitleEncoder + { + private readonly ILibraryManager _libraryManager; + private readonly ILogger _logger; + private readonly IApplicationPaths _appPaths; + private readonly IFileSystem _fileSystem; + private readonly IMediaEncoder _mediaEncoder; + private readonly IJsonSerializer _json; + private readonly IHttpClient _httpClient; + private readonly IMediaSourceManager _mediaSourceManager; + private readonly IProcessFactory _processFactory; + private readonly ITextEncoding _textEncoding; + + public SubtitleEncoder(ILibraryManager libraryManager, ILogger logger, IApplicationPaths appPaths, IFileSystem fileSystem, IMediaEncoder mediaEncoder, IJsonSerializer json, IHttpClient httpClient, IMediaSourceManager mediaSourceManager, IProcessFactory processFactory, ITextEncoding textEncoding) + { + _libraryManager = libraryManager; + _logger = logger; + _appPaths = appPaths; + _fileSystem = fileSystem; + _mediaEncoder = mediaEncoder; + _json = json; + _httpClient = httpClient; + _processFactory = processFactory; + _textEncoding = textEncoding; + } + + private string SubtitleCachePath + { + get + { + return Path.Combine(_appPaths.DataPath, "subtitles"); + } + } + + private Stream ConvertSubtitles(Stream stream, + string inputFormat, + string outputFormat, + long startTimeTicks, + long? endTimeTicks, + bool preserveOriginalTimestamps, + CancellationToken cancellationToken) + { + var ms = new MemoryStream(); + + try + { + var reader = GetReader(inputFormat, true); + + var trackInfo = reader.Parse(stream, cancellationToken); + + FilterEvents(trackInfo, startTimeTicks, endTimeTicks, preserveOriginalTimestamps); + + var writer = GetWriter(outputFormat); + + writer.Write(trackInfo, ms, cancellationToken); + ms.Position = 0; + } + catch + { + ms.Dispose(); + throw; + } + + return ms; + } + + private void FilterEvents(SubtitleTrackInfo track, long startPositionTicks, long? endTimeTicks, bool preserveTimestamps) + { + // Drop subs that are earlier than what we're looking for + track.TrackEvents = track.TrackEvents + .SkipWhile(i => (i.StartPositionTicks - startPositionTicks) < 0 || (i.EndPositionTicks - startPositionTicks) < 0) + .ToArray(); + + if (endTimeTicks.HasValue) + { + var endTime = endTimeTicks.Value; + + track.TrackEvents = track.TrackEvents + .TakeWhile(i => i.StartPositionTicks <= endTime) + .ToArray(); + } + + if (!preserveTimestamps) + { + foreach (var trackEvent in track.TrackEvents) + { + trackEvent.EndPositionTicks -= startPositionTicks; + trackEvent.StartPositionTicks -= startPositionTicks; + } + } + } + + async Task ISubtitleEncoder.GetSubtitles(BaseItem item, string mediaSourceId, int subtitleStreamIndex, string outputFormat, long startTimeTicks, long endTimeTicks, bool preserveOriginalTimestamps, CancellationToken cancellationToken) + { + if (item == null) + { + throw new ArgumentNullException("item"); + } + if (string.IsNullOrWhiteSpace(mediaSourceId)) + { + throw new ArgumentNullException("mediaSourceId"); + } + + // TODO network path substition useful ? + var mediaSources = await _mediaSourceManager.GetPlayackMediaSources(item, null, true, true, cancellationToken).ConfigureAwait(false); + + var mediaSource = mediaSources + .First(i => string.Equals(i.Id, mediaSourceId, StringComparison.OrdinalIgnoreCase)); + + var subtitleStream = mediaSource.MediaStreams + .First(i => i.Type == MediaStreamType.Subtitle && i.Index == subtitleStreamIndex); + + var subtitle = await GetSubtitleStream(mediaSource, subtitleStream, cancellationToken) + .ConfigureAwait(false); + + var inputFormat = subtitle.Item2; + var writer = TryGetWriter(outputFormat); + + // Return the original if we don't have any way of converting it + if (writer == null) + { + return subtitle.Item1; + } + + // Return the original if the same format is being requested + // Character encoding was already handled in GetSubtitleStream + if (string.Equals(inputFormat, outputFormat, StringComparison.OrdinalIgnoreCase)) + { + return subtitle.Item1; + } + + using (var stream = subtitle.Item1) + { + return ConvertSubtitles(stream, inputFormat, outputFormat, startTimeTicks, endTimeTicks, preserveOriginalTimestamps, cancellationToken); + } + } + + private async Task> GetSubtitleStream(MediaSourceInfo mediaSource, + MediaStream subtitleStream, + CancellationToken cancellationToken) + { + var inputFiles = new[] { mediaSource.Path }; + + if (mediaSource.VideoType.HasValue) + { + if (mediaSource.VideoType.Value == VideoType.BluRay || mediaSource.VideoType.Value == VideoType.Dvd) + { + var mediaSourceItem = (Video)_libraryManager.GetItemById(new Guid(mediaSource.Id)); + inputFiles = mediaSourceItem.GetPlayableStreamFileNames(_mediaEncoder).ToArray(); + } + } + + var fileInfo = await GetReadableFile(mediaSource.Path, inputFiles, mediaSource.Protocol, subtitleStream, cancellationToken).ConfigureAwait(false); + + var stream = await GetSubtitleStream(fileInfo.Item1, subtitleStream.Language, fileInfo.Item2, fileInfo.Item4, cancellationToken).ConfigureAwait(false); + + return new Tuple(stream, fileInfo.Item3); + } + + private async Task GetSubtitleStream(string path, string language, MediaProtocol protocol, bool requiresCharset, CancellationToken cancellationToken) + { + if (requiresCharset) + { + var bytes = await GetBytes(path, protocol, cancellationToken).ConfigureAwait(false); + + var charset = _textEncoding.GetDetectedEncodingName(bytes, bytes.Length, language, true); + _logger.Debug("charset {0} detected for {1}", charset ?? "null", path); + + if (!string.IsNullOrEmpty(charset)) + { + using (var inputStream = new MemoryStream(bytes)) + { + using (var reader = new StreamReader(inputStream, _textEncoding.GetEncodingFromCharset(charset))) + { + var text = await reader.ReadToEndAsync().ConfigureAwait(false); + + bytes = Encoding.UTF8.GetBytes(text); + + return new MemoryStream(bytes); + } + } + } + } + + return _fileSystem.OpenRead(path); + } + + private async Task> GetReadableFile(string mediaPath, + string[] inputFiles, + MediaProtocol protocol, + MediaStream subtitleStream, + CancellationToken cancellationToken) + { + if (!subtitleStream.IsExternal) + { + string outputFormat; + string outputCodec; + + if (string.Equals(subtitleStream.Codec, "ass", StringComparison.OrdinalIgnoreCase) || + string.Equals(subtitleStream.Codec, "ssa", StringComparison.OrdinalIgnoreCase) || + string.Equals(subtitleStream.Codec, "srt", StringComparison.OrdinalIgnoreCase)) + { + // Extract + outputCodec = "copy"; + outputFormat = subtitleStream.Codec; + } + else if (string.Equals(subtitleStream.Codec, "subrip", StringComparison.OrdinalIgnoreCase)) + { + // Extract + outputCodec = "copy"; + outputFormat = "srt"; + } + else + { + // Extract + outputCodec = "srt"; + outputFormat = "srt"; + } + + // Extract + var outputPath = GetSubtitleCachePath(mediaPath, protocol, subtitleStream.Index, "." + outputFormat); + + await ExtractTextSubtitle(inputFiles, protocol, subtitleStream.Index, outputCodec, outputPath, cancellationToken) + .ConfigureAwait(false); + + return new Tuple(outputPath, MediaProtocol.File, outputFormat, false); + } + + var currentFormat = (Path.GetExtension(subtitleStream.Path) ?? subtitleStream.Codec) + .TrimStart('.'); + + if (GetReader(currentFormat, false) == null) + { + // Convert + var outputPath = GetSubtitleCachePath(mediaPath, protocol, subtitleStream.Index, ".srt"); + + await ConvertTextSubtitleToSrt(subtitleStream.Path, subtitleStream.Language, protocol, outputPath, cancellationToken).ConfigureAwait(false); + + return new Tuple(outputPath, MediaProtocol.File, "srt", true); + } + + return new Tuple(subtitleStream.Path, protocol, currentFormat, true); + } + + private ISubtitleParser GetReader(string format, bool throwIfMissing) + { + if (string.IsNullOrEmpty(format)) + { + throw new ArgumentNullException("format"); + } + + if (string.Equals(format, SubtitleFormat.SRT, StringComparison.OrdinalIgnoreCase)) + { + return new SrtParser(_logger); + } + if (string.Equals(format, SubtitleFormat.SSA, StringComparison.OrdinalIgnoreCase)) + { + return new SsaParser(); + } + if (string.Equals(format, SubtitleFormat.ASS, StringComparison.OrdinalIgnoreCase)) + { + return new AssParser(); + } + + if (throwIfMissing) + { + throw new ArgumentException("Unsupported format: " + format); + } + + return null; + } + + private ISubtitleWriter TryGetWriter(string format) + { + if (string.IsNullOrEmpty(format)) + { + throw new ArgumentNullException("format"); + } + + if (string.Equals(format, "json", StringComparison.OrdinalIgnoreCase)) + { + return new JsonWriter(_json); + } + if (string.Equals(format, SubtitleFormat.SRT, StringComparison.OrdinalIgnoreCase)) + { + return new SrtWriter(); + } + if (string.Equals(format, SubtitleFormat.VTT, StringComparison.OrdinalIgnoreCase)) + { + return new VttWriter(); + } + if (string.Equals(format, SubtitleFormat.TTML, StringComparison.OrdinalIgnoreCase)) + { + return new TtmlWriter(); + } + + return null; + } + + private ISubtitleWriter GetWriter(string format) + { + var writer = TryGetWriter(format); + + if (writer != null) + { + return writer; + } + + throw new ArgumentException("Unsupported format: " + format); + } + + /// + /// The _semaphoreLocks + /// + private readonly ConcurrentDictionary _semaphoreLocks = + new ConcurrentDictionary(); + + /// + /// Gets the lock. + /// + /// The filename. + /// System.Object. + private SemaphoreSlim GetLock(string filename) + { + return _semaphoreLocks.GetOrAdd(filename, key => new SemaphoreSlim(1, 1)); + } + + /// + /// Converts the text subtitle to SRT. + /// + /// The input path. + /// The input protocol. + /// The output path. + /// The cancellation token. + /// Task. + private async Task ConvertTextSubtitleToSrt(string inputPath, string language, MediaProtocol inputProtocol, string outputPath, CancellationToken cancellationToken) + { + var semaphore = GetLock(outputPath); + + await semaphore.WaitAsync(cancellationToken).ConfigureAwait(false); + + try + { + if (!_fileSystem.FileExists(outputPath)) + { + await ConvertTextSubtitleToSrtInternal(inputPath, language, inputProtocol, outputPath, cancellationToken).ConfigureAwait(false); + } + } + finally + { + semaphore.Release(); + } + } + + /// + /// Converts the text subtitle to SRT internal. + /// + /// The input path. + /// The input protocol. + /// The output path. + /// The cancellation token. + /// Task. + /// + /// inputPath + /// or + /// outputPath + /// + private async Task ConvertTextSubtitleToSrtInternal(string inputPath, string language, MediaProtocol inputProtocol, string outputPath, CancellationToken cancellationToken) + { + if (string.IsNullOrEmpty(inputPath)) + { + throw new ArgumentNullException("inputPath"); + } + + if (string.IsNullOrEmpty(outputPath)) + { + throw new ArgumentNullException("outputPath"); + } + + _fileSystem.CreateDirectory(_fileSystem.GetDirectoryName(outputPath)); + + var encodingParam = await GetSubtitleFileCharacterSet(inputPath, language, inputProtocol, cancellationToken).ConfigureAwait(false); + + if (!string.IsNullOrEmpty(encodingParam)) + { + encodingParam = " -sub_charenc " + encodingParam; + } + + var process = _processFactory.Create(new ProcessOptions + { + CreateNoWindow = true, + UseShellExecute = false, + FileName = _mediaEncoder.EncoderPath, + Arguments = string.Format("{0} -i \"{1}\" -c:s srt \"{2}\"", encodingParam, inputPath, outputPath), + + IsHidden = true, + ErrorDialog = false + }); + + _logger.Info("{0} {1}", process.StartInfo.FileName, process.StartInfo.Arguments); + + try + { + process.Start(); + } + catch (Exception ex) + { + _logger.ErrorException("Error starting ffmpeg", ex); + + throw; + } + + var ranToCompletion = await process.WaitForExitAsync(300000).ConfigureAwait(false); + + if (!ranToCompletion) + { + try + { + _logger.Info("Killing ffmpeg subtitle conversion process"); + + process.Kill(); + } + catch (Exception ex) + { + _logger.ErrorException("Error killing subtitle conversion process", ex); + } + } + + var exitCode = ranToCompletion ? process.ExitCode : -1; + + process.Dispose(); + + var failed = false; + + if (exitCode == -1) + { + failed = true; + + if (_fileSystem.FileExists(outputPath)) + { + try + { + _logger.Info("Deleting converted subtitle due to failure: ", outputPath); + _fileSystem.DeleteFile(outputPath); + } + catch (IOException ex) + { + _logger.ErrorException("Error deleting converted subtitle {0}", ex, outputPath); + } + } + } + else if (!_fileSystem.FileExists(outputPath)) + { + failed = true; + } + + if (failed) + { + var msg = string.Format("ffmpeg subtitle conversion failed for {0}", inputPath); + + _logger.Error(msg); + + throw new Exception(msg); + } + await SetAssFont(outputPath).ConfigureAwait(false); + + _logger.Info("ffmpeg subtitle conversion succeeded for {0}", inputPath); + } + + /// + /// Extracts the text subtitle. + /// + /// The input files. + /// The protocol. + /// Index of the subtitle stream. + /// The output codec. + /// The output path. + /// The cancellation token. + /// Task. + /// Must use inputPath list overload + private async Task ExtractTextSubtitle(string[] inputFiles, MediaProtocol protocol, int subtitleStreamIndex, + string outputCodec, string outputPath, CancellationToken cancellationToken) + { + var semaphore = GetLock(outputPath); + + await semaphore.WaitAsync(cancellationToken).ConfigureAwait(false); + + try + { + if (!_fileSystem.FileExists(outputPath)) + { + await ExtractTextSubtitleInternal(_mediaEncoder.GetInputArgument(inputFiles, protocol), subtitleStreamIndex, outputCodec, outputPath, cancellationToken).ConfigureAwait(false); + } + } + finally + { + semaphore.Release(); + } + } + + private async Task ExtractTextSubtitleInternal(string inputPath, int subtitleStreamIndex, + string outputCodec, string outputPath, CancellationToken cancellationToken) + { + if (string.IsNullOrEmpty(inputPath)) + { + throw new ArgumentNullException("inputPath"); + } + + if (string.IsNullOrEmpty(outputPath)) + { + throw new ArgumentNullException("outputPath"); + } + + _fileSystem.CreateDirectory(_fileSystem.GetDirectoryName(outputPath)); + + var processArgs = string.Format("-i {0} -map 0:{1} -an -vn -c:s {2} \"{3}\"", inputPath, + subtitleStreamIndex, outputCodec, outputPath); + + var process = _processFactory.Create(new ProcessOptions + { + CreateNoWindow = true, + UseShellExecute = false, + + FileName = _mediaEncoder.EncoderPath, + Arguments = processArgs, + IsHidden = true, + ErrorDialog = false + }); + + _logger.Info("{0} {1}", process.StartInfo.FileName, process.StartInfo.Arguments); + + try + { + process.Start(); + } + catch (Exception ex) + { + _logger.ErrorException("Error starting ffmpeg", ex); + + throw; + } + + var ranToCompletion = await process.WaitForExitAsync(300000).ConfigureAwait(false); + + if (!ranToCompletion) + { + try + { + _logger.Info("Killing ffmpeg subtitle extraction process"); + + process.Kill(); + } + catch (Exception ex) + { + _logger.ErrorException("Error killing subtitle extraction process", ex); + } + } + + var exitCode = ranToCompletion ? process.ExitCode : -1; + + process.Dispose(); + + var failed = false; + + if (exitCode == -1) + { + failed = true; + + try + { + _logger.Info("Deleting extracted subtitle due to failure: {0}", outputPath); + _fileSystem.DeleteFile(outputPath); + } + catch (FileNotFoundException) + { + + } + catch (IOException ex) + { + _logger.ErrorException("Error deleting extracted subtitle {0}", ex, outputPath); + } + } + else if (!_fileSystem.FileExists(outputPath)) + { + failed = true; + } + + if (failed) + { + var msg = string.Format("ffmpeg subtitle extraction failed for {0} to {1}", inputPath, outputPath); + + _logger.Error(msg); + + throw new Exception(msg); + } + else + { + var msg = string.Format("ffmpeg subtitle extraction completed for {0} to {1}", inputPath, outputPath); + + _logger.Info(msg); + } + + if (string.Equals(outputCodec, "ass", StringComparison.OrdinalIgnoreCase)) + { + await SetAssFont(outputPath).ConfigureAwait(false); + } + } + + /// + /// Sets the ass font. + /// + /// The file. + /// Task. + private async Task SetAssFont(string file) + { + _logger.Info("Setting ass font within {0}", file); + + string text; + Encoding encoding; + + using (var fileStream = _fileSystem.OpenRead(file)) + { + using (var reader = new StreamReader(fileStream, true)) + { + encoding = reader.CurrentEncoding; + + text = await reader.ReadToEndAsync().ConfigureAwait(false); + } + } + + var newText = text.Replace(",Arial,", ",Arial Unicode MS,"); + + if (!string.Equals(text, newText)) + { + using (var fileStream = _fileSystem.GetFileStream(file, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.Read)) + { + using (var writer = new StreamWriter(fileStream, encoding)) + { + writer.Write(newText); + } + } + } + } + + private string GetSubtitleCachePath(string mediaPath, MediaProtocol protocol, int subtitleStreamIndex, string outputSubtitleExtension) + { + if (protocol == MediaProtocol.File) + { + var ticksParam = string.Empty; + + var date = _fileSystem.GetLastWriteTimeUtc(mediaPath); + + var filename = (mediaPath + "_" + subtitleStreamIndex.ToString(CultureInfo.InvariantCulture) + "_" + date.Ticks.ToString(CultureInfo.InvariantCulture) + ticksParam).GetMD5() + outputSubtitleExtension; + + var prefix = filename.Substring(0, 1); + + return Path.Combine(SubtitleCachePath, prefix, filename); + } + else + { + var filename = (mediaPath + "_" + subtitleStreamIndex.ToString(CultureInfo.InvariantCulture)).GetMD5() + outputSubtitleExtension; + + var prefix = filename.Substring(0, 1); + + return Path.Combine(SubtitleCachePath, prefix, filename); + } + } + + public async Task GetSubtitleFileCharacterSet(string path, string language, MediaProtocol protocol, CancellationToken cancellationToken) + { + var bytes = await GetBytes(path, protocol, cancellationToken).ConfigureAwait(false); + + var charset = _textEncoding.GetDetectedEncodingName(bytes, bytes.Length, language, true); + + _logger.Debug("charset {0} detected for {1}", charset ?? "null", path); + + return charset; + } + + private async Task GetBytes(string path, MediaProtocol protocol, CancellationToken cancellationToken) + { + if (protocol == MediaProtocol.Http) + { + HttpRequestOptions opts = new HttpRequestOptions(); + opts.Url = path; + opts.CancellationToken = cancellationToken; + using (var file = await _httpClient.Get(opts).ConfigureAwait(false)) + { + using (var memoryStream = new MemoryStream()) + { + await file.CopyToAsync(memoryStream).ConfigureAwait(false); + memoryStream.Position = 0; + + return memoryStream.ToArray(); + } + } + } + if (protocol == MediaProtocol.File) + { + return _fileSystem.ReadAllBytes(path); + } + + throw new ArgumentOutOfRangeException("protocol"); + } + + } +} \ No newline at end of file diff --git a/MediaBrowser.MediaEncoding/Subtitles/TtmlWriter.cs b/MediaBrowser.MediaEncoding/Subtitles/TtmlWriter.cs new file mode 100644 index 0000000000..c32005f897 --- /dev/null +++ b/MediaBrowser.MediaEncoding/Subtitles/TtmlWriter.cs @@ -0,0 +1,60 @@ +using System; +using System.IO; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading; +using MediaBrowser.Model.MediaInfo; + +namespace MediaBrowser.MediaEncoding.Subtitles +{ + public class TtmlWriter : ISubtitleWriter + { + public void Write(SubtitleTrackInfo info, Stream stream, CancellationToken cancellationToken) + { + // Example: https://github.com/zmalltalker/ttml2vtt/blob/master/data/sample.xml + // Parser example: https://github.com/mozilla/popcorn-js/blob/master/parsers/parserTTML/popcorn.parserTTML.js + + using (var writer = new StreamWriter(stream, Encoding.UTF8, 1024, true)) + { + writer.WriteLine(""); + writer.WriteLine(""); + + writer.WriteLine(""); + writer.WriteLine(""); + writer.WriteLine("