zoukankan      html  css  js  c++  java
  • 【视频开发】【Live555】live555实现h264码流RTSP传输

    1.概述

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


    2 重要概念:

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

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

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

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

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

     getNextFrame函数来自liveliveMediaFramedSource文件,代码见下

    [cpp] view plain copy
    1. void FramedSource::getNextFrame(unsignedchar* to, unsigned maxSize,  
    2.                 afterGettingFunc*afterGettingFunc,  
    3.                 void*afterGettingClientData,  
    4.                 onCloseFunc*onCloseFunc,  
    5.                 void*onCloseClientData) {  
    6.   // Make sure we're not already beingread:  
    7.   if (fIsCurrentlyAwaitingData){  
    8.     envir() <<"FramedSource[" <<this <<"]::getNextFrame(): attempting to read more than once at the sametime! ";  
    9.     envir().internalError();  
    10.   }  
    11.   fTo = to;  
    12.   fMaxSize = maxSize;  
    13.   fNumTruncatedBytes = 0; // by default;could be changed by doGetNextFrame()  
    14.   fDurationInMicroseconds = 0; // bydefault; could be changed by doGetNextFrame()  
    15.   fAfterGettingFunc = afterGettingFunc;  
    16.   fAfterGettingClientData =afterGettingClientData;  
    17.   fOnCloseFunc = onCloseFunc;  
    18.   fOnCloseClientData = onCloseClientData;  
    19.   fIsCurrentlyAwaitingData = True;  
    20.   doGetNextFrame();  
    21. }  


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

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

    [cpp] view plain copy
    1. voidH264FramedLiveSource::doGetNextFrame()  
    2. {  
    3.     printf("doGetNextFrame ");  
    4.     if( filesize(fp) >  fMaxSize)  
    5.       fFrameSize = fread(fTo,1,fMaxSize,fp);  
    6.     else  
    7.     {  
    8.         fFrameSize =fread(fTo,1,filesize(fp),fp);  
    9.         fseek(fp, 0, SEEK_SET);  
    10.     }  
    11.     //fFrameSize = fMaxSize;  
    12.     nextTask() =envir().taskScheduler().scheduleDelayedTask( 0,  
    13.         (TaskFunc*)FramedSource::afterGetting, this);  
    14.     return;  
    15. }  


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

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

    [cpp] view plain copy
    1. FramedSource*H264LiveVideoServerMediaSubssion::createNewStreamSource( unsignedclientSessionId, unsigned& estBitrate )  
    2. {  
    3.     /* Remain to do : assign estBitrate */  
    4.     estBitrate = 1000; // kbps, estimate  
    5.    
    6.     // Create the video source:  
    7.     H264FramedLiveSource* liveSource =H264FramedLiveSource::createNew(envir(), fFileName);  
    8.     if (liveSource == NULL)  
    9.     {  
    10.         return NULL;  
    11.     }  
    12.    
    13.     // Create a framer for the Video ElementaryStream:  
    14.     returnH264VideoStreamFramer::createNew(envir(), liveSource);  
    15. }  


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

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

     

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

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

  • 相关阅读:
    7-20 (样卷)统计单词的个数 (40 分)
    7-21 删除字符 (30 分)
    7-19 计算有n个字符串中最长的字符串长度 (40 分)
    7-16 列表数字元素加权和(1) (40 分)
    7-17 列表元素个数的加权和(1) (40 分)
    7-15 求出歌手的得分 (40 分)
    7-10 jmu-python-异常-学生成绩处理基本版 (15 分)
    7-11 jmu-python-分段函数&数学函数 (15 分)
    7-12 产生每位数字相同的n位数 (30 分)
    7-9 jmu-python-异常-学生成绩处理专业版 (25 分)
  • 原文地址:https://www.cnblogs.com/huty/p/8517068.html
Copyright © 2011-2022 走看看