live555学习3(转)

九 h264 RTP传输详解(1)


前几章对Server端的介绍中有个比较重要的问题没有仔细探究:如何打开文件并获得其SDP信息。我们就从这里入手吧。


当RTSPServer收到对某个媒体的DESCRIBE请求时,它会找到对应的ServerMediaSession,调用ServerMediaSession::generateSDPDescription()。generateSDPDescription()中会遍历调用ServerMediaSession中所有的调用ServerMediaSubsession,通过subsession->sdpLines()取得每个Subsession的sdp,合并成一个完整的SDP返回之。
我们几乎可以断定,文件的打开和分析应该是在每个Subsession的sdpLines()函数中完成的,看看这个函数:

  1. char const* OnDemandServerMediaSubsession::sdpLines()  
  2. {  
  3.     if (fSDPLines == NULL) {  
  4.         // We need to construct a set of SDP lines that describe this  
  5.         // subsession (as a unicast stream).  To do so, we first create  
  6.         // dummy (unused) source and "RTPSink" objects,  
  7.         // whose parameters we use for the SDP lines:  
  8.         unsigned estBitrate;  
  9.         FramedSource* inputSource = createNewStreamSource(0, estBitrate);  
  10.         if (inputSource == NULL)  
  11.             return NULL; // file not found  
  12.   
  13.         struct in_addr dummyAddr;  
  14.         dummyAddr.s_addr = 0;  
  15.         Groupsock dummyGroupsock(envir(), dummyAddr, 0, 0);  
  16.         unsigned char rtpPayloadType = 96 + trackNumber() - 1; // if dynamic  
  17.         RTPSink* dummyRTPSink = createNewRTPSink(&dummyGroupsock,  
  18.                 rtpPayloadType, inputSource);  
  19.   
  20.         setSDPLinesFromRTPSink(dummyRTPSink, inputSource, estBitrate);  
  21.         Medium::close(dummyRTPSink);  
  22.         closeStreamSource(inputSource);  
  23.     }  
  24.   
  25.     return fSDPLines;  
  26. }  
char const* OnDemandServerMediaSubsession::sdpLines()
{
	if (fSDPLines == NULL) {
		// We need to construct a set of SDP lines that describe this
		// subsession (as a unicast stream).  To do so, we first create
		// dummy (unused) source and "RTPSink" objects,
		// whose parameters we use for the SDP lines:
		unsigned estBitrate;
		FramedSource* inputSource = createNewStreamSource(0, estBitrate);
		if (inputSource == NULL)
			return NULL; // file not found

		struct in_addr dummyAddr;
		dummyAddr.s_addr = 0;
		Groupsock dummyGroupsock(envir(), dummyAddr, 0, 0);
		unsigned char rtpPayloadType = 96 + trackNumber() - 1; // if dynamic
		RTPSink* dummyRTPSink = createNewRTPSink(&dummyGroupsock,
				rtpPayloadType, inputSource);

		setSDPLinesFromRTPSink(dummyRTPSink, inputSource, estBitrate);
		Medium::close(dummyRTPSink);
		closeStreamSource(inputSource);
	}

	return fSDPLines;
}
其所为如是:Subsession中直接保存了对应媒体文件的SDP,但是在第一次获取时fSDPLines为NULL,所以需先获取fSDPLines。其做法比较费事,竟然是建了临时的Source和RTPSink,把它们连接成一个StreamToken,Playing一段时间之后才取得了fSDPLines。createNewStreamSource()和createNewRTPSink()都是虚函数,所以此处创建的source和sink都是继承类指定的,我们分析的是H264,也就是H264VideoFileServerMediaSubsession所指定的,来看一下这两个函数:

  1. FramedSource* H264VideoFileServerMediaSubsession::createNewStreamSource(  
  2.         unsigned /*clientSessionId*/,  
  3.         unsigned& estBitrate)  
  4. {  
  5.     estBitrate = 500; // kbps, estimate  
  6.   
  7.     // Create the video source:  
  8.     ByteStreamFileSource* fileSource = ByteStreamFileSource::createNew(envir(),  
  9.             fFileName);  
  10.     if (fileSource == NULL)  
  11.         return NULL;  
  12.     fFileSize = fileSource->fileSize();  
  13.   
  14.     // Create a framer for the Video Elementary Stream:  
  15.     return H264VideoStreamFramer::createNew(envir(), fileSource);  
  16. }  
  17.   
  18. RTPSink* H264VideoFileServerMediaSubsession::createNewRTPSink(  
  19.         Groupsock* rtpGroupsock,  
  20.         unsigned char rtpPayloadTypeIfDynamic,  
  21.         FramedSource* /*inputSource*/)  
  22. {  
  23.     return H264VideoRTPSink::createNew(envir(), rtpGroupsock,  
  24.             rtpPayloadTypeIfDynamic);  
  25. }  
FramedSource* H264VideoFileServerMediaSubsession::createNewStreamSource(
		unsigned /*clientSessionId*/,
		unsigned& estBitrate)
{
	estBitrate = 500; // kbps, estimate

	// Create the video source:
	ByteStreamFileSource* fileSource = ByteStreamFileSource::createNew(envir(),
			fFileName);
	if (fileSource == NULL)
		return NULL;
	fFileSize = fileSource->fileSize();

	// Create a framer for the Video Elementary Stream:
	return H264VideoStreamFramer::createNew(envir(), fileSource);
}

