Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
PHZ76 committed Oct 19, 2019
1 parent 9f9b408 commit 7655e57
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 28 deletions.
4 changes: 2 additions & 2 deletions example/rtmp_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "net/EventLoop.h"

#define TEST_RTMP_PUSHER 1
#define TEST_MULTI_THREAD 1
#define TEST_MULTI_THREAD 0
#define PUSH_URL "rtmp://127.0.0.1:1935/live/01"
#define PUSH_FILE "./test.h264"
#define HTTP_URL "http://127.0.0.1:8080/live/01.flv"
Expand Down Expand Up @@ -236,7 +236,7 @@ int test_rtmp_publisher(xop::EventLoop *eventLoop)
bool hasSpsPps = false;
uint8_t *frameBuf = new uint8_t[bufSize];

while (1)
while (publisher.isConnected())
{
int frameSize = h264File.readFrame((char*)frameBuf, bufSize, &bEndOfFrame);
if (frameSize > 0)
Expand Down
42 changes: 16 additions & 26 deletions src/xop/RtmpPublisher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,18 +119,16 @@ int RtmpPublisher::openUrl(std::string url, int msec)

if (this->parseRtmpUrl(url) != 0)
{
LOG_INFO("[RtmpPublisher] rtmp url:%s was illegal.\n", url.c_str());
LOG_INFO("[RtmpPublisher] rtmp url(%s) was illegal.\n", url.c_str());
return -1;
}

//LOG_INFO("[RtmpPublisher] ip:%s, port:%hu, stream path:%s\n", m_ip.c_str(), m_port, m_streamPath.c_str());

SOCKET sockfd = 0;

if (m_rtmpConn != nullptr)
{
std::shared_ptr<RtmpConnection> rtmpConn = m_rtmpConn;
sockfd = rtmpConn->fd();
SOCKET sockfd = rtmpConn->fd();
m_taskScheduler->addTriggerEvent([sockfd, rtmpConn]() {
rtmpConn->disconnect();
});
Expand All @@ -139,57 +137,49 @@ int RtmpPublisher::openUrl(std::string url, int msec)

TcpSocket tcpSocket;
tcpSocket.create();
if (!tcpSocket.connect(m_ip, m_port, timeout)) // 3s timeout
if (!tcpSocket.connect(m_ip, m_port, timeout))
{
tcpSocket.close();
return -1;
}

m_taskScheduler = m_eventLoop->getTaskScheduler().get();
m_rtmpConn.reset(new RtmpConnection((RtmpPublisher*)this, m_taskScheduler, tcpSocket.fd()));
m_taskScheduler->addTriggerEvent([sockfd, this]() {
m_taskScheduler->addTriggerEvent([this]() {
m_rtmpConn->handshake();
});

m_videoTimestamp = 0;
m_audioTimestamp = 0;
if (m_mediaIinfo.videoCodecId == RTMP_CODEC_ID_H264)
{
m_hasKeyFrame = false;
}
else
{
m_hasKeyFrame = true;
}

timeout -= (int)tp.elapsed();
if (timeout < 0)
{
timeout = 1000;
}


do
{
xop::Timer::sleep(10);
timeout -= 10;
if (timeout <= 0)
{
break;
}
} while (!m_rtmpConn->isPublisher());
xop::Timer::sleep(100);
timeout -= 100;
} while (!m_rtmpConn->isPublisher() && timeout>0);

if (!m_rtmpConn->isPublisher())
{
std::shared_ptr<RtmpConnection> rtmpConn = m_rtmpConn;
sockfd = rtmpConn->fd();
SOCKET sockfd = rtmpConn->fd();
m_taskScheduler->addTriggerEvent([sockfd, rtmpConn]() {
rtmpConn->disconnect();
});
m_rtmpConn = nullptr;
return -1;
}

m_videoTimestamp = 0;
m_audioTimestamp = 0;
m_hasKeyFrame = true;
if (m_mediaIinfo.videoCodecId == RTMP_CODEC_ID_H264)
{
m_hasKeyFrame = false;
}

return 0;
}

Expand Down

0 comments on commit 7655e57

Please sign in to comment.