playlist.c 7.15 KB
Newer Older
Clément Stenac's avatar
Clément Stenac committed
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>
Clément Stenac's avatar
Clément Stenac 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
Antoine Cellerier's avatar
Antoine Cellerier committed
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
Clément Stenac's avatar
Clément Stenac committed
22 23
 *****************************************************************************/

Clément Stenac's avatar
Clément Stenac committed
24
#include "libvlc_internal.h"
Clément Stenac's avatar
Clément Stenac committed
25
#include <vlc/libvlc.h>
Clément Stenac's avatar
Clément Stenac committed
26
#include <vlc_playlist.h>
Clément Stenac's avatar
Clément Stenac committed
27

28 29
#include <assert.h>

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

32 33
#define PL p_instance->p_libvlc_int->p_playlist

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
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 );
}

51 52 53 54
void libvlc_playlist_loop( libvlc_instance_t *p_instance, vlc_bool_t loop,
                           libvlc_exception_t *p_e)
{
    assert( PL );
55
    var_SetBool( PL, "loop", loop );
56 57
}

Clément Stenac's avatar
Clément Stenac committed
58
void libvlc_playlist_play( libvlc_instance_t *p_instance, int i_id,
Clément Stenac's avatar
Clément Stenac committed
59
                           int i_options, char **ppsz_options,
60
                           libvlc_exception_t *p_e )
Clément Stenac's avatar
Clément Stenac committed
61
{
62
    int did_lock = 0;
63
    assert( PL );
Clément Stenac's avatar
Clément Stenac committed
64 65
    ///\todo Handle additionnal options

Clément Stenac's avatar
Clément Stenac committed
66
    if( PL->items.i_size == 0 ) RAISEVOID( "Empty playlist" );
67
    if( i_id > 0 )
Clément Stenac's avatar
Clément Stenac committed
68
    {
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
        playlist_item_t *p_item;
        if (! playlist_was_locked( p_instance ) )
        {
            playlist_mark_locked( p_instance, 1 );
            vlc_mutex_lock( &PL->object_lock );
            did_lock = 1;
        }

        p_item = playlist_ItemGetByInputId( PL, i_id,
                                            PL->status.p_node );
        if( !p_item )
        {
            if( did_lock == 1 )
            {
                vlc_mutex_unlock( &PL->object_lock );
                playlist_mark_locked( p_instance, 0 );
            }
            RAISEVOID( "Unable to find item" );
        }

        playlist_Control( PL, PLAYLIST_VIEWPLAY, VLC_TRUE,
90
                          PL->status.p_node, p_item );
91 92 93 94 95
        if( did_lock == 1 )
        {
            vlc_mutex_unlock( &PL->object_lock );
            playlist_mark_locked( p_instance, 0 );
        }
Clément Stenac's avatar
Clément Stenac committed
96 97 98
    }
    else
    {
99 100
        playlist_Control( PL, PLAYLIST_PLAY,
                          playlist_was_locked( p_instance ) );
Clément Stenac's avatar
Clément Stenac committed
101
    }
Clément Stenac's avatar
Clément Stenac committed
102 103
}

104
void libvlc_playlist_pause( libvlc_instance_t *p_instance,
105
                            libvlc_exception_t *p_e )
106
{
107
    assert( PL );
108 109
    if( playlist_Control( PL, PLAYLIST_PAUSE,
                          playlist_was_locked( p_instance ) ) != VLC_SUCCESS )
110
        RAISEVOID( "Empty playlist" );
111 112 113
}


114
void libvlc_playlist_stop( libvlc_instance_t *p_instance,
115
                           libvlc_exception_t *p_e )
116
{
117
    assert( PL );
118 119
    if( playlist_Control( PL, PLAYLIST_STOP,
                          playlist_was_locked( p_instance ) ) != VLC_SUCCESS )
120
        RAISEVOID( "Empty playlist" );
121 122 123
}

void libvlc_playlist_clear( libvlc_instance_t *p_instance,
124
                            libvlc_exception_t *p_e )
