VLCExtensionsManager.m 4.7 KB

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