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