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

116 lines
2.9 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.
*
* $Id: streamer.c,v 1.29 2008/01/19 21:08:03 ajhseppa Exp $
2007-09-12 19:28:59 +02:00
*/
#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
protocol(NULL)
2007-09-12 19:28:59 +02:00
{
debug("cIptvStreamer::cIptvStreamer()\n");
}
cIptvStreamer::~cIptvStreamer()
{
debug("cIptvStreamer::~cIptvStreamer()\n");
2007-09-14 17:44:25 +02:00
// Close the protocol
Close();
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-21 23:50:52 +02:00
if (ringBuffer && mutex && protocol && ringBuffer->Free()) {
unsigned char *buffer = NULL;
mutex->Lock();
int length = protocol->Read(&buffer);
2007-09-14 17:44:25 +02:00
if (length >= 0) {
AddStreamerStatistic(length);
int p = ringBuffer->Put(buffer, 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 {
mutex->Unlock();
cCondWait::SleepMs(100); // to reduce cpu load
}
2007-09-13 18:58:22 +02:00
}
else
cCondWait::SleepMs(100); // and 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)
if(!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
if (Running())
Cancel(3);
2007-10-20 10:58:15 +02:00
// Close the protocol. A mutex should be taken here to avoid a race condition
// where thread Action() may be in the process of accessing the protocol.
// Taking a mutex serializes the Close() and Action() -calls.
2008-01-05 00:36:37 +01:00
if (mutex)
mutex->Lock();
if (protocol)
2007-09-14 17:44:25 +02:00
protocol->Close();
2008-01-05 00:36:37 +01:00
if (mutex)
mutex->Unlock();
2008-01-05 00:36:37 +01:00
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
}