core.c 5.98 KB
Newer Older
zorglub's avatar
zorglub committed
1
/*****************************************************************************
zorglub's avatar
zorglub committed
2
 * core.c: Core libvlc new API functions : initialization, exceptions handling
zorglub's avatar
zorglub committed
3
 *****************************************************************************
4
 * Copyright (C) 2005 the VideoLAN team
5
 * $Id$
zorglub's avatar
zorglub committed
6
 *
7
 * Authors: Clément Stenac <zorglub@videolan.org>
zorglub's avatar
zorglub committed
8
9
10
11
12
13
14
15
16
17
18
19
20
 *
 * 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
dionoea's avatar
dionoea committed
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
zorglub's avatar
zorglub committed
22
 *****************************************************************************/
23

zorglub's avatar
zorglub committed
24
#include "libvlc_internal.h"
zorglub's avatar
zorglub committed
25
#include <vlc/libvlc.h>
zorglub's avatar
zorglub committed
26

zorglub's avatar
zorglub committed
27
#include <vlc_interface.h>
zorglub's avatar
zorglub committed
28

29
30
31
32
#include <stdarg.h>
#include <limits.h>
#include <assert.h>

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
33
34
static const char nomemstr[] = "Insufficient memory";

zorglub's avatar
zorglub committed
35
36
37
/*************************************************************************
 * Exceptions handling
 *************************************************************************/
38
void libvlc_exception_init( libvlc_exception_t *p_exception )
zorglub's avatar
zorglub committed
39
{
zorglub's avatar
zorglub committed
40
41
    p_exception->b_raised = 0;
    p_exception->psz_message = NULL;
zorglub's avatar
zorglub committed
42
43
}

zorglub's avatar
zorglub committed
44
45
void libvlc_exception_clear( libvlc_exception_t *p_exception )
{
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
46
    if( p_exception->psz_message != nomemstr )
zorglub's avatar
zorglub committed
47
        free( p_exception->psz_message );
zorglub's avatar
zorglub committed
48
    p_exception->psz_message = NULL;
zorglub's avatar
zorglub committed
49
50
51
    p_exception->b_raised = 0;
}

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
52
int libvlc_exception_raised( const libvlc_exception_t *p_exception )
zorglub's avatar
zorglub committed
53
{
54
    return (NULL != p_exception) && p_exception->b_raised;
zorglub's avatar
zorglub committed
55
56
}

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
57
58
const char *
libvlc_exception_get_message( const libvlc_exception_t *p_exception )
zorglub's avatar
zorglub committed
59
{
zorglub's avatar
zorglub committed
60
    if( p_exception->b_raised == 1 && p_exception->psz_message )
zorglub's avatar
zorglub committed
61
    {
zorglub's avatar
zorglub committed
62
        return p_exception->psz_message;
zorglub's avatar
zorglub committed
63
    }
zorglub's avatar
zorglub committed
64
    return NULL;
zorglub's avatar
zorglub committed
65
66
}

67
static void libvlc_exception_not_handled( const char *psz )
68
{
69
70
    fprintf( stderr, "*** LibVLC Exception not handled: %s\nSet a breakpoint in '%s' to debug.\n",
             psz, __func__ );
71
72
}

73
void libvlc_exception_raise( libvlc_exception_t *p_exception,
74
                             const char *psz_format, ... )
zorglub's avatar
zorglub committed
75
{
zorglub's avatar
zorglub committed
76
    va_list args;
77
    char * psz;
damienf's avatar
damienf committed
78

79
    /* Unformat-ize the message */
zorglub's avatar
zorglub committed
80
    va_start( args, psz_format );
81
82
    if( vasprintf( &psz, psz_format, args ) == -1)
        psz = (char *)nomemstr;
zorglub's avatar
zorglub committed
83
84
    va_end( args );

85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
    /* Does caller care about exceptions ? */
    if( p_exception == NULL ) {
        /* Print something, so lazy third-parties can easily
         * notice that something may have gone unoticedly wrong */
        libvlc_exception_not_handled( psz );
        return;
    }

    /* Make sure that there is no unoticed previous exception */
    if( p_exception->b_raised )
    {
        libvlc_exception_not_handled( p_exception->psz_message );
        libvlc_exception_clear( p_exception );
    }
    p_exception->psz_message = psz;
zorglub's avatar
zorglub committed
100
    p_exception->b_raised = 1;
zorglub's avatar
zorglub committed
101
102
}

