diff --git a/v380/FlvStream.cpp b/v380/FlvStream.cpp index 98c12a1..0816462 100644 --- a/v380/FlvStream.cpp +++ b/v380/FlvStream.cpp @@ -303,6 +303,8 @@ void FlvStream::WriteVideo(const std::vector& packet, bool keyframe) return; } + UtlBuffer buffer; + TFlvTag tag; TFlvVideoData vidData; TFlvAvcVideoPacket avc; @@ -422,17 +424,19 @@ void FlvStream::WriteVideo(const std::vector& 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 @@ -443,23 +447,24 @@ void FlvStream::WriteVideo(const std::vector& 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); } @@ -481,12 +486,14 @@ void FlvStream::WriteAudio(const std::vector& packet) return; } + UtlBuffer buffer; + TFlvTag tag; TFlvAudioData audData; std::vector packetOnly(packet.begin() + 18, packet.end()); std::vector 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); @@ -520,7 +527,7 @@ void FlvStream::WriteAudio(const std::vector& 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. @@ -530,13 +537,14 @@ void FlvStream::WriteAudio(const std::vector& 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); } diff --git a/v380/UtlBuffer.h b/v380/UtlBuffer.h new file mode 100644 index 0000000..f5db6c9 --- /dev/null +++ b/v380/UtlBuffer.h @@ -0,0 +1,39 @@ +#pragma once + +class UtlBuffer +{ +private: + std::vector m_Buff; + uint32_t m_Offset; + +public: + UtlBuffer() + : m_Offset(0) + {} + + std::vector& 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 + uint32_t Write(const std::vector& data) + { + return Write(data.data(), data.size()); + } + + template + uint32_t Write(const T& data) + { + return Write(&data, sizeof(T)); + } +}; diff --git a/v380/stdafx.h b/v380/stdafx.h index aad25bc..ccccc21 100644 --- a/v380/stdafx.h +++ b/v380/stdafx.h @@ -61,3 +61,4 @@ static int WSAGetLastError() #include #include "UtlSemaphore.h" +#include "UtlBuffer.h" diff --git a/v380/v380.vcxproj b/v380/v380.vcxproj index 6d7415e..564463c 100644 --- a/v380/v380.vcxproj +++ b/v380/v380.vcxproj @@ -149,6 +149,7 @@ + diff --git a/v380/v380.vcxproj.filters b/v380/v380.vcxproj.filters index 417fa2d..90f00c6 100644 --- a/v380/v380.vcxproj.filters +++ b/v380/v380.vcxproj.filters @@ -39,6 +39,9 @@ Header Files + + Header Files +