通过live555实现H264 RTSP直播

原文链接http://blog.csdn.net/firehood_/article/details/16844397点击打开链接

 

前面的文章中介绍了《H264视频通过RTMP流直播》,下面将介绍一下如何将H264实时视频通过RTSP直播。

      实现思路是将视频流发送给live555, 由live555来实现H264数据流直播。

      视频采集模块通过FIFO队列将H264数据帧发送给live555. live555 在收到客户端的RTSP播放请求后,开始从FIFO中读取H264视频数据并通过RTSP直播出去。整个流程如下图所示:


调整和修改Live555 MediaServer

        下载live555源码,在media目录下增加四个文件并修改文件live555MediaServer.cpp。增加的四个文件如下:

WW_H264VideoServerMediaSubsession.h

WW_H264VideoServerMediaSubsession.cpp

 WW_H264VideoSource.h

WW_H264VideoSource.cpp

        下面附上四个文件的源码:

WW_H264VideoServerMediaSubsession.h

  1. #pragma once  
  2.   
  3. #include "liveMedia.hh"  
  4. #include "BasicUsageEnvironment.hh"  
  5. #include "GroupsockHelper.hh"  
  6.   
  7. #include "OnDemandServerMediaSubsession.hh"  
  8. #include "WW_H264VideoSource.h"  
  9.   
  10. class WW_H264VideoServerMediaSubsession : public OnDemandServerMediaSubsession  
  11. {  
  12. public:  
  13.     WW_H264VideoServerMediaSubsession(UsageEnvironment & env, FramedSource * source);  
  14.     ~WW_H264VideoServerMediaSubsession(void);  
  15.   
  16. public:  
  17.     virtual char const * getAuxSDPLine(RTPSink * rtpSink, FramedSource * inputSource);  
  18.     virtual FramedSource * createNewStreamSource(unsigned clientSessionId, unsigned & estBitrate); // "estBitrate" is the stream's estimated bitrate, in kbps  
  19.     virtual RTPSink * createNewRTPSink(Groupsock * rtpGroupsock, unsigned char rtpPayloadTypeIfDynamic, FramedSource * inputSource);  
  20.   
  21.     static WW_H264VideoServerMediaSubsession * createNew(UsageEnvironment & env, FramedSource * source);  
  22.   
  23.     static void afterPlayingDummy(void * ptr);  
  24.   
  25.     static void chkForAuxSDPLine(void * ptr);  
  26.     void chkForAuxSDPLine1();  
  27.   
  28. private:  
  29.     FramedSource * m_pSource;  
  30.     char * m_pSDPLine;  
  31.     RTPSink * m_pDummyRTPSink;  
  32.     char m_done;  
  33. };  
#pragma once

#include "liveMedia.hh"
#include "BasicUsageEnvironment.hh"
#include "GroupsockHelper.hh"

#include "OnDemandServerMediaSubsession.hh"
#include "WW_H264VideoSource.h"

class WW_H264VideoServerMediaSubsession : public OnDemandServerMediaSubsession
{
public:
	WW_H264VideoServerMediaSubsession(UsageEnvironment & env, FramedSource * source);
	~WW_H264VideoServerMediaSubsession(void);

public:
	virtual char const * getAuxSDPLine(RTPSink * rtpSink, FramedSource * inputSource);
	virtual FramedSource * createNewStreamSource(unsigned clientSessionId, unsigned & estBitrate); // "estBitrate" is the stream's estimated bitrate, in kbps
	virtual RTPSink * createNewRTPSink(Groupsock * rtpGroupsock, unsigned char rtpPayloadTypeIfDynamic, FramedSource * inputSource);

	static WW_H264VideoServerMediaSubsession * createNew(UsageEnvironment & env, FramedSource * source);

	static void afterPlayingDummy(void * ptr);

	static void chkForAuxSDPLine(void * ptr);
	void chkForAuxSDPLine1();

private:
	FramedSource * m_pSource;
	char * m_pSDPLine;
	RTPSink * m_pDummyRTPSink;
	char m_done;
};

        

