VLCLibrary.m 6.7 KB

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