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