playlist.c 8.65 KB
Newer Older
1 2 3 4 5 6
/*****************************************************************************
 * playlist.c: libvlc new API playlist handling functions
 *****************************************************************************
 * Copyright (C) 2005 the VideoLAN team
 * $Id$
 *
7
 * Authors: Clément Stenac <zorglub@videolan.org>
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
Antoine Cellerier's avatar
Antoine Cellerier committed
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 23
 *****************************************************************************/

24 25
#include "libvlc_internal.h"

26
#include <vlc/libvlc.h>
27
#include <vlc_playlist.h>
28

29 30
#include <assert.h>

31 32
#include "../playlist/playlist_internal.h"

33
#define PL (libvlc_priv (p_instance->p_libvlc_int)->p_playlist)
34

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
static inline int playlist_was_locked( libvlc_instance_t *p_instance )
{
    int was_locked;
    vlc_mutex_lock( &p_instance->instance_lock );
    was_locked = p_instance->b_playlist_locked;
    vlc_mutex_unlock( &p_instance->instance_lock );
    return was_locked;
}

static inline void playlist_mark_locked( libvlc_instance_t *p_instance,
                                         int locked )
{
    vlc_mutex_lock( &p_instance->instance_lock );
    p_instance->b_playlist_locked = locked;
    vlc_mutex_unlock( &p_instance->instance_lock );
}

52
void libvlc_playlist_loop( libvlc_instance_t *p_instance, int loop,
53 54
                           libvlc_exception_t *p_e)
{
55 56
    VLC_UNUSED(p_e);

57
    assert( PL );
58
    var_SetBool( PL, "loop", loop );
59 60
}

61
void libvlc_playlist_play( libvlc_instance_t *p_instance, int i_id,
62
                           int i_options, char **ppsz_options,
63
                           libvlc_exception_t *p_e )
64
{
65
    VLC_UNUSED(p_e); VLC_UNUSED(i_options); VLC_UNUSED(ppsz_options);
66

67
    int did_lock = 0;
68
    assert( PL );
69 70
    ///\todo Handle additionnal options

Clément Stenac's avatar
Clément Stenac committed
71
    if( PL->items.i_size == 0 ) RAISEVOID( "Empty playlist" );
72
    if( i_id > 0 )
73
    {
74 75 76 77
        playlist_item_t *p_item;
        if (! playlist_was_locked( p_instance ) )
        {
            playlist_mark_locked( p_instance, 1 );
78
            vlc_object_lock( PL );
79 80 81 82
            did_lock = 1;
        }

        p_item = playlist_ItemGetByInputId( PL, i_id,
83
                                            PL->p_root_category );
84 85 86 87
        if( !p_item )
        {
            if( did_lock == 1 )
            {
88
                vlc_object_unlock( PL );
89 90 91 92 93
                playlist_mark_locked( p_instance, 0 );
            }
            RAISEVOID( "Unable to find item" );
        }

94
        playlist_Control( PL, PLAYLIST_VIEWPLAY, pl_Locked,
95
                          PL->p_root_category, p_item );
96 97
        if( did_lock == 1 )
        {
98
            vlc_object_unlock( PL );
99 100
            playlist_mark_locked( p_instance, 0 );
        }
101 102 103
    }
    else
    {
104 105
        playlist_Control( PL, PLAYLIST_PLAY,
                          playlist_was_locked( p_instance ) );
106
    }
107 108
}

109
void libvlc_playlist_pause( libvlc_instance_t *p_instance,
110
                            libvlc_exception_t *p_e )
111
{
112
    assert( PL );
113 114
    if( playlist_Control( PL, PLAYLIST_PAUSE,
                          playlist_was_locked( p_instance ) ) != VLC_SUCCESS )
115
        RAISEVOID( "Empty playlist" );
116 117 118
}


119
void libvlc_playlist_stop( libvlc_instance_t *p_instance,
120
                           libvlc_exception_t *p_e )
121
{
122
    assert( PL );
123 124
    if( playlist_Control( PL, PLAYLIST_STOP,
                          playlist_was_locked( p_instance ) ) != VLC_SUCCESS )
125
        RAISEVOID( "Empty playlist" );
126 127 128
}

void libvlc_playlist_clear( libvlc_instance_t *p_instance,
129
                            libvlc_exception_t *p_e )
130
{
131 132
    VLC_UNUSED(p_e);

133
    assert( PL );
134
    playlist_Clear( PL, playlist_was_locked( p_instance ) );
135 136
}

137
void libvlc_playlist_next( libvlc_instance_t *p_instance,
138
                           libvlc_exception_t *p_e )
139
{
140
    assert( PL );
141 142
    if( playlist_Control( PL, PLAYLIST_SKIP, playlist_was_locked( p_instance ),
                          1 ) != VLC_SUCCESS )
143
        RAISEVOID( "Empty playlist" );
144 145 146
}

