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>
34 #include "asxparser.h"
37 #include "playtreeparser.h"
38 #include "stream/stream.h"
39 #include "libmpdemux/demuxer.h"
45 #define WHITES " \n\r\t"
53 for(i
= str
; i
[0] != '\0' && strchr(WHITES
,i
[0]) != NULL
; i
++)
56 memmove(str
,i
,strlen(i
) + 1);
57 for(i
= str
+ strlen(str
) - 1 ; strchr(WHITES
,i
[0]) != NULL
; i
--)
65 play_tree_parser_get_line(play_tree_parser_t
* p
) {
69 if(p
->buffer
== NULL
) {
70 p
->buffer
= malloc(BUF_STEP
);
71 p
->buffer_size
= BUF_STEP
;
76 if(p
->stream
->eof
&& (p
->buffer_end
== 0 || p
->iter
[0] == '\0'))
79 assert(p
->buffer_end
< p
->buffer_size
);
80 assert(!p
->buffer
[p
->buffer_end
]);
85 r
= p
->iter
- p
->buffer
;
86 end
= p
->buffer
+ p
->buffer_end
;
87 if (p
->buffer_size
> INT_MAX
- BUF_STEP
)
89 tmp
= realloc(p
->buffer
, p
->buffer_size
+ BUF_STEP
);
93 p
->iter
= p
->buffer
+ r
;
94 p
->buffer_size
+= BUF_STEP
;
98 if(p
->buffer_size
- p
->buffer_end
> 1 && ! p
->stream
->eof
) {
99 r
= stream_read(p
->stream
,p
->buffer
+ p
->buffer_end
,p
->buffer_size
- p
->buffer_end
- 1);
102 assert(p
->buffer_end
< p
->buffer_size
);
103 p
->buffer
[p
->buffer_end
] = '\0';
104 while(strlen(p
->buffer
+ p
->buffer_end
- r
) != r
)
105 p
->buffer
[p
->buffer_end
- r
+ strlen(p
->buffer
+ p
->buffer_end
- r
)] = '\n';
107 assert(!p
->buffer
[p
->buffer_end
]);
110 end
= strchr(p
->iter
,'\n');
113 end
= p
->buffer
+ p
->buffer_end
;
122 line_end
= (end
> p
->iter
&& *(end
-1) == '\r') ? end
-1 : end
;
123 if(line_end
- p
->iter
>= 0)
124 p
->line
= realloc(p
->line
, line_end
- p
->iter
+ 1);
127 if(line_end
- p
->iter
> 0)
128 strncpy(p
->line
,p
->iter
,line_end
- p
->iter
);
129 p
->line
[line_end
- p
->iter
] = '\0';
135 p
->buffer_end
-= end
-p
->iter
;
136 memmove(p
->buffer
,end
,p
->buffer_end
);
139 p
->buffer
[p
->buffer_end
] = '\0';
148 play_tree_parser_reset(play_tree_parser_t
* p
) {
153 play_tree_parser_stop_keeping(play_tree_parser_t
* p
) {
155 if(p
->iter
&& p
->iter
!= p
->buffer
) {
156 p
->buffer_end
-= p
->iter
-p
->buffer
;
158 memmove(p
->buffer
,p
->iter
,p
->buffer_end
);
159 p
->buffer
[p
->buffer_end
] = 0;
166 parse_asx(play_tree_parser_t
* p
) {
167 int comments
= 0,get_line
= 1;
170 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Trying asx...\n");
174 line
= play_tree_parser_get_line(p
);
183 mp_msg(MSGT_PLAYTREE
,MSGL_DBG2
,"First char isn't '<' but '%c'\n",line
[0]);
184 mp_msg(MSGT_PLAYTREE
,MSGL_DBG3
,"Buffer = [%s]\n",p
->buffer
);
186 } else if(strncmp(line
,"<!--",4) == 0) { // Comments
189 if(line
[0] != '\0' && strlen(line
) > 0)
191 } else if(strncasecmp(line
,"<ASX",4) == 0) // We got an asx element
193 else // We don't get an asx
197 c
= strchr(line
,'-');
199 if (strncmp(c
,"--!>",4) == 0) { // End of comments
202 if(line
[0] != '\0') // There is some more data on this line : keep it
206 line
= c
+1; // Jump the -
207 if(line
[0] != '\0') // Some more data
212 } else // No - on this line (or rest of line) : get next one
217 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Detected asx format\n");
219 // We have an asx : load it in memory and parse
221 while((line
= play_tree_parser_get_line(p
)) != NULL
)
224 mp_msg(MSGT_PLAYTREE
,MSGL_DBG3
,"Parsing asx file: [%s]\n",p
->buffer
);
225 return asx_parser_build_tree(p
->mconfig
, p
->buffer
,p
->deep
);
229 pls_entry_get_value(char* line
) {
232 i
= strchr(line
,'=');
233 if(!i
|| i
[1] == '\0')
239 typedef struct pls_entry
{
246 pls_read_entry(char* line
,pls_entry_t
** _e
,int* _max_entry
,char** val
) {
247 int num
,max_entry
= (*_max_entry
);
248 pls_entry_t
* e
= (*_e
);
249 int limit
= INT_MAX
/ sizeof(*e
);
252 v
= pls_entry_get_value(line
);
254 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"No value in entry %s\n",line
);
259 if(num
<= 0 || num
> limit
) {
260 if (max_entry
>= limit
) {
261 mp_msg(MSGT_PLAYTREE
, MSGL_WARN
, "Too many index entries\n");
265 mp_msg(MSGT_PLAYTREE
,MSGL_WARN
,"No or invalid entry index in entry %s\nAssuming %d\n",line
,num
);
267 if(num
> max_entry
) {
268 e
= realloc(e
, num
* sizeof(pls_entry_t
));
271 memset(&e
[max_entry
],0,(num
-max_entry
)*sizeof(pls_entry_t
));
275 (*_max_entry
) = max_entry
;
283 parse_pls(play_tree_parser_t
* p
) {
285 pls_entry_t
* entries
= NULL
;
286 int n_entries
= 0,max_entry
=0,num
;
287 play_tree_t
*list
= NULL
, *entry
= NULL
, *last_entry
= NULL
;
289 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Trying Winamp playlist...\n");
290 while((line
= play_tree_parser_get_line(p
))) {
297 if(strcasecmp(line
,"[playlist]"))
299 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Detected Winamp playlist format\n");
300 play_tree_parser_stop_keeping(p
);
301 line
= play_tree_parser_get_line(p
);
305 if(strncasecmp(line
,"NumberOfEntries",15) == 0) {
306 v
= pls_entry_get_value(line
);
309 mp_msg(MSGT_PLAYTREE
,MSGL_DBG2
,"Invalid number of entries: very funny!!!\n");
311 mp_msg(MSGT_PLAYTREE
,MSGL_DBG2
,"Playlist claims to have %d entries. Let's see.\n",n_entries
);
312 line
= play_tree_parser_get_line(p
);
317 if(line
[0] == '\0') {
318 line
= play_tree_parser_get_line(p
);
321 if(strncasecmp(line
,"File",4) == 0) {
322 num
= pls_read_entry(line
+4,&entries
,&max_entry
,&v
);
324 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"No value in entry %s\n",line
);
326 entries
[num
-1].file
= strdup(v
);
327 } else if(strncasecmp(line
,"Title",5) == 0) {
328 num
= pls_read_entry(line
+5,&entries
,&max_entry
,&v
);
330 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"No value in entry %s\n",line
);
332 entries
[num
-1].title
= strdup(v
);
333 } else if(strncasecmp(line
,"Length",6) == 0) {
334 num
= pls_read_entry(line
+6,&entries
,&max_entry
,&v
);
336 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"No value in entry %s\n",line
);
338 entries
[num
-1].length
= strdup(v
);
340 mp_msg(MSGT_PLAYTREE
,MSGL_WARN
,"Unknown entry type %s\n",line
);
341 line
= play_tree_parser_get_line(p
);
344 for(num
= 0; num
< max_entry
; num
++) {
345 if(entries
[num
].file
== NULL
)
346 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"Entry %d don't have a file !!!!\n",num
+1);
348 mp_msg(MSGT_PLAYTREE
,MSGL_DBG2
,"Adding entry %s\n",entries
[num
].file
);
349 entry
= play_tree_new();
350 play_tree_add_file(entry
,entries
[num
].file
);
351 free(entries
[num
].file
);
353 play_tree_append_entry(last_entry
,entry
);
358 if(entries
[num
].title
) {
359 // When we have info in playtree we add this info
360 free(entries
[num
].title
);
362 if(entries
[num
].length
) {
363 // When we have info in playtree we add this info
364 free(entries
[num
].length
);
370 entry
= play_tree_new();
371 play_tree_set_child(entry
,list
);
376 Reference Ini-Format: Each entry is assumed a reference
379 parse_ref_ini(play_tree_parser_t
* p
) {
381 play_tree_t
*list
= NULL
, *entry
= NULL
, *last_entry
= NULL
;
383 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Trying reference-ini playlist...\n");
384 if (!(line
= play_tree_parser_get_line(p
)))
387 if(strcasecmp(line
,"[Reference]"))
389 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Detected reference-ini playlist format\n");
390 play_tree_parser_stop_keeping(p
);
391 line
= play_tree_parser_get_line(p
);
396 if(strncasecmp(line
,"Ref",3) == 0) {
397 v
= pls_entry_get_value(line
+3);
399 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"No value in entry %s\n",line
);
402 mp_msg(MSGT_PLAYTREE
,MSGL_DBG2
,"Adding entry %s\n",v
);
403 entry
= play_tree_new();
404 play_tree_add_file(entry
,v
);
406 play_tree_append_entry(last_entry
,entry
);
412 line
= play_tree_parser_get_line(p
);
415 if(!list
) return NULL
;
416 entry
= play_tree_new();
417 play_tree_set_child(entry
,list
);
422 parse_m3u(play_tree_parser_t
* p
) {
424 play_tree_t
*list
= NULL
, *entry
= NULL
, *last_entry
= NULL
;
426 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Trying extended m3u playlist...\n");
427 if (!(line
= play_tree_parser_get_line(p
)))
430 if(strcasecmp(line
,"#EXTM3U"))
432 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Detected extended m3u playlist format\n");
433 play_tree_parser_stop_keeping(p
);
435 while((line
= play_tree_parser_get_line(p
)) != NULL
) {
439 /* EXTM3U files contain such lines:
440 * #EXTINF:<seconds>, <title>
441 * followed by a line with the filename
442 * for now we have no place to put that
443 * so we just skip that extra-info ::atmos
446 #if 0 /* code functional */
447 if(strncasecmp(line
,"#EXTINF:",8) == 0) {
448 mp_msg(MSGT_PLAYTREE
,MSGL_INFO
,"[M3U] Duration: %dsec Title: %s\n",
449 strtol(line
+8,&line
,10), line
+2);
454 entry
= play_tree_new();
455 play_tree_add_file(entry
,line
);
459 play_tree_append_entry(last_entry
,entry
);
463 if(!list
) return NULL
;
464 entry
= play_tree_new();
465 play_tree_set_child(entry
,list
);
470 parse_smil(play_tree_parser_t
* p
) {
472 char* line
,source
[512],*pos
,*s_start
,*s_end
,*src_line
;
473 play_tree_t
*list
= NULL
, *entry
= NULL
, *last_entry
= NULL
;
475 unsigned int npkt
, ttlpkt
;
477 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Trying smil playlist...\n");
480 while((line
= play_tree_parser_get_line(p
)) != NULL
) {
482 if(line
[0] == '\0') // Ignore empties
484 if (strncasecmp(line
,"<?xml",5)==0) // smil in xml
486 if (strncasecmp(line
,"<!DOCTYPE smil",13)==0) // smil in xml
488 if (strncasecmp(line
,"<smil",5)==0 || strncasecmp(line
,"<?wpl",5)==0 ||
489 strncasecmp(line
,"(smil-document",14)==0)
490 break; // smil header found
492 return NULL
; //line not smil exit
495 if (!line
) return NULL
;
496 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Detected smil playlist format\n");
497 play_tree_parser_stop_keeping(p
);
499 if (strncasecmp(line
,"(smil-document",14)==0) {
500 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Special smil-over-realrtsp playlist header\n");
502 if (sscanf(line
, "(smil-document (ver 1.0)(npkt %u)(ttlpkt %u", &npkt
, &ttlpkt
) != 2) {
503 mp_msg(MSGT_PLAYTREE
,MSGL_WARN
,"smil-over-realrtsp: header parsing failure, assuming single packet.\n");
506 if (ttlpkt
== 0 || npkt
> ttlpkt
) {
507 mp_msg(MSGT_PLAYTREE
,MSGL_WARN
,"smil-over-realrtsp: bad packet counters (npkk = %u, ttlpkt = %u), assuming single packet.\n",
513 //Get entries from smil
522 /* If we're parsing smil over realrtsp and this is not the last packet and
523 * this is the last line in the packet (terminating with ") ) we must get
524 * the next line, strip the header, and concatenate it to the current line.
526 if (is_rmsmil
&& npkt
!= ttlpkt
&& strstr(src_line
,"\")")) {
529 line
= strdup(src_line
);
530 if(!(src_line
= play_tree_parser_get_line(p
))) {
531 mp_msg(MSGT_PLAYTREE
,MSGL_WARN
,"smil-over-realrtsp: can't get line from packet %u/%u.\n", npkt
, ttlpkt
);
535 // Skip header, packet starts after "
536 if(!(payload
= strchr(src_line
,'\"'))) {
537 mp_msg(MSGT_PLAYTREE
,MSGL_WARN
,"smil-over-realrtsp: can't find start of packet, using complete line.\n");
541 // Skip ") at the end of the last line from the current packet
542 line
[strlen(line
)-2] = 0;
543 line
= realloc(line
, strlen(line
)+strlen(payload
)+1);
544 strcat (line
, payload
);
547 line
= strdup(src_line
);
548 /* Unescape \" to " for smil-over-rtsp */
549 if (is_rmsmil
&& line
[0] != '\0') {
552 for (i
= 0; i
< strlen(line
); i
++)
553 if (line
[i
] == '\\' && line
[i
+1] == '"')
554 for (j
= i
; line
[j
]; j
++)
559 if (!entrymode
) { // all entries filled so far
560 while ((pos
=strchr(pos
, '<'))) {
561 if (strncasecmp(pos
,"<video",6)==0 || strncasecmp(pos
,"<audio",6)==0 || strncasecmp(pos
,"<media",6)==0) {
563 break; // Got a valid tag, exit '<' search loop
568 if (entrymode
) { //Entry found but not yet filled
569 pos
= strstr(pos
,"src="); // Is source present on this line
572 if (pos
[4] != '"' && pos
[4] != '\'') {
573 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Unknown delimiter %c in source line %s\n", pos
[4], line
);
577 s_end
=strchr(s_start
,pos
[4]);
579 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Error parsing this source line %s\n",line
);
582 if (s_end
-s_start
> 511) {
583 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Cannot store such a large source %s\n",line
);
586 strncpy(source
,s_start
,s_end
-s_start
);
587 source
[(s_end
-s_start
)]='\0'; // Null terminate
588 entry
= play_tree_new();
589 play_tree_add_file(entry
,source
);
590 if(!list
) //Insert new entry
593 play_tree_append_entry(last_entry
,entry
);
599 } while((src_line
= play_tree_parser_get_line(p
)) != NULL
);
604 if(!list
) return NULL
; // Nothing found
606 entry
= play_tree_new();
607 play_tree_set_child(entry
,list
);
612 embedded_playlist_parse(struct m_config
*mconfig
, char *line
) {
613 int f
=DEMUXER_TYPE_PLAYLIST
;
615 play_tree_parser_t
* ptp
;
618 // Get stream opened to link
619 stream
=open_stream(line
,0,&f
);
621 mp_msg(MSGT_PLAYTREE
,MSGL_WARN
,"Can't open playlist %s\n",line
);
626 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Adding playlist %s to element entryref\n",line
);
628 ptp
= play_tree_parser_new(stream
, mconfig
, 1);
629 entry
= play_tree_parser_get_play_tree(ptp
, 1);
630 play_tree_parser_free(ptp
);
637 parse_textplain(play_tree_parser_t
* p
) {
641 play_tree_t
*list
= NULL
, *entry
= NULL
, *last_entry
= NULL
;
643 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Trying plaintext playlist...\n");
644 play_tree_parser_stop_keeping(p
);
646 while((line
= play_tree_parser_get_line(p
)) != NULL
) {
648 if(line
[0] == '\0' || line
[0] == '#' || (line
[0] == '/' && line
[1] == '/'))
651 //Special check for embedded smil or ram reference in file
653 if (strlen(line
) > 5)
654 for(c
= line
; c
[0]; c
++ )
655 if ( ((c
[0] == '.') && //start with . and next have smil with optional ? or &
656 (tolower(c
[1]) == 's') && (tolower(c
[2])== 'm') &&
657 (tolower(c
[3]) == 'i') && (tolower(c
[4]) == 'l') &&
658 (!c
[5] || c
[5] == '?' || c
[5] == '&')) || // or
659 ((c
[0] == '.') && // start with . and next have smi or ram with optional ? or &
660 ( ((tolower(c
[1]) == 's') && (tolower(c
[2])== 'm') && (tolower(c
[3]) == 'i')) ||
661 ((tolower(c
[1]) == 'r') && (tolower(c
[2])== 'a') && (tolower(c
[3]) == 'm')) )
662 && (!c
[4] || c
[4] == '?' || c
[4] == '&')) ){
663 entry
=embedded_playlist_parse(p
->mconfig
, line
);
668 if (!embedded
) { //regular file link
669 entry
= play_tree_new();
670 play_tree_add_file(entry
,line
);
677 play_tree_append_entry(last_entry
,entry
);
682 if(!list
) return NULL
;
683 entry
= play_tree_new();
684 play_tree_set_child(entry
,list
);
689 parse_playtree(stream_t
*stream
, struct m_config
*mconfig
, int forced
) {
690 play_tree_parser_t
* p
;
694 assert(stream
!= NULL
);
697 p
= play_tree_parser_new(stream
, mconfig
, 0);
701 ret
= play_tree_parser_get_play_tree(p
, forced
);
702 play_tree_parser_free(p
);
708 play_tree_add_basepath(play_tree_t
* pt
, char* bp
) {
709 int i
,bl
= strlen(bp
),fl
;
713 for(i
= pt
->child
; i
!= NULL
; i
= i
->next
)
714 play_tree_add_basepath(i
,bp
);
721 for(i
= 0 ; pt
->files
[i
] != NULL
; i
++) {
722 fl
= strlen(pt
->files
[i
]);
723 // if we find a full unix path, url:// or X:\ at the beginning,
725 if(fl
<= 0 || strstr(pt
->files
[i
],"://") || (strstr(pt
->files
[i
],":\\") == pt
->files
[i
] + 1) || (pt
->files
[i
][0] == '/') )
727 // if the path begins with \ then prepend drive letter to it.
728 if (pt
->files
[i
][0] == '\\') {
729 if (pt
->files
[i
][1] == '\\')
731 pt
->files
[i
] = realloc(pt
->files
[i
], 2 + fl
+ 1);
732 memmove(pt
->files
[i
] + 2,pt
->files
[i
],fl
+1);
733 memcpy(pt
->files
[i
],bp
,2);
736 pt
->files
[i
] = realloc(pt
->files
[i
], bl
+ fl
+ 1);
737 memmove(pt
->files
[i
] + bl
,pt
->files
[i
],fl
+1);
738 memcpy(pt
->files
[i
],bp
,bl
);
742 // Wrapper for play_tree_add_basepath (add base path from file)
743 void play_tree_add_bpf(play_tree_t
* pt
, char* filename
)
749 file
= strdup(filename
);
752 ls
= strrchr(file
,'/');
753 if(!ls
) ls
= strrchr(file
,'\\');
756 play_tree_add_basepath(pt
,file
);
764 parse_playlist_file(struct m_config
*mconfig
, char* file
) {
767 int f
=DEMUXER_TYPE_PLAYLIST
;
769 stream
= open_stream(file
,0,&f
);
772 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"Error while opening playlist file %s: %s\n",file
,strerror(errno
));
776 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Parsing playlist file %s...\n",file
);
778 ret
= parse_playtree(stream
, mconfig
, 1);
781 play_tree_add_bpf(ret
, file
);
789 play_tree_parser_new(stream_t
* stream
, struct m_config
*mconfig
, int deep
) {
790 play_tree_parser_t
* p
;
792 p
= calloc(1,sizeof(play_tree_parser_t
));
796 p
->mconfig
= mconfig
;
805 play_tree_parser_free(play_tree_parser_t
* p
) {
811 if(p
->buffer
) free(p
->buffer
);
812 if(p
->line
) free(p
->line
);
817 play_tree_parser_get_play_tree(play_tree_parser_t
* p
, int forced
) {
818 play_tree_t
* tree
= NULL
;
825 while(play_tree_parser_get_line(p
) != NULL
) {
826 play_tree_parser_reset(p
);
830 play_tree_parser_reset(p
);
834 play_tree_parser_reset(p
);
838 play_tree_parser_reset(p
);
840 tree
= parse_ref_ini(p
);
842 play_tree_parser_reset(p
);
844 tree
= parse_smil(p
);
846 play_tree_parser_reset(p
);
848 // Here come the others formats ( textplain must stay the last one )
851 tree
= parse_textplain(p
);
858 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Playlist successfully parsed\n");
860 mp_msg(MSGT_PLAYTREE
,((forced
==1)?MSGL_ERR
:MSGL_V
),"Error while parsing playlist\n");
863 tree
= play_tree_cleanup(tree
);
865 if(!tree
) mp_msg(MSGT_PLAYTREE
,((forced
==1)?MSGL_WARN
:MSGL_V
),"Warning: empty playlist\n");