VLCMedia.m 19.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*****************************************************************************
 * VLCMedia.m: VLC.framework VLCMedia implementation
 *****************************************************************************
 * Copyright (C) 2007 Pierre d'Herbemont
 * Copyright (C) 2007 the VideoLAN team
 * $Id$
 *
 * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 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 General Public License for more details.
 *
 * You should have received a copy of the GNU 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.
 *****************************************************************************/

25
26
#import "VLCMedia.h"
#import "VLCMediaList.h"
27
28
#import "VLCEventManager.h"
#import "VLCLibrary.h"
29
#import "VLCLibVLCBridging.h"
30
31
#include <vlc/libvlc.h>

32
/* Meta Dictionary Keys */
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
NSString * VLCMetaInformationTitle          = @"title";
NSString * VLCMetaInformationArtist         = @"artist";
NSString * VLCMetaInformationGenre          = @"genre";
NSString * VLCMetaInformationCopyright      = @"copyright";
NSString * VLCMetaInformationAlbum          = @"album";
NSString * VLCMetaInformationTrackNumber    = @"trackNumber";
NSString * VLCMetaInformationDescription    = @"description";
NSString * VLCMetaInformationRating         = @"rating";
NSString * VLCMetaInformationDate           = @"date";
NSString * VLCMetaInformationSetting        = @"setting";
NSString * VLCMetaInformationURL            = @"url";
NSString * VLCMetaInformationLanguage       = @"language";
NSString * VLCMetaInformationNowPlaying     = @"nowPlaying";
NSString * VLCMetaInformationPublisher      = @"publisher";
NSString * VLCMetaInformationEncodedBy      = @"encodedBy";
NSString * VLCMetaInformationArtworkURL     = @"artworkURL";
NSString * VLCMetaInformationArtwork        = @"artwork";
NSString * VLCMetaInformationTrackID        = @"trackID";
51

52
/* Notification Messages */
53
NSString * VLCMediaMetaChanged              = @"VLCMediaMetaChanged";
54

55
56
57
58
59
60
61
/******************************************************************************
 * @property (readwrite)
 */
@interface VLCMedia ()
@property (readwrite) VLCMediaState state;
@end

62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/******************************************************************************
 * Interface (Private)
 */
// TODO: Documentation
@interface VLCMedia (Private)
/* Statics */
+ (libvlc_meta_t)stringToMetaType:(NSString *)string;
+ (NSString *)metaTypeToString:(libvlc_meta_t)type;

/* Initializers */
- (void)initInternalMediaDescriptor;

/* Operations */
- (void)fetchMetaInformationFromLibVLCWithType:(NSString*)metaType;
- (void)fetchMetaInformationForArtWorkWithURL:(NSString *)anURL;
77
- (void)setArtwork:(NSImage *)art;
78
79
80

/* Callback Methods */
- (void)metaChanged:(NSString *)metaType;
81
- (void)subItemAdded;
82
- (void)setStateAsNumber:(NSNumber *)newStateAsNumber;
83
84
@end

85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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];
}
101
102
103
104

/******************************************************************************
 * LibVLC Event Callback
 */
105
static void HandleMediaMetaChanged(const libvlc_event_t * event, void * self)
106
{
107
108
109
110
111
112
113
    if( event->u.media_descriptor_meta_changed.meta_type == libvlc_meta_Publisher ||
        event->u.media_descriptor_meta_changed.meta_type == libvlc_meta_NowPlaying )
    {
        /* Skip those meta. We don't really care about them for now.
         * And they occure a lot */
        return;
    }
114
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
115
116
    [[VLCEventManager sharedManager] callOnMainThreadObject:self
                                                 withMethod:@selector(metaChanged:)
117
                                       withArgumentAsObject:[VLCMedia metaTypeToString:event->u.media_descriptor_meta_changed.meta_type]];
118
    [pool release];
119
120
}

121
122
123
124
125
//static void HandleMediaDurationChanged(const libvlc_event_t * event, void * self)
//{
//    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
//    
//    [[VLCEventManager sharedManager] callOnMainThreadObject:self
126
127
128
//                                                 withMethod:@selector(setLength:)
//                                       withArgumentAsObject:[VLCTime timeWithNumber:
//                                           [NSNumber numberWithLongLong:event->u.media_descriptor_duration_changed.new_duration]]];
129
130
//    [pool release];
//}
131

132
static void HandleMediaStateChanged(const libvlc_event_t * event, void * self)
133
{
134
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
135
    
136
    [[VLCEventManager sharedManager] callOnMainThreadObject:self
137
                                                 withMethod:@selector(setStateAsNumber:)
138
139
140
141
142
                                       withArgumentAsObject:[NSNumber numberWithInt:
                                            LibVLCStateToMediaState(event->u.media_descriptor_state_changed.new_state)]];
    [pool release];
}

