]> git.sesse.net Git - vlc/blob - modules/demux/playlist/gvp.c
dda6b4bd16ae735f43faeaf0ef3d6ffea9831756
[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 <stdlib.h>                                      /* malloc(), free() */
41
42 #include <vlc/vlc.h>
43 #include <vlc/input.h>
44 #include <vlc/intf.h>
45
46 #include <errno.h>                                                 /* ENOMEM */
47 #include "playlist.h"
48
49 #define MAX_LINE 1024
50
51 struct demux_sys_t
52 {
53     playlist_t *p_playlist;
54     playlist_item_t *p_current;
55     playlist_item_t *p_item_in_category;
56     int i_parent_id;
57 };
58
59 /*****************************************************************************
60  * Local prototypes
61  *****************************************************************************/
62 static int Demux( demux_t *p_demux);
63 static int Control( demux_t *p_demux, int i_query, va_list args );
64
65 /*****************************************************************************
66  * Import_GVP: main import function
67  *****************************************************************************/
68 int E_(Import_GVP)( vlc_object_t *p_this )
69 {
70     demux_t *p_demux = (demux_t *)p_this;
71     demux_sys_t *p_sys;
72
73     int i_size;
74     byte_t *p_peek;
75
76     i_size = stream_Peek( p_demux->s, &p_peek, MAX_LINE );
77     if( !strstr( (char*)p_peek, "gvp_version:" ) )
78     {
79         return VLC_EGENERIC;
80     }
81
82     msg_Dbg( p_demux, "using Google Video Playlist (gvp) import");
83
84     p_demux->pf_control = Control;
85     p_demux->pf_demux = Demux;
86     p_demux->p_sys = p_sys = malloc( sizeof(demux_sys_t) );
87     if( p_sys == NULL )
88     {
89         msg_Err( p_demux, "out of memory" );
90         return VLC_ENOMEM;
91     }
92
93     p_sys->p_playlist = NULL;
94
95     return VLC_SUCCESS;
96 }
97
98 /*****************************************************************************
99  * Deactivate: frees unused data
100  *****************************************************************************/
101 void E_(Close_GVP)( vlc_object_t *p_this )
102 {
103     demux_t *p_demux = (demux_t *)p_this;
104     demux_sys_t *p_sys = p_demux->p_sys;
105
106     if( p_sys->p_playlist )
107         vlc_object_release( p_sys->p_playlist );
108     free( p_sys );
109 }
110
111 static int Demux( demux_t *p_demux )
112 {
113     demux_sys_t *p_sys = p_demux->p_sys;
114
115     INIT_PLAYLIST_STUFF;
116
117     p_sys->p_playlist = p_playlist;
118     p_sys->p_current = p_current;
119     p_sys->i_parent_id = i_parent_id;
120     p_sys->p_item_in_category = p_item_in_category;
121
122     char *psz_line;
123     char *psz_attrvalue;
124
125     char *psz_version = NULL;
126     char *psz_url = NULL;
127     char *psz_docid = NULL;
128     int i_duration = -1;
129     char *psz_title = NULL;
130     char *psz_description = NULL;
131
132     while( ( psz_line = stream_ReadLine( p_demux->s ) ) )
133     {
134         if( *psz_line == '#' )
135         {
136             /* This is a comment */
137             free( psz_line );
138             continue;
139         }
140         psz_attrvalue = strchr( psz_line, ':' );
141         if( !psz_attrvalue )
142         {
143             msg_Dbg( p_demux, "Unable to parse line (%s)", psz_line );
144             free( psz_line );
145             continue;
146         }
147         *psz_attrvalue = '\0';
148         psz_attrvalue++;
149         if( !strcmp( psz_line, "gvp_version" ) )
150         {
151             psz_version = strdup( psz_attrvalue );
152         }
153         else if( !strcmp( psz_line, "url" ) )
154         {
155             psz_url = strdup( psz_attrvalue );
156         }
157         else if( !strcmp( psz_line, "docid" ) )
158         {
159             psz_docid = strdup( psz_attrvalue );
160         }
161         else if( !strcmp( psz_line, "duration" ) )
162         {
163             i_duration = atoi( psz_attrvalue );
164         }
165         else if( !strcmp( psz_line, "title" ) )
166         {
167             psz_title = strdup( psz_attrvalue );
168         }
169         else if( !strcmp( psz_line, "description" ) )
170         {
171             char *buf;
172             if( !psz_description )
173             {
174                 psz_description = strdup( psz_attrvalue );
175             }
176             else
177             {
178                 /* handle multi-line descriptions */
179                 buf = malloc( strlen( psz_description )
180                             + strlen( psz_attrvalue ) + 2 );
181                 sprintf( buf, "%s\n%s", psz_description, psz_attrvalue );
182                 free( psz_description );
183                 psz_description = buf;
184             }
185             /* remove ^M char at the end of the line (if any) */
186             buf = psz_description + strlen( psz_description );
187             if( buf != psz_description )
188             {
189                 buf--;
190                 if( *buf == '\r' ) *buf = '\0';
191             }
192         }
193         free( psz_line );
194     }
195
196     if( !psz_url )
197     {
198         msg_Err( p_demux, "URL not found" );
199     }
200     else
201     {
202         p_input = input_ItemNewExt( p_sys->p_playlist,
203                                     psz_url, psz_title, 0, NULL, -1 );
204 #define SADD_INFO( type, field ) if( field ) { vlc_input_item_AddInfo( \
205                     p_input, _("Google Video"), _(type), "%s", field ) ; }
206         SADD_INFO( "gvp_version", psz_version );
207         SADD_INFO( "docid", psz_docid );
208         SADD_INFO( "description", psz_description );
209         playlist_AddWhereverNeeded( p_sys->p_playlist, p_input,
210                             p_sys->p_current, p_sys->p_item_in_category,
211                             (p_sys->i_parent_id > 0 ) ? VLC_TRUE: VLC_FALSE,
212                             PLAYLIST_APPEND );
213     }
214
215     HANDLE_PLAY_AND_RELEASE;
216
217     free( psz_version );
218     free( psz_url );
219     free( psz_docid );
220     free( psz_title );
221     free( psz_description );
222
223     p_sys->p_playlist = NULL;
224
225     return VLC_SUCCESS;
226 }
227
228 static int Control( demux_t *p_demux, int i_query, va_list args )
229 {
230     return VLC_EGENERIC;
231 }