顯示具有 streaming 標籤的文章。 顯示所有文章
顯示具有 streaming 標籤的文章。 顯示所有文章

2020年6月14日 星期日

Use Intel® Quick Sync Video (QSV)/Media SDK H264 Encoder for Video Streaming

 
    
※ The reference code you could get from here.

    Intel provides Intel® Quick Sync Video(QSV) to deal with the video processing : like encoding, decoding, resize..etc. The QSV libraries are contained in Intel® Media SDK.
In the most cases, the users are interested  in H264 encoding or trancoding when they are using Intel QSV, of course. But the example code from the tutorial package does not work in streaming.

    The root cause is the H264 NAL(Network Abstract Layer) outputted from the media SDK lacking of SPS(Sequence Parameter Set) and PPS(Picture Parameter Set) information. By default, the media SDK would not contain those information into the H264 Nal. However, in the RSTP standard, those information are required. Thus, this post wants to fill the gap : make the Intel® Media SDK able to be used in the streaming cases.

   This post continues my previous one, A RTSP Server Based on Qt Framework and LIVE555,  the x264 encoder here would be replaced as the Intel® Media SDK's.

  零. Implement the QSV H264Encoder class, based on the tutorial simple_3_encode :

   This class we named IntelHDGraphicsH264Encoder, a subclass of H264Encoder (to unify the interfaces) :
class IntelHDGraphicsH264Encoder : public H264Encoder
{
public:
 static bool IsHardwareSupport(QSize resolution);

public:
 IntelHDGraphicsH264Encoder(void);
 ~IntelHDGraphicsH264Encoder(void) Q_DECL_OVERRIDE;

public :
 int Init(int width, int height) Q_DECL_OVERRIDE;
 void Close(void) Q_DECL_OVERRIDE;

 QQueue<int> Encode(unsigned char *p_frame_data, 
  QQueue<QByteArray> &ref_queue) Q_DECL_OVERRIDE;
private:
 mfxStatus ExtendMfxBitstream(mfxU32 new_size);

private:
 int m_width, m_height;
 MFXVideoSession m_session;
 MFXVideoENCODE *m_p_enc;
 mfxU8 *m_p_surface_buffers;
 mfxFrameSurface1 **m_pp_encoding_surfaces;
 mfxU16 m_num_surfaces;
 mfxBitstream m_mfx_bitsream;

private :
 QMutex m_mutex;
};

     There is a class static function IsHardwareSupport to check if there is hardware supporting with Intel QSV, and if the resolution is encodable or not. (QSV supports with the width being a multiply of 16, and maximum resolution is 1920x1200) 


The class definitions are straight forward, we leave out.  But there are two things we should take notice : one is, the QSV encoder input must be NV12; the other is : while the buffer is not enough, it should be resized by the function ExtendMfxBitstream (about line 378):
  :
  else if (MFX_ERR_NOT_ENOUGH_BUFFER == sts)
  {
   mfxVideoParam par;

   memset(&par, 0, sizeof(par));
   sts = m_p_enc->GetVideoParam(&par);

   //printf("require %d KB\r\n", par.mfx.BufferSizeInKB);
   ExtendMfxBitstream( par.mfx.BufferSizeInKB * 1024);

   break;
  } else
  :


Below it is the integration of IntelHDGraphicsH264Encoder in H264NalFactory.cpp about line 51 :
 :
 m_nal_queue.clear();

#if(1)
 if( true == IntelHDGraphicsH264Encoder::IsHardwareSupport(resolution))
 {
  m_p_h264_encoder = new IntelHDGraphicsH264Encoder();
  qDebug() << "Encoder :: Intel QSV";
 }
 else
#endif
 {
  m_p_h264_encoder = new X264Encoder();
  qDebug() << "Encoder :: X264";
 }
 :


Of course, we need to add the dependency library in the Qt project file :
:
INTEL_MEDIA_SDK_PATH =  $$PWD/../libs/"Intel(R) Media SDK 2019 R1"

