转载自:http://blog.csdn.net/firehood_/article/details/16844397

前面的文章中介绍了《H264视频通过RTMP流直播》,下面将介绍一下如何将H264实时视频通过RTSP直播。

实现思路是将视频流发送给live555, 由live555来实现H264数据流直播。

视频采集模块通过FIFO队列将H264数据帧发送给live555. live555 在收到客户端的RTSP播放请求后,开始从FIFO中读取H264视频数据并通过RTSP直播出去。整个流程如下图所示:

调整和修改Live555 MediaServer

下载live555源码,在media目录下增加四个文件并修改文件live555MediaServer.cpp。增加的四个文件如下:

WW_H264VideoServerMediaSubsession.h

WW_H264VideoServerMediaSubsession.cpp

WW_H264VideoSource.h

WW_H264VideoSource.cpp

下面附上四个文件的源码:

WW_H264VideoServerMediaSubsession.h

[cpp] view plaincopy
  1. #pragma once
  2. #include "liveMedia.hh"
  3. #include "BasicUsageEnvironment.hh"
  4. #include "GroupsockHelper.hh"
  5. #include "OnDemandServerMediaSubsession.hh"
  6. #include "WW_H264VideoSource.h"
  7. class WW_H264VideoServerMediaSubsession : public OnDemandServerMediaSubsession
  8. {
  9. public:
  10. WW_H264VideoServerMediaSubsession(UsageEnvironment & env, FramedSource * source);
  11. ~WW_H264VideoServerMediaSubsession(void);
  12. public:
  13. virtual char const * getAuxSDPLine(RTPSink * rtpSink, FramedSource * inputSource);
  14. virtual FramedSource * createNewStreamSource(unsigned clientSessionId, unsigned & estBitrate); // "estBitrate" is the stream's estimated bitrate, in kbps
  15. virtual RTPSink * createNewRTPSink(Groupsock * rtpGroupsock, unsigned char rtpPayloadTypeIfDynamic, FramedSource * inputSource);
  16. static WW_H264VideoServerMediaSubsession * createNew(UsageEnvironment & env, FramedSource * source);
  17. static void afterPlayingDummy(void * ptr);
  18. static void chkForAuxSDPLine(void * ptr);
  19. void chkForAuxSDPLine1();
  20. private:
  21. FramedSource * m_pSource;
  22. char * m_pSDPLine;
  23. RTPSink * m_pDummyRTPSink;
  24. char m_done;
  25. };

WW_H264VideoServerMediaSubsession.cpp

