Skip to content

Commit

Permalink
Merge branch 'main' into yuyvmeta
Browse files Browse the repository at this point in the history
  • Loading branch information
kumaakh committed Feb 1, 2023
2 parents 993e734 + 5516641 commit d0cdd28
Show file tree
Hide file tree
Showing 10 changed files with 181 additions and 132 deletions.
18 changes: 9 additions & 9 deletions base/include/Mp4WriterSink.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ class DetailH264;
class Mp4WriterSinkProps : public ModuleProps
{
public:
Mp4WriterSinkProps(uint32_t _chunkTime, uint32_t _syncTime, uint16_t _fps, std::string _baseFolder) : ModuleProps()
Mp4WriterSinkProps(uint32_t _chunkTime, uint32_t _syncTimeInSecs, uint16_t _fps, std::string _baseFolder) : ModuleProps()
{
baseFolder = _baseFolder;
fps = _fps;
if (_chunkTime >= 1 && _chunkTime <= 60)
if ((_chunkTime >= 1 && _chunkTime <= 60) || (_chunkTime == UINT32_MAX))
{
chunkTime = _chunkTime;
}
else
{
throw AIPException(AIP_FATAL, "ChuntTime should be within [1,60] minutes limit");
throw AIPException(AIP_FATAL, "ChuntTime should be within [1,60] minutes limit or UINT32_MAX");
}
if (_syncTime >= 1 && _syncTime <= 60)
if (_syncTimeInSecs >= 1 && _syncTimeInSecs <= 60)
{
syncTime = _syncTime;
syncTimeInSecs = _syncTimeInSecs;
}
else
{
Expand All @@ -35,7 +35,7 @@ class Mp4WriterSinkProps : public ModuleProps
{
baseFolder = "./data/mp4_videos/";
chunkTime = 1; //minutes
syncTime = 1;
syncTimeInSecs = 1;
fps = 30;
}

Expand All @@ -44,13 +44,13 @@ class Mp4WriterSinkProps : public ModuleProps
return ModuleProps::getSerializeSize() +
sizeof(baseFolder) +
sizeof(chunkTime) +
sizeof(syncTime) +
sizeof(syncTimeInSecs) +
sizeof(fps);
}

std::string baseFolder;
uint32_t chunkTime = 1;
uint32_t syncTime = 1;
uint32_t syncTimeInSecs = 1;
uint16_t fps = 30;
private:
friend class boost::serialization::access;
Expand All @@ -61,7 +61,7 @@ class Mp4WriterSinkProps : public ModuleProps
ar &boost::serialization::base_object<ModuleProps>(*this);
ar &baseFolder;
ar &chunkTime;
ar &syncTime;
ar &syncTimeInSecs;
ar &fps;
}
};
Expand Down
1 change: 1 addition & 0 deletions base/include/Mp4WriterSinkUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class Mp4WriterSinkUtils
boost::filesystem::path &relPath, std::string &mp4FileName, bool &syncFlag, std::string baseFolder, std::string& nextFrameFileName);
void parseTSH264(uint64_t& tm, uint32_t& chunkTimeInMinutes, uint32_t& syncTimeInSeconds,boost::filesystem::path& relPath,
std::string& mp4FileName, bool& syncFlag,short frameType, short naluType, std::string baseFolder, std::string& nextFrameFileName);
bool customNamedFileDirCheck(std::string baseFolder, uint32_t chunkTimeInMinutes, boost::filesystem::path relPath, std::string& nextFrameFileName);
std::string format_hrs(int &hr);
std::string format_2(int &min);
std::string filePath(boost::filesystem::path relPath, std::string mp4FileName, std::string baseFolder);
Expand Down
25 changes: 21 additions & 4 deletions base/src/Mp4WriterSink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class DetailAbs

void setProps(Mp4WriterSinkProps& _props)
{
mProps.reset(new Mp4WriterSinkProps(_props.chunkTime, _props.syncTime, _props.fps, _props.baseFolder));
mProps.reset(new Mp4WriterSinkProps(_props.chunkTime, _props.syncTimeInSecs, _props.fps, _props.baseFolder));
}

~DetailAbs()
Expand Down Expand Up @@ -149,6 +149,17 @@ class DetailAbs
return !mInputMetadata.get();
}

