2007-09-16 14:18:15 +02:00
|
|
|
/*
|
|
|
|
* protocolfile.c: IPTV plugin for the Video Disk Recorder
|
|
|
|
*
|
|
|
|
* See the README file for copyright information and how to reach the author.
|
|
|
|
*
|
2007-09-28 18:44:59 +02:00
|
|
|
* $Id: protocolfile.c,v 1.8 2007/09/28 16:44:59 rahrenbe Exp $
|
2007-09-16 14:18:15 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <vdr/device.h>
|
|
|
|
|
|
|
|
#include "common.h"
|
2007-09-16 15:38:20 +02:00
|
|
|
#include "config.h"
|
2007-09-16 14:18:15 +02:00
|
|
|
#include "protocolfile.h"
|
|
|
|
|
|
|
|
cIptvProtocolFile::cIptvProtocolFile()
|
2007-09-28 18:44:59 +02:00
|
|
|
: readBufferLen(TS_SIZE * IptvConfig.GetReadBufferTsCount()),
|
|
|
|
isActive(false)
|
2007-09-16 14:18:15 +02:00
|
|
|
{
|
2007-09-28 18:44:59 +02:00
|
|
|
debug("cIptvProtocolFile::cIptvProtocolFile()\n");
|
2007-09-16 14:18:15 +02:00
|
|
|
streamAddr = strdup("");
|
|
|
|
// Allocate receive buffer
|
2007-09-28 18:44:59 +02:00
|
|
|
readBuffer = MALLOC(unsigned char, readBufferLen);
|
2007-09-16 14:18:15 +02:00
|
|
|
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
|
2007-09-28 18:44:59 +02:00
|
|
|
if (!isActive && !isempty(streamAddr)) {
|
2007-09-16 19:31:38 +02:00
|
|
|
fileStream = fopen(streamAddr, "rb");
|
2007-09-16 16:47:01 +02:00
|
|
|
if (ferror(fileStream) || !fileStream) {
|
2007-09-16 14:18:15 +02:00
|
|
|
char tmp[64];
|
2007-09-16 16:47:01 +02:00
|
|
|
error("ERROR: fopen(): %s", strerror_r(errno, tmp, sizeof(tmp)));
|
2007-09-16 14:18:15 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Update active flag
|
2007-09-28 18:44:59 +02:00
|
|
|
isActive = true;
|
2007-09-16 14:18:15 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cIptvProtocolFile::CloseFile(void)
|
|
|
|
{
|
|
|
|
debug("cIptvProtocolFile::CloseFile()\n");
|
2007-09-16 19:31:38 +02:00
|
|
|
// Check that file stream is valid
|
2007-09-28 18:44:59 +02:00
|
|
|
if (isActive && !isempty(streamAddr)) {
|
2007-09-16 16:47:01 +02:00
|
|
|
fclose(fileStream);
|
2007-09-16 19:31:38 +02:00
|
|
|
// Update active flag
|
2007-09-28 18:44:59 +02:00
|
|
|
isActive = false;
|
2007-09-16 14:18:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int cIptvProtocolFile::Read(unsigned char* *BufferAddr)
|
|
|
|
{
|
|
|
|
//debug("cIptvProtocolFile::Read()\n");
|
2007-09-16 16:47:01 +02:00
|
|
|
*BufferAddr = readBuffer;
|
2007-09-16 19:31:38 +02:00
|
|
|
// Check errors
|
|
|
|
if (ferror(fileStream)) {
|
|
|
|
debug("Read error\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
// Rewind if EOF
|
|
|
|
if (feof(fileStream))
|
|
|
|
rewind(fileStream);
|
2007-09-16 20:04:15 +02:00
|
|
|
// Sleep before reading the file stream to prevent aggressive busy looping
|
2007-09-28 18:44:59 +02:00
|
|
|
// and prevent transfer ringbuffer overflows
|
|
|
|
cCondWait::SleepMs(IptvConfig.GetFileIdleTimeMs());
|
2007-09-16 20:04:15 +02:00
|
|
|
// This check is to prevent a race condition where file may be switched off
|
|
|
|
// during the sleep and buffers are disposed. Check here that the plugin is
|
|
|
|
// still active before accessing the buffers
|
2007-09-28 18:44:59 +02:00
|
|
|
if (isActive)
|
|
|
|
return fread(readBuffer, sizeof(unsigned char), readBufferLen, fileStream);
|
2007-09-20 23:15:08 +02:00
|
|
|
return -1;
|
2007-09-16 14:18:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool cIptvProtocolFile::Open(void)
|
|
|
|
{
|
|
|
|
debug("cIptvProtocolFile::Open(): streamAddr=%s\n", streamAddr);
|
2007-09-16 19:31:38 +02:00
|
|
|
// Open the file stream
|
2007-09-16 14:18:15 +02:00
|
|
|
OpenFile();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cIptvProtocolFile::Close(void)
|
|
|
|
{
|
|
|
|
debug("cIptvProtocolFile::Close(): streamAddr=%s\n", streamAddr);
|
2007-09-16 19:31:38 +02:00
|
|
|
// Close the file stream
|
2007-09-16 14:18:15 +02:00
|
|
|
CloseFile();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cIptvProtocolFile::Set(const char* Address, const int Port)
|
|
|
|
{
|
|
|
|
debug("cIptvProtocolFile::Set(): %s:%d\n", Address, Port);
|
|
|
|
if (!isempty(Address)) {
|
2007-09-16 19:31:38 +02:00
|
|
|
// Close the file stream
|
|
|
|
CloseFile();
|
|
|
|
// Update stream address and port
|
|
|
|
streamAddr = strcpyrealloc(streamAddr, Address);
|
|
|
|
streamPort = Port;
|
|
|
|
// Open the file for input
|
|
|
|
OpenFile();
|
|
|
|
}
|
2007-09-16 14:18:15 +02:00
|
|
|
return true;
|
|
|
|
}
|