[cpp] view plaincopy
  1. #include "WW_H264VideoServerMediaSubsession.h"
  2. WW_H264VideoServerMediaSubsession::WW_H264VideoServerMediaSubsession(UsageEnvironment & env, FramedSource * source) : OnDemandServerMediaSubsession(env, True)
  3. {
  4. m_pSource = source;
  5. m_pSDPLine = 0;
  6. }
  7. WW_H264VideoServerMediaSubsession::~WW_H264VideoServerMediaSubsession(void)
  8. {
  9. if (m_pSDPLine)
  10. {
  11. free(m_pSDPLine);
  12. }
  13. }
  14. WW_H264VideoServerMediaSubsession * WW_H264VideoServerMediaSubsession::createNew(UsageEnvironment & env, FramedSource * source)
  15. {
  16. return new WW_H264VideoServerMediaSubsession(env, source);
  17. }
  18. FramedSource * WW_H264VideoServerMediaSubsession::createNewStreamSource(unsigned clientSessionId, unsigned & estBitrate)
  19. {
  20. return H264VideoStreamFramer::createNew(envir(), new WW_H264VideoSource(envir()));
  21. }
  22. RTPSink * WW_H264VideoServerMediaSubsession::createNewRTPSink(Groupsock * rtpGroupsock, unsigned char rtpPayloadTypeIfDynamic, FramedSource * inputSource)
  23. {
  24. return H264VideoRTPSink::createNew(envir(), rtpGroupsock, rtpPayloadTypeIfDynamic);
  25. }
  26. char const * WW_H264VideoServerMediaSubsession::getAuxSDPLine(RTPSink * rtpSink, FramedSource * inputSource)
  27. {
  28. if (m_pSDPLine)
  29. {
  30. return m_pSDPLine;
  31. }
  32. m_pDummyRTPSink = rtpSink;
  33. //mp_dummy_rtpsink->startPlaying(*source, afterPlayingDummy, this);
  34. m_pDummyRTPSink->startPlaying(*inputSource, 0, 0);
  35. chkForAuxSDPLine(this);
  36. m_done = 0;
  37. envir().taskScheduler().doEventLoop(&m_done);
  38. m_pSDPLine = strdup(m_pDummyRTPSink->auxSDPLine());
  39. m_pDummyRTPSink->stopPlaying();
  40. return m_pSDPLine;
  41. }
  42. void WW_H264VideoServerMediaSubsession::afterPlayingDummy(void * ptr)
  43. {
  44. WW_H264VideoServerMediaSubsession * This = (WW_H264VideoServerMediaSubsession *)ptr;
  45. This->m_done = 0xff;
  46. }
  47. void WW_H264VideoServerMediaSubsession::chkForAuxSDPLine(void * ptr)
  48. {
  49. WW_H264VideoServerMediaSubsession * This = (WW_H264VideoServerMediaSubsession *)ptr;
  50. This->chkForAuxSDPLine1();
  51. }
  52. void WW_H264VideoServerMediaSubsession::chkForAuxSDPLine1()
  53. {
  54. if (m_pDummyRTPSink->auxSDPLine())
  55. {
  56. m_done = 0xff;
  57. }
  58. else
  59. {
  60. double delay = 1000.0 / (FRAME_PER_SEC);  // ms
  61. int to_delay = delay * 1000;  // us
  62. nextTask() = envir().taskScheduler().scheduleDelayedTask(to_delay, chkForAuxSDPLine, this);
  63. }
  64. }

WW_H264VideoSource.h

[cpp] view plaincopy
  1. #ifndef _WW_H264VideoSource_H
  2. #define _WW_H264VideoSource_H
  3. #include "liveMedia.hh"
  4. #include "BasicUsageEnvironment.hh"
  5. #include "GroupsockHelper.hh"
  6. #include "FramedSource.hh"
  7. #define FRAME_PER_SEC 25
  8. class WW_H264VideoSource : public FramedSource
  9. {
  10. public:
  11. WW_H264VideoSource(UsageEnvironment & env);
  12. ~WW_H264VideoSource(void);
  13. public:
  14. virtual void doGetNextFrame();
  15. virtual unsigned int maxFrameSize() const;
  16. static void getNextFrame(void * ptr);
  17. void GetFrameData();
  18. private:
  19. void *m_pToken;
  20. char *m_pFrameBuffer;
  21. int  m_hFifo;
  22. };
  23. #endif

WW_H264VideoSource.cpp

