demuxer.c: Add support for parsing LATM
[mplayer/greg.git] / playtreeparser.c
blob93a8dfefc068962ba4c8f5ade87bbf76bef2a7d2
1 /*
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.
19 /// \file
20 /// \ingroup PlaytreeParser
22 #include "config.h"
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <assert.h>
27 #include <errno.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
31 #include <unistd.h>
32 #include <ctype.h>
33 #include <limits.h>
34 #include "asxparser.h"
35 #include "m_config.h"
36 #include "playtree.h"
37 #include "playtreeparser.h"
38 #include "stream/stream.h"
39 #include "libmpdemux/demuxer.h"
40 #include "mp_msg.h"
43 #define BUF_STEP 1024
45 #define WHITES " \n\r\t"
47 static void
48 strstrip(char* str) {
49 char* i;
51 if (str==NULL)
52 return;
53 for(i = str ; i[0] != '\0' && strchr(WHITES,i[0]) != NULL; i++)
54 /* NOTHING */;
55 if(i[0] != '\0') {
56 memmove(str,i,strlen(i) + 1);
57 for(i = str + strlen(str) - 1 ; strchr(WHITES,i[0]) != NULL; i--)
58 /* NOTHING */;
59 i[1] = '\0';
60 } else
61 str[0] = '\0';
64 static char*
65 play_tree_parser_get_line(play_tree_parser_t* p) {
66 char *end,*line_end;
67 int r,resize = 0;
69 if(p->buffer == NULL) {
70 p->buffer = malloc(BUF_STEP);
71 p->buffer_size = BUF_STEP;
72 p->buffer[0] = 0;
73 p->iter = p->buffer;
76 if(p->stream->eof && (p->buffer_end == 0 || p->iter[0] == '\0'))
77 return NULL;
79 assert(p->buffer_end < p->buffer_size);
80 assert(!p->buffer[p->buffer_end]);
81 while(1) {
83 if(resize) {
84 char *tmp;
85 r = p->iter - p->buffer;
86 end = p->buffer + p->buffer_end;
87 if (p->buffer_size > INT_MAX - BUF_STEP)
88 break;
89 tmp = realloc(p->buffer, p->buffer_size + BUF_STEP);
90 if (!tmp)
91 break;
92 p->buffer = tmp;
93 p->iter = p->buffer + r;
94 p->buffer_size += BUF_STEP;
95 resize = 0;
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);
100 if(r > 0) {
101 p->buffer_end += r;
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');
111 if(!end) {
112 if(p->stream->eof) {
113 end = p->buffer + p->buffer_end;
114 break;
116 resize = 1;
117 continue;
119 break;
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);
125 else
126 return NULL;
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';
130 if(end[0] != '\0')
131 end++;
133 if(!p->keep) {
134 if(end[0] != '\0') {
135 p->buffer_end -= end-p->iter;
136 memmove(p->buffer,end,p->buffer_end);
137 } else
138 p->buffer_end = 0;
139 p->buffer[p->buffer_end] = '\0';
140 p->iter = p->buffer;
141 } else
142 p->iter = end;
144 return p->line;
147 static void
148 play_tree_parser_reset(play_tree_parser_t* p) {
149 p->iter = p->buffer;
152 static void
153 play_tree_parser_stop_keeping(play_tree_parser_t* p) {
154 p->keep = 0;
155 if(p->iter && p->iter != p->buffer) {
156 p->buffer_end -= p->iter -p->buffer;
157 if(p->buffer_end)
158 memmove(p->buffer,p->iter,p->buffer_end);
159 p->buffer[p->buffer_end] = 0;
160 p->iter = p->buffer;
165 static play_tree_t*
166 parse_asx(play_tree_parser_t* p) {
167 int comments = 0,get_line = 1;
168 char* line = NULL;
170 mp_msg(MSGT_PLAYTREE,MSGL_V,"Trying asx...\n");
172 while(1) {
173 if(get_line) {
174 line = play_tree_parser_get_line(p);
175 if(!line)
176 return NULL;
177 strstrip(line);
178 if(line[0] == '\0')
179 continue;
181 if(!comments) {
182 if(line[0] != '<') {
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);
185 return NULL;
186 } else if(strncmp(line,"<!--",4) == 0) { // Comments
187 comments = 1;
188 line += 4;
189 if(line[0] != '\0' && strlen(line) > 0)
190 get_line = 0;
191 } else if(strncasecmp(line,"<ASX",4) == 0) // We got an asx element
192 break;
193 else // We don't get an asx
194 return NULL;
195 } else { // Comments
196 char* c;
197 c = strchr(line,'-');
198 if(c) {
199 if (strncmp(c,"--!>",4) == 0) { // End of comments
200 comments = 0;
201 line = c+4;
202 if(line[0] != '\0') // There is some more data on this line : keep it
203 get_line = 0;
205 } else {
206 line = c+1; // Jump the -
207 if(line[0] != '\0') // Some more data
208 get_line = 0;
209 else // End of line
210 get_line = 1;
212 } else // No - on this line (or rest of line) : get next one
213 get_line = 1;
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)
222 /* NOTHING */;
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);
228 static char*
229 pls_entry_get_value(char* line) {
230 char* i;
232 i = strchr(line,'=');
233 if(!i || i[1] == '\0')
234 return NULL;
235 else
236 return i+1;
239 typedef struct pls_entry {
240 char* file;
241 char* title;
242 char* length;
243 } pls_entry_t;
245 static int
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);
250 char* v;
252 v = pls_entry_get_value(line);
253 if(!v) {
254 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"No value in entry %s\n",line);
255 return -1;
258 num = atoi(line);
259 if(num <= 0 || num > limit) {
260 if (max_entry >= limit) {
261 mp_msg(MSGT_PLAYTREE, MSGL_WARN, "Too many index entries\n");
262 return -1;
264 num = max_entry+1;
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));
269 if (!e)
270 return -1;
271 memset(&e[max_entry],0,(num-max_entry)*sizeof(pls_entry_t));
272 max_entry = num;
274 (*_e) = e;
275 (*_max_entry) = max_entry;
276 (*val) = v;
278 return num;
282 static play_tree_t*
283 parse_pls(play_tree_parser_t* p) {
284 char *line,*v;
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))) {
291 strstrip(line);
292 if(strlen(line))
293 break;
295 if (!line)
296 return NULL;
297 if(strcasecmp(line,"[playlist]"))
298 return NULL;
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);
302 if(!line)
303 return NULL;
304 strstrip(line);
305 if(strncasecmp(line,"NumberOfEntries",15) == 0) {
306 v = pls_entry_get_value(line);
307 n_entries = atoi(v);
308 if(n_entries < 0)
309 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Invalid number of entries: very funny!!!\n");
310 else
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);
315 while(line) {
316 strstrip(line);
317 if(line[0] == '\0') {
318 line = play_tree_parser_get_line(p);
319 continue;
321 if(strncasecmp(line,"File",4) == 0) {
322 num = pls_read_entry(line+4,&entries,&max_entry,&v);
323 if(num < 0)
324 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"No value in entry %s\n",line);
325 else
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);
329 if(num < 0)
330 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"No value in entry %s\n",line);
331 else
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);
335 if(num < 0)
336 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"No value in entry %s\n",line);
337 else
338 entries[num-1].length = strdup(v);
339 } else
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);
347 else {
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);
352 if(list)
353 play_tree_append_entry(last_entry,entry);
354 else
355 list = entry;
356 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);
368 free(entries);
370 if (!list)
371 return NULL;
373 entry = play_tree_new();
374 play_tree_set_child(entry,list);
375 return entry;
379 Reference Ini-Format: Each entry is assumed a reference
381 static play_tree_t*
382 parse_ref_ini(play_tree_parser_t* p) {
383 char *line,*v;
384 play_tree_t *list = NULL, *entry = NULL, *last_entry = NULL;
386 mp_msg(MSGT_PLAYTREE,MSGL_V,"Trying reference-ini playlist...\n");
387 if (!(line = play_tree_parser_get_line(p)))
388 return NULL;
389 strstrip(line);
390 if(strcasecmp(line,"[Reference]"))
391 return NULL;
392 mp_msg(MSGT_PLAYTREE,MSGL_V,"Detected reference-ini playlist format\n");
393 play_tree_parser_stop_keeping(p);
394 line = play_tree_parser_get_line(p);
395 if(!line)
396 return NULL;
397 while(line) {
398 strstrip(line);
399 if(strncasecmp(line,"Ref",3) == 0) {
400 v = pls_entry_get_value(line+3);
401 if(!v)
402 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"No value in entry %s\n",line);
403 else
405 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Adding entry %s\n",v);
406 entry = play_tree_new();
407 play_tree_add_file(entry,v);
408 if(list)
409 play_tree_append_entry(last_entry,entry);
410 else
411 list = entry;
412 last_entry = entry;
415 line = play_tree_parser_get_line(p);
418 if(!list) return NULL;
419 entry = play_tree_new();
420 play_tree_set_child(entry,list);
421 return entry;
424 static play_tree_t*
425 parse_m3u(play_tree_parser_t* p) {
426 char* line;
427 play_tree_t *list = NULL, *entry = NULL, *last_entry = NULL;
429 mp_msg(MSGT_PLAYTREE,MSGL_V,"Trying extended m3u playlist...\n");
430 if (!(line = play_tree_parser_get_line(p)))
431 return NULL;
432 strstrip(line);
433 if(strcasecmp(line,"#EXTM3U"))
434 return NULL;
435 mp_msg(MSGT_PLAYTREE,MSGL_V,"Detected extended m3u playlist format\n");
436 play_tree_parser_stop_keeping(p);
438 while((line = play_tree_parser_get_line(p)) != NULL) {
439 strstrip(line);
440 if(line[0] == '\0')
441 continue;
442 /* EXTM3U files contain such lines:
443 * #EXTINF:<seconds>, <title>
444 * followed by a line with the filename
445 * for now we have no place to put that
446 * so we just skip that extra-info ::atmos
448 if(line[0] == '#') {
449 #if 0 /* code functional */
450 if(strncasecmp(line,"#EXTINF:",8) == 0) {
451 mp_msg(MSGT_PLAYTREE,MSGL_INFO,"[M3U] Duration: %dsec Title: %s\n",
452 strtol(line+8,&line,10), line+2);
454 #endif
455 continue;
457 entry = play_tree_new();
458 play_tree_add_file(entry,line);
459 if(!list)
460 list = entry;
461 else
462 play_tree_append_entry(last_entry,entry);
463 last_entry = entry;
466 if(!list) return NULL;
467 entry = play_tree_new();
468 play_tree_set_child(entry,list);
469 return entry;
472 static play_tree_t*
473 parse_smil(play_tree_parser_t* p) {
474 int entrymode=0;
475 char* line,source[512],*pos,*s_start,*s_end,*src_line;
476 play_tree_t *list = NULL, *entry = NULL, *last_entry = NULL;
477 int is_rmsmil = 0;
478 unsigned int npkt, ttlpkt;
480 mp_msg(MSGT_PLAYTREE,MSGL_V,"Trying smil playlist...\n");
482 // Check if smil
483 while((line = play_tree_parser_get_line(p)) != NULL) {
484 strstrip(line);
485 if(line[0] == '\0') // Ignore empties
486 continue;
487 if (strncasecmp(line,"<?xml",5)==0) // smil in xml
488 continue;
489 if (strncasecmp(line,"<!DOCTYPE smil",13)==0) // smil in xml
490 continue;
491 if (strncasecmp(line,"<smil",5)==0 || strncasecmp(line,"<?wpl",5)==0 ||
492 strncasecmp(line,"(smil-document",14)==0)
493 break; // smil header found
494 else
495 return NULL; //line not smil exit
498 if (!line) return NULL;
499 mp_msg(MSGT_PLAYTREE,MSGL_V,"Detected smil playlist format\n");
500 play_tree_parser_stop_keeping(p);
502 if (strncasecmp(line,"(smil-document",14)==0) {
503 mp_msg(MSGT_PLAYTREE,MSGL_V,"Special smil-over-realrtsp playlist header\n");
504 is_rmsmil = 1;
505 if (sscanf(line, "(smil-document (ver 1.0)(npkt %u)(ttlpkt %u", &npkt, &ttlpkt) != 2) {
506 mp_msg(MSGT_PLAYTREE,MSGL_WARN,"smil-over-realrtsp: header parsing failure, assuming single packet.\n");
507 npkt = ttlpkt = 1;
509 if (ttlpkt == 0 || npkt > ttlpkt) {
510 mp_msg(MSGT_PLAYTREE,MSGL_WARN,"smil-over-realrtsp: bad packet counters (npkk = %u, ttlpkt = %u), assuming single packet.\n",
511 npkt, ttlpkt);
512 npkt = ttlpkt = 1;
516 //Get entries from smil
517 src_line = line;
518 line = NULL;
519 do {
520 strstrip(src_line);
521 if (line) {
522 free(line);
523 line = NULL;
525 /* If we're parsing smil over realrtsp and this is not the last packet and
526 * this is the last line in the packet (terminating with ") ) we must get
527 * the next line, strip the header, and concatenate it to the current line.
529 if (is_rmsmil && npkt != ttlpkt && strstr(src_line,"\")")) {
530 char *payload;
532 line = strdup(src_line);
533 if(!(src_line = play_tree_parser_get_line(p))) {
534 mp_msg(MSGT_PLAYTREE,MSGL_WARN,"smil-over-realrtsp: can't get line from packet %u/%u.\n", npkt, ttlpkt);
535 break;
537 strstrip(src_line);
538 // Skip header, packet starts after "
539 if(!(payload = strchr(src_line,'\"'))) {
540 mp_msg(MSGT_PLAYTREE,MSGL_WARN,"smil-over-realrtsp: can't find start of packet, using complete line.\n");
541 payload = src_line;
542 } else
543 payload++;
544 // Skip ") at the end of the last line from the current packet
545 line[strlen(line)-2] = 0;
546 line = realloc(line, strlen(line)+strlen(payload)+1);
547 strcat (line, payload);
548 npkt++;
549 } else
550 line = strdup(src_line);
551 /* Unescape \" to " for smil-over-rtsp */
552 if (is_rmsmil && line[0] != '\0') {
553 int i, j;
555 for (i = 0; i < strlen(line); i++)
556 if (line[i] == '\\' && line[i+1] == '"')
557 for (j = i; line[j]; j++)
558 line[j] = line[j+1];
560 pos = line;
561 while (pos) {
562 if (!entrymode) { // all entries filled so far
563 while ((pos=strchr(pos, '<'))) {
564 if (strncasecmp(pos,"<video",6)==0 || strncasecmp(pos,"<audio",6)==0 || strncasecmp(pos,"<media",6)==0) {
565 entrymode=1;
566 break; // Got a valid tag, exit '<' search loop
568 pos++;
571 if (entrymode) { //Entry found but not yet filled
572 pos = strstr(pos,"src="); // Is source present on this line
573 if (pos != NULL) {
574 entrymode=0;
575 if (pos[4] != '"' && pos[4] != '\'') {
576 mp_msg(MSGT_PLAYTREE,MSGL_V,"Unknown delimiter %c in source line %s\n", pos[4], line);
577 break;
579 s_start=pos+5;
580 s_end=strchr(s_start,pos[4]);
581 if (s_end == NULL) {
582 mp_msg(MSGT_PLAYTREE,MSGL_V,"Error parsing this source line %s\n",line);
583 break;
585 if (s_end-s_start> 511) {
586 mp_msg(MSGT_PLAYTREE,MSGL_V,"Cannot store such a large source %s\n",line);
587 break;
589 strncpy(source,s_start,s_end-s_start);
590 source[(s_end-s_start)]='\0'; // Null terminate
591 entry = play_tree_new();
592 play_tree_add_file(entry,source);
593 if(!list) //Insert new entry
594 list = entry;
595 else
596 play_tree_append_entry(last_entry,entry);
597 last_entry = entry;
598 pos = s_end;
602 } while((src_line = play_tree_parser_get_line(p)) != NULL);
604 if (line)
605 free(line);
607 if(!list) return NULL; // Nothing found
609 entry = play_tree_new();
610 play_tree_set_child(entry,list);
611 return entry;
614 static play_tree_t*
615 embedded_playlist_parse(struct m_config *mconfig, char *line) {
616 int f=DEMUXER_TYPE_PLAYLIST;
617 stream_t* stream;
618 play_tree_parser_t* ptp;
619 play_tree_t* entry;
621 // Get stream opened to link
622 stream=open_stream(line,0,&f);
623 if(!stream) {
624 mp_msg(MSGT_PLAYTREE,MSGL_WARN,"Can't open playlist %s\n",line);
625 return NULL;
628 //add new playtree
629 mp_msg(MSGT_PLAYTREE,MSGL_V,"Adding playlist %s to element entryref\n",line);
631 ptp = play_tree_parser_new(stream, mconfig, 1);
632 entry = play_tree_parser_get_play_tree(ptp, 1);
633 play_tree_parser_free(ptp);
634 free_stream(stream);
636 return entry;
639 static play_tree_t*
640 parse_textplain(play_tree_parser_t* p) {
641 char* line;
642 char *c;
643 int embedded;
644 play_tree_t *list = NULL, *entry = NULL, *last_entry = NULL;
646 mp_msg(MSGT_PLAYTREE,MSGL_V,"Trying plaintext playlist...\n");
647 play_tree_parser_stop_keeping(p);
649 while((line = play_tree_parser_get_line(p)) != NULL) {
650 strstrip(line);
651 if(line[0] == '\0' || line[0] == '#' || (line[0] == '/' && line[1] == '/'))
652 continue;
654 //Special check for embedded smil or ram reference in file
655 embedded = 0;
656 if (strlen(line) > 5)
657 for(c = line; c[0]; c++ )
658 if ( ((c[0] == '.') && //start with . and next have smil with optional ? or &
659 (tolower(c[1]) == 's') && (tolower(c[2])== 'm') &&
660 (tolower(c[3]) == 'i') && (tolower(c[4]) == 'l') &&
661 (!c[5] || c[5] == '?' || c[5] == '&')) || // or
662 ((c[0] == '.') && // start with . and next have smi or ram with optional ? or &
663 ( ((tolower(c[1]) == 's') && (tolower(c[2])== 'm') && (tolower(c[3]) == 'i')) ||
664 ((tolower(c[1]) == 'r') && (tolower(c[2])== 'a') && (tolower(c[3]) == 'm')) )
665 && (!c[4] || c[4] == '?' || c[4] == '&')) ){
666 entry=embedded_playlist_parse(p->mconfig, line);
667 embedded = 1;
668 break;
671 if (!embedded) { //regular file link
672 entry = play_tree_new();
673 play_tree_add_file(entry,line);
676 if (entry != NULL) {
677 if(!list)
678 list = entry;
679 else
680 play_tree_append_entry(last_entry,entry);
681 last_entry = entry;
685 if(!list) return NULL;
686 entry = play_tree_new();
687 play_tree_set_child(entry,list);
688 return entry;
691 play_tree_t*
692 parse_playtree(stream_t *stream, struct m_config *mconfig, int forced) {
693 play_tree_parser_t* p;
694 play_tree_t* ret;
696 #ifdef MP_DEBUG
697 assert(stream != NULL);
698 #endif
700 p = play_tree_parser_new(stream, mconfig, 0);
701 if(!p)
702 return NULL;
704 ret = play_tree_parser_get_play_tree(p, forced);
705 play_tree_parser_free(p);
707 return ret;
710 static void
711 play_tree_add_basepath(play_tree_t* pt, char* bp) {
712 int i,bl = strlen(bp),fl;
714 if(pt->child) {
715 play_tree_t* i;
716 for(i = pt->child ; i != NULL ; i = i->next)
717 play_tree_add_basepath(i,bp);
718 return;
721 if(!pt->files)
722 return;
724 for(i = 0 ; pt->files[i] != NULL ; i++) {
725 fl = strlen(pt->files[i]);
726 // if we find a full unix path, url:// or X:\ at the beginning,
727 // don't mangle it.
728 if(fl <= 0 || strstr(pt->files[i],"://") || (strstr(pt->files[i],":\\") == pt->files[i] + 1) || (pt->files[i][0] == '/') )
729 continue;
730 // if the path begins with \ then prepend drive letter to it.
731 if (pt->files[i][0] == '\\') {
732 if (pt->files[i][1] == '\\')
733 continue;
734 pt->files[i] = realloc(pt->files[i], 2 + fl + 1);
735 memmove(pt->files[i] + 2,pt->files[i],fl+1);
736 memcpy(pt->files[i],bp,2);
737 continue;
739 pt->files[i] = realloc(pt->files[i], bl + fl + 1);
740 memmove(pt->files[i] + bl,pt->files[i],fl+1);
741 memcpy(pt->files[i],bp,bl);
745 // Wrapper for play_tree_add_basepath (add base path from file)
746 void play_tree_add_bpf(play_tree_t* pt, char* filename)
748 char *ls, *file;
750 if (pt && filename)
752 file = strdup(filename);
753 if (file)
755 ls = strrchr(file,'/');
756 if(!ls) ls = strrchr(file,'\\');
757 if(ls) {
758 ls[1] = '\0';
759 play_tree_add_basepath(pt,file);
761 free(file);
766 play_tree_t*
767 parse_playlist_file(struct m_config *mconfig, char* file) {
768 stream_t *stream;
769 play_tree_t* ret;
770 int f=DEMUXER_TYPE_PLAYLIST;
772 stream = open_stream(file,0,&f);
774 if(!stream) {
775 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Error while opening playlist file %s: %s\n",file,strerror(errno));
776 return NULL;
779 mp_msg(MSGT_PLAYTREE,MSGL_V,"Parsing playlist file %s...\n",file);
781 ret = parse_playtree(stream, mconfig, 1);
782 free_stream(stream);
784 play_tree_add_bpf(ret, file);
786 return ret;
791 play_tree_parser_t*
792 play_tree_parser_new(stream_t* stream, struct m_config *mconfig, int deep) {
793 play_tree_parser_t* p;
795 p = calloc(1,sizeof(play_tree_parser_t));
796 if(!p)
797 return NULL;
798 p->stream = stream;
799 p->mconfig = mconfig;
800 p->deep = deep;
801 p->keep = 1;
803 return p;
807 void
808 play_tree_parser_free(play_tree_parser_t* p) {
810 #ifdef MP_DEBUG
811 assert(p != NULL);
812 #endif
814 if(p->buffer) free(p->buffer);
815 if(p->line) free(p->line);
816 free(p);
819 play_tree_t*
820 play_tree_parser_get_play_tree(play_tree_parser_t* p, int forced) {
821 play_tree_t* tree = NULL;
823 #ifdef MP_DEBUG
824 assert(p != NULL);
825 #endif
828 while(play_tree_parser_get_line(p) != NULL) {
829 play_tree_parser_reset(p);
831 tree = parse_asx(p);
832 if(tree) break;
833 play_tree_parser_reset(p);
835 tree = parse_pls(p);
836 if(tree) break;
837 play_tree_parser_reset(p);
839 tree = parse_m3u(p);
840 if(tree) break;
841 play_tree_parser_reset(p);
843 tree = parse_ref_ini(p);
844 if(tree) break;
845 play_tree_parser_reset(p);
847 tree = parse_smil(p);
848 if(tree) break;
849 play_tree_parser_reset(p);
851 // Here come the others formats ( textplain must stay the last one )
852 if (forced)
854 tree = parse_textplain(p);
855 if(tree) break;
857 break;
860 if(tree)
861 mp_msg(MSGT_PLAYTREE,MSGL_V,"Playlist successfully parsed\n");
862 else
863 mp_msg(MSGT_PLAYTREE,((forced==1)?MSGL_ERR:MSGL_V),"Error while parsing playlist\n");
865 if(tree)
866 tree = play_tree_cleanup(tree);
868 if(!tree) mp_msg(MSGT_PLAYTREE,((forced==1)?MSGL_WARN:MSGL_V),"Warning: empty playlist\n");
870 return tree;