VLCMediaPlayer.m 44 KB

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