improve ffmpeg killing

This commit is contained in:
Luke Pulverenti 2015-04-10 18:16:41 -04:00
parent 3a00f003f5
commit 17f5ae8118
6 changed files with 35 additions and 31 deletions

View File

@ -284,16 +284,19 @@ namespace MediaBrowser.Api
{ {
job.ActiveRequestCount++; job.ActiveRequestCount++;
job.DisposeKillTimer(); if (string.IsNullOrWhiteSpace(job.PlaySessionId) || job.Type == TranscodingJobType.Progressive)
{
job.DisposeKillTimer();
}
} }
public void OnTranscodeEndRequest(TranscodingJob job) public void OnTranscodeEndRequest(TranscodingJob job)
{ {
job.ActiveRequestCount--; job.ActiveRequestCount--;
Logger.Debug("OnTranscodeEndRequest job.ActiveRequestCount={0}", job.ActiveRequestCount);
if (job.ActiveRequestCount == 0) if (job.ActiveRequestCount <= 0)
{ {
PingTimer(job, true); PingTimer(job, false);
} }
} }
internal void PingTranscodingJob(string deviceId, string playSessionId) internal void PingTranscodingJob(string deviceId, string playSessionId)
@ -323,11 +326,11 @@ namespace MediaBrowser.Api
foreach (var job in jobs) foreach (var job in jobs)
{ {
PingTimer(job, false); PingTimer(job, true);
} }
} }
private void PingTimer(TranscodingJob job, bool startTimerIfNeeded) private void PingTimer(TranscodingJob job, bool isProgressCheckIn)
{ {
// TODO: Lower this hls timeout // TODO: Lower this hls timeout
var timerDuration = job.Type == TranscodingJobType.Progressive ? var timerDuration = job.Type == TranscodingJobType.Progressive ?
@ -335,20 +338,23 @@ namespace MediaBrowser.Api
1800000; 1800000;
// We can really reduce the timeout for apps that are using the newer api // We can really reduce the timeout for apps that are using the newer api
if (!string.IsNullOrWhiteSpace(job.PlaySessionId) && job.Type == TranscodingJobType.Hls) if (!string.IsNullOrWhiteSpace(job.PlaySessionId) && job.Type != TranscodingJobType.Progressive)
{ {
timerDuration = 40000; timerDuration = 35000;
} }
if (job.KillTimer == null) if (job.KillTimer == null)
{ {
if (startTimerIfNeeded) // Don't start the timer for playback checkins with progressive streaming
if (job.Type != TranscodingJobType.Progressive || !isProgressCheckIn)
{ {
Logger.Debug("Starting kill timer at {0}ms", timerDuration);
job.KillTimer = new Timer(OnTranscodeKillTimerStopped, job, timerDuration, Timeout.Infinite); job.KillTimer = new Timer(OnTranscodeKillTimerStopped, job, timerDuration, Timeout.Infinite);
} }
} }
else else
{ {
Logger.Debug("Changing kill timer to {0}ms", timerDuration);
job.KillTimer.Change(timerDuration, Timeout.Infinite); job.KillTimer.Change(timerDuration, Timeout.Infinite);
} }
} }
@ -439,28 +445,19 @@ namespace MediaBrowser.Api
lock (job.ProcessLock) lock (job.ProcessLock)
{ {
if (job.TranscodingThrottler != null)
{
job.TranscodingThrottler.Stop();
}
var process = job.Process; var process = job.Process;
var hasExited = true; var hasExited = job.HasExited;
try
{
hasExited = process.HasExited;
}
catch (Exception ex)
{
Logger.ErrorException("Error determining if ffmpeg process has exited for {0}", ex, job.Path);
}
if (!hasExited) if (!hasExited)
{ {
try try
{ {
if (job.TranscodingThrottler != null)
{
job.TranscodingThrottler.Stop();
}
Logger.Info("Killing ffmpeg process for {0}", job.Path); Logger.Info("Killing ffmpeg process for {0}", job.Path);
//process.Kill(); //process.Kill();

View File

@ -1706,7 +1706,7 @@ namespace MediaBrowser.Api.Playback
state.OutputAudioCodec = "copy"; state.OutputAudioCodec = "copy";
} }
if (string.Equals(state.OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase)) if (string.Equals(state.OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase) && TranscodingJobType == TranscodingJobType.Hls)
{ {
var segmentLength = GetSegmentLength(state); var segmentLength = GetSegmentLength(state);
if (segmentLength.HasValue) if (segmentLength.HasValue)

View File

@ -144,7 +144,6 @@ namespace MediaBrowser.Api.Playback.Hls
request.StartTimeTicks = GetSeekPositionTicks(state, requestedIndex); request.StartTimeTicks = GetSeekPositionTicks(state, requestedIndex);
job = await StartFfMpeg(state, playlistPath, cancellationTokenSource).ConfigureAwait(false); job = await StartFfMpeg(state, playlistPath, cancellationTokenSource).ConfigureAwait(false);
ApiEntryPoint.Instance.OnTranscodeBeginRequest(job);
} }
catch catch
{ {
@ -154,6 +153,14 @@ namespace MediaBrowser.Api.Playback.Hls
await WaitForMinimumSegmentCount(playlistPath, 1, cancellationTokenSource.Token).ConfigureAwait(false); await WaitForMinimumSegmentCount(playlistPath, 1, cancellationTokenSource.Token).ConfigureAwait(false);
} }
else
{
job = ApiEntryPoint.Instance.OnTranscodeBeginRequest(playlistPath, TranscodingJobType);
if (job.TranscodingThrottler != null)
{
job.TranscodingThrottler.UnpauseTranscoding();
}
}
} }
} }
finally finally

View File

@ -70,7 +70,7 @@ namespace MediaBrowser.Api.Playback
} }
} }
private void UnpauseTranscoding() public void UnpauseTranscoding()
{ {
if (_isPaused) if (_isPaused)
{ {

View File

@ -244,8 +244,8 @@ namespace MediaBrowser.MediaEncoding.Encoder
{ {
try try
{ {
stream.KeyFrames = await GetKeyFrames(inputPath, stream.Index, cancellationToken) //stream.KeyFrames = await GetKeyFrames(inputPath, stream.Index, cancellationToken)
.ConfigureAwait(false); // .ConfigureAwait(false);
} }
catch (OperationCanceledException) catch (OperationCanceledException)
{ {

View File

@ -130,7 +130,7 @@ namespace MediaBrowser.Providers.MediaInfo
return ItemUpdateType.MetadataImport; return ItemUpdateType.MetadataImport;
} }
private const string SchemaVersion = "3"; private const string SchemaVersion = "4";
private async Task<Model.MediaInfo.MediaInfo> GetMediaInfo(Video item, private async Task<Model.MediaInfo.MediaInfo> GetMediaInfo(Video item,
IIsoMount isoMount, IIsoMount isoMount,
@ -145,7 +145,7 @@ namespace MediaBrowser.Providers.MediaInfo
try try
{ {
//return _json.DeserializeFromFile<Model.MediaInfo.MediaInfo>(cachePath); return _json.DeserializeFromFile<Model.MediaInfo.MediaInfo>(cachePath);
} }
catch (FileNotFoundException) catch (FileNotFoundException)
{ {