12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145 |
- /*****************************************************************************
- * VLCMedia.m: VLCKit.framework VLCMedia implementation
- *****************************************************************************
- * Copyright (C) 2007 Pierre d'Herbemont
- * Copyright (C) 2013, 2017 Felix Paul Kühne
- * Copyright (C) 2007, 2013 VLC authors and VideoLAN
- * $Id$
- *
- * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
- * Felix Paul Kühne <fkuehne # videolan.org>
- * Soomin Lee <TheHungryBu # gmail.com>
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as published by
- * the Free Software Foundation; either version 2.1 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
- *****************************************************************************/
- #import "VLCMedia.h"
- #import "VLCMediaList.h"
- #import "VLCEventManager.h"
- #import "VLCLibrary.h"
- #import "VLCLibVLCBridging.h"
- #import <vlc/libvlc.h>
- #import <sys/sysctl.h> // for sysctlbyname
- /* Meta Dictionary Keys */
- NSString *const VLCMetaInformationTitle = @"title";
- NSString *const VLCMetaInformationArtist = @"artist";
- NSString *const VLCMetaInformationGenre = @"genre";
- NSString *const VLCMetaInformationCopyright = @"copyright";
- NSString *const VLCMetaInformationAlbum = @"album";
- NSString *const VLCMetaInformationTrackNumber = @"trackNumber";
- NSString *const VLCMetaInformationDescription = @"description";
- NSString *const VLCMetaInformationRating = @"rating";
- NSString *const VLCMetaInformationDate = @"date";
- NSString *const VLCMetaInformationSetting = @"setting";
- NSString *const VLCMetaInformationURL = @"url";
- NSString *const VLCMetaInformationLanguage = @"language";
- NSString *const VLCMetaInformationNowPlaying = @"nowPlaying";
- NSString *const VLCMetaInformationPublisher = @"publisher";
- NSString *const VLCMetaInformationEncodedBy = @"encodedBy";
- NSString *const VLCMetaInformationArtworkURL = @"artworkURL";
- NSString *const VLCMetaInformationArtwork = @"artwork";
- NSString *const VLCMetaInformationTrackID = @"trackID";
- NSString *const VLCMetaInformationTrackTotal = @"trackTotal";
- NSString *const VLCMetaInformationDirector = @"director";
- NSString *const VLCMetaInformationSeason = @"season";
- NSString *const VLCMetaInformationEpisode = @"episode";
- NSString *const VLCMetaInformationShowName = @"showName";
- NSString *const VLCMetaInformationActors = @"actors";
- NSString *const VLCMetaInformationAlbumArtist = @"AlbumArtist";
- NSString *const VLCMetaInformationDiscNumber = @"discNumber";
- /* Notification Messages */
- NSString *const VLCMediaMetaChanged = @"VLCMediaMetaChanged";
- /******************************************************************************
- * VLC callbacks for streaming.
- */
- int open_cb(void *opaque, void **datap, uint64_t *sizep) {
- NSInputStream *stream = (__bridge NSInputStream *)(opaque);
-
- *datap = opaque;
- *sizep = UINT64_MAX;
-
- // Once a stream is closed, it cannot be reopened.
- if (stream && stream.streamStatus == NSStreamStatusNotOpen) {
- [stream open];
- return 0;
- } else {
- return stream.streamStatus == NSStreamStatusOpen ? 0 : -1;
- }
- }
- ssize_t read_cb(void *opaque, unsigned char *buf, size_t len) {
- NSInputStream *stream = (__bridge NSInputStream *)(opaque);
- if (!stream) {
- return -1;
- }
-
- return [stream read:buf maxLength:len];
- }
- int seek_cb(void *opaque, uint64_t offset) {
- NSInputStream *stream = (__bridge NSInputStream *)(opaque);
- if (!stream) {
- return -1;
- }
-
- /*
- By default, NSStream instances that are not file-based are non-seekable, one-way streams (although custom seekable subclasses are possible).
- Once the data has been provided or consumed, the data cannot be retrieved from the stream.
-
- However, you may want a peer subclass to NSInputStream whose instances are capable of seeking through a stream.
- */
- return [stream setProperty:@(offset) forKey:NSStreamFileCurrentOffsetKey] ? 0 : -1;
- }
- void close_cb(void *opaque) {
- NSInputStream *stream = (__bridge NSInputStream *)(opaque);
- if (stream && stream.streamStatus != NSStreamStatusClosed && stream.streamStatus != NSStreamStatusNotOpen) {
- [stream close];
- }
- return;
- }
- /******************************************************************************
- * VLCMedia ()
- */
- @interface VLCMedia()
- {
- void * p_md; ///< Internal media descriptor instance
- BOOL isArtFetched; ///< Value used to determine of the artwork has been parsed
- BOOL areOthersMetaFetched; ///< Value used to determine of the other meta has been parsed
- BOOL isArtURLFetched; ///< Value used to determine of the other meta has been preparsed
- BOOL eventsAttached; ///< YES when events are attached
- NSMutableDictionary *_metaDictionary; ///< Dictionary to cache metadata read from libvlc
- NSInputStream *stream; ///< Stream object if instance is initialized via NSInputStream to pass to callbacks
- }
- /* Make our properties internally readwrite */
- @property (nonatomic, readwrite) VLCMediaState state;
- @property (nonatomic, readwrite, strong) VLCMediaList * subitems;
- /* Statics */
- + (libvlc_meta_t)stringToMetaType:(NSString *)string;
- + (NSString *)metaTypeToString:(libvlc_meta_t)type;
- /* Initializers */
- - (void)initInternalMediaDescriptor;
- /* Operations */
- - (void)fetchMetaInformationFromLibVLCWithType:(NSString*)metaType;
- #if !TARGET_OS_IPHONE
- - (void)fetchMetaInformationForArtWorkWithURL:(NSString *)anURL;
- - (void)setArtwork:(NSImage *)art;
- #endif
- - (void)parseIfNeeded;
- /* Callback Methods */
- - (void)parsedChanged:(NSNumber *)isParsedAsNumber;
- - (void)metaChanged:(NSString *)metaType;
- - (void)subItemAdded;
- - (void)setStateAsNumber:(NSNumber *)newStateAsNumber;
- @end
- static VLCMediaState libvlc_state_to_media_state[] =
- {
- [libvlc_NothingSpecial] = VLCMediaStateNothingSpecial,
- [libvlc_Stopped] = VLCMediaStateNothingSpecial,
- [libvlc_Opening] = VLCMediaStateNothingSpecial,
- [libvlc_Buffering] = VLCMediaStateBuffering,
- [libvlc_Ended] = VLCMediaStateNothingSpecial,
- [libvlc_Error] = VLCMediaStateError,
- [libvlc_Playing] = VLCMediaStatePlaying,
- [libvlc_Paused] = VLCMediaStatePlaying,
- };
- static inline VLCMediaState LibVLCStateToMediaState( libvlc_state_t state )
- {
- return libvlc_state_to_media_state[state];
- }
- /******************************************************************************
- * LibVLC Event Callback
- */
- static void HandleMediaMetaChanged(const libvlc_event_t * event, void * self)
- {
- @autoreleasepool {
- [[VLCEventManager sharedManager] callOnMainThreadObject:(__bridge id)(self)
- withMethod:@selector(metaChanged:)
- withArgumentAsObject:[VLCMedia metaTypeToString:event->u.media_meta_changed.meta_type]];
- }
- }
- static void HandleMediaDurationChanged(const libvlc_event_t * event, void * self)
- {
- @autoreleasepool {
- [[VLCEventManager sharedManager] callOnMainThreadObject:(__bridge id)(self)
- withMethod:@selector(setLength:)
- withArgumentAsObject:[VLCTime timeWithNumber:
- @(event->u.media_duration_changed.new_duration)]];
- }
- }
- static void HandleMediaStateChanged(const libvlc_event_t * event, void * self)
- {
- @autoreleasepool {
- [[VLCEventManager sharedManager] callOnMainThreadObject:(__bridge id)(self)
- withMethod:@selector(setStateAsNumber:)
- withArgumentAsObject:@(LibVLCStateToMediaState(event->u.media_state_changed.new_state))];
- }
- }
- static void HandleMediaSubItemAdded(const libvlc_event_t * event, void * self)
- {
- @autoreleasepool {
- [[VLCEventManager sharedManager] callOnMainThreadObject:(__bridge id)(self)
- withMethod:@selector(subItemAdded)
- withArgumentAsObject:nil];
- }
- }
- static void HandleMediaParsedChanged(const libvlc_event_t * event, void * self)
- {
- @autoreleasepool {
- [[VLCEventManager sharedManager] callOnMainThreadObject:(__bridge id)(self)
- withMethod:@selector(parsedChanged:)
- withArgumentAsObject:@((BOOL)event->u.media_parsed_changed.new_status)];
- }
- }
- /******************************************************************************
- * Implementation
- */
- @implementation VLCMedia
- + (NSString *)codecNameForFourCC:(uint32_t)fourcc trackType:(NSString *)trackType
- {
- libvlc_track_type_t track_type = libvlc_track_unknown;
- if ([trackType isEqualToString:VLCMediaTracksInformationTypeAudio])
- track_type = libvlc_track_audio;
- else if ([trackType isEqualToString:VLCMediaTracksInformationTypeVideo])
- track_type = libvlc_track_video;
- else if ([trackType isEqualToString:VLCMediaTracksInformationTypeText])
- track_type = libvlc_track_text;
- const char *ret = libvlc_media_get_codec_description(track_type, fourcc);
- if (ret)
- return [NSString stringWithUTF8String:ret];
- return @"";
- }
- + (instancetype)mediaWithURL:(NSURL *)anURL;
- {
- return [[VLCMedia alloc] initWithURL:anURL];
- }
- + (instancetype)mediaWithPath:(NSString *)aPath;
- {
- return [[VLCMedia alloc] initWithPath:aPath];
- }
- + (instancetype)mediaAsNodeWithName:(NSString *)aName;
- {
- return [[VLCMedia alloc] initAsNodeWithName:aName];
- }
- - (instancetype)initWithPath:(NSString *)aPath
- {
- return [self initWithURL:[NSURL fileURLWithPath:aPath isDirectory:NO]];
- }
- - (instancetype)initWithURL:(NSURL *)anURL
- {
- if (self = [super init]) {
- const char *url;
- VLCLibrary *library = [VLCLibrary sharedLibrary];
- NSAssert(library.instance, @"no library instance when creating media");
- url = [[anURL absoluteString] UTF8String];
- p_md = libvlc_media_new_location(library.instance, url);
- _metaDictionary = [[NSMutableDictionary alloc] initWithCapacity:3];
- [self initInternalMediaDescriptor];
- }
- return self;
- }
- - (instancetype)initWithStream:(NSInputStream *)stream
- {
- if (self = [super init]) {
- VLCLibrary *library = [VLCLibrary sharedLibrary];
- NSAssert(library.instance, @"no library instance when creating media");
- NSAssert(stream.streamStatus != NSStreamStatusClosed, @"Passing closed stream to VLCMedia.init does not work");
-
- self->stream = stream;
- p_md = libvlc_media_new_callbacks(library.instance, open_cb, read_cb, seek_cb, close_cb, (__bridge void *)(stream));
-
- _metaDictionary = [[NSMutableDictionary alloc] initWithCapacity:3];
-
- [self initInternalMediaDescriptor];
- }
- return self;
- }
- - (instancetype)initAsNodeWithName:(NSString *)aName
- {
- if (self = [super init]) {
- p_md = libvlc_media_new_as_node([VLCLibrary sharedInstance], [aName UTF8String]);
- _metaDictionary = [[NSMutableDictionary alloc] initWithCapacity:3];
- [self initInternalMediaDescriptor];
- }
- return self;
- }
- - (void)dealloc
- {
- if (eventsAttached)
- {
- libvlc_event_manager_t * p_em = libvlc_media_event_manager(p_md);
- if (p_em) {
- libvlc_event_detach(p_em, libvlc_MediaMetaChanged, HandleMediaMetaChanged, (__bridge void *)(self));
- libvlc_event_detach(p_em, libvlc_MediaDurationChanged, HandleMediaDurationChanged, (__bridge void *)(self));
- libvlc_event_detach(p_em, libvlc_MediaStateChanged, HandleMediaStateChanged, (__bridge void *)(self));
- libvlc_event_detach(p_em, libvlc_MediaSubItemAdded, HandleMediaSubItemAdded, (__bridge void *)(self));
- libvlc_event_detach(p_em, libvlc_MediaParsedChanged, HandleMediaParsedChanged, (__bridge void *)(self));
- }
- }
- [[VLCEventManager sharedManager] cancelCallToObject:self];
- if (p_md)
- libvlc_media_release(p_md);
- }
- - (VLCMediaType)mediaType
- {
- libvlc_media_type_t libmediatype = libvlc_media_get_type(p_md);
- switch (libmediatype) {
- case libvlc_media_type_file:
- return VLCMediaTypeFile;
- case libvlc_media_type_directory:
- return VLCMediaTypeDirectory;
- case libvlc_media_type_disc:
- return VLCMediaTypeDisc;
- case libvlc_media_type_stream:
- return VLCMediaTypeStream;
- case libvlc_media_type_playlist:
- return VLCMediaTypePlaylist;
- default:
- return VLCMediaTypeUnknown;
- }
- }
- - (NSString *)description
- {
- return [NSString stringWithFormat:@"<%@ %p>, md: %p, url: %@", [self class], self, p_md, [[_url absoluteString] stringByRemovingPercentEncoding]];
- }
- - (NSComparisonResult)compare:(VLCMedia *)media
- {
- if (self == media)
- return NSOrderedSame;
- if (!media)
- return NSOrderedDescending;
- return p_md == [media libVLCMediaDescriptor] ? NSOrderedSame : NSOrderedAscending;
- }
- - (BOOL)isEqual:(id)other
- {
- return ([other isKindOfClass: [VLCMedia class]] &&
- [other libVLCMediaDescriptor] == p_md);
- }
- - (VLCTime *)length
- {
- if (!_length) {
- // Try figuring out what the length is
- long long duration = libvlc_media_get_duration( p_md );
- if (duration < 0)
- return [VLCTime nullTime];
- _length = [VLCTime timeWithNumber:@(duration)];
- }
- return _length;
- }
- - (VLCTime *)lengthWaitUntilDate:(NSDate *)aDate
- {
- static const long long thread_sleep = 10000;
- if (!_length) {
- // Force parsing of this item.
- [self parseIfNeeded];
- // wait until we are preparsed
- libvlc_media_parsed_status_t status = libvlc_media_get_parsed_status(p_md);
- while (!_length && !(status == VLCMediaParsedStatusFailed || status == VLCMediaParsedStatusDone) && [aDate timeIntervalSinceNow] > 0) {
- usleep( thread_sleep );
- status = libvlc_media_get_parsed_status(p_md);
- }
- // So we're done waiting, but sometimes we trap the fact that the parsing
- // was done before the length gets assigned, so lets go ahead and assign
- // it ourselves.
- if (!_length)
- return [self length];
- }
- return _length;
- }
- - (VLCMediaParsedStatus)parsedStatus
- {
- if (!p_md)
- return VLCMediaParsedStatusFailed;
- libvlc_media_parsed_status_t status = libvlc_media_get_parsed_status(p_md);
- return (VLCMediaParsedStatus)status;
- }
- - (int)parseWithOptions:(VLCMediaParsingOptions)options timeout:(int)timeoutValue
- {
- if (!p_md)
- return -1;
- // we are using the default time-out value
- return libvlc_media_parse_with_options(p_md,
- options,
- timeoutValue);
- }
- - (int)parseWithOptions:(VLCMediaParsingOptions)options
- {
- if (!p_md)
- return -1;
- // we are using the default time-out value
- return libvlc_media_parse_with_options(p_md,
- options,
- -1);
- }
- - (void)parseStop
- {
- if (p_md) {
- libvlc_media_parse_stop(p_md);
- }
- }
- - (void)addOption:(NSString *)option
- {
- if (p_md) {
- libvlc_media_add_option(p_md, [option UTF8String]);
- }
- }
- - (void)addOptions:(NSDictionary*)options
- {
- if (p_md) {
- [options enumerateKeysAndObjectsUsingBlock:^(NSString *key, id obj, BOOL *stop) {
- if (![obj isKindOfClass:[NSNull class]])
- libvlc_media_add_option(p_md, [[NSString stringWithFormat:@"%@=%@", key, obj] UTF8String]);
- else
- libvlc_media_add_option(p_md, [key UTF8String]);
- }];
- }
- }
- - (int)storeCookie:(NSString * _Nonnull)cookie
- forHost:(NSString *_Nonnull)host
- path:(NSString *_Nonnull)path
- {
- if (!p_md || cookie == NULL || host == NULL || path == NULL) {
- return -1;
- }
- #if TARGET_OS_IPHONE
- return libvlc_media_cookie_jar_store(p_md,
- [cookie UTF8String],
- [host UTF8String],
- [path UTF8String]);
- #else
- return -1;
- #endif
- }
- - (void)clearStoredCookies
- {
- if (!p_md) {
- return;
- }
- #if TARGET_OS_IPHONE
- libvlc_media_cookie_jar_clear(p_md);
- #endif
- }
- - (NSDictionary *)stats
- {
- if (!p_md)
- return nil;
- libvlc_media_stats_t p_stats;
- libvlc_media_get_stats(p_md, &p_stats);
- return @{
- @"demuxBitrate" : @(p_stats.f_demux_bitrate),
- @"inputBitrate" : @(p_stats.f_input_bitrate),
- @"decodedAudio" : @(p_stats.i_decoded_audio),
- @"decodedVideo" : @(p_stats.i_decoded_video),
- @"demuxCorrupted" : @(p_stats.i_demux_corrupted),
- @"demuxDiscontinuity" : @(p_stats.i_demux_discontinuity),
- @"demuxReadBytes" : @(p_stats.i_demux_read_bytes),
- @"displayedPictures" : @(p_stats.i_displayed_pictures),
- @"lostAbuffers" : @(p_stats.i_lost_abuffers),
- @"lostPictures" : @(p_stats.i_lost_pictures),
- @"playedAbuffers" : @(p_stats.i_played_abuffers),
- @"readBytes" : @(p_stats.i_read_bytes)
- };
- }
- - (NSInteger)numberOfReadBytesOnInput
- {
- if (!p_md)
- return 0;
- libvlc_media_stats_t p_stats;
- libvlc_media_get_stats(p_md, &p_stats);
- return p_stats.i_read_bytes;
- }
- - (float)inputBitrate
- {
- if (!p_md)
- return .0;
- libvlc_media_stats_t p_stats;
- libvlc_media_get_stats(p_md, &p_stats);
- return p_stats.f_input_bitrate;
- }
- - (NSInteger)numberOfReadBytesOnDemux
- {
- if (!p_md)
- return 0;
- libvlc_media_stats_t p_stats;
- libvlc_media_get_stats(p_md, &p_stats);
- return p_stats.i_demux_read_bytes;
- }
- - (float)demuxBitrate
- {
- if (!p_md)
- return .0;
- libvlc_media_stats_t p_stats;
- libvlc_media_get_stats(p_md, &p_stats);
- return p_stats.f_demux_bitrate;
- }
- - (NSInteger)numberOfDecodedVideoBlocks
- {
- if (!p_md)
- return 0;
- libvlc_media_stats_t p_stats;
- libvlc_media_get_stats(p_md, &p_stats);
- return p_stats.i_decoded_video;
- }
- - (NSInteger)numberOfDecodedAudioBlocks
- {
- if (!p_md)
- return 0;
- libvlc_media_stats_t p_stats;
- libvlc_media_get_stats(p_md, &p_stats);
- return p_stats.i_decoded_audio;
- }
- - (NSInteger)numberOfDisplayedPictures
- {
- if (!p_md)
- return 0;
- libvlc_media_stats_t p_stats;
- libvlc_media_get_stats(p_md, &p_stats);
- return p_stats.i_displayed_pictures;
- }
- - (NSInteger)numberOfLostPictures
- {
- if (!p_md)
- return 0;
- libvlc_media_stats_t p_stats;
- libvlc_media_get_stats(p_md, &p_stats);
- return p_stats.i_lost_pictures;
- }
- - (NSInteger)numberOfPlayedAudioBuffers
- {
- if (!p_md)
- return 0;
- libvlc_media_stats_t p_stats;
- libvlc_media_get_stats(p_md, &p_stats);
- return p_stats.i_played_abuffers;
- }
- - (NSInteger)numberOfLostAudioBuffers
- {
- if (!p_md)
- return 0;
- libvlc_media_stats_t p_stats;
- libvlc_media_get_stats(p_md, &p_stats);
- return p_stats.i_lost_abuffers;
- }
- - (NSInteger)numberOfCorruptedDataPackets
- {
- if (!p_md)
- return 0;
- libvlc_media_stats_t p_stats;
- libvlc_media_get_stats(p_md, &p_stats);
- return p_stats.i_demux_corrupted;
- }
- - (NSInteger)numberOfDiscontinuties
- {
- if (!p_md)
- return 0;
- libvlc_media_stats_t p_stats;
- libvlc_media_get_stats(p_md, &p_stats);
- return p_stats.i_demux_discontinuity;
- }
- NSString *const VLCMediaTracksInformationCodec = @"codec"; // NSNumber
- NSString *const VLCMediaTracksInformationId = @"id"; // NSNumber
- NSString *const VLCMediaTracksInformationType = @"type"; // NSString
- NSString *const VLCMediaTracksInformationCodecProfile = @"profile"; // NSNumber
- NSString *const VLCMediaTracksInformationCodecLevel = @"level"; // NSNumber
- NSString *const VLCMediaTracksInformationTypeAudio = @"audio";
- NSString *const VLCMediaTracksInformationTypeVideo = @"video";
- NSString *const VLCMediaTracksInformationTypeText = @"text";
- NSString *const VLCMediaTracksInformationTypeUnknown = @"unknown";
- NSString *const VLCMediaTracksInformationBitrate = @"bitrate"; // NSNumber
- NSString *const VLCMediaTracksInformationLanguage = @"language"; // NSString
- NSString *const VLCMediaTracksInformationDescription = @"description"; // NSString
- NSString *const VLCMediaTracksInformationAudioChannelsNumber = @"channelsNumber"; // NSNumber
- NSString *const VLCMediaTracksInformationAudioRate = @"rate"; // NSNumber
- NSString *const VLCMediaTracksInformationVideoHeight = @"height"; // NSNumber
- NSString *const VLCMediaTracksInformationVideoWidth = @"width"; // NSNumber
- NSString *const VLCMediaTracksInformationVideoOrientation = @"orientation"; // NSNumber
- NSString *const VLCMediaTracksInformationVideoProjection = @"projection"; // NSNumber
- NSString *const VLCMediaTracksInformationSourceAspectRatio = @"sar_num"; // NSNumber
- NSString *const VLCMediaTracksInformationSourceAspectRatioDenominator = @"sar_den"; // NSNumber
- NSString *const VLCMediaTracksInformationFrameRate = @"frame_rate_num"; // NSNumber
- NSString *const VLCMediaTracksInformationFrameRateDenominator = @"frame_rate_den"; // NSNumber
- NSString *const VLCMediaTracksInformationTextEncoding = @"encoding"; // NSString
- - (NSArray *)tracksInformation
- {
- libvlc_media_track_t **tracksInfo;
- unsigned int count = libvlc_media_tracks_get(p_md, &tracksInfo);
- NSMutableArray *array = [NSMutableArray array];
- for (NSUInteger i = 0; i < count; i++) {
- NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
- @(tracksInfo[i]->i_codec),
- VLCMediaTracksInformationCodec,
- @(tracksInfo[i]->i_id),
- VLCMediaTracksInformationId,
- @(tracksInfo[i]->i_profile),
- VLCMediaTracksInformationCodecProfile,
- @(tracksInfo[i]->i_level),
- VLCMediaTracksInformationCodecLevel,
- @(tracksInfo[i]->i_bitrate),
- VLCMediaTracksInformationBitrate,
- nil];
- if (tracksInfo[i]->psz_language)
- dictionary[VLCMediaTracksInformationLanguage] = [NSString stringWithUTF8String:tracksInfo[i]->psz_language];
- if (tracksInfo[i]->psz_description)
- dictionary[VLCMediaTracksInformationDescription] = [NSString stringWithUTF8String:tracksInfo[i]->psz_description];
- NSString *type;
- switch (tracksInfo[i]->i_type) {
- case libvlc_track_audio:
- type = VLCMediaTracksInformationTypeAudio;
- dictionary[VLCMediaTracksInformationAudioChannelsNumber] = @(tracksInfo[i]->audio->i_channels);
- dictionary[VLCMediaTracksInformationAudioRate] = @(tracksInfo[i]->audio->i_rate);
- break;
- case libvlc_track_video:
- type = VLCMediaTracksInformationTypeVideo;
- dictionary[VLCMediaTracksInformationVideoWidth] = @(tracksInfo[i]->video->i_width);
- dictionary[VLCMediaTracksInformationVideoHeight] = @(tracksInfo[i]->video->i_height);
- dictionary[VLCMediaTracksInformationVideoOrientation] = @(tracksInfo[i]->video->i_orientation);
- dictionary[VLCMediaTracksInformationVideoProjection] = @(tracksInfo[i]->video->i_projection);
- dictionary[VLCMediaTracksInformationSourceAspectRatio] = @(tracksInfo[i]->video->i_sar_num);
- dictionary[VLCMediaTracksInformationSourceAspectRatioDenominator] = @(tracksInfo[i]->video->i_sar_den);
- dictionary[VLCMediaTracksInformationFrameRate] = @(tracksInfo[i]->video->i_frame_rate_num);
- dictionary[VLCMediaTracksInformationFrameRateDenominator] = @(tracksInfo[i]->video->i_frame_rate_den);
- break;
- case libvlc_track_text:
- type = VLCMediaTracksInformationTypeText;
- if (tracksInfo[i]->subtitle->psz_encoding)
- dictionary[VLCMediaTracksInformationTextEncoding] = [NSString stringWithUTF8String: tracksInfo[i]->subtitle->psz_encoding];
- break;
- case libvlc_track_unknown:
- default:
- type = VLCMediaTracksInformationTypeUnknown;
- break;
- }
- [dictionary setValue:type forKey:VLCMediaTracksInformationType];
- [array addObject:dictionary];
- }
- libvlc_media_tracks_release(tracksInfo, count);
- return array;
- }
- - (BOOL)isMediaSizeSuitableForDevice
- {
- #if TARGET_OS_IPHONE
- // Trigger parsing if needed
- VLCMediaParsedStatus parsedStatus = [self parsedStatus];
- if (parsedStatus == VLCMediaParsedStatusSkipped || parsedStatus == VLCMediaParsedStatusInit) {
- [self parseWithOptions:VLCMediaParseLocal|VLCMediaParseNetwork];
- sleep(2);
- }
- NSUInteger biggestWidth = 0;
- NSUInteger biggestHeight = 0;
- libvlc_media_track_t **tracksInfo;
- unsigned int count = libvlc_media_tracks_get(p_md, &tracksInfo);
- for (NSUInteger i = 0; i < count; i++) {
- switch (tracksInfo[i]->i_type) {
- case libvlc_track_video:
- if (tracksInfo[i]->video->i_width > biggestWidth)
- biggestWidth = tracksInfo[i]->video->i_width;
- if (tracksInfo[i]->video->i_height > biggestHeight)
- biggestHeight = tracksInfo[i]->video->i_height;
- break;
- default:
- break;
- }
- }
- if (biggestHeight > 0 && biggestWidth > 0) {
- size_t size;
- sysctlbyname("hw.machine", NULL, &size, NULL, 0);
- char *answer = malloc(size);
- sysctlbyname("hw.machine", answer, &size, NULL, 0);
- NSString *currentMachine = @(answer);
- free(answer);
- NSUInteger totalNumberOfPixels = biggestWidth * biggestHeight;
- if ([currentMachine hasPrefix:@"iPhone2"] || [currentMachine hasPrefix:@"iPhone3"] || [currentMachine hasPrefix:@"iPad1"] || [currentMachine hasPrefix:@"iPod3"] || [currentMachine hasPrefix:@"iPod4"]) {
- // iPhone 3GS, iPhone 4, first gen. iPad, 3rd and 4th generation iPod touch
- return (totalNumberOfPixels < 600000); // between 480p and 720p
- } else if ([currentMachine hasPrefix:@"iPhone4"] || [currentMachine hasPrefix:@"iPad3,1"] || [currentMachine hasPrefix:@"iPad3,2"] || [currentMachine hasPrefix:@"iPad3,3"] || [currentMachine hasPrefix:@"iPod4"] || [currentMachine hasPrefix:@"iPad2"] || [currentMachine hasPrefix:@"iPod5"]) {
- // iPhone 4S, iPad 2 and 3, iPod 4 and 5
- return (totalNumberOfPixels < 922000); // 720p
- } else {
- // iPhone 5, iPad 4
- return (totalNumberOfPixels < 2074000); // 1080p
- }
- }
- #endif
- return YES;
- }
- - (NSString *)metadataForKey:(NSString *)key
- {
- if (!p_md)
- return nil;
- char *returnValue = libvlc_media_get_meta(p_md, [VLCMedia stringToMetaType:key]);
- if (!returnValue)
- return nil;
- NSString *actualReturnValue = @(returnValue);
- free(returnValue);
- return actualReturnValue;
- }
- - (void)setMetadata:(NSString *)data forKey:(NSString *)key
- {
- if (!p_md)
- return;
- libvlc_media_set_meta(p_md, [VLCMedia stringToMetaType:key], [data UTF8String]);
- }
- - (BOOL)saveMetadata
- {
- if (p_md)
- return libvlc_media_save_meta(p_md) != 0;
- return NO;
- }
- /******************************************************************************
- * Implementation VLCMedia ()
- */
- + (libvlc_meta_t)stringToMetaType:(NSString *)string
- {
- static NSDictionary * stringToMetaDictionary = nil;
- // TODO: Thread safe-ize
- if (!stringToMetaDictionary) {
- #define VLCStringToMeta( name ) [NSNumber numberWithInt: libvlc_meta_##name], VLCMetaInformation##name
- stringToMetaDictionary =
- [NSDictionary dictionaryWithObjectsAndKeys:
- VLCStringToMeta(Title),
- VLCStringToMeta(Artist),
- VLCStringToMeta(Genre),
- VLCStringToMeta(Copyright),
- VLCStringToMeta(Album),
- VLCStringToMeta(TrackNumber),
- VLCStringToMeta(Description),
- VLCStringToMeta(Rating),
- VLCStringToMeta(Date),
- VLCStringToMeta(Setting),
- VLCStringToMeta(URL),
- VLCStringToMeta(Language),
- VLCStringToMeta(NowPlaying),
- VLCStringToMeta(Publisher),
- VLCStringToMeta(ArtworkURL),
- VLCStringToMeta(TrackID),
- VLCStringToMeta(TrackTotal),
- VLCStringToMeta(Director),
- VLCStringToMeta(Season),
- VLCStringToMeta(Episode),
- VLCStringToMeta(ShowName),
- VLCStringToMeta(Actors),
- VLCStringToMeta(AlbumArtist),
- VLCStringToMeta(DiscNumber),
- nil];
- #undef VLCStringToMeta
- }
- NSNumber * number = stringToMetaDictionary[string];
- return (libvlc_meta_t) (number ? [number intValue] : -1);
- }
- + (NSString *)metaTypeToString:(libvlc_meta_t)type
- {
- #define VLCMetaToString( name, type ) if (libvlc_meta_##name == type) return VLCMetaInformation##name;
- VLCMetaToString(Title, type);
- VLCMetaToString(Artist, type);
- VLCMetaToString(Genre, type);
- VLCMetaToString(Copyright, type);
- VLCMetaToString(Album, type);
- VLCMetaToString(TrackNumber, type);
- VLCMetaToString(Description, type);
- VLCMetaToString(Rating, type);
- VLCMetaToString(Date, type);
- VLCMetaToString(Setting, type);
- VLCMetaToString(URL, type);
- VLCMetaToString(Language, type);
- VLCMetaToString(NowPlaying, type);
- VLCMetaToString(Publisher, type);
- VLCMetaToString(ArtworkURL, type);
- VLCMetaToString(TrackID, type);
- VLCMetaToString(TrackTotal, type);
- VLCMetaToString(Director, type);
- VLCMetaToString(Season, type);
- VLCMetaToString(Episode, type);
- VLCMetaToString(ShowName, type);
- VLCMetaToString(Actors, type);
- VLCMetaToString(AlbumArtist, type);
- VLCMetaToString(DiscNumber, type);
- #undef VLCMetaToString
- return nil;
- }
- - (void)initInternalMediaDescriptor
- {
- char * p_url = libvlc_media_get_mrl( p_md );
- if (!p_url)
- return;
- NSString *urlString = [NSString stringWithUTF8String:p_url];
- if (!urlString) {
- free(p_url);
- return;
- }
- _url = [NSURL URLWithString:urlString];
- if (!_url) /* Attempt to interpret as a file path then */ {
- _url = [NSURL fileURLWithPath:urlString];
- if(!_url) {
- free(p_url);
- return;
- }
- }
- free(p_url);
- libvlc_media_set_user_data(p_md, (__bridge void*)self);
- libvlc_event_manager_t * p_em = libvlc_media_event_manager( p_md );
- if (p_em) {
- libvlc_event_attach(p_em, libvlc_MediaMetaChanged, HandleMediaMetaChanged, (__bridge void *)(self));
- libvlc_event_attach(p_em, libvlc_MediaDurationChanged, HandleMediaDurationChanged, (__bridge void *)(self));
- libvlc_event_attach(p_em, libvlc_MediaStateChanged, HandleMediaStateChanged, (__bridge void *)(self));
- libvlc_event_attach(p_em, libvlc_MediaSubItemAdded, HandleMediaSubItemAdded, (__bridge void *)(self));
- libvlc_event_attach(p_em, libvlc_MediaParsedChanged, HandleMediaParsedChanged, (__bridge void *)(self));
- eventsAttached = YES;
- }
- libvlc_media_list_t * p_mlist = libvlc_media_subitems( p_md );
- if (p_mlist) {
- self.subitems = [VLCMediaList mediaListWithLibVLCMediaList:p_mlist];
- libvlc_media_list_release( p_mlist );
- }
- self.state = LibVLCStateToMediaState(libvlc_media_get_state( p_md ));
- }
- - (void)fetchMetaInformationFromLibVLCWithType:(NSString *)metaType
- {
- char * psz_value = libvlc_media_get_meta( p_md, [VLCMedia stringToMetaType:metaType] );
- NSString * newValue = psz_value ? @(psz_value) : nil;
- NSString * oldValue = [_metaDictionary valueForKey:metaType];
- free(psz_value);
- if (newValue != oldValue && !(oldValue && newValue && [oldValue compare:newValue] == NSOrderedSame)) {
- #if !TARGET_OS_IPHONE
- // Only fetch the art if needed. (ie, create the NSImage, if it was requested before)
- if (isArtFetched && [metaType isEqualToString:VLCMetaInformationArtworkURL]) {
- [NSThread detachNewThreadSelector:@selector(fetchMetaInformationForArtWorkWithURL:)
- toTarget:self
- withObject:newValue];
- }
- #endif
- [_metaDictionary setValue:newValue forKeyPath:metaType];
- }
- }
- #if !TARGET_OS_IPHONE
- - (void)fetchMetaInformationForArtWorkWithURL:(NSString *)anURL
- {
- @autoreleasepool {
- NSImage * art = nil;
- if (anURL) {
- // Go ahead and load up the art work
- NSURL * artUrl = [NSURL URLWithString:[anURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
- // Don't attempt to fetch artwork from remote. Core will do that alone
- if ([artUrl isFileURL])
- art = [[NSImage alloc] initWithContentsOfURL:artUrl];
- }
- // If anything was found, lets save it to the meta data dictionary
- [self performSelectorOnMainThread:@selector(setArtwork:) withObject:art waitUntilDone:NO];
- }
- }
- - (void)setArtwork:(NSImage *)art
- {
- if (!art)
- [(NSMutableDictionary *)_metaDictionary removeObjectForKey:@"artwork"];
- else
- ((NSMutableDictionary *)_metaDictionary)[@"artwork"] = art;
- }
- #endif
- - (void)parseIfNeeded
- {
- VLCMediaParsedStatus parsedStatus = [self parsedStatus];
- if (parsedStatus == VLCMediaParsedStatusSkipped || parsedStatus == VLCMediaParsedStatusInit)
- [self parseWithOptions:VLCMediaParseLocal | VLCMediaFetchLocal];
- }
- - (void)metaChanged:(NSString *)metaType
- {
- [self fetchMetaInformationFromLibVLCWithType:metaType];
- if ([_delegate respondsToSelector:@selector(mediaMetaDataDidChange:)])
- [_delegate mediaMetaDataDidChange:self];
- }
- - (void)subItemAdded
- {
- if (_subitems)
- return; /* Nothing to do */
- libvlc_media_list_t * p_mlist = libvlc_media_subitems( p_md );
- NSAssert( p_mlist, @"The mlist shouldn't be nil, we are receiving a subItemAdded");
- self.subitems = [VLCMediaList mediaListWithLibVLCMediaList:p_mlist];
- libvlc_media_list_release( p_mlist );
- }
- - (void)parsedChanged:(NSNumber *)isParsedAsNumber
- {
- [self willChangeValueForKey:@"parsedStatus"];
- [self parsedStatus];
- [self didChangeValueForKey:@"parsedStatus"];
- if (!_delegate)
- return;
- if ([_delegate respondsToSelector:@selector(mediaDidFinishParsing:)])
- [_delegate mediaDidFinishParsing:self];
- }
- - (void)setStateAsNumber:(NSNumber *)newStateAsNumber
- {
- [self setState: [newStateAsNumber intValue]];
- }
- #if TARGET_OS_IPHONE
- - (NSDictionary *)metaDictionary
- {
- if (!areOthersMetaFetched) {
- areOthersMetaFetched = YES;
- [self fetchMetaInformationFromLibVLCWithType: VLCMetaInformationTitle];
- [self fetchMetaInformationFromLibVLCWithType: VLCMetaInformationArtist];
- [self fetchMetaInformationFromLibVLCWithType: VLCMetaInformationAlbum];
- [self fetchMetaInformationFromLibVLCWithType: VLCMetaInformationDate];
- [self fetchMetaInformationFromLibVLCWithType: VLCMetaInformationGenre];
- [self fetchMetaInformationFromLibVLCWithType: VLCMetaInformationTrackNumber];
- [self fetchMetaInformationFromLibVLCWithType: VLCMetaInformationDiscNumber];
- [self fetchMetaInformationFromLibVLCWithType: VLCMetaInformationNowPlaying];
- [self fetchMetaInformationFromLibVLCWithType: VLCMetaInformationLanguage];
- }
- if (!isArtURLFetched) {
- isArtURLFetched = YES;
- /* Force isArtURLFetched, that will trigger artwork download eventually
- * And all the other meta will be added through the libvlc event system */
- [self fetchMetaInformationFromLibVLCWithType: VLCMetaInformationArtworkURL];
- }
- return [NSDictionary dictionaryWithDictionary:_metaDictionary];
- }
- #else
- - (NSDictionary *)metaDictionary
- {
- return [NSDictionary dictionaryWithDictionary:_metaDictionary];
- }
- - (id)valueForKeyPath:(NSString *)keyPath
- {
- if (!isArtFetched && [keyPath isEqualToString:@"metaDictionary.artwork"]) {
- isArtFetched = YES;
- /* Force the retrieval of the artwork now that someone asked for it */
- [self fetchMetaInformationFromLibVLCWithType: VLCMetaInformationArtworkURL];
- } else if (!areOthersMetaFetched && [keyPath hasPrefix:@"metaDictionary."]) {
- areOthersMetaFetched = YES;
- /* Force VLCMetaInformationTitle, that will trigger preparsing
- * And all the other meta will be added through the libvlc event system */
- [self fetchMetaInformationFromLibVLCWithType: VLCMetaInformationTitle];
- } else if (!isArtURLFetched && [keyPath hasPrefix:@"metaDictionary.artworkURL"]) {
- isArtURLFetched = YES;
- /* Force isArtURLFetched, that will trigger artwork download eventually
- * And all the other meta will be added through the libvlc event system */
- [self fetchMetaInformationFromLibVLCWithType: VLCMetaInformationArtworkURL];
- }
- return [super valueForKeyPath:keyPath];
- }
- #endif
- @end
- /******************************************************************************
- * Implementation VLCMedia (LibVLCBridging)
- */
- @implementation VLCMedia (LibVLCBridging)
- + (id)mediaWithLibVLCMediaDescriptor:(void *)md
- {
- return [[VLCMedia alloc] initWithLibVLCMediaDescriptor:md];
- }
- + (id)mediaWithMedia:(VLCMedia *)media andLibVLCOptions:(NSDictionary *)options
- {
- libvlc_media_t * p_md;
- p_md = libvlc_media_duplicate([media libVLCMediaDescriptor]);
- for (NSString * key in [options allKeys]) {
- if (options[key] != [NSNull null])
- libvlc_media_add_option(p_md, [[NSString stringWithFormat:@"%@=%@", key, options[key]] UTF8String]);
- else
- libvlc_media_add_option(p_md, [[NSString stringWithFormat:@"%@", key] UTF8String]);
- }
- return [VLCMedia mediaWithLibVLCMediaDescriptor:p_md];
- }
- - (id)initWithLibVLCMediaDescriptor:(void *)md
- {
- if (self = [super init]) {
- libvlc_media_retain(md);
- p_md = md;
- _metaDictionary = [[NSMutableDictionary alloc] initWithCapacity:3];
- [self initInternalMediaDescriptor];
- }
- return self;
- }
- - (void *)libVLCMediaDescriptor
- {
- return p_md;
- }
- @end
|