]> git.sesse.net Git - vlc/blob - modules/demux/playlist/asx.c
7e94b1c13e16d28e86545788607ba21313dda637
[vlc] / modules / demux / playlist / asx.c
1 /*****************************************************************************
2  * asx.c : ASX playlist format import
3  *****************************************************************************
4  * Copyright (C) 2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Derk-Jan Hartman <hartman at videolan dot 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 /* See also: http://msdn.microsoft.com/library/en-us/wmplay10/mmp_sdk/windowsmediametafilereference.asp
25  */
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30 #include <stdlib.h>                                      /* malloc(), free() */
31 #include <ctype.h>                                              /* isspace() */
32
33 #include <vlc/vlc.h>
34 #include <vlc/input.h>
35
36 #include <errno.h>                                                 /* ENOMEM */
37 #include "charset.h"
38 #include "playlist.h"
39 #include "vlc_meta.h"
40
41 struct demux_sys_t
42 {
43     char    *psz_prefix;
44     char    *psz_data;
45     int64_t i_data_len;
46     vlc_bool_t b_utf8;
47 };
48
49 /*****************************************************************************
50  * Local prototypes
51  *****************************************************************************/
52 static int Demux( demux_t *p_demux);
53 static int Control( demux_t *p_demux, int i_query, va_list args );
54
55 static int StoreString( demux_t *p_demux, char **ppsz_string, char *psz_source_start, char *psz_source_end )
56 {
57     demux_sys_t *p_sys = p_demux->p_sys;
58     int i_strlen = psz_source_end-psz_source_start;
59     if( i_strlen < 1 )
60         return VLC_EGENERIC;
61
62     if( *ppsz_string ) free( *ppsz_string );
63     *ppsz_string = malloc( i_strlen*sizeof( char ) +1);
64     memcpy( *ppsz_string, psz_source_start, i_strlen );
65     (*ppsz_string)[i_strlen] = '\0';
66
67     if( p_sys->b_utf8 )
68         EnsureUTF8( *ppsz_string );
69     else
70     {
71         char *psz_temp;
72         psz_temp = FromLocaleDup( *ppsz_string );
73         if( psz_temp )
74         {
75             free( *ppsz_string );
76             *ppsz_string = psz_temp;
77         } else EnsureUTF8( *ppsz_string );
78     }
79     return VLC_SUCCESS;
80 }
81
82 /*****************************************************************************
83  * Import_ASX: main import function
84  *****************************************************************************/
85 int E_(Import_ASX)( vlc_object_t *p_this )
86 {
87     demux_t *p_demux = (demux_t *)p_this;
88     uint8_t *p_peek, *p_peek_stop;
89     CHECK_PEEK( p_peek, 10 );
90
91     p_peek_stop = p_peek+6;
92
93     // skip over possible leading empty lines
94     while( (p_peek < p_peek_stop) && (*p_peek == '\n' || *p_peek == '\r')) ++p_peek;
95
96     if( POKE( p_peek, "<asx", 4 ) || isExtension( p_demux, ".asx" ) ||
97         isExtension( p_demux, ".wax" ) || isExtension( p_demux, ".wvx" ) ||
98         isDemux( p_demux, "asx-open" ) )
99     {
100         ;
101     }
102     else
103         return VLC_EGENERIC;
104     
105     STANDARD_DEMUX_INIT_MSG( "found valid ASX playlist" );
106     p_demux->p_sys->psz_prefix = E_(FindPrefix)( p_demux );
107     p_demux->p_sys->psz_data = NULL;
108     p_demux->p_sys->i_data_len = -1;
109     p_demux->p_sys->b_utf8 = VLC_FALSE;
110     
111     return VLC_SUCCESS;
112 }
113
114 /*****************************************************************************
115  * Deactivate: frees unused data
116  *****************************************************************************/
117 void E_(Close_ASX)( vlc_object_t *p_this )
118 {
119     demux_t *p_demux = (demux_t *)p_this;
120     demux_sys_t *p_sys = p_demux->p_sys;
121
122     if( p_sys->psz_prefix ) free( p_sys->psz_prefix );
123     if( p_sys->psz_data ) free( p_sys->psz_data );
124     free( p_sys );
125 }
126
127 static int Demux( demux_t *p_demux )
128 {
129     demux_sys_t *p_sys = p_demux->p_sys;
130     char        *psz_parse = NULL;
131     char        *psz_backup = NULL;
132     vlc_bool_t  b_entry = VLC_FALSE;
133
134     INIT_PLAYLIST_STUFF;
135
136     /* init txt */
137     if( p_sys->i_data_len < 0 )
138     {
139         int64_t i_pos = 0;
140         p_sys->i_data_len = stream_Size( p_demux->s ) +1; /* This is a cheat to prevent unnecessary realloc */
141         if( p_sys->i_data_len <= 0 && p_sys->i_data_len < 16384 ) p_sys->i_data_len = 1024;
142         p_sys->psz_data = malloc( p_sys->i_data_len * sizeof(char) +1);
143         
144         /* load the complete file */
145         for( ;; )
146         {
147             int i_read = stream_Read( p_demux->s, &p_sys->psz_data[i_pos], p_sys->i_data_len - i_pos );
148             p_sys->psz_data[i_read] = '\0';
149            
150             if( i_read < p_sys->i_data_len - i_pos ) break; /* Done */
151             
152             i_pos += i_read;
153             p_sys->i_data_len += 1024;
154             p_sys->psz_data = realloc( p_sys->psz_data, p_sys->i_data_len * sizeof( char * ) +1 );
155         }
156         if( p_sys->i_data_len <= 0 ) return VLC_EGENERIC;
157     }
158
159     psz_parse = p_sys->psz_data;
160     /* Find first element */
161     if( ( psz_parse = strcasestr( psz_parse, "<ASX" ) ) )
162     {
163         /* ASX element */
164         char *psz_string = NULL;
165         int i_strlen = 0;
166
167         char *psz_base_asx = NULL;
168         char *psz_title_asx = NULL;
169         char *psz_artist_asx = NULL;
170         char *psz_copyright_asx = NULL;
171         char *psz_moreinfo_asx = NULL;
172         char *psz_abstract_asx = NULL;
173         
174         char *psz_base_entry = NULL;
175         char *psz_title_entry = NULL;
176         char *psz_artist_entry = NULL;
177         char *psz_copyright_entry = NULL;
178         char *psz_moreinfo_entry = NULL;
179         char *psz_abstract_entry = NULL;
180         int i_entry_count = 0;
181     
182         psz_parse = strcasestr( psz_parse, ">" );
183
184         while( ( psz_parse = strcasestr( psz_parse, "<" ) ) && psz_parse && *psz_parse )
185         {
186             if( !strncasecmp( psz_parse, "<!--", 4 ) )
187             {
188                 /* this is a comment */
189                 if( ( psz_parse = strcasestr( psz_parse, "-->" ) ) )
190                     psz_parse+=3;
191                 else continue;
192             }
193             else if( !strncasecmp( psz_parse, "<PARAM ", 7 ) )
194             {
195                 vlc_bool_t b_encoding_flag = VLC_FALSE;
196                 psz_parse+=7;
197                 if( !strncasecmp( psz_parse, "name", 4 ) )
198                 {
199                     if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
200                     {
201                         psz_backup = ++psz_parse;
202                         if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
203                         {
204                             i_strlen = psz_parse-psz_backup;
205                             if( i_strlen < 1 ) continue;
206                             msg_Dbg( p_demux, "param name strlen: %d", i_strlen);
207                             psz_string = malloc( i_strlen *sizeof( char ) +1);
208                             memcpy( psz_string, psz_backup, i_strlen );
209                             psz_string[i_strlen] = '\0';
210                             msg_Dbg( p_demux, "param name: %s", psz_string);
211                             b_encoding_flag = !strcasecmp( psz_string, "encoding" );
212                             free( psz_string );
213                         }
214                         else continue;
215                     }
216                     else continue;
217                 }
218                 psz_parse++;
219                 if( !strncasecmp( psz_parse, "value", 5 ) )
220                 {
221                     if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
222                     {
223                         psz_backup = ++psz_parse;
224                         if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
225                         {
226                             i_strlen = psz_parse-psz_backup;
227                             if( i_strlen < 1 ) continue;
228                             msg_Dbg( p_demux, "param value strlen: %d", i_strlen);
229                             psz_string = malloc( i_strlen *sizeof( char ) +1);
230                             memcpy( psz_string, psz_backup, i_strlen );
231                             psz_string[i_strlen] = '\0';
232                             msg_Dbg( p_demux, "param value: %s", psz_string);
233                             if( b_encoding_flag && !strcasecmp( psz_string, "utf-8" ) ) p_sys->b_utf8 = VLC_TRUE;
234                             free( psz_string );
235                         }
236                         else continue;
237                     }
238                     else continue;
239                 }
240                 if( ( psz_parse = strcasestr( psz_parse, "/>" ) ) )
241                     psz_parse += 2;
242                 else continue;
243             }
244             else if( !strncasecmp( psz_parse, "<BANNER", 7 ) )
245             {
246                 /* We skip this element */
247                 if( ( psz_parse = strcasestr( psz_parse, "</BANNER>" ) ) )
248                     psz_parse += 9;
249                 else continue;
250             }
251             else if( !strncasecmp( psz_parse, "<PREVIEWDURATION", 16 ) ||
252                      !strncasecmp( psz_parse, "<LOGURL", 7 ) ||
253                      !strncasecmp( psz_parse, "<Skin", 5 ) )
254             {
255                 /* We skip this element */
256                 if( ( psz_parse = strcasestr( psz_parse, "/>" ) ) )
257                     psz_parse += 2;
258                 else continue;
259             }
260             else if( !strncasecmp( psz_parse, "<BASE ", 6 ) )
261             {
262                 psz_parse+=6;
263                 if( !strncasecmp( psz_parse, "HREF", 4 ) )
264                 {
265                     if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
266                     {
267                         psz_backup = ++psz_parse;
268                         if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
269                         {
270                             StoreString( p_demux, (b_entry ? &psz_base_entry : &psz_base_asx), psz_backup, psz_parse );
271                         }
272                         else continue;
273                     }
274                     else continue;
275                 }
276                 if( ( psz_parse = strcasestr( psz_parse, "/>" ) ) )
277                     psz_parse += 2;
278                 else continue;
279             }
280             else if( !strncasecmp( psz_parse, "<TITLE>", 7 ) )
281             {
282                 psz_backup = psz_parse+=7;
283                 if( ( psz_parse = strcasestr( psz_parse, "</TITLE>" ) ) )
284                 {
285                     StoreString( p_demux, (b_entry ? &psz_title_entry : &psz_title_asx), psz_backup, psz_parse );
286                     psz_parse += 8;
287                 }
288                 else continue;
289             }
290             else if( !strncasecmp( psz_parse, "<Author>", 8 ) )
291             {
292                 psz_backup = psz_parse+=8;
293                 if( ( psz_parse = strcasestr( psz_parse, "</Author>" ) ) )
294                 {
295                     StoreString( p_demux, (b_entry ? &psz_artist_entry : &psz_artist_asx), psz_backup, psz_parse );
296                     psz_parse += 9;
297                 }
298                 else continue;
299             }
300             else if( !strncasecmp( psz_parse, "<Copyright", 10 ) )
301             {
302                 psz_backup = psz_parse+=11;
303                 if( ( psz_parse = strcasestr( psz_parse, "</Copyright>" ) ) )
304                 {
305                     StoreString( p_demux, (b_entry ? &psz_copyright_entry : &psz_copyright_asx), psz_backup, psz_parse );
306                     psz_parse += 12;
307                 }
308                 else continue;
309             }
310             else if( !strncasecmp( psz_parse, "<MoreInfo ", 10 ) )
311             {
312                 psz_parse+=10;
313                 if( !strncasecmp( psz_parse, "HREF", 4 ) )
314                 {
315                     if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
316                     {
317                         psz_backup = ++psz_parse;
318                         if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
319                         {
320                             StoreString( p_demux, (b_entry ? &psz_moreinfo_entry : &psz_moreinfo_asx), psz_backup, psz_parse );
321                         }
322                         else continue;
323                     }
324                     else continue;
325                 }
326                 if( ( psz_parse = strcasestr( psz_parse, "/>" ) ) )
327                     psz_parse += 2;
328                 else continue;
329             }
330             else if( !strncasecmp( psz_parse, "<ABSTRACT>", 10 ) )
331             {
332                 psz_backup = psz_parse+=10;
333                 if( ( psz_parse = strcasestr( psz_parse, "</ABSTRACT>" ) ) )
334                 {
335                     StoreString( p_demux, (b_entry ? &psz_abstract_entry : &psz_abstract_asx), psz_backup, psz_parse );
336                     psz_parse += 11;
337                 }
338                 else continue;
339             }
340             else if( !strncasecmp( psz_parse, "<EntryRef ", 10 ) )
341             {
342                 psz_parse+=10;
343                 if( !strncasecmp( psz_parse, "HREF", 4 ) )
344                 {
345                     if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
346                     {
347                         psz_backup = ++psz_parse;
348                         if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
349                         {
350                             i_strlen = psz_parse-psz_backup;
351                             if( i_strlen < 1 ) continue;
352                             psz_string = malloc( i_strlen*sizeof( char ) +1);
353                             memcpy( psz_string, psz_backup, i_strlen );
354                             psz_string[i_strlen] = '\0';
355                             p_input = input_ItemNew( p_playlist, psz_string, psz_title_asx );
356                             input_ItemCopyOptions( p_current->p_input, p_input );
357                             playlist_AddWhereverNeeded( p_playlist, p_input, p_current,
358                                  p_item_in_category, (i_parent_id > 0 )? VLC_TRUE : VLC_FALSE,
359                                  PLAYLIST_APPEND );
360                             free( psz_string );
361                         }
362                         else continue;
363                     }
364                     else continue;
365                 }
366                 if( ( psz_parse = strcasestr( psz_parse, "/>" ) ) )
367                     psz_parse += 2;
368                 else continue;
369             }
370             else if( !strncasecmp( psz_parse, "</Entry>", 8 ) )
371             {
372                 /* add a new entry */
373                 psz_parse+=8;
374                 if( !b_entry )
375                 {
376                     msg_Err( p_demux, "end of entry without start?" );
377                     continue;
378                 }
379                 /* cleanup entry */
380                 FREENULL( psz_title_entry )
381                 FREENULL( psz_base_entry )
382                 FREENULL( psz_artist_entry )
383                 FREENULL( psz_copyright_entry )
384                 FREENULL( psz_moreinfo_entry )
385                 FREENULL( psz_abstract_entry )
386                 b_entry = VLC_FALSE;
387             }
388             else if( !strncasecmp( psz_parse, "<Entry", 6 ) )
389             {
390                 psz_parse+=6;
391                 if( b_entry )
392                 {
393                     msg_Err( p_demux, "We already are in an entry section" );
394                     continue;
395                 }
396                 i_entry_count += 1;
397                 b_entry = VLC_TRUE;
398                 psz_parse = strcasestr( psz_parse, ">" );
399             }
400             else if( !strncasecmp( psz_parse, "<Ref ", 5 ) )
401             {
402                 psz_parse+=5;
403                 if( !b_entry )
404                 {
405                     msg_Err( p_demux, "A ref outside an entry section" );
406                     continue;
407                 }
408                 
409                 if( !strncasecmp( psz_parse, "HREF", 4 ) )
410                 {
411                     if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
412                     {
413                         psz_backup = ++psz_parse;
414                         if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
415                         {
416                             input_item_t *p_entry = NULL;
417                             char *psz_name = NULL;
418                             i_strlen = psz_parse-psz_backup;
419                             if( i_strlen < 1 ) continue;
420                             psz_string = malloc( i_strlen*sizeof( char ) +1);
421                             memcpy( psz_string, psz_backup, i_strlen );
422                             psz_string[i_strlen] = '\0';
423
424                             /* create the new entry */
425                             asprintf( &psz_name, "%d %s", i_entry_count, ( psz_title_entry ? psz_title_entry : p_current->p_input->psz_name ) );
426                             p_entry = input_ItemNew( p_playlist, psz_string, psz_name );
427                             FREENULL( psz_name );
428                             
429                             input_ItemCopyOptions( p_current->p_input, p_entry );
430                             p_entry->p_meta = vlc_meta_New();
431                             if( psz_title_entry ) vlc_meta_SetTitle( p_entry->p_meta, psz_title_entry );
432                             if( psz_artist_entry ) vlc_meta_SetArtist( p_entry->p_meta, psz_artist_entry );
433                             if( psz_copyright_entry ) vlc_meta_SetCopyright( p_entry->p_meta, psz_copyright_entry );
434                             if( psz_moreinfo_entry ) vlc_meta_SetURL( p_entry->p_meta, psz_moreinfo_entry );
435                             if( psz_abstract_entry ) vlc_meta_SetDescription( p_entry->p_meta, psz_abstract_entry );
436                             
437                             playlist_AddWhereverNeeded( p_playlist, p_entry, p_current,
438                                 p_item_in_category, (i_parent_id > 0 )? VLC_TRUE : VLC_FALSE,
439                                 PLAYLIST_APPEND );
440                             free( psz_string );
441                         }
442                         else continue;
443                     }
444                     else continue;
445                 }
446                 if( ( psz_parse = strcasestr( psz_parse, "/>" ) ) )
447                     psz_parse += 2;
448                 else continue;
449             }
450             else if( !strncasecmp( psz_parse, "</ASX", 5 ) )
451             {
452                 vlc_mutex_lock( &p_current->p_input->lock );
453                 if( !p_current->p_input->p_meta ) p_current->p_input->p_meta = vlc_meta_New();
454                 if( psz_title_asx ) vlc_meta_SetTitle( p_current->p_input->p_meta, psz_title_asx );
455                 if( psz_artist_asx ) vlc_meta_SetArtist( p_current->p_input->p_meta, psz_artist_asx );
456                 if( psz_copyright_asx ) vlc_meta_SetCopyright( p_current->p_input->p_meta, psz_copyright_asx );
457                 if( psz_moreinfo_asx ) vlc_meta_SetURL( p_current->p_input->p_meta, psz_moreinfo_asx );
458                 if( psz_abstract_asx ) vlc_meta_SetDescription( p_current->p_input->p_meta, psz_abstract_asx );
459                 vlc_mutex_unlock( &p_current->p_input->lock );
460                 FREENULL( psz_base_asx );
461                 FREENULL( psz_title_asx );
462                 FREENULL( psz_artist_asx );
463                 FREENULL( psz_copyright_asx );
464                 FREENULL( psz_moreinfo_asx );
465                 FREENULL( psz_abstract_asx );
466                 psz_parse++;
467             }
468             else psz_parse++;
469         }
470 #if 0
471 /* FIXME Unsupported elements */
472             PARAM
473             EVENT
474             REPEAT
475             DURATION
476             ENDMARK
477             STARTMARK
478             STARTTIME
479 #endif
480     }
481     HANDLE_PLAY_AND_RELEASE;
482     return VLC_SUCCESS;
483 }
484
485 static int Control( demux_t *p_demux, int i_query, va_list args )
486 {
487     return VLC_EGENERIC;
488 }