Add explanatory comments to the #endif part of multiple inclusion guards.
[mplayer/greg.git] / stream / stream.c
blob84cd0a2e41a2ad3caa6484927612cdc0fe2a3999
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 <signal.h>
14 #include <strings.h>
16 #include "config.h"
18 #ifndef HAVE_WINSOCK2
19 #define closesocket close
20 #else
21 #include <winsock2.h>
22 #endif
24 #include "mp_msg.h"
25 #include "help_mp.h"
26 #include "osdep/shmem.h"
28 #include "stream.h"
29 #include "libmpdemux/demuxer.h"
31 #include "m_option.h"
32 #include "m_struct.h"
34 #include "cache2.h"
36 //#include "vcd_read_bincue.h"
38 #ifdef HAVE_VCD
39 extern const stream_info_t stream_info_vcd;
40 #endif
41 #ifdef HAVE_CDDA
42 extern const stream_info_t stream_info_cdda;
43 #endif
44 #ifdef MPLAYER_NETWORK
45 extern const stream_info_t stream_info_netstream;
46 extern const stream_info_t stream_info_pnm;
47 extern const stream_info_t stream_info_asf;
48 extern const stream_info_t stream_info_rtsp;
49 extern const stream_info_t stream_info_rtp;
50 extern const stream_info_t stream_info_udp;
51 extern const stream_info_t stream_info_http1;
52 extern const stream_info_t stream_info_http2;
53 #endif
54 #ifdef HAS_DVBIN_SUPPORT
55 extern const stream_info_t stream_info_dvb;
56 #endif
57 #ifdef USE_TV
58 extern const stream_info_t stream_info_tv;
59 #endif
60 #ifdef USE_RADIO
61 extern const stream_info_t stream_info_radio;
62 #endif
63 #ifdef HAVE_PVR
64 extern const stream_info_t stream_info_pvr;
65 #endif
66 #ifdef HAVE_FTP
67 extern const stream_info_t stream_info_ftp;
68 #endif
69 #ifdef HAVE_VSTREAM
70 extern const stream_info_t stream_info_vstream;
71 #endif
72 #ifdef USE_DVDNAV
73 extern const stream_info_t stream_info_dvdnav;
74 #endif
75 #ifdef LIBSMBCLIENT
76 extern const stream_info_t stream_info_smb;
77 #endif
78 #ifdef STREAMING_LIVE555
79 extern const stream_info_t stream_info_sdp;
80 extern const stream_info_t stream_info_rtsp_sip;
81 #endif
83 extern const stream_info_t stream_info_cue;
84 extern const stream_info_t stream_info_null;
85 extern const stream_info_t stream_info_mf;
86 extern const stream_info_t stream_info_file;
87 #ifdef USE_DVDREAD
88 extern const stream_info_t stream_info_ifo;
89 extern const stream_info_t stream_info_dvd;
90 #endif
92 static const stream_info_t* const auto_open_streams[] = {
93 #ifdef HAVE_VCD
94 &stream_info_vcd,
95 #endif
96 #ifdef HAVE_CDDA
97 &stream_info_cdda,
98 #endif
99 #ifdef MPLAYER_NETWORK
100 &stream_info_netstream,
101 &stream_info_http1,
102 &stream_info_asf,
103 &stream_info_pnm,
104 &stream_info_rtsp,
105 #ifdef STREAMING_LIVE555
106 &stream_info_sdp,
107 &stream_info_rtsp_sip,
108 #endif
109 &stream_info_rtp,
110 &stream_info_udp,
111 &stream_info_http2,
112 #endif
113 #ifdef HAS_DVBIN_SUPPORT
114 &stream_info_dvb,
115 #endif
116 #ifdef USE_TV
117 &stream_info_tv,
118 #endif
119 #ifdef USE_RADIO
120 &stream_info_radio,
121 #endif
122 #ifdef HAVE_PVR
123 &stream_info_pvr,
124 #endif
125 #ifdef HAVE_FTP
126 &stream_info_ftp,
127 #endif
128 #ifdef HAVE_VSTREAM
129 &stream_info_vstream,
130 #endif
131 #ifdef LIBSMBCLIENT
132 &stream_info_smb,
133 #endif
134 &stream_info_cue,
135 #ifdef USE_DVDREAD
136 &stream_info_ifo,
137 &stream_info_dvd,
138 #endif
139 #ifdef USE_DVDNAV
140 &stream_info_dvdnav,
141 #endif
143 &stream_info_null,
144 &stream_info_mf,
145 &stream_info_file,
146 NULL
149 stream_t* open_stream_plugin(const stream_info_t* sinfo,char* filename,int mode,
150 char** options, int* file_format, int* ret,
151 char** redirected_url) {
152 void* arg = NULL;
153 stream_t* s;
154 m_struct_t* desc = (m_struct_t*)sinfo->opts;
156 // Parse options
157 if(desc) {
158 arg = m_struct_alloc(desc);
159 if(sinfo->opts_url) {
160 m_option_t url_opt =
161 { "stream url", arg , CONF_TYPE_CUSTOM_URL, 0, 0 ,0, sinfo->opts };
162 if(m_option_parse(&url_opt,"stream url",filename,arg,M_CONFIG_FILE) < 0) {
163 mp_msg(MSGT_OPEN,MSGL_ERR, "URL parsing failed on url %s\n",filename);
164 m_struct_free(desc,arg);
165 return NULL;
168 if(options) {
169 int i;
170 for(i = 0 ; options[i] != NULL ; i += 2) {
171 mp_msg(MSGT_OPEN,MSGL_DBG2, "Set stream arg %s=%s\n",
172 options[i],options[i+1]);
173 if(!m_struct_set(desc,arg,options[i],options[i+1]))
174 mp_msg(MSGT_OPEN,MSGL_WARN, "Failed to set stream option %s=%s\n",
175 options[i],options[i+1]);
179 s = new_stream(-2,-2);
180 s->url=strdup(filename);
181 s->flags |= mode;
182 *ret = sinfo->open(s,mode,arg,file_format);
183 if((*ret) != STREAM_OK) {
184 #ifdef MPLAYER_NETWORK
185 if (*ret == STREAM_REDIRECTED && redirected_url) {
186 if (s->streaming_ctrl && s->streaming_ctrl->url
187 && s->streaming_ctrl->url->url)
188 *redirected_url = strdup(s->streaming_ctrl->url->url);
189 else
190 *redirected_url = NULL;
192 streaming_ctrl_free(s->streaming_ctrl);
193 #endif
194 free(s->url);
195 free(s);
196 return NULL;
198 if(s->type <= -2)
199 mp_msg(MSGT_OPEN,MSGL_WARN, "Warning streams need a type !!!!\n");
200 if(s->flags & STREAM_SEEK && !s->seek)
201 s->flags &= ~STREAM_SEEK;
202 if(s->seek && !(s->flags & STREAM_SEEK))
203 s->flags |= STREAM_SEEK;
205 s->mode = mode;
207 mp_msg(MSGT_OPEN,MSGL_V, "STREAM: [%s] %s\n",sinfo->name,filename);
208 mp_msg(MSGT_OPEN,MSGL_V, "STREAM: Description: %s\n",sinfo->info);
209 mp_msg(MSGT_OPEN,MSGL_V, "STREAM: Author: %s\n", sinfo->author);
210 mp_msg(MSGT_OPEN,MSGL_V, "STREAM: Comment: %s\n", sinfo->comment);
212 return s;
216 stream_t* open_stream_full(char* filename,int mode, char** options, int* file_format) {
217 int i,j,l,r;
218 const stream_info_t* sinfo;
219 stream_t* s;
220 char *redirected_url = NULL;
222 for(i = 0 ; auto_open_streams[i] ; i++) {
223 sinfo = auto_open_streams[i];
224 if(!sinfo->protocols) {
225 mp_msg(MSGT_OPEN,MSGL_WARN, "Stream type %s has protocols == NULL, it's a bug\n", sinfo->name);
226 continue;
228 for(j = 0 ; sinfo->protocols[j] ; j++) {
229 l = strlen(sinfo->protocols[j]);
230 // l == 0 => Don't do protocol matching (ie network and filenames)
231 if((l == 0 && !strstr(filename, "://")) ||
232 ((strncasecmp(sinfo->protocols[j],filename,l) == 0) &&
233 (strncmp("://",filename+l,3) == 0))) {
234 *file_format = DEMUXER_TYPE_UNKNOWN;
235 s = open_stream_plugin(sinfo,filename,mode,options,file_format,&r,
236 &redirected_url);
237 if(s) return s;
238 if(r == STREAM_REDIRECTED && redirected_url) {
239 mp_msg(MSGT_OPEN,MSGL_V, "[%s] open %s redirected to %s\n",
240 sinfo->info, filename, redirected_url);
241 s = open_stream_full(redirected_url, mode, options, file_format);
242 free(redirected_url);
243 return s;
245 else if(r != STREAM_UNSUPPORTED) {
246 mp_msg(MSGT_OPEN,MSGL_ERR, MSGTR_FailedToOpen,filename);
247 return NULL;
249 break;
254 mp_msg(MSGT_OPEN,MSGL_ERR, "No stream found to handle url %s\n",filename);
255 return NULL;
258 stream_t* open_output_stream(char* filename,char** options) {
259 int file_format; //unused
260 if(!filename) {
261 mp_msg(MSGT_OPEN,MSGL_ERR,"open_output_stream(), NULL filename, report this bug\n");
262 return NULL;
265 return open_stream_full(filename,STREAM_WRITE,options,&file_format);
268 //=================== STREAMER =========================
270 int stream_fill_buffer(stream_t *s){
271 int len;
272 if (/*s->fd == NULL ||*/ s->eof) { s->buf_pos = s->buf_len = 0; return 0; }
273 switch(s->type){
274 case STREAMTYPE_STREAM:
275 #ifdef MPLAYER_NETWORK
276 if( s->streaming_ctrl!=NULL && s->streaming_ctrl->streaming_read ) {
277 len=s->streaming_ctrl->streaming_read(s->fd,s->buffer,STREAM_BUFFER_SIZE, s->streaming_ctrl);break;
278 } else {
279 len=read(s->fd,s->buffer,STREAM_BUFFER_SIZE);break;
281 #else
282 len=read(s->fd,s->buffer,STREAM_BUFFER_SIZE);break;
283 #endif
284 case STREAMTYPE_DS:
285 len = demux_read_data((demux_stream_t*)s->priv,s->buffer,STREAM_BUFFER_SIZE);
286 break;
289 default:
290 len= s->fill_buffer ? s->fill_buffer(s,s->buffer,STREAM_BUFFER_SIZE) : 0;
292 if(len<=0){ s->eof=1; s->buf_pos=s->buf_len=0; return 0; }
293 s->buf_pos=0;
294 s->buf_len=len;
295 s->pos+=len;
296 // printf("[%d]",len);fflush(stdout);
297 return len;
300 int stream_write_buffer(stream_t *s, unsigned char *buf, int len) {
301 int rd;
302 if(!s->write_buffer)
303 return -1;
304 rd = s->write_buffer(s, buf, len);
305 if(rd < 0)
306 return -1;
307 s->pos += rd;
308 return rd;
311 int stream_seek_long(stream_t *s,off_t pos){
312 off_t newpos=0;
314 // if( mp_msg_test(MSGT_STREAM,MSGL_DBG3) ) printf("seek_long to 0x%X\n",(unsigned int)pos);
316 s->buf_pos=s->buf_len=0;
318 if(s->mode == STREAM_WRITE) {
319 if(!s->seek || !s->seek(s,pos))
320 return 0;
321 return 1;
324 if(s->sector_size)
325 newpos = (pos/s->sector_size)*s->sector_size;
326 else
327 newpos = pos&(~((off_t)STREAM_BUFFER_SIZE-1));
329 if( mp_msg_test(MSGT_STREAM,MSGL_DBG3) ){
330 mp_msg(MSGT_STREAM,MSGL_DBG3, "s->pos=%"PRIX64" newpos=%"PRIX64" new_bufpos=%"PRIX64" buflen=%X \n",
331 (int64_t)s->pos,(int64_t)newpos,(int64_t)pos,s->buf_len);
333 pos-=newpos;
335 if(newpos==0 || newpos!=s->pos){
336 switch(s->type){
337 case STREAMTYPE_STREAM:
338 //s->pos=newpos; // real seek
339 // Some streaming protocol allow to seek backward and forward
340 // A function call that return -1 can tell that the protocol
341 // doesn't support seeking.
342 #ifdef MPLAYER_NETWORK
343 if(s->seek) { // new stream seek is much cleaner than streaming_ctrl one
344 if(!s->seek(s,newpos)) {
345 mp_msg(MSGT_STREAM,MSGL_ERR, "Seek failed\n");
346 return 0;
348 break;
351 if( s->streaming_ctrl!=NULL && s->streaming_ctrl->streaming_seek ) {
352 if( s->streaming_ctrl->streaming_seek( s->fd, pos, s->streaming_ctrl )<0 ) {
353 mp_msg(MSGT_STREAM,MSGL_INFO,"Stream not seekable!\n");
354 return 1;
357 #else
358 if(newpos<s->pos){
359 mp_msg(MSGT_STREAM,MSGL_INFO,"Cannot seek backward in linear streams!\n");
360 return 1;
362 while(s->pos<newpos){
363 if(stream_fill_buffer(s)<=0) break; // EOF
365 #endif
366 break;
367 default:
368 // This should at the beginning as soon as all streams are converted
369 if(!s->seek)
370 return 0;
371 // Now seek
372 if(!s->seek(s,newpos)) {
373 mp_msg(MSGT_STREAM,MSGL_ERR, "Seek failed\n");
374 return 0;
377 // putchar('.');fflush(stdout);
378 //} else {
379 // putchar('%');fflush(stdout);
382 while(stream_fill_buffer(s) > 0 && pos >= 0) {
383 if(pos<=s->buf_len){
384 s->buf_pos=pos; // byte position in sector
385 return 1;
387 pos -= s->buf_len;
390 // if(pos==s->buf_len) printf("XXX Seek to last byte of file -> EOF\n");
392 mp_msg(MSGT_STREAM,MSGL_V,"stream_seek: WARNING! Can't seek to 0x%"PRIX64" !\n",(int64_t)(pos+newpos));
393 return 0;
397 void stream_reset(stream_t *s){
398 if(s->eof){
399 s->pos=0; //ftell(f);
400 // s->buf_pos=s->buf_len=0;
401 s->eof=0;
403 if(s->control) s->control(s,STREAM_CTRL_RESET,NULL);
404 //stream_seek(s,0);
407 int stream_control(stream_t *s, int cmd, void *arg){
408 if(!s->control) return STREAM_UNSUPPORTED;
409 return s->control(s, cmd, arg);
412 stream_t* new_memory_stream(unsigned char* data,int len){
413 stream_t *s;
415 if(len < 0)
416 return NULL;
417 s=malloc(sizeof(stream_t)+len);
418 memset(s,0,sizeof(stream_t));
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=malloc(sizeof(stream_t));
431 if(s==NULL) return NULL;
432 memset(s,0,sizeof(stream_t));
434 #ifdef HAVE_WINSOCK2
436 WSADATA wsdata;
437 int temp = WSAStartup(0x0202, &wsdata); // there might be a better place for this (-> later)
438 mp_msg(MSGT_STREAM,MSGL_V,"WINSOCK2 init: %i\n", temp);
440 #endif
442 s->fd=fd;
443 s->type=type;
444 s->buf_pos=s->buf_len=0;
445 s->start_pos=s->end_pos=0;
446 s->priv=NULL;
447 s->url=NULL;
448 s->cache_pid=0;
449 stream_reset(s);
450 return s;
453 void free_stream(stream_t *s){
454 // printf("\n*** free_stream() called ***\n");
455 #ifdef USE_STREAM_CACHE
456 if(s->cache_pid) {
457 cache_uninit(s);
459 #endif
460 if(s->close) s->close(s);
461 if(s->fd>0){
462 /* on unix we define closesocket to close
463 on windows however we have to distinguish between
464 network socket and file */
465 if(s->url && strstr(s->url,"://"))
466 closesocket(s->fd);
467 else close(s->fd);
469 #ifdef HAVE_WINSOCK2
470 mp_msg(MSGT_STREAM,MSGL_V,"WINSOCK2 uninit\n");
471 WSACleanup(); // there might be a better place for this (-> later)
472 #endif
473 // Disabled atm, i don't like that. s->priv can be anything after all
474 // streams should destroy their priv on close
475 //if(s->priv) free(s->priv);
476 if(s->url) free(s->url);
477 free(s);
480 stream_t* new_ds_stream(demux_stream_t *ds) {
481 stream_t* s = new_stream(-1,STREAMTYPE_DS);
482 s->priv = ds;
483 return s;