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

  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!\n";
  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\n");
  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. // Create the video source:
  6. H264FramedLiveSource* liveSource =H264FramedLiveSource::createNew(envir(), fFileName);
  7. if (liveSource == NULL)
  8. {
  9. return NULL;
  10. }
  11. // Create a framer for the Video ElementaryStream:
  12. returnH264VideoStreamFramer::createNew(envir(), liveSource);
  13. }

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

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

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

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

【视频开发】【Live555】live555实现h264码流RTSP传输的更多相关文章

  1. 从H264码流中获取视频宽高 (SPS帧) 升级篇

    之前写过 <从H264码流中获取视频宽高 (SPS帧)> . 但发现很多局限性,而且有时解出来是错误的. 所以重新去研究了. 用了 官方提供的代码库来解析. 花了点时间,从代码库里单独把解 ...

  2. 从H264码流中获取视频宽高 (SPS帧)

    获取.h264视频宽高的方法 花了2个通宵终于搞定.(后面附上完整代码) http://write.blog.csdn.net/postedit/7852406 图像的高和宽在H264的SPS帧中.在 ...

  3. H264码流打包分析(精华)

    H264码流打包分析 SODB 数据比特串-->最原始的编码数据 RBSP 原始字节序列载荷-->在SODB的后面填加了结尾比特(RBSP trailing bits 一个bit“1”)若 ...

  4. H264码流解析及NALU

    ffmpeg 从mp4上提取H264的nalu http://blog.csdn.net/gavinr/article/details/7183499 639     /* bitstream fil ...

  5. RTP协议全解析(H264码流和PS流)

    转自:http://blog.csdn.net/chen495810242/article/details/39207305 写在前面:RTP的解析,网上找了很多资料,但是都不全,所以我力图整理出一个 ...

  6. (转)RTP协议全解(H264码流和PS流)

    写在前面:RTP的解析,网上找了很多资料,但是都不全,所以我力图整理出一个比较全面的解析, 其中借鉴了很多文章,我都列在了文章最后,在此表示感谢. 互联网的发展离不开大家的无私奉献,我决定从我做起,希 ...

  7. H264码流中SPS PPS详解<转>

    转载地址:https://zhuanlan.zhihu.com/p/27896239 1 SPS和PPS从何处而来? 2 SPS和PPS中的每个参数起什么作用? 3 如何解析SDP中包含的H.264的 ...

  8. RTP协议全解(H264码流和PS流)

    写在前面:RTP的解析,网上找了很多资料,但是都不全,所以我力图整理出一个比较全面的解析, 其中借鉴了很多文章,我都列在了文章最后,在此表示感谢. 互联网的发展离不开大家的无私奉献,我决定从我做起,希 ...

  9. H264编码原理以及I帧、B和P帧详解, H264码流结构分析

    H264码流结构分析 http://blog.csdn.net/chenchong_219/article/details/37990541 1.码流总体结构: h264的功能分为两层,视频编码层(V ...

随机推荐

  1. ASP.NET Core ---- 系列文章

    (13)ASP.NET Core 中的选项模式(Options) (12)ASP.NET Core 中的配置二(Configuration) (11)ASP.NET Core 中的配置一(Config ...

  2. 超详细的Hadoop2配置详解

    1. 集群环境 Master 192.168.2.100 Slave1 192.168.2.101 Slave2 192.168.2.102 2. 下载安装包 Master wget http://m ...

  3. [Go] Slices vs Array

    It is recommended to use 'slice' over 'Array'. An array variable denotes the entire array; it is not ...

  4. 4个MySQL优化工具AWR,帮你准确定位数据库瓶颈!(转载)

    对于正在运行的mysql,性能如何,参数设置的是否合理,账号设置的是否存在安全隐患,你是否了然于胸呢? 俗话说工欲善其事,必先利其器,定期对你的MYSQL数据库进行一个体检,是保证数据库安全运行的重要 ...

  5. BZOJ 3166: [Heoi2013]Alo 链表+可持久化trie

    链表这个东西非常好用啊 ~ code: #include <bits/stdc++.h> #define N 50010 #define inf 2000400000 #define se ...

  6. Why We Changed YugaByte DB Licensing to 100% Open Source

    转自:https://blog.yugabyte.com/why-we-changed-yugabyte-db-licensing-to-100-open-source/ 主要说明了YugaByte ...

  7. ShardingSphere初探1 -- 概览

    知道这个框架是通过一期QQ课堂 https://shardingsphere.apache.org 官网 https://github.com/apache/incubator-shardingsph ...

  8. 开源项目 06 NPOI

    using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; using System; using S ...

  9. javascript之随机密码[必包含大写,小写,数字]

    js取两个数字之间的随机数: parseInt(Math.random()*(上限-下限+1)+下限) 如:取1-10之间的随机数   parseInt(Math.random()*(10-1+1)+ ...

  10. mac 下的 tree 命令 终端展示你的目录树结构

    相信很多使用过Linux的用户都用过tree命令,它可以像windows的文件管理器一样清楚明了的显示目录结构.mac 下使用 brew包管理工具安装 tree 前提:安装了homebrew(安装指令 ...