void libvlc_playlist_prev( libvlc_instance_t *p_instance,
147
                           libvlc_exception_t *p_e )
148
{
149 150
    if( playlist_Control( PL, PLAYLIST_SKIP, playlist_was_locked( p_instance ),
                          -1  ) != VLC_SUCCESS )
151
        RAISEVOID( "Empty playlist" );
152 153
}

154
int libvlc_playlist_add( libvlc_instance_t *p_instance, const char *psz_uri,
155
                         const char *psz_name, libvlc_exception_t *p_e )
156 157
{
    return libvlc_playlist_add_extended( p_instance, psz_uri, psz_name,
158
                                         0, NULL, p_e );
159 160
}

161 162 163 164 165
static int PlaylistAddExtended( libvlc_instance_t *p_instance,
                                const char *psz_uri, const char *psz_name,
                                int i_options, const char **ppsz_options,
                                unsigned i_option_flags,
                                libvlc_exception_t *p_e )
166
{
167
    assert( PL );
168 169
    if( playlist_was_locked( p_instance ) )
    {
170
        libvlc_exception_raise( p_e, "You must unlock playlist before "
171 172 173
                               "calling libvlc_playlist_add" );
        return VLC_EGENERIC;
    }
174
    return playlist_AddExt( PL, psz_uri, psz_name,
175
                            PLAYLIST_INSERT, PLAYLIST_END, -1,
176
                            i_options, ppsz_options, i_option_flags,
177
                            true, pl_Unlocked );
178
}
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
int libvlc_playlist_add_extended( libvlc_instance_t *p_instance,
                                  const char *psz_uri, const char *psz_name,
                                  int i_options, const char **ppsz_options,
                                  libvlc_exception_t *p_e )
{
    return PlaylistAddExtended( p_instance, psz_uri, psz_name,
                                i_options, ppsz_options, VLC_INPUT_OPTION_TRUSTED,
                                p_e );
}
int libvlc_playlist_add_extended_untrusted( libvlc_instance_t *p_instance,
                                            const char *psz_uri, const char *psz_name,
                                            int i_options, const char **ppsz_options,
                                            libvlc_exception_t *p_e )
{
    return PlaylistAddExtended( p_instance, psz_uri, psz_name,
                                i_options, ppsz_options, 0,
                                p_e );
}
197

198
int libvlc_playlist_delete_item( libvlc_instance_t *p_instance, int i_id,
199
                                 libvlc_exception_t *p_e )
200
{
201
    assert( PL );
202

203 204
    if( playlist_DeleteFromInput( PL, i_id,
                                  playlist_was_locked( p_instance ) ) )
205 206 207
    {
        libvlc_exception_raise( p_e, "deletion failed" );
        return VLC_ENOITEM;
208
    }
209
    return VLC_SUCCESS;
210 211
}

212
int libvlc_playlist_isplaying( libvlc_instance_t *p_instance,
213
                               libvlc_exception_t *p_e )
214
{
215 216
    VLC_UNUSED(p_e);

217 218
    assert( PL );
    return playlist_IsPlaying( PL );
219
}
220

221
int libvlc_playlist_items_count( libvlc_instance_t *p_instance,
222
                                 libvlc_exception_t *p_e )
223
{
224 225
    VLC_UNUSED(p_e);

226
    assert( PL );
227
    return playlist_CurrentSize( PL );
228
}
229

230 231 232 233 234 235 236 237 238
int libvlc_playlist_get_current_index ( libvlc_instance_t *p_instance,
                                        libvlc_exception_t *p_e )
{
    VLC_UNUSED(p_e);

    assert( PL );
    return playlist_CurrentId( PL );
}

239 240 241
void libvlc_playlist_lock( libvlc_instance_t *p_instance )
{
    assert( PL );
242
    vlc_object_lock( PL );
243 244 245 246 247 248 249
    p_instance->b_playlist_locked = 1;
}

void libvlc_playlist_unlock( libvlc_instance_t *p_instance )
{
    assert( PL );
    p_instance->b_playlist_locked = 0;
250
    vlc_object_unlock( PL );
251 252
}

253
libvlc_media_player_t * libvlc_playlist_get_media_player(
254 255
                                libvlc_instance_t *p_instance,
                                libvlc_exception_t *p_e )
256
{
257
    libvlc_media_player_t *p_mi;
258
    assert( PL );
259 260
    input_thread_t * input = playlist_CurrentInput( PL );
    if( input )
261
    {
262
        p_mi = libvlc_media_player_new_from_input_thread(
263 264
                            p_instance, input, p_e );
        vlc_object_release( input );
265 266
    }
    else
267
    {
268 269
        /* no active input */
        p_mi = NULL;
270 271
        libvlc_exception_raise( p_e, "No active input" );
    }
272

273
    return p_mi;
274
}
275