Include lirc.h in lirc.c to add missing prototypes.
[mplayer.git] / stream / stream.c
blobdaecc09d2cc94c1644828704744853ab0c161bad
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 static int (*stream_check_interrupt_cb)(int time) = NULL;
54 extern const stream_info_t stream_info_vcd;
55 extern const stream_info_t stream_info_cdda;
56 extern const stream_info_t stream_info_netstream;
57 extern const stream_info_t stream_info_pnm;
58 extern const stream_info_t stream_info_asf;
59 extern const stream_info_t stream_info_rtsp;
60 extern const stream_info_t stream_info_rtp;
61 extern const stream_info_t stream_info_udp;
62 extern const stream_info_t stream_info_http1;
63 extern const stream_info_t stream_info_http2;
64 extern const stream_info_t stream_info_dvb;
65 extern const stream_info_t stream_info_tv;
66 extern const stream_info_t stream_info_radio;
67 extern const stream_info_t stream_info_pvr;
68 extern const stream_info_t stream_info_ftp;
69 extern const stream_info_t stream_info_vstream;
70 extern const stream_info_t stream_info_dvdnav;
71 extern const stream_info_t stream_info_smb;
72 extern const stream_info_t stream_info_sdp;
73 extern const stream_info_t stream_info_rtsp_sip;
75 extern const stream_info_t stream_info_cue;
76 extern const stream_info_t stream_info_null;
77 extern const stream_info_t stream_info_mf;
78 extern const stream_info_t stream_info_ffmpeg;
79 extern const stream_info_t stream_info_file;
80 extern const stream_info_t stream_info_ifo;
81 extern const stream_info_t stream_info_dvd;
83 static const stream_info_t* const auto_open_streams[] = {
84 #ifdef CONFIG_VCD
85 &stream_info_vcd,
86 #endif
87 #ifdef CONFIG_CDDA
88 &stream_info_cdda,
89 #endif
90 #ifdef CONFIG_NETWORK
91 &stream_info_netstream,
92 &stream_info_http1,
93 &stream_info_asf,
94 &stream_info_pnm,
95 &stream_info_rtsp,
96 #ifdef CONFIG_LIVE555
97 &stream_info_sdp,
98 &stream_info_rtsp_sip,
99 #endif
100 &stream_info_rtp,
101 &stream_info_udp,
102 &stream_info_http2,
103 #endif
104 #ifdef CONFIG_DVBIN
105 &stream_info_dvb,
106 #endif
107 #ifdef CONFIG_TV
108 &stream_info_tv,
109 #endif
110 #ifdef CONFIG_RADIO
111 &stream_info_radio,
112 #endif
113 #ifdef CONFIG_PVR
114 &stream_info_pvr,
115 #endif
116 #ifdef CONFIG_FTP
117 &stream_info_ftp,
118 #endif
119 #ifdef CONFIG_VSTREAM
120 &stream_info_vstream,
121 #endif
122 #ifdef CONFIG_LIBSMBCLIENT
123 &stream_info_smb,
124 #endif
125 &stream_info_cue,
126 #ifdef CONFIG_DVDREAD
127 &stream_info_ifo,
128 &stream_info_dvd,
129 #endif
130 #ifdef CONFIG_DVDNAV
131 &stream_info_dvdnav,
132 #endif
133 #ifdef CONFIG_LIBAVFORMAT
134 &stream_info_ffmpeg,
135 #endif
137 &stream_info_null,
138 &stream_info_mf,
139 &stream_info_file,
140 NULL
143 static stream_t* open_stream_plugin(const stream_info_t* sinfo, const char* filename,
144 int mode, char** options, int* file_format,
145 int* ret, char** redirected_url)
147 void* arg = NULL;
148 stream_t* s;
149 m_struct_t* desc = (m_struct_t*)sinfo->opts;
151 // Parse options
152 if(desc) {
153 arg = m_struct_alloc(desc);
154 if(sinfo->opts_url) {
155 m_option_t url_opt =
156 { "stream url", arg , CONF_TYPE_CUSTOM_URL, 0, 0 ,0, sinfo->opts };
157 if(m_option_parse(&url_opt,"stream url",filename,arg,M_CONFIG_FILE) < 0) {
158 mp_msg(MSGT_OPEN,MSGL_ERR, "URL parsing failed on url %s\n",filename);
159 m_struct_free(desc,arg);
160 return NULL;
163 if(options) {
164 int i;
165 for(i = 0 ; options[i] != NULL ; i += 2) {
166 mp_msg(MSGT_OPEN,MSGL_DBG2, "Set stream arg %s=%s\n",
167 options[i],options[i+1]);
168 if(!m_struct_set(desc,arg,options[i],options[i+1]))
169 mp_msg(MSGT_OPEN,MSGL_WARN, "Failed to set stream option %s=%s\n",
170 options[i],options[i+1]);
174 s = new_stream(-2,-2);
175 s->url=strdup(filename);
176 s->flags |= mode;
177 *ret = sinfo->open(s,mode,arg,file_format);
178 if((*ret) != STREAM_OK) {
179 #ifdef CONFIG_NETWORK
180 if (*ret == STREAM_REDIRECTED && redirected_url) {
181 if (s->streaming_ctrl && s->streaming_ctrl->url
182 && s->streaming_ctrl->url->url)
183 *redirected_url = strdup(s->streaming_ctrl->url->url);
184 else
185 *redirected_url = NULL;
187 streaming_ctrl_free(s->streaming_ctrl);
188 #endif
189 free(s->url);
190 free(s);
191 return NULL;
193 if(s->type <= -2)
194 mp_msg(MSGT_OPEN,MSGL_WARN, "Warning streams need a type !!!!\n");
195 if(s->flags & MP_STREAM_SEEK && !s->seek)
196 s->flags &= ~MP_STREAM_SEEK;
197 if(s->seek && !(s->flags & MP_STREAM_SEEK))
198 s->flags |= MP_STREAM_SEEK;
200 s->mode = mode;
202 mp_msg(MSGT_OPEN,MSGL_V, "STREAM: [%s] %s\n",sinfo->name,filename);
203 mp_msg(MSGT_OPEN,MSGL_V, "STREAM: Description: %s\n",sinfo->info);
204 mp_msg(MSGT_OPEN,MSGL_V, "STREAM: Author: %s\n", sinfo->author);
205 mp_msg(MSGT_OPEN,MSGL_V, "STREAM: Comment: %s\n", sinfo->comment);
207 return s;
211 stream_t* open_stream_full(const char* filename,int mode, char** options, int* file_format) {
212 int i,j,l,r;
213 const stream_info_t* sinfo;
214 stream_t* s;
215 char *redirected_url = NULL;
217 for(i = 0 ; auto_open_streams[i] ; i++) {
218 sinfo = auto_open_streams[i];
219 if(!sinfo->protocols) {
220 mp_msg(MSGT_OPEN,MSGL_WARN, "Stream type %s has protocols == NULL, it's a bug\n", sinfo->name);
221 continue;
223 for(j = 0 ; sinfo->protocols[j] ; j++) {
224 l = strlen(sinfo->protocols[j]);
225 // l == 0 => Don't do protocol matching (ie network and filenames)
226 if((l == 0 && !strstr(filename, "://")) ||
227 ((strncasecmp(sinfo->protocols[j],filename,l) == 0) &&
228 (strncmp("://",filename+l,3) == 0))) {
229 *file_format = DEMUXER_TYPE_UNKNOWN;
230 s = open_stream_plugin(sinfo,filename,mode,options,file_format,&r,
231 &redirected_url);
232 if(s) return s;
233 if(r == STREAM_REDIRECTED && redirected_url) {
234 mp_msg(MSGT_OPEN,MSGL_V, "[%s] open %s redirected to %s\n",
235 sinfo->info, filename, redirected_url);
236 s = open_stream_full(redirected_url, mode, options, file_format);
237 free(redirected_url);
238 return s;
240 else if(r != STREAM_UNSUPPORTED) {
241 mp_msg(MSGT_OPEN,MSGL_ERR, MSGTR_FailedToOpen,filename);
242 return NULL;
244 break;
249 mp_msg(MSGT_OPEN,MSGL_ERR, "No stream found to handle url %s\n",filename);
250 return NULL;
253 stream_t* open_output_stream(const char* filename, char** options) {
254 int file_format; //unused
255 if(!filename) {
256 mp_msg(MSGT_OPEN,MSGL_ERR,"open_output_stream(), NULL filename, report this bug\n");
257 return NULL;
260 return open_stream_full(filename,STREAM_WRITE,options,&file_format);
263 //=================== STREAMER =========================
265 int stream_fill_buffer(stream_t *s){
266 int len;
267 if (/*s->fd == NULL ||*/ s->eof) { return 0; }
268 switch(s->type){
269 case STREAMTYPE_STREAM:
270 #ifdef CONFIG_NETWORK
271 if( s->streaming_ctrl!=NULL && s->streaming_ctrl->streaming_read ) {
272 len=s->streaming_ctrl->streaming_read(s->fd,s->buffer,STREAM_BUFFER_SIZE, s->streaming_ctrl);
273 } else
274 #endif
275 if (s->fill_buffer)
276 len = s->fill_buffer(s, s->buffer, STREAM_BUFFER_SIZE);
277 else
278 len=read(s->fd,s->buffer,STREAM_BUFFER_SIZE);
279 break;
280 case STREAMTYPE_DS:
281 len = demux_read_data((demux_stream_t*)s->priv,s->buffer,STREAM_BUFFER_SIZE);
282 break;
285 default:
286 len= s->fill_buffer ? s->fill_buffer(s,s->buffer,STREAM_BUFFER_SIZE) : 0;
288 if(len<=0){ s->eof=1; return 0; }
289 s->buf_pos=0;
290 s->buf_len=len;
291 s->pos+=len;
292 // printf("[%d]",len);fflush(stdout);
293 return len;
296 int stream_write_buffer(stream_t *s, unsigned char *buf, int len) {
297 int rd;
298 if(!s->write_buffer)
299 return -1;
300 rd = s->write_buffer(s, buf, len);
301 if(rd < 0)
302 return -1;
303 s->pos += rd;
304 return rd;
307 int stream_seek_long(stream_t *s,off_t pos){
308 off_t newpos=0;
310 // if( mp_msg_test(MSGT_STREAM,MSGL_DBG3) ) printf("seek_long to 0x%X\n",(unsigned int)pos);
312 s->buf_pos=s->buf_len=0;
314 if(s->mode == STREAM_WRITE) {
315 if(!s->seek || !s->seek(s,pos))
316 return 0;
317 return 1;
320 if(s->sector_size)
321 newpos = (pos/s->sector_size)*s->sector_size;
322 else
323 newpos = pos&(~((off_t)STREAM_BUFFER_SIZE-1));
325 if( mp_msg_test(MSGT_STREAM,MSGL_DBG3) ){
326 mp_msg(MSGT_STREAM,MSGL_DBG3, "s->pos=%"PRIX64" newpos=%"PRIX64" new_bufpos=%"PRIX64" buflen=%X \n",
327 (int64_t)s->pos,(int64_t)newpos,(int64_t)pos,s->buf_len);
329 pos-=newpos;
331 if(newpos==0 || newpos!=s->pos){
332 switch(s->type){
333 case STREAMTYPE_STREAM:
334 //s->pos=newpos; // real seek
335 // Some streaming protocol allow to seek backward and forward
336 // A function call that return -1 can tell that the protocol
337 // doesn't support seeking.
338 #ifdef CONFIG_NETWORK
339 if(s->seek) { // new stream seek is much cleaner than streaming_ctrl one
340 if(!s->seek(s,newpos)) {
341 mp_msg(MSGT_STREAM,MSGL_ERR, "Seek failed\n");
342 return 0;
344 break;
347 if( s->streaming_ctrl!=NULL && s->streaming_ctrl->streaming_seek ) {
348 if( s->streaming_ctrl->streaming_seek( s->fd, pos, s->streaming_ctrl )<0 ) {
349 mp_msg(MSGT_STREAM,MSGL_INFO,"Stream not seekable!\n");
350 return 1;
352 break;
354 #endif
355 if(newpos<s->pos){
356 mp_msg(MSGT_STREAM,MSGL_INFO,"Cannot seek backward in linear streams!\n");
357 return 1;
359 while(s->pos<newpos){
360 if(stream_fill_buffer(s)<=0) break; // EOF
362 break;
363 default:
364 // This should at the beginning as soon as all streams are converted
365 if(!s->seek)
366 return 0;
367 // Now seek
368 if(!s->seek(s,newpos)) {
369 mp_msg(MSGT_STREAM,MSGL_ERR, "Seek failed\n");
370 return 0;
373 // putchar('.');fflush(stdout);
374 //} else {
375 // putchar('%');fflush(stdout);
378 while(stream_fill_buffer(s) > 0 && pos >= 0) {
379 if(pos<=s->buf_len){
380 s->buf_pos=pos; // byte position in sector
381 return 1;
383 pos -= s->buf_len;
386 // if(pos==s->buf_len) printf("XXX Seek to last byte of file -> EOF\n");
388 mp_msg(MSGT_STREAM,MSGL_V,"stream_seek: WARNING! Can't seek to 0x%"PRIX64" !\n",(int64_t)(pos+newpos));
389 return 0;
393 void stream_reset(stream_t *s){
394 if(s->eof){
395 s->pos=0;
396 s->buf_pos=s->buf_len=0;
397 s->eof=0;
399 if(s->control) s->control(s,STREAM_CTRL_RESET,NULL);
400 //stream_seek(s,0);
403 int stream_control(stream_t *s, int cmd, void *arg){
404 if(!s->control) return STREAM_UNSUPPORTED;
405 #ifdef CONFIG_STREAM_CACHE
406 if (s->cache_pid)
407 return cache_do_control(s, cmd, arg);
408 #endif
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=calloc(1, sizeof(stream_t)+len);
418 s->fd=-1;
419 s->type=STREAMTYPE_MEMORY;
420 s->buf_pos=0; s->buf_len=len;
421 s->start_pos=0; s->end_pos=len;
422 stream_reset(s);
423 s->pos=len;
424 memcpy(s->buffer,data,len);
425 return s;
428 stream_t* new_stream(int fd,int type){
429 stream_t *s=calloc(1, sizeof(stream_t));
430 if(s==NULL) return NULL;
432 #if HAVE_WINSOCK2_H
434 WSADATA wsdata;
435 int temp = WSAStartup(0x0202, &wsdata); // there might be a better place for this (-> later)
436 mp_msg(MSGT_STREAM,MSGL_V,"WINSOCK2 init: %i\n", temp);
438 #endif
440 s->fd=fd;
441 s->type=type;
442 s->buf_pos=s->buf_len=0;
443 s->start_pos=s->end_pos=0;
444 s->priv=NULL;
445 s->url=NULL;
446 s->cache_pid=0;
447 stream_reset(s);
448 return s;
451 void free_stream(stream_t *s){
452 // printf("\n*** free_stream() called ***\n");
453 #ifdef CONFIG_STREAM_CACHE
454 cache_uninit(s);
455 #endif
456 if(s->close) s->close(s);
457 if(s->fd>0){
458 /* on unix we define closesocket to close
459 on windows however we have to distinguish between
460 network socket and file */
461 if(s->url && strstr(s->url,"://"))
462 closesocket(s->fd);
463 else close(s->fd);
465 #if HAVE_WINSOCK2_H
466 mp_msg(MSGT_STREAM,MSGL_V,"WINSOCK2 uninit\n");
467 WSACleanup(); // there might be a better place for this (-> later)
468 #endif
469 // Disabled atm, i don't like that. s->priv can be anything after all
470 // streams should destroy their priv on close
471 //if(s->priv) free(s->priv);
472 if(s->url) free(s->url);
473 free(s);
476 stream_t* new_ds_stream(demux_stream_t *ds) {
477 stream_t* s = new_stream(-1,STREAMTYPE_DS);
478 s->priv = ds;
479 return s;
482 void stream_set_interrupt_callback(int (*cb)(int)) {
483 stream_check_interrupt_cb = cb;
486 int stream_check_interrupt(int time) {
487 if(!stream_check_interrupt_cb) return 0;
488 return stream_check_interrupt_cb(time);