143
static void HandleMediaSubItemAdded(const libvlc_event_t * event, void * self)
144
{
145
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
146
147
148
149
150
151
    [[VLCEventManager sharedManager] callOnMainThreadObject:self
                                                 withMethod:@selector(subItemAdded)
                                       withArgumentAsObject:nil];
    [pool release];
}

152
153
154
/******************************************************************************
 * Implementation
 */
155
@implementation VLCMedia
156
+ (id)mediaWithURL:(NSURL *)anURL;
157
{
158
    return [[[VLCMedia alloc] initWithURL:anURL] autorelease];
159
160
}

161
+ (id)mediaWithPath:(NSString *)aPath;
162
{
163
    return [[[VLCMedia alloc] initWithPath:aPath] autorelease];
164
165
166
167
168
169
170
}

+ (id)mediaAsNodeWithName:(NSString *)aName;
{
    return [[[VLCMedia alloc] initAsNodeWithName:aName] autorelease];
}

171
172
173
174
175
176
- (id)initWithURL:(NSURL *)anURL
{
    return [self initWithPath:[anURL path]];
}

- (id)initWithPath:(NSString *)aPath
177
178
179
180
181
182
{        
    if (self = [super init])
    {
        libvlc_exception_t ex;
        libvlc_exception_init(&ex);
        
183
184
185
        p_md = libvlc_media_descriptor_new([VLCLibrary sharedInstance],
                                           [aPath UTF8String],
                                           &ex);
186
        catch_exception(&ex);
187
188
189
190
191
192
193
194
195
196
197
        
        delegate = nil;
        metaDictionary = [[NSMutableDictionary alloc] initWithCapacity:3];
        
        // This value is set whenever the demuxer figures out what the length is.
        // TODO: Easy way to tell the length of the movie without having to instiate the demuxer.  Maybe cached info?
        length = nil;

        [self initInternalMediaDescriptor];
    }
    return self;
198
199
}

200
- (id)initAsNodeWithName:(NSString *)aName
201
{        
202
203
204
    if (self = [super init])
    {
        libvlc_exception_t ex;
205
        libvlc_exception_init(&ex);
206
        
207
208
209
        p_md = libvlc_media_descriptor_new_as_node([VLCLibrary sharedInstance],
                                                   [aName UTF8String],
                                                   &ex);
210
        catch_exception(&ex);
211

212
        delegate = nil;
213
        metaDictionary = [[NSMutableDictionary alloc] initWithCapacity:3];
214
215
216
217
        
        // This value is set whenever the demuxer figures out what the length is.
        // TODO: Easy way to tell the length of the movie without having to instiate the demuxer.  Maybe cached info?
        length = nil;
218
        
219
        [self initInternalMediaDescriptor];
220
221
222
223
224
225
226
227
228
229
230
231
232
    }
    return self;
}

- (void)release
{
    @synchronized(self)
    {
        if([self retainCount] <= 1)
        {
            /* We must make sure we won't receive new event after an upcoming dealloc
             * We also may receive a -retain in some event callback that may occcur
             * Before libvlc_event_detach. So this can't happen in dealloc */
233
            libvlc_event_manager_t * p_em = libvlc_media_descriptor_event_manager(p_md, NULL);
234
            libvlc_event_detach(p_em, libvlc_MediaDescriptorMetaChanged,     HandleMediaMetaChanged,     self, NULL);
235
//            libvlc_event_detach(p_em, libvlc_MediaDescriptorDurationChanged, HandleMediaDurationChanged, self, NULL);
236
            libvlc_event_detach(p_em, libvlc_MediaDescriptorStateChanged,    HandleMediaStateChanged,    self, NULL);
237
            libvlc_event_detach(p_em, libvlc_MediaDescriptorSubItemAdded,    HandleMediaSubItemAdded,    self, NULL);
238
239
240
241
242
        }
        [super release];
    }
}

243
- (void)dealloc
244
{
245
246
    // Testing to see if the pointer exists is not required, if the pointer is null
    // then the release message is not sent to it.
247
    delegate = nil;
248
249
    [self setLength:nil];

250
    [url release];
251
252
    [subitems release];
    [metaDictionary release];
253

254
    libvlc_media_descriptor_release( p_md );
255

256
257
258
    [super dealloc];
}

259
260
- (NSString *)description
{
261
    NSString * result = [metaDictionary objectForKey:VLCMetaInformationTitle];
262
    return [NSString stringWithFormat:@"<%@ %p> %@", [self className], self, (result ? result : [url absoluteString])];
263
264
265
266
}

- (NSComparisonResult)compare:(VLCMedia *)media
{
267
    if (self == media)
268
        return NSOrderedSame;
269
    if (!media)
270
        return NSOrderedDescending;
271
    return p_md == [media libVLCMediaDescriptor] ? NSOrderedSame : NSOrderedAscending;
272
273
}