RTPSink* H264VideoFileServerMediaSubsession::createNewRTPSink(
		Groupsock* rtpGroupsock,
		unsigned char rtpPayloadTypeIfDynamic,
		FramedSource* /*inputSource*/)
{
	return H264VideoRTPSink::createNew(envir(), rtpGroupsock,
			rtpPayloadTypeIfDynamic);
}
可以看到,分别创建了H264VideoStreamFramer和H264VideoRTPSink。可以肯定H264VideoStreamFramer也是一个Source,但它内部又利用了另一个source--ByteStreamFileSource。后面会分析为什么要这样做,这里先不要管它。还没有看到真正打开文件的代码,继续探索:

  1. void OnDemandServerMediaSubsession::setSDPLinesFromRTPSink(  
  2.         RTPSink* rtpSink,  
  3.         FramedSource* inputSource,  
  4.         unsigned estBitrate)  
  5. {  
  6.     if (rtpSink == NULL)  
  7.         return;  
  8.   
  9.     char const* mediaType = rtpSink->sdpMediaType();  
  10.     unsigned char rtpPayloadType = rtpSink->rtpPayloadType();  
  11.     struct in_addr serverAddrForSDP;  
  12.     serverAddrForSDP.s_addr = fServerAddressForSDP;  
  13.     charconst ipAddressStr = strDup(our_inet_ntoa(serverAddrForSDP));  
  14.     char* rtpmapLine = rtpSink->rtpmapLine();  
  15.     char const* rangeLine = rangeSDPLine();  
  16.     char const* auxSDPLine = getAuxSDPLine(rtpSink, inputSource);  
  17.     if (auxSDPLine == NULL)  
  18.         auxSDPLine = "";  
  19.   
  20.     char constconst sdpFmt = "m=%s %u RTP/AVP %d\r\n"  
  21.             "c=IN IP4 %s\r\n"  
  22.             "b=AS:%u\r\n"  
  23.             "%s"  
  24.             "%s"  
  25.             "%s"  
  26.             "a=control:%s\r\n";  
  27.     unsigned sdpFmtSize = strlen(sdpFmt) + strlen(mediaType) + 5 /* max short len */  
  28.     + 3 /* max char len */  
  29.     + strlen(ipAddressStr) + 20 /* max int len */  
  30.     + strlen(rtpmapLine) + strlen(rangeLine) + strlen(auxSDPLine)  
  31.             + strlen(trackId());  
  32.     char* sdpLines = new char[sdpFmtSize];  
  33.     sprintf(sdpLines, sdpFmt, mediaType, // m= <media>  
  34.             fPortNumForSDP, // m= <port>   
  35.             rtpPayloadType, // m= <fmt list>  
  36.             ipAddressStr, // c= address   
  37.             estBitrate, // b=AS:<bandwidth>  
  38.             rtpmapLine, // a=rtpmap:... (if present)  
  39.             rangeLine, // a=range:... (if present)  
  40.             auxSDPLine, // optional extra SDP line  
  41.             trackId()); // a=control:<track-id>  
  42.     delete[] (char*) rangeLine;  
  43.     delete[] rtpmapLine;  
  44.     delete[] ipAddressStr;  
  45.   
  46.     fSDPLines = strDup(sdpLines);  
  47.     delete[] sdpLines;  
  48. }  
void OnDemandServerMediaSubsession::setSDPLinesFromRTPSink(
		RTPSink* rtpSink,
		FramedSource* inputSource,
		unsigned estBitrate)
{
	if (rtpSink == NULL)
		return;

	char const* mediaType = rtpSink->sdpMediaType();
	unsigned char rtpPayloadType = rtpSink->rtpPayloadType();
	struct in_addr serverAddrForSDP;
	serverAddrForSDP.s_addr = fServerAddressForSDP;
	char* const ipAddressStr = strDup(our_inet_ntoa(serverAddrForSDP));
	char* rtpmapLine = rtpSink->rtpmapLine();
	char const* rangeLine = rangeSDPLine();
	char const* auxSDPLine = getAuxSDPLine(rtpSink, inputSource);
	if (auxSDPLine == NULL)
		auxSDPLine = "";

	char const* const sdpFmt = "m=%s %u RTP/AVP %d\r\n"
			"c=IN IP4 %s\r\n"
			"b=AS:%u\r\n"
			"%s"
			"%s"
			"%s"
			"a=control:%s\r\n";
	unsigned sdpFmtSize = strlen(sdpFmt) + strlen(mediaType) + 5 /* max short len */
	+ 3 /* max char len */
	+ strlen(ipAddressStr) + 20 /* max int len */
	+ strlen(rtpmapLine) + strlen(rangeLine) + strlen(auxSDPLine)
			+ strlen(trackId());
	char* sdpLines = new char[sdpFmtSize];
	sprintf(sdpLines, sdpFmt, mediaType, // m= <media>
			fPortNumForSDP, // m= <port>
			rtpPayloadType, // m= <fmt list>
			ipAddressStr, // c= address
			estBitrate, // b=AS:<bandwidth>
			rtpmapLine, // a=rtpmap:... (if present)
			rangeLine, // a=range:... (if present)
			auxSDPLine, // optional extra SDP line
			trackId()); // a=control:<track-id>
	delete[] (char*) rangeLine;
	delete[] rtpmapLine;
	delete[] ipAddressStr;

	fSDPLines = strDup(sdpLines);
	delete[] sdpLines;
}
此函数中取得Subsession的sdp并保存到fSDPLines。打开文件应在rtpSink->rtpmapLine()甚至是Source创建时已经做了。我们不防先把它放一放,而是先把SDP的获取过程搞个通透。所以把焦点集中到getAuxSDPLine()上。

  1. char const* OnDemandServerMediaSubsession::getAuxSDPLine(  
  2.         RTPSink* rtpSink,  
  3.         FramedSource* /*inputSource*/)  
  4. {  
  5.     // Default implementation:  
  6.     return rtpSink == NULL ? NULL : rtpSink->auxSDPLine();  
  7. }  
