3 /// \ingroup PlaytreeParser
13 #include <sys/types.h>
20 #include "playtreeparser.h"
21 #include "stream/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
= 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 while((line
= play_tree_parser_get_line(p
))) {
261 if(strcasecmp(line
,"[playlist]"))
263 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Detected Winamp playlist format\n");
264 play_tree_parser_stop_keeping(p
);
265 line
= play_tree_parser_get_line(p
);
269 if(strncasecmp(line
,"NumberOfEntries",15) == 0) {
270 v
= pls_entry_get_value(line
);
273 mp_msg(MSGT_PLAYTREE
,MSGL_DBG2
,"Invalid number of entries: very funny!!!\n");
275 mp_msg(MSGT_PLAYTREE
,MSGL_DBG2
,"Playlist claims to have %d entries. Let's see.\n",n_entries
);
276 line
= play_tree_parser_get_line(p
);
281 if(line
[0] == '\0') {
282 line
= play_tree_parser_get_line(p
);
285 if(strncasecmp(line
,"File",4) == 0) {
286 num
= pls_read_entry(line
+4,&entries
,&max_entry
,&v
);
288 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"No value in entry %s\n",line
);
290 entries
[num
-1].file
= strdup(v
);
291 } else if(strncasecmp(line
,"Title",5) == 0) {
292 num
= pls_read_entry(line
+5,&entries
,&max_entry
,&v
);
294 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"No value in entry %s\n",line
);
296 entries
[num
-1].title
= strdup(v
);
297 } else if(strncasecmp(line
,"Length",6) == 0) {
298 num
= pls_read_entry(line
+6,&entries
,&max_entry
,&v
);
300 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"No value in entry %s\n",line
);
302 entries
[num
-1].length
= strdup(v
);
304 mp_msg(MSGT_PLAYTREE
,MSGL_WARN
,"Unknown entry type %s\n",line
);
305 line
= play_tree_parser_get_line(p
);
308 for(num
= 0; num
< max_entry
; num
++) {
309 if(entries
[num
].file
== NULL
)
310 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"Entry %d don't have a file !!!!\n",num
+1);
312 mp_msg(MSGT_PLAYTREE
,MSGL_DBG2
,"Adding entry %s\n",entries
[num
].file
);
313 entry
= play_tree_new();
314 play_tree_add_file(entry
,entries
[num
].file
);
315 free(entries
[num
].file
);
317 play_tree_append_entry(last_entry
,entry
);
322 if(entries
[num
].title
) {
323 // When we have info in playtree we add this info
324 free(entries
[num
].title
);
326 if(entries
[num
].length
) {
327 // When we have info in playtree we add this info
328 free(entries
[num
].length
);
334 entry
= play_tree_new();
335 play_tree_set_child(entry
,list
);
340 Reference Ini-Format: Each entry is assumed a reference
343 parse_ref_ini(play_tree_parser_t
* p
) {
345 play_tree_t
*list
= NULL
, *entry
= NULL
, *last_entry
= NULL
;
347 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Trying reference-ini playlist...\n");
348 if (!(line
= play_tree_parser_get_line(p
)))
351 if(strcasecmp(line
,"[Reference]"))
353 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Detected reference-ini playlist format\n");
354 play_tree_parser_stop_keeping(p
);
355 line
= play_tree_parser_get_line(p
);
360 if(strncasecmp(line
,"Ref",3) == 0) {
361 v
= pls_entry_get_value(line
+3);
363 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"No value in entry %s\n",line
);
366 mp_msg(MSGT_PLAYTREE
,MSGL_DBG2
,"Adding entry %s\n",v
);
367 entry
= play_tree_new();
368 play_tree_add_file(entry
,v
);
370 play_tree_append_entry(last_entry
,entry
);
376 line
= play_tree_parser_get_line(p
);
379 if(!list
) return NULL
;
380 entry
= play_tree_new();
381 play_tree_set_child(entry
,list
);
386 parse_m3u(play_tree_parser_t
* p
) {
388 play_tree_t
*list
= NULL
, *entry
= NULL
, *last_entry
= NULL
;
390 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Trying extended m3u playlist...\n");
391 if (!(line
= play_tree_parser_get_line(p
)))
394 if(strcasecmp(line
,"#EXTM3U"))
396 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Detected extended m3u playlist format\n");
397 play_tree_parser_stop_keeping(p
);
399 while((line
= play_tree_parser_get_line(p
)) != NULL
) {
403 /* EXTM3U files contain such lines:
404 * #EXTINF:<seconds>, <title>
405 * followed by a line with the filename
406 * for now we have no place to put that
407 * so we just skip that extra-info ::atmos
410 #if 0 /* code functional */
411 if(strncasecmp(line
,"#EXTINF:",8) == 0) {
412 mp_msg(MSGT_PLAYTREE
,MSGL_INFO
,"[M3U] Duration: %dsec Title: %s\n",
413 strtol(line
+8,&line
,10), line
+2);
418 entry
= play_tree_new();
419 play_tree_add_file(entry
,line
);
423 play_tree_append_entry(last_entry
,entry
);
427 if(!list
) return NULL
;
428 entry
= play_tree_new();
429 play_tree_set_child(entry
,list
);
434 parse_smil(play_tree_parser_t
* p
) {
436 char* line
,source
[512],*pos
,*s_start
,*s_end
;
437 play_tree_t
*list
= NULL
, *entry
= NULL
, *last_entry
= NULL
;
439 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Trying smil playlist...\n");
442 while((line
= play_tree_parser_get_line(p
)) != NULL
) {
444 if(line
[0] == '\0') // Ignore empties
446 if (strncasecmp(line
,"<smil",5)==0 || strncasecmp(line
,"<?wpl",5)==0)
447 break; // smil header found
449 return NULL
; //line not smil exit
452 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Detected smil playlist format\n");
453 play_tree_parser_stop_keeping(p
);
456 //Get entries from smil
457 while((line
= play_tree_parser_get_line(p
)) != NULL
) {
461 if (!entrymode
) { // all entries filled so far
462 if (strncasecmp(line
,"<video",6)==0 || strncasecmp(line
,"<audio",6)==0 || strncasecmp(line
,"<media",6)) {
463 pos
=strstr(line
,"src="); // Is source present on this line
466 s_end
=strchr(s_start
,'"');
468 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Error parsing this source line %s\n",line
);
471 if (s_end
-s_start
> 511) {
472 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Cannot store such a large source %s\n",line
);
475 strncpy(source
,s_start
,s_end
-s_start
);
476 source
[(s_end
-s_start
)]='\0'; // Null terminate
477 entry
= play_tree_new();
478 play_tree_add_file(entry
,source
);
479 if(!list
) //Insert new entry
482 play_tree_append_entry(last_entry
,entry
);
488 } else { //Entry found but not yet filled
489 pos
= strstr(line
,"src="); // Is source present on this line
493 s_end
=strchr(s_start
,'"');
495 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Error parsing this source line %s\n",line
);
498 if (s_end
-s_start
> 511) {
499 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Cannot store such a large source %s\n",line
);
502 strncpy(source
,s_start
,s_end
-s_start
);
503 source
[(s_end
-s_start
)]='\0'; // Null terminate
504 entry
= play_tree_new();
505 play_tree_add_file(entry
,source
);
506 if(!list
) //Insert new entry
509 play_tree_append_entry(last_entry
,entry
);
515 if(!list
) return NULL
; // Nothing found
517 entry
= play_tree_new();
518 play_tree_set_child(entry
,list
);
523 embedded_playlist_parse(char *line
) {
524 int f
=DEMUXER_TYPE_PLAYLIST
;
526 play_tree_parser_t
* ptp
;
529 // Get stream opened to link
530 stream
=open_stream(line
,0,&f
);
532 mp_msg(MSGT_PLAYTREE
,MSGL_WARN
,"Can't open playlist %s\n",line
);
537 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Adding playlist %s to element entryref\n",line
);
539 ptp
= play_tree_parser_new(stream
,1);
540 entry
= play_tree_parser_get_play_tree(ptp
, 1);
541 play_tree_parser_free(ptp
);
548 parse_textplain(play_tree_parser_t
* p
) {
552 play_tree_t
*list
= NULL
, *entry
= NULL
, *last_entry
= NULL
;
554 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Trying plaintext playlist...\n");
555 play_tree_parser_stop_keeping(p
);
557 while((line
= play_tree_parser_get_line(p
)) != NULL
) {
559 if(line
[0] == '\0' || line
[0] == '#' || (line
[0] == '/' && line
[1] == '/'))
562 //Special check for embedded smil or ram reference in file
564 if (strlen(line
) > 5)
565 for(c
= line
; c
[0]; c
++ )
566 if ( ((c
[0] == '.') && //start with . and next have smil with optional ? or &
567 (tolower(c
[1]) == 's') && (tolower(c
[2])== 'm') &&
568 (tolower(c
[3]) == 'i') && (tolower(c
[4]) == 'l') &&
569 (!c
[5] || c
[5] == '?' || c
[5] == '&')) || // or
570 ((c
[0] == '.') && // start with . and next have smi or ram with optional ? or &
571 ( ((tolower(c
[1]) == 's') && (tolower(c
[2])== 'm') && (tolower(c
[3]) == 'i')) ||
572 ((tolower(c
[1]) == 'r') && (tolower(c
[2])== 'a') && (tolower(c
[3]) == 'm')) )
573 && (!c
[4] || c
[4] == '?' || c
[4] == '&')) ){
574 entry
=embedded_playlist_parse(line
);
579 if (!embedded
) { //regular file link
580 entry
= play_tree_new();
581 play_tree_add_file(entry
,line
);
588 play_tree_append_entry(last_entry
,entry
);
593 if(!list
) return NULL
;
594 entry
= play_tree_new();
595 play_tree_set_child(entry
,list
);
600 parse_playtree(stream_t
*stream
, int forced
) {
601 play_tree_parser_t
* p
;
605 assert(stream
!= NULL
);
608 p
= play_tree_parser_new(stream
,0);
612 ret
= play_tree_parser_get_play_tree(p
, forced
);
613 play_tree_parser_free(p
);
619 play_tree_add_basepath(play_tree_t
* pt
, char* bp
) {
620 int i
,bl
= strlen(bp
),fl
;
624 for(i
= pt
->child
; i
!= NULL
; i
= i
->next
)
625 play_tree_add_basepath(i
,bp
);
632 for(i
= 0 ; pt
->files
[i
] != NULL
; i
++) {
633 fl
= strlen(pt
->files
[i
]);
634 // if we find a full unix path, url:// or X:\ at the beginning,
636 if(fl
<= 0 || strstr(pt
->files
[i
],"://") || (strstr(pt
->files
[i
],":\\") == pt
->files
[i
] + 1) || (pt
->files
[i
][0] == '/') )
638 // if the path begins with \ then prepend drive letter to it.
639 if (pt
->files
[i
][0] == '\\') {
640 if (pt
->files
[i
][1] == '\\')
642 pt
->files
[i
] = (char*)realloc(pt
->files
[i
],2+fl
+1);
643 memmove(pt
->files
[i
] + 2,pt
->files
[i
],fl
+1);
644 memcpy(pt
->files
[i
],bp
,2);
647 pt
->files
[i
] = (char*)realloc(pt
->files
[i
],bl
+fl
+1);
648 memmove(pt
->files
[i
] + bl
,pt
->files
[i
],fl
+1);
649 memcpy(pt
->files
[i
],bp
,bl
);
653 // Wrapper for play_tree_add_basepath (add base path from file)
654 void play_tree_add_bpf(play_tree_t
* pt
, char* filename
)
660 file
= strdup(filename
);
663 ls
= strrchr(file
,'/');
664 if(!ls
) ls
= strrchr(file
,'\\');
667 play_tree_add_basepath(pt
,file
);
675 parse_playlist_file(char* file
) {
678 int f
=DEMUXER_TYPE_PLAYLIST
;
680 stream
= open_stream(file
,0,&f
);
683 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"Error while opening playlist file %s: %s\n",file
,strerror(errno
));
687 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Parsing playlist file %s...\n",file
);
689 ret
= parse_playtree(stream
,1);
692 play_tree_add_bpf(ret
, file
);
700 play_tree_parser_new(stream_t
* stream
,int deep
) {
701 play_tree_parser_t
* p
;
703 p
= calloc(1,sizeof(play_tree_parser_t
));
715 play_tree_parser_free(play_tree_parser_t
* p
) {
721 if(p
->buffer
) free(p
->buffer
);
722 if(p
->line
) free(p
->line
);
727 play_tree_parser_get_play_tree(play_tree_parser_t
* p
, int forced
) {
728 play_tree_t
* tree
= NULL
;
735 while(play_tree_parser_get_line(p
) != NULL
) {
736 play_tree_parser_reset(p
);
740 play_tree_parser_reset(p
);
744 play_tree_parser_reset(p
);
748 play_tree_parser_reset(p
);
750 tree
= parse_ref_ini(p
);
752 play_tree_parser_reset(p
);
754 tree
= parse_smil(p
);
756 play_tree_parser_reset(p
);
758 // Here come the others formats ( textplain must stay the last one )
761 tree
= parse_textplain(p
);
768 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Playlist successfully parsed\n");
770 mp_msg(MSGT_PLAYTREE
,((forced
==1)?MSGL_ERR
:MSGL_V
),"Error while parsing playlist\n");
773 tree
= play_tree_cleanup(tree
);
775 if(!tree
) mp_msg(MSGT_PLAYTREE
,((forced
==1)?MSGL_WARN
:MSGL_V
),"Warning: empty playlist\n");