VLCExtensionsManager.m 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*****************************************************************************
  2. * VLCKit: VLCExtensionsManager
  3. *****************************************************************************
  4. * Copyright (C) 2010-2012, 2014 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 "../libvlc/vlc/lib/media_player_internal.h"
  32. #import "../libvlc/vlc//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. @interface VLCExtensionsManager ()
  53. {
  54. void *instance;
  55. NSMutableArray *_extensions;
  56. VLCMediaPlayer *_player;
  57. void *_previousInput;
  58. }
  59. @end
  60. @implementation VLCExtensionsManager
  61. + (VLCExtensionsManager *)sharedManager
  62. {
  63. static VLCExtensionsManager *sharedManager;
  64. static dispatch_once_t onceToken;
  65. dispatch_once(&onceToken, ^{
  66. sharedManager = [[self alloc] init];
  67. });
  68. return sharedManager;
  69. }
  70. - (void)dealloc
  71. {
  72. module_unneed(_instance, _instance->p_module);
  73. vlc_object_release(_instance);
  74. [[NSNotificationCenter defaultCenter] removeObserver:self];
  75. }
  76. - (NSArray *)extensions
  77. {
  78. if (!instance)
  79. {
  80. vlc_object_t *libvlc = libvlc_get_vlc_instance([VLCLibrary sharedInstance]);
  81. instance = vlc_object_create(libvlc, sizeof(extensions_manager_t));
  82. if (!_instance)
  83. {
  84. vlc_object_release(libvlc);
  85. return nil;
  86. }
  87. _instance->p_module = module_need(_instance, "extension", NULL, false);
  88. NSAssert(_instance->p_module, @"Unable to load extensions module");
  89. vlc_object_release(libvlc);
  90. }
  91. if (_extensions)
  92. return _extensions;
  93. _extensions = [[NSMutableArray alloc] init];
  94. extension_t *ext;
  95. vlc_mutex_lock(&_instance->lock);
  96. FOREACH_ARRAY(ext, _instance->extensions)
  97. [_extensions addObject:[[VLCExtension alloc] initWithInstance:ext]];
  98. FOREACH_END()
  99. vlc_mutex_unlock(&_instance->lock);
  100. return _extensions;
  101. }
  102. - (void)runExtension:(VLCExtension *)extension
  103. {
  104. extension_t *ext = [extension instance];
  105. if(extension_TriggerOnly(_instance, ext))
  106. extension_Trigger(_instance, ext);
  107. else
  108. {
  109. if(!extension_IsActivated(_instance, ext))
  110. extension_Activate(_instance, ext);
  111. }
  112. }
  113. - (void)mediaPlayerLikelyChangedInput
  114. {
  115. input_thread_t *input = _player ? libvlc_media_player_get_input_thread([_player libVLCMediaPlayer]) : NULL;
  116. // Don't send more than appropriate
  117. if (_previousInput == input)
  118. return;
  119. _previousInput = input;
  120. for(VLCExtension *extension in _extensions)
  121. extension_SetInput(_instance, [extension instance], input);
  122. if (input)
  123. vlc_object_release(input);
  124. }
  125. - (void)setMediaPlayer:(VLCMediaPlayer *)player
  126. {
  127. if (_player == player)
  128. return;
  129. // Don't set a NULL mediaPlayer.
  130. // so that we always have an input around.
  131. if (!player)
  132. return;
  133. NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
  134. [center removeObserver:self name:VLCMediaPlayerStateChanged object:_player];
  135. _player = player;
  136. [self mediaPlayerLikelyChangedInput];
  137. if (player)
  138. [center addObserver:self selector:@selector(mediaPlayerLikelyChangedInput) name:VLCMediaPlayerStateChanged object:_player];
  139. }
  140. - (VLCMediaPlayer *)mediaPlayer
  141. {
  142. return _player;
  143. }
  144. @end