INCLUDEPATH += $$INTEL_MEDIA_SDK_PATH/include
LIBS += $$INTEL_MEDIA_SDK_PATH/lib/x64/libmfx_vs2015.lib

LIBS += Advapi32.lib
:

And now the encoder works : if we save the output NAL into a file,  ffplay or VLCPlayer could play it.

However, the encoder does not work while we use it in streaming:


ffplay hanging, it would not pop-up a windows to play the streaming.



二. Add the SPS and PPS packets in the streaming.

Refer to here and this example code,  we change the encoder code as :

IntelHDGraphicsH264Encoder.h

 :
 mfxBitstream m_mfx_bitsream;

#if !defined(_NO_SPSPPS)
 mfxExtCodingOptionSPSPPS m_spspps_coding_option;
 mfxU8 m_sps_buffer[128];
 mfxU8 m_pps_buffer[128];
#endif
 :


IntelHDGraphicsH264Encoder.cpp, 

Init function, about line 234:

 :
 sts = m_p_enc->Init(&params_in);
 if (MFX_ERR_NONE != sts)
 {
  printf("MFXVideoENCODE.Init failed, %d\r\n", sts);
  return -4;
 }

#if !defined(_NO_SPSPPS)
 memset(&m_spspps_coding_option, 0, sizeof(mfxExtCodingOptionSPSPPS));

 m_spspps_coding_option.Header.BufferId = MFX_EXTBUFF_CODING_OPTION_SPSPPS;
 m_spspps_coding_option.Header.BufferSz = sizeof(mfxExtCodingOptionSPSPPS);

 m_spspps_coding_option.SPSBuffer = &m_sps_buffer[0];
 m_spspps_coding_option.SPSBufSize = sizeof(m_sps_buffer);
 m_spspps_coding_option.PPSBuffer = &m_pps_buffer[0];
 m_spspps_coding_option.PPSBufSize = sizeof(m_pps_buffer);

 mfxExtBuffer* extendedBuffers[1];
 extendedBuffers[0] = (mfxExtBuffer*) & m_spspps_coding_option;
#endif

 mfxVideoParam video_param;
 memset(&video_param, 0, sizeof(video_param));

#if !defined(_NO_SPSPPS)
 video_param.ExtParam = &extendedBuffers[0];
 video_param.NumExtParam = 1;
#endif

 sts = m_p_enc->GetVideoParam(&video_param);
 :


Encode function, about line 414 :

 :
 array = QByteArray((char*)m_mfx_bitsream.Data + m_mfx_bitsream.DataOffset,
        m_mfx_bitsream.DataLength);

#if !defined(_NO_SPSPPS)
 if( MFX_FRAMETYPE_IDR & m_mfx_bitsream.FrameType)
 {
  //printf("MFX_FRAMETYPE_IDR\r\n");
  ref_queue.enqueue(QByteArray((char*)m_spspps_coding_option.SPSBuffer,
          m_spspps_coding_option.SPSBufSize));
  h264_nal_size_queue.enqueue( m_spspps_coding_option.SPSBufSize);
  ref_queue.enqueue(QByteArray((char*)m_spspps_coding_option.PPSBuffer,
          m_spspps_coding_option.PPSBufSize));
  h264_nal_size_queue.enqueue( m_spspps_coding_option.PPSBufSize);
 }
#endif

 ref_queue.enqueue(array);
 :



And Now the streaming works.





2020年4月28日 星期二

A RTSP Server Based on Qt Framework and LIVE555



