66import com .easternedgerobotics .rov .value .VideoValueB ;
77
88import javafx .scene .image .Image ;
9+ import org .pmw .tinylog .Logger ;
910import rx .Observable ;
1011
12+ import java .net .InetAddress ;
13+ import java .net .NetworkInterface ;
14+ import java .net .SocketException ;
15+
16+ import java .util .ArrayList ;
17+ import java .util .Enumeration ;
18+ import java .util .List ;
19+
1120public final class VideoDecoder {
1221 private final EventPublisher eventPublisher ;
1322
@@ -17,9 +26,12 @@ public final class VideoDecoder {
1726
1827 private final FFmpegFXImageDecoder decoderB ;
1928
29+ private final String broadcastIP ;
30+
2031 public VideoDecoder (
2132 final EventPublisher eventPublisher ,
22- final VideoDecoderConfig config
33+ final VideoDecoderConfig config ,
34+ final String broadcast
2335 ) {
2436 this .eventPublisher = eventPublisher ;
2537 this .config = config ;
@@ -41,13 +53,41 @@ public VideoDecoder(
4153 config .preset (),
4254 config .numBuffers (),
4355 config .introVideoLocation ());
56+ final int lastLocation = broadcast .lastIndexOf ('.' );
57+ broadcastIP = broadcast .substring (0 , lastLocation - 1 );
4458 }
4559
4660 public void start () {
4761 decoderA .start ();
4862 decoderB .start ();
49- eventPublisher .emit (new VideoValueA (config .host (), config .portA ()));
50- eventPublisher .emit (new VideoValueB (config .host (), config .portB ()));
63+ boolean initialized = false ;
64+ final List <String > ipAddresses = new ArrayList <String >();
65+ try {
66+ final Enumeration <NetworkInterface > network = NetworkInterface .getNetworkInterfaces ();
67+ while (network .hasMoreElements ()) {
68+ final Enumeration <InetAddress > inet = network .nextElement ().getInetAddresses ();
69+ while (inet .hasMoreElements ()) {
70+ ipAddresses .add (inet .nextElement ().toString ());
71+ }
72+ }
73+ } catch (final SocketException error ) {
74+ Logger .error (error );
75+ }
76+ for (final String newAddress : ipAddresses ) {
77+ final int lastLocation = newAddress .lastIndexOf ('.' );
78+ if (lastLocation >= 0 ) {
79+ final String testAddress = newAddress .substring (1 , lastLocation - 1 );
80+ if (broadcastIP .equals (testAddress )) {
81+ eventPublisher .emit (new VideoValueA (newAddress , config .portA ()));
82+ eventPublisher .emit (new VideoValueB (newAddress , config .portB ()));
83+ initialized = true ;
84+ break ;
85+ }
86+ }
87+ }
88+ if (!initialized ) {
89+ Logger .warn ("Could not detect the IP of the runtime system" );
90+ }
5191 }
5292
5393 public void stop () {
0 commit comments