原子Linux开发板拉流rtsp播放

书接上文,正点原子linux开发板使用ffmpeg api播放视频
现在可以从RTSP拉流了。

视频效果:B站播放拉流的效果
网盘链接
链接:https://pan.baidu.com/s/1ix5OoGJb877tryAETQRMgw
提取码:jc05

上一篇的代码存在内存泄漏的问题,因为在VideoConvert()函数申请了frame的结构,但是我知道使用哪个API能够释放内存。之前在解码时每次都会申请,现在播放码流前只申请一次。解码时之传入参数,不再申请frame,内存泄漏依旧有,大概一分钟增加1MB内存,后面再说吧。

现在存在的问题,如果码流比较大,就会花屏,所以演示视频是播放的时间,因为变化的区域比较小。 而且播放四五分钟后,也会出现部分花屏,这个得等以后了解更多再解决,现在只是跑通代码流程就行。

达到好效果也可以参考ffplay的代码,昨晚用ffplay播放rtsp很流畅,代码路径在ffmpeg源码的fftoos\ffplay.c,总过3700多行,我还没看懂。

在上一篇的基础上,实现代码如下,新建test_004_rtsp.c

/** Copyright (c) 2015 Ludmila Glinskih** Permission is hereby granted, free of charge, to any person obtaining a copy* of this software and associated documentation files (the "Software"), to deal* in the Software without restriction, including without limitation the rights* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell* copies of the Software, and to permit persons to whom the Software is* furnished to do so, subject to the following conditions:** The above copyright notice and this permission notice shall be included in* all copies or substantial portions of the Software.** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN* THE SOFTWARE.*//*** H264 pAVCodec test.*/
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <linux/fb.h>#include "libavutil/adler32.h"
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libavutil/imgutils.h"#include "libavfilter/avfilter.h"
#include "libavutil/avutil.h"
#include "libavutil/pixfmt.h"
#include "libavdevice/avdevice.h"
#include "libswscale/swscale.h"
#include "libswresample/swresample.h"typedef unsigned char uint8_t;int fbfd = 0;
static unsigned int *fbp = NULL;
struct fb_var_screeninfo vinfo;
struct fb_fix_screeninfo finfo;
int scrWid = 0;
int scrHeg = 0;int open_fb()
{unsigned int screen_size;/* 打开framebuffer设备 */if (0 > (fbfd = open("/dev/fb0", O_RDWR))){perror("open error");exit(EXIT_FAILURE);}/* 获取参数信息 */ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo);ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo);screen_size = finfo.line_length * vinfo.yres;scrWid = vinfo.xres;scrHeg = vinfo.yres;/* 将显示缓冲区映射到进程地址空间 */fbp = mmap(NULL, screen_size, PROT_WRITE, MAP_SHARED, fbfd, 0);if (MAP_FAILED == (void *)fbp){perror("mmap error");close(fbfd);exit(EXIT_FAILURE);}scrWid = vinfo.xres;scrHeg = vinfo.yres;printf("scrWid:%d scrHeg:%d\n", scrWid, scrHeg);
}void close_fb(void)
{// 解除映射并关闭framebuffer设备munmap(fbp, finfo.smem_len);close(fbfd);
}#define argb8888_to_rgba888(color) ({ \unsigned int temp = (color);      \((temp & 0xff0000UL) >> 16) |     \((temp & 0xff00UL) >> 0) |    \((temp & 0xffUL) << 16);      \
})/********************************************************************* 函数名称: lcd_draw_point* 功能描述: 打点* 输入参数: x, y, color* 返 回 值: 无********************************************************************/
static void lcd_draw_point(unsigned int x, unsigned int y, unsigned int color)
{unsigned int rgb565_color = argb8888_to_rgba888(color); // 得到RGB565颜色值/* 填充颜色 */fbp[y * scrWid + x] = color;
}void draw_point(int x, int y, uint8_t *color)
{lcd_draw_point(x, y, *(uint32_t *)color);
}void clr_scr(int w, int h)
{static int cnt = 0;printf("clr scr:%d\n", cnt);cnt++;char clor[4] = {0xff, 0xff, 0xff};for (int i = 0; i < h; i++)for (int j = 0; j < w; j++)draw_point(j, i, clor);
}int init_outframe_rgba(AVFrame **ppOutFrame, enum AVPixelFormat eOutFormat, // 输出视频格式int32_t nOutWidth,             // 输出视频宽度int32_t nOutHeight  )         // 输出视频高度)
{AVFrame *pOutFrame = NULL;// 创建输出视频帧对象以及分配相应的缓冲区uint8_t *data[4] = {NULL};int linesize[4] = {0};int res = av_image_alloc(data, linesize, nOutWidth, nOutHeight, eOutFormat, 1);if (res < 0){printf("<VideoConvert> [ERROR] fail to av_image_alloc(), res=%d\n", res);return -2;}pOutFrame = av_frame_alloc();pOutFrame->format = eOutFormat;pOutFrame->width = nOutWidth;pOutFrame->height = nOutHeight;pOutFrame->data[0] = data[0];pOutFrame->data[1] = data[1];pOutFrame->data[2] = data[2];pOutFrame->data[3] = data[3];pOutFrame->linesize[0] = linesize[0];pOutFrame->linesize[1] = linesize[1];pOutFrame->linesize[2] = linesize[2];pOutFrame->linesize[3] = linesize[3];(*ppOutFrame) = pOutFrame;return 0;
}int32_t VideoConvert(const AVFrame *pInFrame,       // 输入视频帧enum AVPixelFormat eOutFormat, // 输出视频格式int32_t nOutWidth,             // 输出视频宽度int32_t nOutHeight,            // 输出视频高度AVFrame **ppOutFrame)          // 输出视频帧
{struct SwsContext *pSwsCtx;AVFrame *pOutFrame = *ppOutFrame;// 创建格式转换器, 指定缩放算法,转换过程中不增加任何滤镜特效处理pSwsCtx = sws_getContext(pInFrame->width, pInFrame->height, (enum AVPixelFormat)pInFrame->format,nOutWidth, nOutHeight, eOutFormat,SWS_BICUBIC, NULL, NULL, NULL);if (pSwsCtx == NULL){printf("<VideoConvert> [ERROR] fail to sws_getContext()\n");return -1;}int res = 0;// 进行格式转换处理res = sws_scale(pSwsCtx,(const uint8_t *const *)(pInFrame->data),pInFrame->linesize,0,pOutFrame->height,pOutFrame->data,pOutFrame->linesize);if (res < 0){printf("<VideoConvert> [ERROR] fail to sws_scale(), res=%d\n", res);sws_freeContext(pSwsCtx);av_frame_free(&pOutFrame);return -3;}sws_freeContext(pSwsCtx); // 释放转换器return 0;
}static int video_decode_example(const char *url_rtsp)
{AVDictionary *pAVDictionary = 0;AVCodec *pAVCodec = NULL;AVCodecContext *pAVCodecContext = NULL;AVCodecParameters *origin_par = NULL;AVFrame *pAVFrame = NULL;AVFrame *pAVFrameRGB32 = NULL;AVStream *pAVStream = NULL;                        // ffmpeg流信息uint8_t *byte_buffer = NULL;AVPacket *pAVPacket = av_packet_alloc();AVFormatContext *pAVFormatContext = NULL;int number_of_written_bytes;int video_stream;int got_frame = 0;int byte_buffer_size;int i = 0;int result;int end_of_stream = 0;av_log(NULL, AV_LOG_ERROR, "enter video\n");pAVFormatContext = avformat_alloc_context(); // 用来申请AVFormatContext类型变量并初始化默认参数,申请的空间result = avformat_open_input(&pAVFormatContext, url_rtsp, NULL, NULL);if (result < 0){av_log(NULL, AV_LOG_ERROR, "Can't open file, res:%d\n", result);return result;}av_log(NULL, AV_LOG_ERROR, "open video file ok\n");result = avformat_find_stream_info(pAVFormatContext, NULL);if (result < 0){av_log(NULL, AV_LOG_ERROR, "Can't get stream info\n");return result;}av_log(NULL, AV_LOG_ERROR, "get stream info\n");video_stream = av_find_best_stream(pAVFormatContext, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0);if (video_stream < 0){av_log(NULL, AV_LOG_ERROR, "Can't find video stream in input file\n");return -1;}av_log(NULL, AV_LOG_ERROR, "get video stream info\n");pAVCodecContext = pAVFormatContext->streams[video_stream]->codec;pAVCodec = avcodec_find_decoder(pAVCodecContext->codec_id);if (!pAVCodec){av_log(NULL, AV_LOG_ERROR, "Can't find decoder\n");return -1;}av_log(NULL, AV_LOG_ERROR, "get video codec \n");// 设置缓存大小 1024000byteav_dict_set(&pAVDictionary, "buffer_size", "4096000", 0);// 设置超时时间 20sav_dict_set(&pAVDictionary, "stimeout", "20000000", 0);// 设置最大延时 3sav_dict_set(&pAVDictionary, "max_delay", "90000000", 0);// 设置打开方式 tcp/udpav_dict_set(&pAVDictionary, "rtsp_transport", "tcp", 0);result = avcodec_open2(pAVCodecContext, pAVCodec, &pAVDictionary);if (result < 0){av_log(pAVCodecContext, AV_LOG_ERROR, "Can't open decoder\n");return result;}av_log(NULL, AV_LOG_ERROR, "open video codec yes\n");pAVStream = pAVFormatContext->streams[video_stream];// 显示视频相关的参数信息(编码上下文)printf( "比特率:%d\n" , pAVCodecContext->bit_rate);printf( "宽高:%d-%d\n" , pAVCodecContext->width, pAVCodecContext->height);printf( "格式:%d\n" , pAVCodecContext->pix_fmt);  // AV_PIX_FMT_YUV420P 0printf( "帧率分母:%d\n" , pAVCodecContext->time_base.den);printf( "帧率分子:%d\n" , pAVCodecContext->time_base.num);printf( "帧率分母:%d\n" , pAVStream->avg_frame_rate.den);printf( "帧率分子:%d\n" , pAVStream->avg_frame_rate.num);printf( "总时长:%d s\n" , pAVStream->duration / 10000.0);printf( "总帧数:%d  \n" , pAVStream->nb_frames);pAVFrame = av_frame_alloc();if (!pAVFrame){av_log(NULL, AV_LOG_ERROR, "Can't allocate frame\n");return AVERROR(ENOMEM);}int out_w = pAVCodecContext->width;int out_h = pAVCodecContext->height;result = init_outframe_rgba(&pAVFrameRGB32, AV_PIX_FMT_BGRA, out_w, out_h);if (result < 0){av_log(pAVCodecContext, AV_LOG_ERROR, "init outfram_rgb failed\n");return result;}printf("#tb %d: %d/%d\n", video_stream, pAVFormatContext->streams[video_stream]->time_base.num,pAVFormatContext->streams[video_stream]->time_base.den);i = 0;av_init_packet(pAVPacket);while (1){result = av_read_frame(pAVFormatContext, pAVPacket);if (result >= 0){if (pAVPacket->stream_index == video_stream){// 步骤八:对读取的数据包进行解码result = avcodec_send_packet(pAVCodecContext, pAVPacket);if (result){printf("Failed to avcodec_send_packet(pAVCodecContext, pAVPacket) ,ret =%d", result);break;}while (!avcodec_receive_frame(pAVCodecContext, pAVFrame)){VideoConvert(pAVFrame, AV_PIX_FMT_BGRA, out_w, out_h, &pAVFrameRGB32);for (int h = 0; h < out_h; h++)for (int w = 0; w < out_w; w++){draw_point(w, h, (pAVFrameRGB32->data[0]) + ((h * out_w * 4 + w * 4)));}printf("draw one pic\n");}//av_frame_free(&pAVFrameRGB32);//av_packet_unref(&pAVPacket);// av_init_packet(&pAVPacket);}}}av_packet_unref(&pAVPacket);av_frame_free(&pAVFrame);avcodec_close(pAVCodecContext);avformat_close_input(&pAVFormatContext);avcodec_free_context(&pAVCodecContext);av_freep(&byte_buffer);return 0;
}int main(int argc, char **argv)
{if (argc < 2){av_log(NULL, AV_LOG_ERROR, "Incorrect input\n");return 1;}avcodec_register_all();printf("reigister net work\n");avformat_network_init();
#if CONFIG_AVDEVICEavdevice_register_all();
#endifprintf("reigister filter\n");avfilter_register_all();av_register_all();open_fb();clr_scr(scrWid, scrHeg);usleep(1000 * 1000 * 1);printf("video file :%s\n", argv[1]);if (video_decode_example(argv[1]) != 0)return 1;close_fb();return 0;
}

makefile文件如下:

FFMPEG=/home/shengy/alpha_build/
CC=arm-linux-gnueabihf-gccCFLAGS=-g -I$(FFMPEG)/includeLDFLAGS = -L$(FFMPEG)/lib/  -lswresample -lavformat -lavdevice -lavcodec -lavutil -lswscale -lavfilter -lm
TARGETS=test_004_rtspall:$(TARGETS)test_004_rtsp:test_004_rtsp.c$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) -std=c99  clean:rm -rf $(TARGETS)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/663912.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

第5课 使用FFmpeg将rtmp流再转推到rtmp服务器

本课对应源文件下载链接&#xff1a; https://download.csdn.net/download/XiBuQiuChong/88801992 通过前面的学习&#xff0c;我们已经可以正常播放网络rtmp流及本地mp4文件。这节课&#xff0c;我们将在前面的基础上实现一个常用的转推功能&#xff1a;读取rtmp流或mp4文件并…

架构学习(二):原生scrapy如何接入scrapy-redis,初步入局分布式

原生scrapy如何接入scrapy-redis&#xff0c;实现初步入局分布式 前言scrpy-redis分布式碎语 实现流程扩展结束 前言 scrpy-redis分布式 下图是scrpy-redis官方提供的架构图&#xff0c;按我理解&#xff0c;与原生scrapy的差异主要是把名单队列服务器化&#xff0c;也是存储…

Modbus协议学习第七篇之libmodbus库API介绍(modbus_write_bits等)

写在前面 在第六篇中我们介绍了基于libmodbus库的演示代码&#xff0c;那本篇博客就详细介绍一下第六篇的代码中使用的基于该库的API函数。另各位读者&#xff0c;Modbus相关知识受众较少&#xff0c;如果觉得我的专栏文章有帮助&#xff0c;请一定点个赞&#xff0c;在此跪谢&…

golang网络编程day3

golang网络编程day3 golang TCP聊天室golang UDP聊天室URL组成golang URL编程golang http编程http请求方法golanghttp框架 golang TCP聊天室 看懂例子把它理解好&#xff0c;知道实现的原理是什么&#xff0c;还可以自己实现出来。 忘了就看day2的讲解&#xff0c;里面的函数…

自然语言处理(NLP)—— Dialogflow ES聊天机器人

1. 背景介绍 这个实验室的目标是让你了解并使用Google的Dialogflow服务。Dialogflow是一个可以让你创建聊天机器人的服务&#xff0c;这个过程不需要或者只需要很少的编程技能。 1.1 账号的创建 为了完成这个实验室&#xff0c;你需要在以下网站上创建账号&#xff1a…

网络安全之SSL证书加密

简介 SSL证书是一种数字证书&#xff0c;遵守SSL协议&#xff0c;由受信任的数字证书颁发机构&#xff08;CA&#xff09;验证服务器身份后颁发。它具有服务器身份验证和数据传输加密的功能&#xff0c;能够确保数据在传输过程中的安全性和完整性。 具体来说&#xff0c;SSL证…

three.js CSS3DObject、CSS2DObject、CSS3DSprite、Sprite的作为标签的区别

CSS3DObject、CSS2DObject、CSS3DSprite、Sprite的作为标签的区别 是否面向相机场景缩放时&#xff0c;是否会跟随是否会被模型遮挡CSS2DObject是否否CSS3DObject否是否CSS3DSprite是是是Sprite是是是 CSS3DObject 和 CSS3DRenderer 搭配来渲染标签&#xff1b; CSS2DObject …

excel统计分析——卡方独立性检验(下)

参考资料&#xff1a;生物统计学 书接上文&#xff1a;https://blog.csdn.net/maizeman126/article/details/135893731 2、配对列联表 配对设计的数据&#xff0c;进行列联表检验时&#xff0c;采用McNemar-Bowker检验法进行检验。检验统计量为&#xff1a; 自由度dfk(k-1)/2…

部署私有知识库项目FastGPT

FastGPT 是一个基于 LLM 大语言模型的知识库问答系统,提供开箱即用的数据处理、模型调用等能力。同时可以通过 Flow 可视化进行工作流编排,从而实现复杂的问答场景。 项目文档: [快速了解 FastGpt | FastGptFastGPT 是一个基于 LLM 大语言模型的知识库问答系统,提供开箱即…

Jmeter直连mysql数据库教程

mysql数据库能够通过Navicat等远程连接工具连接 下载驱动并加入jmeter 1.mysql驱动下载地址&#xff1a;MySQL :: Download MySQL Connector/J (Archived Versions) 找到对应的驱动下载&#xff1a;如下图&#xff1a; 把驱动jar包加入jmeter 配置jmeter连接mysql数据库…

计算机专业一定要参加蓝桥杯吗

蓝桥杯对于计算机专业的学生来说是一个非常重要的赛事&#xff0c;主要有以下几个原因&#xff1a; 提升知识和技能水平&#xff1a;蓝桥杯涵盖了计算机专业的多个方向&#xff0c;包括软件、硬件、网络等。通过参加蓝桥杯&#xff0c;学生可以将所学的知识与实际应用相结合&a…

RK3588 Android 12 Framework修改记录(八)Settings Ethernet以太网 多网卡设置静态IP

https://blog.csdn.net/lishu_/article/details/114391764 如何优雅的实现Android Ethernet 以太网的开启与关闭 RK3588 Android 12 Framework修改记录&#xff08;八&#xff09;Settings Ethernet以太网 多网卡设置静态IP - 知乎 Android 7.1 以太网反射 EthernetManager 配置…

【C语言刷题系列】喝汽水问题

文章目录 一、文章简介 1.先买再换 1.1 代码逻辑&#xff1a; 1.2 完整代码 1.3 运行结果 1.4 根据方法一总结优化 2.边买边换 2.1 代码逻辑&#xff1a; 2.2 完整代码 2.3 运行结果 一、文章简介 本文所述专栏——C语言经典编程问题 C语言刷题_倔强的石头106的博客…

Linux操作系统——管道(二) 进程池

概念层面理解进程池 比如说我们一开始有一个父进程&#xff0c;分别创建5个管道&#xff0c;5个子进程&#xff0c;这5个子进程都向管道里面进行读取&#xff0c;而我们对应的父进程&#xff0c;因为我们前面谈过管道的4种情况里面&#xff0c;有一个种情况是&#xff0c;正常…

vcruntime140.dll怎么下载安装,vcruntime140.dll详细安装教程

vcruntime140.dll’&#xff0c;这个看似陌生而又技术性的文件名&#xff0c;实际上是计算机系统中一个至关重要的动态链接库文件。它是Visual C Redistributable运行时组件的一部分&#xff0c;由微软公司开发并维护&#xff0c;对于许多基于Windows操作系统的软件正常运行起着…

在 SQL Server 中使用 SQL 语句查询不同时间范围的数据

在 SQL Server 中&#xff0c;我们经常需要从数据库中检索特定时间范围内的数据。通过合理运用 SQL 语句&#xff0c;我们可以轻松地查询今天、昨天、近7天、近30天、一个月内、上一月、本年和去年的数据。下面是一些示例 SQL 查询&#xff0c;让我们逐一了解。 查询今天的数据…

PEI是聚醚酰亚胺(Polyetherimide)在粘接使用时使用UV胶水的优势有哪些?要注意哪些事项?

在使用UV胶水进行聚醚酰亚胺&#xff08;Polyetherimide&#xff0c;PEI&#xff09;粘接时&#xff0c;有一些优势和注意事项&#xff1a; 优势&#xff1a; 1.快速固化&#xff1a; UV胶水具有快速固化的特性&#xff0c;通过紫外线光源的照射&#xff0c;可以在几秒到几分钟…

Oracle PL/SQL Programming 第5章:Iterative Processing with Loops 读书笔记

总的目录和进度&#xff0c;请参见开始读 Oracle PL/SQL Programming 第6版 本章探讨 PL/SQL 的迭代控制结构&#xff08;也称为循环&#xff09;&#xff0c;它允许您重复执行相同的代码。 PL/SQL 提供了三种不同类型的循环结构&#xff1a; 简单或无限循环FOR 循环&#x…

MQTT环境搭建

本文介绍了MQTT测试环境的搭建过程和测试时使用的命令&#xff0c;对需要使用MQTT协议的相关开发人员有一定的帮助。 MQTT&#xff08;Message Queuing Telemetry Transport&#xff0c;消息队列遥测传输&#xff09;。 MQTT是 ISO 标准&#xff08;ISO/IEC PRF 20922&#xf…

国标GB/T 28181详解:GB/T28181状态信息报送流程

目 录 一、状态信息报送 二、状态信息报送的基本要求 三、命令流程 1、流程图 2、流程描述 四、协议接口 五、产品说明 六、状态信息报送的作用 七、参考 在国标GBT28181中&#xff0c;定义了状态信息报送的流程&#xff0c;当源设备(包括网关、SIP 设备、SIP 客…