Merge svn changes up to r30672
[mplayer/kovensky.git] / stream / stream.c
blob562675cb2936da6e93035a7137fe08b7ae42e5d6
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 "help_mp.h"
40 #include "osdep/shmem.h"
41 #include "network.h"
42 #include "stream.h"
43 #include "libmpdemux/demuxer.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, char *filename,
146 int mode, struct MPOpts *options,
147 int *file_format, int *ret,
148 char **redirected_url)
150 void* arg = NULL;
151 stream_t* s;
152 m_struct_t* desc = (m_struct_t*)sinfo->opts;
154 // Parse options
155 if(desc) {
156 arg = m_struct_alloc(desc);
157 if(sinfo->opts_url) {
158 m_option_t url_opt =
159 { "stream url", arg , CONF_TYPE_CUSTOM_URL, 0, 0 ,0, sinfo->opts };
160 if(m_option_parse(&url_opt,"stream url",filename,arg,M_CONFIG_FILE) < 0) {
161 mp_msg(MSGT_OPEN,MSGL_ERR, "URL parsing failed on url %s\n",filename);
162 m_struct_free(desc,arg);
163 return NULL;
167 s = new_stream(-2,-2);
168 s->opts = options;
169 s->url=strdup(filename);
170 s->flags |= mode;
171 *ret = sinfo->open(s,mode,arg,file_format);
172 if((*ret) != STREAM_OK) {
173 #ifdef CONFIG_NETWORK
174 if (*ret == STREAM_REDIRECTED && redirected_url) {
175 if (s->streaming_ctrl && s->streaming_ctrl->url
176 && s->streaming_ctrl->url->url)
177 *redirected_url = strdup(s->streaming_ctrl->url->url);
178 else
179 *redirected_url = NULL;
181 streaming_ctrl_free(s->streaming_ctrl);
182 #endif
183 free(s->url);
184 free(s);
185 return NULL;
187 if(s->type <= -2)
188 mp_msg(MSGT_OPEN,MSGL_WARN, "Warning streams need a type !!!!\n");
189 if(s->flags & MP_STREAM_SEEK && !s->seek)
190 s->flags &= ~MP_STREAM_SEEK;
191 if(s->seek && !(s->flags & MP_STREAM_SEEK))
192 s->flags |= MP_STREAM_SEEK;
194 s->mode = mode;
196 mp_msg(MSGT_OPEN,MSGL_V, "STREAM: [%s] %s\n",sinfo->name,filename);
197 mp_msg(MSGT_OPEN,MSGL_V, "STREAM: Description: %s\n",sinfo->info);
198 mp_msg(MSGT_OPEN,MSGL_V, "STREAM: Author: %s\n", sinfo->author);
199 mp_msg(MSGT_OPEN,MSGL_V, "STREAM: Comment: %s\n", sinfo->comment);
201 return s;
205 stream_t *open_stream_full(char *filename,int mode, struct MPOpts *options,
206 int* file_format)
208 int i,j,l,r;
209 const stream_info_t* sinfo;
210 stream_t* s;
211 char *redirected_url = NULL;
213 for(i = 0 ; auto_open_streams[i] ; i++) {
214 sinfo = auto_open_streams[i];
215 if(!sinfo->protocols) {
216 mp_msg(MSGT_OPEN,MSGL_WARN, "Stream type %s has protocols == NULL, it's a bug\n", sinfo->name);
217 continue;
219 for(j = 0 ; sinfo->protocols[j] ; j++) {
220 l = strlen(sinfo->protocols[j]);
221 // l == 0 => Don't do protocol matching (ie network and filenames)
222 if((l == 0 && !strstr(filename, "://")) ||
223 ((strncasecmp(sinfo->protocols[j],filename,l) == 0) &&
224 (strncmp("://",filename+l,3) == 0))) {
225 *file_format = DEMUXER_TYPE_UNKNOWN;
226 s = open_stream_plugin(sinfo,filename,mode,options,file_format,&r,
227 &redirected_url);
228 if(s) return s;
229 if(r == STREAM_REDIRECTED && redirected_url) {
230 mp_msg(MSGT_OPEN,MSGL_V, "[%s] open %s redirected to %s\n",
231 sinfo->info, filename, redirected_url);
232 s = open_stream_full(redirected_url, mode, options, file_format);
233 free(redirected_url);
234 return s;
236 else if(r != STREAM_UNSUPPORTED) {
237 mp_tmsg(MSGT_OPEN,MSGL_ERR, "Failed to open %s.\n",filename);
238 return NULL;
240 break;
245 mp_msg(MSGT_OPEN,MSGL_ERR, "No stream found to handle url %s\n",filename);
246 return NULL;
249 stream_t *open_output_stream(char *filename, struct MPOpts *options)
251 int file_format; //unused
252 if(!filename) {
253 mp_msg(MSGT_OPEN,MSGL_ERR,"open_output_stream(), NULL filename, report this bug\n");
254 return NULL;
257 return open_stream_full(filename,STREAM_WRITE,options,&file_format);
260 //=================== STREAMER =========================
262 int stream_fill_buffer(stream_t *s){
263 int len;
264 if (/*s->fd == NULL ||*/ s->eof) { return 0; }
265 switch(s->type){
266 case STREAMTYPE_STREAM:
267 #ifdef CONFIG_NETWORK
268 if( s->streaming_ctrl!=NULL && s->streaming_ctrl->streaming_read ) {
269 len=s->streaming_ctrl->streaming_read(s->fd,s->buffer,STREAM_BUFFER_SIZE, s->streaming_ctrl);
270 } else
271 #endif
272 if (s->fill_buffer)
273 len = s->fill_buffer(s, s->buffer, STREAM_BUFFER_SIZE);
274 else
275 len=read(s->fd,s->buffer,STREAM_BUFFER_SIZE);
276 break;
277 case STREAMTYPE_DS:
278 len = demux_read_data((demux_stream_t*)s->priv,s->buffer,STREAM_BUFFER_SIZE);
279 break;
282 default:
283 len= s->fill_buffer ? s->fill_buffer(s,s->buffer,STREAM_BUFFER_SIZE) : 0;
285 if(len<=0){ s->eof=1; return 0; }
286 s->buf_pos=0;
287 s->buf_len=len;
288 s->pos+=len;
289 // printf("[%d]",len);fflush(stdout);
290 return len;
293 int stream_write_buffer(stream_t *s, unsigned char *buf, int len) {
294 int rd;
295 if(!s->write_buffer)
296 return -1;
297 rd = s->write_buffer(s, buf, len);
298 if(rd < 0)
299 return -1;
300 s->pos += rd;
301 return rd;
304 int stream_seek_long(stream_t *s,off_t pos){
305 off_t newpos=0;
307 // if( mp_msg_test(MSGT_STREAM,MSGL_DBG3) ) printf("seek_long to 0x%X\n",(unsigned int)pos);
309 s->buf_pos=s->buf_len=0;
311 if(s->mode == STREAM_WRITE) {
312 if(!s->seek || !s->seek(s,pos))
313 return 0;
314 return 1;
317 if(s->sector_size)
318 newpos = (pos/s->sector_size)*s->sector_size;
319 else
320 newpos = pos&(~((off_t)STREAM_BUFFER_SIZE-1));
322 if( mp_msg_test(MSGT_STREAM,MSGL_DBG3) ){
323 mp_msg(MSGT_STREAM,MSGL_DBG3, "s->pos=%"PRIX64" newpos=%"PRIX64" new_bufpos=%"PRIX64" buflen=%X \n",
324 (int64_t)s->pos,(int64_t)newpos,(int64_t)pos,s->buf_len);
326 pos-=newpos;
328 if(newpos==0 || newpos!=s->pos){
329 switch(s->type){
330 case STREAMTYPE_STREAM:
331 //s->pos=newpos; // real seek
332 // Some streaming protocol allow to seek backward and forward
333 // A function call that return -1 can tell that the protocol
334 // doesn't support seeking.
335 #ifdef CONFIG_NETWORK
336 if(s->seek) { // new stream seek is much cleaner than streaming_ctrl one
337 if(!s->seek(s,newpos)) {
338 mp_msg(MSGT_STREAM,MSGL_ERR, "Seek failed\n");
339 return 0;
341 break;
344 if( s->streaming_ctrl!=NULL && s->streaming_ctrl->streaming_seek ) {
345 if( s->streaming_ctrl->streaming_seek( s->fd, pos, s->streaming_ctrl )<0 ) {
346 mp_msg(MSGT_STREAM,MSGL_INFO,"Stream not seekable!\n");
347 return 1;
349 break;
351 #endif
352 if(newpos<s->pos){
353 mp_msg(MSGT_STREAM,MSGL_INFO,"Cannot seek backward in linear streams!\n");
354 return 1;
356 while(s->pos<newpos){
357 if(stream_fill_buffer(s)<=0) break; // EOF
359 break;
360 default:
361 // This should at the beginning as soon as all streams are converted
362 if(!s->seek)
363 return 0;
364 // Now seek
365 if(!s->seek(s,newpos)) {
366 mp_msg(MSGT_STREAM,MSGL_ERR, "Seek failed\n");
367 return 0;
370 // putchar('.');fflush(stdout);
371 //} else {
372 // putchar('%');fflush(stdout);
375 s->eof = 0; // EOF reset when seek succeeds.
376 while (stream_fill_buffer(s) > 0) {
377 if(pos<=s->buf_len){
378 s->buf_pos=pos; // byte position in sector
379 return 1;
381 pos -= s->buf_len;
383 // Fill failed, but seek still is a success.
384 s->pos += pos;
385 s->buf_pos = 0;
386 s->buf_len = 0;
388 mp_msg(MSGT_STREAM,MSGL_V,
389 "stream_seek: Seek to/past EOF: no buffer preloaded.\n");
390 return 1;
394 void stream_reset(stream_t *s){
395 if(s->eof){
396 s->pos=0;
397 s->buf_pos=s->buf_len=0;
398 s->eof=0;
400 if(s->control) s->control(s,STREAM_CTRL_RESET,NULL);
401 //stream_seek(s,0);
404 int stream_control(stream_t *s, int cmd, void *arg){
405 if(!s->control) return STREAM_UNSUPPORTED;
406 #ifdef CONFIG_STREAM_CACHE
407 if (s->cache_pid)
408 return cache_do_control(s, cmd, arg);
409 #endif
410 return s->control(s, cmd, arg);
413 stream_t* new_memory_stream(unsigned char* data,int len){
414 stream_t *s;
416 if(len < 0)
417 return NULL;
418 s=calloc(1, sizeof(stream_t)+len);
419 s->fd=-1;
420 s->type=STREAMTYPE_MEMORY;
421 s->buf_pos=0; s->buf_len=len;
422 s->start_pos=0; s->end_pos=len;
423 stream_reset(s);
424 s->pos=len;
425 memcpy(s->buffer,data,len);
426 return s;
429 stream_t* new_stream(int fd,int type){
430 stream_t *s=calloc(1, sizeof(stream_t));
431 if(s==NULL) return NULL;
433 #if HAVE_WINSOCK2_H
435 WSADATA wsdata;
436 int temp = WSAStartup(0x0202, &wsdata); // there might be a better place for this (-> later)
437 mp_msg(MSGT_STREAM,MSGL_V,"WINSOCK2 init: %i\n", temp);
439 #endif
441 s->fd=fd;
442 s->type=type;
443 s->buf_pos=s->buf_len=0;
444 s->start_pos=s->end_pos=0;
445 s->priv=NULL;
446 s->url=NULL;
447 s->cache_pid=0;
448 stream_reset(s);
449 return s;
452 void free_stream(stream_t *s){
453 // printf("\n*** free_stream() called ***\n");
454 #ifdef CONFIG_STREAM_CACHE
455 cache_uninit(s);
456 #endif
457 if(s->close) s->close(s);
458 if(s->fd>0){
459 /* on unix we define closesocket to close
460 on windows however we have to distinguish between
461 network socket and file */
462 if(s->url && strstr(s->url,"://"))
463 closesocket(s->fd);
464 else close(s->fd);
466 #if HAVE_WINSOCK2_H
467 mp_msg(MSGT_STREAM,MSGL_V,"WINSOCK2 uninit\n");
468 WSACleanup(); // there might be a better place for this (-> later)
469 #endif
470 // Disabled atm, i don't like that. s->priv can be anything after all
471 // streams should destroy their priv on close
472 //if(s->priv) free(s->priv);
473 if(s->url) free(s->url);
474 free(s);
477 stream_t* new_ds_stream(demux_stream_t *ds) {
478 stream_t* s = new_stream(-1,STREAMTYPE_DS);
479 s->priv = ds;
480 return s;
483 void stream_set_interrupt_callback(int (*cb)(struct input_ctx *, int),
484 struct input_ctx *ctx)
486 stream_check_interrupt_cb = cb;
487 stream_check_interrupt_ctx = ctx;
490 int stream_check_interrupt(int time) {
491 if(!stream_check_interrupt_cb) return 0;
492 return stream_check_interrupt_cb(stream_check_interrupt_ctx, time);