VLCLibrary.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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( void )
  41. {
  42. /* Release the global object that may have been alloc-ed
  43. * in -[VLCLibrary init] */
  44. [sharedLibrary release];
  45. sharedLibrary = nil;
  46. return NULL;
  47. }
  48. @implementation VLCLibrary
  49. + (VLCLibrary *)sharedLibrary
  50. {
  51. if (!sharedLibrary)
  52. {
  53. /* Initialize a shared instance */
  54. sharedLibrary = [[self alloc] init];
  55. /* Make sure, this will get released at some point */
  56. atexit( (void*)DestroySharedLibraryAtExit );
  57. }
  58. return [[sharedLibrary retain] autorelease];
  59. }
  60. + (void *)sharedInstance
  61. {
  62. return [[self sharedLibrary] instance];
  63. }
  64. - (id)init
  65. {
  66. if (self = [super init])
  67. {
  68. libvlc_exception_t ex;
  69. libvlc_exception_init( &ex );
  70. // Figure out the frameworks path
  71. char *applicationPath = strdup( [[NSString stringWithFormat:@"%@/Versions/Current/VLC",
  72. [[NSBundle bundleForClass:[VLCLibrary class]] bundlePath]] UTF8String] );
  73. // TODO: Raise error if there is no memory available
  74. char *lib_vlc_params[] = {
  75. applicationPath, "-I", "dummy", "-vvvv",
  76. "--opengl-provider", "minimal_macosx",
  77. "--no-video-title-show", NULL
  78. };
  79. instance = (void *)libvlc_new( 7, lib_vlc_params, &ex );
  80. quit_on_exception( &ex );
  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. }
  86. return self;
  87. }
  88. - (void)dealloc
  89. {
  90. if (instance)
  91. {
  92. libvlc_exception_t ex;
  93. libvlc_exception_init( &ex );
  94. libvlc_release( instance, &ex );
  95. }
  96. instance = nil;
  97. [audio release];
  98. [super dealloc];
  99. }
  100. - (void *)instance
  101. {
  102. return instance;
  103. }
  104. - (VLCAudio *)audio
  105. {
  106. return audio;
  107. }
  108. @end
  109. @implementation VLCLibrary (VLCAudioBridging)
  110. - (void)setAudio:(VLCAudio *)value
  111. {
  112. if (!audio)
  113. audio = value;
  114. }
  115. @end