From 2751ba9d7f127f4b386ab0f06a05bd0364f28975 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antti=20Sepp=C3=A4l=C3=A4?= Date: Sun, 16 Sep 2007 12:18:15 +0000 Subject: [PATCH] Added simple file reader. --- Makefile | 4 +- device.c | 3 +- device.h | 4 +- protocolfile.c | 105 +++++++++++++++++++++++++++++++++++++++++++++++++ protocolfile.h | 38 ++++++++++++++++++ 5 files changed, 150 insertions(+), 4 deletions(-) create mode 100644 protocolfile.c create mode 100644 protocolfile.h diff --git a/Makefile b/Makefile index 547828b..cf77245 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ # # Makefile for a Video Disk Recorder plugin # -# $Id: Makefile,v 1.5 2007/09/16 09:38:00 ajhseppa Exp $ +# $Id: Makefile,v 1.6 2007/09/16 12:18:15 ajhseppa Exp $ # Debugging on/off #IPTV_DEBUG = 1 @@ -57,7 +57,7 @@ endif ### The object files (add further files here): -OBJS = $(PLUGIN).o config.o setup.o device.o streamer.o protocoludp.o protocolhttp.o +OBJS = $(PLUGIN).o config.o setup.o device.o streamer.o protocoludp.o protocolhttp.o protocolfile.o ### The main target: diff --git a/device.c b/device.c index 28774ff..344e6b7 100644 --- a/device.c +++ b/device.c @@ -3,7 +3,7 @@ * * See the README file for copyright information and how to reach the author. * - * $Id: device.c,v 1.17 2007/09/16 09:38:01 ajhseppa Exp $ + * $Id: device.c,v 1.18 2007/09/16 12:18:15 ajhseppa Exp $ */ #include "common.h" @@ -34,6 +34,7 @@ cIptvDevice::cIptvDevice(unsigned int Index) pUdpProtocol = new cIptvProtocolUdp(); //pRtspProtocol = new cIptvProtocolRtsp(); pHttpProtocol = new cIptvProtocolHttp(); + pFileProtocol = new cIptvProtocolFile(); pIptvStreamer = new cIptvStreamer(tsBuffer, &mutex); StartSectionHandler(); } diff --git a/device.h b/device.h index 57d51dc..128a76a 100644 --- a/device.h +++ b/device.h @@ -3,7 +3,7 @@ * * See the README file for copyright information and how to reach the author. * - * $Id: device.h,v 1.7 2007/09/16 09:38:01 ajhseppa Exp $ + * $Id: device.h,v 1.8 2007/09/16 12:18:15 ajhseppa Exp $ */ #ifndef __IPTV_DEVICE_H @@ -13,6 +13,7 @@ #include "protocoludp.h" //#include "protocolrtsp.h" #include "protocolhttp.h" +#include "protocolfile.h" #include "streamer.h" class cIptvDevice : public cDevice { @@ -33,6 +34,7 @@ private: cIptvProtocolUdp *pUdpProtocol; //cIptvProtocolRtsp *pRtspProtocol; cIptvProtocolHttp *pHttpProtocol; + cIptvProtocolFile *pFileProtocol; cIptvStreamer *pIptvStreamer; cMutex mutex; diff --git a/protocolfile.c b/protocolfile.c new file mode 100644 index 0000000..73253cb --- /dev/null +++ b/protocolfile.c @@ -0,0 +1,105 @@ +/* + * protocolfile.c: IPTV plugin for the Video Disk Recorder + * + * See the README file for copyright information and how to reach the author. + * + * $Id: protocolfile.c,v 1.1 2007/09/16 12:18:15 ajhseppa Exp $ + */ + +#include +#include + +#include + +#include "common.h" +#include "protocolfile.h" + +cIptvProtocolFile::cIptvProtocolFile() +: fileDesc(-1), + readBufferLen(TS_SIZE * 7), + fileActive(false) +{ + debug("cIptvProtocolFile::cIptvProtocolFile()\n"); + streamAddr = strdup(""); + // Allocate receive buffer + readBuffer = MALLOC(unsigned char, readBufferLen); + if (!readBuffer) + error("ERROR: MALLOC() failed in ProtocolFile()"); +} + +cIptvProtocolFile::~cIptvProtocolFile() +{ + debug("cIptvProtocolFile::~cIptvProtocolFile()\n"); + // Drop open handles + Close(); + // Free allocated memory + free(streamAddr); + free(readBuffer); +} + +bool cIptvProtocolFile::OpenFile(void) +{ + debug("cIptvProtocolFile::OpenFile()\n"); + // Check that stream address is valid + if (!fileActive && !isempty(streamAddr)) { + + fileDesc = open(streamAddr, O_RDONLY | O_NOCTTY | O_NONBLOCK); + if (fileDesc < 0) { + char tmp[64]; + error("ERROR: open(): %s", strerror_r(errno, tmp, sizeof(tmp))); + return false; + } + // Update active flag + fileActive = true; + } + return true; +} + +void cIptvProtocolFile::CloseFile(void) +{ + debug("cIptvProtocolFile::CloseFile()\n"); + // Check that stream address is valid + if (fileActive && !isempty(streamAddr)) { + close(fileDesc); + // Update multicasting flag + fileActive = false; + } +} + +int cIptvProtocolFile::Read(unsigned char* *BufferAddr) +{ + //debug("cIptvProtocolFile::Read()\n"); + + return read(fileDesc, readBuffer, readBufferLen); +} + +bool cIptvProtocolFile::Open(void) +{ + debug("cIptvProtocolFile::Open(): streamAddr=%s\n", streamAddr); + // Open the file for input + OpenFile(); + return true; +} + +bool cIptvProtocolFile::Close(void) +{ + debug("cIptvProtocolFile::Close(): streamAddr=%s\n", streamAddr); + // Close the file input + CloseFile(); + return true; +} + +bool cIptvProtocolFile::Set(const char* Address, const int Port) +{ + debug("cIptvProtocolFile::Set(): %s:%d\n", Address, Port); + if (!isempty(Address)) { + // Close the file input + CloseFile(); + // Update stream address and port + streamAddr = strcpyrealloc(streamAddr, Address); + streamPort = Port; + // Open the file for input + OpenFile(); + } + return true; +} diff --git a/protocolfile.h b/protocolfile.h new file mode 100644 index 0000000..c5933c0 --- /dev/null +++ b/protocolfile.h @@ -0,0 +1,38 @@ +/* + * protocolfile.h: IPTV plugin for the Video Disk Recorder + * + * See the README file for copyright information and how to reach the author. + * + * $Id: protocolfile.h,v 1.1 2007/09/16 12:18:15 ajhseppa Exp $ + */ + +#ifndef __IPTV_PROTOCOLFILE_H +#define __IPTV_PROTOCOLFILE_H + +#include +#include "protocolif.h" + +class cIptvProtocolFile : public cIptvProtocolIf { +private: + char* streamAddr; + int streamPort; + int fileDesc; + unsigned char* readBuffer; + unsigned int readBufferLen; + bool fileActive; + +private: + bool OpenFile(void); + void CloseFile(void); + +public: + cIptvProtocolFile(); + virtual ~cIptvProtocolFile(); + virtual int Read(unsigned char* *BufferAddr); + virtual bool Set(const char* Address, const int Port); + virtual bool Open(void); + virtual bool Close(void); +}; + +#endif // __IPTV_PROTOCOLFILE_H +