VLCLibrary.m 9.5 KB

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