WW_H264VideoServerMediaSubsession.cpp

  1. #include "WW_H264VideoServerMediaSubsession.h"  
  2.   
  3. WW_H264VideoServerMediaSubsession::WW_H264VideoServerMediaSubsession(UsageEnvironment & env, FramedSource * source) : OnDemandServerMediaSubsession(env, True)  
  4. {  
  5.     m_pSource = source;  
  6.     m_pSDPLine = 0;  
  7. }  
  8.   
  9. WW_H264VideoServerMediaSubsession::~WW_H264VideoServerMediaSubsession(void)  
  10. {  
  11.     if (m_pSDPLine)  
  12.     {  
  13.         free(m_pSDPLine);  
  14.     }  
  15. }  
  16.   
  17. WW_H264VideoServerMediaSubsession * WW_H264VideoServerMediaSubsession::createNew(UsageEnvironment & env, FramedSource * source)  
  18. {  
  19.     return new WW_H264VideoServerMediaSubsession(env, source);  
  20. }  
  21.   
  22. FramedSource * WW_H264VideoServerMediaSubsession::createNewStreamSource(unsigned clientSessionId, unsigned & estBitrate)  
  23. {  
  24.     return H264VideoStreamFramer::createNew(envir(), new WW_H264VideoSource(envir()));  
  25. }  
  26.   
  27. RTPSink * WW_H264VideoServerMediaSubsession::createNewRTPSink(Groupsock * rtpGroupsock, unsigned char rtpPayloadTypeIfDynamic, FramedSource * inputSource)  
  28. {  
  29.     return H264VideoRTPSink::createNew(envir(), rtpGroupsock, rtpPayloadTypeIfDynamic);  
  30. }  
  31.   
  32. char const * WW_H264VideoServerMediaSubsession::getAuxSDPLine(RTPSink * rtpSink, FramedSource * inputSource)  
  33. {  
  34.     if (m_pSDPLine)  
  35.     {  
  36.         return m_pSDPLine;  
  37.     }  
  38.   
  39.     m_pDummyRTPSink = rtpSink;  
  40.   
  41.     //mp_dummy_rtpsink->startPlaying(*source, afterPlayingDummy, this);  
  42.     m_pDummyRTPSink->startPlaying(*inputSource, 0, 0);  
  43.   
  44.     chkForAuxSDPLine(this);  
  45.   
  46.     m_done = 0;  
  47.   
  48.     envir().taskScheduler().doEventLoop(&m_done);  
  49.   
  50.     m_pSDPLine = strdup(m_pDummyRTPSink->auxSDPLine());  
  51.   
  52.     m_pDummyRTPSink->stopPlaying();  
  53.   
  54.     return m_pSDPLine;  
  55. }  
  56.   
  57. void WW_H264VideoServerMediaSubsession::afterPlayingDummy(void * ptr)  
  58. {  
  59.     WW_H264VideoServerMediaSubsession * This = (WW_H264VideoServerMediaSubsession *)ptr;  
  60.   
  61.     This->m_done = 0xff;  
  62. }  
  63.   
  64. void WW_H264VideoServerMediaSubsession::chkForAuxSDPLine(void * ptr)  
  65. {  
  66.     WW_H264VideoServerMediaSubsession * This = (WW_H264VideoServerMediaSubsession *)ptr;  
  67.   
  68.     This->chkForAuxSDPLine1();  
  69. }  
  70.   
  71. void WW_H264VideoServerMediaSubsession::chkForAuxSDPLine1()  
  72. {  
  73.     if (m_pDummyRTPSink->auxSDPLine())  
  74.     {  
  75.         m_done = 0xff;  
  76.     }  
  77.     else  
  78.     {  
  79.         double delay = 1000.0 / (FRAME_PER_SEC);  // ms  
  80.         int to_delay = delay * 1000;  // us  
  81.   
  82.         nextTask() = envir().taskScheduler().scheduleDelayedTask(to_delay, chkForAuxSDPLine, this);  
  83.     }  
  84. }  
#include "WW_H264VideoServerMediaSubsession.h"

WW_H264VideoServerMediaSubsession::WW_H264VideoServerMediaSubsession(UsageEnvironment & env, FramedSource * source) : OnDemandServerMediaSubsession(env, True)
{
	m_pSource = source;
	m_pSDPLine = 0;
}

WW_H264VideoServerMediaSubsession::~WW_H264VideoServerMediaSubsession(void)
{
	if (m_pSDPLine)
	{
		free(m_pSDPLine);
	}
}

WW_H264VideoServerMediaSubsession * WW_H264VideoServerMediaSubsession::createNew(UsageEnvironment & env, FramedSource * source)
{
	return new WW_H264VideoServerMediaSubsession(env, source);
}

FramedSource * WW_H264VideoServerMediaSubsession::createNewStreamSource(unsigned clientSessionId, unsigned & estBitrate)
{
	return H264VideoStreamFramer::createNew(envir(), new WW_H264VideoSource(envir()));
}

RTPSink * WW_H264VideoServerMediaSubsession::createNewRTPSink(Groupsock * rtpGroupsock, unsigned char rtpPayloadTypeIfDynamic, FramedSource * inputSource)
{
	return H264VideoRTPSink::createNew(envir(), rtpGroupsock, rtpPayloadTypeIfDynamic);
}

char const * WW_H264VideoServerMediaSubsession::getAuxSDPLine(RTPSink * rtpSink, FramedSource * inputSource)
{
	if (m_pSDPLine)
	{
		return m_pSDPLine;
	}

	m_pDummyRTPSink = rtpSink;

	//mp_dummy_rtpsink->startPlaying(*source, afterPlayingDummy, this);
	m_pDummyRTPSink->startPlaying(*inputSource, 0, 0);

	chkForAuxSDPLine(this);

	m_done = 0;

	envir().taskScheduler().doEventLoop(&m_done);

	m_pSDPLine = strdup(m_pDummyRTPSink->auxSDPLine());

	m_pDummyRTPSink->stopPlaying();

	return m_pSDPLine;
}

