demux: do not set global sub_utf8
[mplayer.git] / stream / stream.c
blob9e0f5286ec7bec4aaf95ee2f5162f61593f7e539
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 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #ifndef __MINGW32__
26 #include <sys/ioctl.h>
27 #include <sys/wait.h>
28 #endif
29 #include <fcntl.h>
30 #include <strings.h>
31 #include <assert.h>
33 #include <libavutil/intreadwrite.h>
34 #include <libavutil/common.h>
36 #include "talloc.h"
38 #include "config.h"
40 #if HAVE_WINSOCK2_H
41 #include <winsock2.h>
42 #endif
44 #include "mp_msg.h"
45 #include "osdep/shmem.h"
46 #include "osdep/timer.h"
47 #include "network.h"
48 #include "stream.h"
49 #include "libmpdemux/demuxer.h"
51 #include "m_option.h"
52 #include "m_struct.h"
54 #include "cache2.h"
56 struct input_ctx;
57 static int (*stream_check_interrupt_cb)(struct input_ctx *ctx, int time);
58 static struct input_ctx *stream_check_interrupt_ctx;
60 extern const stream_info_t stream_info_vcd;
61 extern const stream_info_t stream_info_cdda;
62 extern const stream_info_t stream_info_netstream;
63 extern const stream_info_t stream_info_pnm;
64 extern const stream_info_t stream_info_asf;
65 extern const stream_info_t stream_info_rtp;
66 extern const stream_info_t stream_info_udp;
67 extern const stream_info_t stream_info_http1;
68 extern const stream_info_t stream_info_http2;
69 extern const stream_info_t stream_info_dvb;
70 extern const stream_info_t stream_info_tv;
71 extern const stream_info_t stream_info_radio;
72 extern const stream_info_t stream_info_pvr;
73 extern const stream_info_t stream_info_ftp;
74 extern const stream_info_t stream_info_vstream;
75 extern const stream_info_t stream_info_dvdnav;
76 extern const stream_info_t stream_info_smb;
78 extern const stream_info_t stream_info_cue;
79 extern const stream_info_t stream_info_null;
80 extern const stream_info_t stream_info_mf;
81 extern const stream_info_t stream_info_ffmpeg;
82 extern const stream_info_t stream_info_file;
83 extern const stream_info_t stream_info_ifo;
84 extern const stream_info_t stream_info_dvd;
85 extern const stream_info_t stream_info_bluray;
87 static const stream_info_t* const auto_open_streams[] = {
88 #ifdef CONFIG_VCD
89 &stream_info_vcd,
90 #endif
91 #ifdef CONFIG_CDDA
92 &stream_info_cdda,
93 #endif
94 #ifdef CONFIG_NETWORKING
95 &stream_info_netstream,
96 &stream_info_http1,
97 &stream_info_asf,
98 &stream_info_pnm,
99 &stream_info_rtp,
100 &stream_info_udp,
101 &stream_info_http2,
102 #endif
103 #ifdef CONFIG_DVBIN
104 &stream_info_dvb,
105 #endif
106 #ifdef CONFIG_TV
107 &stream_info_tv,
108 #endif
109 #ifdef CONFIG_RADIO
110 &stream_info_radio,
111 #endif
112 #ifdef CONFIG_PVR
113 &stream_info_pvr,
114 #endif
115 #ifdef CONFIG_FTP
116 &stream_info_ftp,
117 #endif
118 #ifdef CONFIG_VSTREAM
119 &stream_info_vstream,
120 #endif
121 #ifdef CONFIG_LIBSMBCLIENT
122 &stream_info_smb,
123 #endif
124 &stream_info_cue,
125 #ifdef CONFIG_DVDREAD
126 &stream_info_ifo,
127 &stream_info_dvd,
128 #endif
129 #ifdef CONFIG_DVDNAV
130 &stream_info_dvdnav,
131 #endif
132 #ifdef CONFIG_LIBBLURAY
133 &stream_info_bluray,
134 #endif
135 &stream_info_ffmpeg,
137 &stream_info_null,
138 &stream_info_mf,
139 &stream_info_file,
140 NULL
143 static stream_t *open_stream_plugin(const stream_info_t *sinfo,
144 const char *filename,
145 int mode, struct MPOpts *options,
146 int *file_format, int *ret,
147 char **redirected_url)
149 void* arg = NULL;
150 stream_t* s;
151 m_struct_t* desc = (m_struct_t*)sinfo->opts;
153 // Parse options
154 if(desc) {
155 arg = m_struct_alloc(desc);
156 if(sinfo->opts_url) {
157 m_option_t url_opt =
158 { "stream url", arg , CONF_TYPE_CUSTOM_URL, 0, 0 ,0, (void *)sinfo->opts };
159 if (m_option_parse(&url_opt, bstr("stream url"), bstr(filename), false, arg) < 0) {
160 mp_tmsg(MSGT_OPEN,MSGL_ERR, "URL parsing failed on url %s\n",filename);
161 m_struct_free(desc,arg);
162 return NULL;
166 s = new_stream(-2,-2);
167 s->opts = options;
168 s->url=strdup(filename);
169 s->flags |= mode;
170 *ret = sinfo->open(s,mode,arg,file_format);
171 if((*ret) != STREAM_OK) {
172 #ifdef CONFIG_NETWORKING
173 if (*ret == STREAM_REDIRECTED && redirected_url) {
174 if (s->streaming_ctrl && s->streaming_ctrl->url
175 && s->streaming_ctrl->url->url)
176 *redirected_url = strdup(s->streaming_ctrl->url->url);
177 else
178 *redirected_url = NULL;
180 streaming_ctrl_free(s->streaming_ctrl);
181 #endif
182 free(s->url);
183 free(s);
184 return NULL;
186 if(s->type <= -2)
187 mp_msg(MSGT_OPEN,MSGL_WARN, "Warning streams need a type !!!!\n");
188 if(s->flags & MP_STREAM_SEEK && !s->seek)
189 s->flags &= ~MP_STREAM_SEEK;
190 if(s->seek && !(s->flags & MP_STREAM_SEEK))
191 s->flags |= MP_STREAM_SEEK;
193 s->mode = mode;
195 mp_msg(MSGT_OPEN,MSGL_V, "STREAM: [%s] %s\n",sinfo->name,filename);
196 mp_msg(MSGT_OPEN,MSGL_V, "STREAM: Description: %s\n",sinfo->info);
197 mp_msg(MSGT_OPEN,MSGL_V, "STREAM: Author: %s\n", sinfo->author);
198 mp_msg(MSGT_OPEN,MSGL_V, "STREAM: Comment: %s\n", sinfo->comment);
200 return s;
204 stream_t *open_stream_full(const char *filename, int mode,
205 struct MPOpts *options, int *file_format)
207 int i,j,l,r;
208 const stream_info_t* sinfo;
209 stream_t* s;
210 char *redirected_url = NULL;
212 for(i = 0 ; auto_open_streams[i] ; i++) {
213 sinfo = auto_open_streams[i];
214 if(!sinfo->protocols) {
215 mp_msg(MSGT_OPEN,MSGL_WARN, "Stream type %s has protocols == NULL, it's a bug\n", sinfo->name);
216 continue;
218 for(j = 0 ; sinfo->protocols[j] ; j++) {
219 l = strlen(sinfo->protocols[j]);
220 // l == 0 => Don't do protocol matching (ie network and filenames)
221 if((l == 0 && !strstr(filename, "://")) ||
222 ((strncasecmp(sinfo->protocols[j],filename,l) == 0) &&
223 (strncmp("://",filename+l,3) == 0))) {
224 *file_format = DEMUXER_TYPE_UNKNOWN;
225 s = open_stream_plugin(sinfo,filename,mode,options,file_format,&r,
226 &redirected_url);
227 if(s) return s;
228 if(r == STREAM_REDIRECTED && redirected_url) {
229 mp_msg(MSGT_OPEN,MSGL_V, "[%s] open %s redirected to %s\n",
230 sinfo->info, filename, redirected_url);
231 s = open_stream_full(redirected_url, mode, options, file_format);
232 free(redirected_url);
233 return s;
235 else if(r != STREAM_UNSUPPORTED) {
236 mp_tmsg(MSGT_OPEN,MSGL_ERR, "Failed to open %s.\n",filename);
237 return NULL;
239 break;
244 mp_tmsg(MSGT_OPEN,MSGL_ERR, "No stream found to handle url %s\n", filename);
245 return NULL;
248 stream_t *open_output_stream(const char *filename, struct MPOpts *options)
250 int file_format; //unused
251 if(!filename) {
252 mp_msg(MSGT_OPEN,MSGL_ERR,"open_output_stream(), NULL filename, report this bug\n");
253 return NULL;
256 return open_stream_full(filename,STREAM_WRITE,options,&file_format);
259 //=================== STREAMER =========================
261 void stream_capture_do(stream_t *s)
263 if (fwrite(s->buffer, s->buf_len, 1, s->capture_file) < 1) {
264 mp_tmsg(MSGT_GLOBAL, MSGL_ERR, "Error writing capture file: %s\n",
265 strerror(errno));
266 fclose(s->capture_file);
267 s->capture_file = NULL;
271 int stream_read_internal(stream_t *s, void *buf, int len)
273 int orig_len = len;
274 // we will retry even if we already reached EOF previously.
275 switch(s->type){
276 case STREAMTYPE_STREAM:
277 #ifdef CONFIG_NETWORKING
278 if( s->streaming_ctrl!=NULL && s->streaming_ctrl->streaming_read ) {
279 len=s->streaming_ctrl->streaming_read(s->fd, buf, len, s->streaming_ctrl);
280 if (s->streaming_ctrl->status == streaming_stopped_e)
281 s->eof = 1;
282 } else
283 #endif
284 if (s->fill_buffer)
285 len = s->fill_buffer(s, buf, len);
286 else
287 len = read(s->fd, buf, len);
288 break;
289 case STREAMTYPE_DS:
290 len = demux_read_data((demux_stream_t*)s->priv, buf, len);
291 break;
294 default:
295 len= s->fill_buffer ? s->fill_buffer(s, buf, len) : 0;
297 if(len<=0){
298 off_t pos = s->pos;
299 // do not retry if this looks like proper eof
300 if (s->eof || (s->end_pos && pos == s->end_pos))
301 goto eof_out;
302 // dvdnav has some horrible hacks to "suspend" reads,
303 // we need to skip this code or seeks will hang.
304 if (s->type == STREAMTYPE_DVDNAV)
305 goto eof_out;
307 // just in case this is an error e.g. due to network
308 // timeout reset and retry
309 // Seeking is used as a hack to make network streams
310 // reopen the connection, ideally they would implement
311 // e.g. a STREAM_CTRL_RECONNECT to do this
312 s->eof=1;
313 stream_reset(s);
314 if (stream_seek_internal(s, pos) >= 0 || s->pos != pos) // seek failed
315 goto eof_out;
316 // make sure EOF is set to ensure no endless loops
317 s->eof=1;
318 return stream_read_internal(s, buf, orig_len);
320 eof_out:
321 s->eof=1;
322 return 0;
324 // When reading succeeded we are obviously not at eof.
325 // This e.g. avoids issues with eof getting stuck when lavf seeks in MPEG-TS
326 s->eof=0;
327 s->pos+=len;
328 return len;
331 int stream_fill_buffer(stream_t *s){
332 int len = stream_read_internal(s, s->buffer, STREAM_BUFFER_SIZE);
333 if (len <= 0)
334 return 0;
335 s->buf_pos=0;
336 s->buf_len=len;
337 // printf("[%d]",len);fflush(stdout);
338 if (s->capture_file)
339 stream_capture_do(s);
340 return len;
343 int stream_write_buffer(stream_t *s, unsigned char *buf, int len) {
344 int rd;
345 if(!s->write_buffer)
346 return -1;
347 rd = s->write_buffer(s, buf, len);
348 if(rd < 0)
349 return -1;
350 s->pos += rd;
351 assert(rd == len && "stream_write_buffer(): unexpected short write");
352 return rd;
355 int stream_seek_internal(stream_t *s, off_t newpos)
357 if(newpos==0 || newpos!=s->pos){
358 switch(s->type){
359 case STREAMTYPE_STREAM:
360 //s->pos=newpos; // real seek
361 // Some streaming protocol allow to seek backward and forward
362 // A function call that return -1 can tell that the protocol
363 // doesn't support seeking.
364 #ifdef CONFIG_NETWORKING
365 if(s->seek) { // new stream seek is much cleaner than streaming_ctrl one
366 if(!s->seek(s,newpos)) {
367 mp_tmsg(MSGT_STREAM,MSGL_ERR, "Seek failed\n");
368 return 0;
370 break;
373 if( s->streaming_ctrl!=NULL && s->streaming_ctrl->streaming_seek ) {
374 if( s->streaming_ctrl->streaming_seek( s->fd, newpos, s->streaming_ctrl )<0 ) {
375 mp_tmsg(MSGT_STREAM,MSGL_INFO,"Stream not seekable!\n");
376 return 1;
378 break;
380 #endif
381 if(newpos<s->pos){
382 mp_tmsg(MSGT_STREAM, MSGL_INFO,
383 "Cannot seek backward in linear streams!\n");
384 return 1;
386 break;
387 default:
388 // This should at the beginning as soon as all streams are converted
389 if(!s->seek)
390 return 0;
391 // Now seek
392 if(!s->seek(s,newpos)) {
393 mp_tmsg(MSGT_STREAM,MSGL_ERR, "Seek failed\n");
394 return 0;
397 // putchar('.');fflush(stdout);
398 //} else {
399 // putchar('%');fflush(stdout);
401 return -1;
404 int stream_seek_long(stream_t *s,off_t pos){
405 int res;
406 off_t newpos=0;
408 // if( mp_msg_test(MSGT_STREAM,MSGL_DBG3) ) printf("seek_long to 0x%X\n",(unsigned int)pos);
410 s->buf_pos=s->buf_len=0;
412 if(s->mode == STREAM_WRITE) {
413 if(!s->seek || !s->seek(s,pos))
414 return 0;
415 return 1;
418 if(s->sector_size)
419 newpos = (pos/s->sector_size)*s->sector_size;
420 else
421 newpos = pos&(~((off_t)STREAM_BUFFER_SIZE-1));
423 if( mp_msg_test(MSGT_STREAM,MSGL_DBG3) ){
424 mp_msg(MSGT_STREAM,MSGL_DBG3, "s->pos=%"PRIX64" newpos=%"PRIX64" new_bufpos=%"PRIX64" buflen=%X \n",
425 (int64_t)s->pos,(int64_t)newpos,(int64_t)pos,s->buf_len);
427 pos-=newpos;
429 res = stream_seek_internal(s, newpos);
430 if (res >= 0)
431 return res;
433 while(s->pos<newpos){
434 if(stream_fill_buffer(s)<=0) break; // EOF
437 s->eof = 0; // EOF reset when seek succeeds.
438 while (stream_fill_buffer(s) > 0) {
439 if(pos<=s->buf_len){
440 s->buf_pos=pos; // byte position in sector
441 return 1;
443 pos -= s->buf_len;
445 // Fill failed, but seek still is a success.
446 s->pos += pos;
447 s->buf_pos = 0;
448 s->buf_len = 0;
450 mp_msg(MSGT_STREAM,MSGL_V,
451 "stream_seek: Seek to/past EOF: no buffer preloaded.\n");
452 return 1;
456 void stream_reset(stream_t *s){
457 if(s->eof){
458 s->pos=0;
459 s->buf_pos=s->buf_len=0;
460 s->eof=0;
462 if(s->control) s->control(s,STREAM_CTRL_RESET,NULL);
463 //stream_seek(s,0);
466 int stream_control(stream_t *s, int cmd, void *arg){
467 if(!s->control) return STREAM_UNSUPPORTED;
468 #ifdef CONFIG_STREAM_CACHE
469 if (s->cache_pid)
470 return cache_do_control(s, cmd, arg);
471 #endif
472 return s->control(s, cmd, arg);
475 stream_t* new_memory_stream(unsigned char* data,int len){
476 stream_t *s;
478 if(len < 0)
479 return NULL;
480 s=calloc(1, sizeof(stream_t)+len);
481 s->fd=-1;
482 s->type=STREAMTYPE_MEMORY;
483 s->buf_pos=0; s->buf_len=len;
484 s->start_pos=0; s->end_pos=len;
485 stream_reset(s);
486 s->pos=len;
487 memcpy(s->buffer,data,len);
488 return s;
491 stream_t* new_stream(int fd,int type){
492 stream_t *s=calloc(1, sizeof(stream_t));
493 if(s==NULL) return NULL;
495 #if HAVE_WINSOCK2_H
497 WSADATA wsdata;
498 int temp = WSAStartup(0x0202, &wsdata); // there might be a better place for this (-> later)
499 mp_msg(MSGT_STREAM,MSGL_V,"WINSOCK2 init: %i\n", temp);
501 #endif
503 s->fd=fd;
504 s->type=type;
505 s->buf_pos=s->buf_len=0;
506 s->start_pos=s->end_pos=0;
507 s->priv=NULL;
508 s->url=NULL;
509 s->cache_pid=0;
510 stream_reset(s);
511 return s;
514 void free_stream(stream_t *s){
515 // printf("\n*** free_stream() called ***\n");
516 #ifdef CONFIG_STREAM_CACHE
517 cache_uninit(s);
518 #endif
519 if (s->capture_file) {
520 fclose(s->capture_file);
521 s->capture_file = NULL;
524 if(s->close) s->close(s);
525 if(s->fd>0){
526 /* on unix we define closesocket to close
527 on windows however we have to distinguish between
528 network socket and file */
529 if(s->url && strstr(s->url,"://"))
530 closesocket(s->fd);
531 else close(s->fd);
533 #if HAVE_WINSOCK2_H
534 mp_msg(MSGT_STREAM,MSGL_V,"WINSOCK2 uninit\n");
535 WSACleanup(); // there might be a better place for this (-> later)
536 #endif
537 // Disabled atm, i don't like that. s->priv can be anything after all
538 // streams should destroy their priv on close
539 //free(s->priv);
540 free(s->url);
541 free(s);
544 stream_t* new_ds_stream(demux_stream_t *ds) {
545 stream_t* s = new_stream(-1,STREAMTYPE_DS);
546 s->priv = ds;
547 return s;
550 void stream_set_interrupt_callback(int (*cb)(struct input_ctx *, int),
551 struct input_ctx *ctx)
553 stream_check_interrupt_cb = cb;
554 stream_check_interrupt_ctx = ctx;
557 int stream_check_interrupt(int time) {
558 if(!stream_check_interrupt_cb) {
559 usec_sleep(time * 1000);
560 return 0;
562 return stream_check_interrupt_cb(stream_check_interrupt_ctx, time);
566 * Helper function to read 16 bits little-endian and advance pointer
568 static uint16_t get_le16_inc(const uint8_t **buf)
570 uint16_t v = AV_RL16(*buf);
571 *buf += 2;
572 return v;
576 * Helper function to read 16 bits big-endian and advance pointer
578 static uint16_t get_be16_inc(const uint8_t **buf)
580 uint16_t v = AV_RB16(*buf);
581 *buf += 2;
582 return v;
586 * Find a newline character in buffer
587 * \param buf buffer to search
588 * \param len amount of bytes to search in buffer, may not overread
589 * \param utf16 chose between UTF-8/ASCII/other and LE and BE UTF-16
590 * 0 = UTF-8/ASCII/other, 1 = UTF-16-LE, 2 = UTF-16-BE
592 static const uint8_t *find_newline(const uint8_t *buf, int len, int utf16)
594 uint32_t c;
595 const uint8_t *end = buf + len;
596 switch (utf16) {
597 case 0:
598 return (uint8_t *)memchr(buf, '\n', len);
599 case 1:
600 while (buf < end - 1) {
601 GET_UTF16(c, buf < end - 1 ? get_le16_inc(&buf) : 0, return NULL;)
602 if (buf <= end && c == '\n')
603 return buf - 1;
605 break;
606 case 2:
607 while (buf < end - 1) {
608 GET_UTF16(c, buf < end - 1 ? get_be16_inc(&buf) : 0, return NULL;)
609 if (buf <= end && c == '\n')
610 return buf - 1;
612 break;
614 return NULL;
618 * Copy a number of bytes, converting to UTF-8 if input is UTF-16
619 * \param dst buffer to copy to
620 * \param dstsize size of dst buffer
621 * \param src buffer to copy from
622 * \param len amount of bytes to copy from src
623 * \param utf16 chose between UTF-8/ASCII/other and LE and BE UTF-16
624 * 0 = UTF-8/ASCII/other, 1 = UTF-16-LE, 2 = UTF-16-BE
626 static int copy_characters(uint8_t *dst, int dstsize,
627 const uint8_t *src, int *len, int utf16)
629 uint32_t c;
630 uint8_t *dst_end = dst + dstsize;
631 const uint8_t *end = src + *len;
632 switch (utf16) {
633 case 0:
634 if (*len > dstsize)
635 *len = dstsize;
636 memcpy(dst, src, *len);
637 return *len;
638 case 1:
639 while (src < end - 1 && dst_end - dst > 8) {
640 uint8_t tmp;
641 GET_UTF16(c, src < end - 1 ? get_le16_inc(&src) : 0, ;)
642 PUT_UTF8(c, tmp, *dst++ = tmp;)
644 *len -= end - src;
645 return dstsize - (dst_end - dst);
646 case 2:
647 while (src < end - 1 && dst_end - dst > 8) {
648 uint8_t tmp;
649 GET_UTF16(c, src < end - 1 ? get_be16_inc(&src) : 0, ;)
650 PUT_UTF8(c, tmp, *dst++ = tmp;)
652 *len -= end - src;
653 return dstsize - (dst_end - dst);
655 return 0;
658 unsigned char* stream_read_line(stream_t *s,unsigned char* mem, int max, int utf16) {
659 int len;
660 const unsigned char *end;
661 unsigned char *ptr = mem;
662 if (max < 1) return NULL;
663 max--; // reserve one for 0-termination
664 do {
665 len = s->buf_len-s->buf_pos;
666 // try to fill the buffer
667 if(len <= 0 &&
668 (!cache_stream_fill_buffer(s) ||
669 (len = s->buf_len-s->buf_pos) <= 0)) break;
670 end = find_newline(s->buffer+s->buf_pos, len, utf16);
671 if(end) len = end - (s->buffer+s->buf_pos) + 1;
672 if(len > 0 && max > 0) {
673 int l = copy_characters(ptr, max, s->buffer+s->buf_pos, &len, utf16);
674 max -= l;
675 ptr += l;
676 if (!len)
677 break;
679 s->buf_pos += len;
680 } while(!end);
681 ptr[0] = 0;
682 if(s->eof && ptr == mem) return NULL;
683 return mem;
686 struct bstr stream_read_complete(struct stream *s, void *talloc_ctx,
687 int max_size, int padding_bytes)
689 if (max_size > 1000000000)
690 abort();
692 int bufsize;
693 int total_read = 0;
694 int padding = FFMAX(padding_bytes, 1);
695 char *buf = NULL;
696 if (s->end_pos > max_size)
697 return (struct bstr){NULL, 0};
698 if (s->end_pos > 0)
699 bufsize = s->end_pos + padding;
700 else
701 bufsize = 1000;
702 while (1) {
703 buf = talloc_realloc_size(talloc_ctx, buf, bufsize);
704 int readsize = stream_read(s, buf + total_read, bufsize - total_read);
705 total_read += readsize;
706 if (total_read < bufsize)
707 break;
708 if (bufsize > max_size) {
709 talloc_free(buf);
710 return (struct bstr){NULL, 0};
712 bufsize = FFMIN(bufsize + (bufsize >> 1), max_size + padding);
714 buf = talloc_realloc_size(talloc_ctx, buf, total_read + padding);
715 return (struct bstr){buf, total_read};