因項目需要,這一周弄了一下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" #endif typedef 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 EOF protected: 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 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-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