VLCMediaPlayer.m 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362
  1. /*****************************************************************************
  2. * VLCMediaPlayer.m: VLCKit.framework VLCMediaPlayer implementation
  3. *****************************************************************************
  4. * Copyright (C) 2007-2009 Pierre d'Herbemont
  5. * Copyright (C) 2007-2015 VLC authors and VideoLAN
  6. * Partial Copyright (C) 2009-2015 Felix Paul Kühne
  7. * $Id$
  8. *
  9. * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
  10. * Faustion Osuna <enrique.osuna # gmail.com>
  11. * Felix Paul Kühne <fkuehne # videolan.org>
  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 "VLCLibrary.h"
  28. #import "VLCMediaPlayer.h"
  29. #import "VLCEventManager.h"
  30. #import "VLCLibVLCBridging.h"
  31. #if !TARGET_OS_IPHONE
  32. # import "VLCVideoView.h"
  33. #endif
  34. #ifdef HAVE_CONFIG_H
  35. # include "config.h"
  36. #endif
  37. #if !TARGET_OS_IPHONE
  38. /* prevent system sleep */
  39. # import <CoreServices/CoreServices.h>
  40. /* FIXME: Ugly hack! */
  41. # ifdef __x86_64__
  42. # import <CoreServices/../Frameworks/OSServices.framework/Headers/Power.h>
  43. # endif
  44. #endif
  45. #include <vlc/vlc.h>
  46. /* Notification Messages */
  47. NSString *const VLCMediaPlayerTimeChanged = @"VLCMediaPlayerTimeChanged";
  48. NSString *const VLCMediaPlayerStateChanged = @"VLCMediaPlayerStateChanged";
  49. NSString *const VLCMediaPlayerSnapshotTaken = @"VLCMediaPlayerSnapshotTaken";
  50. /* title keys */
  51. NSString *const VLCTitleDescriptionName = @"VLCTitleDescriptionName";
  52. NSString *const VLCTitleDescriptionDuration = @"VLCTitleDescriptionDuration";
  53. NSString *const VLCTitleDescriptionIsMenu = @"VLCTitleDescriptionIsMenu";
  54. /* chapter keys */
  55. NSString *const VLCChapterDescriptionName = @"VLCChapterDescriptionName";
  56. NSString *const VLCChapterDescriptionTimeOffset = @"VLCChapterDescriptionTimeOffset";
  57. NSString *const VLCChapterDescriptionDuration = @"VLCChapterDescriptionDuration";
  58. NSString * VLCMediaPlayerStateToString(VLCMediaPlayerState state)
  59. {
  60. static NSString * stateToStrings[] = {
  61. [VLCMediaPlayerStateStopped] = @"VLCMediaPlayerStateStopped",
  62. [VLCMediaPlayerStateOpening] = @"VLCMediaPlayerStateOpening",
  63. [VLCMediaPlayerStateBuffering] = @"VLCMediaPlayerStateBuffering",
  64. [VLCMediaPlayerStateEnded] = @"VLCMediaPlayerStateEnded",
  65. [VLCMediaPlayerStateError] = @"VLCMediaPlayerStateError",
  66. [VLCMediaPlayerStatePlaying] = @"VLCMediaPlayerStatePlaying",
  67. [VLCMediaPlayerStatePaused] = @"VLCMediaPlayerStatePaused"
  68. };
  69. return stateToStrings[state];
  70. }
  71. static void HandleMediaTimeChanged(const libvlc_event_t * event, void * self)
  72. {
  73. @autoreleasepool {
  74. [[VLCEventManager sharedManager] callOnMainThreadObject:(__bridge id)(self)
  75. withMethod:@selector(mediaPlayerTimeChanged:)
  76. withArgumentAsObject:@(event->u.media_player_time_changed.new_time)];
  77. [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:(__bridge id)(self)
  78. withDelegateMethod:@selector(mediaPlayerTimeChanged:)
  79. withNotificationName:VLCMediaPlayerTimeChanged];
  80. }
  81. }
  82. static void HandleMediaPositionChanged(const libvlc_event_t * event, void * self)
  83. {
  84. @autoreleasepool {
  85. [[VLCEventManager sharedManager] callOnMainThreadObject:(__bridge id)(self)
  86. withMethod:@selector(mediaPlayerPositionChanged:)
  87. withArgumentAsObject:@(event->u.media_player_position_changed.new_position)];
  88. }
  89. }
  90. static void HandleMediaInstanceStateChanged(const libvlc_event_t * event, void * self)
  91. {
  92. VLCMediaPlayerState newState;
  93. if (event->type == libvlc_MediaPlayerPlaying)
  94. newState = VLCMediaPlayerStatePlaying;
  95. else if (event->type == libvlc_MediaPlayerPaused)
  96. newState = VLCMediaPlayerStatePaused;
  97. else if (event->type == libvlc_MediaPlayerEndReached || event->type == libvlc_MediaPlayerStopped)
  98. newState = VLCMediaPlayerStateStopped;
  99. else if (event->type == libvlc_MediaPlayerEncounteredError)
  100. newState = VLCMediaPlayerStateError;
  101. else if (event->type == libvlc_MediaPlayerBuffering)
  102. newState = VLCMediaPlayerStateBuffering;
  103. else if (event->type == libvlc_MediaPlayerOpening)
  104. newState = VLCMediaPlayerStateOpening;
  105. else {
  106. VKLog(@"%s: Unknown event", __FUNCTION__);
  107. return;
  108. }
  109. @autoreleasepool {
  110. [[VLCEventManager sharedManager] callOnMainThreadObject:(__bridge id)(self)
  111. withMethod:@selector(mediaPlayerStateChanged:)
  112. withArgumentAsObject:@(newState)];
  113. [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:(__bridge id)(self)
  114. withDelegateMethod:@selector(mediaPlayerStateChanged:)
  115. withNotificationName:VLCMediaPlayerStateChanged];
  116. }
  117. }
  118. static void HandleMediaPlayerMediaChanged(const libvlc_event_t * event, void * self)
  119. {
  120. @autoreleasepool {
  121. [[VLCEventManager sharedManager] callOnMainThreadObject:(__bridge id)(self)
  122. withMethod:@selector(mediaPlayerMediaChanged:)
  123. withArgumentAsObject:[VLCMedia mediaWithLibVLCMediaDescriptor:event->u.media_player_media_changed.new_media]];
  124. }
  125. }
  126. #if TARGET_OS_IPHONE
  127. static void HandleMediaPlayerSnapshot(const libvlc_event_t * event, void * self)
  128. {
  129. @autoreleasepool {
  130. if (event->u.media_player_snapshot_taken.psz_filename != NULL) {
  131. [[VLCEventManager sharedManager] callOnMainThreadObject:(__bridge id)(self)
  132. withMethod:@selector(mediaPlayerSnapshot:)
  133. withArgumentAsObject:[NSString stringWithUTF8String:event->u.media_player_snapshot_taken.psz_filename]];
  134. [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:(__bridge id)(self)
  135. withDelegateMethod:@selector(mediaPlayerSnapshot:)
  136. withNotificationName:VLCMediaPlayerSnapshotTaken];
  137. }
  138. }
  139. }
  140. #endif
  141. // TODO: Documentation
  142. @interface VLCMediaPlayer (Private)
  143. - (instancetype)initWithDrawable:(id)aDrawable options:(NSArray *)options;
  144. - (void)registerObservers;
  145. - (void)unregisterObservers;
  146. - (void)mediaPlayerTimeChanged:(NSNumber *)newTime;
  147. - (void)mediaPlayerPositionChanged:(NSNumber *)newTime;
  148. - (void)mediaPlayerStateChanged:(NSNumber *)newState;
  149. - (void)mediaPlayerMediaChanged:(VLCMedia *)media;
  150. #if TARGET_OS_IPHONE
  151. - (void)mediaPlayerSnapshot:(NSString *)fileName;
  152. #endif
  153. @end
  154. @interface VLCMediaPlayer ()
  155. {
  156. VLCLibrary *_privateLibrary;
  157. void * _playerInstance; // Internal
  158. VLCMedia * _media; //< Current media being played
  159. VLCTime * _cachedTime; //< Cached time of the media being played
  160. VLCTime * _cachedRemainingTime; //< Cached remaining time of the media being played
  161. VLCMediaPlayerState _cachedState; //< Cached state of the media being played
  162. float _position; //< The position of the media being played
  163. id _drawable; //< The drawable associated to this media player
  164. #if TARGET_OS_IPHONE
  165. NSMutableArray *_snapshots; //< Array with snapshot file names
  166. #endif
  167. VLCAudio *_audio;
  168. libvlc_equalizer_t *_equalizerInstance;
  169. BOOL _equalizerEnabled;
  170. }
  171. @end
  172. @implementation VLCMediaPlayer
  173. @synthesize libraryInstance = _privateLibrary;
  174. /* Bindings */
  175. + (NSSet *)keyPathsForValuesAffectingValueForKey:(NSString *)key
  176. {
  177. static NSDictionary * dict = nil;
  178. NSSet * superKeyPaths;
  179. if (!dict) {
  180. dict = @{@"playing": [NSSet setWithObject:@"state"],
  181. @"seekable": [NSSet setWithObjects:@"state", @"media", nil],
  182. @"canPause": [NSSet setWithObjects:@"state", @"media", nil],
  183. @"description": [NSSet setWithObjects:@"state", @"media", nil]};
  184. }
  185. if ((superKeyPaths = [super keyPathsForValuesAffectingValueForKey: key])) {
  186. NSMutableSet * ret = [NSMutableSet setWithSet:dict[key]];
  187. [ret unionSet:superKeyPaths];
  188. return ret;
  189. }
  190. return dict[key];
  191. }
  192. /* Constructor */
  193. - (instancetype)init
  194. {
  195. return [self initWithDrawable:nil options:nil];
  196. }
  197. - (instancetype)initWithLibVLCInstance:(void *)playerInstance andLibrary:(VLCLibrary *)library
  198. {
  199. if (self = [super init]) {
  200. _cachedTime = [VLCTime nullTime];
  201. _cachedRemainingTime = [VLCTime nullTime];
  202. _position = 0.0f;
  203. _cachedState = VLCMediaPlayerStateStopped;
  204. _privateLibrary = library;
  205. libvlc_retain([_privateLibrary instance]);
  206. _playerInstance = playerInstance;
  207. libvlc_media_player_retain(_playerInstance);
  208. [self registerObservers];
  209. }
  210. return self;
  211. }
  212. #if !TARGET_OS_IPHONE
  213. - (instancetype)initWithVideoView:(VLCVideoView *)aVideoView
  214. {
  215. return [self initWithDrawable: aVideoView options:nil];
  216. }
  217. - (instancetype)initWithVideoLayer:(VLCVideoLayer *)aVideoLayer
  218. {
  219. return [self initWithDrawable: aVideoLayer options:nil];
  220. }
  221. - (instancetype)initWithVideoView:(VLCVideoView *)aVideoView options:(NSArray *)options
  222. {
  223. return [self initWithDrawable: aVideoView options:options];
  224. }
  225. - (instancetype)initWithVideoLayer:(VLCVideoLayer *)aVideoLayer options:(NSArray *)options
  226. {
  227. return [self initWithDrawable: aVideoLayer options:options];
  228. }
  229. #endif
  230. - (instancetype)initWithOptions:(NSArray *)options
  231. {
  232. return [self initWithDrawable:nil options:options];
  233. }
  234. - (void)dealloc
  235. {
  236. NSAssert(libvlc_media_player_get_state(_playerInstance) == libvlc_Stopped || libvlc_media_player_get_state(_playerInstance) == libvlc_NothingSpecial, @"You released the media player before ensuring that it is stopped");
  237. [self unregisterObservers];
  238. [[VLCEventManager sharedManager] cancelCallToObject:self];
  239. // Always get rid of the delegate first so we can stop sending messages to it
  240. // TODO: Should we tell the delegate that we're shutting down?
  241. _delegate = nil;
  242. // Clear our drawable as we are going to release it, we don't
  243. // want the core to use it from this point. This won't happen as
  244. // the media player must be stopped.
  245. libvlc_media_player_set_nsobject(_playerInstance, nil);
  246. if (_equalizerInstance) {
  247. libvlc_media_player_set_equalizer(_playerInstance, NULL);
  248. libvlc_audio_equalizer_release(_equalizerInstance);
  249. }
  250. libvlc_media_player_release(_playerInstance);
  251. if (_privateLibrary != [VLCLibrary sharedLibrary])
  252. libvlc_release(_privateLibrary.instance);
  253. }
  254. #if !TARGET_OS_IPHONE
  255. - (void)setVideoView:(VLCVideoView *)aVideoView
  256. {
  257. [self setDrawable: aVideoView];
  258. }
  259. - (void)setVideoLayer:(VLCVideoLayer *)aVideoLayer
  260. {
  261. [self setDrawable: aVideoLayer];
  262. }
  263. #endif
  264. - (void)setDrawable:(id)aDrawable
  265. {
  266. // Make sure that this instance has been associated with the drawing canvas.
  267. libvlc_media_player_set_nsobject(_playerInstance, (__bridge void *)(aDrawable));
  268. }
  269. - (id)drawable
  270. {
  271. return (__bridge id)(libvlc_media_player_get_nsobject(_playerInstance));
  272. }
  273. - (VLCAudio *)audio
  274. {
  275. if (!_audio)
  276. _audio = [[VLCAudio alloc] initWithMediaPlayer:self];
  277. return _audio;
  278. }
  279. #pragma mark -
  280. #pragma mark Video Tracks
  281. - (void)setCurrentVideoTrackIndex:(int)value
  282. {
  283. libvlc_video_set_track(_playerInstance, value);
  284. }
  285. - (int)currentVideoTrackIndex
  286. {
  287. int count = libvlc_video_get_track_count(_playerInstance);
  288. if (count <= 0)
  289. return INT_MAX;
  290. return libvlc_video_get_track(_playerInstance);
  291. }
  292. - (NSArray *)videoTrackNames
  293. {
  294. NSInteger count = libvlc_video_get_track_count(_playerInstance);
  295. if (count <= 0)
  296. return @[];
  297. libvlc_track_description_t *firstTrack = libvlc_video_get_track_description(_playerInstance);
  298. libvlc_track_description_t *currentTrack = firstTrack;
  299. NSMutableArray *tempArray = [NSMutableArray array];
  300. while (currentTrack) {
  301. [tempArray addObject:@(currentTrack->psz_name)];
  302. currentTrack = currentTrack->p_next;
  303. }
  304. libvlc_track_description_list_release(firstTrack);
  305. return [NSArray arrayWithArray: tempArray];
  306. }
  307. - (NSArray *)videoTrackIndexes
  308. {
  309. NSInteger count = libvlc_video_get_track_count(_playerInstance);
  310. if (count <= 0)
  311. return @[];
  312. libvlc_track_description_t *firstTrack = libvlc_video_get_track_description(_playerInstance);
  313. libvlc_track_description_t *currentTrack = firstTrack;
  314. NSMutableArray *tempArray = [NSMutableArray array];
  315. while (currentTrack) {
  316. [tempArray addObject:@(currentTrack->i_id)];
  317. currentTrack = currentTrack->p_next;
  318. }
  319. libvlc_track_description_list_release(firstTrack);
  320. return [NSArray arrayWithArray: tempArray];
  321. }
  322. - (int)numberOfVideoTracks
  323. {
  324. return libvlc_video_get_track_count(_playerInstance);
  325. }
  326. #pragma mark -
  327. #pragma mark Subtitles
  328. - (void)setCurrentVideoSubTitleIndex:(int)index
  329. {
  330. libvlc_video_set_spu(_playerInstance, index);
  331. }
  332. - (int)currentVideoSubTitleIndex
  333. {
  334. NSInteger count = libvlc_video_get_spu_count(_playerInstance);
  335. if (count <= 0)
  336. return INT_MAX;
  337. return libvlc_video_get_spu(_playerInstance);
  338. }
  339. - (NSArray *)videoSubTitlesNames
  340. {
  341. NSInteger count = libvlc_video_get_spu_count(_playerInstance);
  342. if (count <= 0)
  343. return @[];
  344. libvlc_track_description_t *firstTrack = libvlc_video_get_spu_description(_playerInstance);
  345. libvlc_track_description_t *currentTrack = firstTrack;
  346. NSMutableArray *tempArray = [NSMutableArray array];
  347. while (currentTrack) {
  348. [tempArray addObject:@(currentTrack->psz_name)];
  349. currentTrack = currentTrack->p_next;
  350. }
  351. libvlc_track_description_list_release(firstTrack);
  352. return [NSArray arrayWithArray: tempArray];
  353. }
  354. - (NSArray *)videoSubTitlesIndexes
  355. {
  356. NSInteger count = libvlc_video_get_spu_count(_playerInstance);
  357. if (count <= 0)
  358. return @[];
  359. libvlc_track_description_t *firstTrack = libvlc_video_get_spu_description(_playerInstance);
  360. libvlc_track_description_t *currentTrack = firstTrack;
  361. NSMutableArray *tempArray = [NSMutableArray array];
  362. while (currentTrack) {
  363. [tempArray addObject:@(currentTrack->i_id)];
  364. currentTrack = currentTrack->p_next;
  365. }
  366. libvlc_track_description_list_release(firstTrack);
  367. return [NSArray arrayWithArray: tempArray];
  368. }
  369. - (int)numberOfSubtitlesTracks
  370. {
  371. return libvlc_video_get_spu_count(_playerInstance);
  372. }
  373. - (BOOL)openVideoSubTitlesFromFile:(NSString *)path
  374. {
  375. return libvlc_video_set_subtitle_file(_playerInstance, [path UTF8String]);
  376. }
  377. - (void)setCurrentVideoSubTitleDelay:(NSInteger)index
  378. {
  379. libvlc_video_set_spu_delay(_playerInstance, index);
  380. }
  381. - (NSInteger)currentVideoSubTitleDelay
  382. {
  383. return libvlc_video_get_spu_delay(_playerInstance);
  384. }
  385. #if TARGET_OS_IPHONE
  386. - (void)setTextRendererFontSize:(NSNumber *)fontSize
  387. {
  388. libvlc_video_set_textrenderer_int(_playerInstance, libvlc_textrender_fontsize, [fontSize intValue]);
  389. }
  390. #endif
  391. #if TARGET_OS_IPHONE
  392. - (void)setTextRendererFont:(NSString *)fontname
  393. {
  394. libvlc_video_set_textrenderer_string(_playerInstance, libvlc_textrender_font, [fontname UTF8String]);
  395. }
  396. #endif
  397. #if TARGET_OS_IPHONE
  398. - (void)setTextRendererFontColor:(NSNumber *)fontColor
  399. {
  400. libvlc_video_set_textrenderer_int(_playerInstance, libvlc_textrender_fontcolor, [fontColor intValue]);
  401. }
  402. #endif
  403. #pragma mark -
  404. #pragma mark Video Crop geometry
  405. - (void)setVideoCropGeometry:(char *)value
  406. {
  407. libvlc_video_set_crop_geometry(_playerInstance, value);
  408. }
  409. - (char *)videoCropGeometry
  410. {
  411. char * result = libvlc_video_get_crop_geometry(_playerInstance);
  412. return result;
  413. }
  414. - (void)setVideoAspectRatio:(char *)value
  415. {
  416. libvlc_video_set_aspect_ratio(_playerInstance, value);
  417. }
  418. - (char *)videoAspectRatio
  419. {
  420. char * result = libvlc_video_get_aspect_ratio(_playerInstance);
  421. return result;
  422. }
  423. - (void)setScaleFactor:(float)value
  424. {
  425. libvlc_video_set_scale(_playerInstance, value);
  426. }
  427. - (float)scaleFactor
  428. {
  429. return libvlc_video_get_scale(_playerInstance);
  430. }
  431. - (void)saveVideoSnapshotAt:(NSString *)path withWidth:(int)width andHeight:(int)height
  432. {
  433. int failure = libvlc_video_take_snapshot(_playerInstance, 0, [path UTF8String], width, height);
  434. if (failure)
  435. [[NSException exceptionWithName:@"Can't take a video snapshot" reason:@"No video output" userInfo:nil] raise];
  436. }
  437. - (void)setDeinterlaceFilter:(NSString *)name
  438. {
  439. if (!name || name.length < 1)
  440. libvlc_video_set_deinterlace(_playerInstance, NULL);
  441. else
  442. libvlc_video_set_deinterlace(_playerInstance, [name UTF8String]);
  443. }
  444. - (BOOL)adjustFilterEnabled
  445. {
  446. return libvlc_video_get_adjust_int(_playerInstance, libvlc_adjust_Enable);
  447. }
  448. - (void)setAdjustFilterEnabled:(BOOL)b_value
  449. {
  450. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, b_value);
  451. }
  452. - (float)contrast
  453. {
  454. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  455. return libvlc_video_get_adjust_float(_playerInstance, libvlc_adjust_Contrast);
  456. }
  457. - (void)setContrast:(float)f_value
  458. {
  459. if (f_value <= 2. && f_value >= 0.) {
  460. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  461. libvlc_video_set_adjust_float(_playerInstance,libvlc_adjust_Contrast, f_value);
  462. }
  463. }
  464. - (float)brightness
  465. {
  466. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  467. return libvlc_video_get_adjust_float(_playerInstance, libvlc_adjust_Brightness);
  468. }
  469. - (void)setBrightness:(float)f_value
  470. {
  471. if (f_value <= 2. && f_value >= 0.) {
  472. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  473. libvlc_video_set_adjust_float(_playerInstance, libvlc_adjust_Brightness, f_value);
  474. }
  475. }
  476. - (int)hue
  477. {
  478. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  479. return libvlc_video_get_adjust_int(_playerInstance, libvlc_adjust_Hue);
  480. }
  481. - (void)setHue:(int)i_value
  482. {
  483. if (i_value <= 360 && i_value >= 0) {
  484. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  485. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Hue, i_value);
  486. }
  487. }
  488. - (float)saturation
  489. {
  490. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  491. return libvlc_video_get_adjust_float(_playerInstance, libvlc_adjust_Saturation);
  492. }
  493. - (void)setSaturation:(float)f_value
  494. {
  495. if (f_value <= 3. && f_value >= 0.) {
  496. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  497. libvlc_video_set_adjust_float(_playerInstance, libvlc_adjust_Saturation, f_value);
  498. }
  499. }
  500. - (float)gamma
  501. {
  502. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  503. return libvlc_video_get_adjust_float(_playerInstance, libvlc_adjust_Gamma);
  504. }
  505. - (void)setGamma:(float)f_value
  506. {
  507. if (f_value <= 10. && f_value >= 0.) {
  508. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  509. libvlc_video_set_adjust_float(_playerInstance, libvlc_adjust_Gamma, f_value);
  510. }
  511. }
  512. - (void)setRate:(float)value
  513. {
  514. libvlc_media_player_set_rate(_playerInstance, value);
  515. }
  516. - (float)rate
  517. {
  518. return libvlc_media_player_get_rate(_playerInstance);
  519. }
  520. - (CGSize)videoSize
  521. {
  522. unsigned height = 0, width = 0;
  523. int failure = libvlc_video_get_size(_playerInstance, 0, &width, &height);
  524. if (failure)
  525. return CGSizeZero;
  526. return CGSizeMake(width, height);
  527. }
  528. - (BOOL)hasVideoOut
  529. {
  530. return libvlc_media_player_has_vout(_playerInstance);
  531. }
  532. - (float)framesPerSecond
  533. {
  534. return .0;
  535. }
  536. - (void)setTime:(VLCTime *)value
  537. {
  538. // Time is managed in seconds, while duration is managed in microseconds
  539. // TODO: Redo VLCTime to provide value numberAsMilliseconds, numberAsMicroseconds, numberAsSeconds, numberAsMinutes, numberAsHours
  540. libvlc_media_player_set_time(_playerInstance, value ? [[value numberValue] longLongValue] : 0);
  541. }
  542. - (VLCTime *)time
  543. {
  544. return _cachedTime;
  545. }
  546. - (VLCTime *)remainingTime
  547. {
  548. return _cachedRemainingTime;
  549. }
  550. #pragma mark -
  551. #pragma mark Chapters
  552. - (void)setCurrentChapterIndex:(int)value;
  553. {
  554. libvlc_media_player_set_chapter(_playerInstance, value);
  555. }
  556. - (int)currentChapterIndex
  557. {
  558. int count = libvlc_media_player_get_chapter_count(_playerInstance);
  559. if (count <= 0)
  560. return INT_MAX;
  561. int result = libvlc_media_player_get_chapter(_playerInstance);
  562. return result;
  563. }
  564. - (void)nextChapter
  565. {
  566. libvlc_media_player_next_chapter(_playerInstance);
  567. }
  568. - (void)previousChapter
  569. {
  570. libvlc_media_player_previous_chapter(_playerInstance);
  571. }
  572. - (NSArray *)chaptersForTitleIndex:(int)title
  573. {
  574. NSInteger count = libvlc_media_player_get_chapter_count(_playerInstance);
  575. if (count <= 0)
  576. return @[];
  577. #pragma clang diagnostic push
  578. #pragma clang diagnostic ignored "-Wdeprecated"
  579. libvlc_track_description_t *firstTrack = libvlc_video_get_chapter_description(_playerInstance, title);
  580. libvlc_track_description_t *currentTrack = firstTrack;
  581. #pragma clang diagnostic push
  582. NSMutableArray *tempArray = [NSMutableArray array];
  583. for (NSInteger i = 0; i < count ; i++) {
  584. [tempArray addObject:@(currentTrack->psz_name)];
  585. currentTrack = currentTrack->p_next;
  586. }
  587. libvlc_track_description_list_release(firstTrack);
  588. return [NSArray arrayWithArray:tempArray];
  589. }
  590. #pragma mark -
  591. #pragma mark Titles
  592. - (void)setCurrentTitleIndex:(int)value
  593. {
  594. libvlc_media_player_set_title(_playerInstance, value);
  595. }
  596. - (int)currentTitleIndex
  597. {
  598. NSInteger count = libvlc_media_player_get_title_count(_playerInstance);
  599. if (count <= 0)
  600. return INT_MAX;
  601. return libvlc_media_player_get_title(_playerInstance);
  602. }
  603. - (int)numberOfTitles
  604. {
  605. return libvlc_media_player_get_title_count(_playerInstance);
  606. }
  607. - (NSUInteger)countOfTitles
  608. {
  609. NSUInteger result = libvlc_media_player_get_title_count(_playerInstance);
  610. return result;
  611. }
  612. - (NSArray *)titles
  613. {
  614. NSUInteger count = [self countOfTitles];
  615. if (count == 0)
  616. return [NSArray array];
  617. #pragma clang diagnostic push
  618. #pragma clang diagnostic ignored "-Wdeprecated"
  619. libvlc_track_description_t *firstTrack = libvlc_video_get_title_description(_playerInstance);
  620. libvlc_track_description_t *currentTrack = firstTrack;
  621. #pragma clang diagnostic pop
  622. if (!currentTrack)
  623. return [NSArray array];
  624. NSMutableArray *tempArray = [NSMutableArray array];
  625. while (1) {
  626. if (currentTrack->psz_name != nil)
  627. [tempArray addObject:@(currentTrack->psz_name)];
  628. if (currentTrack->p_next)
  629. currentTrack = currentTrack->p_next;
  630. else
  631. break;
  632. }
  633. libvlc_track_description_list_release(firstTrack);
  634. return [NSArray arrayWithArray: tempArray];
  635. }
  636. - (NSArray *)titleDescriptions
  637. {
  638. libvlc_title_description_t **titleInfo;
  639. int numberOfTitleDescriptions = libvlc_media_player_get_full_title_descriptions(_playerInstance, &titleInfo);
  640. if (numberOfTitleDescriptions < 0)
  641. return [NSArray array];
  642. if (numberOfTitleDescriptions == 0) {
  643. libvlc_title_descriptions_release(titleInfo, numberOfTitleDescriptions);
  644. return [NSArray array];
  645. }
  646. NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:numberOfTitleDescriptions];
  647. for (int i = 0; i < numberOfTitleDescriptions; i++) {
  648. NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
  649. [NSNumber numberWithLongLong:titleInfo[i]->i_duration],
  650. VLCTitleDescriptionDuration,
  651. @(titleInfo[i]->b_menu),
  652. VLCTitleDescriptionIsMenu,
  653. nil];
  654. if (titleInfo[i]->psz_name != NULL)
  655. dictionary[VLCTitleDescriptionName] = [NSString stringWithUTF8String:titleInfo[i]->psz_name];
  656. [array addObject:[NSDictionary dictionaryWithDictionary:dictionary]];
  657. }
  658. libvlc_title_descriptions_release(titleInfo, numberOfTitleDescriptions);
  659. return [NSArray arrayWithArray:array];
  660. }
  661. - (int)indexOfLongestTitle
  662. {
  663. NSArray *titles = [self titleDescriptions];
  664. NSUInteger titleCount = titles.count;
  665. int currentlyFoundTitle = 0;
  666. int64_t currentlySelectedDuration = 0;
  667. int64_t randomTitleDuration = 0;
  668. for (int x = 0; x < titleCount; x++) {
  669. randomTitleDuration = [[titles[x] valueForKey:VLCTitleDescriptionDuration] longLongValue];
  670. if (randomTitleDuration > currentlySelectedDuration) {
  671. currentlySelectedDuration = randomTitleDuration;
  672. currentlyFoundTitle = x;
  673. }
  674. }
  675. return currentlyFoundTitle;
  676. }
  677. - (int)numberOfChaptersForTitle:(int)titleIndex
  678. {
  679. return libvlc_media_player_get_chapter_count_for_title(_playerInstance, titleIndex);
  680. }
  681. - (NSArray *)chapterDescriptionsOfTitle:(int)titleIndex
  682. {
  683. libvlc_chapter_description_t **chapterDescriptions;
  684. int numberOfChapterDescriptions = libvlc_media_player_get_full_chapter_descriptions(_playerInstance,
  685. titleIndex,
  686. &chapterDescriptions);
  687. if (numberOfChapterDescriptions < 0)
  688. return [NSArray array];
  689. if (numberOfChapterDescriptions == 0) {
  690. libvlc_chapter_descriptions_release(chapterDescriptions, numberOfChapterDescriptions);
  691. return [NSArray array];
  692. }
  693. NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:numberOfChapterDescriptions];
  694. for (int i = 0; i < numberOfChapterDescriptions; i++) {
  695. NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
  696. [NSNumber numberWithLongLong:chapterDescriptions[i]->i_duration],
  697. VLCChapterDescriptionDuration,
  698. [NSNumber numberWithLongLong:chapterDescriptions[i]->i_time_offset],
  699. VLCChapterDescriptionTimeOffset,
  700. nil];
  701. if (chapterDescriptions[i]->psz_name != NULL)
  702. dictionary[VLCChapterDescriptionName] = [NSString stringWithUTF8String:chapterDescriptions[i]->psz_name];
  703. [array addObject:[NSDictionary dictionaryWithDictionary:dictionary]];
  704. }
  705. libvlc_chapter_descriptions_release(chapterDescriptions, numberOfChapterDescriptions);
  706. return [NSArray arrayWithArray:array];
  707. }
  708. #pragma mark -
  709. #pragma mark Audio tracks
  710. - (void)setCurrentAudioTrackIndex:(int)value
  711. {
  712. libvlc_audio_set_track(_playerInstance, value);
  713. }
  714. - (int)currentAudioTrackIndex
  715. {
  716. NSInteger count = libvlc_audio_get_track_count(_playerInstance);
  717. if (count <= 0)
  718. return INT_MAX;
  719. return libvlc_audio_get_track(_playerInstance);
  720. }
  721. - (NSArray *)audioTrackNames
  722. {
  723. NSInteger count = libvlc_audio_get_track_count(_playerInstance);
  724. if (count <= 0)
  725. return @[];
  726. libvlc_track_description_t *firstTrack = libvlc_audio_get_track_description(_playerInstance);
  727. libvlc_track_description_t *currentTrack = firstTrack;
  728. NSMutableArray *tempArray = [NSMutableArray array];
  729. while (currentTrack) {
  730. [tempArray addObject:@(currentTrack->psz_name)];
  731. currentTrack = currentTrack->p_next;
  732. }
  733. libvlc_track_description_list_release(firstTrack);
  734. return [NSArray arrayWithArray: tempArray];
  735. }
  736. - (NSArray *)audioTrackIndexes
  737. {
  738. NSInteger count = libvlc_audio_get_track_count(_playerInstance);
  739. if (count <= 0)
  740. return @[];
  741. libvlc_track_description_t *firstTrack = libvlc_audio_get_track_description(_playerInstance);
  742. libvlc_track_description_t *currentTrack = firstTrack;
  743. NSMutableArray *tempArray = [NSMutableArray array];
  744. while (currentTrack) {
  745. [tempArray addObject:@(currentTrack->i_id)];
  746. currentTrack = currentTrack->p_next;
  747. }
  748. libvlc_track_description_list_release(firstTrack);
  749. return [NSArray arrayWithArray: tempArray];
  750. }
  751. - (int)numberOfAudioTracks
  752. {
  753. return libvlc_audio_get_track_count(_playerInstance);
  754. }
  755. - (void)setAudioChannel:(int)value
  756. {
  757. libvlc_audio_set_channel(_playerInstance, value);
  758. }
  759. - (int)audioChannel
  760. {
  761. return libvlc_audio_get_channel(_playerInstance);
  762. }
  763. - (void)setCurrentAudioPlaybackDelay:(NSInteger)index
  764. {
  765. libvlc_audio_set_delay(_playerInstance, index);
  766. }
  767. - (NSInteger)currentAudioPlaybackDelay
  768. {
  769. return libvlc_audio_get_delay(_playerInstance);
  770. }
  771. #pragma mark -
  772. #pragma mark equalizer
  773. - (void)setEqualizerEnabled:(BOOL)equalizerEnabled
  774. {
  775. _equalizerEnabled = equalizerEnabled;
  776. if (!_equalizerEnabled) {
  777. libvlc_media_player_set_equalizer(_playerInstance, NULL);
  778. if (_equalizerInstance)
  779. libvlc_audio_equalizer_release(_equalizerInstance);
  780. return;
  781. }
  782. if (!_equalizerInstance)
  783. _equalizerInstance = libvlc_audio_equalizer_new();
  784. libvlc_media_player_set_equalizer(_playerInstance, _equalizerInstance);
  785. }
  786. - (BOOL)equalizerEnabled
  787. {
  788. return _equalizerEnabled;
  789. }
  790. - (NSArray *)equalizerProfiles
  791. {
  792. unsigned count = libvlc_audio_equalizer_get_preset_count();
  793. NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:count];
  794. for (unsigned x = 0; x < count; x++)
  795. [array addObject:@(libvlc_audio_equalizer_get_preset_name(x))];
  796. return [NSArray arrayWithArray:array];
  797. }
  798. - (void)resetEqualizerFromProfile:(unsigned)profile
  799. {
  800. BOOL wasactive = NO;
  801. if (_equalizerInstance) {
  802. libvlc_media_player_set_equalizer(_playerInstance, NULL);
  803. libvlc_audio_equalizer_release(_equalizerInstance);
  804. wasactive = YES;
  805. }
  806. _equalizerInstance = libvlc_audio_equalizer_new_from_preset(profile);
  807. if (wasactive)
  808. libvlc_media_player_set_equalizer(_playerInstance, _equalizerInstance);
  809. }
  810. - (CGFloat)preAmplification
  811. {
  812. if (!_equalizerInstance)
  813. return 0.;
  814. return libvlc_audio_equalizer_get_preamp(_equalizerInstance);
  815. }
  816. - (void)setPreAmplification:(CGFloat)preAmplification
  817. {
  818. if (!_equalizerInstance)
  819. _equalizerInstance = libvlc_audio_equalizer_new();
  820. libvlc_audio_equalizer_set_preamp(_equalizerInstance, preAmplification);
  821. libvlc_media_player_set_equalizer(_playerInstance, _equalizerInstance);
  822. }
  823. - (unsigned)numberOfBands
  824. {
  825. return libvlc_audio_equalizer_get_band_count();
  826. }
  827. - (CGFloat)frequencyOfBandAtIndex:(unsigned int)index
  828. {
  829. return libvlc_audio_equalizer_get_band_frequency(index);
  830. }
  831. - (void)setAmplification:(CGFloat)amplification forBand:(unsigned int)index
  832. {
  833. if (!_equalizerInstance)
  834. _equalizerInstance = libvlc_audio_equalizer_new();
  835. libvlc_audio_equalizer_set_amp_at_index(_equalizerInstance, amplification, index);
  836. }
  837. - (CGFloat)amplificationOfBand:(unsigned int)index
  838. {
  839. if (!_equalizerInstance)
  840. return 0.;
  841. return libvlc_audio_equalizer_get_amp_at_index(_equalizerInstance, index);
  842. }
  843. #pragma mark -
  844. #pragma mark set/get media
  845. - (void)setMedia:(VLCMedia *)value
  846. {
  847. if (_media != value) {
  848. if (_media && [_media compare:value] == NSOrderedSame)
  849. return;
  850. _media = value;
  851. libvlc_media_player_set_media(_playerInstance, [_media libVLCMediaDescriptor]);
  852. }
  853. }
  854. - (VLCMedia *)media
  855. {
  856. return _media;
  857. }
  858. #pragma mark -
  859. #pragma mark playback
  860. - (BOOL)play
  861. {
  862. libvlc_media_player_play(_playerInstance);
  863. return YES;
  864. }
  865. - (void)pause
  866. {
  867. if ([NSThread isMainThread]) {
  868. /* Hack because we create a dead lock here, when the vout is stopped
  869. * and tries to recontact us on the main thread */
  870. /* FIXME: to do this properly we need to do some locking. We may want
  871. * to move that to libvlc */
  872. [self performSelectorInBackground:@selector(pause) withObject:nil];
  873. return;
  874. }
  875. // Pause the stream
  876. libvlc_media_player_pause(_playerInstance);
  877. }
  878. - (void)stop
  879. {
  880. if ([NSThread isMainThread]) {
  881. /* Hack because we create a dead lock here, when the vout is stopped
  882. * and tries to recontact us on the main thread */
  883. /* FIXME: to do this properly we need to do some locking. We may want
  884. * to move that to libvlc */
  885. [self performSelectorInBackground:@selector(stop) withObject:nil];
  886. return;
  887. }
  888. libvlc_media_player_stop(_playerInstance);
  889. }
  890. - (void)gotoNextFrame
  891. {
  892. libvlc_media_player_next_frame(_playerInstance);
  893. }
  894. - (void)fastForward
  895. {
  896. [self fastForwardAtRate: 2.0];
  897. }
  898. - (void)fastForwardAtRate:(float)rate
  899. {
  900. [self setRate:rate];
  901. }
  902. - (void)rewind
  903. {
  904. [self rewindAtRate: 2.0];
  905. }
  906. - (void)rewindAtRate:(float)rate
  907. {
  908. [self setRate: -rate];
  909. }
  910. - (void)jumpBackward:(int)interval
  911. {
  912. if ([self isSeekable]) {
  913. interval = interval * 1000;
  914. [self setTime: [VLCTime timeWithInt: ([[self time] intValue] - interval)]];
  915. }
  916. }
  917. - (void)jumpForward:(int)interval
  918. {
  919. if ([self isSeekable]) {
  920. interval = interval * 1000;
  921. [self setTime: [VLCTime timeWithInt: ([[self time] intValue] + interval)]];
  922. }
  923. }
  924. - (void)extraShortJumpBackward
  925. {
  926. [self jumpBackward:3];
  927. }
  928. - (void)extraShortJumpForward
  929. {
  930. [self jumpForward:3];
  931. }
  932. - (void)shortJumpBackward
  933. {
  934. [self jumpBackward:10];
  935. }
  936. - (void)shortJumpForward
  937. {
  938. [self jumpForward:10];
  939. }
  940. - (void)mediumJumpBackward
  941. {
  942. [self jumpBackward:60];
  943. }
  944. - (void)mediumJumpForward
  945. {
  946. [self jumpForward:60];
  947. }
  948. - (void)longJumpBackward
  949. {
  950. [self jumpBackward:300];
  951. }
  952. - (void)longJumpForward
  953. {
  954. [self jumpForward:300];
  955. }
  956. + (NSSet *)keyPathsForValuesAffectingIsPlaying
  957. {
  958. return [NSSet setWithObjects:@"state", nil];
  959. }
  960. - (BOOL)isPlaying
  961. {
  962. return libvlc_media_player_is_playing(_playerInstance);
  963. }
  964. - (BOOL)willPlay
  965. {
  966. return libvlc_media_player_will_play(_playerInstance);
  967. }
  968. - (VLCMediaPlayerState)state
  969. {
  970. return _cachedState;
  971. }
  972. - (float)position
  973. {
  974. return _position;
  975. }
  976. - (void)setPosition:(float)newPosition
  977. {
  978. libvlc_media_player_set_position(_playerInstance, newPosition);
  979. }
  980. - (BOOL)isSeekable
  981. {
  982. return libvlc_media_player_is_seekable(_playerInstance);
  983. }
  984. - (BOOL)canPause
  985. {
  986. return libvlc_media_player_can_pause(_playerInstance);
  987. }
  988. #if TARGET_OS_IPHONE
  989. - (NSArray *)snapshots
  990. {
  991. return [_snapshots copy];
  992. }
  993. - (UIImage *)lastSnapshot {
  994. if (_snapshots == nil) {
  995. return nil;
  996. }
  997. @synchronized(_snapshots) {
  998. if (_snapshots.count == 0)
  999. return nil;
  1000. return [UIImage imageWithContentsOfFile:[_snapshots lastObject]];
  1001. }
  1002. }
  1003. #endif
  1004. - (void *)libVLCMediaPlayer
  1005. {
  1006. return _playerInstance;
  1007. }
  1008. @end
  1009. @implementation VLCMediaPlayer (Private)
  1010. - (instancetype)initWithDrawable:(id)aDrawable options:(NSArray *)options
  1011. {
  1012. if (self = [super init]) {
  1013. _cachedTime = [VLCTime nullTime];
  1014. _cachedRemainingTime = [VLCTime nullTime];
  1015. _position = 0.0f;
  1016. _cachedState = VLCMediaPlayerStateStopped;
  1017. // Create a media instance, it doesn't matter what library we start off with
  1018. // it will change depending on the media descriptor provided to the media
  1019. // instance
  1020. if (options && options.count > 0) {
  1021. VKLog(@"creating player instance with private library as options were given");
  1022. _privateLibrary = [[VLCLibrary alloc] initWithOptions:options];
  1023. } else {
  1024. VKLog(@"creating player instance using shared library");
  1025. _privateLibrary = [VLCLibrary sharedLibrary];
  1026. }
  1027. libvlc_retain([_privateLibrary instance]);
  1028. _playerInstance = libvlc_media_player_new([_privateLibrary instance]);
  1029. libvlc_media_player_retain(_playerInstance);
  1030. [self registerObservers];
  1031. [self setDrawable:aDrawable];
  1032. }
  1033. return self;
  1034. }
  1035. - (void)registerObservers
  1036. {
  1037. // Attach event observers into the media instance
  1038. libvlc_event_manager_t * p_em = libvlc_media_player_event_manager(_playerInstance);
  1039. if (!p_em)
  1040. return;
  1041. libvlc_event_attach(p_em, libvlc_MediaPlayerPlaying, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1042. libvlc_event_attach(p_em, libvlc_MediaPlayerPaused, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1043. libvlc_event_attach(p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1044. libvlc_event_attach(p_em, libvlc_MediaPlayerEndReached, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1045. libvlc_event_attach(p_em, libvlc_MediaPlayerStopped, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1046. libvlc_event_attach(p_em, libvlc_MediaPlayerOpening, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1047. libvlc_event_attach(p_em, libvlc_MediaPlayerBuffering, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1048. libvlc_event_attach(p_em, libvlc_MediaPlayerPositionChanged, HandleMediaPositionChanged, (__bridge void *)(self));
  1049. libvlc_event_attach(p_em, libvlc_MediaPlayerTimeChanged, HandleMediaTimeChanged, (__bridge void *)(self));
  1050. libvlc_event_attach(p_em, libvlc_MediaPlayerMediaChanged, HandleMediaPlayerMediaChanged, (__bridge void *)(self));
  1051. #if TARGET_OS_IPHONE
  1052. libvlc_event_attach(p_em, libvlc_MediaPlayerSnapshotTaken, HandleMediaPlayerSnapshot, (__bridge void *)(self));
  1053. #endif
  1054. }
  1055. - (void)unregisterObservers
  1056. {
  1057. libvlc_event_manager_t * p_em = libvlc_media_player_event_manager(_playerInstance);
  1058. if (!p_em)
  1059. return;
  1060. libvlc_event_detach(p_em, libvlc_MediaPlayerPlaying, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1061. libvlc_event_detach(p_em, libvlc_MediaPlayerPaused, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1062. libvlc_event_detach(p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1063. libvlc_event_detach(p_em, libvlc_MediaPlayerEndReached, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1064. libvlc_event_detach(p_em, libvlc_MediaPlayerStopped, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1065. libvlc_event_detach(p_em, libvlc_MediaPlayerOpening, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1066. libvlc_event_detach(p_em, libvlc_MediaPlayerBuffering, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1067. libvlc_event_detach(p_em, libvlc_MediaPlayerPositionChanged, HandleMediaPositionChanged, (__bridge void *)(self));
  1068. libvlc_event_detach(p_em, libvlc_MediaPlayerTimeChanged, HandleMediaTimeChanged, (__bridge void *)(self));
  1069. libvlc_event_detach(p_em, libvlc_MediaPlayerMediaChanged, HandleMediaPlayerMediaChanged, (__bridge void *)(self));
  1070. #if TARGET_OS_IPHONE
  1071. libvlc_event_detach(p_em, libvlc_MediaPlayerSnapshotTaken, HandleMediaPlayerSnapshot, (__bridge void *)(self));
  1072. #endif
  1073. }
  1074. - (void)mediaPlayerTimeChanged:(NSNumber *)newTime
  1075. {
  1076. [self willChangeValueForKey:@"time"];
  1077. [self willChangeValueForKey:@"remainingTime"];
  1078. _cachedTime = [VLCTime timeWithNumber:newTime];
  1079. double currentTime = [[_cachedTime numberValue] doubleValue];
  1080. if (currentTime > 0) {
  1081. double remaining = currentTime / _position * (1 - _position);
  1082. _cachedRemainingTime = [VLCTime timeWithNumber:@(-remaining)];
  1083. } else
  1084. _cachedRemainingTime = [VLCTime nullTime];
  1085. [self didChangeValueForKey:@"remainingTime"];
  1086. [self didChangeValueForKey:@"time"];
  1087. }
  1088. #if !TARGET_OS_IPHONE
  1089. - (void)delaySleep
  1090. {
  1091. UpdateSystemActivity(UsrActivity);
  1092. }
  1093. #endif
  1094. - (void)mediaPlayerPositionChanged:(NSNumber *)newPosition
  1095. {
  1096. #if !TARGET_OS_IPHONE
  1097. // This seems to be the most relevant place to delay sleeping and screen saver.
  1098. [self delaySleep];
  1099. #endif
  1100. [self willChangeValueForKey:@"position"];
  1101. _position = [newPosition floatValue];
  1102. [self didChangeValueForKey:@"position"];
  1103. }
  1104. - (void)mediaPlayerStateChanged:(NSNumber *)newState
  1105. {
  1106. [self willChangeValueForKey:@"state"];
  1107. _cachedState = [newState intValue];
  1108. #if TARGET_OS_IPHONE
  1109. // Disable idle timer if player is playing media
  1110. // Exclusion can be made for audio only media
  1111. [UIApplication sharedApplication].idleTimerDisabled = [self isPlaying];
  1112. #endif
  1113. [self didChangeValueForKey:@"state"];
  1114. }
  1115. - (void)mediaPlayerMediaChanged:(VLCMedia *)newMedia
  1116. {
  1117. [self willChangeValueForKey:@"media"];
  1118. if (_media != newMedia)
  1119. _media = newMedia;
  1120. [self didChangeValueForKey:@"media"];
  1121. }
  1122. #if TARGET_OS_IPHONE
  1123. - (void)mediaPlayerSnapshot:(NSString *)fileName
  1124. {
  1125. @synchronized(_snapshots) {
  1126. if (!_snapshots) {
  1127. _snapshots = [NSMutableArray array];
  1128. }
  1129. [_snapshots addObject:fileName];
  1130. }
  1131. }
  1132. #endif
  1133. @end