public void QueueRecording(ChannelHistory channelHistory, RecordInfo recordInfo, IConfiguration configuration, bool useLogFlag) { //Write to our very own log as there might be other captures going too StreamWriter logWriter = new StreamWriter(Console.OpenStandardOutput()); if (useLogFlag) { string logPath = Path.Combine(configuration["logPath"], recordInfo.fileName + "Log.txt"); FileStream fileHandle = new FileStream(logPath, FileMode.OpenOrCreate, FileAccess.Write); logWriter = new StreamWriter(fileHandle); } logWriter.AutoFlush = true; //try-catch so we don't crash the whole thing try { //Dump logWriter.WriteLine($"{DateTime.Now}: Queuing show: {recordInfo.description} Starting on {recordInfo.GetStartDT()} for {recordInfo.GetDuration()} minutes ({recordInfo.GetDuration() / 60}hrs ish)"); //Wait here until we're ready to start recording if (recordInfo.strStartDT != null) { TimeSpan oneHour = new TimeSpan(1, 0, 0); DateTime recStart = recordInfo.GetStartDT(); TimeSpan timeToWait = recStart - DateTime.Now; logWriter.WriteLine($"{DateTime.Now}: Starting recording at {recStart} - Waiting for {timeToWait.Days} Days, {timeToWait.Hours} Hours, and {timeToWait.Minutes} minutes."); while (timeToWait.Seconds >= 0 && DateTime.Now < recStart && !recordInfo.cancelledFlag) { if (timeToWait > oneHour) { timeToWait = oneHour; } if (timeToWait.Seconds >= 0) { recordInfo.mre.WaitOne(timeToWait); mre.Reset(); logWriter.WriteLine($"{DateTime.Now}: Waking up to check..."); } timeToWait = recStart - DateTime.Now; } if (recordInfo.cancelledFlag) { logWriter.WriteLine($"{DateTime.Now}: Cancelling due to request"); recordInfo.queuedFlag = false; recordInfo.processSpawnedFlag = false; return; } } //Authenticate Task <string> authTask = Authenticate(); string hashValue = authTask.Result; if (string.IsNullOrEmpty(hashValue)) { Console.WriteLine($"ERROR: Unable to authenticate. Check username and password?"); Environment.Exit(1); } //Get latest channels (Channels may have changed since the show was queued. Exception is thrown if time has changed, or no longer there) logWriter.WriteLine($"{DateTime.Now}: Grabbing latest channels"); new Schedule().RefreshChannelList(configuration, recordInfo); //We need to manage our resulting files VideoFileManager videoFileManager = new VideoFileManager(configuration, logWriter, recordInfo.fileName); //Set capture started flag recordInfo.captureStartedFlag = true; //Capture stream CaptureStream(logWriter, hashValue, channelHistory, recordInfo, videoFileManager); //Let's take care of processing and publishing the video files videoFileManager.ConcatFiles(); videoFileManager.MuxFile(recordInfo.description); videoFileManager.PublishAndCleanUpAfterCapture(recordInfo.category, recordInfo.preMinutes); //Cleanup logWriter.WriteLine($"{DateTime.Now}: Done Capturing"); logWriter.Dispose(); //Send alert mail if (recordInfo.emailFlag) { new Mailer().SendShowReadyMail(configuration, recordInfo); } } catch (Exception e) { logWriter.WriteLine("======================"); logWriter.WriteLine($"{DateTime.Now}: ERROR - Exception!"); logWriter.WriteLine("======================"); logWriter.WriteLine($"{e.Message}\n{e.StackTrace}"); //Send alert mail string body = recordInfo.description + " failed with Exception " + e.Message; body = body + "\n" + e.StackTrace; new Mailer().SendErrorMail(configuration, "StreamCapture Exception! (" + e.Message + ")", body); } }
public void QueueRecording(ChannelHistory channelHistory, RecordInfo recordInfo, IConfiguration configuration, bool useLogFlag) { //Write to our very own log as there might be other captures going too StreamWriter logWriter = new StreamWriter(Console.OpenStandardOutput()); if (useLogFlag) { string logPath = Path.Combine(configuration["logPath"], recordInfo.fileName + "Log.txt"); FileStream fileHandle = new FileStream(logPath, FileMode.OpenOrCreate, FileAccess.Write); logWriter = new StreamWriter(fileHandle); } logWriter.AutoFlush = true; //try-catch so we don't crash the whole thing try { //Dump DumpRecordInfo(logWriter, recordInfo); //Wait here until we're ready to start recording if (recordInfo.strStartDT != null) { DateTime recStart = recordInfo.GetStartDT(); TimeSpan timeToWait = recStart - DateTime.Now; logWriter.WriteLine($"{DateTime.Now}: Starting recording at {recStart} - Waiting for {timeToWait.Days} Days, {timeToWait.Hours} Hours, and {timeToWait.Minutes} minutes."); if (timeToWait.Seconds >= 0) { Thread.Sleep(timeToWait); } } //Authenticate Task <string> authTask = Authenticate(); string hashValue = authTask.Result; if (string.IsNullOrEmpty(hashValue)) { Console.WriteLine($"ERROR: Unable to authenticate. Check username and password?"); Environment.Exit(1); } //We need to manage our resulting files VideoFileManager videoFileManager = new VideoFileManager(configuration, logWriter, recordInfo.fileName); //Capture stream CaptureStream(logWriter, hashValue, channelHistory, recordInfo, videoFileManager); //Let's take care of processing and publishing the video files videoFileManager.ConcatFiles(); videoFileManager.MuxFile(recordInfo.description); videoFileManager.PublishAndCleanUpAfterCapture(recordInfo.category); //Cleanup logWriter.WriteLine($"{DateTime.Now}: Done Capturing"); logWriter.Dispose(); //Send alert mail if (recordInfo.emailFlag) { new Mailer().SendShowReadyMail(configuration, recordInfo); } } catch (Exception e) { logWriter.WriteLine("======================"); logWriter.WriteLine($"{DateTime.Now}: ERROR - Exception!"); logWriter.WriteLine("======================"); logWriter.WriteLine($"{e.Message}\n{e.StackTrace}"); //Send alert mail string body = recordInfo.description + " failed with Exception " + e.Message; body = body + "\n" + e.StackTrace; new Mailer().SendErrorMail(configuration, "StreamCapture Exception! (" + e.Message + ")", body); } }