Hi,
I use libav API (libav-0.8.3) to receive RTSP stream from IP camera and show a
live view of the IP camera.
We use an embedded hardware with a h264 decoder. h264 frames retrieved by
av_read_frame() are send to the
decoder and we get live view. This works very well.
The code snippet shows my usecase running in a thread.
Now I want use the same setup to show video playback of a MP4 file.
I am not sure how to setup the playback. Is it possible to use av_read_play() ?
How is timing handled in this case ?ยด
For first tests was able to playback h264 files, but I had to put a sleep in
the loop
To see the decoded content.
//////////////////////// RTSP viewer //////////////////////////////
AVFormatContext* context;
int video_stream_index = 0;
static AVPacket packet;
av_register_all();
avcodec_register_all();
avformat_network_init();
context = avformat_alloc_context();
if(avformat_open_input(&context, ipcam_url, NULL, NULL) != 0)
{
LOG(LOG_ERR, "Can not open RTSP stream!");
avformat_close_input(&context);
return(NULL);
}
if(avformat_find_stream_info(context,NULL) < 0)
{
LOG(LOG_ERR, "Can not find RTSP stream!");
avformat_close_input(&context);
return(NULL);
}
/* Search video stream */
for(i=0; i<context->nb_streams; i++)
{
if(context->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
video_stream_index = i;
}
av_init_packet(&packet);
/* Play RTSP */
av_read_play(context);
/* Start reading packets from stream and write them to file */
semWait(&RTSP_start_semaphore, TIMEOUT_FOREVER);
while( RTSP_thread_exit == FALSE )
{
if( av_read_frame(context, &packet >= 0 )
{
if( packet.stream_index == video_stream_index ) /* Packet is video
? */
{
/* A new frame arrived, feed to decoder here */
}
}
}
av_read_pause(context);
avformat_close_input(&context);
}
????????????????????????????? Playback viewer ????????????????????????????????
AVFormatContext* context = NULL;
int video_stream_index = -1;
int i;
av_register_all();
avcodec_register_all();
context = avformat_alloc_context();
if(avformat_open_input(&context, video_playback_path_file, NULL, NULL) != 0)
{
LOG(LOG_ERR, "Can not open video playback file!");
return(NULL);
}
if(avformat_find_stream_info(context,NULL) < 0)
{
LOG(LOG_ERR, "Can not find file info!");
av_close_input_file(context);//avformat_close_input(&context);
return(NULL);
}
/* Search video stream */
for(i=0; i<context->nb_streams; i++)
{
if(context->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
video_stream_index = i;
}
if( video_stream_index == -1 )
{
LOG(LOG_ERR, "Could not find video content!");
av_close_input_file(context);
return(NULL);
}
av_init_packet(&packet);
/* Start reading packets from stream and write them to file */
semWait(&video_playback_start_semaphore, TIMEOUT_FOREVER);
/* Play */
av_read_play(context); // ?????
while( video_playback_thread_exit == FALSE )
{
if( av_read_frame(context, &packet) >= 0 )
{
if( packet.stream_index == video_stream_index ) /* Packet is video
? */
{
/* A new frame to decode */
}
// sleep 20 ms here for test
}
else
{
/* Reached end */
break;
}
}
av_read_pause(context); // ?????
av_close_input_file(context);
}
_______________________________________________
libav-tools mailing list
[email protected]
https://lists.libav.org/mailman/listinfo/libav-tools