/
Kinect2ToolsPlaybackNode.cs
275 lines (234 loc) · 7.91 KB
/
Kinect2ToolsPlaybackNode.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#region usings
using System;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.Threading;
using VVVV.PluginInterfaces.V1;
using VVVV.PluginInterfaces.V2;
using VVVV.Utils.VColor;
using VVVV.Utils.VMath;
using VVVV.Core.Logging;
using Microsoft.Kinect.Tools;
#endregion usings
namespace VVVV.Kinect2Tools.Nodes
{
#region PluginInfo
[PluginInfo(Name = "Playback",
Category = "Kinect2 Tools",
Version = "Microsoft",
Author = "id144",
Help = "Playback recording of sensor stream using Kinect Tools",
Tags = "")]
#endregion PluginInfo
public class Kinect2ToolsPlaybackNode : IPluginEvaluate
{
#region fields & pins
[Input("Play", DefaultValue = 0.0, IsToggle=true)]
public IDiffSpread<bool> FInputPlay;
[Input("Loop", DefaultValue = 0.0, IsToggle = true)]
public IDiffSpread<bool> FInputLoop;
[Input("Pause", IsSingle = true, IsToggle = true, DefaultValue = 0.0)]
public IDiffSpread<bool> FInputPause;
[Input("StepOnce", IsSingle = true, IsBang = true, DefaultValue = 0.0)]
public IDiffSpread<bool> FInputStep;
[Input("Do Seek", IsSingle = true, IsBang = true, DefaultValue = 0.0)]
public IDiffSpread<bool> FInputDoSeek;
[Input("Seek Time", IsSingle = true, DefaultValue = 0.0)]
public IDiffSpread<double> FInputSeek;
[Input("In Point", IsSingle = true, DefaultValue = 0.0, MinValue = 0.0)]
public IDiffSpread<double> FInputInPoint;
[Input("Out Point", IsSingle = true, DefaultValue = 999.0, MinValue = 0.0)]
public IDiffSpread<double> FInputOutPoint;
[Input("Filename", IsSingle = true, StringType = StringType.Filename, FileMask = "*.xef", DefaultString = "")]
public IDiffSpread<string> FInputFilename;
[Output("Status")]
public ISpread<string> FRecord;
[Output("Time")]
public ISpread<double> FRecordDuration;
[Output("Duration", IsSingle = true)]
public ISpread<double> FRecordTotalDuration;
[Import()]
public ILogger FLogger;
#endregion fields & pins
private delegate void OneArgDelegate(string arg, bool arg2);
private bool doPlay;
private bool doLoop;
private KStudioPlayback playback = null;
private string xefFilePath;
public Kinect2ToolsPlaybackNode()
{
}
private bool doStep;
public bool fileChanged;
public void Evaluate(int SpreadMax)
{
if (FInputFilename.IsChanged)
{
if (playback!=null)
{
if ((playback.State == KStudioPlaybackState.Playing) || (playback.State == KStudioPlaybackState.Paused) )
{
playback.Stop();
}
fileChanged = true;
}
}
if (FInputPlay.IsChanged || fileChanged || doLoop)
{
doLoop = false;
if (FInputPlay[0])
{
xefFilePath = @FInputFilename[0];
if (!string.IsNullOrEmpty(xefFilePath))
{
doPlay = true;
OneArgDelegate recording = new OneArgDelegate(this.PlayClip);
recording.BeginInvoke(xefFilePath, false, null, null);
}
}
else
{
if (playback != null) playback.Stop();
}
}
if (FInputPause.IsChanged)
{
if (playback != null)
{
if (FInputPause[0])
{
if ((playback.State == KStudioPlaybackState.Playing))
{
playback.Pause();
}
}
else
{
if ((playback.State == KStudioPlaybackState.Paused))
{
playback.Resume();
}
}
}
else
{
if (FInputPause[0])
{
doPlay = true;
OneArgDelegate recording = new OneArgDelegate(this.PlayClip);
recording.BeginInvoke(xefFilePath, true, null, null);
}
}
}
fileChanged = false;
if (FInputStep[0])
{
doStep = true;
}
if (FInputDoSeek[0] && (playback !=null))
{
TimeSpan seekTime = TimeSpan.FromSeconds(FInputSeek[0]);
try
{
playback.Pause();
playback.SeekByRelativeTime(seekTime);
playback.Resume();
}
catch
{
}
}
if (playback!=null)
{
try
{
FRecordDuration[0] = playback.CurrentRelativeTime.TotalSeconds;
FRecord[0] = playback.State.ToString();
}
catch
{
}
}
}
private void PlayClip(string filePath, bool startpaused)
{
using (KStudioClient client = KStudio.CreateClient())
{
client.ConnectToService();
KStudioPlaybackFlags flags = KStudioPlaybackFlags.IgnoreOptionalStreams;
try
{
playback = client.CreatePlayback(filePath, flags);
}
catch (Exception ex)
{
FLogger.Log(LogType.Debug, ex.ToString());
}
using (playback)
{
playback.PropertyChanged += Playback_PropertyChanged;
playback.StateChanged += Playback_StateChanged;
playback.LoopCount = 0;
playback.InPointByRelativeTime = TimeSpan.FromSeconds(FInputInPoint[0]);
playback.OutPointByRelativeTime = TimeSpan.FromSeconds(Math.Min(FInputOutPoint[0],playback.Duration.TotalSeconds));
var kStudioMetadata = playback.GetMetadata(KStudioMetadataType.Public);
if (startpaused)
{
playback.StartPaused();
}
else
{
playback.Start();
};
FRecordTotalDuration[0] = playback.Duration.TotalSeconds;
while ((playback.State == KStudioPlaybackState.Playing)||(playback.State == KStudioPlaybackState.Paused) && (doPlay))
{
if (doStep)
{
if ((playback.State == KStudioPlaybackState.Playing))
{
playback.Pause();
}
if(playback.State == KStudioPlaybackState.Paused)
{
playback.StepOnce();
Thread.Sleep(20);
}
doStep = false;
}
Thread.Sleep(20);
}
playback.Stop();
}
playback = null;
client.DisconnectFromService();
}
}
public void StopPlayback()
{
doPlay = false;
}
private void Playback_StateChanged(object sender, EventArgs e)
{
}
private void Playback_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if ((e != null))
{
if (e.PropertyName == "State")
{
if (playback.State == KStudioPlaybackState.Stopped)
{
doLoop = (FInputPlay[0] && FInputLoop[0]);
}
}
try
{
}
catch (Exception)
{
}
}
}
}
}