VLCLibrary.m 6.0 KB

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