分享

使用librtmp发布flash视频直播流(H264、AAC)的C++类代码

 昵称597197 2015-09-14

使用librtmp发布flash视频直播流(H264、AAC)的C++类代码

  (2013-06-26 14:32:10)

[置顶] 使用librtmp发布flash视频直播流(H264、AAC)的C++类代码

分类: 音视频编码 2013-03-11 20:09 353人阅读 评论(5) 收藏 举报

最近在研究使用开源librtmp库发布flash视频直播流,经过几天的折腾后,写了一个封装调用librtmp的类,时间紧迫没好好整理,仅供需要的朋友参考


头文件:RtmpStream.h

  1. #pragma once  
  2.   
  3. /// 对开源库rtmpdump的封装  
  4.   
  5. #include   
  6. #include "common.h"  
  7.   
  8. struct RTMP;  
  9. struct RTMPMetadata;  
  10. struct RTMPPacket;  
  11.   
  12. class CRtmpStream  
  13.  
  14. public 
  15.     CRtmpStream(void);  
  16.     virtual ~CRtmpStream(void);  
  17.   
  18. public 
  19.     /// 打开  
  20.     BOOL Open(const char apUrl);  
  21.   
  22.     /// 重新打开最近的连接  
  23.     BOOL Reopen();  
  24.   
  25.     /// 关闭  
  26.     void Close();  
  27.   
  28.     /// 是否打开  
  29.     inline BOOL IsOpened() const  
  30.      
  31.         return m_pHandle != NULL;  
  32.      
  33.   
  34.     /// 发送元数据  
  35.     BOOL SendMetadata(const RTMPMetadata astruMetadata);  
  36.   
  37.     /// 发送AAC数据  
  38.     BOOL SendAACData(const void apBuf, const unsigned int auBufLen, const unsigned int auTimestamp);  
  39.   
  40.     /// 发送H264数据  
  41.     BOOL SendH264Data(const void apBuf, const unsigned int auBufLen, const unsigned int auTimestamp,  
  42.                       const BOOL abIsKey, const int aiOffset);  
  43.   
  44. private 
  45.     void CloseStream();  
  46.   
  47. private 
  48.     RTMP                    m_pHandle;  
  49.     unsigned int            m_uStartAudioTime;  
  50.     unsigned int            m_uStartVideoTime;  
  51.     std::string                m_strLastUrl;  
  52.     CCriticalSectionEx        m_oLock;  
  53.     BOOL                    m_bIsLiving;  
  54. };  