[cpp] view plaincopy
  1. #include "WW_H264VideoSource.h"
  2. #include <stdio.h>
  3. #ifdef WIN32
  4. #include <windows.h>
  5. #else
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <string.h>
  9. #include <fcntl.h>
  10. #include <unistd.h>
  11. #include <limits.h>
  12. #endif
  13. #define FIFO_NAME     "/tmp/H264_fifo"
  14. #define BUFFER_SIZE   PIPE_BUF
  15. #define REV_BUF_SIZE  (1024*1024)
  16. #ifdef WIN32
  17. #define mSleep(ms)    Sleep(ms)
  18. #else
  19. #define mSleep(ms)    usleep(ms*1000)
  20. #endif
  21. WW_H264VideoSource::WW_H264VideoSource(UsageEnvironment & env) :
  22. FramedSource(env),
  23. m_pToken(0),
  24. m_pFrameBuffer(0),
  25. m_hFifo(0)
  26. {
  27. m_hFifo = open(FIFO_NAME,O_RDONLY);
  28. printf("[MEDIA SERVER] open fifo result = [%d]\n",m_hFifo);
  29. if(m_hFifo == -1)
  30. {
  31. return;
  32. }
  33. m_pFrameBuffer = new char[REV_BUF_SIZE];
  34. if(m_pFrameBuffer == NULL)
  35. {
  36. printf("[MEDIA SERVER] error malloc data buffer failed\n");
  37. return;
  38. }
  39. memset(m_pFrameBuffer,0,REV_BUF_SIZE);
  40. }
  41. WW_H264VideoSource::~WW_H264VideoSource(void)
  42. {
  43. if(m_hFifo)
  44. {
  45. ::close(m_hFifo);
  46. }
  47. envir().taskScheduler().unscheduleDelayedTask(m_pToken);
  48. if(m_pFrameBuffer)
  49. {
  50. delete[] m_pFrameBuffer;
  51. m_pFrameBuffer = NULL;
  52. }
  53. printf("[MEDIA SERVER] rtsp connection closed\n");
  54. }
  55. void WW_H264VideoSource::doGetNextFrame()
  56. {
  57. // 根据 fps,计算等待时间
  58. double delay = 1000.0 / (FRAME_PER_SEC * 2);  // ms
  59. int to_delay = delay * 1000;  // us
  60. m_pToken = envir().taskScheduler().scheduleDelayedTask(to_delay, getNextFrame, this);
  61. }
  62. unsigned int WW_H264VideoSource::maxFrameSize() const
  63. {
  64. return 1024*200;
  65. }
  66. void WW_H264VideoSource::getNextFrame(void * ptr)
  67. {
  68. ((WW_H264VideoSource *)ptr)->GetFrameData();
  69. }
  70. void WW_H264VideoSource::GetFrameData()
  71. {
  72. gettimeofday(&fPresentationTime, 0);
  73. fFrameSize = 0;
  74. int len = 0;
  75. unsigned char buffer[BUFFER_SIZE] = {0};
  76. while((len = read(m_hFifo,buffer,BUFFER_SIZE))>0)
  77. {
  78. memcpy(m_pFrameBuffer+fFrameSize,buffer,len);
  79. fFrameSize+=len;
  80. }
  81. //printf("[MEDIA SERVER] GetFrameData len = [%d],fMaxSize = [%d]\n",fFrameSize,fMaxSize);
  82. // fill frame data
  83. memcpy(fTo,m_pFrameBuffer,fFrameSize);
  84. if (fFrameSize > fMaxSize)
  85. {
  86. fNumTruncatedBytes = fFrameSize - fMaxSize;
  87. fFrameSize = fMaxSize;
  88. }
  89. else
  90. {
  91. fNumTruncatedBytes = 0;
  92. }
  93. afterGetting(this);
  94. }
[cpp] view plaincopy

修改live555MediaServer.cpp文件如下

[cpp] view plaincopy
  1. /**********
  2. This library is free software; you can redistribute it and/or modify it under
  3. the terms of the GNU Lesser General Public License as published by the
  4. Free Software Foundation; either version 2.1 of the License, or (at your
  5. option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.)
  6. This library is distributed in the hope that it will be useful, but WITHOUT
  7. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
  9. more details.
  10. You should have received a copy of the GNU Lesser General Public License
  11. along with this library; if not, write to the Free Software Foundation, Inc.,
  12. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
  13. **********/
  14. // Copyright (c) 1996-2013, Live Networks, Inc.  All rights reserved
  15. // LIVE555 Media Server
  16. // main program
  17. #include <BasicUsageEnvironment.hh>
  18. #include "DynamicRTSPServer.hh"
  19. #include "version.hh"
  20. #include "WW_H264VideoSource.h"
  21. #include "WW_H264VideoServerMediaSubsession.h"
  22. int main(int argc, char** argv) {
  23. // Begin by setting up our usage environment:
  24. TaskScheduler* scheduler = BasicTaskScheduler::createNew();
  25. UsageEnvironment* env = BasicUsageEnvironment::createNew(*scheduler);
  26. UserAuthenticationDatabase* authDB = NULL;
  27. #ifdef ACCESS_CONTROL
  28. // To implement client access control to the RTSP server, do the following:
  29. authDB = new UserAuthenticationDatabase;
  30. authDB->addUserRecord("username1", "password1"); // replace these with real strings
  31. // Repeat the above with each <username>, <password> that you wish to allow
  32. // access to the server.
  33. #endif
  34. // Create the RTSP server:
  35. RTSPServer* rtspServer = RTSPServer::createNew(*env, 554, authDB);
  36. if (rtspServer == NULL) {
  37. *env << "Failed to create RTSP server: " << env->getResultMsg() << "\n";
  38. exit(1);
  39. }
  40. // Add live stream
  41. WW_H264VideoSource * videoSource = 0;
  42. ServerMediaSession * sms = ServerMediaSession::createNew(*env, "live", 0, "ww live test");
  43. sms->addSubsession(WW_H264VideoServerMediaSubsession::createNew(*env, videoSource));
  44. rtspServer->addServerMediaSession(sms);
  45. char * url = rtspServer->rtspURL(sms);
  46. *env << "using url \"" << url << "\"\n";
  47. delete[] url;
  48. // Run loop
  49. env->taskScheduler().doEventLoop();
  50. rtspServer->removeServerMediaSession(sms);
  51. Medium::close(rtspServer);
  52. env->reclaim();
  53. delete scheduler;
  54. return 1;
  55. }

