VLCMediaPlayer.m 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  1. /*****************************************************************************
  2. * VLCMediaPlayer.m: VLCKit.framework VLCMediaPlayer implementation
  3. *****************************************************************************
  4. * Copyright (C) 2007-2009 Pierre d'Herbemont
  5. * Copyright (C) 2007-2013 VLC authors and VideoLAN
  6. * Partial Copyright (C) 2009-2013 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 * VLCMediaPlayerTimeChanged = @"VLCMediaPlayerTimeChanged";
  48. NSString * VLCMediaPlayerStateChanged = @"VLCMediaPlayerStateChanged";
  49. NSString * VLCMediaPlayerStateToString(VLCMediaPlayerState state)
  50. {
  51. static NSString * stateToStrings[] = {
  52. [VLCMediaPlayerStateStopped] = @"VLCMediaPlayerStateStopped",
  53. [VLCMediaPlayerStateOpening] = @"VLCMediaPlayerStateOpening",
  54. [VLCMediaPlayerStateBuffering] = @"VLCMediaPlayerStateBuffering",
  55. [VLCMediaPlayerStateEnded] = @"VLCMediaPlayerStateEnded",
  56. [VLCMediaPlayerStateError] = @"VLCMediaPlayerStateError",
  57. [VLCMediaPlayerStatePlaying] = @"VLCMediaPlayerStatePlaying",
  58. [VLCMediaPlayerStatePaused] = @"VLCMediaPlayerStatePaused"
  59. };
  60. return stateToStrings[state];
  61. }
  62. /* libvlc event callback */
  63. static void HandleMediaInstanceVolumeChanged(const libvlc_event_t * event, void * self)
  64. {
  65. [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:self
  66. withDelegateMethod:@selector(mediaPlayerVolumeChanged:)
  67. withNotificationName:VLCMediaPlayerVolumeChanged];
  68. }
  69. static void HandleMediaTimeChanged(const libvlc_event_t * event, void * self)
  70. {
  71. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  72. [[VLCEventManager sharedManager] callOnMainThreadObject:self
  73. withMethod:@selector(mediaPlayerTimeChanged:)
  74. withArgumentAsObject:@(event->u.media_player_time_changed.new_time)];
  75. [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:self
  76. withDelegateMethod:@selector(mediaPlayerTimeChanged:)
  77. withNotificationName:VLCMediaPlayerTimeChanged];
  78. [pool release];
  79. }
  80. static void HandleMediaPositionChanged(const libvlc_event_t * event, void * self)
  81. {
  82. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  83. [[VLCEventManager sharedManager] callOnMainThreadObject:self
  84. withMethod:@selector(mediaPlayerPositionChanged:)
  85. withArgumentAsObject:@(event->u.media_player_position_changed.new_position)];
  86. [pool release];
  87. }
  88. static void HandleMediaInstanceStateChanged(const libvlc_event_t * event, void * self)
  89. {
  90. VLCMediaPlayerState newState;
  91. if (event->type == libvlc_MediaPlayerPlaying)
  92. newState = VLCMediaPlayerStatePlaying;
  93. else if (event->type == libvlc_MediaPlayerPaused)
  94. newState = VLCMediaPlayerStatePaused;
  95. else if (event->type == libvlc_MediaPlayerEndReached)
  96. newState = VLCMediaPlayerStateStopped;
  97. else if (event->type == libvlc_MediaPlayerEncounteredError)
  98. newState = VLCMediaPlayerStateError;
  99. else if (event->type == libvlc_MediaPlayerBuffering)
  100. newState = VLCMediaPlayerStateBuffering;
  101. else if (event->type == libvlc_MediaPlayerOpening)
  102. newState = VLCMediaPlayerStateOpening;
  103. else {
  104. VKLog(@"%s: Unknown event", __FUNCTION__);
  105. return;
  106. }
  107. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  108. [[VLCEventManager sharedManager] callOnMainThreadObject:self
  109. withMethod:@selector(mediaPlayerStateChanged:)
  110. withArgumentAsObject:@(newState)];
  111. [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:self
  112. withDelegateMethod:@selector(mediaPlayerStateChanged:)
  113. withNotificationName:VLCMediaPlayerStateChanged];
  114. [pool release];
  115. }
  116. static void HandleMediaPlayerMediaChanged(const libvlc_event_t * event, void * self)
  117. {
  118. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  119. [[VLCEventManager sharedManager] callOnMainThreadObject:self
  120. withMethod:@selector(mediaPlayerMediaChanged:)
  121. withArgumentAsObject:[VLCMedia mediaWithLibVLCMediaDescriptor:event->u.media_player_media_changed.new_media]];
  122. [pool release];
  123. }
  124. // TODO: Documentation
  125. @interface VLCMediaPlayer (Private)
  126. - (id)initWithDrawable:(id)aDrawable;
  127. - (void)registerObservers;
  128. - (void)unregisterObservers;
  129. - (void)mediaPlayerTimeChanged:(NSNumber *)newTime;
  130. - (void)mediaPlayerPositionChanged:(NSNumber *)newTime;
  131. - (void)mediaPlayerStateChanged:(NSNumber *)newState;
  132. - (void)mediaPlayerMediaChanged:(VLCMedia *)media;
  133. @end
  134. @implementation VLCMediaPlayer
  135. /* Bindings */
  136. + (NSSet *)keyPathsForValuesAffectingValueForKey:(NSString *)key
  137. {
  138. static NSDictionary * dict = nil;
  139. NSSet * superKeyPaths;
  140. if (!dict) {
  141. dict = [@{@"playing": [NSSet setWithObject:@"state"],
  142. @"seekable": [NSSet setWithObjects:@"state", @"media", nil],
  143. @"canPause": [NSSet setWithObjects:@"state", @"media", nil],
  144. @"description": [NSSet setWithObjects:@"state", @"media", nil]} retain];
  145. }
  146. if ((superKeyPaths = [super keyPathsForValuesAffectingValueForKey: key])) {
  147. NSMutableSet * ret = [NSMutableSet setWithSet:dict[key]];
  148. [ret unionSet:superKeyPaths];
  149. return ret;
  150. }
  151. return dict[key];
  152. }
  153. /* Contructor */
  154. - (id)init
  155. {
  156. return [self initWithDrawable:nil];
  157. }
  158. #if !TARGET_OS_IPHONE
  159. - (id)initWithVideoView:(VLCVideoView *)aVideoView
  160. {
  161. return [self initWithDrawable: aVideoView];
  162. }
  163. - (id)initWithVideoLayer:(VLCVideoLayer *)aVideoLayer
  164. {
  165. return [self initWithDrawable: aVideoLayer];
  166. }
  167. #endif
  168. - (void)dealloc
  169. {
  170. NSAssert(libvlc_media_player_get_state(instance) == libvlc_Stopped, @"You released the media player before ensuring that it is stopped");
  171. [self unregisterObservers];
  172. [[VLCEventManager sharedManager] cancelCallToObject:self];
  173. // Always get rid of the delegate first so we can stop sending messages to it
  174. // TODO: Should we tell the delegate that we're shutting down?
  175. delegate = nil;
  176. // Clear our drawable as we are going to release it, we don't
  177. // want the core to use it from this point. This won't happen as
  178. // the media player must be stopped.
  179. libvlc_media_player_set_nsobject(instance, nil);
  180. libvlc_media_player_release(instance);
  181. // Get rid of everything else
  182. [media release];
  183. [cachedTime release];
  184. [cachedRemainingTime release];
  185. [drawable release];
  186. [audio release];
  187. [super dealloc];
  188. }
  189. - (void)setDelegate:(id)value
  190. {
  191. delegate = value;
  192. }
  193. - (id)delegate
  194. {
  195. return delegate;
  196. }
  197. #if !TARGET_OS_IPHONE
  198. - (void)setVideoView:(VLCVideoView *)aVideoView
  199. {
  200. [self setDrawable: aVideoView];
  201. }
  202. - (void)setVideoLayer:(VLCVideoLayer *)aVideoLayer
  203. {
  204. [self setDrawable: aVideoLayer];
  205. }
  206. #endif
  207. - (void)setDrawable:(id)aDrawable
  208. {
  209. // Make sure that this instance has been associated with the drawing canvas.
  210. libvlc_media_player_set_nsobject(instance, aDrawable);
  211. }
  212. - (id)drawable
  213. {
  214. return libvlc_media_player_get_nsobject(instance);
  215. }
  216. - (VLCAudio *)audio
  217. {
  218. if (!audio)
  219. audio = [[VLCAudio alloc] initWithMediaPlayer:self];
  220. return audio;
  221. }
  222. #pragma mark -
  223. #pragma mark Video Tracks
  224. - (void)setCurrentVideoTrackIndex:(NSUInteger)value
  225. {
  226. libvlc_video_set_track(instance, (int)value);
  227. }
  228. - (NSUInteger)currentVideoTrackIndex
  229. {
  230. NSInteger count = libvlc_video_get_track_count(instance);
  231. if (count <= 0)
  232. return NSNotFound;
  233. NSUInteger result = libvlc_video_get_track(instance);
  234. return result;
  235. }
  236. - (NSArray *)videoTrackNames
  237. {
  238. NSInteger count = libvlc_video_get_track_count(instance);
  239. if (count <= 0)
  240. return @[];
  241. libvlc_track_description_t *currentTrack = libvlc_video_get_track_description(instance);
  242. NSMutableArray *tempArray = [NSMutableArray array];
  243. while (currentTrack) {
  244. [tempArray addObject:@(currentTrack->psz_name)];
  245. currentTrack = currentTrack->p_next;
  246. }
  247. libvlc_track_description_list_release(currentTrack);
  248. return [NSArray arrayWithArray: tempArray];
  249. }
  250. - (NSArray *)videoTrackIndexes
  251. {
  252. NSInteger count = libvlc_video_get_track_count(instance);
  253. if (count <= 0)
  254. return @[];
  255. libvlc_track_description_t *currentTrack = libvlc_video_get_track_description(instance);
  256. NSMutableArray *tempArray = [NSMutableArray array];
  257. while (currentTrack) {
  258. [tempArray addObject:@(currentTrack->i_id)];
  259. currentTrack = currentTrack->p_next;
  260. }
  261. libvlc_track_description_list_release(currentTrack);
  262. return [NSArray arrayWithArray: tempArray];
  263. }
  264. - (NSArray *)videoTracks
  265. {
  266. NSInteger count = libvlc_video_get_track_count(instance);
  267. if (count <= 0)
  268. return @[];
  269. libvlc_track_description_t *tracks = libvlc_video_get_track_description(instance);
  270. NSMutableArray *tempArray = [NSMutableArray array];
  271. for (NSUInteger i = 0; i < count ; i++) {
  272. [tempArray addObject:@(tracks->psz_name)];
  273. tracks = tracks->p_next;
  274. }
  275. libvlc_track_description_list_release(tracks);
  276. return [NSArray arrayWithArray: tempArray];
  277. }
  278. #pragma mark -
  279. #pragma mark Subtitles
  280. - (void)setCurrentVideoSubTitleIndex:(NSUInteger)index
  281. {
  282. libvlc_video_set_spu(instance, (int)index);
  283. }
  284. - (NSUInteger)currentVideoSubTitleIndex
  285. {
  286. NSInteger count = libvlc_video_get_spu_count(instance);
  287. if (count <= 0)
  288. return NSNotFound;
  289. return libvlc_video_get_spu(instance);
  290. }
  291. - (NSArray *)videoSubTitlesNames
  292. {
  293. NSInteger count = libvlc_video_get_spu_count(instance);
  294. if (count <= 0)
  295. return @[];
  296. libvlc_track_description_t *currentTrack = libvlc_video_get_spu_description(instance);
  297. NSMutableArray *tempArray = [NSMutableArray array];
  298. while (currentTrack) {
  299. [tempArray addObject:@(currentTrack->psz_name)];
  300. currentTrack = currentTrack->p_next;
  301. }
  302. libvlc_track_description_list_release(currentTrack);
  303. return [NSArray arrayWithArray: tempArray];
  304. }
  305. - (NSArray *)videoSubTitlesIndexes
  306. {
  307. NSInteger count = libvlc_video_get_spu_count(instance);
  308. if (count <= 0)
  309. return @[];
  310. libvlc_track_description_t *currentTrack = libvlc_video_get_spu_description(instance);
  311. NSMutableArray *tempArray = [NSMutableArray array];
  312. while (currentTrack) {
  313. [tempArray addObject:@(currentTrack->i_id)];
  314. currentTrack = currentTrack->p_next;
  315. }
  316. libvlc_track_description_list_release(currentTrack);
  317. return [NSArray arrayWithArray: tempArray];
  318. }
  319. - (BOOL)openVideoSubTitlesFromFile:(NSString *)path
  320. {
  321. return libvlc_video_set_subtitle_file(instance, [path UTF8String]);
  322. }
  323. - (NSArray *)videoSubTitles
  324. {
  325. libvlc_track_description_t *currentTrack = libvlc_video_get_spu_description(instance);
  326. NSMutableArray *tempArray = [NSMutableArray array];
  327. while (currentTrack) {
  328. [tempArray addObject:@(currentTrack->psz_name)];
  329. currentTrack = currentTrack->p_next;
  330. }
  331. libvlc_track_description_list_release(currentTrack);
  332. return [NSArray arrayWithArray: tempArray];
  333. }
  334. - (void)setCurrentVideoSubTitleDelay:(NSInteger)index
  335. {
  336. libvlc_video_set_spu_delay(instance, index);
  337. }
  338. - (NSInteger)currentVideoSubTitleDelay
  339. {
  340. return libvlc_video_get_spu_delay(instance);
  341. }
  342. #pragma mark -
  343. #pragma mark Video Crop geometry
  344. - (void)setVideoCropGeometry:(char *)value
  345. {
  346. libvlc_video_set_crop_geometry(instance, value);
  347. }
  348. - (char *)videoCropGeometry
  349. {
  350. char * result = libvlc_video_get_crop_geometry(instance);
  351. return result;
  352. }
  353. - (void)setVideoAspectRatio:(char *)value
  354. {
  355. libvlc_video_set_aspect_ratio(instance, value);
  356. }
  357. - (char *)videoAspectRatio
  358. {
  359. char * result = libvlc_video_get_aspect_ratio(instance);
  360. return result;
  361. }
  362. - (void)setScaleFactor:(float)value
  363. {
  364. libvlc_video_set_scale(instance, value);
  365. }
  366. - (float)scaleFactor
  367. {
  368. return libvlc_video_get_scale(instance);
  369. }
  370. - (void)saveVideoSnapshotAt:(NSString *)path withWidth:(NSUInteger)width andHeight:(NSUInteger)height
  371. {
  372. int failure = libvlc_video_take_snapshot(instance, 0, [path UTF8String], width, height);
  373. if (failure)
  374. [[NSException exceptionWithName:@"Can't take a video snapshot" reason:@"No video output" userInfo:nil] raise];
  375. }
  376. - (void)setDeinterlaceFilter:(NSString *)name
  377. {
  378. libvlc_video_set_deinterlace(instance, [name UTF8String]);
  379. }
  380. - (BOOL)adjustFilterEnabled
  381. {
  382. return libvlc_video_get_adjust_int(instance, libvlc_adjust_Enable);
  383. }
  384. - (void)setAdjustFilterEnabled:(BOOL)b_value
  385. {
  386. libvlc_video_set_adjust_int(instance, libvlc_adjust_Enable, b_value);
  387. }
  388. - (float)contrast
  389. {
  390. libvlc_video_set_adjust_int(instance, libvlc_adjust_Enable, 1);
  391. return libvlc_video_get_adjust_float(instance, libvlc_adjust_Contrast);
  392. }
  393. - (void)setContrast:(float)f_value
  394. {
  395. if (f_value <= 2. && f_value >= 0.) {
  396. libvlc_video_set_adjust_int(instance, libvlc_adjust_Enable, 1);
  397. libvlc_video_set_adjust_float(instance,libvlc_adjust_Contrast, f_value);
  398. }
  399. }
  400. - (float)brightness
  401. {
  402. libvlc_video_set_adjust_int(instance, libvlc_adjust_Enable, 1);
  403. return libvlc_video_get_adjust_float(instance, libvlc_adjust_Brightness);
  404. }
  405. - (void)setBrightness:(float)f_value
  406. {
  407. if (f_value <= 2. && f_value >= 0.) {
  408. libvlc_video_set_adjust_int(instance, libvlc_adjust_Enable, 1);
  409. libvlc_video_set_adjust_float(instance, libvlc_adjust_Brightness, f_value);
  410. }
  411. }
  412. - (NSInteger)hue
  413. {
  414. libvlc_video_set_adjust_int(instance, libvlc_adjust_Enable, 1);
  415. return libvlc_video_get_adjust_int(instance, libvlc_adjust_Hue);
  416. }
  417. - (void)setHue:(NSInteger)i_value
  418. {
  419. if (i_value <= 360 && i_value >= 0) {
  420. libvlc_video_set_adjust_int(instance, libvlc_adjust_Enable, 1);
  421. libvlc_video_set_adjust_int(instance, libvlc_adjust_Hue, i_value);
  422. }
  423. }
  424. - (float)saturation
  425. {
  426. libvlc_video_set_adjust_int(instance, libvlc_adjust_Enable, 1);
  427. return libvlc_video_get_adjust_float(instance, libvlc_adjust_Saturation);
  428. }
  429. - (void)setSaturation:(float)f_value
  430. {
  431. if (f_value <= 3. && f_value >= 0.) {
  432. libvlc_video_set_adjust_int(instance, libvlc_adjust_Enable, 1);
  433. libvlc_video_set_adjust_float(instance, libvlc_adjust_Saturation, f_value);
  434. }
  435. }
  436. - (float)gamma
  437. {
  438. libvlc_video_set_adjust_int(instance, libvlc_adjust_Enable, 1);
  439. return libvlc_video_get_adjust_float(instance, libvlc_adjust_Gamma);
  440. }
  441. - (void)setGamma:(float)f_value
  442. {
  443. if (f_value <= 10. && f_value >= 0.) {
  444. libvlc_video_set_adjust_int(instance, libvlc_adjust_Enable, 1);
  445. libvlc_video_set_adjust_float(instance, libvlc_adjust_Gamma, f_value);
  446. }
  447. }
  448. - (void)setRate:(float)value
  449. {
  450. libvlc_media_player_set_rate(instance, value);
  451. }
  452. - (float)rate
  453. {
  454. return libvlc_media_player_get_rate(instance);
  455. }
  456. - (CGSize)videoSize
  457. {
  458. unsigned height = 0, width = 0;
  459. int failure = libvlc_video_get_size(instance, 0, &width, &height);
  460. if (failure)
  461. [[NSException exceptionWithName:@"Can't get video size" reason:@"No video output" userInfo:nil] raise];
  462. return CGSizeMake(width, height);
  463. }
  464. - (BOOL)hasVideoOut
  465. {
  466. return libvlc_media_player_has_vout(instance);
  467. }
  468. - (float)framesPerSecond
  469. {
  470. return libvlc_media_player_get_fps(instance);
  471. }
  472. - (void)setTime:(VLCTime *)value
  473. {
  474. // Time is managed in seconds, while duration is managed in microseconds
  475. // TODO: Redo VLCTime to provide value numberAsMilliseconds, numberAsMicroseconds, numberAsSeconds, numberAsMinutes, numberAsHours
  476. libvlc_media_player_set_time(instance, value ? [[value numberValue] longLongValue] : 0);
  477. }
  478. - (VLCTime *)time
  479. {
  480. return cachedTime;
  481. }
  482. - (VLCTime *)remainingTime
  483. {
  484. return cachedRemainingTime;
  485. }
  486. - (NSUInteger)fps
  487. {
  488. return libvlc_media_player_get_fps(instance);
  489. }
  490. #pragma mark -
  491. #pragma mark Chapters
  492. - (void)setCurrentChapterIndex:(NSUInteger)value;
  493. {
  494. libvlc_media_player_set_chapter(instance, value);
  495. }
  496. - (NSUInteger)currentChapterIndex
  497. {
  498. NSInteger count = libvlc_media_player_get_chapter_count(instance);
  499. if (count <= 0)
  500. return NSNotFound;
  501. NSUInteger result = libvlc_media_player_get_chapter(instance);
  502. return result;
  503. }
  504. - (void)nextChapter
  505. {
  506. libvlc_media_player_next_chapter(instance);
  507. }
  508. - (void)previousChapter
  509. {
  510. libvlc_media_player_previous_chapter(instance);
  511. }
  512. - (NSArray *)chaptersForTitleIndex:(NSUInteger)title
  513. {
  514. NSInteger count = libvlc_media_player_get_chapter_count(instance);
  515. if (count <= 0)
  516. return @[];
  517. libvlc_track_description_t *tracks = libvlc_video_get_chapter_description(instance, title);
  518. NSMutableArray *tempArray = [NSMutableArray array];
  519. for (NSInteger i = 0; i < count ; i++) {
  520. [tempArray addObject:@(tracks->psz_name)];
  521. tracks = tracks->p_next;
  522. }
  523. libvlc_track_description_list_release(tracks);
  524. return [NSArray arrayWithArray:tempArray];
  525. }
  526. #pragma mark -
  527. #pragma mark Titles
  528. - (void)setCurrentTitleIndex:(NSUInteger)value
  529. {
  530. libvlc_media_player_set_title(instance, value);
  531. }
  532. - (NSUInteger)currentTitleIndex
  533. {
  534. NSInteger count = libvlc_media_player_get_title_count(instance);
  535. if (count <= 0)
  536. return NSNotFound;
  537. return libvlc_media_player_get_title(instance);
  538. }
  539. - (NSUInteger)countOfTitles
  540. {
  541. NSUInteger result = libvlc_media_player_get_title_count(instance);
  542. return result;
  543. }
  544. - (NSArray *)titles
  545. {
  546. libvlc_track_description_t *tracks = libvlc_video_get_title_description(instance);
  547. NSMutableArray *tempArray = [NSMutableArray array];
  548. for (NSInteger i = 0; i < [self countOfTitles] ; i++) {
  549. [tempArray addObject:@(tracks->psz_name)];
  550. tracks = tracks->p_next;
  551. }
  552. libvlc_track_description_list_release(tracks);
  553. return [NSArray arrayWithArray: tempArray];
  554. }
  555. #pragma mark -
  556. #pragma mark Audio tracks
  557. - (void)setCurrentAudioTrackIndex:(NSUInteger)value
  558. {
  559. libvlc_audio_set_track(instance, (int)value);
  560. }
  561. - (NSUInteger)currentAudioTrackIndex
  562. {
  563. NSInteger count = libvlc_audio_get_track_count(instance);
  564. if (count <= 0)
  565. return NSNotFound;
  566. NSUInteger result = libvlc_audio_get_track(instance);
  567. return result;
  568. }
  569. - (NSArray *)audioTrackNames
  570. {
  571. NSInteger count = libvlc_audio_get_track_count(instance);
  572. if (count <= 0)
  573. return @[];
  574. libvlc_track_description_t *currentTrack = libvlc_audio_get_track_description(instance);
  575. NSMutableArray *tempArray = [NSMutableArray array];
  576. while (currentTrack) {
  577. [tempArray addObject:@(currentTrack->psz_name)];
  578. currentTrack = currentTrack->p_next;
  579. }
  580. libvlc_track_description_list_release(currentTrack);
  581. return [NSArray arrayWithArray: tempArray];
  582. }
  583. - (NSArray *)audioTrackIndexes
  584. {
  585. NSInteger count = libvlc_audio_get_track_count(instance);
  586. if (count <= 0)
  587. return @[];
  588. libvlc_track_description_t *currentTrack = libvlc_audio_get_track_description(instance);
  589. NSMutableArray *tempArray = [NSMutableArray array];
  590. while (currentTrack) {
  591. [tempArray addObject:@(currentTrack->i_id)];
  592. currentTrack = currentTrack->p_next;
  593. }
  594. libvlc_track_description_list_release(currentTrack);
  595. return [NSArray arrayWithArray: tempArray];
  596. }
  597. - (NSArray *)audioTracks
  598. {
  599. NSInteger count = libvlc_audio_get_track_count(instance);
  600. if (count <= 0)
  601. return @[];
  602. libvlc_track_description_t *tracks = libvlc_audio_get_track_description(instance);
  603. NSMutableArray *tempArray = [NSMutableArray array];
  604. for (NSUInteger i = 0; i < count ; i++) {
  605. [tempArray addObject:@(tracks->psz_name)];
  606. tracks = tracks->p_next;
  607. }
  608. libvlc_track_description_list_release(tracks);
  609. return [NSArray arrayWithArray: tempArray];
  610. }
  611. - (void)setAudioChannel:(NSInteger)value
  612. {
  613. libvlc_audio_set_channel(instance, value);
  614. }
  615. - (NSInteger)audioChannel
  616. {
  617. return libvlc_audio_get_channel(instance);
  618. }
  619. - (void)setCurrentAudioPlaybackDelay:(NSInteger)index
  620. {
  621. libvlc_audio_set_delay(instance, index);
  622. }
  623. - (NSInteger)currentAudioPlaybackDelay
  624. {
  625. return libvlc_audio_get_delay(instance);
  626. }
  627. #pragma mark -
  628. #pragma mark set/get media
  629. - (void)setMedia:(VLCMedia *)value
  630. {
  631. if (media != value) {
  632. if (media && [media compare:value] == NSOrderedSame)
  633. return;
  634. [media release];
  635. media = [value retain];
  636. libvlc_media_player_set_media(instance, [media libVLCMediaDescriptor]);
  637. }
  638. }
  639. - (VLCMedia *)media
  640. {
  641. return media;
  642. }
  643. #pragma mark -
  644. #pragma mark playback
  645. - (BOOL)play
  646. {
  647. libvlc_media_player_play(instance);
  648. return YES;
  649. }
  650. - (void)pause
  651. {
  652. if ([NSThread isMainThread]) {
  653. /* Hack because we create a dead lock here, when the vout is stopped
  654. * and tries to recontact us on the main thread */
  655. /* FIXME: to do this properly we need to do some locking. We may want
  656. * to move that to libvlc */
  657. [self performSelectorInBackground:@selector(pause) withObject:nil];
  658. return;
  659. }
  660. // Pause the stream
  661. libvlc_media_player_pause(instance);
  662. }
  663. - (void)stop
  664. {
  665. libvlc_media_player_stop(instance);
  666. }
  667. - (void)gotoNextFrame
  668. {
  669. libvlc_media_player_next_frame(instance);
  670. }
  671. - (void)fastForward
  672. {
  673. [self fastForwardAtRate: 2.0];
  674. }
  675. - (void)fastForwardAtRate:(float)rate
  676. {
  677. [self setRate:rate];
  678. }
  679. - (void)rewind
  680. {
  681. [self rewindAtRate: 2.0];
  682. }
  683. - (void)rewindAtRate:(float)rate
  684. {
  685. [self setRate: -rate];
  686. }
  687. - (void)jumpBackward:(NSInteger)interval
  688. {
  689. if ([self isSeekable]) {
  690. interval = interval * 1000;
  691. [self setTime: [VLCTime timeWithInt: ([[self time] intValue] - interval)]];
  692. }
  693. }
  694. - (void)jumpForward:(NSInteger)interval
  695. {
  696. if ([self isSeekable]) {
  697. interval = interval * 1000;
  698. [self setTime: [VLCTime timeWithInt: ([[self time] intValue] + interval)]];
  699. }
  700. }
  701. - (void)extraShortJumpBackward
  702. {
  703. [self jumpBackward:3];
  704. }
  705. - (void)extraShortJumpForward
  706. {
  707. [self jumpForward:3];
  708. }
  709. - (void)shortJumpBackward
  710. {
  711. [self jumpBackward:10];
  712. }
  713. - (void)shortJumpForward
  714. {
  715. [self jumpForward:10];
  716. }
  717. - (void)mediumJumpBackward
  718. {
  719. [self jumpBackward:60];
  720. }
  721. - (void)mediumJumpForward
  722. {
  723. [self jumpForward:60];
  724. }
  725. - (void)longJumpBackward
  726. {
  727. [self jumpBackward:300];
  728. }
  729. - (void)longJumpForward
  730. {
  731. [self jumpForward:300];
  732. }
  733. + (NSSet *)keyPathsForValuesAffectingIsPlaying
  734. {
  735. return [NSSet setWithObjects:@"state", nil];
  736. }
  737. - (BOOL)isPlaying
  738. {
  739. VLCMediaPlayerState state = [self state];
  740. return ((state == VLCMediaPlayerStateOpening) || (state == VLCMediaPlayerStateBuffering) ||
  741. (state == VLCMediaPlayerStatePlaying));
  742. }
  743. - (BOOL)willPlay
  744. {
  745. return libvlc_media_player_will_play(instance);
  746. }
  747. static const VLCMediaPlayerState libvlc_to_local_state[] =
  748. {
  749. [libvlc_Stopped] = VLCMediaPlayerStateStopped,
  750. [libvlc_Opening] = VLCMediaPlayerStateOpening,
  751. [libvlc_Buffering] = VLCMediaPlayerStateBuffering,
  752. [libvlc_Playing] = VLCMediaPlayerStatePlaying,
  753. [libvlc_Paused] = VLCMediaPlayerStatePaused,
  754. [libvlc_Ended] = VLCMediaPlayerStateEnded,
  755. [libvlc_Error] = VLCMediaPlayerStateError
  756. };
  757. - (VLCMediaPlayerState)state
  758. {
  759. return cachedState;
  760. }
  761. - (float)position
  762. {
  763. return position;
  764. }
  765. - (void)setPosition:(float)newPosition
  766. {
  767. libvlc_media_player_set_position(instance, newPosition);
  768. }
  769. - (BOOL)isSeekable
  770. {
  771. return libvlc_media_player_is_seekable(instance);
  772. }
  773. - (BOOL)canPause
  774. {
  775. return libvlc_media_player_can_pause(instance);
  776. }
  777. - (void *)libVLCMediaPlayer
  778. {
  779. return instance;
  780. }
  781. @end
  782. @implementation VLCMediaPlayer (Private)
  783. - (id)initWithDrawable:(id)aDrawable
  784. {
  785. if (self = [super init]) {
  786. delegate = nil;
  787. media = nil;
  788. cachedTime = [[VLCTime nullTime] retain];
  789. cachedRemainingTime = [[VLCTime nullTime] retain];
  790. position = 0.0f;
  791. cachedState = VLCMediaPlayerStateStopped;
  792. // Create a media instance, it doesn't matter what library we start off with
  793. // it will change depending on the media descriptor provided to the media
  794. // instance
  795. instance = libvlc_media_player_new([VLCLibrary sharedInstance]);
  796. [self registerObservers];
  797. [self setDrawable:aDrawable];
  798. }
  799. return self;
  800. }
  801. - (void)registerObservers
  802. {
  803. // Attach event observers into the media instance
  804. libvlc_event_manager_t * p_em = libvlc_media_player_event_manager(instance);
  805. libvlc_event_attach(p_em, libvlc_MediaPlayerPlaying, HandleMediaInstanceStateChanged, self);
  806. libvlc_event_attach(p_em, libvlc_MediaPlayerPaused, HandleMediaInstanceStateChanged, self);
  807. libvlc_event_attach(p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, self);
  808. libvlc_event_attach(p_em, libvlc_MediaPlayerEndReached, HandleMediaInstanceStateChanged, self);
  809. libvlc_event_attach(p_em, libvlc_MediaPlayerOpening, HandleMediaInstanceStateChanged, self);
  810. libvlc_event_attach(p_em, libvlc_MediaPlayerBuffering, HandleMediaInstanceStateChanged, self);
  811. /* FIXME: We may want to turn that off when none is interested by that */
  812. libvlc_event_attach(p_em, libvlc_MediaPlayerPositionChanged, HandleMediaPositionChanged, self);
  813. libvlc_event_attach(p_em, libvlc_MediaPlayerTimeChanged, HandleMediaTimeChanged, self);
  814. libvlc_event_attach(p_em, libvlc_MediaPlayerMediaChanged, HandleMediaPlayerMediaChanged, self);
  815. }
  816. - (void)unregisterObservers
  817. {
  818. libvlc_event_manager_t * p_em = libvlc_media_player_event_manager(instance);
  819. libvlc_event_detach(p_em, libvlc_MediaPlayerPlaying, HandleMediaInstanceStateChanged, self);
  820. libvlc_event_detach(p_em, libvlc_MediaPlayerPaused, HandleMediaInstanceStateChanged, self);
  821. libvlc_event_detach(p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, self);
  822. libvlc_event_detach(p_em, libvlc_MediaPlayerEndReached, HandleMediaInstanceStateChanged, self);
  823. libvlc_event_detach(p_em, libvlc_MediaPlayerOpening, HandleMediaInstanceStateChanged, self);
  824. libvlc_event_detach(p_em, libvlc_MediaPlayerBuffering, HandleMediaInstanceStateChanged, self);
  825. libvlc_event_detach(p_em, libvlc_MediaPlayerPositionChanged, HandleMediaPositionChanged, self);
  826. libvlc_event_detach(p_em, libvlc_MediaPlayerTimeChanged, HandleMediaTimeChanged, self);
  827. libvlc_event_detach(p_em, libvlc_MediaPlayerMediaChanged, HandleMediaPlayerMediaChanged, self);
  828. }
  829. - (void)mediaPlayerTimeChanged:(NSNumber *)newTime
  830. {
  831. [self willChangeValueForKey:@"time"];
  832. [self willChangeValueForKey:@"remainingTime"];
  833. [cachedTime release];
  834. cachedTime = [[VLCTime timeWithNumber:newTime] retain];
  835. [cachedRemainingTime release];
  836. double currentTime = [[cachedTime numberValue] doubleValue];
  837. double remaining = currentTime / position * (1 - position);
  838. cachedRemainingTime = [[VLCTime timeWithNumber:@(-remaining)] retain];
  839. [self didChangeValueForKey:@"remainingTime"];
  840. [self didChangeValueForKey:@"time"];
  841. }
  842. #if !TARGET_OS_IPHONE
  843. - (void)delaySleep
  844. {
  845. UpdateSystemActivity(UsrActivity);
  846. }
  847. #endif
  848. - (void)mediaPlayerPositionChanged:(NSNumber *)newPosition
  849. {
  850. #if !TARGET_OS_IPHONE
  851. // This seems to be the most relevant place to delay sleeping and screen saver.
  852. [self delaySleep];
  853. #endif
  854. [self willChangeValueForKey:@"position"];
  855. position = [newPosition floatValue];
  856. [self didChangeValueForKey:@"position"];
  857. }
  858. - (void)mediaPlayerStateChanged:(NSNumber *)newState
  859. {
  860. [self willChangeValueForKey:@"state"];
  861. cachedState = [newState intValue];
  862. #if TARGET_OS_IPHONE
  863. // Disable idle timer if player is playing media
  864. // Exclusion can be made for audio only media
  865. [UIApplication sharedApplication].idleTimerDisabled = [self isPlaying];
  866. #endif
  867. [self didChangeValueForKey:@"state"];
  868. }
  869. - (void)mediaPlayerMediaChanged:(VLCMedia *)newMedia
  870. {
  871. [self willChangeValueForKey:@"media"];
  872. if (media != newMedia)
  873. {
  874. [media release];
  875. media = [newMedia retain];
  876. }
  877. [self didChangeValueForKey:@"media"];
  878. }
  879. @end