live555学习笔记17-H264VideoStreamParser详解 .

2024-02-22 04:18

本文主要是介绍live555学习笔记17-H264VideoStreamParser详解 .,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

十七:H264VideoStreamParser详解

很多人要做实时H264 RTP传输,那么如何充分利用live555来做呢?
大家可以看到现有的H264VideoFileServerMediaSubsession中,sink使用了H264VideoRTPSink,source使用了H264VideoStreamFramer,然而这个连接是很复杂的,在这两个节点间要插入了很多其它的节点,其实际情况是这样的:ByteStreamFileSource-->H264VideoStreamParser-->H264VideoStreamFramer-->H264FUAFragmenter-->H264VideoRTPSink.哇!真的这么复杂吗?一点没错!
当然你可以不用理它们的来龙去脉,你只需自己实现一个source,能采集图像并进行h264编码的source(当然你可以用CPU也可以用DSP进行编码),然后用它替代ByteStreamFileSource,就成了,比如你这个source可以叫做H264ByteStreamSource.当然为了提高效率,采集和编码部分应放在另一个线程中执行.

然而,我还是很想了解H264VideoStreamParser到底是什么,Parser到底有什么用?它做了什么?它与H264VideoStreamFramer是如何配合的?它们之间有内存copy发生吗?

先设想一个问题:
H264VideoStreamFramer是什么角色?跟据H264VideoFileServerMediaSubsession的代码,H264VideoStreamFramer是真正代表source的,Sink所面对的Source就是它.但是它又连接了一个ByteStreamFileSource.look一下这部分代码:

[cpp]  view plain copy
  1. FramedSource* H264VideoFileServerMediaSubsession::  
  2. createNewStreamSource(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. }  
[cpp]  view plain copy
  1. FramedSource* H264VideoFileServerMediaSubsession::  
  2. createNewStreamSource(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. }  

是吧?我没有忽悠吧?
ByteStreamFileSource是从文件取得数据的,它不管是到底什么媒体格式,它只是读文件.所以很明显H264VideoStreamFramer利用ByteStreamFileSource从文件取得数据,然后H264VideoStreamFramer再对数据进行分析.比如找出每个NALU,然后传给Sink.但是H264VideoStreamFramer没有自己去分析,而是利用了Parser,所以那一串中就多了一个H264VideoStreamParser.
H264VideoStreamParser拥有两个source指针,一个是FramedSource* fInputSource,另一个是H264VideoStreamFramer* fUsingSource.可以看出,H264VideoStreamParser把fInputSource和fUsingSource串了起来,那么fInputSource就是ByteStreamFileSource.

我们想像一下H264VideoStreamParser的所作所为:H264VideoStreamFramer把自己的缓冲(其实是sink的)传给H264VideoStreamParser,每当H264VideoStreamFramer要获取一个NALU时,就跟H264VideoStreamParser要,H264VideoStreamParser就从ByteStreamFileSource读一坨数据,然后进行分析,如果取得了一个NALU,就传给H264VideoStreamFramer.唉,H264VideoStreamFramer真是个不劳而获的坏家伙!

看一下实际的流程:

[cpp]  view plain copy
  1. //Sink调用Source(H264VideoStreamFramer)的GetNextFrame()获取数据,   
  2. //H264VideoStreamFramer从MPEGVideoStreamFramer派生,所以下面的函数会被调用:   
  3. void MPEGVideoStreamFramer::doGetNextFrame()  
  4. {  
  5.     fParser->registerReadInterest(fTo, fMaxSize);  
  6.     continueReadProcessing();  
  7. }  
  8.   
  9. void MPEGVideoStreamFramer::continueReadProcessing(void* clientData,  
  10.         unsigned char/*ptr*/,  
  11.         unsigned /*size*/,  
  12.         struct timeval /*presentationTime*/)  
  13. {  
  14.     MPEGVideoStreamFramer* framer = (MPEGVideoStreamFramer*) clientData;  
  15.     framer->continueReadProcessing();  
  16. }  
