]> git.sesse.net Git - vlc/blob - modules/demux/playlist/gvp.c
1b810de1cda449d87cff638e64047be9960a62b2
[vlc] / modules / demux / playlist / gvp.c
1 /*****************************************************************************
2  * gvp.c: Google Video Playlist demuxer
3  *****************************************************************************
4  * Copyright (C) 2006 VLC authors and VideoLAN
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 it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * 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  * Example:
37
38 # download the free Google Video Player from http://video.google.com/
39 gvp_version:1.1
40 url:http://vp.video.google.com/videodownload?version=0&secureurl=uAAAAMVHt_Q99OwfGxlWVWH7jd6AA_3n4TboaxIELD_kCg3KcBPSxExZFvQv5DGAxrahVg57KZNZvd0EORPBM3xrxTJ3FdLEWBYiduklpviqjE1Q5zLAkiEZaUsUSFtmbBZDTUUBuN9moYY59eK8lpWXsgTbYB1tLVtaxNBpAMRMyVeHoiJ7BzYdENk-PqJeBbr50QbQ83WK87yJAbN2pSRnF-ucCuNMSLBV7wBL4IcxFpYb1WOK-YXkyxY0NtWlPBufTA&sigh=matNCEVSOR8c-3zN9Gtx0zGinwU&begin=0&len=59749&docid=-715862862672743260
41 docid:-715862862672743260
42 duration:59749
43 title:Apple Macintosh 1984 Superbowl Commercial
44 description:The now infamous Apple Macintosh commercial aired during the 1984 SuperBowl.
45
46  */
47
48 /*****************************************************************************
49  * Preamble
50  *****************************************************************************/
51 #ifdef HAVE_CONFIG_H
52 # include "config.h"
53 #endif
54
55 #include <vlc_common.h>
56 #include <vlc_demux.h>
57
58 #include "playlist.h"
59
60 #define MAX_LINE 1024
61
62 struct demux_sys_t
63 {
64     input_item_t *p_current_input;
65 };
66
67 /*****************************************************************************
68  * Local prototypes
69  *****************************************************************************/
70 static int Demux( demux_t *p_demux);
71
72 /*****************************************************************************
73  * Import_GVP: main import function
74  *****************************************************************************/
75 int Import_GVP( vlc_object_t *p_this )
76 {
77     demux_t *p_demux = (demux_t *)p_this;
78     int i_peek, i, b_found = false;
79     const uint8_t *p_peek;
80
81     i_peek = stream_Peek( p_demux->s, &p_peek, MAX_LINE );
82
83     for( i = 0; i < i_peek - (int)sizeof("gvp_version:"); i++ )
84     {
85         if( p_peek[i] == 'g' && p_peek[i+1] == 'v' && p_peek[i+2] == 'p' &&
86             !memcmp( p_peek+i, "gvp_version:", sizeof("gvp_version:") - 1 ) )
87         {
88             b_found = true;
89             break;
90         }
91     }
92
93     if( !b_found ) return VLC_EGENERIC;
94
95     STANDARD_DEMUX_INIT_MSG(  "using Google Video Playlist (gvp) import" );
96     p_demux->pf_control = Control;
97     p_demux->pf_demux = Demux;
98     p_demux->p_sys = malloc( sizeof( demux_sys_t ) );
99     if( !p_demux->p_sys )
100         return VLC_ENOMEM;
101
102     return VLC_SUCCESS;
103 }
104
105 /*****************************************************************************
106  * Deactivate: frees unused data
107  *****************************************************************************/
108 void Close_GVP( vlc_object_t *p_this )
109 {
110     demux_t *p_demux = (demux_t *)p_this;
111     demux_sys_t *p_sys = p_demux->p_sys;
112
113     free( p_sys );
114 }
115
116 static int Demux( demux_t *p_demux )
117 {
118     demux_sys_t *p_sys = p_demux->p_sys;
119
120     char *psz_line;
121     char *psz_attrvalue;
122
123     char *psz_version = NULL;
124     char *psz_url = NULL;
125     char *psz_docid = NULL;
126     int i_duration = -1;
127     char *psz_title = NULL;
128     char *psz_description = NULL;
129     input_item_t *p_input;
130
131     input_item_t *p_current_input = GetCurrentItem(p_demux);
132
133     input_item_node_t *p_subitems = input_item_node_Create( p_current_input );
134
135     p_sys->p_current_input = p_current_input;
136
137     while( ( psz_line = stream_ReadLine( p_demux->s ) ) )
138     {
139         if( *psz_line == '#' )
140         {
141             /* This is a comment */
142             free( psz_line );
143             continue;
144         }
145         psz_attrvalue = strchr( psz_line, ':' );
146         if( !psz_attrvalue )
147         {
148             msg_Dbg( p_demux, "Unable to parse line (%s)", psz_line );
149             free( psz_line );
150             continue;
151         }
152         *psz_attrvalue = '\0';
153         psz_attrvalue++;
154         if( !strcmp( psz_line, "gvp_version" ) )
155         {
156             psz_version = strdup( psz_attrvalue );
157         }
158         else if( !strcmp( psz_line, "url" ) )
159         {
160             psz_url = strdup( psz_attrvalue );
161         }
162         else if( !strcmp( psz_line, "docid" ) )
163         {
164             psz_docid = strdup( psz_attrvalue );
165         }
166         else if( !strcmp( psz_line, "duration" ) )
167         {
168             i_duration = atoi( psz_attrvalue );
169         }
170         else if( !strcmp( psz_line, "title" ) )
171         {
172             psz_title = strdup( psz_attrvalue );
173         }
174         else if( !strcmp( psz_line, "description" ) )
175         {
176             char *buf;
177             if( !psz_description )
178             {
179                 psz_description = strdup( psz_attrvalue );
180             }
181             else
182             {
183                 /* handle multi-line descriptions */
184                 if( asprintf( &buf, "%s\n%s", psz_description, psz_attrvalue ) == -1 )
185                     buf = NULL;
186                 free( psz_description );
187                 psz_description = buf;
188             }
189             /* remove ^M char at the end of the line (if any) */
190             buf = psz_description + strlen( psz_description );
191             if( buf != psz_description )
192             {
193                 buf--;
194                 if( *buf == '\r' ) *buf = '\0';
195             }
196         }
197         free( psz_line );
198     }
199
200     if( !psz_url )
201     {
202         msg_Err( p_demux, "URL not found" );
203     }
204     else
205     {
206         p_input = input_item_New( psz_url, psz_title );
207 #define SADD_INFO( type, field ) if( field ) { input_item_AddInfo( \
208                     p_input, _("Google Video"), type, "%s", field ) ; }
209         SADD_INFO( "gvp_version", psz_version );
210         SADD_INFO( "docid", psz_docid );
211         SADD_INFO( "description", psz_description );
212         input_item_node_AppendItem( p_subitems, p_input );
213         vlc_gc_decref( p_input );
214     }
215
216     input_item_node_PostAndDelete( p_subitems );
217
218     vlc_gc_decref(p_current_input);
219
220     free( psz_version );
221     free( psz_url );
222     free( psz_docid );
223     free( psz_title );
224     free( psz_description );
225
226     return 0; /* Needed for correct operation of go back */
227 }