void addMetadataInVideoHeader(frame_sp inFrame)
{
if (!lastFrameTS)
{
/* \251sts -> ©sts */
std::string key = "\251sts";
std::string val = std::to_string(inFrame->timestamp);
mp4_mux_add_file_metadata(mux, key.c_str(), val.c_str());
}
}

boost::shared_ptr<Mp4WriterSinkProps> mProps;
bool mMetadataEnabled = false;
bool isKeyFrame;
Expand Down Expand Up @@ -230,7 +241,7 @@ bool DetailJpeg::write(frame_container& frames)
short naluType = 0;
std::string _nextFrameFileName;
mWriterSinkUtils.getFilenameForNextFrame(_nextFrameFileName, inJpegImageFrame->timestamp, mProps->baseFolder,
mProps->chunkTime, mProps->syncTime, syncFlag ,mFrameType, naluType);
mProps->chunkTime, mProps->syncTimeInSecs, syncFlag ,mFrameType, naluType);

if (_nextFrameFileName == "")
{
Expand All @@ -249,6 +260,9 @@ bool DetailJpeg::write(frame_container& frames)
mp4_mux_sync(mux);
syncFlag = false;
}

addMetadataInVideoHeader(inJpegImageFrame);

mux_sample.buffer = static_cast<uint8_t*>(inJpegImageFrame->data());
mux_sample.len = inJpegImageFrame->size();
mux_sample.sync = 0;
Expand Down Expand Up @@ -310,7 +324,7 @@ bool DetailH264::write(frame_container& frames)

std::string _nextFrameFileName;
mWriterSinkUtils.getFilenameForNextFrame(_nextFrameFileName,inH264ImageFrame->timestamp, mProps->baseFolder,
mProps->chunkTime, mProps->syncTime, syncFlag,mFrameType, typeFound);
mProps->chunkTime, mProps->syncTimeInSecs, syncFlag,mFrameType, typeFound);

if (_nextFrameFileName == "")
{
Expand Down Expand Up @@ -338,6 +352,9 @@ bool DetailH264::write(frame_container& frames)
{
isKeyFrame = false;
}

addMetadataInVideoHeader(inH264ImageFrame);

mux_sample.buffer = static_cast<uint8_t*>(inH264ImageFrame->data());
mux_sample.len = inH264ImageFrame->size();
mux_sample.sync = isKeyFrame ? 1 : 0;
Expand Down Expand Up @@ -518,7 +535,7 @@ bool Mp4WriterSink::processEOS(string& pinId)

Mp4WriterSinkProps Mp4WriterSink::getProps()
{
auto tempProps = Mp4WriterSinkProps(mDetail->mProps->chunkTime, mDetail->mProps->syncTime, mDetail->mProps->fps, mDetail->mProps->baseFolder);
auto tempProps = Mp4WriterSinkProps(mDetail->mProps->chunkTime, mDetail->mProps->syncTimeInSecs, mDetail->mProps->fps, mDetail->mProps->baseFolder);
fillProps(tempProps);
return tempProps;
}
Expand Down
71 changes: 58 additions & 13 deletions base/src/Mp4WriterSinkUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,44 @@ std::string Mp4WriterSinkUtils::filePath(boost::filesystem::path relPath, std::s
}
}

bool Mp4WriterSinkUtils::customNamedFileDirCheck(std::string baseFolder, uint32_t chunkTimeInMinutes, boost::filesystem::path relPath, std::string& nextFrameFileName)
{
if (boost::filesystem::extension(baseFolder) == ".mp4")
{
if (chunkTimeInMinutes == UINT32_MAX)
{
auto folderPath = boost::filesystem::path(baseFolder) / relPath;
auto path = folderPath.remove_filename();
if (boost::filesystem::is_directory(path))
{
nextFrameFileName = baseFolder;
return true;
}

if (boost::filesystem::create_directories(path))
{
nextFrameFileName = baseFolder;
return true;
}
else
{
LOG_ERROR << "Failed to create the directory <" << folderPath << ">";
LOG_ERROR << "Check the dir permissions.";
return true;
}
}
else
{
LOG_ERROR << "Custom video file name only supported while writing to a single file.";
throw AIPException(AIP_FATAL, "Custom video file name only supported while writing to a single file.");
}
}
else
{
return false;
}
}

