Fix vf_tcdump's compilation
[mplayer/kovensky.git] / stream / stream.c
blob890a12f41cf5a77ab130dfa4c42b42506976c156
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>
32 #include "config.h"
34 #if HAVE_WINSOCK2_H
35 #include <winsock2.h>
36 #endif
38 #include "mp_msg.h"
39 #include "osdep/shmem.h"
40 #include "network.h"
41 #include "stream.h"
42 #include "libmpdemux/demuxer.h"
43 #include "ffmpeg_files/intreadwrite.h"
45 #include "m_option.h"
46 #include "m_struct.h"
48 #include "cache2.h"
50 //#include "vcd_read_bincue.h"
52 struct input_ctx;
53 static int (*stream_check_interrupt_cb)(struct input_ctx *ctx, int time);
54 static struct input_ctx *stream_check_interrupt_ctx;
56 extern const stream_info_t stream_info_vcd;
57 extern const stream_info_t stream_info_cdda;
58 extern const stream_info_t stream_info_netstream;
59 extern const stream_info_t stream_info_pnm;
60 extern const stream_info_t stream_info_asf;
61 extern const stream_info_t stream_info_rtsp;
62 extern const stream_info_t stream_info_rtp;
63 extern const stream_info_t stream_info_udp;
64 extern const stream_info_t stream_info_http1;
65 extern const stream_info_t stream_info_http2;
66 extern const stream_info_t stream_info_dvb;
67 extern const stream_info_t stream_info_tv;
68 extern const stream_info_t stream_info_radio;
69 extern const stream_info_t stream_info_pvr;
70 extern const stream_info_t stream_info_ftp;
71 extern const stream_info_t stream_info_vstream;
72 extern const stream_info_t stream_info_dvdnav;
73 extern const stream_info_t stream_info_smb;
74 extern const stream_info_t stream_info_sdp;
75 extern const stream_info_t stream_info_rtsp_sip;
77 extern const stream_info_t stream_info_cue;
78 extern const stream_info_t stream_info_null;
79 extern const stream_info_t stream_info_mf;
80 extern const stream_info_t stream_info_ffmpeg;
81 extern const stream_info_t stream_info_file;
82 extern const stream_info_t stream_info_ifo;
83 extern const stream_info_t stream_info_dvd;
85 static const stream_info_t* const auto_open_streams[] = {
86 #ifdef CONFIG_VCD
87 &stream_info_vcd,
88 #endif
89 #ifdef CONFIG_CDDA
90 &stream_info_cdda,
91 #endif
92 #ifdef CONFIG_NETWORK
93 &stream_info_netstream,
94 &stream_info_http1,
95 &stream_info_asf,
96 &stream_info_pnm,
97 &stream_info_rtsp,
98 #ifdef CONFIG_LIVE555
99 &stream_info_sdp,
100 &stream_info_rtsp_sip,
101 #endif
102 &stream_info_rtp,
103 &stream_info_udp,
104 &stream_info_http2,
105 #endif
106 #ifdef CONFIG_DVBIN
107 &stream_info_dvb,
108 #endif
109 #ifdef CONFIG_TV
110 &stream_info_tv,
111 #endif
112 #ifdef CONFIG_RADIO
113 &stream_info_radio,
114 #endif
115 #ifdef CONFIG_PVR
116 &stream_info_pvr,
117 #endif
118 #ifdef CONFIG_FTP
119 &stream_info_ftp,
120 #endif
121 #ifdef CONFIG_VSTREAM
122 &stream_info_vstream,
123 #endif
124 #ifdef CONFIG_LIBSMBCLIENT
125 &stream_info_smb,
126 #endif
127 &stream_info_cue,
128 #ifdef CONFIG_DVDREAD
129 &stream_info_ifo,
130 &stream_info_dvd,
131 #endif
132 #ifdef CONFIG_DVDNAV
133 &stream_info_dvdnav,
134 #endif
135 #ifdef CONFIG_LIBAVFORMAT
136 &stream_info_ffmpeg,
137 #endif
139 &stream_info_null,
140 &stream_info_mf,
141 &stream_info_file,
142 NULL
145 static stream_t *open_stream_plugin(const stream_info_t *sinfo,
146 const char *filename,
147 int mode, struct MPOpts *options,
148 int *file_format, int *ret,
149 char **redirected_url)
151 void* arg = NULL;
152 stream_t* s;
153 m_struct_t* desc = (m_struct_t*)sinfo->opts;
155 // Parse options
156 if(desc) {
157 arg = m_struct_alloc(desc);
158 if(sinfo->opts_url) {
159 m_option_t url_opt =
160 { "stream url", arg , CONF_TYPE_CUSTOM_URL, 0, 0 ,0, sinfo->opts };
161 if(m_option_parse(&url_opt,"stream url",filename,arg,M_CONFIG_FILE) < 0) {
162 mp_msg(MSGT_OPEN,MSGL_ERR, "URL parsing failed on url %s\n",filename);
163 m_struct_free(desc,arg);
164 return NULL;
168 s = new_stream(-2,-2);
169 s->opts = options;
170 s->url=strdup(filename);
171 s->flags |= mode;
172 *ret = sinfo->open(s,mode,arg,file_format);
173 if((*ret) != STREAM_OK) {
174 #ifdef CONFIG_NETWORK
175 if (*ret == STREAM_REDIRECTED && redirected_url) {
176 if (s->streaming_ctrl && s->streaming_ctrl->url
177 && s->streaming_ctrl->url->url)
178 *redirected_url = strdup(s->streaming_ctrl->url->url);
179 else
180 *redirected_url = NULL;
182 streaming_ctrl_free(s->streaming_ctrl);
183 #endif
184 free(s->url);
185 free(s);
186 return NULL;
188 if(s->type <= -2)
189 mp_msg(MSGT_OPEN,MSGL_WARN, "Warning streams need a type !!!!\n");
190 if(s->flags & MP_STREAM_SEEK && !s->seek)
191 s->flags &= ~MP_STREAM_SEEK;
192 if(s->seek && !(s->flags & MP_STREAM_SEEK))
193 s->flags |= MP_STREAM_SEEK;
195 s->mode = mode;
197 mp_msg(MSGT_OPEN,MSGL_V, "STREAM: [%s] %s\n",sinfo->name,filename);
198 mp_msg(MSGT_OPEN,MSGL_V, "STREAM: Description: %s\n",sinfo->info);
199 mp_msg(MSGT_OPEN,MSGL_V, "STREAM: Author: %s\n", sinfo->author);
200 mp_msg(MSGT_OPEN,MSGL_V, "STREAM: Comment: %s\n", sinfo->comment);
202 return s;
206 stream_t *open_stream_full(const char *filename, int mode,
207 struct MPOpts *options, int *file_format)
209 int i,j,l,r;
210 const stream_info_t* sinfo;
211 stream_t* s;
212 char *redirected_url = NULL;
214 for(i = 0 ; auto_open_streams[i] ; i++) {
215 sinfo = auto_open_streams[i];
216 if(!sinfo->protocols) {
217 mp_msg(MSGT_OPEN,MSGL_WARN, "Stream type %s has protocols == NULL, it's a bug\n", sinfo->name);
218 continue;
220 for(j = 0 ; sinfo->protocols[j] ; j++) {
221 l = strlen(sinfo->protocols[j]);
222 // l == 0 => Don't do protocol matching (ie network and filenames)
223 if((l == 0 && !strstr(filename, "://")) ||
224 ((strncasecmp(sinfo->protocols[j],filename,l) == 0) &&
225 (strncmp("://",filename+l,3) == 0))) {
226 *file_format = DEMUXER_TYPE_UNKNOWN;
227 s = open_stream_plugin(sinfo,filename,mode,options,file_format,&r,
228 &redirected_url);
229 if(s) return s;
230 if(r == STREAM_REDIRECTED && redirected_url) {
231 mp_msg(MSGT_OPEN,MSGL_V, "[%s] open %s redirected to %s\n",
232 sinfo->info, filename, redirected_url);
233 s = open_stream_full(redirected_url, mode, options, file_format);
234 free(redirected_url);
235 return s;
237 else if(r != STREAM_UNSUPPORTED) {
238 mp_tmsg(MSGT_OPEN,MSGL_ERR, "Failed to open %s.\n",filename);
239 return NULL;
241 break;
246 mp_msg(MSGT_OPEN,MSGL_ERR, "No stream found to handle url %s\n",filename);
247 return NULL;
250 stream_t *open_output_stream(const char *filename, struct MPOpts *options)
252 int file_format; //unused
253 if(!filename) {
254 mp_msg(MSGT_OPEN,MSGL_ERR,"open_output_stream(), NULL filename, report this bug\n");
255 return NULL;
258 return open_stream_full(filename,STREAM_WRITE,options,&file_format);
261 //=================== STREAMER =========================
263 int stream_fill_buffer(stream_t *s){
264 int len;
265 if (/*s->fd == NULL ||*/ s->eof) { return 0; }
266 switch(s->type){
267 case STREAMTYPE_STREAM:
268 #ifdef CONFIG_NETWORK
269 if( s->streaming_ctrl!=NULL && s->streaming_ctrl->streaming_read ) {
270 len=s->streaming_ctrl->streaming_read(s->fd,s->buffer,STREAM_BUFFER_SIZE, s->streaming_ctrl);
271 } else
272 #endif
273 if (s->fill_buffer)
274 len = s->fill_buffer(s, s->buffer, STREAM_BUFFER_SIZE);
275 else
276 len=read(s->fd,s->buffer,STREAM_BUFFER_SIZE);
277 break;
278 case STREAMTYPE_DS:
279 len = demux_read_data((demux_stream_t*)s->priv,s->buffer,STREAM_BUFFER_SIZE);
280 break;
283 default:
284 len= s->fill_buffer ? s->fill_buffer(s,s->buffer,STREAM_BUFFER_SIZE) : 0;
286 if(len<=0){ s->eof=1; return 0; }
287 s->buf_pos=0;
288 s->buf_len=len;
289 s->pos+=len;
290 // printf("[%d]",len);fflush(stdout);
291 return len;
294 int stream_write_buffer(stream_t *s, unsigned char *buf, int len) {
295 int rd;
296 if(!s->write_buffer)
297 return -1;
298 rd = s->write_buffer(s, buf, len);
299 if(rd < 0)
300 return -1;
301 s->pos += rd;
302 return rd;
305 int stream_seek_long(stream_t *s,off_t pos){
306 off_t newpos=0;
308 // if( mp_msg_test(MSGT_STREAM,MSGL_DBG3) ) printf("seek_long to 0x%X\n",(unsigned int)pos);
310 s->buf_pos=s->buf_len=0;
312 if(s->mode == STREAM_WRITE) {
313 if(!s->seek || !s->seek(s,pos))
314 return 0;
315 return 1;
318 if(s->sector_size)
319 newpos = (pos/s->sector_size)*s->sector_size;
320 else
321 newpos = pos&(~((off_t)STREAM_BUFFER_SIZE-1));
323 if( mp_msg_test(MSGT_STREAM,MSGL_DBG3) ){
324 mp_msg(MSGT_STREAM,MSGL_DBG3, "s->pos=%"PRIX64" newpos=%"PRIX64" new_bufpos=%"PRIX64" buflen=%X \n",
325 (int64_t)s->pos,(int64_t)newpos,(int64_t)pos,s->buf_len);
327 pos-=newpos;
329 if(newpos==0 || newpos!=s->pos){
330 switch(s->type){
331 case STREAMTYPE_STREAM:
332 //s->pos=newpos; // real seek
333 // Some streaming protocol allow to seek backward and forward
334 // A function call that return -1 can tell that the protocol
335 // doesn't support seeking.
336 #ifdef CONFIG_NETWORK
337 if(s->seek) { // new stream seek is much cleaner than streaming_ctrl one
338 if(!s->seek(s,newpos)) {
339 mp_msg(MSGT_STREAM,MSGL_ERR, "Seek failed\n");
340 return 0;
342 break;
345 if( s->streaming_ctrl!=NULL && s->streaming_ctrl->streaming_seek ) {
346 if( s->streaming_ctrl->streaming_seek( s->fd, pos, s->streaming_ctrl )<0 ) {
347 mp_msg(MSGT_STREAM,MSGL_INFO,"Stream not seekable!\n");
348 return 1;
350 break;
352 #endif
353 if(newpos<s->pos){
354 mp_msg(MSGT_STREAM,MSGL_INFO,"Cannot seek backward in linear streams!\n");
355 return 1;
357 while(s->pos<newpos){
358 if(stream_fill_buffer(s)<=0) break; // EOF
360 break;
361 default:
362 // This should at the beginning as soon as all streams are converted
363 if(!s->seek)
364 return 0;
365 // Now seek
366 if(!s->seek(s,newpos)) {
367 mp_msg(MSGT_STREAM,MSGL_ERR, "Seek failed\n");
368 return 0;
371 // putchar('.');fflush(stdout);
372 //} else {
373 // putchar('%');fflush(stdout);
376 s->eof = 0; // EOF reset when seek succeeds.
377 while (stream_fill_buffer(s) > 0) {
378 if(pos<=s->buf_len){
379 s->buf_pos=pos; // byte position in sector
380 return 1;
382 pos -= s->buf_len;
384 // Fill failed, but seek still is a success.
385 s->pos += pos;
386 s->buf_pos = 0;
387 s->buf_len = 0;
389 mp_msg(MSGT_STREAM,MSGL_V,
390 "stream_seek: Seek to/past EOF: no buffer preloaded.\n");
391 return 1;
395 void stream_reset(stream_t *s){
396 if(s->eof){
397 s->pos=0;
398 s->buf_pos=s->buf_len=0;
399 s->eof=0;
401 if(s->control) s->control(s,STREAM_CTRL_RESET,NULL);
402 //stream_seek(s,0);
405 int stream_control(stream_t *s, int cmd, void *arg){
406 if(!s->control) return STREAM_UNSUPPORTED;
407 #ifdef CONFIG_STREAM_CACHE
408 if (s->cache_pid)
409 return cache_do_control(s, cmd, arg);
410 #endif
411 return s->control(s, cmd, arg);
414 stream_t* new_memory_stream(unsigned char* data,int len){
415 stream_t *s;
417 if(len < 0)
418 return NULL;
419 s=calloc(1, sizeof(stream_t)+len);
420 s->fd=-1;
421 s->type=STREAMTYPE_MEMORY;
422 s->buf_pos=0; s->buf_len=len;
423 s->start_pos=0; s->end_pos=len;
424 stream_reset(s);
425 s->pos=len;
426 memcpy(s->buffer,data,len);
427 return s;
430 stream_t* new_stream(int fd,int type){
431 stream_t *s=calloc(1, sizeof(stream_t));
432 if(s==NULL) return NULL;
434 #if HAVE_WINSOCK2_H
436 WSADATA wsdata;
437 int temp = WSAStartup(0x0202, &wsdata); // there might be a better place for this (-> later)
438 mp_msg(MSGT_STREAM,MSGL_V,"WINSOCK2 init: %i\n", temp);
440 #endif
442 s->fd=fd;
443 s->type=type;
444 s->buf_pos=s->buf_len=0;
445 s->start_pos=s->end_pos=0;
446 s->priv=NULL;
447 s->url=NULL;
448 s->cache_pid=0;
449 stream_reset(s);
450 return s;
453 void free_stream(stream_t *s){
454 // printf("\n*** free_stream() called ***\n");
455 #ifdef CONFIG_STREAM_CACHE
456 cache_uninit(s);
457 #endif
458 if(s->close) s->close(s);
459 if(s->fd>0){
460 /* on unix we define closesocket to close
461 on windows however we have to distinguish between
462 network socket and file */
463 if(s->url && strstr(s->url,"://"))
464 closesocket(s->fd);
465 else close(s->fd);
467 #if HAVE_WINSOCK2_H
468 mp_msg(MSGT_STREAM,MSGL_V,"WINSOCK2 uninit\n");
469 WSACleanup(); // there might be a better place for this (-> later)
470 #endif
471 // Disabled atm, i don't like that. s->priv can be anything after all
472 // streams should destroy their priv on close
473 //if(s->priv) free(s->priv);
474 if(s->url) free(s->url);
475 free(s);
478 stream_t* new_ds_stream(demux_stream_t *ds) {
479 stream_t* s = new_stream(-1,STREAMTYPE_DS);
480 s->priv = ds;
481 return s;
484 void stream_set_interrupt_callback(int (*cb)(struct input_ctx *, int),
485 struct input_ctx *ctx)
487 stream_check_interrupt_cb = cb;
488 stream_check_interrupt_ctx = ctx;
491 int stream_check_interrupt(int time) {
492 if(!stream_check_interrupt_cb) return 0;
493 return stream_check_interrupt_cb(stream_check_interrupt_ctx, time);
497 * Helper function to read 16 bits little-endian and advance pointer
499 static uint16_t get_le16_inc(const uint8_t **buf)
501 uint16_t v = AV_RL16(*buf);
502 *buf += 2;
503 return v;
507 * Helper function to read 16 bits big-endian and advance pointer
509 static uint16_t get_be16_inc(const uint8_t **buf)
511 uint16_t v = AV_RB16(*buf);
512 *buf += 2;
513 return v;
517 * Find a newline character in buffer
518 * \param buf buffer to search
519 * \param len amount of bytes to search in buffer, may not overread
520 * \param utf16 chose between UTF-8/ASCII/other and LE and BE UTF-16
521 * 0 = UTF-8/ASCII/other, 1 = UTF-16-LE, 2 = UTF-16-BE
523 static const uint8_t *find_newline(const uint8_t *buf, int len, int utf16)
525 uint32_t c;
526 const uint8_t *end = buf + len;
527 switch (utf16) {
528 case 0:
529 return (uint8_t *)memchr(buf, '\n', len);
530 case 1:
531 while (buf < end - 1) {
532 GET_UTF16(c, buf < end - 1 ? get_le16_inc(&buf) : 0, return NULL;)
533 if (buf <= end && c == '\n')
534 return buf - 1;
536 break;
537 case 2:
538 while (buf < end - 1) {
539 GET_UTF16(c, buf < end - 1 ? get_be16_inc(&buf) : 0, return NULL;)
540 if (buf <= end && c == '\n')
541 return buf - 1;
543 break;
545 return NULL;
549 * Copy a number of bytes, converting to UTF-8 if input is UTF-16
550 * \param dst buffer to copy to
551 * \param dstsize size of dst buffer
552 * \param src buffer to copy from
553 * \param len amount of bytes to copy from src
554 * \param utf16 chose between UTF-8/ASCII/other and LE and BE UTF-16
555 * 0 = UTF-8/ASCII/other, 1 = UTF-16-LE, 2 = UTF-16-BE
557 static int copy_characters(uint8_t *dst, int dstsize,
558 const uint8_t *src, int *len, int utf16)
560 uint32_t c;
561 uint8_t *dst_end = dst + dstsize;
562 const uint8_t *end = src + *len;
563 switch (utf16) {
564 case 0:
565 if (*len > dstsize)
566 *len = dstsize;
567 memcpy(dst, src, *len);
568 return *len;
569 case 1:
570 while (src < end - 1 && dst_end - dst > 8) {
571 uint8_t tmp;
572 GET_UTF16(c, src < end - 1 ? get_le16_inc(&src) : 0, ;)
573 PUT_UTF8(c, tmp, *dst++ = tmp;)
575 *len -= end - src;
576 return dstsize - (dst_end - dst);
577 case 2:
578 while (src < end - 1 && dst_end - dst > 8) {
579 uint8_t tmp;
580 GET_UTF16(c, src < end - 1 ? get_be16_inc(&src) : 0, ;)
581 PUT_UTF8(c, tmp, *dst++ = tmp;)
583 *len -= end - src;
584 return dstsize - (dst_end - dst);
586 return 0;
589 unsigned char* stream_read_line(stream_t *s,unsigned char* mem, int max, int utf16) {
590 int len;
591 const unsigned char *end;
592 unsigned char *ptr = mem;
593 if (max < 1) return NULL;
594 max--; // reserve one for 0-termination
595 do {
596 len = s->buf_len-s->buf_pos;
597 // try to fill the buffer
598 if(len <= 0 &&
599 (!cache_stream_fill_buffer(s) ||
600 (len = s->buf_len-s->buf_pos) <= 0)) break;
601 end = find_newline(s->buffer+s->buf_pos, len, utf16);
602 if(end) len = end - (s->buffer+s->buf_pos) + 1;
603 if(len > 0 && max > 0) {
604 int l = copy_characters(ptr, max, s->buffer+s->buf_pos, &len, utf16);
605 max -= l;
606 ptr += l;
607 if (!len)
608 break;
610 s->buf_pos += len;
611 } while(!end);
612 if(s->eof && ptr == mem) return NULL;
613 ptr[0] = 0;
614 return mem;