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