VLCLibrary.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*****************************************************************************
  2. * VLCLibrary.h: VLC.framework VLCLibrary implementation
  3. *****************************************************************************
  4. * Copyright (C) 2007 Pierre d'Herbemont
  5. * Copyright (C) 2007 the VideoLAN team
  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
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 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 General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  23. *****************************************************************************/
  24. #import "VLCLibrary.h"
  25. #include <vlc/vlc.h>
  26. #include <vlc/libvlc_structures.h>
  27. static VLCLibrary *sharedLibrary = nil;
  28. // TODO: Change from a terminal error to raising an exception?
  29. void __quit_on_exception( void * e, const char * function, const char * file, int line_number )
  30. {
  31. libvlc_exception_t *ex = (libvlc_exception_t *)e;
  32. if (libvlc_exception_raised( ex ))
  33. {
  34. /* XXX: localization */
  35. NSRunCriticalAlertPanel( @"Error", [NSString stringWithFormat:@"libvlc has thrown us an error: %s (%s:%d %s)",
  36. libvlc_exception_get_message( ex ), file, line_number, function], @"Quit", nil, nil );
  37. exit( ex->i_code );
  38. }
  39. }
  40. static void *DestroySharedLibraryAtExit()
  41. {
  42. /* Release the global object that may have been alloc-ed
  43. * in -[VLCLibrary init] */
  44. [sharedLibrary release];
  45. sharedLibrary = nil;
  46. return nil;
  47. }
  48. @implementation VLCLibrary
  49. + (VLCLibrary *)sharedLibrary
  50. {
  51. if (!sharedLibrary)
  52. {
  53. // Initialize a shared instance
  54. [[self alloc] init];
  55. }
  56. return [[sharedLibrary retain] autorelease];
  57. }
  58. + (void *)sharedInstance
  59. {
  60. return [[self sharedLibrary] instance];
  61. }
  62. - (id)init
  63. {
  64. if (self = [super init])
  65. {
  66. libvlc_exception_t ex;
  67. libvlc_exception_init( &ex );
  68. // Figure out the frameworks path
  69. char *applicationPath = strdup( [[NSString stringWithFormat:@"%@/Versions/Current/VLC",
  70. [[NSBundle bundleForClass:[VLCLibrary class]] bundlePath]] UTF8String] );
  71. // TODO: Raise error if there is no memory available
  72. char *lib_vlc_params[] = {
  73. applicationPath, "-I", "dummy", "-vvvv",
  74. "--opengl-provider", "minimal_macosx",
  75. "--no-video-title-show", NULL
  76. };
  77. instance = (void *)libvlc_new( 7, lib_vlc_params, &ex );
  78. quit_on_exception( &ex );
  79. if (!sharedLibrary)
  80. sharedLibrary = self;
  81. // Assignment unneeded, as the audio unit will do it for us
  82. /*audio = */ [[VLCAudio alloc] initWithLibrary:self];
  83. // free allocated resources
  84. free( applicationPath );
  85. atexit(DestroySharedLibraryAtExit);
  86. }
  87. return self;
  88. }
  89. - (void)dealloc
  90. {
  91. if (instance)
  92. {
  93. libvlc_exception_t ex;
  94. libvlc_exception_init( &ex );
  95. libvlc_destroy( instance, &ex );
  96. }
  97. instance = nil;
  98. [audio release];
  99. [super dealloc];
  100. }
  101. - (void *)instance
  102. {
  103. return instance;
  104. }
  105. - (VLCAudio *)audio
  106. {
  107. return audio;
  108. }
  109. @end
  110. @implementation VLCLibrary (VLCAudioBridging)
  111. - (void)setAudio:(VLCAudio *)value
  112. {
  113. if (!audio)
  114. audio = value;
  115. }
  116. @end