2016-02-12 00:01:38 -07:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2016-10-25 12:02:04 -07:00
|
|
|
|
using MediaBrowser.Model.IO;
|
2016-06-20 15:07:18 -07:00
|
|
|
|
using MediaBrowser.Common.Net;
|
|
|
|
|
using MediaBrowser.Controller;
|
2017-04-21 13:03:07 -07:00
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
2016-02-12 00:01:38 -07:00
|
|
|
|
using MediaBrowser.Controller.MediaEncoding;
|
2017-04-21 13:03:07 -07:00
|
|
|
|
using MediaBrowser.Model.Configuration;
|
2016-11-03 16:35:19 -07:00
|
|
|
|
using MediaBrowser.Model.Diagnostics;
|
2016-02-12 00:01:38 -07:00
|
|
|
|
using MediaBrowser.Model.Dto;
|
|
|
|
|
using MediaBrowser.Model.Entities;
|
2016-04-27 14:26:28 -07:00
|
|
|
|
using MediaBrowser.Model.LiveTv;
|
2018-12-13 06:18:25 -07:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2016-02-12 00:01:38 -07:00
|
|
|
|
using MediaBrowser.Model.Serialization;
|
2017-04-21 13:03:07 -07:00
|
|
|
|
using MediaBrowser.Common.Configuration;
|
2017-05-15 12:45:39 -07:00
|
|
|
|
using MediaBrowser.Controller.Library;
|
2018-09-12 10:26:21 -07:00
|
|
|
|
using MediaBrowser.Model.Reflection;
|
2016-02-12 00:01:38 -07:00
|
|
|
|
|
2016-11-03 16:35:19 -07:00
|
|
|
|
namespace Emby.Server.Implementations.LiveTv.EmbyTV
|
2016-02-12 00:01:38 -07:00
|
|
|
|
{
|
|
|
|
|
public class EncodedRecorder : IRecorder
|
|
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
private readonly IFileSystem _fileSystem;
|
2016-06-20 15:07:18 -07:00
|
|
|
|
private readonly IHttpClient _httpClient;
|
2016-02-12 00:01:38 -07:00
|
|
|
|
private readonly IMediaEncoder _mediaEncoder;
|
2016-06-20 15:07:18 -07:00
|
|
|
|
private readonly IServerApplicationPaths _appPaths;
|
2016-02-12 00:01:38 -07:00
|
|
|
|
private bool _hasExited;
|
2016-11-30 12:50:39 -07:00
|
|
|
|
private Stream _logFileStream;
|
2016-02-12 00:01:38 -07:00
|
|
|
|
private string _targetPath;
|
2016-11-03 16:35:19 -07:00
|
|
|
|
private IProcess _process;
|
|
|
|
|
private readonly IProcessFactory _processFactory;
|
2016-02-12 00:01:38 -07:00
|
|
|
|
private readonly IJsonSerializer _json;
|
2016-03-06 21:56:45 -07:00
|
|
|
|
private readonly TaskCompletionSource<bool> _taskCompletionSource = new TaskCompletionSource<bool>();
|
2017-04-21 13:03:07 -07:00
|
|
|
|
private readonly IServerConfigurationManager _config;
|
2018-09-12 10:26:21 -07:00
|
|
|
|
private readonly IAssemblyInfo _assemblyInfo;
|
2016-02-12 00:01:38 -07:00
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
|
public EncodedRecorder(ILogger logger, IFileSystem fileSystem, IMediaEncoder mediaEncoder, IServerApplicationPaths appPaths, IJsonSerializer json, IHttpClient httpClient, IProcessFactory processFactory, IServerConfigurationManager config, IAssemblyInfo assemblyInfo)
|
2016-02-12 00:01:38 -07:00
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_fileSystem = fileSystem;
|
|
|
|
|
_mediaEncoder = mediaEncoder;
|
|
|
|
|
_appPaths = appPaths;
|
|
|
|
|
_json = json;
|
2016-06-20 15:07:18 -07:00
|
|
|
|
_httpClient = httpClient;
|
2016-11-03 16:35:19 -07:00
|
|
|
|
_processFactory = processFactory;
|
2017-04-21 13:03:07 -07:00
|
|
|
|
_config = config;
|
2018-09-12 10:26:21 -07:00
|
|
|
|
_assemblyInfo = assemblyInfo;
|
2016-09-07 23:41:49 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-21 11:04:35 -07:00
|
|
|
|
private bool CopySubtitles
|
|
|
|
|
{
|
2017-02-22 13:58:02 -07:00
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
//return string.Equals(OutputFormat, "mkv", StringComparison.OrdinalIgnoreCase);
|
|
|
|
|
}
|
2017-02-21 11:04:35 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-05-09 22:15:06 -07:00
|
|
|
|
public string GetOutputPath(MediaSourceInfo mediaSource, string targetFile)
|
|
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
|
return Path.ChangeExtension(targetFile, ".ts");
|
2016-05-09 22:15:06 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-15 12:45:39 -07:00
|
|
|
|
public async Task Record(IDirectStreamProvider directStreamProvider, MediaSourceInfo mediaSource, string targetFile, TimeSpan duration, Action onStarted, CancellationToken cancellationToken)
|
2016-02-12 00:01:38 -07:00
|
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
|
// The media source is infinite so we need to handle stopping ourselves
|
|
|
|
|
var durationToken = new CancellationTokenSource(duration);
|
|
|
|
|
cancellationToken = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, durationToken.Token).Token;
|
2016-09-05 13:07:36 -07:00
|
|
|
|
|
2016-09-29 05:55:49 -07:00
|
|
|
|
await RecordFromFile(mediaSource, mediaSource.Path, targetFile, duration, onStarted, cancellationToken).ConfigureAwait(false);
|
2016-06-20 15:07:18 -07:00
|
|
|
|
|
2018-12-13 06:18:25 -07:00
|
|
|
|
_logger.LogInformation("Recording completed to file {0}", targetFile);
|
2016-09-14 09:21:33 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-21 13:03:07 -07:00
|
|
|
|
private EncodingOptions GetEncodingOptions()
|
|
|
|
|
{
|
|
|
|
|
return _config.GetConfiguration<EncodingOptions>("encoding");
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-29 05:55:49 -07:00
|
|
|
|
private Task RecordFromFile(MediaSourceInfo mediaSource, string inputFile, string targetFile, TimeSpan duration, Action onStarted, CancellationToken cancellationToken)
|
2016-06-20 15:07:18 -07:00
|
|
|
|
{
|
2016-02-12 00:01:38 -07:00
|
|
|
|
_targetPath = targetFile;
|
2017-05-04 11:14:45 -07:00
|
|
|
|
_fileSystem.CreateDirectory(_fileSystem.GetDirectoryName(targetFile));
|
2016-02-12 00:01:38 -07:00
|
|
|
|
|
2016-11-03 16:35:19 -07:00
|
|
|
|
var process = _processFactory.Create(new ProcessOptions
|
2016-02-12 00:01:38 -07:00
|
|
|
|
{
|
2016-11-03 16:35:19 -07:00
|
|
|
|
CreateNoWindow = true,
|
2016-11-30 12:50:39 -07:00
|
|
|
|
UseShellExecute = false,
|
2016-02-12 00:01:38 -07:00
|
|
|
|
|
2016-11-03 16:35:19 -07:00
|
|
|
|
// Must consume both stdout and stderr or deadlocks may occur
|
|
|
|
|
//RedirectStandardOutput = true,
|
2016-11-30 12:50:39 -07:00
|
|
|
|
RedirectStandardError = true,
|
|
|
|
|
RedirectStandardInput = true,
|
2016-02-12 00:01:38 -07:00
|
|
|
|
|
2016-11-03 16:35:19 -07:00
|
|
|
|
FileName = _mediaEncoder.EncoderPath,
|
|
|
|
|
Arguments = GetCommandLineArgs(mediaSource, inputFile, targetFile, duration),
|
2016-02-12 00:01:38 -07:00
|
|
|
|
|
2016-11-03 16:35:19 -07:00
|
|
|
|
IsHidden = true,
|
|
|
|
|
ErrorDialog = false,
|
2016-11-30 12:50:39 -07:00
|
|
|
|
EnableRaisingEvents = true
|
2016-11-03 16:35:19 -07:00
|
|
|
|
});
|
2016-02-12 00:01:38 -07:00
|
|
|
|
|
|
|
|
|
_process = process;
|
|
|
|
|
|
|
|
|
|
var commandLineLogMessage = process.StartInfo.FileName + " " + process.StartInfo.Arguments;
|
2018-12-13 06:18:25 -07:00
|
|
|
|
_logger.LogInformation(commandLineLogMessage);
|
2016-02-12 00:01:38 -07:00
|
|
|
|
|
2016-11-30 12:50:39 -07:00
|
|
|
|
var logFilePath = Path.Combine(_appPaths.LogDirectoryPath, "record-transcode-" + Guid.NewGuid() + ".txt");
|
2017-05-04 11:14:45 -07:00
|
|
|
|
_fileSystem.CreateDirectory(_fileSystem.GetDirectoryName(logFilePath));
|
2016-02-12 00:01:38 -07:00
|
|
|
|
|
2016-11-30 12:50:39 -07:00
|
|
|
|
// FFMpeg writes debug/error info to stderr. This is useful when debugging so let's put it in the log directory.
|
|
|
|
|
_logFileStream = _fileSystem.GetFileStream(logFilePath, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.Read, true);
|
|
|
|
|
|
|
|
|
|
var commandLineLogMessageBytes = Encoding.UTF8.GetBytes(_json.SerializeToString(mediaSource) + Environment.NewLine + Environment.NewLine + commandLineLogMessage + Environment.NewLine + Environment.NewLine);
|
|
|
|
|
_logFileStream.Write(commandLineLogMessageBytes, 0, commandLineLogMessageBytes.Length);
|
2016-02-12 00:01:38 -07:00
|
|
|
|
|
2016-09-29 05:55:49 -07:00
|
|
|
|
process.Exited += (sender, args) => OnFfMpegProcessExited(process, inputFile);
|
2016-02-12 00:01:38 -07:00
|
|
|
|
|
|
|
|
|
process.Start();
|
|
|
|
|
|
|
|
|
|
cancellationToken.Register(Stop);
|
|
|
|
|
|
|
|
|
|
// MUST read both stdout and stderr asynchronously or a deadlock may occurr
|
2016-07-01 19:16:05 -07:00
|
|
|
|
//process.BeginOutputReadLine();
|
2016-02-12 00:01:38 -07:00
|
|
|
|
|
2016-02-29 09:25:21 -07:00
|
|
|
|
onStarted();
|
|
|
|
|
|
2016-11-30 12:50:39 -07:00
|
|
|
|
// Important - don't await the log task or we won't be able to kill ffmpeg when the user stops playback
|
|
|
|
|
StartStreamingLog(process.StandardError.BaseStream, _logFileStream);
|
|
|
|
|
|
2018-12-13 06:18:25 -07:00
|
|
|
|
_logger.LogInformation("ffmpeg recording process started for {0}", _targetPath);
|
2016-09-30 11:43:59 -07:00
|
|
|
|
|
2016-06-21 21:40:11 -07:00
|
|
|
|
return _taskCompletionSource.Task;
|
2016-02-12 00:01:38 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-06-21 21:40:11 -07:00
|
|
|
|
private string GetCommandLineArgs(MediaSourceInfo mediaSource, string inputTempFile, string targetFile, TimeSpan duration)
|
2016-02-12 00:01:38 -07:00
|
|
|
|
{
|
|
|
|
|
string videoArgs;
|
|
|
|
|
if (EncodeVideo(mediaSource))
|
|
|
|
|
{
|
|
|
|
|
var maxBitrate = 25000000;
|
|
|
|
|
videoArgs = string.Format(
|
2016-09-30 11:43:59 -07:00
|
|
|
|
"-codec:v:0 libx264 -force_key_frames \"expr:gte(t,n_forced*5)\" {0} -pix_fmt yuv420p -preset superfast -crf 23 -b:v {1} -maxrate {1} -bufsize ({1}*2) -vsync -1 -profile:v high -level 41",
|
2016-02-12 00:01:38 -07:00
|
|
|
|
GetOutputSizeParam(),
|
|
|
|
|
maxBitrate.ToString(CultureInfo.InvariantCulture));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
videoArgs = "-codec:v:0 copy";
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-15 12:45:39 -07:00
|
|
|
|
videoArgs += " -fflags +genpts";
|
|
|
|
|
|
2016-06-21 21:40:11 -07:00
|
|
|
|
var durationParam = " -t " + _mediaEncoder.GetTimeParameter(duration.Ticks);
|
2018-09-12 10:26:21 -07:00
|
|
|
|
durationParam = string.Empty;
|
2017-04-21 13:03:07 -07:00
|
|
|
|
|
2017-05-02 05:53:21 -07:00
|
|
|
|
var flags = new List<string>();
|
|
|
|
|
if (mediaSource.IgnoreDts)
|
2017-04-21 13:03:07 -07:00
|
|
|
|
{
|
2017-05-02 05:53:21 -07:00
|
|
|
|
flags.Add("+igndts");
|
|
|
|
|
}
|
|
|
|
|
if (mediaSource.IgnoreIndex)
|
|
|
|
|
{
|
|
|
|
|
flags.Add("+ignidx");
|
2017-04-21 13:03:07 -07:00
|
|
|
|
}
|
2017-05-15 12:45:39 -07:00
|
|
|
|
if (mediaSource.GenPtsInput)
|
|
|
|
|
{
|
|
|
|
|
flags.Add("+genpts");
|
|
|
|
|
}
|
2017-04-21 13:03:07 -07:00
|
|
|
|
|
2017-05-21 00:25:49 -07:00
|
|
|
|
var inputModifier = "-async 1 -vsync -1";
|
2016-09-29 23:50:06 -07:00
|
|
|
|
|
2017-05-02 05:53:21 -07:00
|
|
|
|
if (flags.Count > 0)
|
|
|
|
|
{
|
2017-05-21 00:25:49 -07:00
|
|
|
|
inputModifier += " -fflags " + string.Join("", flags.ToArray());
|
2017-05-02 05:53:21 -07:00
|
|
|
|
}
|
2016-02-12 00:01:38 -07:00
|
|
|
|
|
2017-08-11 14:55:48 -07:00
|
|
|
|
var videoStream = mediaSource.VideoStream;
|
2018-09-12 10:26:21 -07:00
|
|
|
|
string videoDecoder = null;
|
2017-08-11 14:55:48 -07:00
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
|
if (!string.IsNullOrEmpty(videoDecoder))
|
2016-02-12 00:01:38 -07:00
|
|
|
|
{
|
2017-08-11 14:55:48 -07:00
|
|
|
|
inputModifier += " " + videoDecoder;
|
2016-09-29 23:50:06 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-02 05:53:21 -07:00
|
|
|
|
if (mediaSource.ReadAtNativeFramerate)
|
2016-09-29 23:50:06 -07:00
|
|
|
|
{
|
2017-05-21 00:25:49 -07:00
|
|
|
|
inputModifier += " -re";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (mediaSource.RequiresLooping)
|
|
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
|
inputModifier += " -stream_loop -1 -reconnect_at_eof 1 -reconnect_streamed 1 -reconnect_delay_max 2";
|
2016-02-12 00:01:38 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-09 10:25:33 -07:00
|
|
|
|
var analyzeDurationSeconds = 5;
|
|
|
|
|
var analyzeDuration = " -analyzeduration " +
|
|
|
|
|
(analyzeDurationSeconds * 1000000).ToString(CultureInfo.InvariantCulture);
|
2017-05-21 00:25:49 -07:00
|
|
|
|
inputModifier += analyzeDuration;
|
2016-11-09 10:25:33 -07:00
|
|
|
|
|
2017-02-21 11:04:35 -07:00
|
|
|
|
var subtitleArgs = CopySubtitles ? " -codec:s copy" : " -sn";
|
|
|
|
|
|
2017-05-03 14:53:33 -07:00
|
|
|
|
//var outputParam = string.Equals(Path.GetExtension(targetFile), ".mp4", StringComparison.OrdinalIgnoreCase) ?
|
|
|
|
|
// " -f mp4 -movflags frag_keyframe+empty_moov" :
|
|
|
|
|
// string.Empty;
|
|
|
|
|
|
|
|
|
|
var outputParam = string.Empty;
|
2017-03-25 21:22:30 -07:00
|
|
|
|
|
2019-01-07 16:27:46 -07:00
|
|
|
|
var commandLineArgs = string.Format("-i \"{0}\"{5} {2} -map_metadata -1 -threads 0 {3}{4}{6} -y \"{1}\"",
|
|
|
|
|
inputTempFile,
|
|
|
|
|
targetFile,
|
|
|
|
|
videoArgs,
|
|
|
|
|
GetAudioArgs(mediaSource),
|
|
|
|
|
subtitleArgs,
|
|
|
|
|
durationParam,
|
2017-05-02 05:53:21 -07:00
|
|
|
|
outputParam);
|
2016-02-12 00:01:38 -07:00
|
|
|
|
|
2017-05-21 00:25:49 -07:00
|
|
|
|
return inputModifier + " " + commandLineArgs;
|
2016-02-12 00:01:38 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetAudioArgs(MediaSourceInfo mediaSource)
|
|
|
|
|
{
|
|
|
|
|
var mediaStreams = mediaSource.MediaStreams ?? new List<MediaStream>();
|
2016-06-15 11:56:37 -07:00
|
|
|
|
var inputAudioCodec = mediaStreams.Where(i => i.Type == MediaStreamType.Audio).Select(i => i.Codec).FirstOrDefault() ?? string.Empty;
|
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
|
return "-codec:a:0 copy";
|
2016-02-12 00:01:38 -07:00
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
|
//var audioChannels = 2;
|
|
|
|
|
//var audioStream = mediaStreams.FirstOrDefault(i => i.Type == MediaStreamType.Audio);
|
|
|
|
|
//if (audioStream != null)
|
|
|
|
|
//{
|
|
|
|
|
// audioChannels = audioStream.Channels ?? audioChannels;
|
|
|
|
|
//}
|
|
|
|
|
//return "-codec:a:0 aac -strict experimental -ab 320000";
|
2016-02-12 00:01:38 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool EncodeVideo(MediaSourceInfo mediaSource)
|
|
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
|
return false;
|
2016-02-12 00:01:38 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected string GetOutputSizeParam()
|
|
|
|
|
{
|
|
|
|
|
var filters = new List<string>();
|
2018-09-12 10:26:21 -07:00
|
|
|
|
|
|
|
|
|
filters.Add("yadif=0:-1:0");
|
2016-02-12 00:01:38 -07:00
|
|
|
|
|
|
|
|
|
var output = string.Empty;
|
|
|
|
|
|
|
|
|
|
if (filters.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
output += string.Format(" -vf \"{0}\"", string.Join(",", filters.ToArray()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return output;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Stop()
|
|
|
|
|
{
|
|
|
|
|
if (!_hasExited)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2018-12-20 05:11:26 -07:00
|
|
|
|
_logger.LogInformation("Stopping ffmpeg recording process for {path}", _targetPath);
|
2016-02-12 00:01:38 -07:00
|
|
|
|
|
2016-11-30 12:50:39 -07:00
|
|
|
|
//process.Kill();
|
|
|
|
|
_process.StandardInput.WriteLine("q");
|
2016-02-12 00:01:38 -07:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2018-12-20 05:11:26 -07:00
|
|
|
|
_logger.LogError(ex, "Error stopping recording transcoding job for {path}", _targetPath);
|
2017-01-20 10:53:48 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_hasExited)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2018-12-20 05:11:26 -07:00
|
|
|
|
_logger.LogInformation("Calling recording process.WaitForExit for {path}", _targetPath);
|
2017-01-20 10:53:48 -07:00
|
|
|
|
|
2017-02-17 14:11:13 -07:00
|
|
|
|
if (_process.WaitForExit(10000))
|
2017-01-20 10:53:48 -07:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2018-12-20 05:11:26 -07:00
|
|
|
|
_logger.LogError(ex, "Error waiting for recording process to exit for {path}", _targetPath);
|
2017-01-20 10:53:48 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_hasExited)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2018-12-20 05:11:26 -07:00
|
|
|
|
_logger.LogInformation("Killing ffmpeg recording process for {path}", _targetPath);
|
2017-01-20 10:53:48 -07:00
|
|
|
|
|
|
|
|
|
_process.Kill();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2018-12-20 05:11:26 -07:00
|
|
|
|
_logger.LogError(ex, "Error killing recording transcoding job for {path}", _targetPath);
|
2016-02-12 00:01:38 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Processes the exited.
|
|
|
|
|
/// </summary>
|
2016-11-03 16:35:19 -07:00
|
|
|
|
private void OnFfMpegProcessExited(IProcess process, string inputFile)
|
2016-02-12 00:01:38 -07:00
|
|
|
|
{
|
|
|
|
|
_hasExited = true;
|
|
|
|
|
|
2016-11-30 12:50:39 -07:00
|
|
|
|
DisposeLogStream();
|
|
|
|
|
|
2016-02-12 00:01:38 -07:00
|
|
|
|
try
|
|
|
|
|
{
|
2016-11-30 12:50:39 -07:00
|
|
|
|
var exitCode = process.ExitCode;
|
2016-03-06 21:56:45 -07:00
|
|
|
|
|
2018-12-20 05:11:26 -07:00
|
|
|
|
_logger.LogInformation("FFMpeg recording exited with code {ExitCode} for {path}", exitCode, _targetPath);
|
2016-03-06 21:56:45 -07:00
|
|
|
|
|
|
|
|
|
if (exitCode == 0)
|
|
|
|
|
{
|
|
|
|
|
_taskCompletionSource.TrySetResult(true);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-12-20 05:11:26 -07:00
|
|
|
|
_taskCompletionSource.TrySetException(new Exception(string.Format("Recording for {path} failed. Exit code {ExitCode}", _targetPath, exitCode)));
|
2016-03-06 21:56:45 -07:00
|
|
|
|
}
|
2016-02-12 00:01:38 -07:00
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
2018-12-20 05:11:26 -07:00
|
|
|
|
_logger.LogError("FFMpeg recording exited with an error for {path}.", _targetPath);
|
|
|
|
|
_taskCompletionSource.TrySetException(new Exception(string.Format("Recording for {path} failed", _targetPath)));
|
2016-02-12 00:01:38 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-11-30 12:50:39 -07:00
|
|
|
|
|
|
|
|
|
private void DisposeLogStream()
|
|
|
|
|
{
|
|
|
|
|
if (_logFileStream != null)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_logFileStream.Dispose();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2018-12-20 05:11:26 -07:00
|
|
|
|
_logger.LogError(ex, "Error disposing recording log stream");
|
2016-11-30 12:50:39 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logFileStream = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async void StartStreamingLog(Stream source, Stream target)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using (var reader = new StreamReader(source))
|
|
|
|
|
{
|
|
|
|
|
while (!reader.EndOfStream)
|
|
|
|
|
{
|
|
|
|
|
var line = await reader.ReadLineAsync().ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
var bytes = Encoding.UTF8.GetBytes(Environment.NewLine + line);
|
|
|
|
|
|
|
|
|
|
await target.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false);
|
|
|
|
|
await target.FlushAsync().ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (ObjectDisposedException)
|
|
|
|
|
{
|
|
|
|
|
// Don't spam the log. This doesn't seem to throw in windows, but sometimes under linux
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2018-12-20 05:11:26 -07:00
|
|
|
|
_logger.LogError(ex, "Error reading ffmpeg recording log");
|
2016-11-30 12:50:39 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-02-12 00:01:38 -07:00
|
|
|
|
}
|
2018-12-13 06:18:25 -07:00
|
|
|
|
}
|