/
Streaming.java
105 lines (88 loc) · 2.22 KB
/
Streaming.java
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
package L3D;
//import UDP library
public class Streaming {
public static L3D_UDP udp; // define the UDP object
boolean multicast;
public static String address;
private static int port=2222;
//multicast constructor
public Streaming()
{
multicast=true;
address = "224.0.0.1"; // this is the multicast IP -- the router reserves this address,
//and forwards any packets it receives at this address to all hosts on the LAN
udp = new L3D_UDP( this, port, address );
}
public Streaming(int _port)
{
multicast=true;
port=_port;
address = "224.0.0.1"; // this is the multicast IP -- the router reserves this address,
//and forwards any packets it receives at this address to all hosts on the LAN
udp = new L3D_UDP( this, port, address );
}
//direct streaming constructor
public Streaming(String ip)
{
multicast=false;
address=ip;
System.out.println("IP address is: "+address);
udp = new L3D_UDP( this );
}
//direct streaming constructor
public Streaming(int _port, String ip)
{
multicast=false;
address=ip;
port=_port;
udp = new L3D_UDP( this );
}
void sendData(int[][][] cube)
{
byte[] data=serializeCube(cube);
if (multicast)
{
udp.send( data);
//System.out.println("sent multicast data");
}
else
udp.send( data, address, port);
}
public static byte[] serializeCube(int[][][] cube)
{
int index=0;
byte[] data=new byte[cube[0][0].length*cube[0].length*cube.length];
for (int z=0; z<cube[0][0].length; z++)
for (int y=0; y<cube[0].length; y++)
for (int x=0; x<cube.length; x++)
{
index=z*cube.length*cube[0].length+y*cube.length+x;
data[index]=colorByte(cube[x][y][z]);
}
return data;
}
public static void sendData(String _address, byte[] data)
{
udp.send(data, _address,port);
}
public static byte colorByte(int col)
{
return (byte)(red(col)&224 | (green(col)&224)>>3 | (blue(col)&192)>>6);
}
public static int red(int col)
{
return((col>>16)&255);
}
public static int green(int col)
{
return((col>>8)&255);
}
public static int blue(int col)
{
return(col&255);
}
public static void setPort(int _port)
{
port=_port;
}
}