vdr-plugin-streamdev/server/streamer.c

148 lines
2.9 KiB
C
Raw Normal View History

2004-12-30 23:43:55 +01:00
/*
* $Id: streamer.c,v 1.15 2007/04/02 10:32:34 schmirl Exp $
2004-12-30 23:43:55 +01:00
*/
#include <vdr/ringbuffer.h>
#include <vdr/device.h>
#include <sys/types.h>
#include <unistd.h>
#include "server/streamer.h"
#include "server/suspend.h"
#include "server/setup.h"
#include "tools/socket.h"
#include "tools/select.h"
2004-12-30 23:43:55 +01:00
#include "common.h"
// --- cStreamdevWriter -------------------------------------------------------
2005-05-09 22:22:29 +02:00
cStreamdevWriter::cStreamdevWriter(cTBSocket *Socket,
cStreamdevStreamer *Streamer):
2005-02-08 20:54:52 +01:00
cThread("streamdev-writer"),
m_Streamer(Streamer),
m_Socket(Socket),
m_Active(false)
{
}
cStreamdevWriter::~cStreamdevWriter()
{
Dprintf("destructing writer\n");
2005-02-08 20:54:52 +01:00
m_Active = false;
Cancel(3);
}
void cStreamdevWriter::Action(void)
{
cTBSelect sel;
Dprintf("Writer start\n");
2005-02-08 20:54:52 +01:00
int max = 0;
2005-04-30 16:59:56 +02:00
uchar *block = NULL;
int count, offset = 0;
2005-02-08 20:54:52 +01:00
m_Active = true;
sel.Clear();
sel.Add(*m_Socket, true);
2005-02-08 20:54:52 +01:00
while (m_Active) {
2005-04-30 16:59:56 +02:00
if (block == NULL) {
block = m_Streamer->Get(count);
offset = 0;
}
2005-02-08 20:54:52 +01:00
2005-05-09 22:22:29 +02:00
if (block != NULL) {
if (sel.Select(15000) == -1) {
2005-02-09 20:47:09 +01:00
esyslog("ERROR: streamdev-server: couldn't send data: %m");
break;
}
if (sel.CanWrite(*m_Socket)) {
int written;
2005-04-30 16:59:56 +02:00
if ((written = m_Socket->Write(block + offset, count)) == -1) {
esyslog("ERROR: streamdev-server: couldn't send data: %m");
break;
}
if (count > max)
max = count;
2005-04-30 16:59:56 +02:00
offset += written;
count -= written;
if (count == 0) {
m_Streamer->Del(offset);
2005-04-30 16:59:56 +02:00
block = NULL;
}
}
2005-02-08 20:54:52 +01:00
}
}
m_Active = false;
Dprintf("Max. Transmit Blocksize was: %d\n", max);
}
2004-12-30 23:43:55 +01:00
// --- cStreamdevStreamer -----------------------------------------------------
2005-02-08 18:22:35 +01:00
cStreamdevStreamer::cStreamdevStreamer(const char *Name):
2005-02-08 20:54:52 +01:00
cThread(Name),
m_Active(false),
2005-02-11 18:02:22 +01:00
m_Running(false),
2005-02-08 20:54:52 +01:00
m_Writer(NULL),
m_RingBuffer(new cRingBufferLinear(STREAMERBUFSIZE, TS_SIZE * 2,
true, "streamdev-streamer")),
m_SendBuffer(new cRingBufferLinear(WRITERBUFSIZE, TS_SIZE * 2))
2004-12-30 23:43:55 +01:00
{
2005-02-08 20:54:52 +01:00
m_RingBuffer->SetTimeouts(0, 100);
m_SendBuffer->SetTimeouts(0, 100);
2004-12-30 23:43:55 +01:00
}
2005-02-08 20:54:52 +01:00
cStreamdevStreamer::~cStreamdevStreamer()
{
2005-02-11 17:44:14 +01:00
Dprintf("Desctructing streamer\n");
2004-12-30 23:43:55 +01:00
delete m_RingBuffer;
2005-02-08 20:54:52 +01:00
delete m_SendBuffer;
2004-12-30 23:43:55 +01:00
}
2005-02-08 20:54:52 +01:00
void cStreamdevStreamer::Start(cTBSocket *Socket)
{
Dprintf("start streamer\n");
2005-02-08 20:54:52 +01:00
m_Writer = new cStreamdevWriter(Socket, this);
2005-02-11 18:02:22 +01:00
m_Running = true;
2004-12-30 23:43:55 +01:00
Attach();
2005-02-08 20:54:52 +01:00
}
void cStreamdevStreamer::Activate(bool On)
{
if (On && !m_Active) {
2005-02-11 17:44:14 +01:00
Dprintf("activate streamer\n");
2005-02-08 20:54:52 +01:00
m_Writer->Start();
2004-12-30 23:43:55 +01:00
cThread::Start();
2005-02-08 20:54:52 +01:00
}
2004-12-30 23:43:55 +01:00
}
2005-02-08 20:54:52 +01:00
void cStreamdevStreamer::Stop(void)
{
2004-12-30 23:43:55 +01:00
if (m_Active) {
2005-02-11 17:44:14 +01:00
Dprintf("stopping streamer\n");
2004-12-30 23:43:55 +01:00
m_Active = false;
Cancel(3);
}
if (m_Running) {
Detach();
m_Running = false;
DELETENULL(m_Writer);
}
2004-12-30 23:43:55 +01:00
}
2005-02-08 20:54:52 +01:00
void cStreamdevStreamer::Action(void)
{
2004-12-30 23:43:55 +01:00
m_Active = true;
while (m_Active) {
2005-02-08 20:54:52 +01:00
int got;
uchar *block = m_RingBuffer->Get(got);
2005-02-09 20:47:09 +01:00
if (block) {
2005-02-08 20:54:52 +01:00
int count = Put(block, got);
if (count)
m_RingBuffer->Del(count);
}
2004-12-30 23:43:55 +01:00
}
}