23 #include "osdep/shmem.h"
26 #include "libmpdemux/demuxer.h"
33 //#include "vcd_read_bincue.h"
36 static int (*stream_check_interrupt_cb
)(struct input_ctx
*ctx
, int time
);
37 static struct input_ctx
*stream_check_interrupt_ctx
;
39 extern const stream_info_t stream_info_vcd
;
40 extern const stream_info_t stream_info_cdda
;
41 extern const stream_info_t stream_info_netstream
;
42 extern const stream_info_t stream_info_pnm
;
43 extern const stream_info_t stream_info_asf
;
44 extern const stream_info_t stream_info_rtsp
;
45 extern const stream_info_t stream_info_rtp
;
46 extern const stream_info_t stream_info_udp
;
47 extern const stream_info_t stream_info_http1
;
48 extern const stream_info_t stream_info_http2
;
49 extern const stream_info_t stream_info_dvb
;
50 extern const stream_info_t stream_info_tv
;
51 extern const stream_info_t stream_info_radio
;
52 extern const stream_info_t stream_info_pvr
;
53 extern const stream_info_t stream_info_ftp
;
54 extern const stream_info_t stream_info_vstream
;
55 extern const stream_info_t stream_info_dvdnav
;
56 extern const stream_info_t stream_info_smb
;
57 extern const stream_info_t stream_info_sdp
;
58 extern const stream_info_t stream_info_rtsp_sip
;
60 extern const stream_info_t stream_info_cue
;
61 extern const stream_info_t stream_info_null
;
62 extern const stream_info_t stream_info_mf
;
63 extern const stream_info_t stream_info_file
;
64 extern const stream_info_t stream_info_ifo
;
65 extern const stream_info_t stream_info_dvd
;
67 static const stream_info_t
* const auto_open_streams
[] = {
75 &stream_info_netstream
,
82 &stream_info_rtsp_sip
,
103 #ifdef CONFIG_VSTREAM
104 &stream_info_vstream
,
106 #ifdef CONFIG_LIBSMBCLIENT
110 #ifdef CONFIG_DVDREAD
124 static stream_t
*open_stream_plugin(const stream_info_t
*sinfo
, char *filename
,
125 int mode
, struct MPOpts
*options
,
126 int *file_format
, int *ret
,
127 char **redirected_url
)
131 m_struct_t
* desc
= (m_struct_t
*)sinfo
->opts
;
135 arg
= m_struct_alloc(desc
);
136 if(sinfo
->opts_url
) {
138 { "stream url", arg
, CONF_TYPE_CUSTOM_URL
, 0, 0 ,0, sinfo
->opts
};
139 if(m_option_parse(&url_opt
,"stream url",filename
,arg
,M_CONFIG_FILE
) < 0) {
140 mp_msg(MSGT_OPEN
,MSGL_ERR
, "URL parsing failed on url %s\n",filename
);
141 m_struct_free(desc
,arg
);
146 s
= new_stream(-2,-2);
148 s
->url
=strdup(filename
);
150 *ret
= sinfo
->open(s
,mode
,arg
,file_format
);
151 if((*ret
) != STREAM_OK
) {
152 #ifdef CONFIG_NETWORK
153 if (*ret
== STREAM_REDIRECTED
&& redirected_url
) {
154 if (s
->streaming_ctrl
&& s
->streaming_ctrl
->url
155 && s
->streaming_ctrl
->url
->url
)
156 *redirected_url
= strdup(s
->streaming_ctrl
->url
->url
);
158 *redirected_url
= NULL
;
160 streaming_ctrl_free(s
->streaming_ctrl
);
167 mp_msg(MSGT_OPEN
,MSGL_WARN
, "Warning streams need a type !!!!\n");
168 if(s
->flags
& STREAM_SEEK
&& !s
->seek
)
169 s
->flags
&= ~STREAM_SEEK
;
170 if(s
->seek
&& !(s
->flags
& STREAM_SEEK
))
171 s
->flags
|= STREAM_SEEK
;
175 mp_msg(MSGT_OPEN
,MSGL_V
, "STREAM: [%s] %s\n",sinfo
->name
,filename
);
176 mp_msg(MSGT_OPEN
,MSGL_V
, "STREAM: Description: %s\n",sinfo
->info
);
177 mp_msg(MSGT_OPEN
,MSGL_V
, "STREAM: Author: %s\n", sinfo
->author
);
178 mp_msg(MSGT_OPEN
,MSGL_V
, "STREAM: Comment: %s\n", sinfo
->comment
);
184 stream_t
*open_stream_full(char *filename
,int mode
, struct MPOpts
*options
,
188 const stream_info_t
* sinfo
;
190 char *redirected_url
= NULL
;
192 for(i
= 0 ; auto_open_streams
[i
] ; i
++) {
193 sinfo
= auto_open_streams
[i
];
194 if(!sinfo
->protocols
) {
195 mp_msg(MSGT_OPEN
,MSGL_WARN
, "Stream type %s has protocols == NULL, it's a bug\n", sinfo
->name
);
198 for(j
= 0 ; sinfo
->protocols
[j
] ; j
++) {
199 l
= strlen(sinfo
->protocols
[j
]);
200 // l == 0 => Don't do protocol matching (ie network and filenames)
201 if((l
== 0 && !strstr(filename
, "://")) ||
202 ((strncasecmp(sinfo
->protocols
[j
],filename
,l
) == 0) &&
203 (strncmp("://",filename
+l
,3) == 0))) {
204 *file_format
= DEMUXER_TYPE_UNKNOWN
;
205 s
= open_stream_plugin(sinfo
,filename
,mode
,options
,file_format
,&r
,
208 if(r
== STREAM_REDIRECTED
&& redirected_url
) {
209 mp_msg(MSGT_OPEN
,MSGL_V
, "[%s] open %s redirected to %s\n",
210 sinfo
->info
, filename
, redirected_url
);
211 s
= open_stream_full(redirected_url
, mode
, options
, file_format
);
212 free(redirected_url
);
215 else if(r
!= STREAM_UNSUPPORTED
) {
216 mp_tmsg(MSGT_OPEN
,MSGL_ERR
, "Failed to open %s.\n",filename
);
224 mp_msg(MSGT_OPEN
,MSGL_ERR
, "No stream found to handle url %s\n",filename
);
228 stream_t
*open_output_stream(char *filename
, struct MPOpts
*options
)
230 int file_format
; //unused
232 mp_msg(MSGT_OPEN
,MSGL_ERR
,"open_output_stream(), NULL filename, report this bug\n");
236 return open_stream_full(filename
,STREAM_WRITE
,options
,&file_format
);
239 //=================== STREAMER =========================
241 int stream_fill_buffer(stream_t
*s
){
243 if (/*s->fd == NULL ||*/ s
->eof
) { s
->buf_pos
= s
->buf_len
= 0; return 0; }
245 case STREAMTYPE_STREAM
:
246 #ifdef CONFIG_NETWORK
247 if( s
->streaming_ctrl
!=NULL
&& s
->streaming_ctrl
->streaming_read
) {
248 len
=s
->streaming_ctrl
->streaming_read(s
->fd
,s
->buffer
,STREAM_BUFFER_SIZE
, s
->streaming_ctrl
);
251 len
=read(s
->fd
,s
->buffer
,STREAM_BUFFER_SIZE
);
254 len
= demux_read_data((demux_stream_t
*)s
->priv
,s
->buffer
,STREAM_BUFFER_SIZE
);
259 len
= s
->fill_buffer
? s
->fill_buffer(s
,s
->buffer
,STREAM_BUFFER_SIZE
) : 0;
261 if(len
<=0){ s
->eof
=1; s
->buf_pos
=s
->buf_len
=0; return 0; }
265 // printf("[%d]",len);fflush(stdout);
269 int stream_write_buffer(stream_t
*s
, unsigned char *buf
, int len
) {
273 rd
= s
->write_buffer(s
, buf
, len
);
280 int stream_seek_long(stream_t
*s
,off_t pos
){
283 // if( mp_msg_test(MSGT_STREAM,MSGL_DBG3) ) printf("seek_long to 0x%X\n",(unsigned int)pos);
285 s
->buf_pos
=s
->buf_len
=0;
287 if(s
->mode
== STREAM_WRITE
) {
288 if(!s
->seek
|| !s
->seek(s
,pos
))
294 newpos
= (pos
/s
->sector_size
)*s
->sector_size
;
296 newpos
= pos
&(~((off_t
)STREAM_BUFFER_SIZE
-1));
298 if( mp_msg_test(MSGT_STREAM
,MSGL_DBG3
) ){
299 mp_msg(MSGT_STREAM
,MSGL_DBG3
, "s->pos=%"PRIX64
" newpos=%"PRIX64
" new_bufpos=%"PRIX64
" buflen=%X \n",
300 (int64_t)s
->pos
,(int64_t)newpos
,(int64_t)pos
,s
->buf_len
);
304 if(newpos
==0 || newpos
!=s
->pos
){
306 case STREAMTYPE_STREAM
:
307 //s->pos=newpos; // real seek
308 // Some streaming protocol allow to seek backward and forward
309 // A function call that return -1 can tell that the protocol
310 // doesn't support seeking.
311 #ifdef CONFIG_NETWORK
312 if(s
->seek
) { // new stream seek is much cleaner than streaming_ctrl one
313 if(!s
->seek(s
,newpos
)) {
314 mp_msg(MSGT_STREAM
,MSGL_ERR
, "Seek failed\n");
320 if( s
->streaming_ctrl
!=NULL
&& s
->streaming_ctrl
->streaming_seek
) {
321 if( s
->streaming_ctrl
->streaming_seek( s
->fd
, pos
, s
->streaming_ctrl
)<0 ) {
322 mp_msg(MSGT_STREAM
,MSGL_INFO
,"Stream not seekable!\n");
328 mp_msg(MSGT_STREAM
,MSGL_INFO
,"Cannot seek backward in linear streams!\n");
331 while(s
->pos
<newpos
){
332 if(stream_fill_buffer(s
)<=0) break; // EOF
337 // This should at the beginning as soon as all streams are converted
341 if(!s
->seek(s
,newpos
)) {
342 mp_msg(MSGT_STREAM
,MSGL_ERR
, "Seek failed\n");
346 // putchar('.');fflush(stdout);
348 // putchar('%');fflush(stdout);
351 while(stream_fill_buffer(s
) > 0 && pos
>= 0) {
353 s
->buf_pos
=pos
; // byte position in sector
359 // if(pos==s->buf_len) printf("XXX Seek to last byte of file -> EOF\n");
361 mp_msg(MSGT_STREAM
,MSGL_V
,"stream_seek: WARNING! Can't seek to 0x%"PRIX64
" !\n",(int64_t)(pos
+newpos
));
366 void stream_reset(stream_t
*s
){
368 s
->pos
=0; //ftell(f);
369 // s->buf_pos=s->buf_len=0;
372 if(s
->control
) s
->control(s
,STREAM_CTRL_RESET
,NULL
);
376 int stream_control(stream_t
*s
, int cmd
, void *arg
){
377 if(!s
->control
) return STREAM_UNSUPPORTED
;
378 #ifdef CONFIG_STREAM_CACHE
380 return cache_do_control(s
, cmd
, arg
);
382 return s
->control(s
, cmd
, arg
);
385 stream_t
* new_memory_stream(unsigned char* data
,int len
){
390 s
=malloc(sizeof(stream_t
)+len
);
391 memset(s
,0,sizeof(stream_t
));
393 s
->type
=STREAMTYPE_MEMORY
;
394 s
->buf_pos
=0; s
->buf_len
=len
;
395 s
->start_pos
=0; s
->end_pos
=len
;
398 memcpy(s
->buffer
,data
,len
);
402 stream_t
* new_stream(int fd
,int type
){
403 stream_t
*s
=malloc(sizeof(stream_t
));
404 if(s
==NULL
) return NULL
;
405 memset(s
,0,sizeof(stream_t
));
410 int temp
= WSAStartup(0x0202, &wsdata
); // there might be a better place for this (-> later)
411 mp_msg(MSGT_STREAM
,MSGL_V
,"WINSOCK2 init: %i\n", temp
);
417 s
->buf_pos
=s
->buf_len
=0;
418 s
->start_pos
=s
->end_pos
=0;
426 void free_stream(stream_t
*s
){
427 // printf("\n*** free_stream() called ***\n");
428 #ifdef CONFIG_STREAM_CACHE
433 if(s
->close
) s
->close(s
);
435 /* on unix we define closesocket to close
436 on windows however we have to distinguish between
437 network socket and file */
438 if(s
->url
&& strstr(s
->url
,"://"))
443 mp_msg(MSGT_STREAM
,MSGL_V
,"WINSOCK2 uninit\n");
444 WSACleanup(); // there might be a better place for this (-> later)
446 // Disabled atm, i don't like that. s->priv can be anything after all
447 // streams should destroy their priv on close
448 //if(s->priv) free(s->priv);
449 if(s
->url
) free(s
->url
);
453 stream_t
* new_ds_stream(demux_stream_t
*ds
) {
454 stream_t
* s
= new_stream(-1,STREAMTYPE_DS
);
459 void stream_set_interrupt_callback(int (*cb
)(struct input_ctx
*, int),
460 struct input_ctx
*ctx
)
462 stream_check_interrupt_cb
= cb
;
463 stream_check_interrupt_ctx
= ctx
;
466 int stream_check_interrupt(int time
) {
467 if(!stream_check_interrupt_cb
) return 0;
468 return stream_check_interrupt_cb(stream_check_interrupt_ctx
, time
);