vdr-plugin-iptv/protocolfile.c

118 lines
2.9 KiB
C
Raw Normal View History

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.
*
*/
#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-10-19 23:36:27 +02:00
: fileDelay(0),
2009-03-20 16:43:31 +01:00
fileStream(NULL),
isActive(false)
2007-09-16 14:18:15 +02:00
{
debug("cIptvProtocolFile::cIptvProtocolFile()\n");
2007-10-19 23:36:27 +02:00
fileLocation = strdup("");
2007-09-16 14:18:15 +02:00
}
cIptvProtocolFile::~cIptvProtocolFile()
{
debug("cIptvProtocolFile::~cIptvProtocolFile()\n");
// Drop open handles
2008-01-05 00:36:37 +01:00
cIptvProtocolFile::Close();
2007-09-16 14:18:15 +02:00
// Free allocated memory
2007-10-19 23:36:27 +02:00
free(fileLocation);
2007-09-16 14:18:15 +02:00
}
bool cIptvProtocolFile::OpenFile(void)
{
debug("cIptvProtocolFile::OpenFile()\n");
// Check that stream address is valid
2007-10-19 23:36:27 +02:00
if (!isActive && !isempty(fileLocation)) {
fileStream = fopen(fileLocation, "rb");
ERROR_IF_RET(!fileStream || ferror(fileStream), "fopen()", return false);
2007-09-16 14:18:15 +02:00
// Update active flag
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-10-19 23:36:27 +02:00
if (isActive && !isempty(fileLocation)) {
fclose(fileStream);
2007-09-16 19:31:38 +02:00
// Update active flag
isActive = false;
2007-09-16 14:18:15 +02:00
}
}
2009-02-26 15:04:12 +01:00
int cIptvProtocolFile::Read(unsigned char* BufferAddr, unsigned int BufferLen)
2007-09-16 14:18:15 +02:00
{
//debug("cIptvProtocolFile::Read()\n");
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
// and prevent transfer ringbuffer overflows
2007-10-19 23:36:27 +02:00
if (fileDelay)
cCondWait::SleepMs(fileDelay);
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
if (isActive)
2009-06-19 12:28:53 +02:00
return (int)fread(BufferAddr, sizeof(unsigned char), BufferLen, fileStream);
2007-09-20 23:15:08 +02:00
return -1;
2007-09-16 14:18:15 +02:00
}
bool cIptvProtocolFile::Open(void)
{
2007-10-19 23:36:27 +02:00
debug("cIptvProtocolFile::Open()\n");
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)
{
2007-10-19 23:36:27 +02:00
debug("cIptvProtocolFile::Close()\n");
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* Location, const int Parameter, const int Index)
2007-09-16 14:18:15 +02:00
{
debug("cIptvProtocolFile::Set(): Location=%s Parameter=%d Index=%d\n", Location, Parameter, Index);
2007-10-19 23:36:27 +02:00
if (!isempty(Location)) {
2007-09-16 19:31:38 +02:00
// Close the file stream
CloseFile();
// Update stream address and port
2007-10-19 23:36:27 +02:00
fileLocation = strcpyrealloc(fileLocation, Location);
fileDelay = Parameter;
2007-09-16 19:31:38 +02:00
// Open the file for input
OpenFile();
}
2007-09-16 14:18:15 +02:00
return true;
}
2007-10-08 00:54:09 +02:00
cString cIptvProtocolFile::GetInformation(void)
{
//debug("cIptvProtocolFile::GetInformation()");
2007-10-19 23:36:27 +02:00
return cString::sprintf("file://%s:%d", fileLocation, fileDelay);
2007-10-08 00:54:09 +02:00
}