3 /// \ingroup PlaytreeParser
13 #include <sys/types.h>
20 #include "playtreeparser.h"
21 #include "stream/stream.h"
22 #include "libmpdemux/demuxer.h"
27 asx_parser_build_tree(char* buffer
, int ref
);
31 #define WHITES " \n\r\t"
39 for(i
= str
; i
[0] != '\0' && strchr(WHITES
,i
[0]) != NULL
; i
++)
42 memmove(str
,i
,strlen(i
) + 1);
43 for(i
= str
+ strlen(str
) - 1 ; strchr(WHITES
,i
[0]) != NULL
; i
--)
51 play_tree_parser_get_line(play_tree_parser_t
* p
) {
55 if(p
->buffer
== NULL
) {
56 p
->buffer
= malloc(BUF_STEP
);
57 p
->buffer_size
= BUF_STEP
;
61 if(p
->stream
->eof
&& (p
->buffer_end
== 0 || p
->iter
[0] == '\0'))
67 r
= p
->iter
- p
->buffer
;
68 p
->buffer
= (char*)realloc(p
->buffer
,p
->buffer_size
+BUF_STEP
);
69 p
->iter
= p
->buffer
+ r
;
70 p
->buffer_size
+= BUF_STEP
;
74 if(p
->buffer_size
- p
->buffer_end
> 1 && ! p
->stream
->eof
) {
75 r
= stream_read(p
->stream
,p
->buffer
+ p
->buffer_end
,p
->buffer_size
- p
->buffer_end
- 1);
78 p
->buffer
[p
->buffer_end
] = '\0';
79 while(strlen(p
->buffer
+ p
->buffer_end
- r
) != r
)
80 p
->buffer
[p
->buffer_end
- r
+ strlen(p
->buffer
+ p
->buffer_end
- r
)] = '\n';
84 end
= strchr(p
->iter
,'\n');
87 end
= p
->buffer
+ p
->buffer_end
;
96 line_end
= ((*(end
-1)) == '\r') ? end
-1 : end
;
97 if(line_end
- p
->iter
>= 0)
98 p
->line
= (char*)realloc(p
->line
,line_end
- p
->iter
+1);
101 if(line_end
- p
->iter
> 0)
102 strncpy(p
->line
,p
->iter
,line_end
- p
->iter
);
103 p
->line
[line_end
- p
->iter
] = '\0';
109 p
->buffer_end
-= end
-p
->iter
;
110 memmove(p
->buffer
,end
,p
->buffer_end
);
111 p
->buffer
[p
->buffer_end
] = '\0';
122 play_tree_parser_reset(play_tree_parser_t
* p
) {
127 play_tree_parser_stop_keeping(play_tree_parser_t
* p
) {
129 if(p
->iter
&& p
->iter
!= p
->buffer
) {
130 p
->buffer_end
-= p
->iter
-p
->buffer
;
132 memmove(p
->buffer
,p
->iter
,p
->buffer_end
);
139 parse_asx(play_tree_parser_t
* p
) {
140 int comments
= 0,get_line
= 1;
143 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Trying asx...\n");
147 line
= play_tree_parser_get_line(p
);
156 mp_msg(MSGT_PLAYTREE
,MSGL_DBG2
,"First char isn't '<' but '%c'\n",line
[0]);
157 mp_msg(MSGT_PLAYTREE
,MSGL_DBG3
,"Buffer = [%s]\n",p
->buffer
);
159 } else if(strncmp(line
,"<!--",4) == 0) { // Comments
162 if(line
[0] != '\0' && strlen(line
) > 0)
164 } else if(strncasecmp(line
,"<ASX",4) == 0) // We got an asx element
166 else // We don't get an asx
170 c
= strchr(line
,'-');
172 if (strncmp(c
,"--!>",4) == 0) { // End of comments
175 if(line
[0] != '\0') // There is some more data on this line : keep it
179 line
= c
+1; // Jump the -
180 if(line
[0] != '\0') // Some more data
185 } else // No - on this line (or rest of line) : get next one
190 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Detected asx format\n");
192 // We have an asx : load it in memory and parse
194 while((line
= play_tree_parser_get_line(p
)) != NULL
)
197 mp_msg(MSGT_PLAYTREE
,MSGL_DBG3
,"Parsing asx file: [%s]\n",p
->buffer
);
198 return asx_parser_build_tree(p
->buffer
,p
->deep
);
202 pls_entry_get_value(char* line
) {
205 i
= strchr(line
,'=');
206 if(!i
|| i
[1] == '\0')
212 typedef struct pls_entry
{
219 pls_read_entry(char* line
,pls_entry_t
** _e
,int* _max_entry
,char** val
) {
220 int num
,max_entry
= (*_max_entry
);
221 pls_entry_t
* e
= (*_e
);
224 v
= pls_entry_get_value(line
);
226 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"No value in entry %s\n",line
);
233 mp_msg(MSGT_PLAYTREE
,MSGL_WARN
,"No entry index in entry %s\nAssuming %d\n",line
,num
);
235 if(num
> max_entry
) {
236 e
= (pls_entry_t
*)realloc(e
,num
*sizeof(pls_entry_t
));
237 memset(&e
[max_entry
],0,(num
-max_entry
)*sizeof(pls_entry_t
));
241 (*_max_entry
) = max_entry
;
249 parse_pls(play_tree_parser_t
* p
) {
251 pls_entry_t
* entries
= NULL
;
252 int n_entries
= 0,max_entry
=0,num
;
253 play_tree_t
*list
= NULL
, *entry
= NULL
, *last_entry
= NULL
;
255 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Trying Winamp playlist...\n");
256 while((line
= play_tree_parser_get_line(p
))) {
263 if(strcasecmp(line
,"[playlist]"))
265 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Detected Winamp playlist format\n");
266 play_tree_parser_stop_keeping(p
);
267 line
= play_tree_parser_get_line(p
);
271 if(strncasecmp(line
,"NumberOfEntries",15) == 0) {
272 v
= pls_entry_get_value(line
);
275 mp_msg(MSGT_PLAYTREE
,MSGL_DBG2
,"Invalid number of entries: very funny!!!\n");
277 mp_msg(MSGT_PLAYTREE
,MSGL_DBG2
,"Playlist claims to have %d entries. Let's see.\n",n_entries
);
278 line
= play_tree_parser_get_line(p
);
283 if(line
[0] == '\0') {
284 line
= play_tree_parser_get_line(p
);
287 if(strncasecmp(line
,"File",4) == 0) {
288 num
= pls_read_entry(line
+4,&entries
,&max_entry
,&v
);
290 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"No value in entry %s\n",line
);
292 entries
[num
-1].file
= strdup(v
);
293 } else if(strncasecmp(line
,"Title",5) == 0) {
294 num
= pls_read_entry(line
+5,&entries
,&max_entry
,&v
);
296 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"No value in entry %s\n",line
);
298 entries
[num
-1].title
= strdup(v
);
299 } else if(strncasecmp(line
,"Length",6) == 0) {
300 num
= pls_read_entry(line
+6,&entries
,&max_entry
,&v
);
302 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"No value in entry %s\n",line
);
304 entries
[num
-1].length
= strdup(v
);
306 mp_msg(MSGT_PLAYTREE
,MSGL_WARN
,"Unknown entry type %s\n",line
);
307 line
= play_tree_parser_get_line(p
);
310 for(num
= 0; num
< max_entry
; num
++) {
311 if(entries
[num
].file
== NULL
)
312 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"Entry %d don't have a file !!!!\n",num
+1);
314 mp_msg(MSGT_PLAYTREE
,MSGL_DBG2
,"Adding entry %s\n",entries
[num
].file
);
315 entry
= play_tree_new();
316 play_tree_add_file(entry
,entries
[num
].file
);
317 free(entries
[num
].file
);
319 play_tree_append_entry(last_entry
,entry
);
324 if(entries
[num
].title
) {
325 // When we have info in playtree we add this info
326 free(entries
[num
].title
);
328 if(entries
[num
].length
) {
329 // When we have info in playtree we add this info
330 free(entries
[num
].length
);
336 entry
= play_tree_new();
337 play_tree_set_child(entry
,list
);
342 Reference Ini-Format: Each entry is assumed a reference
345 parse_ref_ini(play_tree_parser_t
* p
) {
347 play_tree_t
*list
= NULL
, *entry
= NULL
, *last_entry
= NULL
;
349 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Trying reference-ini playlist...\n");
350 if (!(line
= play_tree_parser_get_line(p
)))
353 if(strcasecmp(line
,"[Reference]"))
355 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Detected reference-ini playlist format\n");
356 play_tree_parser_stop_keeping(p
);
357 line
= play_tree_parser_get_line(p
);
362 if(strncasecmp(line
,"Ref",3) == 0) {
363 v
= pls_entry_get_value(line
+3);
365 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"No value in entry %s\n",line
);
368 mp_msg(MSGT_PLAYTREE
,MSGL_DBG2
,"Adding entry %s\n",v
);
369 entry
= play_tree_new();
370 play_tree_add_file(entry
,v
);
372 play_tree_append_entry(last_entry
,entry
);
378 line
= play_tree_parser_get_line(p
);
381 if(!list
) return NULL
;
382 entry
= play_tree_new();
383 play_tree_set_child(entry
,list
);
388 parse_m3u(play_tree_parser_t
* p
) {
390 play_tree_t
*list
= NULL
, *entry
= NULL
, *last_entry
= NULL
;
392 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Trying extended m3u playlist...\n");
393 if (!(line
= play_tree_parser_get_line(p
)))
396 if(strcasecmp(line
,"#EXTM3U"))
398 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Detected extended m3u playlist format\n");
399 play_tree_parser_stop_keeping(p
);
401 while((line
= play_tree_parser_get_line(p
)) != NULL
) {
405 /* EXTM3U files contain such lines:
406 * #EXTINF:<seconds>, <title>
407 * followed by a line with the filename
408 * for now we have no place to put that
409 * so we just skip that extra-info ::atmos
412 #if 0 /* code functional */
413 if(strncasecmp(line
,"#EXTINF:",8) == 0) {
414 mp_msg(MSGT_PLAYTREE
,MSGL_INFO
,"[M3U] Duration: %dsec Title: %s\n",
415 strtol(line
+8,&line
,10), line
+2);
420 entry
= play_tree_new();
421 play_tree_add_file(entry
,line
);
425 play_tree_append_entry(last_entry
,entry
);
429 if(!list
) return NULL
;
430 entry
= play_tree_new();
431 play_tree_set_child(entry
,list
);
436 parse_smil(play_tree_parser_t
* p
) {
438 char* line
,source
[512],*pos
,*s_start
,*s_end
,*src_line
;
439 play_tree_t
*list
= NULL
, *entry
= NULL
, *last_entry
= NULL
;
441 unsigned int npkt
, ttlpkt
;
443 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Trying smil playlist...\n");
446 while((line
= play_tree_parser_get_line(p
)) != NULL
) {
448 if(line
[0] == '\0') // Ignore empties
450 if (strncasecmp(line
,"<smil",5)==0 || strncasecmp(line
,"<?wpl",5)==0 ||
451 strncasecmp(line
,"(smil-document",14)==0)
452 break; // smil header found
454 return NULL
; //line not smil exit
457 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Detected smil playlist format\n");
458 play_tree_parser_stop_keeping(p
);
460 if (strncasecmp(line
,"(smil-document",14)==0) {
461 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Special smil-over-realrtsp playlist header\n");
463 if (sscanf(line
, "(smil-document (ver 1.0)(npkt %u)(ttlpkt %u", &npkt
, &ttlpkt
) != 2) {
464 mp_msg(MSGT_PLAYTREE
,MSGL_WARN
,"smil-over-realrtsp: header parsing failure, assuming single packet.\n");
467 if (ttlpkt
== 0 || npkt
> ttlpkt
) {
468 mp_msg(MSGT_PLAYTREE
,MSGL_WARN
,"smil-over-realrtsp: bad packet counters (npkk = %u, ttlpkt = %u), assuming single packet.\n",
474 //Get entries from smil
476 while((src_line
= play_tree_parser_get_line(p
)) != NULL
) {
482 /* If we're parsing smil over realrtsp and this is not the last packet and
483 * this is the last line in the packet (terminating with ") ) we must get
484 * the next line, strip the header, and concatenate it to the current line.
486 if (is_rmsmil
&& npkt
!= ttlpkt
&& strstr(src_line
,"\")")) {
489 line
= strdup(src_line
);
490 if(!(src_line
= play_tree_parser_get_line(p
))) {
491 mp_msg(MSGT_PLAYTREE
,MSGL_WARN
,"smil-over-realrtsp: can't get line from packet %u/%u.\n", npkt
, ttlpkt
);
495 // Skip header, packet starts after "
496 if(!(payload
= strchr(src_line
,'\"'))) {
497 mp_msg(MSGT_PLAYTREE
,MSGL_WARN
,"smil-over-realrtsp: can't find start of packet, using complete line.\n");
501 // Skip ") at the end of the last line from the current packet
502 line
[strlen(line
)-2] = 0;
503 line
= realloc(line
, strlen(line
)+strlen(payload
));
504 strcat (line
, payload
);
507 line
= strdup(src_line
);
508 /* Unescape \" to " for smil-over-rtsp */
509 if (is_rmsmil
&& line
[0] != '\0') {
512 for (i
= 0; i
< strlen(line
); i
++)
513 if (line
[i
] == '\\' && line
[i
+1] == '"')
514 for (j
= i
; line
[j
]; j
++)
519 if (!entrymode
) { // all entries filled so far
520 if (strncasecmp(line
,"<video",6)==0 || strncasecmp(line
,"<audio",6)==0 || strncasecmp(line
,"<media",6)==0) {
521 pos
=strstr(line
,"src="); // Is source present on this line
524 s_end
=strchr(s_start
,'"');
526 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Error parsing this source line %s\n",line
);
529 if (s_end
-s_start
> 511) {
530 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Cannot store such a large source %s\n",line
);
533 strncpy(source
,s_start
,s_end
-s_start
);
534 source
[(s_end
-s_start
)]='\0'; // Null terminate
535 entry
= play_tree_new();
536 play_tree_add_file(entry
,source
);
537 if(!list
) //Insert new entry
540 play_tree_append_entry(last_entry
,entry
);
546 } else { //Entry found but not yet filled
547 pos
= strstr(line
,"src="); // Is source present on this line
551 s_end
=strchr(s_start
,'"');
553 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Error parsing this source line %s\n",line
);
556 if (s_end
-s_start
> 511) {
557 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Cannot store such a large source %s\n",line
);
560 strncpy(source
,s_start
,s_end
-s_start
);
561 source
[(s_end
-s_start
)]='\0'; // Null terminate
562 entry
= play_tree_new();
563 play_tree_add_file(entry
,source
);
564 if(!list
) //Insert new entry
567 play_tree_append_entry(last_entry
,entry
);
576 if(!list
) return NULL
; // Nothing found
578 entry
= play_tree_new();
579 play_tree_set_child(entry
,list
);
584 embedded_playlist_parse(char *line
) {
585 int f
=DEMUXER_TYPE_PLAYLIST
;
587 play_tree_parser_t
* ptp
;
590 // Get stream opened to link
591 stream
=open_stream(line
,0,&f
);
593 mp_msg(MSGT_PLAYTREE
,MSGL_WARN
,"Can't open playlist %s\n",line
);
598 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Adding playlist %s to element entryref\n",line
);
600 ptp
= play_tree_parser_new(stream
,1);
601 entry
= play_tree_parser_get_play_tree(ptp
, 1);
602 play_tree_parser_free(ptp
);
609 parse_textplain(play_tree_parser_t
* p
) {
613 play_tree_t
*list
= NULL
, *entry
= NULL
, *last_entry
= NULL
;
615 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Trying plaintext playlist...\n");
616 play_tree_parser_stop_keeping(p
);
618 while((line
= play_tree_parser_get_line(p
)) != NULL
) {
620 if(line
[0] == '\0' || line
[0] == '#' || (line
[0] == '/' && line
[1] == '/'))
623 //Special check for embedded smil or ram reference in file
625 if (strlen(line
) > 5)
626 for(c
= line
; c
[0]; c
++ )
627 if ( ((c
[0] == '.') && //start with . and next have smil with optional ? or &
628 (tolower(c
[1]) == 's') && (tolower(c
[2])== 'm') &&
629 (tolower(c
[3]) == 'i') && (tolower(c
[4]) == 'l') &&
630 (!c
[5] || c
[5] == '?' || c
[5] == '&')) || // or
631 ((c
[0] == '.') && // start with . and next have smi or ram with optional ? or &
632 ( ((tolower(c
[1]) == 's') && (tolower(c
[2])== 'm') && (tolower(c
[3]) == 'i')) ||
633 ((tolower(c
[1]) == 'r') && (tolower(c
[2])== 'a') && (tolower(c
[3]) == 'm')) )
634 && (!c
[4] || c
[4] == '?' || c
[4] == '&')) ){
635 entry
=embedded_playlist_parse(line
);
640 if (!embedded
) { //regular file link
641 entry
= play_tree_new();
642 play_tree_add_file(entry
,line
);
649 play_tree_append_entry(last_entry
,entry
);
654 if(!list
) return NULL
;
655 entry
= play_tree_new();
656 play_tree_set_child(entry
,list
);
661 parse_playtree(stream_t
*stream
, int forced
) {
662 play_tree_parser_t
* p
;
666 assert(stream
!= NULL
);
669 p
= play_tree_parser_new(stream
,0);
673 ret
= play_tree_parser_get_play_tree(p
, forced
);
674 play_tree_parser_free(p
);
680 play_tree_add_basepath(play_tree_t
* pt
, char* bp
) {
681 int i
,bl
= strlen(bp
),fl
;
685 for(i
= pt
->child
; i
!= NULL
; i
= i
->next
)
686 play_tree_add_basepath(i
,bp
);
693 for(i
= 0 ; pt
->files
[i
] != NULL
; i
++) {
694 fl
= strlen(pt
->files
[i
]);
695 // if we find a full unix path, url:// or X:\ at the beginning,
697 if(fl
<= 0 || strstr(pt
->files
[i
],"://") || (strstr(pt
->files
[i
],":\\") == pt
->files
[i
] + 1) || (pt
->files
[i
][0] == '/') )
699 // if the path begins with \ then prepend drive letter to it.
700 if (pt
->files
[i
][0] == '\\') {
701 if (pt
->files
[i
][1] == '\\')
703 pt
->files
[i
] = (char*)realloc(pt
->files
[i
],2+fl
+1);
704 memmove(pt
->files
[i
] + 2,pt
->files
[i
],fl
+1);
705 memcpy(pt
->files
[i
],bp
,2);
708 pt
->files
[i
] = (char*)realloc(pt
->files
[i
],bl
+fl
+1);
709 memmove(pt
->files
[i
] + bl
,pt
->files
[i
],fl
+1);
710 memcpy(pt
->files
[i
],bp
,bl
);
714 // Wrapper for play_tree_add_basepath (add base path from file)
715 void play_tree_add_bpf(play_tree_t
* pt
, char* filename
)
721 file
= strdup(filename
);
724 ls
= strrchr(file
,'/');
725 if(!ls
) ls
= strrchr(file
,'\\');
728 play_tree_add_basepath(pt
,file
);
736 parse_playlist_file(char* file
) {
739 int f
=DEMUXER_TYPE_PLAYLIST
;
741 stream
= open_stream(file
,0,&f
);
744 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"Error while opening playlist file %s: %s\n",file
,strerror(errno
));
748 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Parsing playlist file %s...\n",file
);
750 ret
= parse_playtree(stream
,1);
753 play_tree_add_bpf(ret
, file
);
761 play_tree_parser_new(stream_t
* stream
,int deep
) {
762 play_tree_parser_t
* p
;
764 p
= calloc(1,sizeof(play_tree_parser_t
));
776 play_tree_parser_free(play_tree_parser_t
* p
) {
782 if(p
->buffer
) free(p
->buffer
);
783 if(p
->line
) free(p
->line
);
788 play_tree_parser_get_play_tree(play_tree_parser_t
* p
, int forced
) {
789 play_tree_t
* tree
= NULL
;
796 while(play_tree_parser_get_line(p
) != NULL
) {
797 play_tree_parser_reset(p
);
801 play_tree_parser_reset(p
);
805 play_tree_parser_reset(p
);
809 play_tree_parser_reset(p
);
811 tree
= parse_ref_ini(p
);
813 play_tree_parser_reset(p
);
815 tree
= parse_smil(p
);
817 play_tree_parser_reset(p
);
819 // Here come the others formats ( textplain must stay the last one )
822 tree
= parse_textplain(p
);
829 mp_msg(MSGT_PLAYTREE
,MSGL_V
,"Playlist successfully parsed\n");
831 mp_msg(MSGT_PLAYTREE
,((forced
==1)?MSGL_ERR
:MSGL_V
),"Error while parsing playlist\n");
834 tree
= play_tree_cleanup(tree
);
836 if(!tree
) mp_msg(MSGT_PLAYTREE
,((forced
==1)?MSGL_WARN
:MSGL_V
),"Warning: empty playlist\n");