RTSP/RTMP推流组件PushStream推送H265流到EasyDarwin示例

12 篇文章 0 订阅
1 篇文章 0 订阅

技术在于交流、沟通,转载请注明出处并保持作品的完整性。

原文:https://blog.csdn.net/hiwubihe/article/details/84670875

[本系列相关文章] 

  1. 基于c++实现RTSP/RTMP推流组件PushStream简介
  2. RTSP/RTMP推流组件PushStream推送H264到EasyDarwin示例
  3. RTSP/RTMP推流组件PushStream推送AAC到EasyDarwin示例
  4. RTSP/RTMP推流组件PushStream推送G711到EasyDarwin示例
  5. RTSP/RTMP推流组件PushStream推送(H264+AAC)到EasyDarwin示例
  6. RTSP/RTMP推流组件PushStream推送PS流到EasyDarwin示例
  7. RTSP/RTMP推流组件PushStream推送H264流到nginx-rtmp示例
  8. RTSP/RTMP推流组件PushStream推送(H264+AAC)流到nginx-rtmp示例
  9. RTSP/RTMP推流组件PushStream推送PS流到nginx-rtmp示例
  10. RTSP/RTMP推流组件PushStream推送H265流到EasyDarwin示例
  11. RTMP/RTSP推流组件推送摄像机IPC(H264)到EasyDarwin

本篇介绍怎么用PushStream推送H265到RTSP服务器上,Demo测试结构如下:

                          

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模拟H265编码器,从文件
                循环读取H265数据发送给EasyDarwin服务器
--------------------------------------------------------------------------------
Modification History
DATE          AUTHOR          DESCRIPTION
--------------------------------------------------------------------------------

********************************************************************************/
#include "IPushStream.h"
#include "XBase/XThread.h"
#include <queue>


//只推送H265数据到RTSP服务器上
#define PUSH_H265_ONLY




#ifdef PUSH_H265_ONLY
#define INPUT_FILE_NAME		("huangdun.265")
#endif


//推流地址
#define PUSH_STREAM_URL		("rtsp://192.168.1.110/live.sdp")

#define MAX_BUFFER_SIZE     (1024*8)

//读取文件
static FILE*   gInputFile = NULL;
//推流句柄
PSTREAM_HANDLE gPushHandle	;
//H264流句柄
int			   gStreamId;

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;
	//或者采用阻塞版本TryWaitQuit(uint32)
	while(!pThread->IsExitThread())
	{

		int iReadSize = fread(gszReadBuffer, 1, MAX_BUFFER_SIZE, gInputFile);

		if(iReadSize > 0)
		{
			//流送入库
			PSTREAM_DataInput(gPushHandle,gStreamId,gszReadBuffer,iReadSize);
		}
		else
		{
			//文件结束从头读
			fseek(gInputFile,0,SEEK_SET);
		}
		XThread::Sleep(20);
	}
	printf("线程:%ld 运行结束\n",XThread::SelfID());
	return 0;
}



