2004-12-30 23:43:55 +01:00
|
|
|
/*
|
2007-04-02 12:32:34 +02: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"
|
2005-03-24 22:31:38 +01:00
|
|
|
#include "tools/select.h"
|
2004-12-30 23:43:55 +01:00
|
|
|
#include "common.h"
|
|
|
|
|
2005-02-10 23:24:26 +01:00
|
|
|
// --- 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()
|
|
|
|
{
|
2005-03-24 22:31:38 +01:00
|
|
|
Dprintf("destructing writer\n");
|
2005-02-08 20:54:52 +01:00
|
|
|
m_Active = false;
|
|
|
|
Cancel(3);
|
|
|
|
}
|
|
|
|
|
|
|
|
void cStreamdevWriter::Action(void)
|
|
|
|
{
|
2005-03-24 22:31:38 +01:00
|
|
|
cTBSelect sel;
|
2005-02-10 23:24:26 +01:00
|
|
|
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;
|
2007-04-02 12:32:34 +02:00
|
|
|
|
|
|
|
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) {
|
2007-04-02 12:32:34 +02:00
|
|
|
if (sel.Select(15000) == -1) {
|
2005-02-09 20:47:09 +01:00
|
|
|
esyslog("ERROR: streamdev-server: couldn't send data: %m");
|
|
|
|
break;
|
|
|
|
}
|
2005-03-24 22:31:38 +01:00
|
|
|
|
|
|
|
if (sel.CanWrite(*m_Socket)) {
|
|
|
|
int written;
|
2005-04-30 16:59:56 +02:00
|
|
|
if ((written = m_Socket->Write(block + offset, count)) == -1) {
|
2005-03-24 22:31:38 +01:00
|
|
|
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;
|
2005-04-30 21:41:08 +02:00
|
|
|
if (count == 0) {
|
|
|
|
m_Streamer->Del(offset);
|
2005-04-30 16:59:56 +02:00
|
|
|
block = NULL;
|
2005-04-30 21:41:08 +02:00
|
|
|
}
|
2005-03-24 22:31:38 +01:00
|
|
|
}
|
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
|
|
|
|
2005-02-10 23:24:26 +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),
|
2005-04-27 19:55:43 +02:00
|
|
|
m_RingBuffer(new cRingBufferLinear(STREAMERBUFSIZE, TS_SIZE * 2,
|
|
|
|
true, "streamdev-streamer")),
|
2005-02-10 23:24:26 +01:00
|
|
|
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)
|
|
|
|
{
|
2005-02-10 23:24:26 +01:00
|
|
|
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);
|
|
|
|
}
|
2005-03-24 22:31:38 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|