VLCMediaListPlayer.m 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*****************************************************************************
  2. * VLCMediaListPlayer.m: VLCKit.framework VLCMediaListPlayer implementation
  3. *****************************************************************************
  4. * Copyright (C) 2009 Pierre d'Herbemont
  5. * Partial Copyright (C) 2009-2017 Felix Paul Kühne
  6. * Copyright (C) 2009-2019 VLC authors and VideoLAN
  7. * $Id$
  8. *
  9. * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
  10. * Felix Paul Kühne <fkuehne # videolan.org>
  11. * Soomin Lee <bubu # mikan.io>
  12. *
  13. * This program is free software; you can redistribute it and/or modify it
  14. * under the terms of the GNU Lesser General Public License as published by
  15. * the Free Software Foundation; either version 2.1 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Lesser General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Lesser General Public License
  24. * along with this program; if not, write to the Free Software Foundation,
  25. * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  26. *****************************************************************************/
  27. #import "VLCMediaListPlayer.h"
  28. #import "VLCMedia.h"
  29. #import "VLCMediaPlayer.h"
  30. #import "VLCMediaList.h"
  31. #import "VLCLibVLCBridging.h"
  32. #import "VLCLibrary.h"
  33. #import "VLCEventManager.h"
  34. @interface VLCMediaListPlayer () {
  35. void *instance;
  36. VLCMedia *_rootMedia;
  37. VLCMediaPlayer *_mediaPlayer;
  38. VLCMediaList *_mediaList;
  39. VLCRepeatMode _repeatMode;
  40. dispatch_queue_t _libVLCBackgroundQueue;
  41. }
  42. - (void)mediaListPlayerPlayed;
  43. - (void)mediaListPlayerNextItemSet:(VLCMedia *)media;
  44. - (void)mediaListPlayerStopped;
  45. @end
  46. static void HandleMediaListPlayerPlayed(const libvlc_event_t * event, void * self)
  47. {
  48. @autoreleasepool {
  49. [[VLCEventManager sharedManager] callOnMainThreadObject:(__bridge id)(self)
  50. withMethod:@selector(mediaListPlayerPlayed)
  51. withArgumentAsObject:nil];
  52. }
  53. }
  54. static void HandleMediaListPlayerNextItemSet(const libvlc_event_t * event, void * self)
  55. {
  56. @autoreleasepool {
  57. VLCMedia *media = [[VLCMedia alloc]
  58. initWithLibVLCMediaDescriptor:event->u.media_list_player_next_item_set.item];
  59. [[VLCEventManager sharedManager] callOnMainThreadObject:(__bridge id)(self)
  60. withMethod:@selector(mediaListPlayerNextItemSet:)
  61. withArgumentAsObject:media];
  62. }
  63. }
  64. static void HandleMediaListPlayerStopped(const libvlc_event_t * event, void * self)
  65. {
  66. @autoreleasepool {
  67. [[VLCEventManager sharedManager] callOnMainThreadObject:(__bridge id)(self)
  68. withMethod:@selector(mediaListPlayerStopped)
  69. withArgumentAsObject:nil];
  70. }
  71. }
  72. @implementation VLCMediaListPlayer
  73. - (instancetype)initWithOptions:(NSArray *)options andDrawable:(id)drawable
  74. {
  75. if (self = [super init]) {
  76. _libVLCBackgroundQueue = dispatch_queue_create("libvlcQueue", DISPATCH_QUEUE_SERIAL);
  77. VLCLibrary *library;
  78. if (options != nil) {
  79. library = [[VLCLibrary alloc] initWithOptions:options];
  80. } else
  81. library = [VLCLibrary sharedLibrary];
  82. instance = libvlc_media_list_player_new([library instance]);
  83. _mediaPlayer = [[VLCMediaPlayer alloc] initWithLibVLCInstance:libvlc_media_list_player_get_media_player(instance) andLibrary:library];
  84. if (drawable != nil)
  85. [_mediaPlayer setDrawable:drawable];
  86. [self registerObservers];
  87. }
  88. return self;
  89. }
  90. - (void)registerObservers
  91. {
  92. __block libvlc_event_manager_t * p_em = libvlc_media_list_player_event_manager(instance);
  93. if (!p_em) {
  94. return;
  95. }
  96. dispatch_sync(_libVLCBackgroundQueue,^{
  97. libvlc_event_attach(p_em, libvlc_MediaListPlayerPlayed,
  98. HandleMediaListPlayerPlayed, (__bridge void *)(self));
  99. libvlc_event_attach(p_em, libvlc_MediaListPlayerNextItemSet,
  100. HandleMediaListPlayerNextItemSet, (__bridge void *)(self));
  101. libvlc_event_attach(p_em, libvlc_MediaListPlayerStopped,
  102. HandleMediaListPlayerStopped, (__bridge void *)(self));
  103. });
  104. }
  105. - (void)unregisterObservers
  106. {
  107. libvlc_event_manager_t * p_em = libvlc_media_list_player_event_manager(instance);
  108. if (!p_em) {
  109. return;
  110. }
  111. libvlc_event_detach(p_em, libvlc_MediaListPlayerPlayed,
  112. HandleMediaListPlayerPlayed, (__bridge void *)(self));
  113. libvlc_event_detach(p_em, libvlc_MediaListPlayerNextItemSet,
  114. HandleMediaListPlayerNextItemSet, (__bridge void *)(self));
  115. libvlc_event_detach(p_em, libvlc_MediaListPlayerStopped,
  116. HandleMediaListPlayerStopped, (__bridge void *)(self));
  117. }
  118. - (instancetype)initWithOptions:(NSArray *)options
  119. {
  120. return [self initWithOptions:options andDrawable:nil];
  121. }
  122. - (instancetype)init
  123. {
  124. return [self initWithOptions:nil andDrawable:nil];
  125. }
  126. - (instancetype)initWithDrawable:(id)drawable
  127. {
  128. return [self initWithOptions:nil andDrawable:drawable];
  129. }
  130. - (void)dealloc
  131. {
  132. [_mediaPlayer stop];
  133. [self unregisterObservers];
  134. libvlc_media_list_player_release(instance);
  135. }
  136. - (VLCMediaPlayer *)mediaPlayer
  137. {
  138. return _mediaPlayer;
  139. }
  140. - (void)setMediaList:(VLCMediaList *)mediaList
  141. {
  142. if (_mediaList == mediaList)
  143. return;
  144. _mediaList = mediaList;
  145. libvlc_media_list_player_set_media_list(instance, [mediaList libVLCMediaList]);
  146. [self willChangeValueForKey:@"rootMedia"];
  147. _rootMedia = nil;
  148. [self didChangeValueForKey:@"rootMedia"];
  149. }
  150. - (VLCMediaList *)mediaList
  151. {
  152. return _mediaList;
  153. }
  154. - (void)setRootMedia:(VLCMedia *)media
  155. {
  156. if (_rootMedia == media)
  157. return;
  158. _rootMedia = nil;
  159. VLCMediaList *mediaList = [[VLCMediaList alloc] init];
  160. if (media)
  161. [mediaList addMedia:media];
  162. // This will clean rootMedia
  163. [self setMediaList:mediaList];
  164. // Thus set rootMedia here.
  165. _rootMedia = media;
  166. }
  167. - (VLCMedia *)rootMedia
  168. {
  169. return _rootMedia;
  170. }
  171. - (void)playMedia:(VLCMedia *)media
  172. {
  173. dispatch_async(_libVLCBackgroundQueue, ^{
  174. libvlc_media_list_player_play_item(instance, [media libVLCMediaDescriptor]);
  175. });
  176. }
  177. - (void)play
  178. {
  179. dispatch_async(_libVLCBackgroundQueue, ^{
  180. libvlc_media_list_player_play(instance);
  181. });
  182. }
  183. - (void)pause
  184. {
  185. dispatch_async(_libVLCBackgroundQueue, ^{
  186. libvlc_media_list_player_set_pause(instance, 1);
  187. });
  188. }
  189. - (void)stop
  190. {
  191. libvlc_media_list_player_stop_async(instance);
  192. }
  193. - (BOOL)next
  194. {
  195. return libvlc_media_list_player_next(instance) == 0 ? YES : NO;
  196. }
  197. - (BOOL)previous
  198. {
  199. return libvlc_media_list_player_previous(instance) == 0 ? YES : NO;
  200. }
  201. - (void)playItemAtNumber:(NSNumber *)index
  202. {
  203. dispatch_async(_libVLCBackgroundQueue, ^{
  204. libvlc_media_list_player_play_item_at_index(instance, [index intValue]);
  205. });
  206. }
  207. - (void)setRepeatMode:(VLCRepeatMode)repeatMode
  208. {
  209. libvlc_playback_mode_t mode;
  210. switch (repeatMode) {
  211. case VLCRepeatAllItems:
  212. mode = libvlc_playback_mode_loop;
  213. break;
  214. case VLCDoNotRepeat:
  215. mode = libvlc_playback_mode_default;
  216. break;
  217. case VLCRepeatCurrentItem:
  218. mode = libvlc_playback_mode_repeat;
  219. break;
  220. default:
  221. NSAssert(0, @"Should not be reached");
  222. break;
  223. }
  224. libvlc_media_list_player_set_playback_mode(instance, mode);
  225. _repeatMode = repeatMode;
  226. }
  227. - (VLCRepeatMode)repeatMode
  228. {
  229. return _repeatMode;
  230. }
  231. #pragma mark - Delegate methods
  232. - (void)mediaListPlayerPlayed
  233. {
  234. if ([_delegate respondsToSelector:@selector(mediaListPlayerFinishedPlayback:)]) {
  235. [_delegate mediaListPlayerFinishedPlayback:self];
  236. }
  237. }
  238. - (void)mediaListPlayerNextItemSet:(VLCMedia *)media
  239. {
  240. if ([_delegate respondsToSelector:@selector(mediaListPlayer:nextMedia:)]) {
  241. [_delegate mediaListPlayer:self nextMedia:media];
  242. }
  243. }
  244. - (void)mediaListPlayerStopped
  245. {
  246. if ([_delegate respondsToSelector:@selector(mediaListPlayerStopped:)]) {
  247. [_delegate mediaListPlayerStopped:self];
  248. }
  249. }
  250. @end