Windows下利用live555实现H264实时流RTSP发送的方案
发布时间:2023-10-10 18:21:40
最近在做的项目要求利用RTSP协议转发处理完的H264视频数据给上一层客户端,在网上查了一下发现别人贴出来的代码基本都是Linux上面的Windows不能用,所以下面分享一个用live555进行RTSP的播放适用于Windows系统的,需要的朋友可以参考下
文如其名,最近在做的项目要求利用RTSP协议转发处理完的H264视频数据给上一层客户端,环境是Windows的VS2013,于是就各种百度谷歌找代码。结果在得到利用live555去做比较简单的结论的同时也悲情地发现,网上别人贴出来的代码基本都是Linux上面的。在修改了两份来适用于Windows无效后,又一次陷入了百度谷歌的无尽搜索中。Anyway,最后终于解决了,所以贴出代码跟大家分享下,希望能给和我需求相似的童鞋一点启发,也希望有高手指正其中的问题。
用live555进行RTSP的播放基本上是通过修改其给出来的播放本地文件的DEMO来实现的。但由于其DEMO封装的比较深,所以要直接修改他的fread处的代码变成内存拷贝来实现实时传输会显得比较别扭。本文参考了网上的一些代码,自定义了一个继承自H264VideoFileServerMediaSubsession的类来来进行处理,同时定义了一个继承自FramedSource的类来做内存的拷贝操作,该类亦是区别于读本地文件和实时流之紧要处。
代码如下,如果觉得需要或者懒得自己搭建live555的环境亦可以在文中最后的链接中下载该工程(环境为VS2013),如果你的VS版本合适即可直接运行。
主文件(程序入口)
#include "H264LiveVideoServerMediaSubssion.hh"
#include "H264FramedLiveSource.hh"
#include "liveMedia.hh"
#include "BasicUsageEnvironment.hh"
#define BUFSIZE 1024*200
static void announceStream(RTSPServer* rtspServer, ServerMediaSession* sms,char const* streamName)//显示RTSP连接信息
{
char* url = rtspServer->rtspURL(sms);
UsageEnvironment& env = rtspServer->envir();
env <<streamName<< "\n";
env << "Play this stream using the URL \"" << url << "\"\n";
delete[] url;
}
int main(int argc, char** argv)
{
//设置环境
UsageEnvironment* env;
Boolean reuseFirstSource = False;//如果为“true”则其他接入的客户端跟第一个客户端看到一样的视频流,否则其他客户端接入的时候将重新播放
TaskScheduler* scheduler = BasicTaskScheduler::createNew();
env = BasicUsageEnvironment::createNew(*scheduler);
//创建RTSP服务器
UserAuthenticationDatabase* authDB = NULL;
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\"";
//模拟实时流发送相关变量
int datasize;//数据区长度
unsigned char* databuf;//数据区指针
databuf = (unsigned char*)malloc(1024*1024);
bool dosent;//rtsp发送标志位,为true则发送,否则退出
//从文件中拷贝1M数据到内存中作为实时网络传输内存模拟,如果实时网络传输应该是双线程结构,记得在这里加上线程锁
//此外实时传输的数据拷贝应该是发生在H264FramedLiveSource文件中,所以这里只是自上往下的传指针过去给它
FILE *pf;
fopen_s(&pf, "test.264", "rb");
fread(databuf, 1, BUFSIZE, pf);
datasize = BUFSIZE;
dosent = true;
fclose(pf);
//上面的部分除了模拟网络传输的部分外其他的基本跟live555提供的demo一样,而下面则修改为网络传输的形式,为此重写addSubsession的第一个参数相关文件
char const* streamName = "h264ESVideoTest";
ServerMediaSession* sms = ServerMediaSession::createNew(*env, streamName, streamName,descriptionString);
sms->addSubsession(H264LiveVideoServerMediaSubssion::createNew(*env, reuseFirstSource, &datasize, databuf,&dosent));//修改为自己实现的H264LiveVideoServerMediaSubssion
rtspServer->addServerMediaSession(sms);
announceStream(rtspServer, sms, streamName);//提示用户输入连接信息
env->taskScheduler().doEventLoop(); //循环等待连接
free(databuf);//释放掉内存
return 0;
}
自定义H264VideoFileServerMediaSubsession类
H264VideoFileServerMediaSubsession.hh
#ifndef _H264_LIVE_VIDEO_SERVER_MEDIA_SUBSESSION_HH
#define _H264_LIVE_VIDEO_SERVER_MEDIA_SUBSESSION_HH
#include "H264VideoFileServerMediaSubsession.hh"
class H264LiveVideoServerMediaSubssion : public H264VideoFileServerMediaSubsession {
public:
static H264LiveVideoServerMediaSubssion* createNew(UsageEnvironment& env, Boolean reuseFirstSource, int *datasize, unsigned char* databuf, bool *dosent);
protected: // we're a virtual base class
H264LiveVideoServerMediaSubssion(UsageEnvironment& env, Boolean reuseFirstSource, int *datasize, unsigned char* databuf, bool *dosent);
~H264LiveVideoServerMediaSubssion();
protected: // redefined virtual functions
FramedSource* createNewStreamSource(unsigned clientSessionId,unsigned& estBitrate);
public:
char fFileName[100];
int *Server_datasize;//数据区大小指针
unsigned char* Server_databuf;//数据区指针
bool *Server_dosent;//发送标示
};
#endifH264VideoFileServerMediaSubsession.cpp
#include "H264LiveVideoServerMediaSubssion.hh"
#include "H264FramedLiveSource.hh"
#include "H264VideoStreamFramer.hh"
H264LiveVideoServerMediaSubssion* H264LiveVideoServerMediaSubssion::createNew(UsageEnvironment& env, Boolean reuseFirstSource, int *datasize, unsigned char* databuf, bool *dosent)
{
return new H264LiveVideoServerMediaSubssion(env, reuseFirstSource, datasize, databuf, dosent);
}
H264LiveVideoServerMediaSubssion::H264LiveVideoServerMediaSubssion(UsageEnvironment& env, Boolean reuseFirstSource, int *datasize, unsigned char* databuf, bool *dosent)
: H264VideoFileServerMediaSubsession(env, fFileName, reuseFirstSource)//H264VideoFileServerMediaSubsession不是我们需要修改的文件,
//但是我们又要用它来初始化我们的函数,
//所以给个空数组进去即可
{
Server_datasize = datasize;//数据区大小指针
Server_databuf = databuf;//数据区指针
Server_dosent = dosent;//发送标示
}
H264LiveVideoServerMediaSubssion::~H264LiveVideoServerMediaSubssion()
{
}
FramedSource* H264LiveVideoServerMediaSubssion::createNewStreamSource(unsigned clientSessionId, unsigned& estBitrate)
{
/* Remain to do : assign estBitrate */
estBitrate = 1000; // kbps, estimate
//创建视频源
H264FramedLiveSource* liveSource = H264FramedLiveSource::createNew(envir(), Server_datasize, Server_databuf, Server_dosent);
if (liveSource == NULL)
{
return NULL;
}
// Create a framer for the Video Elementary Stream:
return H264VideoStreamFramer::createNew(envir(), liveSource);
}
自定义H264FramedLiveSource类
H264FramedLiveSource.hh
#ifndef _H264FRAMEDLIVESOURCE_HH
#define _H264FRAMEDLIVESOURCE_HH
#include
class H264FramedLiveSource : public FramedSource
{
public:
static H264FramedLiveSource* createNew(UsageEnvironment& env, int *datasize, unsigned char* databuf, bool *dosent, unsigned preferredFrameSize = 0, unsigned playTimePerFrame = 0);
protected:
H264FramedLiveSource(UsageEnvironment& env, int *datasize, unsigned char* databuf, bool *dosent, unsigned preferredFrameSize, unsigned playTimePerFrame);
~H264FramedLiveSource();
private:
virtual void doGetNextFrame();
int TransportData(unsigned char* to, unsigned maxSize);
protected:
int *Framed_datasize;//数据区大小指针
unsigned char *Framed_databuf;//数据区指针
bool *Framed_dosent;//发送标示
int readbufsize;//记录已读取数据区大小
int bufsizel;//记录数据区大小
};
#endifH264FramedLiveSource.cpp
#include "H264FramedLiveSource.hh"
H264FramedLiveSource::H264FramedLiveSource(UsageEnvironment& env, int *datasize, unsigned char* databuf, bool *dosent, unsigned preferredFrameSize, unsigned playTimePerFrame)
: FramedSource(env)
{
Framed_datasize = datasize;//数据区大小指针
Framed_databuf = databuf;//数据区指针
Framed_dosent = dosent;//发送标示
}
H264FramedLiveSource* H264FramedLiveSource::createNew(UsageEnvironment& env, int *datasize, unsigned char* databuf, bool *dosent, unsigned preferredFrameSize, unsigned playTimePerFrame)
{
H264FramedLiveSource* newSource = new H264FramedLiveSource(env, datasize, databuf, dosent, preferredFrameSize, playTimePerFrame);
return newSource;
}
H264FramedLiveSource::~H264FramedLiveSource()
{
}
void H264FramedLiveSource::doGetNextFrame()
{
if (*Framed_dosent == true)
{
*Framed_dosent = false;
bufsizel = *Framed_datasize;
readbufsize = 0;
fFrameSize = fMaxSize;
memcpy(fTo, Framed_databuf + readbufsize, fFrameSize);
readbufsize += fFrameSize;
}
else
{
if (bufsizel - readbufsize>fMaxSize)
{
fFrameSize = fMaxSize;
memcpy(fTo, Framed_databuf + readbufsize, fFrameSize);
readbufsize += fFrameSize;
}
else
{
memcpy(fTo, Framed_databuf + readbufsize, bufsizel - readbufsize);
*Framed_dosent = true;
}
}
nextTask() = envir().taskScheduler().scheduleDelayedTask(0,(TaskFunc*)FramedSource::afterGetting, this);//表示延迟0秒后再执行 afterGetting 函数
return;
}


