19 #define closesocket close
26 #include "osdep/shmem.h"
29 #include "libmpdemux/demuxer.h"
36 //#include "vcd_read_bincue.h"
39 extern const stream_info_t stream_info_vcd
;
42 extern const stream_info_t stream_info_cdda
;
44 #ifdef MPLAYER_NETWORK
45 extern const stream_info_t stream_info_netstream
;
46 extern const stream_info_t stream_info_pnm
;
47 extern const stream_info_t stream_info_asf
;
48 extern const stream_info_t stream_info_rtsp
;
49 extern const stream_info_t stream_info_rtp
;
50 extern const stream_info_t stream_info_udp
;
51 extern const stream_info_t stream_info_http1
;
52 extern const stream_info_t stream_info_http2
;
54 #ifdef HAS_DVBIN_SUPPORT
55 extern const stream_info_t stream_info_dvb
;
58 extern const stream_info_t stream_info_tv
;
61 extern const stream_info_t stream_info_radio
;
64 extern const stream_info_t stream_info_pvr
;
67 extern const stream_info_t stream_info_ftp
;
70 extern const stream_info_t stream_info_vstream
;
73 extern const stream_info_t stream_info_dvdnav
;
76 extern const stream_info_t stream_info_smb
;
78 #ifdef STREAMING_LIVE555
79 extern const stream_info_t stream_info_sdp
;
80 extern const stream_info_t stream_info_rtsp_sip
;
83 extern const stream_info_t stream_info_cue
;
84 extern const stream_info_t stream_info_null
;
85 extern const stream_info_t stream_info_mf
;
86 extern const stream_info_t stream_info_file
;
88 extern const stream_info_t stream_info_ifo
;
89 extern const stream_info_t stream_info_dvd
;
92 static const stream_info_t
* const auto_open_streams
[] = {
99 #ifdef MPLAYER_NETWORK
100 &stream_info_netstream
,
105 #ifdef STREAMING_LIVE555
107 &stream_info_rtsp_sip
,
113 #ifdef HAS_DVBIN_SUPPORT
129 &stream_info_vstream
,
149 stream_t
* open_stream_plugin(const stream_info_t
* sinfo
,char* filename
,int mode
,
150 char** options
, int* file_format
, int* ret
,
151 char** redirected_url
) {
154 m_struct_t
* desc
= (m_struct_t
*)sinfo
->opts
;
158 arg
= m_struct_alloc(desc
);
159 if(sinfo
->opts_url
) {
161 { "stream url", arg
, CONF_TYPE_CUSTOM_URL
, 0, 0 ,0, sinfo
->opts
};
162 if(m_option_parse(&url_opt
,"stream url",filename
,arg
,M_CONFIG_FILE
) < 0) {
163 mp_msg(MSGT_OPEN
,MSGL_ERR
, "URL parsing failed on url %s\n",filename
);
164 m_struct_free(desc
,arg
);
170 for(i
= 0 ; options
[i
] != NULL
; i
+= 2) {
171 mp_msg(MSGT_OPEN
,MSGL_DBG2
, "Set stream arg %s=%s\n",
172 options
[i
],options
[i
+1]);
173 if(!m_struct_set(desc
,arg
,options
[i
],options
[i
+1]))
174 mp_msg(MSGT_OPEN
,MSGL_WARN
, "Failed to set stream option %s=%s\n",
175 options
[i
],options
[i
+1]);
179 s
= new_stream(-2,-2);
180 s
->url
=strdup(filename
);
182 *ret
= sinfo
->open(s
,mode
,arg
,file_format
);
183 if((*ret
) != STREAM_OK
) {
184 #ifdef MPLAYER_NETWORK
185 if (*ret
== STREAM_REDIRECTED
&& redirected_url
) {
186 if (s
->streaming_ctrl
&& s
->streaming_ctrl
->url
187 && s
->streaming_ctrl
->url
->url
)
188 *redirected_url
= strdup(s
->streaming_ctrl
->url
->url
);
190 *redirected_url
= NULL
;
192 streaming_ctrl_free(s
->streaming_ctrl
);
199 mp_msg(MSGT_OPEN
,MSGL_WARN
, "Warning streams need a type !!!!\n");
200 if(s
->flags
& STREAM_SEEK
&& !s
->seek
)
201 s
->flags
&= ~STREAM_SEEK
;
202 if(s
->seek
&& !(s
->flags
& STREAM_SEEK
))
203 s
->flags
|= STREAM_SEEK
;
207 mp_msg(MSGT_OPEN
,MSGL_V
, "STREAM: [%s] %s\n",sinfo
->name
,filename
);
208 mp_msg(MSGT_OPEN
,MSGL_V
, "STREAM: Description: %s\n",sinfo
->info
);
209 mp_msg(MSGT_OPEN
,MSGL_V
, "STREAM: Author: %s\n", sinfo
->author
);
210 mp_msg(MSGT_OPEN
,MSGL_V
, "STREAM: Comment: %s\n", sinfo
->comment
);
216 stream_t
* open_stream_full(char* filename
,int mode
, char** options
, int* file_format
) {
218 const stream_info_t
* sinfo
;
220 char *redirected_url
= NULL
;
222 for(i
= 0 ; auto_open_streams
[i
] ; i
++) {
223 sinfo
= auto_open_streams
[i
];
224 if(!sinfo
->protocols
) {
225 mp_msg(MSGT_OPEN
,MSGL_WARN
, "Stream type %s has protocols == NULL, it's a bug\n", sinfo
->name
);
228 for(j
= 0 ; sinfo
->protocols
[j
] ; j
++) {
229 l
= strlen(sinfo
->protocols
[j
]);
230 // l == 0 => Don't do protocol matching (ie network and filenames)
231 if((l
== 0 && !strstr(filename
, "://")) ||
232 ((strncasecmp(sinfo
->protocols
[j
],filename
,l
) == 0) &&
233 (strncmp("://",filename
+l
,3) == 0))) {
234 *file_format
= DEMUXER_TYPE_UNKNOWN
;
235 s
= open_stream_plugin(sinfo
,filename
,mode
,options
,file_format
,&r
,
238 if(r
== STREAM_REDIRECTED
&& redirected_url
) {
239 mp_msg(MSGT_OPEN
,MSGL_V
, "[%s] open %s redirected to %s\n",
240 sinfo
->info
, filename
, redirected_url
);
241 s
= open_stream_full(redirected_url
, mode
, options
, file_format
);
242 free(redirected_url
);
245 else if(r
!= STREAM_UNSUPPORTED
) {
246 mp_msg(MSGT_OPEN
,MSGL_ERR
, MSGTR_FailedToOpen
,filename
);
254 mp_msg(MSGT_OPEN
,MSGL_ERR
, "No stream found to handle url %s\n",filename
);
258 stream_t
* open_output_stream(char* filename
,char** options
) {
259 int file_format
; //unused
261 mp_msg(MSGT_OPEN
,MSGL_ERR
,"open_output_stream(), NULL filename, report this bug\n");
265 return open_stream_full(filename
,STREAM_WRITE
,options
,&file_format
);
268 //=================== STREAMER =========================
270 int stream_fill_buffer(stream_t
*s
){
272 if (/*s->fd == NULL ||*/ s
->eof
) { s
->buf_pos
= s
->buf_len
= 0; return 0; }
274 case STREAMTYPE_STREAM
:
275 #ifdef MPLAYER_NETWORK
276 if( s
->streaming_ctrl
!=NULL
&& s
->streaming_ctrl
->streaming_read
) {
277 len
=s
->streaming_ctrl
->streaming_read(s
->fd
,s
->buffer
,STREAM_BUFFER_SIZE
, s
->streaming_ctrl
);break;
279 len
=read(s
->fd
,s
->buffer
,STREAM_BUFFER_SIZE
);break;
282 len
=read(s
->fd
,s
->buffer
,STREAM_BUFFER_SIZE
);break;
285 len
= demux_read_data((demux_stream_t
*)s
->priv
,s
->buffer
,STREAM_BUFFER_SIZE
);
290 len
= s
->fill_buffer
? s
->fill_buffer(s
,s
->buffer
,STREAM_BUFFER_SIZE
) : 0;
292 if(len
<=0){ s
->eof
=1; s
->buf_pos
=s
->buf_len
=0; return 0; }
296 // printf("[%d]",len);fflush(stdout);
300 int stream_write_buffer(stream_t
*s
, unsigned char *buf
, int len
) {
304 rd
= s
->write_buffer(s
, buf
, len
);
311 int stream_seek_long(stream_t
*s
,off_t pos
){
314 // if( mp_msg_test(MSGT_STREAM,MSGL_DBG3) ) printf("seek_long to 0x%X\n",(unsigned int)pos);
316 s
->buf_pos
=s
->buf_len
=0;
318 if(s
->mode
== STREAM_WRITE
) {
319 if(!s
->seek
|| !s
->seek(s
,pos
))
325 newpos
= (pos
/s
->sector_size
)*s
->sector_size
;
327 newpos
= pos
&(~((off_t
)STREAM_BUFFER_SIZE
-1));
329 if( mp_msg_test(MSGT_STREAM
,MSGL_DBG3
) ){
330 mp_msg(MSGT_STREAM
,MSGL_DBG3
, "s->pos=%"PRIX64
" newpos=%"PRIX64
" new_bufpos=%"PRIX64
" buflen=%X \n",
331 (int64_t)s
->pos
,(int64_t)newpos
,(int64_t)pos
,s
->buf_len
);
335 if(newpos
==0 || newpos
!=s
->pos
){
337 case STREAMTYPE_STREAM
:
338 //s->pos=newpos; // real seek
339 // Some streaming protocol allow to seek backward and forward
340 // A function call that return -1 can tell that the protocol
341 // doesn't support seeking.
342 #ifdef MPLAYER_NETWORK
343 if(s
->seek
) { // new stream seek is much cleaner than streaming_ctrl one
344 if(!s
->seek(s
,newpos
)) {
345 mp_msg(MSGT_STREAM
,MSGL_ERR
, "Seek failed\n");
351 if( s
->streaming_ctrl
!=NULL
&& s
->streaming_ctrl
->streaming_seek
) {
352 if( s
->streaming_ctrl
->streaming_seek( s
->fd
, pos
, s
->streaming_ctrl
)<0 ) {
353 mp_msg(MSGT_STREAM
,MSGL_INFO
,"Stream not seekable!\n");
359 mp_msg(MSGT_STREAM
,MSGL_INFO
,"Cannot seek backward in linear streams!\n");
362 while(s
->pos
<newpos
){
363 if(stream_fill_buffer(s
)<=0) break; // EOF
368 // This should at the beginning as soon as all streams are converted
372 if(!s
->seek(s
,newpos
)) {
373 mp_msg(MSGT_STREAM
,MSGL_ERR
, "Seek failed\n");
377 // putchar('.');fflush(stdout);
379 // putchar('%');fflush(stdout);
382 while(stream_fill_buffer(s
) > 0 && pos
>= 0) {
384 s
->buf_pos
=pos
; // byte position in sector
390 // if(pos==s->buf_len) printf("XXX Seek to last byte of file -> EOF\n");
392 mp_msg(MSGT_STREAM
,MSGL_V
,"stream_seek: WARNING! Can't seek to 0x%"PRIX64
" !\n",(int64_t)(pos
+newpos
));
397 void stream_reset(stream_t
*s
){
399 s
->pos
=0; //ftell(f);
400 // s->buf_pos=s->buf_len=0;
403 if(s
->control
) s
->control(s
,STREAM_CTRL_RESET
,NULL
);
407 int stream_control(stream_t
*s
, int cmd
, void *arg
){
408 if(!s
->control
) return STREAM_UNSUPPORTED
;
409 return s
->control(s
, cmd
, arg
);
412 stream_t
* new_memory_stream(unsigned char* data
,int len
){
417 s
=malloc(sizeof(stream_t
)+len
);
418 memset(s
,0,sizeof(stream_t
));
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
=malloc(sizeof(stream_t
));
431 if(s
==NULL
) return NULL
;
432 memset(s
,0,sizeof(stream_t
));
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
);
444 s
->buf_pos
=s
->buf_len
=0;
445 s
->start_pos
=s
->end_pos
=0;
453 void free_stream(stream_t
*s
){
454 // printf("\n*** free_stream() called ***\n");
455 #ifdef USE_STREAM_CACHE
460 if(s
->close
) s
->close(s
);
462 /* on unix we define closesocket to close
463 on windows however we have to distinguish between
464 network socket and file */
465 if(s
->url
&& strstr(s
->url
,"://"))
470 mp_msg(MSGT_STREAM
,MSGL_V
,"WINSOCK2 uninit\n");
471 WSACleanup(); // there might be a better place for this (-> later)
473 // Disabled atm, i don't like that. s->priv can be anything after all
474 // streams should destroy their priv on close
475 //if(s->priv) free(s->priv);
476 if(s
->url
) free(s
->url
);
480 stream_t
* new_ds_stream(demux_stream_t
*ds
) {
481 stream_t
* s
= new_stream(-1,STREAMTYPE_DS
);