stream.c: make some stream messages translatable
[mplayer.git] / stream / stream.c
blob4b55072561b9a74542dee68aff51a79a58285060
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 "talloc.h"
35 #include "config.h"
37 #if HAVE_WINSOCK2_H
38 #include <winsock2.h>
39 #endif
41 #include <libavutil/common.h>
43 #include "mp_msg.h"
44 #include "osdep/shmem.h"
45 #include "osdep/timer.h"
46 #include "network.h"
47 #include "stream.h"
48 #include "libmpdemux/demuxer.h"
49 #include "ffmpeg_files/intreadwrite.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_rtsp;
66 extern const stream_info_t stream_info_rtp;
67 extern const stream_info_t stream_info_udp;
68 extern const stream_info_t stream_info_http1;
69 extern const stream_info_t stream_info_http2;
70 extern const stream_info_t stream_info_dvb;
71 extern const stream_info_t stream_info_tv;
72 extern const stream_info_t stream_info_radio;
73 extern const stream_info_t stream_info_pvr;
74 extern const stream_info_t stream_info_ftp;
75 extern const stream_info_t stream_info_vstream;
76 extern const stream_info_t stream_info_dvdnav;
77 extern const stream_info_t stream_info_smb;
78 extern const stream_info_t stream_info_sdp;
79 extern const stream_info_t stream_info_rtsp_sip;
81 extern const stream_info_t stream_info_cue;
82 extern const stream_info_t stream_info_null;
83 extern const stream_info_t stream_info_mf;
84 extern const stream_info_t stream_info_ffmpeg;
85 extern const stream_info_t stream_info_file;
86 extern const stream_info_t stream_info_ifo;
87 extern const stream_info_t stream_info_dvd;
88 extern const stream_info_t stream_info_bluray;
90 static const stream_info_t* const auto_open_streams[] = {
91 #ifdef CONFIG_VCD
92 &stream_info_vcd,
93 #endif
94 #ifdef CONFIG_CDDA
95 &stream_info_cdda,
96 #endif
97 #ifdef CONFIG_NETWORKING
98 &stream_info_netstream,
99 &stream_info_http1,
100 &stream_info_asf,
101 &stream_info_pnm,
102 &stream_info_rtsp,
103 #ifdef CONFIG_LIVE555
104 &stream_info_sdp,
105 &stream_info_rtsp_sip,
106 #endif
107 &stream_info_rtp,
108 &stream_info_udp,
109 &stream_info_http2,
110 #endif
111 #ifdef CONFIG_DVBIN
112 &stream_info_dvb,
113 #endif
114 #ifdef CONFIG_TV
115 &stream_info_tv,
116 #endif
117 #ifdef CONFIG_RADIO
118 &stream_info_radio,
119 #endif
120 #ifdef CONFIG_PVR
121 &stream_info_pvr,
122 #endif
123 #ifdef CONFIG_FTP
124 &stream_info_ftp,
125 #endif
126 #ifdef CONFIG_VSTREAM
127 &stream_info_vstream,
128 #endif
129 #ifdef CONFIG_LIBSMBCLIENT
130 &stream_info_smb,
131 #endif
132 &stream_info_cue,
133 #ifdef CONFIG_DVDREAD
134 &stream_info_ifo,
135 &stream_info_dvd,
136 #endif
137 #ifdef CONFIG_DVDNAV
138 &stream_info_dvdnav,
139 #endif
140 #ifdef CONFIG_LIBBLURAY
141 &stream_info_bluray,
142 #endif
143 #ifdef CONFIG_FFMPEG
144 &stream_info_ffmpeg,
145 #endif
147 &stream_info_null,
148 &stream_info_mf,
149 &stream_info_file,
150 NULL
153 static stream_t *open_stream_plugin(const stream_info_t *sinfo,
154 const char *filename,
155 int mode, struct MPOpts *options,
156 int *file_format, int *ret,
157 char **redirected_url)
159 void* arg = NULL;
160 stream_t* s;
161 m_struct_t* desc = (m_struct_t*)sinfo->opts;
163 // Parse options
164 if(desc) {
165 arg = m_struct_alloc(desc);
166 if(sinfo->opts_url) {
167 m_option_t url_opt =
168 { "stream url", arg , CONF_TYPE_CUSTOM_URL, 0, 0 ,0, (void *)sinfo->opts };
169 if(m_option_parse(&url_opt,"stream url",filename,arg,M_CONFIG_FILE) < 0) {
170 mp_tmsg(MSGT_OPEN,MSGL_ERR, "URL parsing failed on url %s\n",filename);
171 m_struct_free(desc,arg);
172 return NULL;
176 s = new_stream(-2,-2);
177 s->opts = options;
178 s->url=strdup(filename);
179 s->flags |= mode;
180 *ret = sinfo->open(s,mode,arg,file_format);
181 if((*ret) != STREAM_OK) {
182 #ifdef CONFIG_NETWORKING
183 if (*ret == STREAM_REDIRECTED && redirected_url) {
184 if (s->streaming_ctrl && s->streaming_ctrl->url
185 && s->streaming_ctrl->url->url)
186 *redirected_url = strdup(s->streaming_ctrl->url->url);
187 else
188 *redirected_url = NULL;
190 streaming_ctrl_free(s->streaming_ctrl);
191 #endif
192 free(s->url);
193 free(s);
194 return NULL;
196 if(s->type <= -2)
197 mp_msg(MSGT_OPEN,MSGL_WARN, "Warning streams need a type !!!!\n");
198 if(s->flags & MP_STREAM_SEEK && !s->seek)
199 s->flags &= ~MP_STREAM_SEEK;
200 if(s->seek && !(s->flags & MP_STREAM_SEEK))
201 s->flags |= MP_STREAM_SEEK;
203 s->mode = mode;
205 mp_msg(MSGT_OPEN,MSGL_V, "STREAM: [%s] %s\n",sinfo->name,filename);
206 mp_msg(MSGT_OPEN,MSGL_V, "STREAM: Description: %s\n",sinfo->info);
207 mp_msg(MSGT_OPEN,MSGL_V, "STREAM: Author: %s\n", sinfo->author);
208 mp_msg(MSGT_OPEN,MSGL_V, "STREAM: Comment: %s\n", sinfo->comment);
210 return s;
214 stream_t *open_stream_full(const char *filename, int mode,
215 struct MPOpts *options, int *file_format)
217 int i,j,l,r;
218 const stream_info_t* sinfo;
219 stream_t* s;
220 char *redirected_url = NULL;
222 for(i = 0 ; auto_open_streams[i] ; i++) {
223 sinfo = auto_open_streams[i];
224 if(!sinfo->protocols) {
225 mp_msg(MSGT_OPEN,MSGL_WARN, "Stream type %s has protocols == NULL, it's a bug\n", sinfo->name);
226 continue;
228 for(j = 0 ; sinfo->protocols[j] ; j++) {
229 l = strlen(sinfo->protocols[j]);
230 // l == 0 => Don't do protocol matching (ie network and filenames)
231 if((l == 0 && !strstr(filename, "://")) ||
232 ((strncasecmp(sinfo->protocols[j],filename,l) == 0) &&
233 (strncmp("://",filename+l,3) == 0))) {
234 *file_format = DEMUXER_TYPE_UNKNOWN;
235 s = open_stream_plugin(sinfo,filename,mode,options,file_format,&r,
236 &redirected_url);
237 if(s) return s;
238 if(r == STREAM_REDIRECTED && redirected_url) {
239 mp_msg(MSGT_OPEN,MSGL_V, "[%s] open %s redirected to %s\n",
240 sinfo->info, filename, redirected_url);
241 s = open_stream_full(redirected_url, mode, options, file_format);
242 free(redirected_url);
243 return s;
245 else if(r != STREAM_UNSUPPORTED) {
246 mp_tmsg(MSGT_OPEN,MSGL_ERR, "Failed to open %s.\n",filename);
247 return NULL;
249 break;
254 mp_tmsg(MSGT_OPEN,MSGL_ERR, "No stream found to handle url %s\n", filename);
255 return NULL;
258 stream_t *open_output_stream(const char *filename, struct MPOpts *options)
260 int file_format; //unused
261 if(!filename) {
262 mp_msg(MSGT_OPEN,MSGL_ERR,"open_output_stream(), NULL filename, report this bug\n");
263 return NULL;
266 return open_stream_full(filename,STREAM_WRITE,options,&file_format);
269 //=================== STREAMER =========================
271 void stream_capture_do(stream_t *s)
273 if (fwrite(s->buffer, s->buf_len, 1, s->capture_file) < 1) {
274 mp_tmsg(MSGT_GLOBAL, MSGL_ERR, "Error writing capture file: %s\n",
275 strerror(errno));
276 fclose(s->capture_file);
277 s->capture_file = NULL;
281 int stream_read_internal(stream_t *s, void *buf, int len)
283 int orig_len = len;
284 // we will retry even if we already reached EOF previously.
285 switch(s->type){
286 case STREAMTYPE_STREAM:
287 #ifdef CONFIG_NETWORKING
288 if( s->streaming_ctrl!=NULL && s->streaming_ctrl->streaming_read ) {
289 len=s->streaming_ctrl->streaming_read(s->fd, buf, len, s->streaming_ctrl);
290 } else
291 #endif
292 if (s->fill_buffer)
293 len = s->fill_buffer(s, buf, len);
294 else
295 len = read(s->fd, buf, len);
296 break;
297 case STREAMTYPE_DS:
298 len = demux_read_data((demux_stream_t*)s->priv, buf, len);
299 break;
302 default:
303 len= s->fill_buffer ? s->fill_buffer(s, buf, len) : 0;
305 if(len<=0){
306 // dvdnav has some horrible hacks to "suspend" reads,
307 // we need to skip this code or seeks will hang.
308 if (!s->eof && s->type != STREAMTYPE_DVDNAV) {
309 // just in case this is an error e.g. due to network
310 // timeout reset and retry
311 // Seeking is used as a hack to make network streams
312 // reopen the connection, ideally they would implement
313 // e.g. a STREAM_CTRL_RECONNECT to do this
314 off_t pos = s->pos;
315 s->eof=1;
316 stream_reset(s);
317 stream_seek_internal(s, pos);
318 // make sure EOF is set to ensure no endless loops
319 s->eof=1;
320 return stream_read_internal(s, buf, orig_len);
322 s->eof=1;
323 return 0;
325 // When reading succeeded we are obviously not at eof.
326 // This e.g. avoids issues with eof getting stuck when lavf seeks in MPEG-TS
327 s->eof=0;
328 s->pos+=len;
329 return len;
332 int stream_fill_buffer(stream_t *s){
333 int len = stream_read_internal(s, s->buffer, STREAM_BUFFER_SIZE);
334 if (len <= 0)
335 return 0;
336 s->buf_pos=0;
337 s->buf_len=len;
338 // printf("[%d]",len);fflush(stdout);
339 if (s->capture_file)
340 stream_capture_do(s);
341 return len;
344 int stream_write_buffer(stream_t *s, unsigned char *buf, int len) {
345 int rd;
346 if(!s->write_buffer)
347 return -1;
348 rd = s->write_buffer(s, buf, len);
349 if(rd < 0)
350 return -1;
351 s->pos += rd;
352 assert(rd == len && "stream_write_buffer(): unexpected short write");
353 return rd;
356 int stream_seek_internal(stream_t *s, off_t newpos)
358 if(newpos==0 || newpos!=s->pos){
359 switch(s->type){
360 case STREAMTYPE_STREAM:
361 //s->pos=newpos; // real seek
362 // Some streaming protocol allow to seek backward and forward
363 // A function call that return -1 can tell that the protocol
364 // doesn't support seeking.
365 #ifdef CONFIG_NETWORKING
366 if(s->seek) { // new stream seek is much cleaner than streaming_ctrl one
367 if(!s->seek(s,newpos)) {
368 mp_tmsg(MSGT_STREAM,MSGL_ERR, "Seek failed\n");
369 return 0;
371 break;
374 if( s->streaming_ctrl!=NULL && s->streaming_ctrl->streaming_seek ) {
375 if( s->streaming_ctrl->streaming_seek( s->fd, newpos, s->streaming_ctrl )<0 ) {
376 mp_tmsg(MSGT_STREAM,MSGL_INFO,"Stream not seekable!\n");
377 return 1;
379 break;
381 #endif
382 if(newpos<s->pos){
383 mp_tmsg(MSGT_STREAM, MSGL_INFO,
384 "Cannot seek backward in linear streams!\n");
385 return 1;
387 break;
388 default:
389 // This should at the beginning as soon as all streams are converted
390 if(!s->seek)
391 return 0;
392 // Now seek
393 if(!s->seek(s,newpos)) {
394 mp_tmsg(MSGT_STREAM,MSGL_ERR, "Seek failed\n");
395 return 0;
398 // putchar('.');fflush(stdout);
399 //} else {
400 // putchar('%');fflush(stdout);
402 return -1;
405 int stream_seek_long(stream_t *s,off_t pos){
406 int res;
407 off_t newpos=0;
409 // if( mp_msg_test(MSGT_STREAM,MSGL_DBG3) ) printf("seek_long to 0x%X\n",(unsigned int)pos);
411 s->buf_pos=s->buf_len=0;
413 if(s->mode == STREAM_WRITE) {
414 if(!s->seek || !s->seek(s,pos))
415 return 0;
416 return 1;
419 if(s->sector_size)
420 newpos = (pos/s->sector_size)*s->sector_size;
421 else
422 newpos = pos&(~((off_t)STREAM_BUFFER_SIZE-1));
424 if( mp_msg_test(MSGT_STREAM,MSGL_DBG3) ){
425 mp_msg(MSGT_STREAM,MSGL_DBG3, "s->pos=%"PRIX64" newpos=%"PRIX64" new_bufpos=%"PRIX64" buflen=%X \n",
426 (int64_t)s->pos,(int64_t)newpos,(int64_t)pos,s->buf_len);
428 pos-=newpos;
430 res = stream_seek_internal(s, newpos);
431 if (res >= 0)
432 return res;
434 while(s->pos<newpos){
435 if(stream_fill_buffer(s)<=0) break; // EOF
438 s->eof = 0; // EOF reset when seek succeeds.
439 while (stream_fill_buffer(s) > 0) {
440 if(pos<=s->buf_len){
441 s->buf_pos=pos; // byte position in sector
442 return 1;
444 pos -= s->buf_len;
446 // Fill failed, but seek still is a success.
447 s->pos += pos;
448 s->buf_pos = 0;
449 s->buf_len = 0;
451 mp_msg(MSGT_STREAM,MSGL_V,
452 "stream_seek: Seek to/past EOF: no buffer preloaded.\n");
453 return 1;
457 void stream_reset(stream_t *s){
458 if(s->eof){
459 s->pos=0;
460 s->buf_pos=s->buf_len=0;
461 s->eof=0;
463 if(s->control) s->control(s,STREAM_CTRL_RESET,NULL);
464 //stream_seek(s,0);
467 int stream_control(stream_t *s, int cmd, void *arg){
468 if(!s->control) return STREAM_UNSUPPORTED;
469 #ifdef CONFIG_STREAM_CACHE
470 if (s->cache_pid)
471 return cache_do_control(s, cmd, arg);
472 #endif
473 return s->control(s, cmd, arg);
476 stream_t* new_memory_stream(unsigned char* data,int len){
477 stream_t *s;
479 if(len < 0)
480 return NULL;
481 s=calloc(1, sizeof(stream_t)+len);
482 s->fd=-1;
483 s->type=STREAMTYPE_MEMORY;
484 s->buf_pos=0; s->buf_len=len;
485 s->start_pos=0; s->end_pos=len;
486 stream_reset(s);
487 s->pos=len;
488 memcpy(s->buffer,data,len);
489 return s;
492 stream_t* new_stream(int fd,int type){
493 stream_t *s=calloc(1, sizeof(stream_t));
494 if(s==NULL) return NULL;
496 #if HAVE_WINSOCK2_H
498 WSADATA wsdata;
499 int temp = WSAStartup(0x0202, &wsdata); // there might be a better place for this (-> later)
500 mp_msg(MSGT_STREAM,MSGL_V,"WINSOCK2 init: %i\n", temp);
502 #endif
504 s->fd=fd;
505 s->type=type;
506 s->buf_pos=s->buf_len=0;
507 s->start_pos=s->end_pos=0;
508 s->priv=NULL;
509 s->url=NULL;
510 s->cache_pid=0;
511 stream_reset(s);
512 return s;
515 void free_stream(stream_t *s){
516 // printf("\n*** free_stream() called ***\n");
517 #ifdef CONFIG_STREAM_CACHE
518 cache_uninit(s);
519 #endif
520 if (s->capture_file) {
521 fclose(s->capture_file);
522 s->capture_file = NULL;
525 if(s->close) s->close(s);
526 if(s->fd>0){
527 /* on unix we define closesocket to close
528 on windows however we have to distinguish between
529 network socket and file */
530 if(s->url && strstr(s->url,"://"))
531 closesocket(s->fd);
532 else close(s->fd);
534 #if HAVE_WINSOCK2_H
535 mp_msg(MSGT_STREAM,MSGL_V,"WINSOCK2 uninit\n");
536 WSACleanup(); // there might be a better place for this (-> later)
537 #endif
538 // Disabled atm, i don't like that. s->priv can be anything after all
539 // streams should destroy their priv on close
540 //free(s->priv);
541 free(s->url);
542 free(s);
545 stream_t* new_ds_stream(demux_stream_t *ds) {
546 stream_t* s = new_stream(-1,STREAMTYPE_DS);
547 s->priv = ds;
548 return s;
551 void stream_set_interrupt_callback(int (*cb)(struct input_ctx *, int),
552 struct input_ctx *ctx)
554 stream_check_interrupt_cb = cb;
555 stream_check_interrupt_ctx = ctx;
558 int stream_check_interrupt(int time) {
559 if(!stream_check_interrupt_cb) {
560 usec_sleep(time * 1000);
561 return 0;
563 return stream_check_interrupt_cb(stream_check_interrupt_ctx, time);
567 * Helper function to read 16 bits little-endian and advance pointer
569 static uint16_t get_le16_inc(const uint8_t **buf)
571 uint16_t v = AV_RL16(*buf);
572 *buf += 2;
573 return v;
577 * Helper function to read 16 bits big-endian and advance pointer
579 static uint16_t get_be16_inc(const uint8_t **buf)
581 uint16_t v = AV_RB16(*buf);
582 *buf += 2;
583 return v;
587 * Find a newline character in buffer
588 * \param buf buffer to search
589 * \param len amount of bytes to search in buffer, may not overread
590 * \param utf16 chose between UTF-8/ASCII/other and LE and BE UTF-16
591 * 0 = UTF-8/ASCII/other, 1 = UTF-16-LE, 2 = UTF-16-BE
593 static const uint8_t *find_newline(const uint8_t *buf, int len, int utf16)
595 uint32_t c;
596 const uint8_t *end = buf + len;
597 switch (utf16) {
598 case 0:
599 return (uint8_t *)memchr(buf, '\n', len);
600 case 1:
601 while (buf < end - 1) {
602 GET_UTF16(c, buf < end - 1 ? get_le16_inc(&buf) : 0, return NULL;)
603 if (buf <= end && c == '\n')
604 return buf - 1;
606 break;
607 case 2:
608 while (buf < end - 1) {
609 GET_UTF16(c, buf < end - 1 ? get_be16_inc(&buf) : 0, return NULL;)
610 if (buf <= end && c == '\n')
611 return buf - 1;
613 break;
615 return NULL;
619 * Copy a number of bytes, converting to UTF-8 if input is UTF-16
620 * \param dst buffer to copy to
621 * \param dstsize size of dst buffer
622 * \param src buffer to copy from
623 * \param len amount of bytes to copy from src
624 * \param utf16 chose between UTF-8/ASCII/other and LE and BE UTF-16
625 * 0 = UTF-8/ASCII/other, 1 = UTF-16-LE, 2 = UTF-16-BE
627 static int copy_characters(uint8_t *dst, int dstsize,
628 const uint8_t *src, int *len, int utf16)
630 uint32_t c;
631 uint8_t *dst_end = dst + dstsize;
632 const uint8_t *end = src + *len;
633 switch (utf16) {
634 case 0:
635 if (*len > dstsize)
636 *len = dstsize;
637 memcpy(dst, src, *len);
638 return *len;
639 case 1:
640 while (src < end - 1 && dst_end - dst > 8) {
641 uint8_t tmp;
642 GET_UTF16(c, src < end - 1 ? get_le16_inc(&src) : 0, ;)
643 PUT_UTF8(c, tmp, *dst++ = tmp;)
645 *len -= end - src;
646 return dstsize - (dst_end - dst);
647 case 2:
648 while (src < end - 1 && dst_end - dst > 8) {
649 uint8_t tmp;
650 GET_UTF16(c, src < end - 1 ? get_be16_inc(&src) : 0, ;)
651 PUT_UTF8(c, tmp, *dst++ = tmp;)
653 *len -= end - src;
654 return dstsize - (dst_end - dst);
656 return 0;
659 unsigned char* stream_read_line(stream_t *s,unsigned char* mem, int max, int utf16) {
660 int len;
661 const unsigned char *end;
662 unsigned char *ptr = mem;
663 if (max < 1) return NULL;
664 max--; // reserve one for 0-termination
665 do {
666 len = s->buf_len-s->buf_pos;
667 // try to fill the buffer
668 if(len <= 0 &&
669 (!cache_stream_fill_buffer(s) ||
670 (len = s->buf_len-s->buf_pos) <= 0)) break;
671 end = find_newline(s->buffer+s->buf_pos, len, utf16);
672 if(end) len = end - (s->buffer+s->buf_pos) + 1;
673 if(len > 0 && max > 0) {
674 int l = copy_characters(ptr, max, s->buffer+s->buf_pos, &len, utf16);
675 max -= l;
676 ptr += l;
677 if (!len)
678 break;
680 s->buf_pos += len;
681 } while(!end);
682 ptr[0] = 0;
683 if(s->eof && ptr == mem) return NULL;
684 return mem;
687 struct bstr stream_read_complete(struct stream *s, void *talloc_ctx,
688 int max_size, int padding_bytes)
690 if (max_size > 1000000000)
691 abort();
693 int bufsize;
694 int total_read = 0;
695 int padding = FFMAX(padding_bytes, 1);
696 char *buf = NULL;
697 if (s->end_pos > max_size)
698 return (struct bstr){NULL, 0};
699 if (s->end_pos > 0)
700 bufsize = s->end_pos + padding;
701 else
702 bufsize = 1000;
703 while (1) {
704 buf = talloc_realloc_size(talloc_ctx, buf, bufsize);
705 int readsize = stream_read(s, buf + total_read, bufsize - total_read);
706 total_read += readsize;
707 if (total_read < bufsize)
708 break;
709 if (bufsize > max_size) {
710 talloc_free(buf);
711 return (struct bstr){NULL, 0};
713 bufsize = FFMIN(bufsize + (bufsize >> 1), max_size + padding);
715 buf = talloc_realloc_size(talloc_ctx, buf, total_read + padding);
716 return (struct bstr){buf, total_read};