char const* OnDemandServerMediaSubsession::getAuxSDPLine(
		RTPSink* rtpSink,
		FramedSource* /*inputSource*/)
{
	// Default implementation:
	return rtpSink == NULL ? NULL : rtpSink->auxSDPLine();
}
很简单,调用了rtpSink->auxSDPLine()那么我们要看H264VideoRTPSink::auxSDPLine():不用看了,很简单,取得source 中保存的PPS,SPS等形成a=fmpt行。但事实上并没有这么简单,H264VideoFileServerMediaSubsession重写了getAuxSDPLine()!如果不重写,则说明auxSDPLine已经在前面分析文件时获得了,那么既然重写,就说明前面没有获取到,只能在这个函数中重写。look H264VideoFileServerMediaSubsession中这个函数:

  1. char const* H264VideoFileServerMediaSubsession::getAuxSDPLine(  
  2.         RTPSink* rtpSink,  
  3.         FramedSource* inputSource)  
  4. {  
  5.     if (fAuxSDPLine != NULL)  
  6.         return fAuxSDPLine; // it's already been set up (for a previous client)  
  7.   
  8.     if (fDummyRTPSink == NULL) { // we're not already setting it up for another, concurrent stream  
  9.         // Note: For H264 video files, the 'config' information ("profile-level-id" and "sprop-parameter-sets") isn't known  
  10.         // until we start reading the file.  This means that "rtpSink"s "auxSDPLine()" will be NULL initially,  
  11.         // and we need to start reading data from our file until this changes.  
  12.         fDummyRTPSink = rtpSink;  
  13.   
  14.         // Start reading the file:   
  15.         fDummyRTPSink->startPlaying(*inputSource, afterPlayingDummy, this);  
  16.   
  17.         // Check whether the sink's 'auxSDPLine()' is ready:  
  18.         checkForAuxSDPLine(this);  
  19.     }  
  20.   
  21.     envir().taskScheduler().doEventLoop(&fDoneFlag);  
  22.   
  23.     return fAuxSDPLine;  
  24. }  
char const* H264VideoFileServerMediaSubsession::getAuxSDPLine(
		RTPSink* rtpSink,
		FramedSource* inputSource)
{
	if (fAuxSDPLine != NULL)
		return fAuxSDPLine; // it's already been set up (for a previous client)

	if (fDummyRTPSink == NULL) { // we're not already setting it up for another, concurrent stream
		// Note: For H264 video files, the 'config' information ("profile-level-id" and "sprop-parameter-sets") isn't known
		// until we start reading the file.  This means that "rtpSink"s "auxSDPLine()" will be NULL initially,
		// and we need to start reading data from our file until this changes.
		fDummyRTPSink = rtpSink;

		// Start reading the file:
		fDummyRTPSink->startPlaying(*inputSource, afterPlayingDummy, this);

		// Check whether the sink's 'auxSDPLine()' is ready:
		checkForAuxSDPLine(this);
	}

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

	return fAuxSDPLine;
}
注释里面解释得很清楚,H264不能在文件头中取得PPS/SPS,必须在播放一下后(当然,它是一个原始流文件,没有文件头)才行。也就是说不能从rtpSink中取得了。为了保证在函数退出前能取得AuxSDP,把大循环搬到这里来了。afterPlayingDummy()是在播放结束也就是取得aux sdp之后执行。在大循环之前的checkForAuxSDPLine()做了什么呢?

  1. void H264VideoFileServerMediaSubsession::checkForAuxSDPLine1()  
  2. {  
  3.     char const* dasl;  
  4.   
  5.     if (fAuxSDPLine != NULL) {  
  6.         // Signal the event loop that we're done:  
  7.         setDoneFlag();  
  8.     } else if (fDummyRTPSink != NULL  
  9.             && (dasl = fDummyRTPSink->auxSDPLine()) != NULL) {  
  10.         fAuxSDPLine = strDup(dasl);  
  11.         fDummyRTPSink = NULL;  
  12.   
  13.         // Signal the event loop that we're done:  
  14.         setDoneFlag();  
  15.     } else {  
  16.         // try again after a brief delay:   
  17.         int uSecsToDelay = 100000; // 100 ms  
  18.         nextTask() = envir().taskScheduler().scheduleDelayedTask(uSecsToDelay,  
  19.                 (TaskFunc*) checkForAuxSDPLine, this);  
  20.     }  
  21. }  
