建立live555海思编码推流服务

来源:互联网 发布:mac导入照片到ipad 编辑:程序博客网 时间:2024/05/01 16:55

因项目需要,这一周弄了一下live555。需求:海思编码——>RTSP server,使用VLC可以访问,类似于网络摄像机的需求。看了一下,live555的架构太复杂了,半桶水的C++水平还真的需要花点时间才可以明白。由于live555的例子server使用的是读取文件,打包成RTSP包然后发送。例子运行live555MediaServer,把对应的视频文件发到该服务的目录下面,在VLC使用rtsp://ip:8554/file.264即可播放file.264视频。

个人简单理解live555的架构,具体如下:

简单理解框架
source是数据源,负责采集数据,比如live555的默认的读取文件
sink就是分析数据、处理数据、发送数据(sendPacketIfNecessary)等
sink的做的东西最多,复杂!
server就是监听client、维护client、RTSP命令的处理等操作例如play、post、get等cmd处理

经过阅读代码可以发现读文件使用的是ByteStreamFileSource类继承的是FramedSource类。FrameSource是所有的源的基类。所以我们要添加自己的类也必须是继承FramedSource,我这里定义了ByteFrameLiveVideoSource类直接继承了FrameSource,在该类完成获取编码的数据, 具体的函数名为: void doGetNextFrameFormEncoder(),并定义了一个回调函数指针给doGetNextFrameFormEncoder函数调用,该接口主要是为了给C做lib的时候使用。

定义自己的server,以便符合自己的访问格式,这里使用的格式为:rtsp://ip:8554/chX/main 最后一个可以是mian或者sub、X代表通道地址0开始。
这里定义了自己的rtspserver类为:liveVideoRTSPServer继承RTSPServerSupportingHTTPStreaming类,完成服务器开始。至此完成自己的RTSP服务推流。

由于自定义的Source类是类似于读文件的类的作用,所以读取数据的时候还是读取一块的数据,然后传给sink分析数据,主要是H264的格式分析。解析数据帧,由于我们从编码出来的已经是一个Frame了,按道理来说不应该当做stream了。所以可以改prase,或者定义自己的prase来搞,这个比较复杂。对于轻量级的server这个已经足够了。而且单线程的live555也不足以完成重量级的server,效率也根不上。搞了一周,感觉live555理解起来比较吃力,C++又好久没搞了。
头文件代码如下:

#ifndef _BYTE_FRAME_LIVE_VIDEO_SOURCE_HH_#define _BYTE_FRAME_LIVE_VIDEO_SOURCE_HH_#ifndef _FRAMED_SOURCE_HH#include "FramedSource.hh"#endiftypedef int (*GetFrameCB)(int chId,int srcId,unsigned char* buf,int size);class ByteFrameLiveVideoSource: public FramedSource{public: static ByteFrameLiveVideoSource* createNew(UsageEnvironment& env,                     GetFrameCB funcCb,int chId=0,int srcId =0,                     unsigned preferredFrameSize = 0,                     unsigned playTimePerFrame = 0);  //void seekToByteAbsolute(u_int64_t byteNumber, u_int64_t numBytesToStream = 0);    // if "numBytesToStream" is >0, then we limit the stream to that number of bytes, before treating it as EOFprotected:  ByteFrameLiveVideoSource(UsageEnvironment& env,               int mchId,int msrcId,               unsigned preferredFrameSize,               unsigned playTimePerFrame);    // called only by createNew()  virtual ~ByteFrameLiveVideoSource();  static void getFrameableHandler(ByteFrameLiveVideoSource* source, int mask); void doGetNextFrameFormEncoder();private:  // redefined virtual functions:  virtual void doGetNextFrame();  virtual void doStopGettingFrames();  GetFrameCB getFrame;private:  int chId;  int srcId;    unsigned fPreferredFrameSize;  unsigned fPlayTimePerFrame;  Boolean fFidIsSeekable;  unsigned fLastPlayTime;  Boolean fHaveStartedReading;  Boolean fLimitNumBytesToStream;  u_int64_t fNumBytesToStream; // used iff "fLimitNumBytesToStream" is True};#endif

liveVideoRTSPServer头文件:

/**********This library is free software; you can redistribute it and/or modify it underthe terms of the GNU Lesser General Public License as published by theFree Software Foundation; either version 2.1 of the License, or (at youroption) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.)This library is distributed in the hope that it will be useful, but WITHOUTANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESSFOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License formore details.You should have received a copy of the GNU Lesser General Public Licensealong 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-2013, Live Networks, Inc.  All rights reserved// A subclass of "RTSPServer" that creates "ServerMediaSession"s on demand,// based on whether or not the specified stream name exists as a file// Header file#ifndef _LIVE_VIDEO_RTSP_SERVER_H#define _LIVE_VIDEO_RTSP_SERVER_H#ifndef _RTSP_SERVER_SUPPORTING_HTTP_STREAMING_HH#include "RTSPServerSupportingHTTPStreaming.hh"#endif#include <liveMedia.hh>class liveVideoRTSPServer: public RTSPServerSupportingHTTPStreaming {public:  static liveVideoRTSPServer* createNew(  UsageEnvironment& env,Port ourPort,UserAuthenticationDatabase* authDatabase,    GetFrameCB cb,unsigned reclamationTestSeconds = 65);protected:  liveVideoRTSPServer(UsageEnvironment& env, int ourSocket, Port ourPort,            UserAuthenticationDatabase* authDatabase, unsigned reclamationTestSeconds);  // called only by createNew();  virtual ~liveVideoRTSPServer();protected: // redefined virtual functions  virtual ServerMediaSession* lookupServerMediaSession(char const* streamName);private:    GetFrameCB readFreamCb;};#endif

readFrameCB是回调函数,一遍live555做成lib给c调用,海思的是C平台,所以这里用到了回调。

资源地址:
链接:http://pan.baidu.com/s/1skZax2H 密码:kzi1

0 0