void WW_H264VideoServerMediaSubsession::afterPlayingDummy(void * ptr)
{
	WW_H264VideoServerMediaSubsession * This = (WW_H264VideoServerMediaSubsession *)ptr;

	This->m_done = 0xff;
}

void WW_H264VideoServerMediaSubsession::chkForAuxSDPLine(void * ptr)
{
	WW_H264VideoServerMediaSubsession * This = (WW_H264VideoServerMediaSubsession *)ptr;

	This->chkForAuxSDPLine1();
}

void WW_H264VideoServerMediaSubsession::chkForAuxSDPLine1()
{
	if (m_pDummyRTPSink->auxSDPLine())
	{
		m_done = 0xff;
	}
	else
	{
		double delay = 1000.0 / (FRAME_PER_SEC);  // ms
		int to_delay = delay * 1000;  // us

		nextTask() = envir().taskScheduler().scheduleDelayedTask(to_delay, chkForAuxSDPLine, this);
	}
}

WW_H264VideoSource.h

  1. #ifndef _WW_H264VideoSource_H  
  2. #define _WW_H264VideoSource_H  
  3.   
  4. #include "liveMedia.hh"  
  5. #include "BasicUsageEnvironment.hh"  
  6. #include "GroupsockHelper.hh"  
  7. #include "FramedSource.hh"  
  8.   
  9. #define FRAME_PER_SEC 25  
  10.   
  11. class WW_H264VideoSource : public FramedSource  
  12. {  
  13. public:  
  14.     WW_H264VideoSource(UsageEnvironment & env);  
  15.     ~WW_H264VideoSource(void);  
  16.   
  17. public:  
  18.     virtual void doGetNextFrame();  
  19.     virtual unsigned int maxFrameSize() const;  
  20.   
  21.     static void getNextFrame(void * ptr);  
  22.     void GetFrameData();  
  23.   
  24. private:  
  25.     void *m_pToken;  
  26.     char *m_pFrameBuffer;  
  27.     int  m_hFifo;  
  28. };  
  29.   
  30. #endif  
#ifndef _WW_H264VideoSource_H
#define _WW_H264VideoSource_H

#include "liveMedia.hh"
#include "BasicUsageEnvironment.hh"
#include "GroupsockHelper.hh"
#include "FramedSource.hh"

#define FRAME_PER_SEC 25

class WW_H264VideoSource : public FramedSource
{
public:
	WW_H264VideoSource(UsageEnvironment & env);
	~WW_H264VideoSource(void);

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

	static void getNextFrame(void * ptr);
	void GetFrameData();

private:
	void *m_pToken;
	char *m_pFrameBuffer;
	int  m_hFifo;
};

#endif


WW_H264VideoSource.cpp

  1. #include "WW_H264VideoSource.h"  
  2. #include <stdio.h>  
  3. #ifdef WIN32  
  4. #include <windows.h>  
  5. #else  
  6. #include <sys/types.h>  
  7. #include <sys/stat.h>  
  8. #include <string.h>  
  9. #include <fcntl.h>  
  10. #include <unistd.h>  
  11. #include <limits.h>  
  12. #endif  
  13.   
  14. #define FIFO_NAME     "/tmp/H264_fifo"  
  15. #define BUFFER_SIZE   PIPE_BUF  
  16. #define REV_BUF_SIZE  (1024*1024)  
  17.   
  18. #ifdef WIN32  
  19. #define mSleep(ms)    Sleep(ms)  
  20. #else  
  21. #define mSleep(ms)    usleep(ms*1000)  
  22. #endif  
  23.   
  24.   
  25. WW_H264VideoSource::WW_H264VideoSource(UsageEnvironment & env) :   
  26. FramedSource(env),  
  27. m_pToken(0),  
  28. m_pFrameBuffer(0),  
  29. m_hFifo(0)  
  30. {  
  31.     m_hFifo = open(FIFO_NAME,O_RDONLY);  
  32.         printf("[MEDIA SERVER] open fifo result = [%d]\n",m_hFifo);  
  33.     if(m_hFifo == -1)  
  34.     {  
  35.         return;  
  36.     }  
  37.       
  38.     m_pFrameBuffer = new char[REV_BUF_SIZE];  
  39.     if(m_pFrameBuffer == NULL)  
  40.     {  
  41.         printf("[MEDIA SERVER] error malloc data buffer failed\n");  
  42.         return;  
  43.     }  
  44.     memset(m_pFrameBuffer,0,REV_BUF_SIZE);  
  45. }  
  46.   
  47. WW_H264VideoSource::~WW_H264VideoSource(void)  
  48. {  
  49.     if(m_hFifo)  
  50.     {  
  51.         ::close(m_hFifo);  
  52.     }  
  53.       
  54.     envir().taskScheduler().unscheduleDelayedTask(m_pToken);  
  55.   
  56.     if(m_pFrameBuffer)  
  57.     {  
  58.         delete[] m_pFrameBuffer;  
  59.         m_pFrameBuffer = NULL;  
  60.     }  
  61.   
  62.     printf("[MEDIA SERVER] rtsp connection closed\n");  
  63. }  
  64.   
  65. void WW_H264VideoSource::doGetNextFrame()  
  66. {  
  67.     // 根据 fps,计算等待时间  
  68.     double delay = 1000.0 / (FRAME_PER_SEC * 2);  // ms  
  69.     int to_delay = delay * 1000;  // us  
  70.   
  71.     m_pToken = envir().taskScheduler().scheduleDelayedTask(to_delay, getNextFrame, this);  
  72. }  
  73.   
  74. unsigned int WW_H264VideoSource::maxFrameSize() const  
  75. {  
  76.     return 1024*200;  
  77. }  
  78.   
  79. void WW_H264VideoSource::getNextFrame(void * ptr)  
  80. {  
  81.     ((WW_H264VideoSource *)ptr)->GetFrameData();  
  82. }  
  83.   
  84. void WW_H264VideoSource::GetFrameData()  
  85. {  
  86.     gettimeofday(&fPresentationTime, 0);  
  87.   
  88.     fFrameSize = 0;  
  89.   
  90.     int len = 0;  
  91.     unsigned char buffer[BUFFER_SIZE] = {0};  
  92.     while((len = read(m_hFifo,buffer,BUFFER_SIZE))>0)  
  93.     {  
  94.         memcpy(m_pFrameBuffer+fFrameSize,buffer,len);  
  95.         fFrameSize+=len;  
  96.     }  
  97.     //printf("[MEDIA SERVER] GetFrameData len = [%d],fMaxSize = [%d]\n",fFrameSize,fMaxSize);  
  98.   
  99.     // fill frame data  
  100.     memcpy(fTo,m_pFrameBuffer,fFrameSize);  
  101.   
  102.     if (fFrameSize > fMaxSize)  
  103.     {  
  104.         fNumTruncatedBytes = fFrameSize - fMaxSize;  
  105.         fFrameSize = fMaxSize;  
  106.     }  
  107.     else  
  108.     {  
  109.         fNumTruncatedBytes = 0;  
  110.     }  
  111.                    
  112.     afterGetting(this);  
  113. }  
