3 /// \ingroup PlaytreeParser
13 #include <sys/types.h>
20 #include "playtreeparser.h"
21 #include "libmpdemux/stream.h"
22 #include "libmpdemux/demuxer.h"
27 asx_parser_build_tree(char* buffer
, int ref
);
31 #define WHITES " \n\r\t"
39 for(i
= str
; i
[0] != '\0' && strchr(WHITES
,i
[0]) != NULL
; i
++)
42 memmove(str
,i
,strlen(i
) + 1);
43 for(i
= str
+ strlen(str
) - 1 ; strchr(WHITES
,i
[0]) != NULL
; i
--)
51 play_tree_parser_get_line(play_tree_parser_t
* p
) {
55 if(p
->buffer
== NULL
) {
56 p
->buffer
= (char*)malloc(BUF_STEP
);
57 p
->buffer_size
= BUF_STEP
;
61 if(p
->stream
->eof
&& (p
->buffer_end
== 0 || p
->iter
[0] == '\0'))
67 r
= p
->iter
- p
->buffer
;
68 p
->buffer
= (char*)realloc(p
->buffer
,p
->buffer_size
+BUF_STEP
);
69 p
->iter
= p
->buffer
+ r
;
70 p
->buffer_size
+= BUF_STEP
;
74 if(p
->buffer_size
- p
->buffer_end
> 1 && ! p
->stream
->eof
) {
75 r
= stream_read(p
->stream
,p
->buffer
+ p
->buffer_end
,p
->buffer_size
- p
->buffer_end
- 1);
78 p
->buffer
[p
->buffer_end
] = '\0';
82 end
= strchr(p
->iter
,'\n');
85 end
= p
->buffer
+ p
->buffer_end
;
94 line_end
= ((*(end
-1)) == '\r') ? end
-1 : end
;
95 if(line_end
- p
->iter
>= 0)
96 p
->line
= (char*)realloc(p
->line
,line_end
- p
->iter
+1);
99 if(line_end
- p
->iter
> 0)
100 strncpy(p
->line
,p
->iter
,line_end
- p
->iter
);
101 p
->line
[line_end
- p
->iter
] = '\0';
107 p
->buffer_end
-= end
-p
->iter
;
108 memmove(p
->buffer
,end
,p
->buffer_end
);
109 p
->buffer
[p
->buffer_end
] = '\0';
120 play_tree_parser_reset(play_tree_parser_t
* p
) {
125 play_tree_parser_stop_keeping(play_tree_parser_t
* p
) {
127 if(p
->iter
&& p
->iter
!= p
->buffer
) {
128 p
->buffer_end
-= p
->iter
-p
->buffer
;
130 memmove(p
->buffer
,p
->iter
,p
->buffer_end
);
137 parse_asx(play_tree_parser_t
* p
) {
138 int comments
= 0,get_line
= 1;
141 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Trying asx...\n");
145 line
= play_tree_parser_get_line(p
);
154 mp_msg(MSGT_PLAYTREE
,MSGL_DBG2
,"First char isn't '<' but '%c'\n",line
[0]);
155 mp_msg(MSGT_PLAYTREE
,MSGL_DBG3
,"Buffer = [%s]\n",p
->buffer
);
157 } else if(strncmp(line
,"<!--",4) == 0) { // Comments
160 if(line
[0] != '\0' && strlen(line
) > 0)
162 } else if(strncasecmp(line
,"<ASX",4) == 0) // We got an asx element
164 else // We don't get an asx
168 c
= strchr(line
,'-');
170 if (strncmp(c
,"--!>",4) == 0) { // End of comments
173 if(line
[0] != '\0') // There is some more data on this line : keep it
177 line
= c
+1; // Jump the -
178 if(line
[0] != '\0') // Some more data
183 } else // No - on this line (or rest of line) : get next one
188 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Detected asx format\n");
190 // We have an asx : load it in memory and parse
192 while((line
= play_tree_parser_get_line(p
)) != NULL
)
195 mp_msg(MSGT_PLAYTREE
,MSGL_DBG3
,"Parsing asx file: [%s]\n",p
->buffer
);
196 return asx_parser_build_tree(p
->buffer
,p
->deep
);
200 pls_entry_get_value(char* line
) {
203 i
= strchr(line
,'=');
204 if(!i
|| i
[1] == '\0')
210 typedef struct pls_entry
{
217 pls_read_entry(char* line
,pls_entry_t
** _e
,int* _max_entry
,char** val
) {
218 int num
,max_entry
= (*_max_entry
);
219 pls_entry_t
* e
= (*_e
);
222 v
= pls_entry_get_value(line
);
224 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"No value in entry %s\n",line
);
231 mp_msg(MSGT_PLAYTREE
,MSGL_WARN
,"No entry index in entry %s\nAssuming %d\n",line
,num
);
233 if(num
> max_entry
) {
234 e
= (pls_entry_t
*)realloc(e
,num
*sizeof(pls_entry_t
));
235 memset(&e
[max_entry
],0,(num
-max_entry
)*sizeof(pls_entry_t
));
239 (*_max_entry
) = max_entry
;
247 parse_pls(play_tree_parser_t
* p
) {
249 pls_entry_t
* entries
= NULL
;
250 int n_entries
= 0,max_entry
=0,num
;
251 play_tree_t
*list
= NULL
, *entry
= NULL
, *last_entry
= NULL
;
253 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Trying Winamp playlist...\n");
254 if (!(line
= play_tree_parser_get_line(p
)))
257 if(strcasecmp(line
,"[playlist]"))
259 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Detected Winamp playlist format\n");
260 play_tree_parser_stop_keeping(p
);
261 line
= play_tree_parser_get_line(p
);
265 if(strncasecmp(line
,"NumberOfEntries",15) == 0) {
266 v
= pls_entry_get_value(line
);
269 mp_msg(MSGT_PLAYTREE
,MSGL_DBG2
,"Invalid number of entries: very funny!!!\n");
271 mp_msg(MSGT_PLAYTREE
,MSGL_DBG2
,"Playlist claims to have %d entries. Let's see.\n",n_entries
);
272 line
= play_tree_parser_get_line(p
);
277 if(line
[0] == '\0') {
278 line
= play_tree_parser_get_line(p
);
281 if(strncasecmp(line
,"File",4) == 0) {
282 num
= pls_read_entry(line
+4,&entries
,&max_entry
,&v
);
284 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"No value in entry %s\n",line
);
286 entries
[num
-1].file
= strdup(v
);
287 } else if(strncasecmp(line
,"Title",5) == 0) {
288 num
= pls_read_entry(line
+5,&entries
,&max_entry
,&v
);
290 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"No value in entry %s\n",line
);
292 entries
[num
-1].title
= strdup(v
);
293 } else if(strncasecmp(line
,"Length",6) == 0) {
294 num
= pls_read_entry(line
+6,&entries
,&max_entry
,&v
);
296 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"No value in entry %s\n",line
);
298 entries
[num
-1].length
= strdup(v
);
300 mp_msg(MSGT_PLAYTREE
,MSGL_WARN
,"Unknown entry type %s\n",line
);
301 line
= play_tree_parser_get_line(p
);
304 for(num
= 0; num
< max_entry
; num
++) {
305 if(entries
[num
].file
== NULL
)
306 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"Entry %d don't have a file !!!!\n",num
+1);
308 mp_msg(MSGT_PLAYTREE
,MSGL_DBG2
,"Adding entry %s\n",entries
[num
].file
);
309 entry
= play_tree_new();
310 play_tree_add_file(entry
,entries
[num
].file
);
311 free(entries
[num
].file
);
313 play_tree_append_entry(last_entry
,entry
);
318 if(entries
[num
].title
) {
319 // When we have info in playtree we add this info
320 free(entries
[num
].title
);
322 if(entries
[num
].length
) {
323 // When we have info in playtree we add this info
324 free(entries
[num
].length
);
330 entry
= play_tree_new();
331 play_tree_set_child(entry
,list
);
336 Reference Ini-Format: Each entry is assumed a reference
339 parse_ref_ini(play_tree_parser_t
* p
) {
341 play_tree_t
*list
= NULL
, *entry
= NULL
, *last_entry
= NULL
;
343 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Trying reference-ini playlist...\n");
344 if (!(line
= play_tree_parser_get_line(p
)))
347 if(strcasecmp(line
,"[Reference]"))
349 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Detected reference-ini playlist format\n");
350 play_tree_parser_stop_keeping(p
);
351 line
= play_tree_parser_get_line(p
);
356 if(strncasecmp(line
,"Ref",3) == 0) {
357 v
= pls_entry_get_value(line
+3);
359 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"No value in entry %s\n",line
);
362 mp_msg(MSGT_PLAYTREE
,MSGL_DBG2
,"Adding entry %s\n",v
);
363 entry
= play_tree_new();
364 play_tree_add_file(entry
,v
);
366 play_tree_append_entry(last_entry
,entry
);
372 line
= play_tree_parser_get_line(p
);
375 if(!list
) return NULL
;
376 entry
= play_tree_new();
377 play_tree_set_child(entry
,list
);
382 parse_m3u(play_tree_parser_t
* p
) {
384 play_tree_t
*list
= NULL
, *entry
= NULL
, *last_entry
= NULL
;
386 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Trying extended m3u playlist...\n");
387 if (!(line
= play_tree_parser_get_line(p
)))
390 if(strcasecmp(line
,"#EXTM3U"))
392 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Detected extended m3u playlist format\n");
393 play_tree_parser_stop_keeping(p
);
395 while((line
= play_tree_parser_get_line(p
)) != NULL
) {
399 /* EXTM3U files contain such lines:
400 * #EXTINF:<seconds>, <title>
401 * followed by a line with the filename
402 * for now we have no place to put that
403 * so we just skip that extra-info ::atmos
406 #if 0 /* code functional */
407 if(strncasecmp(line
,"#EXTINF:",8) == 0) {
408 mp_msg(MSGT_PLAYTREE
,MSGL_INFO
,"[M3U] Duration: %dsec Title: %s\n",
409 strtol(line
+8,&line
,10), line
+2);
414 entry
= play_tree_new();
415 play_tree_add_file(entry
,line
);
419 play_tree_append_entry(last_entry
,entry
);
423 if(!list
) return NULL
;
424 entry
= play_tree_new();
425 play_tree_set_child(entry
,list
);
430 parse_smil(play_tree_parser_t
* p
) {
432 char* line
,source
[512],*pos
,*s_start
,*s_end
;
433 play_tree_t
*list
= NULL
, *entry
= NULL
, *last_entry
= NULL
;
435 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Trying smil playlist...\n");
438 while((line
= play_tree_parser_get_line(p
)) != NULL
) {
440 if(line
[0] == '\0') // Ignore empties
442 if (strncasecmp(line
,"<smil",5)==0 || strncasecmp(line
,"<?wpl",5)==0)
443 break; // smil header found
445 return NULL
; //line not smil exit
448 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Detected smil playlist format\n");
449 play_tree_parser_stop_keeping(p
);
452 //Get entries from smil
453 while((line
= play_tree_parser_get_line(p
)) != NULL
) {
457 if (!entrymode
) { // all entries filled so far
458 if (strncasecmp(line
,"<video",6)==0 || strncasecmp(line
,"<audio",6)==0 || strncasecmp(line
,"<media",6)) {
459 pos
=strstr(line
,"src="); // Is source present on this line
462 s_end
=strchr(s_start
,'"');
464 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Error parsing this source line %s\n",line
);
467 if (s_end
-s_start
> 511) {
468 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Cannot store such a large source %s\n",line
);
471 strncpy(source
,s_start
,s_end
-s_start
);
472 source
[(s_end
-s_start
)]='\0'; // Null terminate
473 entry
= play_tree_new();
474 play_tree_add_file(entry
,source
);
475 if(!list
) //Insert new entry
478 play_tree_append_entry(last_entry
,entry
);
484 } else { //Entry found but not yet filled
485 pos
= strstr(line
,"src="); // Is source present on this line
489 s_end
=strchr(s_start
,'"');
491 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Error parsing this source line %s\n",line
);
494 if (s_end
-s_start
> 511) {
495 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Cannot store such a large source %s\n",line
);
498 strncpy(source
,s_start
,s_end
-s_start
);
499 source
[(s_end
-s_start
)]='\0'; // Null terminate
500 entry
= play_tree_new();
501 play_tree_add_file(entry
,source
);
502 if(!list
) //Insert new entry
505 play_tree_append_entry(last_entry
,entry
);
511 if(!list
) return NULL
; // Nothing found
513 entry
= play_tree_new();
514 play_tree_set_child(entry
,list
);
519 embedded_playlist_parse(char *line
) {
520 int f
=DEMUXER_TYPE_PLAYLIST
;
522 play_tree_parser_t
* ptp
;
525 // Get stream opened to link
526 stream
=open_stream(line
,0,&f
);
528 mp_msg(MSGT_PLAYTREE
,MSGL_WARN
,"Can't open playlist %s\n",line
);
533 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Adding playlist %s to element entryref\n",line
);
535 ptp
= play_tree_parser_new(stream
,1);
536 entry
= play_tree_parser_get_play_tree(ptp
, 1);
537 play_tree_parser_free(ptp
);
544 parse_textplain(play_tree_parser_t
* p
) {
548 play_tree_t
*list
= NULL
, *entry
= NULL
, *last_entry
= NULL
;
550 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Trying plaintext playlist...\n");
551 play_tree_parser_stop_keeping(p
);
553 while((line
= play_tree_parser_get_line(p
)) != NULL
) {
555 if(line
[0] == '\0' || line
[0] == '#' || (line
[0] == '/' && line
[1] == '/'))
558 //Special check for embedded smil or ram reference in file
560 if (strlen(line
) > 5)
561 for(c
= line
; c
[0]; c
++ )
562 if ( ((c
[0] == '.') && //start with . and next have smil with optional ? or &
563 (tolower(c
[1]) == 's') && (tolower(c
[2])== 'm') &&
564 (tolower(c
[3]) == 'i') && (tolower(c
[4]) == 'l') &&
565 (!c
[5] || c
[5] == '?' || c
[5] == '&')) || // or
566 ((c
[0] == '.') && // start with . and next have smi or ram with optional ? or &
567 ( ((tolower(c
[1]) == 's') && (tolower(c
[2])== 'm') && (tolower(c
[3]) == 'i')) ||
568 ((tolower(c
[1]) == 'r') && (tolower(c
[2])== 'a') && (tolower(c
[3]) == 'm')) )
569 && (!c
[4] || c
[4] == '?' || c
[4] == '&')) ){
570 entry
=embedded_playlist_parse(line
);
575 if (!embedded
) { //regular file link
576 entry
= play_tree_new();
577 play_tree_add_file(entry
,line
);
584 play_tree_append_entry(last_entry
,entry
);
589 if(!list
) return NULL
;
590 entry
= play_tree_new();
591 play_tree_set_child(entry
,list
);
596 parse_playtree(stream_t
*stream
, int forced
) {
597 play_tree_parser_t
* p
;
601 assert(stream
!= NULL
);
604 p
= play_tree_parser_new(stream
,0);
608 ret
= play_tree_parser_get_play_tree(p
, forced
);
609 play_tree_parser_free(p
);
615 play_tree_add_basepath(play_tree_t
* pt
, char* bp
) {
616 int i
,bl
= strlen(bp
),fl
;
620 for(i
= pt
->child
; i
!= NULL
; i
= i
->next
)
621 play_tree_add_basepath(i
,bp
);
628 for(i
= 0 ; pt
->files
[i
] != NULL
; i
++) {
629 fl
= strlen(pt
->files
[i
]);
630 // if we find a full unix path, url:// or X:\ at the beginning,
632 if(fl
<= 0 || strstr(pt
->files
[i
],"://") || (strstr(pt
->files
[i
],":\\") == pt
->files
[i
] + 1) || (pt
->files
[i
][0] == '/') )
634 // if the path begins with \ then prepend drive letter to it.
635 if (pt
->files
[i
][0] == '\\') {
636 pt
->files
[i
] = (char*)realloc(pt
->files
[i
],2+fl
+1);
637 memmove(pt
->files
[i
] + 2,pt
->files
[i
],fl
+1);
638 memcpy(pt
->files
[i
],bp
,2);
641 pt
->files
[i
] = (char*)realloc(pt
->files
[i
],bl
+fl
+1);
642 memmove(pt
->files
[i
] + bl
,pt
->files
[i
],fl
+1);
643 memcpy(pt
->files
[i
],bp
,bl
);
647 // Wrapper for play_tree_add_basepath (add base path from file)
648 void play_tree_add_bpf(play_tree_t
* pt
, char* filename
)
654 file
= strdup(filename
);
657 ls
= strrchr(file
,'/');
658 if(!ls
) ls
= strrchr(file
,'\\');
661 play_tree_add_basepath(pt
,file
);
669 parse_playlist_file(char* file
) {
672 int f
=DEMUXER_TYPE_PLAYLIST
;
674 stream
= open_stream(file
,0,&f
);
677 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"Error while opening playlist file %s: %s\n",file
,strerror(errno
));
681 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Parsing playlist file %s...\n",file
);
683 ret
= parse_playtree(stream
,1);
686 play_tree_add_bpf(ret
, file
);
694 play_tree_parser_new(stream_t
* stream
,int deep
) {
695 play_tree_parser_t
* p
;
697 p
= (play_tree_parser_t
*)calloc(1,sizeof(play_tree_parser_t
));
709 play_tree_parser_free(play_tree_parser_t
* p
) {
715 if(p
->buffer
) free(p
->buffer
);
716 if(p
->line
) free(p
->line
);
721 play_tree_parser_get_play_tree(play_tree_parser_t
* p
, int forced
) {
722 play_tree_t
* tree
= NULL
;
729 while(play_tree_parser_get_line(p
) != NULL
) {
730 play_tree_parser_reset(p
);
734 play_tree_parser_reset(p
);
738 play_tree_parser_reset(p
);
742 play_tree_parser_reset(p
);
744 tree
= parse_ref_ini(p
);
746 play_tree_parser_reset(p
);
748 tree
= parse_smil(p
);
750 play_tree_parser_reset(p
);
752 // Here come the others formats ( textplain must stay the last one )
755 tree
= parse_textplain(p
);
762 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Playlist successfully parsed\n");
764 mp_msg(MSGT_PLAYTREE
,((forced
==1)?MSGL_ERR
:MSGL_V
),"Error while parsing playlist\n");
767 tree
= play_tree_cleanup(tree
);
769 if(!tree
) mp_msg(MSGT_PLAYTREE
,((forced
==1)?MSGL_WARN
:MSGL_V
),"Warning: empty playlist\n");