فهرست منبع

VLCMedia: added a new API to determine whether the media size is supposedly suitable for the current device or not

Felix Paul Kühne 12 سال پیش
والد
کامیت
da93fe25ee
2فایلهای تغییر یافته به همراه57 افزوده شده و 0 حذف شده
  1. 5 0
      Headers/Public/VLCMedia.h
  2. 52 0
      Sources/VLCMedia.m

+ 5 - 0
Headers/Public/VLCMedia.h

@@ -237,6 +237,11 @@ typedef enum VLCMediaState
 @property (readonly) VLCMediaState state;
 
 /**
+ * returns a bool whether is the media is expected to play fluently on this
+ * device or not. It always returns YES on a Mac.*/
+- (BOOL)isMediaSizeSuitableForDevice;
+
+/**
  * Tracks information NSDictionary Possible Keys
  */
 

+ 52 - 0
Sources/VLCMedia.m

@@ -30,6 +30,7 @@
 #import "VLCLibrary.h"
 #import "VLCLibVLCBridging.h"
 #import <vlc/libvlc.h>
+#import <sys/sysctl.h> // for sysctlbyname
 
 /* Meta Dictionary Keys */
 NSString * VLCMetaInformationTitle          = @"title";
@@ -453,6 +454,57 @@ NSString *VLCMediaTracksInformationTextEncoding = @"encoding"; // NSString
     return array;
 }
 
+- (BOOL)isMediaSizeSuitableForDevice
+{
+#if TARGET_OS_IPHONE
+    // Trigger parsing if needed
+    [self parseIfNeeded];
+
+    NSUInteger biggestWidth = 0;
+    NSUInteger biggestHeight = 0;
+    libvlc_media_track_t **tracksInfo;
+    unsigned int count = libvlc_media_tracks_get(p_md, &tracksInfo);
+    for (NSUInteger i = 0; i < count; i++) {
+        switch (tracksInfo[i]->i_type) {
+            case libvlc_track_video:
+                if (tracksInfo[i]->video->i_width > biggestWidth)
+                    biggestWidth = tracksInfo[i]->video->i_width;
+                if (tracksInfo[i]->video->i_height > biggestHeight)
+                    biggestHeight = tracksInfo[i]->video->i_height;
+                break;
+            default:
+                break;
+        }
+    }
+
+    if (biggestHeight > 0 && biggestWidth > 0) {
+        size_t size;
+        sysctlbyname("hw.machine", NULL, &size, NULL, 0);
+
+        char *answer = malloc(size);
+        sysctlbyname("hw.machine", answer, &size, NULL, 0);
+
+        NSString *currentMachine = [NSString stringWithCString:answer encoding: NSUTF8StringEncoding];
+        free(answer);
+
+        NSUInteger totalNumberOfPixels = biggestWidth * biggestHeight;
+
+        if ([currentMachine hasPrefix:@"iPhone2"] || [currentMachine hasPrefix:@"iPhone3"] || [currentMachine hasPrefix:@"iPad1"] || [currentMachine hasPrefix:@"iPod3"] || [currentMachine hasPrefix:@"iPod4"]) {
+            // iPhone 3GS, iPhone 4, first gen. iPad, 3rd and 4th generation iPod touch
+            return (totalNumberOfPixels < 600000); // between 480p and 720p
+        } else if ([currentMachine hasPrefix:@"iPhone4"] || [currentMachine hasPrefix:@"iPad3,1"] || [currentMachine hasPrefix:@"iPad3,2"] || [currentMachine hasPrefix:@"iPad3,3"] || [currentMachine hasPrefix:@"iPod4"] || [currentMachine hasPrefix:@"iPad2"] || [currentMachine hasPrefix:@"iPod5"]) {
+            // iPhone 4S, iPad 2 and 3, iPod 4 and 5
+            return (totalNumberOfPixels < 922000); // 720p
+        } else {
+            // iPhone 5, iPad 4
+            return (totalNumberOfPixels < 2074000); // 1080p
+        }
+    }
+#endif
+
+    return YES;
+}
+
 @synthesize url;
 @synthesize subitems;
 @synthesize metaDictionary;