#include "WW_H264VideoSource.h"
#include <stdio.h>
#ifdef WIN32
#include <windows.h>
#else
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <limits.h>
#endif

#define FIFO_NAME     "/tmp/H264_fifo"
#define BUFFER_SIZE   PIPE_BUF
#define REV_BUF_SIZE  (1024*1024)

#ifdef WIN32
#define mSleep(ms)    Sleep(ms)
#else
#define mSleep(ms)    usleep(ms*1000)
#endif


WW_H264VideoSource::WW_H264VideoSource(UsageEnvironment & env) : 
FramedSource(env),
m_pToken(0),
m_pFrameBuffer(0),
m_hFifo(0)
{
	m_hFifo = open(FIFO_NAME,O_RDONLY);
        printf("[MEDIA SERVER] open fifo result = [%d]\n",m_hFifo);
	if(m_hFifo == -1)
	{
		return;
	}
	
	m_pFrameBuffer = new char[REV_BUF_SIZE];
	if(m_pFrameBuffer == NULL)
	{
		printf("[MEDIA SERVER] error malloc data buffer failed\n");
		return;
	}
	memset(m_pFrameBuffer,0,REV_BUF_SIZE);
}

WW_H264VideoSource::~WW_H264VideoSource(void)
{
	if(m_hFifo)
	{
		::close(m_hFifo);
	}
	
	envir().taskScheduler().unscheduleDelayedTask(m_pToken);

	if(m_pFrameBuffer)
	{
    	delete[] m_pFrameBuffer;
		m_pFrameBuffer = NULL;
	}

	printf("[MEDIA SERVER] rtsp connection closed\n");
}

void WW_H264VideoSource::doGetNextFrame()
{
	// 根据 fps,计算等待时间
	double delay = 1000.0 / (FRAME_PER_SEC * 2);  // ms
	int to_delay = delay * 1000;  // us

	m_pToken = envir().taskScheduler().scheduleDelayedTask(to_delay, getNextFrame, this);
}

unsigned int WW_H264VideoSource::maxFrameSize() const
{
	return 1024*200;
}

void WW_H264VideoSource::getNextFrame(void * ptr)
{
	((WW_H264VideoSource *)ptr)->GetFrameData();
}

void WW_H264VideoSource::GetFrameData()
{
	gettimeofday(&fPresentationTime, 0);

    fFrameSize = 0;

	int len = 0;
	unsigned char buffer[BUFFER_SIZE] = {0};
	while((len = read(m_hFifo,buffer,BUFFER_SIZE))>0)
	{
		memcpy(m_pFrameBuffer+fFrameSize,buffer,len);
		fFrameSize+=len;
	}
	//printf("[MEDIA SERVER] GetFrameData len = [%d],fMaxSize = [%d]\n",fFrameSize,fMaxSize);

	// fill frame data
	memcpy(fTo,m_pFrameBuffer,fFrameSize);

	if (fFrameSize > fMaxSize)
	{
		fNumTruncatedBytes = fFrameSize - fMaxSize;
		fFrameSize = fMaxSize;
	}
	else
	{
		fNumTruncatedBytes = 0;
	}
				 
	afterGetting(this);
}
  1.   
