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-10-20 10:58:15 +02:00
|
|
|
* $Id: streamer.c,v 1.27 2007/10/20 08:58:15 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()) {
|
2007-09-15 22:33:15 +02:00
|
|
|
unsigned char *buffer = NULL;
|
2007-10-20 10:32:00 +02:00
|
|
|
mutex->Lock();
|
2007-09-15 23:27:00 +02:00
|
|
|
int length = protocol->Read(&buffer);
|
2007-09-14 17:44:25 +02:00
|
|
|
if (length >= 0) {
|
2007-10-10 00:12:17 +02:00
|
|
|
AddStatistic(length);
|
2007-09-15 22:33:15 +02:00
|
|
|
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
|
|
|
}
|
2007-10-20 10:32:00 +02:00
|
|
|
else {
|
|
|
|
mutex->Unlock();
|
2007-09-15 22:33:15 +02:00
|
|
|
cCondWait::SleepMs(100); // to reduce cpu load
|
2007-10-20 10:32:00 +02:00
|
|
|
}
|
2007-09-13 18:58:22 +02:00
|
|
|
}
|
|
|
|
else
|
2007-09-15 22:33:15 +02:00
|
|
|
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-12 20:33:56 +02:00
|
|
|
{
|
2007-09-14 17:44:25 +02:00
|
|
|
debug("cIptvStreamer::Open()\n");
|
|
|
|
// Open the protocol
|
|
|
|
if (protocol)
|
2007-09-29 13:17:57 +02:00
|
|
|
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.
|
2007-10-20 10:32:00 +02:00
|
|
|
if (protocol) {
|
|
|
|
mutex->Lock();
|
2007-09-14 17:44:25 +02:00
|
|
|
protocol->Close();
|
2007-10-20 10:32:00 +02:00
|
|
|
mutex->Unlock();
|
|
|
|
}
|
2007-09-12 19:28:59 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-10-20 00:18:55 +02:00
|
|
|
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)) {
|
2007-09-15 22:33:15 +02:00
|
|
|
// 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
|
2007-09-15 22:33:15 +02:00
|
|
|
if (protocol)
|
2007-10-20 00:18:55 +02:00
|
|
|
protocol->Set(Location, Parameter, Index);
|
2007-09-15 22:33:15 +02:00
|
|
|
}
|
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)
|
2007-10-12 01:06:49 +02:00
|
|
|
info = cString::sprintf("%s %s", *info, *protocol->GetInformation());
|
|
|
|
return cString::sprintf("%s\n", *info);
|
2007-10-08 00:54:09 +02:00
|
|
|
}
|