发送H264视频流的RTSPStream

[cpp] view plaincopy
  1. /********************************************************************
  2. filename:   RTSPStream.h
  3. created:    2013-08-01
  4. author:     firehood
  5. purpose:    通过live555实现H264 RTSP直播
  6. *********************************************************************/
  7. #pragma once
  8. #include <stdio.h>
  9. #ifdef WIN32
  10. #include <windows.h>
  11. #else
  12. #include <pthread.h>
  13. #endif
  14. #ifdef WIN32
  15. typedef HANDLE       ThreadHandle;
  16. #define mSleep(ms)   Sleep(ms)
  17. #else
  18. typedef unsigned int SOCKET;
  19. typedef pthread_t    ThreadHandle;
  20. #define mSleep(ms)   usleep(ms*1000)
  21. #endif
  22. #define FILEBUFSIZE (1024 * 1024)
  23. class CRTSPStream
  24. {
  25. public:
  26. CRTSPStream(void);
  27. ~CRTSPStream(void);
  28. public:
  29. // 初始化
  30. bool Init();
  31. // 卸载
  32. void Uninit();
  33. // 发送H264文件
  34. bool SendH264File(const char *pFileName);
  35. // 发送H264数据帧
  36. int SendH264Data(const unsigned char *data,unsigned int size);
  37. };
