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