Main Page | Modules | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages

item.c

00001 /*****************************************************************************
00002  * item.c : Playlist item functions
00003  *****************************************************************************
00004  * Copyright (C) 1999-2004 the VideoLAN team
00005  * $Id: item.c 11699 2005-07-11 08:52:14Z sam $
00006  *
00007  * Authors: Samuel Hocevar <[email protected]>
00008  *
00009  * This program is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
00022  *****************************************************************************/
00023 #include <stdlib.h>                                      /* free(), strtol() */
00024 #include <stdio.h>                                              /* sprintf() */
00025 #include <string.h>                                            /* strerror() */
00026 
00027 #include <vlc/vlc.h>
00028 #include <vlc/input.h>
00029 
00030 #include "vlc_input.h"
00031 #include "vlc_playlist.h"
00032 
00033 static void GuessType( input_item_t *p_item);
00034 
00044 playlist_item_t * __playlist_ItemNew( vlc_object_t *p_obj,
00045                                       const char *psz_uri,
00046                                       const char *psz_name )
00047 {
00048     return playlist_ItemNewWithType( p_obj, psz_uri,
00049                                      psz_name, ITEM_TYPE_UNKNOWN );
00050 }
00051 
00052 playlist_item_t * playlist_ItemNewWithType( vlc_object_t *p_obj,
00053                                             const char *psz_uri,
00054                                             const char *psz_name,
00055                                             int i_type )
00056 {
00057     playlist_item_t * p_item;
00058 
00059     if( psz_uri == NULL) return NULL;
00060 
00061     p_item = malloc( sizeof( playlist_item_t ) );
00062     if( p_item == NULL ) return NULL;
00063 
00064     memset( p_item, 0, sizeof( playlist_item_t ) );
00065 
00066     p_item->input.psz_uri = strdup( psz_uri );
00067 
00068     if( psz_name != NULL ) p_item->input.psz_name = strdup( psz_name );
00069     else p_item->input.psz_name = strdup ( psz_uri );
00070 
00071     p_item->input.i_type = i_type;
00072 
00073     p_item->b_enabled = VLC_TRUE;
00074     p_item->i_nb_played = 0;
00075 
00076     p_item->i_children = -1;
00077     p_item->pp_children = NULL;
00078 
00079     p_item->i_flags = 0;
00080     p_item->i_flags |= PLAYLIST_SKIP_FLAG;
00081     p_item->i_flags |= PLAYLIST_SAVE_FLAG;
00082 
00083     p_item->input.i_duration = -1;
00084     p_item->input.ppsz_options = NULL;
00085     p_item->input.i_options = 0;
00086 
00087     vlc_mutex_init( p_obj, &p_item->input.lock );
00088 
00089     if( p_item->input.i_type == ITEM_TYPE_UNKNOWN )
00090         GuessType( &p_item->input );
00091 
00092     return p_item;
00093 }
00094 
00105 playlist_item_t *__playlist_ItemCopy( vlc_object_t *p_obj,
00106                                       playlist_item_t *p_item )
00107 {
00108     playlist_item_t *p_res;
00109     int i;
00110     vlc_mutex_lock( &p_item->input.lock );
00111 
00112     p_res = malloc( sizeof( playlist_item_t ) );
00113     if( p_res == NULL )
00114     {
00115         vlc_mutex_unlock( &p_item->input.lock );
00116         return NULL;
00117     }
00118 
00119     *p_res = *p_item;
00120     vlc_mutex_init( p_obj, &p_res->input.lock );
00121 
00122     if( p_item->input.i_options )
00123         p_res->input.ppsz_options =
00124             malloc( p_item->input.i_options * sizeof(char*) );
00125     for( i = 0; i < p_item->input.i_options; i++ )
00126     {
00127         p_res->input.ppsz_options[i] = strdup( p_item->input.ppsz_options[i] );
00128     }
00129 
00130     if( p_item->i_children != -1 )
00131     {
00132         msg_Warn( p_obj, "not copying playlist items children" );
00133         p_res->i_children = -1;
00134         p_res->pp_children = NULL;
00135     }
00136     p_res->i_parents = 0;
00137     p_res->pp_parents = NULL;
00138     
00139     if( p_item->input.psz_name )
00140         p_res->input.psz_name = strdup( p_item->input.psz_name );
00141     if( p_item->input.psz_uri )
00142         p_res->input.psz_uri = strdup( p_item->input.psz_uri );
00143     
00144     if( p_item->input.i_es )
00145     {
00146         p_res->input.es =
00147             (es_format_t**)malloc( p_item->input.i_es * sizeof(es_format_t*));
00148         for( i = 0; i < p_item->input.i_es; i++ )
00149         {
00150             p_res->input.es[ i ] = (es_format_t*)malloc(sizeof(es_format_t*));
00151             es_format_Copy( p_res->input.es[ i ],
00152                          p_item->input.es[ i ] );
00153         }
00154     }
00155     if( p_item->input.i_categories )
00156     {
00157         p_res->input.pp_categories = NULL;
00158         p_res->input.i_categories = 0;
00159         for( i = 0; i < p_item->input.i_categories; i++ )
00160         {
00161             info_category_t *p_incat;
00162             p_incat = p_item->input.pp_categories[i];
00163             if( p_incat->i_infos )
00164             {
00165                 int j;
00166                 for( j = 0; j < p_incat->i_infos; j++ )
00167                 {
00168                     vlc_input_item_AddInfo( &p_res->input, p_incat->psz_name,
00169                                             p_incat->pp_infos[j]->psz_name,
00170                                             "%s", /* to be safe */
00171                                             p_incat->pp_infos[j]->psz_value );
00172                 }
00173             }
00174         }
00175     }
00176 
00177     vlc_mutex_unlock( &p_item->input.lock );
00178     return p_res;
00179 }
00180 
00187 int playlist_ItemDelete( playlist_item_t *p_item )
00188 {
00189     vlc_mutex_lock( &p_item->input.lock );
00190 
00191     if( p_item->input.psz_name ) free( p_item->input.psz_name );
00192     if( p_item->input.psz_uri ) free( p_item->input.psz_uri );
00193 
00194     /* Free the info categories */
00195     if( p_item->input.i_categories > 0 )
00196     {
00197         int i, j;
00198 
00199         for( i = 0; i < p_item->input.i_categories; i++ )
00200         {
00201             info_category_t *p_category = p_item->input.pp_categories[i];
00202 
00203             for( j = 0; j < p_category->i_infos; j++)
00204             {
00205                 if( p_category->pp_infos[j]->psz_name )
00206                 {
00207                     free( p_category->pp_infos[j]->psz_name);
00208                 }
00209                 if( p_category->pp_infos[j]->psz_value )
00210                 {
00211                     free( p_category->pp_infos[j]->psz_value );
00212                 }
00213                 free( p_category->pp_infos[j] );
00214             }
00215 
00216             if( p_category->i_infos ) free( p_category->pp_infos );
00217             if( p_category->psz_name ) free( p_category->psz_name );
00218             free( p_category );
00219         }
00220 
00221         free( p_item->input.pp_categories );
00222     }
00223 
00224     for( ; p_item->input.i_options > 0; p_item->input.i_options-- )
00225     {
00226         free( p_item->input.ppsz_options[p_item->input.i_options - 1] );
00227         if( p_item->input.i_options == 1 ) free( p_item->input.ppsz_options );
00228     }
00229 
00230     for( ; p_item->i_parents > 0 ; )
00231     {
00232         struct item_parent_t *p_parent =  p_item->pp_parents[0];
00233         REMOVE_ELEM( p_item->pp_parents, p_item->i_parents, 0 );
00234         free( p_parent );
00235     }
00236 
00237     vlc_mutex_unlock( &p_item->input.lock );
00238     vlc_mutex_destroy( &p_item->input.lock );
00239 
00240     free( p_item );
00241 
00242     return VLC_SUCCESS;
00243 }
00244 
00252 int playlist_ItemAddOption( playlist_item_t *p_item, const char *psz_option )
00253 {
00254     if( !psz_option ) return VLC_EGENERIC;
00255 
00256     vlc_mutex_lock( &p_item->input.lock );
00257     INSERT_ELEM( p_item->input.ppsz_options, p_item->input.i_options,
00258                  p_item->input.i_options, strdup( psz_option ) );
00259     vlc_mutex_unlock( &p_item->input.lock );
00260 
00261     return VLC_SUCCESS;
00262 }
00263 
00272 int playlist_ItemAddParent( playlist_item_t *p_item, int i_view,
00273                             playlist_item_t *p_parent )
00274 {
00275    vlc_bool_t b_found = VLC_FALSE;
00276    int i;
00277 
00278    for( i= 0; i< p_item->i_parents ; i++ )
00279    {
00280        if( p_item->pp_parents[i]->i_view == i_view )
00281 
00282        {
00283            b_found = VLC_TRUE;
00284            break;
00285        }
00286    }
00287    if( b_found == VLC_FALSE )
00288    {
00289 
00290        struct item_parent_t *p_ip = (struct item_parent_t *)
00291                malloc(sizeof(struct item_parent_t) );
00292        p_ip->i_view = i_view;
00293        p_ip->p_parent = p_parent;
00294 
00295        INSERT_ELEM( p_item->pp_parents,
00296                     p_item->i_parents, p_item->i_parents,
00297                     p_ip );
00298    }
00299    return VLC_SUCCESS;
00300 }
00301 
00305 int playlist_CopyParents( playlist_item_t *p_parent,
00306                            playlist_item_t *p_child )
00307 {
00308     int i=0;
00309     for( i= 0 ; i< p_parent->i_parents; i ++ )
00310     {
00311         playlist_ItemAddParent( p_child,
00312                                 p_parent->pp_parents[i]->i_view,
00313                                 p_parent );
00314     }
00315     return VLC_SUCCESS;
00316 }
00317 
00318 
00319 /**********************************************************************
00320  * playlist_item_t structure accessors
00321  * These functions give access to the fields of the playlist_item_t
00322  * structure
00323  **********************************************************************/
00324 
00325 
00333 int playlist_ItemSetName( playlist_item_t *p_item, char *psz_name )
00334 {
00335     if( psz_name && p_item )
00336     {
00337         if( p_item->input.psz_name ) free( p_item->input.psz_name );
00338         p_item->input.psz_name = strdup( psz_name );
00339         return VLC_SUCCESS;
00340     }
00341     return VLC_EGENERIC;
00342 }
00343 
00352 int playlist_ItemSetDuration( playlist_item_t *p_item, mtime_t i_duration )
00353 {
00354     char psz_buffer[MSTRTIME_MAX_SIZE];
00355     if( p_item )
00356     {
00357         p_item->input.i_duration = i_duration;
00358         if( i_duration != -1 )
00359         {
00360             secstotimestr( psz_buffer, (int)(i_duration/1000000) );
00361         }
00362         else
00363         {
00364             memcpy( psz_buffer, "--:--:--", sizeof("--:--:--") );
00365         }
00366         vlc_input_item_AddInfo( &p_item->input, _("General") , _("Duration"),
00367                                 "%s", psz_buffer );
00368 
00369         return VLC_SUCCESS;
00370     }
00371     return VLC_EGENERIC;
00372 }
00373 
00374 /*
00375  * Guess the type of the item using the beginning of the mrl */
00376 static void GuessType( input_item_t *p_item)
00377 {
00378     int i;
00379     static struct { char *psz_search; int i_type; }  types_array[] =
00380     {
00381         { "http", ITEM_TYPE_NET },
00382         { "dvd", ITEM_TYPE_DISC },
00383         { "cdda", ITEM_TYPE_CDDA },
00384         { "mms", ITEM_TYPE_NET },
00385         { "rtsp", ITEM_TYPE_NET },
00386         { "udp", ITEM_TYPE_NET },
00387         { "rtp", ITEM_TYPE_NET },
00388         { "vcd", ITEM_TYPE_DISC },
00389         { "v4l", ITEM_TYPE_CARD },
00390         { "dshow", ITEM_TYPE_CARD },
00391         { "pvr", ITEM_TYPE_CARD },
00392         { "dvb", ITEM_TYPE_CARD },
00393         { "qpsk", ITEM_TYPE_CARD },
00394         { "sdp", ITEM_TYPE_NET },
00395         { NULL, 0 }
00396     };
00397 
00398 #if 0 /* Unused */
00399     static struct { char *psz_search; int i_type; } exts_array[] =
00400     {
00401         { "mp3", ITEM_TYPE_AFILE },
00402         { NULL, 0 }
00403     };
00404 #endif
00405 
00406     for( i = 0; types_array[i].psz_search != NULL; i++ )
00407     {
00408         if( !strncmp( p_item->psz_uri, types_array[i].psz_search,
00409                       strlen( types_array[i].psz_search ) ) )
00410         {
00411             p_item->i_type = types_array[i].i_type;
00412             return;
00413         }
00414     }
00415     p_item->i_type = ITEM_TYPE_VFILE;
00416 }

Generated on Tue Dec 20 10:15:01 2005 for vlc-0.8.4a by  doxygen 1.4.2