librtmp使用方法
Posted byxdaz
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了librtmp使用方法相关的知识,希望对你有一定的参考价值。
rtmp是常见的一种流媒体协议,它是由Adobe公司提出的一种应用层协议。rtmp传输的是flv格式的封装数据,flv中保存的一般是H.264视频流和AAC音频流。librtmp库实现了rtmp协议的客户端功能,以及少数服务端功能。 librtmp的下载地址:RTMPDump
1、rtmp播放流步骤
1)初始化
RTMP *pRtmp = RTMP_Alloc();
if(pRtmp != NULL)
RTMP_Init();
if(!RTMP_SetupURL(pRtmp,"http://127.0.0.1:1935/live/test"))
RTMP_Free(pRtmp);
pRtmp = NULL;
2)连接服务器
RTMP_Connect(pRtmp, NULL);
3)是否连接服务器成功
bool bIsConnectedSuccess = false;
if(RTMP_IsConnected(pRtmp) == 0)
bIsConnectedSuccess = false;
else
bIsConnectedSuccess = true;
4)连接流
bool bSuccess = false;
if(RTMP_ConnectStream(pRtmp,0) == 0)
bSuccess = false;
else
bSuccess = true;
5)读取流
循环调用RTMP_ReadPacket
6)断开流
RTMP_DeleteStream(pRtmp);
RTMP_SendCreateStream(pRtmp);
7)断开服务器
RTMP_Close(pRtmp);
8)反初始化
if(pRtmp != NULL)
RTMP_Free(pRtmp);
pRtmp = NULL;
//播放流过程中服务器断开,重连处理。调用ReConnect函数或者RTMP_ReconnectStream函数
bool ReConnect()
bool bIsConnected = false;
if(RTMP_IsConnected(pRtmp) == 0)
RTMP_Close(pRtmp);
if(RTMP_SetupURL(pRtmp,"http://127.0.0.1:1935/live/test"))
if(RTMP_Connect(pRtmp, NULL))
if(RTMP_ConnectStrream(pRtmp,0))
bool bIsConnected = true;
return bIsConnected;
2、rtmp发布流步骤
1)初始化
RTMP *pRtmp = RTMP_Alloc();
if(pRtmp != NULL)
RTMP_Init();
if(!RTMP_SetupURL(pRtmp,"http://127.0.0.1:1935/live/test"))
RTMP_Free(pRtmp);
else
RTMP_EnableWrite(pRtmp);
2)连接服务器
RTMP_Connect(pRtmp, NULL);
3)是否连接服务器成功
bool bIsConnectedSuccess = false;
if(RTMP_IsConnected(pRtmp) == 0)
bIsConnectedSuccess = false;
else
bIsConnectedSuccess = true;
4)连接流
bool bSuccess = false;
if(RTMP_ConnectStream(pRtmp,0) == 0)
bSuccess = false;
else
bSuccess = true;
5)写入流
循环调用RTMP_SendPacket
6)断开流
RTMP_DeleteStream(pRtmp);
RTMP_SendCreateStream(pRtmp);
7)断开服务器
RTMP_Close(pRtmp);
8)反初始化
if(pRtmp != NULL)
RTMP_Free(pRtmp);
pRtmp = NULL;
//写入流过程中服务器断开,重连处理。调用ReConnect函数或者RTMP_ReconnectStream函数
bool ReConnect()
bool bIsConnected = false;
if(RTMP_IsConnected(pRtmp) == 0)
RTMP_Close(pRtmp);
if(RTMP_SetupURL(pRtmp,"http://127.0.0.1:1935/live/test"))
RTMP_EnableWrite(pRtmp);
if(RTMP_Connect(pRtmp, NULL))
if(RTMP_ConnectStrream(pRtmp,0))
bool bIsConnected = true;
return bIsConnected;
参考资料:
https://blog.csdn.net/qq_34305316/article/details/109401401
https://blog.csdn.net/leixiaohua1020/article/details/42104893
https://blog.csdn.net/leixiaohua1020/article/details/42104945
以上是关于librtmp使用方法的主要内容,如果未能解决你的问题,请参考以下文章