lv_ffmpeg学习及播放rtsp

lvgl8.3有ffmpeg支持

FFmpeg support

c 复制代码
typedef struct {
    lv_img_t img;
    lv_timer_t * timer;
    lv_img_dsc_t imgdsc;
    bool auto_restart;
    struct ffmpeg_context_s * ffmpeg_ctx;
} lv_ffmpeg_player_t;

typedef enum {
    LV_FFMPEG_PLAYER_CMD_START,
    LV_FFMPEG_PLAYER_CMD_STOP,
    LV_FFMPEG_PLAYER_CMD_PAUSE,
    LV_FFMPEG_PLAYER_CMD_RESUME,
    _LV_FFMPEG_PLAYER_CMD_LAST
} lv_ffmpeg_player_cmd_t;

struct ffmpeg_context_s {
    AVFormatContext * fmt_ctx;
    AVCodecContext * video_dec_ctx;
    AVStream * video_stream;
    uint8_t * video_src_data[4];
    uint8_t * video_dst_data[4];
    struct SwsContext * sws_ctx;
    AVFrame * frame;
    AVPacket pkt;
    int video_stream_idx;
    int video_src_linesize[4];
    int video_dst_linesize[4];
    enum AVPixelFormat video_dst_pix_fmt;
    bool has_alpha;
};

1. lv_ffmpeg_player_create 调用构造函数创建播放器

c 复制代码
//构造函数
lv_ffmpeg_player_constructor
{
// 主要创建用于刷新图像的定时器
player->timer = lv_timer_create(lv_ffmpeg_player_frame_update_cb,
                                    FRAME_DEF_REFR_PERIOD, obj);
}

2.lv_ffmpeg_player_set_src(player, "/userdata/lvgl_app/birds.mp4");

2.1 打开给定视频文件

c 复制代码
    //打开文件, 寻找视频流及对应解码器 设置最终像素格式等参数
    player->ffmpeg_ctx = ffmpeg_open_file(path);

    //根据获得的视频流信息申请图像空间及ffmpeg需要的frame pkt
    if(ffmpeg_image_allocate(player->ffmpeg_ctx) < 0) 
    
    //将ffmpeg解码数据与lvgl图片刷新区域相关联
    player->imgdsc.data = ffmpeg_get_img_data(player->ffmpeg_ctx);
    lv_img_set_src(&player->img.obj, &(player->imgdsc));

3. lv_ffmpeg_player_set_cmd(player, LV_FFMPEG_PLAYER_CMD_START);开始播放

c 复制代码
            //重新启动图像刷新定时器
            lv_timer_resume(timer);
            
构造函数中创建
    // 主要创建用于刷新图像的定时器
    player->timer = lv_timer_create(lv_ffmpeg_player_frame_update_cb,
                                    FRAME_DEF_REFR_PERIOD, obj);

4.简单修改,实现播放rtsp流

c 复制代码
    lv_obj_t * player = lv_ffmpeg_player_create(lv_scr_act());
    //lv_ffmpeg_player_set_src(player, "/userdata/lvgl_app/birds.mp4");
    lv_ffmpeg_player_set_src(player, "rtsp://admin:p@ssw0rd@192.168.51.210/h264/ch33/main/av_stream");
    //lv_ffmpeg_player_set_auto_restart(player, true);
    lv_ffmpeg_player_set_cmd(player, LV_FFMPEG_PLAYER_CMD_START);
    lv_obj_center(player);

4.1 关闭所有暂停及seek操作

cpp 复制代码
            av_seek_frame(player->ffmpeg_ctx->fmt_ctx,
                          0, 0, AVSEEK_FLAG_BACKWARD);
                          
                           lv_timer_pause(player->timer);

4.1 网络初始化

cpp 复制代码
avformat_network_init();

4.2 设置帧率

cpp 复制代码
lv_res_t lv_ffmpeg_player_set_src(lv_obj_t * obj, const char * path)
{
   if(period > 0) {
        LV_LOG_INFO("frame refresh period = %d ms, rate = %d fps",
                    period, 1000 / period);
        lv_timer_set_period(player->timer, period);
    }
    else {
        LV_LOG_WARN("unable to get frame refresh period");
#if rtsp
        lv_timer_set_period(player->timer, (1/25)*1000);
#endif
    }
    }

4.3 设置参数

c 复制代码
struct ffmpeg_context_s * ffmpeg_open_file(const char * path){
    //3.设置打开媒体文件的相关参数
    AVDictionary *options = NULL;
    av_dict_set(&options, "buffer_size", "6M", 0); // 设置 buffer_size 为 2MB
    //以tcp方式打开,如果以udp方式打开将tcp替换为udp
    av_dict_set(&options, "rtsp_transport", "tcp", 0);
    //设置超时断开连接时间,单位微秒,3000000表示3秒
    av_dict_set(&options, "stimeout", "1000000", 0);
    //设置最大时延,单位微秒,1000000表示1秒
    av_dict_set(&options, "max_delay", "1000000", 0);
    //自动开启线程数
    av_dict_set(&options, "threads", "auto", 0);
    // 设置分析持续时间
    //av_dict_set(&options, "analyzeduration", "1000000", 0);
    // 设置探测大小
    av_dict_set(&options, "probesize", "5000000", 0);

    if(avformat_open_input(&(ffmpeg_ctx->fmt_ctx), path, NULL, &options) < 0) {
        LV_LOG_ERROR("Could not open source file %s", path);
        goto failed;
    }

        //修改解码帧颜色格式
        ffmpeg_ctx->video_dst_pix_fmt = AV_PIX_FMT_RGBA ;
}
相关推荐
李日华大战鸡红4 小时前
FOC状态空间方程模型推导(学习记录)
python·学习·线性代数
kkkkkkkkkk_Z6 小时前
新手自学嵌入式 | 学习日记:异常与中断基础、i.MX6ULL裸机开发环境搭建、GPIO模块与点灯原理
学习
m4Rk_7 小时前
【论文阅读】Agent 记忆机制(94):MemGen——在推理过程中动态生成并织入潜在记忆
论文阅读·人工智能·学习·开源·github
晓德7 小时前
0、LLM大模型安全学习系列(启)
学习·安全
Titan20248 小时前
MySQL索引学习笔记
笔记·学习·mysql
JWASX8 小时前
Java 转 go 学习 - kitex(5)
学习·golang
AOI小白新手上路8 小时前
VDMamba·AutoDL 复现笔记 · 学习笔记
笔记·学习
~kiss~8 小时前
LangGraph ~ 为图添加跨线程持久化
学习
xianrenli389 小时前
水肺潜水学习
学习
凌云若寒11 小时前
BarTender提示#807错误:无法在拥有其他许可证的Licensing Service上激活节点锁定的 Professional 版许可证 的解决办法
运维·服务器·前端·学习·软件需求