void H264VideoFileServerMediaSubsession::checkForAuxSDPLine1()
{
	char const* dasl;

	if (fAuxSDPLine != NULL) {
		// Signal the event loop that we're done:
		setDoneFlag();
	} else if (fDummyRTPSink != NULL
			&& (dasl = fDummyRTPSink->auxSDPLine()) != NULL) {
		fAuxSDPLine = strDup(dasl);
		fDummyRTPSink = NULL;

		// Signal the event loop that we're done:
		setDoneFlag();
	} else {
		// try again after a brief delay:
		int uSecsToDelay = 100000; // 100 ms
		nextTask() = envir().taskScheduler().scheduleDelayedTask(uSecsToDelay,
				(TaskFunc*) checkForAuxSDPLine, this);
	}
}

它检查是否已取得Aux sdp,如果取得了,设置结束标志,直接返回。如果没有,就检查是否sink中已取得了aux sdp,如果是,也设置结束标志,返回。如果还没有取得,则把这个检查函数做为delay task加入计划任务中。每100毫秒检查一次,每检查一次主要就是调用一次fDummyRTPSink->auxSDPLine()。大循环在检测到fDoneFlag改变时停止,此时已取得了aux sdp。但是如果直到文件结束也没有得到aux sdp,则afterPlayingDummy()被执行,在其中停止掉这个大循环。然后在父Subsession类中关掉这些临时的source和sink。在直正播放时重新创建。

 

 

 

十 h264 RTP传输详解(2)


上一章并没有把打开文件分析文件的代码找到,因为发现它隐藏得比较深,而且H264的Source又有多个,形成了连环计。所以此章中就将文件处理与H264的Source们并在一起分析吧。
从哪里开始呢?从source开始吧!为什么要从它开始呢?我就想从这里开始,行了吧?

  1. FramedSource* H264VideoFileServerMediaSubsession::createNewStreamSource(  
  2.         unsigned /*clientSessionId*/,  
  3.         unsigned& estBitrate)  
  4. {  
  5.     estBitrate = 500; // kbps, estimate   
  6.   
  7.     // Create the video source:   
  8.     ByteStreamFileSource* fileSource = ByteStreamFileSource::createNew(envir(),  
  9.             fFileName);  
  10.     if (fileSource == NULL)  
  11.         return NULL;  
  12.     fFileSize = fileSource->fileSize();  
  13.   
  14.     // Create a framer for the Video Elementary Stream:   
  15.     return H264VideoStreamFramer::createNew(envir(), fileSource);  
  16. }  
FramedSource* H264VideoFileServerMediaSubsession::createNewStreamSource(
		unsigned /*clientSessionId*/,
		unsigned& estBitrate)
{
	estBitrate = 500; // kbps, estimate

	// Create the video source:
	ByteStreamFileSource* fileSource = ByteStreamFileSource::createNew(envir(),
			fFileName);
	if (fileSource == NULL)
		return NULL;
	fFileSize = fileSource->fileSize();

	// Create a framer for the Video Elementary Stream:
	return H264VideoStreamFramer::createNew(envir(), fileSource);
}

先创建一个ByteStreamFileSource,显然这是一个从文件按字节读取数据的source,没什么可细说的。但是,打开文件,读写文件操作的确就在其中。最终来处理h264文件,分析其格式,解析出帧或nal的应是这个source: H264VideoStreamFramer。打开文件的地方找到了,但分析文件的代码才是更有价值的。那我们只能来看H264VideoStreamFramer。
H264VideoStreamFramer继承自MPEGVideoStreamFramer,MPEGVideoStreamFramer继承自FramedFilter,FramedFilter继承自FramedSource。
啊,中间又冒出个Filter。看到它,是不是联想到了DirectShow的filter?或者说Photoshop中的filter?它们的意义应该都差不多吧?即插入到source和render(sink)之间的处理媒体数据的东东?如果这样理解,还是更接近于photoshop中的概念。唉,说实话,我估计自己说的也不全对,反正就这样认识吧,谬不了一千里。既然我们这样认识了,那么我们就有理由相信可能会出现多个filter们一个连一个,然后高唱:手牵着脚脚牵着手一起向前走...
H264VideoStreamFramer继承自MPEGVideoStreamFramer,MPEGVideoStreamFramer比较简单,它只是把一些工作交给了MPEGVideoStreamParser(又出来个parser,这可是个新东西哦,先不要管它吧),重点来看一下。
构造函数:

  1. H264VideoStreamFramer::H264VideoStreamFramer(UsageEnvironment& env,  
  2.         FramedSource* inputSource,  
  3.         Boolean createParser,  
  4.         Boolean includeStartCodeInOutput)  
  5.         : MPEGVideoStreamFramer(env, inputSource),  
  6.                 fIncludeStartCodeInOutput(includeStartCodeInOutput),  
  7.                 fLastSeenSPS(NULL),  
  8.                 fLastSeenSPSSize(0),  
  9.                 fLastSeenPPS(NULL),  
  10.                 fLastSeenPPSSize(0)  
  11. {  
  12.     fParser = createParser ?  
  13.             new H264VideoStreamParser(this, inputSource,  
  14.                     includeStartCodeInOutput) : NULL;  
  15.     fNextPresentationTime = fPresentationTimeBase;  
  16.     fFrameRate = 25.0; // We assume a frame rate of 25 fps,    
  17.         //unless we learn otherwise (from parsing a Sequence Parameter Set NAL unit)   
  18. }  
