Fix:
[mplayer/glamo.git] / libmpdemux / stream.c
blob052212bfe7ee2c39be9ce7bd72540f11690a3d2b
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 "demuxer.h"
31 #include "m_option.h"
32 #include "m_struct.h"
35 void cache_uninit(stream_t *s); // defined in cache2.c
37 //#include "vcd_read_bincue.h"
39 #ifdef HAVE_VCD
40 extern stream_info_t stream_info_vcd;
41 #endif
42 #ifdef HAVE_CDDA
43 extern stream_info_t stream_info_cdda;
44 #endif
45 #ifdef MPLAYER_NETWORK
46 extern stream_info_t stream_info_netstream;
47 extern stream_info_t stream_info_pnm;
48 extern stream_info_t stream_info_asf;
49 extern stream_info_t stream_info_rtsp;
50 extern stream_info_t stream_info_rtp_udp;
51 extern stream_info_t stream_info_http1;
52 extern stream_info_t stream_info_http2;
53 #endif
54 #ifdef HAS_DVBIN_SUPPORT
55 extern stream_info_t stream_info_dvb;
56 #endif
57 #ifdef HAVE_FTP
58 extern stream_info_t stream_info_ftp;
59 #endif
60 #ifdef HAVE_VSTREAM
61 extern stream_info_t stream_info_vstream;
62 #endif
63 #ifdef USE_DVDNAV
64 extern stream_info_t stream_info_dvdnav;
65 #endif
66 #ifdef LIBSMBCLIENT
67 extern stream_info_t stream_info_smb;
68 #endif
69 #ifdef STREAMING_LIVE555
70 extern stream_info_t stream_info_sdp;
71 extern stream_info_t stream_info_rtsp_sip;
72 #endif
74 extern stream_info_t stream_info_cue;
75 extern stream_info_t stream_info_null;
76 extern stream_info_t stream_info_file;
77 #ifdef HAVE_DVD
78 extern stream_info_t stream_info_dvd;
79 #endif
81 stream_info_t* auto_open_streams[] = {
82 #ifdef HAVE_VCD
83 &stream_info_vcd,
84 #endif
85 #ifdef HAVE_CDDA
86 &stream_info_cdda,
87 #endif
88 #ifdef MPLAYER_NETWORK
89 &stream_info_netstream,
90 &stream_info_http1,
91 &stream_info_asf,
92 &stream_info_pnm,
93 &stream_info_rtsp,
94 #ifdef STREAMING_LIVE555
95 &stream_info_sdp,
96 &stream_info_rtsp_sip,
97 #endif
98 &stream_info_rtp_udp,
99 &stream_info_http2,
100 #endif
101 #ifdef HAS_DVBIN_SUPPORT
102 &stream_info_dvb,
103 #endif
104 #ifdef HAVE_FTP
105 &stream_info_ftp,
106 #endif
107 #ifdef HAVE_VSTREAM
108 &stream_info_vstream,
109 #endif
110 #ifdef LIBSMBCLIENT
111 &stream_info_smb,
112 #endif
113 &stream_info_cue,
114 #ifdef HAVE_DVD
115 &stream_info_dvd,
116 #endif
117 #ifdef USE_DVDNAV
118 &stream_info_dvdnav;
119 #endif
121 &stream_info_null,
122 &stream_info_file,
123 NULL
126 stream_t* open_stream_plugin(stream_info_t* sinfo,char* filename,int mode,
127 char** options, int* file_format, int* ret) {
128 void* arg = NULL;
129 stream_t* s;
130 m_struct_t* desc = (m_struct_t*)sinfo->opts;
132 // Parse options
133 if(desc) {
134 arg = m_struct_alloc(desc);
135 if(sinfo->opts_url) {
136 m_option_t url_opt =
137 { "stream url", arg , CONF_TYPE_CUSTOM_URL, 0, 0 ,0, sinfo->opts };
138 if(m_option_parse(&url_opt,"stream url",filename,arg,M_CONFIG_FILE) < 0) {
139 mp_msg(MSGT_OPEN,MSGL_ERR, "URL parsing failed on url %s\n",filename);
140 m_struct_free(desc,arg);
141 return NULL;
144 if(options) {
145 int i;
146 for(i = 0 ; options[i] != NULL ; i += 2) {
147 mp_msg(MSGT_OPEN,MSGL_DBG2, "Set stream arg %s=%s\n",
148 options[i],options[i+1]);
149 if(!m_struct_set(desc,arg,options[i],options[i+1]))
150 mp_msg(MSGT_OPEN,MSGL_WARN, "Failed to set stream option %s=%s\n",
151 options[i],options[i+1]);
155 s = new_stream(-2,-2);
156 s->url=strdup(filename);
157 s->flags |= mode;
158 *ret = sinfo->open(s,mode,arg,file_format);
159 if((*ret) != STREAM_OK) {
160 free(s->url);
161 free(s);
162 return NULL;
164 if(s->type <= -2)
165 mp_msg(MSGT_OPEN,MSGL_WARN, "Warning streams need a type !!!!\n");
166 if(s->flags & STREAM_SEEK && !s->seek)
167 s->flags &= ~STREAM_SEEK;
168 if(s->seek && !(s->flags & STREAM_SEEK))
169 s->flags |= STREAM_SEEK;
172 mp_msg(MSGT_OPEN,MSGL_V, "STREAM: [%s] %s\n",sinfo->name,filename);
173 mp_msg(MSGT_OPEN,MSGL_V, "STREAM: Description: %s\n",sinfo->info);
174 mp_msg(MSGT_OPEN,MSGL_V, "STREAM: Author: %s\n", sinfo->author);
175 mp_msg(MSGT_OPEN,MSGL_V, "STREAM: Comment: %s\n", sinfo->comment);
177 return s;
181 stream_t* open_stream_full(char* filename,int mode, char** options, int* file_format) {
182 int i,j,l,r;
183 stream_info_t* sinfo;
184 stream_t* s;
186 for(i = 0 ; auto_open_streams[i] ; i++) {
187 sinfo = auto_open_streams[i];
188 if(!sinfo->protocols) {
189 mp_msg(MSGT_OPEN,MSGL_WARN, "Stream type %s has protocols == NULL, it's a bug\n", sinfo->name);
190 continue;
192 for(j = 0 ; sinfo->protocols[j] ; j++) {
193 l = strlen(sinfo->protocols[j]);
194 // l == 0 => Don't do protocol matching (ie network and filenames)
195 if((l == 0) || ((strncmp(sinfo->protocols[j],filename,l) == 0) &&
196 (strncmp("://",filename+l,3) == 0))) {
197 *file_format = DEMUXER_TYPE_UNKNOWN;
198 s = open_stream_plugin(sinfo,filename,mode,options,file_format,&r);
199 if(s) return s;
200 if(r != STREAM_UNSUPORTED) {
201 mp_msg(MSGT_OPEN,MSGL_ERR, MSGTR_FailedToOpen,filename);
202 return NULL;
204 break;
209 mp_msg(MSGT_OPEN,MSGL_ERR, "No stream found to handle url %s\n",filename);
210 return NULL;
213 //=================== STREAMER =========================
215 int stream_fill_buffer(stream_t *s){
216 int len;
217 if (/*s->fd == NULL ||*/ s->eof) { s->buf_pos = s->buf_len = 0; return 0; }
218 switch(s->type){
219 case STREAMTYPE_STREAM:
220 #ifdef MPLAYER_NETWORK
221 if( s->streaming_ctrl!=NULL ) {
222 len=s->streaming_ctrl->streaming_read(s->fd,s->buffer,STREAM_BUFFER_SIZE, s->streaming_ctrl);break;
223 } else {
224 len=read(s->fd,s->buffer,STREAM_BUFFER_SIZE);break;
226 #else
227 len=read(s->fd,s->buffer,STREAM_BUFFER_SIZE);break;
228 #endif
229 case STREAMTYPE_DS:
230 len = demux_read_data((demux_stream_t*)s->priv,s->buffer,STREAM_BUFFER_SIZE);
231 break;
234 default:
235 len= s->fill_buffer ? s->fill_buffer(s,s->buffer,STREAM_BUFFER_SIZE) : 0;
237 if(len<=0){ s->eof=1; s->buf_pos=s->buf_len=0; return 0; }
238 s->buf_pos=0;
239 s->buf_len=len;
240 s->pos+=len;
241 // printf("[%d]",len);fflush(stdout);
242 return len;
245 int stream_seek_long(stream_t *s,off_t pos){
246 off_t newpos=0;
248 // if( mp_msg_test(MSGT_STREAM,MSGL_DBG3) ) printf("seek_long to 0x%X\n",(unsigned int)pos);
250 s->buf_pos=s->buf_len=0;
252 switch(s->type){
253 case STREAMTYPE_STREAM:
254 #ifdef _LARGEFILE_SOURCE
255 newpos=pos&(~((long long)STREAM_BUFFER_SIZE-1));break;
256 #else
257 newpos=pos&(~(STREAM_BUFFER_SIZE-1));break;
258 #endif
259 default:
260 // Round on sector size
261 if(s->sector_size)
262 newpos=(pos/s->sector_size)*s->sector_size;
263 else { // Otherwise on the buffer size
264 #ifdef _LARGEFILE_SOURCE
265 newpos=pos&(~((long long)STREAM_BUFFER_SIZE-1));break;
266 #else
267 newpos=pos&(~(STREAM_BUFFER_SIZE-1));break;
268 #endif
270 break;
273 if( mp_msg_test(MSGT_STREAM,MSGL_DBG3) ){
274 mp_msg(MSGT_STREAM,MSGL_DBG3, "s->pos=%"PRIX64" newpos=%"PRIX64" new_bufpos=%"PRIX64" buflen=%X \n",
275 (int64_t)s->pos,(int64_t)newpos,(int64_t)pos,s->buf_len);
277 pos-=newpos;
279 if(newpos==0 || newpos!=s->pos){
280 switch(s->type){
281 case STREAMTYPE_STREAM:
282 //s->pos=newpos; // real seek
283 // Some streaming protocol allow to seek backward and forward
284 // A function call that return -1 can tell that the protocol
285 // doesn't support seeking.
286 #ifdef MPLAYER_NETWORK
287 if(s->seek) { // new stream seek is much cleaner than streaming_ctrl one
288 if(!s->seek(s,newpos)) {
289 mp_msg(MSGT_STREAM,MSGL_ERR, "Seek failed\n");
290 return 0;
292 break;
295 if( s->streaming_ctrl!=NULL && s->streaming_ctrl->streaming_seek ) {
296 if( s->streaming_ctrl->streaming_seek( s->fd, pos, s->streaming_ctrl )<0 ) {
297 mp_msg(MSGT_STREAM,MSGL_INFO,"Stream not seekable!\n");
298 return 1;
301 #else
302 if(newpos<s->pos){
303 mp_msg(MSGT_STREAM,MSGL_INFO,"Cannot seek backward in linear streams!\n");
304 return 1;
306 while(s->pos<newpos){
307 if(stream_fill_buffer(s)<=0) break; // EOF
309 #endif
310 break;
311 default:
312 // This should at the beginning as soon as all streams are converted
313 if(!s->seek)
314 return 0;
315 // Now seek
316 if(!s->seek(s,newpos)) {
317 mp_msg(MSGT_STREAM,MSGL_ERR, "Seek failed\n");
318 return 0;
321 // putchar('.');fflush(stdout);
322 //} else {
323 // putchar('%');fflush(stdout);
326 while(stream_fill_buffer(s) > 0 && pos >= 0) {
327 if(pos<=s->buf_len){
328 s->buf_pos=pos; // byte position in sector
329 return 1;
331 pos -= s->buf_len;
334 // if(pos==s->buf_len) printf("XXX Seek to last byte of file -> EOF\n");
336 mp_msg(MSGT_STREAM,MSGL_V,"stream_seek: WARNING! Can't seek to 0x%"PRIX64" !\n",(int64_t)(pos+newpos));
337 return 0;
341 void stream_reset(stream_t *s){
342 if(s->eof){
343 s->pos=0; //ftell(f);
344 // s->buf_pos=s->buf_len=0;
345 s->eof=0;
347 if(s->control) s->control(s,STREAM_CTRL_RESET,NULL);
348 //stream_seek(s,0);
351 int stream_control(stream_t *s, int cmd, void *arg){
352 if(!s->control) return STREAM_UNSUPORTED;
353 return s->control(s, cmd, arg);
356 stream_t* new_memory_stream(unsigned char* data,int len){
357 stream_t *s=malloc(sizeof(stream_t)+len);
358 memset(s,0,sizeof(stream_t));
359 s->fd=-1;
360 s->type=STREAMTYPE_MEMORY;
361 s->buf_pos=0; s->buf_len=len;
362 s->start_pos=0; s->end_pos=len;
363 stream_reset(s);
364 s->pos=len;
365 memcpy(s->buffer,data,len);
366 return s;
369 stream_t* new_stream(int fd,int type){
370 stream_t *s=malloc(sizeof(stream_t));
371 if(s==NULL) return NULL;
372 memset(s,0,sizeof(stream_t));
374 #ifdef HAVE_WINSOCK2
376 WSADATA wsdata;
377 int temp = WSAStartup(0x0202, &wsdata); // there might be a better place for this (-> later)
378 mp_msg(MSGT_STREAM,MSGL_V,"WINSOCK2 init: %i\n", temp);
380 #endif
382 s->fd=fd;
383 s->type=type;
384 s->buf_pos=s->buf_len=0;
385 s->start_pos=s->end_pos=0;
386 s->priv=NULL;
387 s->url=NULL;
388 s->cache_pid=0;
389 stream_reset(s);
390 return s;
393 void free_stream(stream_t *s){
394 // printf("\n*** free_stream() called ***\n");
395 #ifdef USE_STREAM_CACHE
396 if(s->cache_pid) {
397 cache_uninit(s);
399 #endif
400 if(s->close) s->close(s);
401 if(s->fd>0){
402 /* on unix we define closesocket to close
403 on windows however we have to distinguish between
404 network socket and file */
405 if(s->url && strstr(s->url,"://"))
406 closesocket(s->fd);
407 else close(s->fd);
409 #ifdef HAVE_WINSOCK2
410 mp_msg(MSGT_STREAM,MSGL_V,"WINSOCK2 uninit\n");
411 WSACleanup(); // there might be a better place for this (-> later)
412 #endif
413 // Disabled atm, i don't like that. s->priv can be anything after all
414 // streams should destroy their priv on close
415 //if(s->priv) free(s->priv);
416 if(s->url) free(s->url);
417 free(s);
420 stream_t* new_ds_stream(demux_stream_t *ds) {
421 stream_t* s = new_stream(-1,STREAMTYPE_DS);
422 s->priv = ds;
423 return s;