274
@synthesize delegate;
275

276
- (VLCTime *)length
277
{
278
279
280
    if (!length) 
    {
        // Try figuring out what the length is
281
        long long duration = libvlc_media_descriptor_get_duration( p_md, NULL );
282
283
284
285
286
287
288
289
290
        if (duration > -1) 
        {
            [self setLength:[VLCTime timeWithNumber:[NSNumber numberWithLongLong:duration]]];
            return [[length retain] autorelease];
        } 
    }
    return [VLCTime nullTime];
}

291
292
- (VLCTime *)lengthWaitUntilDate:(NSDate *)aDate
{
293
    static const long long thread_sleep = 10000;
294

295
    if (!length)
296
297
298
    {
        while (!length && ![self isPreparsed] && [aDate timeIntervalSinceNow] > 0)
        {
299
            usleep( thread_sleep );
300
301
302
303
304
305
306
307
        }
        
        // 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];
    }
308

309
    return [[length retain] autorelease];
310
}
311
312
313

- (BOOL)isPreparsed
{
314
    return libvlc_media_descriptor_is_preparsed( p_md, NULL );
315
316
}

317
318
319
320
@synthesize url;
@synthesize subitems;
@synthesize metaDictionary;
@synthesize state;
321

322
323
@end

324
325
326
/******************************************************************************
 * Implementation VLCMedia (LibVLCBridging)
 */
327
@implementation VLCMedia (LibVLCBridging)
328
329
330

+ (id)mediaWithLibVLCMediaDescriptor:(void *)md
{
331
    return [[[VLCMedia alloc] initWithLibVLCMediaDescriptor:md] autorelease];
332
333
334
}

- (id)initWithLibVLCMediaDescriptor:(void *)md
335
336
337
{
    if (self = [super init])
    {
338
339
        libvlc_exception_t ex;
        libvlc_exception_init( &ex );
340
                
341
        libvlc_media_descriptor_retain( md );
342
343
        p_md = md;
        
344
        metaDictionary = [[NSMutableDictionary alloc] initWithCapacity:3];
345
        [self initInternalMediaDescriptor];
346
347
348
349
    }
    return self;
}

350
- (void *)libVLCMediaDescriptor 
351
{
352
    return p_md;
353
}
354
355
356
357
358
359
360
361
362
363
364
365

+ (id)mediaWithMedia:(VLCMedia *)media andLibVLCOptions:(NSDictionary *)options
{
    libvlc_media_descriptor_t * p_md;
    p_md = libvlc_media_descriptor_duplicate( [media libVLCMediaDescriptor] );
    for( NSString * key in [options allKeys] )
    {
        libvlc_media_descriptor_add_option(p_md, [[NSString stringWithFormat:@"%@=#%@", key, [options objectForKey:key]] UTF8String], NULL);
    }
    return [VLCMedia mediaWithLibVLCMediaDescriptor:p_md];
}

366
367
@end

368
369
370
/******************************************************************************
 * Implementation VLCMedia (Private)
 */
371
@implementation VLCMedia (Private)
372

373
374
+ (libvlc_meta_t)stringToMetaType:(NSString *)string
{
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
    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),
                nil] retain];
399
#undef VLCStringToMeta
400
401
402
    }
    NSNumber * number = [stringToMetaDictionary objectForKey:string];
    return number ? [number intValue] : -1;
403
404
405
406
}

+ (NSString *)metaTypeToString:(libvlc_meta_t)type
{
407
#define VLCMetaToString( name, type )   if (libvlc_meta_##name == type) return VLCMetaInformation##name;
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
    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);
424
#undef VLCMetaToString
425
426
427
428
    return nil;
}

