技術在于交流、溝通,轉載請注明出處并保持作品的完整性。
原文:https://blog.csdn.net/hiwubihe/article/details/84670875
[本系列相關文章]
- 基于c++實作RTSP/RTMP推流元件PushStream簡介
- RTSP/RTMP推流元件PushStream推送H264到EasyDarwin示例
- RTSP/RTMP推流元件PushStream推送AAC到EasyDarwin示例
- RTSP/RTMP推流元件PushStream推送G711到EasyDarwin示例
- RTSP/RTMP推流元件PushStream推送(H264+AAC)到EasyDarwin示例
- RTSP/RTMP推流元件PushStream推送PS流到EasyDarwin示例
- RTSP/RTMP推流元件PushStream推送H264流到nginx-rtmp示例
- RTSP/RTMP推流元件PushStream推送(H264+AAC)流到nginx-rtmp示例
- RTSP/RTMP推流元件PushStream推送PS流到nginx-rtmp示例
- RTSP/RTMP推流元件PushStream推送H265流到EasyDarwin示例
- RTMP/RTSP推流元件推送錄影機IPC(H264)到EasyDarwin
本篇介紹怎麼用PushStream推送H264和AAC到RTSP伺服器上,PushStream已經做好了音視訊同步等問題,Demo調用與前面章節差不多,測試結構如下:
Demo代碼
/*******************************************************************************
Copyright (c) wubihe Tech. Co., Ltd. All rights reserved.
--------------------------------------------------------------------------------
Date Created: 2014-10-25
Author: wubihe QQ:1269122125 Email:[email protected]
Description: 推流工具庫,協定支援RTMP/RTSP,RTSP支援RTP OVER TCP/RTP OVER UDP
視訊編碼格式支援H264,H265,音頻編碼格式支援AAC,G711A/G711U
封裝格式支援MPEG2-PS,MPEG2-TS,FLV格式,本Demo模拟AAC與H264編碼器,
從檔案循環讀取AAC和H264資料發送給EasyDarwin伺服器,并保證音視訊同步
--------------------------------------------------------------------------------
Modification History
DATE AUTHOR DESCRIPTION
--------------------------------------------------------------------------------
********************************************************************************/
#include "IPushStream.h"
#include "XBase/XThread.h"
#include <queue>
#define INPUT_H264_FILE_NAME ("huangdun.264")
#define INPUT_AAC_FILE_NAME ("huangdun.aac")
//推流位址
#define PUSH_STREAM_URL ("rtsp://192.168.1.110/live.sdp")
#define MAX_BUFFER_SIZE (1024*8)
//讀取H264檔案
static FILE* gInputH264File = NULL;
//讀取Aac檔案
static FILE* gInputAacFile = NULL;
//推流句柄
PSTREAM_HANDLE gPushHandle ;
//H264流句柄
int gH264StreamId;
//Aac流句柄
int gAacStreamId;
unsigned char gszReadBuffer[MAX_BUFFER_SIZE];
//日志回調
void CALLBACK LogCBFun(PSTREAM_LOG_LEVEL nLogLevel, const char *szMessage, void* pUserData )
{
printf("%s\n",szMessage);
}
//消息回調
void CALLBACK MsgCBFun(PSTREAM_HANDLE lHandle, PSTREAM_MSG_TYPE eType,void* pUserData )
{
switch (eType)
{
case PSTREAM_MSG_TYPE_OFFLINE:
printf("收到離線消息\n");
break;
default:
break;
}
}
//線程循環讀取本地檔案 模拟從編碼器擷取的編碼資料
#ifdef __WINDOWS__
unsigned XAPI Thread1Handle(void* pParam)
#endif//__WINDOWS__
#ifdef __GNUC__
void* XAPI Thread1Handle(void* pParam)
#endif//__GNUC__
{
XThread* pThread = (XThread*)pParam;
int iReadSize;
//或者采用阻塞版本TryWaitQuit(uint32)
while(!pThread->IsExitThread())
{
//調用者 隻要讀取資料送個PushStream即可 同步問題PushStream内部會處理
//讀取H264檔案
iReadSize = fread(gszReadBuffer, 1, MAX_BUFFER_SIZE, gInputH264File);
if(iReadSize > 0)
{
//流送入庫
PSTREAM_DataInput(gPushHandle,gH264StreamId,gszReadBuffer,iReadSize);
}
else
{
//檔案結束從頭讀
fseek(gInputH264File,0,SEEK_SET);
}
//讀取H264檔案
iReadSize = fread(gszReadBuffer, 1, MAX_BUFFER_SIZE, gInputAacFile);
if(iReadSize > 0)
{
//流送入庫
PSTREAM_DataInput(gPushHandle,gAacStreamId,gszReadBuffer,iReadSize);
}
else
{
//檔案結束從頭讀
fseek(gInputAacFile,0,SEEK_SET);
}
XThread::Sleep(20);
}
printf("線程:%ld 運作結束\n",XThread::SelfID());
return 0;
}
int main()
{
gInputH264File = fopen(INPUT_H264_FILE_NAME, "rb");
if (!gInputH264File)
{
printf("read input file :%s failed!\n",INPUT_H264_FILE_NAME);
getchar();
return 0;
}
gInputAacFile = fopen(INPUT_AAC_FILE_NAME, "rb");
if (!gInputAacFile)
{
printf("read input file :%s failed!\n",INPUT_AAC_FILE_NAME);
getchar();
return 0;
}
PSTREAM_SetLogCallBack(LogCBFun,NULL);
//RTSP/RTP OVER TCP 推流
gPushHandle = PSTREAM_CreatePushHandle(PSTREAM_RTSP_RTP_TCP);
if(!gPushHandle)
{
printf("PSTREAM_CreatePushHandle Error!\n");
getchar();
return 0;
}
//設定消息回調
PSTREAM_SetMsgCallBack(gPushHandle,MsgCBFun, NULL);
//添加音視訊軌道 這裡添加H264軌道
gH264StreamId = PSTREAM_AddStream(gPushHandle, PSTREAM_VIDEO_TYPE_H264);
//添加音視訊軌道 這裡添加AAC軌道
gAacStreamId = PSTREAM_AddStream(gPushHandle, PSTREAM_AUDIO_TYPE_AAC);
if(gH264StreamId<0 || gAacStreamId<0)
{
printf("PSTREAM_AddStream Error!\n");
PSTREAM_DestroyPushHandle(gPushHandle);
getchar();
return 0;
}
//建立資料讀取線程
XThread ReadThread;
ReadThread.Start(Thread1Handle,&ReadThread);
XThread::Sleep(1000);
//探測是否可以打開推流器
int iStartPushRst = PSTREAM_StartPush(gPushHandle,(unsigned char*)PUSH_STREAM_URL);
while(iStartPushRst != E_PUSH_SUCCESS)
{
//資料未準備好 休息一會繼續探測
if(iStartPushRst == E_PUSH_NOTREADY)
{
XThread::Sleep(100);
printf("資料探測失敗 再次嘗試...!\n");
iStartPushRst = PSTREAM_StartPush(gPushHandle,(unsigned char*)PUSH_STREAM_URL);
}
else
{
printf("PSTREAM_StartPush Error!\n");
ReadThread.PostStop();
ReadThread.Join();
PSTREAM_DestroyPushHandle(gPushHandle);
getchar();
return 0;
}
}
//推流300S後結束
XThread::Sleep(1000*500);
printf("推流完成...");
PSTREAM_StopPush(gPushHandle);
ReadThread.PostStop();
ReadThread.Join();
PSTREAM_DestroyPushHandle(gPushHandle);
printf("Demo 測試完成...");
getchar();
return 1;
}
程式編譯環境
VS2008+Win10系統,用高版本的VS編譯應該沒有問題。
測試流程
1.搭建EasyDarwin流媒體伺服器,可以參考RTSP基礎之EasyDarwin流媒體轉發環境搭建,搭建完啟動服務。
2.修改程式中推流位址,編譯運作即可。
3.打開EasyPlayer播放,VLC也可播放,但是感覺VLC播放延遲比較高。
CSDN下載下傳位址 RTSP/RTMP推流元件PushStream推送H264+AAC到EasyDarwin示例