forked from mucoboy/RTSP-Alaw-Recorder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
393 lines (317 loc) · 11.5 KB
/
MainWindow.xaml.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
using NAudio.Wave;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace RTSP_Recorder
{
//implement INotify interface to notify user
public partial class MainWindow : Window, INotify
{
WaveOut waveOut;
BufferedWaveProvider bwp;
Recorder recorder;
ObservableCollection<string> clientList;
ObservableCollection<RecordItem> recordList;
object clientLock = new object();//for thread safety of client list
public MainWindow()
{
InitializeComponent();
//audio output settings
waveOut = new WaveOut();
waveOut.Volume = 1;
bwp = new BufferedWaveProvider(new WaveFormat(8000,16,1));
bwp.BufferLength = 10_000_000;
bwp.DiscardOnBufferOverflow = true;
waveOut.Init(bwp);
waveOut.PlaybackStopped += WaveOut_PlaybackStopped;
waveOut.Play();
clientList = new ObservableCollection<string>();
recordList = new ObservableCollection<RecordItem>();
clientListBox.ItemsSource = clientList;
getFiles();
recordingsDataGrid.ItemsSource = recordList;
recorder = new Recorder(this);//create recorder object
liveCheckBox.IsChecked = true;
}
#region Methods
void getFiles()
{
try
{
foreach (var item in Directory.GetFiles(Environment.CurrentDirectory + "/iprecorder"))
{
//if there is a invalid file, ignore it
try
{
var items = item.Substring(item.IndexOf("iprecorder")).Remove(0, 11).Replace(".wav", "").Split('_');
var ID = Convert.ToInt32(items[0]);
var recordItem = new RecordItem
{
ID = ID,
Source = items[1],
From = items[2],
To = items[3],
};
recordList.Add(recordItem);
if (Recorder.ID < ID)
Recorder.ID = ID;
}
catch (Exception)
{
}
}
//sort by id number
if (recordList.Count > 0)
{
for (int i = 0; i < recordList.Count; i++)
{
var smallest = recordList[i];
for (int j = i; j < recordList.Count; j++)
{
if (smallest.ID > recordList[j].ID)
smallest = recordList[j];
}
recordList.Remove(smallest);
recordList.Insert(i, smallest);
}
recordingsDataGrid.ScrollIntoView(recordList[recordList.Count - 1]);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
#endregion
#region Events
//when playback stopped, reset values
private void WaveOut_PlaybackStopped(object sender, StoppedEventArgs e)
{
playButton.IsChecked = false;
soundProgress.Value = 1;
}
//start or stop recorder
private void startStopButton_Click(object sender, RoutedEventArgs e)
{
startStopButton.IsChecked = !startStopButton.IsChecked;
if (startStopButton.IsChecked == false)
{
if (ushort.TryParse(portTextBox.Text, out ushort port))
recorder.start(port);
else
MessageBox.Show("Invalid port number");
}
else
recorder.stop();
}
//play or stop a recording.
private void playButton_Click(object sender, RoutedEventArgs e)
{
playButton.IsChecked = !playButton.IsChecked;
if (playButton.IsChecked == false && recordingsDataGrid.SelectedIndex >= 0)
{
var recordItem = recordingsDataGrid.SelectedItem as RecordItem;
var path = Environment.CurrentDirectory + "/iprecorder/" + recordItem.ID + "_" + recordItem.Source + "_" + recordItem.From + "_" + recordItem.To + ".wav";
play(path);
playButton.IsChecked = true;
}
//if recording not selected, notify user
else if (playButton.IsChecked == false)
MessageBox.Show("Select a Recording!");
else
{
playButton.IsChecked = false;
if (waveOut.PlaybackState == PlaybackState.Playing)
waveOut.Stop();
}
}
//play a recording in new thread
void play(string path)
{
new Thread(() => {
try
{
bwp.ClearBuffer();
if (waveOut.PlaybackState != PlaybackState.Playing)
waveOut.Play();
var buffer = new byte[10_000_000];
var length = 0;
//get the record from /iprecorder path
using (var fileStream = new FileStream(path, FileMode.Open, FileAccess.Read))
{
length = fileStream.Read(buffer, 0, buffer.Length);
}
bwp.AddSamples(buffer, 44, length - 44);
var duration = length / 16;
var stopWatch = new Stopwatch();
stopWatch.Start();
while (stopWatch.ElapsedMilliseconds<duration)
{
soundProgress.Dispatcher.Invoke(() => soundProgress.Value = (double)(buffer[stopWatch.ElapsedMilliseconds] / 2.5));
Thread.Sleep(20);
//if playback stopped, terminate this thread
if (waveOut.PlaybackState != PlaybackState.Playing) return;
}
if (waveOut.PlaybackState == PlaybackState.Playing)
waveOut.Stop();
}
catch (Exception)
{
if (waveOut.PlaybackState == PlaybackState.Playing)
waveOut.Stop();
}
}).Start();
}
//update content of play button
private void playButton_Checked(object sender, RoutedEventArgs e)
{
playButton.Content = "Pause";
}
private void playButton_Unchecked(object sender, RoutedEventArgs e)
{
playButton.Content = "Play";
}
//start live broadcast
private void liveCheckBox_Checked(object sender, RoutedEventArgs e)
{
try
{
if (waveOut.PlaybackState == PlaybackState.Playing)
{
waveOut.Stop();
bwp.ClearBuffer();
}
playButton.IsChecked = false;
playButton.IsEnabled = false;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
//enable play button
private void liveCheckBox_Unchecked(object sender, RoutedEventArgs e)
{
try
{
if (waveOut.PlaybackState == PlaybackState.Playing)
{
waveOut.Stop();
bwp.ClearBuffer();
}
playButton.IsChecked = false;
playButton.IsEnabled = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
//before closing, release all resources
private void Window_Closed(object sender, EventArgs e)
{
recorder.stop();
Process.GetCurrentProcess().Kill();
}
#endregion
#region INotify implementation
//when a client connected, add it to list
public void onClientConnected(string clientName)
{
try
{
lock (clientLock)
{
clientListBox.Dispatcher.Invoke(() => clientList.Add(clientName));
}
}
catch (Exception)
{
}
}
//when a client disconnected, remove it from list
public void onClientDisconnected(string clientName)
{
try
{
lock (clientLock)
{
clientListBox.Dispatcher.Invoke(() => clientList.Remove(clientName));
}
}
catch (Exception)
{
}
}
//when data received, add it to buffer and update progress bar
public void onDataReceived(byte[] data)
{
try
{
Dispatcher.Invoke(() => {
if (liveCheckBox.IsChecked == true)
{
if (waveOut.PlaybackState != PlaybackState.Playing)
waveOut.Play();
bwp.AddSamples(data, 0, data.Length);
}
soundProgress.Value = (double)(data[0] / 2.5);
});
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
//on listener started, update button and label
public void onListenerStarted()
{
Dispatcher.Invoke(() => {
startStopButton.IsChecked = true;
startStopButton.Content = "Stop";
listenerStatusLabel.Content = "Recording started!";
listenerStatusLabel.Foreground = Brushes.Green;
});
}
//if an exception occured, notify user
public void onListenerStopped(string cause)
{
Dispatcher.Invoke(() => {
startStopButton.IsChecked = false;
startStopButton.Content = "Start";
listenerStatusLabel.Content = "Press Start for Recording!";
listenerStatusLabel.Foreground = Brushes.Red;
soundProgress.Value = 1;
});
if (cause != "user action")
MessageBox.Show("Fault: " + cause);
}
//add recording to list
public void onRecordingStopped(RecordItem recordItem)
{
Dispatcher.Invoke(() => {
recordList.Add(recordItem);
soundProgress.Value = 1;
recordingsDataGrid.ScrollIntoView(recordList[recordList.Count - 1]);
});
}
#endregion
}
}