/// <summary> /// Calls record create handler and updates status if success. /// </summary> /// <param name="handler">BroadcastLoggerHandler instance.</param> /// <param name="cur">Current item in queue.</param> private void recordCreateHelper(BroadcastLoggerHandler handler, StorageResult cur) { bool realtime = false; Console.WriteLine("\n\n\n\n"); Console.WriteLine("DateTime.UtcNow: " + DateTime.UtcNow); Console.WriteLine("cur.CreationDate.toUniversalTime(): " + cur.CreationDate.ToUniversalTime()); Console.WriteLine("\n\n\n\n"); // Set realtime if this item's Creation Date is less than two seconds from the current time. if ((int)(cur.CreationDate.Subtract(DateTime.UtcNow)).TotalMilliseconds < 2000) { realtime = true; } bool succeeded = false; succeeded = handler.recordingCreate(cur.StationID, authCode, cur.CreationDate.ToUniversalTime(), realtime); if (succeeded) { cur.RecordID = handler.RecordingId.ToString(); storage.UpdateRecordID(Int32.Parse(cur.UploadID), cur.RecordID); cur.Status = Status.ID_CREATED; storage.UpdateStatus(Int32.Parse(cur.UploadID), Status.ID_CREATED); } else { Console.WriteLine(handler.ErrorMessage); } }
//-------------------------------------------------------------------------------------------- /// <summary> /// Recording loop, called in main loop. Called by timer to start every hour on the hour. /// </summary> public async void record(Object obj = null) { int count = 0; while (!recordStop) { Console.WriteLine("\n\n\n"); Console.WriteLine("---------------- " + ++count + " -------------------"); StatusMessage = "Creating new recording at " + DateTime.Now + "..."; LogWriter.Write("Creating new recording at " + DateTime.Now + "..."); Console.WriteLine("\n\n\n"); String localFileName = ""; timeLeft = (uint)setRecordingTime(); //timeLeft = 5; // ### ADD BACK IN ON RELEASE // Get the bitrate through authStation, in case it has been updated. var handler = new BroadcastLoggerHandler(); bool succeeded = handler.authStation(stationId, authCode); if (succeeded) { bitRate = handler.BitRate; } else { StatusMessage = handler.ErrorMessage + "\nBacklogging record."; LogWriter.Write("Backlogging record"); } localFileName = new FFmpegHandler("ffmpeg", "ffmpeg.exe") .beginRecording(audioDevice, timeLeft, bitRate); storage.AddToQueue(stationId, localFileName); await Task.Delay((int)timeLeft * 1000); } }
/// <summary> /// Calls setAsUploaded handler and updates status if success. /// </summary> /// <param name="handler">BroadcastLoggerHandler instance.</param> /// <param name="cur">Current item in queue.</param> private void recordUploadedHelper(BroadcastLoggerHandler handler, StorageResult cur) { bool succeeded = handler.setAsUploaded(cur.StationID, authCode, Int32.Parse(cur.RecordID)); if (succeeded) { cur.Status = Status.SET_UPLOADED; storage.UpdateStatus(Int32.Parse(cur.UploadID), Status.SET_UPLOADED); } }
/// <summary> /// Calls authStation handler and updates status if success. /// </summary> /// <param name="handler">BroadcastLoggerHandler instance.</param> /// <param name="cur">Current item in queue.</param> private void authHelper(BroadcastLoggerHandler handler, StorageResult cur) { // boolean var is used to help with code clarity. // handler has a Status property, but we used this var to avoid // the possibility of overwriting the property. bool succeeded = handler.authStation(cur.StationID, authCode); if (succeeded) { cur.Status = Status.AUTHORIZED; storage.UpdateStatus(Int32.Parse(cur.UploadID), Status.AUTHORIZED); } }
//-------------------------------------------------------------------------------------------- /// <summary> /// Setup database/queue. /// </summary> public void Initialization() { //ffmpegFirst = new FFmpegHandler("ffmpeg", "ffmpeg.exe"); //ffmpegSecond = new FFmpegHandler("ffmpeg", "ffmpeg.exe"); //ffmpegFirst.FinishedRecording += ffmpegFirst_FinishedRecording; storage = new Storage(); //storage2 = new Storage(); var handler = new BroadcastLoggerHandler(); handler.authStation(stationId, authCode); BitRate = handler.BitRate; storage.AddStation(stationId, authCode); storage.SetStationName(stationId, "Example DB"); //ffmpegFirstDone = true; //ffmpegSecondDone = true; }
/// <summary> /// Calls record done handler and updates status if success. /// </summary> /// <param name="handler">BroadcastLoggerHandler instance.</param> /// <param name="cur">Current item in queue.</param> /// <param name="next">Next item in queue.</param> private void recordDoneHelper(BroadcastLoggerHandler handler, StorageResult cur, StorageResult next) { // First check to see if cur has finished recording; otherwise, it will still be in use // and any attempt to upload it will throw an IOException. if (isFileAvailable(cur.LocalFileName)) { // Get the AWS object name (URL to upload to Amazon S3). bool succeeded = handler.recordingDone(cur.StationID, authCode, Int32.Parse(cur.RecordID), Int32.Parse(next.RecordID)); if (succeeded) { cur.AWSFileName = handler.ObjectName; storage.UpdateAWSFileName(Int32.Parse(cur.UploadID), cur.AWSFileName); cur.Status = Status.REC_SET_DONE; storage.UpdateStatus(Int32.Parse(cur.UploadID), Status.REC_SET_DONE); } } }
//-------------------------------------------------------------------------------------------- /// <summary> /// Handles items in queue and uploads them. /// </summary> /// <param name="obj">Temporary object to allow compatibility with delegate.</param> public void handleQueue(Object obj = null) { Console.Write("----------------handleQueue----------------"); if (isHandlingQueue) { return; } else { isHandlingQueue = true; } // Load items from SQLite database into a List of StorageResult objects List <StorageResult> results = storage.GetSeveral(storage.Length); // Process each item in the list for (int i = 0; i < results.Count; i++) { // Instantiate new handlers each time to ensure a fresh start. BroadcastLoggerHandler handler = new BroadcastLoggerHandler(); AmazonHandler amazonHandler = new AmazonHandler(); Console.WriteLine("storage.Length: " + storage.Length); LogWriter.Write("Storage.Length: " + storage.Length); StorageResult cur = null, next = null; if (results[i] != null) { Console.WriteLine("cur = results[i]"); cur = results[i]; } else { continue; } if (i + 1 < results.Count && results[i + 1] != null) { Console.WriteLine("next = results[i + 1]"); next = results[i + 1]; } // Authorize current item. // Create a local variable to lock in the auth code for the loop, // just in case it changes. if (cur.Status == Status.NEW) { Console.WriteLine("---------- STATUS --------- NEW"); LogWriter.Write("Status : New"); authHelper(handler, cur); } // Get recording ID if (cur.RecordID == "" && cur.Status == Status.AUTHORIZED) { Console.WriteLine("---------- STATUS --------- AUTHORIZED"); LogWriter.Write("Status: Authorized"); recordCreateHelper(handler, cur); } // Set recording as 'Done' only if cur AND next both have a recording ID; if (cur.Status == Status.ID_CREATED && next != null && next.Status == Status.ID_CREATED) { Console.WriteLine("---------- STATUS --------- ID_CREATED"); LogWriter.Write("Status: ID_Created"); recordDoneHelper(handler, cur, next); } else if (recordStop && cur.Status == Status.ID_CREATED) { Console.WriteLine("---------- STATUS --------- ID_CREATED"); LogWriter.Write("Status: ID_Created"); singleRecordDoneHelper(handler, cur); } // Upload audio log. if (cur.Status == Status.REC_SET_DONE) { Console.WriteLine("---------- STATUS --------- REC_SET_DONE"); LogWriter.Write("Status: REC_SET_DONE"); amazonHelper(amazonHandler, cur); } // Set audio log as uploaded. if (cur.Status == Status.UPLOADED) { Console.WriteLine("---------- STATUS --------- UPLOADED"); LogWriter.Write("Status: UPLOADED"); recordUploadedHelper(handler, cur); } // Delete the local file. if (cur.Status == Status.SET_UPLOADED) { Console.WriteLine("---------- STATUS --------- SET_UPLOADED"); LogWriter.Write("Status: SET_UPLOADED"); try { deleteFile(cur.LocalFileName); // May lead to build up of temp files. storage.Pop(); } catch (IOException ex) { StatusMessage = "Unable to delete " + cur.LocalFileName; LogWriter.Write("Unable to delete: " + cur.LocalFileName); Console.Write(ex.StackTrace); } } } isHandlingQueue = false; if (recordStop) { StatusMessage = "Recording Stopped."; if (RecordingStopped != null) { RecordingStopped(); } } }