3 /// \ingroup PlaytreeParser
11 #include <sys/types.h>
18 #include "playtreeparser.h"
19 #include "stream/stream.h"
20 #include "libmpdemux/demuxer.h"
25 asx_parser_build_tree(struct m_config
*mconfig
, char* buffer
, int ref
);
29 #define WHITES " \n\r\t"
37 for(i
= str
; i
[0] != '\0' && strchr(WHITES
,i
[0]) != NULL
; i
++)
40 memmove(str
,i
,strlen(i
) + 1);
41 for(i
= str
+ strlen(str
) - 1 ; strchr(WHITES
,i
[0]) != NULL
; i
--)
49 play_tree_parser_get_line(play_tree_parser_t
* p
) {
53 if(p
->buffer
== NULL
) {
54 p
->buffer
= malloc(BUF_STEP
);
55 p
->buffer_size
= BUF_STEP
;
60 if(p
->stream
->eof
&& (p
->buffer_end
== 0 || p
->iter
[0] == '\0'))
63 assert(p
->buffer_end
< p
->buffer_size
);
64 assert(!p
->buffer
[p
->buffer_end
]);
68 r
= p
->iter
- p
->buffer
;
69 p
->buffer
= (char*)realloc(p
->buffer
,p
->buffer_size
+BUF_STEP
);
70 p
->iter
= p
->buffer
+ r
;
71 p
->buffer_size
+= BUF_STEP
;
75 if(p
->buffer_size
- p
->buffer_end
> 1 && ! p
->stream
->eof
) {
76 r
= stream_read(p
->stream
,p
->buffer
+ p
->buffer_end
,p
->buffer_size
- p
->buffer_end
- 1);
79 assert(p
->buffer_end
< p
->buffer_size
);
80 p
->buffer
[p
->buffer_end
] = '\0';
81 while(strlen(p
->buffer
+ p
->buffer_end
- r
) != r
)
82 p
->buffer
[p
->buffer_end
- r
+ strlen(p
->buffer
+ p
->buffer_end
- r
)] = '\n';
84 assert(!p
->buffer
[p
->buffer_end
]);
87 end
= strchr(p
->iter
,'\n');
90 end
= p
->buffer
+ p
->buffer_end
;
99 line_end
= (end
> p
->iter
&& *(end
-1) == '\r') ? end
-1 : end
;
100 if(line_end
- p
->iter
>= 0)
101 p
->line
= (char*)realloc(p
->line
,line_end
- p
->iter
+1);
104 if(line_end
- p
->iter
> 0)
105 strncpy(p
->line
,p
->iter
,line_end
- p
->iter
);
106 p
->line
[line_end
- p
->iter
] = '\0';
112 p
->buffer_end
-= end
-p
->iter
;
113 memmove(p
->buffer
,end
,p
->buffer_end
);
116 p
->buffer
[p
->buffer_end
] = '\0';
125 play_tree_parser_reset(play_tree_parser_t
* p
) {
130 play_tree_parser_stop_keeping(play_tree_parser_t
* p
) {
132 if(p
->iter
&& p
->iter
!= p
->buffer
) {
133 p
->buffer_end
-= p
->iter
-p
->buffer
;
135 memmove(p
->buffer
,p
->iter
,p
->buffer_end
);
136 p
->buffer
[p
->buffer_end
] = 0;
143 parse_asx(play_tree_parser_t
* p
) {
144 int comments
= 0,get_line
= 1;
147 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Trying asx...\n");
151 line
= play_tree_parser_get_line(p
);
160 mp_msg(MSGT_PLAYTREE
,MSGL_DBG2
,"First char isn't '<' but '%c'\n",line
[0]);
161 mp_msg(MSGT_PLAYTREE
,MSGL_DBG3
,"Buffer = [%s]\n",p
->buffer
);
163 } else if(strncmp(line
,"<!--",4) == 0) { // Comments
166 if(line
[0] != '\0' && strlen(line
) > 0)
168 } else if(strncasecmp(line
,"<ASX",4) == 0) // We got an asx element
170 else // We don't get an asx
174 c
= strchr(line
,'-');
176 if (strncmp(c
,"--!>",4) == 0) { // End of comments
179 if(line
[0] != '\0') // There is some more data on this line : keep it
183 line
= c
+1; // Jump the -
184 if(line
[0] != '\0') // Some more data
189 } else // No - on this line (or rest of line) : get next one
194 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Detected asx format\n");
196 // We have an asx : load it in memory and parse
198 while((line
= play_tree_parser_get_line(p
)) != NULL
)
201 mp_msg(MSGT_PLAYTREE
,MSGL_DBG3
,"Parsing asx file: [%s]\n",p
->buffer
);
202 return asx_parser_build_tree(p
->mconfig
, p
->buffer
,p
->deep
);
206 pls_entry_get_value(char* line
) {
209 i
= strchr(line
,'=');
210 if(!i
|| i
[1] == '\0')
216 typedef struct pls_entry
{
223 pls_read_entry(char* line
,pls_entry_t
** _e
,int* _max_entry
,char** val
) {
224 int num
,max_entry
= (*_max_entry
);
225 pls_entry_t
* e
= (*_e
);
228 v
= pls_entry_get_value(line
);
230 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"No value in entry %s\n",line
);
237 mp_msg(MSGT_PLAYTREE
,MSGL_WARN
,"No entry index in entry %s\nAssuming %d\n",line
,num
);
239 if(num
> max_entry
) {
240 e
= (pls_entry_t
*)realloc(e
,num
*sizeof(pls_entry_t
));
241 memset(&e
[max_entry
],0,(num
-max_entry
)*sizeof(pls_entry_t
));
245 (*_max_entry
) = max_entry
;
253 parse_pls(play_tree_parser_t
* p
) {
255 pls_entry_t
* entries
= NULL
;
256 int n_entries
= 0,max_entry
=0,num
;
257 play_tree_t
*list
= NULL
, *entry
= NULL
, *last_entry
= NULL
;
259 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Trying Winamp playlist...\n");
260 while((line
= play_tree_parser_get_line(p
))) {
267 if(strcasecmp(line
,"[playlist]"))
269 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Detected Winamp playlist format\n");
270 play_tree_parser_stop_keeping(p
);
271 line
= play_tree_parser_get_line(p
);
275 if(strncasecmp(line
,"NumberOfEntries",15) == 0) {
276 v
= pls_entry_get_value(line
);
279 mp_msg(MSGT_PLAYTREE
,MSGL_DBG2
,"Invalid number of entries: very funny!!!\n");
281 mp_msg(MSGT_PLAYTREE
,MSGL_DBG2
,"Playlist claims to have %d entries. Let's see.\n",n_entries
);
282 line
= play_tree_parser_get_line(p
);
287 if(line
[0] == '\0') {
288 line
= play_tree_parser_get_line(p
);
291 if(strncasecmp(line
,"File",4) == 0) {
292 num
= pls_read_entry(line
+4,&entries
,&max_entry
,&v
);
294 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"No value in entry %s\n",line
);
296 entries
[num
-1].file
= strdup(v
);
297 } else if(strncasecmp(line
,"Title",5) == 0) {
298 num
= pls_read_entry(line
+5,&entries
,&max_entry
,&v
);
300 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"No value in entry %s\n",line
);
302 entries
[num
-1].title
= strdup(v
);
303 } else if(strncasecmp(line
,"Length",6) == 0) {
304 num
= pls_read_entry(line
+6,&entries
,&max_entry
,&v
);
306 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"No value in entry %s\n",line
);
308 entries
[num
-1].length
= strdup(v
);
310 mp_msg(MSGT_PLAYTREE
,MSGL_WARN
,"Unknown entry type %s\n",line
);
311 line
= play_tree_parser_get_line(p
);
314 for(num
= 0; num
< max_entry
; num
++) {
315 if(entries
[num
].file
== NULL
)
316 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"Entry %d don't have a file !!!!\n",num
+1);
318 mp_msg(MSGT_PLAYTREE
,MSGL_DBG2
,"Adding entry %s\n",entries
[num
].file
);
319 entry
= play_tree_new();
320 play_tree_add_file(entry
,entries
[num
].file
);
321 free(entries
[num
].file
);
323 play_tree_append_entry(last_entry
,entry
);
328 if(entries
[num
].title
) {
329 // When we have info in playtree we add this info
330 free(entries
[num
].title
);
332 if(entries
[num
].length
) {
333 // When we have info in playtree we add this info
334 free(entries
[num
].length
);
340 entry
= play_tree_new();
341 play_tree_set_child(entry
,list
);
346 Reference Ini-Format: Each entry is assumed a reference
349 parse_ref_ini(play_tree_parser_t
* p
) {
351 play_tree_t
*list
= NULL
, *entry
= NULL
, *last_entry
= NULL
;
353 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Trying reference-ini playlist...\n");
354 if (!(line
= play_tree_parser_get_line(p
)))
357 if(strcasecmp(line
,"[Reference]"))
359 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Detected reference-ini playlist format\n");
360 play_tree_parser_stop_keeping(p
);
361 line
= play_tree_parser_get_line(p
);
366 if(strncasecmp(line
,"Ref",3) == 0) {
367 v
= pls_entry_get_value(line
+3);
369 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"No value in entry %s\n",line
);
372 mp_msg(MSGT_PLAYTREE
,MSGL_DBG2
,"Adding entry %s\n",v
);
373 entry
= play_tree_new();
374 play_tree_add_file(entry
,v
);
376 play_tree_append_entry(last_entry
,entry
);
382 line
= play_tree_parser_get_line(p
);
385 if(!list
) return NULL
;
386 entry
= play_tree_new();
387 play_tree_set_child(entry
,list
);
392 parse_m3u(play_tree_parser_t
* p
) {
394 play_tree_t
*list
= NULL
, *entry
= NULL
, *last_entry
= NULL
;
396 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Trying extended m3u playlist...\n");
397 if (!(line
= play_tree_parser_get_line(p
)))
400 if(strcasecmp(line
,"#EXTM3U"))
402 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Detected extended m3u playlist format\n");
403 play_tree_parser_stop_keeping(p
);
405 while((line
= play_tree_parser_get_line(p
)) != NULL
) {
409 /* EXTM3U files contain such lines:
410 * #EXTINF:<seconds>, <title>
411 * followed by a line with the filename
412 * for now we have no place to put that
413 * so we just skip that extra-info ::atmos
416 #if 0 /* code functional */
417 if(strncasecmp(line
,"#EXTINF:",8) == 0) {
418 mp_msg(MSGT_PLAYTREE
,MSGL_INFO
,"[M3U] Duration: %dsec Title: %s\n",
419 strtol(line
+8,&line
,10), line
+2);
424 entry
= play_tree_new();
425 play_tree_add_file(entry
,line
);
429 play_tree_append_entry(last_entry
,entry
);
433 if(!list
) return NULL
;
434 entry
= play_tree_new();
435 play_tree_set_child(entry
,list
);
440 parse_smil(play_tree_parser_t
* p
) {
442 char* line
,source
[512],*pos
,*s_start
,*s_end
,*src_line
;
443 play_tree_t
*list
= NULL
, *entry
= NULL
, *last_entry
= NULL
;
445 unsigned int npkt
, ttlpkt
;
447 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Trying smil playlist...\n");
450 while((line
= play_tree_parser_get_line(p
)) != NULL
) {
452 if(line
[0] == '\0') // Ignore empties
454 if (strncasecmp(line
,"<?xml",5)==0) // smil in xml
456 if (strncasecmp(line
,"<!DOCTYPE smil",13)==0) // smil in xml
458 if (strncasecmp(line
,"<smil",5)==0 || strncasecmp(line
,"<?wpl",5)==0 ||
459 strncasecmp(line
,"(smil-document",14)==0)
460 break; // smil header found
462 return NULL
; //line not smil exit
465 if (!line
) return NULL
;
466 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Detected smil playlist format\n");
467 play_tree_parser_stop_keeping(p
);
469 if (strncasecmp(line
,"(smil-document",14)==0) {
470 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Special smil-over-realrtsp playlist header\n");
472 if (sscanf(line
, "(smil-document (ver 1.0)(npkt %u)(ttlpkt %u", &npkt
, &ttlpkt
) != 2) {
473 mp_msg(MSGT_PLAYTREE
,MSGL_WARN
,"smil-over-realrtsp: header parsing failure, assuming single packet.\n");
476 if (ttlpkt
== 0 || npkt
> ttlpkt
) {
477 mp_msg(MSGT_PLAYTREE
,MSGL_WARN
,"smil-over-realrtsp: bad packet counters (npkk = %u, ttlpkt = %u), assuming single packet.\n",
483 //Get entries from smil
492 /* If we're parsing smil over realrtsp and this is not the last packet and
493 * this is the last line in the packet (terminating with ") ) we must get
494 * the next line, strip the header, and concatenate it to the current line.
496 if (is_rmsmil
&& npkt
!= ttlpkt
&& strstr(src_line
,"\")")) {
499 line
= strdup(src_line
);
500 if(!(src_line
= play_tree_parser_get_line(p
))) {
501 mp_msg(MSGT_PLAYTREE
,MSGL_WARN
,"smil-over-realrtsp: can't get line from packet %u/%u.\n", npkt
, ttlpkt
);
505 // Skip header, packet starts after "
506 if(!(payload
= strchr(src_line
,'\"'))) {
507 mp_msg(MSGT_PLAYTREE
,MSGL_WARN
,"smil-over-realrtsp: can't find start of packet, using complete line.\n");
511 // Skip ") at the end of the last line from the current packet
512 line
[strlen(line
)-2] = 0;
513 line
= realloc(line
, strlen(line
)+strlen(payload
)+1);
514 strcat (line
, payload
);
517 line
= strdup(src_line
);
518 /* Unescape \" to " for smil-over-rtsp */
519 if (is_rmsmil
&& line
[0] != '\0') {
522 for (i
= 0; i
< strlen(line
); i
++)
523 if (line
[i
] == '\\' && line
[i
+1] == '"')
524 for (j
= i
; line
[j
]; j
++)
529 if (!entrymode
) { // all entries filled so far
530 while ((pos
=strchr(pos
, '<'))) {
531 if (strncasecmp(pos
,"<video",6)==0 || strncasecmp(pos
,"<audio",6)==0 || strncasecmp(pos
,"<media",6)==0) {
533 break; // Got a valid tag, exit '<' search loop
538 if (entrymode
) { //Entry found but not yet filled
539 pos
= strstr(pos
,"src="); // Is source present on this line
542 if (pos
[4] != '"' && pos
[4] != '\'') {
543 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Unknown delimiter %c in source line %s\n", pos
[4], line
);
547 s_end
=strchr(s_start
,pos
[4]);
549 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Error parsing this source line %s\n",line
);
552 if (s_end
-s_start
> 511) {
553 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Cannot store such a large source %s\n",line
);
556 strncpy(source
,s_start
,s_end
-s_start
);
557 source
[(s_end
-s_start
)]='\0'; // Null terminate
558 entry
= play_tree_new();
559 play_tree_add_file(entry
,source
);
560 if(!list
) //Insert new entry
563 play_tree_append_entry(last_entry
,entry
);
569 } while((src_line
= play_tree_parser_get_line(p
)) != NULL
);
574 if(!list
) return NULL
; // Nothing found
576 entry
= play_tree_new();
577 play_tree_set_child(entry
,list
);
582 embedded_playlist_parse(struct m_config
*mconfig
, char *line
) {
583 int f
=DEMUXER_TYPE_PLAYLIST
;
585 play_tree_parser_t
* ptp
;
588 // Get stream opened to link
589 stream
=open_stream(line
,0,&f
);
591 mp_msg(MSGT_PLAYTREE
,MSGL_WARN
,"Can't open playlist %s\n",line
);
596 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Adding playlist %s to element entryref\n",line
);
598 ptp
= play_tree_parser_new(stream
, mconfig
, 1);
599 entry
= play_tree_parser_get_play_tree(ptp
, 1);
600 play_tree_parser_free(ptp
);
607 parse_textplain(play_tree_parser_t
* p
) {
611 play_tree_t
*list
= NULL
, *entry
= NULL
, *last_entry
= NULL
;
613 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Trying plaintext playlist...\n");
614 play_tree_parser_stop_keeping(p
);
616 while((line
= play_tree_parser_get_line(p
)) != NULL
) {
618 if(line
[0] == '\0' || line
[0] == '#' || (line
[0] == '/' && line
[1] == '/'))
621 //Special check for embedded smil or ram reference in file
623 if (strlen(line
) > 5)
624 for(c
= line
; c
[0]; c
++ )
625 if ( ((c
[0] == '.') && //start with . and next have smil with optional ? or &
626 (tolower(c
[1]) == 's') && (tolower(c
[2])== 'm') &&
627 (tolower(c
[3]) == 'i') && (tolower(c
[4]) == 'l') &&
628 (!c
[5] || c
[5] == '?' || c
[5] == '&')) || // or
629 ((c
[0] == '.') && // start with . and next have smi or ram with optional ? or &
630 ( ((tolower(c
[1]) == 's') && (tolower(c
[2])== 'm') && (tolower(c
[3]) == 'i')) ||
631 ((tolower(c
[1]) == 'r') && (tolower(c
[2])== 'a') && (tolower(c
[3]) == 'm')) )
632 && (!c
[4] || c
[4] == '?' || c
[4] == '&')) ){
633 entry
=embedded_playlist_parse(p
->mconfig
, line
);
638 if (!embedded
) { //regular file link
639 entry
= play_tree_new();
640 play_tree_add_file(entry
,line
);
647 play_tree_append_entry(last_entry
,entry
);
652 if(!list
) return NULL
;
653 entry
= play_tree_new();
654 play_tree_set_child(entry
,list
);
659 parse_playtree(stream_t
*stream
, struct m_config
*mconfig
, int forced
) {
660 play_tree_parser_t
* p
;
664 assert(stream
!= NULL
);
667 p
= play_tree_parser_new(stream
, mconfig
, 0);
671 ret
= play_tree_parser_get_play_tree(p
, forced
);
672 play_tree_parser_free(p
);
678 play_tree_add_basepath(play_tree_t
* pt
, char* bp
) {
679 int i
,bl
= strlen(bp
),fl
;
683 for(i
= pt
->child
; i
!= NULL
; i
= i
->next
)
684 play_tree_add_basepath(i
,bp
);
691 for(i
= 0 ; pt
->files
[i
] != NULL
; i
++) {
692 fl
= strlen(pt
->files
[i
]);
693 // if we find a full unix path, url:// or X:\ at the beginning,
695 if(fl
<= 0 || strstr(pt
->files
[i
],"://") || (strstr(pt
->files
[i
],":\\") == pt
->files
[i
] + 1) || (pt
->files
[i
][0] == '/') )
697 // if the path begins with \ then prepend drive letter to it.
698 if (pt
->files
[i
][0] == '\\') {
699 if (pt
->files
[i
][1] == '\\')
701 pt
->files
[i
] = (char*)realloc(pt
->files
[i
],2+fl
+1);
702 memmove(pt
->files
[i
] + 2,pt
->files
[i
],fl
+1);
703 memcpy(pt
->files
[i
],bp
,2);
706 pt
->files
[i
] = (char*)realloc(pt
->files
[i
],bl
+fl
+1);
707 memmove(pt
->files
[i
] + bl
,pt
->files
[i
],fl
+1);
708 memcpy(pt
->files
[i
],bp
,bl
);
712 // Wrapper for play_tree_add_basepath (add base path from file)
713 void play_tree_add_bpf(play_tree_t
* pt
, char* filename
)
719 file
= strdup(filename
);
722 ls
= strrchr(file
,'/');
723 if(!ls
) ls
= strrchr(file
,'\\');
726 play_tree_add_basepath(pt
,file
);
734 parse_playlist_file(struct m_config
*mconfig
, char* file
) {
737 int f
=DEMUXER_TYPE_PLAYLIST
;
739 stream
= open_stream(file
,0,&f
);
742 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"Error while opening playlist file %s: %s\n",file
,strerror(errno
));
746 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Parsing playlist file %s...\n",file
);
748 ret
= parse_playtree(stream
, mconfig
, 1);
751 play_tree_add_bpf(ret
, file
);
759 play_tree_parser_new(stream_t
* stream
, struct m_config
*mconfig
, int deep
) {
760 play_tree_parser_t
* p
;
762 p
= calloc(1,sizeof(play_tree_parser_t
));
766 p
->mconfig
= mconfig
;
775 play_tree_parser_free(play_tree_parser_t
* p
) {
781 if(p
->buffer
) free(p
->buffer
);
782 if(p
->line
) free(p
->line
);
787 play_tree_parser_get_play_tree(play_tree_parser_t
* p
, int forced
) {
788 play_tree_t
* tree
= NULL
;
795 while(play_tree_parser_get_line(p
) != NULL
) {
796 play_tree_parser_reset(p
);
800 play_tree_parser_reset(p
);
804 play_tree_parser_reset(p
);
808 play_tree_parser_reset(p
);
810 tree
= parse_ref_ini(p
);
812 play_tree_parser_reset(p
);
814 tree
= parse_smil(p
);
816 play_tree_parser_reset(p
);
818 // Here come the others formats ( textplain must stay the last one )
821 tree
= parse_textplain(p
);
828 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Playlist successfully parsed\n");
830 mp_msg(MSGT_PLAYTREE
,((forced
==1)?MSGL_ERR
:MSGL_V
),"Error while parsing playlist\n");
833 tree
= play_tree_cleanup(tree
);
835 if(!tree
) mp_msg(MSGT_PLAYTREE
,((forced
==1)?MSGL_WARN
:MSGL_V
),"Warning: empty playlist\n");