]> git.sesse.net Git - vlc/blob - modules/demux/playlist/gvp.c
BeOS / gcc 2.x fixes, patch by Cian Duffy
[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     CHECK_PEEK( p_peek, 12 );
72     if( !POKE( p_peek, "gvp_version:", 12 ) )
73     {
74         return VLC_EGENERIC;
75     }
76
77     STANDARD_DEMUX_INIT_MSG(  "using Google Video Playlist (gvp) import" )
78     p_demux->pf_control = Control;
79     p_demux->pf_demux = Demux;
80     MALLOC_ERR( p_demux->p_sys, demux_sys_t );
81     p_demux->p_sys->p_playlist = NULL;
82
83     return VLC_SUCCESS;
84 }
85
86 /*****************************************************************************
87  * Deactivate: frees unused data
88  *****************************************************************************/
89 void E_(Close_GVP)( vlc_object_t *p_this )
90 {
91     demux_t *p_demux = (demux_t *)p_this;
92     demux_sys_t *p_sys = p_demux->p_sys;
93
94     if( p_sys->p_playlist )
95         vlc_object_release( p_sys->p_playlist );
96     free( p_sys );
97 }
98
99 static int Demux( demux_t *p_demux )
100 {
101     demux_sys_t *p_sys = p_demux->p_sys;
102
103     char *psz_line;
104     char *psz_attrvalue;
105
106     char *psz_version = NULL;
107     char *psz_url = NULL;
108     char *psz_docid = NULL;
109     int i_duration = -1;
110     char *psz_title = NULL;
111     char *psz_description = NULL;
112
113     INIT_PLAYLIST_STUFF;
114
115     p_sys->p_playlist = p_playlist;
116     p_sys->p_current = p_current;
117     p_sys->i_parent_id = i_parent_id;
118     p_sys->p_item_in_category = p_item_in_category;
119
120     while( ( psz_line = stream_ReadLine( p_demux->s ) ) )
121     {
122         if( *psz_line == '#' )
123         {
124             /* This is a comment */
125             free( psz_line );
126             continue;
127         }
128         psz_attrvalue = strchr( psz_line, ':' );
129         if( !psz_attrvalue )
130         {
131             msg_Dbg( p_demux, "Unable to parse line (%s)", psz_line );
132             free( psz_line );
133             continue;
134         }
135         *psz_attrvalue = '\0';
136         psz_attrvalue++;
137         if( !strcmp( psz_line, "gvp_version" ) )
138         {
139             psz_version = strdup( psz_attrvalue );
140         }
141         else if( !strcmp( psz_line, "url" ) )
142         {
143             psz_url = strdup( psz_attrvalue );
144         }
145         else if( !strcmp( psz_line, "docid" ) )
146         {
147             psz_docid = strdup( psz_attrvalue );
148         }
149         else if( !strcmp( psz_line, "duration" ) )
150         {
151             i_duration = atoi( psz_attrvalue );
152         }
153         else if( !strcmp( psz_line, "title" ) )
154         {
155             psz_title = strdup( psz_attrvalue );
156         }
157         else if( !strcmp( psz_line, "description" ) )
158         {
159             char *buf;
160             if( !psz_description )
161             {
162                 psz_description = strdup( psz_attrvalue );
163             }
164             else
165             {
166                 /* handle multi-line descriptions */
167                 buf = malloc( strlen( psz_description )
168                             + strlen( psz_attrvalue ) + 2 );
169                 sprintf( buf, "%s\n%s", psz_description, psz_attrvalue );
170                 free( psz_description );
171                 psz_description = buf;
172             }
173             /* remove ^M char at the end of the line (if any) */
174             buf = psz_description + strlen( psz_description );
175             if( buf != psz_description )
176             {
177                 buf--;
178                 if( *buf == '\r' ) *buf = '\0';
179             }
180         }
181         free( psz_line );
182     }
183
184     if( !psz_url )
185     {
186         msg_Err( p_demux, "URL not found" );
187     }
188     else
189     {
190         p_input = input_ItemNewExt( p_sys->p_playlist,
191                                     psz_url, psz_title, 0, NULL, -1 );
192 #define SADD_INFO( type, field ) if( field ) { vlc_input_item_AddInfo( \
193                     p_input, _("Google Video"), _(type), "%s", field ) ; }
194         SADD_INFO( "gvp_version", psz_version );
195         SADD_INFO( "docid", psz_docid );
196         SADD_INFO( "description", psz_description );
197         playlist_AddWhereverNeeded( p_sys->p_playlist, p_input,
198                             p_sys->p_current, p_sys->p_item_in_category,
199                             (p_sys->i_parent_id > 0 ) ? VLC_TRUE: VLC_FALSE,
200                             PLAYLIST_APPEND );
201     }
202
203     HANDLE_PLAY_AND_RELEASE;
204
205     free( psz_version );
206     free( psz_url );
207     free( psz_docid );
208     free( psz_title );
209     free( psz_description );
210
211     p_sys->p_playlist = NULL;
212
213     return VLC_SUCCESS;
214 }
215
216 static int Control( demux_t *p_demux, int i_query, va_list args )
217 {
218     return VLC_EGENERIC;
219 }