2004-12-30 23:43:55 +01:00
|
|
|
/*
|
2010-12-02 09:57:17 +01:00
|
|
|
* $Id: device.c,v 1.27 2010/08/18 10:26:55 schmirl Exp $
|
2004-12-30 23:43:55 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "client/device.h"
|
|
|
|
#include "client/setup.h"
|
|
|
|
#include "client/filter.h"
|
|
|
|
|
|
|
|
#include "tools/select.h"
|
|
|
|
|
2010-12-02 09:57:17 +01:00
|
|
|
#include <vdr/config.h>
|
2004-12-30 23:43:55 +01:00
|
|
|
#include <vdr/channels.h>
|
|
|
|
#include <vdr/ringbuffer.h>
|
|
|
|
#include <vdr/eit.h>
|
|
|
|
#include <vdr/timers.h>
|
|
|
|
|
|
|
|
#include <time.h>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2012-04-21 22:28:58 +02:00
|
|
|
#ifndef LIVEPRIORITY
|
|
|
|
#define LIVEPRIORITY 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef TRANSFERPRIORITY
|
|
|
|
#define TRANSFERPRIORITY -1
|
|
|
|
#endif
|
|
|
|
|
2004-12-30 23:43:55 +01:00
|
|
|
#define VIDEOBUFSIZE MEGABYTE(3)
|
|
|
|
|
|
|
|
cStreamdevDevice *cStreamdevDevice::m_Device = NULL;
|
2011-12-11 17:03:09 +01:00
|
|
|
const cChannel *cStreamdevDevice::m_DenyChannel = NULL;
|
2004-12-30 23:43:55 +01:00
|
|
|
|
|
|
|
cStreamdevDevice::cStreamdevDevice(void) {
|
|
|
|
m_Channel = NULL;
|
|
|
|
m_TSBuffer = NULL;
|
|
|
|
|
|
|
|
m_Filters = new cStreamdevFilters;
|
|
|
|
StartSectionHandler();
|
2008-04-07 16:50:32 +02:00
|
|
|
isyslog("streamdev-client: got device number %d", CardIndex() + 1);
|
2004-12-30 23:43:55 +01:00
|
|
|
|
|
|
|
m_Device = this;
|
2007-04-24 12:43:40 +02:00
|
|
|
m_Pids = 0;
|
2012-03-04 01:20:57 +01:00
|
|
|
m_Priority = -100;
|
2004-12-30 23:43:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
cStreamdevDevice::~cStreamdevDevice() {
|
|
|
|
Dprintf("Device gets destructed\n");
|
2007-07-20 08:46:47 +02:00
|
|
|
|
|
|
|
Lock();
|
2004-12-30 23:43:55 +01:00
|
|
|
m_Device = NULL;
|
2007-07-20 08:46:47 +02:00
|
|
|
m_Filters->SetConnection(-1);
|
|
|
|
ClientSocket.Quit();
|
|
|
|
ClientSocket.Reset();
|
|
|
|
Unlock();
|
|
|
|
|
|
|
|
Cancel(3);
|
|
|
|
|
2009-04-06 08:48:59 +02:00
|
|
|
StopSectionHandler();
|
2007-07-20 08:46:47 +02:00
|
|
|
DELETENULL(m_Filters);
|
|
|
|
DELETENULL(m_TSBuffer);
|
2004-12-30 23:43:55 +01:00
|
|
|
}
|
|
|
|
|
2011-02-16 08:57:05 +01:00
|
|
|
#if APIVERSNUM >= 10700
|
|
|
|
int cStreamdevDevice::NumProvidedSystems(void) const
|
|
|
|
{ return StreamdevClientSetup.NumProvidedSystems; }
|
|
|
|
#endif
|
|
|
|
|
2004-12-30 23:43:55 +01:00
|
|
|
bool cStreamdevDevice::ProvidesSource(int Source) const {
|
|
|
|
Dprintf("ProvidesSource, Source=%d\n", Source);
|
2007-04-23 13:42:16 +02:00
|
|
|
return true;
|
2004-12-30 23:43:55 +01:00
|
|
|
}
|
|
|
|
|
2005-02-08 16:21:19 +01:00
|
|
|
bool cStreamdevDevice::ProvidesTransponder(const cChannel *Channel) const
|
|
|
|
{
|
|
|
|
Dprintf("ProvidesTransponder\n");
|
2007-04-23 13:42:16 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-12-09 09:05:09 +01:00
|
|
|
#if APIVERSNUM >= 10722
|
|
|
|
bool cStreamdevDevice::IsTunedToTransponder(const cChannel *Channel) const
|
|
|
|
#else
|
2007-04-23 13:42:16 +02:00
|
|
|
bool cStreamdevDevice::IsTunedToTransponder(const cChannel *Channel)
|
2011-12-09 09:05:09 +01:00
|
|
|
#endif
|
2007-04-23 13:42:16 +02:00
|
|
|
{
|
2012-05-29 01:25:47 +02:00
|
|
|
return ClientSocket.DataSocket(siLive) != NULL &&
|
|
|
|
m_Channel != NULL &&
|
|
|
|
Channel->Transponder() == m_Channel->Transponder();
|
2005-02-08 16:21:19 +01:00
|
|
|
}
|
|
|
|
|
2004-12-30 23:43:55 +01:00
|
|
|
bool cStreamdevDevice::ProvidesChannel(const cChannel *Channel, int Priority,
|
|
|
|
bool *NeedsDetachReceivers) const {
|
2012-04-21 22:28:58 +02:00
|
|
|
#if APIVERSNUM >= 10725
|
|
|
|
bool prio = Priority == IDLEPRIORITY || Priority >= this->Priority();
|
|
|
|
#else
|
2004-12-30 23:43:55 +01:00
|
|
|
bool prio = Priority < 0 || Priority > this->Priority();
|
2012-04-21 22:28:58 +02:00
|
|
|
#endif
|
2012-05-29 01:25:47 +02:00
|
|
|
bool res = prio;
|
2004-12-30 23:43:55 +01:00
|
|
|
bool ndr = false;
|
2008-04-07 16:50:32 +02:00
|
|
|
|
2011-12-11 17:03:09 +01:00
|
|
|
if (!StreamdevClientSetup.StartClient || Channel == m_DenyChannel)
|
2008-04-07 16:50:32 +02:00
|
|
|
return false;
|
|
|
|
|
2004-12-30 23:43:55 +01:00
|
|
|
Dprintf("ProvidesChannel, Channel=%s, Prio=%d\n", Channel->Name(), Priority);
|
|
|
|
|
2009-01-29 08:48:58 +01:00
|
|
|
if (StreamdevClientSetup.MinPriority <= StreamdevClientSetup.MaxPriority)
|
|
|
|
{
|
|
|
|
if (Priority < StreamdevClientSetup.MinPriority ||
|
|
|
|
Priority > StreamdevClientSetup.MaxPriority)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (Priority < StreamdevClientSetup.MinPriority &&
|
|
|
|
Priority > StreamdevClientSetup.MaxPriority)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-05-29 01:25:47 +02:00
|
|
|
if (IsTunedToTransponder(Channel)) {
|
|
|
|
if (Channel->Ca() < CA_ENCRYPTED_MIN ||
|
|
|
|
(Channel->Vpid() && HasPid(Channel->Vpid())) ||
|
|
|
|
(Channel->Apid(0) && HasPid(Channel->Apid(0))))
|
|
|
|
res = true;
|
|
|
|
else
|
|
|
|
ndr = true;
|
|
|
|
}
|
|
|
|
else if (prio) {
|
|
|
|
if (Priority == LIVEPRIORITY) {
|
|
|
|
if (ClientSocket.ServerVersion() >= 100) {
|
2012-04-21 22:28:58 +02:00
|
|
|
Priority = StreamdevClientSetup.LivePriority;
|
|
|
|
UpdatePriority(true);
|
|
|
|
}
|
2012-05-29 01:25:47 +02:00
|
|
|
else {
|
2012-04-21 22:28:58 +02:00
|
|
|
if (StreamdevClientSetup.LivePriority >= 0)
|
|
|
|
Priority = StreamdevClientSetup.LivePriority;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-29 01:25:47 +02:00
|
|
|
res = ClientSocket.ProvidesChannel(Channel, Priority);
|
|
|
|
ndr = Receiving();
|
2012-04-21 22:28:58 +02:00
|
|
|
|
|
|
|
if (ClientSocket.ServerVersion() >= 100)
|
|
|
|
UpdatePriority(false);
|
2004-12-30 23:43:55 +01:00
|
|
|
}
|
2010-12-02 09:57:17 +01:00
|
|
|
|
2004-12-30 23:43:55 +01:00
|
|
|
if (NeedsDetachReceivers)
|
|
|
|
*NeedsDetachReceivers = ndr;
|
|
|
|
Dprintf("prov res = %d, ndr = %d\n", res, ndr);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cStreamdevDevice::SetChannelDevice(const cChannel *Channel,
|
|
|
|
bool LiveView) {
|
2012-05-29 01:25:47 +02:00
|
|
|
bool res;
|
2004-12-30 23:43:55 +01:00
|
|
|
Dprintf("SetChannelDevice Channel: %s, LiveView: %s\n", Channel->Name(),
|
|
|
|
LiveView ? "true" : "false");
|
2010-12-02 09:57:17 +01:00
|
|
|
LOCK_THREAD;
|
|
|
|
|
2004-12-30 23:43:55 +01:00
|
|
|
if (LiveView)
|
|
|
|
return false;
|
|
|
|
|
2012-05-29 01:25:47 +02:00
|
|
|
if (Receiving() && IsTunedToTransponder(Channel) && (
|
|
|
|
Channel->Ca() < CA_ENCRYPTED_MIN ||
|
|
|
|
(Channel->Vpid() && HasPid(Channel->Vpid())) ||
|
|
|
|
(Channel->Apid(0) && HasPid(Channel->Apid(0))))) {
|
|
|
|
res = true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
DetachAllReceivers();
|
|
|
|
m_Channel = Channel;
|
|
|
|
// Old servers delete cStreamdevLiveStreamer in ABRT.
|
|
|
|
// Delete it now or it will happen after we tuned to new channel
|
|
|
|
if (ClientSocket.ServerVersion() < 100)
|
|
|
|
CloseDvr();
|
|
|
|
res = ClientSocket.SetChannelDevice(m_Channel);
|
|
|
|
}
|
|
|
|
Dprintf("setchanneldevice res=%d\n", res);
|
|
|
|
return res;
|
2004-12-30 23:43:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool cStreamdevDevice::SetPid(cPidHandle *Handle, int Type, bool On) {
|
2012-05-21 00:42:08 +02:00
|
|
|
Dprintf("SetPid, Pid=%d, Type=%d, On=%d, used=%d\n", Handle->pid, Type, On, Handle->used);
|
2007-04-24 12:43:40 +02:00
|
|
|
LOCK_THREAD;
|
|
|
|
|
|
|
|
bool res = true;
|
|
|
|
if (Handle->pid && (On || !Handle->used)) {
|
|
|
|
res = ClientSocket.SetPid(Handle->pid, On);
|
|
|
|
|
|
|
|
m_Pids += (!res) ? 0 : On ? 1 : -1;
|
|
|
|
if (m_Pids < 0)
|
|
|
|
m_Pids = 0;
|
|
|
|
}
|
|
|
|
return res;
|
2004-12-30 23:43:55 +01:00
|
|
|
}
|
|
|
|
|
2012-05-21 00:42:08 +02:00
|
|
|
bool cStreamdevDevice::OpenDvr(void) {
|
|
|
|
Dprintf("OpenDvr\n");
|
2007-04-24 12:43:40 +02:00
|
|
|
LOCK_THREAD;
|
|
|
|
|
2012-05-21 00:42:08 +02:00
|
|
|
CloseDvr();
|
2004-12-30 23:43:55 +01:00
|
|
|
if (ClientSocket.CreateDataConnection(siLive)) {
|
2005-04-24 18:21:59 +02:00
|
|
|
m_TSBuffer = new cTSBuffer(*ClientSocket.DataSocket(siLive), MEGABYTE(2), CardIndex() + 1);
|
2004-12-30 23:43:55 +01:00
|
|
|
}
|
2012-05-21 00:42:08 +02:00
|
|
|
else {
|
|
|
|
esyslog("cStreamdevDevice::OpenDvr(): DVR connection FAILED");
|
2007-04-24 12:43:40 +02:00
|
|
|
}
|
2012-05-21 00:42:08 +02:00
|
|
|
return m_TSBuffer != NULL;
|
2004-12-30 23:43:55 +01:00
|
|
|
}
|
|
|
|
|
2012-05-21 00:42:08 +02:00
|
|
|
|
2007-04-24 12:43:40 +02:00
|
|
|
void cStreamdevDevice::CloseDvr(void) {
|
|
|
|
Dprintf("CloseDvr\n");
|
|
|
|
LOCK_THREAD;
|
|
|
|
|
2012-05-21 00:42:08 +02:00
|
|
|
ClientSocket.CloseDvr();
|
|
|
|
DELETENULL(m_TSBuffer);
|
2007-04-24 12:43:40 +02:00
|
|
|
}
|
|
|
|
|
2004-12-30 23:43:55 +01:00
|
|
|
bool cStreamdevDevice::GetTSPacket(uchar *&Data) {
|
2007-07-20 08:46:47 +02:00
|
|
|
if (m_TSBuffer && m_Device) {
|
2005-01-25 15:14:43 +01:00
|
|
|
Data = m_TSBuffer->Get();
|
2007-04-24 12:46:21 +02:00
|
|
|
#if 1 // TODO: this should be fixed in vdr cTSBuffer
|
|
|
|
// simple disconnect detection
|
|
|
|
static int m_TSFails = 0;
|
|
|
|
if (!Data) {
|
2007-05-07 14:18:18 +02:00
|
|
|
LOCK_THREAD;
|
|
|
|
if(!ClientSocket.DataSocket(siLive)) {
|
|
|
|
return false; // triggers CloseDvr() + OpenDvr() in cDevice
|
|
|
|
}
|
2007-04-24 12:46:21 +02:00
|
|
|
cPoller Poller(*ClientSocket.DataSocket(siLive));
|
|
|
|
errno = 0;
|
|
|
|
if (Poller.Poll() && !errno) {
|
|
|
|
char tmp[1];
|
|
|
|
if (recv(*ClientSocket.DataSocket(siLive), tmp, 1, MSG_PEEK) == 0 && !errno) {
|
|
|
|
esyslog("cStreamDevice::GetTSPacket: GetChecked: NOTHING (%d)", m_TSFails);
|
|
|
|
m_TSFails++;
|
|
|
|
if (m_TSFails > 10) {
|
|
|
|
isyslog("cStreamdevDevice::GetTSPacket(): disconnected");
|
|
|
|
m_Pids = 0;
|
2012-05-21 00:42:08 +02:00
|
|
|
CloseDvr();
|
2007-04-24 12:46:21 +02:00
|
|
|
m_TSFails = 0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m_TSFails = 0;
|
|
|
|
}
|
|
|
|
#endif
|
2005-01-25 15:14:43 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2004-12-30 23:43:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int cStreamdevDevice::OpenFilter(u_short Pid, u_char Tid, u_char Mask) {
|
|
|
|
Dprintf("OpenFilter\n");
|
2007-04-24 13:24:38 +02:00
|
|
|
|
|
|
|
if (!StreamdevClientSetup.StreamFilters)
|
2004-12-30 23:43:55 +01:00
|
|
|
return -1;
|
2007-04-24 13:24:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
if (!ClientSocket.DataSocket(siLiveFilter)) {
|
|
|
|
if (ClientSocket.CreateDataConnection(siLiveFilter)) {
|
|
|
|
m_Filters->SetConnection(*ClientSocket.DataSocket(siLiveFilter));
|
|
|
|
} else {
|
|
|
|
isyslog("cStreamdevDevice::OpenFilter: connect failed: %m");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ClientSocket.SetFilter(Pid, Tid, Mask, true))
|
|
|
|
return m_Filters->OpenFilter(Pid, Tid, Mask);
|
|
|
|
|
|
|
|
return -1;
|
2004-12-30 23:43:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool cStreamdevDevice::Init(void) {
|
|
|
|
if (m_Device == NULL && StreamdevClientSetup.StartClient)
|
|
|
|
new cStreamdevDevice;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cStreamdevDevice::ReInit(void) {
|
2007-05-07 14:18:18 +02:00
|
|
|
if(m_Device) {
|
|
|
|
m_Device->Lock();
|
|
|
|
m_Device->m_Filters->SetConnection(-1);
|
|
|
|
m_Device->m_Pids = 0;
|
|
|
|
}
|
2004-12-30 23:43:55 +01:00
|
|
|
ClientSocket.Quit();
|
|
|
|
ClientSocket.Reset();
|
|
|
|
if (m_Device != NULL) {
|
2007-05-07 14:18:18 +02:00
|
|
|
//DELETENULL(m_Device->m_TSBuffer);
|
|
|
|
m_Device->Unlock();
|
2004-12-30 23:43:55 +01:00
|
|
|
}
|
|
|
|
return StreamdevClientSetup.StartClient ? Init() : true;
|
|
|
|
}
|
|
|
|
|
2012-04-21 22:28:58 +02:00
|
|
|
void cStreamdevDevice::UpdatePriority(bool SwitchingChannels) {
|
2010-12-02 09:57:17 +01:00
|
|
|
if (m_Device) {
|
|
|
|
m_Device->Lock();
|
2012-05-27 01:31:45 +02:00
|
|
|
if (ClientSocket.SupportsPrio() && ClientSocket.DataSocket(siLive)) {
|
2010-12-02 09:57:17 +01:00
|
|
|
int Priority = m_Device->Priority();
|
2012-03-04 01:15:40 +01:00
|
|
|
// override TRANSFERPRIORITY (-1) with live TV priority from setup
|
2012-04-21 22:28:58 +02:00
|
|
|
if (m_Device == cDevice::ActualDevice() && Priority == TRANSFERPRIORITY) {
|
2012-03-04 01:15:40 +01:00
|
|
|
Priority = StreamdevClientSetup.LivePriority;
|
2012-04-21 22:28:58 +02:00
|
|
|
// temporarily lower priority
|
|
|
|
if (SwitchingChannels)
|
|
|
|
Priority--;
|
|
|
|
if (Priority < 0 && ClientSocket.ServerVersion() < 100)
|
|
|
|
Priority = 0;
|
|
|
|
|
|
|
|
}
|
2010-12-02 09:57:17 +01:00
|
|
|
if (m_Device->m_Priority != Priority && ClientSocket.SetPriority(Priority))
|
|
|
|
m_Device->m_Priority = Priority;
|
|
|
|
}
|
|
|
|
m_Device->Unlock();
|
|
|
|
}
|
|
|
|
}
|
2011-07-06 09:13:50 +02:00
|
|
|
|
|
|
|
int cStreamdevDevice::SignalStrength(void) const {
|
|
|
|
int strength = -1;
|
|
|
|
if (ClientSocket.DataSocket(siLive) != NULL)
|
|
|
|
ClientSocket.GetSignal(&strength, NULL);
|
|
|
|
return strength;
|
|
|
|
}
|
|
|
|
|
|
|
|
int cStreamdevDevice::SignalQuality(void) const {
|
|
|
|
int quality = -1;
|
|
|
|
if (ClientSocket.DataSocket(siLive) != NULL)
|
|
|
|
ClientSocket.GetSignal(NULL, &quality);
|
|
|
|
return quality;
|
|
|
|
}
|
|
|
|
|