Search in sources :

Example 96 with DatagramSocket

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");
}
Also used : RtpPacket(local.net.RtpPacket) DatagramSocket(java.net.DatagramSocket)

Example 97 with DatagramSocket

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;
}
Also used : MulticastSocket(java.net.MulticastSocket) SocketException(java.net.SocketException) DatagramSocket(java.net.DatagramSocket) InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException)

Example 98 with DatagramSocket

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());
}
Also used : DatagramSocket(java.net.DatagramSocket)

Example 99 with DatagramSocket

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();
    }
}
Also used : DatagramSocket(java.net.DatagramSocket) DatagramChannel(java.nio.channels.DatagramChannel)

Example 100 with DatagramSocket

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) {
    }
}
Also used : DatagramSocket(java.net.DatagramSocket) DatagramChannel(java.nio.channels.DatagramChannel) ByteBuffer(java.nio.ByteBuffer)

Aggregations

DatagramSocket (java.net.DatagramSocket)266 DatagramPacket (java.net.DatagramPacket)120 IOException (java.io.IOException)98 SocketException (java.net.SocketException)71 InetAddress (java.net.InetAddress)57 InetSocketAddress (java.net.InetSocketAddress)45 UnknownHostException (java.net.UnknownHostException)26 Test (org.junit.Test)25 SocketTimeoutException (java.net.SocketTimeoutException)24 InterruptedIOException (java.io.InterruptedIOException)18 PortUnreachableException (java.net.PortUnreachableException)18 BindException (java.net.BindException)16 IllegalBlockingModeException (java.nio.channels.IllegalBlockingModeException)16 ServerSocket (java.net.ServerSocket)14 DatagramChannel (java.nio.channels.DatagramChannel)13 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)11 MulticastSocket (java.net.MulticastSocket)10 SocketAddress (java.net.SocketAddress)8 ByteBuffer (java.nio.ByteBuffer)8 SmallTest (android.test.suitebuilder.annotation.SmallTest)5