vdr-plugin-iptv/streamer.c

113 lines
2.8 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.
*
*/
#include <vdr/thread.h>
#include <vdr/ringbuffer.h>
#include "common.h"
#include "streamer.h"
2009-02-26 15:04:12 +01:00
cIptvStreamer::cIptvStreamer(cRingBufferLinear* RingBuffer, unsigned int PacketLen)
2007-09-12 19:28:59 +02:00
: cThread("IPTV streamer"),
2007-09-14 17:44:25 +02:00
ringBuffer(RingBuffer),
2009-02-26 15:04:12 +01:00
packetBufferLen(PacketLen),
protocol(NULL)
2007-09-12 19:28:59 +02:00
{
2009-02-26 15:04:12 +01:00
debug("cIptvStreamer::cIptvStreamer(%d)\n", packetBufferLen);
// Allocate packet buffer
packetBuffer = MALLOC(unsigned char, packetBufferLen);
if (packetBuffer)
memset(packetBuffer, 0, packetBufferLen);
else
error("ERROR: MALLOC() failed for packet buffer");
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();
2009-02-26 15:04:12 +01:00
// Free allocated memory
free(packetBuffer);
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");
2009-02-26 15:04:12 +01:00
// Increase priority
//SetPriority(-1);
2007-09-13 18:58:22 +02:00
// Do the thread loop
2009-02-26 15:04:12 +01:00
while (packetBuffer && Running()) {
int length = -1;
if (protocol)
length = protocol->Read(packetBuffer, packetBufferLen);
if (length >= 0) {
AddStreamerStatistic(length);
if (ringBuffer) {
int p = ringBuffer->Put(packetBuffer, length);
if (p != length)
ringBuffer->ReportOverflow(length - p);
}
}
else
sleep.Wait(10); // to avoid busy loop and reduce cpu load
}
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
2008-01-30 22:57:33 +01:00
if (protocol && !protocol->Open())
return false;
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
2008-09-10 16:22:36 +02:00
sleep.Signal();
2007-09-13 18:58:22 +02:00
if (Running())
Cancel(3);
2009-02-26 15:04:12 +01:00
// Close the protocol
2008-01-05 00:36:37 +01:00
if (protocol)
2007-09-14 17:44:25 +02:00
protocol->Close();
2007-09-12 19:28:59 +02:00
return true;
}
bool cIptvStreamer::Set(const char* Location, const int Parameter, const int Index, cIptvProtocolIf* Protocol)
2007-09-12 19:28:59 +02:00
{
2007-10-19 23:36:27 +02:00
debug("cIptvStreamer::Set(): %s:%d\n", Location, Parameter);
if (!isempty(Location)) {
// Update protocol; Close the existing one if changed
if (protocol != Protocol) {
if (protocol)
protocol->Close();
protocol = Protocol;
if (protocol)
protocol->Open();
}
2007-10-19 23:36:27 +02:00
// Set protocol location and parameter
if (protocol)
protocol->Set(Location, Parameter, Index);
}
2007-09-12 19:28:59 +02:00
return true;
}
2007-10-08 00:54:09 +02:00
cString cIptvStreamer::GetInformation(void)
{
//debug("cIptvStreamer::GetInformation()");
2007-10-11 23:05:35 +02:00
cString info("Stream:");
2007-10-08 00:54:09 +02:00
if (protocol)
info = cString::sprintf("%s %s", *info, *protocol->GetInformation());
return cString::sprintf("%s\n", *info);
2007-10-08 00:54:09 +02:00
}