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.
23 #include <sys/types.h>
26 #include <sys/ioctl.h>
40 #include "osdep/shmem.h"
43 #include "libmpdemux/demuxer.h"
50 //#include "vcd_read_bincue.h"
53 static int (*stream_check_interrupt_cb
)(struct input_ctx
*ctx
, int time
);
54 static struct input_ctx
*stream_check_interrupt_ctx
;
56 extern const stream_info_t stream_info_vcd
;
57 extern const stream_info_t stream_info_cdda
;
58 extern const stream_info_t stream_info_netstream
;
59 extern const stream_info_t stream_info_pnm
;
60 extern const stream_info_t stream_info_asf
;
61 extern const stream_info_t stream_info_rtsp
;
62 extern const stream_info_t stream_info_rtp
;
63 extern const stream_info_t stream_info_udp
;
64 extern const stream_info_t stream_info_http1
;
65 extern const stream_info_t stream_info_http2
;
66 extern const stream_info_t stream_info_dvb
;
67 extern const stream_info_t stream_info_tv
;
68 extern const stream_info_t stream_info_radio
;
69 extern const stream_info_t stream_info_pvr
;
70 extern const stream_info_t stream_info_ftp
;
71 extern const stream_info_t stream_info_vstream
;
72 extern const stream_info_t stream_info_dvdnav
;
73 extern const stream_info_t stream_info_smb
;
74 extern const stream_info_t stream_info_sdp
;
75 extern const stream_info_t stream_info_rtsp_sip
;
77 extern const stream_info_t stream_info_cue
;
78 extern const stream_info_t stream_info_null
;
79 extern const stream_info_t stream_info_mf
;
80 extern const stream_info_t stream_info_ffmpeg
;
81 extern const stream_info_t stream_info_file
;
82 extern const stream_info_t stream_info_ifo
;
83 extern const stream_info_t stream_info_dvd
;
85 static const stream_info_t
* const auto_open_streams
[] = {
93 &stream_info_netstream
,
100 &stream_info_rtsp_sip
,
121 #ifdef CONFIG_VSTREAM
122 &stream_info_vstream
,
124 #ifdef CONFIG_LIBSMBCLIENT
128 #ifdef CONFIG_DVDREAD
135 #ifdef CONFIG_LIBAVFORMAT
145 static stream_t
*open_stream_plugin(const stream_info_t
*sinfo
, char *filename
,
146 int mode
, struct MPOpts
*options
,
147 int *file_format
, int *ret
,
148 char **redirected_url
)
152 m_struct_t
* desc
= (m_struct_t
*)sinfo
->opts
;
156 arg
= m_struct_alloc(desc
);
157 if(sinfo
->opts_url
) {
159 { "stream url", arg
, CONF_TYPE_CUSTOM_URL
, 0, 0 ,0, sinfo
->opts
};
160 if(m_option_parse(&url_opt
,"stream url",filename
,arg
,M_CONFIG_FILE
) < 0) {
161 mp_msg(MSGT_OPEN
,MSGL_ERR
, "URL parsing failed on url %s\n",filename
);
162 m_struct_free(desc
,arg
);
167 s
= new_stream(-2,-2);
169 s
->url
=strdup(filename
);
171 *ret
= sinfo
->open(s
,mode
,arg
,file_format
);
172 if((*ret
) != STREAM_OK
) {
173 #ifdef CONFIG_NETWORK
174 if (*ret
== STREAM_REDIRECTED
&& redirected_url
) {
175 if (s
->streaming_ctrl
&& s
->streaming_ctrl
->url
176 && s
->streaming_ctrl
->url
->url
)
177 *redirected_url
= strdup(s
->streaming_ctrl
->url
->url
);
179 *redirected_url
= NULL
;
181 streaming_ctrl_free(s
->streaming_ctrl
);
188 mp_msg(MSGT_OPEN
,MSGL_WARN
, "Warning streams need a type !!!!\n");
189 if(s
->flags
& MP_STREAM_SEEK
&& !s
->seek
)
190 s
->flags
&= ~MP_STREAM_SEEK
;
191 if(s
->seek
&& !(s
->flags
& MP_STREAM_SEEK
))
192 s
->flags
|= MP_STREAM_SEEK
;
196 mp_msg(MSGT_OPEN
,MSGL_V
, "STREAM: [%s] %s\n",sinfo
->name
,filename
);
197 mp_msg(MSGT_OPEN
,MSGL_V
, "STREAM: Description: %s\n",sinfo
->info
);
198 mp_msg(MSGT_OPEN
,MSGL_V
, "STREAM: Author: %s\n", sinfo
->author
);
199 mp_msg(MSGT_OPEN
,MSGL_V
, "STREAM: Comment: %s\n", sinfo
->comment
);
205 stream_t
*open_stream_full(char *filename
,int mode
, struct MPOpts
*options
,
209 const stream_info_t
* sinfo
;
211 char *redirected_url
= NULL
;
213 for(i
= 0 ; auto_open_streams
[i
] ; i
++) {
214 sinfo
= auto_open_streams
[i
];
215 if(!sinfo
->protocols
) {
216 mp_msg(MSGT_OPEN
,MSGL_WARN
, "Stream type %s has protocols == NULL, it's a bug\n", sinfo
->name
);
219 for(j
= 0 ; sinfo
->protocols
[j
] ; j
++) {
220 l
= strlen(sinfo
->protocols
[j
]);
221 // l == 0 => Don't do protocol matching (ie network and filenames)
222 if((l
== 0 && !strstr(filename
, "://")) ||
223 ((strncasecmp(sinfo
->protocols
[j
],filename
,l
) == 0) &&
224 (strncmp("://",filename
+l
,3) == 0))) {
225 *file_format
= DEMUXER_TYPE_UNKNOWN
;
226 s
= open_stream_plugin(sinfo
,filename
,mode
,options
,file_format
,&r
,
229 if(r
== STREAM_REDIRECTED
&& redirected_url
) {
230 mp_msg(MSGT_OPEN
,MSGL_V
, "[%s] open %s redirected to %s\n",
231 sinfo
->info
, filename
, redirected_url
);
232 s
= open_stream_full(redirected_url
, mode
, options
, file_format
);
233 free(redirected_url
);
236 else if(r
!= STREAM_UNSUPPORTED
) {
237 mp_tmsg(MSGT_OPEN
,MSGL_ERR
, "Failed to open %s.\n",filename
);
245 mp_msg(MSGT_OPEN
,MSGL_ERR
, "No stream found to handle url %s\n",filename
);
249 stream_t
*open_output_stream(char *filename
, struct MPOpts
*options
)
251 int file_format
; //unused
253 mp_msg(MSGT_OPEN
,MSGL_ERR
,"open_output_stream(), NULL filename, report this bug\n");
257 return open_stream_full(filename
,STREAM_WRITE
,options
,&file_format
);
260 //=================== STREAMER =========================
262 int stream_fill_buffer(stream_t
*s
){
264 if (/*s->fd == NULL ||*/ s
->eof
) { return 0; }
266 case STREAMTYPE_STREAM
:
267 #ifdef CONFIG_NETWORK
268 if( s
->streaming_ctrl
!=NULL
&& s
->streaming_ctrl
->streaming_read
) {
269 len
=s
->streaming_ctrl
->streaming_read(s
->fd
,s
->buffer
,STREAM_BUFFER_SIZE
, s
->streaming_ctrl
);
273 len
= s
->fill_buffer(s
, s
->buffer
, STREAM_BUFFER_SIZE
);
275 len
=read(s
->fd
,s
->buffer
,STREAM_BUFFER_SIZE
);
278 len
= demux_read_data((demux_stream_t
*)s
->priv
,s
->buffer
,STREAM_BUFFER_SIZE
);
283 len
= s
->fill_buffer
? s
->fill_buffer(s
,s
->buffer
,STREAM_BUFFER_SIZE
) : 0;
285 if(len
<=0){ s
->eof
=1; return 0; }
289 // printf("[%d]",len);fflush(stdout);
293 int stream_write_buffer(stream_t
*s
, unsigned char *buf
, int len
) {
297 rd
= s
->write_buffer(s
, buf
, len
);
304 int stream_seek_long(stream_t
*s
,off_t pos
){
307 // if( mp_msg_test(MSGT_STREAM,MSGL_DBG3) ) printf("seek_long to 0x%X\n",(unsigned int)pos);
309 s
->buf_pos
=s
->buf_len
=0;
311 if(s
->mode
== STREAM_WRITE
) {
312 if(!s
->seek
|| !s
->seek(s
,pos
))
318 newpos
= (pos
/s
->sector_size
)*s
->sector_size
;
320 newpos
= pos
&(~((off_t
)STREAM_BUFFER_SIZE
-1));
322 if( mp_msg_test(MSGT_STREAM
,MSGL_DBG3
) ){
323 mp_msg(MSGT_STREAM
,MSGL_DBG3
, "s->pos=%"PRIX64
" newpos=%"PRIX64
" new_bufpos=%"PRIX64
" buflen=%X \n",
324 (int64_t)s
->pos
,(int64_t)newpos
,(int64_t)pos
,s
->buf_len
);
328 if(newpos
==0 || newpos
!=s
->pos
){
330 case STREAMTYPE_STREAM
:
331 //s->pos=newpos; // real seek
332 // Some streaming protocol allow to seek backward and forward
333 // A function call that return -1 can tell that the protocol
334 // doesn't support seeking.
335 #ifdef CONFIG_NETWORK
336 if(s
->seek
) { // new stream seek is much cleaner than streaming_ctrl one
337 if(!s
->seek(s
,newpos
)) {
338 mp_msg(MSGT_STREAM
,MSGL_ERR
, "Seek failed\n");
344 if( s
->streaming_ctrl
!=NULL
&& s
->streaming_ctrl
->streaming_seek
) {
345 if( s
->streaming_ctrl
->streaming_seek( s
->fd
, pos
, s
->streaming_ctrl
)<0 ) {
346 mp_msg(MSGT_STREAM
,MSGL_INFO
,"Stream not seekable!\n");
353 mp_msg(MSGT_STREAM
,MSGL_INFO
,"Cannot seek backward in linear streams!\n");
356 while(s
->pos
<newpos
){
357 if(stream_fill_buffer(s
)<=0) break; // EOF
361 // This should at the beginning as soon as all streams are converted
365 if(!s
->seek(s
,newpos
)) {
366 mp_msg(MSGT_STREAM
,MSGL_ERR
, "Seek failed\n");
370 // putchar('.');fflush(stdout);
372 // putchar('%');fflush(stdout);
375 s
->eof
= 0; // EOF reset when seek succeeds.
376 while (stream_fill_buffer(s
) > 0) {
378 s
->buf_pos
=pos
; // byte position in sector
383 // Fill failed, but seek still is a success.
388 mp_msg(MSGT_STREAM
,MSGL_V
,
389 "stream_seek: Seek to/past EOF: no buffer preloaded.\n");
394 void stream_reset(stream_t
*s
){
397 s
->buf_pos
=s
->buf_len
=0;
400 if(s
->control
) s
->control(s
,STREAM_CTRL_RESET
,NULL
);
404 int stream_control(stream_t
*s
, int cmd
, void *arg
){
405 if(!s
->control
) return STREAM_UNSUPPORTED
;
406 #ifdef CONFIG_STREAM_CACHE
408 return cache_do_control(s
, cmd
, arg
);
410 return s
->control(s
, cmd
, arg
);
413 stream_t
* new_memory_stream(unsigned char* data
,int len
){
418 s
=calloc(1, sizeof(stream_t
)+len
);
420 s
->type
=STREAMTYPE_MEMORY
;
421 s
->buf_pos
=0; s
->buf_len
=len
;
422 s
->start_pos
=0; s
->end_pos
=len
;
425 memcpy(s
->buffer
,data
,len
);
429 stream_t
* new_stream(int fd
,int type
){
430 stream_t
*s
=calloc(1, sizeof(stream_t
));
431 if(s
==NULL
) return NULL
;
436 int temp
= WSAStartup(0x0202, &wsdata
); // there might be a better place for this (-> later)
437 mp_msg(MSGT_STREAM
,MSGL_V
,"WINSOCK2 init: %i\n", temp
);
443 s
->buf_pos
=s
->buf_len
=0;
444 s
->start_pos
=s
->end_pos
=0;
452 void free_stream(stream_t
*s
){
453 // printf("\n*** free_stream() called ***\n");
454 #ifdef CONFIG_STREAM_CACHE
457 if(s
->close
) s
->close(s
);
459 /* on unix we define closesocket to close
460 on windows however we have to distinguish between
461 network socket and file */
462 if(s
->url
&& strstr(s
->url
,"://"))
467 mp_msg(MSGT_STREAM
,MSGL_V
,"WINSOCK2 uninit\n");
468 WSACleanup(); // there might be a better place for this (-> later)
470 // Disabled atm, i don't like that. s->priv can be anything after all
471 // streams should destroy their priv on close
472 //if(s->priv) free(s->priv);
473 if(s
->url
) free(s
->url
);
477 stream_t
* new_ds_stream(demux_stream_t
*ds
) {
478 stream_t
* s
= new_stream(-1,STREAMTYPE_DS
);
483 void stream_set_interrupt_callback(int (*cb
)(struct input_ctx
*, int),
484 struct input_ctx
*ctx
)
486 stream_check_interrupt_cb
= cb
;
487 stream_check_interrupt_ctx
= ctx
;
490 int stream_check_interrupt(int time
) {
491 if(!stream_check_interrupt_cb
) return 0;
492 return stream_check_interrupt_cb(stream_check_interrupt_ctx
, time
);