]> git.sesse.net Git - vlc/blob - src/playlist/item.c
FSF address change.
[vlc] / src / playlist / item.c
1 /*****************************************************************************
2  * item.c : Playlist item functions
3  *****************************************************************************
4  * Copyright (C) 1999-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Samuel Hocevar <sam@zoy.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 <stdlib.h>                                      /* free(), strtol() */
24 #include <stdio.h>                                              /* sprintf() */
25 #include <string.h>                                            /* strerror() */
26
27 #include <vlc/vlc.h>
28 #include <vlc/input.h>
29
30 #include "vlc_input.h"
31 #include "vlc_playlist.h"
32
33 static void GuessType( input_item_t *p_item);
34
35 /**
36  * Create a new item, without adding it to the playlist
37  *
38  * \param p_obj a vlc object (anyone will do)
39  * \param psz_uri the mrl of the item
40  * \param psz_name a text giving a name or description of the item
41  * \return the new item or NULL on failure
42  */
43
44 playlist_item_t * __playlist_ItemNew( vlc_object_t *p_obj,
45                                       const char *psz_uri,
46                                       const char *psz_name )
47 {
48     return playlist_ItemNewWithType( p_obj, psz_uri,
49                                      psz_name, ITEM_TYPE_UNKNOWN );
50 }
51
52 playlist_item_t * playlist_ItemNewWithType( vlc_object_t *p_obj,
53                                             const char *psz_uri,
54                                             const char *psz_name,
55                                             int i_type )
56 {
57     playlist_item_t * p_item;
58
59     if( psz_uri == NULL) return NULL;
60
61     p_item = malloc( sizeof( playlist_item_t ) );
62     if( p_item == NULL ) return NULL;
63
64     memset( p_item, 0, sizeof( playlist_item_t ) );
65
66     vlc_input_item_Init( p_obj, &p_item->input );
67
68     p_item->input.psz_uri = strdup( psz_uri );
69
70     if( psz_name != NULL ) p_item->input.psz_name = strdup( psz_name );
71     else p_item->input.psz_name = strdup ( psz_uri );
72
73     p_item->input.i_type = i_type;
74
75     p_item->b_enabled = VLC_TRUE;
76     p_item->i_nb_played = 0;
77
78     p_item->i_children = -1;
79     p_item->pp_children = NULL;
80
81     p_item->i_flags = 0;
82     p_item->i_flags |= PLAYLIST_SKIP_FLAG;
83     p_item->i_flags |= PLAYLIST_SAVE_FLAG;
84
85     p_item->input.i_duration = -1;
86     p_item->input.ppsz_options = NULL;
87     p_item->input.i_options = 0;
88
89     vlc_mutex_init( p_obj, &p_item->input.lock );
90
91     if( p_item->input.i_type == ITEM_TYPE_UNKNOWN )
92         GuessType( &p_item->input );
93
94     return p_item;
95 }
96
97 /**
98  * Copy a playlist item
99  *
100  * Creates a new item with name, mrl and meta infor like the
101  * source. Does not copy children for node type items.
102  * \param p_obj any vlc object, needed for mutex init
103  * \param p_item the item to copy
104  * \return pointer to the new item, or NULL on error
105  * \note function takes the lock on p_item
106  */
107 playlist_item_t *__playlist_ItemCopy( vlc_object_t *p_obj,
108                                       playlist_item_t *p_item )
109 {
110     playlist_item_t *p_res;
111     int i;
112     vlc_mutex_lock( &p_item->input.lock );
113
114     p_res = malloc( sizeof( playlist_item_t ) );
115     if( p_res == NULL )
116     {
117         vlc_mutex_unlock( &p_item->input.lock );
118         return NULL;
119     }
120
121     *p_res = *p_item;
122     vlc_mutex_init( p_obj, &p_res->input.lock );
123
124     if( p_item->input.i_options )
125         p_res->input.ppsz_options =
126             malloc( p_item->input.i_options * sizeof(char*) );
127     for( i = 0; i < p_item->input.i_options; i++ )
128     {
129         p_res->input.ppsz_options[i] = strdup( p_item->input.ppsz_options[i] );
130     }
131
132     if( p_item->i_children != -1 )
133     {
134         msg_Warn( p_obj, "not copying playlist items children" );
135         p_res->i_children = -1;
136         p_res->pp_children = NULL;
137     }
138     p_res->i_parents = 0;
139     p_res->pp_parents = NULL;
140     
141     if( p_item->input.psz_name )
142         p_res->input.psz_name = strdup( p_item->input.psz_name );
143     if( p_item->input.psz_uri )
144         p_res->input.psz_uri = strdup( p_item->input.psz_uri );
145     
146     if( p_item->input.i_es )
147     {
148         p_res->input.es =
149             (es_format_t**)malloc( p_item->input.i_es * sizeof(es_format_t*));
150         for( i = 0; i < p_item->input.i_es; i++ )
151         {
152             p_res->input.es[ i ] = (es_format_t*)malloc(sizeof(es_format_t*));
153             es_format_Copy( p_res->input.es[ i ],
154                          p_item->input.es[ i ] );
155         }
156     }
157     if( p_item->input.i_categories )
158     {
159         p_res->input.pp_categories = NULL;
160         p_res->input.i_categories = 0;
161         for( i = 0; i < p_item->input.i_categories; i++ )
162         {
163             info_category_t *p_incat;
164             p_incat = p_item->input.pp_categories[i];
165             if( p_incat->i_infos )
166             {
167                 int j;
168                 for( j = 0; j < p_incat->i_infos; j++ )
169                 {
170                     vlc_input_item_AddInfo( &p_res->input, p_incat->psz_name,
171                                             p_incat->pp_infos[j]->psz_name,
172                                             "%s", /* to be safe */
173                                             p_incat->pp_infos[j]->psz_value );
174                 }
175             }
176         }
177     }
178
179     vlc_mutex_unlock( &p_item->input.lock );
180     return p_res;
181 }
182
183 /**
184  * Deletes a playlist item
185  *
186  * \param p_item the item to delete
187  * \return nothing
188  */
189 int playlist_ItemDelete( playlist_item_t *p_item )
190 {
191     vlc_mutex_lock( &p_item->input.lock );
192
193     if( p_item->input.psz_name ) free( p_item->input.psz_name );
194     if( p_item->input.psz_uri ) free( p_item->input.psz_uri );
195
196     /* Free the info categories */
197     if( p_item->input.i_categories > 0 )
198     {
199         int i, j;
200
201         for( i = 0; i < p_item->input.i_categories; i++ )
202         {
203             info_category_t *p_category = p_item->input.pp_categories[i];
204
205             for( j = 0; j < p_category->i_infos; j++)
206             {
207                 if( p_category->pp_infos[j]->psz_name )
208                 {
209                     free( p_category->pp_infos[j]->psz_name);
210                 }
211                 if( p_category->pp_infos[j]->psz_value )
212                 {
213                     free( p_category->pp_infos[j]->psz_value );
214                 }
215                 free( p_category->pp_infos[j] );
216             }
217
218             if( p_category->i_infos ) free( p_category->pp_infos );
219             if( p_category->psz_name ) free( p_category->psz_name );
220             free( p_category );
221         }
222
223         free( p_item->input.pp_categories );
224     }
225
226     for( ; p_item->input.i_options > 0; p_item->input.i_options-- )
227     {
228         free( p_item->input.ppsz_options[p_item->input.i_options - 1] );
229         if( p_item->input.i_options == 1 ) free( p_item->input.ppsz_options );
230     }
231
232     for( ; p_item->i_parents > 0 ; )
233     {
234         struct item_parent_t *p_parent =  p_item->pp_parents[0];
235         REMOVE_ELEM( p_item->pp_parents, p_item->i_parents, 0 );
236         free( p_parent );
237     }
238
239     vlc_mutex_unlock( &p_item->input.lock );
240     vlc_mutex_destroy( &p_item->input.lock );
241
242     free( p_item );
243
244     return VLC_SUCCESS;
245 }
246
247 /**
248  *  Add a option to one item ( no need for p_playlist )
249  *
250  * \param p_item the item on which we want the info
251  * \param psz_option the option
252  * \return 0 on success
253  */
254 int playlist_ItemAddOption( playlist_item_t *p_item, const char *psz_option )
255 {
256     if( !psz_option ) return VLC_EGENERIC;
257
258     vlc_mutex_lock( &p_item->input.lock );
259     INSERT_ELEM( p_item->input.ppsz_options, p_item->input.i_options,
260                  p_item->input.i_options, strdup( psz_option ) );
261     vlc_mutex_unlock( &p_item->input.lock );
262
263     return VLC_SUCCESS;
264 }
265
266 /**
267  * Add a parent to an item
268  *
269  * \param p_item the item
270  * \param i_view the view in which the parent is
271  * \param p_parent the parent to add
272  * \return nothing
273  */
274 int playlist_ItemAddParent( playlist_item_t *p_item, int i_view,
275                             playlist_item_t *p_parent )
276 {
277    vlc_bool_t b_found = VLC_FALSE;
278    int i;
279
280    for( i= 0; i< p_item->i_parents ; i++ )
281    {
282        if( p_item->pp_parents[i]->i_view == i_view )
283
284        {
285            b_found = VLC_TRUE;
286            break;
287        }
288    }
289    if( b_found == VLC_FALSE )
290    {
291
292        struct item_parent_t *p_ip = (struct item_parent_t *)
293                malloc(sizeof(struct item_parent_t) );
294        p_ip->i_view = i_view;
295        p_ip->p_parent = p_parent;
296
297        INSERT_ELEM( p_item->pp_parents,
298                     p_item->i_parents, p_item->i_parents,
299                     p_ip );
300    }
301    return VLC_SUCCESS;
302 }
303
304 /**
305  * Copy all parents from parent to child
306  */
307 int playlist_CopyParents( playlist_item_t *p_parent,
308                            playlist_item_t *p_child )
309 {
310     int i=0;
311     for( i= 0 ; i< p_parent->i_parents; i ++ )
312     {
313         playlist_ItemAddParent( p_child,
314                                 p_parent->pp_parents[i]->i_view,
315                                 p_parent );
316     }
317     return VLC_SUCCESS;
318 }
319
320
321 /**********************************************************************
322  * playlist_item_t structure accessors
323  * These functions give access to the fields of the playlist_item_t
324  * structure
325  **********************************************************************/
326
327
328 /**
329  * Set the name of a playlist item
330  *
331  * \param p_item the item
332  * \param psz_name the new name
333  * \return VLC_SUCCESS on success, VLC_EGENERIC on failure
334  */
335 int playlist_ItemSetName( playlist_item_t *p_item, char *psz_name )
336 {
337     if( psz_name && p_item )
338     {
339         if( p_item->input.psz_name ) free( p_item->input.psz_name );
340         p_item->input.psz_name = strdup( psz_name );
341         return VLC_SUCCESS;
342     }
343     return VLC_EGENERIC;
344 }
345
346 /**
347  * Set the duration of a playlist item
348  * This function must be entered with the item lock
349  *
350  * \param p_item the item
351  * \param i_duration the new duration
352  * \return VLC_SUCCESS on success, VLC_EGENERIC on failure
353  */
354 int playlist_ItemSetDuration( playlist_item_t *p_item, mtime_t i_duration )
355 {
356     char psz_buffer[MSTRTIME_MAX_SIZE];
357     if( p_item )
358     {
359         p_item->input.i_duration = i_duration;
360         if( i_duration != -1 )
361         {
362             secstotimestr( psz_buffer, (int)(i_duration/1000000) );
363         }
364         else
365         {
366             memcpy( psz_buffer, "--:--:--", sizeof("--:--:--") );
367         }
368         vlc_input_item_AddInfo( &p_item->input, _("General") , _("Duration"),
369                                 "%s", psz_buffer );
370
371         return VLC_SUCCESS;
372     }
373     return VLC_EGENERIC;
374 }
375
376 /*
377  * Guess the type of the item using the beginning of the mrl */
378 static void GuessType( input_item_t *p_item)
379 {
380     int i;
381     static struct { char *psz_search; int i_type; }  types_array[] =
382     {
383         { "http", ITEM_TYPE_NET },
384         { "dvd", ITEM_TYPE_DISC },
385         { "cdda", ITEM_TYPE_CDDA },
386         { "mms", ITEM_TYPE_NET },
387         { "rtsp", ITEM_TYPE_NET },
388         { "udp", ITEM_TYPE_NET },
389         { "rtp", ITEM_TYPE_NET },
390         { "vcd", ITEM_TYPE_DISC },
391         { "v4l", ITEM_TYPE_CARD },
392         { "dshow", ITEM_TYPE_CARD },
393         { "pvr", ITEM_TYPE_CARD },
394         { "dvb", ITEM_TYPE_CARD },
395         { "qpsk", ITEM_TYPE_CARD },
396         { "sdp", ITEM_TYPE_NET },
397         { NULL, 0 }
398     };
399
400 #if 0 /* Unused */
401     static struct { char *psz_search; int i_type; } exts_array[] =
402     {
403         { "mp3", ITEM_TYPE_AFILE },
404         { NULL, 0 }
405     };
406 #endif
407
408     for( i = 0; types_array[i].psz_search != NULL; i++ )
409     {
410         if( !strncmp( p_item->psz_uri, types_array[i].psz_search,
411                       strlen( types_array[i].psz_search ) ) )
412         {
413             p_item->i_type = types_array[i].i_type;
414             return;
415         }
416     }
417     p_item->i_type = ITEM_TYPE_VFILE;
418 }