Audio and video project—Audio and video player analysis based on FFmpeg and SDL (11)

introduce

In this series, I plan to spend a lot of time explaining my gitee project audio and video player. In this project, you can learn audio and video decapsulation, decoding, and SDL rendering related knowledge. If you are interested in the source code, please check out the audio and video player based on FFmpeg and SDL

If you don’t understand this article, you can refer to my previous article Audio and Video Project— Audio and Video Player Analysis Based on FFmpeg and SDL (10)

parse

Our article explains AVFrameQueue, first look at the .h file code

#ifndef AVFRAMEQUEUE_H_
#define AVFRAMEQUEUE_H_

#include"queue.h"

#ifdef __cplusplus

extern "C" {
#include"libavutil/avutil.h"
#include"libavformat/avformat.h"
#include"libavcodec/avcodec.h"
}

#endif

class AVFrameQueue{
public:
    AVFrameQueue();
    ~AVFrameQueue();
    void Abort();
    int Push(AVFrame* val);
    AVFrame* Pop(const int timeout);
    AVFrame* Front();
    int Size();
private:
    void release();
    Queue<AVFrame*> queue_t;
};

#endif

On the whole, there is no difference between the functions of AVPacketQueue. They are all Abort, Push, Pop, Front, Size, and release. The only difference is that the data stored is different. One stores the packet (Packet) and the other stores the frame (Frame). .

Let’s take a look at the implementation of the function

Abort:
void AVFrameQueue::Abort(){
    release();
    queue_t.Abort();
}

This function is responsible for interrupting the program. It first calls the release function to release the data in the queue, and then calls the Abort method of the Queue parent class to terminate the program.

Push:
int AVFrameQueue::Push(AVFrame* val){
    return queue_t.Push(val);
}

This function is responsible for adding data to the queue. Directly call the Push method of the Queue parent class, which is easy to understand.

Pop:
AVFrame* AVFrameQueue::Pop(const int timeout){
    AVFrame* av_frame = nullptr;
    int ret = queue_t.Pop(av_frame, timeout);
    if(ret < 0){
        perror("AVFrameQueue:: Pop failed");
    }
    return av_frame;
}

This function is responsible for popping data from the queue. Since the method of this Queue parent class needs to assign the pop-up data to parameters, we need to add an AVFrame variable to the function header to pass it to the function.

Front:
AVFrame* AVFrameQueue::Front(){
    AVFrame* av_frame = nullptr;
    int ret = queue_t.Front(av_frame);
    if(ret < 0){
        perror("AVFrameQueue:: Front failed");
    }
    return av_frame;
}

This function is responsible for returning the first data of the queue. This is also easy to understand. Directly call the Front function of the Queue parent class.

Size:
int AVFrameQueue::Size(){
    return queue_t.Size();
}

This function is responsible for returning the length of the queue. Just call the Queue's Size method directly.

release:
void AVFrameQueue::release(){
    while(true){
        AVFrame* av_frame = nullptr;
        int ret = queue_t.Pop(av_frame, 1);
        if(ret < 0){
            break;
        }else {
            av_frame_free(&av_frame);
        }
    }
}

This function is responsible for releasing all elements of the queue. The function has an infinite loop of while, and then continuously pops the data in the queue until there is no more data, then breaks out of the loop.

Okay, Queue, AVPacketQueue, and AVFrameQueue are all finished. In the next article, we will talk about the mechanism of audio and video synchronization and how to implement it.

If you want to know what happened next, please listen to the breakdown next time.

Guess you like

Origin blog.csdn.net/weixin_60701731/article/details/134474591