I'm thinking about how to design a loop that reads frames from internet, feed to ffmpeg decoder and then receive from ffmpeg to send to a render.
Take this pseudocode loop in consideration:
while true {
auto packet = receive_packet_from_network();
avcodec_send_packet(packet);
auto frame = alloc_empty_frame();
int r = avcodec_receive_frame(&frame);
if (r==0) {
send_to_render(frame);
}
}
do avcodec_send_packet or avcodec_receive_frame block or ffmpeg has an internal thread? I'm concerned about this loop because it waits for packets from network, so it has some delay. I'd like to do something like this instead:
//thread 1
while true {
auto packet = receive_packet_from_network();
avcodec_send_packet(packet);
}
//thread 2
while true {
auto frame = alloc_empty_frame();
int r = avcodec_receive_frame(&frame);
if (r==0) {
send_to_render(frame);
}
}
however, now, if avcodec_receive_frame does not block, then this loop would run too fast, millions of times per second.
So, how should I design the send/receive of packets in ffmpeg in the most efficient way? I don't want to spend cpu cycles like in the thread2 loop.