您的位置:首页 > 其它

【视频开发】【Live555】live555实现h264码流RTSP传输

2017-07-28 23:31 726 查看
1.概述

liveMedia 库中有一系列类,基类是Medium,这些类针对不同的流媒体类型和编码。 其中的StreamFrame类文件(如MPEG4VideoStreamFramer)为流传输关键。

2 重要概念:

StreamFrame类:该类继承FramedSource基类,实现数据流的控制和传输。

StreamFrame(H264VideoStreamFramer) -->FramedFilter--> FramedSource----> MediaSource

   FramedSource 派继承MediaSource父类,一帧码流的实现。

   注意:unsigned char* fTo;为指向发送的码流的指针,采集到视频数据后填充到该指针中即可实现码流的传输。

主要步骤:1.定义自己的StreamFramer类,实现
getNextFrame
重写。


 getNextFrame
函数来自
live\liveMedia\FramedSource
文件,代码见下




[cpp] view
plain copy

void FramedSource::getNextFrame(unsignedchar* to, unsigned maxSize,  

                afterGettingFunc*afterGettingFunc,  

                void*afterGettingClientData,  

                onCloseFunc*onCloseFunc,  

                void*onCloseClientData) {  

  // Make sure we're not already beingread:  

  if (fIsCurrentlyAwaitingData){  

    envir() <<"FramedSource[" <<this <<"]::getNextFrame(): attempting to read more than once at the sametime!\n";  

    envir().internalError();  

  }  

  fTo = to;  

  fMaxSize = maxSize;  

  fNumTruncatedBytes = 0; // by default;could be changed by doGetNextFrame()  

  fDurationInMicroseconds = 0; // bydefault; could be changed by doGetNextFrame()  

  fAfterGettingFunc = afterGettingFunc;  

  fAfterGettingClientData =afterGettingClientData;  

  fOnCloseFunc = onCloseFunc;  

  fOnCloseClientData = onCloseClientData;  

  fIsCurrentlyAwaitingData = True;  

  doGetNextFrame();  

}  



其中最后的doGetNextFrame(); 是一个虚函数,具体各种编码模式,我们可以根据自己的码流类型定义一个派生自FramedSource的类(本工程H264FramedLiveSource类), 重新再定义doGetNextFrame如何获得下一帧的码流,在自己重定义的doGetNextFrame() 中将fTo指向要发送的缓存即可。这样我们就实现了流的传输而非文件传输。


本工程中doGetNextFrame()代码如下:


[cpp] view
plain copy

voidH264FramedLiveSource::doGetNextFrame()  

{  

    printf("doGetNextFrame\n");  

    if( filesize(fp) >  fMaxSize)  

      fFrameSize = fread(fTo,1,fMaxSize,fp);  

    else  

    {  

        fFrameSize =fread(fTo,1,filesize(fp),fp);  

        fseek(fp, 0, SEEK_SET);  

    }  

    //fFrameSize = fMaxSize;  

    nextTask() =envir().taskScheduler().scheduleDelayedTask( 0,  

        (TaskFunc*)FramedSource::afterGetting, this);  

    return;  

}  

2.实现fTO与会话连接,自定义
ServerMediaSubsession


 
定义ServerMediaSubsession类H264LiveVideoServerMediaSubssion,该类由ServerMediaSubsession 派生而来。该类中有私有函数virtual
 
FramedSource* createNewStreamSource
,在该函数中进行重新定义即可实现。


[cpp] view
plain copy

FramedSource*H264LiveVideoServerMediaSubssion::createNewStreamSource( unsignedclientSessionId, unsigned& estBitrate )  

{  

    /* Remain to do : assign estBitrate */  

    estBitrate = 1000; // kbps, estimate  

   

    // Create the video source:  

    H264FramedLiveSource* liveSource =H264FramedLiveSource::createNew(envir(), fFileName);  

    if (liveSource == NULL)  

    {  

        return NULL;  

    }  

   

    // Create a framer for the Video ElementaryStream:  

    returnH264VideoStreamFramer::createNew(envir(), liveSource);  

}  

主要最后返回的H264VideoStreamFramer继承自FramedSource,定义了从文件获取source的方法,从而将ServerMedia 与source联系起来。

代码为vs2008工程,采用VLC测试,测试结果如下图所示

 


 代码见http://download.csdn.NET/detail/xiahua882/9619900

注:工程中CaremaLive为该博客代码,MediaServer为live555标准服务器工程也可以运行。代码工程图见下

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: