use of java.net.DatagramSocket in project bigbluebutton by bigbluebutton.
the class RtpStreamSender method run.
/** Runs it in a new Thread. */
public void run() {
if (rtp_socket == null || input_stream == null)
return;
//else
byte[] buffer = new byte[frame_size + 12];
RtpPacket rtp_packet = new RtpPacket(buffer, 0);
rtp_packet.setPayloadType(p_type);
int seqn = 0;
long time = 0;
//long start_time=System.currentTimeMillis();
long byte_rate = frame_rate * frame_size;
running = true;
if (DEBUG)
println("Reading blocks of " + (buffer.length - 12) + " bytes");
try {
while (running) {
//if (DEBUG) System.out.print("o");
int num = input_stream.read(buffer, 12, buffer.length - 12);
//if (DEBUG) System.out.print("*");
if (num > 0) {
rtp_packet.setSequenceNumber(seqn++);
rtp_packet.setTimestamp(time);
rtp_packet.setPayloadLength(num);
rtp_socket.send(rtp_packet);
// update rtp timestamp (in milliseconds)
long frame_time = (num * 1000) / byte_rate;
time += frame_time;
// wait fo next departure
if (do_sync) {
// wait before next departure..
//long frame_time=start_time+time-System.currentTimeMillis();
// accellerate in order to compensate possible program latency.. ;)
frame_time -= sync_adj;
try {
Thread.sleep(frame_time);
} catch (Exception e) {
}
}
} else if (num < 0) {
running = false;
if (DEBUG)
println("Error reading from InputStream");
}
}
} catch (Exception e) {
running = false;
e.printStackTrace();
}
//if (DEBUG) println("rtp time: "+time);
//if (DEBUG) println("real time: "+(System.currentTimeMillis()-start_time));
// close RtpSocket and local DatagramSocket
DatagramSocket socket = rtp_socket.getDatagramSocket();
rtp_socket.close();
if (socket_is_local && socket != null)
socket.close();
// free all
input_stream = null;
rtp_socket = null;
if (DEBUG)
println("rtp sender terminated");
}
use of java.net.DatagramSocket in project ExoPlayer by google.
the class UdpDataSource method open.
@Override
public long open(DataSpec dataSpec) throws UdpDataSourceException {
uri = dataSpec.uri;
String host = uri.getHost();
int port = uri.getPort();
try {
address = InetAddress.getByName(host);
socketAddress = new InetSocketAddress(address, port);
if (address.isMulticastAddress()) {
multicastSocket = new MulticastSocket(socketAddress);
multicastSocket.joinGroup(address);
socket = multicastSocket;
} else {
socket = new DatagramSocket(socketAddress);
}
} catch (IOException e) {
throw new UdpDataSourceException(e);
}
try {
socket.setSoTimeout(socketTimeoutMillis);
} catch (SocketException e) {
throw new UdpDataSourceException(e);
}
opened = true;
if (listener != null) {
listener.onTransferStart(this, dataSpec);
}
return C.LENGTH_UNSET;
}
use of java.net.DatagramSocket in project j2objc by google.
the class DatagramSocketTest method testStateAfterClose.
public void testStateAfterClose() throws Exception {
DatagramSocket ds = new DatagramSocket();
ds.close();
assertTrue(ds.isBound());
assertTrue(ds.isClosed());
assertFalse(ds.isConnected());
assertNull(ds.getLocalAddress());
assertEquals(-1, ds.getLocalPort());
assertNull(ds.getLocalSocketAddress());
}
use of java.net.DatagramSocket in project j2objc by google.
the class DatagramChannelTest method testInitialState.
public void testInitialState() throws Exception {
DatagramChannel dc = DatagramChannel.open();
try {
DatagramSocket socket = dc.socket();
assertFalse(socket.isBound());
assertFalse(socket.getBroadcast());
assertFalse(socket.isClosed());
assertFalse(socket.isConnected());
assertEquals(0, socket.getLocalPort());
assertTrue(socket.getLocalAddress().isAnyLocalAddress());
assertNull(socket.getLocalSocketAddress());
assertNull(socket.getInetAddress());
assertEquals(-1, socket.getPort());
assertNull(socket.getRemoteSocketAddress());
assertFalse(socket.getReuseAddress());
assertSame(dc, socket.getChannel());
} finally {
dc.close();
}
}
use of java.net.DatagramSocket in project j2objc by google.
the class DatagramChannelTest method test_read_intoReadOnlyByteArrays.
public void test_read_intoReadOnlyByteArrays() throws Exception {
ByteBuffer readOnly = ByteBuffer.allocate(1).asReadOnlyBuffer();
DatagramSocket ds = new DatagramSocket(0);
DatagramChannel dc = DatagramChannel.open();
dc.connect(ds.getLocalSocketAddress());
try {
dc.read(readOnly);
fail();
} catch (IllegalArgumentException expected) {
}
try {
dc.read(new ByteBuffer[] { readOnly });
fail();
} catch (IllegalArgumentException expected) {
}
try {
dc.read(new ByteBuffer[] { readOnly }, 0, 1);
fail();
} catch (IllegalArgumentException expected) {
}
}
Aggregations