Skip to content

Commit

Permalink
Initialize RTMPPublisher encoder data before spawning threads
Browse files Browse the repository at this point in the history
  • Loading branch information
palana committed Sep 24, 2014
1 parent fadce38 commit e7804fc
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 21 deletions.
54 changes: 33 additions & 21 deletions Source/RTMPPublisher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,6 @@ bool RTMPPublisher::Init(UINT tcpBufferSize)

hDataBufferMutex = OSCreateMutex();

dataBufferSize = (App->GetVideoEncoder()->GetBitRate() + App->GetAudioEncoder()->GetBitRate()) / 8 * 1024;
if (dataBufferSize < 131072)
dataBufferSize = 131072;

dataBuffer = (BYTE *)Allocate(dataBufferSize);

hSocketThread = OSCreateThread((XTHREAD)RTMPPublisher::SocketThread, this);
Expand All @@ -202,6 +198,31 @@ bool RTMPPublisher::Init(UINT tcpBufferSize)
return true;
}

void RTMPPublisher::InitEncoderData()
{
if (encoderDataInitialized)
return;

encoderDataInitialized = true;

dataBufferSize = (App->GetVideoEncoder()->GetBitRate() + App->GetAudioEncoder()->GetBitRate()) / 8 * 1024;
if (dataBufferSize < 131072)
dataBufferSize = 131072;

metaDataPacketBuffer.resize(2048);

char *enc = metaDataPacketBuffer.data() + RTMP_MAX_HEADER_SIZE;
char *pend = metaDataPacketBuffer.data() + metaDataPacketBuffer.size();
enc = AMF_EncodeString(enc, pend, &av_setDataFrame);
enc = AMF_EncodeString(enc, pend, &av_onMetaData);
enc = App->EncMetaData(enc, pend);
metaDataPacketBuffer.resize(enc - metaDataPacketBuffer.data());

App->GetAudioHeaders(audioHeaders);

App->GetVideoHeaders(videoHeaders);
}

RTMPPublisher::~RTMPPublisher()
{
//OSDebugOut (TEXT("*** ~RTMPPublisher (%d queued, %d buffered, %d data)\n"), queuedPackets.Num(), bufferedPackets.Num(), curDataBufferLen);
Expand Down Expand Up @@ -522,6 +543,8 @@ void RTMPPublisher::ProcessPackets()

void RTMPPublisher::SendPacket(BYTE *data, UINT size, DWORD timestamp, PacketType type)
{
InitEncoderData();

if(!bConnected && !bConnecting && !bStopping)
{
hConnectionThread = OSCreateThread((XTHREAD)CreateConnectionThread, this);
Expand Down Expand Up @@ -703,22 +726,15 @@ void RTMPPublisher::BeginPublishingInternal()
{
RTMPPacket packet;

char pbuf[2048], *pend = pbuf+sizeof(pbuf);

packet.m_nChannel = 0x03; // control channel (invoke)
packet.m_headerType = RTMP_PACKET_SIZE_LARGE;
packet.m_packetType = RTMP_PACKET_TYPE_INFO;
packet.m_nTimeStamp = 0;
packet.m_nInfoField2 = rtmp->m_stream_id;
packet.m_hasAbsTimestamp = TRUE;
packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE;

char *enc = packet.m_body;
enc = AMF_EncodeString(enc, pend, &av_setDataFrame);
enc = AMF_EncodeString(enc, pend, &av_onMetaData);
enc = App->EncMetaData(enc, pend);
packet.m_body = metaDataPacketBuffer.data() + RTMP_MAX_HEADER_SIZE;

packet.m_nBodySize = enc - packet.m_body;
packet.m_nBodySize = metaDataPacketBuffer.size() - RTMP_MAX_HEADER_SIZE;
if(!RTMP_SendPacket(rtmp, &packet, FALSE))
{
App->PostStopMessage();
Expand All @@ -728,20 +744,17 @@ void RTMPPublisher::BeginPublishingInternal()
//----------------------------------------------

List<BYTE> packetPadding;
DataPacket mediaHeaders;

//----------------------------------------------

packet.m_nChannel = 0x05; // source channel
packet.m_packetType = RTMP_PACKET_TYPE_AUDIO;

App->GetAudioHeaders(mediaHeaders);

packetPadding.SetSize(RTMP_MAX_HEADER_SIZE);
packetPadding.AppendArray(mediaHeaders.lpPacket, mediaHeaders.size);
packetPadding.AppendArray(audioHeaders.lpPacket, audioHeaders.size);

packet.m_body = (char*)packetPadding.Array()+RTMP_MAX_HEADER_SIZE;
packet.m_nBodySize = mediaHeaders.size;
packet.m_nBodySize = audioHeaders.size;
if(!RTMP_SendPacket(rtmp, &packet, FALSE))
{
App->PostStopMessage();
Expand All @@ -754,13 +767,12 @@ void RTMPPublisher::BeginPublishingInternal()
packet.m_headerType = RTMP_PACKET_SIZE_LARGE;
packet.m_packetType = RTMP_PACKET_TYPE_VIDEO;

App->GetVideoHeaders(mediaHeaders);

packetPadding.SetSize(RTMP_MAX_HEADER_SIZE);
packetPadding.AppendArray(mediaHeaders.lpPacket, mediaHeaders.size);
packetPadding.AppendArray(videoHeaders.lpPacket, videoHeaders.size);

packet.m_body = (char*)packetPadding.Array()+RTMP_MAX_HEADER_SIZE;
packet.m_nBodySize = mediaHeaders.size;
packet.m_nBodySize = videoHeaders.size;
if(!RTMP_SendPacket(rtmp, &packet, FALSE))
{
App->PostStopMessage();
Expand Down
5 changes: 5 additions & 0 deletions Source/RTMPPublisher.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ class RTMPPublisher : public NetworkStream
void InitializeBuffer();
void SendPacketForReal(BYTE *data, UINT size, DWORD timestamp, PacketType type);

bool encoderDataInitialized = false;
std::vector<char> metaDataPacketBuffer;
DataPacket audioHeaders, videoHeaders;
void InitEncoderData();

//-----------------------------------------------
// frame drop stuff

Expand Down

0 comments on commit e7804fc

Please sign in to comment.