</pre><p></p>修改live555MediaServer.cpp文件如下<p></p><div class="dp-highlighter bg_cpp"><div class="bar"><div class="tools"><strong>[cpp]</strong> <a target=_blank title="view plain" class="ViewSource" href="http://blog.csdn.net/firehood_/article/details/16844397#">view plain</a><a target=_blank title="copy" class="CopyToClipboard" href="http://blog.csdn.net/firehood_/article/details/16844397#">copy</a><a target=_blank title="print" class="PrintSource" href="http://blog.csdn.net/firehood_/article/details/16844397#">print</a><a target=_blank title="?" class="About" href="http://blog.csdn.net/firehood_/article/details/16844397#">?</a><a target=_blank title="在CODE上查看代码片" style="text-indent: 0px;" href="https://code.csdn.net/snippets/74899" target="_blank"><img width="12" height="12" style="left: 2px; top: 1px; position: relative;" alt="在CODE上查看代码片" src="https://code.csdn.net/assets/CODE_ico.png" /></a><a target=_blank title="派生到我的代码片" style="text-indent: 0px;" href="https://code.csdn.net/snippets/74899/fork" target="_blank"><img width="12" height="12" style="left: 2px; top: 2px; position: relative;" alt="派生到我的代码片" src="https://code.csdn.net/assets/ico_fork.svg" /></a></div></div><ol class="dp-cpp"><li class="alt"><span><span class="comment">/**********</span> </span></li><li><span><span class="comment">This library is free software; you can redistribute it and/or modify it under</span> </span></li><li class="alt"><span><span class="comment">the terms of the GNU Lesser General Public License as published by the</span> </span></li><li><span><span class="comment">Free Software Foundation; either version 2.1 of the License, or (at your</span> </span></li><li class="alt"><span><span class="comment">option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.)</span> </span></li><li><span><span class="comment"></span> </span></li><li class="alt"><span><span class="comment">This library is distributed in the hope that it will be useful, but WITHOUT</span> </span></li><li><span><span class="comment">ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS</span> </span></li><li class="alt"><span><span class="comment">FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for</span> </span></li><li><span><span class="comment">more details.</span> </span></li><li class="alt"><span><span class="comment"></span> </span></li><li><span><span class="comment">You should have received a copy of the GNU Lesser General Public License</span> </span></li><li class="alt"><span><span class="comment">along with this library; if not, write to the Free Software Foundation, Inc.,</span> </span></li><li><span><span class="comment">51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA</span> </span></li><li class="alt"><span><span class="comment">**********/</span><span>  </span></span></li><li><span><span class="comment">// Copyright (c) 1996-2013, Live Networks, Inc.  All rights reserved</span><span>  </span></span></li><li class="alt"><span><span class="comment">// LIVE555 Media Server</span><span>  </span></span></li><li><span><span class="comment">// main program</span><span>  </span></span></li><li class="alt"><span>  </span></li><li><span><span class="preprocessor">#include <BasicUsageEnvironment.hh></span><span>  </span></span></li><li class="alt"><span><span class="preprocessor">#include "DynamicRTSPServer.hh"</span><span>  </span></span></li><li><span><span class="preprocessor">#include "version.hh"</span><span>  </span></span></li><li class="alt"><span><span class="preprocessor">#include "WW_H264VideoSource.h"</span><span>  </span></span></li><li><span><span class="preprocessor">#include "WW_H264VideoServerMediaSubsession.h"</span><span>  </span></span></li><li class="alt"><span>  </span></li><li><span><span class="datatypes">int</span><span> main(</span><span class="datatypes">int</span><span> argc, </span><span class="datatypes">char</span><span>** argv) {  </span></span></li><li class="alt"><span>    <span class="comment">// Begin by setting up our usage environment:</span><span>  </span></span></li><li><span>    TaskScheduler* scheduler = BasicTaskScheduler::createNew();  </span></li><li class="alt"><span>    UsageEnvironment* env = BasicUsageEnvironment::createNew(*scheduler);  </span></li><li><span>  </span></li><li class="alt"><span>    UserAuthenticationDatabase* authDB = NULL;  </span></li><li><span><span class="preprocessor">#ifdef ACCESS_CONTROL</span><span>  </span></span></li><li class="alt"><span>    <span class="comment">// To implement client access control to the RTSP server, do the following:</span><span>  </span></span></li><li><span>    authDB = <span class="keyword">new</span><span> UserAuthenticationDatabase;  </span></span></li><li class="alt"><span>    authDB->addUserRecord(<span class="string">"username1"</span><span>, </span><span class="string">"password1"</span><span>); </span><span class="comment">// replace these with real strings</span><span>  </span></span></li><li><span>    <span class="comment">// Repeat the above with each <username>, <password> that you wish to allow</span><span>  </span></span></li><li class="alt"><span>    <span class="comment">// access to the server.</span><span>  </span></span></li><li><span><span class="preprocessor">#endif</span><span>  </span></span></li><li class="alt"><span>  </span></li><li><span>    <span class="comment">// Create the RTSP server:</span><span>  </span></span></li><li class="alt"><span>    RTSPServer* rtspServer = RTSPServer::createNew(*env, 554, authDB);  </span></li><li><span>    <span class="keyword">if</span><span> (rtspServer == NULL) {  </span></span></li><li class="alt"><span>        *env << <span class="string">"Failed to create RTSP server: "</span><span> << env->getResultMsg() << </span><span class="string">"\n"</span><span>;  </span></span></li><li><span>        exit(1);  </span></li><li class="alt"><span>    }  </span></li><li><span>  </span></li><li class="alt"><span>    <span class="comment">// Add live stream</span><span>  </span></span></li><li><span>  </span></li><li class="alt"><span>    WW_H264VideoSource * videoSource = 0;  </span></li><li><span>  </span></li><li class="alt"><span>    ServerMediaSession * sms = ServerMediaSession::createNew(*env, <span class="string">"live"</span><span>, 0, </span><span class="string">"ww live test"</span><span>);  </span></span></li><li><span>    sms->addSubsession(WW_H264VideoServerMediaSubsession::createNew(*env, videoSource));  </span></li><li class="alt"><span>    rtspServer->addServerMediaSession(sms);  </span></li><li><span>  </span></li><li class="alt"><span>    <span class="datatypes">char</span><span> * url = rtspServer->rtspURL(sms);  </span></span></li><li><span>    *env << <span class="string">"using url \""</span><span> << url << </span><span class="string">"\"\n"</span><span>;  </span></span></li><li class="alt"><span>    <span class="keyword">delete</span><span>[] url;  </span></span></li><li><span>  </span></li><li class="alt"><span>    <span class="comment">// Run loop</span><span>  </span></span></li><li><span>    env->taskScheduler().doEventLoop();  </span></li><li class="alt"><span>  </span></li><li><span>    rtspServer->removeServerMediaSession(sms);  </span></li><li class="alt"><span>  </span></li><li><span>    Medium::close(rtspServer);  </span></li><li class="alt"><span>  </span></li><li><span>    env->reclaim();  </span></li><li class="alt"><span>  </span></li><li><span>    <span class="keyword">delete</span><span> scheduler;  </span></span></li><li class="alt"><span>  </span></li><li><span>    <span class="keyword">return</span><span> 1;  </span></span></li><li class="alt"><span>}  </span></li></ol></div><pre class="cpp" style="display: none;" name="code" code_snippet_id="74899" snippet_file_name="blog_20131120_5_3071795">/**********
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
// LIVE555 Media Server
// main program

