18 #define closesocket close
25 #include "osdep/shmem.h"
28 #include "libmpdemux/demuxer.h"
35 //#include "vcd_read_bincue.h"
38 static int (*stream_check_interrupt_cb
)(struct input_ctx
*ctx
, int time
);
39 static struct input_ctx
*stream_check_interrupt_ctx
;
41 extern const stream_info_t stream_info_vcd
;
42 extern const stream_info_t stream_info_cdda
;
43 extern const stream_info_t stream_info_netstream
;
44 extern const stream_info_t stream_info_pnm
;
45 extern const stream_info_t stream_info_asf
;
46 extern const stream_info_t stream_info_rtsp
;
47 extern const stream_info_t stream_info_rtp
;
48 extern const stream_info_t stream_info_udp
;
49 extern const stream_info_t stream_info_http1
;
50 extern const stream_info_t stream_info_http2
;
51 extern const stream_info_t stream_info_dvb
;
52 extern const stream_info_t stream_info_tv
;
53 extern const stream_info_t stream_info_radio
;
54 extern const stream_info_t stream_info_pvr
;
55 extern const stream_info_t stream_info_ftp
;
56 extern const stream_info_t stream_info_vstream
;
57 extern const stream_info_t stream_info_dvdnav
;
58 extern const stream_info_t stream_info_smb
;
59 extern const stream_info_t stream_info_sdp
;
60 extern const stream_info_t stream_info_rtsp_sip
;
62 extern const stream_info_t stream_info_cue
;
63 extern const stream_info_t stream_info_null
;
64 extern const stream_info_t stream_info_mf
;
65 extern const stream_info_t stream_info_file
;
66 extern const stream_info_t stream_info_ifo
;
67 extern const stream_info_t stream_info_dvd
;
69 static const stream_info_t
* const auto_open_streams
[] = {
77 &stream_info_netstream
,
84 &stream_info_rtsp_sip
,
105 #ifdef CONFIG_VSTREAM
106 &stream_info_vstream
,
108 #ifdef CONFIG_LIBSMBCLIENT
112 #ifdef CONFIG_DVDREAD
126 static stream_t
*open_stream_plugin(const stream_info_t
*sinfo
, char *filename
,
127 int mode
, struct MPOpts
*options
,
128 int *file_format
, int *ret
,
129 char **redirected_url
)
133 m_struct_t
* desc
= (m_struct_t
*)sinfo
->opts
;
137 arg
= m_struct_alloc(desc
);
138 if(sinfo
->opts_url
) {
140 { "stream url", arg
, CONF_TYPE_CUSTOM_URL
, 0, 0 ,0, sinfo
->opts
};
141 if(m_option_parse(&url_opt
,"stream url",filename
,arg
,M_CONFIG_FILE
) < 0) {
142 mp_msg(MSGT_OPEN
,MSGL_ERR
, "URL parsing failed on url %s\n",filename
);
143 m_struct_free(desc
,arg
);
148 s
= new_stream(-2,-2);
150 s
->url
=strdup(filename
);
152 *ret
= sinfo
->open(s
,mode
,arg
,file_format
);
153 if((*ret
) != STREAM_OK
) {
154 #ifdef CONFIG_NETWORK
155 if (*ret
== STREAM_REDIRECTED
&& redirected_url
) {
156 if (s
->streaming_ctrl
&& s
->streaming_ctrl
->url
157 && s
->streaming_ctrl
->url
->url
)
158 *redirected_url
= strdup(s
->streaming_ctrl
->url
->url
);
160 *redirected_url
= NULL
;
162 streaming_ctrl_free(s
->streaming_ctrl
);
169 mp_msg(MSGT_OPEN
,MSGL_WARN
, "Warning streams need a type !!!!\n");
170 if(s
->flags
& STREAM_SEEK
&& !s
->seek
)
171 s
->flags
&= ~STREAM_SEEK
;
172 if(s
->seek
&& !(s
->flags
& STREAM_SEEK
))
173 s
->flags
|= STREAM_SEEK
;
177 mp_msg(MSGT_OPEN
,MSGL_V
, "STREAM: [%s] %s\n",sinfo
->name
,filename
);
178 mp_msg(MSGT_OPEN
,MSGL_V
, "STREAM: Description: %s\n",sinfo
->info
);
179 mp_msg(MSGT_OPEN
,MSGL_V
, "STREAM: Author: %s\n", sinfo
->author
);
180 mp_msg(MSGT_OPEN
,MSGL_V
, "STREAM: Comment: %s\n", sinfo
->comment
);
186 stream_t
*open_stream_full(char *filename
,int mode
, struct MPOpts
*options
,
190 const stream_info_t
* sinfo
;
192 char *redirected_url
= NULL
;
194 for(i
= 0 ; auto_open_streams
[i
] ; i
++) {
195 sinfo
= auto_open_streams
[i
];
196 if(!sinfo
->protocols
) {
197 mp_msg(MSGT_OPEN
,MSGL_WARN
, "Stream type %s has protocols == NULL, it's a bug\n", sinfo
->name
);
200 for(j
= 0 ; sinfo
->protocols
[j
] ; j
++) {
201 l
= strlen(sinfo
->protocols
[j
]);
202 // l == 0 => Don't do protocol matching (ie network and filenames)
203 if((l
== 0 && !strstr(filename
, "://")) ||
204 ((strncasecmp(sinfo
->protocols
[j
],filename
,l
) == 0) &&
205 (strncmp("://",filename
+l
,3) == 0))) {
206 *file_format
= DEMUXER_TYPE_UNKNOWN
;
207 s
= open_stream_plugin(sinfo
,filename
,mode
,options
,file_format
,&r
,
210 if(r
== STREAM_REDIRECTED
&& redirected_url
) {
211 mp_msg(MSGT_OPEN
,MSGL_V
, "[%s] open %s redirected to %s\n",
212 sinfo
->info
, filename
, redirected_url
);
213 s
= open_stream_full(redirected_url
, mode
, options
, file_format
);
214 free(redirected_url
);
217 else if(r
!= STREAM_UNSUPPORTED
) {
218 mp_msg(MSGT_OPEN
,MSGL_ERR
, MSGTR_FailedToOpen
,filename
);
226 mp_msg(MSGT_OPEN
,MSGL_ERR
, "No stream found to handle url %s\n",filename
);
230 stream_t
*open_output_stream(char *filename
, struct MPOpts
*options
)
232 int file_format
; //unused
234 mp_msg(MSGT_OPEN
,MSGL_ERR
,"open_output_stream(), NULL filename, report this bug\n");
238 return open_stream_full(filename
,STREAM_WRITE
,options
,&file_format
);
241 //=================== STREAMER =========================
243 int stream_fill_buffer(stream_t
*s
){
245 if (/*s->fd == NULL ||*/ s
->eof
) { s
->buf_pos
= s
->buf_len
= 0; return 0; }
247 case STREAMTYPE_STREAM
:
248 #ifdef CONFIG_NETWORK
249 if( s
->streaming_ctrl
!=NULL
&& s
->streaming_ctrl
->streaming_read
) {
250 len
=s
->streaming_ctrl
->streaming_read(s
->fd
,s
->buffer
,STREAM_BUFFER_SIZE
, s
->streaming_ctrl
);break;
252 len
=read(s
->fd
,s
->buffer
,STREAM_BUFFER_SIZE
);break;
255 len
=read(s
->fd
,s
->buffer
,STREAM_BUFFER_SIZE
);break;
258 len
= demux_read_data((demux_stream_t
*)s
->priv
,s
->buffer
,STREAM_BUFFER_SIZE
);
263 len
= s
->fill_buffer
? s
->fill_buffer(s
,s
->buffer
,STREAM_BUFFER_SIZE
) : 0;
265 if(len
<=0){ s
->eof
=1; s
->buf_pos
=s
->buf_len
=0; return 0; }
269 // printf("[%d]",len);fflush(stdout);
273 int stream_write_buffer(stream_t
*s
, unsigned char *buf
, int len
) {
277 rd
= s
->write_buffer(s
, buf
, len
);
284 int stream_seek_long(stream_t
*s
,off_t pos
){
287 // if( mp_msg_test(MSGT_STREAM,MSGL_DBG3) ) printf("seek_long to 0x%X\n",(unsigned int)pos);
289 s
->buf_pos
=s
->buf_len
=0;
291 if(s
->mode
== STREAM_WRITE
) {
292 if(!s
->seek
|| !s
->seek(s
,pos
))
298 newpos
= (pos
/s
->sector_size
)*s
->sector_size
;
300 newpos
= pos
&(~((off_t
)STREAM_BUFFER_SIZE
-1));
302 if( mp_msg_test(MSGT_STREAM
,MSGL_DBG3
) ){
303 mp_msg(MSGT_STREAM
,MSGL_DBG3
, "s->pos=%"PRIX64
" newpos=%"PRIX64
" new_bufpos=%"PRIX64
" buflen=%X \n",
304 (int64_t)s
->pos
,(int64_t)newpos
,(int64_t)pos
,s
->buf_len
);
308 if(newpos
==0 || newpos
!=s
->pos
){
310 case STREAMTYPE_STREAM
:
311 //s->pos=newpos; // real seek
312 // Some streaming protocol allow to seek backward and forward
313 // A function call that return -1 can tell that the protocol
314 // doesn't support seeking.
315 #ifdef CONFIG_NETWORK
316 if(s
->seek
) { // new stream seek is much cleaner than streaming_ctrl one
317 if(!s
->seek(s
,newpos
)) {
318 mp_msg(MSGT_STREAM
,MSGL_ERR
, "Seek failed\n");
324 if( s
->streaming_ctrl
!=NULL
&& s
->streaming_ctrl
->streaming_seek
) {
325 if( s
->streaming_ctrl
->streaming_seek( s
->fd
, pos
, s
->streaming_ctrl
)<0 ) {
326 mp_msg(MSGT_STREAM
,MSGL_INFO
,"Stream not seekable!\n");
332 mp_msg(MSGT_STREAM
,MSGL_INFO
,"Cannot seek backward in linear streams!\n");
335 while(s
->pos
<newpos
){
336 if(stream_fill_buffer(s
)<=0) break; // EOF
341 // This should at the beginning as soon as all streams are converted
345 if(!s
->seek(s
,newpos
)) {
346 mp_msg(MSGT_STREAM
,MSGL_ERR
, "Seek failed\n");
350 // putchar('.');fflush(stdout);
352 // putchar('%');fflush(stdout);
355 while(stream_fill_buffer(s
) > 0 && pos
>= 0) {
357 s
->buf_pos
=pos
; // byte position in sector
363 // if(pos==s->buf_len) printf("XXX Seek to last byte of file -> EOF\n");
365 mp_msg(MSGT_STREAM
,MSGL_V
,"stream_seek: WARNING! Can't seek to 0x%"PRIX64
" !\n",(int64_t)(pos
+newpos
));
370 void stream_reset(stream_t
*s
){
372 s
->pos
=0; //ftell(f);
373 // s->buf_pos=s->buf_len=0;
376 if(s
->control
) s
->control(s
,STREAM_CTRL_RESET
,NULL
);
380 int stream_control(stream_t
*s
, int cmd
, void *arg
){
381 if(!s
->control
) return STREAM_UNSUPPORTED
;
382 #ifdef CONFIG_STREAM_CACHE
384 return cache_do_control(s
, cmd
, arg
);
386 return s
->control(s
, cmd
, arg
);
389 stream_t
* new_memory_stream(unsigned char* data
,int len
){
394 s
=malloc(sizeof(stream_t
)+len
);
395 memset(s
,0,sizeof(stream_t
));
397 s
->type
=STREAMTYPE_MEMORY
;
398 s
->buf_pos
=0; s
->buf_len
=len
;
399 s
->start_pos
=0; s
->end_pos
=len
;
402 memcpy(s
->buffer
,data
,len
);
406 stream_t
* new_stream(int fd
,int type
){
407 stream_t
*s
=malloc(sizeof(stream_t
));
408 if(s
==NULL
) return NULL
;
409 memset(s
,0,sizeof(stream_t
));
414 int temp
= WSAStartup(0x0202, &wsdata
); // there might be a better place for this (-> later)
415 mp_msg(MSGT_STREAM
,MSGL_V
,"WINSOCK2 init: %i\n", temp
);
421 s
->buf_pos
=s
->buf_len
=0;
422 s
->start_pos
=s
->end_pos
=0;
430 void free_stream(stream_t
*s
){
431 // printf("\n*** free_stream() called ***\n");
432 #ifdef CONFIG_STREAM_CACHE
437 if(s
->close
) s
->close(s
);
439 /* on unix we define closesocket to close
440 on windows however we have to distinguish between
441 network socket and file */
442 if(s
->url
&& strstr(s
->url
,"://"))
447 mp_msg(MSGT_STREAM
,MSGL_V
,"WINSOCK2 uninit\n");
448 WSACleanup(); // there might be a better place for this (-> later)
450 // Disabled atm, i don't like that. s->priv can be anything after all
451 // streams should destroy their priv on close
452 //if(s->priv) free(s->priv);
453 if(s
->url
) free(s
->url
);
457 stream_t
* new_ds_stream(demux_stream_t
*ds
) {
458 stream_t
* s
= new_stream(-1,STREAMTYPE_DS
);
463 void stream_set_interrupt_callback(int (*cb
)(struct input_ctx
*, int),
464 struct input_ctx
*ctx
)
466 stream_check_interrupt_cb
= cb
;
467 stream_check_interrupt_ctx
= ctx
;
470 int stream_check_interrupt(int time
) {
471 if(!stream_check_interrupt_cb
) return 0;
472 return stream_check_interrupt_cb(stream_check_interrupt_ctx
, time
);