Skip to content

Commit

Permalink
Add buffer class
Browse files Browse the repository at this point in the history
  • Loading branch information
prsyahmi committed Dec 5, 2020
1 parent e06ae42 commit 1259261
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 16 deletions.
40 changes: 24 additions & 16 deletions v380/FlvStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,8 @@ void FlvStream::WriteVideo(const std::vector<uint8_t>& packet, bool keyframe)
return;
}

UtlBuffer buffer;

TFlvTag tag;
TFlvVideoData vidData;
TFlvAvcVideoPacket avc;
Expand Down Expand Up @@ -422,17 +424,19 @@ void FlvStream::WriteVideo(const std::vector<uint8_t>& packet, bool keyframe)
tag.Timestamp.setSwap(0);
tag.TimestampExtended = 0;
tag.StreamID.setSwap(0);
fwrite(&tag, 1, sizeof(tag), stdout);
buffer.Write(tag);

vidData.FrameType = 1;
vidData.CodecID = 7;
fwrite(&vidData, 1, sizeof(vidData), stdout);
buffer.Write(vidData);

avc.AVCPacketType = 0;
avc.CompositionTime.setSwap(0);
fwrite(&avc, 1, sizeof(avc), stdout);
fwrite(AvcDcrData.data(), 1, AvcDcrData.size(), stdout);
buffer.Write(avc);
buffer.Write(AvcDcrData);

uint32_t prevTagSize = bswap_u32(sizeof(tag) + sizeof(vidData) + sizeof(avc) + AvcDcrData.size());
fwrite(&prevTagSize, 1, sizeof(uint32_t), stdout);
buffer.Write(prevTagSize);
}

// Not sure yet how to construct timestamp and compositionTime
Expand All @@ -443,23 +447,24 @@ void FlvStream::WriteVideo(const std::vector<uint8_t>& packet, bool keyframe)
tag.Timestamp.setSwap(timestamp);
tag.TimestampExtended = 0;
tag.StreamID.setSwap(0);
fwrite(&tag, 1, sizeof(tag), stdout);
buffer.Write(tag);

vidData.FrameType = keyframe ? 1 : 2;
vidData.CodecID = 7;
fwrite(&vidData, 1, sizeof(vidData), stdout);
buffer.Write(vidData);

avc.AVCPacketType = 1;
avc.CompositionTime.setSwap(cts);
fwrite(&avc, 1, sizeof(avc), stdout);
buffer.Write(avc);

for (auto it = Nals.begin(); it != Nals.end(); it++) {
fwrite(it->data(), 1, it->size(), stdout);
buffer.Write(*it);
}

uint32_t prevTagSize = bswap_u32(sizeof(tag) + sizeof(vidData) + sizeof(avc) + totalNalSize);
fwrite(&prevTagSize, 1, sizeof(uint32_t), stdout);
buffer.Write(prevTagSize);

fwrite(buffer.GetBuffer().data(), 1, buffer.GetBuffer().size(), stdout);
fflush(stdout);
}

Expand All @@ -481,12 +486,14 @@ void FlvStream::WriteAudio(const std::vector<uint8_t>& packet)
return;
}

UtlBuffer buffer;

TFlvTag tag;
TFlvAudioData audData;
std::vector<uint8_t> packetOnly(packet.begin() + 18, packet.end());
std::vector<int16_t> pcmData;

pcmData.resize(1024, 0);
pcmData.resize(2048, 0);

// There is no output format for ADPCM 8000hz, we need to convert it internally
int nPcmData = adpcm_decoder(0, (char *)packetOnly.data(), pcmData.data(), 505, 1);
Expand Down Expand Up @@ -520,7 +527,7 @@ void FlvStream::WriteAudio(const std::vector<uint8_t>& packet)
tag.Timestamp.setSwap(timestamp);
tag.TimestampExtended = 0;
tag.StreamID.setSwap(0);
fwrite(&tag, 1, sizeof(TFlvTag), stdout);
buffer.Write(tag);

// Format 3: linear PCM, stores raw PCM samples.
// If the data is 8 - bit, the samples are unsigned bytes.
Expand All @@ -530,13 +537,14 @@ void FlvStream::WriteAudio(const std::vector<uint8_t>& packet)
audData.SoundRate = 1; // 11khz
audData.SoundSize = 1;
audData.SoundType = 0;
fwrite(&audData, 1, sizeof(audData), stdout);
buffer.Write(audData);

fwrite(pcmData.data(), 1, nPcmData, stdout);
buffer.Write(pcmData.data(), nPcmData);

uint32_t prevTagSize = bswap_u32(sizeof(tag) + sizeof (audData) + nPcmData);
fwrite(&prevTagSize, 1, sizeof(uint32_t), stdout);
uint32_t prevTagSize = bswap_u32(sizeof(tag) + sizeof(audData) + nPcmData);
buffer.Write(prevTagSize);

fwrite(buffer.GetBuffer().data(), 1, buffer.GetBuffer().size(), stdout);
fflush(stdout);
}

Expand Down
39 changes: 39 additions & 0 deletions v380/UtlBuffer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once

class UtlBuffer
{
private:
std::vector<uint8_t> m_Buff;
uint32_t m_Offset;

public:
UtlBuffer()
: m_Offset(0)
{}

std::vector<uint8_t>& GetBuffer()
{
return m_Buff;
}

uint32_t Write(const void* pubData, uint32_t cubData)
{
m_Buff.resize(m_Buff.size() + cubData);
memcpy_s(m_Buff.data() + m_Offset, cubData, pubData, cubData);
m_Offset += cubData;

return cubData;
}

template<class T>
uint32_t Write(const std::vector<T>& data)
{
return Write(data.data(), data.size());
}

template<class T>
uint32_t Write(const T& data)
{
return Write(&data, sizeof(T));
}
};
1 change: 1 addition & 0 deletions v380/stdafx.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,4 @@ static int WSAGetLastError()
#include <condition_variable>

#include "UtlSemaphore.h"
#include "UtlBuffer.h"
1 change: 1 addition & 0 deletions v380/v380.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@
<ClInclude Include="FlvStream.h" />
<ClInclude Include="stdafx.h" />
<ClInclude Include="targetver.h" />
<ClInclude Include="UtlBuffer.h" />
<ClInclude Include="UtlDiscovery.h" />
<ClInclude Include="UtlSemaphore.h" />
<ClInclude Include="UtlSocket.h" />
Expand Down
3 changes: 3 additions & 0 deletions v380/v380.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
<ClInclude Include="UtlSemaphore.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="UtlBuffer.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="stdafx.cpp">
Expand Down

0 comments on commit 1259261

Please sign in to comment.