[cpp]  view plain copy
  1. //Sink调用Source(H264VideoStreamFramer)的GetNextFrame()获取数据,  
  2. //H264VideoStreamFramer从MPEGVideoStreamFramer派生,所以下面的函数会被调用:  
  3. void MPEGVideoStreamFramer::doGetNextFrame()  
  4. {  
  5.     fParser->registerReadInterest(fTo, fMaxSize);  
  6.     continueReadProcessing();  
  7. }  
  8.   
  9. void MPEGVideoStreamFramer::continueReadProcessing(void* clientData,  
  10.         unsigned char/*ptr*/,  
  11.         unsigned /*size*/,  
  12.         struct timeval /*presentationTime*/)  
  13. {  
  14.     MPEGVideoStreamFramer* framer = (MPEGVideoStreamFramer*) clientData;  
  15.     framer->continueReadProcessing();  
  16. }  
上两个是过渡,最终在这里执行:

[cpp]  view plain copy
  1. void MPEGVideoStreamFramer::continueReadProcessing()  
  2. {  
  3.     //调用Parser的parser()分析出一个NALU.如果得到了一个NALU,则   
  4.     //用afterGetting(this)返回给Sink.   
  5.     unsigned acquiredFrameSize = fParser->parse();  
  6.     if (acquiredFrameSize > 0)  
  7.     {  
  8.         // We were able to acquire a frame from the input.   
  9.         // It has already been copied to the reader's space.   
  10.         fFrameSize = acquiredFrameSize;  
  11.         fNumTruncatedBytes = fParser->numTruncatedBytes();  
  12.   
  13.         // "fPresentationTime" should have already been computed.   
  14.   
  15.         // Compute "fDurationInMicroseconds" now:   
  16.         fDurationInMicroseconds =  
  17.                 (fFrameRate == 0.0 || ((int) fPictureCount) < 0) ?  
  18.                         0 : (unsigned) ((fPictureCount * 1000000) / fFrameRate);  
  19.         fPictureCount = 0;  
  20.   
  21.         // Call our own 'after getting' function.  Because we're not a 'leaf'   
  22.         // source, we can call this directly, without risking infinite recursion.   
  23.         afterGetting(this);  
  24.     }  
  25.     else  
  26.     {  
  27.         //执行到此处并不代表parser()中没有取得数据!!   
  28.         // We were unable to parse a complete frame from the input, because:   
  29.         // - we had to read more data from the source stream, or   
  30.         // - the source stream has ended.   
  31.     }  
  32. }  
[cpp]  view plain copy
  1. void MPEGVideoStreamFramer::continueReadProcessing()  
  2. {  
  3.     //调用Parser的parser()分析出一个NALU.如果得到了一个NALU,则  
  4.     //用afterGetting(this)返回给Sink.  
  5.     unsigned acquiredFrameSize = fParser->parse();  
  6.     if (acquiredFrameSize > 0)  
  7.     {  
  8.         // We were able to acquire a frame from the input.  
  9.         // It has already been copied to the reader's space.  
  10.         fFrameSize = acquiredFrameSize;  
  11.         fNumTruncatedBytes = fParser->numTruncatedBytes();  
  12.   
  13.         // "fPresentationTime" should have already been computed.  
  14.   
  15.         // Compute "fDurationInMicroseconds" now:  
  16.         fDurationInMicroseconds =  
  17.                 (fFrameRate == 0.0 || ((int) fPictureCount) < 0) ?  
  18.                         0 : (unsigned) ((fPictureCount * 1000000) / fFrameRate);  
  19.         fPictureCount = 0;  
  20.   
  21.         // Call our own 'after getting' function.  Because we're not a 'leaf'  
  22.         // source, we can call this directly, without risking infinite recursion.  
  23.         afterGetting(this);  
  24.     }  
  25.     else  
  26.     {  
  27.         //执行到此处并不代表parser()中没有取得数据!!  
  28.         // We were unable to parse a complete frame from the input, because:  
  29.         // - we had to read more data from the source stream, or  
  30.         // - the source stream has ended.  
  31.     }  
  32. }  

上面这个函数的else{}中的注释大家注意了没有?这里关连到一个很难搞懂的现象,后面会解释之.这里先看一下parser()函数是怎样取得数据并进行分析的.
parser()中读新数据是由那些test4Bytes(),skipBytes()之类的函数引起的,它们都最终调用了ensureValidBytes1():

