2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 /// \ingroup PlaytreeParser
28 #include <sys/types.h>
35 #include "playtreeparser.h"
36 #include "stream/stream.h"
37 #include "libmpdemux/demuxer.h"
42 asx_parser_build_tree(char* buffer
, int ref
);
46 #define WHITES " \n\r\t"
54 for(i
= str
; i
[0] != '\0' && strchr(WHITES
,i
[0]) != NULL
; i
++)
57 memmove(str
,i
,strlen(i
) + 1);
58 for(i
= str
+ strlen(str
) - 1 ; strchr(WHITES
,i
[0]) != NULL
; i
--)
66 play_tree_parser_get_line(play_tree_parser_t
* p
) {
70 if(p
->buffer
== NULL
) {
71 p
->buffer
= malloc(BUF_STEP
);
72 p
->buffer_size
= BUF_STEP
;
77 if(p
->stream
->eof
&& (p
->buffer_end
== 0 || p
->iter
[0] == '\0'))
80 assert(p
->buffer_end
< p
->buffer_size
);
81 assert(!p
->buffer
[p
->buffer_end
]);
85 r
= p
->iter
- p
->buffer
;
86 p
->buffer
= (char*)realloc(p
->buffer
,p
->buffer_size
+BUF_STEP
);
87 p
->iter
= p
->buffer
+ r
;
88 p
->buffer_size
+= BUF_STEP
;
92 if(p
->buffer_size
- p
->buffer_end
> 1 && ! p
->stream
->eof
) {
93 r
= stream_read(p
->stream
,p
->buffer
+ p
->buffer_end
,p
->buffer_size
- p
->buffer_end
- 1);
96 assert(p
->buffer_end
< p
->buffer_size
);
97 p
->buffer
[p
->buffer_end
] = '\0';
98 while(strlen(p
->buffer
+ p
->buffer_end
- r
) != r
)
99 p
->buffer
[p
->buffer_end
- r
+ strlen(p
->buffer
+ p
->buffer_end
- r
)] = '\n';
101 assert(!p
->buffer
[p
->buffer_end
]);
104 end
= strchr(p
->iter
,'\n');
107 end
= p
->buffer
+ p
->buffer_end
;
116 line_end
= (end
> p
->iter
&& *(end
-1) == '\r') ? end
-1 : end
;
117 if(line_end
- p
->iter
>= 0)
118 p
->line
= (char*)realloc(p
->line
,line_end
- p
->iter
+1);
121 if(line_end
- p
->iter
> 0)
122 strncpy(p
->line
,p
->iter
,line_end
- p
->iter
);
123 p
->line
[line_end
- p
->iter
] = '\0';
129 p
->buffer_end
-= end
-p
->iter
;
130 memmove(p
->buffer
,end
,p
->buffer_end
);
133 p
->buffer
[p
->buffer_end
] = '\0';
142 play_tree_parser_reset(play_tree_parser_t
* p
) {
147 play_tree_parser_stop_keeping(play_tree_parser_t
* p
) {
149 if(p
->iter
&& p
->iter
!= p
->buffer
) {
150 p
->buffer_end
-= p
->iter
-p
->buffer
;
152 memmove(p
->buffer
,p
->iter
,p
->buffer_end
);
153 p
->buffer
[p
->buffer_end
] = 0;
160 parse_asx(play_tree_parser_t
* p
) {
161 int comments
= 0,get_line
= 1;
164 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Trying asx...\n");
168 line
= play_tree_parser_get_line(p
);
177 mp_msg(MSGT_PLAYTREE
,MSGL_DBG2
,"First char isn't '<' but '%c'\n",line
[0]);
178 mp_msg(MSGT_PLAYTREE
,MSGL_DBG3
,"Buffer = [%s]\n",p
->buffer
);
180 } else if(strncmp(line
,"<!--",4) == 0) { // Comments
183 if(line
[0] != '\0' && strlen(line
) > 0)
185 } else if(strncasecmp(line
,"<ASX",4) == 0) // We got an asx element
187 else // We don't get an asx
191 c
= strchr(line
,'-');
193 if (strncmp(c
,"--!>",4) == 0) { // End of comments
196 if(line
[0] != '\0') // There is some more data on this line : keep it
200 line
= c
+1; // Jump the -
201 if(line
[0] != '\0') // Some more data
206 } else // No - on this line (or rest of line) : get next one
211 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Detected asx format\n");
213 // We have an asx : load it in memory and parse
215 while((line
= play_tree_parser_get_line(p
)) != NULL
)
218 mp_msg(MSGT_PLAYTREE
,MSGL_DBG3
,"Parsing asx file: [%s]\n",p
->buffer
);
219 return asx_parser_build_tree(p
->buffer
,p
->deep
);
223 pls_entry_get_value(char* line
) {
226 i
= strchr(line
,'=');
227 if(!i
|| i
[1] == '\0')
233 typedef struct pls_entry
{
240 pls_read_entry(char* line
,pls_entry_t
** _e
,int* _max_entry
,char** val
) {
241 int num
,max_entry
= (*_max_entry
);
242 pls_entry_t
* e
= (*_e
);
245 v
= pls_entry_get_value(line
);
247 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"No value in entry %s\n",line
);
254 mp_msg(MSGT_PLAYTREE
,MSGL_WARN
,"No entry index in entry %s\nAssuming %d\n",line
,num
);
256 if(num
> max_entry
) {
257 e
= (pls_entry_t
*)realloc(e
,num
*sizeof(pls_entry_t
));
258 memset(&e
[max_entry
],0,(num
-max_entry
)*sizeof(pls_entry_t
));
262 (*_max_entry
) = max_entry
;
270 parse_pls(play_tree_parser_t
* p
) {
272 pls_entry_t
* entries
= NULL
;
273 int n_entries
= 0,max_entry
=0,num
;
274 play_tree_t
*list
= NULL
, *entry
= NULL
, *last_entry
= NULL
;
276 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Trying Winamp playlist...\n");
277 while((line
= play_tree_parser_get_line(p
))) {
284 if(strcasecmp(line
,"[playlist]"))
286 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Detected Winamp playlist format\n");
287 play_tree_parser_stop_keeping(p
);
288 line
= play_tree_parser_get_line(p
);
292 if(strncasecmp(line
,"NumberOfEntries",15) == 0) {
293 v
= pls_entry_get_value(line
);
296 mp_msg(MSGT_PLAYTREE
,MSGL_DBG2
,"Invalid number of entries: very funny!!!\n");
298 mp_msg(MSGT_PLAYTREE
,MSGL_DBG2
,"Playlist claims to have %d entries. Let's see.\n",n_entries
);
299 line
= play_tree_parser_get_line(p
);
304 if(line
[0] == '\0') {
305 line
= play_tree_parser_get_line(p
);
308 if(strncasecmp(line
,"File",4) == 0) {
309 num
= pls_read_entry(line
+4,&entries
,&max_entry
,&v
);
311 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"No value in entry %s\n",line
);
313 entries
[num
-1].file
= strdup(v
);
314 } else if(strncasecmp(line
,"Title",5) == 0) {
315 num
= pls_read_entry(line
+5,&entries
,&max_entry
,&v
);
317 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"No value in entry %s\n",line
);
319 entries
[num
-1].title
= strdup(v
);
320 } else if(strncasecmp(line
,"Length",6) == 0) {
321 num
= pls_read_entry(line
+6,&entries
,&max_entry
,&v
);
323 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"No value in entry %s\n",line
);
325 entries
[num
-1].length
= strdup(v
);
327 mp_msg(MSGT_PLAYTREE
,MSGL_WARN
,"Unknown entry type %s\n",line
);
328 line
= play_tree_parser_get_line(p
);
331 for(num
= 0; num
< max_entry
; num
++) {
332 if(entries
[num
].file
== NULL
)
333 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"Entry %d don't have a file !!!!\n",num
+1);
335 mp_msg(MSGT_PLAYTREE
,MSGL_DBG2
,"Adding entry %s\n",entries
[num
].file
);
336 entry
= play_tree_new();
337 play_tree_add_file(entry
,entries
[num
].file
);
338 free(entries
[num
].file
);
340 play_tree_append_entry(last_entry
,entry
);
345 if(entries
[num
].title
) {
346 // When we have info in playtree we add this info
347 free(entries
[num
].title
);
349 if(entries
[num
].length
) {
350 // When we have info in playtree we add this info
351 free(entries
[num
].length
);
357 entry
= play_tree_new();
358 play_tree_set_child(entry
,list
);
363 Reference Ini-Format: Each entry is assumed a reference
366 parse_ref_ini(play_tree_parser_t
* p
) {
368 play_tree_t
*list
= NULL
, *entry
= NULL
, *last_entry
= NULL
;
370 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Trying reference-ini playlist...\n");
371 if (!(line
= play_tree_parser_get_line(p
)))
374 if(strcasecmp(line
,"[Reference]"))
376 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Detected reference-ini playlist format\n");
377 play_tree_parser_stop_keeping(p
);
378 line
= play_tree_parser_get_line(p
);
383 if(strncasecmp(line
,"Ref",3) == 0) {
384 v
= pls_entry_get_value(line
+3);
386 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"No value in entry %s\n",line
);
389 mp_msg(MSGT_PLAYTREE
,MSGL_DBG2
,"Adding entry %s\n",v
);
390 entry
= play_tree_new();
391 play_tree_add_file(entry
,v
);
393 play_tree_append_entry(last_entry
,entry
);
399 line
= play_tree_parser_get_line(p
);
402 if(!list
) return NULL
;
403 entry
= play_tree_new();
404 play_tree_set_child(entry
,list
);
409 parse_m3u(play_tree_parser_t
* p
) {
411 play_tree_t
*list
= NULL
, *entry
= NULL
, *last_entry
= NULL
;
413 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Trying extended m3u playlist...\n");
414 if (!(line
= play_tree_parser_get_line(p
)))
417 if(strcasecmp(line
,"#EXTM3U"))
419 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Detected extended m3u playlist format\n");
420 play_tree_parser_stop_keeping(p
);
422 while((line
= play_tree_parser_get_line(p
)) != NULL
) {
426 /* EXTM3U files contain such lines:
427 * #EXTINF:<seconds>, <title>
428 * followed by a line with the filename
429 * for now we have no place to put that
430 * so we just skip that extra-info ::atmos
433 #if 0 /* code functional */
434 if(strncasecmp(line
,"#EXTINF:",8) == 0) {
435 mp_msg(MSGT_PLAYTREE
,MSGL_INFO
,"[M3U] Duration: %dsec Title: %s\n",
436 strtol(line
+8,&line
,10), line
+2);
441 entry
= play_tree_new();
442 play_tree_add_file(entry
,line
);
446 play_tree_append_entry(last_entry
,entry
);
450 if(!list
) return NULL
;
451 entry
= play_tree_new();
452 play_tree_set_child(entry
,list
);
457 parse_smil(play_tree_parser_t
* p
) {
459 char* line
,source
[512],*pos
,*s_start
,*s_end
,*src_line
;
460 play_tree_t
*list
= NULL
, *entry
= NULL
, *last_entry
= NULL
;
462 unsigned int npkt
, ttlpkt
;
464 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Trying smil playlist...\n");
467 while((line
= play_tree_parser_get_line(p
)) != NULL
) {
469 if(line
[0] == '\0') // Ignore empties
471 if (strncasecmp(line
,"<?xml",5)==0) // smil in xml
473 if (strncasecmp(line
,"<!DOCTYPE smil",13)==0) // smil in xml
475 if (strncasecmp(line
,"<smil",5)==0 || strncasecmp(line
,"<?wpl",5)==0 ||
476 strncasecmp(line
,"(smil-document",14)==0)
477 break; // smil header found
479 return NULL
; //line not smil exit
482 if (!line
) return NULL
;
483 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Detected smil playlist format\n");
484 play_tree_parser_stop_keeping(p
);
486 if (strncasecmp(line
,"(smil-document",14)==0) {
487 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Special smil-over-realrtsp playlist header\n");
489 if (sscanf(line
, "(smil-document (ver 1.0)(npkt %u)(ttlpkt %u", &npkt
, &ttlpkt
) != 2) {
490 mp_msg(MSGT_PLAYTREE
,MSGL_WARN
,"smil-over-realrtsp: header parsing failure, assuming single packet.\n");
493 if (ttlpkt
== 0 || npkt
> ttlpkt
) {
494 mp_msg(MSGT_PLAYTREE
,MSGL_WARN
,"smil-over-realrtsp: bad packet counters (npkk = %u, ttlpkt = %u), assuming single packet.\n",
500 //Get entries from smil
509 /* If we're parsing smil over realrtsp and this is not the last packet and
510 * this is the last line in the packet (terminating with ") ) we must get
511 * the next line, strip the header, and concatenate it to the current line.
513 if (is_rmsmil
&& npkt
!= ttlpkt
&& strstr(src_line
,"\")")) {
516 line
= strdup(src_line
);
517 if(!(src_line
= play_tree_parser_get_line(p
))) {
518 mp_msg(MSGT_PLAYTREE
,MSGL_WARN
,"smil-over-realrtsp: can't get line from packet %u/%u.\n", npkt
, ttlpkt
);
522 // Skip header, packet starts after "
523 if(!(payload
= strchr(src_line
,'\"'))) {
524 mp_msg(MSGT_PLAYTREE
,MSGL_WARN
,"smil-over-realrtsp: can't find start of packet, using complete line.\n");
528 // Skip ") at the end of the last line from the current packet
529 line
[strlen(line
)-2] = 0;
530 line
= realloc(line
, strlen(line
)+strlen(payload
)+1);
531 strcat (line
, payload
);
534 line
= strdup(src_line
);
535 /* Unescape \" to " for smil-over-rtsp */
536 if (is_rmsmil
&& line
[0] != '\0') {
539 for (i
= 0; i
< strlen(line
); i
++)
540 if (line
[i
] == '\\' && line
[i
+1] == '"')
541 for (j
= i
; line
[j
]; j
++)
546 if (!entrymode
) { // all entries filled so far
547 while ((pos
=strchr(pos
, '<'))) {
548 if (strncasecmp(pos
,"<video",6)==0 || strncasecmp(pos
,"<audio",6)==0 || strncasecmp(pos
,"<media",6)==0) {
550 break; // Got a valid tag, exit '<' search loop
555 if (entrymode
) { //Entry found but not yet filled
556 pos
= strstr(pos
,"src="); // Is source present on this line
559 if (pos
[4] != '"' && pos
[4] != '\'') {
560 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Unknown delimiter %c in source line %s\n", pos
[4], line
);
564 s_end
=strchr(s_start
,pos
[4]);
566 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Error parsing this source line %s\n",line
);
569 if (s_end
-s_start
> 511) {
570 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Cannot store such a large source %s\n",line
);
573 strncpy(source
,s_start
,s_end
-s_start
);
574 source
[(s_end
-s_start
)]='\0'; // Null terminate
575 entry
= play_tree_new();
576 play_tree_add_file(entry
,source
);
577 if(!list
) //Insert new entry
580 play_tree_append_entry(last_entry
,entry
);
586 } while((src_line
= play_tree_parser_get_line(p
)) != NULL
);
591 if(!list
) return NULL
; // Nothing found
593 entry
= play_tree_new();
594 play_tree_set_child(entry
,list
);
599 embedded_playlist_parse(char *line
) {
600 int f
=DEMUXER_TYPE_PLAYLIST
;
602 play_tree_parser_t
* ptp
;
605 // Get stream opened to link
606 stream
=open_stream(line
,0,&f
);
608 mp_msg(MSGT_PLAYTREE
,MSGL_WARN
,"Can't open playlist %s\n",line
);
613 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Adding playlist %s to element entryref\n",line
);
615 ptp
= play_tree_parser_new(stream
,1);
616 entry
= play_tree_parser_get_play_tree(ptp
, 1);
617 play_tree_parser_free(ptp
);
624 parse_textplain(play_tree_parser_t
* p
) {
628 play_tree_t
*list
= NULL
, *entry
= NULL
, *last_entry
= NULL
;
630 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Trying plaintext playlist...\n");
631 play_tree_parser_stop_keeping(p
);
633 while((line
= play_tree_parser_get_line(p
)) != NULL
) {
635 if(line
[0] == '\0' || line
[0] == '#' || (line
[0] == '/' && line
[1] == '/'))
638 //Special check for embedded smil or ram reference in file
640 if (strlen(line
) > 5)
641 for(c
= line
; c
[0]; c
++ )
642 if ( ((c
[0] == '.') && //start with . and next have smil with optional ? or &
643 (tolower(c
[1]) == 's') && (tolower(c
[2])== 'm') &&
644 (tolower(c
[3]) == 'i') && (tolower(c
[4]) == 'l') &&
645 (!c
[5] || c
[5] == '?' || c
[5] == '&')) || // or
646 ((c
[0] == '.') && // start with . and next have smi or ram with optional ? or &
647 ( ((tolower(c
[1]) == 's') && (tolower(c
[2])== 'm') && (tolower(c
[3]) == 'i')) ||
648 ((tolower(c
[1]) == 'r') && (tolower(c
[2])== 'a') && (tolower(c
[3]) == 'm')) )
649 && (!c
[4] || c
[4] == '?' || c
[4] == '&')) ){
650 entry
=embedded_playlist_parse(line
);
655 if (!embedded
) { //regular file link
656 entry
= play_tree_new();
657 play_tree_add_file(entry
,line
);
664 play_tree_append_entry(last_entry
,entry
);
669 if(!list
) return NULL
;
670 entry
= play_tree_new();
671 play_tree_set_child(entry
,list
);
676 parse_playtree(stream_t
*stream
, int forced
) {
677 play_tree_parser_t
* p
;
681 assert(stream
!= NULL
);
684 p
= play_tree_parser_new(stream
,0);
688 ret
= play_tree_parser_get_play_tree(p
, forced
);
689 play_tree_parser_free(p
);
695 play_tree_add_basepath(play_tree_t
* pt
, char* bp
) {
696 int i
,bl
= strlen(bp
),fl
;
700 for(i
= pt
->child
; i
!= NULL
; i
= i
->next
)
701 play_tree_add_basepath(i
,bp
);
708 for(i
= 0 ; pt
->files
[i
] != NULL
; i
++) {
709 fl
= strlen(pt
->files
[i
]);
710 // if we find a full unix path, url:// or X:\ at the beginning,
712 if(fl
<= 0 || strstr(pt
->files
[i
],"://") || (strstr(pt
->files
[i
],":\\") == pt
->files
[i
] + 1) || (pt
->files
[i
][0] == '/') )
714 // if the path begins with \ then prepend drive letter to it.
715 if (pt
->files
[i
][0] == '\\') {
716 if (pt
->files
[i
][1] == '\\')
718 pt
->files
[i
] = (char*)realloc(pt
->files
[i
],2+fl
+1);
719 memmove(pt
->files
[i
] + 2,pt
->files
[i
],fl
+1);
720 memcpy(pt
->files
[i
],bp
,2);
723 pt
->files
[i
] = (char*)realloc(pt
->files
[i
],bl
+fl
+1);
724 memmove(pt
->files
[i
] + bl
,pt
->files
[i
],fl
+1);
725 memcpy(pt
->files
[i
],bp
,bl
);
729 // Wrapper for play_tree_add_basepath (add base path from file)
730 void play_tree_add_bpf(play_tree_t
* pt
, char* filename
)
736 file
= strdup(filename
);
739 ls
= strrchr(file
,'/');
740 if(!ls
) ls
= strrchr(file
,'\\');
743 play_tree_add_basepath(pt
,file
);
751 parse_playlist_file(char* file
) {
754 int f
=DEMUXER_TYPE_PLAYLIST
;
756 stream
= open_stream(file
,0,&f
);
759 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"Error while opening playlist file %s: %s\n",file
,strerror(errno
));
763 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Parsing playlist file %s...\n",file
);
765 ret
= parse_playtree(stream
,1);
768 play_tree_add_bpf(ret
, file
);
776 play_tree_parser_new(stream_t
* stream
,int deep
) {
777 play_tree_parser_t
* p
;
779 p
= calloc(1,sizeof(play_tree_parser_t
));
791 play_tree_parser_free(play_tree_parser_t
* p
) {
797 if(p
->buffer
) free(p
->buffer
);
798 if(p
->line
) free(p
->line
);
803 play_tree_parser_get_play_tree(play_tree_parser_t
* p
, int forced
) {
804 play_tree_t
* tree
= NULL
;
811 while(play_tree_parser_get_line(p
) != NULL
) {
812 play_tree_parser_reset(p
);
816 play_tree_parser_reset(p
);
820 play_tree_parser_reset(p
);
824 play_tree_parser_reset(p
);
826 tree
= parse_ref_ini(p
);
828 play_tree_parser_reset(p
);
830 tree
= parse_smil(p
);
832 play_tree_parser_reset(p
);
834 // Here come the others formats ( textplain must stay the last one )
837 tree
= parse_textplain(p
);
844 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Playlist successfully parsed\n");
846 mp_msg(MSGT_PLAYTREE
,((forced
==1)?MSGL_ERR
:MSGL_V
),"Error while parsing playlist\n");
849 tree
= play_tree_cleanup(tree
);
851 if(!tree
) mp_msg(MSGT_PLAYTREE
,((forced
==1)?MSGL_WARN
:MSGL_V
),"Warning: empty playlist\n");