2004-12-30 23:43:55 +01:00
|
|
|
/*
|
2008-10-22 13:59:35 +02:00
|
|
|
* $Id: streamer.c,v 1.16.2.1 2008/10/22 11:59:37 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),
|
2008-10-22 13:59:35 +02:00
|
|
|
m_Socket(Socket)
|
2005-02-08 20:54:52 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
cStreamdevWriter::~cStreamdevWriter()
|
|
|
|
{
|
2005-03-24 22:31:38 +01:00
|
|
|
Dprintf("destructing writer\n");
|
2008-10-22 13:59:35 +02:00
|
|
|
if (Running())
|
|
|
|
Cancel(3);
|
2005-02-08 20:54:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2007-04-02 12:32:34 +02:00
|
|
|
|
|
|
|
sel.Clear();
|
|
|
|
sel.Add(*m_Socket, true);
|
2008-10-22 13:59:35 +02:00
|
|
|
while (Running()) {
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
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),
|
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)
|
|
|
|
{
|
2008-10-22 13:59:35 +02:00
|
|
|
if (On && !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)
|
|
|
|
{
|
2008-10-22 13:59:35 +02:00
|
|
|
if (Running()) {
|
2005-02-11 17:44:14 +01:00
|
|
|
Dprintf("stopping streamer\n");
|
2004-12-30 23:43:55 +01:00
|
|
|
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)
|
|
|
|
{
|
2008-10-22 13:59:35 +02:00
|
|
|
while (Running()) {
|
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);
|
2007-09-21 13:45:53 +02:00
|
|
|
else
|
|
|
|
cCondWait::SleepMs(100);
|
2005-02-08 20:54:52 +01:00
|
|
|
}
|
2004-12-30 23:43:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|