VideoCodec/Modules/Input/StreamInputModule.cpp
#include "StreamInputModule.h"
namespace VideoCodec
{
StreamInputModule::StreamInputModule() : Module(STREAM_INPUT_MODULE_ID)
{
this->inputFile = NULL;
}
StreamInputModule::~StreamInputModule()
{
if (this->inputFile != NULL)
this->CloseFile();
}
ConfigurationStatus StreamInputModule::Configure(ConfigurationElement* configuration)
{
if (configuration->GetType() == CONFIGURATION_PAIRS)
{
std::string* value = configuration->GetValue("file")->GetString();
if (value == NULL)
{
Logger::LogMessage("An input file name must be specified.");
return MODULE_CONFIGURATION_ERROR;
}
this->fileName = *value;
return MODULE_CONFIGURATION_OK;
}
else
{
return MODULE_CONFIGURATION_ERROR;
}
}
void StreamInputModule::OpenFile()
{
if (this->inputFile != NULL)
Logger::LogMessage("Warning: A file was already open.");
// Open the specified file for reading.
this->inputFile = fopen(fileName.c_str(), "r");
if (this->inputFile == NULL)
Logger::LogMessage("Warning: No file was opened.");
}
void StreamInputModule::CloseFile()
{
if (this->inputFile == NULL)
Logger::LogMessage("Warning: No file was open.");
// Close the file that was previously open and set it to be NULL.
fclose(this->inputFile);
this->inputFile = NULL;
}
unsigned long int StreamInputModule::Read(void* data, size_t size, unsigned long int count)
{
unsigned long int readCount;
// If zero elements are read in, there is probably a problem. Otherwise the end of the file was probably reached, or all the required bits were read in.
if ((readCount = fread(data, size, count, this->inputFile)) != count && readCount == 0)
Logger::LogMessage("Warning: Stream input module did not read in any data.");
// Re-order the bytes.
unsigned char byteCount = (unsigned char)size;
unsigned char* byteData = (unsigned char*)data;
for (unsigned long int index = 0; index < count; index++)
{
for (int i = 0; i < (byteCount >> 1); i++)
{
unsigned char t = byteData[i];
byteData[i] = byteData[byteCount - i - 1];
byteData[byteCount - i - 1] = t;
}
byteData += byteCount;
}
return count;
}
}