Give better name to Inverse_Table_6_9
[mplayer/glamo.git] / stream / stream.c
blob85b0a1f9072b8284de68fb185db5cf2fbb6aca5f
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 static int (*stream_check_interrupt_cb)(int time) = NULL;
37 extern const stream_info_t stream_info_vcd;
38 extern const stream_info_t stream_info_cdda;
39 extern const stream_info_t stream_info_netstream;
40 extern const stream_info_t stream_info_pnm;
41 extern const stream_info_t stream_info_asf;
42 extern const stream_info_t stream_info_rtsp;
43 extern const stream_info_t stream_info_rtp;
44 extern const stream_info_t stream_info_udp;
45 extern const stream_info_t stream_info_http1;
46 extern const stream_info_t stream_info_http2;
47 extern const stream_info_t stream_info_dvb;
48 extern const stream_info_t stream_info_tv;
49 extern const stream_info_t stream_info_radio;
50 extern const stream_info_t stream_info_pvr;
51 extern const stream_info_t stream_info_ftp;
52 extern const stream_info_t stream_info_vstream;
53 extern const stream_info_t stream_info_dvdnav;
54 extern const stream_info_t stream_info_smb;
55 extern const stream_info_t stream_info_sdp;
56 extern const stream_info_t stream_info_rtsp_sip;
58 extern const stream_info_t stream_info_cue;
59 extern const stream_info_t stream_info_null;
60 extern const stream_info_t stream_info_mf;
61 extern const stream_info_t stream_info_file;
62 extern const stream_info_t stream_info_ifo;
63 extern const stream_info_t stream_info_dvd;
65 static const stream_info_t* const auto_open_streams[] = {
66 #ifdef CONFIG_VCD
67 &stream_info_vcd,
68 #endif
69 #ifdef CONFIG_CDDA
70 &stream_info_cdda,
71 #endif
72 #ifdef CONFIG_NETWORK
73 &stream_info_netstream,
74 &stream_info_http1,
75 &stream_info_asf,
76 &stream_info_pnm,
77 &stream_info_rtsp,
78 #ifdef CONFIG_LIVE555
79 &stream_info_sdp,
80 &stream_info_rtsp_sip,
81 #endif
82 &stream_info_rtp,
83 &stream_info_udp,
84 &stream_info_http2,
85 #endif
86 #ifdef CONFIG_DVBIN
87 &stream_info_dvb,
88 #endif
89 #ifdef CONFIG_TV
90 &stream_info_tv,
91 #endif
92 #ifdef CONFIG_RADIO
93 &stream_info_radio,
94 #endif
95 #ifdef CONFIG_PVR
96 &stream_info_pvr,
97 #endif
98 #ifdef CONFIG_FTP
99 &stream_info_ftp,
100 #endif
101 #ifdef CONFIG_VSTREAM
102 &stream_info_vstream,
103 #endif
104 #ifdef CONFIG_LIBSMBCLIENT
105 &stream_info_smb,
106 #endif
107 &stream_info_cue,
108 #ifdef CONFIG_DVDREAD
109 &stream_info_ifo,
110 &stream_info_dvd,
111 #endif
112 #ifdef CONFIG_DVDNAV
113 &stream_info_dvdnav,
114 #endif
116 &stream_info_null,
117 &stream_info_mf,
118 &stream_info_file,
119 NULL
122 stream_t* open_stream_plugin(const stream_info_t* sinfo,char* filename,int mode,
123 char** options, int* file_format, int* ret,
124 char** redirected_url) {
125 void* arg = NULL;
126 stream_t* s;
127 m_struct_t* desc = (m_struct_t*)sinfo->opts;
129 // Parse options
130 if(desc) {
131 arg = m_struct_alloc(desc);
132 if(sinfo->opts_url) {
133 m_option_t url_opt =
134 { "stream url", arg , CONF_TYPE_CUSTOM_URL, 0, 0 ,0, sinfo->opts };
135 if(m_option_parse(&url_opt,"stream url",filename,arg,M_CONFIG_FILE) < 0) {
136 mp_msg(MSGT_OPEN,MSGL_ERR, "URL parsing failed on url %s\n",filename);
137 m_struct_free(desc,arg);
138 return NULL;
141 if(options) {
142 int i;
143 for(i = 0 ; options[i] != NULL ; i += 2) {
144 mp_msg(MSGT_OPEN,MSGL_DBG2, "Set stream arg %s=%s\n",
145 options[i],options[i+1]);
146 if(!m_struct_set(desc,arg,options[i],options[i+1]))
147 mp_msg(MSGT_OPEN,MSGL_WARN, "Failed to set stream option %s=%s\n",
148 options[i],options[i+1]);
152 s = new_stream(-2,-2);
153 s->url=strdup(filename);
154 s->flags |= mode;
155 *ret = sinfo->open(s,mode,arg,file_format);
156 if((*ret) != STREAM_OK) {
157 #ifdef CONFIG_NETWORK
158 if (*ret == STREAM_REDIRECTED && redirected_url) {
159 if (s->streaming_ctrl && s->streaming_ctrl->url
160 && s->streaming_ctrl->url->url)
161 *redirected_url = strdup(s->streaming_ctrl->url->url);
162 else
163 *redirected_url = NULL;
165 streaming_ctrl_free(s->streaming_ctrl);
166 #endif
167 free(s->url);
168 free(s);
169 return NULL;
171 if(s->type <= -2)
172 mp_msg(MSGT_OPEN,MSGL_WARN, "Warning streams need a type !!!!\n");
173 if(s->flags & STREAM_SEEK && !s->seek)
174 s->flags &= ~STREAM_SEEK;
175 if(s->seek && !(s->flags & STREAM_SEEK))
176 s->flags |= STREAM_SEEK;
178 s->mode = mode;
180 mp_msg(MSGT_OPEN,MSGL_V, "STREAM: [%s] %s\n",sinfo->name,filename);
181 mp_msg(MSGT_OPEN,MSGL_V, "STREAM: Description: %s\n",sinfo->info);
182 mp_msg(MSGT_OPEN,MSGL_V, "STREAM: Author: %s\n", sinfo->author);
183 mp_msg(MSGT_OPEN,MSGL_V, "STREAM: Comment: %s\n", sinfo->comment);
185 return s;
189 stream_t* open_stream_full(char* filename,int mode, char** options, int* file_format) {
190 int i,j,l,r;
191 const stream_info_t* sinfo;
192 stream_t* s;
193 char *redirected_url = NULL;
195 for(i = 0 ; auto_open_streams[i] ; i++) {
196 sinfo = auto_open_streams[i];
197 if(!sinfo->protocols) {
198 mp_msg(MSGT_OPEN,MSGL_WARN, "Stream type %s has protocols == NULL, it's a bug\n", sinfo->name);
199 continue;
201 for(j = 0 ; sinfo->protocols[j] ; j++) {
202 l = strlen(sinfo->protocols[j]);
203 // l == 0 => Don't do protocol matching (ie network and filenames)
204 if((l == 0 && !strstr(filename, "://")) ||
205 ((strncasecmp(sinfo->protocols[j],filename,l) == 0) &&
206 (strncmp("://",filename+l,3) == 0))) {
207 *file_format = DEMUXER_TYPE_UNKNOWN;
208 s = open_stream_plugin(sinfo,filename,mode,options,file_format,&r,
209 &redirected_url);
210 if(s) return s;
211 if(r == STREAM_REDIRECTED && redirected_url) {
212 mp_msg(MSGT_OPEN,MSGL_V, "[%s] open %s redirected to %s\n",
213 sinfo->info, filename, redirected_url);
214 s = open_stream_full(redirected_url, mode, options, file_format);
215 free(redirected_url);
216 return s;
218 else if(r != STREAM_UNSUPPORTED) {
219 mp_msg(MSGT_OPEN,MSGL_ERR, MSGTR_FailedToOpen,filename);
220 return NULL;
222 break;
227 mp_msg(MSGT_OPEN,MSGL_ERR, "No stream found to handle url %s\n",filename);
228 return NULL;
231 stream_t* open_output_stream(char* filename,char** 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 #if HAVE_WINSOCK2_H
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 #if HAVE_WINSOCK2_H
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)(int)) {
464 stream_check_interrupt_cb = cb;
467 int stream_check_interrupt(int time) {
468 if(!stream_check_interrupt_cb) return 0;
469 return stream_check_interrupt_cb(time);