動画の分散エンコードクラスタを自作・運用している話・80
音声を出力したい2
音声の出力は出来てるじゃん?って話なんですけど、よく考えてください。
「音声の出力」ってエンコードの処理なんですよね。それならマネージャよりもっと適任がいるはずです。そう、ノードですね。
なので、この音声のエンコードをノードに任せたいのが今回のチャレンジになります。
構造の改良
今まで1種類だったジョブですが、音声のエンコードは今までと異なる手続きで処理されていくので、全体の作り替えが必要になります。
なので、ますそれを終わらせましょう。
まずはマネージャ側。
public abstract partial class OPERATION
{
public abstract bool CheckFinish(APPLICATION AppData, Dictionary<string, string> recieve_data);
public string Type;
public Dictionary<string, string> Arg;
public string DstPath;
public bool finish;
public int FrameLength;
public DateTime StartTime;
public DateTime EndTime;
public double Progress;
public Dictionary<string, int> ErrorHistory;
public OPERATION(string op_type, Dictionary<string,string> data)
{
Type = op_type;
Arg = data;
DstPath = data[JOB_DATA.OUTPUT];
FrameLength = data[JOB_DATA.END].ToInt()- data[JOB_DATA.START].ToInt();
StartTime = DateTime.MinValue;
EndTime = DateTime.MinValue;
finish = false;
Progress = 0;
ErrorHistory = new Dictionary<string, int>();
}
}
public partial class OPERATION
{
public int ErrorCountUp(string server_name)
{
if (ErrorHistory.Keys.Contains(server_name) == true)
ErrorHistory[server_name]++;
else
ErrorHistory[server_name] = 1;
return ErrorHistory[server_name];
}
public int ErrorCount(string server_name)
{
int ans = 0;
if (ErrorHistory.Keys.Contains(server_name) == true)
ans = ErrorHistory[server_name];
return ans;
}
public void ResetOperation()
{
while (File.Exists(this.DstPath) == true)
{
File.Delete(this.DstPath);
Thread.Sleep(100);
}
this.StartTime = DateTime.MinValue;
}
public double GetPassedSecond()
{
if (this.StartTime == DateTime.MinValue)
return -1;
if (this.EndTime > this.StartTime)
return (this.EndTime - this.StartTime).TotalSeconds;
return (DateTime.Now - this.StartTime).TotalSeconds;
}
}
public partial class OPERATION
{
public static string CreateJsonString(Dictionary<string, string> data)
{
return JsonSerializer.Serialize(data);
}
}
public struct OPERATION_TYPE
{
public const string VIDEO = "VIDEO";
}public class OPERATION_CREATER
{
public static OPERATION CreateOperation(Dictionary<string, string> data)
{
OPERATION op = null;
if (data[JOB_DATA.TYPE] == OPERATION_TYPE.VIDEO)
{
op = new OPERATION_VIDEO(data);
}
return op;
}
}public void CreateJobList(uint initial_split_num, uint min_frame_size)
{
~省略~
var json_string = new Dictionary<string, string>();
{
json_string[JOB_DATA.TYPE] = OPERATION_TYPE.VIDEO;
json_string[JOB_DATA.INPUT] = this.InputPath;
json_string[JOB_DATA.OUTPUT] = dst_path;
json_string[JOB_DATA.START] = $"{start_frame}";
json_string[JOB_DATA.END] = $"{end_frame}";
}
var op = OPERATION_CREATER.CreateOperation(json_string);
if (op != null)
this.OperationList.Add(op);
}
}public partial class OPERATION_VIDEO : OPERATION
{
public OPERATION_VIDEO(Dictionary<string, string> data) : base(OPERATION_TYPE.VIDEO, data) { }
public override bool CheckFinish(APPLICATION AppData, Dictionary<string, string> recieve_data)
{
if (recieve_data.Keys.Contains(JOB_DATA.PROGRESS) == true)
this.Progress = recieve_data[JOB_DATA.PROGRESS].ToDouble(0);
if (File.Exists(this.DstPath) == false)
return false;
var result_length = APPLICATION.GetFileFrame(AppData, this.DstPath);
var job_length = this.FrameLength;
if (result_length != job_length)
return false;
this.EndTime = DateTime.Now;
this.finish = true;
return true;
}
}そしてノード側。
public bool SameCommand(Dictionary<string, string> old_command,
Dictionary<string, string> new_command)
{
var ans = true;
if (old_command == null)
return false;
if (CompareValue(old_command, new_command, JOB_DATA.TYPE) == false)
ans = false;
~省略~
return ans;
}public struct JOB_DATA
{
public const string TYPE = "TYPE";
}
public struct OPERATION_TYPE
{
public const string VIDEO = "VIDEO";
}主な変更は、従来のジョブの基底クラスとして「OPERATION」クラスを導入し、それを継承した「OPERATION_VIDEO」クラスを配置しました。外部からは、「OPERATION」クラス経由でアクセスします。
また、オブジェクトの生成器としての「OPERATION_CREATER」クラスを導入して、OPERATIONから派生するクラスは、この部分で生成するようにします。ついでに「JOB」クラスを「OPERATION」クラスに名称変更したため、関連するほとんどの場所が変更になっています。
ノード側はそれに合わせて、STATUSと一緒にやってくるJOB_DATA.TYPEを処理できるように整えた程度です。(情報は特に使っていません)
機能の追加
本命となる音声エンコードジョブへの対応です。
まずはノード側。
public class SERVER_STATE_READY : SERVER_STATE_BASE
{
public override string Transition(APPLICATION AppData, Dictionary<string, string> recieve_msg)
{
~省略~
if (command == JOB_COMMAND.START)
{
AppData.State = new SERVER_STATE_START();
send_msg = $"{AppData.State.Type()}";
AppData.RecieveCommand = recieve_msg;
AppData.Task = OPERATION_SELECTOR.Select(AppData, recieve_msg);
}
~省略~
}
}public static class OPERATION_SELECTOR
{
public static Task<string> Select(APPLICATION AppData, Dictionary<string, string> recieve_data)
{
Task<string> task = null;
switch (recieve_data[JOB_DATA.TYPE])
{
case OPERATION_TYPE.VIDEO:
task = new OPERATION_VIDEO().StartOperation(AppData, recieve_data);
break;
case OPERATION_TYPE.AUDIO:
task = new OPERATION_AUDIO().StartOperation(AppData, recieve_data);
break;
}
return task;
}
}public class OPERATION_VIDEO
{
public Task<string> StartOperation(APPLICATION AppData, Dictionary<string, string> recieve_data)
public string Operation(APPLICATION AppData, Dictionary<string, string> data)
private static void OutputDataReceived(object sender, DataReceivedEventArgs e)
}public class OPERATION_AUDIO
{
public Task<string> StartOperation(APPLICATION AppData, Dictionary<string, string> recieve_data)
{
var msg = new string[]
{
"\nStart : ",
$"{recieve_data[JOB_DATA.INPUT]}",
"\n\t > [",
$"{recieve_data[JOB_DATA.TYPE]}",
"] ",
$"{recieve_data[JOB_DATA.OUTPUT]}",
}.Aggregate((a, b) => $"{a}{b}");
Console.WriteLine(msg);
AppData.CTS = new CancellationTokenSource();
// 計算の実行
return Task.Run(() => Operation(AppData, recieve_data));
}
public string Operation(APPLICATION AppData, Dictionary<string, string> data)
{
using (var p = new Process())
{
p.StartInfo.FileName = AppData.FFmpegPath;
p.StartInfo.Arguments = new List<string>()
{
$@"-i",
$@"""{data[JOB_DATA.INPUT]}""",
$@"-c:a aac -b:a 192k",
$@"""{data[JOB_DATA.OUTPUT]}""",
}.Aggregate((a, b) => $@"{a} {b}");
p.Start();
while (p.HasExited == false)
{
if (AppData.CTS.IsCancellationRequested == true)
{
p.Kill();
return JOB_STATUS.TERMINATED;
}
Thread.Sleep(100);
}
}
return JOB_STATUS.FINISH;
}
}そしてマネージャ側。
switch (recieve_data[JOB_DATA.STATUS])
{
// 処理完了
case JOB_STATUS.FINISH:
if (this.Operation != null)
{
if (is_job_finish == true)
{
this.Operation.FinishOperation();
task_status = this.FinishOperation(AppData);
}
~省略~
break;
~省略~public abstract partial class OPERATION
{
public abstract bool CheckFinish(APPLICATION AppData, Dictionary<string, string> recieve_data);
public virtual double GetEstimateTime(APPLICATION AppData, double speed) => 0;
public virtual double GetOperationSpeed(APPLICATION AppData, double time_sec) => 0;
public virtual double GetRemainFrame(APPLICATION AppData) => 0;
public string Type;
public Dictionary<string, string> Arg;
public string DstPath;
public bool finish;
public int OperationSize;
public DateTime StartTime;
public DateTime EndTime;
public double Progress;
public Dictionary<string, int> ErrorHistory;
public OPERATION(string op_type, Dictionary<string,string> data, int op_size)
{
Type = op_type;
Arg = data;
OperationSize = op_size;
DstPath = data[JOB_DATA.OUTPUT];
StartTime = DateTime.MinValue;
EndTime = DateTime.MinValue;
finish = false;
Progress = 0;
ErrorHistory = new Dictionary<string, int>();
}
}
public partial class OPERATION
{
public double GetLimitTime(APPLICATION AppData, double speed)
=> GetEstimateTime(AppData, speed) * AppData.TimeLimitRate;
public int ErrorCountUp(string server_name)
{
if (ErrorHistory.Keys.Contains(server_name) == true)
ErrorHistory[server_name]++;
else
ErrorHistory[server_name] = 1;
return ErrorHistory[server_name];
}
public int ErrorCount(string server_name)
{
int ans = 0;
if (ErrorHistory.Keys.Contains(server_name) == true)
ans = ErrorHistory[server_name];
return ans;
}
public void FinishOperation()
{
this.EndTime = DateTime.Now;
this.finish = true;
}
public void ResetOperation()
{
while (File.Exists(this.DstPath) == true)
{
File.Delete(this.DstPath);
Thread.Sleep(100);
}
this.StartTime = DateTime.MinValue;
}
public double GetPassedSecond()
{
if (this.StartTime == DateTime.MinValue)
return -1;
if (this.EndTime > this.StartTime)
return (this.EndTime - this.StartTime).TotalSeconds;
return (DateTime.Now - this.StartTime).TotalSeconds;
}
}
public partial class OPERATION
{
public static string CreateJsonString(Dictionary<string, string> data)
{
return JsonSerializer.Serialize(data);
}
}
public struct OPERATION_TYPE
{
public const string VIDEO = "VIDEO";
public const string AUDIO = "AUDIO";
}
public class OPERATION_CREATER
{
public static OPERATION CreateOperation(Dictionary<string, string> data, int op_size)
{
OPERATION op = null;
switch (data[JOB_DATA.TYPE])
{
case OPERATION_TYPE.VIDEO:
op = new OPERATION_VIDEO(data, op_size);
break;
case OPERATION_TYPE.AUDIO:
op = new OPERATION_AUDIO(data, op_size);
break;
}
return op;
}
}public partial class OPERATION_VIDEO : OPERATION
{
public OPERATION_VIDEO(Dictionary<string, string> data, int frame_length) : base(OPERATION_TYPE.VIDEO, data, frame_length)
{
OperationSize = data[JOB_DATA.END].ToInt() - data[JOB_DATA.START].ToInt();
}
public override bool CheckFinish(APPLICATION AppData, Dictionary<string, string> recieve_data)
{
if (recieve_data.Keys.Contains(JOB_DATA.PROGRESS) == true)
this.Progress = recieve_data[JOB_DATA.PROGRESS].ToDouble(0);
if (File.Exists(this.DstPath) == false)
return false;
var result_length = APPLICATION.GetFileFrame(AppData, this.DstPath);
var job_length = this.OperationSize;
if (result_length != job_length)
return false;
return true;
}
public override double GetRemainFrame(APPLICATION AppData)
=> this.OperationSize * (100 - this.Progress) / 100;
public override double GetEstimateTime(APPLICATION AppData, double speed)
=> this.OperationSize / speed;
public override double GetOperationSpeed(APPLICATION AppData, double time)
=> this.OperationSize / time;
}public partial class OPERATION_AUDIO : OPERATION
{
public OPERATION_AUDIO(Dictionary<string, string> data, int frame_length) : base(OPERATION_TYPE.AUDIO, data, frame_length) { }
public override bool CheckFinish(APPLICATION AppData, Dictionary<string, string> recieve_data)
{
if (File.Exists(this.DstPath) == false)
return false;
return true;
}
}public string FinishOperation(APPLICATION AppData)
{
var passed_time = this.Operation.GetPassedSecond();
var dst_file_name = $@"{Path.GetFileName(this.Operation.DstPath)}";
var task_status = new List<string>()
{
$"{this.Name} :"
};
switch (this.Operation.Type)
{
case OPERATION_TYPE.VIDEO:
this.Speed = this.Operation.OperationSize / passed_time;
task_status.Add($"{this.Operation.Progress:0.0}%");
task_status.Add($"{dst_file_name} Compelete.");
task_status.Add($"{this.Operation.OperationSize}frames");
task_status.Add($"{passed_time:0.0}s");
task_status.Add($"{this.Speed:0.0}fps");
break;
case OPERATION_TYPE.AUDIO:
task_status.Add($"{dst_file_name} Compelete.");
task_status.Add($"{passed_time:0.0}s");
break;
}
return task_status.ToList().Aggregate((a, b) => $"{a} {b}");
}public string OperatingOperation(APPLICATION AppData)
{
var passed_time = this.Operation.GetPassedSecond();
var dst_file_name = $@"{Path.GetFileName(this.Operation.DstPath)}";
var task_status = new List<string>()
{
$"{this.Name} :",
};
switch (this.Operation.Type)
{
case OPERATION_TYPE.VIDEO:
task_status.Add($"{this.Operation.Progress:0.0}%");
task_status.Add($"{dst_file_name} Operating.");
task_status.Add($"{passed_time:0.0}s/{this.Operation.OperationSize / this.Speed:0.0}s");
break;
case OPERATION_TYPE.AUDIO:
task_status.Add($"{dst_file_name} Operating.");
task_status.Add($"{passed_time:0.0}s");
break;
}
return task_status.ToList().Aggregate((a, b) => $"{a} {b}");
}前段で変更した抽象化した基底クラスを拡張して、音声エンコード用の「OPERATION_AUDIO」クラスを追加しました。ノード側はこれを受け取ると、「OPERATION_SELECTOR」で専用の分岐を行い、処理内容に応じたクラスを起動します。
併せて、エンコードの最終確認処理がすこし早とちり気味だったので、「JOB_STATUS.FINISH」内に移動しました。
大掛かりになりましたが、エンコード処理が分散できるようになりましたね。