H264VideoStreamFramer::H264VideoStreamFramer(UsageEnvironment& env,
		FramedSource* inputSource,
		Boolean createParser,
		Boolean includeStartCodeInOutput)
		: MPEGVideoStreamFramer(env, inputSource),
				fIncludeStartCodeInOutput(includeStartCodeInOutput),
				fLastSeenSPS(NULL),
				fLastSeenSPSSize(0),
				fLastSeenPPS(NULL),
				fLastSeenPPSSize(0)
{
	fParser = createParser ?
			new H264VideoStreamParser(this, inputSource,
					includeStartCodeInOutput) :	NULL;
	fNextPresentationTime = fPresentationTimeBase;
	fFrameRate = 25.0; // We assume a frame rate of 25 fps, 
		//unless we learn otherwise (from parsing a Sequence Parameter Set NAL unit)
}

由于createParser肯定为真,所以主要内容是创建了H264VideoStreamParser对象(先不管这个parser)。
其它的函数就没什么可看的了,都集中在所保存的PPS与SPS上。看来分析工作移到了H264VideoStreamParser,Parser嘛,就是分析器。分析器的基类是StreamParser。StreamParser做了不少的工作,那我们就先搞明白StreamParser做了哪些工作吧,并且可能为继承者提供什么样的调用框架呢?.....我看完了,呵呵。直接说分析结果吧:
StreamParser的主要工作是实现了对数据以位为单位进行访问。因为在处理媒体格式时,按位分析是很常见的情况。这两个函数skipBits(unsigned numBits)和unsigned getBits(unsigned numBits)很明显是基于位的操作。unsigned char* fBank[2]这个变量中的两个缓冲区被轮换使用。这个类中保存了一个Source,理所当然地它应该保存ByteStreamFileSource的实例,而不是FramedFilter的。那些getBytes()或getBits()最终会导致读文件的操作。从文件读取一次数据后,StreamParser::afterGettingBytes1()被调用,StreamParser::afterGettingBytes1()中做一点简单的工作后便调用fClientContinueFunc这个回调函数。fClientContinueFunc可能指向Frame的函数体也可能是指向RtpSink的函数体--因为Framer完全可以把RtpSink的函数体传给Parser。至于到底指向哪个,只能在进一步分析之后才得知。

下面再来分析StreamParser的儿子:MPEGVideoStreamParser。

  1. MPEGVideoStreamParser::MPEGVideoStreamParser(  
  2.         MPEGVideoStreamFramer* usingSource,  
  3.         FramedSource* inputSource)  
  4.         : StreamParser(inputSource,  
  5.             FramedSource::handleClosure,   
  6.             usingSource,  
  7.             &MPEGVideoStreamFramer::continueReadProcessing,  
  8.             usingSource),  
  9.             fUsingSource(usingSource)  
  10. {  
  11. }  
MPEGVideoStreamParser::MPEGVideoStreamParser(
		MPEGVideoStreamFramer* usingSource,
		FramedSource* inputSource)
		: StreamParser(inputSource,
			FramedSource::handleClosure, 
			usingSource,
			&MPEGVideoStreamFramer::continueReadProcessing,
			usingSource),
			fUsingSource(usingSource)
{
}

MPEGVideoStreamParser的构造函数中有很多有意思的东西。
首先参数usingSource是什么意思?表示正在使用这个Parser的Source? inputSource 很明确,就是能获取数据的source,也就是 ByteStreamFileSource。而且很明显的,StreamParser中保存的source是ByteStreamFileSource。从传入给StreamParser的回调函数以及它们的参数可以看出,这些回调函数全是指向的StreamParser的子类的函数(为啥不用虚函数的方式?哦,回调函数全是静态函数,不能成为虚函数)。这说明在每读一次数据后,MPEGVideoStreamFramer::continueReadProcessing()被调用,在其中对帧进行界定和分析,完成后再调用RTPSink的相应函数,RTPSink中对帧进行打包和发送(还记得吗,不记得了请回头看以前的章节)。
MPEGVideoStreamParser的fTo是RTPSink传入的缓冲指针,其saveByte(),save4Bytes()是把数据从StreamParser的缓冲把数据复制到fTo中,是给继承类使用的。saveToNextCode()是复制数据直到遇到一个同步字节串(比如h264中分隔nal的那一陀东东,当然此处的跟h264还不一样),也是给继承类使用的。纯虚函数parse()很明显是留继承类去写帧分析代码的地方。registerReadInterest()被调用者用来告诉MPEGVideoStreamParser其接收帧的缓冲地址与容量。
现在应该来分析一下MPEGVideoStreamFramer,以明确MPEGVideoStreamFramer与MPEGVideoStreamParser是怎样配合的。
MPEGVideoStreamFramer中用到Parser的重要的函数只有两个,一是:

  1. void MPEGVideoStreamFramer::doGetNextFrame()  
  2. {  
  3.     fParser->registerReadInterest(fTo, fMaxSize);  
  4.     continueReadProcessing();  
  5. }  
