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>
33 #include "asxparser.h"
36 #include "playtreeparser.h"
37 #include "stream/stream.h"
38 #include "libmpdemux/demuxer.h"
44 #define WHITES " \n\r\t"
52 for(i
= str
; i
[0] != '\0' && strchr(WHITES
,i
[0]) != NULL
; i
++)
55 memmove(str
,i
,strlen(i
) + 1);
56 for(i
= str
+ strlen(str
) - 1 ; strchr(WHITES
,i
[0]) != NULL
; i
--)
64 play_tree_parser_get_line(play_tree_parser_t
* p
) {
68 if(p
->buffer
== NULL
) {
69 p
->buffer
= malloc(BUF_STEP
);
70 p
->buffer_size
= BUF_STEP
;
75 if(p
->stream
->eof
&& (p
->buffer_end
== 0 || p
->iter
[0] == '\0'))
78 assert(p
->buffer_end
< p
->buffer_size
);
79 assert(!p
->buffer
[p
->buffer_end
]);
83 r
= p
->iter
- p
->buffer
;
84 p
->buffer
= realloc(p
->buffer
, p
->buffer_size
+ BUF_STEP
);
85 p
->iter
= p
->buffer
+ r
;
86 p
->buffer_size
+= BUF_STEP
;
90 if(p
->buffer_size
- p
->buffer_end
> 1 && ! p
->stream
->eof
) {
91 r
= stream_read(p
->stream
,p
->buffer
+ p
->buffer_end
,p
->buffer_size
- p
->buffer_end
- 1);
94 assert(p
->buffer_end
< p
->buffer_size
);
95 p
->buffer
[p
->buffer_end
] = '\0';
96 while(strlen(p
->buffer
+ p
->buffer_end
- r
) != r
)
97 p
->buffer
[p
->buffer_end
- r
+ strlen(p
->buffer
+ p
->buffer_end
- r
)] = '\n';
99 assert(!p
->buffer
[p
->buffer_end
]);
102 end
= strchr(p
->iter
,'\n');
105 end
= p
->buffer
+ p
->buffer_end
;
114 line_end
= (end
> p
->iter
&& *(end
-1) == '\r') ? end
-1 : end
;
115 if(line_end
- p
->iter
>= 0)
116 p
->line
= realloc(p
->line
, line_end
- p
->iter
+ 1);
119 if(line_end
- p
->iter
> 0)
120 strncpy(p
->line
,p
->iter
,line_end
- p
->iter
);
121 p
->line
[line_end
- p
->iter
] = '\0';
127 p
->buffer_end
-= end
-p
->iter
;
128 memmove(p
->buffer
,end
,p
->buffer_end
);
131 p
->buffer
[p
->buffer_end
] = '\0';
140 play_tree_parser_reset(play_tree_parser_t
* p
) {
145 play_tree_parser_stop_keeping(play_tree_parser_t
* p
) {
147 if(p
->iter
&& p
->iter
!= p
->buffer
) {
148 p
->buffer_end
-= p
->iter
-p
->buffer
;
150 memmove(p
->buffer
,p
->iter
,p
->buffer_end
);
151 p
->buffer
[p
->buffer_end
] = 0;
158 parse_asx(play_tree_parser_t
* p
) {
159 int comments
= 0,get_line
= 1;
162 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Trying asx...\n");
166 line
= play_tree_parser_get_line(p
);
175 mp_msg(MSGT_PLAYTREE
,MSGL_DBG2
,"First char isn't '<' but '%c'\n",line
[0]);
176 mp_msg(MSGT_PLAYTREE
,MSGL_DBG3
,"Buffer = [%s]\n",p
->buffer
);
178 } else if(strncmp(line
,"<!--",4) == 0) { // Comments
181 if(line
[0] != '\0' && strlen(line
) > 0)
183 } else if(strncasecmp(line
,"<ASX",4) == 0) // We got an asx element
185 else // We don't get an asx
189 c
= strchr(line
,'-');
191 if (strncmp(c
,"--!>",4) == 0) { // End of comments
194 if(line
[0] != '\0') // There is some more data on this line : keep it
198 line
= c
+1; // Jump the -
199 if(line
[0] != '\0') // Some more data
204 } else // No - on this line (or rest of line) : get next one
209 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Detected asx format\n");
211 // We have an asx : load it in memory and parse
213 while((line
= play_tree_parser_get_line(p
)) != NULL
)
216 mp_msg(MSGT_PLAYTREE
,MSGL_DBG3
,"Parsing asx file: [%s]\n",p
->buffer
);
217 return asx_parser_build_tree(p
->buffer
,p
->deep
);
221 pls_entry_get_value(char* line
) {
224 i
= strchr(line
,'=');
225 if(!i
|| i
[1] == '\0')
231 typedef struct pls_entry
{
238 pls_read_entry(char* line
,pls_entry_t
** _e
,int* _max_entry
,char** val
) {
239 int num
,max_entry
= (*_max_entry
);
240 pls_entry_t
* e
= (*_e
);
243 v
= pls_entry_get_value(line
);
245 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"No value in entry %s\n",line
);
252 mp_msg(MSGT_PLAYTREE
,MSGL_WARN
,"No entry index in entry %s\nAssuming %d\n",line
,num
);
254 if(num
> max_entry
) {
255 e
= realloc(e
, num
* sizeof(pls_entry_t
));
256 memset(&e
[max_entry
],0,(num
-max_entry
)*sizeof(pls_entry_t
));
260 (*_max_entry
) = max_entry
;
268 parse_pls(play_tree_parser_t
* p
) {
270 pls_entry_t
* entries
= NULL
;
271 int n_entries
= 0,max_entry
=0,num
;
272 play_tree_t
*list
= NULL
, *entry
= NULL
, *last_entry
= NULL
;
274 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Trying Winamp playlist...\n");
275 while((line
= play_tree_parser_get_line(p
))) {
282 if(strcasecmp(line
,"[playlist]"))
284 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Detected Winamp playlist format\n");
285 play_tree_parser_stop_keeping(p
);
286 line
= play_tree_parser_get_line(p
);
290 if(strncasecmp(line
,"NumberOfEntries",15) == 0) {
291 v
= pls_entry_get_value(line
);
294 mp_msg(MSGT_PLAYTREE
,MSGL_DBG2
,"Invalid number of entries: very funny!!!\n");
296 mp_msg(MSGT_PLAYTREE
,MSGL_DBG2
,"Playlist claims to have %d entries. Let's see.\n",n_entries
);
297 line
= play_tree_parser_get_line(p
);
302 if(line
[0] == '\0') {
303 line
= play_tree_parser_get_line(p
);
306 if(strncasecmp(line
,"File",4) == 0) {
307 num
= pls_read_entry(line
+4,&entries
,&max_entry
,&v
);
309 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"No value in entry %s\n",line
);
311 entries
[num
-1].file
= strdup(v
);
312 } else if(strncasecmp(line
,"Title",5) == 0) {
313 num
= pls_read_entry(line
+5,&entries
,&max_entry
,&v
);
315 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"No value in entry %s\n",line
);
317 entries
[num
-1].title
= strdup(v
);
318 } else if(strncasecmp(line
,"Length",6) == 0) {
319 num
= pls_read_entry(line
+6,&entries
,&max_entry
,&v
);
321 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"No value in entry %s\n",line
);
323 entries
[num
-1].length
= strdup(v
);
325 mp_msg(MSGT_PLAYTREE
,MSGL_WARN
,"Unknown entry type %s\n",line
);
326 line
= play_tree_parser_get_line(p
);
329 for(num
= 0; num
< max_entry
; num
++) {
330 if(entries
[num
].file
== NULL
)
331 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"Entry %d don't have a file !!!!\n",num
+1);
333 mp_msg(MSGT_PLAYTREE
,MSGL_DBG2
,"Adding entry %s\n",entries
[num
].file
);
334 entry
= play_tree_new();
335 play_tree_add_file(entry
,entries
[num
].file
);
336 free(entries
[num
].file
);
338 play_tree_append_entry(last_entry
,entry
);
343 if(entries
[num
].title
) {
344 // When we have info in playtree we add this info
345 free(entries
[num
].title
);
347 if(entries
[num
].length
) {
348 // When we have info in playtree we add this info
349 free(entries
[num
].length
);
355 entry
= play_tree_new();
356 play_tree_set_child(entry
,list
);
361 Reference Ini-Format: Each entry is assumed a reference
364 parse_ref_ini(play_tree_parser_t
* p
) {
366 play_tree_t
*list
= NULL
, *entry
= NULL
, *last_entry
= NULL
;
368 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Trying reference-ini playlist...\n");
369 if (!(line
= play_tree_parser_get_line(p
)))
372 if(strcasecmp(line
,"[Reference]"))
374 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Detected reference-ini playlist format\n");
375 play_tree_parser_stop_keeping(p
);
376 line
= play_tree_parser_get_line(p
);
381 if(strncasecmp(line
,"Ref",3) == 0) {
382 v
= pls_entry_get_value(line
+3);
384 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"No value in entry %s\n",line
);
387 mp_msg(MSGT_PLAYTREE
,MSGL_DBG2
,"Adding entry %s\n",v
);
388 entry
= play_tree_new();
389 play_tree_add_file(entry
,v
);
391 play_tree_append_entry(last_entry
,entry
);
397 line
= play_tree_parser_get_line(p
);
400 if(!list
) return NULL
;
401 entry
= play_tree_new();
402 play_tree_set_child(entry
,list
);
407 parse_m3u(play_tree_parser_t
* p
) {
409 play_tree_t
*list
= NULL
, *entry
= NULL
, *last_entry
= NULL
;
411 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Trying extended m3u playlist...\n");
412 if (!(line
= play_tree_parser_get_line(p
)))
415 if(strcasecmp(line
,"#EXTM3U"))
417 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Detected extended m3u playlist format\n");
418 play_tree_parser_stop_keeping(p
);
420 while((line
= play_tree_parser_get_line(p
)) != NULL
) {
424 /* EXTM3U files contain such lines:
425 * #EXTINF:<seconds>, <title>
426 * followed by a line with the filename
427 * for now we have no place to put that
428 * so we just skip that extra-info ::atmos
431 #if 0 /* code functional */
432 if(strncasecmp(line
,"#EXTINF:",8) == 0) {
433 mp_msg(MSGT_PLAYTREE
,MSGL_INFO
,"[M3U] Duration: %dsec Title: %s\n",
434 strtol(line
+8,&line
,10), line
+2);
439 entry
= play_tree_new();
440 play_tree_add_file(entry
,line
);
444 play_tree_append_entry(last_entry
,entry
);
448 if(!list
) return NULL
;
449 entry
= play_tree_new();
450 play_tree_set_child(entry
,list
);
455 parse_smil(play_tree_parser_t
* p
) {
457 char* line
,source
[512],*pos
,*s_start
,*s_end
,*src_line
;
458 play_tree_t
*list
= NULL
, *entry
= NULL
, *last_entry
= NULL
;
460 unsigned int npkt
, ttlpkt
;
462 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Trying smil playlist...\n");
465 while((line
= play_tree_parser_get_line(p
)) != NULL
) {
467 if(line
[0] == '\0') // Ignore empties
469 if (strncasecmp(line
,"<?xml",5)==0) // smil in xml
471 if (strncasecmp(line
,"<!DOCTYPE smil",13)==0) // smil in xml
473 if (strncasecmp(line
,"<smil",5)==0 || strncasecmp(line
,"<?wpl",5)==0 ||
474 strncasecmp(line
,"(smil-document",14)==0)
475 break; // smil header found
477 return NULL
; //line not smil exit
480 if (!line
) return NULL
;
481 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Detected smil playlist format\n");
482 play_tree_parser_stop_keeping(p
);
484 if (strncasecmp(line
,"(smil-document",14)==0) {
485 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Special smil-over-realrtsp playlist header\n");
487 if (sscanf(line
, "(smil-document (ver 1.0)(npkt %u)(ttlpkt %u", &npkt
, &ttlpkt
) != 2) {
488 mp_msg(MSGT_PLAYTREE
,MSGL_WARN
,"smil-over-realrtsp: header parsing failure, assuming single packet.\n");
491 if (ttlpkt
== 0 || npkt
> ttlpkt
) {
492 mp_msg(MSGT_PLAYTREE
,MSGL_WARN
,"smil-over-realrtsp: bad packet counters (npkk = %u, ttlpkt = %u), assuming single packet.\n",
498 //Get entries from smil
507 /* If we're parsing smil over realrtsp and this is not the last packet and
508 * this is the last line in the packet (terminating with ") ) we must get
509 * the next line, strip the header, and concatenate it to the current line.
511 if (is_rmsmil
&& npkt
!= ttlpkt
&& strstr(src_line
,"\")")) {
514 line
= strdup(src_line
);
515 if(!(src_line
= play_tree_parser_get_line(p
))) {
516 mp_msg(MSGT_PLAYTREE
,MSGL_WARN
,"smil-over-realrtsp: can't get line from packet %u/%u.\n", npkt
, ttlpkt
);
520 // Skip header, packet starts after "
521 if(!(payload
= strchr(src_line
,'\"'))) {
522 mp_msg(MSGT_PLAYTREE
,MSGL_WARN
,"smil-over-realrtsp: can't find start of packet, using complete line.\n");
526 // Skip ") at the end of the last line from the current packet
527 line
[strlen(line
)-2] = 0;
528 line
= realloc(line
, strlen(line
)+strlen(payload
)+1);
529 strcat (line
, payload
);
532 line
= strdup(src_line
);
533 /* Unescape \" to " for smil-over-rtsp */
534 if (is_rmsmil
&& line
[0] != '\0') {
537 for (i
= 0; i
< strlen(line
); i
++)
538 if (line
[i
] == '\\' && line
[i
+1] == '"')
539 for (j
= i
; line
[j
]; j
++)
544 if (!entrymode
) { // all entries filled so far
545 while ((pos
=strchr(pos
, '<'))) {
546 if (strncasecmp(pos
,"<video",6)==0 || strncasecmp(pos
,"<audio",6)==0 || strncasecmp(pos
,"<media",6)==0) {
548 break; // Got a valid tag, exit '<' search loop
553 if (entrymode
) { //Entry found but not yet filled
554 pos
= strstr(pos
,"src="); // Is source present on this line
557 if (pos
[4] != '"' && pos
[4] != '\'') {
558 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Unknown delimiter %c in source line %s\n", pos
[4], line
);
562 s_end
=strchr(s_start
,pos
[4]);
564 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Error parsing this source line %s\n",line
);
567 if (s_end
-s_start
> 511) {
568 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Cannot store such a large source %s\n",line
);
571 strncpy(source
,s_start
,s_end
-s_start
);
572 source
[(s_end
-s_start
)]='\0'; // Null terminate
573 entry
= play_tree_new();
574 play_tree_add_file(entry
,source
);
575 if(!list
) //Insert new entry
578 play_tree_append_entry(last_entry
,entry
);
584 } while((src_line
= play_tree_parser_get_line(p
)) != NULL
);
589 if(!list
) return NULL
; // Nothing found
591 entry
= play_tree_new();
592 play_tree_set_child(entry
,list
);
597 embedded_playlist_parse(char *line
) {
598 int f
=DEMUXER_TYPE_PLAYLIST
;
600 play_tree_parser_t
* ptp
;
603 // Get stream opened to link
604 stream
=open_stream(line
,0,&f
);
606 mp_msg(MSGT_PLAYTREE
,MSGL_WARN
,"Can't open playlist %s\n",line
);
611 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Adding playlist %s to element entryref\n",line
);
613 ptp
= play_tree_parser_new(stream
,1);
614 entry
= play_tree_parser_get_play_tree(ptp
, 1);
615 play_tree_parser_free(ptp
);
622 parse_textplain(play_tree_parser_t
* p
) {
626 play_tree_t
*list
= NULL
, *entry
= NULL
, *last_entry
= NULL
;
628 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Trying plaintext playlist...\n");
629 play_tree_parser_stop_keeping(p
);
631 while((line
= play_tree_parser_get_line(p
)) != NULL
) {
633 if(line
[0] == '\0' || line
[0] == '#' || (line
[0] == '/' && line
[1] == '/'))
636 //Special check for embedded smil or ram reference in file
638 if (strlen(line
) > 5)
639 for(c
= line
; c
[0]; c
++ )
640 if ( ((c
[0] == '.') && //start with . and next have smil with optional ? or &
641 (tolower(c
[1]) == 's') && (tolower(c
[2])== 'm') &&
642 (tolower(c
[3]) == 'i') && (tolower(c
[4]) == 'l') &&
643 (!c
[5] || c
[5] == '?' || c
[5] == '&')) || // or
644 ((c
[0] == '.') && // start with . and next have smi or ram with optional ? or &
645 ( ((tolower(c
[1]) == 's') && (tolower(c
[2])== 'm') && (tolower(c
[3]) == 'i')) ||
646 ((tolower(c
[1]) == 'r') && (tolower(c
[2])== 'a') && (tolower(c
[3]) == 'm')) )
647 && (!c
[4] || c
[4] == '?' || c
[4] == '&')) ){
648 entry
=embedded_playlist_parse(line
);
653 if (!embedded
) { //regular file link
654 entry
= play_tree_new();
655 play_tree_add_file(entry
,line
);
662 play_tree_append_entry(last_entry
,entry
);
667 if(!list
) return NULL
;
668 entry
= play_tree_new();
669 play_tree_set_child(entry
,list
);
674 parse_playtree(stream_t
*stream
, int forced
) {
675 play_tree_parser_t
* p
;
679 assert(stream
!= NULL
);
682 p
= play_tree_parser_new(stream
,0);
686 ret
= play_tree_parser_get_play_tree(p
, forced
);
687 play_tree_parser_free(p
);
693 play_tree_add_basepath(play_tree_t
* pt
, char* bp
) {
694 int i
,bl
= strlen(bp
),fl
;
698 for(i
= pt
->child
; i
!= NULL
; i
= i
->next
)
699 play_tree_add_basepath(i
,bp
);
706 for(i
= 0 ; pt
->files
[i
] != NULL
; i
++) {
707 fl
= strlen(pt
->files
[i
]);
708 // if we find a full unix path, url:// or X:\ at the beginning,
710 if(fl
<= 0 || strstr(pt
->files
[i
],"://") || (strstr(pt
->files
[i
],":\\") == pt
->files
[i
] + 1) || (pt
->files
[i
][0] == '/') )
712 // if the path begins with \ then prepend drive letter to it.
713 if (pt
->files
[i
][0] == '\\') {
714 if (pt
->files
[i
][1] == '\\')
716 pt
->files
[i
] = realloc(pt
->files
[i
], 2 + fl
+ 1);
717 memmove(pt
->files
[i
] + 2,pt
->files
[i
],fl
+1);
718 memcpy(pt
->files
[i
],bp
,2);
721 pt
->files
[i
] = realloc(pt
->files
[i
], bl
+ fl
+ 1);
722 memmove(pt
->files
[i
] + bl
,pt
->files
[i
],fl
+1);
723 memcpy(pt
->files
[i
],bp
,bl
);
727 // Wrapper for play_tree_add_basepath (add base path from file)
728 void play_tree_add_bpf(play_tree_t
* pt
, char* filename
)
734 file
= strdup(filename
);
737 ls
= strrchr(file
,'/');
738 if(!ls
) ls
= strrchr(file
,'\\');
741 play_tree_add_basepath(pt
,file
);
749 parse_playlist_file(char* file
) {
752 int f
=DEMUXER_TYPE_PLAYLIST
;
754 stream
= open_stream(file
,0,&f
);
757 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"Error while opening playlist file %s: %s\n",file
,strerror(errno
));
761 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Parsing playlist file %s...\n",file
);
763 ret
= parse_playtree(stream
,1);
766 play_tree_add_bpf(ret
, file
);
774 play_tree_parser_new(stream_t
* stream
,int deep
) {
775 play_tree_parser_t
* p
;
777 p
= calloc(1,sizeof(play_tree_parser_t
));
789 play_tree_parser_free(play_tree_parser_t
* p
) {
795 if(p
->buffer
) free(p
->buffer
);
796 if(p
->line
) free(p
->line
);
801 play_tree_parser_get_play_tree(play_tree_parser_t
* p
, int forced
) {
802 play_tree_t
* tree
= NULL
;
809 while(play_tree_parser_get_line(p
) != NULL
) {
810 play_tree_parser_reset(p
);
814 play_tree_parser_reset(p
);
818 play_tree_parser_reset(p
);
822 play_tree_parser_reset(p
);
824 tree
= parse_ref_ini(p
);
826 play_tree_parser_reset(p
);
828 tree
= parse_smil(p
);
830 play_tree_parser_reset(p
);
832 // Here come the others formats ( textplain must stay the last one )
835 tree
= parse_textplain(p
);
842 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Playlist successfully parsed\n");
844 mp_msg(MSGT_PLAYTREE
,((forced
==1)?MSGL_ERR
:MSGL_V
),"Error while parsing playlist\n");
847 tree
= play_tree_cleanup(tree
);
849 if(!tree
) mp_msg(MSGT_PLAYTREE
,((forced
==1)?MSGL_WARN
:MSGL_V
),"Warning: empty playlist\n");