#include <BasicUsageEnvironment.hh>
#include "DynamicRTSPServer.hh"
#include "version.hh"
#include "WW_H264VideoSource.h"
#include "WW_H264VideoServerMediaSubsession.h"

int main(int argc, char** argv) {
	// Begin by setting up our usage environment:
	TaskScheduler* scheduler = BasicTaskScheduler::createNew();
	UsageEnvironment* env = BasicUsageEnvironment::createNew(*scheduler);

	UserAuthenticationDatabase* authDB = NULL;
#ifdef ACCESS_CONTROL
	// To implement client access control to the RTSP server, do the following:
	authDB = new UserAuthenticationDatabase;
	authDB->addUserRecord("username1", "password1"); // replace these with real strings
	// Repeat the above with each <username>, <password> that you wish to allow
	// access to the server.
#endif

	// Create the RTSP server:
	RTSPServer* rtspServer = RTSPServer::createNew(*env, 554, authDB);
	if (rtspServer == NULL) {
		*env << "Failed to create RTSP server: " << env->getResultMsg() << "\n";
		exit(1);
	}

	// Add live stream

	WW_H264VideoSource * videoSource = 0;

	ServerMediaSession * sms = ServerMediaSession::createNew(*env, "live", 0, "ww live test");
	sms->addSubsession(WW_H264VideoServerMediaSubsession::createNew(*env, videoSource));
	rtspServer->addServerMediaSession(sms);

	char * url = rtspServer->rtspURL(sms);
	*env << "using url \"" << url << "\"\n";
	delete[] url;

	// Run loop
	env->taskScheduler().doEventLoop();

	rtspServer->removeServerMediaSession(sms);

	Medium::close(rtspServer);

	env->reclaim();

	delete scheduler;

	return 1;
}

