VLCMediaListPlayer.m 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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-2013 VLC authors and VideoLAN
  7. * $Id$
  8. *
  9. * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
  10. * Felix Paul Kühne <fkuehne # videolan.org
  11. *
  12. * This program is free software; you can redistribute it and/or modify it
  13. * under the terms of the GNU Lesser General Public License as published by
  14. * the Free Software Foundation; either version 2.1 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Lesser General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Lesser General Public License
  23. * along with this program; if not, write to the Free Software Foundation,
  24. * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  25. *****************************************************************************/
  26. #import "VLCMediaListPlayer.h"
  27. #import "VLCMedia.h"
  28. #import "VLCMediaPlayer.h"
  29. #import "VLCMediaList.h"
  30. #import "VLCLibVLCBridging.h"
  31. #import "VLCLibrary.h"
  32. @interface VLCMediaListPlayer () {
  33. void *instance;
  34. VLCMedia *_rootMedia;
  35. VLCMediaPlayer *_mediaPlayer;
  36. VLCMediaList *_mediaList;
  37. VLCRepeatMode _repeatMode;
  38. dispatch_queue_t _libVLCBackgroundQueue;
  39. }
  40. @end
  41. @implementation VLCMediaListPlayer
  42. - (instancetype)initWithOptions:(NSArray *)options andDrawable:(id)drawable
  43. {
  44. if (self = [super init]) {
  45. _libVLCBackgroundQueue = dispatch_queue_create("libvlcQueue", DISPATCH_QUEUE_SERIAL);
  46. VLCLibrary *library;
  47. if (options != nil) {
  48. library = [[VLCLibrary alloc] initWithOptions:options];
  49. } else
  50. library = [VLCLibrary sharedLibrary];
  51. instance = libvlc_media_list_player_new([library instance]);
  52. _mediaPlayer = [[VLCMediaPlayer alloc] initWithLibVLCInstance:libvlc_media_list_player_get_media_player(instance) andLibrary:library];
  53. if (drawable != nil)
  54. [_mediaPlayer setDrawable:drawable];
  55. }
  56. return self;
  57. }
  58. - (instancetype)initWithOptions:(NSArray *)options
  59. {
  60. return [self initWithOptions:options andDrawable:nil];
  61. }
  62. - (instancetype)init
  63. {
  64. return [self initWithOptions:nil andDrawable:nil];
  65. }
  66. - (instancetype)initWithDrawable:(id)drawable
  67. {
  68. return [self initWithOptions:nil andDrawable:drawable];
  69. }
  70. - (void)dealloc
  71. {
  72. [_mediaPlayer stop];
  73. libvlc_media_list_player_release(instance);
  74. }
  75. - (VLCMediaPlayer *)mediaPlayer
  76. {
  77. return _mediaPlayer;
  78. }
  79. - (void)setMediaList:(VLCMediaList *)mediaList
  80. {
  81. if (_mediaList == mediaList)
  82. return;
  83. _mediaList = mediaList;
  84. libvlc_media_list_player_set_media_list(instance, [mediaList libVLCMediaList]);
  85. [self willChangeValueForKey:@"rootMedia"];
  86. _rootMedia = nil;
  87. [self didChangeValueForKey:@"rootMedia"];
  88. }
  89. - (VLCMediaList *)mediaList
  90. {
  91. return _mediaList;
  92. }
  93. - (void)setRootMedia:(VLCMedia *)media
  94. {
  95. if (_rootMedia == media)
  96. return;
  97. _rootMedia = nil;
  98. VLCMediaList *mediaList = [[VLCMediaList alloc] init];
  99. if (media)
  100. [mediaList addMedia:media];
  101. // This will clean rootMedia
  102. [self setMediaList:mediaList];
  103. // Thus set rootMedia here.
  104. _rootMedia = media;
  105. }
  106. - (VLCMedia *)rootMedia
  107. {
  108. return _rootMedia;
  109. }
  110. - (void)playMedia:(VLCMedia *)media
  111. {
  112. dispatch_async(_libVLCBackgroundQueue, ^{
  113. libvlc_media_list_player_play_item(instance, [media libVLCMediaDescriptor]);
  114. });
  115. }
  116. - (void)play
  117. {
  118. dispatch_async(_libVLCBackgroundQueue, ^{
  119. libvlc_media_list_player_play(instance);
  120. });
  121. }
  122. - (void)pause
  123. {
  124. dispatch_async(_libVLCBackgroundQueue, ^{
  125. libvlc_media_list_player_set_pause(instance, 1);
  126. });
  127. }
  128. - (void)stop
  129. {
  130. dispatch_async(_libVLCBackgroundQueue, ^{
  131. libvlc_media_list_player_stop(instance);
  132. });
  133. }
  134. - (BOOL)next
  135. {
  136. return libvlc_media_list_player_next(instance) == 0 ? YES : NO;
  137. }
  138. - (BOOL)previous
  139. {
  140. return libvlc_media_list_player_previous(instance) == 0 ? YES : NO;
  141. }
  142. - (BOOL)playItemAtIndex:(int)index
  143. {
  144. return libvlc_media_list_player_play_item_at_index(instance, index) == 0 ? YES : NO;
  145. }
  146. - (void)playItemAtNumber:(NSNumber *)index
  147. {
  148. dispatch_async(_libVLCBackgroundQueue, ^{
  149. libvlc_media_list_player_play_item_at_index(instance, [index intValue]);
  150. });
  151. }
  152. - (void)setRepeatMode:(VLCRepeatMode)repeatMode
  153. {
  154. libvlc_playback_mode_t mode;
  155. switch (repeatMode) {
  156. case VLCRepeatAllItems:
  157. mode = libvlc_playback_mode_loop;
  158. break;
  159. case VLCDoNotRepeat:
  160. mode = libvlc_playback_mode_default;
  161. break;
  162. case VLCRepeatCurrentItem:
  163. mode = libvlc_playback_mode_repeat;
  164. break;
  165. default:
  166. NSAssert(0, @"Should not be reached");
  167. break;
  168. }
  169. libvlc_media_list_player_set_playback_mode(instance, mode);
  170. _repeatMode = repeatMode;
  171. }
  172. - (VLCRepeatMode)repeatMode
  173. {
  174. return _repeatMode;
  175. }
  176. @end