lv_ffmpeg学习及播放rtsp

打印 上一主题 下一主题

主题 1015|帖子 1015|积分 3045

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
lvgl8.3有ffmpeg支持

FFmpeg support
  1. typedef struct {
  2.     lv_img_t img;
  3.     lv_timer_t * timer;
  4.     lv_img_dsc_t imgdsc;
  5.     bool auto_restart;
  6.     struct ffmpeg_context_s * ffmpeg_ctx;
  7. } lv_ffmpeg_player_t;
  8. typedef enum {
  9.     LV_FFMPEG_PLAYER_CMD_START,
  10.     LV_FFMPEG_PLAYER_CMD_STOP,
  11.     LV_FFMPEG_PLAYER_CMD_PAUSE,
  12.     LV_FFMPEG_PLAYER_CMD_RESUME,
  13.     _LV_FFMPEG_PLAYER_CMD_LAST
  14. } lv_ffmpeg_player_cmd_t;
  15. struct ffmpeg_context_s {
  16.     AVFormatContext * fmt_ctx;
  17.     AVCodecContext * video_dec_ctx;
  18.     AVStream * video_stream;
  19.     uint8_t * video_src_data[4];
  20.     uint8_t * video_dst_data[4];
  21.     struct SwsContext * sws_ctx;
  22.     AVFrame * frame;
  23.     AVPacket pkt;
  24.     int video_stream_idx;
  25.     int video_src_linesize[4];
  26.     int video_dst_linesize[4];
  27.     enum AVPixelFormat video_dst_pix_fmt;
  28.     bool has_alpha;
  29. };
复制代码
1. lv_ffmpeg_player_create 调用构造函数创建播放器

  1. //构造函数
  2. lv_ffmpeg_player_constructor
  3. {
  4. // 主要创建用于刷新图像的定时器
  5. player->timer = lv_timer_create(lv_ffmpeg_player_frame_update_cb,
  6.                                     FRAME_DEF_REFR_PERIOD, obj);
  7. }
复制代码
2.lv_ffmpeg_player_set_src(player, "/userdata/lvgl_app/birds.mp4");

2.1 打开给定视频文件
  1.     //打开文件, 寻找视频流及对应解码器 设置最终像素格式等参数
  2.     player->ffmpeg_ctx = ffmpeg_open_file(path);
  3.     //根据获得的视频流信息申请图像空间及ffmpeg需要的frame pkt
  4.     if(ffmpeg_image_allocate(player->ffmpeg_ctx) < 0)
  5.    
  6.     //将ffmpeg解码数据与lvgl图片刷新区域相关联
  7.     player->imgdsc.data = ffmpeg_get_img_data(player->ffmpeg_ctx);
  8.     lv_img_set_src(&player->img.obj, &(player->imgdsc));
复制代码
3. lv_ffmpeg_player_set_cmd(player, LV_FFMPEG_PLAYER_CMD_START);开始播放

  1.             //重新启动图像刷新定时器
  2.             lv_timer_resume(timer);
  3.             
  4. 构造函数中创建
  5.     // 主要创建用于刷新图像的定时器
  6.     player->timer = lv_timer_create(lv_ffmpeg_player_frame_update_cb,
  7.                                     FRAME_DEF_REFR_PERIOD, obj);
复制代码
4.简朴修改,实现播放rtsp流

  1.     lv_obj_t * player = lv_ffmpeg_player_create(lv_scr_act());
  2.     //lv_ffmpeg_player_set_src(player, "/userdata/lvgl_app/birds.mp4");
  3.     lv_ffmpeg_player_set_src(player, "rtsp://admin:p@ssw0rd@192.168.51.210/h264/ch33/main/av_stream");
  4.     //lv_ffmpeg_player_set_auto_restart(player, true);
  5.     lv_ffmpeg_player_set_cmd(player, LV_FFMPEG_PLAYER_CMD_START);
  6.     lv_obj_center(player);
复制代码
4.1 关闭全部暂停及seek操作

  1.             av_seek_frame(player->ffmpeg_ctx->fmt_ctx,
  2.                           0, 0, AVSEEK_FLAG_BACKWARD);
  3.                           
  4.                            lv_timer_pause(player->timer);
复制代码
4.1 网络初始化

  1. avformat_network_init();
复制代码
4.2 设置帧率

  1. lv_res_t lv_ffmpeg_player_set_src(lv_obj_t * obj, const char * path)
  2. {
  3.    if(period > 0) {
  4.         LV_LOG_INFO("frame refresh period = %d ms, rate = %d fps",
  5.                     period, 1000 / period);
  6.         lv_timer_set_period(player->timer, period);
  7.     }
  8.     else {
  9.         LV_LOG_WARN("unable to get frame refresh period");
  10. #if rtsp
  11.         lv_timer_set_period(player->timer, (1/25)*1000);
  12. #endif
  13.     }
  14.     }
复制代码
4.3 设置参数

  1. struct ffmpeg_context_s * ffmpeg_open_file(const char * path){
  2.     //3.设置打开媒体文件的相关参数
  3.     AVDictionary *options = NULL;
  4.     av_dict_set(&options, "buffer_size", "6M", 0); // 设置 buffer_size 为 2MB
  5.     //以tcp方式打开,如果以udp方式打开将tcp替换为udp
  6.     av_dict_set(&options, "rtsp_transport", "tcp", 0);
  7.     //设置超时断开连接时间,单位微秒,3000000表示3秒
  8.     av_dict_set(&options, "stimeout", "1000000", 0);
  9.     //设置最大时延,单位微秒,1000000表示1秒
  10.     av_dict_set(&options, "max_delay", "1000000", 0);
  11.     //自动开启线程数
  12.     av_dict_set(&options, "threads", "auto", 0);
  13.     // 设置分析持续时间
  14.     //av_dict_set(&options, "analyzeduration", "1000000", 0);
  15.     // 设置探测大小
  16.     av_dict_set(&options, "probesize", "5000000", 0);
  17.     if(avformat_open_input(&(ffmpeg_ctx->fmt_ctx), path, NULL, &options) < 0) {
  18.         LV_LOG_ERROR("Could not open source file %s", path);
  19.         goto failed;
  20.     }
  21.         //修改解码帧颜色格式
  22.         ffmpeg_ctx->video_dst_pix_fmt = AV_PIX_FMT_RGBA ;
  23. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

天津储鑫盛钢材现货供应商

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表