VLCMediaPlayer.m 30 KB

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