[cpp] view plaincopy
  1. /********************************************************************
  2. filename:   RTSPStream.cpp
  3. created:    2013-08-01
  4. author:     firehood
  5. purpose:    通过live555实现H264 RTSP直播
  6. *********************************************************************/
  7. #include "RTSPStream.h"
  8. #ifdef WIN32
  9. #else
  10. #include <sys/types.h>
  11. #include <sys/stat.h>
  12. #include <string.h>
  13. #include <fcntl.h>
  14. #include <unistd.h>
  15. #include <limits.h>
  16. #include <errno.h>
  17. #endif
  18. #define FIFO_NAME    "/tmp/H264_fifo"
  19. #define BUFFERSIZE   PIPE_BUF
  20. CRTSPStream::CRTSPStream(void)
  21. {
  22. }
  23. CRTSPStream::~CRTSPStream(void)
  24. {
  25. }
  26. bool CRTSPStream::Init()
  27. {
  28. if(access(FIFO_NAME,F_OK) == -1)
  29. {
  30. int res = mkfifo(FIFO_NAME,0777);
  31. if(res != 0)
  32. {
  33. printf("[RTSPStream] Create fifo failed.\n");
  34. return false;
  35. }
  36. }
  37. return true;
  38. }
  39. void CRTSPStream::Uninit()
  40. {
  41. }
  42. bool CRTSPStream::SendH264File(const char *pFileName)
  43. {
  44. if(pFileName == NULL)
  45. {
  46. return false;
  47. }
  48. FILE *fp = fopen(pFileName, "rb");
  49. if(!fp)
  50. {
  51. printf("[RTSPStream] error:open file %s failed!",pFileName);
  52. }
  53. fseek(fp, 0, SEEK_SET);
  54. unsigned char *buffer  = new unsigned char[FILEBUFSIZE];
  55. int pos = 0;
  56. while(1)
  57. {
  58. int readlen = fread(buffer+pos, sizeof(unsigned char), FILEBUFSIZE-pos, fp);
  59. if(readlen<=0)
  60. {
  61. break;
  62. }
  63. readlen+=pos;
  64. int writelen = SendH264Data(buffer,readlen);
  65. if(writelen<=0)
  66. {
  67. break;
  68. }
  69. memcpy(buffer,buffer+writelen,readlen-writelen);
  70. pos = readlen-writelen;
  71. mSleep(25);
  72. }
  73. fclose(fp);
  74. delete[] buffer;
  75. return true;
  76. }
  77. // 发送H264数据帧
  78. int CRTSPStream::SendH264Data(const unsigned char *data,unsigned int size)
  79. {
  80. if(data == NULL)
  81. {
  82. return 0;
  83. }
  84. // open pipe with non_block mode
  85. int pipe_fd = open(FIFO_NAME, O_WRONLY|O_NONBLOCK);
  86. //printf("[RTSPStream] open fifo result = [%d]\n",pipe_fd);
  87. if(pipe_fd == -1)
  88. {
  89. return 0;
  90. }
  91. int send_size = 0;
  92. int remain_size = size;
  93. while(send_size < size)
  94. {
  95. int data_len = (remain_size<BUFFERSIZE) ? remain_size : BUFFERSIZE;
  96. int len = write(pipe_fd,data+send_size,data_len);
  97. if(len == -1)
  98. {
  99. static int resend_conut = 0;
  100. if(errno == EAGAIN && ++resend_conut<=3)
  101. {
  102. printf("[RTSPStream] write fifo error,resend..\n");
  103. continue;
  104. }
  105. resend_conut = 0;
  106. printf("[RTSPStream] write fifo error,errorcode[%d],send_size[%d]\n",errno,send_size);
  107. break;
  108. }
  109. else
  110. {
  111. send_size+= len;
  112. remain_size-= len;
  113. }
  114. }
  115. close(pipe_fd);
  116. //printf("[RTSPStream] SendH264Data datalen[%d], sendsize = [%d]\n",size,send_size);
  117. return 0;
  118. }

测试程序代码

[cpp] view plaincopy
  1. #include <stdio.h>
  2. #include "RTSPStream.h"
  3. int main(int argc,char* argv[])
  4. {
  5. CRTSPStream rtspSender;
  6. bool bRet = rtspSender.Init();
  7. rtspSender.SendH264File("E:\\测试视频\\test.264");
  8. system("pause");
  9. }

FROM:  http://www.cppblog.com/tx7do/archive/2014/05/31/207155.html

