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>
38 #include <libavutil/common.h>
41 #include "osdep/shmem.h"
42 #include "osdep/timer.h"
45 #include "libmpdemux/demuxer.h"
46 #include "ffmpeg_files/intreadwrite.h"
54 static int (*stream_check_interrupt_cb
)(struct input_ctx
*ctx
, int time
);
55 static struct input_ctx
*stream_check_interrupt_ctx
;
57 extern const stream_info_t stream_info_vcd
;
58 extern const stream_info_t stream_info_cdda
;
59 extern const stream_info_t stream_info_netstream
;
60 extern const stream_info_t stream_info_pnm
;
61 extern const stream_info_t stream_info_asf
;
62 extern const stream_info_t stream_info_rtsp
;
63 extern const stream_info_t stream_info_rtp
;
64 extern const stream_info_t stream_info_udp
;
65 extern const stream_info_t stream_info_http1
;
66 extern const stream_info_t stream_info_http2
;
67 extern const stream_info_t stream_info_dvb
;
68 extern const stream_info_t stream_info_tv
;
69 extern const stream_info_t stream_info_radio
;
70 extern const stream_info_t stream_info_pvr
;
71 extern const stream_info_t stream_info_ftp
;
72 extern const stream_info_t stream_info_vstream
;
73 extern const stream_info_t stream_info_dvdnav
;
74 extern const stream_info_t stream_info_smb
;
75 extern const stream_info_t stream_info_sdp
;
76 extern const stream_info_t stream_info_rtsp_sip
;
78 extern const stream_info_t stream_info_cue
;
79 extern const stream_info_t stream_info_null
;
80 extern const stream_info_t stream_info_mf
;
81 extern const stream_info_t stream_info_ffmpeg
;
82 extern const stream_info_t stream_info_file
;
83 extern const stream_info_t stream_info_ifo
;
84 extern const stream_info_t stream_info_dvd
;
85 extern const stream_info_t stream_info_bluray
;
87 static const stream_info_t
* const auto_open_streams
[] = {
94 #ifdef CONFIG_NETWORKING
95 &stream_info_netstream
,
100 #ifdef CONFIG_LIVE555
102 &stream_info_rtsp_sip
,
123 #ifdef CONFIG_VSTREAM
124 &stream_info_vstream
,
126 #ifdef CONFIG_LIBSMBCLIENT
130 #ifdef CONFIG_DVDREAD
137 #ifdef CONFIG_LIBBLURAY
150 static stream_t
*open_stream_plugin(const stream_info_t
*sinfo
,
151 const char *filename
,
152 int mode
, struct MPOpts
*options
,
153 int *file_format
, int *ret
,
154 char **redirected_url
)
158 m_struct_t
* desc
= (m_struct_t
*)sinfo
->opts
;
162 arg
= m_struct_alloc(desc
);
163 if(sinfo
->opts_url
) {
165 { "stream url", arg
, CONF_TYPE_CUSTOM_URL
, 0, 0 ,0, sinfo
->opts
};
166 if(m_option_parse(&url_opt
,"stream url",filename
,arg
,M_CONFIG_FILE
) < 0) {
167 mp_msg(MSGT_OPEN
,MSGL_ERR
, "URL parsing failed on url %s\n",filename
);
168 m_struct_free(desc
,arg
);
173 s
= new_stream(-2,-2);
175 s
->url
=strdup(filename
);
177 *ret
= sinfo
->open(s
,mode
,arg
,file_format
);
178 if((*ret
) != STREAM_OK
) {
179 #ifdef CONFIG_NETWORKING
180 if (*ret
== STREAM_REDIRECTED
&& redirected_url
) {
181 if (s
->streaming_ctrl
&& s
->streaming_ctrl
->url
182 && s
->streaming_ctrl
->url
->url
)
183 *redirected_url
= strdup(s
->streaming_ctrl
->url
->url
);
185 *redirected_url
= NULL
;
187 streaming_ctrl_free(s
->streaming_ctrl
);
194 mp_msg(MSGT_OPEN
,MSGL_WARN
, "Warning streams need a type !!!!\n");
195 if(s
->flags
& MP_STREAM_SEEK
&& !s
->seek
)
196 s
->flags
&= ~MP_STREAM_SEEK
;
197 if(s
->seek
&& !(s
->flags
& MP_STREAM_SEEK
))
198 s
->flags
|= MP_STREAM_SEEK
;
202 mp_msg(MSGT_OPEN
,MSGL_V
, "STREAM: [%s] %s\n",sinfo
->name
,filename
);
203 mp_msg(MSGT_OPEN
,MSGL_V
, "STREAM: Description: %s\n",sinfo
->info
);
204 mp_msg(MSGT_OPEN
,MSGL_V
, "STREAM: Author: %s\n", sinfo
->author
);
205 mp_msg(MSGT_OPEN
,MSGL_V
, "STREAM: Comment: %s\n", sinfo
->comment
);
211 stream_t
*open_stream_full(const char *filename
, int mode
,
212 struct MPOpts
*options
, int *file_format
)
215 const stream_info_t
* sinfo
;
217 char *redirected_url
= NULL
;
219 for(i
= 0 ; auto_open_streams
[i
] ; i
++) {
220 sinfo
= auto_open_streams
[i
];
221 if(!sinfo
->protocols
) {
222 mp_msg(MSGT_OPEN
,MSGL_WARN
, "Stream type %s has protocols == NULL, it's a bug\n", sinfo
->name
);
225 for(j
= 0 ; sinfo
->protocols
[j
] ; j
++) {
226 l
= strlen(sinfo
->protocols
[j
]);
227 // l == 0 => Don't do protocol matching (ie network and filenames)
228 if((l
== 0 && !strstr(filename
, "://")) ||
229 ((strncasecmp(sinfo
->protocols
[j
],filename
,l
) == 0) &&
230 (strncmp("://",filename
+l
,3) == 0))) {
231 *file_format
= DEMUXER_TYPE_UNKNOWN
;
232 s
= open_stream_plugin(sinfo
,filename
,mode
,options
,file_format
,&r
,
235 if(r
== STREAM_REDIRECTED
&& redirected_url
) {
236 mp_msg(MSGT_OPEN
,MSGL_V
, "[%s] open %s redirected to %s\n",
237 sinfo
->info
, filename
, redirected_url
);
238 s
= open_stream_full(redirected_url
, mode
, options
, file_format
);
239 free(redirected_url
);
242 else if(r
!= STREAM_UNSUPPORTED
) {
243 mp_tmsg(MSGT_OPEN
,MSGL_ERR
, "Failed to open %s.\n",filename
);
251 mp_msg(MSGT_OPEN
,MSGL_ERR
, "No stream found to handle url %s\n",filename
);
255 stream_t
*open_output_stream(const char *filename
, struct MPOpts
*options
)
257 int file_format
; //unused
259 mp_msg(MSGT_OPEN
,MSGL_ERR
,"open_output_stream(), NULL filename, report this bug\n");
263 return open_stream_full(filename
,STREAM_WRITE
,options
,&file_format
);
266 //=================== STREAMER =========================
268 void stream_capture_do(stream_t
*s
)
270 if (fwrite(s
->buffer
, s
->buf_len
, 1, s
->capture_file
) < 1) {
271 mp_tmsg(MSGT_GLOBAL
, MSGL_ERR
, "Error writing capture file: %s\n",
273 fclose(s
->capture_file
);
274 s
->capture_file
= NULL
;
278 int stream_read_internal(stream_t
*s
, void *buf
, int len
)
280 // we will retry even if we already reached EOF previously.
282 case STREAMTYPE_STREAM
:
283 #ifdef CONFIG_NETWORKING
284 if( s
->streaming_ctrl
!=NULL
&& s
->streaming_ctrl
->streaming_read
) {
285 len
=s
->streaming_ctrl
->streaming_read(s
->fd
, buf
, len
, s
->streaming_ctrl
);
289 len
= s
->fill_buffer(s
, buf
, len
);
291 len
= read(s
->fd
, buf
, len
);
294 len
= demux_read_data((demux_stream_t
*)s
->priv
, buf
, len
);
299 len
= s
->fill_buffer
? s
->fill_buffer(s
, buf
, len
) : 0;
301 if(len
<=0){ s
->eof
=1; return 0; }
302 // When reading succeeded we are obviously not at eof.
303 // This e.g. avoids issues with eof getting stuck when lavf seeks in MPEG-TS
309 int stream_fill_buffer(stream_t
*s
){
310 int len
= stream_read_internal(s
, s
->buffer
, STREAM_BUFFER_SIZE
);
315 // printf("[%d]",len);fflush(stdout);
317 stream_capture_do(s
);
321 int stream_write_buffer(stream_t
*s
, unsigned char *buf
, int len
) {
325 rd
= s
->write_buffer(s
, buf
, len
);
332 int stream_seek_internal(stream_t
*s
, off_t newpos
)
334 if(newpos
==0 || newpos
!=s
->pos
){
336 case STREAMTYPE_STREAM
:
337 //s->pos=newpos; // real seek
338 // Some streaming protocol allow to seek backward and forward
339 // A function call that return -1 can tell that the protocol
340 // doesn't support seeking.
341 #ifdef CONFIG_NETWORKING
342 if(s
->seek
) { // new stream seek is much cleaner than streaming_ctrl one
343 if(!s
->seek(s
,newpos
)) {
344 mp_msg(MSGT_STREAM
,MSGL_ERR
, "Seek failed\n");
350 if( s
->streaming_ctrl
!=NULL
&& s
->streaming_ctrl
->streaming_seek
) {
351 if( s
->streaming_ctrl
->streaming_seek( s
->fd
, newpos
, s
->streaming_ctrl
)<0 ) {
352 mp_msg(MSGT_STREAM
,MSGL_INFO
,"Stream not seekable!\n");
359 mp_msg(MSGT_STREAM
,MSGL_INFO
,"Cannot seek backward in linear streams!\n");
364 // This should at the beginning as soon as all streams are converted
368 if(!s
->seek(s
,newpos
)) {
369 mp_msg(MSGT_STREAM
,MSGL_ERR
, "Seek failed\n");
373 // putchar('.');fflush(stdout);
375 // putchar('%');fflush(stdout);
380 int stream_seek_long(stream_t
*s
,off_t pos
){
384 // if( mp_msg_test(MSGT_STREAM,MSGL_DBG3) ) printf("seek_long to 0x%X\n",(unsigned int)pos);
386 s
->buf_pos
=s
->buf_len
=0;
388 if(s
->mode
== STREAM_WRITE
) {
389 if(!s
->seek
|| !s
->seek(s
,pos
))
395 newpos
= (pos
/s
->sector_size
)*s
->sector_size
;
397 newpos
= pos
&(~((off_t
)STREAM_BUFFER_SIZE
-1));
399 if( mp_msg_test(MSGT_STREAM
,MSGL_DBG3
) ){
400 mp_msg(MSGT_STREAM
,MSGL_DBG3
, "s->pos=%"PRIX64
" newpos=%"PRIX64
" new_bufpos=%"PRIX64
" buflen=%X \n",
401 (int64_t)s
->pos
,(int64_t)newpos
,(int64_t)pos
,s
->buf_len
);
405 res
= stream_seek_internal(s
, newpos
);
409 while(s
->pos
<newpos
){
410 if(stream_fill_buffer(s
)<=0) break; // EOF
413 s
->eof
= 0; // EOF reset when seek succeeds.
414 while (stream_fill_buffer(s
) > 0) {
416 s
->buf_pos
=pos
; // byte position in sector
421 // Fill failed, but seek still is a success.
426 mp_msg(MSGT_STREAM
,MSGL_V
,
427 "stream_seek: Seek to/past EOF: no buffer preloaded.\n");
432 void stream_reset(stream_t
*s
){
435 s
->buf_pos
=s
->buf_len
=0;
438 if(s
->control
) s
->control(s
,STREAM_CTRL_RESET
,NULL
);
442 int stream_control(stream_t
*s
, int cmd
, void *arg
){
443 if(!s
->control
) return STREAM_UNSUPPORTED
;
444 #ifdef CONFIG_STREAM_CACHE
446 return cache_do_control(s
, cmd
, arg
);
448 return s
->control(s
, cmd
, arg
);
451 stream_t
* new_memory_stream(unsigned char* data
,int len
){
456 s
=calloc(1, sizeof(stream_t
)+len
);
458 s
->type
=STREAMTYPE_MEMORY
;
459 s
->buf_pos
=0; s
->buf_len
=len
;
460 s
->start_pos
=0; s
->end_pos
=len
;
463 memcpy(s
->buffer
,data
,len
);
467 stream_t
* new_stream(int fd
,int type
){
468 stream_t
*s
=calloc(1, sizeof(stream_t
));
469 if(s
==NULL
) return NULL
;
474 int temp
= WSAStartup(0x0202, &wsdata
); // there might be a better place for this (-> later)
475 mp_msg(MSGT_STREAM
,MSGL_V
,"WINSOCK2 init: %i\n", temp
);
481 s
->buf_pos
=s
->buf_len
=0;
482 s
->start_pos
=s
->end_pos
=0;
490 void free_stream(stream_t
*s
){
491 // printf("\n*** free_stream() called ***\n");
492 #ifdef CONFIG_STREAM_CACHE
495 if (s
->capture_file
) {
496 fclose(s
->capture_file
);
497 s
->capture_file
= NULL
;
500 if(s
->close
) s
->close(s
);
502 /* on unix we define closesocket to close
503 on windows however we have to distinguish between
504 network socket and file */
505 if(s
->url
&& strstr(s
->url
,"://"))
510 mp_msg(MSGT_STREAM
,MSGL_V
,"WINSOCK2 uninit\n");
511 WSACleanup(); // there might be a better place for this (-> later)
513 // Disabled atm, i don't like that. s->priv can be anything after all
514 // streams should destroy their priv on close
520 stream_t
* new_ds_stream(demux_stream_t
*ds
) {
521 stream_t
* s
= new_stream(-1,STREAMTYPE_DS
);
526 void stream_set_interrupt_callback(int (*cb
)(struct input_ctx
*, int),
527 struct input_ctx
*ctx
)
529 stream_check_interrupt_cb
= cb
;
530 stream_check_interrupt_ctx
= ctx
;
533 int stream_check_interrupt(int time
) {
534 if(!stream_check_interrupt_cb
) {
535 usec_sleep(time
* 1000);
538 return stream_check_interrupt_cb(stream_check_interrupt_ctx
, time
);
542 * Helper function to read 16 bits little-endian and advance pointer
544 static uint16_t get_le16_inc(const uint8_t **buf
)
546 uint16_t v
= AV_RL16(*buf
);
552 * Helper function to read 16 bits big-endian and advance pointer
554 static uint16_t get_be16_inc(const uint8_t **buf
)
556 uint16_t v
= AV_RB16(*buf
);
562 * Find a newline character in buffer
563 * \param buf buffer to search
564 * \param len amount of bytes to search in buffer, may not overread
565 * \param utf16 chose between UTF-8/ASCII/other and LE and BE UTF-16
566 * 0 = UTF-8/ASCII/other, 1 = UTF-16-LE, 2 = UTF-16-BE
568 static const uint8_t *find_newline(const uint8_t *buf
, int len
, int utf16
)
571 const uint8_t *end
= buf
+ len
;
574 return (uint8_t *)memchr(buf
, '\n', len
);
576 while (buf
< end
- 1) {
577 GET_UTF16(c
, buf
< end
- 1 ? get_le16_inc(&buf
) : 0, return NULL
;)
578 if (buf
<= end
&& c
== '\n')
583 while (buf
< end
- 1) {
584 GET_UTF16(c
, buf
< end
- 1 ? get_be16_inc(&buf
) : 0, return NULL
;)
585 if (buf
<= end
&& c
== '\n')
594 * Copy a number of bytes, converting to UTF-8 if input is UTF-16
595 * \param dst buffer to copy to
596 * \param dstsize size of dst buffer
597 * \param src buffer to copy from
598 * \param len amount of bytes to copy from src
599 * \param utf16 chose between UTF-8/ASCII/other and LE and BE UTF-16
600 * 0 = UTF-8/ASCII/other, 1 = UTF-16-LE, 2 = UTF-16-BE
602 static int copy_characters(uint8_t *dst
, int dstsize
,
603 const uint8_t *src
, int *len
, int utf16
)
606 uint8_t *dst_end
= dst
+ dstsize
;
607 const uint8_t *end
= src
+ *len
;
612 memcpy(dst
, src
, *len
);
615 while (src
< end
- 1 && dst_end
- dst
> 8) {
617 GET_UTF16(c
, src
< end
- 1 ? get_le16_inc(&src
) : 0, ;)
618 PUT_UTF8(c
, tmp
, *dst
++ = tmp
;)
621 return dstsize
- (dst_end
- dst
);
623 while (src
< end
- 1 && dst_end
- dst
> 8) {
625 GET_UTF16(c
, src
< end
- 1 ? get_be16_inc(&src
) : 0, ;)
626 PUT_UTF8(c
, tmp
, *dst
++ = tmp
;)
629 return dstsize
- (dst_end
- dst
);
634 unsigned char* stream_read_line(stream_t
*s
,unsigned char* mem
, int max
, int utf16
) {
636 const unsigned char *end
;
637 unsigned char *ptr
= mem
;
638 if (max
< 1) return NULL
;
639 max
--; // reserve one for 0-termination
641 len
= s
->buf_len
-s
->buf_pos
;
642 // try to fill the buffer
644 (!cache_stream_fill_buffer(s
) ||
645 (len
= s
->buf_len
-s
->buf_pos
) <= 0)) break;
646 end
= find_newline(s
->buffer
+s
->buf_pos
, len
, utf16
);
647 if(end
) len
= end
- (s
->buffer
+s
->buf_pos
) + 1;
648 if(len
> 0 && max
> 0) {
649 int l
= copy_characters(ptr
, max
, s
->buffer
+s
->buf_pos
, &len
, utf16
);
658 if(s
->eof
&& ptr
== mem
) return NULL
;