23 #include "osdep/shmem.h"
26 #include "libmpdemux/demuxer.h"
33 //#include "vcd_read_bincue.h"
35 static int (*stream_check_interrupt_cb
)(int time
) = NULL
;
37 extern const stream_info_t stream_info_vcd
;
38 extern const stream_info_t stream_info_cdda
;
39 extern const stream_info_t stream_info_netstream
;
40 extern const stream_info_t stream_info_pnm
;
41 extern const stream_info_t stream_info_asf
;
42 extern const stream_info_t stream_info_rtsp
;
43 extern const stream_info_t stream_info_rtp
;
44 extern const stream_info_t stream_info_udp
;
45 extern const stream_info_t stream_info_http1
;
46 extern const stream_info_t stream_info_http2
;
47 extern const stream_info_t stream_info_dvb
;
48 extern const stream_info_t stream_info_tv
;
49 extern const stream_info_t stream_info_radio
;
50 extern const stream_info_t stream_info_pvr
;
51 extern const stream_info_t stream_info_ftp
;
52 extern const stream_info_t stream_info_vstream
;
53 extern const stream_info_t stream_info_dvdnav
;
54 extern const stream_info_t stream_info_smb
;
55 extern const stream_info_t stream_info_sdp
;
56 extern const stream_info_t stream_info_rtsp_sip
;
58 extern const stream_info_t stream_info_cue
;
59 extern const stream_info_t stream_info_null
;
60 extern const stream_info_t stream_info_mf
;
61 extern const stream_info_t stream_info_file
;
62 extern const stream_info_t stream_info_ifo
;
63 extern const stream_info_t stream_info_dvd
;
65 static const stream_info_t
* const auto_open_streams
[] = {
73 &stream_info_netstream
,
80 &stream_info_rtsp_sip
,
101 #ifdef CONFIG_VSTREAM
102 &stream_info_vstream
,
104 #ifdef CONFIG_LIBSMBCLIENT
108 #ifdef CONFIG_DVDREAD
122 stream_t
* open_stream_plugin(const stream_info_t
* sinfo
,char* filename
,int mode
,
123 char** options
, int* file_format
, int* ret
,
124 char** redirected_url
) {
127 m_struct_t
* desc
= (m_struct_t
*)sinfo
->opts
;
131 arg
= m_struct_alloc(desc
);
132 if(sinfo
->opts_url
) {
134 { "stream url", arg
, CONF_TYPE_CUSTOM_URL
, 0, 0 ,0, sinfo
->opts
};
135 if(m_option_parse(&url_opt
,"stream url",filename
,arg
,M_CONFIG_FILE
) < 0) {
136 mp_msg(MSGT_OPEN
,MSGL_ERR
, "URL parsing failed on url %s\n",filename
);
137 m_struct_free(desc
,arg
);
143 for(i
= 0 ; options
[i
] != NULL
; i
+= 2) {
144 mp_msg(MSGT_OPEN
,MSGL_DBG2
, "Set stream arg %s=%s\n",
145 options
[i
],options
[i
+1]);
146 if(!m_struct_set(desc
,arg
,options
[i
],options
[i
+1]))
147 mp_msg(MSGT_OPEN
,MSGL_WARN
, "Failed to set stream option %s=%s\n",
148 options
[i
],options
[i
+1]);
152 s
= new_stream(-2,-2);
153 s
->url
=strdup(filename
);
155 *ret
= sinfo
->open(s
,mode
,arg
,file_format
);
156 if((*ret
) != STREAM_OK
) {
157 #ifdef CONFIG_NETWORK
158 if (*ret
== STREAM_REDIRECTED
&& redirected_url
) {
159 if (s
->streaming_ctrl
&& s
->streaming_ctrl
->url
160 && s
->streaming_ctrl
->url
->url
)
161 *redirected_url
= strdup(s
->streaming_ctrl
->url
->url
);
163 *redirected_url
= NULL
;
165 streaming_ctrl_free(s
->streaming_ctrl
);
172 mp_msg(MSGT_OPEN
,MSGL_WARN
, "Warning streams need a type !!!!\n");
173 if(s
->flags
& STREAM_SEEK
&& !s
->seek
)
174 s
->flags
&= ~STREAM_SEEK
;
175 if(s
->seek
&& !(s
->flags
& STREAM_SEEK
))
176 s
->flags
|= STREAM_SEEK
;
180 mp_msg(MSGT_OPEN
,MSGL_V
, "STREAM: [%s] %s\n",sinfo
->name
,filename
);
181 mp_msg(MSGT_OPEN
,MSGL_V
, "STREAM: Description: %s\n",sinfo
->info
);
182 mp_msg(MSGT_OPEN
,MSGL_V
, "STREAM: Author: %s\n", sinfo
->author
);
183 mp_msg(MSGT_OPEN
,MSGL_V
, "STREAM: Comment: %s\n", sinfo
->comment
);
189 stream_t
* open_stream_full(char* filename
,int mode
, char** options
, int* file_format
) {
191 const stream_info_t
* sinfo
;
193 char *redirected_url
= NULL
;
195 for(i
= 0 ; auto_open_streams
[i
] ; i
++) {
196 sinfo
= auto_open_streams
[i
];
197 if(!sinfo
->protocols
) {
198 mp_msg(MSGT_OPEN
,MSGL_WARN
, "Stream type %s has protocols == NULL, it's a bug\n", sinfo
->name
);
201 for(j
= 0 ; sinfo
->protocols
[j
] ; j
++) {
202 l
= strlen(sinfo
->protocols
[j
]);
203 // l == 0 => Don't do protocol matching (ie network and filenames)
204 if((l
== 0 && !strstr(filename
, "://")) ||
205 ((strncasecmp(sinfo
->protocols
[j
],filename
,l
) == 0) &&
206 (strncmp("://",filename
+l
,3) == 0))) {
207 *file_format
= DEMUXER_TYPE_UNKNOWN
;
208 s
= open_stream_plugin(sinfo
,filename
,mode
,options
,file_format
,&r
,
211 if(r
== STREAM_REDIRECTED
&& redirected_url
) {
212 mp_msg(MSGT_OPEN
,MSGL_V
, "[%s] open %s redirected to %s\n",
213 sinfo
->info
, filename
, redirected_url
);
214 s
= open_stream_full(redirected_url
, mode
, options
, file_format
);
215 free(redirected_url
);
218 else if(r
!= STREAM_UNSUPPORTED
) {
219 mp_msg(MSGT_OPEN
,MSGL_ERR
, MSGTR_FailedToOpen
,filename
);
227 mp_msg(MSGT_OPEN
,MSGL_ERR
, "No stream found to handle url %s\n",filename
);
231 stream_t
* open_output_stream(char* filename
,char** 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
)(int)) {
464 stream_check_interrupt_cb
= cb
;
467 int stream_check_interrupt(int time
) {
468 if(!stream_check_interrupt_cb
) return 0;
469 return stream_check_interrupt_cb(time
);