[cpp]  view plain copy
  1. void StreamParser::ensureValidBytes1(unsigned numBytesNeeded)  
  2. {  
  3.     // We need to read some more bytes from the input source.   
  4.     // First, clarify how much data to ask for:   
  5.     unsigned maxInputFrameSize = fInputSource->maxFrameSize();  
  6.     if (maxInputFrameSize > numBytesNeeded)  
  7.         numBytesNeeded = maxInputFrameSize;  
  8.   
  9.     // First, check whether these new bytes would overflow the current   
  10.     // bank.  If so, start using a new bank now.   
  11.     if (fCurParserIndex + numBytesNeeded > BANK_SIZE)  
  12.     {  
  13.         // Swap banks, but save any still-needed bytes from the old bank:   
  14.         unsigned numBytesToSave = fTotNumValidBytes - fSavedParserIndex;  
  15.         unsigned char const* from = &curBank()[fSavedParserIndex];  
  16.   
  17.         fCurBankNum = (fCurBankNum + 1) % 2;  
  18.         fCurBank = fBank[fCurBankNum];  
  19.         memmove(curBank(), from, numBytesToSave);  
  20.         fCurParserIndex = fCurParserIndex - fSavedParserIndex;  
  21.         fSavedParserIndex = 0;  
  22.         fTotNumValidBytes = numBytesToSave;  
  23.     }  
  24.   
  25.     // ASSERT: fCurParserIndex + numBytesNeeded > fTotNumValidBytes   
  26.     //      && fCurParserIndex + numBytesNeeded <= BANK_SIZE   
  27.     if (fCurParserIndex + numBytesNeeded > BANK_SIZE)  
  28.     {  
  29.         // If this happens, it means that we have too much saved parser state.   
  30.         // To fix this, increase BANK_SIZE as appropriate.   
  31.         fInputSource->envir() << "StreamParser internal error ("  
  32.                 << fCurParserIndex << "+ " << numBytesNeeded << " > "  
  33.                 << BANK_SIZE << ")\n";  
  34.         fInputSource->envir().internalError();  
  35.     }  
  36.   
  37.     // Try to read as many new bytes as will fit in the current bank:   
  38.     unsigned maxNumBytesToRead = BANK_SIZE - fTotNumValidBytes;  
  39.     fInputSource->getNextFrame(&curBank()[fTotNumValidBytes], maxNumBytesToRead,  
  40.             afterGettingBytes, this, onInputClosure, this);  
  41.   
  42.     throw NO_MORE_BUFFERED_INPUT;  
  43. }  
[cpp]  view plain copy
  1. void StreamParser::ensureValidBytes1(unsigned numBytesNeeded)  
  2. {  
  3.     // We need to read some more bytes from the input source.  
  4.     // First, clarify how much data to ask for:  
  5.     unsigned maxInputFrameSize = fInputSource->maxFrameSize();  
  6.     if (maxInputFrameSize > numBytesNeeded)  
  7.         numBytesNeeded = maxInputFrameSize;  
  8.   
  9.     // First, check whether these new bytes would overflow the current  
  10.     // bank.  If so, start using a new bank now.  
  11.     if (fCurParserIndex + numBytesNeeded > BANK_SIZE)  
  12.     {  
  13.         // Swap banks, but save any still-needed bytes from the old bank:  
  14.         unsigned numBytesToSave = fTotNumValidBytes - fSavedParserIndex;  
  15.         unsigned char const* from = &curBank()[fSavedParserIndex];  
  16.   
  17.         fCurBankNum = (fCurBankNum + 1) % 2;  
  18.         fCurBank = fBank[fCurBankNum];  
  19.         memmove(curBank(), from, numBytesToSave);  
  20.         fCurParserIndex = fCurParserIndex - fSavedParserIndex;  
  21.         fSavedParserIndex = 0;  
  22.         fTotNumValidBytes = numBytesToSave;  
  23.     }  
  24.   
  25.     // ASSERT: fCurParserIndex + numBytesNeeded > fTotNumValidBytes  
  26.     //      && fCurParserIndex + numBytesNeeded <= BANK_SIZE  
  27.     if (fCurParserIndex + numBytesNeeded > BANK_SIZE)  
  28.     {  
  29.         // If this happens, it means that we have too much saved parser state.  
  30.         // To fix this, increase BANK_SIZE as appropriate.  
  31.         fInputSource->envir() << "StreamParser internal error ("  
  32.                 << fCurParserIndex << "+ " << numBytesNeeded << " > "  
  33.                 << BANK_SIZE << ")\n";  
  34.         fInputSource->envir().internalError();  
  35.     }  
  36.   
  37.     // Try to read as many new bytes as will fit in the current bank:  
  38.     unsigned maxNumBytesToRead = BANK_SIZE - fTotNumValidBytes;  
  39.     fInputSource->getNextFrame(&curBank()[fTotNumValidBytes], maxNumBytesToRead,  
  40.             afterGettingBytes, this, onInputClosure, this);  
  41.   
  42.     throw NO_MORE_BUFFERED_INPUT;  
  43. }  