103
libvlc_instance_t * libvlc_new( int argc, const char *const *argv,
104
                                libvlc_exception_t *p_e )
105
{
zorglub's avatar
zorglub committed
106
    libvlc_instance_t *p_new;
107

108
    libvlc_int_t *p_libvlc_int = libvlc_InternalCreate();
109
    if( !p_libvlc_int ) RAISENULL( "VLC initialization failed" );
110

zorglub's avatar
zorglub committed
111
    p_new = (libvlc_instance_t *)malloc( sizeof( libvlc_instance_t ) );
112
    if( !p_new ) RAISENULL( "Out of memory" );
zorglub's avatar
zorglub committed
113

114
115
116
117
118
119
120
    const char *my_argv[argc + 2];

    my_argv[0] = "libvlc"; /* dummy arg0, skipped by getopt() et al */
    for( int i = 0; i < argc; i++ )
         my_argv[i + 1] = argv[i];
    my_argv[argc + 1] = NULL; /* C calling conventions require a NULL */

zorglub's avatar
zorglub committed
121
    /** \todo Look for interface settings. If we don't have any, add -I dummy */
122
    /* Because we probably don't want a GUI by default */
123

124
    if( libvlc_InternalInit( p_libvlc_int, argc + 1, my_argv ) )
125
        RAISENULL( "VLC initialization failed" );
zorglub's avatar
zorglub committed
126

127
    p_new->p_libvlc_int = p_libvlc_int;
zorglub's avatar
zorglub committed
128
    p_new->p_vlm = NULL;
129
    p_new->b_playlist_locked = 0;
130
    p_new->ref_count = 1;
131
    p_new->p_callback_list = NULL;
132
133
    vlc_mutex_init(&p_new->instance_lock);
    vlc_mutex_init(&p_new->event_callback_lock);
134

zorglub's avatar
zorglub committed
135
    return p_new;
zorglub's avatar
zorglub committed
136
137
}

138
139
140
141
142
143
144
145
146
147
void libvlc_retain( libvlc_instance_t *p_instance )
{
    assert( p_instance != NULL );
    assert( p_instance->ref_count < UINT_MAX );

    vlc_mutex_lock( &p_instance->instance_lock );
    p_instance->ref_count++;
    vlc_mutex_unlock( &p_instance->instance_lock );
}

148
void libvlc_release( libvlc_instance_t *p_instance )
zorglub's avatar
zorglub committed
149
{
150
151
152
153
154
    vlc_mutex_t *lock = &p_instance->instance_lock;
    int refs;

    assert( p_instance->ref_count > 0 );

155
    vlc_mutex_lock( lock );
156
    refs = --p_instance->ref_count;
157
    vlc_mutex_unlock( lock );
158
159
160

    if( refs == 0 )
    {
161
        vlc_mutex_destroy( lock );
162
163
        vlc_mutex_destroy( &p_instance->event_callback_lock );
        libvlc_InternalCleanup( p_instance->p_libvlc_int );
164
        libvlc_InternalDestroy( p_instance->p_libvlc_int, false );
165
166
        free( p_instance );
    }
zorglub's avatar
zorglub committed
167
}
damienf's avatar
damienf committed
168

169
170
void libvlc_add_intf( libvlc_instance_t *p_i, const char *name,
                      libvlc_exception_t *p_e )
171
{
172
    if( libvlc_InternalAddIntf( p_i->p_libvlc_int, name, true ) )
173
174
175
        RAISEVOID( "Interface initialization failed" );
}

176
177
178
179
void libvlc_wait( libvlc_instance_t *p_i )
{
    libvlc_int_t *p_libvlc = p_i->p_libvlc_int;
    vlc_object_lock( p_libvlc );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
180
181
    while( vlc_object_alive( p_libvlc ) )
        vlc_object_wait( p_libvlc );
182
183
184
    vlc_object_unlock( p_libvlc );
}

damienf's avatar
damienf committed
185
186
int libvlc_get_vlc_id( libvlc_instance_t *p_instance )
{
187
    return p_instance->p_libvlc_int->i_object_id;
damienf's avatar
damienf committed
188
}
189

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
190
const char * libvlc_get_version(void)
191
192
193
194
{
    return VLC_Version();
}

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
195
const char * libvlc_get_compiler(void)
196
197
198
199
{
    return VLC_Compiler();
}

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
200
const char * libvlc_get_changeset(void)
201
202
203
{
    return VLC_Changeset();
}