2010-01-30 22:26:47 +00:00
|
|
|
/*
|
|
|
|
* This file is part of MPlayer.
|
|
|
|
*
|
|
|
|
* MPlayer is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* MPlayer is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with MPlayer; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2001-08-01 09:14:02 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2001-07-31 23:18:16 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2003-04-02 16:52:00 +00:00
|
|
|
#ifndef __MINGW32__
|
2001-04-22 16:56:20 +00:00
|
|
|
#include <sys/ioctl.h>
|
2002-07-26 04:04:51 +00:00
|
|
|
#include <sys/wait.h>
|
2003-04-02 16:52:00 +00:00
|
|
|
#endif
|
2001-07-31 23:18:16 +00:00
|
|
|
#include <fcntl.h>
|
2003-04-02 16:25:07 +00:00
|
|
|
#include <strings.h>
|
2001-04-22 16:56:20 +00:00
|
|
|
|
2001-08-01 09:14:02 +00:00
|
|
|
#include "config.h"
|
2003-06-11 16:48:09 +00:00
|
|
|
|
2009-02-01 13:42:27 +00:00
|
|
|
#if HAVE_WINSOCK2_H
|
2003-06-11 16:48:09 +00:00
|
|
|
#include <winsock2.h>
|
|
|
|
#endif
|
|
|
|
|
2010-07-10 08:07:27 +00:00
|
|
|
#include <libavutil/common.h>
|
|
|
|
|
2001-09-26 21:35:14 +00:00
|
|
|
#include "mp_msg.h"
|
2005-11-18 14:39:25 +00:00
|
|
|
#include "osdep/shmem.h"
|
2010-05-30 13:39:41 +00:00
|
|
|
#include "osdep/timer.h"
|
2008-09-01 07:31:57 +00:00
|
|
|
#include "network.h"
|
2001-04-22 16:56:20 +00:00
|
|
|
#include "stream.h"
|
2006-08-04 17:01:29 +00:00
|
|
|
#include "libmpdemux/demuxer.h"
|
2010-03-10 00:36:35 +00:00
|
|
|
#include "ffmpeg_files/intreadwrite.h"
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2005-11-18 14:39:25 +00:00
|
|
|
#include "m_option.h"
|
|
|
|
#include "m_struct.h"
|
2003-04-02 16:25:07 +00:00
|
|
|
|
2006-09-12 16:24:23 +00:00
|
|
|
#include "cache2.h"
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2008-04-30 04:15:52 +00:00
|
|
|
struct input_ctx;
|
|
|
|
static int (*stream_check_interrupt_cb)(struct input_ctx *ctx, int time);
|
|
|
|
static struct input_ctx *stream_check_interrupt_ctx;
|
2008-04-09 00:36:28 +00:00
|
|
|
|
2007-12-02 13:22:53 +00:00
|
|
|
extern const stream_info_t stream_info_vcd;
|
|
|
|
extern const stream_info_t stream_info_cdda;
|
|
|
|
extern const stream_info_t stream_info_netstream;
|
|
|
|
extern const stream_info_t stream_info_pnm;
|
|
|
|
extern const stream_info_t stream_info_asf;
|
|
|
|
extern const stream_info_t stream_info_rtsp;
|
|
|
|
extern const stream_info_t stream_info_rtp;
|
|
|
|
extern const stream_info_t stream_info_udp;
|
|
|
|
extern const stream_info_t stream_info_http1;
|
|
|
|
extern const stream_info_t stream_info_http2;
|
|
|
|
extern const stream_info_t stream_info_dvb;
|
|
|
|
extern const stream_info_t stream_info_tv;
|
|
|
|
extern const stream_info_t stream_info_radio;
|
|
|
|
extern const stream_info_t stream_info_pvr;
|
|
|
|
extern const stream_info_t stream_info_ftp;
|
|
|
|
extern const stream_info_t stream_info_vstream;
|
|
|
|
extern const stream_info_t stream_info_dvdnav;
|
|
|
|
extern const stream_info_t stream_info_smb;
|
|
|
|
extern const stream_info_t stream_info_sdp;
|
|
|
|
extern const stream_info_t stream_info_rtsp_sip;
|
2003-08-11 00:02:46 +00:00
|
|
|
|
2007-12-02 13:22:53 +00:00
|
|
|
extern const stream_info_t stream_info_cue;
|
|
|
|
extern const stream_info_t stream_info_null;
|
|
|
|
extern const stream_info_t stream_info_mf;
|
2009-11-17 16:09:17 +00:00
|
|
|
extern const stream_info_t stream_info_ffmpeg;
|
2007-12-02 13:22:53 +00:00
|
|
|
extern const stream_info_t stream_info_file;
|
|
|
|
extern const stream_info_t stream_info_ifo;
|
|
|
|
extern const stream_info_t stream_info_dvd;
|
2010-07-05 17:04:46 +00:00
|
|
|
extern const stream_info_t stream_info_bluray;
|
2003-04-02 16:25:07 +00:00
|
|
|
|
2007-12-02 13:07:35 +00:00
|
|
|
static const stream_info_t* const auto_open_streams[] = {
|
2008-08-03 15:21:40 +00:00
|
|
|
#ifdef CONFIG_VCD
|
2003-04-09 07:31:11 +00:00
|
|
|
&stream_info_vcd,
|
|
|
|
#endif
|
2008-08-07 08:58:07 +00:00
|
|
|
#ifdef CONFIG_CDDA
|
2003-04-02 19:29:26 +00:00
|
|
|
&stream_info_cdda,
|
2003-04-06 16:36:17 +00:00
|
|
|
#endif
|
2010-07-17 13:14:53 +00:00
|
|
|
#ifdef CONFIG_NETWORKING
|
2003-04-06 16:36:17 +00:00
|
|
|
&stream_info_netstream,
|
2005-05-29 12:54:00 +00:00
|
|
|
&stream_info_http1,
|
|
|
|
&stream_info_asf,
|
|
|
|
&stream_info_pnm,
|
|
|
|
&stream_info_rtsp,
|
2008-08-07 12:20:50 +00:00
|
|
|
#ifdef CONFIG_LIVE555
|
2005-05-29 12:54:00 +00:00
|
|
|
&stream_info_sdp,
|
|
|
|
&stream_info_rtsp_sip,
|
|
|
|
#endif
|
2006-08-04 18:32:36 +00:00
|
|
|
&stream_info_rtp,
|
|
|
|
&stream_info_udp,
|
2005-05-29 12:54:00 +00:00
|
|
|
&stream_info_http2,
|
2003-04-03 12:00:50 +00:00
|
|
|
#endif
|
2008-08-03 15:21:40 +00:00
|
|
|
#ifdef CONFIG_DVBIN
|
2003-08-11 00:02:46 +00:00
|
|
|
&stream_info_dvb,
|
|
|
|
#endif
|
2008-07-30 12:01:30 +00:00
|
|
|
#ifdef CONFIG_TV
|
2006-07-31 18:36:29 +00:00
|
|
|
&stream_info_tv,
|
|
|
|
#endif
|
2008-07-30 12:01:30 +00:00
|
|
|
#ifdef CONFIG_RADIO
|
2006-08-28 17:05:18 +00:00
|
|
|
&stream_info_radio,
|
|
|
|
#endif
|
2008-08-03 15:21:40 +00:00
|
|
|
#ifdef CONFIG_PVR
|
2006-07-10 21:32:19 +00:00
|
|
|
&stream_info_pvr,
|
|
|
|
#endif
|
2008-08-07 08:58:07 +00:00
|
|
|
#ifdef CONFIG_FTP
|
2003-08-15 19:13:23 +00:00
|
|
|
&stream_info_ftp,
|
2005-02-27 04:25:12 +00:00
|
|
|
#endif
|
2008-08-07 08:58:07 +00:00
|
|
|
#ifdef CONFIG_VSTREAM
|
2005-02-27 04:25:12 +00:00
|
|
|
&stream_info_vstream,
|
2005-05-19 20:58:11 +00:00
|
|
|
#endif
|
2008-08-07 12:20:50 +00:00
|
|
|
#ifdef CONFIG_LIBSMBCLIENT
|
2005-05-19 20:58:11 +00:00
|
|
|
&stream_info_smb,
|
2003-08-15 19:13:23 +00:00
|
|
|
#endif
|
2005-05-15 07:38:42 +00:00
|
|
|
&stream_info_cue,
|
2008-07-30 12:01:30 +00:00
|
|
|
#ifdef CONFIG_DVDREAD
|
2007-12-02 13:13:02 +00:00
|
|
|
&stream_info_ifo,
|
2005-05-19 19:50:39 +00:00
|
|
|
&stream_info_dvd,
|
2005-12-14 21:52:41 +00:00
|
|
|
#endif
|
2008-07-30 12:01:30 +00:00
|
|
|
#ifdef CONFIG_DVDNAV
|
2006-06-14 21:58:17 +00:00
|
|
|
&stream_info_dvdnav,
|
2005-05-19 19:50:39 +00:00
|
|
|
#endif
|
2010-07-05 17:04:46 +00:00
|
|
|
#ifdef CONFIG_LIBBLURAY
|
|
|
|
&stream_info_bluray,
|
|
|
|
#endif
|
2010-10-31 00:19:56 +00:00
|
|
|
#ifdef CONFIG_FFMPEG
|
2009-11-17 16:09:17 +00:00
|
|
|
&stream_info_ffmpeg,
|
|
|
|
#endif
|
2005-05-19 19:50:39 +00:00
|
|
|
|
2003-04-10 10:59:12 +00:00
|
|
|
&stream_info_null,
|
2006-08-03 19:43:02 +00:00
|
|
|
&stream_info_mf,
|
2003-04-02 16:25:07 +00:00
|
|
|
&stream_info_file,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2010-03-09 23:46:46 +00:00
|
|
|
static stream_t *open_stream_plugin(const stream_info_t *sinfo,
|
|
|
|
const char *filename,
|
2008-04-23 03:35:36 +00:00
|
|
|
int mode, struct MPOpts *options,
|
|
|
|
int *file_format, int *ret,
|
|
|
|
char **redirected_url)
|
|
|
|
{
|
2003-04-02 16:25:07 +00:00
|
|
|
void* arg = NULL;
|
|
|
|
stream_t* s;
|
|
|
|
m_struct_t* desc = (m_struct_t*)sinfo->opts;
|
|
|
|
|
|
|
|
// Parse options
|
|
|
|
if(desc) {
|
|
|
|
arg = m_struct_alloc(desc);
|
|
|
|
if(sinfo->opts_url) {
|
2009-07-06 23:26:13 +00:00
|
|
|
m_option_t url_opt =
|
2003-04-02 16:25:07 +00:00
|
|
|
{ "stream url", arg , CONF_TYPE_CUSTOM_URL, 0, 0 ,0, sinfo->opts };
|
|
|
|
if(m_option_parse(&url_opt,"stream url",filename,arg,M_CONFIG_FILE) < 0) {
|
|
|
|
mp_msg(MSGT_OPEN,MSGL_ERR, "URL parsing failed on url %s\n",filename);
|
|
|
|
m_struct_free(desc,arg);
|
|
|
|
return NULL;
|
2009-07-06 23:26:13 +00:00
|
|
|
}
|
2003-04-02 16:25:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
s = new_stream(-2,-2);
|
2008-04-23 03:35:36 +00:00
|
|
|
s->opts = options;
|
2003-04-02 16:25:07 +00:00
|
|
|
s->url=strdup(filename);
|
|
|
|
s->flags |= mode;
|
|
|
|
*ret = sinfo->open(s,mode,arg,file_format);
|
|
|
|
if((*ret) != STREAM_OK) {
|
2010-07-17 13:14:53 +00:00
|
|
|
#ifdef CONFIG_NETWORKING
|
2007-11-26 00:41:21 +00:00
|
|
|
if (*ret == STREAM_REDIRECTED && redirected_url) {
|
|
|
|
if (s->streaming_ctrl && s->streaming_ctrl->url
|
|
|
|
&& s->streaming_ctrl->url->url)
|
|
|
|
*redirected_url = strdup(s->streaming_ctrl->url->url);
|
|
|
|
else
|
|
|
|
*redirected_url = NULL;
|
|
|
|
}
|
|
|
|
streaming_ctrl_free(s->streaming_ctrl);
|
|
|
|
#endif
|
2003-04-02 16:25:07 +00:00
|
|
|
free(s->url);
|
|
|
|
free(s);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if(s->type <= -2)
|
|
|
|
mp_msg(MSGT_OPEN,MSGL_WARN, "Warning streams need a type !!!!\n");
|
2009-11-22 15:18:21 +00:00
|
|
|
if(s->flags & MP_STREAM_SEEK && !s->seek)
|
|
|
|
s->flags &= ~MP_STREAM_SEEK;
|
|
|
|
if(s->seek && !(s->flags & MP_STREAM_SEEK))
|
|
|
|
s->flags |= MP_STREAM_SEEK;
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2006-12-18 20:56:24 +00:00
|
|
|
s->mode = mode;
|
2003-04-02 16:25:07 +00:00
|
|
|
|
|
|
|
mp_msg(MSGT_OPEN,MSGL_V, "STREAM: [%s] %s\n",sinfo->name,filename);
|
|
|
|
mp_msg(MSGT_OPEN,MSGL_V, "STREAM: Description: %s\n",sinfo->info);
|
|
|
|
mp_msg(MSGT_OPEN,MSGL_V, "STREAM: Author: %s\n", sinfo->author);
|
|
|
|
mp_msg(MSGT_OPEN,MSGL_V, "STREAM: Comment: %s\n", sinfo->comment);
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2003-04-02 16:25:07 +00:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-09 23:46:46 +00:00
|
|
|
stream_t *open_stream_full(const char *filename, int mode,
|
|
|
|
struct MPOpts *options, int *file_format)
|
2008-04-23 03:35:36 +00:00
|
|
|
{
|
2003-04-02 16:25:07 +00:00
|
|
|
int i,j,l,r;
|
2007-12-01 16:31:49 +00:00
|
|
|
const stream_info_t* sinfo;
|
2003-04-02 16:25:07 +00:00
|
|
|
stream_t* s;
|
2007-11-26 00:41:21 +00:00
|
|
|
char *redirected_url = NULL;
|
2003-04-02 16:25:07 +00:00
|
|
|
|
|
|
|
for(i = 0 ; auto_open_streams[i] ; i++) {
|
|
|
|
sinfo = auto_open_streams[i];
|
|
|
|
if(!sinfo->protocols) {
|
2003-08-31 18:51:05 +00:00
|
|
|
mp_msg(MSGT_OPEN,MSGL_WARN, "Stream type %s has protocols == NULL, it's a bug\n", sinfo->name);
|
2003-04-02 16:25:07 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for(j = 0 ; sinfo->protocols[j] ; j++) {
|
|
|
|
l = strlen(sinfo->protocols[j]);
|
|
|
|
// l == 0 => Don't do protocol matching (ie network and filenames)
|
2006-12-05 18:30:48 +00:00
|
|
|
if((l == 0 && !strstr(filename, "://")) ||
|
2007-12-19 01:28:00 +00:00
|
|
|
((strncasecmp(sinfo->protocols[j],filename,l) == 0) &&
|
2003-04-02 16:25:07 +00:00
|
|
|
(strncmp("://",filename+l,3) == 0))) {
|
|
|
|
*file_format = DEMUXER_TYPE_UNKNOWN;
|
2007-11-26 00:41:21 +00:00
|
|
|
s = open_stream_plugin(sinfo,filename,mode,options,file_format,&r,
|
|
|
|
&redirected_url);
|
2003-04-02 16:25:07 +00:00
|
|
|
if(s) return s;
|
2007-11-26 00:41:21 +00:00
|
|
|
if(r == STREAM_REDIRECTED && redirected_url) {
|
|
|
|
mp_msg(MSGT_OPEN,MSGL_V, "[%s] open %s redirected to %s\n",
|
|
|
|
sinfo->info, filename, redirected_url);
|
|
|
|
s = open_stream_full(redirected_url, mode, options, file_format);
|
|
|
|
free(redirected_url);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
else if(r != STREAM_UNSUPPORTED) {
|
2009-07-06 22:15:02 +00:00
|
|
|
mp_tmsg(MSGT_OPEN,MSGL_ERR, "Failed to open %s.\n",filename);
|
2003-04-02 16:25:07 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_msg(MSGT_OPEN,MSGL_ERR, "No stream found to handle url %s\n",filename);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-03-09 23:46:46 +00:00
|
|
|
stream_t *open_output_stream(const char *filename, struct MPOpts *options)
|
2008-04-23 03:35:36 +00:00
|
|
|
{
|
2006-12-18 20:56:24 +00:00
|
|
|
int file_format; //unused
|
|
|
|
if(!filename) {
|
|
|
|
mp_msg(MSGT_OPEN,MSGL_ERR,"open_output_stream(), NULL filename, report this bug\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return open_stream_full(filename,STREAM_WRITE,options,&file_format);
|
|
|
|
}
|
|
|
|
|
2001-04-22 16:56:20 +00:00
|
|
|
//=================== STREAMER =========================
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2010-11-02 01:17:41 +00:00
|
|
|
void stream_capture_do(stream_t *s)
|
|
|
|
{
|
|
|
|
if (fwrite(s->buffer, s->buf_len, 1, s->capture_file) < 1) {
|
|
|
|
mp_tmsg(MSGT_GLOBAL, MSGL_ERR, "Error writing capture file: %s\n",
|
|
|
|
strerror(errno));
|
|
|
|
fclose(s->capture_file);
|
|
|
|
s->capture_file = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-27 19:04:04 +00:00
|
|
|
int stream_read_internal(stream_t *s, void *buf, int len)
|
|
|
|
{
|
2010-05-26 18:23:43 +00:00
|
|
|
// we will retry even if we already reached EOF previously.
|
2001-02-24 20:28:24 +00:00
|
|
|
switch(s->type){
|
2001-05-03 23:32:56 +00:00
|
|
|
case STREAMTYPE_STREAM:
|
2010-07-17 13:14:53 +00:00
|
|
|
#ifdef CONFIG_NETWORKING
|
2007-11-08 23:07:19 +00:00
|
|
|
if( s->streaming_ctrl!=NULL && s->streaming_ctrl->streaming_read ) {
|
2010-10-27 19:04:04 +00:00
|
|
|
len=s->streaming_ctrl->streaming_read(s->fd, buf, len, s->streaming_ctrl);
|
2009-11-09 14:12:14 +00:00
|
|
|
} else
|
2001-02-24 20:28:24 +00:00
|
|
|
#endif
|
2009-11-17 18:05:54 +00:00
|
|
|
if (s->fill_buffer)
|
2010-10-27 19:04:04 +00:00
|
|
|
len = s->fill_buffer(s, buf, len);
|
2009-11-17 18:05:54 +00:00
|
|
|
else
|
2010-10-27 19:04:04 +00:00
|
|
|
len = read(s->fd, buf, len);
|
2009-11-09 14:08:35 +00:00
|
|
|
break;
|
2002-03-16 14:32:03 +00:00
|
|
|
case STREAMTYPE_DS:
|
2010-10-27 19:04:04 +00:00
|
|
|
len = demux_read_data((demux_stream_t*)s->priv, buf, len);
|
2002-03-16 14:32:03 +00:00
|
|
|
break;
|
2009-07-06 23:26:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
default:
|
2010-10-27 19:04:04 +00:00
|
|
|
len= s->fill_buffer ? s->fill_buffer(s, buf, len) : 0;
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
2010-02-20 18:53:07 +00:00
|
|
|
if(len<=0){ s->eof=1; return 0; }
|
2010-05-26 18:23:43 +00:00
|
|
|
// When reading succeeded we are obviously not at eof.
|
|
|
|
// This e.g. avoids issues with eof getting stuck when lavf seeks in MPEG-TS
|
|
|
|
s->eof=0;
|
2010-10-27 19:04:04 +00:00
|
|
|
s->pos+=len;
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
int stream_fill_buffer(stream_t *s){
|
|
|
|
int len = stream_read_internal(s, s->buffer, STREAM_BUFFER_SIZE);
|
|
|
|
if (len <= 0)
|
|
|
|
return 0;
|
2001-02-24 20:28:24 +00:00
|
|
|
s->buf_pos=0;
|
|
|
|
s->buf_len=len;
|
|
|
|
// printf("[%d]",len);fflush(stdout);
|
2010-11-02 01:17:41 +00:00
|
|
|
if (s->capture_file)
|
|
|
|
stream_capture_do(s);
|
2001-02-24 20:28:24 +00:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2006-12-18 20:56:24 +00:00
|
|
|
int stream_write_buffer(stream_t *s, unsigned char *buf, int len) {
|
|
|
|
int rd;
|
|
|
|
if(!s->write_buffer)
|
|
|
|
return -1;
|
|
|
|
rd = s->write_buffer(s, buf, len);
|
|
|
|
if(rd < 0)
|
|
|
|
return -1;
|
|
|
|
s->pos += rd;
|
|
|
|
return rd;
|
|
|
|
}
|
|
|
|
|
2010-10-27 19:04:04 +00:00
|
|
|
int stream_seek_internal(stream_t *s, off_t newpos)
|
|
|
|
{
|
2001-02-24 20:28:24 +00:00
|
|
|
if(newpos==0 || newpos!=s->pos){
|
|
|
|
switch(s->type){
|
2001-05-03 23:32:56 +00:00
|
|
|
case STREAMTYPE_STREAM:
|
|
|
|
//s->pos=newpos; // real seek
|
2001-11-20 22:22:21 +00:00
|
|
|
// Some streaming protocol allow to seek backward and forward
|
|
|
|
// A function call that return -1 can tell that the protocol
|
|
|
|
// doesn't support seeking.
|
2010-07-17 13:14:53 +00:00
|
|
|
#ifdef CONFIG_NETWORKING
|
2004-02-17 12:30:45 +00:00
|
|
|
if(s->seek) { // new stream seek is much cleaner than streaming_ctrl one
|
|
|
|
if(!s->seek(s,newpos)) {
|
|
|
|
mp_msg(MSGT_STREAM,MSGL_ERR, "Seek failed\n");
|
2004-03-13 16:10:02 +00:00
|
|
|
return 0;
|
2004-02-17 12:30:45 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2003-01-12 20:16:28 +00:00
|
|
|
if( s->streaming_ctrl!=NULL && s->streaming_ctrl->streaming_seek ) {
|
2010-10-27 19:04:04 +00:00
|
|
|
if( s->streaming_ctrl->streaming_seek( s->fd, newpos, s->streaming_ctrl )<0 ) {
|
2001-11-20 22:22:21 +00:00
|
|
|
mp_msg(MSGT_STREAM,MSGL_INFO,"Stream not seekable!\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2009-11-17 18:08:18 +00:00
|
|
|
break;
|
2009-07-06 23:26:13 +00:00
|
|
|
}
|
2009-11-17 18:08:18 +00:00
|
|
|
#endif
|
2001-05-03 23:32:56 +00:00
|
|
|
if(newpos<s->pos){
|
2001-09-26 21:35:14 +00:00
|
|
|
mp_msg(MSGT_STREAM,MSGL_INFO,"Cannot seek backward in linear streams!\n");
|
2001-05-03 23:32:56 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
2002-09-15 20:51:55 +00:00
|
|
|
default:
|
2003-04-02 16:25:07 +00:00
|
|
|
// This should at the beginning as soon as all streams are converted
|
|
|
|
if(!s->seek)
|
|
|
|
return 0;
|
|
|
|
// Now seek
|
|
|
|
if(!s->seek(s,newpos)) {
|
|
|
|
mp_msg(MSGT_STREAM,MSGL_ERR, "Seek failed\n");
|
|
|
|
return 0;
|
|
|
|
}
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
|
|
|
// putchar('.');fflush(stdout);
|
|
|
|
//} else {
|
|
|
|
// putchar('%');fflush(stdout);
|
|
|
|
}
|
2010-10-27 19:04:04 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int stream_seek_long(stream_t *s,off_t pos){
|
|
|
|
int res;
|
|
|
|
off_t newpos=0;
|
|
|
|
|
|
|
|
// if( mp_msg_test(MSGT_STREAM,MSGL_DBG3) ) printf("seek_long to 0x%X\n",(unsigned int)pos);
|
|
|
|
|
|
|
|
s->buf_pos=s->buf_len=0;
|
|
|
|
|
|
|
|
if(s->mode == STREAM_WRITE) {
|
|
|
|
if(!s->seek || !s->seek(s,pos))
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(s->sector_size)
|
|
|
|
newpos = (pos/s->sector_size)*s->sector_size;
|
|
|
|
else
|
|
|
|
newpos = pos&(~((off_t)STREAM_BUFFER_SIZE-1));
|
|
|
|
|
|
|
|
if( mp_msg_test(MSGT_STREAM,MSGL_DBG3) ){
|
|
|
|
mp_msg(MSGT_STREAM,MSGL_DBG3, "s->pos=%"PRIX64" newpos=%"PRIX64" new_bufpos=%"PRIX64" buflen=%X \n",
|
|
|
|
(int64_t)s->pos,(int64_t)newpos,(int64_t)pos,s->buf_len);
|
|
|
|
}
|
|
|
|
pos-=newpos;
|
|
|
|
|
|
|
|
res = stream_seek_internal(s, newpos);
|
|
|
|
if (res >= 0)
|
|
|
|
return res;
|
|
|
|
|
|
|
|
while(s->pos<newpos){
|
|
|
|
if(stream_fill_buffer(s)<=0) break; // EOF
|
|
|
|
}
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2010-01-18 12:38:56 +00:00
|
|
|
s->eof = 0; // EOF reset when seek succeeds.
|
|
|
|
while (stream_fill_buffer(s) > 0) {
|
2006-03-16 14:24:22 +00:00
|
|
|
if(pos<=s->buf_len){
|
2001-02-24 20:28:24 +00:00
|
|
|
s->buf_pos=pos; // byte position in sector
|
|
|
|
return 1;
|
|
|
|
}
|
2006-03-16 14:24:22 +00:00
|
|
|
pos -= s->buf_len;
|
|
|
|
}
|
2010-01-18 12:38:56 +00:00
|
|
|
// Fill failed, but seek still is a success.
|
|
|
|
s->pos += pos;
|
|
|
|
s->buf_pos = 0;
|
|
|
|
s->buf_len = 0;
|
|
|
|
|
|
|
|
mp_msg(MSGT_STREAM,MSGL_V,
|
|
|
|
"stream_seek: Seek to/past EOF: no buffer preloaded.\n");
|
|
|
|
return 1;
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void stream_reset(stream_t *s){
|
|
|
|
if(s->eof){
|
2010-02-20 18:53:07 +00:00
|
|
|
s->pos=0;
|
|
|
|
s->buf_pos=s->buf_len=0;
|
2001-02-24 20:28:24 +00:00
|
|
|
s->eof=0;
|
|
|
|
}
|
2003-04-06 16:36:17 +00:00
|
|
|
if(s->control) s->control(s,STREAM_CTRL_RESET,NULL);
|
2001-02-24 20:28:24 +00:00
|
|
|
//stream_seek(s,0);
|
|
|
|
}
|
|
|
|
|
2006-03-01 21:56:30 +00:00
|
|
|
int stream_control(stream_t *s, int cmd, void *arg){
|
2007-08-28 22:38:45 +00:00
|
|
|
if(!s->control) return STREAM_UNSUPPORTED;
|
2008-07-30 12:01:30 +00:00
|
|
|
#ifdef CONFIG_STREAM_CACHE
|
2008-05-24 07:48:35 +00:00
|
|
|
if (s->cache_pid)
|
|
|
|
return cache_do_control(s, cmd, arg);
|
2008-07-05 14:22:57 +00:00
|
|
|
#endif
|
2006-02-27 21:06:47 +00:00
|
|
|
return s->control(s, cmd, arg);
|
|
|
|
}
|
|
|
|
|
2001-10-10 00:47:17 +00:00
|
|
|
stream_t* new_memory_stream(unsigned char* data,int len){
|
2006-06-04 22:41:27 +00:00
|
|
|
stream_t *s;
|
|
|
|
|
|
|
|
if(len < 0)
|
|
|
|
return NULL;
|
2009-11-17 19:14:42 +00:00
|
|
|
s=calloc(1, sizeof(stream_t)+len);
|
2001-10-10 00:47:17 +00:00
|
|
|
s->fd=-1;
|
|
|
|
s->type=STREAMTYPE_MEMORY;
|
|
|
|
s->buf_pos=0; s->buf_len=len;
|
|
|
|
s->start_pos=0; s->end_pos=len;
|
|
|
|
stream_reset(s);
|
|
|
|
s->pos=len;
|
|
|
|
memcpy(s->buffer,data,len);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2001-02-24 20:28:24 +00:00
|
|
|
stream_t* new_stream(int fd,int type){
|
2009-11-17 19:14:42 +00:00
|
|
|
stream_t *s=calloc(1, sizeof(stream_t));
|
2001-12-03 17:14:36 +00:00
|
|
|
if(s==NULL) return NULL;
|
2003-06-11 16:48:09 +00:00
|
|
|
|
2009-02-01 13:42:27 +00:00
|
|
|
#if HAVE_WINSOCK2_H
|
2003-06-11 16:48:09 +00:00
|
|
|
{
|
|
|
|
WSADATA wsdata;
|
|
|
|
int temp = WSAStartup(0x0202, &wsdata); // there might be a better place for this (-> later)
|
|
|
|
mp_msg(MSGT_STREAM,MSGL_V,"WINSOCK2 init: %i\n", temp);
|
|
|
|
}
|
|
|
|
#endif
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2001-02-24 20:28:24 +00:00
|
|
|
s->fd=fd;
|
|
|
|
s->type=type;
|
2001-03-21 03:20:17 +00:00
|
|
|
s->buf_pos=s->buf_len=0;
|
2001-04-23 21:25:09 +00:00
|
|
|
s->start_pos=s->end_pos=0;
|
2001-08-22 23:54:38 +00:00
|
|
|
s->priv=NULL;
|
2002-09-15 19:47:49 +00:00
|
|
|
s->url=NULL;
|
2001-10-20 23:51:02 +00:00
|
|
|
s->cache_pid=0;
|
2001-02-24 20:28:24 +00:00
|
|
|
stream_reset(s);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
void free_stream(stream_t *s){
|
2002-05-20 03:25:26 +00:00
|
|
|
// printf("\n*** free_stream() called ***\n");
|
2008-07-30 12:01:30 +00:00
|
|
|
#ifdef CONFIG_STREAM_CACHE
|
2003-04-12 13:53:33 +00:00
|
|
|
cache_uninit(s);
|
2003-04-02 16:52:00 +00:00
|
|
|
#endif
|
2010-11-02 01:17:41 +00:00
|
|
|
if (s->capture_file) {
|
|
|
|
fclose(s->capture_file);
|
|
|
|
s->capture_file = NULL;
|
|
|
|
}
|
|
|
|
|
2005-05-19 20:58:11 +00:00
|
|
|
if(s->close) s->close(s);
|
2005-05-12 22:07:11 +00:00
|
|
|
if(s->fd>0){
|
|
|
|
/* on unix we define closesocket to close
|
|
|
|
on windows however we have to distinguish between
|
|
|
|
network socket and file */
|
|
|
|
if(s->url && strstr(s->url,"://"))
|
|
|
|
closesocket(s->fd);
|
|
|
|
else close(s->fd);
|
|
|
|
}
|
2009-02-01 13:42:27 +00:00
|
|
|
#if HAVE_WINSOCK2_H
|
2003-06-11 16:48:09 +00:00
|
|
|
mp_msg(MSGT_STREAM,MSGL_V,"WINSOCK2 uninit\n");
|
|
|
|
WSACleanup(); // there might be a better place for this (-> later)
|
|
|
|
#endif
|
2003-04-02 16:25:07 +00:00
|
|
|
// Disabled atm, i don't like that. s->priv can be anything after all
|
|
|
|
// streams should destroy their priv on close
|
2010-11-07 12:47:40 +00:00
|
|
|
//free(s->priv);
|
|
|
|
free(s->url);
|
2001-02-24 20:28:24 +00:00
|
|
|
free(s);
|
|
|
|
}
|
|
|
|
|
2002-03-16 14:32:03 +00:00
|
|
|
stream_t* new_ds_stream(demux_stream_t *ds) {
|
|
|
|
stream_t* s = new_stream(-1,STREAMTYPE_DS);
|
|
|
|
s->priv = ds;
|
|
|
|
return s;
|
|
|
|
}
|
2008-04-09 00:36:28 +00:00
|
|
|
|
2008-04-30 04:15:52 +00:00
|
|
|
void stream_set_interrupt_callback(int (*cb)(struct input_ctx *, int),
|
|
|
|
struct input_ctx *ctx)
|
|
|
|
{
|
2008-04-09 00:36:28 +00:00
|
|
|
stream_check_interrupt_cb = cb;
|
2008-04-30 04:15:52 +00:00
|
|
|
stream_check_interrupt_ctx = ctx;
|
2008-04-09 00:36:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int stream_check_interrupt(int time) {
|
2010-05-28 18:49:02 +00:00
|
|
|
if(!stream_check_interrupt_cb) {
|
|
|
|
usec_sleep(time * 1000);
|
|
|
|
return 0;
|
|
|
|
}
|
2008-04-30 04:15:52 +00:00
|
|
|
return stream_check_interrupt_cb(stream_check_interrupt_ctx, time);
|
2008-04-09 00:36:28 +00:00
|
|
|
}
|
2010-02-28 13:54:55 +00:00
|
|
|
|
2010-02-28 15:24:30 +00:00
|
|
|
/**
|
|
|
|
* Helper function to read 16 bits little-endian and advance pointer
|
|
|
|
*/
|
|
|
|
static uint16_t get_le16_inc(const uint8_t **buf)
|
|
|
|
{
|
|
|
|
uint16_t v = AV_RL16(*buf);
|
|
|
|
*buf += 2;
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function to read 16 bits big-endian and advance pointer
|
|
|
|
*/
|
|
|
|
static uint16_t get_be16_inc(const uint8_t **buf)
|
|
|
|
{
|
|
|
|
uint16_t v = AV_RB16(*buf);
|
|
|
|
*buf += 2;
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find a newline character in buffer
|
|
|
|
* \param buf buffer to search
|
|
|
|
* \param len amount of bytes to search in buffer, may not overread
|
|
|
|
* \param utf16 chose between UTF-8/ASCII/other and LE and BE UTF-16
|
|
|
|
* 0 = UTF-8/ASCII/other, 1 = UTF-16-LE, 2 = UTF-16-BE
|
|
|
|
*/
|
|
|
|
static const uint8_t *find_newline(const uint8_t *buf, int len, int utf16)
|
|
|
|
{
|
|
|
|
uint32_t c;
|
|
|
|
const uint8_t *end = buf + len;
|
|
|
|
switch (utf16) {
|
|
|
|
case 0:
|
|
|
|
return (uint8_t *)memchr(buf, '\n', len);
|
|
|
|
case 1:
|
|
|
|
while (buf < end - 1) {
|
|
|
|
GET_UTF16(c, buf < end - 1 ? get_le16_inc(&buf) : 0, return NULL;)
|
|
|
|
if (buf <= end && c == '\n')
|
|
|
|
return buf - 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
while (buf < end - 1) {
|
|
|
|
GET_UTF16(c, buf < end - 1 ? get_be16_inc(&buf) : 0, return NULL;)
|
|
|
|
if (buf <= end && c == '\n')
|
|
|
|
return buf - 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy a number of bytes, converting to UTF-8 if input is UTF-16
|
|
|
|
* \param dst buffer to copy to
|
|
|
|
* \param dstsize size of dst buffer
|
|
|
|
* \param src buffer to copy from
|
|
|
|
* \param len amount of bytes to copy from src
|
|
|
|
* \param utf16 chose between UTF-8/ASCII/other and LE and BE UTF-16
|
|
|
|
* 0 = UTF-8/ASCII/other, 1 = UTF-16-LE, 2 = UTF-16-BE
|
|
|
|
*/
|
|
|
|
static int copy_characters(uint8_t *dst, int dstsize,
|
|
|
|
const uint8_t *src, int *len, int utf16)
|
|
|
|
{
|
|
|
|
uint32_t c;
|
|
|
|
uint8_t *dst_end = dst + dstsize;
|
|
|
|
const uint8_t *end = src + *len;
|
|
|
|
switch (utf16) {
|
|
|
|
case 0:
|
|
|
|
if (*len > dstsize)
|
|
|
|
*len = dstsize;
|
|
|
|
memcpy(dst, src, *len);
|
|
|
|
return *len;
|
|
|
|
case 1:
|
|
|
|
while (src < end - 1 && dst_end - dst > 8) {
|
|
|
|
uint8_t tmp;
|
|
|
|
GET_UTF16(c, src < end - 1 ? get_le16_inc(&src) : 0, ;)
|
|
|
|
PUT_UTF8(c, tmp, *dst++ = tmp;)
|
|
|
|
}
|
|
|
|
*len -= end - src;
|
|
|
|
return dstsize - (dst_end - dst);
|
|
|
|
case 2:
|
|
|
|
while (src < end - 1 && dst_end - dst > 8) {
|
|
|
|
uint8_t tmp;
|
|
|
|
GET_UTF16(c, src < end - 1 ? get_be16_inc(&src) : 0, ;)
|
|
|
|
PUT_UTF8(c, tmp, *dst++ = tmp;)
|
|
|
|
}
|
|
|
|
*len -= end - src;
|
|
|
|
return dstsize - (dst_end - dst);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char* stream_read_line(stream_t *s,unsigned char* mem, int max, int utf16) {
|
2010-02-28 13:54:55 +00:00
|
|
|
int len;
|
2010-02-28 15:24:30 +00:00
|
|
|
const unsigned char *end;
|
|
|
|
unsigned char *ptr = mem;
|
2010-02-28 13:54:55 +00:00
|
|
|
if (max < 1) return NULL;
|
|
|
|
max--; // reserve one for 0-termination
|
|
|
|
do {
|
|
|
|
len = s->buf_len-s->buf_pos;
|
|
|
|
// try to fill the buffer
|
|
|
|
if(len <= 0 &&
|
|
|
|
(!cache_stream_fill_buffer(s) ||
|
|
|
|
(len = s->buf_len-s->buf_pos) <= 0)) break;
|
2010-02-28 15:24:30 +00:00
|
|
|
end = find_newline(s->buffer+s->buf_pos, len, utf16);
|
2010-02-28 13:54:55 +00:00
|
|
|
if(end) len = end - (s->buffer+s->buf_pos) + 1;
|
|
|
|
if(len > 0 && max > 0) {
|
2010-02-28 15:24:30 +00:00
|
|
|
int l = copy_characters(ptr, max, s->buffer+s->buf_pos, &len, utf16);
|
2010-02-28 13:54:55 +00:00
|
|
|
max -= l;
|
|
|
|
ptr += l;
|
2010-02-28 15:24:30 +00:00
|
|
|
if (!len)
|
|
|
|
break;
|
2010-02-28 13:54:55 +00:00
|
|
|
}
|
|
|
|
s->buf_pos += len;
|
|
|
|
} while(!end);
|
|
|
|
ptr[0] = 0;
|
2010-08-21 10:08:29 +00:00
|
|
|
if(s->eof && ptr == mem) return NULL;
|
2010-02-28 13:54:55 +00:00
|
|
|
return mem;
|
|
|
|
}
|