一、概述
案例:使用nginx+rtmpdump搭建一个简单的流媒体服务器,使用rtmplib来实现推流
librtmp推流步骤:
二、代码示例
/** * 开始推理 * @param args * @return */ void *run_start(void *args) { char *path = static_cast< char *>(args); RTMP *rtmp = 0; do { rtmp = RTMP_Alloc();//给RTMP申请内存 if (!rtmp) { LOGE("alloc rtmp 失败"); break; } //初始化 RTMP_Init(rtmp); //设置推流url int ret = RTMP_SetupURL(rtmp, path); if (!ret) { LOGE("设置RTMP推流地址失败:%s", path); break; } //设置超时时间 rtmp->Link.timeout = 5; //启用RTMP写入功能 RTMP_EnableWrite(rtmp); //链接服务器 ret = RTMP_Connect(rtmp, 0); if (!ret) { LOGE("链接服务器失败:%s", path); break; } //链接流 ret = RTMP_ConnectStream(rtmp, 0); if (!ret) { LOGE("链接流失败:%s", path); break; } //记录一个推流的开始时间 //记录一个开始时间 start_time = RTMP_GetTime(); //表示可以开始推流了 readyPushing = 1; packets.setWork(1);//设置队列生效 RTMPPacket *packet = 0; while (readyPushing) { packets.pop(packet); if (!readyPushing) { break; } if (!packet) { continue; } packet->m_nInfoField2 = rtmp->m_stream_id; //发送rtmp包 1:队列 // 意外断网?发送失败,rtmpdump 内部会调用RTMP_Close // RTMP_Close 又会调用 RTMP_SendPacket // RTMP_SendPacket 又会调用 RTMP_Close // 将rtmp.c 里面WriteN方法的 Rtmp_Close注释掉 ret = RTMP_SendPacket(rtmp, packet, 1); releasePacketCallback(packet); if (!ret) { LOGE("发送RTMP数据包失败"); break; } } releasePacketCallback(packet); } while (0); // isStart = 0; readyPushing = 0; packets.setWork(0); packets.clear(); if (rtmp) { RTMP_Close(rtmp); RTMP_Free(rtmp); } delete (path); return 0; }
标签:packet,推流,步骤,ret,break,rtmp,librtmp,RTMP From: https://www.cnblogs.com/tony-yang-flutter/p/16623132.html