技术在于交流、沟通,转载请注明出处并保持作品的完整性。
原文: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推送实时视频流到RTSP服务器上,Demo代码中基于FFMPEG获取IPC视频流,IPC视频流编码为H264,然后把视频流通过PushStream推送到EasyDarwin,再用客户端播放视频。
Demo代码
/*******************************************************************************
Copyright (c) wubihe Tech. Co., Ltd. All rights reserved.
--------------------------------------------------------------------------------
Date Created: 2014-10-25
Author: wubihe QQ:1269122125 Email:1269122125@qq.com
Description: 推流工具库,协议支持RTMP/RTSP,RTSP支持RTP OVER TCP/RTP OVER UDP
视频编码格式支持H264,H265,音频编码格式支持AAC,G711A/G711U封装格式支持
MPEG2-PS,MPEG2-TS,FLV格式,本Demo调用FFMPEG,从实时IPC通过RTSP协议获取
数据流,通过PushStream发送给EasyDarwin服务器,进行延迟测试
--------------------------------------------------------------------------------
Modification History
DATE AUTHOR DESCRIPTION
--------------------------------------------------------------------------------
********************************************************************************/
#include "IPushStream.h"
#include "XBase/XThread.h"
#include <queue>
#include <time.h>
#define __STDC_CONSTANT_MACROS
extern "C"
{
#include "libavformat/avformat.h"
};
//推流地址
#define PUSH_STREAM_URL ("rtsp://192.168.1.110/live.sdp")
//推流句柄
PSTREAM_HANDLE gPushHandle ;
//H264流句柄
int gStreamId ;
//输入上下文
AVFormatContext *gIfmtCtx = NULL;
int gVideoIndex= -1;
//日志回调
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;
AVPacket pkt;
AVBitStreamFilterContext* h264bsfc = av_bitstream_filter_init("h264_mp4toannexb");
//或者采用阻塞版本TryWaitQuit(uint32)
while(!pThread->IsExitThread())
{
if(av_read_frame(gIfmtCtx, &pkt)>=0)
{
if(pkt.stream_index==gVideoIndex)
{
//av_bitstream_filter_filter(h264bsfc, gIfmtCtx->streams[gVideoIndex]->codec, NULL, &pkt.data, &pkt.size, pkt.data, pkt.size, 0);
//printf("Write Video Packet. size:%d\tpts:%lld\n",pkt.size,pkt.pts);
//流送入库
PSTREAM_DataInput(gPushHandle,gStreamId,pkt.data,pkt.size);
}
av_free_packet(&pkt);
}
}
av_bitstream_filter_close(h264bsfc);
printf("线程:%ld 运行结束\n",XThread::SelfID());
return 0;
}
int main()
{
int ret, i;
const char *in_filename = "rtsp://admin:1234qwer@192.168.1.100:554/cam/realmonitor?channel=1&subtype=1"; //Input file URL
av_register_all();
//Input
if ((ret = avformat_open_input(&gIfmtCtx, in_filename, 0, 0)) < 0)
{
printf( "Could not open input file.");
return -1;
}
if ((ret = avformat_find_stream_info(gIfmtCtx, 0)) < 0)
{
printf( "Failed to retrieve input stream information");
return -1;
}
for(i=0; i<gIfmtCtx->nb_streams; i++)
{
if(gIfmtCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO)
{
gVideoIndex=i;
}
}
PSTREAM_SetLogCallBack(LogCBFun,NULL);
//RTSP/RTP OVER TCP 推流
gPushHandle = PSTREAM_CreatePushHandle(PSTREAM_RTSP_RTP_TCP,PSTREAM_REAL_STREAM);
if(!gPushHandle)
{
printf("PSTREAM_CreatePushHandle Error!\n");
return 0;
}
//设置消息回调
PSTREAM_SetMsgCallBack(gPushHandle,MsgCBFun, NULL);
//添加音视频轨道 这里添加H264轨道
gStreamId = PSTREAM_AddStream(gPushHandle, PSTREAM_VIDEO_TYPE_H264);
if(gStreamId<0)
{
printf("PSTREAM_AddStream Error!\n");
PSTREAM_DestroyPushHandle(gPushHandle);
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);
return 0;
}
}
//推流300S后结束
XThread::Sleep(1000*3000);
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播放延迟比较高。