void Mp4WriterSinkUtils::parseTSJpeg(uint64_t &ts, uint32_t &chunkTimeInMinutes, uint32_t &syncTimeInSeconds,
boost::filesystem::path &relPath, std::string &mp4FileName, bool &syncFlag, std::string baseFolder, std::string& nextFrameFileName)
{
Expand All @@ -83,24 +121,27 @@ void Mp4WriterSinkUtils::parseTSJpeg(uint64_t &ts, uint32_t &chunkTimeInMinutes,

// used cached values if the difference in ts is less than chunkTime
uint32_t chunkTimeInSecs = 60 * chunkTimeInMinutes;
if ((t - lastVideoTS) < chunkTimeInSecs && currentFolder == baseFolder)
if ((t - lastVideoTS) < chunkTimeInSecs && currentFolder == baseFolder && chunkTimeInMinutes != UINT32_MAX)
{
relPath = lastVideoFolderPath;
mp4FileName = lastVideoName;
nextFrameFileName = filePath(relPath, mp4FileName, baseFolder);
return;
}

// get new video path
std::string yyyymmdd = std::to_string(1900 + tm.tm_year) + format_2(tm.tm_mon) + format_2(tm.tm_mday);
relPath = boost::filesystem::path(yyyymmdd) / format_hrs(tm.tm_hour);
mp4FileName = std::to_string(ts) + ".mp4";

// cache new values
currentFolder = baseFolder;
lastVideoTS = t;
lastVideoFolderPath = relPath;
lastVideoMinute = tm.tm_min;

if (customNamedFileDirCheck(baseFolder, chunkTimeInMinutes, relPath, nextFrameFileName))
return;

std::string yyyymmdd = std::to_string(1900 + tm.tm_year) + format_2(tm.tm_mon) + format_2(tm.tm_mday);
relPath = boost::filesystem::path(yyyymmdd) / format_hrs(tm.tm_hour);
mp4FileName = std::to_string(ts) + ".mp4";
lastVideoFolderPath = relPath;

lastVideoName = mp4FileName;

nextFrameFileName = filePath(relPath, mp4FileName, baseFolder);
Expand Down Expand Up @@ -128,32 +169,36 @@ void Mp4WriterSinkUtils::parseTSH264(uint64_t& ts, uint32_t& chunkTimeInMinutes,
}
// used cached values if the difference in ts is less than chunkTime
uint32_t chunkTimeInSecs = 60 * chunkTimeInMinutes;
if ((t - lastVideoTS) < chunkTimeInSecs && currentFolder == baseFolder)
if ((t - lastVideoTS) < chunkTimeInSecs && currentFolder == baseFolder && chunkTimeInMinutes != UINT32_MAX)
{
relPath = lastVideoFolderPath;
mp4FileName = lastVideoName;
nextFrameFileName = filePath(relPath, mp4FileName, baseFolder);
return;
}
// cannot be merged with if condition above.
if (naluType != H264Utils::H264_NAL_TYPE::H264_NAL_TYPE_IDR_SLICE)
if (naluType != H264Utils::H264_NAL_TYPE::H264_NAL_TYPE_IDR_SLICE && chunkTimeInMinutes != UINT32_MAX)
{
relPath = lastVideoFolderPath;
mp4FileName = lastVideoName;
nextFrameFileName = tempNextFrameFileName;
return;
}
// get new video path
std::string yyyymmdd = std::to_string(1900 + tm.tm_year) + format_2(tm.tm_mon) + format_2(tm.tm_mday);
relPath = boost::filesystem::path(yyyymmdd) / format_hrs(tm.tm_hour);
mp4FileName = std::to_string(ts) + ".mp4";

// cache new values
currentFolder = baseFolder;
lastVideoTS = t;
lastVideoFolderPath = relPath;
lastVideoMinute = tm.tm_min;

if (customNamedFileDirCheck(baseFolder, chunkTimeInMinutes, relPath, nextFrameFileName))
return;

std::string yyyymmdd = std::to_string(1900 + tm.tm_year) + format_2(tm.tm_mon) + format_2(tm.tm_mday);
relPath = boost::filesystem::path(yyyymmdd) / format_hrs(tm.tm_hour);
mp4FileName = std::to_string(ts) + ".mp4";
lastVideoName = mp4FileName;
lastVideoFolderPath = relPath;

nextFrameFileName = filePath(relPath, mp4FileName, baseFolder);
tempNextFrameFileName = nextFrameFileName;
Expand Down
Loading

0 comments on commit d0cdd28

Please sign in to comment.