af_scaletempo: Fix delay value after changing scale to 1
[mplayer.git] / stream / stream.c
blob8570f0b125499e4ccb09af1b8b6f99cab6170e4b
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <unistd.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #ifndef __MINGW32__
9 #include <sys/ioctl.h>
10 #include <sys/wait.h>
11 #endif
12 #include <fcntl.h>
13 #include <strings.h>
15 #include "config.h"
17 #if HAVE_WINSOCK2_H
18 #include <winsock2.h>
19 #endif
21 #include "mp_msg.h"
22 #include "help_mp.h"
23 #include "osdep/shmem.h"
24 #include "network.h"
25 #include "stream.h"
26 #include "libmpdemux/demuxer.h"
28 #include "m_option.h"
29 #include "m_struct.h"
31 #include "cache2.h"
33 //#include "vcd_read_bincue.h"
35 struct input_ctx;
36 static int (*stream_check_interrupt_cb)(struct input_ctx *ctx, int time);
37 static struct input_ctx *stream_check_interrupt_ctx;
39 extern const stream_info_t stream_info_vcd;
40 extern const stream_info_t stream_info_cdda;
41 extern const stream_info_t stream_info_netstream;
42 extern const stream_info_t stream_info_pnm;
43 extern const stream_info_t stream_info_asf;
44 extern const stream_info_t stream_info_rtsp;
45 extern const stream_info_t stream_info_rtp;
46 extern const stream_info_t stream_info_udp;
47 extern const stream_info_t stream_info_http1;
48 extern const stream_info_t stream_info_http2;
49 extern const stream_info_t stream_info_dvb;
50 extern const stream_info_t stream_info_tv;
51 extern const stream_info_t stream_info_radio;
52 extern const stream_info_t stream_info_pvr;
53 extern const stream_info_t stream_info_ftp;
54 extern const stream_info_t stream_info_vstream;
55 extern const stream_info_t stream_info_dvdnav;
56 extern const stream_info_t stream_info_smb;
57 extern const stream_info_t stream_info_sdp;
58 extern const stream_info_t stream_info_rtsp_sip;
60 extern const stream_info_t stream_info_cue;
61 extern const stream_info_t stream_info_null;
62 extern const stream_info_t stream_info_mf;
63 extern const stream_info_t stream_info_file;
64 extern const stream_info_t stream_info_ifo;
65 extern const stream_info_t stream_info_dvd;
67 static const stream_info_t* const auto_open_streams[] = {
68 #ifdef CONFIG_VCD
69 &stream_info_vcd,
70 #endif
71 #ifdef CONFIG_CDDA
72 &stream_info_cdda,
73 #endif
74 #ifdef CONFIG_NETWORK
75 &stream_info_netstream,
76 &stream_info_http1,
77 &stream_info_asf,
78 &stream_info_pnm,
79 &stream_info_rtsp,
80 #ifdef CONFIG_LIVE555
81 &stream_info_sdp,
82 &stream_info_rtsp_sip,
83 #endif
84 &stream_info_rtp,
85 &stream_info_udp,
86 &stream_info_http2,
87 #endif
88 #ifdef CONFIG_DVBIN
89 &stream_info_dvb,
90 #endif
91 #ifdef CONFIG_TV
92 &stream_info_tv,
93 #endif
94 #ifdef CONFIG_RADIO
95 &stream_info_radio,
96 #endif
97 #ifdef CONFIG_PVR
98 &stream_info_pvr,
99 #endif
100 #ifdef CONFIG_FTP
101 &stream_info_ftp,
102 #endif
103 #ifdef CONFIG_VSTREAM
104 &stream_info_vstream,
105 #endif
106 #ifdef CONFIG_LIBSMBCLIENT
107 &stream_info_smb,
108 #endif
109 &stream_info_cue,
110 #ifdef CONFIG_DVDREAD
111 &stream_info_ifo,
112 &stream_info_dvd,
113 #endif
114 #ifdef CONFIG_DVDNAV
115 &stream_info_dvdnav,
116 #endif
118 &stream_info_null,
119 &stream_info_mf,
120 &stream_info_file,
121 NULL
124 static stream_t *open_stream_plugin(const stream_info_t *sinfo, char *filename,
125 int mode, struct MPOpts *options,
126 int *file_format, int *ret,
127 char **redirected_url)
129 void* arg = NULL;
130 stream_t* s;
131 m_struct_t* desc = (m_struct_t*)sinfo->opts;
133 // Parse options
134 if(desc) {
135 arg = m_struct_alloc(desc);
136 if(sinfo->opts_url) {
137 m_option_t url_opt =
138 { "stream url", arg , CONF_TYPE_CUSTOM_URL, 0, 0 ,0, sinfo->opts };
139 if(m_option_parse(&url_opt,"stream url",filename,arg,M_CONFIG_FILE) < 0) {
140 mp_msg(MSGT_OPEN,MSGL_ERR, "URL parsing failed on url %s\n",filename);
141 m_struct_free(desc,arg);
142 return NULL;
146 s = new_stream(-2,-2);
147 s->opts = options;
148 s->url=strdup(filename);
149 s->flags |= mode;
150 *ret = sinfo->open(s,mode,arg,file_format);
151 if((*ret) != STREAM_OK) {
152 #ifdef CONFIG_NETWORK
153 if (*ret == STREAM_REDIRECTED && redirected_url) {
154 if (s->streaming_ctrl && s->streaming_ctrl->url
155 && s->streaming_ctrl->url->url)
156 *redirected_url = strdup(s->streaming_ctrl->url->url);
157 else
158 *redirected_url = NULL;
160 streaming_ctrl_free(s->streaming_ctrl);
161 #endif
162 free(s->url);
163 free(s);
164 return NULL;
166 if(s->type <= -2)
167 mp_msg(MSGT_OPEN,MSGL_WARN, "Warning streams need a type !!!!\n");
168 if(s->flags & STREAM_SEEK && !s->seek)
169 s->flags &= ~STREAM_SEEK;
170 if(s->seek && !(s->flags & STREAM_SEEK))
171 s->flags |= STREAM_SEEK;
173 s->mode = mode;
175 mp_msg(MSGT_OPEN,MSGL_V, "STREAM: [%s] %s\n",sinfo->name,filename);
176 mp_msg(MSGT_OPEN,MSGL_V, "STREAM: Description: %s\n",sinfo->info);
177 mp_msg(MSGT_OPEN,MSGL_V, "STREAM: Author: %s\n", sinfo->author);
178 mp_msg(MSGT_OPEN,MSGL_V, "STREAM: Comment: %s\n", sinfo->comment);
180 return s;
184 stream_t *open_stream_full(char *filename,int mode, struct MPOpts *options,
185 int* file_format)
187 int i,j,l,r;
188 const stream_info_t* sinfo;
189 stream_t* s;
190 char *redirected_url = NULL;
192 for(i = 0 ; auto_open_streams[i] ; i++) {
193 sinfo = auto_open_streams[i];
194 if(!sinfo->protocols) {
195 mp_msg(MSGT_OPEN,MSGL_WARN, "Stream type %s has protocols == NULL, it's a bug\n", sinfo->name);
196 continue;
198 for(j = 0 ; sinfo->protocols[j] ; j++) {
199 l = strlen(sinfo->protocols[j]);
200 // l == 0 => Don't do protocol matching (ie network and filenames)
201 if((l == 0 && !strstr(filename, "://")) ||
202 ((strncasecmp(sinfo->protocols[j],filename,l) == 0) &&
203 (strncmp("://",filename+l,3) == 0))) {
204 *file_format = DEMUXER_TYPE_UNKNOWN;
205 s = open_stream_plugin(sinfo,filename,mode,options,file_format,&r,
206 &redirected_url);
207 if(s) return s;
208 if(r == STREAM_REDIRECTED && redirected_url) {
209 mp_msg(MSGT_OPEN,MSGL_V, "[%s] open %s redirected to %s\n",
210 sinfo->info, filename, redirected_url);
211 s = open_stream_full(redirected_url, mode, options, file_format);
212 free(redirected_url);
213 return s;
215 else if(r != STREAM_UNSUPPORTED) {
216 mp_tmsg(MSGT_OPEN,MSGL_ERR, "Failed to open %s.\n",filename);
217 return NULL;
219 break;
224 mp_msg(MSGT_OPEN,MSGL_ERR, "No stream found to handle url %s\n",filename);
225 return NULL;
228 stream_t *open_output_stream(char *filename, struct MPOpts *options)
230 int file_format; //unused
231 if(!filename) {
232 mp_msg(MSGT_OPEN,MSGL_ERR,"open_output_stream(), NULL filename, report this bug\n");
233 return NULL;
236 return open_stream_full(filename,STREAM_WRITE,options,&file_format);
239 //=================== STREAMER =========================
241 int stream_fill_buffer(stream_t *s){
242 int len;
243 if (/*s->fd == NULL ||*/ s->eof) { s->buf_pos = s->buf_len = 0; return 0; }
244 switch(s->type){
245 case STREAMTYPE_STREAM:
246 #ifdef CONFIG_NETWORK
247 if( s->streaming_ctrl!=NULL && s->streaming_ctrl->streaming_read ) {
248 len=s->streaming_ctrl->streaming_read(s->fd,s->buffer,STREAM_BUFFER_SIZE, s->streaming_ctrl);
249 } else
250 #endif
251 len=read(s->fd,s->buffer,STREAM_BUFFER_SIZE);
252 break;
253 case STREAMTYPE_DS:
254 len = demux_read_data((demux_stream_t*)s->priv,s->buffer,STREAM_BUFFER_SIZE);
255 break;
258 default:
259 len= s->fill_buffer ? s->fill_buffer(s,s->buffer,STREAM_BUFFER_SIZE) : 0;
261 if(len<=0){ s->eof=1; s->buf_pos=s->buf_len=0; return 0; }
262 s->buf_pos=0;
263 s->buf_len=len;
264 s->pos+=len;
265 // printf("[%d]",len);fflush(stdout);
266 return len;
269 int stream_write_buffer(stream_t *s, unsigned char *buf, int len) {
270 int rd;
271 if(!s->write_buffer)
272 return -1;
273 rd = s->write_buffer(s, buf, len);
274 if(rd < 0)
275 return -1;
276 s->pos += rd;
277 return rd;
280 int stream_seek_long(stream_t *s,off_t pos){
281 off_t newpos=0;
283 // if( mp_msg_test(MSGT_STREAM,MSGL_DBG3) ) printf("seek_long to 0x%X\n",(unsigned int)pos);
285 s->buf_pos=s->buf_len=0;
287 if(s->mode == STREAM_WRITE) {
288 if(!s->seek || !s->seek(s,pos))
289 return 0;
290 return 1;
293 if(s->sector_size)
294 newpos = (pos/s->sector_size)*s->sector_size;
295 else
296 newpos = pos&(~((off_t)STREAM_BUFFER_SIZE-1));
298 if( mp_msg_test(MSGT_STREAM,MSGL_DBG3) ){
299 mp_msg(MSGT_STREAM,MSGL_DBG3, "s->pos=%"PRIX64" newpos=%"PRIX64" new_bufpos=%"PRIX64" buflen=%X \n",
300 (int64_t)s->pos,(int64_t)newpos,(int64_t)pos,s->buf_len);
302 pos-=newpos;
304 if(newpos==0 || newpos!=s->pos){
305 switch(s->type){
306 case STREAMTYPE_STREAM:
307 //s->pos=newpos; // real seek
308 // Some streaming protocol allow to seek backward and forward
309 // A function call that return -1 can tell that the protocol
310 // doesn't support seeking.
311 #ifdef CONFIG_NETWORK
312 if(s->seek) { // new stream seek is much cleaner than streaming_ctrl one
313 if(!s->seek(s,newpos)) {
314 mp_msg(MSGT_STREAM,MSGL_ERR, "Seek failed\n");
315 return 0;
317 break;
320 if( s->streaming_ctrl!=NULL && s->streaming_ctrl->streaming_seek ) {
321 if( s->streaming_ctrl->streaming_seek( s->fd, pos, s->streaming_ctrl )<0 ) {
322 mp_msg(MSGT_STREAM,MSGL_INFO,"Stream not seekable!\n");
323 return 1;
326 #else
327 if(newpos<s->pos){
328 mp_msg(MSGT_STREAM,MSGL_INFO,"Cannot seek backward in linear streams!\n");
329 return 1;
331 while(s->pos<newpos){
332 if(stream_fill_buffer(s)<=0) break; // EOF
334 #endif
335 break;
336 default:
337 // This should at the beginning as soon as all streams are converted
338 if(!s->seek)
339 return 0;
340 // Now seek
341 if(!s->seek(s,newpos)) {
342 mp_msg(MSGT_STREAM,MSGL_ERR, "Seek failed\n");
343 return 0;
346 // putchar('.');fflush(stdout);
347 //} else {
348 // putchar('%');fflush(stdout);
351 while(stream_fill_buffer(s) > 0 && pos >= 0) {
352 if(pos<=s->buf_len){
353 s->buf_pos=pos; // byte position in sector
354 return 1;
356 pos -= s->buf_len;
359 // if(pos==s->buf_len) printf("XXX Seek to last byte of file -> EOF\n");
361 mp_msg(MSGT_STREAM,MSGL_V,"stream_seek: WARNING! Can't seek to 0x%"PRIX64" !\n",(int64_t)(pos+newpos));
362 return 0;
366 void stream_reset(stream_t *s){
367 if(s->eof){
368 s->pos=0; //ftell(f);
369 // s->buf_pos=s->buf_len=0;
370 s->eof=0;
372 if(s->control) s->control(s,STREAM_CTRL_RESET,NULL);
373 //stream_seek(s,0);
376 int stream_control(stream_t *s, int cmd, void *arg){
377 if(!s->control) return STREAM_UNSUPPORTED;
378 #ifdef CONFIG_STREAM_CACHE
379 if (s->cache_pid)
380 return cache_do_control(s, cmd, arg);
381 #endif
382 return s->control(s, cmd, arg);
385 stream_t* new_memory_stream(unsigned char* data,int len){
386 stream_t *s;
388 if(len < 0)
389 return NULL;
390 s=malloc(sizeof(stream_t)+len);
391 memset(s,0,sizeof(stream_t));
392 s->fd=-1;
393 s->type=STREAMTYPE_MEMORY;
394 s->buf_pos=0; s->buf_len=len;
395 s->start_pos=0; s->end_pos=len;
396 stream_reset(s);
397 s->pos=len;
398 memcpy(s->buffer,data,len);
399 return s;
402 stream_t* new_stream(int fd,int type){
403 stream_t *s=malloc(sizeof(stream_t));
404 if(s==NULL) return NULL;
405 memset(s,0,sizeof(stream_t));
407 #if HAVE_WINSOCK2_H
409 WSADATA wsdata;
410 int temp = WSAStartup(0x0202, &wsdata); // there might be a better place for this (-> later)
411 mp_msg(MSGT_STREAM,MSGL_V,"WINSOCK2 init: %i\n", temp);
413 #endif
415 s->fd=fd;
416 s->type=type;
417 s->buf_pos=s->buf_len=0;
418 s->start_pos=s->end_pos=0;
419 s->priv=NULL;
420 s->url=NULL;
421 s->cache_pid=0;
422 stream_reset(s);
423 return s;
426 void free_stream(stream_t *s){
427 // printf("\n*** free_stream() called ***\n");
428 #ifdef CONFIG_STREAM_CACHE
429 if(s->cache_pid) {
430 cache_uninit(s);
432 #endif
433 if(s->close) s->close(s);
434 if(s->fd>0){
435 /* on unix we define closesocket to close
436 on windows however we have to distinguish between
437 network socket and file */
438 if(s->url && strstr(s->url,"://"))
439 closesocket(s->fd);
440 else close(s->fd);
442 #if HAVE_WINSOCK2_H
443 mp_msg(MSGT_STREAM,MSGL_V,"WINSOCK2 uninit\n");
444 WSACleanup(); // there might be a better place for this (-> later)
445 #endif
446 // Disabled atm, i don't like that. s->priv can be anything after all
447 // streams should destroy their priv on close
448 //if(s->priv) free(s->priv);
449 if(s->url) free(s->url);
450 free(s);
453 stream_t* new_ds_stream(demux_stream_t *ds) {
454 stream_t* s = new_stream(-1,STREAMTYPE_DS);
455 s->priv = ds;
456 return s;
459 void stream_set_interrupt_callback(int (*cb)(struct input_ctx *, int),
460 struct input_ctx *ctx)
462 stream_check_interrupt_cb = cb;
463 stream_check_interrupt_ctx = ctx;
466 int stream_check_interrupt(int time) {
467 if(!stream_check_interrupt_cb) return 0;
468 return stream_check_interrupt_cb(stream_check_interrupt_ctx, time);