Rename exit_player() use outside core to exit_player_bad()
[mplayer.git] / stream / stream.c
blob47d55be71fdac2269282b0f445e1e7880b572eb7
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 #ifndef HAVE_WINSOCK2
18 #define closesocket close
19 #else
20 #include <winsock2.h>
21 #endif
23 #include "mp_msg.h"
24 #include "help_mp.h"
25 #include "osdep/shmem.h"
27 #include "stream.h"
28 #include "libmpdemux/demuxer.h"
30 #include "m_option.h"
31 #include "m_struct.h"
33 #include "cache2.h"
35 //#include "vcd_read_bincue.h"
37 struct input_ctx;
38 static int (*stream_check_interrupt_cb)(struct input_ctx *ctx, int time);
39 static struct input_ctx *stream_check_interrupt_ctx;
41 extern const stream_info_t stream_info_vcd;
42 extern const stream_info_t stream_info_cdda;
43 extern const stream_info_t stream_info_netstream;
44 extern const stream_info_t stream_info_pnm;
45 extern const stream_info_t stream_info_asf;
46 extern const stream_info_t stream_info_rtsp;
47 extern const stream_info_t stream_info_rtp;
48 extern const stream_info_t stream_info_udp;
49 extern const stream_info_t stream_info_http1;
50 extern const stream_info_t stream_info_http2;
51 extern const stream_info_t stream_info_dvb;
52 extern const stream_info_t stream_info_tv;
53 extern const stream_info_t stream_info_radio;
54 extern const stream_info_t stream_info_pvr;
55 extern const stream_info_t stream_info_ftp;
56 extern const stream_info_t stream_info_vstream;
57 extern const stream_info_t stream_info_dvdnav;
58 extern const stream_info_t stream_info_smb;
59 extern const stream_info_t stream_info_sdp;
60 extern const stream_info_t stream_info_rtsp_sip;
62 extern const stream_info_t stream_info_cue;
63 extern const stream_info_t stream_info_null;
64 extern const stream_info_t stream_info_mf;
65 extern const stream_info_t stream_info_file;
66 extern const stream_info_t stream_info_ifo;
67 extern const stream_info_t stream_info_dvd;
69 static const stream_info_t* const auto_open_streams[] = {
70 #ifdef CONFIG_VCD
71 &stream_info_vcd,
72 #endif
73 #ifdef CONFIG_CDDA
74 &stream_info_cdda,
75 #endif
76 #ifdef CONFIG_NETWORK
77 &stream_info_netstream,
78 &stream_info_http1,
79 &stream_info_asf,
80 &stream_info_pnm,
81 &stream_info_rtsp,
82 #ifdef CONFIG_LIVE555
83 &stream_info_sdp,
84 &stream_info_rtsp_sip,
85 #endif
86 &stream_info_rtp,
87 &stream_info_udp,
88 &stream_info_http2,
89 #endif
90 #ifdef CONFIG_DVBIN
91 &stream_info_dvb,
92 #endif
93 #ifdef CONFIG_TV
94 &stream_info_tv,
95 #endif
96 #ifdef CONFIG_RADIO
97 &stream_info_radio,
98 #endif
99 #ifdef CONFIG_PVR
100 &stream_info_pvr,
101 #endif
102 #ifdef CONFIG_FTP
103 &stream_info_ftp,
104 #endif
105 #ifdef CONFIG_VSTREAM
106 &stream_info_vstream,
107 #endif
108 #ifdef CONFIG_LIBSMBCLIENT
109 &stream_info_smb,
110 #endif
111 &stream_info_cue,
112 #ifdef CONFIG_DVDREAD
113 &stream_info_ifo,
114 &stream_info_dvd,
115 #endif
116 #ifdef CONFIG_DVDNAV
117 &stream_info_dvdnav,
118 #endif
120 &stream_info_null,
121 &stream_info_mf,
122 &stream_info_file,
123 NULL
126 static stream_t *open_stream_plugin(const stream_info_t *sinfo, char *filename,
127 int mode, struct MPOpts *options,
128 int *file_format, int *ret,
129 char **redirected_url)
131 void* arg = NULL;
132 stream_t* s;
133 m_struct_t* desc = (m_struct_t*)sinfo->opts;
135 // Parse options
136 if(desc) {
137 arg = m_struct_alloc(desc);
138 if(sinfo->opts_url) {
139 m_option_t url_opt =
140 { "stream url", arg , CONF_TYPE_CUSTOM_URL, 0, 0 ,0, sinfo->opts };
141 if(m_option_parse(&url_opt,"stream url",filename,arg,M_CONFIG_FILE) < 0) {
142 mp_msg(MSGT_OPEN,MSGL_ERR, "URL parsing failed on url %s\n",filename);
143 m_struct_free(desc,arg);
144 return NULL;
148 s = new_stream(-2,-2);
149 s->opts = options;
150 s->url=strdup(filename);
151 s->flags |= mode;
152 *ret = sinfo->open(s,mode,arg,file_format);
153 if((*ret) != STREAM_OK) {
154 #ifdef CONFIG_NETWORK
155 if (*ret == STREAM_REDIRECTED && redirected_url) {
156 if (s->streaming_ctrl && s->streaming_ctrl->url
157 && s->streaming_ctrl->url->url)
158 *redirected_url = strdup(s->streaming_ctrl->url->url);
159 else
160 *redirected_url = NULL;
162 streaming_ctrl_free(s->streaming_ctrl);
163 #endif
164 free(s->url);
165 free(s);
166 return NULL;
168 if(s->type <= -2)
169 mp_msg(MSGT_OPEN,MSGL_WARN, "Warning streams need a type !!!!\n");
170 if(s->flags & STREAM_SEEK && !s->seek)
171 s->flags &= ~STREAM_SEEK;
172 if(s->seek && !(s->flags & STREAM_SEEK))
173 s->flags |= STREAM_SEEK;
175 s->mode = mode;
177 mp_msg(MSGT_OPEN,MSGL_V, "STREAM: [%s] %s\n",sinfo->name,filename);
178 mp_msg(MSGT_OPEN,MSGL_V, "STREAM: Description: %s\n",sinfo->info);
179 mp_msg(MSGT_OPEN,MSGL_V, "STREAM: Author: %s\n", sinfo->author);
180 mp_msg(MSGT_OPEN,MSGL_V, "STREAM: Comment: %s\n", sinfo->comment);
182 return s;
186 stream_t *open_stream_full(char *filename,int mode, struct MPOpts *options,
187 int* file_format)
189 int i,j,l,r;
190 const stream_info_t* sinfo;
191 stream_t* s;
192 char *redirected_url = NULL;
194 for(i = 0 ; auto_open_streams[i] ; i++) {
195 sinfo = auto_open_streams[i];
196 if(!sinfo->protocols) {
197 mp_msg(MSGT_OPEN,MSGL_WARN, "Stream type %s has protocols == NULL, it's a bug\n", sinfo->name);
198 continue;
200 for(j = 0 ; sinfo->protocols[j] ; j++) {
201 l = strlen(sinfo->protocols[j]);
202 // l == 0 => Don't do protocol matching (ie network and filenames)
203 if((l == 0 && !strstr(filename, "://")) ||
204 ((strncasecmp(sinfo->protocols[j],filename,l) == 0) &&
205 (strncmp("://",filename+l,3) == 0))) {
206 *file_format = DEMUXER_TYPE_UNKNOWN;
207 s = open_stream_plugin(sinfo,filename,mode,options,file_format,&r,
208 &redirected_url);
209 if(s) return s;
210 if(r == STREAM_REDIRECTED && redirected_url) {
211 mp_msg(MSGT_OPEN,MSGL_V, "[%s] open %s redirected to %s\n",
212 sinfo->info, filename, redirected_url);
213 s = open_stream_full(redirected_url, mode, options, file_format);
214 free(redirected_url);
215 return s;
217 else if(r != STREAM_UNSUPPORTED) {
218 mp_msg(MSGT_OPEN,MSGL_ERR, MSGTR_FailedToOpen,filename);
219 return NULL;
221 break;
226 mp_msg(MSGT_OPEN,MSGL_ERR, "No stream found to handle url %s\n",filename);
227 return NULL;
230 stream_t *open_output_stream(char *filename, struct MPOpts *options)
232 int file_format; //unused
233 if(!filename) {
234 mp_msg(MSGT_OPEN,MSGL_ERR,"open_output_stream(), NULL filename, report this bug\n");
235 return NULL;
238 return open_stream_full(filename,STREAM_WRITE,options,&file_format);
241 //=================== STREAMER =========================
243 int stream_fill_buffer(stream_t *s){
244 int len;
245 if (/*s->fd == NULL ||*/ s->eof) { s->buf_pos = s->buf_len = 0; return 0; }
246 switch(s->type){
247 case STREAMTYPE_STREAM:
248 #ifdef CONFIG_NETWORK
249 if( s->streaming_ctrl!=NULL && s->streaming_ctrl->streaming_read ) {
250 len=s->streaming_ctrl->streaming_read(s->fd,s->buffer,STREAM_BUFFER_SIZE, s->streaming_ctrl);break;
251 } else {
252 len=read(s->fd,s->buffer,STREAM_BUFFER_SIZE);break;
254 #else
255 len=read(s->fd,s->buffer,STREAM_BUFFER_SIZE);break;
256 #endif
257 case STREAMTYPE_DS:
258 len = demux_read_data((demux_stream_t*)s->priv,s->buffer,STREAM_BUFFER_SIZE);
259 break;
262 default:
263 len= s->fill_buffer ? s->fill_buffer(s,s->buffer,STREAM_BUFFER_SIZE) : 0;
265 if(len<=0){ s->eof=1; s->buf_pos=s->buf_len=0; return 0; }
266 s->buf_pos=0;
267 s->buf_len=len;
268 s->pos+=len;
269 // printf("[%d]",len);fflush(stdout);
270 return len;
273 int stream_write_buffer(stream_t *s, unsigned char *buf, int len) {
274 int rd;
275 if(!s->write_buffer)
276 return -1;
277 rd = s->write_buffer(s, buf, len);
278 if(rd < 0)
279 return -1;
280 s->pos += rd;
281 return rd;
284 int stream_seek_long(stream_t *s,off_t pos){
285 off_t newpos=0;
287 // if( mp_msg_test(MSGT_STREAM,MSGL_DBG3) ) printf("seek_long to 0x%X\n",(unsigned int)pos);
289 s->buf_pos=s->buf_len=0;
291 if(s->mode == STREAM_WRITE) {
292 if(!s->seek || !s->seek(s,pos))
293 return 0;
294 return 1;
297 if(s->sector_size)
298 newpos = (pos/s->sector_size)*s->sector_size;
299 else
300 newpos = pos&(~((off_t)STREAM_BUFFER_SIZE-1));
302 if( mp_msg_test(MSGT_STREAM,MSGL_DBG3) ){
303 mp_msg(MSGT_STREAM,MSGL_DBG3, "s->pos=%"PRIX64" newpos=%"PRIX64" new_bufpos=%"PRIX64" buflen=%X \n",
304 (int64_t)s->pos,(int64_t)newpos,(int64_t)pos,s->buf_len);
306 pos-=newpos;
308 if(newpos==0 || newpos!=s->pos){
309 switch(s->type){
310 case STREAMTYPE_STREAM:
311 //s->pos=newpos; // real seek
312 // Some streaming protocol allow to seek backward and forward
313 // A function call that return -1 can tell that the protocol
314 // doesn't support seeking.
315 #ifdef CONFIG_NETWORK
316 if(s->seek) { // new stream seek is much cleaner than streaming_ctrl one
317 if(!s->seek(s,newpos)) {
318 mp_msg(MSGT_STREAM,MSGL_ERR, "Seek failed\n");
319 return 0;
321 break;
324 if( s->streaming_ctrl!=NULL && s->streaming_ctrl->streaming_seek ) {
325 if( s->streaming_ctrl->streaming_seek( s->fd, pos, s->streaming_ctrl )<0 ) {
326 mp_msg(MSGT_STREAM,MSGL_INFO,"Stream not seekable!\n");
327 return 1;
330 #else
331 if(newpos<s->pos){
332 mp_msg(MSGT_STREAM,MSGL_INFO,"Cannot seek backward in linear streams!\n");
333 return 1;
335 while(s->pos<newpos){
336 if(stream_fill_buffer(s)<=0) break; // EOF
338 #endif
339 break;
340 default:
341 // This should at the beginning as soon as all streams are converted
342 if(!s->seek)
343 return 0;
344 // Now seek
345 if(!s->seek(s,newpos)) {
346 mp_msg(MSGT_STREAM,MSGL_ERR, "Seek failed\n");
347 return 0;
350 // putchar('.');fflush(stdout);
351 //} else {
352 // putchar('%');fflush(stdout);
355 while(stream_fill_buffer(s) > 0 && pos >= 0) {
356 if(pos<=s->buf_len){
357 s->buf_pos=pos; // byte position in sector
358 return 1;
360 pos -= s->buf_len;
363 // if(pos==s->buf_len) printf("XXX Seek to last byte of file -> EOF\n");
365 mp_msg(MSGT_STREAM,MSGL_V,"stream_seek: WARNING! Can't seek to 0x%"PRIX64" !\n",(int64_t)(pos+newpos));
366 return 0;
370 void stream_reset(stream_t *s){
371 if(s->eof){
372 s->pos=0; //ftell(f);
373 // s->buf_pos=s->buf_len=0;
374 s->eof=0;
376 if(s->control) s->control(s,STREAM_CTRL_RESET,NULL);
377 //stream_seek(s,0);
380 int stream_control(stream_t *s, int cmd, void *arg){
381 if(!s->control) return STREAM_UNSUPPORTED;
382 #ifdef CONFIG_STREAM_CACHE
383 if (s->cache_pid)
384 return cache_do_control(s, cmd, arg);
385 #endif
386 return s->control(s, cmd, arg);
389 stream_t* new_memory_stream(unsigned char* data,int len){
390 stream_t *s;
392 if(len < 0)
393 return NULL;
394 s=malloc(sizeof(stream_t)+len);
395 memset(s,0,sizeof(stream_t));
396 s->fd=-1;
397 s->type=STREAMTYPE_MEMORY;
398 s->buf_pos=0; s->buf_len=len;
399 s->start_pos=0; s->end_pos=len;
400 stream_reset(s);
401 s->pos=len;
402 memcpy(s->buffer,data,len);
403 return s;
406 stream_t* new_stream(int fd,int type){
407 stream_t *s=malloc(sizeof(stream_t));
408 if(s==NULL) return NULL;
409 memset(s,0,sizeof(stream_t));
411 #ifdef HAVE_WINSOCK2
413 WSADATA wsdata;
414 int temp = WSAStartup(0x0202, &wsdata); // there might be a better place for this (-> later)
415 mp_msg(MSGT_STREAM,MSGL_V,"WINSOCK2 init: %i\n", temp);
417 #endif
419 s->fd=fd;
420 s->type=type;
421 s->buf_pos=s->buf_len=0;
422 s->start_pos=s->end_pos=0;
423 s->priv=NULL;
424 s->url=NULL;
425 s->cache_pid=0;
426 stream_reset(s);
427 return s;
430 void free_stream(stream_t *s){
431 // printf("\n*** free_stream() called ***\n");
432 #ifdef CONFIG_STREAM_CACHE
433 if(s->cache_pid) {
434 cache_uninit(s);
436 #endif
437 if(s->close) s->close(s);
438 if(s->fd>0){
439 /* on unix we define closesocket to close
440 on windows however we have to distinguish between
441 network socket and file */
442 if(s->url && strstr(s->url,"://"))
443 closesocket(s->fd);
444 else close(s->fd);
446 #ifdef HAVE_WINSOCK2
447 mp_msg(MSGT_STREAM,MSGL_V,"WINSOCK2 uninit\n");
448 WSACleanup(); // there might be a better place for this (-> later)
449 #endif
450 // Disabled atm, i don't like that. s->priv can be anything after all
451 // streams should destroy their priv on close
452 //if(s->priv) free(s->priv);
453 if(s->url) free(s->url);
454 free(s);
457 stream_t* new_ds_stream(demux_stream_t *ds) {
458 stream_t* s = new_stream(-1,STREAMTYPE_DS);
459 s->priv = ds;
460 return s;
463 void stream_set_interrupt_callback(int (*cb)(struct input_ctx *, int),
464 struct input_ctx *ctx)
466 stream_check_interrupt_cb = cb;
467 stream_check_interrupt_ctx = ctx;
470 int stream_check_interrupt(int time) {
471 if(!stream_check_interrupt_cb) return 0;
472 return stream_check_interrupt_cb(stream_check_interrupt_ctx, time);