通过live555实现H264 RTSP直播相关推荐

  1. 通过live555实现H264 RTSP直播(Windows版)

    为何标明"Windows版",因为firehood大神已经实现了linux版:通过live555实现H264 RTSP直播 相关文章: [1]Win7(Windows 7)下用VS ...

  2. live555 android 直播,通过live555实现H264 RTSP直播

    前面的文章中介绍了<H264视频通过RTMP流直播>,下面将介绍一下如何将H264实时视频通过RTSP直播. 实现思路是将视频流发送给live555, 由live555来实现H264数据流 ...

  3. LIVE555再学习 -- live555实现RTSP直播服务器 分析

    上一篇文章 讲到了 live555实现RTSP直播服务器,但是篇幅有点长,没有来得及对源码进行分析. 这篇文章就好好看看,源码部分这次参看Linux版本下的 通过live555实现H264 RTSP直 ...

  4. LIVE555再学习 -- live555实现RTSP直播服务器

    分析完 testOnDemandRTSPServer 和 testH264VideoStreamer 的源码.我们现在就可以做相关的项目工程. 我之前写过一个,参看: DM368开发 -- 编码并实时 ...

  5. [live555]rtsp直播基于live555的实现

    一直很想做流媒体的直播,最近花时间看了有关live555的有关代码,这里隆重的推荐两篇: http://blog.csdn.net/nkmnkm (道长的文章,分析的很不错) http://blog. ...

  6. 麒麟操作系统|Linux下低延时RTMP|RTSP直播播放实现

    背景 国产操作系统多为以Linux为基础二次开发的操作系统.2014年4月8日起,美国微软公司停止了对Windows XP SP3操作系统提供服务支持,这引起了社会和广大用户的广泛关注和对信息安全的担 ...

  7. Windows平台真实时毫秒级4K H264/H265直播技术方案探讨

    背景 在刚提出4K视频的时候,大多数人都觉得没有必要,4K的出现,意味着更高的硬件规格和传输要求,1080P看的很爽.很清晰,完全满足了日常的需求.随着电视的尺寸越来越大,原本1080P成像已经无法满 ...

  8. 跨平台低延迟的RTMP/RTSP直播播放器设计实现

    开发背景 2015年,当我们试图在市面上找一款专供直播播放使用的低延迟播放器,来配合测试我们的RTMP推送模块使用时,居然发现没有一款好用的,市面上的,如VLC或Vitamio,说白了都是基于FFMP ...

  9. 网络摄像头RTSP直播方案(三)

    前面的部分讲了关于RTSP连接的交互过程,在RTSP推流的过程中,RTSP协议只是做一个控制作用,底层真正进行传输的流媒体协议还是RTP协议.做这一部分主要是要先了解RTP协议的封装格式,这里我不详细 ...

最新文章

  1. 确定修改——取消修改
  2. php mssql 端口,MSSQL_SQL Server端口更改后的数据库连接方式,SQL Server端口,大家可以通过quot - phpStudy...
  3. android手机存储大小设置在哪里看,Android 最新获取手机内置存储大小,SD卡存储空间大小方法...
  4. Java实战之04JavaWeb-02Request和Response
  5. 联系 Contact
  6. iOS开发(5)动态监听键盘通知
  7. AngularJS复习小结
  8. python定义一个circle类、根据圆的半径_定义一个“圆”类Circle,该圆类的数据成员包括:圆心点位置及圆的半径...
  9. 麒麟MIPS:用光盘安装提示Out Of Memory,用U盘安装成功
  10. QQ输入法 for iPhone2.3
  11. 合成分红游戏源码_养成合成分红游戏源码,广告分红游戏开发
  12. Linux mmc驱动框架(4)——卡检测及初始化
  13. Revel敏捷后台开发框架
  14. html中的abbr有什么作用,html中关于abbr标签的使用以及作用的详解
  15. Springboot疫情防控下基于微信小程序的食堂订餐系统 毕业设计-附源码261620
  16. 客户端,从服务器上下载文件流程
  17. 效率系列(二) Win10常用快捷键
  18. 【蓝桥杯Web】第十四届蓝桥杯(Web 应用开发)模拟赛 1 期-职业院校组 | 精品题解
  19. Android Weekly - 42 : 打铁还需自身硬
  20. 【stm32】delay详解

热门文章

  1. 关于vue2.0组件通信
  2. vsc系统是什么意思_电脑蓝屏是什么意思?蓝屏就一定要重装系统吗?你可不要弄错了...
  3. vs2010MFC D3D播放YUV格式视频详细制作全过程
  4. 【自动驾驶】11.百度Apollo对ROS的优化
  5. 深入理解java SPI机制
  6. Java程序员从笨鸟到菜鸟之(七十二)细谈Spring(四)利用注解实现spring基本配置详解
  7. 精简自己20%的代码
  8. 简明python教程 --C++程序员的视角(四):容器类型(字符串、元组、列表、字典)和参考
  9. Windows文件夹、文件源代码对比工具--WinMerge
  10. HTML5 技术在风电、光伏等新能源领域的应用