Exemple #1
0
static int img_read_packet(AVFormatContext *s1, AVPacket *pkt)
{
    VideoData *s = s1->priv_data;
    char filename[1024];
    int i;
    int size[3]={0}, ret[3]={0};
    ByteIOContext f1[3], *f[3]= {&f1[0], &f1[1], &f1[2]};
    AVCodecContext *codec= &s1->streams[0]->codec;

    if (!s->is_pipe) {
        /* loop over input */
        if (loop_input && s->img_number > s->img_last) {
            s->img_number = s->img_first;
        }
        if (get_frame_filename(filename, sizeof(filename),
                               s->path, s->img_number)<0 && s->img_number > 1)
            return AVERROR_IO;
        for(i=0; i<3; i++){
            if (url_fopen(f[i], filename, URL_RDONLY) < 0)
                return AVERROR_IO;
            size[i]= url_filesize(url_fileno(f[i]));
            
            if(codec->codec_id != CODEC_ID_RAWVIDEO)
                break;
            filename[ strlen(filename) - 1 ]= 'U' + i;
        }
        
        if(codec->codec_id == CODEC_ID_RAWVIDEO && !codec->width)
            infer_size(&codec->width, &codec->height, size[0]);
    } else {
        f[0] = &s1->pb;
        if (url_feof(f[0]))
            return AVERROR_IO;
        size[0]= 4096;
    }

    av_new_packet(pkt, size[0] + size[1] + size[2]);
    pkt->stream_index = 0;
    pkt->flags |= PKT_FLAG_KEY;

    pkt->size= 0;
    for(i=0; i<3; i++){
        if(size[i]){
            ret[i]= get_buffer(f[i], pkt->data + pkt->size, size[i]);
            if (!s->is_pipe)
                url_fclose(f[i]);
            if(ret[i]>0)
                pkt->size += ret[i];
        }
    }

    if (ret[0] <= 0 || ret[1]<0 || ret[2]<0) {
        av_free_packet(pkt);
        return AVERROR_IO; /* signal EOF */
    } else {
        s->img_count++;
        s->img_number++;
        return 0;
    }
}
static int64_t read_data_exseek(URLContext *opaque, int64_t offset, int whence)
{
#ifdef  USED_LP_BUF
	URLContext *url=opaque;
	struct variant *v = url->priv_data;
	HLSContext *c = v->parent->priv_data;
#else
	struct variant *v = opaque;
	HLSContext *c = v->parent->priv_data;
#endif
	int ret=AVERROR(EPIPE);;
	if (whence == AVSEEK_FULLTIME)
	{
		//no support
	}
	else if (whence == AVSEEK_BUFFERED_TIME)
	{
		struct segment *seg = v->segments[v->cur_seq_no - v->start_seq_no];
		int bufedtime=0;
		if(seg){
			bufedtime=seg->seg_starttime;
			if(v->input)
				bufedtime+=seg->duration*url_seek(v->input, 0, SEEK_CUR)/url_filesize(v->input);
			return bufedtime;
		}
	}
	else if(whence == AVSEEK_TO_TIME ){
	 	//no support
	}
	else if(whence == AVSEEK_SIZE){
	 	//no support
	}
	return ret;
}	
static int open_f(stream_t *stream, int mode, void *opts, int *file_format)
{
    int flags = 0;
    const char *filename;
    URLContext *ctx = NULL;
    int res = STREAM_ERROR;
    int64_t size;
    int dummy;

    av_register_all();
    if (mode == STREAM_READ)
        flags = URL_RDONLY;
    else if (mode == STREAM_WRITE)
        flags = URL_WRONLY;
    else {
        mp_msg(MSGT_OPEN, MSGL_ERR, "[ffmpeg] Unknown open mode %d\n", mode);
        res = STREAM_UNSUPPORTED;
        goto out;
    }

    if (stream->url)
        filename = stream->url;
    else {
        mp_msg(MSGT_OPEN, MSGL_ERR, "[ffmpeg] No URL\n");
        goto out;
    }
    if (!strncmp(filename, prefix, strlen(prefix)))
        filename += strlen(prefix);
    dummy = !strncmp(filename, "rtsp:", 5);
    mp_msg(MSGT_OPEN, MSGL_V, "[ffmpeg] Opening %s\n", filename);

    if (!dummy && url_open(&ctx, filename, flags) < 0)
        goto out;

    stream->priv = ctx;
    size = dummy ? 0 : url_filesize(ctx);
    if (size >= 0)
        stream->end_pos = size;
    stream->type = STREAMTYPE_FILE;
    stream->seek = seek;
    if (dummy || ctx->is_streamed) {
        stream->type = STREAMTYPE_STREAM;
        stream->seek = NULL;
    }
    if (!dummy) {
        stream->fill_buffer = fill_buffer;
        stream->write_buffer = write_buffer;
        stream->control = control;
        stream->close = close_f;
    }
    res = STREAM_OK;

out:
    return res;
}
static struct input_stream *
input_ffmpeg_open(const char *uri,
		  GMutex *mutex, GCond *cond,
		  GError **error_r)
{
	struct input_ffmpeg *i;

	if (!g_str_has_prefix(uri, "gopher://") &&
	    !g_str_has_prefix(uri, "rtp://") &&
	    !g_str_has_prefix(uri, "rtsp://") &&
	    !g_str_has_prefix(uri, "rtmp://") &&
	    !g_str_has_prefix(uri, "rtmpt://") &&
	    !g_str_has_prefix(uri, "rtmps://"))
		return NULL;

	i = g_new(struct input_ffmpeg, 1);
	input_stream_init(&i->base, &input_plugin_ffmpeg, uri,
			  mutex, cond);

#if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(53,1,0)
	int ret = avio_open(&i->h, uri, AVIO_FLAG_READ);
#elif LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(53,0,0)
	int ret = avio_open(&i->h, uri, AVIO_RDONLY);
#else
	int ret = url_open(&i->h, uri, URL_RDONLY);
#endif
	if (ret != 0) {
		g_free(i);
		g_set_error(error_r, ffmpeg_quark(), ret,
			    "libavformat failed to open the URI");
		return NULL;
	}

	i->eof = false;

	i->base.ready = true;
#if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(53,0,0)
	i->base.seekable = (i->h->seekable & AVIO_SEEKABLE_NORMAL) != 0;
	i->base.size = avio_size(i->h);
#else
	i->base.seekable = !i->h->is_streamed;
	i->base.size = url_filesize(i->h);
#endif

	/* hack to make MPD select the "ffmpeg" decoder plugin - since
	   avio.h doesn't tell us the MIME type of the resource, we
	   can't select a decoder plugin, but the "ffmpeg" plugin is
	   quite good at auto-detection */
	i->base.mime = g_strdup("audio/x-mpd-ffmpeg");

	return &i->base;
}
static int hls_buffering_data(AVFormatContext *s,int size){
	HLSContext *c = s->priv_data;
	int i;
	int ret=-1;
	struct variant *var =NULL,*v;
	 if (c && c->end_of_segment || c->first_packet) {
        	recheck_discard_flags(s, c->first_packet);
		c->end_of_segment=0;		
		c->first_packet=0;
    	}
	
	for (i = 0; i < c->n_variants; i++) {
      	  	v = c->variants[i];
		if(v->needed){
			var=v;
			break;
		}
	}
	if(!var)
		return ret;
	if(size<0){
		struct segment *seg = NULL;
		int bufedtime=0;
		if(var->cur_seq_no>=v->start_seq_no + v->n_segments){/*bufed to the end segments.*/
			seg=var->segments[var->start_seq_no+v->n_segments-1];
			if(seg)
				bufedtime=seg->seg_starttime+seg->duration;
			return bufedtime;
		}
		seg=var->segments[var->cur_seq_no - var->start_seq_no];
		
		if(seg){
			int64_t pos,filesize;
			bufedtime=seg->seg_starttime;
			if(var->input ){
				pos=url_seek(var->input, 0, SEEK_CUR);
				filesize=url_filesize(var->input);
				if(filesize>0 && pos<=filesize){
					bufedtime+=seg->duration*pos/filesize;
				}	
			}
			return bufedtime;
		}
		return -1;
	}else{
	 	if(var->urllpbuf.lpbuf)
	  		ret=url_lp_intelligent_buffering(&var->urllpbuf,size);
	}
	 return ret;
}
Exemple #6
0
static int img_read_packet(AVFormatContext *s1, AVPacket *pkt)
{
    VideoData *s = s1->priv_data;
    char filename[1024];
    int ret;
    ByteIOContext f1, *f;

    if (!s->is_pipe) {
        /* loop over input */
/*        if (loop_input && s->img_number > s->img_last) {
            s->img_number = s->img_first;
        }*/
        if (get_frame_filename(filename, sizeof(filename),
                               s->path, s->img_number)<0 && s->img_number > 1)
            return AVERROR_IO;
        f = &f1;
        if (url_fopen(f, filename, URL_RDONLY) < 0)
            return AVERROR_IO;
    } else {
        f = &s1->pb;
        if (url_feof(f))
            return AVERROR_IO;
    }

    if (s->is_pipe) {
        av_new_packet(pkt, 4096);
    }else{
        av_new_packet(pkt, url_filesize(url_fileno(f)));
    }
    pkt->stream_index = 0;
    pkt->flags |= PKT_FLAG_KEY;

    ret = get_buffer(f, pkt->data, pkt->size);
    if (!s->is_pipe) {
        url_fclose(f);
    }

    if (ret <= 0) {
        av_free_packet(pkt);
        return AVERROR_IO; /* signal EOF */
    } else {
        s->img_count++;
        s->img_number++;
        return 0;
    }
}
static int control(stream_t *s, int cmd, void *arg)
{
    int64_t size, ts;
    double pts;
    switch(cmd) {
    case STREAM_CTRL_GET_SIZE:
        size = url_filesize(s->priv);
        if(size >= 0) {
            *(loff_t *)arg = size;
            return 1;
        }
        break;
    case STREAM_CTRL_SEEK_TO_TIME:
        pts = *(double *)arg;
        ts = pts * AV_TIME_BASE;
        ts = av_url_read_seek(s->priv, -1, ts, 0);
        if (ts >= 0)
            return 1;
        break;
    }
    return STREAM_UNSUPPORTED;
}
Exemple #8
0
int url_lpopen(URLContext *s,int size)
{
	url_lpbuf_t *lp;
	int blocksize=32*1024;
	int ret;
	float value=0.0;
	int bufsize=0;

	if(size==0){
		//size=am_getconfig_float("libplayer.ffmpeg.lpbufsizemax",&value);
		//if(size<=0)
			size=IO_LP_BUFFER_SIZE;
	}
	lp_bprint( AV_LOG_INFO,"url_lpopen=%d\n",size);
	if(!s)
		return -1;
		lp_bprint( AV_LOG_INFO,"url_lpopen2=%d\n",size);
	//ret=am_getconfig_float("libplayer.ffmpeg.lpbufblocksize",&value);
	//if(ret>=0 && value>=32){
	//	blocksize=(int)value;
	//}	
	lp_sprint( AV_LOG_INFO,"lpbuffer block size=%d\n",blocksize);
	lp=av_mallocz(sizeof(url_lpbuf_t));
	if(!lp)
		return AVERROR(ENOMEM);
	lp->buffer=av_malloc(size);
	if(!lp->buffer)
	{
		int failedsize=size/2;/*if no memory used 1/2 size */
		//ret=am_getconfig_float("libplayer.ffmpeg.lpbuffaildsize",&value);
		//if(ret>=0 && value>=1024){
		//	failedsize=(int)value;
		//}
		lp_sprint( AV_LOG_INFO,"malloc buf failed,used failed size=%d\n",failedsize);

		lp->buffer=av_malloc(failedsize);	
		while(!lp->buffer){
			failedsize=failedsize/2;
			if(failedsize<16*1024)/*do't malloc too small size failed size*/
				return AVERROR(ENOMEM);
			lp->buffer=av_malloc(failedsize);
		}
		bufsize=failedsize;
	}else{
		bufsize=size;
	}
	lp_sprint( AV_LOG_INFO,"url_lpopen used lp buf size=%d\n",bufsize);
	s->lpbuf=lp;
	lp->buffer_size=bufsize;
	lp->rp=lp->buffer;
	lp->wp=lp->buffer;
	lp->buffer_end=lp->buffer+bufsize;
	lp->valid_data_size=0;
	lp->pos=0;
	lp->block_read_size=FFMIN(blocksize,bufsize>>4);
	lp_lock_init(&lp->mutex,NULL);
	lp->file_size=url_lpseek(s,0,AVSEEK_SIZE);
	lp->cache_enable=0;
	lp->cache_id=aviolp_cache_open(s->filename,url_filesize(s));
	lp->dbg_cnt=0;
	if(lp->cache_id!=0)
		lp->cache_enable=1;
	lp_bprint( AV_LOG_INFO,"url_lpopen4%d\n",bufsize);
	return 0;
}
long long StreamingRingBuffer::GetRealFileSize(void) const
{
    if (m_context)
        return url_filesize(m_context);
    return -1;
}
Exemple #10
0
static av_cold int concat_open(URLContext *h, const char *uri, int flags)
{
    char *node_uri = NULL, *tmp_uri;
    int err = 0;
    int64_t size;
    size_t  len, i;
    URLContext *uc;
    struct concat_data  *data;
    struct concat_nodes *nodes;

    av_strstart(uri, "concat:", &uri);

    /* creating data */
    if (!(data = av_mallocz(sizeof(*data))))
        return AVERROR(ENOMEM);
    h->priv_data = data;

    for (i = 0, len = 1; uri[i]; i++)
        if (uri[i] == *AV_CAT_SEPARATOR)
            /* integer overflow */
            if (++len == UINT_MAX / sizeof(*nodes)) {
                av_freep(&h->priv_data);
                return AVERROR(ENAMETOOLONG);
            }

    if (!(nodes = av_malloc(sizeof(*nodes) * len))) {
        av_freep(&h->priv_data);
        return AVERROR(ENOMEM);
    } else
        data->nodes = nodes;

    /* handle input */
    if (!*uri)
        err = AVERROR(ENOENT);
    for (i = 0; *uri; i++) {
        /* parsing uri */
        len = strcspn(uri, AV_CAT_SEPARATOR);
        if (!(tmp_uri = av_realloc(node_uri, len+1))) {
            err = AVERROR(ENOMEM);
            break;
        } else
            node_uri = tmp_uri;
        av_strlcpy(node_uri, uri, len+1);
        uri += len + strspn(uri+len, AV_CAT_SEPARATOR);

        /* creating URLContext */
        if ((err = url_open(&uc, node_uri, flags)) < 0)
            break;

        /* creating size */
        if ((size = url_filesize(uc)) < 0) {
            url_close(uc);
            err = AVERROR(ENOSYS);
            break;
        }

        /* assembling */
        nodes[i].uc   = uc;
        nodes[i].size = size;
    }
    av_free(node_uri);
    data->length = i;

    if (err < 0)
        concat_close(h);
    else if (!(nodes = av_realloc(nodes, data->length * sizeof(*nodes)))) {
        concat_close(h);
        err = AVERROR(ENOMEM);
    } else
        data->nodes = nodes;
    return err;
}