001package net.bramp.ffmpeg.progress; 002 003import static com.google.common.base.Preconditions.checkNotNull; 004 005import java.io.IOException; 006import java.net.DatagramSocket; 007import java.net.InetAddress; 008import java.net.SocketException; 009import java.net.URI; 010import java.net.URISyntaxException; 011import java.util.concurrent.CountDownLatch; 012 013public class UdpProgressParser extends AbstractSocketProgressParser { 014 015 final DatagramSocket socket; 016 final URI address; 017 018 public UdpProgressParser(ProgressListener listener) throws SocketException, URISyntaxException { 019 this(listener, 0, InetAddress.getLoopbackAddress()); 020 } 021 022 public UdpProgressParser(ProgressListener listener, int port, InetAddress addr) 023 throws SocketException, URISyntaxException { 024 025 super(listener); 026 027 this.socket = new DatagramSocket(port, checkNotNull(addr)); 028 this.address = createUri("udp", socket.getLocalAddress(), socket.getLocalPort()); 029 030 this.socket.setBroadcast(false); 031 // this.socket.setSoTimeout(); // TODO Setup timeouts 032 } 033 034 @Override 035 public synchronized void stop() throws IOException { 036 if (socket.isClosed()) { 037 // Allow double stop, and ignore 038 return; 039 } 040 041 socket.close(); 042 super.stop(); 043 } 044 045 @Override 046 protected String getThreadName() { 047 return "UdpProgressParser"; 048 } 049 050 @Override 051 protected Runnable getRunnable(CountDownLatch startSignal) { 052 return new UdpProgressParserRunnable(parser, socket, startSignal); 053 } 054 055 @Override 056 public URI getUri() { 057 return address; 058 } 059}