odstraneni nekolika gui bugu, pridani librtmp, prepsani parseru na aktualni verzi...
[ctkuk.git] / mplayer / stream.c
blob93a8d8b8355ebd83f1d800f75e38c8b017df3e83
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 #if !defined(WIN32) && !defined(WIN64)
20 #include <unistd.h>
21 #include <sys/time.h>
22 #include <strings.h>
23 #include <sys/ioctl.h>
24 #include <sys/wait.h>
25 #endif /* Def WIN32 or Def WIN64 */
27 #include <stdio.h>
28 #include <stdlib.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <fcntl.h>
34 #include "config.h"
36 #if HAVE_WINSOCK2_H
37 #include <winsock2.h>
38 #endif
40 #include "mp_msg.h"
41 #include "help_mp.h"
42 //#include "osdep/shmem.h"
43 #include "network.h"
44 #include "stream.h"
45 #include "demuxer.h"
47 #include "m_option.h"
48 #include "m_struct.h"
50 //#include "cache2.h"
52 //#include "vcd_read_bincue.h"
54 static int (*stream_check_interrupt_cb)(int time) = NULL;
56 const stream_info_t stream_info_vcd;
57 const stream_info_t stream_info_cdda;
58 const stream_info_t stream_info_netstream;
59 const stream_info_t stream_info_pnm;
60 const stream_info_t stream_info_asf;
61 const stream_info_t stream_info_rtsp;
62 const stream_info_t stream_info_rtp;
63 const stream_info_t stream_info_udp;
64 const stream_info_t stream_info_http1;
65 const stream_info_t stream_info_http2;
66 const stream_info_t stream_info_dvb;
67 const stream_info_t stream_info_tv;
68 const stream_info_t stream_info_radio;
69 const stream_info_t stream_info_pvr;
70 const stream_info_t stream_info_ftp;
71 const stream_info_t stream_info_vstream;
72 const stream_info_t stream_info_dvdnav;
73 const stream_info_t stream_info_smb;
74 const stream_info_t stream_info_sdp;
75 const stream_info_t stream_info_rtsp_sip;
77 const stream_info_t stream_info_cue;
78 const stream_info_t stream_info_null;
79 const stream_info_t stream_info_mf;
80 const stream_info_t stream_info_ffmpeg;
81 const stream_info_t stream_info_file;
82 const stream_info_t stream_info_ifo;
83 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, const char* filename,
146 int mode, char** options, int* file_format,
147 int* ret, char** redirected_url)
149 void* arg = NULL;
150 stream_t* s;
151 m_struct_t* desc = (m_struct_t*)sinfo->opts;
153 // Parse options
154 if(desc) {
155 arg = m_struct_alloc(desc);
156 if(sinfo->opts_url) {
157 m_option_t url_opt =
158 { "stream url", arg , CONF_TYPE_CUSTOM_URL, 0, 0 ,0, sinfo->opts };
159 if(m_option_parse(&url_opt,"stream url",filename,arg,M_CONFIG_FILE) < 0) {
160 mp_msg(MSGT_OPEN,MSGL_ERR, "URL parsing failed on url %s\n",filename);
161 m_struct_free(desc,arg);
162 return NULL;
165 if(options) {
166 int i;
167 for(i = 0 ; options[i] != NULL ; i += 2) {
168 mp_msg(MSGT_OPEN,MSGL_DBG2, "Set stream arg %s=%s\n",
169 options[i],options[i+1]);
170 if(!m_struct_set(desc,arg,options[i],options[i+1]))
171 mp_msg(MSGT_OPEN,MSGL_WARN, "Failed to set stream option %s=%s\n",
172 options[i],options[i+1]);
176 s = new_stream(-2,-2);
177 s->url=strdup(filename);
178 s->flags |= mode;
179 *ret = sinfo->open(s,mode,arg,file_format);
180 if((*ret) != STREAM_OK) {
181 #ifdef CONFIG_NETWORK
182 if (*ret == STREAM_REDIRECTED && redirected_url) {
183 if (s->streaming_ctrl && s->streaming_ctrl->url
184 && s->streaming_ctrl->url->url)
185 *redirected_url = strdup(s->streaming_ctrl->url->url);
186 else
187 *redirected_url = NULL;
189 streaming_ctrl_free(s->streaming_ctrl);
190 #endif
191 free(s->url);
192 free(s);
193 return NULL;
195 if(s->type <= -2)
196 mp_msg(MSGT_OPEN,MSGL_WARN, "Warning streams need a type !!!!\n");
197 if(s->flags & MP_STREAM_SEEK && !s->seek)
198 s->flags &= ~MP_STREAM_SEEK;
199 if(s->seek && !(s->flags & MP_STREAM_SEEK))
200 s->flags |= MP_STREAM_SEEK;
202 s->mode = mode;
204 mp_msg(MSGT_OPEN,MSGL_V, "STREAM: [%s] %s\n",sinfo->name,filename);
205 mp_msg(MSGT_OPEN,MSGL_V, "STREAM: Description: %s\n",sinfo->info);
206 mp_msg(MSGT_OPEN,MSGL_V, "STREAM: Author: %s\n", sinfo->author);
207 mp_msg(MSGT_OPEN,MSGL_V, "STREAM: Comment: %s\n", sinfo->comment);
209 return s;
213 stream_t* open_stream_full(const char* filename,int mode, char** options, int* file_format) {
214 int i,j,l,r;
215 const stream_info_t* sinfo;
216 stream_t* s;
217 char *redirected_url = NULL;
219 for(i = 0 ; auto_open_streams[i] ; i++) {
220 sinfo = auto_open_streams[i];
221 if(!sinfo->protocols) {
222 mp_msg(MSGT_OPEN,MSGL_WARN, "Stream type %s has protocols == NULL, it's a bug\n", sinfo->name);
223 continue;
225 for(j = 0 ; sinfo->protocols[j] ; j++) {
226 l = strlen(sinfo->protocols[j]);
227 // l == 0 => Don't do protocol matching (ie network and filenames)
228 if((l == 0 && !strstr(filename, "://")) ||
229 ((strncasecmp(sinfo->protocols[j],filename,l) == 0) &&
230 (strncmp("://",filename+l,3) == 0))) {
231 *file_format = DEMUXER_TYPE_UNKNOWN;
232 s = open_stream_plugin(sinfo,filename,mode,options,file_format,&r,
233 &redirected_url);
234 if(s) return s;
235 if(r == STREAM_REDIRECTED && redirected_url) {
236 mp_msg(MSGT_OPEN,MSGL_V, "[%s] open %s redirected to %s\n",
237 sinfo->info, filename, redirected_url);
238 s = open_stream_full(redirected_url, mode, options, file_format);
239 free(redirected_url);
240 return s;
242 else if(r != STREAM_UNSUPPORTED) {
243 mp_msg(MSGT_OPEN,MSGL_ERR, MSGTR_FailedToOpen,filename);
244 return NULL;
246 break;
251 mp_msg(MSGT_OPEN,MSGL_ERR, "No stream found to handle url %s\n",filename);
252 return NULL;
255 stream_t* open_output_stream(const char* filename, char** options) {
256 int file_format; //unused
257 if(!filename) {
258 mp_msg(MSGT_OPEN,MSGL_ERR,"open_output_stream(), NULL filename, report this bug\n");
259 return NULL;
262 return open_stream_full(filename,STREAM_WRITE,options,&file_format);
265 //=================== STREAMER =========================
267 int stream_fill_buffer(stream_t *s){
268 int len;
269 if (/*s->fd == NULL ||*/ s->eof) { return 0; }
270 switch(s->type){
271 case STREAMTYPE_STREAM:
272 #ifdef CONFIG_NETWORK
273 if( s->streaming_ctrl!=NULL && s->streaming_ctrl->streaming_read ) {
274 len=s->streaming_ctrl->streaming_read(s->fd,s->buffer,STREAM_BUFFER_SIZE, s->streaming_ctrl);
275 } else
276 #endif
277 if (s->fill_buffer)
278 len = s->fill_buffer(s, s->buffer, STREAM_BUFFER_SIZE);
279 else
280 len=read(s->fd,s->buffer,STREAM_BUFFER_SIZE);
281 break;
282 case STREAMTYPE_DS:
283 // len = demux_read_data((demux_stream_t*)s->priv,s->buffer,STREAM_BUFFER_SIZE);
284 break;
287 default:
288 len= s->fill_buffer ? s->fill_buffer(s,s->buffer,STREAM_BUFFER_SIZE) : 0;
290 if(len<=0){ s->eof=1; return 0; }
291 s->buf_pos=0;
292 s->buf_len=len;
293 s->pos+=len;
294 // printf("[%d]",len);fflush(stdout);
295 return len;
298 int stream_write_buffer(stream_t *s, unsigned char *buf, int len) {
299 int rd;
300 if(!s->write_buffer)
301 return -1;
302 rd = s->write_buffer(s, buf, len);
303 if(rd < 0)
304 return -1;
305 s->pos += rd;
306 return rd;
309 int stream_seek_long(stream_t *s,off_t pos){
310 off_t newpos=0;
312 // if( mp_msg_test(MSGT_STREAM,MSGL_DBG3) ) printf("seek_long to 0x%X\n",(unsigned int)pos);
314 s->buf_pos=s->buf_len=0;
316 if(s->mode == STREAM_WRITE) {
317 if(!s->seek || !s->seek(s,pos))
318 return 0;
319 return 1;
322 if(s->sector_size)
323 newpos = (pos/s->sector_size)*s->sector_size;
324 else
325 newpos = pos&(~((off_t)STREAM_BUFFER_SIZE-1));
327 if( mp_msg_test(MSGT_STREAM,MSGL_DBG3) ){
328 mp_msg(MSGT_STREAM,MSGL_DBG3, "s->pos=%"PRIX64" newpos=%"PRIX64" new_bufpos=%"PRIX64" buflen=%X \n",
329 (int64_t)s->pos,(int64_t)newpos,(int64_t)pos,s->buf_len);
331 pos-=newpos;
333 if(newpos==0 || newpos!=s->pos){
334 switch(s->type){
335 case STREAMTYPE_STREAM:
336 //s->pos=newpos; // real seek
337 // Some streaming protocol allow to seek backward and forward
338 // A function call that return -1 can tell that the protocol
339 // doesn't support seeking.
340 #ifdef CONFIG_NETWORK
341 if(s->seek) { // new stream seek is much cleaner than streaming_ctrl one
342 if(!s->seek(s,newpos)) {
343 mp_msg(MSGT_STREAM,MSGL_ERR, "Seek failed\n");
344 return 0;
346 break;
349 if( s->streaming_ctrl!=NULL && s->streaming_ctrl->streaming_seek ) {
350 if( s->streaming_ctrl->streaming_seek( s->fd, pos, s->streaming_ctrl )<0 ) {
351 mp_msg(MSGT_STREAM,MSGL_INFO,"Stream not seekable!\n");
352 return 1;
354 break;
356 #endif
357 if(newpos<s->pos){
358 mp_msg(MSGT_STREAM,MSGL_INFO,"Cannot seek backward in linear streams!\n");
359 return 1;
361 while(s->pos<newpos){
362 if(stream_fill_buffer(s)<=0) break; // EOF
364 break;
365 default:
366 // This should at the beginning as soon as all streams are converted
367 if(!s->seek)
368 return 0;
369 // Now seek
370 if(!s->seek(s,newpos)) {
371 mp_msg(MSGT_STREAM,MSGL_ERR, "Seek failed\n");
372 return 0;
375 // putchar('.');fflush(stdout);
376 //} else {
377 // putchar('%');fflush(stdout);
380 while(stream_fill_buffer(s) > 0 && pos >= 0) {
381 if(pos<=s->buf_len){
382 s->buf_pos=pos; // byte position in sector
383 return 1;
385 pos -= s->buf_len;
388 // if(pos==s->buf_len) printf("XXX Seek to last byte of file -> EOF\n");
390 mp_msg(MSGT_STREAM,MSGL_V,"stream_seek: WARNING! Can't seek to 0x%"PRIX64" !\n",(int64_t)(pos+newpos));
391 return 0;
395 void stream_reset(stream_t *s){
396 if(s->eof){
397 s->pos=0;
398 s->buf_pos=s->buf_len=0;
399 s->eof=0;
401 if(s->control) s->control(s,STREAM_CTRL_RESET,NULL);
402 //stream_seek(s,0);
405 int stream_control(stream_t *s, int cmd, void *arg){
406 if(!s->control) return STREAM_UNSUPPORTED;
407 #ifdef CONFIG_STREAM_CACHE
408 if (s->cache_pid)
409 return cache_do_control(s, cmd, arg);
410 #endif
411 return s->control(s, cmd, arg);
414 stream_t* new_memory_stream(unsigned char* data,int len){
415 stream_t *s;
417 if(len < 0)
418 return NULL;
419 s=calloc(1, sizeof(stream_t)+len);
420 s->fd=-1;
421 s->type=STREAMTYPE_MEMORY;
422 s->buf_pos=0; s->buf_len=len;
423 s->start_pos=0; s->end_pos=len;
424 stream_reset(s);
425 s->pos=len;
426 memcpy(s->buffer,data,len);
427 return s;
430 stream_t* new_stream(int fd,int type){
431 stream_t *s=calloc(1, sizeof(stream_t));
432 if(s==NULL) return NULL;
434 #if HAVE_WINSOCK2_H
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 CONFIG_STREAM_CACHE
456 cache_uninit(s);
457 #endif
458 if(s->close) s->close(s);
459 if(s->fd>0){
460 /* on unix we define closesocket to close
461 on windows however we have to distinguish between
462 network socket and file */
463 if(s->url && strstr(s->url,"://"))
464 closesocket(s->fd);
465 else close(s->fd);
467 #if WIN32
468 mp_msg(MSGT_STREAM,MSGL_V,"WINSOCK2 uninit\n");
469 WSACleanup(); // there might be a better place for this (-> later)
470 #endif
471 // Disabled atm, i don't like that. s->priv can be anything after all
472 // streams should destroy their priv on close
473 //if(s->priv) free(s->priv);
474 if(s->url) free(s->url);
475 free(s);
478 stream_t* new_ds_stream(demux_stream_t *ds) {
479 stream_t* s = new_stream(-1,STREAMTYPE_DS);
480 s->priv = ds;
481 return s;
484 void stream_set_interrupt_callback(int (*cb)(int)) {
485 stream_check_interrupt_cb = cb;
488 int stream_check_interrupt(int time) {
489 if(!stream_check_interrupt_cb) return 0;
490 return stream_check_interrupt_cb(time);