Play and add (Refs:#457)
[vlc.git] / src / control / playlist.c
1 /*****************************************************************************
2  * playlist.c: libvlc new API playlist handling functions
3  *****************************************************************************
4  * Copyright (C) 2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #include <libvlc_internal.h>
25 #include <vlc/libvlc.h>
26
27 #include <vlc/intf.h>
28
29 void libvlc_playlist_play( libvlc_instance_t *p_instance, int i_id,
30                            int i_options, char **ppsz_options,
31                            libvlc_exception_t *p_exception )
32 {
33     ///\todo Handle additionnal options
34
35     if( p_instance->p_playlist->i_size == 0 )
36     {
37         libvlc_exception_raise( p_exception, "Empty playlist" );
38         return;
39     }
40     if( i_id >= 0 )
41     {
42         /* Always use the current view when using libvlc */
43         playlist_view_t *p_view;
44         playlist_item_t *p_item;
45
46         if( p_instance->p_playlist->status.i_view == -1 )
47         {
48             playlist_Control( p_instance->p_playlist, PLAYLIST_GOTO,
49                               i_id );
50         }
51         p_view = playlist_ViewFind( p_instance->p_playlist,
52                                     p_instance->p_playlist->status.i_view );
53         if( !p_view )
54         {
55              libvlc_exception_raise( p_exception,
56                                      "Unable to find current playlist view ");
57              return;
58         }
59
60         p_item = playlist_ItemGetById( p_instance->p_playlist, i_id );
61
62         if( !p_item )
63         {
64             libvlc_exception_raise( p_exception, "Unable to find item " );
65             return;
66         }
67
68         playlist_Control( p_instance->p_playlist, PLAYLIST_VIEWPLAY,
69                           p_instance->p_playlist->status.i_view,
70                           p_view->p_root, p_item );
71     }
72     else
73     {
74         playlist_Play( p_instance->p_playlist );
75     }
76 }
77
78 void libvlc_playlist_stop( libvlc_instance_t *p_instance,
79                            libvlc_exception_t *p_exception )
80 {
81     if( playlist_Stop( p_instance->p_playlist ) != VLC_SUCCESS )
82     {
83         libvlc_exception_raise( p_exception, "Empty playlist" );
84     }
85 }
86
87 void libvlc_playlist_clear( libvlc_instance_t *p_instance,
88                            libvlc_exception_t *p_exception )
89 {
90     playlist_Clear( p_instance->p_playlist );
91 }
92
93 int libvlc_playlist_add( libvlc_instance_t *p_instance, const char *psz_uri,
94                          const char *psz_name, libvlc_exception_t *p_exception )
95 {
96     return libvlc_playlist_add_extended( p_instance, psz_uri, psz_name,
97                                          0, NULL, p_exception );
98 }
99
100 int libvlc_playlist_add_extended( libvlc_instance_t *p_instance,
101                                   const char *psz_uri, const char *psz_name,
102                                   int i_options, const char **ppsz_options,
103                                   libvlc_exception_t *p_exception )
104 {
105     return playlist_AddExt( p_instance->p_playlist, psz_uri, psz_name,
106                             PLAYLIST_INSERT, PLAYLIST_END, -1, ppsz_options,
107                             i_options );
108 }
109
110
111
112 libvlc_input_t * libvlc_playlist_get_input( libvlc_instance_t *p_instance,
113                                             libvlc_exception_t *p_exception )
114 {
115     libvlc_input_t *p_input;
116
117     vlc_mutex_lock( &p_instance->p_playlist->object_lock );
118     if( p_instance->p_playlist->p_input == NULL )
119     {
120         libvlc_exception_raise( p_exception, "No active input" );
121         vlc_mutex_unlock( &p_instance->p_playlist->object_lock );
122         return NULL;
123     }
124     p_input = (libvlc_input_t *)malloc( sizeof( libvlc_input_t ) );
125
126     p_input->i_input_id = p_instance->p_playlist->p_input->i_object_id;
127     p_input->p_instance = p_instance;
128     vlc_mutex_unlock( &p_instance->p_playlist->object_lock );
129
130     return p_input;
131 }