V4L2+FFMPEG+live555实现流媒体服务端

2024-03-15 11:20

本文主要是介绍V4L2+FFMPEG+live555实现流媒体服务端,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

源码下载地址:源码下载

<span style="font-size: 14px; font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">代码主要参考了如下一篇博客,当然还有其它很多代码,就不一一列举了。</span>

博客链接:http://blog.csdn.net/nieyongs/article/details/17919325

分析LIVE555库,设计了两个类:

1.V4L2FramedSource类,是FramedSource类的直接子类,主要提供如下功能:

    使用V4L2提供的MMAP内存映射,建立了V4L2FramedSource::fNumCameraBuffers个缓冲帧

    基于FFMPEG的libswscale模块实现缩放与转换图像格式和大小

2.StreamEncoder类,是FramedFilter类的直接子类,不过也算FramedSource类的孙子类吧!基于FFMPEG的libavcodec模块实现H264编码。

这是Live555库中非常常见的两个类,FrameFilter类是SOURCE到SINK途径中可以无限制插入的类,它能够非常出色的将流水作业模块化!

程序中图像数据传递流程为:

V4L2FramedSource →StreamEncoder→H264VideoStreamFramer

H264VideoRTPSink

程序调用流程是刚好相反的方向,当RTPSink类将图像数据打包完毕之后,它就会向H264VideoStreamFramer要数据,这样。。。。

H264VideoRTPSink是按照[RFC 3984]协议对NALU进行分片,不过分片功能则是交给H264or5Fragmenter类实现

//liveMedia/H264VideoRTPSink.cpp文件
// "liveMedia"
// Copyright (c) 1996-2014 Live Networks, Inc.  All rights reserved.
// RTP sink for H.264 video (RFC 3984)
// Implementation

具体分片方法可查看live/liveMedia/H264or5VideoRTPSink.cpp文件的H264or5Fragmenter::doGetNextFrame()函数。

H264VideoStreamFramer类实现了解析各个图像帧,并且一次只向H264or5Fragmenter对象传递一个NAL单元。

Tips:因此,在确保一次传递一个NALU的情况下,完全可以直接砍掉 解析NALU单元的模块,这个最大的鸡肋。不过

FFMPEG库的avcodec_encode_video2()返回的AVPacket确实是一帧已编码的H.264图像,还不能够这样霸气!

StreamEncoder和H264VideoStreamFramer是FramedFilter的子类

V4L2FramedSource是FramedSource的子类

H264VideoRTPSink是RTPSink的子类




/**********
This library is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the
Free Software Foundation; either version 2.1 of the License, or (at your
option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.)This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
more details.You should have received a copy of the GNU Lesser General Public License
along with this library; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
**********/
// Copyright (c) 1996-2014, Live Networks, Inc.  All rights reserved
// A test program that demonstrates how to stream - via unicast RTP
// - various kinds of file on demand, using a built-in RTSP server.
// main program#include <liveMedia.hh>
#include <BasicUsageEnvironment.hh>
#include "DD_H264VideoFileServerMediaSubsession.hh"UsageEnvironment* env;// To make the second and subsequent client for each stream reuse the same
// input stream as the first client (rather than playing the file from the
// start for each client), change the following "False" to "True":
Boolean reuseFirstSource = True;static void announceStream(RTSPServer* rtspServer, ServerMediaSession* sms,char const* streamName, char const* inputFileName); // fwdint main(int argc, char** argv) {// Begin by setting up our usage environment:TaskScheduler* scheduler = BasicTaskScheduler::createNew();env = BasicUsageEnvironment::createNew(*scheduler);UserAuthenticationDatabase* authDB = NULL;
#ifdef ACCESS_CONTROL// To implement client access control to the RTSP server, do the following:authDB = new UserAuthenticationDatabase;authDB->addUserRecord("username1", "password1"); // replace these with real strings// Repeat the above with each <username>, <password> that you wish to allow// access to the server.
#endif// Create the RTSP server:RTSPServer* rtspServer = RTSPServer::createNew(*env, 8554, authDB);if (rtspServer == NULL) {*env << "Failed to create RTSP server: " << env->getResultMsg() << "\n";exit(1);}char const* descriptionString= "Session streamed by \"testOnDemandRTSPServer\"";// Set up each of the possible streams that can be served by the// RTSP server.  Each such stream is implemented using a// "ServerMediaSession" object, plus one or more// "ServerMediaSubsession" objects for each audio/video substream.// A H.264 video elementary stream:{char const* streamName = "h264ESVideoTest";char const* inputFileName = "test.264";ServerMediaSession* sms= ServerMediaSession::createNew(*env, streamName, streamName,descriptionString);sms->addSubsession(DD_H264VideoFileServerMediaSubsession::createNew(*env, inputFileName, reuseFirstSource));rtspServer->addServerMediaSession(sms);announceStream(rtspServer, sms, streamName, inputFileName);}// Also, attempt to create a HTTP server for RTSP-over-HTTP tunneling.// Try first with the default HTTP port (80), and then with the alternative HTTP// port numbers (8000 and 8080).if (rtspServer->setUpTunnelingOverHTTP(80) || rtspServer->setUpTunnelingOverHTTP(8000) || rtspServer->setUpTunnelingOverHTTP(8080)) {*env << "\n(We use port " << rtspServer->httpServerPortNum() << " for optional RTSP-over-HTTP tunneling.)\n";} else {*env << "\n(RTSP-over-HTTP tunneling is not available.)\n";}env->taskScheduler().doEventLoop(); // does not returnreturn 0; // only to prevent compiler warning
}static void announceStream(RTSPServer* rtspServer, ServerMediaSession* sms,char const* streamName, char const* inputFileName) {char* url = rtspServer->rtspURL(sms);UsageEnvironment& env = rtspServer->envir();env << "\n\"" << streamName << "\" stream, from the file \""<< inputFileName << "\"\n";env << "Play this stream using the URL \"" << url << "\"\n";delete[] url;
}

