[Libav-user] RTMP output laggy
yy-zed
samhalligan15 at gmail.com
Mon May 6 22:03:50 CEST 2013
Hi guys,
I'm using av_write_frame() with the source destination being an RTMP
server(twitch.tv in this case). My code is broadcasting to twitch ok, but
frames stutter every couple of seconds, freeze for a second, and then start
again. If I save the output to a local flv file it plays back perfectly. I
suspected a multithreaded approach would solve this, having a thread for
incoming frames and another for outgoing, but the problem stayed the same
when I implemented it. Now I'm thinking it may be something to do with my
encoder settings. Here they are:
c->codec_id = codec_id;
c->bit_rate = 600000;
// Resolution must be a multiple of two.
c->width = pCodecCtx->width;
c->height = pCodecCtx->height;
c->time_base.den = STREAM_FRAME_RATE;
c->time_base.num = 1;
//emit one intra frame every twelve frames at most
c->gop_size = 40;
c->pix_fmt = STREAM_PIX_FMT;
if (codec_id == CODEC_ID_H264) {
av_opt_set(c->priv_data, "preset", "fast", 0);
av_opt_set(c->priv_data, "tune", "zerolatency", 0);
}
I've fiddled with these a lot, but nothing really makes a difference.(other
than change output quality)
This is the method i call to capture and write a frame to output:
int ret;
uint8_t *buffer = NULL;
static struct SwsContext *sws_ctx;
//Setup the codec context, and set its width and height to be equal to
the input video width and height
AVCodecContext *c = st->codec;
c->width = pCodecCtx->width;
c->height = pCodecCtx->height;
av_init_packet(&packet);
frameYUV = avcodec_alloc_frame();
//Determine how big the buffer will need to be to store the captured
frame
numBytes =
avpicture_get_size(STREAM_PIX_FMT,pCodecCtx->width,pCodecCtx->height);
//Allocate the needed buffer size
buffer = new uint8_t[numBytes];
sws_ctx =
sws_getContext(pCodecCtx->width,pCodecCtx->height,pCodecCtx->pix_fmt,
pCodecCtx->width,pCodecCtx->height,
STREAM_PIX_FMT,SWS_BICUBIC,NULL,NULL,NULL);
//Fill the output frame
avpicture_fill((AVPicture
*)frameYUV,buffer,STREAM_PIX_FMT,pCodecCtx->width,pCodecCtx->height);
//Read a video frame in
av_read_frame(pFormatCtx,&packet);
//Decode the contents of packet into pFrame
avcodec_decode_video2(pCodecCtx,pFrame,&frameFinished,&packet);
//Scale pFrame into frameYUV, and convert to PIXFMTYUV420P
sws_scale
(
sws_ctx,
(uint8_t const * const *)pFrame->data,
pFrame->linesize,
0,
pCodecCtx->height,
frameYUV->data,
frameYUV->linesize
);
AVPacket pkt = { 0 };
int got_packet;
av_init_packet(&pkt);
//encode the image
ret = avcodec_encode_video2(c, &pkt, frameYUV, &got_packet);
if (ret < 0){
debugLogStreamSave->debug("Error encoding video frame");
exit(1);
}
if (!ret && got_packet && pkt.size){
pkt.stream_index = st->index;
// Write the compressed frame to our output
ret = av_write_frame(oc, &pkt);
//Free up any memory we used
av_free_packet(&pkt);
av_free_packet(&packet);
avcodec_free_frame(&frameYUV);
//avcodec_free_frame(&pFrame);
delete[] buffer;
}
Any ideas would be appreciated!
Sam
--
View this message in context: http://libav-users.943685.n4.nabble.com/RTMP-output-laggy-tp4657525.html
Sent from the libav-users mailing list archive at Nabble.com.
More information about the Libav-user
mailing list