[FFmpeg + OpenGL + OpenSL ES]获取视频AVFrame 并且释放相关资源 - 2

从队列中的AVPacket解码出AVFrame的相关函数:

步骤一:AVPacket *avPacket = av_packet_alloc();

              queue->getAvpacket(avPacket);

              avcodec_send_packet(avCodecContext, avPacket);

步骤二:AVFrame *avFrame = av_frame_alloc();

              avcodec_receive_frame(avCodecContext, avFrame);


在        video.cpp        文件中的        void * playVideo(void *data)      方法中        while(video->playstatus != NULL && !video->playstatus->exit)       下编写   

 if (video->playstatus->seek) {
            av_usleep(1000 * 100);
        }
        if (video->queue->getQueueSize() == 0) {
            if (video->playstatus->load) {
                video->playstatus->load = true;
                video->wlCallJava->onCallLoad(CHILD_THREAD, true);
            }
            av_usleep(1000 * 100);
            continue;
        } else {
            if (video->playstatus->load) {
                video->playstatus->load = false;
                video->wlCallJava->onCallLoad(CHILD_THREAD, false);
            }
        }
        //声明avPacket的内存空间
        AVPacket *avPacket = av_packet_alloc();
        if (video->queue->getAvpacket(avPacket) != 0) {
            av_packet_free(&avPacket);
            av_free(avPacket);
            continue;
        }
        if (avcodec_send_packet(video->avCodecContext, avPacket) != 0) {
            av_packet_free(&avPacket);
            av_free(avPacket);
            continue;
        }
        AVFrame *avFrame = av_frame_alloc();
        if (avcodec_receive_frame(video->avCodecContext, avFrame) != 0) {
            av_frame_free(&avFrame);
            av_free(avFrame);
            av_packet_free(&avPacket);
            av_free(avPacket);
            continue;
        }
        LOGE("子线程解码一个avFrame成功");

        av_frame_free(&avFrame);
        av_free(avFrame);
        av_packet_free(&avPacket);
        av_free(avPacket);

*使用        av_usleep()        需要在        video.h        导入头文件

extern "C"
{
/*...*/
#include 
};

在        video.h        文件中声明        void release();       方法用来释放video的相关资源

主要释放的资源有:

释放队列
    delete(queue);

释放解码器上下文
    avcodec_close(avCodecContext);
    avcodec_free_context(&avCodecContext);

在        video.cpp        文件中实现该方法

void WlVideo::release() {
    if (queue != NULL) {
        delete (queue);
        queue = NULL;
    }
    if (avCodecContext != NULL) {
        avcodec_close(avCodecContext);
        avcodec_free_context(&avCodecContext);
        avCodecContext = NULL;
    }
    if (playstatus != NULL) {
        playstatus = NULL;
    }
    if (wlCallJava != NULL) {
        wlCallJava = NULL;
    }
}

在        FFmpeg.cpp        文件的        void WlFFmpeg::release()        方法下        调用释放video资源的release方法

if(video != NULL)
    {
        video->release();
        delete(video);
        video = NULL;
    }

*在使用        delete        关键字的时候需要注意被释放的实例的类中需要存在析构函数并且实现该析构函数

在        video.h       下声明析构函数并实现

你可能感兴趣的:(视频播放器,ffmpeg,android)