1. 程式人生 > >建立live555海思編碼推流服務

建立live555海思編碼推流服務

因專案需要,這一週弄了一下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