core.c 4.18 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
 *****************************************************************************/
zorglub's avatar
zorglub committed
23
#include <stdarg.h>
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

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

zorglub's avatar
zorglub committed
31
32
33
/*************************************************************************
 * Exceptions handling
 *************************************************************************/
34
void libvlc_exception_init( libvlc_exception_t *p_exception )
zorglub's avatar
zorglub committed
35
{
zorglub's avatar
zorglub committed
36
37
    p_exception->b_raised = 0;
    p_exception->psz_message = NULL;
zorglub's avatar
zorglub committed
38
39
}

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

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
48
int libvlc_exception_raised( const libvlc_exception_t *p_exception )
zorglub's avatar
zorglub committed
49
{
50
    return (NULL != p_exception) && p_exception->b_raised;
zorglub's avatar
zorglub committed
51
52
}

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

63
64
void libvlc_exception_raise( libvlc_exception_t *p_exception,
                                           const char *psz_format, ... )
zorglub's avatar
zorglub committed
65
{
zorglub's avatar
zorglub committed
66
    va_list args;
damienf's avatar
damienf committed
67
68

    /* does caller care about exceptions ? */
69
    if( p_exception == NULL ) return;
damienf's avatar
damienf committed
70
71

    /* remove previous exception if it wasn't cleared */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
72
    libvlc_exception_clear( p_exception );
damienf's avatar
damienf committed
73

zorglub's avatar
zorglub committed
74
    va_start( args, psz_format );
75
    if( vasprintf( &p_exception->psz_message, psz_format, args ) == -1)
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
76
        p_exception->psz_message = (char *)nomemstr;
zorglub's avatar
zorglub committed
77
78
    va_end( args );

zorglub's avatar
zorglub committed
79
    p_exception->b_raised = 1;
zorglub's avatar
zorglub committed
80
81
}

82
libvlc_instance_t * libvlc_new( int argc, const char *const *argv,
83
                                libvlc_exception_t *p_e )
84
{
zorglub's avatar
zorglub committed
85
    libvlc_instance_t *p_new;
86

87
88
89
    for (int i = 0; i < argc; i++)
        printf ("%d: %s\n", i, argv[i]);

90
    libvlc_int_t *p_libvlc_int = libvlc_InternalCreate();
91
    if( !p_libvlc_int ) RAISENULL( "VLC initialization failed" );
92

zorglub's avatar
zorglub committed
93
    p_new = (libvlc_instance_t *)malloc( sizeof( libvlc_instance_t ) );
94
    if( !p_new ) RAISENULL( "Out of memory" );
zorglub's avatar
zorglub committed
95

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

99
    if( libvlc_InternalInit( p_libvlc_int, argc, argv ) )
100
        RAISENULL( "VLC initialization failed" );
zorglub's avatar
zorglub committed
101

102
    p_new->p_libvlc_int = p_libvlc_int;
zorglub's avatar
zorglub committed
103
    p_new->p_vlm = NULL;
104
    p_new->b_playlist_locked = 0;
105
    p_new->p_callback_list = NULL;
106
    vlc_mutex_init(p_libvlc_int, &p_new->instance_lock);
107
    vlc_mutex_init(p_libvlc_int, &p_new->event_callback_lock);
108
 
109
    libvlc_event_init(p_new, p_e);
110

zorglub's avatar
zorglub committed
111
    return p_new;
zorglub's avatar
zorglub committed
112
113
}

zorglub's avatar
zorglub committed
114
void libvlc_destroy( libvlc_instance_t *p_instance, libvlc_exception_t *p_e )
zorglub's avatar
zorglub committed
115
{
116
    libvlc_event_fini( p_instance, p_e );
117
118
    vlc_mutex_destroy( &p_instance->instance_lock );
    vlc_mutex_destroy( &p_instance->event_callback_lock);
119
120
    libvlc_InternalCleanup( p_instance->p_libvlc_int );
    libvlc_InternalDestroy( p_instance->p_libvlc_int, VLC_FALSE );
121
    free( p_instance );
zorglub's avatar
zorglub committed
122
}
damienf's avatar
damienf committed
123
124
125

int libvlc_get_vlc_id( libvlc_instance_t *p_instance )
{
126
    return p_instance->p_libvlc_int->i_object_id;
damienf's avatar
damienf committed
127
}