VLCExtensionsManager.m 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*****************************************************************************
  2. * VLCKit: VLCExtensionsManager
  3. *****************************************************************************
  4. * Copyright (C) 2010-2012 Pierre d'Herbemont and VideoLAN
  5. *
  6. * Authors: Pierre d'Herbemont
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU Lesser General Public License as published by
  10. * the Free Software Foundation; either version 2.1 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public License
  19. * along with this program; if not, write to the Free Software Foundation,
  20. * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  21. *****************************************************************************/
  22. #import "VLCExtensionsManager.h"
  23. #import "VLCExtension.h"
  24. #import "VLCLibrary.h"
  25. #import "VLCLibVLCBridging.h"
  26. #import <vlc_extensions.h>
  27. #import <vlc_input.h>
  28. #include <vlc_modules.h>
  29. // Here comes the nasty hack.
  30. #define MODULE_STRING "VLCKit"
  31. #import "../vlc-unstable/lib/media_player_internal.h"
  32. #import "../vlc-unstable/lib/libvlc_internal.h"
  33. static input_thread_t *libvlc_media_player_get_input_thread(libvlc_media_player_t *player)
  34. {
  35. vlc_mutex_lock(&player->input.lock);
  36. input_thread_t *input = player->input.p_thread;
  37. if(input)
  38. vlc_object_hold(input);
  39. vlc_mutex_unlock(&player->input.lock);
  40. return input;
  41. }
  42. static vlc_object_t *libvlc_get_vlc_instance(libvlc_instance_t *instance)
  43. {
  44. vlc_mutex_lock(&instance->instance_lock);
  45. libvlc_int_t *libvlc = instance->p_libvlc_int;
  46. if(libvlc)
  47. vlc_object_hold(libvlc);
  48. vlc_mutex_unlock(&instance->instance_lock);
  49. return VLC_OBJECT(libvlc);
  50. }
  51. #define _instance ((extensions_manager_t *)instance)
  52. @implementation VLCExtensionsManager
  53. static VLCExtensionsManager *sharedManager = nil;
  54. + (VLCExtensionsManager *)sharedManager
  55. {
  56. if (!sharedManager)
  57. {
  58. /* Initialize a shared instance */
  59. sharedManager = [[self alloc] init];
  60. }
  61. return sharedManager;
  62. }
  63. - (void)dealloc
  64. {
  65. module_unneed(_instance, _instance->p_module);
  66. vlc_object_release(_instance);
  67. [[NSNotificationCenter defaultCenter] removeObserver:self];
  68. [_extensions release];
  69. [super dealloc];
  70. }
  71. - (NSArray *)extensions
  72. {
  73. if (!instance)
  74. {
  75. vlc_object_t *libvlc = libvlc_get_vlc_instance([VLCLibrary sharedInstance]);
  76. instance = vlc_object_create(libvlc, sizeof(extensions_manager_t));
  77. if (!_instance)
  78. {
  79. vlc_object_release(libvlc);
  80. return nil;
  81. }
  82. _instance->p_module = module_need(_instance, "extension", NULL, false);
  83. NSAssert(_instance->p_module, @"Unable to load extensions module");
  84. vlc_object_release(libvlc);
  85. }
  86. if (_extensions)
  87. return _extensions;
  88. _extensions = [[NSMutableArray alloc] init];
  89. extension_t *ext;
  90. vlc_mutex_lock(&_instance->lock);
  91. FOREACH_ARRAY(ext, _instance->extensions)
  92. VLCExtension *extension = [[VLCExtension alloc] initWithInstance:ext];
  93. [_extensions addObject:extension];
  94. [extension release];
  95. FOREACH_END()
  96. vlc_mutex_unlock(&_instance->lock);
  97. return _extensions;
  98. }
  99. - (void)runExtension:(VLCExtension *)extension
  100. {
  101. extension_t *ext = [extension instance];
  102. if(extension_TriggerOnly(_instance, ext))
  103. extension_Trigger(_instance, ext);
  104. else
  105. {
  106. if(!extension_IsActivated(_instance, ext))
  107. extension_Activate(_instance, ext);
  108. }
  109. }
  110. - (void)mediaPlayerLikelyChangedInput
  111. {
  112. input_thread_t *input = _player ? libvlc_media_player_get_input_thread([_player libVLCMediaPlayer]) : NULL;
  113. // Don't send more than appropriate
  114. if (_previousInput == input)
  115. return;
  116. _previousInput = input;
  117. for(VLCExtension *extension in _extensions)
  118. extension_SetInput(_instance, [extension instance], input);
  119. if (input)
  120. vlc_object_release(input);
  121. }
  122. - (void)setMediaPlayer:(VLCMediaPlayer *)player
  123. {
  124. if (_player == player)
  125. return;
  126. // Don't set a NULL mediaPlayer.
  127. // so that we always have an input around.
  128. if (!player)
  129. return;
  130. NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
  131. [center removeObserver:self name:VLCMediaPlayerStateChanged object:_player];
  132. [_player release];
  133. _player = [player retain];
  134. [self mediaPlayerLikelyChangedInput];
  135. if (player)
  136. [center addObserver:self selector:@selector(mediaPlayerLikelyChangedInput) name:VLCMediaPlayerStateChanged object:_player];
  137. }
  138. - (VLCMediaPlayer *)mediaPlayer
  139. {
  140. return _player;
  141. }
  142. @end