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.
|
|
|
|
*
|
2003-05-11 09:01:51 +02:00
|
|
|
* $Id: transfer.c 1.12 2003/05/11 08:48:05 kls Exp $
|
2002-06-22 13:45:53 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "transfer.h"
|
|
|
|
|
|
|
|
//XXX+ also used in recorder.c - find a better place???
|
|
|
|
// The size of the array used to buffer video data:
|
|
|
|
// (must be larger than MINVIDEODATA - see remux.h)
|
|
|
|
#define VIDEOBUFSIZE MEGABYTE(1)
|
|
|
|
|
|
|
|
// --- cTransfer -------------------------------------------------------------
|
|
|
|
|
|
|
|
cTransfer::cTransfer(int VPid, int APid1, int APid2, int DPid1, int DPid2)
|
2002-10-26 10:24:42 +02:00
|
|
|
:cReceiver(0, -1, 5, VPid, APid1, APid2, DPid1, DPid2)
|
2002-06-22 13:45:53 +02:00
|
|
|
{
|
2003-01-26 09:59:35 +01:00
|
|
|
ringBuffer = new cRingBufferLinear(VIDEOBUFSIZE, TS_SIZE * 2, true);
|
2002-06-22 13:45:53 +02:00
|
|
|
remux = new cRemux(VPid, APid1, APid2, DPid1, DPid2);
|
2002-10-12 14:29:46 +02:00
|
|
|
canToggleAudioTrack = false;
|
|
|
|
audioTrack = 0xC0;
|
2002-06-22 13:45:53 +02:00
|
|
|
gotBufferReserve = false;
|
|
|
|
active = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
cTransfer::~cTransfer()
|
|
|
|
{
|
|
|
|
cReceiver::Detach();
|
|
|
|
cPlayer::Detach();
|
|
|
|
delete remux;
|
|
|
|
delete ringBuffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cTransfer::Activate(bool On)
|
|
|
|
{
|
|
|
|
if (On) {
|
|
|
|
if (!active)
|
|
|
|
Start();
|
|
|
|
}
|
|
|
|
else if (active) {
|
|
|
|
active = false;
|
|
|
|
Cancel(3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void cTransfer::Receive(uchar *Data, int Length)
|
|
|
|
{
|
2002-09-22 09:52:38 +02:00
|
|
|
if (IsAttached()) {
|
2003-02-15 14:12:41 +01:00
|
|
|
int i = 0;
|
|
|
|
while (active && Length > 0) {
|
|
|
|
if (i++ > 10) {
|
|
|
|
esyslog("ERROR: ring buffer overflow (%d bytes dropped)", Length);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
int p = ringBuffer->Put(Data, Length);
|
|
|
|
Length -= p;
|
|
|
|
Data += p;
|
|
|
|
}
|
2002-09-22 09:52:38 +02:00
|
|
|
}
|
2002-06-22 13:45:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void cTransfer::Action(void)
|
|
|
|
{
|
|
|
|
dsyslog("transfer thread started (pid=%d)", getpid());
|
|
|
|
|
|
|
|
active = true;
|
|
|
|
while (active) {
|
|
|
|
|
|
|
|
//XXX+ Maybe we need this to avoid "buffer empty" log messages from the driver.
|
|
|
|
//XXX+ But then again, it appears to play just fine without this...
|
|
|
|
/*
|
|
|
|
if (!gotBufferReserve) {
|
|
|
|
if (ringBuffer->Available() < 4 * MAXFRAMESIZE) {
|
|
|
|
usleep(100000); // allow the buffer to collect some reserve
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
gotBufferReserve = true;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Get data from the buffer:
|
|
|
|
|
2003-01-26 09:59:35 +01:00
|
|
|
int r;
|
|
|
|
const uchar *b = ringBuffer->Get(r);
|
2002-06-22 13:45:53 +02:00
|
|
|
|
|
|
|
// Play the data:
|
|
|
|
|
2003-01-26 09:59:35 +01:00
|
|
|
if (b) {
|
2002-06-22 13:45:53 +02:00
|
|
|
int Count = r, Result;
|
2002-10-12 14:29:46 +02:00
|
|
|
uchar *p = remux->Process(b, Count, Result);
|
2003-01-26 09:59:35 +01:00
|
|
|
ringBuffer->Del(Count);
|
2002-06-22 13:45:53 +02:00
|
|
|
if (p) {
|
2002-10-12 14:29:46 +02:00
|
|
|
StripAudioPackets(p, Result, audioTrack);
|
2002-06-22 13:45:53 +02:00
|
|
|
while (Result > 0 && active) {
|
2003-02-15 14:12:41 +01:00
|
|
|
cPoller Poller;
|
|
|
|
if (DevicePoll(Poller, 100)) {
|
|
|
|
int w = PlayVideo(p, Result);
|
|
|
|
if (w > 0) {
|
|
|
|
p += w;
|
|
|
|
Result -= w;
|
|
|
|
}
|
|
|
|
else if (w < 0 && FATALERRNO) {
|
|
|
|
LOG_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
2002-06-22 13:45:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-12-14 13:37:01 +01:00
|
|
|
else
|
|
|
|
usleep(1); // this keeps the CPU load low
|
2002-06-22 13:45:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
dsyslog("transfer thread ended (pid=%d)", getpid());
|
|
|
|
}
|
|
|
|
|
2002-10-12 14:29:46 +02:00
|
|
|
void cTransfer::StripAudioPackets(uchar *b, int Length, uchar Except)
|
2002-06-22 13:45:53 +02:00
|
|
|
{
|
2002-10-12 14:29:46 +02:00
|
|
|
for (int i = 0; i < Length - 6; i++) {
|
|
|
|
if (b[i] == 0x00 && b[i + 1] == 0x00 && b[i + 2] == 0x01) {
|
|
|
|
uchar c = b[i + 3];
|
|
|
|
int l = b[i + 4] * 256 + b[i + 5] + 6;
|
|
|
|
switch (c) {
|
|
|
|
case 0xBD: // dolby
|
|
|
|
if (Except)
|
2002-11-03 11:53:58 +01:00
|
|
|
PlayAudio(&b[i], l);
|
2002-10-12 14:29:46 +02:00
|
|
|
// continue with deleting the data - otherwise it disturbs DVB replay
|
|
|
|
case 0xC0 ... 0xC1: // audio
|
|
|
|
if (c == 0xC1)
|
|
|
|
canToggleAudioTrack = true;
|
2003-03-30 12:56:30 +02:00
|
|
|
if (!Except || c != Except)
|
|
|
|
memset(&b[i], 0x00, min(l, Length-i));
|
2002-10-12 14:29:46 +02:00
|
|
|
break;
|
|
|
|
case 0xE0 ... 0xEF: // video
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
//esyslog("ERROR: unexpected packet id %02X", c);
|
|
|
|
l = 0;
|
|
|
|
}
|
|
|
|
if (l)
|
|
|
|
i += l - 1; // the loop increments, too!
|
|
|
|
}
|
|
|
|
/*XXX
|
|
|
|
else
|
|
|
|
esyslog("ERROR: broken packet header");
|
|
|
|
XXX*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int cTransfer::NumAudioTracks(void) const
|
|
|
|
{
|
|
|
|
return canToggleAudioTrack ? 2 : 1;
|
|
|
|
}
|
|
|
|
|
2002-10-13 16:12:55 +02:00
|
|
|
const char **cTransfer::GetAudioTracks(int *CurrentTrack) const
|
2002-10-12 14:29:46 +02:00
|
|
|
{
|
|
|
|
if (NumAudioTracks()) {
|
|
|
|
if (CurrentTrack)
|
|
|
|
*CurrentTrack = (audioTrack == 0xC0) ? 0 : 1;
|
|
|
|
static const char *audioTracks1[] = { "Audio 1", NULL };
|
|
|
|
static const char *audioTracks2[] = { "Audio 1", "Audio 2", NULL };
|
|
|
|
return NumAudioTracks() > 1 ? audioTracks2 : audioTracks1;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cTransfer::SetAudioTrack(int Index)
|
|
|
|
{
|
|
|
|
if ((audioTrack == 0xC0) != (Index == 0)) {
|
|
|
|
audioTrack = (Index == 1) ? 0xC1 : 0xC0;
|
|
|
|
DeviceClear();
|
|
|
|
}
|
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;
|
|
|
|
|
2002-06-23 12:59:58 +02:00
|
|
|
cTransferControl::cTransferControl(cDevice *ReceiverDevice, int VPid, int APid1, int APid2, int DPid1, int DPid2)
|
|
|
|
:cControl(transfer = new cTransfer(VPid, APid1, APid2, DPid1, DPid2), true)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|