debian/: remove watch
[mplayer.git] / stream / stream.c
blob8b8a84760ac9142d256e07c1fc686bd67805de65
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);break;
249 } else {
250 len=read(s->fd,s->buffer,STREAM_BUFFER_SIZE);break;
252 #else
253 len=read(s->fd,s->buffer,STREAM_BUFFER_SIZE);break;
254 #endif
255 case STREAMTYPE_DS:
256 len = demux_read_data((demux_stream_t*)s->priv,s->buffer,STREAM_BUFFER_SIZE);
257 break;
260 default:
261 len= s->fill_buffer ? s->fill_buffer(s,s->buffer,STREAM_BUFFER_SIZE) : 0;
263 if(len<=0){ s->eof=1; s->buf_pos=s->buf_len=0; return 0; }
264 s->buf_pos=0;
265 s->buf_len=len;
266 s->pos+=len;
267 // printf("[%d]",len);fflush(stdout);
268 return len;
271 int stream_write_buffer(stream_t *s, unsigned char *buf, int len) {
272 int rd;
273 if(!s->write_buffer)
274 return -1;
275 rd = s->write_buffer(s, buf, len);
276 if(rd < 0)
277 return -1;
278 s->pos += rd;
279 return rd;
282 int stream_seek_long(stream_t *s,off_t pos){
283 off_t newpos=0;
285 // if( mp_msg_test(MSGT_STREAM,MSGL_DBG3) ) printf("seek_long to 0x%X\n",(unsigned int)pos);
287 s->buf_pos=s->buf_len=0;
289 if(s->mode == STREAM_WRITE) {
290 if(!s->seek || !s->seek(s,pos))
291 return 0;
292 return 1;
295 if(s->sector_size)
296 newpos = (pos/s->sector_size)*s->sector_size;
297 else
298 newpos = pos&(~((off_t)STREAM_BUFFER_SIZE-1));
300 if( mp_msg_test(MSGT_STREAM,MSGL_DBG3) ){
301 mp_msg(MSGT_STREAM,MSGL_DBG3, "s->pos=%"PRIX64" newpos=%"PRIX64" new_bufpos=%"PRIX64" buflen=%X \n",
302 (int64_t)s->pos,(int64_t)newpos,(int64_t)pos,s->buf_len);
304 pos-=newpos;
306 if(newpos==0 || newpos!=s->pos){
307 switch(s->type){
308 case STREAMTYPE_STREAM:
309 //s->pos=newpos; // real seek
310 // Some streaming protocol allow to seek backward and forward
311 // A function call that return -1 can tell that the protocol
312 // doesn't support seeking.
313 #ifdef CONFIG_NETWORK
314 if(s->seek) { // new stream seek is much cleaner than streaming_ctrl one
315 if(!s->seek(s,newpos)) {
316 mp_msg(MSGT_STREAM,MSGL_ERR, "Seek failed\n");
317 return 0;
319 break;
322 if( s->streaming_ctrl!=NULL && s->streaming_ctrl->streaming_seek ) {
323 if( s->streaming_ctrl->streaming_seek( s->fd, pos, s->streaming_ctrl )<0 ) {
324 mp_msg(MSGT_STREAM,MSGL_INFO,"Stream not seekable!\n");
325 return 1;
328 #else
329 if(newpos<s->pos){
330 mp_msg(MSGT_STREAM,MSGL_INFO,"Cannot seek backward in linear streams!\n");
331 return 1;
333 while(s->pos<newpos){
334 if(stream_fill_buffer(s)<=0) break; // EOF
336 #endif
337 break;
338 default:
339 // This should at the beginning as soon as all streams are converted
340 if(!s->seek)
341 return 0;
342 // Now seek
343 if(!s->seek(s,newpos)) {
344 mp_msg(MSGT_STREAM,MSGL_ERR, "Seek failed\n");
345 return 0;
348 // putchar('.');fflush(stdout);
349 //} else {
350 // putchar('%');fflush(stdout);
353 while(stream_fill_buffer(s) > 0 && pos >= 0) {
354 if(pos<=s->buf_len){
355 s->buf_pos=pos; // byte position in sector
356 return 1;
358 pos -= s->buf_len;
361 // if(pos==s->buf_len) printf("XXX Seek to last byte of file -> EOF\n");
363 mp_msg(MSGT_STREAM,MSGL_V,"stream_seek: WARNING! Can't seek to 0x%"PRIX64" !\n",(int64_t)(pos+newpos));
364 return 0;
368 void stream_reset(stream_t *s){
369 if(s->eof){
370 s->pos=0; //ftell(f);
371 // s->buf_pos=s->buf_len=0;
372 s->eof=0;
374 if(s->control) s->control(s,STREAM_CTRL_RESET,NULL);
375 //stream_seek(s,0);
378 int stream_control(stream_t *s, int cmd, void *arg){
379 if(!s->control) return STREAM_UNSUPPORTED;
380 #ifdef CONFIG_STREAM_CACHE
381 if (s->cache_pid)
382 return cache_do_control(s, cmd, arg);
383 #endif
384 return s->control(s, cmd, arg);
387 stream_t* new_memory_stream(unsigned char* data,int len){
388 stream_t *s;
390 if(len < 0)
391 return NULL;
392 s=malloc(sizeof(stream_t)+len);
393 memset(s,0,sizeof(stream_t));
394 s->fd=-1;
395 s->type=STREAMTYPE_MEMORY;
396 s->buf_pos=0; s->buf_len=len;
397 s->start_pos=0; s->end_pos=len;
398 stream_reset(s);
399 s->pos=len;
400 memcpy(s->buffer,data,len);
401 return s;
404 stream_t* new_stream(int fd,int type){
405 stream_t *s=malloc(sizeof(stream_t));
406 if(s==NULL) return NULL;
407 memset(s,0,sizeof(stream_t));
409 #if HAVE_WINSOCK2_H
411 WSADATA wsdata;
412 int temp = WSAStartup(0x0202, &wsdata); // there might be a better place for this (-> later)
413 mp_msg(MSGT_STREAM,MSGL_V,"WINSOCK2 init: %i\n", temp);
415 #endif
417 s->fd=fd;
418 s->type=type;
419 s->buf_pos=s->buf_len=0;
420 s->start_pos=s->end_pos=0;
421 s->priv=NULL;
422 s->url=NULL;
423 s->cache_pid=0;
424 stream_reset(s);
425 return s;
428 void free_stream(stream_t *s){
429 // printf("\n*** free_stream() called ***\n");
430 #ifdef CONFIG_STREAM_CACHE
431 if(s->cache_pid) {
432 cache_uninit(s);
434 #endif
435 if(s->close) s->close(s);
436 if(s->fd>0){
437 /* on unix we define closesocket to close
438 on windows however we have to distinguish between
439 network socket and file */
440 if(s->url && strstr(s->url,"://"))
441 closesocket(s->fd);
442 else close(s->fd);
444 #if HAVE_WINSOCK2_H
445 mp_msg(MSGT_STREAM,MSGL_V,"WINSOCK2 uninit\n");
446 WSACleanup(); // there might be a better place for this (-> later)
447 #endif
448 // Disabled atm, i don't like that. s->priv can be anything after all
449 // streams should destroy their priv on close
450 //if(s->priv) free(s->priv);
451 if(s->url) free(s->url);
452 free(s);
455 stream_t* new_ds_stream(demux_stream_t *ds) {
456 stream_t* s = new_stream(-1,STREAMTYPE_DS);
457 s->priv = ds;
458 return s;
461 void stream_set_interrupt_callback(int (*cb)(struct input_ctx *, int),
462 struct input_ctx *ctx)
464 stream_check_interrupt_cb = cb;
465 stream_check_interrupt_ctx = ctx;
468 int stream_check_interrupt(int time) {
469 if(!stream_check_interrupt_cb) return 0;
470 return stream_check_interrupt_cb(stream_check_interrupt_ctx, time);