]> git.sesse.net Git - vlc/blob - modules/demux/playlist/gvp.c
src/input/item.c: if we don't have an item name and item is a file, then only use...
[vlc] / modules / demux / playlist / gvp.c
1 /*****************************************************************************
2  * gvp.c: Google Video Playlist demuxer
3  *****************************************************************************
4  * Copyright (C) 2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Antoine Cellerier <dionoea @t videolan d.t 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 /**
25  * Format seems to be:
26  * gvp_version:<version> (1.1)
27  * url:<the media's url>
28  * docid:<integer>
29  * duration:<integer ms ?>
30  * title:<the title>
31  * description:<desc line1>^M
32  * description:<desc linei>^M
33  * description:<desc final line (no ^M)>
34  * lines starting with # are comments
35  */
36
37 /*****************************************************************************
38  * Preamble
39  *****************************************************************************/
40 #include <vlc/vlc.h>
41 #include <vlc/input.h>
42 #include <vlc/intf.h>
43
44 #include <errno.h>                                                 /* ENOMEM */
45 #include "playlist.h"
46
47 #define MAX_LINE 1024
48
49 struct demux_sys_t
50 {
51     playlist_t *p_playlist;
52     playlist_item_t *p_current;
53     playlist_item_t *p_item_in_category;
54     int i_parent_id;
55 };
56
57 /*****************************************************************************
58  * Local prototypes
59  *****************************************************************************/
60 static int Demux( demux_t *p_demux);
61 static int Control( demux_t *p_demux, int i_query, va_list args );
62
63 /*****************************************************************************
64  * Import_GVP: main import function
65  *****************************************************************************/
66 int E_(Import_GVP)( vlc_object_t *p_this )
67 {
68     demux_t *p_demux = (demux_t *)p_this;
69     byte_t *p_peek;
70
71     /* FIXME: we need to skip comment lines !!!! */
72     CHECK_PEEK( p_peek, 12 );
73     if( !POKE( p_peek, "gvp_version:", 12 ) )
74     {
75         return VLC_EGENERIC;
76     }
77
78     STANDARD_DEMUX_INIT_MSG(  "using Google Video Playlist (gvp) import" )
79     p_demux->pf_control = Control;
80     p_demux->pf_demux = Demux;
81     MALLOC_ERR( p_demux->p_sys, demux_sys_t );
82     p_demux->p_sys->p_playlist = NULL;
83
84     return VLC_SUCCESS;
85 }
86
87 /*****************************************************************************
88  * Deactivate: frees unused data
89  *****************************************************************************/
90 void E_(Close_GVP)( vlc_object_t *p_this )
91 {
92     demux_t *p_demux = (demux_t *)p_this;
93     demux_sys_t *p_sys = p_demux->p_sys;
94
95     if( p_sys->p_playlist )
96         vlc_object_release( p_sys->p_playlist );
97     free( p_sys );
98 }
99
100 static int Demux( demux_t *p_demux )
101 {
102     demux_sys_t *p_sys = p_demux->p_sys;
103
104     char *psz_line;
105     char *psz_attrvalue;
106
107     char *psz_version = NULL;
108     char *psz_url = NULL;
109     char *psz_docid = NULL;
110     int i_duration = -1;
111     char *psz_title = NULL;
112     char *psz_description = NULL;
113
114     INIT_PLAYLIST_STUFF;
115
116     p_sys->p_playlist = p_playlist;
117     p_sys->p_current = p_current;
118     p_sys->i_parent_id = i_parent_id;
119     p_sys->p_item_in_category = p_item_in_category;
120
121     while( ( psz_line = stream_ReadLine( p_demux->s ) ) )
122     {
123         if( *psz_line == '#' )
124         {
125             /* This is a comment */
126             free( psz_line );
127             continue;
128         }
129         psz_attrvalue = strchr( psz_line, ':' );
130         if( !psz_attrvalue )
131         {
132             msg_Dbg( p_demux, "Unable to parse line (%s)", psz_line );
133             free( psz_line );
134             continue;
135         }
136         *psz_attrvalue = '\0';
137         psz_attrvalue++;
138         if( !strcmp( psz_line, "gvp_version" ) )
139         {
140             psz_version = strdup( psz_attrvalue );
141         }
142         else if( !strcmp( psz_line, "url" ) )
143         {
144             psz_url = strdup( psz_attrvalue );
145         }
146         else if( !strcmp( psz_line, "docid" ) )
147         {
148             psz_docid = strdup( psz_attrvalue );
149         }
150         else if( !strcmp( psz_line, "duration" ) )
151         {
152             i_duration = atoi( psz_attrvalue );
153         }
154         else if( !strcmp( psz_line, "title" ) )
155         {
156             psz_title = strdup( psz_attrvalue );
157         }
158         else if( !strcmp( psz_line, "description" ) )
159         {
160             char *buf;
161             if( !psz_description )
162             {
163                 psz_description = strdup( psz_attrvalue );
164             }
165             else
166             {
167                 /* handle multi-line descriptions */
168                 buf = malloc( strlen( psz_description )
169                             + strlen( psz_attrvalue ) + 2 );
170                 sprintf( buf, "%s\n%s", psz_description, psz_attrvalue );
171                 free( psz_description );
172                 psz_description = buf;
173             }
174             /* remove ^M char at the end of the line (if any) */
175             buf = psz_description + strlen( psz_description );
176             if( buf != psz_description )
177             {
178                 buf--;
179                 if( *buf == '\r' ) *buf = '\0';
180             }
181         }
182         free( psz_line );
183     }
184
185     if( !psz_url )
186     {
187         msg_Err( p_demux, "URL not found" );
188     }
189     else
190     {
191         p_input = input_ItemNewExt( p_sys->p_playlist,
192                                     psz_url, psz_title, 0, NULL, -1 );
193 #define SADD_INFO( type, field ) if( field ) { input_ItemAddInfo( \
194                     p_input, _("Google Video"), _(type), "%s", field ) ; }
195         SADD_INFO( "gvp_version", psz_version );
196         SADD_INFO( "docid", psz_docid );
197         SADD_INFO( "description", psz_description );
198         playlist_AddWhereverNeeded( p_sys->p_playlist, p_input,
199                             p_sys->p_current, p_sys->p_item_in_category,
200                             (p_sys->i_parent_id > 0 ) ? VLC_TRUE: VLC_FALSE,
201                             PLAYLIST_APPEND );
202     }
203
204     HANDLE_PLAY_AND_RELEASE;
205
206     free( psz_version );
207     free( psz_url );
208     free( psz_docid );
209     free( psz_title );
210     free( psz_description );
211
212     p_sys->p_playlist = NULL;
213
214     return VLC_SUCCESS;
215 }
216
217 static int Control( demux_t *p_demux, int i_query, va_list args )
218 {
219     return VLC_EGENERIC;
220 }