/*
Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Her Majesty the Queen in
Right of Canada (Communications Research Center Canada)
Copyright (C) 2013 Matthias P. Braendli
http://mpb.li
*/
/*
This file is part of CRC-DabMux.
CRC-DabMux is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
CRC-DabMux is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with CRC-DabMux. If not, see .
*/
#ifndef _INPUT_H_
#define _INPUT_H_
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include
#include
#include
#include
#include
class InputBase {
public:
virtual int Open() = 0;
// ReadFrame must return exactly the right number of bytes
// for the format, which means it must fill the buffer to
// bytes.
virtual int ReadFrame(void* buffer, int size) = 0;
virtual int Close() = 0;
//virtual int Clean() = 0; // TODO destructor
};
enum BufferState
{
PREBUFFERING = 0,
RUNNING
};
class InputBuffered {
protected:
InputBuffered(int prebuffer_stages, int source_size,
int overfull_thresh, unsigned int frame_split) :
m_source_size(source_size),
m_prebuffer_count(prebuffer_stages),
m_prebuffer_stages(prebuffer_stages),
m_overfull_thresh(overfull_thresh),
m_framesplit(frame_split),
m_bufferstate(PREBUFFERING)
{
if (source_size % frame_split != 0) {
throw invalid_argument("Incorrect source size asked: ");
}
};
virtual int ReadFrameFromBuffer(void* buffer, int size);
// Reads a frame from the source, into the buffer
virtual int ReadSource(void* buffer, int size) = 0;
// Must return a string with a name, used for logging
virtual const char* GetName() = 0;
virtual bool FillBuffer();
private:
int m_source_size;
int m_prebuffer_count;
int m_prebuffer_stages;
int m_overfull_thresh;
unsigned int m_framesplit;
BufferState m_bufferstate;
std::list< std::vector > m_buffer;
};
/********************************************/
/** FILE INPUTS ***********************/
/********************************************/
#ifdef HAVE_INPUT_FILE
// The InputFile is not used directly but defines some behaviour
// for Fifo, and also for different file types.
class InputFile : public InputBase {
public:
InputFile(std::string fileName) : m_filename(fileName) {}
virtual int Open();
virtual int Close();
virtual int Rewind();
int ReadFrame(void* buffer, int size) { return ReadData(buffer, size, 10); };
long ReadData(void* data, size_t size, unsigned int tries);
protected:
std::string m_filename;
int file; // the file descriptor
};
#ifdef HAVE_FORMAT_MPEG
class InputMpegFile : public InputFile, public InputBuffered {
public:
InputMpegFile(std::string fileName,
int prebuffer_stages,
int frame_size,
int overfull_thresh) :
InputFile(fileName),
InputBuffered(prebuffer_stages, frame_size, overfull_thresh, 1)
{ m_name = "mpeg " + m_filename;}
int ReadFrame(void* buffer, int size);
const char* GetName() { return m_name.c_str(); }
private:
std::string m_name;
int ReadSource(void* buffer, int size);
bool parity;
};
#endif // HAVE_FORMAT_MPEG
#ifdef HAVE_FORMAT_DABPLUS
class InputDabplusFile : public InputFile, public InputBuffered {
public:
InputDabplusFile(
std::string fileName,
int prebuffer_stages,
int frame_size,
int overfull_thresh) :
InputFile(fileName),
InputBuffered(prebuffer_stages, frame_size, overfull_thresh, 5)
{ m_name = "dabplus " + m_filename;}
int ReadFrame(void* buffer, int size);
const char* GetName() { return m_name.c_str(); }
private:
int ReadSource(void* buffer, int size);
std::string m_name;
int ReadSuperFrame(void* buffer, int size);
std::vector buffer;
size_t bufferIndex;
};
#endif
#endif // INPUT_FILE
/********************************************/
/** TEST INPUT ***********************/
/********************************************/
#if 0
// Implement mostly everything, but don't do anything
class InputTest : InputBase {
public:
InputTest() : counter(0) {}
int Open();
int ReadFrame(void* buffer, int size);
const char* GetName();
int Close();
int Clean();
private:
unsigned long counter;
};
#endif
#endif