-
Notifications
You must be signed in to change notification settings - Fork 0
/
RecordContext.cs
229 lines (192 loc) · 7.79 KB
/
RecordContext.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
using Suconbu.Mobile;
using Suconbu.Toolbox;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Suconbu.Sumacon
{
struct RecordSetting
{
public int SequenceNo;
public string DirectoryPath;
public string FileNamePattern;
public int TimeLimitSeconds;
public float ViewSizeMultiply;
public int Bitrate;
public bool Timestamp;
}
class RecordContext : IDisposable
{
public enum RecordState { Recording, ManualStopping, Pulling, Finished, Aborted }
public Device Device { get; private set; }
public static int TimeLimitSecondsMax { get { return Properties.Settings.Default.RecordLimitTimeMax; } }
public RecordState State
{
get { return this.state; }
set
{
if(this.state != value)
{
this.state = value;
this.onStateChanged(value);
}
}
}
public DateTime StartedAt { get; private set; } = DateTime.MaxValue;
public TimeSpan Elapsed
{
get
{
var elapsed = (this.stoppedAt == DateTime.MaxValue) ? (DateTime.Now - this.StartedAt) : (this.stoppedAt - this.StartedAt);
return (elapsed.TotalSeconds <= this.setting.TimeLimitSeconds) ? elapsed : TimeSpan.FromSeconds(this.setting.TimeLimitSeconds);
}
}
public string FilePath { get; private set; }
RecordSetting setting;
CommandContext recordCommandContext;
CommandContext pullCommandContext;
Size recordScreenSize;
string filePathInDevice;
string filePathInPc;
RecordState state = RecordState.Recording;
DateTime stoppedAt = DateTime.MaxValue;
Action<RecordState> onStateChanged = delegate { };
RecordContext() { }
public static RecordContext StartNew(Device device, RecordSetting setting, Action<RecordState> onStateChanged)
{
var instance = new RecordContext();
instance.Device = device;
instance.setting = setting;
instance.onStateChanged = onStateChanged;
instance.StartRecord();
return instance;
}
void StartRecord()
{
Debug.Print(Util.GetCurrentMethodName());
if (!Directory.Exists(this.setting.DirectoryPath))
{
Directory.CreateDirectory(this.setting.DirectoryPath);
}
this.recordScreenSize = this.Device.RotatedScreenSize;
if (this.setting.ViewSizeMultiply != 1.0f)
{
this.recordScreenSize = this.recordScreenSize.Multiplied(this.setting.ViewSizeMultiply);
}
var fileName = this.GetFileName(this.setting.FileNamePattern, this.recordScreenSize, this.setting.TimeLimitSeconds);
this.filePathInDevice = $"{Properties.Settings.Default.DeviceTemporaryDirectoryPath}/{fileName}";
var option = new StringBuilder();
if (0 < this.setting.TimeLimitSeconds && this.setting.TimeLimitSeconds <= RecordContext.TimeLimitSecondsMax)
{
option.Append($" --time-limit {this.setting.TimeLimitSeconds}");
}
if(this.recordScreenSize != this.Device.ScreenSize)
{
option.Append($" --size {this.recordScreenSize.Width}x{this.recordScreenSize.Height}");
}
option.Append($" --bit-rate {this.setting.Bitrate}");
if (this.setting.Timestamp)
{
option.Append($" --show-frame-time");
}
this.StartedAt = DateTime.Now;
var command = $"shell screenrecord {option} {this.filePathInDevice}";
this.recordCommandContext = this.Device.RunCommandOutputTextAsync(command, this.OnRecordCommandFinished);
}
public void Stop()
{
if (this.State != RecordState.Recording) return;
this.State = RecordState.ManualStopping;
// 中断後そのうちOnRecordCommandFinishedが呼ばれる
this.recordCommandContext?.Cancel();
this.recordCommandContext = null;
}
public void Cancel()
{
this.Dispose();
}
void OnRecordCommandFinished(string output, string error)
{
this.recordCommandContext = null;
if (!string.IsNullOrEmpty(output))
{
// なにかエラー
Trace.TraceError(output);
this.Dispose();
return;
}
Debug.Assert(this.State == RecordState.Recording || this.state == RecordState.ManualStopping);
var manualStopped = this.State == RecordState.ManualStopping;
this.stoppedAt = manualStopped ? DateTime.Now : this.StartedAt.AddSeconds(this.setting.TimeLimitSeconds);
// 手動で止めた時は書き込み終わるまでちょっと待つ
int pullDelayMilliseconds = manualStopped ? Properties.Settings.Default.RecordPullDelayMillisecondsAtManualStop : 1;
var pcFileName = this.GetFileName(this.setting.FileNamePattern, this.recordScreenSize, (int)this.Elapsed.TotalSeconds);
this.filePathInPc = Path.Combine(this.setting.DirectoryPath, pcFileName);
var command = $"pull {this.filePathInDevice} {this.filePathInPc}";
Delay.SetTimeout(() =>
{
this.State = RecordState.Pulling;
this.pullCommandContext = this.Device.RunCommandOutputTextAsync(command, this.OnPullCommandFinished);
}, pullDelayMilliseconds);
}
void OnPullCommandFinished(string output, string error)
{
this.pullCommandContext = null;
if (output.ToLower().Contains("error"))
{
// なにかエラー
Trace.TraceError(output);
this.Dispose();
return;
}
this.OnPullFinished();
}
void OnPullFinished()
{
Debug.Assert(this.State == RecordState.Pulling);
var command = $"shell rm {this.filePathInDevice}";
this.Device.RunCommandAsync(command);
if(!File.Exists(this.filePathInPc))
{
Trace.TraceError("Not found '{this.filePathInPc}'.");
this.Dispose();
return;
}
this.FilePath = this.filePathInPc;
this.State = RecordState.Finished;
}
string GetFileName(string pattern, Size size, int durationSeconds)
{
var now = DateTime.Now;
var replacer = new Dictionary<string, string>()
{
{ "date", now.ToString("yyyy-MM-dd") },
{ "time", now.ToString("HHmmss") },
{ "width", size.Width.ToString() },
{ "height", size.Height.ToString() },
{ "no", (this.setting.SequenceNo % 10000).ToString("0000") },
{ "duration", durationSeconds.ToString("0sec") }
};
pattern = this.Device.ToString(pattern);
return pattern.Replace(replacer, "-");
}
#region IDisposable Support
bool disposed = false;
public virtual void Dispose()
{
if (this.disposed) return;
this.State = RecordState.Aborted;
this.recordCommandContext?.Cancel();
this.recordCommandContext = null;
this.pullCommandContext?.Cancel();
this.pullCommandContext = null;
this.disposed = true;
}
#endregion
}
}