-
Notifications
You must be signed in to change notification settings - Fork 0
/
Streamer.cs
92 lines (73 loc) · 3.83 KB
/
Streamer.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
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace iuF
{
public class Streamer
{
private string _address;
private int _port_skeleton;
private int _port_pixels;
private int _chunk_size;
private UdpClient _client_skeleton;
private UdpClient _client_pixels;
public Streamer(string address, int port_skeleton, int port_pixels)
{
_address = address;
_port_skeleton = port_skeleton;
_port_pixels = port_pixels;
_client_skeleton = new UdpClient();
_client_pixels = new UdpClient();
_chunk_size = 1500 - 20 - 8; // 20 bytes for the IP Headers, 8 bytes for the UDP Headers
}
/* Try to connect to the remote addresses */
public bool TryConnect() {
try { _client_skeleton.Connect(_address, _port_skeleton); }
catch (Exception e) { Console.WriteLine("Failed to connect to {0}:{1}", _address, _port_skeleton); return false; }
try {_client_pixels.Connect(_address, _port_pixels); }
catch (Exception e) { Console.WriteLine("Failed to connect to {0}:{1}", _address, _port_pixels); return false; }
Console.WriteLine("{0} Connected\nStreaming:\n * Skeleton on port {1}\n * Pixels on port {2}", _address, _port_skeleton, _port_pixels);
return true;
}
/* Send the skeleton datas */
public void SendSkeleton(byte[] joints, ulong timestamp) {
byte[] buffer;
byte[] time = BitConverter.GetBytes((Int64)timestamp);
int buffer_cursor = 0;
int time_size = time.Length;
while (joints.Length - buffer_cursor > 0) {
int buffer_size = _chunk_size;
if (joints.Length - buffer_cursor + time_size < _chunk_size) { buffer_size = joints.Length - buffer_cursor + time_size; }
int data_size = buffer_size - time_size;
buffer = new byte[buffer_size];
Array.Copy(time, 0, buffer, 0, time_size); // Copy the TimeStamp at the beginning of the buffer
Array.Copy(joints, buffer_cursor, buffer, 0, data_size); // Copy the Joints datas into the buffer
// Console.WriteLine("Pixels [{0}/{1}] sended (buffer size: {2})", buffer_cursor, joints.Length, buffer_size);
_client_skeleton.Send(buffer, buffer_size);
buffer_cursor += data_size;
}
// Console.WriteLine("Skeleton Sended at {0}:{1} [{2}]", _address, _port, timestamp);
}
/* Send the pixels datas */
public void SendPixels(byte[] pixels, ulong timestamp) {
byte[] buffer;
byte[] time = BitConverter.GetBytes((Int64)timestamp); // 8bytes Time Stamp
int buffer_cursor = 0;
int time_size = time.Length;
while (pixels.Length - buffer_cursor > 0) {
int buffer_size = _chunk_size;
if (pixels.Length - buffer_cursor + time_size < _chunk_size) { buffer_size = pixels.Length - buffer_cursor + time_size; }
int data_size = buffer_size - time_size;
buffer = new byte[buffer_size];
Array.Copy(time, 0, buffer, 0, time_size); // Copy the TimeStamp at the beginning of the buffer
Array.Copy(pixels, buffer_cursor, buffer, 0, data_size); // Copy the Pixels datas into the buffer
//Console.WriteLine("Pixels [{0}/{1}] sended (buffer size: {2})", buffer_cursor, pixels.Length, buffer_size);
_client_pixels.Send(buffer, buffer_size);
buffer_cursor += data_size;
}
//Console.WriteLine("Pixels Sended at {0}:{1} [{2}]", _address, _port, timestamp);
}
}
}