vdr-plugin-streamdev/server/streamer.c

170 lines
3.7 KiB
C
Raw Permalink Normal View History

2010-12-02 08:53:01 +01:00
/*
2010-12-02 09:57:17 +01:00
* $Id: streamer.c,v 1.21 2010/07/30 10:01:11 schmirl Exp $
2010-12-02 08:53:01 +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"
#include "common.h"
2010-12-02 09:44:53 +01:00
// --- cStreamdevBuffer -------------------------------------------------------
cStreamdevBuffer::cStreamdevBuffer(int Size, int Margin, bool Statistics, const char *Description):
cRingBufferLinear(Size, Margin, Statistics, Description)
{
}
2010-12-02 08:53:01 +01:00
// --- cStreamdevWriter -------------------------------------------------------
cStreamdevWriter::cStreamdevWriter(cTBSocket *Socket,
cStreamdevStreamer *Streamer):
cThread("streamdev-writer"),
m_Streamer(Streamer),
2010-12-02 09:43:13 +01:00
m_Socket(Socket)
2010-12-02 08:53:01 +01:00
{
}
cStreamdevWriter::~cStreamdevWriter()
{
Dprintf("destructing writer\n");
2010-12-02 09:43:13 +01:00
if (Running())
Cancel(3);
2010-12-02 08:53:01 +01:00
}
void cStreamdevWriter::Action(void)
{
cTBSelect sel;
Dprintf("Writer start\n");
int max = 0;
uchar *block = NULL;
int count, offset = 0;
2010-12-02 08:56:19 +01:00
2010-12-02 09:57:17 +01:00
#if APIVERSNUM >= 10705
SetPriority(-3);
#endif
2010-12-02 08:56:19 +01:00
sel.Clear();
sel.Add(*m_Socket, true);
2010-12-02 09:43:13 +01:00
while (Running()) {
2010-12-02 08:53:01 +01:00
if (block == NULL) {
block = m_Streamer->Get(count);
offset = 0;
}
if (block != NULL) {
2010-12-02 08:56:19 +01:00
if (sel.Select(15000) == -1) {
2010-12-02 08:53:01 +01:00
esyslog("ERROR: streamdev-server: couldn't send data: %m");
break;
}
if (sel.CanWrite(*m_Socket)) {
int written;
2010-12-02 09:43:13 +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);
2010-12-02 08:53:01 +01:00
break;
}
2010-12-02 09:43:13 +01:00
// statistics
2010-12-02 08:53:01 +01:00
if (count > max)
max = count;
offset += written;
count -= written;
2010-12-02 09:43:13 +01:00
// less than one TS packet left:
// delete what we've written so far and get next chunk
if (count < TS_SIZE) {
2010-12-02 08:53:01 +01:00
m_Streamer->Del(offset);
block = NULL;
}
}
}
}
Dprintf("Max. Transmit Blocksize was: %d\n", max);
}
// --- cStreamdevStreamer -----------------------------------------------------
2010-12-02 09:48:23 +01:00
cStreamdevStreamer::cStreamdevStreamer(const char *Name, const cServerConnection *Connection):
2010-12-02 08:53:01 +01:00
cThread(Name),
2010-12-02 09:48:23 +01:00
m_Connection(Connection),
2010-12-02 08:53:01 +01:00
m_Writer(NULL),
2010-12-02 09:44:53 +01:00
m_RingBuffer(new cStreamdevBuffer(STREAMERBUFSIZE, TS_SIZE * 2,
2010-12-02 08:53:01 +01:00
true, "streamdev-streamer")),
2010-12-02 09:44:53 +01:00
m_SendBuffer(new cStreamdevBuffer(WRITERBUFSIZE, TS_SIZE * 2))
2010-12-02 08:53:01 +01:00
{
m_RingBuffer->SetTimeouts(0, 100);
2010-12-02 09:44:53 +01:00
m_SendBuffer->SetTimeouts(100, 100);
2010-12-02 08:53:01 +01:00
}
cStreamdevStreamer::~cStreamdevStreamer()
{
Dprintf("Desctructing streamer\n");
delete m_RingBuffer;
delete m_SendBuffer;
}
void cStreamdevStreamer::Start(cTBSocket *Socket)
{
Dprintf("start streamer\n");
m_Writer = new cStreamdevWriter(Socket, this);
Attach();
}
void cStreamdevStreamer::Activate(bool On)
{
2010-12-02 09:43:13 +01:00
if (On && !Active()) {
2010-12-02 08:53:01 +01:00
Dprintf("activate streamer\n");
m_Writer->Start();
cThread::Start();
}
}
void cStreamdevStreamer::Stop(void)
{
2010-12-02 09:43:13 +01:00
if (Running()) {
2010-12-02 08:53:01 +01:00
Dprintf("stopping streamer\n");
Cancel(3);
}
2010-12-02 09:44:53 +01:00
if (m_Writer) {
2010-12-02 08:53:01 +01:00
Detach();
DELETENULL(m_Writer);
}
}
void cStreamdevStreamer::Action(void)
{
2010-12-02 09:57:17 +01:00
#if APIVERSNUM >= 10705
SetPriority(-3);
#endif
2010-12-02 09:43:13 +01:00
while (Running()) {
2010-12-02 08:53:01 +01:00
int got;
uchar *block = m_RingBuffer->Get(got);
if (block) {
int count = Put(block, got);
if (count)
m_RingBuffer->Del(count);
}
}
}