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