125
{
126
    assert( PL );
127
    playlist_Clear( PL, playlist_was_locked( p_instance ) );
128 129
}

130
void libvlc_playlist_next( libvlc_instance_t *p_instance,
131
                           libvlc_exception_t *p_e )
132
{
133
    assert( PL );
134 135
    if( playlist_Control( PL, PLAYLIST_SKIP, playlist_was_locked( p_instance ),
                          1 ) != VLC_SUCCESS )
136
        RAISEVOID( "Empty playlist" );
137 138 139
}

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

Clément Stenac's avatar
Clément Stenac committed
147
int libvlc_playlist_add( libvlc_instance_t *p_instance, const char *psz_uri,
148
                         const char *psz_name, libvlc_exception_t *p_e )
Clément Stenac's avatar
Clément Stenac committed
149 150
{
    return libvlc_playlist_add_extended( p_instance, psz_uri, psz_name,
151
                                         0, NULL, p_e );
Clément Stenac's avatar
Clément Stenac committed
152 153 154 155 156
}

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,
157
                                  libvlc_exception_t *p_e )
Clément Stenac's avatar
Clément Stenac committed
158
{
159
    assert( PL );
160 161
    if( playlist_was_locked( p_instance ) )
    {
162
        libvlc_exception_raise( p_e, "You must unlock playlist before "
163 164 165
                               "calling libvlc_playlist_add" );
        return VLC_EGENERIC;
    }
Filippo Carone's avatar
Filippo Carone committed
166
    return playlist_AddExt( PL, psz_uri, psz_name,
Clément Stenac's avatar
Clément Stenac committed
167
                            PLAYLIST_INSERT, PLAYLIST_END, -1, ppsz_options,
168
                            i_options, 1, VLC_FALSE );
Clément Stenac's avatar
Clément Stenac committed
169 170
}

171

172
int libvlc_playlist_delete_item( libvlc_instance_t *p_instance, int i_id,
173
                                 libvlc_exception_t *p_e )
174
{
175
    assert( PL );
176

177 178
    if( playlist_DeleteFromInput( PL, i_id,
                                  playlist_was_locked( p_instance ) ) )
179 180 181
    {
        libvlc_exception_raise( p_e, "deletion failed" );
        return VLC_ENOITEM;
182
    }
183
    return VLC_SUCCESS;
184 185
}

186
int libvlc_playlist_isplaying( libvlc_instance_t *p_instance,
187
                               libvlc_exception_t *p_e )
188
{
189 190
    assert( PL );
    return playlist_IsPlaying( PL );
191
}
Clément Stenac's avatar
Clément Stenac committed
192

193
int libvlc_playlist_items_count( libvlc_instance_t *p_instance,
194
                                 libvlc_exception_t *p_e )
195
{
196
    assert( PL );
197
    return playlist_CurrentSize( PL );
198
}
199

200 201 202 203 204 205 206 207 208 209 210 211 212 213
void libvlc_playlist_lock( libvlc_instance_t *p_instance )
{
    assert( PL );
    vlc_mutex_lock( &PL->object_lock );
    p_instance->b_playlist_locked = 1;
}

void libvlc_playlist_unlock( libvlc_instance_t *p_instance )
{
    assert( PL );
    p_instance->b_playlist_locked = 0;
    vlc_mutex_unlock( &PL->object_lock );
}

214 215 216
libvlc_media_instance_t * libvlc_playlist_get_media_instance(
                                libvlc_instance_t *p_instance,
                                libvlc_exception_t *p_e )
Clément Stenac's avatar
Clément Stenac committed
217
{
218
    libvlc_media_instance_t *p_mi;
219
    assert( PL );
Clément Stenac's avatar
Clément Stenac committed
220

221
    vlc_mutex_lock( &PL->object_lock );
222
    p_mi = libvlc_media_instance_new_from_input_thread(
223
                        p_instance, PL->p_input, p_e );
224
    vlc_mutex_unlock( &PL->object_lock );
Clément Stenac's avatar
Clément Stenac committed
225

226
    return p_mi;
Clément Stenac's avatar
Clément Stenac committed
227
}