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(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
->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
,"<smil",5)==0 || strncasecmp(line
,"<?wpl",5)==0 ||
457 strncasecmp(line
,"(smil-document",14)==0)
458 break; // smil header found
460 return NULL
; //line not smil exit
463 if (!line
) return NULL
;
464 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Detected smil playlist format\n");
465 play_tree_parser_stop_keeping(p
);
467 if (strncasecmp(line
,"(smil-document",14)==0) {
468 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Special smil-over-realrtsp playlist header\n");
470 if (sscanf(line
, "(smil-document (ver 1.0)(npkt %u)(ttlpkt %u", &npkt
, &ttlpkt
) != 2) {
471 mp_msg(MSGT_PLAYTREE
,MSGL_WARN
,"smil-over-realrtsp: header parsing failure, assuming single packet.\n");
474 if (ttlpkt
== 0 || npkt
> ttlpkt
) {
475 mp_msg(MSGT_PLAYTREE
,MSGL_WARN
,"smil-over-realrtsp: bad packet counters (npkk = %u, ttlpkt = %u), assuming single packet.\n",
481 //Get entries from smil
490 /* If we're parsing smil over realrtsp and this is not the last packet and
491 * this is the last line in the packet (terminating with ") ) we must get
492 * the next line, strip the header, and concatenate it to the current line.
494 if (is_rmsmil
&& npkt
!= ttlpkt
&& strstr(src_line
,"\")")) {
497 line
= strdup(src_line
);
498 if(!(src_line
= play_tree_parser_get_line(p
))) {
499 mp_msg(MSGT_PLAYTREE
,MSGL_WARN
,"smil-over-realrtsp: can't get line from packet %u/%u.\n", npkt
, ttlpkt
);
503 // Skip header, packet starts after "
504 if(!(payload
= strchr(src_line
,'\"'))) {
505 mp_msg(MSGT_PLAYTREE
,MSGL_WARN
,"smil-over-realrtsp: can't find start of packet, using complete line.\n");
509 // Skip ") at the end of the last line from the current packet
510 line
[strlen(line
)-2] = 0;
511 line
= realloc(line
, strlen(line
)+strlen(payload
)+1);
512 strcat (line
, payload
);
515 line
= strdup(src_line
);
516 /* Unescape \" to " for smil-over-rtsp */
517 if (is_rmsmil
&& line
[0] != '\0') {
520 for (i
= 0; i
< strlen(line
); i
++)
521 if (line
[i
] == '\\' && line
[i
+1] == '"')
522 for (j
= i
; line
[j
]; j
++)
527 if (!entrymode
) { // all entries filled so far
528 while ((pos
=strchr(pos
, '<'))) {
529 if (strncasecmp(pos
,"<video",6)==0 || strncasecmp(pos
,"<audio",6)==0 || strncasecmp(pos
,"<media",6)==0) {
531 break; // Got a valid tag, exit '<' search loop
536 if (entrymode
) { //Entry found but not yet filled
537 pos
= strstr(pos
,"src="); // Is source present on this line
540 if (pos
[4] != '"' && pos
[4] != '\'') {
541 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Unknown delimiter %c in source line %s\n", pos
[4], line
);
545 s_end
=strchr(s_start
,pos
[4]);
547 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Error parsing this source line %s\n",line
);
550 if (s_end
-s_start
> 511) {
551 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Cannot store such a large source %s\n",line
);
554 strncpy(source
,s_start
,s_end
-s_start
);
555 source
[(s_end
-s_start
)]='\0'; // Null terminate
556 entry
= play_tree_new();
557 play_tree_add_file(entry
,source
);
558 if(!list
) //Insert new entry
561 play_tree_append_entry(last_entry
,entry
);
567 } while((src_line
= play_tree_parser_get_line(p
)) != NULL
);
572 if(!list
) return NULL
; // Nothing found
574 entry
= play_tree_new();
575 play_tree_set_child(entry
,list
);
580 embedded_playlist_parse(char *line
) {
581 int f
=DEMUXER_TYPE_PLAYLIST
;
583 play_tree_parser_t
* ptp
;
586 // Get stream opened to link
587 stream
=open_stream(line
,0,&f
);
589 mp_msg(MSGT_PLAYTREE
,MSGL_WARN
,"Can't open playlist %s\n",line
);
594 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Adding playlist %s to element entryref\n",line
);
596 ptp
= play_tree_parser_new(stream
,1);
597 entry
= play_tree_parser_get_play_tree(ptp
, 1);
598 play_tree_parser_free(ptp
);
605 parse_textplain(play_tree_parser_t
* p
) {
609 play_tree_t
*list
= NULL
, *entry
= NULL
, *last_entry
= NULL
;
611 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Trying plaintext playlist...\n");
612 play_tree_parser_stop_keeping(p
);
614 while((line
= play_tree_parser_get_line(p
)) != NULL
) {
616 if(line
[0] == '\0' || line
[0] == '#' || (line
[0] == '/' && line
[1] == '/'))
619 //Special check for embedded smil or ram reference in file
621 if (strlen(line
) > 5)
622 for(c
= line
; c
[0]; c
++ )
623 if ( ((c
[0] == '.') && //start with . and next have smil with optional ? or &
624 (tolower(c
[1]) == 's') && (tolower(c
[2])== 'm') &&
625 (tolower(c
[3]) == 'i') && (tolower(c
[4]) == 'l') &&
626 (!c
[5] || c
[5] == '?' || c
[5] == '&')) || // or
627 ((c
[0] == '.') && // start with . and next have smi or ram with optional ? or &
628 ( ((tolower(c
[1]) == 's') && (tolower(c
[2])== 'm') && (tolower(c
[3]) == 'i')) ||
629 ((tolower(c
[1]) == 'r') && (tolower(c
[2])== 'a') && (tolower(c
[3]) == 'm')) )
630 && (!c
[4] || c
[4] == '?' || c
[4] == '&')) ){
631 entry
=embedded_playlist_parse(line
);
636 if (!embedded
) { //regular file link
637 entry
= play_tree_new();
638 play_tree_add_file(entry
,line
);
645 play_tree_append_entry(last_entry
,entry
);
650 if(!list
) return NULL
;
651 entry
= play_tree_new();
652 play_tree_set_child(entry
,list
);
657 parse_playtree(stream_t
*stream
, int forced
) {
658 play_tree_parser_t
* p
;
662 assert(stream
!= NULL
);
665 p
= play_tree_parser_new(stream
,0);
669 ret
= play_tree_parser_get_play_tree(p
, forced
);
670 play_tree_parser_free(p
);
676 play_tree_add_basepath(play_tree_t
* pt
, char* bp
) {
677 int i
,bl
= strlen(bp
),fl
;
681 for(i
= pt
->child
; i
!= NULL
; i
= i
->next
)
682 play_tree_add_basepath(i
,bp
);
689 for(i
= 0 ; pt
->files
[i
] != NULL
; i
++) {
690 fl
= strlen(pt
->files
[i
]);
691 // if we find a full unix path, url:// or X:\ at the beginning,
693 if(fl
<= 0 || strstr(pt
->files
[i
],"://") || (strstr(pt
->files
[i
],":\\") == pt
->files
[i
] + 1) || (pt
->files
[i
][0] == '/') )
695 // if the path begins with \ then prepend drive letter to it.
696 if (pt
->files
[i
][0] == '\\') {
697 if (pt
->files
[i
][1] == '\\')
699 pt
->files
[i
] = (char*)realloc(pt
->files
[i
],2+fl
+1);
700 memmove(pt
->files
[i
] + 2,pt
->files
[i
],fl
+1);
701 memcpy(pt
->files
[i
],bp
,2);
704 pt
->files
[i
] = (char*)realloc(pt
->files
[i
],bl
+fl
+1);
705 memmove(pt
->files
[i
] + bl
,pt
->files
[i
],fl
+1);
706 memcpy(pt
->files
[i
],bp
,bl
);
710 // Wrapper for play_tree_add_basepath (add base path from file)
711 void play_tree_add_bpf(play_tree_t
* pt
, char* filename
)
717 file
= strdup(filename
);
720 ls
= strrchr(file
,'/');
721 if(!ls
) ls
= strrchr(file
,'\\');
724 play_tree_add_basepath(pt
,file
);
732 parse_playlist_file(char* file
) {
735 int f
=DEMUXER_TYPE_PLAYLIST
;
737 stream
= open_stream(file
,0,&f
);
740 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"Error while opening playlist file %s: %s\n",file
,strerror(errno
));
744 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Parsing playlist file %s...\n",file
);
746 ret
= parse_playtree(stream
,1);
749 play_tree_add_bpf(ret
, file
);
757 play_tree_parser_new(stream_t
* stream
,int deep
) {
758 play_tree_parser_t
* p
;
760 p
= calloc(1,sizeof(play_tree_parser_t
));
772 play_tree_parser_free(play_tree_parser_t
* p
) {
778 if(p
->buffer
) free(p
->buffer
);
779 if(p
->line
) free(p
->line
);
784 play_tree_parser_get_play_tree(play_tree_parser_t
* p
, int forced
) {
785 play_tree_t
* tree
= NULL
;
792 while(play_tree_parser_get_line(p
) != NULL
) {
793 play_tree_parser_reset(p
);
797 play_tree_parser_reset(p
);
801 play_tree_parser_reset(p
);
805 play_tree_parser_reset(p
);
807 tree
= parse_ref_ini(p
);
809 play_tree_parser_reset(p
);
811 tree
= parse_smil(p
);
813 play_tree_parser_reset(p
);
815 // Here come the others formats ( textplain must stay the last one )
818 tree
= parse_textplain(p
);
825 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Playlist successfully parsed\n");
827 mp_msg(MSGT_PLAYTREE
,((forced
==1)?MSGL_ERR
:MSGL_V
),"Error while parsing playlist\n");
830 tree
= play_tree_cleanup(tree
);
832 if(!tree
) mp_msg(MSGT_PLAYTREE
,((forced
==1)?MSGL_WARN
:MSGL_V
),"Warning: empty playlist\n");