VLCLibrary.m 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 VLCLibrary * sharedLibrary = nil;
  40. @interface VLCLibrary()
  41. {
  42. FILE *_logFileStream;
  43. }
  44. @end
  45. @implementation VLCLibrary
  46. + (VLCLibrary *)sharedLibrary
  47. {
  48. static dispatch_once_t onceToken;
  49. dispatch_once(&onceToken, ^{
  50. sharedLibrary = [[VLCLibrary alloc] init];
  51. });
  52. return sharedLibrary;
  53. }
  54. + (void *)sharedInstance
  55. {
  56. return [self sharedLibrary].instance;
  57. }
  58. - (instancetype)init
  59. {
  60. if (self = [super init]) {
  61. [self prepareInstanceWithOptions:nil];
  62. }
  63. return self;
  64. }
  65. - (instancetype)initWithOptions:(NSArray *)options
  66. {
  67. if (self = [super init]) {
  68. [self prepareInstanceWithOptions:options];
  69. }
  70. return self;
  71. }
  72. - (void)prepareInstanceWithOptions:(NSArray *)options
  73. {
  74. NSArray *allOptions = options ? [[self _defaultOptions] arrayByAddingObjectsFromArray:options] : [self _defaultOptions];
  75. NSUInteger paramNum = 0;
  76. int count = (int)allOptions.count;
  77. const char *lib_vlc_params[count];
  78. while (paramNum < count) {
  79. lib_vlc_params[paramNum] = [allOptions[paramNum] cStringUsingEncoding:NSASCIIStringEncoding];
  80. paramNum++;
  81. }
  82. _instance = libvlc_new(count, lib_vlc_params);
  83. NSAssert(_instance, @"libvlc failed to initialize");
  84. }
  85. - (NSArray *)_defaultOptions
  86. {
  87. NSArray *vlcParams = [[NSUserDefaults standardUserDefaults] objectForKey:@"VLCParams"];
  88. #if TARGET_OS_IPHONE
  89. if (!vlcParams) {
  90. vlcParams = @[@"--no-color",
  91. @"--no-osd",
  92. @"--no-video-title-show",
  93. @"--no-stats",
  94. @"--no-snapshot-preview",
  95. @"--http-reconnect",
  96. #ifndef NOSCARYCODECS
  97. #ifndef __LP64__
  98. @"--avcodec-fast",
  99. #endif
  100. #endif
  101. @"--text-renderer=freetype",
  102. @"--avi-index=3",
  103. @"--audio-resampler=soxr"];
  104. }
  105. #else
  106. if (!vlcParams) {
  107. NSMutableArray *defaultParams = [NSMutableArray array];
  108. [defaultParams addObject:@"--play-and-pause"]; // We want every movie to pause instead of stopping at eof
  109. [defaultParams addObject:@"--no-color"]; // Don't use color in output (Xcode doesn't show it)
  110. [defaultParams addObject:@"--no-video-title-show"]; // Don't show the title on overlay when starting to play
  111. [defaultParams addObject:@"--verbose=4"]; // Let's not wreck the logs
  112. [defaultParams addObject:@"--no-sout-keep"];
  113. [defaultParams addObject:@"--vout=macosx"]; // Select Mac OS X video output
  114. [defaultParams addObject:@"--text-renderer=freetype"];
  115. [defaultParams addObject:@"--extraintf=macosx_dialog_provider"]; // Some extra dialog (login, progress) may come up from here
  116. [defaultParams addObject:@"--audio-resampler=soxr"]; // High quality resamper (will be used by default on VLC 4.0)
  117. [[NSUserDefaults standardUserDefaults] setObject:defaultParams forKey:@"VLCParams"];
  118. [[NSUserDefaults standardUserDefaults] synchronize];
  119. vlcParams = defaultParams;
  120. }
  121. #endif
  122. return vlcParams;
  123. }
  124. - (void)setDebugLogging:(BOOL)debugLogging
  125. {
  126. if (!_instance)
  127. return;
  128. _debugLogging = debugLogging;
  129. if (debugLogging) {
  130. libvlc_log_set(_instance, HandleMessage, (__bridge void *)(self));
  131. } else {
  132. libvlc_log_unset(_instance);
  133. if (_logFileStream)
  134. fclose(_logFileStream);
  135. }
  136. }
  137. - (void)setDebugLoggingLevel:(int)debugLoggingLevel
  138. {
  139. if (debugLoggingLevel >= 0 && debugLoggingLevel <= 4) {
  140. _debugLoggingLevel = debugLoggingLevel;
  141. } else {
  142. VKLog(@"Invalid debugLoggingLevel of %d provided", debugLoggingLevel);
  143. VKLog(@"Please provide a valid debugLoggingLevel between 0 and 4");
  144. VKLog(@"Defaulting debugLoggingLevel to 0 (just errors)");
  145. _debugLoggingLevel = 0;
  146. }
  147. }
  148. - (void)setDebugLoggingToFile:(NSString * _Nonnull)filePath
  149. {
  150. if (!filePath)
  151. return;
  152. if (!_instance)
  153. return;
  154. if (_debugLogging) {
  155. libvlc_log_unset(_instance);
  156. }
  157. _logFileStream = fopen([filePath UTF8String], "a");
  158. if (_logFileStream) {
  159. libvlc_log_set_file(_instance, _logFileStream);
  160. }
  161. }
  162. - (NSString *)version
  163. {
  164. return @(libvlc_get_version());
  165. }
  166. - (NSString *)compiler
  167. {
  168. return @(libvlc_get_compiler());
  169. }
  170. - (NSString *)changeset
  171. {
  172. return @(libvlc_get_changeset());
  173. }
  174. - (void)setHumanReadableName:(NSString *)readableName withHTTPUserAgent:(NSString *)userAgent
  175. {
  176. if (_instance)
  177. libvlc_set_user_agent(_instance, [readableName UTF8String], [userAgent UTF8String]);
  178. }
  179. - (void)setApplicationIdentifier:(NSString *)identifier withVersion:(NSString *)version andApplicationIconName:(NSString *)icon
  180. {
  181. if (_instance)
  182. libvlc_set_app_id(_instance, [identifier UTF8String], [version UTF8String], [icon UTF8String]);
  183. }
  184. - (void)dealloc
  185. {
  186. if (_instance) {
  187. libvlc_log_unset(_instance);
  188. libvlc_release(_instance);
  189. }
  190. if (_logFileStream) {
  191. fclose(_logFileStream);
  192. }
  193. }
  194. @end
  195. static void HandleMessage(void *data,
  196. int level,
  197. const libvlc_log_t *ctx,
  198. const char *fmt,
  199. va_list args)
  200. {
  201. VLCLibrary *libraryInstance = (__bridge VLCLibrary *)data;
  202. if (level > libraryInstance.debugLoggingLevel)
  203. return;
  204. char *str;
  205. if (vasprintf(&str, fmt, args) == -1) {
  206. if (str)
  207. free(str);
  208. str = NULL;
  209. return;
  210. }
  211. if (str == NULL)
  212. return;
  213. VKLog(@"%s", str);
  214. free(str);
  215. str = NULL;
  216. }