-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathBBFrameUDP.cs
More file actions
122 lines (108 loc) · 3.6 KB
/
BBFrameUDP.cs
File metadata and controls
122 lines (108 loc) · 3.6 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.IO;
using System.Windows.Forms.VisualStyles;
using Serilog;
namespace opentuner
{
public class BBFrameUDP
{
CircularBuffer _ts_data_queue = new CircularBuffer(GlobalDefines.CircularBufferStartingCapacity);
object locker = new object();
protected bool _stream;
public bool stream
{
get
{
lock (locker)
return _stream;
}
set
{
lock (locker)
_stream = value;
}
}
bool streaming = false;
string udp_address = "";
int udp_port = 0;
public BBFrameUDP(TSThread _ts_thread, string udp_address, int udp_port)
{
_ts_thread.RegisterTSConsumer(_ts_data_queue);
this.udp_address = udp_address;
this.udp_port = udp_port;
}
public void worker_thread()
{
byte data;
// Create a UDP client
UdpClient udpClient = new UdpClient();
IPAddress vlcIpAddress = IPAddress.Parse(udp_address);
int vlcPort = udp_port;
BinaryWriter binWriter = null;
try
{
while (true)
{
if (streaming == false && stream == true)
{
string fileName = DateTime.Now.ToString("yyyy-dd-M--HH-mm-ss") + "_" + ".bin";
Log.Information("New BBFrame File: " + fileName);
binWriter = new BinaryWriter(File.Open(fileName, FileMode.Create));
streaming = true;
}
else
{
if (streaming == true && stream == false)
{
streaming = false;
if (binWriter != null)
{
binWriter.Close();
}
}
}
// we are streaming
if (streaming )
{
if (_ts_data_queue.Count > 0)
{
data = _ts_data_queue.Dequeue();
binWriter.Write(data);
//udpClient.Send(dt, count, new IPEndPoint(vlcIpAddress, vlcPort));
}
else // streaming but not enough data yet
{
Thread.Sleep(100);
}
}
else
{
// throw away data
if (_ts_data_queue.Count > 0)
_ts_data_queue.Clear();
Thread.Sleep(100);
}
}
}
catch (ThreadAbortException)
{
Log.Information("BBFrame Thread: Closing ");
}
finally
{
Log.Information("Closing BBFrame UDP");
if (binWriter != null)
{
binWriter.Close();
}
}
}
}
}