可以看到一个奇怪的现象:这个函数没有返回值,但最终抛出了一个异常,而且只要执行这个函数,就会抛出这个异常.
还是先分析一下这个函数做了什么吧:
首先判断自己的缓冲区是否能容纳所需的数据量,如果实在不能,也只能提示一下,最后从ByteStreamFileSource获取一坨数据.curBack()返回的就是Parser自己的缓冲.而afterGettingBytes这个回调函数是H264VideoStreamFramer传入的,所以获取数据之后会执行H264VideoStreamFramer的函数,中转几下后,最终执行的就是上面的void MPEGVideoStreamFramer::continueReadProcessing().哇,看到了一个问题:Parser()中嵌套执行Parser()!而第二次执行Parser()完成后,返回到ensureValidBytes1(),然后由于抛出异常而退出,退出到哪里了呢?退回到上次调用的Parser()中了,因为Parser()中写了try{}catch{}.catch{}中的代码如下:

[cpp]  view plain copy
  1.     catch (int /*e*/)  
  2.     {  
  3. #ifdef DEBUG   
  4.         fprintf(stderr, "H264VideoStreamParser::parse() EXCEPTION (This is normal behavior - *not* an error)\n");  
  5. #endif   
  6.         return 0; // the parsing got interrupted   
  7.     }  
[cpp]  view plain copy
  1.     catch (int /*e*/)  
  2.     {  
  3. #ifdef DEBUG  
  4.         fprintf(stderr, "H264VideoStreamParser::parse() EXCEPTION (This is normal behavior - *not* an error)\n");  
  5. #endif  
  6.         return 0; // the parsing got interrupted  
  7.     }  
可见parser()此时返回0,parser()返回0就执行到MPEGVideoStreamFramer::continueReadProcessing()中的else{}部分了,回去看看吧,其实啥也没做.也就是说,第一次调用parser()时,它只是从ByteStreamFileSource获取数据,那么这个parser()获取数据后什么也不做,但实际上对NALU分析和处里在这次Parser()的调用中已经完成了,不是在它本身完成的,而是在它引起了parser()的嵌套调用中完成.好迷糊,理顺一下过程就知道了:
sink要获取数据,执行到MPEGVideoStreamFramer::continueReadProcessing(),MPEGVideoStreamFramer::continueReadProcessing调用parser(),parser()要使用数据时发现没有,于是ensureValidBytes1()被调用来从ByteStreamFileSource获取数据,取得数据后MPEGVideoStreamFramer::afterGettingBytes()被调用,并中转到MPEGVideoStreamFramer::continueReadProcessing(),MPEGVideoStreamFramer::continueReadProcessing()被嵌套调用!,MPEGVideoStreamFramer::continueReadProcessing()中又会调用parser(),此时parser()要使用数据时发现有数据了,所以就进行分析,分析出一个NALU后,返回到MPEGVideoStreamFramer::continueReadProcessing(),MPEGVideoStreamFramer::continueReadProcessing()会调用afterGetting(this)把数据返回给sink.sink处理完数据后返回到MPEGVideoStreamFramer::continueReadProcessing(),MPEGVideoStreamFramer::continueReadProcessing()再返回到ensureValidBytes1(),ensureValidBytes1()抛出异常返回到第一次被调用的parser()的catch{}中,parser()返回到第一次调用的MPEGVideoStreamFramer::continueReadProcessing()中,MPEGVideoStreamFramer::continueReadProcessing()发现parser()没有取得NALU,于是啥也不做,返回到sink中,sink会继续通过source->getNextFrame()->MPEGVideoStreamFramer::continueReadProcessing()...这样再次获取NALU.

好曲折离奇的故事!不过终于讲完了!

可以看到,parser中是有自己的缓冲的,而且其大小是固定的:
#define BANK_SIZE 150000
你自己写Source时,每次输出的是一帧数据,包含多个NALU,所以你只要确定你的一帧不超过150000字节,你就可以放心的往fTo中copy,如果你的帧太大,就改这个宏吧.

