VLCLibrary.m 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. #endif
  31. #ifdef HAVE_CONFIG_H
  32. # include "config.h"
  33. #endif
  34. #include <vlc/vlc.h>
  35. static void HandleMessage(void *,
  36. int,
  37. const libvlc_log_t *,
  38. const char *,
  39. va_list);
  40. static VLCLibrary * sharedLibrary = nil;
  41. @implementation VLCLibrary
  42. + (VLCLibrary *)sharedLibrary
  43. {
  44. static dispatch_once_t onceToken;
  45. dispatch_once(&onceToken, ^{
  46. sharedLibrary = [[VLCLibrary alloc] init];
  47. });
  48. return sharedLibrary;
  49. }
  50. + (void *)sharedInstance
  51. {
  52. return [self sharedLibrary].instance;
  53. }
  54. - (instancetype)init
  55. {
  56. if (self = [super init]) {
  57. [self prepareInstanceWithOptions:nil];
  58. }
  59. return self;
  60. }
  61. - (instancetype)initWithOptions:(NSArray*)options
  62. {
  63. if (self = [super init]) {
  64. [self prepareInstanceWithOptions:options];
  65. }
  66. return self;
  67. }
  68. - (void)prepareInstanceWithOptions:(NSArray *)options
  69. {
  70. NSArray *allOptions = options ? [[self _defaultOptions] arrayByAddingObjectsFromArray:options] : [self _defaultOptions];
  71. NSUInteger paramNum = 0;
  72. int count = (int)allOptions.count;
  73. const char *lib_vlc_params[count];
  74. while (paramNum < count) {
  75. lib_vlc_params[paramNum] = [allOptions[paramNum] cStringUsingEncoding:NSASCIIStringEncoding];
  76. paramNum++;
  77. }
  78. _instance = libvlc_new(count, lib_vlc_params);
  79. NSAssert(_instance, @"libvlc failed to initialize");
  80. }
  81. - (NSArray *)_defaultOptions
  82. {
  83. NSArray *vlcParams = [[NSUserDefaults standardUserDefaults] objectForKey:@"VLCParams"];
  84. #if TARGET_OS_IPHONE
  85. if (!vlcParams) {
  86. vlcParams = @[@"--no-color",
  87. @"--no-osd",
  88. @"--no-video-title-show",
  89. @"--no-stats",
  90. @"--no-snapshot-preview",
  91. #ifndef NOSCARYCODECS
  92. #ifndef __LP64__
  93. @"--avcodec-fast",
  94. #endif
  95. #endif
  96. @"--text-renderer=freetype",
  97. @"--avi-index=3"];
  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. [[NSUserDefaults standardUserDefaults] setObject:defaultParams forKey:@"VLCParams"];
  111. [[NSUserDefaults standardUserDefaults] synchronize];
  112. vlcParams = defaultParams;
  113. }
  114. #endif
  115. return vlcParams;
  116. }
  117. - (void)setDebugLogging:(BOOL)debugLogging
  118. {
  119. if (!_instance)
  120. return;
  121. if (debugLogging) {
  122. libvlc_log_set(_instance, HandleMessage, (__bridge void *)(self));
  123. } else {
  124. libvlc_log_unset(_instance);
  125. }
  126. }
  127. - (NSString *)version
  128. {
  129. return @(libvlc_get_version());
  130. }
  131. - (NSString *)compiler
  132. {
  133. return @(libvlc_get_compiler());
  134. }
  135. - (NSString *)changeset
  136. {
  137. return @(libvlc_get_changeset());
  138. }
  139. - (void)setHumanReadableName:(NSString *)readableName withHTTPUserAgent:(NSString *)userAgent
  140. {
  141. if (_instance)
  142. libvlc_set_user_agent(_instance, [readableName UTF8String], [userAgent UTF8String]);
  143. }
  144. - (void)setApplicationIdentifier:(NSString *)identifier withVersion:(NSString *)version andApplicationIconName:(NSString *)icon
  145. {
  146. if (_instance)
  147. libvlc_set_app_id(_instance, [identifier UTF8String], [version UTF8String], [icon UTF8String]);
  148. }
  149. - (void)dealloc
  150. {
  151. if (_instance)
  152. libvlc_release(_instance);
  153. }
  154. @end
  155. static void HandleMessage(void *data,
  156. int level,
  157. const libvlc_log_t *ctx,
  158. const char *fmt,
  159. va_list args)
  160. {
  161. VLCLibrary *libraryInstance = (__bridge VLCLibrary *)data;
  162. if (level < libraryInstance.debugLoggingLevel)
  163. return;
  164. char *str;
  165. if (vasprintf(&str, fmt, args) == -1) {
  166. if (str)
  167. free(str);
  168. str = NULL;
  169. return;
  170. }
  171. if (str == NULL)
  172. return;
  173. VKLog(@"%s", str);
  174. free(str);
  175. str = NULL;
  176. }