protected void SaveAll() { try { saving = true; FastSerialize.Serializer serializer = new FastSerialize.Serializer(typeof(FastSerialize.JsonSerializerGeneric)); var data = serializer.Serialize(presets); StreamWriter sw = new StreamWriter(File.Open("presets.json", FileMode.Create, FileAccess.Write, FileShare.Read)); using (sw) { sw.Write(data); } saving = false; } catch (OperationCanceledException) { log.Debug("Shutting down presets"); } catch (ThreadInterruptedException) { log.Debug("Shutting down presets"); } catch (ThreadAbortException) { log.Debug("Shutting down presets"); } catch (Exception e) { log.Error("Exception in prests file saving occured", e); } finally { saving = false; } }
static void Main(string[] args) { string file = "assembly.json"; List <string> assemblies = new List <string>(); if (args.Length == 1) { file = args[0]; } if (!File.Exists(file)) { System.Console.WriteLine("Unable to assembly.json file"); } StreamReader sw = new StreamReader(file); string data = sw.ReadToEnd(); sw.Close(); var serializer = new FastSerialize.Serializer(typeof(FastSerialize.JsonSerializerString)); assemblies = serializer.Deserialize <List <string> >(data); SchemaGenerator.AddGenerator(new ConfigurationSchemaGenerator()); SchemaGenerator.AddGenerator(new DataBoundObjectSchemaGenerator()); foreach (var assemblyPath in assemblies) { Console.WriteLine("Generating Schema for assembly " + assemblyPath); Console.WriteLine(SchemaGenerator.Generate(assemblyPath)); } Console.ReadLine(); }
public void Download() { try { if (downloaded) { if (this.probeInfo == null) { var data = FFmpeg.ExecProbe(buildProbeArguments(this.workingDir + dest)); if (string.IsNullOrEmpty(data)) { this.complete = true; this.failed = true; log.Debug("Failed to probe information from video file"); throw new JobFailureException("Failed to probe information from video file"); } var serializer = new FastSerialize.Serializer(typeof(FastSerialize.JsonSerializerGeneric)); this.probeInfo = serializer.Deserialize <ProbeInfo>(data, false); } this.complete = true; } else { //Download the file System.Net.WebClient wc = new System.Net.WebClient(); wc.DownloadFile(url, this.workingDir + dest); if (this.probeInfo == null) { var data = FFmpeg.ExecProbe(buildProbeArguments(this.workingDir + dest)); if (string.IsNullOrEmpty(data)) { this.complete = true; this.failed = true; log.Debug("Failed to probe information from video file"); throw new JobFailureException("Failed to probe information from video file"); } var serializer = new FastSerialize.Serializer(typeof(FastSerialize.JsonSerializerGeneric)); this.probeInfo = serializer.Deserialize <ProbeInfo>(data, false); } this.downloaded = true; this.complete = true; } } catch (Exception ex) { log.Error("An error occured Download()", ex); } }
public Presets() { presets = new ConcurrentDictionary <string, string>(); // Check if we have an existing preset file if (File.Exists("presets.json")) { FastSerialize.Serializer serializer = new FastSerialize.Serializer(typeof(FastSerialize.JsonSerializerGeneric)); StreamReader sr = new StreamReader(File.Open("presets.json", FileMode.Open, FileAccess.Read, FileShare.ReadWrite)); string data = sr.ReadToEnd(); sr.Close(); Dictionary <string, string> saved = serializer.Deserialize <Dictionary <string, string> >(data); foreach (var val in saved) { presets.TryAdd(val.Key, val.Value); } } cancelToken = new CancellationTokenSource(); // Start a background task to save the preset file on a regular basis presetSaveTask = TaskExecutorFactory.Begin(SaveAll, 5000, 100, Timeout.Infinite, -1, cancelToken.Token); }
public Presets() { presets = new ConcurrentDictionary<string,string>(); // Check if we have an existing preset file if (File.Exists("presets.json")) { FastSerialize.Serializer serializer = new FastSerialize.Serializer(typeof(FastSerialize.JsonSerializerGeneric)); StreamReader sr = new StreamReader(File.Open("presets.json", FileMode.Open, FileAccess.Read, FileShare.ReadWrite)); string data = sr.ReadToEnd(); sr.Close(); Dictionary<string, string> saved = serializer.Deserialize<Dictionary<string, string>>(data); foreach (var val in saved) { presets.TryAdd(val.Key, val.Value); } } cancelToken = new CancellationTokenSource(); // Start a background task to save the preset file on a regular basis presetSaveTask = TaskExecutorFactory.Begin(SaveAll, 5000, 100,Timeout.Infinite,-1,cancelToken.Token); }
public void NotifyException(Exception ex) { log.Error("Exeception occured for job: " + this.jobToken, ex); try { if (ex is JobFailureException || (ex.InnerException != null && ex.InnerException is JobFailureException)) { if (!string.IsNullOrEmpty(callbackUrl)) { try { if (this.callbackUrl == null) { return; } var request = WebRequest.Create(callbackUrl); request.Method = "POST"; var serializer = new FastSerialize.Serializer(typeof(FastSerialize.JsonSerializerGeneric)); var postData = "data=" + System.Web.HttpUtility.UrlEncode(serializer.Serialize(this)); byte[] rawData = Encoding.UTF8.GetBytes(postData); request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = rawData.Length; Stream dataStream = request.GetRequestStream(); dataStream.Write(rawData, 0, rawData.Length); dataStream.Close(); var response = request.GetResponse(); dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStream); string responseFromServer = reader.ReadToEnd(); reader.Close(); dataStream.Close(); response.Close(); System.Threading.Interlocked.Increment(ref this.markedTasks); } catch (Exception exInner) { log.Error("Failed to make http callback request inner " + this.callbackUrl, exInner); } } this.Fail(ex.Message); } else { this.message = ex.Message; if (taskSet.IsComplete) { if (flaggedComplete) status = "Incomplete"; else status = "Waiting"; } } } catch (Exception innerEx) { log.Error("An error occured handled error notification: " + this.jobToken, innerEx); } }
public void NotifyCompleted(object data) { log.Debug("Task completed: " + this.jobToken); try { if (data is TranscoderResult) { if (results != null) { if (FFRest.config["mode"] == "move") { var re = data as TranscoderResult; if (!string.IsNullOrEmpty(re.ResultFile)) { Directory.CreateDirectory(FFRest.config["file-root"] + Path.DirectorySeparatorChar + FFRest.config["video-destination"] + Path.DirectorySeparatorChar + this.jobToken); File.Delete(FFRest.config["file-root"] + Path.DirectorySeparatorChar + FFRest.config["video-destination"] + Path.DirectorySeparatorChar + this.jobToken + Path.DirectorySeparatorChar + re.ResultFile); if (File.Exists(this.workingDirectory + re.ResultFile)) { File.Move(this.workingDirectory + re.ResultFile, FFRest.config["file-root"] + Path.DirectorySeparatorChar + FFRest.config["video-destination"] + Path.DirectorySeparatorChar + this.jobToken + Path.DirectorySeparatorChar + re.ResultFile); } } if (re.HasSegments) { Directory.CreateDirectory(FFRest.config["file-root"] + Path.DirectorySeparatorChar + FFRest.config["video-destination"] + Path.DirectorySeparatorChar + this.jobToken); File.Delete(FFRest.config["file-root"] + Path.DirectorySeparatorChar + FFRest.config["video-destination"] + Path.DirectorySeparatorChar + this.jobToken + Path.DirectorySeparatorChar + re.PlaylistFile); if (File.Exists(this.workingDirectory + re.PlaylistFile)) { File.Move(this.workingDirectory + re.PlaylistFile, FFRest.config["file-root"] + Path.DirectorySeparatorChar + FFRest.config["video-destination"] + Path.DirectorySeparatorChar + this.jobToken + Path.DirectorySeparatorChar + re.PlaylistFile); } foreach (var file in re.SegmentsFiles) { File.Delete(FFRest.config["file-root"] + Path.DirectorySeparatorChar + FFRest.config["video-destination"] + Path.DirectorySeparatorChar + this.jobToken + Path.DirectorySeparatorChar + file); if (File.Exists(this.workingDirectory + file)) { File.Move(this.workingDirectory + file, FFRest.config["file-root"] + Path.DirectorySeparatorChar + FFRest.config["video-destination"] + Path.DirectorySeparatorChar + this.jobToken + Path.DirectorySeparatorChar + file); } } } StringBuilder sb = new StringBuilder(); sb.AppendLine("#EXTM3U"); foreach (var task in Results.OrderBy(x => x.BitRate)) { if (task.ResultFile != null) { if (task.HasSegments) { sb.AppendLine("#EXT-X-STREAM-INF:PROGRAM-ID=1, BANDWIDTH=" + (task.BitRate)); sb.AppendLine(task.Playlist); } } } using (StreamWriter sw = new StreamWriter(FFRest.config["file-root"] + Path.DirectorySeparatorChar + FFRest.config["video-destination"] + Path.DirectorySeparatorChar + this.jobToken + Path.DirectorySeparatorChar + "adaptive.m3u8")) { sw.Write(sb.ToString()); } } } if (!string.IsNullOrEmpty(callbackUrl)) { try { if (this.callbackUrl == null) return; var request = WebRequest.Create(callbackUrl); request.Method = "POST"; var serializer = new FastSerialize.Serializer(typeof(FastSerialize.JsonSerializerGeneric)); var postData = "data=" + System.Web.HttpUtility.UrlEncode(serializer.Serialize(data)); byte[] rawData = Encoding.UTF8.GetBytes(postData); request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = rawData.Length; Stream dataStream = request.GetRequestStream(); dataStream.Write(rawData, 0, rawData.Length); dataStream.Close(); var response = request.GetResponse(); dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStream); string responseFromServer = reader.ReadToEnd(); reader.Close(); dataStream.Close(); response.Close(); } catch (Exception ex) { log.Error("Failed to make http callback request: " + this.callbackUrl, ex); } System.Threading.Interlocked.Increment(ref this.markedTasks); } } if (taskSet.IsComplete && !this.failed) { if (flaggedComplete) { status = "Complete"; this.endTime = DateTime.Now; this.jobTime = this.endTime - this.startTime; } else status = "Waiting"; } } catch (Exception ex) { log.Error("An error occured handled completed notification: " + this.jobToken,ex); } }
public void Download() { try { if (downloaded) { if (this.probeInfo == null) { var data = FFmpeg.ExecProbe(buildProbeArguments(this.workingDir + dest)); if (string.IsNullOrEmpty(data)) { this.complete = true; this.failed = true; log.Debug("Failed to probe information from video file"); throw new JobFailureException("Failed to probe information from video file"); } var serializer = new FastSerialize.Serializer(typeof(FastSerialize.JsonSerializerGeneric)); this.probeInfo = serializer.Deserialize<ProbeInfo>(data, false); } this.complete = true; } else { //Download the file System.Net.WebClient wc = new System.Net.WebClient(); wc.DownloadFile(url, this.workingDir + dest); if (this.probeInfo == null) { var data = FFmpeg.ExecProbe(buildProbeArguments(this.workingDir + dest)); if (string.IsNullOrEmpty(data)) { this.complete = true; this.failed = true; log.Debug("Failed to probe information from video file"); throw new JobFailureException("Failed to probe information from video file"); } var serializer = new FastSerialize.Serializer(typeof(FastSerialize.JsonSerializerGeneric)); this.probeInfo = serializer.Deserialize<ProbeInfo>(data, false); } this.downloaded = true; this.complete = true; } } catch (Exception ex) { log.Error("An error occured Download()", ex); } }
public void NotifyException(Exception ex) { log.Error("Exeception occured for job: " + this.jobToken, ex); try { if (ex is JobFailureException || (ex.InnerException != null && ex.InnerException is JobFailureException)) { if (!string.IsNullOrEmpty(callbackUrl)) { try { if (this.callbackUrl == null) { return; } var request = WebRequest.Create(callbackUrl); request.Method = "POST"; var serializer = new FastSerialize.Serializer(typeof(FastSerialize.JsonSerializerGeneric)); var postData = "data=" + System.Web.HttpUtility.UrlEncode(serializer.Serialize(this)); byte[] rawData = Encoding.UTF8.GetBytes(postData); request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = rawData.Length; Stream dataStream = request.GetRequestStream(); dataStream.Write(rawData, 0, rawData.Length); dataStream.Close(); var response = request.GetResponse(); dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStream); string responseFromServer = reader.ReadToEnd(); reader.Close(); dataStream.Close(); response.Close(); System.Threading.Interlocked.Increment(ref this.markedTasks); } catch (Exception exInner) { log.Error("Failed to make http callback request inner " + this.callbackUrl, exInner); } } this.Fail(ex.Message); } else { this.message = ex.Message; if (taskSet.IsComplete) { if (flaggedComplete) { status = "Incomplete"; } else { status = "Waiting"; } } } } catch (Exception innerEx) { log.Error("An error occured handled error notification: " + this.jobToken, innerEx); } }
public void NotifyCompleted(object data) { log.Debug("Task completed: " + this.jobToken); try { if (data is TranscoderResult) { if (results != null) { if (FFRest.config["mode"] == "move") { var re = data as TranscoderResult; if (!string.IsNullOrEmpty(re.ResultFile)) { Directory.CreateDirectory(FFRest.config["file-root"] + Path.DirectorySeparatorChar + FFRest.config["video-destination"] + Path.DirectorySeparatorChar + this.jobToken); File.Delete(FFRest.config["file-root"] + Path.DirectorySeparatorChar + FFRest.config["video-destination"] + Path.DirectorySeparatorChar + this.jobToken + Path.DirectorySeparatorChar + re.ResultFile); if (File.Exists(this.workingDirectory + re.ResultFile)) { File.Move(this.workingDirectory + re.ResultFile, FFRest.config["file-root"] + Path.DirectorySeparatorChar + FFRest.config["video-destination"] + Path.DirectorySeparatorChar + this.jobToken + Path.DirectorySeparatorChar + re.ResultFile); } } if (re.HasSegments) { Directory.CreateDirectory(FFRest.config["file-root"] + Path.DirectorySeparatorChar + FFRest.config["video-destination"] + Path.DirectorySeparatorChar + this.jobToken); File.Delete(FFRest.config["file-root"] + Path.DirectorySeparatorChar + FFRest.config["video-destination"] + Path.DirectorySeparatorChar + this.jobToken + Path.DirectorySeparatorChar + re.PlaylistFile); if (File.Exists(this.workingDirectory + re.PlaylistFile)) { File.Move(this.workingDirectory + re.PlaylistFile, FFRest.config["file-root"] + Path.DirectorySeparatorChar + FFRest.config["video-destination"] + Path.DirectorySeparatorChar + this.jobToken + Path.DirectorySeparatorChar + re.PlaylistFile); } foreach (var file in re.SegmentsFiles) { File.Delete(FFRest.config["file-root"] + Path.DirectorySeparatorChar + FFRest.config["video-destination"] + Path.DirectorySeparatorChar + this.jobToken + Path.DirectorySeparatorChar + file); if (File.Exists(this.workingDirectory + file)) { File.Move(this.workingDirectory + file, FFRest.config["file-root"] + Path.DirectorySeparatorChar + FFRest.config["video-destination"] + Path.DirectorySeparatorChar + this.jobToken + Path.DirectorySeparatorChar + file); } } } StringBuilder sb = new StringBuilder(); sb.AppendLine("#EXTM3U"); foreach (var task in Results.OrderBy(x => x.BitRate)) { if (task.ResultFile != null) { if (task.HasSegments) { sb.AppendLine("#EXT-X-STREAM-INF:PROGRAM-ID=1, BANDWIDTH=" + (task.BitRate)); sb.AppendLine(task.Playlist); } } } using (StreamWriter sw = new StreamWriter(FFRest.config["file-root"] + Path.DirectorySeparatorChar + FFRest.config["video-destination"] + Path.DirectorySeparatorChar + this.jobToken + Path.DirectorySeparatorChar + "adaptive.m3u8")) { sw.Write(sb.ToString()); } } } if (!string.IsNullOrEmpty(callbackUrl)) { try { if (this.callbackUrl == null) { return; } var request = WebRequest.Create(callbackUrl); request.Method = "POST"; var serializer = new FastSerialize.Serializer(typeof(FastSerialize.JsonSerializerGeneric)); var postData = "data=" + System.Web.HttpUtility.UrlEncode(serializer.Serialize(data)); byte[] rawData = Encoding.UTF8.GetBytes(postData); request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = rawData.Length; Stream dataStream = request.GetRequestStream(); dataStream.Write(rawData, 0, rawData.Length); dataStream.Close(); var response = request.GetResponse(); dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStream); string responseFromServer = reader.ReadToEnd(); reader.Close(); dataStream.Close(); response.Close(); } catch (Exception ex) { log.Error("Failed to make http callback request: " + this.callbackUrl, ex); } System.Threading.Interlocked.Increment(ref this.markedTasks); } } if (taskSet.IsComplete && !this.failed) { if (flaggedComplete) { status = "Complete"; this.endTime = DateTime.Now; this.jobTime = this.endTime - this.startTime; } else { status = "Waiting"; } } } catch (Exception ex) { log.Error("An error occured handled completed notification: " + this.jobToken, ex); } }