文如其名,最近在做的项目要求利用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 <FramedSource.hh> 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
<pre name="code" class="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; if (bufsizel<fMaxSize) { fFrameSize = bufsizel; } else { 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 { fFrameSize= bufsizel - readbufsize;//fFrameSize是会告诉live555发送数据区的大小,必须赋予正确的值 memcpy(fTo, Framed_databuf + readbufsize,fFrameSize); *Framed_dosent = true; } } nextTask() = envir().taskScheduler().scheduleDelayedTask(0,(TaskFunc*)FramedSource::afterGetting, this);//表示延迟0秒后再执行 afterGetting 函数 return; }
工程下载地址:点击打开链接
每个工程都是有bug的哈哈,之前写这个文章的时候并没有做认真的测试,在之后做项目的时候用到这一块的时候发现了一些问题,今天就更新一下这份代码吧。如果是下了我之前发布的工程的只能说抱歉了,csdn似乎上传资源一段时间就删不了了,无法更新到最新的版本。工程中H264FramedLiveSource.cpp这个文件中的函数
void H264FramedLiveSource::doGetNextFrame()中我少写了一句话,就是在
memcpy(fTo, Framed_databuf + readbufsize, bufsizel - readbufsize);后面需要加上
fFrameSize= bufsizel - readbufsize;//fFrameSize是会告诉live555发送数据区的大小,必须赋予正确的值
这个我已经更新了我博客的代码了,写在这里是提醒下下载我工程的人原来是忘了加的。
然后由于LIVE555默认OutPacketBuffer的大小只有60000
unsigned OutPacketBuffer::maxSize = 60000;所以导致如果264文件数据超过这个的时候可能会报错,这个时候可以选择包含MediaSink.hh,MediaSink.cpp这两个文件(在工程目录搜索就可以找到)到工程中,并且直接搜索整个工程。
unsigned OutPacketBuffer::maxSize = 60000
这句代码,将60000改成288000再重新编译就可以了。
对于将从文件截取数据段改小之后播放一段时间会报内存错误的问题,如:
#define BUFSIZE 1024*200改为
#define BUFSIZE 1024*80是由于live555要去截取数据段的时候要求的数据比数据区有的数据还多照成的,将H264FramedLiveSource.cpp中的
fFrameSize = fMaxSize;改为
if (bufsizel<fMaxSize) { fFrameSize = bufsizel; } else { fFrameSize = fMaxSize; }即可。
对于其他的一些问题,由于本人还没进行进一步的论证就先不放出来了,如果大家使用过程中遇到什么问题可以留言跟我说。