1
0
mirror of https://github.com/rofafor/vdr-plugin-iptv.git synced 2023-10-10 13:37:03 +02:00
vdr-plugin-iptv/protocolfile.c

125 lines
3.1 KiB
C
Raw Permalink 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"
2015-03-08 13:33:18 +01:00
#include "log.h"
2007-09-16 14:18:15 +02:00
#include "protocolfile.h"
cIptvProtocolFile::cIptvProtocolFile()
2013-02-23 14:31:11 +01:00
: fileLocationM(strdup("")),
fileDelayM(0),
fileStreamM(NULL),
isActiveM(false)
2007-09-16 14:18:15 +02:00
{
2015-03-08 13:33:18 +01:00
debug1("%s", __PRETTY_FUNCTION__);
2007-09-16 14:18:15 +02:00
}
cIptvProtocolFile::~cIptvProtocolFile()
{
2015-03-08 13:33:18 +01:00
debug1("%s", __PRETTY_FUNCTION__);
2007-09-16 14:18:15 +02:00
// Drop open handles
2008-01-05 00:36:37 +01:00
cIptvProtocolFile::Close();
2007-09-16 14:18:15 +02:00
// Free allocated memory
2013-02-23 14:31:11 +01:00
free(fileLocationM);
2007-09-16 14:18:15 +02:00
}
bool cIptvProtocolFile::OpenFile(void)
{
2015-03-08 13:33:18 +01:00
debug1("%s", __PRETTY_FUNCTION__);
2007-09-16 14:18:15 +02:00
// Check that stream address is valid
2013-02-23 14:31:11 +01:00
if (!isActiveM && !isempty(fileLocationM)) {
fileStreamM = fopen(fileLocationM, "rb");
ERROR_IF_RET(!fileStreamM || ferror(fileStreamM), "fopen()", return false);
2007-09-16 14:18:15 +02:00
// Update active flag
2013-02-23 14:31:11 +01:00
isActiveM = true;
2007-09-16 14:18:15 +02:00
}
return true;
}
void cIptvProtocolFile::CloseFile(void)
{
2015-03-08 13:33:18 +01:00
debug1("%s", __PRETTY_FUNCTION__);
2007-09-16 19:31:38 +02:00
// Check that file stream is valid
2013-02-23 14:31:11 +01:00
if (isActiveM && !isempty(fileLocationM)) {
fclose(fileStreamM);
2007-09-16 19:31:38 +02:00
// Update active flag
2013-02-23 14:31:11 +01:00
isActiveM = false;
2007-09-16 14:18:15 +02:00
}
}
2013-02-23 14:31:11 +01:00
int cIptvProtocolFile::Read(unsigned char* bufferAddrP, unsigned int bufferLenP)
2007-09-16 14:18:15 +02:00
{
2015-03-08 13:33:18 +01:00
debug16("%s (, %u)", __PRETTY_FUNCTION__, bufferLenP);
2007-09-16 19:31:38 +02:00
// Check errors
if (!fileStreamM || ferror(fileStreamM)) {
2015-03-08 13:33:18 +01:00
debug1("%s (, %d) Stream error", __PRETTY_FUNCTION__, bufferLenP);
2007-09-16 19:31:38 +02:00
return -1;
}
// Rewind if EOF
2013-02-23 14:31:11 +01:00
if (feof(fileStreamM))
rewind(fileStreamM);
2007-09-16 20:04:15 +02:00
// Sleep before reading the file stream to prevent aggressive busy looping
// and prevent transfer ringbuffer overflows
2013-02-23 14:31:11 +01:00
if (fileDelayM)
cCondWait::SleepMs(fileDelayM);
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
2013-02-23 14:31:11 +01:00
if (isActiveM)
return (int)fread(bufferAddrP, sizeof(unsigned char), bufferLenP, fileStreamM);
2007-09-20 23:15:08 +02:00
return -1;
2007-09-16 14:18:15 +02:00
}
bool cIptvProtocolFile::Open(void)
{
2015-03-08 13:33:18 +01:00
debug1("%s", __PRETTY_FUNCTION__);
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)
{
2015-03-08 13:33:18 +01:00
debug1("%s", __PRETTY_FUNCTION__);
2007-09-16 19:31:38 +02:00
// Close the file stream
2007-09-16 14:18:15 +02:00
CloseFile();
return true;
}
2014-02-09 18:22:02 +01:00
bool cIptvProtocolFile::SetSource(const char* locationP, const int parameterP, const int indexP)
2007-09-16 14:18:15 +02:00
{
2015-03-08 13:33:18 +01:00
debug1("%s (%s, %d, %d)", __PRETTY_FUNCTION__, locationP, parameterP, indexP);
2013-02-23 14:31:11 +01:00
if (!isempty(locationP)) {
2007-09-16 19:31:38 +02:00
// Close the file stream
CloseFile();
// Update stream address and port
2013-02-23 14:31:11 +01:00
fileLocationM = strcpyrealloc(fileLocationM, locationP);
fileDelayM = parameterP;
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
2014-02-09 18:22:02 +01:00
bool cIptvProtocolFile::SetPid(int pidP, int typeP, bool onP)
{
2015-03-08 13:33:18 +01:00
debug16("%s (%d, %d, %d)", __PRETTY_FUNCTION__, pidP, typeP, onP);
2014-02-09 18:22:02 +01:00
return true;
}
2007-10-08 00:54:09 +02:00
cString cIptvProtocolFile::GetInformation(void)
{
2015-03-08 13:33:18 +01:00
debug16("%s", __PRETTY_FUNCTION__);
2013-02-23 14:31:11 +01:00
return cString::sprintf("file://%s:%d", fileLocationM, fileDelayM);
2007-10-08 00:54:09 +02:00
}