在此公布一下 live555 QQ群号 :  224847583 .欢迎研究流媒体的人类加入,在其中可以讨论流媒体相关的任何东西,live555,ffmpeg,vlc,rtmp等等....

这篇关于live555学习笔记17-H264VideoStreamParser详解 .的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/734096

相关文章

MySQL数据库双机热备的配置方法详解

《MySQL数据库双机热备的配置方法详解》在企业级应用中,数据库的高可用性和数据的安全性是至关重要的,MySQL作为最流行的开源关系型数据库管理系统之一,提供了多种方式来实现高可用性,其中双机热备(M... 目录1. 环境准备1.1 安装mysql1.2 配置MySQL1.2.1 主服务器配置1.2.2 从

Linux kill正在执行的后台任务 kill进程组使用详解

《Linuxkill正在执行的后台任务kill进程组使用详解》文章介绍了两个脚本的功能和区别,以及执行这些脚本时遇到的进程管理问题,通过查看进程树、使用`kill`命令和`lsof`命令,分析了子... 目录零. 用到的命令一. 待执行的脚本二. 执行含子进程的脚本,并kill2.1 进程查看2.2 遇到的

MyBatis常用XML语法详解

《MyBatis常用XML语法详解》文章介绍了MyBatis常用XML语法,包括结果映射、查询语句、插入语句、更新语句、删除语句、动态SQL标签以及ehcache.xml文件的使用,感兴趣的朋友跟随小... 目录1、定义结果映射2、查询语句3、插入语句4、更新语句5、删除语句6、动态 SQL 标签7、ehc

详解SpringBoot+Ehcache使用示例

《详解SpringBoot+Ehcache使用示例》本文介绍了SpringBoot中配置Ehcache、自定义get/set方式,并实际使用缓存的过程,文中通过示例代码介绍的非常详细,对大家的学习或者... 目录摘要概念内存与磁盘持久化存储:配置灵活性:编码示例引入依赖:配置ehcache.XML文件:配置

从基础到高级详解Go语言中错误处理的实践指南

《从基础到高级详解Go语言中错误处理的实践指南》Go语言采用了一种独特而明确的错误处理哲学,与其他主流编程语言形成鲜明对比,本文将为大家详细介绍Go语言中错误处理详细方法,希望对大家有所帮助... 目录1 Go 错误处理哲学与核心机制1.1 错误接口设计1.2 错误与异常的区别2 错误创建与检查2.1 基础

k8s按需创建PV和使用PVC详解

《k8s按需创建PV和使用PVC详解》Kubernetes中,PV和PVC用于管理持久存储,StorageClass实现动态PV分配,PVC声明存储需求并绑定PV,通过kubectl验证状态,注意回收... 目录1.按需创建 PV(使用 StorageClass)创建 StorageClass2.创建 PV

Python版本信息获取方法详解与实战

《Python版本信息获取方法详解与实战》在Python开发中,获取Python版本号是调试、兼容性检查和版本控制的重要基础操作,本文详细介绍了如何使用sys和platform模块获取Python的主... 目录1. python版本号获取基础2. 使用sys模块获取版本信息2.1 sys模块概述2.1.1

一文详解Python如何开发游戏

《一文详解Python如何开发游戏》Python是一种非常流行的编程语言,也可以用来开发游戏模组,:本文主要介绍Python如何开发游戏的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下... 目录一、python简介二、Python 开发 2D 游戏的优劣势优势缺点三、Python 开发 3D

Redis 基本数据类型和使用详解

《Redis基本数据类型和使用详解》String是Redis最基本的数据类型,一个键对应一个值,它的功能十分强大,可以存储字符串、整数、浮点数等多种数据格式,本文给大家介绍Redis基本数据类型和... 目录一、Redis 入门介绍二、Redis 的五大基本数据类型2.1 String 类型2.2 Hash

Java中的.close()举例详解

《Java中的.close()举例详解》.close()方法只适用于通过window.open()打开的弹出窗口,对于浏览器的主窗口,如果没有得到用户允许是不能关闭的,:本文主要介绍Java中的.... 目录当你遇到以下三种情况时,一定要记得使用 .close():用法作用举例如何判断代码中的 input