VLCLibrary.m 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*****************************************************************************
  2. * VLCLibrary.m: VLCKit.framework VLCLibrary implementation
  3. *****************************************************************************
  4. * Copyright (C) 2007 Pierre d'Herbemont
  5. * Copyright (C) 2007 VLC authors and VideoLAN
  6. * $Id$
  7. *
  8. * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU Lesser General Public License as published by
  12. * the Free Software Foundation; either version 2.1 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public License
  21. * along with this program; if not, write to the Free Software Foundation,
  22. * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  23. *****************************************************************************/
  24. #import "VLCLibrary.h"
  25. #if TARGET_OS_TV
  26. # include "vlc-plugins-AppleTV.h"
  27. #elif TARGET_OS_IPHONE
  28. # include "vlc-plugins-iPhone.h"
  29. #else
  30. # include "vlc-plugins-MacOSX.h"
  31. #endif
  32. #include <vlc/vlc.h>
  33. static void HandleMessage(void *,
  34. int,
  35. const libvlc_log_t *,
  36. const char *,
  37. va_list);
  38. static VLCLibrary * sharedLibrary = nil;
  39. @implementation VLCLibrary
  40. + (VLCLibrary *)sharedLibrary
  41. {
  42. static dispatch_once_t onceToken;
  43. dispatch_once(&onceToken, ^{
  44. sharedLibrary = [[VLCLibrary alloc] init];
  45. });
  46. return sharedLibrary;
  47. }
  48. + (void *)sharedInstance
  49. {
  50. return [self sharedLibrary].instance;
  51. }
  52. - (instancetype)init
  53. {
  54. if (self = [super init]) {
  55. [self prepareInstanceWithOptions:nil];
  56. }
  57. return self;
  58. }
  59. - (instancetype)initWithOptions:(NSArray *)options
  60. {
  61. if (self = [super init]) {
  62. [self prepareInstanceWithOptions:options];
  63. }
  64. return self;
  65. }
  66. - (void)prepareInstanceWithOptions:(NSArray *)options
  67. {
  68. NSArray *allOptions = options ? [[self _defaultOptions] arrayByAddingObjectsFromArray:options] : [self _defaultOptions];
  69. NSUInteger paramNum = 0;
  70. int count = (int)allOptions.count;
  71. const char *lib_vlc_params[count];
  72. while (paramNum < count) {
  73. lib_vlc_params[paramNum] = [allOptions[paramNum] cStringUsingEncoding:NSASCIIStringEncoding];
  74. paramNum++;
  75. }
  76. _instance = libvlc_new(count, lib_vlc_params);
  77. NSAssert(_instance, @"libvlc failed to initialize");
  78. }
  79. - (NSArray *)_defaultOptions
  80. {
  81. NSArray *vlcParams = [[NSUserDefaults standardUserDefaults] objectForKey:@"VLCParams"];
  82. #if TARGET_OS_IPHONE
  83. if (!vlcParams) {
  84. vlcParams = @[@"--no-color",
  85. @"--no-osd",
  86. @"--no-video-title-show",
  87. @"--no-stats",
  88. @"--no-snapshot-preview",
  89. @"--http-reconnect",
  90. #ifndef NOSCARYCODECS
  91. #ifndef __LP64__
  92. @"--avcodec-fast",
  93. #endif
  94. #endif
  95. @"--text-renderer=freetype",
  96. @"--avi-index=3",
  97. @"--audio-resampler=soxr"];
  98. }
  99. #else
  100. if (!vlcParams) {
  101. NSMutableArray *defaultParams = [NSMutableArray array];
  102. [defaultParams addObject:@"--play-and-pause"]; // We want every movie to pause instead of stopping at eof
  103. [defaultParams addObject:@"--no-color"]; // Don't use color in output (Xcode doesn't show it)
  104. [defaultParams addObject:@"--no-video-title-show"]; // Don't show the title on overlay when starting to play
  105. [defaultParams addObject:@"--verbose=4"]; // Let's not wreck the logs
  106. [defaultParams addObject:@"--no-sout-keep"];
  107. [defaultParams addObject:@"--vout=macosx"]; // Select Mac OS X video output
  108. [defaultParams addObject:@"--text-renderer=freetype"];
  109. [defaultParams addObject:@"--extraintf=macosx_dialog_provider"]; // Some extra dialog (login, progress) may come up from here
  110. [defaultParams addObject:@"--audio-resampler=soxr"]; // High quality resamper (will be used by default on VLC 4.0)
  111. [[NSUserDefaults standardUserDefaults] setObject:defaultParams forKey:@"VLCParams"];
  112. [[NSUserDefaults standardUserDefaults] synchronize];
  113. vlcParams = defaultParams;
  114. }
  115. #endif
  116. return vlcParams;
  117. }
  118. - (void)setDebugLogging:(BOOL)debugLogging
  119. {
  120. if (!_instance)
  121. return;
  122. _debugLogging = debugLogging;
  123. if (debugLogging) {
  124. libvlc_log_set(_instance, HandleMessage, (__bridge void *)(self));
  125. } else {
  126. libvlc_log_unset(_instance);
  127. }
  128. }
  129. - (void)setDebugLoggingLevel:(int)debugLoggingLevel
  130. {
  131. if (debugLoggingLevel >= 0 && debugLoggingLevel <= 4) {
  132. _debugLoggingLevel = debugLoggingLevel;
  133. } else {
  134. VKLog(@"Invalid debugLoggingLevel of %d provided", debugLoggingLevel);
  135. VKLog(@"Please provide a valid debugLoggingLevel between 0 and 4");
  136. VKLog(@"Defaulting debugLoggingLevel to 0 (just errors)");
  137. _debugLoggingLevel = 0;
  138. }
  139. }
  140. - (NSString *)version
  141. {
  142. return @(libvlc_get_version());
  143. }
  144. - (NSString *)compiler
  145. {
  146. return @(libvlc_get_compiler());
  147. }
  148. - (NSString *)changeset
  149. {
  150. return @(libvlc_get_changeset());
  151. }
  152. - (void)setHumanReadableName:(NSString *)readableName withHTTPUserAgent:(NSString *)userAgent
  153. {
  154. if (_instance)
  155. libvlc_set_user_agent(_instance, [readableName UTF8String], [userAgent UTF8String]);
  156. }
  157. - (void)setApplicationIdentifier:(NSString *)identifier withVersion:(NSString *)version andApplicationIconName:(NSString *)icon
  158. {
  159. if (_instance)
  160. libvlc_set_app_id(_instance, [identifier UTF8String], [version UTF8String], [icon UTF8String]);
  161. }
  162. - (void)dealloc
  163. {
  164. if (_instance)
  165. libvlc_release(_instance);
  166. }
  167. @end
  168. static void HandleMessage(void *data,
  169. int level,
  170. const libvlc_log_t *ctx,
  171. const char *fmt,
  172. va_list args)
  173. {
  174. VLCLibrary *libraryInstance = (__bridge VLCLibrary *)data;
  175. if (level < libraryInstance.debugLoggingLevel)
  176. return;
  177. char *str;
  178. if (vasprintf(&str, fmt, args) == -1) {
  179. if (str)
  180. free(str);
  181. str = NULL;
  182. return;
  183. }
  184. if (str == NULL)
  185. return;
  186. VKLog(@"%s", str);
  187. free(str);
  188. str = NULL;
  189. }