void MPEGVideoStreamFramer::doGetNextFrame()
{
	fParser->registerReadInterest(fTo, fMaxSize);
	continueReadProcessing();
}

很简单,只是告诉了Parser保存帧的缓冲和缓冲的大小,然后执行continueReadProcessing(),那么来看一下continueReadProcessing():

  1. void MPEGVideoStreamFramer::continueReadProcessing()  
  2. {  
  3.     unsigned acquiredFrameSize = fParser->parse();  
  4.     if (acquiredFrameSize > 0) {  
  5.         // We were able to acquire a frame from the input.   
  6.         // It has already been copied to the reader's space.   
  7.         fFrameSize = acquiredFrameSize;  
  8.         fNumTruncatedBytes = fParser->numTruncatedBytes();  
  9.   
  10.         // "fPresentationTime" should have already been computed.   
  11.   
  12.         // Compute "fDurationInMicroseconds" now:   
  13.         fDurationInMicroseconds =  
  14.                 (fFrameRate == 0.0 || ((int) fPictureCount) < 0) ?  
  15.                         0 : (unsigned) ((fPictureCount * 1000000) / fFrameRate);  
  16.         fPictureCount = 0;  
  17.   
  18.         // Call our own 'after getting' function.  Because we're not a 'leaf'   
  19.         // source, we can call this directly, without risking infinite recursion.   
  20.         afterGetting(this);  
  21.     } else {  
  22.         // We were unable to parse a complete frame from the input, because:   
  23.         // - we had to read more data from the source stream, or   
  24.         // - the source stream has ended.   
  25.     }  
  26. }  
void MPEGVideoStreamFramer::continueReadProcessing()
{
	unsigned acquiredFrameSize = fParser->parse();
	if (acquiredFrameSize > 0) {
		// We were able to acquire a frame from the input.
		// It has already been copied to the reader's space.
		fFrameSize = acquiredFrameSize;
		fNumTruncatedBytes = fParser->numTruncatedBytes();

		// "fPresentationTime" should have already been computed.

		// Compute "fDurationInMicroseconds" now:
		fDurationInMicroseconds =
				(fFrameRate == 0.0 || ((int) fPictureCount) < 0) ?
						0 : (unsigned) ((fPictureCount * 1000000) / fFrameRate);
		fPictureCount = 0;

		// Call our own 'after getting' function.  Because we're not a 'leaf'
		// source, we can call this directly, without risking infinite recursion.
		afterGetting(this);
	} else {
		// We were unable to parse a complete frame from the input, because:
		// - we had to read more data from the source stream, or
		// - the source stream has ended.
	}
}

先利用Parser进行分析(应该是解析出一帧吧?),分析完成后,帧数据已到了MPEGVideoStreamFramer的缓冲fTo中。计算出帧的持续时间后,调用FrameSource的afterGetting(),最终会调用RTPSink的函数。
看到这里,可以总结一下,其实看来看去,Parser直正被外部使用的函数几乎只有一个:parse()。

下面可以看H264VideoStreamParser了。其实也很简单,多了一些用于分析h264格式的函数,当然是非公开的,只供自己使用的。在哪里使用呢?当然是在parser()中使用。至于H264VideoStreamFramer前面已经说过了,没什么太多的东西,所以就不看了。总结起来也就是这样:RTPSink向H264VideoStreamFramer要下一帧(其实h264中肯定不是一帧了,而是一个NAL Unit),H264VideoStreamFramer告诉H264VideoStreamParser输出缓冲和容内的字节数,然后调用H264VideoStreamParser的parser()函数,parser()中调用ByteStreamFileSource从文件中读取数据,直到parser()获得完整的一帧,parser()返回,H264VideoStreamFramer进行一些自己的处理后把这一帧返回给了RTPSink(当然是以回调函数的方式返回)。
还有一个东西,H264FUAFragmenter,被H264VideoRTPSink所使用,继承自FramedFilter。它最初在RTPSink开始播放后创建,如下:

  1. Boolean H264VideoRTPSink::continuePlaying()  
  2. {  
  3.     // First, check whether we have a 'fragmenter' class set up yet.   
  4.     // If not, create it now:   
  5.     if (fOurFragmenter == NULL) {  
  6.         fOurFragmenter = new H264FUAFragmenter(envir(), fSource,  
  7.                 OutPacketBuffer::maxSize,  
  8.                 ourMaxPacketSize() - 12/*RTP hdr size*/);  
  9.         fSource = fOurFragmenter;  
  10.     }  
  11.   
  12.     // Then call the parent class's implementation:   
  13.     return MultiFramedRTPSink::continuePlaying();  
  14. }  
Boolean H264VideoRTPSink::continuePlaying()
{
	// First, check whether we have a 'fragmenter' class set up yet.
	// If not, create it now:
	if (fOurFragmenter == NULL) {
		fOurFragmenter = new H264FUAFragmenter(envir(), fSource,
				OutPacketBuffer::maxSize,
				ourMaxPacketSize() - 12/*RTP hdr size*/);
		fSource = fOurFragmenter;
	}

	// Then call the parent class's implementation:
	return MultiFramedRTPSink::continuePlaying();
}

