10 #include <sys/types.h>
17 #include "playtreeparser.h"
18 #include "libmpdemux/stream.h"
19 #include "libmpdemux/demuxer.h"
24 asx_parser_build_tree(char* buffer
, int ref
);
28 #define WHITES " \n\r\t"
36 for(i
= str
; i
[0] != '\0' && strchr(WHITES
,i
[0]) != NULL
; i
++)
39 memmove(str
,i
,strlen(i
) + 1);
40 for(i
= str
+ strlen(str
) - 1 ; strchr(WHITES
,i
[0]) != NULL
; i
--)
48 play_tree_parser_get_line(play_tree_parser_t
* p
) {
52 if(p
->buffer
== NULL
) {
53 p
->buffer
= (char*)malloc(BUF_STEP
);
54 p
->buffer_size
= BUF_STEP
;
58 if(p
->stream
->eof
&& (p
->buffer_end
== 0 || p
->iter
[0] == '\0'))
64 r
= p
->iter
- p
->buffer
;
65 p
->buffer
= (char*)realloc(p
->buffer
,p
->buffer_size
+BUF_STEP
);
66 p
->iter
= p
->buffer
+ r
;
67 p
->buffer_size
+= BUF_STEP
;
71 if(p
->buffer_size
- p
->buffer_end
> 1 && ! p
->stream
->eof
) {
72 r
= stream_read(p
->stream
,p
->buffer
+ p
->buffer_end
,p
->buffer_size
- p
->buffer_end
- 1);
75 p
->buffer
[p
->buffer_end
] = '\0';
79 end
= strchr(p
->iter
,'\n');
82 end
= p
->buffer
+ p
->buffer_end
;
91 line_end
= ((*(end
-1)) == '\r') ? end
-1 : end
;
92 if(line_end
- p
->iter
>= 0)
93 p
->line
= (char*)realloc(p
->line
,line_end
- p
->iter
+1);
96 if(line_end
- p
->iter
> 0)
97 strncpy(p
->line
,p
->iter
,line_end
- p
->iter
);
98 p
->line
[line_end
- p
->iter
] = '\0';
104 p
->buffer_end
-= end
-p
->iter
;
105 memmove(p
->buffer
,end
,p
->buffer_end
);
106 p
->buffer
[p
->buffer_end
] = '\0';
117 play_tree_parser_reset(play_tree_parser_t
* p
) {
122 play_tree_parser_stop_keeping(play_tree_parser_t
* p
) {
124 if(p
->iter
&& p
->iter
!= p
->buffer
) {
125 p
->buffer_end
-= p
->iter
-p
->buffer
;
127 memmove(p
->buffer
,p
->iter
,p
->buffer_end
);
134 parse_asx(play_tree_parser_t
* p
) {
135 int comments
= 0,get_line
= 1;
138 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Trying asx...\n");
142 line
= play_tree_parser_get_line(p
);
151 mp_msg(MSGT_PLAYTREE
,MSGL_DBG2
,"First char isn't '<' but '%c'\n",line
[0]);
152 mp_msg(MSGT_PLAYTREE
,MSGL_DBG3
,"Buffer = [%s]\n",p
->buffer
);
154 } else if(strncmp(line
,"<!--",4) == 0) { // Comments
157 if(line
[0] != '\0' && strlen(line
) > 0)
159 } else if(strncasecmp(line
,"<ASX",4) == 0) // We got an asx element
161 else // We don't get an asx
165 c
= strchr(line
,'-');
167 if (strncmp(c
,"--!>",4) == 0) { // End of comments
170 if(line
[0] != '\0') // There is some more data on this line : keep it
174 line
= c
+1; // Jump the -
175 if(line
[0] != '\0') // Some more data
180 } else // No - on this line (or rest of line) : get next one
185 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Detected asx format\n");
187 // We have an asx : load it in memory and parse
189 while((line
= play_tree_parser_get_line(p
)) != NULL
)
192 mp_msg(MSGT_PLAYTREE
,MSGL_DBG3
,"Parsing asx file: [%s]\n",p
->buffer
);
193 return asx_parser_build_tree(p
->buffer
,p
->deep
);
197 pls_entry_get_value(char* line
) {
200 i
= strchr(line
,'=');
201 if(!i
|| i
[1] == '\0')
207 typedef struct pls_entry
{
214 pls_read_entry(char* line
,pls_entry_t
** _e
,int* _max_entry
,char** val
) {
215 int num
,max_entry
= (*_max_entry
);
216 pls_entry_t
* e
= (*_e
);
219 v
= pls_entry_get_value(line
);
221 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"No value in entry %s\n",line
);
228 mp_msg(MSGT_PLAYTREE
,MSGL_WARN
,"No entry index in entry %s\nAssuming %d\n",line
,num
);
230 if(num
> max_entry
) {
231 e
= (pls_entry_t
*)realloc(e
,num
*sizeof(pls_entry_t
));
232 memset(&e
[max_entry
],0,(num
-max_entry
)*sizeof(pls_entry_t
));
236 (*_max_entry
) = max_entry
;
244 parse_pls(play_tree_parser_t
* p
) {
246 pls_entry_t
* entries
= NULL
;
247 int n_entries
= 0,max_entry
=0,num
;
248 play_tree_t
*list
= NULL
, *entry
= NULL
, *last_entry
= NULL
;
250 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Trying Winamp playlist...\n");
251 if (!(line
= play_tree_parser_get_line(p
)))
254 if(strcasecmp(line
,"[playlist]"))
256 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Detected Winamp playlist format\n");
257 play_tree_parser_stop_keeping(p
);
258 line
= play_tree_parser_get_line(p
);
262 if(strncasecmp(line
,"NumberOfEntries",15) == 0) {
263 v
= pls_entry_get_value(line
);
266 mp_msg(MSGT_PLAYTREE
,MSGL_DBG2
,"Invalid number of entries: very funny!!!\n");
268 mp_msg(MSGT_PLAYTREE
,MSGL_DBG2
,"Playlist claims to have %d entries. Let's see.\n",n_entries
);
269 line
= play_tree_parser_get_line(p
);
274 if(line
[0] == '\0') {
275 line
= play_tree_parser_get_line(p
);
278 if(strncasecmp(line
,"File",4) == 0) {
279 num
= pls_read_entry(line
+4,&entries
,&max_entry
,&v
);
281 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"No value in entry %s\n",line
);
283 entries
[num
-1].file
= strdup(v
);
284 } else if(strncasecmp(line
,"Title",5) == 0) {
285 num
= pls_read_entry(line
+5,&entries
,&max_entry
,&v
);
287 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"No value in entry %s\n",line
);
289 entries
[num
-1].title
= strdup(v
);
290 } else if(strncasecmp(line
,"Length",6) == 0) {
291 num
= pls_read_entry(line
+6,&entries
,&max_entry
,&v
);
293 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"No value in entry %s\n",line
);
295 entries
[num
-1].length
= strdup(v
);
297 mp_msg(MSGT_PLAYTREE
,MSGL_WARN
,"Unknown entry type %s\n",line
);
298 line
= play_tree_parser_get_line(p
);
301 for(num
= 0; num
< max_entry
; num
++) {
302 if(entries
[num
].file
== NULL
)
303 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"Entry %d don't have a file !!!!\n",num
+1);
305 mp_msg(MSGT_PLAYTREE
,MSGL_DBG2
,"Adding entry %s\n",entries
[num
].file
);
306 entry
= play_tree_new();
307 play_tree_add_file(entry
,entries
[num
].file
);
308 free(entries
[num
].file
);
310 play_tree_append_entry(last_entry
,entry
);
315 if(entries
[num
].title
) {
316 // When we have info in playtree we add this info
317 free(entries
[num
].title
);
319 if(entries
[num
].length
) {
320 // When we have info in playtree we add this info
321 free(entries
[num
].length
);
327 entry
= play_tree_new();
328 play_tree_set_child(entry
,list
);
333 Reference Ini-Format: Each entry is assumed a reference
336 parse_ref_ini(play_tree_parser_t
* p
) {
338 play_tree_t
*list
= NULL
, *entry
= NULL
, *last_entry
= NULL
;
340 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Trying reference-ini playlist...\n");
341 if (!(line
= play_tree_parser_get_line(p
)))
344 if(strcasecmp(line
,"[Reference]"))
346 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Detected reference-ini playlist format\n");
347 play_tree_parser_stop_keeping(p
);
348 line
= play_tree_parser_get_line(p
);
353 if(strncasecmp(line
,"Ref",3) == 0) {
354 v
= pls_entry_get_value(line
+3);
356 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"No value in entry %s\n",line
);
359 mp_msg(MSGT_PLAYTREE
,MSGL_DBG2
,"Adding entry %s\n",v
);
360 entry
= play_tree_new();
361 play_tree_add_file(entry
,v
);
363 play_tree_append_entry(last_entry
,entry
);
369 line
= play_tree_parser_get_line(p
);
372 if(!list
) return NULL
;
373 entry
= play_tree_new();
374 play_tree_set_child(entry
,list
);
379 parse_m3u(play_tree_parser_t
* p
) {
381 play_tree_t
*list
= NULL
, *entry
= NULL
, *last_entry
= NULL
;
383 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Trying extended m3u playlist...\n");
384 if (!(line
= play_tree_parser_get_line(p
)))
387 if(strcasecmp(line
,"#EXTM3U"))
389 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Detected extended m3u playlist format\n");
390 play_tree_parser_stop_keeping(p
);
392 while((line
= play_tree_parser_get_line(p
)) != NULL
) {
396 /* EXTM3U files contain such lines:
397 * #EXTINF:<seconds>, <title>
398 * followed by a line with the filename
399 * for now we have no place to put that
400 * so we just skip that extra-info ::atmos
403 #if 0 /* code functional */
404 if(strncasecmp(line
,"#EXTINF:",8) == 0) {
405 mp_msg(MSGT_PLAYTREE
,MSGL_INFO
,"[M3U] Duration: %dsec Title: %s\n",
406 strtol(line
+8,&line
,10), line
+2);
411 entry
= play_tree_new();
412 play_tree_add_file(entry
,line
);
416 play_tree_append_entry(last_entry
,entry
);
420 if(!list
) return NULL
;
421 entry
= play_tree_new();
422 play_tree_set_child(entry
,list
);
427 parse_smil(play_tree_parser_t
* p
) {
429 char* line
,source
[512],*pos
,*s_start
,*s_end
;
430 play_tree_t
*list
= NULL
, *entry
= NULL
, *last_entry
= NULL
;
432 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Trying smil playlist...\n");
435 while((line
= play_tree_parser_get_line(p
)) != NULL
) {
437 if(line
[0] == '\0') // Ignore empties
439 if (strncasecmp(line
,"<smil",5)==0)
440 break; // smil header found
442 return NULL
; //line not smil exit
445 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Detected smil playlist format\n");
446 play_tree_parser_stop_keeping(p
);
449 //Get entries from smil
450 while((line
= play_tree_parser_get_line(p
)) != NULL
) {
454 if (!entrymode
) { // all entries filled so far
455 if (strncasecmp(line
,"<video",6)==0 || strncasecmp(line
,"<audio",6)==0) {
456 pos
=strstr(line
,"src="); // Is source present on this line
459 s_end
=strchr(s_start
,'"');
461 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Error parsing this source line %s\n",line
);
464 if (s_end
-s_start
> 511) {
465 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Cannot store such a large source %s\n",line
);
468 strncpy(source
,s_start
,s_end
-s_start
);
469 source
[(s_end
-s_start
)]='\0'; // Null terminate
470 entry
= play_tree_new();
471 play_tree_add_file(entry
,source
);
472 if(!list
) //Insert new entry
475 play_tree_append_entry(last_entry
,entry
);
481 } else { //Entry found but not yet filled
482 pos
= strstr(line
,"src="); // Is source present on this line
486 s_end
=strchr(s_start
,'"');
488 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Error parsing this source line %s\n",line
);
491 if (s_end
-s_start
> 511) {
492 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Cannot store such a large source %s\n",line
);
495 strncpy(source
,s_start
,s_end
-s_start
);
496 source
[(s_end
-s_start
)]='\0'; // Null terminate
497 entry
= play_tree_new();
498 play_tree_add_file(entry
,source
);
499 if(!list
) //Insert new entry
502 play_tree_append_entry(last_entry
,entry
);
508 if(!list
) return NULL
; // Nothing found
510 entry
= play_tree_new();
511 play_tree_set_child(entry
,list
);
516 embedded_playlist_parse(char *line
) {
517 int f
=DEMUXER_TYPE_PLAYLIST
;
519 play_tree_parser_t
* ptp
;
522 // Get stream opened to link
523 stream
=open_stream(line
,0,&f
);
525 mp_msg(MSGT_PLAYTREE
,MSGL_WARN
,"Can't open playlist %s\n",line
);
530 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Adding playlist %s to element entryref\n",line
);
532 ptp
= play_tree_parser_new(stream
,1);
533 entry
= play_tree_parser_get_play_tree(ptp
, 1);
534 play_tree_parser_free(ptp
);
541 parse_textplain(play_tree_parser_t
* p
) {
545 play_tree_t
*list
= NULL
, *entry
= NULL
, *last_entry
= NULL
;
547 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Trying plaintext playlist...\n");
548 play_tree_parser_stop_keeping(p
);
550 while((line
= play_tree_parser_get_line(p
)) != NULL
) {
552 if(line
[0] == '\0' || line
[0] == '#' || (line
[0] == '/' && line
[1] == '/'))
555 //Special check for embedded smil or ram reference in file
557 if (strlen(line
) > 5)
558 for(c
= line
; c
[0]; c
++ )
559 if ( ((c
[0] == '.') && //start with . and next have smil with optional ? or &
560 (tolower(c
[1]) == 's') && (tolower(c
[2])== 'm') &&
561 (tolower(c
[3]) == 'i') && (tolower(c
[4]) == 'l') &&
562 (!c
[5] || c
[5] == '?' || c
[5] == '&')) || // or
563 ((c
[0] == '.') && // start with . and next have smi or ram with optional ? or &
564 ( ((tolower(c
[1]) == 's') && (tolower(c
[2])== 'm') && (tolower(c
[3]) == 'i')) ||
565 ((tolower(c
[1]) == 'r') && (tolower(c
[2])== 'a') && (tolower(c
[3]) == 'm')) )
566 && (!c
[4] || c
[4] == '?' || c
[4] == '&')) ){
567 entry
=embedded_playlist_parse(line
);
572 if (!embedded
) { //regular file link
573 entry
= play_tree_new();
574 play_tree_add_file(entry
,line
);
581 play_tree_append_entry(last_entry
,entry
);
586 if(!list
) return NULL
;
587 entry
= play_tree_new();
588 play_tree_set_child(entry
,list
);
593 parse_playtree(stream_t
*stream
, int forced
) {
594 play_tree_parser_t
* p
;
598 assert(stream
!= NULL
);
601 p
= play_tree_parser_new(stream
,0);
605 ret
= play_tree_parser_get_play_tree(p
, forced
);
606 play_tree_parser_free(p
);
612 play_tree_add_basepath(play_tree_t
* pt
, char* bp
) {
613 int i
,bl
= strlen(bp
),fl
;
617 for(i
= pt
->child
; i
!= NULL
; i
= i
->next
)
618 play_tree_add_basepath(i
,bp
);
625 for(i
= 0 ; pt
->files
[i
] != NULL
; i
++) {
626 fl
= strlen(pt
->files
[i
]);
627 // if we find a full unix path, url:// or X:\ at the beginning,
629 if(fl
<= 0 || strstr(pt
->files
[i
],"://") || (strstr(pt
->files
[i
],":\\") == pt
->files
[i
] + 1) || (pt
->files
[i
][0] == '/') )
631 // if the path begins with \ then prepend drive letter to it.
632 if (pt
->files
[i
][0] == '\\') {
633 pt
->files
[i
] = (char*)realloc(pt
->files
[i
],2+fl
+1);
634 memmove(pt
->files
[i
] + 2,pt
->files
[i
],fl
+1);
635 memcpy(pt
->files
[i
],bp
,2);
638 pt
->files
[i
] = (char*)realloc(pt
->files
[i
],bl
+fl
+1);
639 memmove(pt
->files
[i
] + bl
,pt
->files
[i
],fl
+1);
640 memcpy(pt
->files
[i
],bp
,bl
);
644 // Wrapper for play_tree_add_basepath (add base path from file)
645 void play_tree_add_bpf(play_tree_t
* pt
, char* filename
)
651 file
= strdup(filename
);
654 ls
= strrchr(file
,'/');
655 if(!ls
) ls
= strrchr(file
,'\\');
658 play_tree_add_basepath(pt
,file
);
666 parse_playlist_file(char* file
) {
669 int f
=DEMUXER_TYPE_PLAYLIST
;
671 stream
= open_stream(file
,0,&f
);
674 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"Error while opening playlist file %s: %s\n",file
,strerror(errno
));
678 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Parsing playlist file %s...\n",file
);
680 ret
= parse_playtree(stream
,1);
683 play_tree_add_bpf(ret
, file
);
691 play_tree_parser_new(stream_t
* stream
,int deep
) {
692 play_tree_parser_t
* p
;
694 p
= (play_tree_parser_t
*)calloc(1,sizeof(play_tree_parser_t
));
706 play_tree_parser_free(play_tree_parser_t
* p
) {
712 if(p
->buffer
) free(p
->buffer
);
713 if(p
->line
) free(p
->line
);
718 play_tree_parser_get_play_tree(play_tree_parser_t
* p
, int forced
) {
719 play_tree_t
* tree
= NULL
;
726 while(play_tree_parser_get_line(p
) != NULL
) {
727 play_tree_parser_reset(p
);
731 play_tree_parser_reset(p
);
735 play_tree_parser_reset(p
);
739 play_tree_parser_reset(p
);
741 tree
= parse_ref_ini(p
);
743 play_tree_parser_reset(p
);
745 tree
= parse_smil(p
);
747 play_tree_parser_reset(p
);
749 // Here come the others formats ( textplain must stay the last one )
752 tree
= parse_textplain(p
);
759 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Playlist successfully parsed\n");
761 mp_msg(MSGT_PLAYTREE
,((forced
==1)?MSGL_ERR
:MSGL_V
),"Error while parsing playlist\n");
764 tree
= play_tree_cleanup(tree
);
766 if(!tree
) mp_msg(MSGT_PLAYTREE
,((forced
==1)?MSGL_WARN
:MSGL_V
),"Warning: empty playlist\n");