2002-06-22 13:45:53 +02:00
|
|
|
/*
|
|
|
|
* transfer.c: Transfer mode
|
|
|
|
*
|
|
|
|
* See the main source file 'vdr.c' for copyright information and
|
|
|
|
* how to reach the author.
|
|
|
|
*
|
2005-08-13 13:17:24 +02:00
|
|
|
* $Id: transfer.c 1.29 2005/08/13 11:19:46 kls Exp $
|
2002-06-22 13:45:53 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "transfer.h"
|
|
|
|
|
2004-10-16 09:36:28 +02:00
|
|
|
#define TRANSFERBUFSIZE MEGABYTE(2)
|
2005-02-19 14:40:36 +01:00
|
|
|
#define POLLTIMEOUTS_BEFORE_DEVICECLEAR 6
|
2002-06-22 13:45:53 +02:00
|
|
|
|
|
|
|
// --- cTransfer -------------------------------------------------------------
|
|
|
|
|
2005-01-16 14:40:47 +01:00
|
|
|
cTransfer::cTransfer(int VPid, const int *APids, const int *DPids, const int *SPids)
|
|
|
|
:cReceiver(0, -1, VPid, APids, Setup.UseDolbyDigital ? DPids : NULL, SPids)
|
2003-10-18 12:29:08 +02:00
|
|
|
,cThread("transfer")
|
2002-06-22 13:45:53 +02:00
|
|
|
{
|
2004-10-16 09:36:28 +02:00
|
|
|
ringBuffer = new cRingBufferLinear(TRANSFERBUFSIZE, TS_SIZE * 2, true, "Transfer");
|
2005-01-16 14:40:47 +01:00
|
|
|
remux = new cRemux(VPid, APids, Setup.UseDolbyDigital ? DPids : NULL, SPids);
|
|
|
|
needsBufferReserve = Setup.UseDolbyDigital && VPid != 0 && DPids && DPids[0] != 0;
|
2002-06-22 13:45:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
cTransfer::~cTransfer()
|
|
|
|
{
|
|
|
|
cReceiver::Detach();
|
|
|
|
cPlayer::Detach();
|
|
|
|
delete remux;
|
|
|
|
delete ringBuffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cTransfer::Activate(bool On)
|
|
|
|
{
|
2005-08-13 13:17:24 +02:00
|
|
|
if (On)
|
|
|
|
Start();
|
|
|
|
else
|
2002-06-22 13:45:53 +02:00
|
|
|
Cancel(3);
|
|
|
|
}
|
|
|
|
|
|
|
|
void cTransfer::Receive(uchar *Data, int Length)
|
|
|
|
{
|
2005-08-13 13:17:24 +02:00
|
|
|
if (IsAttached() && Active()) {
|
2004-10-16 09:36:28 +02:00
|
|
|
int p = ringBuffer->Put(Data, Length);
|
2005-08-13 13:17:24 +02:00
|
|
|
if (p != Length && Active())
|
2004-10-16 09:36:28 +02:00
|
|
|
ringBuffer->ReportOverflow(Length - p);
|
|
|
|
return;
|
2002-09-22 09:52:38 +02:00
|
|
|
}
|
2002-06-22 13:45:53 +02:00
|
|
|
}
|
|
|
|
|
2005-02-12 15:54:06 +01:00
|
|
|
#define FW_NEEDS_BUFFER_RESERVE_FOR_AC3
|
|
|
|
#ifdef FW_NEEDS_BUFFER_RESERVE_FOR_AC3
|
|
|
|
//XXX This is a very ugly hack to allow cDvbOsd to reduce the buffer
|
|
|
|
//XXX requirements in cTransfer if it detects a 4MB full featured DVB card.
|
|
|
|
bool DvbCardWith4MBofSDRAM = false;
|
|
|
|
#endif
|
|
|
|
|
2002-06-22 13:45:53 +02:00
|
|
|
void cTransfer::Action(void)
|
|
|
|
{
|
2003-08-31 12:41:19 +02:00
|
|
|
int PollTimeouts = 0;
|
2004-10-16 09:36:28 +02:00
|
|
|
uchar *p = NULL;
|
|
|
|
int Result = 0;
|
2004-12-17 14:55:49 +01:00
|
|
|
#ifdef FW_NEEDS_BUFFER_RESERVE_FOR_AC3
|
|
|
|
bool GotBufferReserve = false;
|
2005-02-12 15:54:06 +01:00
|
|
|
int RequiredBufferReserve = KILOBYTE(DvbCardWith4MBofSDRAM ? 288 : 576);
|
2004-12-17 14:55:49 +01:00
|
|
|
#endif
|
2005-08-13 13:17:24 +02:00
|
|
|
while (Active()) {
|
2004-12-17 14:55:49 +01:00
|
|
|
#ifdef FW_NEEDS_BUFFER_RESERVE_FOR_AC3
|
2005-02-12 13:52:35 +01:00
|
|
|
if (needsBufferReserve && !GotBufferReserve) {
|
2004-12-17 14:55:49 +01:00
|
|
|
//XXX For dolby we've to fill the buffer because the firmware does
|
|
|
|
//XXX not decode dolby but use a PCM stream for transport, therefore
|
|
|
|
//XXX the firmware has not enough buffer for noiseless skipping early
|
|
|
|
//XXX PCM samples (each dolby frame requires 6144 bytes in PCM and
|
|
|
|
//XXX audio is mostly to early in comparison to video).
|
|
|
|
//XXX To resolve this, the remuxer or PlayPes() should synchronize
|
|
|
|
//XXX audio with the video frames. 2004/09/09 Werner
|
2005-02-12 15:54:06 +01:00
|
|
|
if (ringBuffer->Available() < RequiredBufferReserve) { // used to be MAXFRAMESIZE, but the HDTV value of KILOBYTE(512) is way too much here
|
2005-02-12 13:52:35 +01:00
|
|
|
cCondWait::SleepMs(20); // allow the buffer to collect some reserve
|
|
|
|
continue;
|
2004-12-17 14:55:49 +01:00
|
|
|
}
|
2005-02-12 13:52:35 +01:00
|
|
|
else
|
|
|
|
GotBufferReserve = true;
|
2004-12-17 14:55:49 +01:00
|
|
|
}
|
|
|
|
#endif
|
2004-10-16 09:36:28 +02:00
|
|
|
int Count;
|
|
|
|
uchar *b = ringBuffer->Get(Count);
|
|
|
|
if (b) {
|
2004-10-23 13:40:56 +02:00
|
|
|
if (ringBuffer->Available() > TRANSFERBUFSIZE * 9 / 10) {
|
2004-10-16 09:36:28 +02:00
|
|
|
// If the buffer runs full, we have no chance of ever catching up
|
|
|
|
// since the data comes in at the same rate as it goes out (it's "live").
|
|
|
|
// So let's clear the buffer instead of suffering from permanent
|
|
|
|
// overflows.
|
|
|
|
dsyslog("clearing transfer buffer to avoid overflows");
|
2005-02-12 13:52:35 +01:00
|
|
|
DeviceClear();
|
2004-10-16 09:36:28 +02:00
|
|
|
ringBuffer->Clear();
|
|
|
|
remux->Clear();
|
2005-01-23 14:29:35 +01:00
|
|
|
PlayPes(NULL, 0);
|
2004-10-16 09:36:28 +02:00
|
|
|
p = NULL;
|
2005-02-12 13:52:35 +01:00
|
|
|
#ifdef FW_NEEDS_BUFFER_RESERVE_FOR_AC3
|
|
|
|
GotBufferReserve = false;
|
|
|
|
#endif
|
2002-06-22 13:45:53 +02:00
|
|
|
continue;
|
|
|
|
}
|
2004-10-16 09:36:28 +02:00
|
|
|
Count = remux->Put(b, Count);
|
|
|
|
if (Count)
|
|
|
|
ringBuffer->Del(Count);
|
2002-06-22 13:45:53 +02:00
|
|
|
}
|
2004-12-17 14:55:49 +01:00
|
|
|
if (!p)
|
|
|
|
p = remux->Get(Result);
|
2004-10-16 09:36:28 +02:00
|
|
|
if (p) {
|
|
|
|
cPoller Poller;
|
|
|
|
if (DevicePoll(Poller, 100)) {
|
|
|
|
PollTimeouts = 0;
|
2004-12-17 14:55:49 +01:00
|
|
|
int w = PlayPes(p, Result);
|
2004-10-16 09:36:28 +02:00
|
|
|
if (w > 0) {
|
|
|
|
p += w;
|
|
|
|
Result -= w;
|
|
|
|
remux->Del(w);
|
|
|
|
if (Result <= 0)
|
|
|
|
p = NULL;
|
|
|
|
}
|
|
|
|
else if (w < 0 && FATALERRNO)
|
|
|
|
LOG_ERROR;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PollTimeouts++;
|
|
|
|
if (PollTimeouts == POLLTIMEOUTS_BEFORE_DEVICECLEAR) {
|
|
|
|
dsyslog("clearing device because of consecutive poll timeouts");
|
|
|
|
DeviceClear();
|
|
|
|
ringBuffer->Clear();
|
|
|
|
remux->Clear();
|
2005-02-12 13:52:35 +01:00
|
|
|
PlayPes(NULL, 0);
|
2004-10-16 09:36:28 +02:00
|
|
|
p = NULL;
|
2005-02-12 13:52:35 +01:00
|
|
|
#ifdef FW_NEEDS_BUFFER_RESERVE_FOR_AC3
|
|
|
|
GotBufferReserve = false;
|
|
|
|
#endif
|
2004-10-16 09:36:28 +02:00
|
|
|
}
|
2002-06-22 13:45:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-23 12:59:58 +02:00
|
|
|
// --- cTransferControl ------------------------------------------------------
|
|
|
|
|
2003-05-11 09:01:51 +02:00
|
|
|
cDevice *cTransferControl::receiverDevice = NULL;
|
|
|
|
|
2005-01-16 14:40:47 +01:00
|
|
|
cTransferControl::cTransferControl(cDevice *ReceiverDevice, int VPid, const int *APids, const int *DPids, const int *SPids)
|
|
|
|
:cControl(transfer = new cTransfer(VPid, APids, DPids, SPids), true)
|
2002-06-23 12:59:58 +02:00
|
|
|
{
|
|
|
|
ReceiverDevice->AttachReceiver(transfer);
|
2003-05-11 09:01:51 +02:00
|
|
|
receiverDevice = ReceiverDevice;
|
2002-06-23 12:59:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
cTransferControl::~cTransferControl()
|
|
|
|
{
|
2003-05-11 09:01:51 +02:00
|
|
|
receiverDevice = NULL;
|
2002-06-23 12:59:58 +02:00
|
|
|
delete transfer;
|
|
|
|
}
|