猜你喜欢
- 各人好, 咱们在 操纵WPS 笔墨行 一样平常办公时, 常常会写 大批 笔墨段落,若 咱们想 疾速找到 此中一段,让想找的落 愈加 疾速找到
- 由于很多网页使用了防止复制或盗链的技术,使得用户经常会遇到无法复制需要的网页内容的情况。尽管可以通过查看源文件或其他方法复制网页中的文字,但
- Excel表格是大家都会用到的表格文件,平时在办公的时候经常会用来计数和统计,Excel表格的编辑功能齐全,满足基本的办公需求。很多时候都会
- win10安装没多久,暂时还不清楚win10的详细的使用情况,比如win10如何查看手机数据使用量,下面一起来看一下win10统计流量查询功
- 开机总是提示press ctrl+alt+del to restart,这要如何解决?每次开机都会遇到这一问题,非常烦人,出现press c
- 印象笔记有个非常好用的提醒设置功能,可以帮助用户记录重要的时间和重要的事情,很多用户不知道在哪里设置,今天小编就给大家带来了详细的设置教程。
- 要说当下最实用的办公软件,相信不少网友都会投“Excel”一票。不过,近来一些网友表示,他们不清楚Excel只读权限该怎么进行设置!针对这一
- 用户可以添加自己的段落样式、给现有样式重新命名和删除不想保留的样式。对样式所做的任何更改仅会影响当前文稿,不影响使用 Pages 文稿创建的
- dll已加载但找不到入口点DLLRegisterServer怎么办?不少人在使用注册表时,经常遇到dll已加载但找不到入口点DLLRegis
- excel表格打开后是灰色不显示内容该怎么办呢?下面小编来告诉大家该怎么办。01、首先,我们打开我们电脑上面的excel,,我们可以看到此时
- 文章介绍了excel中vba日期函数和vba时间函数的应用以及相关的函数列表。第一,vba日期和时间函数的基本用法Excel中vba日期函数
- 使用Win11游戏人多就卡怎么办?最近有用户反映这个问题,不知道怎么解决,很多玩家喜欢使用电脑玩游戏,但是最近有Win11系统的用户反映每次
- 自从win10发布之后,很多用户都升级了win10,但说实话win10自带的头像是好难看啊,那么win10怎样为用户添加个性化的头像?下面就
- 在制作大量的Excel表格的时候,我们常常自己都可能搞忘记哪些数据是非常重要的,可能一个误操作就会导致一些自己辛苦的数据丢失了。在制作大量的
- 总的来说wps软件是一款用户经常使用的办公软件,给用户带来的便利是不容忽视的,越来越多的用户开始需要编辑各种各样的文件,因此使用办公软件的频
- 当我们在使用XP系统电脑的时候,系统会自动地记录着我们在电脑上使用过的记录,那么怎么查看电脑使用情况记录呢?本文将提供XP系统电脑使用记录的
- PPT弥散阴影怎么做?有用户发现在PPT的制作中,如果添加上弥散阴影效果,整体PPT看起来更有质感,那么应该如何制作呢?下面小编为大家提供两
- 大家经常会使用win10的搜索框来搜索文件、文件夹、软件,但是最近有用户发现搜索框不能输入了,这是怎么回事?应该是注册表出现问题导致的。下面
- 电脑开机蓝屏代码0x00000000怎么办?我们在使用电脑的过程中总是会出现一些奇奇怪怪的蓝屏代码让我们电脑无法运行,这不小编就遇到了0x0
- win10的任务栏由于操作问题可能会自动隐藏,那么如何取消win10系统任务栏的自动隐藏呢?下面分享取消win10系统任务栏自动隐藏的方法,