vo_corevideo.m: remote useless parentheses and casts
[mplayer/glamo.git] / stream / stream.c
blobb562c56cfe1bf93c21b7b23f31e6b8e3eabc684a
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 <libavutil/common.h>
40 #include "mp_msg.h"
41 #include "osdep/shmem.h"
42 #include "osdep/timer.h"
43 #include "network.h"
44 #include "stream.h"
45 #include "libmpdemux/demuxer.h"
46 #include "ffmpeg_files/intreadwrite.h"
48 #include "m_option.h"
49 #include "m_struct.h"
51 #include "cache2.h"
53 struct input_ctx;
54 static int (*stream_check_interrupt_cb)(struct input_ctx *ctx, int time);
55 static struct input_ctx *stream_check_interrupt_ctx;
57 extern const stream_info_t stream_info_vcd;
58 extern const stream_info_t stream_info_cdda;
59 extern const stream_info_t stream_info_netstream;
60 extern const stream_info_t stream_info_pnm;
61 extern const stream_info_t stream_info_asf;
62 extern const stream_info_t stream_info_rtsp;
63 extern const stream_info_t stream_info_rtp;
64 extern const stream_info_t stream_info_udp;
65 extern const stream_info_t stream_info_http1;
66 extern const stream_info_t stream_info_http2;
67 extern const stream_info_t stream_info_dvb;
68 extern const stream_info_t stream_info_tv;
69 extern const stream_info_t stream_info_radio;
70 extern const stream_info_t stream_info_pvr;
71 extern const stream_info_t stream_info_ftp;
72 extern const stream_info_t stream_info_vstream;
73 extern const stream_info_t stream_info_dvdnav;
74 extern const stream_info_t stream_info_smb;
75 extern const stream_info_t stream_info_sdp;
76 extern const stream_info_t stream_info_rtsp_sip;
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_rtsp,
100 #ifdef CONFIG_LIVE555
101 &stream_info_sdp,
102 &stream_info_rtsp_sip,
103 #endif
104 &stream_info_rtp,
105 &stream_info_udp,
106 &stream_info_http2,
107 #endif
108 #ifdef CONFIG_DVBIN
109 &stream_info_dvb,
110 #endif
111 #ifdef CONFIG_TV
112 &stream_info_tv,
113 #endif
114 #ifdef CONFIG_RADIO
115 &stream_info_radio,
116 #endif
117 #ifdef CONFIG_PVR
118 &stream_info_pvr,
119 #endif
120 #ifdef CONFIG_FTP
121 &stream_info_ftp,
122 #endif
123 #ifdef CONFIG_VSTREAM
124 &stream_info_vstream,
125 #endif
126 #ifdef CONFIG_LIBSMBCLIENT
127 &stream_info_smb,
128 #endif
129 &stream_info_cue,
130 #ifdef CONFIG_DVDREAD
131 &stream_info_ifo,
132 &stream_info_dvd,
133 #endif
134 #ifdef CONFIG_DVDNAV
135 &stream_info_dvdnav,
136 #endif
137 #ifdef CONFIG_LIBBLURAY
138 &stream_info_bluray,
139 #endif
140 #ifdef CONFIG_LIBAVFORMAT
141 &stream_info_ffmpeg,
142 #endif
144 &stream_info_null,
145 &stream_info_mf,
146 &stream_info_file,
147 NULL
150 static stream_t *open_stream_plugin(const stream_info_t *sinfo,
151 const char *filename,
152 int mode, struct MPOpts *options,
153 int *file_format, int *ret,
154 char **redirected_url)
156 void* arg = NULL;
157 stream_t* s;
158 m_struct_t* desc = (m_struct_t*)sinfo->opts;
160 // Parse options
161 if(desc) {
162 arg = m_struct_alloc(desc);
163 if(sinfo->opts_url) {
164 m_option_t url_opt =
165 { "stream url", arg , CONF_TYPE_CUSTOM_URL, 0, 0 ,0, sinfo->opts };
166 if(m_option_parse(&url_opt,"stream url",filename,arg,M_CONFIG_FILE) < 0) {
167 mp_msg(MSGT_OPEN,MSGL_ERR, "URL parsing failed on url %s\n",filename);
168 m_struct_free(desc,arg);
169 return NULL;
173 s = new_stream(-2,-2);
174 s->opts = options;
175 s->url=strdup(filename);
176 s->flags |= mode;
177 *ret = sinfo->open(s,mode,arg,file_format);
178 if((*ret) != STREAM_OK) {
179 #ifdef CONFIG_NETWORKING
180 if (*ret == STREAM_REDIRECTED && redirected_url) {
181 if (s->streaming_ctrl && s->streaming_ctrl->url
182 && s->streaming_ctrl->url->url)
183 *redirected_url = strdup(s->streaming_ctrl->url->url);
184 else
185 *redirected_url = NULL;
187 streaming_ctrl_free(s->streaming_ctrl);
188 #endif
189 free(s->url);
190 free(s);
191 return NULL;
193 if(s->type <= -2)
194 mp_msg(MSGT_OPEN,MSGL_WARN, "Warning streams need a type !!!!\n");
195 if(s->flags & MP_STREAM_SEEK && !s->seek)
196 s->flags &= ~MP_STREAM_SEEK;
197 if(s->seek && !(s->flags & MP_STREAM_SEEK))
198 s->flags |= MP_STREAM_SEEK;
200 s->mode = mode;
202 mp_msg(MSGT_OPEN,MSGL_V, "STREAM: [%s] %s\n",sinfo->name,filename);
203 mp_msg(MSGT_OPEN,MSGL_V, "STREAM: Description: %s\n",sinfo->info);
204 mp_msg(MSGT_OPEN,MSGL_V, "STREAM: Author: %s\n", sinfo->author);
205 mp_msg(MSGT_OPEN,MSGL_V, "STREAM: Comment: %s\n", sinfo->comment);
207 return s;
211 stream_t *open_stream_full(const char *filename, int mode,
212 struct MPOpts *options, int *file_format)
214 int i,j,l,r;
215 const stream_info_t* sinfo;
216 stream_t* s;
217 char *redirected_url = NULL;
219 for(i = 0 ; auto_open_streams[i] ; i++) {
220 sinfo = auto_open_streams[i];
221 if(!sinfo->protocols) {
222 mp_msg(MSGT_OPEN,MSGL_WARN, "Stream type %s has protocols == NULL, it's a bug\n", sinfo->name);
223 continue;
225 for(j = 0 ; sinfo->protocols[j] ; j++) {
226 l = strlen(sinfo->protocols[j]);
227 // l == 0 => Don't do protocol matching (ie network and filenames)
228 if((l == 0 && !strstr(filename, "://")) ||
229 ((strncasecmp(sinfo->protocols[j],filename,l) == 0) &&
230 (strncmp("://",filename+l,3) == 0))) {
231 *file_format = DEMUXER_TYPE_UNKNOWN;
232 s = open_stream_plugin(sinfo,filename,mode,options,file_format,&r,
233 &redirected_url);
234 if(s) return s;
235 if(r == STREAM_REDIRECTED && redirected_url) {
236 mp_msg(MSGT_OPEN,MSGL_V, "[%s] open %s redirected to %s\n",
237 sinfo->info, filename, redirected_url);
238 s = open_stream_full(redirected_url, mode, options, file_format);
239 free(redirected_url);
240 return s;
242 else if(r != STREAM_UNSUPPORTED) {
243 mp_tmsg(MSGT_OPEN,MSGL_ERR, "Failed to open %s.\n",filename);
244 return NULL;
246 break;
251 mp_msg(MSGT_OPEN,MSGL_ERR, "No stream found to handle url %s\n",filename);
252 return NULL;
255 stream_t *open_output_stream(const char *filename, struct MPOpts *options)
257 int file_format; //unused
258 if(!filename) {
259 mp_msg(MSGT_OPEN,MSGL_ERR,"open_output_stream(), NULL filename, report this bug\n");
260 return NULL;
263 return open_stream_full(filename,STREAM_WRITE,options,&file_format);
266 //=================== STREAMER =========================
268 int stream_fill_buffer(stream_t *s){
269 int len;
270 // we will retry even if we already reached EOF previously.
271 switch(s->type){
272 case STREAMTYPE_STREAM:
273 #ifdef CONFIG_NETWORKING
274 if( s->streaming_ctrl!=NULL && s->streaming_ctrl->streaming_read ) {
275 len=s->streaming_ctrl->streaming_read(s->fd,s->buffer,STREAM_BUFFER_SIZE, s->streaming_ctrl);
276 } else
277 #endif
278 if (s->fill_buffer)
279 len = s->fill_buffer(s, s->buffer, STREAM_BUFFER_SIZE);
280 else
281 len=read(s->fd,s->buffer,STREAM_BUFFER_SIZE);
282 break;
283 case STREAMTYPE_DS:
284 len = demux_read_data((demux_stream_t*)s->priv,s->buffer,STREAM_BUFFER_SIZE);
285 break;
288 default:
289 len= s->fill_buffer ? s->fill_buffer(s,s->buffer,STREAM_BUFFER_SIZE) : 0;
291 if(len<=0){ s->eof=1; return 0; }
292 // When reading succeeded we are obviously not at eof.
293 // This e.g. avoids issues with eof getting stuck when lavf seeks in MPEG-TS
294 s->eof=0;
295 s->buf_pos=0;
296 s->buf_len=len;
297 s->pos+=len;
298 // printf("[%d]",len);fflush(stdout);
299 return len;
302 int stream_write_buffer(stream_t *s, unsigned char *buf, int len) {
303 int rd;
304 if(!s->write_buffer)
305 return -1;
306 rd = s->write_buffer(s, buf, len);
307 if(rd < 0)
308 return -1;
309 s->pos += rd;
310 return rd;
313 int stream_seek_long(stream_t *s,off_t pos){
314 off_t newpos=0;
316 // if( mp_msg_test(MSGT_STREAM,MSGL_DBG3) ) printf("seek_long to 0x%X\n",(unsigned int)pos);
318 s->buf_pos=s->buf_len=0;
320 if(s->mode == STREAM_WRITE) {
321 if(!s->seek || !s->seek(s,pos))
322 return 0;
323 return 1;
326 if(s->sector_size)
327 newpos = (pos/s->sector_size)*s->sector_size;
328 else
329 newpos = pos&(~((off_t)STREAM_BUFFER_SIZE-1));
331 if( mp_msg_test(MSGT_STREAM,MSGL_DBG3) ){
332 mp_msg(MSGT_STREAM,MSGL_DBG3, "s->pos=%"PRIX64" newpos=%"PRIX64" new_bufpos=%"PRIX64" buflen=%X \n",
333 (int64_t)s->pos,(int64_t)newpos,(int64_t)pos,s->buf_len);
335 pos-=newpos;
337 if(newpos==0 || newpos!=s->pos){
338 switch(s->type){
339 case STREAMTYPE_STREAM:
340 //s->pos=newpos; // real seek
341 // Some streaming protocol allow to seek backward and forward
342 // A function call that return -1 can tell that the protocol
343 // doesn't support seeking.
344 #ifdef CONFIG_NETWORKING
345 if(s->seek) { // new stream seek is much cleaner than streaming_ctrl one
346 if(!s->seek(s,newpos)) {
347 mp_msg(MSGT_STREAM,MSGL_ERR, "Seek failed\n");
348 return 0;
350 break;
353 if( s->streaming_ctrl!=NULL && s->streaming_ctrl->streaming_seek ) {
354 if( s->streaming_ctrl->streaming_seek( s->fd, pos, s->streaming_ctrl )<0 ) {
355 mp_msg(MSGT_STREAM,MSGL_INFO,"Stream not seekable!\n");
356 return 1;
358 break;
360 #endif
361 if(newpos<s->pos){
362 mp_msg(MSGT_STREAM,MSGL_INFO,"Cannot seek backward in linear streams!\n");
363 return 1;
365 while(s->pos<newpos){
366 if(stream_fill_buffer(s)<=0) break; // EOF
368 break;
369 default:
370 // This should at the beginning as soon as all streams are converted
371 if(!s->seek)
372 return 0;
373 // Now seek
374 if(!s->seek(s,newpos)) {
375 mp_msg(MSGT_STREAM,MSGL_ERR, "Seek failed\n");
376 return 0;
379 // putchar('.');fflush(stdout);
380 //} else {
381 // putchar('%');fflush(stdout);
384 s->eof = 0; // EOF reset when seek succeeds.
385 while (stream_fill_buffer(s) > 0) {
386 if(pos<=s->buf_len){
387 s->buf_pos=pos; // byte position in sector
388 return 1;
390 pos -= s->buf_len;
392 // Fill failed, but seek still is a success.
393 s->pos += pos;
394 s->buf_pos = 0;
395 s->buf_len = 0;
397 mp_msg(MSGT_STREAM,MSGL_V,
398 "stream_seek: Seek to/past EOF: no buffer preloaded.\n");
399 return 1;
403 void stream_reset(stream_t *s){
404 if(s->eof){
405 s->pos=0;
406 s->buf_pos=s->buf_len=0;
407 s->eof=0;
409 if(s->control) s->control(s,STREAM_CTRL_RESET,NULL);
410 //stream_seek(s,0);
413 int stream_control(stream_t *s, int cmd, void *arg){
414 if(!s->control) return STREAM_UNSUPPORTED;
415 #ifdef CONFIG_STREAM_CACHE
416 if (s->cache_pid)
417 return cache_do_control(s, cmd, arg);
418 #endif
419 return s->control(s, cmd, arg);
422 stream_t* new_memory_stream(unsigned char* data,int len){
423 stream_t *s;
425 if(len < 0)
426 return NULL;
427 s=calloc(1, sizeof(stream_t)+len);
428 s->fd=-1;
429 s->type=STREAMTYPE_MEMORY;
430 s->buf_pos=0; s->buf_len=len;
431 s->start_pos=0; s->end_pos=len;
432 stream_reset(s);
433 s->pos=len;
434 memcpy(s->buffer,data,len);
435 return s;
438 stream_t* new_stream(int fd,int type){
439 stream_t *s=calloc(1, sizeof(stream_t));
440 if(s==NULL) return NULL;
442 #if HAVE_WINSOCK2_H
444 WSADATA wsdata;
445 int temp = WSAStartup(0x0202, &wsdata); // there might be a better place for this (-> later)
446 mp_msg(MSGT_STREAM,MSGL_V,"WINSOCK2 init: %i\n", temp);
448 #endif
450 s->fd=fd;
451 s->type=type;
452 s->buf_pos=s->buf_len=0;
453 s->start_pos=s->end_pos=0;
454 s->priv=NULL;
455 s->url=NULL;
456 s->cache_pid=0;
457 stream_reset(s);
458 return s;
461 void free_stream(stream_t *s){
462 // printf("\n*** free_stream() called ***\n");
463 #ifdef CONFIG_STREAM_CACHE
464 cache_uninit(s);
465 #endif
466 if(s->close) s->close(s);
467 if(s->fd>0){
468 /* on unix we define closesocket to close
469 on windows however we have to distinguish between
470 network socket and file */
471 if(s->url && strstr(s->url,"://"))
472 closesocket(s->fd);
473 else close(s->fd);
475 #if HAVE_WINSOCK2_H
476 mp_msg(MSGT_STREAM,MSGL_V,"WINSOCK2 uninit\n");
477 WSACleanup(); // there might be a better place for this (-> later)
478 #endif
479 // Disabled atm, i don't like that. s->priv can be anything after all
480 // streams should destroy their priv on close
481 //if(s->priv) free(s->priv);
482 if(s->url) free(s->url);
483 free(s);
486 stream_t* new_ds_stream(demux_stream_t *ds) {
487 stream_t* s = new_stream(-1,STREAMTYPE_DS);
488 s->priv = ds;
489 return s;
492 void stream_set_interrupt_callback(int (*cb)(struct input_ctx *, int),
493 struct input_ctx *ctx)
495 stream_check_interrupt_cb = cb;
496 stream_check_interrupt_ctx = ctx;
499 int stream_check_interrupt(int time) {
500 if(!stream_check_interrupt_cb) {
501 usec_sleep(time * 1000);
502 return 0;
504 return stream_check_interrupt_cb(stream_check_interrupt_ctx, time);
508 * Helper function to read 16 bits little-endian and advance pointer
510 static uint16_t get_le16_inc(const uint8_t **buf)
512 uint16_t v = AV_RL16(*buf);
513 *buf += 2;
514 return v;
518 * Helper function to read 16 bits big-endian and advance pointer
520 static uint16_t get_be16_inc(const uint8_t **buf)
522 uint16_t v = AV_RB16(*buf);
523 *buf += 2;
524 return v;
528 * Find a newline character in buffer
529 * \param buf buffer to search
530 * \param len amount of bytes to search in buffer, may not overread
531 * \param utf16 chose between UTF-8/ASCII/other and LE and BE UTF-16
532 * 0 = UTF-8/ASCII/other, 1 = UTF-16-LE, 2 = UTF-16-BE
534 static const uint8_t *find_newline(const uint8_t *buf, int len, int utf16)
536 uint32_t c;
537 const uint8_t *end = buf + len;
538 switch (utf16) {
539 case 0:
540 return (uint8_t *)memchr(buf, '\n', len);
541 case 1:
542 while (buf < end - 1) {
543 GET_UTF16(c, buf < end - 1 ? get_le16_inc(&buf) : 0, return NULL;)
544 if (buf <= end && c == '\n')
545 return buf - 1;
547 break;
548 case 2:
549 while (buf < end - 1) {
550 GET_UTF16(c, buf < end - 1 ? get_be16_inc(&buf) : 0, return NULL;)
551 if (buf <= end && c == '\n')
552 return buf - 1;
554 break;
556 return NULL;
560 * Copy a number of bytes, converting to UTF-8 if input is UTF-16
561 * \param dst buffer to copy to
562 * \param dstsize size of dst buffer
563 * \param src buffer to copy from
564 * \param len amount of bytes to copy from src
565 * \param utf16 chose between UTF-8/ASCII/other and LE and BE UTF-16
566 * 0 = UTF-8/ASCII/other, 1 = UTF-16-LE, 2 = UTF-16-BE
568 static int copy_characters(uint8_t *dst, int dstsize,
569 const uint8_t *src, int *len, int utf16)
571 uint32_t c;
572 uint8_t *dst_end = dst + dstsize;
573 const uint8_t *end = src + *len;
574 switch (utf16) {
575 case 0:
576 if (*len > dstsize)
577 *len = dstsize;
578 memcpy(dst, src, *len);
579 return *len;
580 case 1:
581 while (src < end - 1 && dst_end - dst > 8) {
582 uint8_t tmp;
583 GET_UTF16(c, src < end - 1 ? get_le16_inc(&src) : 0, ;)
584 PUT_UTF8(c, tmp, *dst++ = tmp;)
586 *len -= end - src;
587 return dstsize - (dst_end - dst);
588 case 2:
589 while (src < end - 1 && dst_end - dst > 8) {
590 uint8_t tmp;
591 GET_UTF16(c, src < end - 1 ? get_be16_inc(&src) : 0, ;)
592 PUT_UTF8(c, tmp, *dst++ = tmp;)
594 *len -= end - src;
595 return dstsize - (dst_end - dst);
597 return 0;
600 unsigned char* stream_read_line(stream_t *s,unsigned char* mem, int max, int utf16) {
601 int len;
602 const unsigned char *end;
603 unsigned char *ptr = mem;
604 if (max < 1) return NULL;
605 max--; // reserve one for 0-termination
606 do {
607 len = s->buf_len-s->buf_pos;
608 // try to fill the buffer
609 if(len <= 0 &&
610 (!cache_stream_fill_buffer(s) ||
611 (len = s->buf_len-s->buf_pos) <= 0)) break;
612 end = find_newline(s->buffer+s->buf_pos, len, utf16);
613 if(end) len = end - (s->buffer+s->buf_pos) + 1;
614 if(len > 0 && max > 0) {
615 int l = copy_characters(ptr, max, s->buffer+s->buf_pos, &len, utf16);
616 max -= l;
617 ptr += l;
618 if (!len)
619 break;
621 s->buf_pos += len;
622 } while(!end);
623 if(s->eof && ptr == mem) return NULL;
624 ptr[0] = 0;
625 return mem;