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

110 lines
2.5 KiB
C
Raw Normal View History

2007-09-12 19:28:59 +02:00
/*
* streamer.c: IPTV plugin for the Video Disk Recorder
*
* See the README file for copyright information and how to reach the author.
*
2007-09-14 17:44:25 +02:00
* $Id: streamer.c,v 1.11 2007/09/14 15:44:25 rahrenbe Exp $
2007-09-12 19:28:59 +02:00
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <fcntl.h>
#include <unistd.h>
#include <vdr/device.h>
#include <vdr/thread.h>
#include <vdr/ringbuffer.h>
#include "common.h"
#include "streamer.h"
2007-09-14 17:44:25 +02:00
cIptvStreamer::cIptvStreamer(cRingBufferLinear* RingBuffer, cMutex* Mutex)
2007-09-12 19:28:59 +02:00
: cThread("IPTV streamer"),
2007-09-14 17:44:25 +02:00
ringBuffer(RingBuffer),
2007-09-12 19:28:59 +02:00
mutex(Mutex),
2007-09-14 17:44:25 +02:00
readBufferLen(TS_SIZE * 7),
protocol(NULL)
2007-09-12 19:28:59 +02:00
{
debug("cIptvStreamer::cIptvStreamer()\n");
2007-09-13 18:58:22 +02:00
// Allocate receive buffer
2007-09-14 17:44:25 +02:00
readBuffer = MALLOC(unsigned char, readBufferLen);
if (!readBuffer)
error("ERROR: MALLOC(readBuffer) failed");
2007-09-12 19:28:59 +02:00
}
cIptvStreamer::~cIptvStreamer()
{
debug("cIptvStreamer::~cIptvStreamer()\n");
2007-09-14 17:44:25 +02:00
// Close the protocol
Close();
2007-09-13 18:58:22 +02:00
// Free allocated memory
2007-09-14 17:44:25 +02:00
free(readBuffer);
2007-09-12 19:28:59 +02:00
}
2007-09-13 18:58:22 +02:00
void cIptvStreamer::Action(void)
2007-09-12 19:28:59 +02:00
{
debug("cIptvStreamer::Action(): Entering\n");
2007-09-13 18:58:22 +02:00
// Do the thread loop
2007-09-12 19:28:59 +02:00
while (Running()) {
2007-09-14 17:44:25 +02:00
if (ringBuffer && mutex && readBuffer && protocol) {
int length = protocol->Read(readBuffer, readBufferLen);
if (length >= 0) {
2007-09-13 18:58:22 +02:00
mutex->Lock();
2007-09-14 17:44:25 +02:00
int p = ringBuffer->Put(readBuffer, length);
2007-09-13 18:58:22 +02:00
if (p != length && Running())
2007-09-14 17:44:25 +02:00
ringBuffer->ReportOverflow(length - p);
2007-09-13 18:58:22 +02:00
mutex->Unlock();
2007-09-14 17:44:25 +02:00
}
else
cCondWait::SleepMs(3); // reduce cpu load
2007-09-13 18:58:22 +02:00
}
else
cCondWait::SleepMs(100); // avoid busy loop
2007-09-12 19:28:59 +02:00
}
debug("cIptvStreamer::Action(): Exiting\n");
}
2007-09-14 17:44:25 +02:00
bool cIptvStreamer::Open(void)
{
2007-09-14 17:44:25 +02:00
debug("cIptvStreamer::Open()\n");
// Open the protocol
if (protocol)
protocol->Open();
2007-09-13 18:58:22 +02:00
// Start thread
Start();
return true;
}
2007-09-14 17:44:25 +02:00
bool cIptvStreamer::Close(void)
2007-09-13 18:58:22 +02:00
{
2007-09-14 17:44:25 +02:00
debug("cIptvStreamer::Close()\n");
2007-09-13 18:58:22 +02:00
// Stop thread
if (Running())
Cancel(3);
2007-09-14 17:44:25 +02:00
// Close the protocol
if (protocol)
protocol->Close();
2007-09-12 19:28:59 +02:00
return true;
}
2007-09-14 17:44:25 +02:00
bool cIptvStreamer::Set(const char* Address, const int Port, cIptvProtocolIf* Protocol)
2007-09-12 19:28:59 +02:00
{
2007-09-14 17:44:25 +02:00
debug("cIptvStreamer::Set(): %s:%d\n", Address, Port);
if (!isempty(Address)) {
// Update protocol; Close the existing one if changed
if (protocol != Protocol) {
if (protocol)
protocol->Close();
protocol = Protocol;
if (protocol)
protocol->Open();
}
// Set protocol address and port
if (protocol)
protocol->Set(Address, Port);
2007-09-13 18:58:22 +02:00
}
2007-09-12 19:28:59 +02:00
return true;
}