Joinig to video broadcast group on OSX improved
[mardrone] / mardrone / dronelib / video.cpp
index 7ba76ad..a4ec155 100644 (file)
   !
   !
   *===================================================================*/
+#include <stdio.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
 #include "video.h"
+#include <QNetworkConfigurationManager>
+#include <QNetworkSession>
+#include <QList>
 #include <QGraphicsView>
 #include <QDateTime>
 #include <QDir>
@@ -99,12 +107,39 @@ QRectF DroneVideo::boundingRect() const
 
 VideoThread::VideoThread(DroneVideo *parentp,QHostAddress host,QImage *_image)
 {
+    struct ip_mreq imreq;
+    QString my_ip;
+    my_ip="192.168.1.2";
     image=_image;
     qDebug() << "videoThread::videoThread";
     stopped=false;
     parent=parentp;
     videoSock=new QUdpSocket();
-    videoSock->bind(QHostAddress::Any,5555);
+    if(!videoSock->bind(QHostAddress::Any,5555)) qDebug() << "VideoThread::VideoThread ** Cant Bind to port 5555 ***" << videoSock->errorString();
+    QList<QNetworkConfiguration> netconfs=QNetworkConfigurationManager().allConfigurations();
+    foreach (QNetworkConfiguration np,netconfs) {
+        qDebug() << "network Confifuration name " << np.name() << np.bearerName() << np.bearerTypeName();
+        QNetworkSession ns(np);
+        if(ns.interface().addressEntries().empty())
+            qDebug() << "network session " << ns.interface().humanReadableName() << "**NotConfig**";
+        else {
+            qDebug() << "network session " << ns.interface().humanReadableName() <<   ns.interface().addressEntries().first().ip();
+            // Ubuntu may give wlan0 as "Ethernet"
+            if((np.bearerName()==QString("WLAN")) || (ns.interface().humanReadableName()==QString("wlan0")) ) {
+               my_ip=ns.interface().addressEntries().first().ip().toString();
+               qDebug() << "My IP is " << my_ip;
+        }
+        }
+    }
+    // Qt 4.7.x
+    imreq.imr_multiaddr.s_addr=inet_addr("224.1.1.1");
+    imreq.imr_interface.s_addr=inet_addr(my_ip.toAscii());
+    setsockopt(videoSock->socketDescriptor(),IPPROTO_IP,IP_ADD_MEMBERSHIP,&imreq,sizeof(imreq));
+//#else
+    // Qt 4.8
+    if(!videoSock->joinMulticastGroup(QHostAddress("224.1.1.1")))
+        qDebug() << "VideoThread::VideoThread can't join multicast Group 224.1.1.1" << videoSock->errorString();
+//#endif
     droneHost=host;
     videoRec=FALSE;
     videoFile=NULL;