原子Linux开发板拉流rtsp播放

2024-02-02 15:04

本文主要是介绍原子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)

这篇关于原子Linux开发板拉流rtsp播放的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/671096

相关文章

linux生产者,消费者问题

pthread_cond_wait() :用于阻塞当前线程,等待别的线程使用pthread_cond_signal()或pthread_cond_broadcast来唤醒它。 pthread_cond_wait() 必须与pthread_mutex 配套使用。pthread_cond_wait()函数一进入wait状态就会自动release mutex。当其他线程通过pthread

Linux 安装、配置Tomcat 的HTTPS

Linux 安装 、配置Tomcat的HTTPS 安装Tomcat 这里选择的是 tomcat 10.X ,需要Java 11及更高版本 Binary Distributions ->Core->选择 tar.gz包 下载、上传到内网服务器 /opt 目录tar -xzf 解压将解压的根目录改名为 tomat-10 并移动到 /opt 下, 形成个人习惯的路径 /opt/tomcat-10

RedHat运维-Linux文本操作基础-AWK进阶

你不用整理,跟着敲一遍,有个印象,然后把它保存到本地,以后要用再去看,如果有了新东西,你自个再添加。这是我参考牛客上的shell编程专项题,只不过换成了问答的方式而已。不用背,就算是我自己亲自敲,我现在好多也记不住。 1. 输出nowcoder.txt文件第5行的内容 2. 输出nowcoder.txt文件第6行的内容 3. 输出nowcoder.txt文件第7行的内容 4. 输出nowcode

【Linux进阶】UNIX体系结构分解——操作系统,内核,shell

1.什么是操作系统? 从严格意义上说,可将操作系统定义为一种软件,它控制计算机硬件资源,提供程序运行环境。我们通常将这种软件称为内核(kerel),因为它相对较小,而且位于环境的核心。  从广义上说,操作系统包括了内核和一些其他软件,这些软件使得计算机能够发挥作用,并使计算机具有自己的特生。这里所说的其他软件包括系统实用程序(system utility)、应用程序、shell以及公用函数库等

Windows/macOS/Linux 安装 Redis 和 Redis Desktop Manager 可视化工具

本文所有安装都在macOS High Sierra 10.13.4进行,Windows安装相对容易些,Linux安装与macOS类似,文中会做区分讲解 1. Redis安装 1.下载Redis https://redis.io/download 把下载的源码更名为redis-4.0.9-source,我喜欢跟maven、Tomcat放在一起,就放到/Users/zhan/Documents

Linux系统稳定性的奥秘:探究其背后的机制与哲学

在计算机操作系统的世界里,Linux以其卓越的稳定性和可靠性著称,成为服务器、嵌入式系统乃至个人电脑用户的首选。那么,是什么造就了Linux如此之高的稳定性呢?本文将深入解析Linux系统稳定性的几个关键因素,揭示其背后的技术哲学与实践。 1. 开源协作的力量Linux是一个开源项目,意味着任何人都可以查看、修改和贡献其源代码。这种开放性吸引了全球成千上万的开发者参与到内核的维护与优化中,形成了

Linux 下的Vim命令宝贝

vim 命令详解(转自:https://www.cnblogs.com/usergaojie/p/4583796.html) vi: Visual Interface 可视化接口 vim: VI iMproved VI增强版 全屏编辑器,模式化编辑器 vim模式: 编辑模式(命令模式)输入模式末行模式 模式转换: 编辑-->输入: i: 在当前光标所在字符的前面,转为输入模式

Linux和Mac分卷压缩

使用 zip 命令压缩文件 使用 zip 命令压缩文件,并结合 split 命令来分卷: zip - largefile | split -b 500k 举例: zip - ./tomcat.dmg |split -b 500k 上述命令将文件 largefile 压缩成 zip 包并分卷成不超过 500k 的文件,分解后文件名默认是 x* ,后缀为 2 位a-z 字母,如 aa、ab。

Linux文本三剑客sed

sed和awk grep就是查找文本当中的内容,最强大的功能就是使用扩展正则表达式 sed sed是一种流编辑器,一次处理一行内容。 如果只是展示,会放在缓冲区(模式空间),展示结束后,会从模式空间把结果删除 一行行处理,处理完当前行,才会处理下一行。直到文件的末尾。 sed的命令格式和操作选项: sed -e '操作符 ' -e '操作符' 文件1 文件2 -e表示可以跟多个操作

Linux中拷贝 cp命令中拷贝所有的写法详解

This text from: http://www.jb51.net/article/101641.htm 一、预备  cp就是拷贝,最简单的使用方式就是: cp oldfile newfile 但这样只能拷贝文件,不能拷贝目录,所以通常用: cp -r old/ new/ 那就会把old目录整个拷贝到new目录下。注意,不是把old目录里面的文件拷贝到new目录,