并且它取代了H264VideoStreamFramer成为直接与RTPSink发生关系的source.如此一来,RTPSink要获取帧时,都是从它获取的.看它最主要的一个函数吧:

  1. void H264FUAFragmenter::doGetNextFrame()  
  2. {  
  3.     if (fNumValidDataBytes == 1) {  
  4.         // We have no NAL unit data currently in the buffer.  Read a new one:   
  5.         fInputSource->getNextFrame(&fInputBuffer[1], fInputBufferSize - 1,  
  6.                 afterGettingFrame, this, FramedSource::handleClosure, this);  
  7.     } else {  
  8.         // We have NAL unit data in the buffer.  There are three cases to consider:   
  9.         // 1. There is a new NAL unit in the buffer, and it's small enough to deliver   
  10.         //    to the RTP sink (as is).   
  11.         // 2. There is a new NAL unit in the buffer, but it's too large to deliver to   
  12.         //    the RTP sink in its entirety.  Deliver the first fragment of this data,   
  13.         //    as a FU-A packet, with one extra preceding header byte.   
  14.         // 3. There is a NAL unit in the buffer, and we've already delivered some   
  15.         //    fragment(s) of this.  Deliver the next fragment of this data,   
  16.         //    as a FU-A packet, with two extra preceding header bytes.   
  17.   
  18.         if (fMaxSize < fMaxOutputPacketSize) { // shouldn't happen   
  19.             envir() << "H264FUAFragmenter::doGetNextFrame(): fMaxSize ("  
  20.                     << fMaxSize << ") is smaller than expected\n";  
  21.         } else {  
  22.             fMaxSize = fMaxOutputPacketSize;  
  23.         }  
  24.   
  25.         fLastFragmentCompletedNALUnit = True; // by default   
  26.         if (fCurDataOffset == 1) { // case 1 or 2   
  27.             if (fNumValidDataBytes - 1 <= fMaxSize) { // case 1   
  28.                 memmove(fTo, &fInputBuffer[1], fNumValidDataBytes - 1);  
  29.                 fFrameSize = fNumValidDataBytes - 1;  
  30.                 fCurDataOffset = fNumValidDataBytes;  
  31.             } else { // case 2   
  32.                 // We need to send the NAL unit data as FU-A packets.  Deliver the first   
  33.                 // packet now.  Note that we add FU indicator and FU header bytes to the front   
  34.                 // of the packet (reusing the existing NAL header byte for the FU header).   
  35.                 fInputBuffer[0] = (fInputBuffer[1] & 0xE0) | 28; // FU indicator   
  36.                 fInputBuffer[1] = 0x80 | (fInputBuffer[1] & 0x1F); // FU header (with S bit)   
  37.                 memmove(fTo, fInputBuffer, fMaxSize);  
  38.                 fFrameSize = fMaxSize;  
  39.                 fCurDataOffset += fMaxSize - 1;  
  40.                 fLastFragmentCompletedNALUnit = False;  
  41.             }  
  42.         } else { // case 3   
  43.             // We are sending this NAL unit data as FU-A packets.  We've already sent the   
  44.             // first packet (fragment).  Now, send the next fragment.  Note that we add   
  45.             // FU indicator and FU header bytes to the front.  (We reuse these bytes that   
  46.             // we already sent for the first fragment, but clear the S bit, and add the E   
  47.             // bit if this is the last fragment.)   
  48.             fInputBuffer[fCurDataOffset - 2] = fInputBuffer[0]; // FU indicator   
  49.             fInputBuffer[fCurDataOffset - 1] = fInputBuffer[1] & ~0x80; // FU header (no S bit)   
  50.             unsigned numBytesToSend = 2 + fNumValidDataBytes - fCurDataOffset;  
  51.             if (numBytesToSend > fMaxSize) {  
  52.                 // We can't send all of the remaining data this time:   
  53.                 numBytesToSend = fMaxSize;  
  54.                 fLastFragmentCompletedNALUnit = False;  
  55.             } else {  
  56.                 // This is the last fragment:   
  57.                 fInputBuffer[fCurDataOffset - 1] |= 0x40; // set the E bit in the FU header   
  58.                 fNumTruncatedBytes = fSaveNumTruncatedBytes;  
  59.             }  
  60.             memmove(fTo, &fInputBuffer[fCurDataOffset - 2], numBytesToSend);  
  61.             fFrameSize = numBytesToSend;  
  62.             fCurDataOffset += numBytesToSend - 2;  
  63.         }  
  64.   
  65.         if (fCurDataOffset >= fNumValidDataBytes) {  
  66.             // We're done with this data.  Reset the pointers for receiving new data:   
  67.             fNumValidDataBytes = fCurDataOffset = 1;  
  68.         }  
  69.   
  70.         // Complete delivery to the client:   
  71.         FramedSource::afterGetting(this);  
  72.     }  
  73. }  