发送H264视频流的RTSPStream

  1. /********************************************************************  
  2. filename:   RTSPStream.h 
  3. created:    2013-08-01 
  4. author:     firehood  
  5. purpose:    通过live555实现H264 RTSP直播 
  6. *********************************************************************/   
  7. #pragma once  
  8. #include <stdio.h>  
  9. #ifdef WIN32  
  10. #include <windows.h>  
  11. #else  
  12. #include <pthread.h>  
  13. #endif  
  14.   
  15. #ifdef WIN32  
  16. typedef HANDLE       ThreadHandle;  
  17. #define mSleep(ms)   Sleep(ms)  
  18. #else  
  19. typedef unsigned int SOCKET;  
  20. typedef pthread_t    ThreadHandle;  
  21. #define mSleep(ms)   usleep(ms*1000)  
  22. #endif  
  23.   
  24. #define FILEBUFSIZE (1024 * 1024)   
  25.   
  26.   
  27. class CRTSPStream  
  28. {  
  29. public:  
  30.     CRTSPStream(void);  
  31.     ~CRTSPStream(void);  
  32. public:  
  33.     // 初始化  
  34.     bool Init();  
  35.     // 卸载  
  36.     void Uninit();  
  37.     // 发送H264文件  
  38.     bool SendH264File(const char *pFileName);  
  39.     // 发送H264数据帧  
  40.     int SendH264Data(const unsigned char *data,unsigned int size);  
  41. };  
/******************************************************************** 
filename:   RTSPStream.h
created:    2013-08-01
author:     firehood 
purpose:    通过live555实现H264 RTSP直播
*********************************************************************/ 
#pragma once
#include <stdio.h>
#ifdef WIN32
#include <windows.h>
#else
#include <pthread.h>
#endif

#ifdef WIN32
typedef HANDLE       ThreadHandle;
#define mSleep(ms)   Sleep(ms)
#else
typedef unsigned int SOCKET;
typedef pthread_t    ThreadHandle;
#define mSleep(ms)   usleep(ms*1000)
#endif

#define FILEBUFSIZE (1024 * 1024) 


class CRTSPStream
{
public:
	CRTSPStream(void);
	~CRTSPStream(void);
public:
	// 初始化
	bool Init();
    // 卸载
	void Uninit();
	// 发送H264文件
	bool SendH264File(const char *pFileName);
	// 发送H264数据帧
    int SendH264Data(const unsigned char *data,unsigned int size);
};

  1. /********************************************************************  
  2. filename:   RTSPStream.cpp 
  3. created:    2013-08-01 
  4. author:     firehood  
  5. purpose:    通过live555实现H264 RTSP直播 
  6. *********************************************************************/   
  7. #include "RTSPStream.h"  
  8. #ifdef WIN32  
  9. #else  
  10. #include <sys/types.h>  
  11. #include <sys/stat.h>  
  12. #include <string.h>  
  13. #include <fcntl.h>  
  14. #include <unistd.h>  
  15. #include <limits.h>  
  16. #include <errno.h>  
  17. #endif  
  18.   
  19. #define FIFO_NAME    "/tmp/H264_fifo"  
  20. #define BUFFERSIZE   PIPE_BUF  
  21.   
  22. CRTSPStream::CRTSPStream(void)  
  23. {  
  24.       
  25. }  
  26.   
  27. CRTSPStream::~CRTSPStream(void)  
  28. {  
  29.       
  30. }  
  31.   
  32. bool CRTSPStream::Init()  
  33. {  
  34.     if(access(FIFO_NAME,F_OK) == -1)  
  35.     {  
  36.         int res = mkfifo(FIFO_NAME,0777);  
  37.         if(res != 0)  
  38.         {  
  39.             printf("[RTSPStream] Create fifo failed.\n");  
  40.             return false;  
  41.         }  
  42.     }  
  43.     return true;  
  44. }  
  45.   
  46.   
  47. void CRTSPStream::Uninit()  
  48. {  
  49.       
  50. }  
  51.   
  52. bool CRTSPStream::SendH264File(const char *pFileName)  
  53. {  
  54.     if(pFileName == NULL)  
  55.     {  
  56.         return false;  
  57.     }  
  58.     FILE *fp = fopen(pFileName, "rb");    
  59.     if(!fp)    
  60.     {    
  61.         printf("[RTSPStream] error:open file %s failed!",pFileName);  
  62.     }    
  63.     fseek(fp, 0, SEEK_SET);  
  64.   
  65.     unsigned char *buffer  = new unsigned char[FILEBUFSIZE];  
  66.     int pos = 0;  
  67.     while(1)  
  68.     {  
  69.         int readlen = fread(buffer+pos, sizeof(unsigned char), FILEBUFSIZE-pos, fp);  
  70.   
  71.         if(readlen<=0)  
  72.         {  
  73.             break;  
  74.         }  
  75.   
  76.         readlen+=pos;  
  77.   
  78.         int writelen = SendH264Data(buffer,readlen);  
  79.         if(writelen<=0)  
  80.         {  
  81.             break;  
  82.         }  
  83.         memcpy(buffer,buffer+writelen,readlen-writelen);  
  84.         pos = readlen-writelen;  
  85.   
  86.         mSleep(25);  
  87.     }  
  88.     fclose(fp);  
  89.     delete[] buffer;  
  90.     return true;  
  91. }  
  92.   
  93. // 发送H264数据帧  
  94. int CRTSPStream::SendH264Data(const unsigned char *data,unsigned int size)  
  95. {  
  96.     if(data == NULL)  
  97.     {  
  98.         return 0;  
  99.     }  
  100.     // open pipe with non_block mode  
  101.     int pipe_fd = open(FIFO_NAME, O_WRONLY|O_NONBLOCK);  
  102.     //printf("[RTSPStream] open fifo result = [%d]\n",pipe_fd);  
  103.     if(pipe_fd == -1)  
  104.     {  
  105.         return 0;  
  106.     }  
  107.    
  108.     int send_size = 0;  
  109.     int remain_size = size;  
  110.     while(send_size < size)  
  111.     {  
  112.         int data_len = (remain_size<BUFFERSIZE) ? remain_size : BUFFERSIZE;  
  113.         int len = write(pipe_fd,data+send_size,data_len);  
  114.         if(len == -1)  
  115.         {  
  116.             static int resend_conut = 0;  
  117.             if(errno == EAGAIN && ++resend_conut<=3)  
  118.             {  
  119.                 printf("[RTSPStream] write fifo error,resend..\n");  
  120.                 continue;  
  121.             }  
  122.             resend_conut = 0;  
  123.             printf("[RTSPStream] write fifo error,errorcode[%d],send_size[%d]\n",errno,send_size);  
  124.             break;  
  125.         }  
  126.         else  
  127.         {    
  128.             send_size+= len;  
  129.             remain_size-= len;  
  130.         }  
  131.     }  
  132.     close(pipe_fd);  
  133.     //printf("[RTSPStream] SendH264Data datalen[%d], sendsize = [%d]\n",size,send_size);  
  134.     return 0;  
  135. }  
