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