void H264FUAFragmenter::doGetNextFrame()
{
	if (fNumValidDataBytes == 1) {
		// We have no NAL unit data currently in the buffer.  Read a new one:
		fInputSource->getNextFrame(&fInputBuffer[1], fInputBufferSize - 1,
				afterGettingFrame, this, FramedSource::handleClosure, this);
	} else {
		// We have NAL unit data in the buffer.  There are three cases to consider:
		// 1. There is a new NAL unit in the buffer, and it's small enough to deliver
		//    to the RTP sink (as is).
		// 2. There is a new NAL unit in the buffer, but it's too large to deliver to
		//    the RTP sink in its entirety.  Deliver the first fragment of this data,
		//    as a FU-A packet, with one extra preceding header byte.
		// 3. There is a NAL unit in the buffer, and we've already delivered some
		//    fragment(s) of this.  Deliver the next fragment of this data,
		//    as a FU-A packet, with two extra preceding header bytes.

		if (fMaxSize < fMaxOutputPacketSize) { // shouldn't happen
			envir() << "H264FUAFragmenter::doGetNextFrame(): fMaxSize ("
					<< fMaxSize << ") is smaller than expected\n";
		} else {
			fMaxSize = fMaxOutputPacketSize;
		}

		fLastFragmentCompletedNALUnit = True; // by default
		if (fCurDataOffset == 1) { // case 1 or 2
			if (fNumValidDataBytes - 1 <= fMaxSize) { // case 1
				memmove(fTo, &fInputBuffer[1], fNumValidDataBytes - 1);
				fFrameSize = fNumValidDataBytes - 1;
				fCurDataOffset = fNumValidDataBytes;
			} else { // case 2
				// We need to send the NAL unit data as FU-A packets.  Deliver the first
				// packet now.  Note that we add FU indicator and FU header bytes to the front
				// of the packet (reusing the existing NAL header byte for the FU header).
				fInputBuffer[0] = (fInputBuffer[1] & 0xE0) | 28; // FU indicator
				fInputBuffer[1] = 0x80 | (fInputBuffer[1] & 0x1F); // FU header (with S bit)
				memmove(fTo, fInputBuffer, fMaxSize);
				fFrameSize = fMaxSize;
				fCurDataOffset += fMaxSize - 1;
				fLastFragmentCompletedNALUnit = False;
			}
		} else { // case 3
			// We are sending this NAL unit data as FU-A packets.  We've already sent the
			// first packet (fragment).  Now, send the next fragment.  Note that we add
			// FU indicator and FU header bytes to the front.  (We reuse these bytes that
			// we already sent for the first fragment, but clear the S bit, and add the E
			// bit if this is the last fragment.)
			fInputBuffer[fCurDataOffset - 2] = fInputBuffer[0]; // FU indicator
			fInputBuffer[fCurDataOffset - 1] = fInputBuffer[1] & ~0x80; // FU header (no S bit)
			unsigned numBytesToSend = 2 + fNumValidDataBytes - fCurDataOffset;
			if (numBytesToSend > fMaxSize) {
				// We can't send all of the remaining data this time:
				numBytesToSend = fMaxSize;
				fLastFragmentCompletedNALUnit = False;
			} else {
				// This is the last fragment:
				fInputBuffer[fCurDataOffset - 1] |= 0x40; // set the E bit in the FU header
				fNumTruncatedBytes = fSaveNumTruncatedBytes;
			}
			memmove(fTo, &fInputBuffer[fCurDataOffset - 2], numBytesToSend);
			fFrameSize = numBytesToSend;
			fCurDataOffset += numBytesToSend - 2;
		}

		if (fCurDataOffset >= fNumValidDataBytes) {
			// We're done with this data.  Reset the pointers for receiving new data:
			fNumValidDataBytes = fCurDataOffset = 1;
		}

		// Complete delivery to the client:
		FramedSource::afterGetting(this);
	}
}

如果输入缓冲中没有数据,调用fInputSource->getNextFrame(),fInputSource是H264VideoStreamFramer,H264VideoStreamFramer的getNextFrame()会调用H264VideoStreamParser的parser(),parser()又调用ByteStreamFileSource获取数据,然后分析,parser()完成后会调用:

  1. void H264FUAFragmenter::afterGettingFrame1(  
  2.         unsigned frameSize,  
  3.         unsigned numTruncatedBytes,  
  4.         struct timeval presentationTime,  
  5.         unsigned durationInMicroseconds)  
  6. {  
  7.     fNumValidDataBytes += frameSize;  
  8.     fSaveNumTruncatedBytes = numTruncatedBytes;  
  9.     fPresentationTime = presentationTime;  
  10.     fDurationInMicroseconds = durationInMicroseconds;  
  11.   
  12.     // Deliver data to the client:   
  13.     doGetNextFrame();  
  14. }  
void H264FUAFragmenter::afterGettingFrame1(
		unsigned frameSize,
		unsigned numTruncatedBytes,
		struct timeval presentationTime,
		unsigned durationInMicroseconds)
{
	fNumValidDataBytes += frameSize;
	fSaveNumTruncatedBytes = numTruncatedBytes;
	fPresentationTime = presentationTime;
	fDurationInMicroseconds = durationInMicroseconds;

	// Deliver data to the client:
	doGetNextFrame();
}

然后又调用回H264FUAFragmenter::doGetNextFrame(),此时输入缓冲中有数据了,H264FUAFragmenter就进行分析处理.H264FUAFragmenter又对数据做了什么呢?

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值