diff options
| author | LukePulverenti Luke Pulverenti luke pulverenti <LukePulverenti Luke Pulverenti luke.pulverenti@gmail.com> | 2012-08-12 00:03:19 -0400 |
|---|---|---|
| committer | LukePulverenti Luke Pulverenti luke pulverenti <LukePulverenti Luke Pulverenti luke.pulverenti@gmail.com> | 2012-08-12 00:03:19 -0400 |
| commit | bae04374e5423e969b2feb8dd3735b03c9eb5300 (patch) | |
| tree | c4396a3538a1a222adf79cc97d858712f3f2c5f2 /MediaBrowser.Api/HttpHandlers/AudioHandler.cs | |
| parent | e0089349e1e526549b3c9c7ea749c04345d94156 (diff) | |
Fixed stdout/stderr deadlock issue that was causing ffmpeg to hang when working with large files.
Diffstat (limited to 'MediaBrowser.Api/HttpHandlers/AudioHandler.cs')
| -rw-r--r-- | MediaBrowser.Api/HttpHandlers/AudioHandler.cs | 77 |
1 files changed, 42 insertions, 35 deletions
diff --git a/MediaBrowser.Api/HttpHandlers/AudioHandler.cs b/MediaBrowser.Api/HttpHandlers/AudioHandler.cs index f3839a892..294a5b7af 100644 --- a/MediaBrowser.Api/HttpHandlers/AudioHandler.cs +++ b/MediaBrowser.Api/HttpHandlers/AudioHandler.cs @@ -105,7 +105,7 @@ namespace MediaBrowser.Api.HttpHandlers /// <summary>
/// Creates arguments to pass to ffmpeg
/// </summary>
- private string GetAudioArguments()
+ protected override string GetCommandLineArguments()
{
List<string> audioTranscodeParams = new List<string>();
@@ -132,40 +132,6 @@ namespace MediaBrowser.Api.HttpHandlers return "-i \"" + LibraryItem.Path + "\" -vn " + string.Join(" ", audioTranscodeParams.ToArray()) + " -";
}
-
- protected async override Task WriteResponseToOutputStream(Stream stream)
- {
- ProcessStartInfo startInfo = new ProcessStartInfo();
-
- startInfo.CreateNoWindow = true;
-
- startInfo.UseShellExecute = false;
- startInfo.RedirectStandardOutput = true;
-
- startInfo.FileName = ApiService.FFMpegPath;
- startInfo.WorkingDirectory = ApiService.FFMpegDirectory;
- startInfo.Arguments = GetAudioArguments();
-
- Logger.LogInfo(startInfo.FileName + " " + startInfo.Arguments);
-
- Process process = new Process();
- process.StartInfo = startInfo;
-
- try
- {
- process.Start();
-
- await process.StandardOutput.BaseStream.CopyToAsync(stream);
- }
- catch (Exception ex)
- {
- Logger.LogException(ex);
- }
- finally
- {
- process.Dispose();
- }
- }
}
public abstract class BaseMediaHandler<T> : BaseHandler
@@ -252,7 +218,48 @@ namespace MediaBrowser.Api.HttpHandlers base.ProcessRequest(ctx);
}
+ protected abstract string GetCommandLineArguments();
protected abstract string GetOutputFormat();
protected abstract bool RequiresConversion();
+
+ protected async override Task WriteResponseToOutputStream(Stream stream)
+ {
+ ProcessStartInfo startInfo = new ProcessStartInfo();
+
+ startInfo.CreateNoWindow = true;
+
+ startInfo.UseShellExecute = false;
+ startInfo.RedirectStandardOutput = true;
+ startInfo.RedirectStandardError = true;
+
+ startInfo.FileName = ApiService.FFMpegPath;
+ startInfo.WorkingDirectory = ApiService.FFMpegDirectory;
+ startInfo.Arguments = GetCommandLineArguments();
+
+ Logger.LogInfo(startInfo.FileName + " " + startInfo.Arguments);
+
+ Process process = new Process();
+ process.StartInfo = startInfo;
+
+ try
+ {
+ process.Start();
+
+ // MUST read both stdout and stderr asynchronously or a deadlock may occurr
+ process.BeginErrorReadLine();
+
+ await process.StandardOutput.BaseStream.CopyToAsync(stream);
+
+ process.WaitForExit();
+ }
+ catch (Exception ex)
+ {
+ Logger.LogException(ex);
+ }
+ finally
+ {
+ process.Dispose();
+ }
+ }
}
}
|