/** V4L2.h**  Created on: 2013年12月17日*      Author: ny*/#ifndef V4L2_H_
#define V4L2_H_#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>#include <getopt.h>#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <malloc.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <sys/ioctl.h>#include <asm/types.h>#include <linux/videodev2.h>#ifndef _FRAMED_SOURCE_HH
#include "FramedSource.hh"
#endifextern "C"
{#include <libavcodec/avcodec.h>
//	#include <libavformat/avformat.h>
//	#include <libavfilter/avfilter.h>#include <libswscale/swscale.h>#include "libavutil/opt.h"#include <libavutil/imgutils.h>
}#define CLEAR(x) memset(&(x),0,sizeof(x))struct buffer
{void * start;unsigned int length;
};class V4L2FramedSource: public FramedSource
{
public:V4L2FramedSource(UsageEnvironment& env);virtual ~V4L2FramedSource();
//	bool initDev(const char * devName, int width, int height);//摄像头初始化bool startStream();//开始启动摄像头int getWidth();int getHeight();void camera_get_format(void);void camera_set_format(int width, int height);void output_get_format(void);void output_set_format(int width, int height);
//	bool setSize(int width, int height);b

这篇关于V4L2+FFMPEG+live555实现流媒体服务端的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

Java实现优雅日期处理的方案详解

《Java实现优雅日期处理的方案详解》在我们的日常工作中,需要经常处理各种格式,各种类似的的日期或者时间,下面我们就来看看如何使用java处理这样的日期问题吧,感兴趣的小伙伴可以跟随小编一起学习一下... 目录前言一、日期的坑1.1 日期格式化陷阱1.2 时区转换二、优雅方案的进阶之路2.1 线程安全重构2

Android实现两台手机屏幕共享和远程控制功能

《Android实现两台手机屏幕共享和远程控制功能》在远程协助、在线教学、技术支持等多种场景下,实时获得另一部移动设备的屏幕画面,并对其进行操作,具有极高的应用价值,本项目旨在实现两台Android手... 目录一、项目概述二、相关知识2.1 MediaProjection API2.2 Socket 网络

使用Python实现图像LBP特征提取的操作方法

《使用Python实现图像LBP特征提取的操作方法》LBP特征叫做局部二值模式,常用于纹理特征提取,并在纹理分类中具有较强的区分能力,本文给大家介绍了如何使用Python实现图像LBP特征提取的操作方... 目录一、LBP特征介绍二、LBP特征描述三、一些改进版本的LBP1.圆形LBP算子2.旋转不变的LB

Redis消息队列实现异步秒杀功能

《Redis消息队列实现异步秒杀功能》在高并发场景下,为了提高秒杀业务的性能,可将部分工作交给Redis处理,并通过异步方式执行,Redis提供了多种数据结构来实现消息队列,总结三种,本文详细介绍Re... 目录1 Redis消息队列1.1 List 结构1.2 Pub/Sub 模式1.3 Stream 结

C# Where 泛型约束的实现

《C#Where泛型约束的实现》本文主要介绍了C#Where泛型约束的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录使用的对象约束分类where T : structwhere T : classwhere T : ne

将Java程序打包成EXE文件的实现方式

《将Java程序打包成EXE文件的实现方式》:本文主要介绍将Java程序打包成EXE文件的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录如何将Java程序编程打包成EXE文件1.准备Java程序2.生成JAR包3.选择并安装打包工具4.配置Launch4

MySQL索引的优化之LIKE模糊查询功能实现

《MySQL索引的优化之LIKE模糊查询功能实现》:本文主要介绍MySQL索引的优化之LIKE模糊查询功能实现,本文通过示例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧... 目录一、前缀匹配优化二、后缀匹配优化三、中间匹配优化四、覆盖索引优化五、减少查询范围六、避免通配符开头七、使用外部搜索引擎八、分

Python实现特殊字符判断并去掉非字母和数字的特殊字符

《Python实现特殊字符判断并去掉非字母和数字的特殊字符》在Python中,可以通过多种方法来判断字符串中是否包含非字母、数字的特殊字符,并将这些特殊字符去掉,本文为大家整理了一些常用的,希望对大家... 目录1. 使用正则表达式判断字符串中是否包含特殊字符去掉字符串中的特殊字符2. 使用 str.isa

Spring Boot 集成 Quartz并使用Cron 表达式实现定时任务

《SpringBoot集成Quartz并使用Cron表达式实现定时任务》本篇文章介绍了如何在SpringBoot中集成Quartz进行定时任务调度,并通过Cron表达式控制任务... 目录前言1. 添加 Quartz 依赖2. 创建 Quartz 任务3. 配置 Quartz 任务调度4. 启动 Sprin

Android实现悬浮按钮功能

《Android实现悬浮按钮功能》在很多场景中,我们希望在应用或系统任意界面上都能看到一个小的“悬浮按钮”(FloatingButton),用来快速启动工具、展示未读信息或快捷操作,所以本文给大家介绍... 目录一、项目概述二、相关技术知识三、实现思路四、整合代码4.1 Java 代码(MainActivi