VideoCodec/Modules/Output/StreamOutputModule.cpp
#include "StreamOutputModule.h"
namespace VideoCodec
{
StreamOutputModule::StreamOutputModule() : Module(STREAM_OUTPUT_MODULE_ID)
{
this->outputFile = NULL;
}
StreamOutputModule::~StreamOutputModule()
{
if (this->outputFile != NULL)
this->CloseFile();
}
ConfigurationStatus StreamOutputModule::Configure(ConfigurationElement* configuration)
{
if (configuration->GetType() == CONFIGURATION_PAIRS)
{
std::string* value = configuration->GetValue("file")->GetString();
if (value == NULL)
{
Logger::LogMessage("An output file name must be specified.");
return MODULE_CONFIGURATION_ERROR;
}
this->fileName = *value;
return MODULE_CONFIGURATION_OK;
}
else
{
return MODULE_CONFIGURATION_ERROR;
}
}
void StreamOutputModule::OpenFile()
{
if (this->outputFile != NULL)
Logger::LogMessage("Warning: A file was already open.");
// Open the specified file for writing.
this->outputFile = fopen(this->fileName.c_str(), "w");
if (this->outputFile == NULL)
Logger::LogMessage("Warning: The output file could not be opened for writing! Check that the directory exists and is writeable.");
}
void StreamOutputModule::CloseFile()
{
if (this->outputFile == NULL)
Logger::LogMessage("Warning: No file was open.");
// Close the file which was previously opened and set it to be NULL.
fclose(this->outputFile);
this->outputFile = NULL;
}
void StreamOutputModule::Write(unsigned char* data, unsigned long int length)
{
if (fwrite(data, sizeof(unsigned char), length, this->outputFile) != length)
Logger::LogMessage("Warning: Stream output module did not write out all the stream bits.");
}
void StreamOutputModule::Write(unsigned char value)
{
// Write a single byte to the stream.
if (fwrite(&value, sizeof(unsigned char), 1, this->outputFile) != 1)
Logger::LogMessage("Warning: Stream output module did not write out the value.");
}
void StreamOutputModule::Write(unsigned short int value)
{
// The bytes need to be swapped to be in big-endian form.
unsigned char part = ((value >> 8) & 0x00FF);
if (fwrite(&part, sizeof(unsigned char), 1, this->outputFile) != 1)
Logger::LogMessage("Warning: Stream output module did not write out the value.");
part = (value & 0x00FF);
if (fwrite(&part, sizeof(unsigned char), 1, this->outputFile) != 1)
Logger::LogMessage("Warning: Stream output module did not write out the value.");
}
void StreamOutputModule::Write(unsigned long int value)
{
// The bytes need to be swapped to be in big-endian form.
unsigned char part;
unsigned char shift = 64;
do
{
shift -= 8;
part = ((value >> shift) & 0x00FF);
if (fwrite(&part, sizeof(unsigned char), 1, this->outputFile) != 1)
Logger::LogMessage("Warning: Stream output module did not write out the value.");
}
while (shift != 0);
}
}