]> git.sesse.net Git - vlc/blob - src/control/media_list_player.c
control/media_list_player.c: Initialize p_mlist, and p_mi.
[vlc] / src / control / media_list_player.c
1 /*****************************************************************************
2  * media_list_player.c: libvlc new API media_list player functions
3  *****************************************************************************
4  * Copyright (C) 2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Pierre d'Herbemont <pdherbemont # 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 #include "libvlc_internal.h"
24 #include <vlc/libvlc.h>
25
26 /*
27  * Private functions
28  */
29
30 /**************************************************************************
31  *       get_next_index (private)
32  *
33  * Simple next item fetcher.
34  **************************************************************************/
35 static int get_next_index( libvlc_media_list_player_t * p_mlp )
36 {
37         /* We are entered with libvlc_media_list_lock( p_mlp->p_list ) */
38         
39         int next = p_mlp->i_current_playing_index + 1;
40
41         if( libvlc_media_list_count( p_mlp->p_mlist, NULL ) >= next )
42                 return -1; /* no more to play */
43
44         return next;
45 }
46
47 /**************************************************************************
48  *       media_instance_reached_end (private) (Event Callback)
49  **************************************************************************/
50 static void 
51 media_instance_reached_end( const libvlc_event_t * p_event,
52                             void * p_user_data )
53 {
54         libvlc_media_list_player_t * p_mlp = p_user_data;
55         libvlc_media_instance_t * p_mi = p_event->p_obj;
56     libvlc_media_descriptor_t *p_md, * p_current_md;
57     p_md = libvlc_media_instance_get_media_descriptor( p_mi, NULL );
58     /* XXX: need if p_mlp->p_current_playing_index is beyond */
59     p_current_md = libvlc_media_list_item_at_index(
60                         p_mlp->p_mlist,
61                         p_mlp->i_current_playing_index,
62                         NULL );
63         if( p_md != p_current_md )
64         {
65                 msg_Warn( p_mlp->p_libvlc_instance->p_libvlc_int,
66                                   "We are not sync-ed with the media instance" );
67         libvlc_media_descriptor_release( p_md );
68         libvlc_media_descriptor_release( p_current_md );
69                 return;
70         }
71     libvlc_media_descriptor_release( p_md );
72     libvlc_media_descriptor_release( p_current_md );
73         libvlc_media_list_player_next( p_mlp, NULL );
74 }
75
76 /**************************************************************************
77  *       playlist_item_deleted (private) (Event Callback)
78  **************************************************************************/
79 static void 
80 mlist_item_deleted( const libvlc_event_t * p_event, void * p_user_data )
81 {
82     libvlc_media_descriptor_t * p_current_md;    
83         libvlc_media_list_player_t * p_mlp = p_user_data;
84         libvlc_media_list_t * p_emitting_mlist = p_event->p_obj;
85     /* XXX: need if p_mlp->p_current_playing_index is beyond */
86     p_current_md = libvlc_media_list_item_at_index(
87                         p_mlp->p_mlist,
88                         p_mlp->i_current_playing_index,
89                         NULL );
90
91         if( p_event->u.media_list_item_deleted.item == p_current_md &&
92             p_emitting_mlist == p_mlp->p_mlist )
93         {
94                 /* We are playing this item, we choose to stop */
95                 libvlc_media_list_player_stop( p_mlp, NULL );
96         }
97 }
98
99 /**************************************************************************
100  *       install_playlist_observer (private)
101  **************************************************************************/
102 static void
103 install_playlist_observer( libvlc_media_list_player_t * p_mlp )
104 {
105         libvlc_event_attach( libvlc_media_list_event_manager( p_mlp->p_mlist, NULL ),
106             libvlc_MediaListItemDeleted, mlist_item_deleted, p_mlp, NULL );
107 }
108
109 /**************************************************************************
110  *       uninstall_playlist_observer (private)
111  **************************************************************************/
112 static void
113 uninstall_playlist_observer( libvlc_media_list_player_t * p_mlp )
114 {
115     libvlc_event_detach( libvlc_media_list_event_manager( p_mlp->p_mlist, NULL ),
116             libvlc_MediaListItemDeleted, mlist_item_deleted, p_mlp, NULL );
117 }
118
119 /**************************************************************************
120  *       install_media_instance_observer (private)
121  **************************************************************************/
122 static void
123 install_media_instance_observer( libvlc_media_list_player_t * p_mlp )
124 {
125         libvlc_event_attach( libvlc_media_instance_event_manager( p_mlp->p_mi, NULL ),
126                          libvlc_MediaInstanceReachedEnd,
127                                               media_instance_reached_end, p_mlp, NULL );
128 }
129
130
131 /**************************************************************************
132  *       uninstall_media_instance_observer (private)
133  **************************************************************************/
134 static void
135 uninstall_media_instance_observer( libvlc_media_list_player_t * p_mlp )
136 {
137         libvlc_event_detach( libvlc_media_instance_event_manager( p_mlp->p_mi, NULL ),
138                          libvlc_MediaInstanceReachedEnd,
139                                              media_instance_reached_end, p_mlp, NULL );
140 }
141 /**************************************************************************
142  *       Stop (Public)
143  **************************************************************************/
144 static vlc_bool_t
145 libvlc_media_list_player_is_playing( libvlc_media_list_player_t * p_mlp,
146                                     libvlc_exception_t * p_e )
147 {
148     libvlc_exception_raise( p_e, "Unimplemented" );
149     return 0;
150 }
151
152
153 /*
154  * Public libvlc functions
155  */
156
157 /**************************************************************************
158  *         libvlc_media_list_player_new (Public)
159  **************************************************************************/
160 libvlc_media_list_player_t *
161 libvlc_media_list_player_new( libvlc_instance_t * p_instance,
162                               libvlc_exception_t * p_e )
163 {
164     (void)p_e;
165     libvlc_media_list_player_t * p_mlp;
166     p_mlp = malloc(sizeof(libvlc_media_list_player_t));
167         p_mlp->i_current_playing_index = -1;
168     p_mlp->p_mi = NULL;
169     p_mlp->p_mlist = NULL;
170     vlc_mutex_init( p_instance->p_libvlc_int, &p_mlp->object_lock );
171     
172         return p_mlp;
173 }
174
175 /**************************************************************************
176  *         libvlc_media_list_player_release (Public)
177  **************************************************************************/
178 void libvlc_media_list_player_release( libvlc_media_list_player_t * p_mlp )
179 {
180     free(p_mlp);
181 }
182
183 /**************************************************************************
184  *        libvlc_media_list_player_set_media_instance (Public)
185  **************************************************************************/
186 void libvlc_media_list_player_set_media_instance(
187                                      libvlc_media_list_player_t * p_mlp,
188                                      libvlc_media_instance_t * p_mi,
189                                      libvlc_exception_t * p_e )
190 {
191     vlc_mutex_lock( &p_mlp->object_lock );
192
193         if( p_mlp->p_mi )
194         {
195                 uninstall_media_instance_observer( p_mlp );
196                 libvlc_media_instance_release( p_mlp->p_mi );
197         }
198         libvlc_media_instance_retain( p_mi );
199         p_mlp->p_mi = p_mi;
200
201     install_media_instance_observer( p_mlp );
202
203     vlc_mutex_unlock( &p_mlp->object_lock );
204 }
205
206 /**************************************************************************
207  *       Set a playlist (Public)
208  **************************************************************************/
209 void libvlc_media_list_player_set_media_list(
210                                      libvlc_media_list_player_t * p_mlp,
211                                      libvlc_media_list_t * p_mlist,
212                                      libvlc_exception_t * p_e )
213 {
214     vlc_mutex_lock( &p_mlp->object_lock );
215     
216         if( libvlc_media_list_player_is_playing( p_mlp, p_e ) )
217                 libvlc_media_list_player_stop( p_mlp, p_e );
218
219         if( p_mlp->p_mlist )
220         {
221                 uninstall_playlist_observer( p_mlp );
222                 libvlc_media_list_release( p_mlp->p_mlist );
223         }
224         libvlc_media_list_retain( p_mlist );
225         p_mlp->p_mlist = p_mlist;
226     
227         install_playlist_observer( p_mlp );
228
229     vlc_mutex_unlock( &p_mlp->object_lock );
230 }
231
232 /**************************************************************************
233  *        Play (Public)
234  **************************************************************************/
235 void libvlc_media_list_player_play( libvlc_media_list_player_t * p_mlp,
236                                   libvlc_exception_t * p_e )
237 {
238         libvlc_media_list_player_next( p_mlp, p_e );
239
240         if( libvlc_exception_raised( p_e ) )
241                 return;
242
243         libvlc_media_instance_play( p_mlp->p_mi, p_e );
244 }
245
246 /**************************************************************************
247  *       Stop (Public)
248  **************************************************************************/
249 void libvlc_media_list_player_stop( libvlc_media_list_player_t * p_mlp,
250                                     libvlc_exception_t * p_e )
251 {
252         libvlc_media_instance_stop( p_mlp->p_mi, p_e );
253
254     vlc_mutex_lock( &p_mlp->object_lock );
255         p_mlp->i_current_playing_index = -1;
256     vlc_mutex_unlock( &p_mlp->object_lock );
257 }
258
259 /**************************************************************************
260  *       Next (Public)
261  **************************************************************************/
262 void libvlc_media_list_player_next( libvlc_media_list_player_t * p_mlp,
263                                     libvlc_exception_t * p_e )
264 {
265         libvlc_media_descriptor_t * p_md;
266         
267         int index;
268         
269         libvlc_media_list_lock( p_mlp->p_mlist );
270
271         index = get_next_index( p_mlp );
272
273         if( index < 0 )
274         {
275                 libvlc_media_list_unlock( p_mlp->p_mlist );
276                 libvlc_exception_raise( p_e, "No more element to play" );
277                 libvlc_media_list_player_stop( p_mlp, p_e );
278                 return;
279         }
280
281         p_md = libvlc_media_list_item_at_index( p_mlp->p_mlist, index, p_e );
282         if( !p_md )
283         {
284                 libvlc_media_list_unlock( p_mlp->p_mlist );             
285                 if( !libvlc_exception_raised( p_e ) )
286                         libvlc_exception_raise( p_e, "Can't obtain a media" );
287                 return;
288         }
289
290     vlc_mutex_lock( &p_mlp->object_lock );
291         
292     p_mlp->i_current_playing_index = index;
293
294         /* We are not interested in getting media_descriptor stop event now */
295         uninstall_media_instance_observer( p_mlp );
296     libvlc_media_instance_set_media_descriptor( p_mlp->p_mi, p_md, NULL );
297 //      wait_playing_state(); /* If we want to be synchronous */
298         install_media_instance_observer( p_mlp );
299
300     vlc_mutex_unlock( &p_mlp->object_lock );
301
302         libvlc_media_list_unlock( p_mlp->p_mlist );             
303         
304         libvlc_media_descriptor_release( p_md ); /* for libvlc_media_list_item_at_index */
305 }
306