- (void)initInternalMediaDescriptor
429
{
430
    libvlc_exception_t ex;
431
432
    libvlc_exception_init( &ex );

433
434
    artFetched = NO;

435
436
437
438
439
440
441
442
    char * p_url = libvlc_media_descriptor_get_mrl( p_md, &ex );
    catch_exception( &ex );

    url = [[NSURL URLWithString:[NSString stringWithUTF8String:p_url]] retain];
    if( !url ) /* Attempt to interpret as a file path then */
        url = [[NSURL fileURLWithPath:[NSString stringWithUTF8String:p_url]] retain];
    free( p_url );

443
    libvlc_media_descriptor_set_user_data( p_md, (void*)self, &ex );
444
    catch_exception( &ex );
445

446
    libvlc_event_manager_t * p_em = libvlc_media_descriptor_event_manager( p_md, &ex );
447
    libvlc_event_attach(p_em, libvlc_MediaDescriptorMetaChanged,     HandleMediaMetaChanged,     self, &ex);
448
//    libvlc_event_attach(p_em, libvlc_MediaDescriptorDurationChanged, HandleMediaDurationChanged, self, &ex);
449
    libvlc_event_attach(p_em, libvlc_MediaDescriptorStateChanged,    HandleMediaStateChanged,    self, &ex);
450
    libvlc_event_attach(p_em, libvlc_MediaDescriptorSubItemAdded,    HandleMediaSubItemAdded,    self, &ex);
451
    catch_exception( &ex );
452
    
453
    libvlc_media_list_t * p_mlist = libvlc_media_descriptor_subitems( p_md, NULL );
454

455
456
457
458
    if (!p_mlist)
        subitems = nil;
    else
    {
459
        subitems = [[VLCMediaList mediaListWithLibVLCMediaList:p_mlist] retain];
460
        libvlc_media_list_release( p_mlist );
461
    }
462

463
    state = LibVLCStateToMediaState(libvlc_media_descriptor_get_state( p_md, NULL ));
464

465
466
    /* Force VLCMetaInformationTitle, that will trigger preparsing
     * And all the other meta will be added through the libvlc event system */
467
    [self fetchMetaInformationFromLibVLCWithType: VLCMetaInformationTitle];
468
}
469

470
- (void)fetchMetaInformationFromLibVLCWithType:(NSString *)metaType
471
{
472
    char * psz_value = libvlc_media_descriptor_get_meta( p_md, [VLCMedia stringToMetaType:metaType], NULL);
473
474
    NSString * newValue = psz_value ? [NSString stringWithUTF8String: psz_value] : nil;
    NSString * oldValue = [metaDictionary valueForKey:metaType];
475
476
    free(psz_value);

477
    if ( newValue != oldValue && !(oldValue && newValue && [oldValue compare:newValue] == NSOrderedSame) )
478
    {
479
        if ([metaType isEqualToString:VLCMetaInformationArtworkURL])
480
        {
481
            [NSThread detachNewThreadSelector:@selector(fetchMetaInformationForArtWorkWithURL:) 
482
                                         toTarget:self
483
484
485
                                       withObject:newValue];
            return;
        }
486

487
        [metaDictionary setValue:newValue forKeyPath:metaType];
488
489
490
491
492
    }
}

- (void)fetchMetaInformationForArtWorkWithURL:(NSString *)anURL
{
493
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
494
495
496
    NSImage * art = nil;

    if( anURL )
497
    {
498
499
500
501
502
503
        // 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] autorelease]; 
504
505
    }

506
507
508
    // If anything was found, lets save it to the meta data dictionary
    [self performSelectorOnMainThread:@selector(setArtwork:) withObject:art waitUntilDone:NO];

509
    [pool release];
510
511
}

512
513
- (void)setArtwork:(NSImage *)art
{
514
515
516
517
518
519
    if (!art)
    {
        [metaDictionary removeObjectForKey:@"artwork"];
        return;
    }

520
521
522
    [metaDictionary setObject:art forKey:@"artwork"];
}

523
- (void)metaChanged:(NSString *)metaType
524
{
525
    [self fetchMetaInformationFromLibVLCWithType:metaType];
526
}
527
528
529
530
531
532

- (void)subItemAdded
{
    if( subitems )
        return; /* Nothing to do */

533
    libvlc_media_list_t * p_mlist = libvlc_media_descriptor_subitems( p_md, NULL );
534
535
536
537
538
539
540
541

    NSAssert( p_mlist, @"The mlist shouldn't be nil, we are receiving a subItemAdded");

    [self willChangeValueForKey:@"subitems"];
    subitems = [[VLCMediaList mediaListWithLibVLCMediaList:p_mlist] retain];
    [self didChangeValueForKey:@"subitems"];
    libvlc_media_list_release( p_mlist );
}
542
543
544
545
546

- (void)setStateAsNumber:(NSNumber *)newStateAsNumber
{
    [self setState: [newStateAsNumber intValue]];
}
547
548
549

- (id)valueForKeyPath:(NSString *)keyPath
{
550
    if( !artFetched && [keyPath isEqualToString:@"metaDictionary.artwork"])
551
552
553
554
555
556
557
    {
        artFetched = YES;
        /* Force the retrieval of the artwork now that someone asked for it */
        [self fetchMetaInformationFromLibVLCWithType: VLCMetaInformationArtworkURL];
    }
    return [super valueForKeyPath:keyPath];
}
558
559
@end

560
561
562
563
/******************************************************************************
 * Implementation VLCMedia (VLCMediaPlayerBridging)
 */

564
565
566
567
@implementation VLCMedia (VLCMediaPlayerBridging)

- (void)setLength:(VLCTime *)value
{
568
569
    if (length && value && [length compare:value] == NSOrderedSame)
        return;
570
        
571
572
    [length release];       
    length = value ? [value retain] : nil;
573
}
574

575
@end