VLCLibrary.m 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*****************************************************************************
  2. * VLCLibrary.m: VLCKit.framework VLCLibrary implementation
  3. *****************************************************************************
  4. * Copyright (C) 2007 Pierre d'Herbemont
  5. * Copyright (C) 2007-2019 VLC authors and VideoLAN
  6. * $Id$
  7. *
  8. * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
  9. * Felix Paul Kühne <fkuehne # videolan.org>
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms of the GNU Lesser General Public License as published by
  13. * the Free Software Foundation; either version 2.1 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public License
  22. * along with this program; if not, write to the Free Software Foundation,
  23. * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  24. *****************************************************************************/
  25. #import "VLCLibrary.h"
  26. #if TARGET_OS_TV
  27. # include "vlc-plugins-AppleTV.h"
  28. #elif TARGET_OS_IPHONE
  29. # include "vlc-plugins-iPhone.h"
  30. #else
  31. # include "vlc-plugins-MacOSX.h"
  32. #endif
  33. #include <vlc/vlc.h>
  34. static void HandleMessage(void *,
  35. int,
  36. const libvlc_log_t *,
  37. const char *,
  38. va_list);
  39. static void HandleMessageForCustomTarget(void *,
  40. int,
  41. const libvlc_log_t *,
  42. const char *,
  43. va_list);
  44. static VLCLibrary * sharedLibrary = nil;
  45. @interface VLCLibrary()
  46. {
  47. FILE *_logFileStream;
  48. }
  49. @end
  50. @implementation VLCLibrary
  51. + (VLCLibrary *)sharedLibrary
  52. {
  53. static dispatch_once_t onceToken;
  54. dispatch_once(&onceToken, ^{
  55. sharedLibrary = [[VLCLibrary alloc] init];
  56. });
  57. return sharedLibrary;
  58. }
  59. + (void *)sharedInstance
  60. {
  61. return [self sharedLibrary].instance;
  62. }
  63. - (instancetype)init
  64. {
  65. if (self = [super init]) {
  66. [self prepareInstanceWithOptions:nil];
  67. }
  68. return self;
  69. }
  70. - (instancetype)initWithOptions:(NSArray *)options
  71. {
  72. if (self = [super init]) {
  73. [self prepareInstanceWithOptions:options];
  74. }
  75. return self;
  76. }
  77. - (void)prepareInstanceWithOptions:(NSArray *)options
  78. {
  79. NSArray *allOptions = options ? [[self _defaultOptions] arrayByAddingObjectsFromArray:options] : [self _defaultOptions];
  80. NSUInteger paramNum = 0;
  81. int count = (int)allOptions.count;
  82. const char *lib_vlc_params[count];
  83. while (paramNum < count) {
  84. lib_vlc_params[paramNum] = [allOptions[paramNum] cStringUsingEncoding:NSASCIIStringEncoding];
  85. paramNum++;
  86. }
  87. _instance = libvlc_new(count, lib_vlc_params);
  88. NSAssert(_instance, @"libvlc failed to initialize");
  89. }
  90. - (NSArray *)_defaultOptions
  91. {
  92. NSArray *vlcParams = [[NSUserDefaults standardUserDefaults] objectForKey:@"VLCParams"];
  93. #if TARGET_OS_IPHONE
  94. if (!vlcParams) {
  95. vlcParams = @[@"--no-color",
  96. @"--no-osd",
  97. @"--no-video-title-show",
  98. @"--no-snapshot-preview",
  99. @"--http-reconnect",
  100. #ifndef NOSCARYCODECS
  101. #ifndef __LP64__
  102. @"--avcodec-fast",
  103. #endif
  104. #endif
  105. @"--text-renderer=freetype",
  106. @"--avi-index=3",
  107. @"--audio-resampler=soxr"];
  108. }
  109. #else
  110. if (!vlcParams) {
  111. NSMutableArray *defaultParams = [NSMutableArray array];
  112. [defaultParams addObject:@"--play-and-pause"]; // We want every movie to pause instead of stopping at eof
  113. [defaultParams addObject:@"--no-color"]; // Don't use color in output (Xcode doesn't show it)
  114. [defaultParams addObject:@"--no-video-title-show"]; // Don't show the title on overlay when starting to play
  115. [defaultParams addObject:@"--verbose=4"]; // Let's not wreck the logs
  116. [defaultParams addObject:@"--no-sout-keep"];
  117. [defaultParams addObject:@"--vout=macosx"]; // Select Mac OS X video output
  118. [defaultParams addObject:@"--text-renderer=freetype"];
  119. [defaultParams addObject:@"--extraintf=macosx_dialog_provider"]; // Some extra dialog (login, progress) may come up from here
  120. [defaultParams addObject:@"--audio-resampler=soxr"]; // High quality resamper (will be used by default on VLC 4.0)
  121. [[NSUserDefaults standardUserDefaults] setObject:defaultParams forKey:@"VLCParams"];
  122. [[NSUserDefaults standardUserDefaults] synchronize];
  123. vlcParams = defaultParams;
  124. }
  125. #endif
  126. return vlcParams;
  127. }
  128. - (void)setDebugLogging:(BOOL)debugLogging
  129. {
  130. if (!_instance)
  131. return;
  132. _debugLogging = debugLogging;
  133. if (debugLogging) {
  134. libvlc_log_set(_instance, HandleMessage, (__bridge void *)(self));
  135. } else {
  136. libvlc_log_unset(_instance);
  137. if (_logFileStream)
  138. fclose(_logFileStream);
  139. }
  140. }
  141. - (void)setDebugLoggingLevel:(int)debugLoggingLevel
  142. {
  143. if (debugLoggingLevel >= 0 && debugLoggingLevel <= 4) {
  144. _debugLoggingLevel = debugLoggingLevel;
  145. } else {
  146. VKLog(@"Invalid debugLoggingLevel of %d provided", debugLoggingLevel);
  147. VKLog(@"Please provide a valid debugLoggingLevel between 0 and 4");
  148. VKLog(@"Defaulting debugLoggingLevel to 0 (just errors)");
  149. _debugLoggingLevel = 0;
  150. }
  151. }
  152. - (BOOL)setDebugLoggingToFile:(NSString * _Nonnull)filePath
  153. {
  154. if (!filePath)
  155. return NO;
  156. if (!_instance)
  157. return NO;
  158. if (_debugLogging) {
  159. libvlc_log_unset(_instance);
  160. }
  161. if (_logFileStream) {
  162. fclose(_logFileStream);
  163. }
  164. _logFileStream = fopen([filePath UTF8String], "a");
  165. if (_logFileStream) {
  166. libvlc_log_set_file(_instance, _logFileStream);
  167. _debugLogging = YES;
  168. return YES;
  169. }
  170. return NO;
  171. }
  172. - (void)setDebugLoggingTarget:(id<VLCLibraryLogReceiverProtocol>) target
  173. {
  174. if (![target respondsToSelector:@selector(handleMessage:debugLevel:)]) {
  175. VKLog(@"%s: target object does not implement required protocol", __func__);
  176. return;
  177. }
  178. _debugLoggingTarget = target;
  179. if (!_instance)
  180. return;
  181. if (_debugLogging) {
  182. libvlc_log_unset(_instance);
  183. }
  184. if (_logFileStream)
  185. fclose(_logFileStream);
  186. if (target) {
  187. libvlc_log_set(_instance, HandleMessageForCustomTarget, (__bridge void *)(self));
  188. _debugLogging = YES;
  189. }
  190. }
  191. - (NSString *)version
  192. {
  193. return @(libvlc_get_version());
  194. }
  195. - (NSString *)compiler
  196. {
  197. return @(libvlc_get_compiler());
  198. }
  199. - (NSString *)changeset
  200. {
  201. return @(libvlc_get_changeset());
  202. }
  203. - (void)setHumanReadableName:(NSString *)readableName withHTTPUserAgent:(NSString *)userAgent
  204. {
  205. if (_instance)
  206. libvlc_set_user_agent(_instance, [readableName UTF8String], [userAgent UTF8String]);
  207. }
  208. - (void)setApplicationIdentifier:(NSString *)identifier withVersion:(NSString *)version andApplicationIconName:(NSString *)icon
  209. {
  210. if (_instance)
  211. libvlc_set_app_id(_instance, [identifier UTF8String], [version UTF8String], [icon UTF8String]);
  212. }
  213. - (void)dealloc
  214. {
  215. if (_instance) {
  216. libvlc_log_unset(_instance);
  217. libvlc_release(_instance);
  218. }
  219. if (_logFileStream) {
  220. fclose(_logFileStream);
  221. }
  222. }
  223. @end
  224. static void HandleMessage(void *data,
  225. int level,
  226. const libvlc_log_t *ctx,
  227. const char *fmt,
  228. va_list args)
  229. {
  230. VLCLibrary *libraryInstance = (__bridge VLCLibrary *)data;
  231. const char *log_prefix;
  232. switch (level)
  233. {
  234. case LIBVLC_NOTICE:
  235. level = 0;
  236. log_prefix = "INF";
  237. break;
  238. case LIBVLC_ERROR:
  239. level = 1;
  240. log_prefix = "ERR";
  241. break;
  242. case LIBVLC_WARNING:
  243. level = 2;
  244. log_prefix = "WAR";
  245. break;
  246. case LIBVLC_DEBUG:
  247. default:
  248. level = 3;
  249. log_prefix = "DBG";
  250. break;
  251. }
  252. if (level > libraryInstance.debugLoggingLevel)
  253. return;
  254. char *str;
  255. if (vasprintf(&str, fmt, args) == -1)
  256. return;
  257. VKLog(@"[%s] %s", log_prefix, str);
  258. free(str);
  259. }
  260. static void HandleMessageForCustomTarget(void *data,
  261. int level,
  262. const libvlc_log_t *ctx,
  263. const char *fmt,
  264. va_list args)
  265. {
  266. VLCLibrary *libraryInstance = (__bridge VLCLibrary *)data;
  267. id debugLoggingTarget = libraryInstance.debugLoggingTarget;
  268. if (!debugLoggingTarget) {
  269. return;
  270. }
  271. char *str = NULL;
  272. if (vasprintf(&str, fmt, args) == -1) {
  273. if (str)
  274. free(str);
  275. return;
  276. }
  277. if (str == NULL)
  278. return;
  279. NSString *message = [[NSString alloc] initWithBytesNoCopy:str length:strlen(str) encoding:NSUTF8StringEncoding freeWhenDone:YES];
  280. [debugLoggingTarget handleMessage:message debugLevel:level];
  281. }