VLCLibrary.m 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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-stats",
  99. @"--no-snapshot-preview",
  100. @"--http-reconnect",
  101. #ifndef NOSCARYCODECS
  102. #ifndef __LP64__
  103. @"--avcodec-fast",
  104. #endif
  105. #endif
  106. @"--text-renderer=freetype",
  107. @"--avi-index=3",
  108. @"--audio-resampler=soxr"];
  109. }
  110. #else
  111. if (!vlcParams) {
  112. NSMutableArray *defaultParams = [NSMutableArray array];
  113. [defaultParams addObject:@"--play-and-pause"]; // We want every movie to pause instead of stopping at eof
  114. [defaultParams addObject:@"--no-color"]; // Don't use color in output (Xcode doesn't show it)
  115. [defaultParams addObject:@"--no-video-title-show"]; // Don't show the title on overlay when starting to play
  116. [defaultParams addObject:@"--verbose=4"]; // Let's not wreck the logs
  117. [defaultParams addObject:@"--no-sout-keep"];
  118. [defaultParams addObject:@"--vout=macosx"]; // Select Mac OS X video output
  119. [defaultParams addObject:@"--text-renderer=freetype"];
  120. [defaultParams addObject:@"--extraintf=macosx_dialog_provider"]; // Some extra dialog (login, progress) may come up from here
  121. [defaultParams addObject:@"--audio-resampler=soxr"]; // High quality resamper (will be used by default on VLC 4.0)
  122. [[NSUserDefaults standardUserDefaults] setObject:defaultParams forKey:@"VLCParams"];
  123. [[NSUserDefaults standardUserDefaults] synchronize];
  124. vlcParams = defaultParams;
  125. }
  126. #endif
  127. return vlcParams;
  128. }
  129. - (void)setDebugLogging:(BOOL)debugLogging
  130. {
  131. if (!_instance)
  132. return;
  133. _debugLogging = debugLogging;
  134. if (debugLogging) {
  135. libvlc_log_set(_instance, HandleMessage, (__bridge void *)(self));
  136. } else {
  137. libvlc_log_unset(_instance);
  138. if (_logFileStream)
  139. fclose(_logFileStream);
  140. }
  141. }
  142. - (void)setDebugLoggingLevel:(int)debugLoggingLevel
  143. {
  144. if (debugLoggingLevel >= 0 && debugLoggingLevel <= 4) {
  145. _debugLoggingLevel = debugLoggingLevel;
  146. } else {
  147. VKLog(@"Invalid debugLoggingLevel of %d provided", debugLoggingLevel);
  148. VKLog(@"Please provide a valid debugLoggingLevel between 0 and 4");
  149. VKLog(@"Defaulting debugLoggingLevel to 0 (just errors)");
  150. _debugLoggingLevel = 0;
  151. }
  152. }
  153. - (BOOL)setDebugLoggingToFile:(NSString * _Nonnull)filePath
  154. {
  155. if (!filePath)
  156. return NO;
  157. if (!_instance)
  158. return NO;
  159. if (_debugLogging) {
  160. libvlc_log_unset(_instance);
  161. }
  162. if (_logFileStream) {
  163. fclose(_logFileStream);
  164. }
  165. _logFileStream = fopen([filePath UTF8String], "a");
  166. if (_logFileStream) {
  167. libvlc_log_set_file(_instance, _logFileStream);
  168. _debugLogging = YES;
  169. return YES;
  170. }
  171. return NO;
  172. }
  173. - (void)setDebugLoggingTarget:(id<VLCLibraryLogReceiverProtocol>) target
  174. {
  175. if (![target respondsToSelector:@selector(handleMessage:debugLevel:)]) {
  176. VKLog(@"%s: target object does not implement required protocol", __func__);
  177. return;
  178. }
  179. _debugLoggingTarget = target;
  180. if (!_instance)
  181. return;
  182. if (_debugLogging) {
  183. libvlc_log_unset(_instance);
  184. }
  185. if (_logFileStream)
  186. fclose(_logFileStream);
  187. if (target) {
  188. libvlc_log_set(_instance, HandleMessageForCustomTarget, (__bridge void *)(self));
  189. _debugLogging = YES;
  190. }
  191. }
  192. - (NSString *)version
  193. {
  194. return @(libvlc_get_version());
  195. }
  196. - (NSString *)compiler
  197. {
  198. return @(libvlc_get_compiler());
  199. }
  200. - (NSString *)changeset
  201. {
  202. return @(libvlc_get_changeset());
  203. }
  204. - (void)setHumanReadableName:(NSString *)readableName withHTTPUserAgent:(NSString *)userAgent
  205. {
  206. if (_instance)
  207. libvlc_set_user_agent(_instance, [readableName UTF8String], [userAgent UTF8String]);
  208. }
  209. - (void)setApplicationIdentifier:(NSString *)identifier withVersion:(NSString *)version andApplicationIconName:(NSString *)icon
  210. {
  211. if (_instance)
  212. libvlc_set_app_id(_instance, [identifier UTF8String], [version UTF8String], [icon UTF8String]);
  213. }
  214. - (void)dealloc
  215. {
  216. if (_instance) {
  217. libvlc_log_unset(_instance);
  218. libvlc_release(_instance);
  219. }
  220. if (_logFileStream) {
  221. fclose(_logFileStream);
  222. }
  223. }
  224. @end
  225. static void HandleMessage(void *data,
  226. int level,
  227. const libvlc_log_t *ctx,
  228. const char *fmt,
  229. va_list args)
  230. {
  231. VLCLibrary *libraryInstance = (__bridge VLCLibrary *)data;
  232. if (level > libraryInstance.debugLoggingLevel)
  233. return;
  234. char *str = NULL;
  235. if (vasprintf(&str, fmt, args) == -1) {
  236. if (str)
  237. free(str);
  238. return;
  239. }
  240. if (str == NULL)
  241. return;
  242. VKLog(@"%s", str);
  243. free(str);
  244. }
  245. static void HandleMessageForCustomTarget(void *data,
  246. int level,
  247. const libvlc_log_t *ctx,
  248. const char *fmt,
  249. va_list args)
  250. {
  251. VLCLibrary *libraryInstance = (__bridge VLCLibrary *)data;
  252. id debugLoggingTarget = libraryInstance.debugLoggingTarget;
  253. if (!debugLoggingTarget) {
  254. return;
  255. }
  256. char *str = NULL;
  257. if (vasprintf(&str, fmt, args) == -1) {
  258. if (str)
  259. free(str);
  260. return;
  261. }
  262. if (str == NULL)
  263. return;
  264. NSString *message = [[NSString alloc] initWithBytesNoCopy:str length:strlen(str) encoding:NSUTF8StringEncoding freeWhenDone:YES];
  265. [debugLoggingTarget handleMessage:message debugLevel:level];
  266. }