int main()
{
	
	gInputFile = fopen(INPUT_FILE_NAME, "rb");
	if (!gInputFile)
	{
		printf("read input file :%s failed!\n",INPUT_FILE_NAME);
		return 0;
	}

	PSTREAM_SetLogCallBack(LogCBFun,NULL);

	//RTSP/RTP OVER TCP 推流
	gPushHandle = PSTREAM_CreatePushHandle(PSTREAM_RTSP_RTP_TCP);

	if(!gPushHandle)
	{
		printf("PSTREAM_CreatePushHandle Error!\n");
		return 0;
	}
	//设置消息回调
	PSTREAM_SetMsgCallBack(gPushHandle,MsgCBFun, NULL);

	#ifdef PUSH_H265_ONLY
	//添加音视频轨道 这里添加H264轨道
	gStreamId = PSTREAM_AddStream(gPushHandle,	PSTREAM_VIDEO_TYPE_H265);
	#endif


	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*300);

	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推送H265到EasyDarwin示例 

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
以下是使用 Live555 库进行 RTMP 推流示例代码,需要在项目中引入 Live555 库: ```csharp using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using Live; using Live.LiveMedia; namespace RtmpPusher { class RtmpPusher { private RTMPClient rtmpClient; private H264VideoStreamSource videoSource; public RtmpPusher() { rtspClient = new RTSPClient(Environment.GetCommandLineArgs()[1], 0, null, 0, -1); videoSource = H264VideoStreamSource.createNew(Envir.instance(), null); videoSource.setFramerate(30); videoSource.start(); videoSource.getNextFrame(true); rtspClient.setStreamSource(videoSource, null, null, null); rtspClient.sendDescribeCommand(new RTSPClient.DescribeCallback(describeCallback)); } private void describeCallback(RTSPClient.clientContinueFunc continueFunc) { rtspClient.sendSetupCommand(0, new RTSPClient.SetupCallback(setupCallback)); } private void setupCallback(RTSPClient.clientContinueFunc continueFunc, int resultCode, string resultString, Session session, MediaSubsession subsession) { rtspClient.sendPlayCommand(continueFunc, 0.0f, -1.0f, 1.0f); rtspClient.sendPauseCommand(continueFunc, 0.0f); rtspClient.sendPauseCommand(continueFunc, 0.0f); rtspClient.sendPlayCommand(continueFunc, 0.0f, -1.0f, 1.0f); rtspClient.sendPauseCommand(continueFunc, 0.0f); rtspClient.sendPauseCommand(continueFunc, 0.0f); rtspClient.sendPlayCommand(continueFunc, 0.0f, -1.0f, 1.0f); rtspClient.sendPauseCommand(continueFunc, 0.0f); rtspClient.sendPauseCommand(continueFunc, 0.0f); rtspClient.sendPlayCommand(continueFunc, 0.0f, -1.0f, 1.0f); rtspClient.sendOptionsCommand(continueFunc, 0); rtspClient.sendAnnounceCommand(continueFunc, null); rtspClient.sendRecordCommand(continueFunc, null); rtspClient.sendTeardownCommand(continueFunc, null); rtspClient.sendOptionsCommand(continueFunc, 1); rtspClient.sendSetParameterCommand(continueFunc, null); rtspClient.sendGetParameterCommand(continueFunc, null); rtspClient.sendPauseCommand(continueFunc, 0.0f); rtspClient.sendPlayCommand(continueFunc, 0.0f, -1.0f, 1.0f); rtspClient.sendGetParameterCommand(continueFunc, null); } public void pushStream() { // 指定 RTMP 服务器地址和应用名称以及流名称 rtspClient.sendAnnounceCommand(new RTSPClient.AnnounceCallback(announceCallback), "rtmp://server-ip-address/application/stream-name", "rtmp", "video/avc", "H264", "96", null); } private void announceCallback(RTSPClient.clientContinueFunc continueFunc, int resultCode, string resultString) { rtspClient.sendSetParameterCommand(continueFunc, null); rtspClient.sendOptionsCommand(continueFunc, 1); rtspClient.sendGetParameterCommand(continueFunc, null); rtspClient.sendPauseCommand(continueFunc, 0.0f); rtspClient.sendPlayCommand(continueFunc, 0.0f, -1.0f, 1.0f); rtspClient.sendGetParameterCommand(continueFunc, null); } public void close() { rtspClient.sendTeardownCommand(null, null); } } } ``` 这个示例代码使用了 Live555 库中的 RTSPClient 和 H264VideoStreamSource 类,通过 RTSPClient 从 RTSP 服务器获取视频流,然后使用 H264VideoStreamSource 将视频流转换为 H.264 编码格式,并将其推送RTMP 服务器上。需要注意的是,这只是一个简单的示例,实际应用中还需要根据具体需求进行修改和调整。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值