浏览代码

WiFi Sharing / Remote Playback: automatically select a good interface to listen on

Felix Paul Kühne 9 年之前
父节点
当前提交
f3bdd42e14
共有 3 个文件被更改,包括 39 次插入13 次删除
  1. 0 6
      Sources/VLCConstants.h
  2. 39 1
      Sources/VLCHTTPUploaderController.m
  3. 0 6
      VLC for Apple TV/VLCTVConstants.h

+ 0 - 6
Sources/VLCConstants.h

@@ -79,12 +79,6 @@
 
 #define kVLCDarwinNotificationNowPlayingInfoUpdate @"org.videolan.ios-app.nowPlayingInfoUpdate"
 
-#if TARGET_IPHONE_SIMULATOR
-#define WifiInterfaceName @"en1"
-#else
-#define WifiInterfaceName @"en0"
-#endif
-
 #define kVLCMigratedToUbiquitousStoredServerList @"kVLCMigratedToUbiquitousStoredServerList"
 #define kVLCStoredServerList @"kVLCStoredServerList"
 #define kVLCStoreDropboxCredentials @"kVLCStoreDropboxCredentials"

+ 39 - 1
Sources/VLCHTTPUploaderController.m

@@ -138,7 +138,45 @@
 
     // Initialize our http server
     _httpServer = [[HTTPServer alloc] init];
-    [_httpServer setInterface:WifiInterfaceName];
+
+    // find an interface to listen on
+    struct ifaddrs *listOfInterfaces = NULL;
+    struct ifaddrs *anInterface = NULL;
+    NSString *interfaceToUse = nil;
+    int ret = getifaddrs(&listOfInterfaces);
+    if (ret == 0) {
+        anInterface = listOfInterfaces;
+
+        while (anInterface != NULL) {
+            if (anInterface->ifa_addr->sa_family == AF_INET) {
+                APLog(@"Found interface %s", anInterface->ifa_name);
+
+                /* check for primary interface first */
+                if (strncmp (anInterface->ifa_name,"en0",strlen("en0")) == 0) {
+                    unsigned int flags = anInterface->ifa_flags;
+                    if( (flags & 0x1) && (flags & 0x40) && !(flags & 0x8) ) {
+                        interfaceToUse = [NSString stringWithUTF8String:anInterface->ifa_name];
+                        break;
+                    }
+                }
+
+                /* oh well, let's move on to the secondary interface */
+                if (strncmp (anInterface->ifa_name,"en1",strlen("en1")) == 0) {
+                    unsigned int flags = anInterface->ifa_flags;
+                    if( (flags & 0x1) && (flags & 0x40) && !(flags & 0x8) ) {
+                        interfaceToUse = [NSString stringWithUTF8String:anInterface->ifa_name];
+                        break;
+                    }
+                }
+            }
+            anInterface = anInterface->ifa_next;
+        }
+        freeifaddrs(listOfInterfaces);
+    }
+    if (interfaceToUse == nil)
+        return false;
+
+    [_httpServer setInterface:interfaceToUse];
 
     [_httpServer setIPv4Enabled:YES];
     [_httpServer setIPv6Enabled:[[[NSUserDefaults standardUserDefaults] objectForKey:kVLCSettingWiFiSharingIPv6] boolValue]];

+ 0 - 6
VLC for Apple TV/VLCTVConstants.h

@@ -62,10 +62,4 @@
 #define kVLCSettingWiFiSharingIPv6 @"wifi-sharing-ipv6"
 #define kVLCSettingWiFiSharingIPv6DefaultValue @(NO)
 
-#if TARGET_IPHONE_SIMULATOR
-#define WifiInterfaceName @"en1"
-#else
-#define WifiInterfaceName @"en1"
-#endif
-
 #define kVLCfortvOSMovieDBKey @""