Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing the FileReaderModule issue #395

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions base/include/FileSequenceDriver.h
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class FileSequenceDriver

bool Read(uint8_t*& dataToRead, size_t& dataSize, uint64_t& index);
bool ReadP(BufferMaker& buffMaker, uint64_t& index);
bool ReadP(BufferMaker& buffMaker, uint64_t& index, size_t& userMetadataSize);
bool Write(const uint8_t* dataToWrite, size_t dataSize);

void SetReadLoop(bool readLoop);
Expand Down
27 changes: 22 additions & 5 deletions base/src/FileReaderModule.cpp
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,29 @@ bool FileReaderModule::produce()
}

FFBufferMaker buffMaker(*this);

uint64_t fIndex2 = 0;
if (!mDriver->ReadP(buffMaker, fIndex2))
{
return false;
}

auto metadata = getOutputFrameFactory().begin()->second->getFrameMetadata();
size_t userMetadataSize = metadata->getDataSize();

// Handle cases where dataSize is not set
if (!metadata->isSet() || userMetadataSize == NOT_SET_NUM)
{
// Use file size as buffer size
if (!mDriver->ReadP(buffMaker, fIndex2))
{
return false;
}
}
else
{
// Use metadata's dataSize
if (!mDriver->ReadP(buffMaker, fIndex2, userMetadataSize))
{
return false;
}
}

auto frame = buffMaker.getFrame();
frame->fIndex2 = fIndex2;

Expand Down
54 changes: 52 additions & 2 deletions base/src/FileSequenceDriver.cpp
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ void FileSequenceDriver::jump(uint64_t index)
bool FileSequenceDriver::ReadP(BufferMaker& buffMaker, uint64_t& index)
{
bool readRes = false;

const std::string fileNameToUse = mStrategy->GetFileNameToUse(true, index);

if (!fileNameToUse.empty())
Expand All @@ -106,7 +106,57 @@ bool FileSequenceDriver::ReadP(BufferMaker& buffMaker, uint64_t& index)
readRes = true;

}
else {
else
{
LOG_ERROR << "FileSequenceDriver::Read can not read file " << fileNameToUse;
}

file.close();
}
}

return readRes;
}

bool FileSequenceDriver::ReadP(BufferMaker& buffMaker, uint64_t& index, size_t &userMetadataSize)
{
bool readRes = false;

const std::string fileNameToUse = mStrategy->GetFileNameToUse(true, index);

if (!fileNameToUse.empty())
{
LOG_TRACE << "FileSequenceDriver::Reading File " << fileNameToUse;

std::ifstream file(fileNameToUse.c_str(), std::ios::in | std::ios::binary | std::ios::ate);
if (file.is_open())
{
//ios::ate implies that file has been seeked to the end
//so tellg should give total size
size_t fileSize = static_cast<size_t>(file.tellg());
if (fileSize > 0U)
{
// Determine buffer size
size_t bufferSize = fileSize;
if (userMetadataSize > fileSize)
{
bufferSize = userMetadataSize; // Resize frame to userMetadataSize
}
else
{
LOG_WARNING << "File size is larger than user metadata size. Reading the entire file.";
}

auto dataToRead = buffMaker.make(bufferSize);
file.seekg(0, std::ios::beg);
file.read((char*)dataToRead, fileSize);

LOG_TRACE << "FileSequenceDriver::Read " << fileSize << " Bytes ";

readRes = true;
}
else
{
LOG_ERROR << "FileSequenceDriver::Read can not read file " << fileNameToUse;
}

Expand Down
43 changes: 43 additions & 0 deletions base/test/filereadermodule_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,49 @@

BOOST_AUTO_TEST_SUITE(filereadermodule_tests)

BOOST_AUTO_TEST_CASE(metadatasize)
{
auto fileReader = boost::shared_ptr<FileReaderModule>(new FileReaderModule(FileReaderModuleProps("./data/RGB_320x180.raw")));

// Custom RawImageMetadata configurations
int width = 500;
int height = 500;
int channels = 3;
int type = CV_8UC3;
size_t alignLength = 1;
int depth = CV_8U;
auto rawImageMetadata = framemetadata_sp(new RawImageMetadata(width, height, ImageMetadata::RGB, type, alignLength, depth, FrameMetadata::HOST, true));
auto expectedDataSize = width * height * channels;

auto pinId = fileReader->addOutputPin(rawImageMetadata);

auto sink = boost::shared_ptr<ExternalSinkModule>(new ExternalSinkModule());
fileReader->setNext(sink);

BOOST_TEST(fileReader->init());
BOOST_TEST(sink->init());

fileReader->play(true);

fileReader->step();
auto frames = sink->pop();

BOOST_TEST((frames.find(pinId) != frames.end()));

auto frame = frames[pinId];

BOOST_TEST(frame->size() == expectedDataSize);

// Print the size of the frame and the metadata size
std::cout << "Frame size: " << frame->size() << std::endl;
std::cout << "Metadata size: " << rawImageMetadata->getDataSize() << std::endl;
std::cout << "FrameType: " << rawImageMetadata->getFrameType() << std::endl;
std::cout << "MemType: " << rawImageMetadata->getMemType() << std::endl;

fileReader->term();
sink->term();
}

BOOST_AUTO_TEST_CASE(basic)
{
auto fileReader = boost::shared_ptr<FileReaderModule>(new FileReaderModule(FileReaderModuleProps("./data/filenamestrategydata/?.txt")));
Expand Down
Loading