实现:RtmpStream.cpp

  1. #include "stdafx.h"  
  2. #include "RtmpStream.h"  
  3. #include "rtmp/rtmp.h"  
  4. #include "rtmp/flvpush.h"  
  5. #include   
  6. #include   
  7.   
  8. static class CInitSocket  
  9.  
  10. public 
  11.     CInitSocket()  
  12.      
  13. #ifdef WIN32  
  14.         WSADATA wsa_data;  
  15.         WORD w_version_requested MAKEWORD(2, 2);  
  16.         if WSAStartup(w_version_requested, &wsa_data) !=  
  17.          
  18.             m_bResult false 
  19.             return 
  20.          
  21.         //else if LOBYTE(wsa_data.wVersion) != || HIBYTE(wsa_data.wVersion) != )  
  22.         //{  
  23.         //    WSACleanup();  
  24.         //    return false;  
  25.         //}  
  26. #endif  
  27.         m_bResult true 
  28.      
  29.   
  30.     ~CInitSocket()  
  31.      
  32. #ifdef WIN32  
  33.         if m_bResult  
  34.          
  35.             WSACleanup();  
  36.          
  37. #endif  
  38.      
  39.   
  40.     bool m_bResult;  
  41.   
  42. s_oInitSocket;  
  43.   
  44.   
  45. CRtmpStream::CRtmpStream(void 
  46.     m_pHandle(NULL)  
  47.     m_bIsLiving(FALSE)  
  48.     m_uStartAudioTime(0)  
  49.     m_uStartVideoTime(0)  
  50.  
  51.  
  52.   
  53. CRtmpStream::~CRtmpStream(void 
  54.  
  55.     Close();  
  56.  
  57.   
  58. BOOL CRtmpStream::Open(const char apUrl)  
  59.  
  60.     CCriticalSectionHelperEx oDoLock(m_oLock);  
  61.   
  62.     if m_bIsLiving  
  63.      
  64.         return FALSE;  
  65.      
  66.   
  67.     if IsOpened()  
  68.      
  69.         return FALSE;  
  70.      
  71.   
  72.     m_pHandle RTMP_Alloc();  
  73.     if m_pHandle == NULL  
  74.      
  75.         assert(FALSE);  
  76.         return FALSE;  
  77.      
  78.   
  79.     RTMP_Init(m_pHandle);  
  80.   
  81.     if !RTMP_SetupURL(m_pHandle, apUrl)  
  82.      
  83.         Close();  
  84.         assert(FALSE);  
  85.         return FALSE;  
  86.      
  87.       
  88.     RTMP_EnableWrite(m_pHandle);  
  89.   
  90.     if !RTMP_Connect(m_pHandle, NULL)  
  91.      
  92.         Close();  
  93.         assert(FALSE);  
  94.         return FALSE;  
  95.      
  96.   
  97.     if !RTMP_ConnectStream(m_pHandle, 0)  
  98.      
  99.         Close();  
  100.         assert(FALSE);  
  101.         return FALSE;  
  102.      
  103.   
  104.     m_bIsLiving TRUE;  
  105.     m_uStartAudioTime 0;  
  106.     m_uStartVideoTime 0;  
  107.       
  108.     if m_strLastUrl.c_str() != apUrl  
  109.      
  110.         m_strLastUrl apUrl;  
  111.      
  112.   
  113.     return TRUE;  
  114.  
  115.   
  116. /// 重新打开最近的连接  
  117. BOOL CRtmpStream::Reopen()  
  118.  
  119.     return Open(m_strLastUrl.c_str());  
  120.  
  121.   
  122. void CRtmpStream::CloseStream()  
  123.  
  124.     if m_pHandle != NULL  
  125.      
  126.         RTMP_Close(m_pHandle);  
  127.         ::Sleep(20);  
  128.         RTMP_Free(m_pHandle);  
  129.         m_pHandle NULL;  
  130.      
  131.  
  132.   
  133. void CRtmpStream::Close()  
  134.  
  135.     CCriticalSectionHelperEx oDoLock(m_oLock);  
  136.   
  137.     CloseStream();  
  138.   
  139.     m_bIsLiving FALSE;  
  140.  
  141.   
  142. /// 发送元数据  
  143. BOOL CRtmpStream::SendMetadata(const RTMPMetadata astruMetadata)  
  144.  
  145.     if !m_bIsLiving  
  146.      
  147.         return FALSE;  
  148.      
  149.   
  150.     CCriticalSectionHelperEx oDoLock(m_oLock);  
  151.   
  152.     if !IsOpened()  
  153.      
  154.         assert(FALSE);  
  155.         return FALSE;  
  156.      
  157.   
  158.     if !RTMP_SendMetadata(m_pHandle, &astruMetadata)  
  159.      
  160.         assert(FALSE);  
  161.         return FALSE;  
  162.      
  163.   
  164.     return TRUE;  
  165.  
  166.   
  167. /// 发送AAC数据  
  168. BOOL CRtmpStream::SendAACData(const void apBuf, const unsigned int auBufLen, const unsigned int auTimestamp)  
  169.  
  170.     if !m_bIsLiving  
  171.      
  172.         return FALSE;  
  173.      
  174.   
  175.     if auBufLen ==  
  176.      
  177.         return TRUE;  
  178.      
  179.   
  180.     CCriticalSectionHelperEx oDoLock(m_oLock);  
  181.   
  182.     if !IsOpened()  
  183.      
  184.         return TRUE;  
  185.      
  186.   
  187.     unsigned int uCurTimestamp;  
  188.     if m_uStartAudioTime  
  189.      
  190.         // 有值  
  191.         uCurTimestamp auTimestamp m_uStartAudioTime;  
  192.      
  193.     else  
  194.      
  195.         // is 0  
  196.         uCurTimestamp 1;  
  197.         m_uStartAudioTime auTimestamp;  
  198.      
  199.   
  200.     if !RTMP_SendAACData(m_pHandle, apBuf, auBufLen, uCurTimestamp)  
  201.      
  202.         Close();  
  203.         Reopen();  
  204.   
  205.         assert(FALSE);  
  206.         return FALSE;  
  207.      
  208.   
  209.     return TRUE;  
  210.  
  211.   
  212. /// 发送H264数据  
  213. BOOL CRtmpStream::SendH264Data(const void apBuf, const unsigned int auBufLen, const unsigned int auTimestamp,  
  214.                                const BOOL abIsKey, const int aiOffset)  
  215.  
  216.     if !m_bIsLiving  
  217.      
  218.         return FALSE;  
  219.      
  220.   
  221.     if auBufLen ==  
  222.      
  223.         return TRUE;  
  224.      
  225.   
  226.   
  227.     CCriticalSectionHelperEx oDoLock(m_oLock);  
  228.   
  229.     if !IsOpened()  
  230.      
  231.         return TRUE;  
  232.      
  233.   
  234.     unsigned int uCurTimestamp;  
  235.     if m_uStartVideoTime  
  236.      
  237.         // 有值  
  238.         uCurTimestamp auTimestamp m_uStartVideoTime;  
  239.      
  240.     else  
  241.      
  242.         // is 0  
  243.         uCurTimestamp 1;  
  244.         m_uStartVideoTime auTimestamp;  
  245.      
  246.   
  247.     if !RTMP_SendH264Data(m_pHandle, apBuf, auBufLen, uCurTimestamp, abIsKey, aiOffset)  
  248.      
  249.         Close();  
  250.         Reopen();  
  251.   
  252.         assert(FALSE);  
  253.         return FALSE;  
  254.      
  255.   
  256.     return TRUE;  
  257.  

调用顺序:


1、打开Open
2、发送元数据SendMetadata
3、发送音频AAC数据SendAACData或视频H264数据SendH264Data
4、Close

我的资源里有编译好的静态库和代码(使用mingw gcc编译,VC里也可直接使用)


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多