2004-12-30 23:43:55 +01:00
|
|
|
/*
|
2010-12-02 09:57:17 +01:00
|
|
|
* $Id: streamer.c,v 1.21 2010/07/30 10:01:11 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"
|
|
|
|
|
2009-06-19 08:32:38 +02:00
|
|
|
// --- cStreamdevBuffer -------------------------------------------------------
|
|
|
|
|
|
|
|
cStreamdevBuffer::cStreamdevBuffer(int Size, int Margin, bool Statistics, const char *Description):
|
|
|
|
cRingBufferLinear(Size, Margin, Statistics, Description)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
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:31 +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:31 +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;
|
2011-04-11 13:56:49 +02:00
|
|
|
int timeout = 0;
|
2007-04-02 12:32:34 +02:00
|
|
|
|
2010-12-02 09:57:17 +01:00
|
|
|
SetPriority(-3);
|
2007-04-02 12:32:34 +02:00
|
|
|
sel.Clear();
|
|
|
|
sel.Add(*m_Socket, true);
|
2008-10-22 13:59:31 +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) {
|
2011-04-11 13:56:49 +02:00
|
|
|
if (sel.Select(600) == -1) {
|
|
|
|
if (errno == ETIMEDOUT && timeout++ < 20)
|
|
|
|
continue; // still Running()?
|
2005-02-09 20:47:09 +01:00
|
|
|
esyslog("ERROR: streamdev-server: couldn't send data: %m");
|
|
|
|
break;
|
|
|
|
}
|
2011-04-11 13:56:49 +02:00
|
|
|
timeout = 0;
|
2005-03-24 22:31:38 +01:00
|
|
|
|
|
|
|
if (sel.CanWrite(*m_Socket)) {
|
|
|
|
int written;
|
2009-02-13 11:39:20 +01:00
|
|
|
int pkgsize = count;
|
|
|
|
// SOCK_DGRAM indicates multicast
|
|
|
|
if (m_Socket->Type() == SOCK_DGRAM) {
|
|
|
|
// don't fragment multicast packets
|
|
|
|
// max. payload on standard local ethernet is 1416 to 1456 bytes
|
|
|
|
// and some STBs expect complete TS packets
|
|
|
|
// so let's always limit to 7 * TS_SIZE = 1316
|
|
|
|
if (pkgsize > 7 * TS_SIZE)
|
|
|
|
pkgsize = 7 * TS_SIZE;
|
|
|
|
else
|
|
|
|
pkgsize -= pkgsize % TS_SIZE;
|
|
|
|
}
|
|
|
|
if ((written = m_Socket->Write(block + offset, pkgsize)) == -1) {
|
|
|
|
esyslog("ERROR: streamdev-server: couldn't send %d bytes: %m", pkgsize);
|
2005-03-24 22:31:38 +01:00
|
|
|
break;
|
|
|
|
}
|
2009-02-13 11:39:20 +01:00
|
|
|
|
|
|
|
// statistics
|
2005-03-24 22:31:38 +01:00
|
|
|
if (count > max)
|
|
|
|
max = count;
|
2005-04-30 16:59:56 +02:00
|
|
|
|
|
|
|
offset += written;
|
|
|
|
count -= written;
|
2009-02-13 11:39:20 +01:00
|
|
|
|
|
|
|
// less than one TS packet left:
|
|
|
|
// delete what we've written so far and get next chunk
|
|
|
|
if (count < TS_SIZE) {
|
2005-04-30 21:41:08 +02:00
|
|
|
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 -----------------------------------------------------
|
|
|
|
|
2010-12-02 09:57:17 +01:00
|
|
|
cStreamdevStreamer::cStreamdevStreamer(const char *Name, const cServerConnection *Connection):
|
2005-02-08 20:54:52 +01:00
|
|
|
cThread(Name),
|
2010-12-02 09:57:17 +01:00
|
|
|
m_Connection(Connection),
|
2005-02-08 20:54:52 +01:00
|
|
|
m_Writer(NULL),
|
2009-06-19 08:32:38 +02:00
|
|
|
m_SendBuffer(new cStreamdevBuffer(WRITERBUFSIZE, TS_SIZE * 2))
|
2004-12-30 23:43:55 +01:00
|
|
|
{
|
2009-06-19 08:32:38 +02:00
|
|
|
m_SendBuffer->SetTimeouts(100, 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");
|
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)
|
|
|
|
{
|
2012-12-04 17:21:36 +01:00
|
|
|
Dprintf("start writer\n");
|
2005-02-08 20:54:52 +01:00
|
|
|
m_Writer = new cStreamdevWriter(Socket, this);
|
2012-11-02 09:02:22 +01:00
|
|
|
m_Writer->Start();
|
2012-12-04 17:21:36 +01:00
|
|
|
if (!Active()) {
|
|
|
|
Dprintf("start streamer\n");
|
2004-12-30 23:43:55 +01:00
|
|
|
cThread::Start();
|
2005-02-08 20:54:52 +01:00
|
|
|
}
|
2012-12-04 17:21:36 +01:00
|
|
|
Attach();
|
2004-12-30 23:43:55 +01:00
|
|
|
}
|
|
|
|
|
2005-02-08 20:54:52 +01:00
|
|
|
void cStreamdevStreamer::Stop(void)
|
|
|
|
{
|
2012-12-04 17:21:36 +01:00
|
|
|
Detach();
|
2008-10-22 13:59:31 +02:00
|
|
|
if (Running()) {
|
2012-12-04 17:21:36 +01:00
|
|
|
Dprintf("stop streamer\n");
|
2004-12-30 23:43:55 +01:00
|
|
|
Cancel(3);
|
|
|
|
}
|
2012-12-04 17:21:36 +01:00
|
|
|
Dprintf("stop writer\n");
|
|
|
|
DELETENULL(m_Writer);
|
2004-12-30 23:43:55 +01:00
|
|
|
}
|
|
|
|
|
2005-02-08 20:54:52 +01:00
|
|
|
void cStreamdevStreamer::Action(void)
|
|
|
|
{
|
2010-12-02 09:57:17 +01:00
|
|
|
SetPriority(-3);
|
2008-10-22 13:59:31 +02:00
|
|
|
while (Running()) {
|
2005-02-08 20:54:52 +01:00
|
|
|
int got;
|
2012-12-16 12:40:44 +01:00
|
|
|
uchar *block = GetFromReceiver(got);
|
2005-02-08 20:54:52 +01:00
|
|
|
|
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)
|
2012-12-16 12:40:44 +01:00
|
|
|
DelFromReceiver(count);
|
2005-02-08 20:54:52 +01:00
|
|
|
}
|
2004-12-30 23:43:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|