VLCLibrary.m 6.1 KB

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