]> git.sesse.net Git - vlc/blob - modules/demux/playlist/ram.c
Remove useless <ctype.h> inclusions
[vlc] / modules / demux / playlist / ram.c
1 /*****************************************************************************
2  * ram.c : RAM playlist format import
3  *****************************************************************************
4  * Copyright (C) 2009 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Srikanth Raju <srikiraju@gmail.com>
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 An example:
26 rtsp://helixserver.example.com/video1.rm?rpcontextheight=250
27 &rpcontextwidth=280&rpcontexturl="http://www.example.com/relatedinfo1.html"
28 rtsp://helixserver.example.com/video2.rm?rpurl="http://www.example.com/index.html"
29 rtsp://helixserver.example.com/sample1.smil?screensize=full
30 rtsp://helixserver.example.com/audio1.rm?start=55&end=1:25
31 rtsp://helixserver.example.com/introvid.rm?title="Introduction to Streaming Media
32 Production"&author="RealNetworks, Inc."&copyright="&#169;2001, RealNetworks, Inc."
33 rtsp://helixserver.example.com/song1.rm?clipinfo="title=Artist of the Year|artist name=Your Name
34 Here|album name=My Debut|genre=Rock|copyright=2001|year=2001|comments=This one really
35 knows how to rock!"
36
37 See also:
38 http://service.real.com/help/library/guides/realone/IntroGuide/HTML/htmfiles/ramsum.htm
39 http://service.real.com/help/library/guides/realone/IntroGuide/HTML/htmfiles/ramfile.htm
40 */
41
42
43 /*****************************************************************************
44  * Preamble
45  *****************************************************************************/
46 #ifdef HAVE_CONFIG_H
47 # include "config.h"
48 #endif
49
50 #include <vlc_common.h>
51 #include <vlc_demux.h>
52 #include <vlc_url.h>
53
54 #include "playlist.h"
55
56 struct demux_sys_t
57 {
58     char *psz_prefix;
59 };
60
61 /*****************************************************************************
62  * Local prototypes
63  *****************************************************************************/
64 static int Demux( demux_t *p_demux);
65 static int Control( demux_t *p_demux, int i_query, va_list args );
66 static void ParseClipInfo( const char * psz_clipinfo, char **ppsz_artist, char **ppsz_title,
67                            char **ppsz_album, char **ppsz_genre, char **ppsz_year,
68                            char **ppsz_cdnum, char **ppsz_comments );
69
70 /**
71  * Import_RAM: main import function
72  * @param p_this: this demux object
73  * @return VLC_SUCCESS if everything is okay
74  */
75 int Import_RAM( vlc_object_t *p_this )
76 {
77     demux_t *p_demux = (demux_t *)p_this;
78
79     if(! demux_IsPathExtension( p_demux, ".ram" ) ||
80          demux_IsPathExtension( p_demux, ".rm" ) )
81         return VLC_EGENERIC;
82
83     STANDARD_DEMUX_INIT_MSG( "found valid RAM playlist" );
84     p_demux->p_sys->psz_prefix = FindPrefix( p_demux );
85
86     return VLC_SUCCESS;
87 }
88
89 /**
90  * Frees up memory on module close
91  * @param p_this: this demux object
92  */
93 void Close_RAM( vlc_object_t *p_this )
94 {
95     demux_t *p_demux = (demux_t *)p_this;
96     free( p_demux->p_sys->psz_prefix );
97     free( p_demux->p_sys );
98 }
99
100 /**
101  * Skips blanks in a given buffer
102  * @param s: input string
103  * @param i_strlen: length of the buffer
104  */
105 static const char *SkipBlanks( const char *s, size_t i_strlen )
106 {
107     while( i_strlen > 0 ) {
108         switch( *s )
109         {
110             case ' ':
111             case '\t':
112             case '\r':
113             case '\n':
114                 --i_strlen;
115                 ++s;
116                 break;
117             default:
118                 i_strlen = 0;
119         }
120     }
121     return s;
122 }
123
124 /**
125  * Converts a time of format hour:minutes:sec.fraction to seconds
126  * @param s: input string
127  * @param i_strlen: length of the buffer
128  * @return time in seconds
129  */
130 static int ParseTime( const char *s, size_t i_strlen)
131 {
132     // need to parse hour:minutes:sec.fraction string
133     int result = 0;
134     int val;
135     const char *end = s + i_strlen;
136     // skip leading spaces if any
137     s = SkipBlanks(s, i_strlen);
138
139     val = 0;
140     while( (s < end) && isdigit(*s) )
141     {
142         int newval = val*10 + (*s - '0');
143         if( newval < val )
144         {
145             // overflow
146             val = 0;
147             break;
148         }
149         val = newval;
150         ++s;
151     }
152     result = val;
153     s = SkipBlanks(s, end-s);
154     if( *s == ':' )
155     {
156         ++s;
157         s = SkipBlanks(s, end-s);
158         result = result * 60;
159         val = 0;
160         while( (s < end) && isdigit(*s) )
161         {
162             int newval = val*10 + (*s - '0');
163             if( newval < val )
164             {
165                 // overflow
166                 val = 0;
167                 break;
168             }
169             val = newval;
170             ++s;
171         }
172         result += val;
173         s = SkipBlanks(s, end-s);
174         if( *s == ':' )
175         {
176             ++s;
177             s = SkipBlanks(s, end-s);
178             result = result * 60;
179             val = 0;
180             while( (s < end) && isdigit(*s) )
181             {
182                 int newval = val*10 + (*s - '0');
183                 if( newval < val )
184                 {
185                     // overflow
186                     val = 0;
187                     break;
188                 }
189                 val = newval;
190                 ++s;
191             }
192             result += val;
193             // TODO: one day, we may need to parse fraction for sub-second resolution
194         }
195     }
196     return result;
197 }
198
199 /**
200  * Main demux callback function
201  * @param p_demux: this demux object
202  */
203 static int Demux( demux_t *p_demux )
204 {
205     char       *psz_line;
206     char       *psz_artist = NULL, *psz_album = NULL, *psz_genre = NULL, *psz_year = NULL;
207     char       *psz_author = NULL, *psz_title = NULL, *psz_copyright = NULL, *psz_cdnum = NULL, *psz_comments = NULL;
208     int        i_parsed_duration = 0;
209     mtime_t    i_duration = -1;
210     const char **ppsz_options = NULL;
211     int        i_options = 0, i_start = 0, i_stop = 0;
212     bool b_cleanup = false;
213     input_item_t *p_input;
214
215     input_item_t *p_current_input = GetCurrentItem(p_demux);
216
217     psz_line = stream_ReadLine( p_demux->s );
218     while( psz_line )
219     {
220         char *psz_parse = psz_line;
221
222         /* Skip leading tabs and spaces */
223         while( *psz_parse == ' ' || *psz_parse == '\t' ||
224                *psz_parse == '\n' || *psz_parse == '\r' ) psz_parse++;
225
226         if( *psz_parse == '#' )
227         {
228             /* Ignore comments */
229         }
230         else if( *psz_parse )
231         {
232             char *psz_mrl, *psz_option_next, *psz_option;
233             char *psz_param, *psz_value;
234
235             /* Get the MRL from the file. Note that this might contain parameters of form ?param1=value1&param2=value2 in a RAM file */
236             psz_mrl = ProcessMRL( psz_parse, p_demux->p_sys->psz_prefix );
237
238             b_cleanup = true;
239             if ( !psz_mrl ) goto error;
240
241             /* We have the MRL, now we have to check for options and parse them from MRL */
242             psz_option = strchr( psz_mrl, '?' ); /* Look for start of options */
243             if( psz_option )
244             {
245                 /* Remove options from MRL
246                    because VLC can't get the file otherwise */
247                 *psz_option = '\0';
248                 psz_option++;
249                 psz_option_next = psz_option;
250                 while( 1 ) /* Process each option */
251                 {
252                     /* Look for end of first option which maybe a & or \0 */
253                     psz_option = psz_option_next;
254                     psz_option_next = strchr( psz_option, '&' );
255                     if( psz_option_next )
256                     {
257                         *psz_option_next = '\0';
258                         psz_option_next++;
259                     }
260                     else
261                         psz_option_next = strchr( psz_option, '\0' );
262                     /* Quit if options are over */
263                     if( psz_option_next == psz_option )
264                         break;
265
266                     /* Parse out param and value */
267                     psz_param = psz_option;
268                     psz_value = strchr( psz_option, '=' );
269                     if( psz_value == NULL )
270                         break;
271                     *psz_value = '\0';
272                     psz_value++;
273
274                     /* Take action based on parameter value in the below if else structure */
275                     /* TODO: Remove any quotes surrounding values if required */
276                     if( !strcmp( psz_param, "clipinfo" ) )
277                     {
278                         ParseClipInfo( psz_value, &psz_artist, &psz_title,
279                            &psz_album, &psz_genre, &psz_year,
280                            &psz_cdnum, &psz_comments ); /* clipinfo has various sub parameters, which is parsed by this function */
281                     }
282                     else if( !strcmp( psz_param, "author" ) )
283                         psz_author = decode_URI_duplicate(psz_value);
284                     else if( !strcmp( psz_param, "start" ) )
285                     {
286                         i_start = ParseTime( psz_value, strlen( psz_value ) );
287                         char *temp;
288                         if( i_start )
289                         {
290                             if( asprintf( &temp, ":start-time=%d", i_start ) != -1 )
291                                 INSERT_ELEM( ppsz_options, i_options, i_options, temp );
292                         }
293                     }
294                     else if( !strcmp( psz_param, "end" ) )
295                     {
296                         i_stop = ParseTime( psz_value, strlen( psz_value ) );
297                         char *temp;
298                         if( i_stop )
299                         {
300                             if( asprintf( &temp, ":stop-time=%d", i_stop ) != -1 )
301                                 INSERT_ELEM( ppsz_options, i_options, i_options, temp );
302                         }
303                     }
304                     else if( !strcmp( psz_param, "title" ) )
305                         psz_title = decode_URI_duplicate(psz_value);
306                     else if( !strcmp( psz_param, "copyright" ) )
307                         psz_copyright = decode_URI_duplicate(psz_value);
308                     else
309                     {   /* TODO: insert option anyway? Currently ignores*/
310                         /* INSERT_ELEM( ppsz_options, i_options, i_options, psz_option ); */
311                     }
312                 }
313             }
314
315             /* Create the input item and pump in all the options into playlist item */
316             p_input = input_item_NewExt( p_demux, psz_mrl, psz_title, i_options, ppsz_options, 0, i_duration );
317
318             if( !EMPTY_STR( psz_artist ) ) input_item_SetArtist( p_input, psz_artist );
319             if( !EMPTY_STR( psz_author ) ) input_item_SetPublisher( p_input, psz_author );
320             if( !EMPTY_STR( psz_title ) ) input_item_SetTitle( p_input, psz_title );
321             if( !EMPTY_STR( psz_copyright ) ) input_item_SetCopyright( p_input, psz_copyright );
322             if( !EMPTY_STR( psz_album ) ) input_item_SetAlbum( p_input, psz_album );
323             if( !EMPTY_STR( psz_genre ) ) input_item_SetGenre( p_input, psz_genre );
324             if( !EMPTY_STR( psz_year ) ) input_item_SetDate( p_input, psz_copyright );
325             if( !EMPTY_STR( psz_cdnum ) ) input_item_SetTrackNum( p_input, psz_cdnum );
326             if( !EMPTY_STR( psz_comments ) ) input_item_SetDescription( p_input, psz_comments );
327
328             input_item_AddSubItem( p_current_input, p_input );
329             vlc_gc_decref( p_input );
330             free( psz_mrl );
331         }
332
333  error:
334         /* Fetch another line */
335         free( psz_line );
336         psz_line = stream_ReadLine( p_demux->s );
337         if( !psz_line ) b_cleanup = true;
338
339         if( b_cleanup )
340         {
341             /* Cleanup state */
342             while( i_options-- ) free( (char*)ppsz_options[i_options] );
343             FREENULL( ppsz_options );
344             FREENULL( psz_artist );
345             FREENULL( psz_title );
346             FREENULL( psz_author );
347             FREENULL( psz_copyright );
348             FREENULL( psz_album );
349             FREENULL( psz_genre );
350             FREENULL( psz_year );
351             FREENULL( psz_cdnum );
352             FREENULL( psz_comments );
353             i_options = 0;
354             i_parsed_duration = 0;
355             i_duration = -1;
356             i_start = 0;
357             i_stop = 0;
358             b_cleanup = false;
359         }
360     }
361     vlc_gc_decref(p_current_input);
362     var_Destroy( p_demux, "m3u-extvlcopt" );
363     return 0; /* Needed for correct operation of go back */
364 }
365
366 /**
367  * @param p_demux: This object
368  * @param i_query:
369  * @param args: List of arguments
370  */
371 static int Control( demux_t *p_demux, int i_query, va_list args )
372 {
373     VLC_UNUSED(p_demux); VLC_UNUSED(i_query); VLC_UNUSED(args);
374     return VLC_EGENERIC;
375 }
376
377 /**
378  * Parses clipinfo parameter
379  * @param psz_clipinfo: string containing the clipinfo parameter along with quotes
380  * @param ppsz_artist: Buffer to store artist name
381  * @param ppsz_title: Buffer to store title
382  * @param ppsz_album: Buffer to store album
383  * @param ppsz_genre: Buffer to store genre
384  * @param ppsz_year: Buffer to store year
385  * @param ppsz_cdnum: Buffer to store cdnum
386  * @param ppsz_comments: Buffer to store comments
387  */
388 static void ParseClipInfo( const char *psz_clipinfo, char **ppsz_artist, char **ppsz_title,
389                            char **ppsz_album, char **ppsz_genre, char **ppsz_year,
390                            char **ppsz_cdnum, char **ppsz_comments )
391 {
392     char *psz_option_next, *psz_option_start, *psz_param, *psz_value, *psz_suboption;
393     char *psz_temp_clipinfo = strdup( psz_clipinfo );
394     psz_option_start = strchr( psz_temp_clipinfo, '"' );
395     if( !psz_option_start )
396     {
397         free( psz_temp_clipinfo );
398         return;
399     }
400
401     psz_option_start++;
402     psz_option_next = psz_option_start;
403     while( 1 ) /* Process each sub option */
404     {
405         /* Get the sub option */
406         psz_option_start = psz_option_next;
407         psz_option_next = strchr( psz_option_start, '|' );
408         if( psz_option_next )
409             *psz_option_next = '\0';
410         else
411             psz_option_next = strchr( psz_option_start, '"' );
412         if( psz_option_next )
413             *psz_option_next = '\0';
414         else
415             psz_option_next = strchr( psz_option_start, '\0' );
416         if( psz_option_next == psz_option_start )
417             break;
418
419         psz_suboption = strdup( psz_option_start );
420         if( !psz_suboption )
421             break;
422
423         /* Parse out param and value */
424         psz_param = psz_suboption;
425         if( strchr( psz_suboption, '=' ) )
426         {
427             psz_value = strchr( psz_suboption, '=' ) + 1;
428             *( strchr( psz_suboption, '=' ) ) = '\0';
429         }
430         else
431             break;
432         /* Put into args */
433         if( !strcmp( psz_param, "artist name" ) )
434             *ppsz_artist = decode_URI_duplicate( psz_value );
435         else if( !strcmp( psz_param, "title" ) )
436             *ppsz_title = decode_URI_duplicate( psz_value );
437         else if( !strcmp( psz_param, "album name" ) )
438             *ppsz_album = decode_URI_duplicate( psz_value );
439         else if( !strcmp( psz_param, "genre" ) )
440             *ppsz_genre = decode_URI_duplicate( psz_value );
441         else if( !strcmp( psz_param, "year" ) )
442             *ppsz_year = decode_URI_duplicate( psz_value );
443         else if( !strcmp( psz_param, "cdnum" ) )
444             *ppsz_cdnum = decode_URI_duplicate( psz_value );
445         else if( !strcmp( psz_param, "comments" ) )
446             *ppsz_comments = decode_URI_duplicate( psz_value );
447
448         free( psz_suboption );
449         psz_option_next++;
450     }
451
452     free( psz_temp_clipinfo );
453 }