Created
December 20, 2017 09:53
-
-
Save undali/53c940e491023e2c3513c66d46fcea5b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "LiveStreamingHeader.h" | |
#include <stdio.h> | |
#include <assert.h> | |
void LiveStreamHeader::setAudioFramesSizeList(const std::vector<int> list) | |
{ | |
m_vAudioFrameSizeList = list; | |
} | |
void LiveStreamHeader::setVideoFramesSizeList(const std::vector<int> list) | |
{ | |
m_vVideoFrameSizeList = list; | |
} | |
std::vector<int> LiveStreamHeader::getAudioFramesSizeList() | |
{ | |
return m_vAudioFrameSizeList; | |
} | |
std::vector<int> LiveStreamHeader::getVideoFramesSizeList() | |
{ | |
return m_vVideoFrameSizeList; | |
} | |
int LiveStreamHeader::getHeaderSize() | |
{ | |
return m_iStaticHeaderSize + ((m_vVideoFrameSizeList.size() + m_vAudioFrameSizeList.size()) * singleFrameSizeFieldLength); | |
} | |
void LiveStreamHeader::writeHeaderTo(unsigned char* destination) | |
{ | |
destination[versionFieldPosition] = (unsigned char)m_iVersion; | |
Tools::SetIntegerIntoUnsignedChar(destination, headerLengthPosition, headerLengthFieldLength, m_iHeaderLength); | |
Tools::SetIntegerIntoUnsignedChar(destination, chunkDurationPosition, chunkDurationFieldLength, m_iChunkDuration); | |
Tools::SetIntegerIntoUnsignedChar(destination, relativeTimestampFieldPosition, relativeTimestampFieldLength, m_iRelativeTimestamp); | |
Tools::SetIntegerIntoUnsignedChar(destination, audioSizeFieldPosition, audioSizeFieldLength, m_iAudioDataSize); | |
Tools::SetIntegerIntoUnsignedChar(destination, videoSizeFieldPosition, videoSizeFieldLength, m_iVideoDataSize); | |
Tools::SetIntegerIntoUnsignedChar(destination, audioFrameCountFieldPosition, audioFramesCountFieldLength, m_vAudioFrameSizeList.size()); | |
int dynamicPosition = audioFrameCountFieldPosition + audioFramesCountFieldLength; | |
for (int a : m_vAudioFrameSizeList) | |
{ | |
Tools::SetIntegerIntoUnsignedChar(destination, dynamicPosition, singleFrameSizeFieldLength, a); | |
dynamicPosition += singleFrameSizeFieldLength; | |
} | |
Tools::SetIntegerIntoUnsignedChar(destination, dynamicPosition, videoFrameCountFieldLength, m_vVideoFrameSizeList.size()); | |
dynamicPosition += videoFrameCountFieldLength; | |
for (int a : m_vVideoFrameSizeList) | |
{ | |
Tools::SetIntegerIntoUnsignedChar(destination, dynamicPosition, singleFrameSizeFieldLength, a); | |
dynamicPosition += singleFrameSizeFieldLength; | |
} | |
} | |
void LiveStreamHeader::readHeaderFrom(unsigned char* source) | |
{ | |
clear(); | |
m_iVersion = source[versionFieldPosition]; | |
m_iHeaderLength = Tools::GetIntegerFromUnsignedChar(source, headerLengthPosition, headerLengthFieldLength); | |
m_iChunkDuration = Tools::GetIntegerFromUnsignedChar(source, chunkDurationPosition, chunkDurationFieldLength); | |
m_iRelativeTimestamp = Tools::GetIntegerFromUnsignedChar(source, relativeTimestampFieldPosition, relativeTimestampFieldLength); | |
m_iAudioDataSize = Tools::GetIntegerFromUnsignedChar(source, audioSizeFieldPosition, audioSizeFieldLength); | |
m_iVideoDataSize = Tools::GetIntegerFromUnsignedChar(source, videoSizeFieldPosition, videoSizeFieldLength); | |
int nNumberOfAudioFrame = Tools::GetIntegerFromUnsignedChar(source, audioFrameCountFieldPosition, audioFramesCountFieldLength); | |
int dynamicPosition = audioFrameCountFieldPosition + audioFramesCountFieldLength; | |
for (int i = 0; i < nNumberOfAudioFrame; i++) | |
{ | |
m_vAudioFrameSizeList.push_back(Tools::GetIntegerFromUnsignedChar(source, dynamicPosition, singleFrameSizeFieldLength)); | |
dynamicPosition += singleFrameSizeFieldLength; | |
} | |
int nNumberOfVideoFrame = Tools::GetIntegerFromUnsignedChar(source, dynamicPosition, videoFrameCountFieldLength); | |
dynamicPosition += videoFrameCountFieldLength; | |
for (int i = 0; i < nNumberOfVideoFrame; i++) | |
{ | |
m_vVideoFrameSizeList.push_back(Tools::GetIntegerFromUnsignedChar(source, dynamicPosition, singleFrameSizeFieldLength)); | |
dynamicPosition += singleFrameSizeFieldLength; | |
} | |
} | |
void LiveStreamHeader::clear() | |
{ | |
m_iVersion = m_iRelativeTimestamp = m_iAudioDataSize = m_iVideoDataSize = m_iChunkDuration = m_iHeaderLength = 0; | |
m_vVideoFrameSizeList.clear(); | |
m_vAudioFrameSizeList.clear(); | |
} | |
int main() | |
{ | |
LiveStreamHeader header; | |
unsigned char array[5000]; | |
//memset(array, 0, 500); | |
//return 0; | |
for (int i = 0; i < 250; i++) | |
{ | |
////////////////////////////////////////////// | |
//header.clear(); | |
header.setVersion(i); | |
header.setRelativeTimeStamp(i * 3600 * 24 * 1000); | |
header.setAudioDataSize(i + 3); | |
header.setVideoDataSize(i + 4); | |
header.setChunkDuration(i * 200); | |
std::vector<int> a; | |
for (int k = 0; k < i; k++) | |
{ | |
a.push_back(k); | |
} | |
header.setAudioFramesSizeList(a); | |
header.setVideoFramesSizeList(a); | |
header.writeHeaderTo(array); | |
////////////////////////////////////////////// | |
////////////////////////////////////////////// | |
//header.clear(); | |
printf("## %d", i); | |
header.readHeaderFrom(array); | |
//printf("header#version %d -> %d\n", i, header.getVersion()); | |
assert(i == header.getVersion()); | |
//printf("time %d -> %d\n", i + 2, header.getRelativeTimeStamp()); | |
assert(i * 3600 * 24 * 1000 == header.getRelativeTimeStamp()); | |
//printf("a size %d\n", header.getAudioDataSize()); | |
assert(i + 3 == header.getAudioDataSize()); | |
//printf("v size %d\n", header.getVideoDataSize()); | |
assert(i + 4 == header.getVideoDataSize()); | |
assert(i * 200 == header.getChunkDuration()); | |
assert(i == header.getAudioFramesSizeList().size()); | |
std::vector<int> tmp = header.getAudioFramesSizeList(); | |
for (int p = 0; p < tmp.size(); p++) | |
{ | |
//printf("-> %d ", tmp[p]); | |
assert(tmp[p] == p); | |
} | |
putchar('\n'); | |
tmp = header.getVideoFramesSizeList(); | |
for (int p = 0; p < tmp.size(); p++) | |
{ | |
//printf("-> %d ", tmp[p]); | |
assert(tmp[p] == p); | |
} | |
putchar('\n'); | |
printf("# Header Size %d\n", header.getHeaderSize()); | |
assert(header.getHeaderSize() == 18 + i * 6); | |
////////////////////////////////////////////// | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment