]> git.sesse.net Git - vlc/blob - projects/macosx/framework/Sources/VLCLibrary.m
e2a11924796e0d7280391b6f05cc2c41136b3457
[vlc] / projects / macosx / framework / Sources / VLCLibrary.m
1 /*****************************************************************************
2  * VLCLibrary.m: VLCKit.framework VLCLibrary implementation
3  *****************************************************************************
4  * Copyright (C) 2007 Pierre d'Herbemont
5  * Copyright (C) 2007 the VideoLAN team
6  * $Id$
7  *
8  * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #import "VLCLibrary.h"
26 #import "VLCLibVLCBridging.h"
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc/vlc.h>
33 #include <vlc/libvlc_structures.h>
34
35 static VLCLibrary * sharedLibrary = nil;
36
37 void __catch_exception( void * e, const char * function, const char * file, int line_number )
38 {
39     libvlc_exception_t * ex = (libvlc_exception_t *)e;
40     if( libvlc_exception_raised( ex ) )
41     {
42         NSException* libvlcException = [NSException
43             exceptionWithName:@"LibVLCException"
44             reason:[NSString stringWithFormat:@"libvlc has thrown us an error: %s (%s:%d %s)", 
45                 libvlc_errmsg(), file, line_number, function]
46             userInfo:nil];
47         libvlc_exception_clear( ex );
48         @throw libvlcException;
49     }
50 }
51
52 @implementation VLCLibrary
53 + (VLCLibrary *)sharedLibrary
54 {
55     if (!sharedLibrary) 
56     {
57         /* Initialize a shared instance */
58         sharedLibrary = [[self alloc] init];
59     }
60     return sharedLibrary;
61 }
62
63 - (id)init 
64 {
65     if (self = [super init]) 
66     {
67         libvlc_exception_t ex;
68         libvlc_exception_init( &ex );
69         
70         NSArray *vlcParams = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"VLCParams"];
71         if (!vlcParams) {
72             NSMutableArray *defaultParams = [NSMutableArray array];
73             [defaultParams addObject:@"-I macosx_dialog_provider"];                 // No actual interface, just dialogs and nagging
74             [defaultParams addObject:@"--no-video-title-show"];                     // Don't show the title on overlay when starting to play
75             [defaultParams addObject:@"--no-sout-keep"];
76             [defaultParams addObject:@"--ignore-config"];                           // Don't read and write VLC config files
77             [defaultParams addObject:@"--vout=macosx"];
78             [defaultParams addObject:@"--text-renderer=quartztext"];                // our CoreText-based renderer
79             [defaultParams addObject:@"--verbose=-1"];                              // Don't polute the stdio log
80             [defaultParams addObject:@"--syslog"];                                  // log to syslog
81             [defaultParams addObject:@"--log-verbose=4"];                           // log everything
82             [defaultParams addObject:@"--no-color"];
83             [defaultParams addObject:@"--no-media-library"];
84             [defaultParams addObject:@"--play-and-pause"];
85             [defaultParams addObject:@"--extraintf=macosx_dialog_provider"];
86             vlcParams = defaultParams;
87         }
88
89         NSUInteger paramNum = 0;
90         NSUInteger count = [vlcParams count];
91         const char *lib_vlc_params[count];
92         while (paramNum < count) {
93             NSString *vlcParam = [vlcParams objectAtIndex:paramNum];
94             lib_vlc_params[paramNum] = [vlcParam cStringUsingEncoding:NSASCIIStringEncoding];
95             paramNum++;
96         }
97         instance = (void *)libvlc_new( sizeof(lib_vlc_params)/sizeof(lib_vlc_params[0]), lib_vlc_params, &ex );
98         catch_exception( &ex );
99         NSAssert(instance, @"libvlc failed to initialize");
100
101         // Assignment unneeded, as the audio unit will do it for us
102         /*audio = */ [[VLCAudio alloc] initWithLibrary:self];
103     }
104     return self;
105 }
106
107 - (NSString *)version 
108 {
109     return [NSString stringWithUTF8String:libvlc_get_version()];
110 }
111
112 - (NSString *)changeset 
113 {
114     return [NSString stringWithUTF8String:libvlc_get_changeset()];
115 }
116
117 - (void)dealloc 
118 {
119     if( instance ) 
120         libvlc_release( instance );
121     
122     if( self == sharedLibrary ) 
123         sharedLibrary = nil;
124     
125     instance = nil;
126     [audio release];
127     [super dealloc];
128 }
129
130 @synthesize audio;
131 @end
132
133 @implementation VLCLibrary (VLCLibVLCBridging)
134 + (void *)sharedInstance
135 {
136     return [self sharedLibrary].instance;
137 }
138
139 - (void *)instance
140 {
141     return instance;
142 }
143 @end
144
145 @implementation VLCLibrary (VLCAudioBridging)
146 - (void)setAudio:(VLCAudio *)value
147 {
148     if (!audio)
149         audio = value;
150 }
151 @end