※ The code corresponding to this post is here, the "Server" folder.

    Recent years, the streaming technologies have been more and more widely used : the drone remote control,  the live broadcasting and the video conference.. and so forth. There are several protocols, like HTTP Live Streaming (HLS), WebRTC..etc. But almost all the protocols are based on Real Time Streaming Protocol(RTSP). Thus, RTSP is the zeroth choice for the applications just integrated with the streaming function. `

   One of the famous RTSP libraries is LIVE555. In this post, I  give the key steps for how to set a camera RTSP server on Qt Framework. Why I use Qt ? It is Qt able to reduce the work of Camera grabbing and window drawing. We could pay more attention on LIVE555 without the disturbing from the other issues.

  The video format in here is H264, for its low data-rate, acceptable image-quality and the mature codec implementations, H264 is widely used in streaming. The most famous software implementation of H264 is x264, so I adopt it for this post.


零. Build the x264 library

    ※ I have prepared this library in the git-repository.
    My environment is Windows, Thus I use Mingw-w64 to build x264. You could refer to here to know to install it. After it has been done, download, untar x264 source code and put it in the Mingw-w64 user's folder.
My configuration arguments are :

 ./configure --host=mingw64 --prefix=$PWD/build --enable-shared
 Them make & make install, the x264 dll library would be in the folder build. We need that as the h264 encoder.


一. The Camera Frame Grabber

   We write a class named CameraFramGabber to fetch the image from the camera. That inherits from the class QAbstractVideoSurface, which requires its subclass implemented the 2 pure-virtual functions, present and supportedPixelFormats. While the frame has been fetched, the signal UpdateFrame , which contains the image, would be sent from the present function.

class CameraFrameGrabber : public QAbstractVideoSurface{
:
public:
 virtual bool present(const QVideoFrame &frame) Q_DECL_OVERRIDE;

 virtual QList<QVideoFrame::PixelFormat>
  supportedPixelFormats(QAbstractVideoBuffer::HandleType type) const 
 Q_DECL_OVERRIDE;
:
public slots:
:
 void UpdateFrame(const QImage image);
:
};



bool CameraFrameGrabber::present(const QVideoFrame &frame)
{
 //qDebug() << "grabbed pixelFormat is " << cloneFrame.pixelFormat();
 QVideoFrame cloneFrame(frame);

 cloneFrame.map(QAbstractVideoBuffer::ReadOnly);

 QImage grabbed_image;

 if(QVideoFrame::Format_YUYV == cloneFrame.pixelFormat())
 {
  QByteArray temp;
  temp.resize(cloneFrame.width() * cloneFrame.height() * 3);

  YUYVtoRGB(cloneFrame.width(), cloneFrame.height(),
      cloneFrame.bits(), (unsigned char*)temp.data());

  grabbed_image =  QImage((unsigned char*)temp.data(), cloneFrame.width(),
        cloneFrame.height(), cloneFrame.width() * 3, QImage::Format_RGB888).copy();
 }
#if(0)
 else if(QVideoFrame::Format_RGB32 == cloneFrame.pixelFormat())
 {
  grabbed_image = QImage(cloneFrame.bits(), cloneFrame.width(),
          cloneFrame.height(), cloneFrame.width() * 4, QImage::Format_RGB32).copy();
 }
#endif
 cloneFrame.unmap();

 emit FrameUpdated(grabbed_image);

 return true;
}

  The VideoFrame format at present, would be one of the formats returned from supportedPixelFormats :
QList<QVideoFrame::PixelFormat>
 CameraFrameGrabber::supportedPixelFormats(
  QAbstractVideoBuffer::HandleType type) const
{
 if (type != QAbstractVideoBuffer::NoHandle)
  return QList<QVideoFrame::PixelFormat>();

 QList<QVideoFrame::PixelFormat> pixel_fmt;
#if(1)
 pixel_fmt.append(QVideoFrame::Format_RGB24);
 pixel_fmt.append(QVideoFrame::Format_RGB32);
#endif
 pixel_fmt.append(QVideoFrame::Format_YUYV);

 return pixel_fmt;
}

  We set the output format as  QVideoFrame::Format_YUYV in the function ChangeCameraSetting. it brings we only need to prepare one set of format conversion(YUYV to RGB24).
void CameraFrameGrabber::ChangeCameraSetting(QCamera::State state)
{
	:
	QCameraViewfinderSettings viewfinderSettings;
	viewfinderSettings = m_p_camera->viewfinderSettings();
	:
	qDebug() << m_p_camera->supportedViewfinderPixelFormats();
	viewfinderSettings.setPixelFormat(QVideoFrame::Format_YUYV);
	:

    However,  supporting with only one conversion may make the fetching inefficient : if the camera supports with RGB32, the YUYV to RGB24 conversion consumes more CPU resource than RGB32 to RGB24. The reason why we choose YUYV format is, the format is the most compatible : almost every USB-camera supports with this format.

※ My YUYVtoRGB function assumes the camera resolution is a multiple of four. If it is not, it may lead the application crashed or the image slanted .


二. The H264 Encoder and H264 NAL Factory

 Though currently there is only one H264 encoder, but it may change to the other encoders( like vEnc, OpenH264, DirectShow..etc) in the future. Thus we prepare a H264 virtual class to unify the calling interfaces. That is :

class H264Encoder
{
public:
 H264Encoder(void){}
 virtual ~H264Encoder(void){}

public :
 virtual int Init(int width, int height) = 0;
 virtual void Close(void) = 0;

 virtual QQueue<int> Encode(unsigned char *p_frame_data, 
      QQueue<QByteArray> &ref_queue) = 0;

};

    Notice that the H264 property : one frame may be encoded into multi-NAL units, so we use a queue to store the HAL data and data length.
  The class X264Encoder, using the interfaces of H264Encoder, is our H264 Encoder class directly calling x264 API. I leave out the explanation of X264Encoder, for the x264 calling examples are very easy to be found.
    There is a class H264NalFactory, containing X264Encoder. It makes the encoder is easily to be replaced. Beside, because the image producer(the frame grabber) and the consumer (the RTSP Server) are in the different thread, thus we need a synchronizer to control the race condition.

class H264NalFactory : public QObject
{
 Q_OBJECT
public:
 H264NalFactory(QObject *p_frame_grabber);
 ~H264NalFactory(void) Q_DECL_OVERRIDE;

public:
 int GetH264Nal(unsigned char *p_data, int buffer_size);

public slots:
 void SetResolution(QSize resolution);
 int Encode(QImage image);
 
:
 H264Encoder *m_p_h264_encoder;
 QQueue<QByteArray> m_nal_queue;
};
Here we use a queue to contain the NAL data.

The two key functions are as below, as the producer and consumer interfaces, and we use a semaphore for the synchronization.
int H264NalFactory::Encode(QImage image)
{
 :
 QQueue<int> nal_size_queue;
 
 nal_size_queue = m_p_h264_encoder->Encode(image.bits(), m_nal_queue);
 m_semaphore.release(nal_size_queue.size());
 :
}

int H264NalFactory::GetH264Nal(unsigned char *p_data, int buffer_size)
{
 :
 m_semaphore.tryAcquire(1, MAX_WAIT_TIME_IN_MS);

 if(0 == m_nal_queue.size())
   return 0;
 :
}


三. Prepare the LIVE555 libraries.

    The LIVE555 libaries could be download here. Despite the libraries (ver. 1.00) have been prepared in my repository, I still suggest you to download and build it by yourself.
    We need to create four Qt projects as static library : BasicUsageEnvironment, groupsock, liveMedia and UsageEnvironment, those are corresponding to the LIVE555 folder names, respectively. If your  LIVE555 is later than ver.0.98, It is necesary to disable the OPENSSL function in the liveMedia.

in liveMedia.pro :
:
CONFIG += c++11

DEFINES += NO_OPENSSL
:


四. Implement the RTSP Server.

    Create a project file, we name QtRTSPServer, to contain the build information. It is a very typical QtCreator .pro file.  I omit the most explanation. One thing I need to mention is as the below lines :
LIBS += $$LIVE555_PATH/groupsock/$$BUILD_MODE/groupsock.lib
LIBS += $$LIVE555_PATH/liveMedia/$$BUILD_MODE/liveMedia.lib

PRE_TARGETDEPS += $$LIVE555_PATH/BasicUsageEnvironment/$$BUILD_MODE/BasicUsageEnvironment.lib
PRE_TARGETDEPS += $$LIVE555_PATH/UsageEnvironment/$$BUILD_MODE/UsageEnvironment.lib
PRE_TARGETDEPS += $$LIVE555_PATH/groupsock/$$BUILD_MODE/groupsock.lib
PRE_TARGETDEPS += $$LIVE555_PATH/liveMedia/$$BUILD_MODE/liveMedia.lib

LIBS += Ws2_32.lib

After adding those lines, run qmake, (with make argument install) it will let QtCreator build the dependencies libraries first.


   Below heavily refer to testOnDemandRTSPServer.cpp, which is our RSTP server's prototype.

   As testOnDemandRTSPServer, our server on a thread, thus it is a subclass of QThread with some members related to LIVE555, is declaration is :
class CameraRTSPServer : public QThread
{
 Q_OBJECT
public:
 explicit CameraRTSPServer(QObject *p_grabber, 
  unsigned short port = 554);

 ~CameraRTSPServer(void) Q_DECL_OVERRIDE;

 void Stop(void);

protected:
 virtual void run(void) Q_DECL_OVERRIDE;
private:
 int CreateLive555Objects(void);
 void DestroyLive555Objects(void);
 void AnnounceStream(RTSPServer* p_rtsp_server, 
          ServerMediaSession *m_p_media_session);

private:
 H264NalFactory *m_p_h264_nal_factory;

 unsigned short m_port;
 UsageEnvironment *m_p_env;

 TaskScheduler *m_p_scheduler;
 RTSPServer *m_p_rtsp_server;
 ServerMediaSession *m_p_server_media_session;
 volatile char m_is_to_stop_scheduler;

};

    The LIVE555 members initialization/destroy have been organized as the function CreateLive555Objects and DestoryLive555Objects, brings the run function simple:
void CameraRTSPServer::run(void)
{
 if(0 > CreateLive555Objects())
  return ;

 AnnounceStream(m_p_rtsp_server, 
  m_p_server_media_session);

 m_is_to_stop_scheduler = 0;
 m_p_scheduler->doEventLoop(&m_is_to_stop_scheduler);

 DestroyLive555Objects();
}

    And we need a bridge connecting the RTSP server with the H264NalFactory. Refer to testOnDemandRTSPServer.cpp, about line 101.
// A H.264 video elementary strea
  {
    char const* streamName = "h264ESVideoTest";
    char const* inputFileName = "test.264";
    ServerMediaSession* sms = ServerMediaSession::createNew(
          *env, streamName, streamName, descriptionString);
    sms->addSubsession(H264VideoFileServerMediaSubsession::createNew(
          *env, inputFileName, reuseFirstSource));
    rtspServer->addServerMediaSession(sms);

    announceStream(rtspServer, sms, streamName, inputFileName);
  }

The class H264VideoFileServerMediaSubsession :
class H264VideoFileServerMediaSubsession: public FileServerMediaSubsession {
public:
  static H264VideoFileServerMediaSubsession* createNew(UsageEnvironment& env,
           char const* fileName, Boolean reuseFirstSource);

  // Used to implement "getAuxSDPLine()":
  void checkForAuxSDPLine1();
  void afterPlayingDummy1();

protected:
  H264VideoFileServerMediaSubsession(UsageEnvironment& env,
          char const* fileName, Boolean reuseFirstSource);
      // called only by createNew();
  virtual ~H264VideoFileServerMediaSubsession();

  void setDoneFlag() { fDoneFlag = ~0; }

protected: // redefined virtual functions
  virtual char const* getAuxSDPLine(RTPSink* rtpSink, FramedSource* inputSource);
  virtual FramedSource* createNewStreamSource(unsigned clientSessionId,
           unsigned& estBitrate);
  virtual RTPSink* createNewRTPSink(Groupsock* rtpGroupsock,
           unsigned char rtpPayloadTypeIfDynamic, FramedSource* inputSource);

private:
  char* fAuxSDPLine;
  char fDoneFlag; // used when setting up "fAuxSDPLine"
  RTPSink* fDummyRTPSink; // ditto
};
※ the class FileServerMediaSubsession inherits from the class OnDemandServerMediaSubsession.

    It is : there is a class, named H264VideoFileServerMediaSubsession derived from the class OnDemandServerMediaSubsession, set into the RTSP server.

    For our class to be set into the RTSP Server, is the class H264MediaSubsession, which is pretty like class H264VideoFileServerMediaSubsession :
class H264MediaSubsession : public OnDemandServerMediaSubsession
{
public:
 H264MediaSubsession(UsageEnvironment& ref_env, 
 bool is_reuse_first_source, void *p_grabber);
 virtual ~H264MediaSubsession(void);
 void setDoneFlag() { m_done_flag = 1; }

protected:
 virtual char const* getAuxSDPLine(RTPSink* p_rtp_sink, 
       FramedSource *p_input_source);
 
 virtual FramedSource* createNewStreamSource(unsigned client_session_id,
       unsigned &ref_estimate_bitrate);

 virtual RTPSink* createNewRTPSink(Groupsock* rtpGroupsock, 
       unsigned char rtpPayloadTypeIfDynamic, FramedSource* inputSource);

public:
 void checkForAuxSDPLine1(void);
 void afterPlayingDummy1(void);

private:
 void *m_p_grabber;
 char* m_p_AuxSDPLine;
 char m_done_flag;
 RTPSink *m_p_dummy_Sink;

};

    The function createNewStreamSource is the major difference between our H264MediaSubsession and the H264VideoFileServerMediaSubsession. Here we use the H264VideoStreamDiscreteFramer as the framer, instread of the H264VideoStreamFramer :
FramedSource* H264MediaSubsession::createNewStreamSource(unsigned client_session_id,
               unsigned &ref_estimate_bitrate)
{
 ref_estimate_bitrate = 2 * 1024 * 1024;

 CameraH264Source *p_source;
 p_source = CameraH264Source::createNew(envir(),
  client_session_id, m_p_grabber);

 return H264VideoStreamDiscreteFramer::createNew(envir(), p_source);
}

※ How H264VideoFileServerMediaSubssesion works, your could refer to here (in 漢文).

    Now,  refer to ByteStreamFileSource.cpp, which is used in the H264VideoFileServerMediaSubssesion. We need to implement our own source class, named CameraH264Source.

    As the H264VideoFileServerMediaSubssesion declaration indicates, the source must be a subclass of the FramedSource. In our case, the source class is named CameraH264Source, its declaration as below.
#ifndef CameraH264Source_H
#define CameraH264Source_H

#include "FramedSource.hh"
#include "H264NalFactory.h"

class CameraH264Source : public FramedSource
{
public:
 static CameraH264Source *createNew(UsageEnvironment &ref_env, 
       unsigned client_session_id, void *p_h264_nal_factory);

protected:
 CameraH264Source(UsageEnvironment &ref_env, 
       H264NalFactory *m_p_h264_nal_factory);
 ~CameraH264Source(void);

public:
 virtual void doGetNextFrame();
 virtual unsigned int maxFrameSize() const;

public:
 virtual Boolean isH264VideoStreamFramer(void) const {
  return true; // default implementation
 }


protected:
 void DeliverFrame(void);

private:
 H264NalFactory *m_p_h264_nal_factory;
 unsigned char *m_p_frame_buffer;
};

#endif // CameraH264Source_H
※ VERY important : the isH264VideoStreamFramer function (declared in class MediaSource, the parent of the class FramedSource ) must be overridden, to denote the data is H264 data.

    The most importance of the class CameraH264Source is the interface (virtual) function doGetNextFrame, where the H264 NAL data would be copied to the FramedSource buffer. The calling of the function FramedSource::afterGetting must be in the end of doGetNextFrame, it will schedule when is the next time doGetNextFrame will be called.
    Notice that the H264 NAL head (0x000001 or 0x00000001) must be removed, or the H264VideoStreamDiscreteFramer will keep complaining and bring the streaming fail.

    ※The function DeliverFrame is the implementation of doGetNextFrame.
void CameraH264Source::DeliverFrame(void)
{
 FramedSource::fFrameSize = 0;
 FramedSource::fNumTruncatedBytes = 0;

 unsigned int h264_data_length;

 h264_data_length = (unsigned int)m_p_h264_nal_factory->GetH264Nal(
       m_p_frame_buffer, ONE_FRAME_BUFFER_SIZE);

 if(0 >= h264_data_length)
  return ;


 int trancate;
 trancate = 0;

 {
  char trancate_4[4] = {0x00, 0x00, 0x00, 0x01};
  char trancate_3[3] = {0x00, 0x00, 0x01};

  if(0 == memcmp(&m_p_frame_buffer[0], &trancate_4[0], sizeof(trancate_4)))
  {
   trancate = 4;
  }
  else if(0 == memcmp(&m_p_frame_buffer[0], &trancate_3[0], sizeof(trancate_3)))
  {
   trancate = 3;
  }
 }

 FramedSource::fFrameSize = h264_data_length;
 FramedSource::fNumTruncatedBytes = 0;

 if(h264_data_length > FramedSource::fMaxSize)
 {
  FramedSource::fFrameSize = FramedSource::fMaxSize;
  FramedSource::fNumTruncatedBytes =
    h264_data_length - FramedSource::fMaxSize;
 }/*if */

 gettimeofday(&(FramedSource::fPresentationTime), nullptr);

 memmove(FramedSource::fTo, m_p_frame_buffer + trancate,
  FramedSource::fFrameSize - trancate);

 FramedSource::afterGetting(this);
}

To here, the implementation of the RTSP server has completed.


Now, run the QtSTPServer we done, it will show the RTSP URL in the console.
:
"h264" stream
Play this stream using the URL "rtsp://192.168.13.183:8554/h264"

※In Creator, you had better to add install in the make arguments, in Project -> Build & Run->Build Settings -> Build Steps -> Make -> Make arguments. It will automatically build the dependencies(LIVE555), and copy the necessary dll (x264) to the working folder.


五. VLC player as the RTSP Client.

    There are some players support with RTSP client, VLC is one. We run the VLC in the same computer, and input the streaming URL in that ( VLC menu -> Open Network Stream).

※Beware : if your camera FPS is low( < 10), you need to increase the caching value.


Theoretically, the QtRTSPServer will be streaming to the VLC player, as the below figures show.

Apparently, there is color shift.

My next post would discuss how to replaces the x264 encoder as the Intel QSV encoder. 

Reference.
https://stackoverflow.com/questions/19427576/live555-x264-stream-live-source-based-on-testondemandrtspserver








2015年4月9日 星期四

Dump H264 Raw Data From LIVE555 Client


      Maybe lots persons encounter the same circumstances : after the succession of building LIVE555, and execute the testRTSPClient to connect a rtsp server: the debug log  seems be pettey good; But when we damp the h264 data to be played by VLC or ffplay, that is unplayable. Of cource, if we deliver the h264 data to a h264 decoder directly, there would occur error always.


   I found there is no article being concise with complete code  in the website, I fill it in here.

The code base on LIVE555\testProgs\testRTSPClient.cpp.

modify the constructor and destructor of DummySink as below, it is about line 479:


unsigned char *pH264 = NULL;

DummySink::DummySink(UsageEnvironment& env, MediaSubsession& subsession, char const* streamId)
  : MediaSink(env),
    fSubsession(subsession) {
  fStreamId = strDup(streamId);
  fReceiveBuffer = new u_int8_t[DUMMY_SINK_RECEIVE_BUFFER_SIZE];
  pH264 = (unsigned char*)malloc(256*1024);
}

DummySink::~DummySink() {
  delete[] fReceiveBuffer;
  delete[] fStreamId;
  if(NULL != pH264)
   free(pH264);
}



And modify afterGettingFrame (the one with debug log ) as


void DummySink::afterGettingFrame(unsigned frameSize, unsigned numTruncatedBytes,
      struct timeval presentationTime, unsigned /*durationInMicroseconds*/) {
#if(0)
  // We've just received a frame of data.  (Optionally) print out information about it:
#ifdef DEBUG_PRINT_EACH_RECEIVED_FRAME
  if (fStreamId != NULL) envir() << "Stream \"" << fStreamId << "\"; ";
  envir() << fSubsession.mediumName() << "/" << fSubsession.codecName() << ":\tReceived " << frameSize << " bytes";
  if (numTruncatedBytes > 0) envir() << " (with " << numTruncatedBytes << " bytes truncated)";
  char uSecsStr[6+1]; // used to output the 'microseconds' part of the presentation time
  sprintf(uSecsStr, "%06u", (unsigned)presentationTime.tv_usec);
  envir() << ".\tPresentation time: " << (int)presentationTime.tv_sec << "." << uSecsStr;
  if (fSubsession.rtpSource() != NULL && !fSubsession.rtpSource()->hasBeenSynchronizedUsingRTCP()) {
    envir() << "!"; // mark the debugging output to indicate that this presentation time is not RTCP-synchronized
  }
#ifdef DEBUG_PRINT_NPT
  envir() << "\tNPT: " << fSubsession.getNormalPlayTime(presentationTime);
#endif
  envir() << "\n";
#endif
#else
  printf("__FUNCTION__  = %s\n", __FUNCTION__ );
 if(0 == strncmp(fSubsession.codecName(), "H264", 16))
 { 
  unsigned char nalu_header[4] = { 0, 0, 0, 1 };   
  unsigned char extraData[256]; 
  unsigned int num = 0;    
  
  SPropRecord *pSPropRecord;
  pSPropRecord = parseSPropParameterSets(fSubsession.fmtp_spropparametersets(), num);  
  
  unsigned int extraLen;
  extraLen = 0;

  //p_record[0] is sps 
  //p+record[1] is pps
  for(unsigned int i = 0; i < num; i++){ 
   memcpy(&extraData[extraLen], &nalu_header[0], 4);
   extraLen += 4;
   memcpy(&extraData[extraLen], pSPropRecord[i].sPropBytes, pSPropRecord[i].sPropLength);
   extraLen += pSPropRecord[i].sPropLength;
  }/*for i*/

  memcpy(&extraData[extraLen], &nalu_header[0], 4);
  extraLen += 4;

  delete[] pSPropRecord ;  

  memcpy(pH264, &extraData[0], extraLen);
  memcpy(pH264 + extraLen, fReceiveBuffer, frameSize); 
  
  int totalSize;
  totalSize = extraLen + frameSize;

  static FILE *fp = fopen("saved.h264", "wb");

  fwrite(pH264, 1,  totalSize, fp);
  fflush(fp);
  printf("\tsaved %d bytes\n", totalSize);
 }/*if 0 == strncmp(fSubsession.codecName(), "H264", 16)*/
#endif  
  // Then continue, to request the next frame of data:
  continuePlaying();
}





That is, LIVE555 server  omits the SPS(Sequence Parameter Set) and PPS(Picture Parameter Set) parser information of each h264 slice.

Here  I compliment SPS and PPS  flag, which be "0x00 0x00 0x00 0x01".


You could refer to here for more detail about SPS and PPS.