summaryrefslogtreecommitdiff
path: root/eitscan.c
blob: a12bb44d36cf551eb9d3e1fd260f5bfca0cbe06a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
 * eitscan.c: EIT scanner
 *
 * See the main source file 'vdr.c' for copyright information and
 * how to reach the author.
 *
 * $Id: eitscan.c 1.14 2003/09/06 13:06:13 kls Exp $
 */

#include "eitscan.h"
#include <stdlib.h>
#include "channels.h"
#include "dvbdevice.h"

cEITScanner EITScanner;

cEITScanner::cEITScanner(void)
{
  lastScan = lastActivity = time(NULL);
  currentDevice = NULL;
  currentChannel = 0;
  memset(lastChannel, 0, sizeof(lastChannel));
  numTransponders = 0;
  transponders = NULL;
}

cEITScanner::~cEITScanner()
{
  free(transponders);
}

bool cEITScanner::TransponderScanned(cChannel *Channel)
{
  for (int i = 0; i < numTransponders; i++) {
      if (transponders[i] == Channel->Frequency())
         return true;
      }
  transponders = (int *)realloc(transponders, ++numTransponders * sizeof(int));
  transponders[numTransponders - 1] = Channel->Frequency();
  return false;
}

void cEITScanner::Activity(void)
{
  if (currentChannel) {
     Channels.SwitchTo(currentChannel);
     currentChannel = 0;
     }
  lastActivity = time(NULL);
}

void cEITScanner::Process(void)
{
  if (Setup.EPGScanTimeout && Channels.MaxNumber() > 1) {
     time_t now = time(NULL);
     if (now - lastScan > ScanTimeout && now - lastActivity > ActivityTimeout) {
        for (int i = 0; i < cDevice::NumDevices(); i++) {
            cDevice *Device = cDevice::GetDevice(i);
            if (Device && Device->CardIndex() < MAXDVBDEVICES) {
               if (Device != cDevice::PrimaryDevice() || (cDevice::NumDevices() == 1 && Setup.EPGScanTimeout && now - lastActivity > Setup.EPGScanTimeout * 3600)) {
                  if (!(Device->Receiving(true) || Device->Replaying())) {
                     for (;;) {
                         cChannel *Channel = Channels.GetByNumber(lastChannel[Device->DeviceNumber()] + 1, 1);
                         if (Channel) {
                            lastChannel[Device->DeviceNumber()] = Channel->Number();
                            if (Channel->Sid() && Device->ProvidesChannel(Channel) && !TransponderScanned(Channel)) {
                               if (Device == cDevice::PrimaryDevice() && !currentChannel) {
                                  currentChannel = Device->CurrentChannel();
                                  }
                               currentDevice = Device;
                               Device->SwitchChannel(Channel, false);
                               currentDevice = NULL;
                               break;
                               }
                            }
                         else {
                            if (lastChannel[Device->DeviceNumber()])
                               numTransponders = 0;
                            lastChannel[Device->DeviceNumber()] = 0;
                            break;
                            }
                         }
                     }
                  }
               }
            }
        lastScan = time(NULL);
        }
     }
}