diff options
author | Matthias P. Braendli <matthias.braendli@mpb.li> | 2013-12-22 16:11:43 +0100 |
---|---|---|
committer | Matthias P. Braendli <matthias.braendli@mpb.li> | 2013-12-24 18:09:27 +0100 |
commit | b0183733ef4c6f08ef8c7b73155268e54ca3f152 (patch) | |
tree | 5aa855d5dc36badbf1b2268bef79db0257146853 | |
parent | 7094945ef7ecb8a918dfe3b4619bdc6f239cbb50 (diff) | |
download | dabmux-b0183733ef4c6f08ef8c7b73155268e54ca3f152.tar.gz dabmux-b0183733ef4c6f08ef8c7b73155268e54ca3f152.tar.bz2 dabmux-b0183733ef4c6f08ef8c7b73155268e54ca3f152.zip |
InputBuffered know how to split frames
-rw-r--r-- | src/inputs/Input.h | 53 | ||||
-rw-r--r-- | src/inputs/InputBuffered.cpp | 27 | ||||
-rw-r--r-- | src/inputs/InputDabplusFile.cpp | 5 | ||||
-rw-r--r-- | src/inputs/InputFile.cpp | 3 | ||||
-rw-r--r-- | src/inputs/TcpLog.h | 23 | ||||
-rw-r--r-- | src/inputs/Test.cpp | 39 | ||||
-rwxr-xr-x | src/inputs/test.sh | 3 |
7 files changed, 126 insertions, 27 deletions
diff --git a/src/inputs/Input.h b/src/inputs/Input.h index a6a7231..fe54160 100644 --- a/src/inputs/Input.h +++ b/src/inputs/Input.h @@ -30,6 +30,7 @@ # include "config.h" #endif +#include <stdexcept> #include <vector> #include <list> #include <string> @@ -38,18 +39,15 @@ class InputBase { public: - virtual int Open(); - - // Must return a string with a name, used for logging - virtual const char* GetName(); + virtual int Open() = 0; // ReadFrame must return exactly the right number of bytes // for the format, which means it must fill the buffer to // <size> bytes. - virtual int ReadFrame(void* buffer, int size); + virtual int ReadFrame(void* buffer, int size) = 0; - virtual int Close(); - virtual int Clean(); // TODO destructor + virtual int Close() = 0; + //virtual int Clean() = 0; // TODO destructor }; enum BufferState @@ -58,30 +56,38 @@ enum BufferState RUNNING }; -class InputBuffered : public InputBase { - public: - virtual int ReadFrame(void* buffer, int size); +class InputBuffered { protected: InputBuffered(int prebuffer_stages, int source_size, - int overfull_thresh) : + 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); + 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: - InputBuffered() {}; - 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<char> > m_buffer; @@ -95,12 +101,15 @@ class InputBuffered : public InputBase { // The InputFile is not used directly but defines some behaviour // for Fifo, and also for different file types. -class InputFile : InputBase { +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 @@ -111,10 +120,10 @@ class InputMpegFile : public InputFile, public InputBuffered { public: InputMpegFile(std::string fileName) : InputFile(fileName), - InputBuffered(prebuffer_stages, source_size, overfull_thresh) + InputBuffered(prebuffer_stages, source_size, overfull_thresh, 1) { m_name = "mpeg " + m_filename;} int ReadFrame(void* buffer, int size); - const char* GetName(); + const char* GetName() { return m_name.c_str(); } private: std::string m_name; int ReadSource(void* buffer, int size); @@ -131,14 +140,14 @@ class InputDabplusFile : public InputFile, public InputBuffered { int frame_size, int overfull_thresh) : InputFile(fileName), - InputBuffered(prebuffer_stages, frame_size, overfull_thresh) + InputBuffered(prebuffer_stages, frame_size, overfull_thresh, 5) { m_name = "dabplus " + m_filename;} int ReadFrame(void* buffer, int size); - const char* GetName(); + 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); - int ReadSource(void* buffer, int size); std::vector<uint8_t> buffer; size_t bufferIndex; }; @@ -151,6 +160,7 @@ class InputDabplusFile : public InputFile, public InputBuffered { /** TEST INPUT ***********************/ /********************************************/ +#if 0 // Implement mostly everything, but don't do anything class InputTest : InputBase { public: @@ -164,6 +174,7 @@ class InputTest : InputBase { private: unsigned long counter; }; +#endif #endif diff --git a/src/inputs/InputBuffered.cpp b/src/inputs/InputBuffered.cpp index 9eea2a7..093220b 100644 --- a/src/inputs/InputBuffered.cpp +++ b/src/inputs/InputBuffered.cpp @@ -27,11 +27,13 @@ #include "inputs/Input.h" +#ifdef HAVE_INPUT_FILE #include "TcpLog.h" #include <string> #include <cstring> +#include <iostream> #include <cstdlib> #include <cstdio> #include <fcntl.h> @@ -44,7 +46,7 @@ extern TcpLog etiLog; -int InputBuffered::ReadFrame(void* buffer, int size) +int InputBuffered::ReadFrameFromBuffer(void* buffer, int size) { int retval = 0; bool rc; @@ -115,11 +117,26 @@ bool InputBuffered::FillBuffer() } else { // copy the input frame into the frame_buffer one to one - std::vector<char> frame(m_source_size); - int rc = ReadSource(&frame.front(), m_source_size); + std::vector<char> superframe(m_source_size); + int rc = ReadSource(&superframe.front(), m_source_size); if (rc == m_source_size) { - m_buffer.push_back(frame); + int frame_size = m_source_size/m_framesplit; + std::vector<char> frame(frame_size); + + for (char* framestart = &superframe.front(); + framestart < &superframe[frame_size]; + framestart += frame_size) { + memcpy(&frame.front(), framestart, frame_size); + m_buffer.push_back(frame); + } + + int i = 0; + for (std::list< std::vector<char> >::iterator it = m_buffer.begin(); + it != m_buffer.end(); ++it, i++) + { + cout << "FRAME " << dec << i << " size " << it->size() << endl; + } return true; } else @@ -129,3 +146,5 @@ bool InputBuffered::FillBuffer() } } +#endif // HAVE_INPUT_FILE + diff --git a/src/inputs/InputDabplusFile.cpp b/src/inputs/InputDabplusFile.cpp index ad76f9c..45ce579 100644 --- a/src/inputs/InputDabplusFile.cpp +++ b/src/inputs/InputDabplusFile.cpp @@ -108,6 +108,11 @@ int InputDabplusFile::ReadSource(void* buffer, int size) return size; } +int InputDabplusFile::ReadFrame(void* buffer, int size) +{ + return ReadFrameFromBuffer(buffer, size); +} + #endif #endif diff --git a/src/inputs/InputFile.cpp b/src/inputs/InputFile.cpp index da45456..6b56cc5 100644 --- a/src/inputs/InputFile.cpp +++ b/src/inputs/InputFile.cpp @@ -42,6 +42,7 @@ int InputFile::Open() { + fprintf(stderr, "Opening %s\n", m_filename.c_str()); this->file = open(this->m_filename.c_str(), O_RDONLY | O_BINARY); if (this->file == -1) { perror(m_filename.c_str()); @@ -50,7 +51,6 @@ int InputFile::Open() return 0; } -#if 0 /** * This function replace the read function by trying many times a reading. * It tries to read until all bytes are read. Very useful when reading from a @@ -86,7 +86,6 @@ long InputFile::ReadData(void* data, size_t size, unsigned int tries) offset += result; return offset; } -#endif int InputFile::Rewind() diff --git a/src/inputs/TcpLog.h b/src/inputs/TcpLog.h new file mode 100644 index 0000000..65cd41b --- /dev/null +++ b/src/inputs/TcpLog.h @@ -0,0 +1,23 @@ +#ifndef _T_ +#define _T_ + +#include <cstdio> + +using namespace std; + +class TcpLog +{ + public: + const static int WARNING=1; + const static int ERR=2; + const static int NOTICE=3; + const static int CRIT=4; + + void print(const int priority, const char *format, ...) + { + fprintf(stderr, "%s\n", format); + } +}; + +#endif + diff --git a/src/inputs/Test.cpp b/src/inputs/Test.cpp new file mode 100644 index 0000000..925f871 --- /dev/null +++ b/src/inputs/Test.cpp @@ -0,0 +1,39 @@ +#include <string> +#include <iostream> +#include <vector> +#include "TcpLog.h" +#include "Input.h" + +using namespace std; + +TcpLog etiLog; + +int main(int argc, char** argv) +{ + const int bufsize = 188; + const int num = 10; + const string fname = "/home/bram/dab/mmbtools-aux/fb.dab"; + + cout << "Hello" << endl; + + InputDabplusFile input(fname, 1, 5*bufsize, 100); + //InputFile input(fname); + + cout << "Open " << input.Open() << endl; + + cout << "Opened " << input.GetName() << endl; + + std::vector<uint8_t> buffer(bufsize); + + for (int i = 0; i < num; i++) { + int rc = input.ReadFrame(&buffer.front(), bufsize); + + cout << "ReadFrame " << dec << rc << endl; + + for(std::vector<uint8_t>::iterator it = buffer.begin(); it != buffer.end(); ++it) { + cout << hex << (unsigned int)*it << ", "; + } + } + cout << endl; + +} diff --git a/src/inputs/test.sh b/src/inputs/test.sh new file mode 100755 index 0000000..c652cb8 --- /dev/null +++ b/src/inputs/test.sh @@ -0,0 +1,3 @@ +#!/bin/sh +clang++ -Wall -I.. -I../.. -I../../.. -includeTcpLog.h -DHAVE_INPUT_FILE -DHAVE_FORMAT_DABPLUS InputBuffered.cpp InputFile.cpp InputDabplusFile.cpp Test.cpp -o test \ +&& ./test |