/******************************************************************** 
filename:   RTSPStream.cpp
created:    2013-08-01
author:     firehood 
purpose:    通过live555实现H264 RTSP直播
*********************************************************************/ 
#include "RTSPStream.h"
#ifdef WIN32
#else
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <limits.h>
#include <errno.h>
#endif

#define FIFO_NAME    "/tmp/H264_fifo"
#define BUFFERSIZE   PIPE_BUF

CRTSPStream::CRTSPStream(void)
{
	
}

CRTSPStream::~CRTSPStream(void)
{
	
}

bool CRTSPStream::Init()
{
	if(access(FIFO_NAME,F_OK) == -1)
	{
		int res = mkfifo(FIFO_NAME,0777);
		if(res != 0)
		{
			printf("[RTSPStream] Create fifo failed.\n");
			return false;
		}
	}
	return true;
}


void CRTSPStream::Uninit()
{
	
}

bool CRTSPStream::SendH264File(const char *pFileName)
{
	if(pFileName == NULL)
	{
		return false;
	}
	FILE *fp = fopen(pFileName, "rb");  
	if(!fp)  
	{  
		printf("[RTSPStream] error:open file %s failed!",pFileName);
	}  
	fseek(fp, 0, SEEK_SET);

	unsigned char *buffer  = new unsigned char[FILEBUFSIZE];
	int pos = 0;
	while(1)
	{
		int readlen = fread(buffer+pos, sizeof(unsigned char), FILEBUFSIZE-pos, fp);

		if(readlen<=0)
		{
			break;
		}

		readlen+=pos;

		int writelen = SendH264Data(buffer,readlen);
		if(writelen<=0)
		{
			break;
		}
		memcpy(buffer,buffer+writelen,readlen-writelen);
		pos = readlen-writelen;

		mSleep(25);
	}
	fclose(fp);
	delete[] buffer;
	return true;
}

// 发送H264数据帧
int CRTSPStream::SendH264Data(const unsigned char *data,unsigned int size)
{
	if(data == NULL)
	{
		return 0;
	}
	// open pipe with non_block mode
	int pipe_fd = open(FIFO_NAME, O_WRONLY|O_NONBLOCK);
	//printf("[RTSPStream] open fifo result = [%d]\n",pipe_fd);
	if(pipe_fd == -1)
	{
		return 0;
	}
 
	int send_size = 0;
	int remain_size = size;
	while(send_size < size)
	{
		int data_len = (remain_size<BUFFERSIZE) ? remain_size : BUFFERSIZE;
		int len = write(pipe_fd,data+send_size,data_len);
		if(len == -1)
		{
			static int resend_conut = 0;
			if(errno == EAGAIN && ++resend_conut<=3)
			{
				printf("[RTSPStream] write fifo error,resend..\n");
				continue;
			}
			resend_conut = 0;
			printf("[RTSPStream] write fifo error,errorcode[%d],send_size[%d]\n",errno,send_size);
			break;
		}
		else
		{  
			send_size+= len;
			remain_size-= len;
		}
	}
	close(pipe_fd);
	//printf("[RTSPStream] SendH264Data datalen[%d], sendsize = [%d]\n",size,send_size);
	return 0;
}

测试程序代码

  1. #include <stdio.h>  
  2. #include "RTSPStream.h"  
  3.   
  4. int main(int argc,char* argv[])  
  5. {  
  6.     CRTSPStream rtspSender;  
  7.     bool bRet = rtspSender.Init();  
  8.     rtspSender.SendH264File("E:\\测试视频\\test.264");  
  9.     system("pause");    
  10. }  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值