vdr/epg.c
Klaus Schmidinger a26aae3ce8 Version 2.3.1
VDR developer version 2.3.1 is now available at

       ftp://ftp.tvdr.de/vdr/Developer/vdr-2.3.1.tar.bz2

A 'diff' against the previous version is available at

       ftp://ftp.tvdr.de/vdr/Developer/vdr-2.2.0-2.3.1.diff

MD5 checksums:

391c2ed60e2f7d24563fe3ed5854bc4f  vdr-2.3.1.tar.bz2
983fd4bad7d19cd98301d54173107129  vdr-2.2.0-2.3.1.diff

WARNING:
========

This is a *developer* version. Even though *I* use it in my productive
environment, I strongly recommend that you only use it under controlled
conditions and for testing and debugging.

*** PLEASE BE VERY CAREFUL WHEN USING THIS DEVELOPER VERSION, ESPECIALLY
*** IF YOU ENABLE THE NEW SVDRP PEERING! KEEP BACKUPS OF ALL YOUR TIMERS
*** AND OBSERVE VERY CLOSELY WHETHER EVERYTHING WORKS AS EXPECTED. THIS
*** VERSION INTRODUCES SOME MAJOR CHANGES IN HANDLING GLOBAL LISTS AND
*** LOCKING, SO ANYTHING CAN HAPPEN! YOU HAVE BEEN WARNED!

The main focus of this developer version is on the new locking mechanism
for global lists, and the ability to handle remote timers.
Any plugins that access the global lists of timers, channels, schedules
or recordings, will need to be adjusted (see below for details). Please
do initial tests with plain vanilla VDR and just the output plugin you
need.

Known bugs/problems:

- After deleting the last recording in a sub folder, the cursor may not
   be positioned correctly.
- Instant recordings and pausing live video don't (yet) use the default
   SVDRP host for recording.

From the HISTORY file:
 - The new function cOsd::MaxPixmapSize() can be called to determine the maximum size
  a cPixmap may have on the current OSD. The 'osddemo' example has been modified
  accordingly. Plugin authors may want to use this function in case they use pixmaps
  that are larger than the full OSD size. The default implementation sets this limit
  to 2048x2048 pixel.
- The Setup/CAM menu now displays which device an individual CAM is currently
  assigned to (suggested by Frank Neumann).
- Added detection of 24fps (thanks to Thomas Reufer).
- Added a note about the VDR User Counter and VDR's facebook page to the README file.
- The dvbhddevice plugin is no longer part of the VDR source archive.
  You can get the latest version of this plugin from the author's repository at
  https://bitbucket.org/powARman/dvbhddevice.
- The dvbsddevice and rcu plugins are no longer part of the VDR source archive.
  You can get the latest versions of these plugins from ftp://ftp.tvdr.de/vdr/Plugins.
- Added a section about Output Devices to the INSTALL file.
- Fixed setting the source value of newly created channels, in case the NIT is
  received from a different, but very close satellite position (reported by Daniel
  Ribeiro). The code for handling different NITs has been removed from nit.c, because
  according to the DVB standard table id 0x40 carries only the NIT of the actual
  network.
- Added some comment to cPixmap about the relation between OSD, ViewPort and DrawPort
  (suggested by Thomas Reufer).
- Improved syncing on sections when parsing the NIT and SDT.
- Fixed scaling subtitles (their areas could sometimes extend outside the actual OSD).
- Reduced the priority of the "video directory scanner" thread (suggested by Thomas
  Reufer) and checking cIoThrottle::Engaged() when it is running.
- The script that gets called for recordings is now also called right before a
  recording is edited, with the first parameter being "editing" (suggested by
  Dieter Ferdinand).
- The new setup option "OSD/Default sort mode for recordings" can be used to define
  how recordings shall be sorted by default (either by time or by name, with "by time"
  being the default). If a particular sort mode has been selected for a folder by
  pressing '0', the default no longer applies to that folder. Repeating timers no
  longer write a ".sort" file into a recordings folder to have the recordings sorted
  by time.
- The command line option -D now accepts the value '-' (as in -D-), which prevents
  VDR from using any DVB devices (suggested by Dietmar Spingler).
- The -V and -h options now list the plugins in alphabetical order (suggested by
  Dietmar Spingler).
- Fixed a compiler warning in font.c.
- Commented out the line
  #define DEPRECATED_VIDEOSYSTEM
  in device.h. If a plugin doesn't compile with this version of VDR, you can uncomment
  this line as a quick workaround. In the long run the plugin will need to be adapted.
- The function cOsd::GetBitmap() is now 'protected'. If a plugin doesn't compile with
  this version of VDR, you can uncomment the line
  //#define DEPRECATED_GETBITMAP
  in osd.h as a quick workaround. In the long run the plugin will need to be adapted.
- The -u option now also accepts a numerical user id (suggested by Derek Kelly).
- The SVDRP port now accepts multiple concurrent connections. You can now keep an
  SVDRP connection open as long as you wish, without preventing others from
  connecting. Note, though, that SVDRP connections still get closed automatically
  if there has been no activity for 300 seconds (configurable via
  "Setup/Miscellaneous/SVDRP timeout (s)").
- The SVDRP log messages have been unified and now always contain the IP and port
  number of the remote host.
- SVDRP connections are now handled in a separate "SVDRP server handler" thread,
  which makes them more responsive. Note that there is only one thread that handles
  all concurrent SVDRP connections. That way each SVDRP command is guaranteed to be
  processed separately, without interfering with any other SVDRP commands that might
  be issued at the same time. Plugins that implement SVDRP commands may need to take
  care of proper locking if the commands access global data.
- VDR now sends out a broadcast to port 6419/udp, which was assigned to 'svdrp-disc'
  by the IANA. VDRs listening on that port will automatically initiate an SVDRP
  connection to the broadcasting VDR, and in turn send out a broadcast to make
  other VDRs connect to them. That way all VDRs within the local network will
  have permanent "peer-to-peer" SVDRP connections between each other. The
  configuration in the svdrphosts.conf file is taken into account when considering
  whether or not to respond to an SVDRP discover broadcast.
- The new SVDRP command PING is used by automatically established peer-to-peer
  connections to keep them alive.
- The new function GetSVDRPServerNames() can be used to get a list of all VDRs
  this VDR is connected to via SVDRP.
- The new function ExecSVDRPCommand() can be used to execute an SVDRP command on
  one of the servers this VDR is connected to, and retrieve the result.
  The helper functions SVDRPCode() and SVDRPValue() can be used to easily access
  the codes and values returned by ExecSVDRPCommand().
- The cTimer class now has a new member named 'remote', which holds the name of the
  remote server this timer will record on. If this is NULL, it is a local timer.
- Timers from other VDRs that are connected to this VDR via SVDRP are now
  automatically fetched and stored in the global Timers list. In order for this
  to work, all of the channels used by timers on the remote VDR must also be
  defined on the local VDR (however, not necessarily in the same sequence).
  Automatic channel syncing will be implemented later.
- The main menu of the LCARS skin now displays a small rectangle on the left side
  of a timer if this is a remote timer. The color of that rectangle changes if
  the timer is currently recording on the remote VDR.
- Accessing the global Timers list now has to be protected by proper locking,
  because SVDRP commands are now executed in a separate thread.
  The introduction of this locking mechanism required the following changes:
  + The new classes cStateLock and cStateKey are used to implement locking
    with quick detection of state changes.
  + cConfig::cConfig() now has a parameter that indicates whether this list
    requires locking.
  + The global lists of Timers, Channels, Schedules and Recordings are no longer
    static variables. They are now pointers that need to be retrieved through
    a call to cTimers::GetTimersRead/Write(), cChannels::GetChannelsRead/Write(),
    cSchedules::GetSchedulesRead/Write() and cRecordings::GetRecordingsRead/Write(),
    respectively.
  + References from/to link channels are now removed in cChannels::Del() rather
    than cChannel::~cChannel(), to make sure the caller holds a proper lock.
  + cChannel::HasTimer() has been removed. This information is now retrieved
    via cSchedule::HasTimer().
  + Several member functions of cChannel, cTimer, cMarks and cRecording have
    been made 'const', and some of them are now available as both 'const' and
    'non-const' versions.
  + The cChannel::Set...() functions are now 'bool' and return true if they have
    actually changed any of the channels's members.
  + cChannels::SetModified() has been renamed to cChannels::SetModifiedByUser().
  + cChannels::Modified() has been renamed to cChannels::ModifiedByUser(), and
    now has a 'State' parameter that allows the caller to see whether a channel
    has been modified since the last call to this function with the same State
    variable.
  + The macros CHANNELSMOD_NONE/_AUTO/_USER have been removed.
  + cMarks now requires locking via cStateKey.
  + cSortedTimers now requires a pointer to the list of timers.
  + cEvent::HasTimer() no longer scans the list of timers to check whether an event
    is referenced by a timer, but rather keeps score of how many timers reference
    it. This was necessary in order to avoid having to lock the list of timers from
    within a cEvent.
  + The new class cListGarbageCollector is used to temporary store any objects deleted
    from cLists that require locking. This allows pointers to such objects to be
    dereferenced even if the objects are no longer part of the list.
  + cListBase::Contains() can be used to check whether a particular object is still
    contained in that list.
  + Outdated events are no longer "phased out", but rather deleted right away and thus
    taken care of by the new "garbage collector" of the list.
  + Deleted cRecording objects are no longer kept in a list of "vanished" recordings,
    but are rather taken care of by the new "garbage collector" of the list.
  + cSchedules::ClearAll() has been removed. The functionality is now implemented
    directly in cSVDRPServer::CmdCLRE().
  + tEventID has been changed to u_int16_t in order to make room for the new member
    numTimers in cEvent.
  + cSchedule now has a member Modified(), which can be used with a State variable
    to quickly determine whether this schedule has been modified since the last call
    to this function with the same State variable.
  + cSchedulesLock has been removed. Locking the list of schedules is now done via
    the cList's new locking mechanism.
  + The 'OnlyRunningStatus' parameters in cEpgHandler::BeginSegmentTransfer() and
    cEpgHandler::EndSegmentTransfer() are now obsolete. They are still present in
    the interface for backward compatibility, but may be removed in a future version.
    Their value is always 'false'.
  + The constant tcMod is no longer used in cStatus::TimerChange(). The definition is
    still there for backward compatibility.
  Plugins that access the global lists of Timers, Channels, Recordings or Schedules
  will need to be adapted as follows:
  + Instead of directly accessing the global variables Timers, Channels or Recordings,
    they need to set up a cStateKey variable and call the proper getter function,
    as in
      cStateKey StateKey;
      if (const cTimers *Timers = cTimers::GetTimersRead(StateKey)) {
         // access the timers
         StateKey.Remove();
         }
    and
      cStateKey StateKey;
      if (cTimers *Timers = cTimers::GetTimersWrite(StateKey)) {
         // access the timers
         StateKey.Remove();
         }
    See timers.h, thread.h and tools.h for details on this new locking mechanism.
  + There are convenience macros for easily accessing these lists without having
    to explicitly set up a cStateKey and calling its Remove() function. These macros
    have the form LOCK_*_READ/WRITE (with '*' being TIMERS, CHANNELS, SCHEDULES or
    RECORDINGS). Simply put such a macro before the point where you need to access
    the respective list, and there will be a pointer named Timers, Channels, Schedules
    or Recordings, respectively, which is valid until the end of the current block.
  + If a plugin needs to access several of the global lists in parallel, locking must
    always be done in the sequence Timers, Channels, Recordings, Schedules. This is
    necessary to make sure that different threads that need to lock several lists at
    the same time don't end up in a deadlock.
  + Some pointer variables may need to be made 'const'. The compiler will tell you
    about these.
- cSectionSyncer has been improved to better handle missed sections.
- Added a missing initialization of 'seen' in cChannel's copy constructor.
- Background modifications of channels, timers and events are now displayed immediately
  in the corresponding menus.
- cEIT now checks the version of the tables before doing any processing, which saves
  a lot of locking and processing.
- If a timer is newly created with the Red button in the Schedule menu, and the timer
  is presented to the user in the "Edit timer" menu because it will start immediately,
  it now *must* be confirmed with "Ok" to set the timer. Otherwise the timer will not
  be created.
- Recordings and deleted recordings are now scanned in a single thread.
- The new SVDRP command POLL is used by automatically established peer-to-peer
  connections to trigger fetching remote timers.
- You can now set DumpSVDRPDataTransfer in svdrp.c to true to have all SVDRP
  communication printed to the console for debugging.
- Added a missing 'const' to cReceiver::Receive(), to protect the given Data from
  being modified.
- The SVDRP commands that deal with timers (DELT, LSTT, MODT, NEWT, NEXT and UPDT)
  as well as any log messages that refer to timers, now use a unique id for each
  timer, which remains valid as long as this instance of VDR is running. This means
  that timers are no longer continuously numbered from 1 to N in LSTT. There may be
  gaps in the sequence, in case timers have been deleted.
- The Timers menu now displays the name of the remote VDR in front of the timer's
  file name, if this is a remote timer.
- The new options "Setup/Miscellaneous/SVDRP peering", ".../SVDRP host name" and
  ".../SVDRP default host" can be used to configure automatic peering between VDRs
  in the same network. Peering is disabled by default and can be enabled by setting
  "SVDRP peering" to "yes".
- The function cTimer::ToText() no longer returns a newline character at the end of
  the string. The newline is now added by the caller as necessary. This was changed
  because cTimer::ToText() is now also needed in a context where the terminating
  newline can't be used. Consequently, cChannel::ToText() and cMark::ToText() have
  been modified accordingly.
- All timer related response strings from SVDRP commands now use the channel ID
  instead of channel numbers.
- The "Edit timer" menu now has a new parameter "Record on", which can be used to
  select the VDR on which this timer shall record. Timers can be freely moved
  between connected VDRs by simply selecting the desired machine in this field.
- The SVDRP command DELT no longer checks whether the timer that shall be deleted
  is currently recording.
- The character 0x0D is now stripped from EPG texts (reported by Janne Pänkälä).
- The EPG scanner no longer moves the dish if there is a positioner.
- The 'newplugin' script now creates the 'po' subdirectory for translations (thanks
  to Thomas Reufer).
- Skins can now implement cSkinDisplayMenu::MenuOrientation() to display horizontal
  menus (thanks to Stefan Braun).
- Fixed a possible stack overflow in cListBase::Sort() (thanks to Oliver Endriss).
- Changed the description of the --chartab option in the INSTALL file to refer to
  "DVB SI table strings" instead of "EPG data".
- The width and height of the OSD are now limited to the actual maximum dimensions
  of the output device, taking into account the top and left offset.
- The new setup option "Recording/Record key handling" can be used to define
  what happens if the Record key on the remote control is pressed during
  live tv (suggested by Dietmar Spingler).
- Empty adaptation field TS packets are now skipped when recording (thanks to
  Christopher Reimer, based on the "AFFcleaner" by Stefan Pöschel).
2015-09-18 00:04:12 +02:00

1545 lines
45 KiB
C

/*
* epg.c: Electronic Program Guide
*
* See the main source file 'vdr.c' for copyright information and
* how to reach the author.
*
* Original version (as used in VDR before 1.3.0) written by
* Robert Schneider <Robert.Schneider@web.de> and Rolf Hakenes <hakenes@hippomi.de>.
*
* $Id: epg.c 4.2 2015/09/10 10:58:19 kls Exp $
*/
#include "epg.h"
#include <ctype.h>
#include <limits.h>
#include <time.h>
#include "libsi/si.h"
#define RUNNINGSTATUSTIMEOUT 30 // seconds before the running status is considered unknown
#define EPGDATAWRITEDELTA 600 // seconds between writing the epg.data file
// --- tComponent ------------------------------------------------------------
cString tComponent::ToString(void)
{
char buffer[256];
snprintf(buffer, sizeof(buffer), "%X %02X %s %s", stream, type, language, description ? description : "");
return buffer;
}
bool tComponent::FromString(const char *s)
{
unsigned int Stream, Type;
int n = sscanf(s, "%X %02X %7s %m[^\n]", &Stream, &Type, language, &description); // 7 = MAXLANGCODE2 - 1
if (n != 4 || isempty(description)) {
free(description);
description = NULL;
}
stream = Stream;
type = Type;
return n >= 3;
}
// --- cComponents -----------------------------------------------------------
cComponents::cComponents(void)
{
numComponents = 0;
components = NULL;
}
cComponents::~cComponents(void)
{
for (int i = 0; i < numComponents; i++)
free(components[i].description);
free(components);
}
bool cComponents::Realloc(int Index)
{
if (Index >= numComponents) {
Index++;
if (tComponent *NewBuffer = (tComponent *)realloc(components, Index * sizeof(tComponent))) {
int n = numComponents;
numComponents = Index;
components = NewBuffer;
memset(&components[n], 0, sizeof(tComponent) * (numComponents - n));
}
else {
esyslog("ERROR: out of memory");
return false;
}
}
return true;
}
void cComponents::SetComponent(int Index, const char *s)
{
if (Realloc(Index))
components[Index].FromString(s);
}
void cComponents::SetComponent(int Index, uchar Stream, uchar Type, const char *Language, const char *Description)
{
if (!Realloc(Index))
return;
tComponent *p = &components[Index];
p->stream = Stream;
p->type = Type;
strn0cpy(p->language, Language, sizeof(p->language));
char *q = strchr(p->language, ',');
if (q)
*q = 0; // strips rest of "normalized" language codes
p->description = strcpyrealloc(p->description, !isempty(Description) ? Description : NULL);
}
tComponent *cComponents::GetComponent(int Index, uchar Stream, uchar Type)
{
for (int i = 0; i < numComponents; i++) {
if (components[i].stream == Stream && (
Type == 0 || // don't care about the actual Type
Stream == 2 && (components[i].type < 5) == (Type < 5) // fallback "Dolby" component according to the "Premiere pseudo standard"
)) {
if (!Index--)
return &components[i];
}
}
return NULL;
}
// --- cEvent ----------------------------------------------------------------
cMutex cEvent::numTimersMutex;
cEvent::cEvent(tEventID EventID)
{
schedule = NULL;
numTimers = 0;
eventID = EventID;
tableID = 0xFF; // actual table ids are 0x4E..0x60
version = 0xFF; // actual version numbers are 0..31
runningStatus = SI::RunningStatusUndefined;
title = NULL;
shortText = NULL;
description = NULL;
components = NULL;
memset(contents, 0, sizeof(contents));
parentalRating = 0;
startTime = 0;
duration = 0;
vps = 0;
SetSeen();
}
cEvent::~cEvent()
{
free(title);
free(shortText);
free(description);
delete components;
}
int cEvent::Compare(const cListObject &ListObject) const
{
cEvent *e = (cEvent *)&ListObject;
return startTime - e->startTime;
}
tChannelID cEvent::ChannelID(void) const
{
return schedule ? schedule->ChannelID() : tChannelID();
}
void cEvent::SetEventID(tEventID EventID)
{
if (eventID != EventID) {
if (schedule)
schedule->UnhashEvent(this);
eventID = EventID;
if (schedule)
schedule->HashEvent(this);
}
}
void cEvent::SetTableID(uchar TableID)
{
tableID = TableID;
}
void cEvent::SetVersion(uchar Version)
{
version = Version;
}
void cEvent::SetRunningStatus(int RunningStatus, const cChannel *Channel)
{
if (Channel && runningStatus != RunningStatus && (RunningStatus > SI::RunningStatusNotRunning || runningStatus > SI::RunningStatusUndefined) && schedule && schedule->HasTimer())
isyslog("channel %d (%s) event %s status %d", Channel->Number(), Channel->Name(), *ToDescr(), RunningStatus);
runningStatus = RunningStatus;
}
void cEvent::SetTitle(const char *Title)
{
title = strcpyrealloc(title, Title);
}
void cEvent::SetShortText(const char *ShortText)
{
shortText = strcpyrealloc(shortText, ShortText);
}
void cEvent::SetDescription(const char *Description)
{
description = strcpyrealloc(description, Description);
}
void cEvent::SetComponents(cComponents *Components)
{
delete components;
components = Components;
}
void cEvent::SetContents(uchar *Contents)
{
for (int i = 0; i < MaxEventContents; i++)
contents[i] = Contents[i];
}
void cEvent::SetParentalRating(int ParentalRating)
{
parentalRating = ParentalRating;
}
void cEvent::SetStartTime(time_t StartTime)
{
if (startTime != StartTime) {
if (schedule)
schedule->UnhashEvent(this);
startTime = StartTime;
if (schedule)
schedule->HashEvent(this);
}
}
void cEvent::SetDuration(int Duration)
{
duration = Duration;
}
void cEvent::SetVps(time_t Vps)
{
vps = Vps;
}
void cEvent::SetSeen(void)
{
seen = time(NULL);
}
cString cEvent::ToDescr(void) const
{
char vpsbuf[64] = "";
if (Vps())
sprintf(vpsbuf, "(VPS: %s) ", *GetVpsString());
return cString::sprintf("%s %s-%s %s'%s'", *GetDateString(), *GetTimeString(), *GetEndTimeString(), vpsbuf, Title());
}
void cEvent::IncNumTimers(void) const
{
numTimersMutex.Lock();
numTimers++;
if (schedule)
schedule->IncNumTimers();
numTimersMutex.Unlock();
}
void cEvent::DecNumTimers(void) const
{
numTimersMutex.Lock();
numTimers--;
if (schedule)
schedule->DecNumTimers();
numTimersMutex.Unlock();
}
bool cEvent::IsRunning(bool OrAboutToStart) const
{
return runningStatus >= (OrAboutToStart ? SI::RunningStatusStartsInAFewSeconds : SI::RunningStatusPausing);
}
const char *cEvent::ContentToString(uchar Content)
{
switch (Content & 0xF0) {
case ecgMovieDrama:
switch (Content & 0x0F) {
default:
case 0x00: return tr("Content$Movie/Drama");
case 0x01: return tr("Content$Detective/Thriller");
case 0x02: return tr("Content$Adventure/Western/War");
case 0x03: return tr("Content$Science Fiction/Fantasy/Horror");
case 0x04: return tr("Content$Comedy");
case 0x05: return tr("Content$Soap/Melodrama/Folkloric");
case 0x06: return tr("Content$Romance");
case 0x07: return tr("Content$Serious/Classical/Religious/Historical Movie/Drama");
case 0x08: return tr("Content$Adult Movie/Drama");
}
break;
case ecgNewsCurrentAffairs:
switch (Content & 0x0F) {
default:
case 0x00: return tr("Content$News/Current Affairs");
case 0x01: return tr("Content$News/Weather Report");
case 0x02: return tr("Content$News Magazine");
case 0x03: return tr("Content$Documentary");
case 0x04: return tr("Content$Discussion/Inverview/Debate");
}
break;
case ecgShow:
switch (Content & 0x0F) {
default:
case 0x00: return tr("Content$Show/Game Show");
case 0x01: return tr("Content$Game Show/Quiz/Contest");
case 0x02: return tr("Content$Variety Show");
case 0x03: return tr("Content$Talk Show");
}
break;
case ecgSports:
switch (Content & 0x0F) {
default:
case 0x00: return tr("Content$Sports");
case 0x01: return tr("Content$Special Event");
case 0x02: return tr("Content$Sport Magazine");
case 0x03: return tr("Content$Football/Soccer");
case 0x04: return tr("Content$Tennis/Squash");
case 0x05: return tr("Content$Team Sports");
case 0x06: return tr("Content$Athletics");
case 0x07: return tr("Content$Motor Sport");
case 0x08: return tr("Content$Water Sport");
case 0x09: return tr("Content$Winter Sports");
case 0x0A: return tr("Content$Equestrian");
case 0x0B: return tr("Content$Martial Sports");
}
break;
case ecgChildrenYouth:
switch (Content & 0x0F) {
default:
case 0x00: return tr("Content$Children's/Youth Programme");
case 0x01: return tr("Content$Pre-school Children's Programme");
case 0x02: return tr("Content$Entertainment Programme for 6 to 14");
case 0x03: return tr("Content$Entertainment Programme for 10 to 16");
case 0x04: return tr("Content$Informational/Educational/School Programme");
case 0x05: return tr("Content$Cartoons/Puppets");
}
break;
case ecgMusicBalletDance:
switch (Content & 0x0F) {
default:
case 0x00: return tr("Content$Music/Ballet/Dance");
case 0x01: return tr("Content$Rock/Pop");
case 0x02: return tr("Content$Serious/Classical Music");
case 0x03: return tr("Content$Folk/Tradional Music");
case 0x04: return tr("Content$Jazz");
case 0x05: return tr("Content$Musical/Opera");
case 0x06: return tr("Content$Ballet");
}
break;
case ecgArtsCulture:
switch (Content & 0x0F) {
default:
case 0x00: return tr("Content$Arts/Culture");
case 0x01: return tr("Content$Performing Arts");
case 0x02: return tr("Content$Fine Arts");
case 0x03: return tr("Content$Religion");
case 0x04: return tr("Content$Popular Culture/Traditional Arts");
case 0x05: return tr("Content$Literature");
case 0x06: return tr("Content$Film/Cinema");
case 0x07: return tr("Content$Experimental Film/Video");
case 0x08: return tr("Content$Broadcasting/Press");
case 0x09: return tr("Content$New Media");
case 0x0A: return tr("Content$Arts/Culture Magazine");
case 0x0B: return tr("Content$Fashion");
}
break;
case ecgSocialPoliticalEconomics:
switch (Content & 0x0F) {
default:
case 0x00: return tr("Content$Social/Political/Economics");
case 0x01: return tr("Content$Magazine/Report/Documentary");
case 0x02: return tr("Content$Economics/Social Advisory");
case 0x03: return tr("Content$Remarkable People");
}
break;
case ecgEducationalScience:
switch (Content & 0x0F) {
default:
case 0x00: return tr("Content$Education/Science/Factual");
case 0x01: return tr("Content$Nature/Animals/Environment");
case 0x02: return tr("Content$Technology/Natural Sciences");
case 0x03: return tr("Content$Medicine/Physiology/Psychology");
case 0x04: return tr("Content$Foreign Countries/Expeditions");
case 0x05: return tr("Content$Social/Spiritual Sciences");
case 0x06: return tr("Content$Further Education");
case 0x07: return tr("Content$Languages");
}
break;
case ecgLeisureHobbies:
switch (Content & 0x0F) {
default:
case 0x00: return tr("Content$Leisure/Hobbies");
case 0x01: return tr("Content$Tourism/Travel");
case 0x02: return tr("Content$Handicraft");
case 0x03: return tr("Content$Motoring");
case 0x04: return tr("Content$Fitness & Health");
case 0x05: return tr("Content$Cooking");
case 0x06: return tr("Content$Advertisement/Shopping");
case 0x07: return tr("Content$Gardening");
}
break;
case ecgSpecial:
switch (Content & 0x0F) {
case 0x00: return tr("Content$Original Language");
case 0x01: return tr("Content$Black & White");
case 0x02: return tr("Content$Unpublished");
case 0x03: return tr("Content$Live Broadcast");
default: ;
}
break;
default: ;
}
return "";
}
cString cEvent::GetParentalRatingString(void) const
{
if (parentalRating)
return cString::sprintf(tr("ParentalRating$from %d"), parentalRating);
return NULL;
}
cString cEvent::GetDateString(void) const
{
return DateString(startTime);
}
cString cEvent::GetTimeString(void) const
{
return TimeString(startTime);
}
cString cEvent::GetEndTimeString(void) const
{
return TimeString(startTime + duration);
}
cString cEvent::GetVpsString(void) const
{
char buf[25];
struct tm tm_r;
strftime(buf, sizeof(buf), "%d.%m. %R", localtime_r(&vps, &tm_r));
return buf;
}
void cEvent::Dump(FILE *f, const char *Prefix, bool InfoOnly) const
{
if (InfoOnly || startTime + duration + Setup.EPGLinger * 60 >= time(NULL)) {
fprintf(f, "%sE %u %ld %d %X %X\n", Prefix, eventID, startTime, duration, tableID, version);
if (!isempty(title))
fprintf(f, "%sT %s\n", Prefix, title);
if (!isempty(shortText))
fprintf(f, "%sS %s\n", Prefix, shortText);
if (!isempty(description)) {
strreplace(description, '\n', '|');
fprintf(f, "%sD %s\n", Prefix, description);
strreplace(description, '|', '\n');
}
if (contents[0]) {
fprintf(f, "%sG", Prefix);
for (int i = 0; Contents(i); i++)
fprintf(f, " %02X", Contents(i));
fprintf(f, "\n");
}
if (parentalRating)
fprintf(f, "%sR %d\n", Prefix, parentalRating);
if (components) {
for (int i = 0; i < components->NumComponents(); i++) {
tComponent *p = components->Component(i);
fprintf(f, "%sX %s\n", Prefix, *p->ToString());
}
}
if (vps)
fprintf(f, "%sV %ld\n", Prefix, vps);
if (!InfoOnly)
fprintf(f, "%se\n", Prefix);
}
}
bool cEvent::Parse(char *s)
{
char *t = skipspace(s + 1);
switch (*s) {
case 'T': SetTitle(t);
break;
case 'S': SetShortText(t);
break;
case 'D': strreplace(t, '|', '\n');
SetDescription(t);
break;
case 'G': {
memset(contents, 0, sizeof(contents));
for (int i = 0; i < MaxEventContents; i++) {
char *tail = NULL;
int c = strtol(t, &tail, 16);
if (0x00 < c && c <= 0xFF) {
contents[i] = c;
t = tail;
}
else
break;
}
}
break;
case 'R': SetParentalRating(atoi(t));
break;
case 'X': if (!components)
components = new cComponents;
components->SetComponent(components->NumComponents(), t);
break;
case 'V': SetVps(atoi(t));
break;
default: esyslog("ERROR: unexpected tag while reading EPG data: %s", s);
return false;
}
return true;
}
bool cEvent::Read(FILE *f, cSchedule *Schedule)
{
if (Schedule) {
cEvent *Event = NULL;
char *s;
int line = 0;
cReadLine ReadLine;
while ((s = ReadLine.Read(f)) != NULL) {
line++;
char *t = skipspace(s + 1);
switch (*s) {
case 'E': if (!Event) {
unsigned int EventID;
time_t StartTime;
int Duration;
unsigned int TableID = 0;
unsigned int Version = 0xFF; // actual value is ignored
int n = sscanf(t, "%u %ld %d %X %X", &EventID, &StartTime, &Duration, &TableID, &Version);
if (n >= 3 && n <= 5) {
Event = (cEvent *)Schedule->GetEvent(EventID, StartTime);
cEvent *newEvent = NULL;
if (Event)
DELETENULL(Event->components);
if (!Event) {
Event = newEvent = new cEvent(EventID);
Event->seen = 0;
}
if (Event) {
Event->SetTableID(TableID);
Event->SetStartTime(StartTime);
Event->SetDuration(Duration);
if (newEvent)
Schedule->AddEvent(newEvent);
}
}
}
break;
case 'e': if (Event && !Event->Title())
Event->SetTitle(tr("No title"));
Event = NULL;
break;
case 'c': // to keep things simple we react on 'c' here
return true;
default: if (Event && !Event->Parse(s)) {
esyslog("ERROR: EPG data problem in line %d", line);
return false;
}
}
}
esyslog("ERROR: unexpected end of file while reading EPG data");
}
return false;
}
#define MAXEPGBUGFIXSTATS 13
#define MAXEPGBUGFIXCHANS 100
struct tEpgBugFixStats {
int hits;
int n;
tChannelID channelIDs[MAXEPGBUGFIXCHANS];
tEpgBugFixStats(void) { hits = n = 0; }
};
tEpgBugFixStats EpgBugFixStats[MAXEPGBUGFIXSTATS];
static void EpgBugFixStat(int Number, tChannelID ChannelID)
{
if (0 <= Number && Number < MAXEPGBUGFIXSTATS) {
tEpgBugFixStats *p = &EpgBugFixStats[Number];
p->hits++;
int i = 0;
for (; i < p->n; i++) {
if (p->channelIDs[i] == ChannelID)
break;
}
if (i == p->n && p->n < MAXEPGBUGFIXCHANS)
p->channelIDs[p->n++] = ChannelID;
}
}
void ReportEpgBugFixStats(bool Force)
{
if (Setup.EPGBugfixLevel > 0) {
static time_t LastReport = 0;
time_t now = time(NULL);
if (now - LastReport > 3600 || Force) {
LastReport = now;
struct tm tm_r;
struct tm *ptm = localtime_r(&now, &tm_r);
if (ptm->tm_hour != 5)
return;
}
else
return;
bool GotHits = false;
char buffer[1024];
for (int i = 0; i < MAXEPGBUGFIXSTATS; i++) {
const char *delim = " ";
tEpgBugFixStats *p = &EpgBugFixStats[i];
if (p->hits) {
bool PrintedStats = false;
char *q = buffer;
*buffer = 0;
LOCK_CHANNELS_READ;
for (int c = 0; c < p->n; c++) {
if (const cChannel *Channel = Channels->GetByChannelID(p->channelIDs[c], true)) {
if (!GotHits) {
dsyslog("=====================");
dsyslog("EPG bugfix statistics");
dsyslog("=====================");
dsyslog("IF SOMEBODY WHO IS IN CHARGE OF THE EPG DATA FOR ONE OF THE LISTED");
dsyslog("CHANNELS READS THIS: PLEASE TAKE A LOOK AT THE FUNCTION cEvent::FixEpgBugs()");
dsyslog("IN VDR/epg.c TO LEARN WHAT'S WRONG WITH YOUR DATA, AND FIX IT!");
dsyslog("=====================");
dsyslog("Fix Hits Channels");
GotHits = true;
}
if (!PrintedStats) {
q += snprintf(q, sizeof(buffer) - (q - buffer), "%-3d %-4d", i, p->hits);
PrintedStats = true;
}
q += snprintf(q, sizeof(buffer) - (q - buffer), "%s%s", delim, Channel->Name());
delim = ", ";
if (q - buffer > 80) {
q += snprintf(q, sizeof(buffer) - (q - buffer), "%s...", delim);
break;
}
}
}
if (*buffer)
dsyslog("%s", buffer);
}
p->hits = p->n = 0;
}
if (GotHits)
dsyslog("=====================");
}
}
static void StripControlCharacters(char *s)
{
if (s) {
int len = strlen(s);
while (len > 0) {
int l = Utf8CharLen(s);
uchar *p = (uchar *)s;
if (l == 2 && *p == 0xC2) // UTF-8 sequence
p++;
if (*p == 0x86 || *p == 0x87 || *p == 0x0D) {
memmove(s, p + 1, len - l + 1); // we also copy the terminating 0!
len -= l;
l = 0;
}
s += l;
len -= l;
}
}
}
void cEvent::FixEpgBugs(void)
{
if (isempty(title)) {
// we don't want any "(null)" titles
title = strcpyrealloc(title, tr("No title"));
EpgBugFixStat(12, ChannelID());
}
if (Setup.EPGBugfixLevel == 0)
goto Final;
// Some TV stations apparently have their own idea about how to fill in the
// EPG data. Let's fix their bugs as good as we can:
// Some channels put the ShortText in quotes and use either the ShortText
// or the Description field, depending on how long the string is:
//
// Title
// "ShortText". Description
//
if ((shortText == NULL) != (description == NULL)) {
char *p = shortText ? shortText : description;
if (*p == '"') {
const char *delim = "\".";
char *e = strstr(p + 1, delim);
if (e) {
*e = 0;
char *s = strdup(p + 1);
char *d = strdup(e + strlen(delim));
free(shortText);
free(description);
shortText = s;
description = d;
EpgBugFixStat(1, ChannelID());
}
}
}
// Some channels put the Description into the ShortText (preceded
// by a blank) if there is no actual ShortText and the Description
// is short enough:
//
// Title
// Description
//
if (shortText && !description) {
if (*shortText == ' ') {
memmove(shortText, shortText + 1, strlen(shortText));
description = shortText;
shortText = NULL;
EpgBugFixStat(2, ChannelID());
}
}
// Sometimes they repeat the Title in the ShortText:
//
// Title
// Title
//
if (shortText && strcmp(title, shortText) == 0) {
free(shortText);
shortText = NULL;
EpgBugFixStat(3, ChannelID());
}
// Some channels put the ShortText between double quotes, which is nothing
// but annoying (some even put a '.' after the closing '"'):
//
// Title
// "ShortText"[.]
//
if (shortText && *shortText == '"') {
int l = strlen(shortText);
if (l > 2 && (shortText[l - 1] == '"' || (shortText[l - 1] == '.' && shortText[l - 2] == '"'))) {
memmove(shortText, shortText + 1, l);
char *p = strrchr(shortText, '"');
if (p)
*p = 0;
EpgBugFixStat(4, ChannelID());
}
}
if (Setup.EPGBugfixLevel <= 1)
goto Final;
// Some channels apparently try to do some formatting in the texts,
// which is a bad idea because they have no way of knowing the width
// of the window that will actually display the text.
// Remove excess whitespace:
title = compactspace(title);
shortText = compactspace(shortText);
description = compactspace(description);
#define MAX_USEFUL_EPISODE_LENGTH 40
// Some channels put a whole lot of information in the ShortText and leave
// the Description totally empty. So if the ShortText length exceeds
// MAX_USEFUL_EPISODE_LENGTH, let's put this into the Description
// instead:
if (!isempty(shortText) && isempty(description)) {
if (strlen(shortText) > MAX_USEFUL_EPISODE_LENGTH) {
free(description);
description = shortText;
shortText = NULL;
EpgBugFixStat(6, ChannelID());
}
}
// Some channels put the same information into ShortText and Description.
// In that case we delete one of them:
if (shortText && description && strcmp(shortText, description) == 0) {
if (strlen(shortText) > MAX_USEFUL_EPISODE_LENGTH) {
free(shortText);
shortText = NULL;
}
else {
free(description);
description = NULL;
}
EpgBugFixStat(7, ChannelID());
}
// Some channels use the ` ("backtick") character, where a ' (single quote)
// would be normally used. Actually, "backticks" in normal text don't make
// much sense, so let's replace them:
strreplace(title, '`', '\'');
strreplace(shortText, '`', '\'');
strreplace(description, '`', '\'');
if (Setup.EPGBugfixLevel <= 2)
goto Final;
// The stream components have a "description" field which some channels
// apparently have no idea of how to set correctly:
if (components) {
for (int i = 0; i < components->NumComponents(); i++) {
tComponent *p = components->Component(i);
switch (p->stream) {
case 0x01: { // video
if (p->description) {
if (strcasecmp(p->description, "Video") == 0 ||
strcasecmp(p->description, "Bildformat") == 0) {
// Yes, we know it's video - that's what the 'stream' code
// is for! But _which_ video is it?
free(p->description);
p->description = NULL;
EpgBugFixStat(8, ChannelID());
}
}
if (!p->description) {
switch (p->type) {
case 0x01:
case 0x05: p->description = strdup("4:3"); break;
case 0x02:
case 0x03:
case 0x06:
case 0x07: p->description = strdup("16:9"); break;
case 0x04:
case 0x08: p->description = strdup(">16:9"); break;
case 0x09:
case 0x0D: p->description = strdup("HD 4:3"); break;
case 0x0A:
case 0x0B:
case 0x0E:
case 0x0F: p->description = strdup("HD 16:9"); break;
case 0x0C:
case 0x10: p->description = strdup("HD >16:9"); break;
default: ;
}
EpgBugFixStat(9, ChannelID());
}
}
break;
case 0x02: { // audio
if (p->description) {
if (strcasecmp(p->description, "Audio") == 0) {
// Yes, we know it's audio - that's what the 'stream' code
// is for! But _which_ audio is it?
free(p->description);
p->description = NULL;
EpgBugFixStat(10, ChannelID());
}
}
if (!p->description) {
switch (p->type) {
case 0x05: p->description = strdup("Dolby Digital"); break;
default: ; // all others will just display the language
}
EpgBugFixStat(11, ChannelID());
}
}
break;
default: ;
}
}
}
Final:
// VDR can't usefully handle newline characters in the title, shortText or component description of EPG
// data, so let's always convert them to blanks (independent of the setting of EPGBugfixLevel):
strreplace(title, '\n', ' ');
strreplace(shortText, '\n', ' ');
if (components) {
for (int i = 0; i < components->NumComponents(); i++) {
tComponent *p = components->Component(i);
if (p->description)
strreplace(p->description, '\n', ' ');
}
}
// Same for control characters:
StripControlCharacters(title);
StripControlCharacters(shortText);
StripControlCharacters(description);
}
// --- cSchedule -------------------------------------------------------------
cMutex cSchedule::numTimersMutex;
cSchedule::cSchedule(tChannelID ChannelID)
{
channelID = ChannelID;
events.SetUseGarbageCollector();
numTimers = 0;
hasRunning = false;
modified = 0;
presentSeen = 0;
}
void cSchedule::IncNumTimers(void) const
{
numTimersMutex.Lock();
numTimers++;
numTimersMutex.Unlock();
}
void cSchedule::DecNumTimers(void) const
{
numTimersMutex.Lock();
numTimers--;
numTimersMutex.Unlock();
}
cEvent *cSchedule::AddEvent(cEvent *Event)
{
events.Add(Event);
Event->schedule = this;
HashEvent(Event);
return Event;
}
void cSchedule::DelEvent(cEvent *Event)
{
if (Event->schedule == this) {
UnhashEvent(Event);
events.Del(Event);
}
}
void cSchedule::HashEvent(cEvent *Event)
{
eventsHashID.Add(Event, Event->EventID());
if (Event->StartTime() > 0) // 'StartTime < 0' is apparently used with NVOD channels
eventsHashStartTime.Add(Event, Event->StartTime());
}
void cSchedule::UnhashEvent(cEvent *Event)
{
eventsHashID.Del(Event, Event->EventID());
if (Event->StartTime() > 0) // 'StartTime < 0' is apparently used with NVOD channels
eventsHashStartTime.Del(Event, Event->StartTime());
}
const cEvent *cSchedule::GetPresentEvent(void) const
{
const cEvent *pe = NULL;
time_t now = time(NULL);
for (const cEvent *p = events.First(); p; p = events.Next(p)) {
if (p->StartTime() <= now)
pe = p;
else if (p->StartTime() > now + 3600)
break;
if (p->SeenWithin(RUNNINGSTATUSTIMEOUT) && p->RunningStatus() >= SI::RunningStatusPausing)
return p;
}
return pe;
}
const cEvent *cSchedule::GetFollowingEvent(void) const
{
const cEvent *p = GetPresentEvent();
if (p)
p = events.Next(p);
else {
time_t now = time(NULL);
for (p = events.First(); p; p = events.Next(p)) {
if (p->StartTime() >= now)
break;
}
}
return p;
}
const cEvent *cSchedule::GetEvent(tEventID EventID, time_t StartTime) const
{
// Returns the event info with the given StartTime or, if no actual StartTime
// is given, the one with the given EventID.
if (StartTime > 0) // 'StartTime < 0' is apparently used with NVOD channels
return eventsHashStartTime.Get(StartTime);
else
return eventsHashID.Get(EventID);
}
const cEvent *cSchedule::GetEventAround(time_t Time) const
{
const cEvent *pe = NULL;
time_t delta = INT_MAX;
for (const cEvent *p = events.First(); p; p = events.Next(p)) {
time_t dt = Time - p->StartTime();
if (dt >= 0 && dt < delta && p->EndTime() >= Time) {
delta = dt;
pe = p;
}
}
return pe;
}
void cSchedule::SetRunningStatus(cEvent *Event, int RunningStatus, const cChannel *Channel)
{
hasRunning = false;
for (cEvent *p = events.First(); p; p = events.Next(p)) {
if (p == Event) {
if (p->RunningStatus() > SI::RunningStatusNotRunning || RunningStatus > SI::RunningStatusNotRunning) {
p->SetRunningStatus(RunningStatus, Channel);
break;
}
}
else if (RunningStatus >= SI::RunningStatusPausing && p->StartTime() < Event->StartTime())
p->SetRunningStatus(SI::RunningStatusNotRunning);
if (p->RunningStatus() >= SI::RunningStatusPausing)
hasRunning = true;
}
SetPresentSeen();
}
void cSchedule::ClrRunningStatus(cChannel *Channel)
{
if (hasRunning) {
for (cEvent *p = events.First(); p; p = events.Next(p)) {
if (p->RunningStatus() >= SI::RunningStatusPausing) {
p->SetRunningStatus(SI::RunningStatusNotRunning, Channel);
hasRunning = false;
break;
}
}
}
}
void cSchedule::ResetVersions(void)
{
for (cEvent *p = events.First(); p; p = events.Next(p))
p->SetVersion(0xFF);
}
void cSchedule::Sort(void)
{
events.Sort();
// Make sure there are no RunningStatusUndefined before the currently running event:
if (hasRunning) {
for (cEvent *p = events.First(); p; p = events.Next(p)) {
if (p->RunningStatus() >= SI::RunningStatusPausing)
break;
p->SetRunningStatus(SI::RunningStatusNotRunning);
}
}
SetModified();
}
void cSchedule::DropOutdated(time_t SegmentStart, time_t SegmentEnd, uchar TableID, uchar Version)
{
if (SegmentStart > 0 && SegmentEnd > 0) {
cEvent *p = events.First();
while (p) {
cEvent *n = events.Next(p);
if (p->EndTime() > SegmentStart) {
if (p->StartTime() < SegmentEnd) {
// The event overlaps with the given time segment.
if (p->TableID() > TableID || p->TableID() == TableID && p->Version() != Version) {
// The segment overwrites all events from tables with higher ids, and
// within the same table id all events must have the same version.
DelEvent(p);
}
}
else
break;
}
p = n;
}
}
}
void cSchedule::Cleanup(void)
{
Cleanup(time(NULL));
}
void cSchedule::Cleanup(time_t Time)
{
cEvent *Event;
while ((Event = events.First()) != NULL) {
if (!Event->HasTimer() && Event->EndTime() + Setup.EPGLinger * 60 + 3600 < Time) // adding one hour for safety
DelEvent(Event);
else
break;
}
}
void cSchedule::Dump(FILE *f, const char *Prefix, eDumpMode DumpMode, time_t AtTime) const
{
LOCK_CHANNELS_READ;
if (const cChannel *Channel = Channels->GetByChannelID(channelID, true)) {
fprintf(f, "%sC %s %s\n", Prefix, *Channel->GetChannelID().ToString(), Channel->Name());
const cEvent *p;
switch (DumpMode) {
case dmAll: {
for (p = events.First(); p; p = events.Next(p))
p->Dump(f, Prefix);
}
break;
case dmPresent: {
if ((p = GetPresentEvent()) != NULL)
p->Dump(f, Prefix);
}
break;
case dmFollowing: {
if ((p = GetFollowingEvent()) != NULL)
p->Dump(f, Prefix);
}
break;
case dmAtTime: {
if ((p = GetEventAround(AtTime)) != NULL)
p->Dump(f, Prefix);
}
break;
default: esyslog("ERROR: unknown DumpMode %d (%s %d)", DumpMode, __FUNCTION__, __LINE__);
}
fprintf(f, "%sc\n", Prefix);
}
}
bool cSchedule::Read(FILE *f, cSchedules *Schedules)
{
if (Schedules) {
cReadLine ReadLine;
char *s;
while ((s = ReadLine.Read(f)) != NULL) {
if (*s == 'C') {
s = skipspace(s + 1);
char *p = strchr(s, ' ');
if (p)
*p = 0; // strips optional channel name
if (*s) {
tChannelID channelID = tChannelID::FromString(s);
if (channelID.Valid()) {
if (cSchedule *p = Schedules->AddSchedule(channelID)) {
if (!cEvent::Read(f, p))
return false;
p->Sort();
}
}
else {
esyslog("ERROR: invalid channel ID: %s", s);
return false;
}
}
}
else {
esyslog("ERROR: unexpected tag while reading EPG data: %s", s);
return false;
}
}
return true;
}
return false;
}
// --- cEpgDataWriter --------------------------------------------------------
class cEpgDataWriter : public cThread {
private:
cMutex mutex;
bool dump;
protected:
virtual void Action(void);
public:
cEpgDataWriter(void);
void SetDump(bool Dump) { dump = Dump; }
void Perform(void);
};
cEpgDataWriter::cEpgDataWriter(void)
:cThread("epg data writer", true)
{
dump = false;
}
void cEpgDataWriter::Action(void)
{
Perform();
}
void cEpgDataWriter::Perform(void)
{
cMutexLock MutexLock(&mutex); // to make sure fore- and background calls don't cause parellel dumps!
{
cStateKey StateKey;
if (cSchedules *Schedules = cSchedules::GetSchedulesWrite(StateKey, 1000)) {
time_t now = time(NULL);
for (cSchedule *p = Schedules->First(); p; p = Schedules->Next(p))
p->Cleanup(now);
StateKey.Remove();
}
}
if (dump)
cSchedules::Dump();
}
static cEpgDataWriter EpgDataWriter;
// --- cSchedules ------------------------------------------------------------
cSchedules cSchedules::schedules;
char *cSchedules::epgDataFileName = NULL;
time_t cSchedules::lastDump = time(NULL);
cSchedules::cSchedules(void)
:cList<cSchedule>("Schedules")
{
}
const cSchedules *cSchedules::GetSchedulesRead(cStateKey &StateKey, int TimeoutMs)
{
return schedules.Lock(StateKey, false, TimeoutMs) ? &schedules : NULL;
}
cSchedules *cSchedules::GetSchedulesWrite(cStateKey &StateKey, int TimeoutMs)
{
return schedules.Lock(StateKey, true, TimeoutMs) ? &schedules : NULL;
}
void cSchedules::SetEpgDataFileName(const char *FileName)
{
free(epgDataFileName);
epgDataFileName = FileName ? strdup(FileName) : NULL;
EpgDataWriter.SetDump(epgDataFileName != NULL);
}
void cSchedules::Cleanup(bool Force)
{
if (Force)
lastDump = 0;
time_t now = time(NULL);
if (now - lastDump > EPGDATAWRITEDELTA) {
if (Force)
EpgDataWriter.Perform();
else if (!EpgDataWriter.Active())
EpgDataWriter.Start();
lastDump = now;
}
}
void cSchedules::ResetVersions(void)
{
LOCK_SCHEDULES_WRITE;
for (cSchedule *Schedule = Schedules->First(); Schedule; Schedule = Schedules->Next(Schedule))
Schedule->ResetVersions();
}
bool cSchedules::Dump(FILE *f, const char *Prefix, eDumpMode DumpMode, time_t AtTime)
{
cSafeFile *sf = NULL;
if (!f) {
sf = new cSafeFile(epgDataFileName);
if (sf->Open())
f = *sf;
else {
LOG_ERROR;
delete sf;
return false;
}
}
LOCK_SCHEDULES_READ;
for (const cSchedule *p = Schedules->First(); p; p = Schedules->Next(p))
p->Dump(f, Prefix, DumpMode, AtTime);
if (sf) {
sf->Close();
delete sf;
}
return true;
}
bool cSchedules::Read(FILE *f)
{
bool OwnFile = f == NULL;
if (OwnFile) {
if (epgDataFileName && access(epgDataFileName, R_OK) == 0) {
dsyslog("reading EPG data from %s", epgDataFileName);
if ((f = fopen(epgDataFileName, "r")) == NULL) {
LOG_ERROR;
return false;
}
}
else
return false;
}
LOCK_CHANNELS_WRITE;
LOCK_SCHEDULES_WRITE;
bool result = cSchedule::Read(f, Schedules);
if (OwnFile)
fclose(f);
if (result) {
// Initialize the channels' schedule pointers, so that the first WhatsOn menu will come up faster:
for (cChannel *Channel = Channels->First(); Channel; Channel = Channels->Next(Channel))
Schedules->GetSchedule(Channel);
}
return result;
}
cSchedule *cSchedules::AddSchedule(tChannelID ChannelID)
{
ChannelID.ClrRid();
cSchedule *p = (cSchedule *)GetSchedule(ChannelID);
if (!p) {
p = new cSchedule(ChannelID);
Add(p);
}
return p;
}
const cSchedule *cSchedules::GetSchedule(tChannelID ChannelID) const
{
ChannelID.ClrRid();
for (const cSchedule *p = First(); p; p = Next(p)) {
if (p->ChannelID() == ChannelID)
return p;
}
return NULL;
}
const cSchedule *cSchedules::GetSchedule(const cChannel *Channel, bool AddIfMissing) const
{
// This is not very beautiful, but it dramatically speeds up the
// "What's on now/next?" menus.
static cSchedule DummySchedule(tChannelID::InvalidID);
if (!Channel->schedule)
Channel->schedule = GetSchedule(Channel->GetChannelID());
if (!Channel->schedule)
Channel->schedule = &DummySchedule;
if (Channel->schedule == &DummySchedule && AddIfMissing) {
cSchedule *Schedule = new cSchedule(Channel->GetChannelID());
((cSchedules *)this)->Add(Schedule);
Channel->schedule = Schedule;
}
return Channel->schedule != &DummySchedule? Channel->schedule : NULL;
}
// --- cEpgDataReader --------------------------------------------------------
cEpgDataReader::cEpgDataReader(void)
:cThread("epg data reader")
{
}
void cEpgDataReader::Action(void)
{
cSchedules::Read();
}
// --- cEpgHandler -----------------------------------------------------------
cEpgHandler::cEpgHandler(void)
{
EpgHandlers.Add(this);
}
cEpgHandler::~cEpgHandler()
{
EpgHandlers.Del(this, false);
}
// --- cEpgHandlers ----------------------------------------------------------
cEpgHandlers EpgHandlers;
bool cEpgHandlers::IgnoreChannel(const cChannel *Channel)
{
for (cEpgHandler *eh = First(); eh; eh = Next(eh)) {
if (eh->IgnoreChannel(Channel))
return true;
}
return false;
}
bool cEpgHandlers::HandleEitEvent(cSchedule *Schedule, const SI::EIT::Event *EitEvent, uchar TableID, uchar Version)
{
for (cEpgHandler *eh = First(); eh; eh = Next(eh)) {
if (eh->HandleEitEvent(Schedule, EitEvent, TableID, Version))
return true;
}
return false;
}
bool cEpgHandlers::HandledExternally(const cChannel *Channel)
{
for (cEpgHandler *eh = First(); eh; eh = Next(eh)) {
if (eh->HandledExternally(Channel))
return true;
}
return false;
}
bool cEpgHandlers::IsUpdate(tEventID EventID, time_t StartTime, uchar TableID, uchar Version)
{
for (cEpgHandler *eh = First(); eh; eh = Next(eh)) {
if (eh->IsUpdate(EventID, StartTime, TableID, Version))
return true;
}
return false;
}
void cEpgHandlers::SetEventID(cEvent *Event, tEventID EventID)
{
for (cEpgHandler *eh = First(); eh; eh = Next(eh)) {
if (eh->SetEventID(Event, EventID))
return;
}
Event->SetEventID(EventID);
}
void cEpgHandlers::SetTitle(cEvent *Event, const char *Title)
{
for (cEpgHandler *eh = First(); eh; eh = Next(eh)) {
if (eh->SetTitle(Event, Title))
return;
}
Event->SetTitle(Title);
}
void cEpgHandlers::SetShortText(cEvent *Event, const char *ShortText)
{
for (cEpgHandler *eh = First(); eh; eh = Next(eh)) {
if (eh->SetShortText(Event, ShortText))
return;
}
Event->SetShortText(ShortText);
}
void cEpgHandlers::SetDescription(cEvent *Event, const char *Description)
{
for (cEpgHandler *eh = First(); eh; eh = Next(eh)) {
if (eh->SetDescription(Event, Description))
return;
}
Event->SetDescription(Description);
}
void cEpgHandlers::SetContents(cEvent *Event, uchar *Contents)
{
for (cEpgHandler *eh = First(); eh; eh = Next(eh)) {
if (eh->SetContents(Event, Contents))
return;
}
Event->SetContents(Contents);
}
void cEpgHandlers::SetParentalRating(cEvent *Event, int ParentalRating)
{
for (cEpgHandler *eh = First(); eh; eh = Next(eh)) {
if (eh->SetParentalRating(Event, ParentalRating))
return;
}
Event->SetParentalRating(ParentalRating);
}
void cEpgHandlers::SetStartTime(cEvent *Event, time_t StartTime)
{
for (cEpgHandler *eh = First(); eh; eh = Next(eh)) {
if (eh->SetStartTime(Event, StartTime))
return;
}
Event->SetStartTime(StartTime);
}
void cEpgHandlers::SetDuration(cEvent *Event, int Duration)
{
for (cEpgHandler *eh = First(); eh; eh = Next(eh)) {
if (eh->SetDuration(Event, Duration))
return;
}
Event->SetDuration(Duration);
}
void cEpgHandlers::SetVps(cEvent *Event, time_t Vps)
{
for (cEpgHandler *eh = First(); eh; eh = Next(eh)) {
if (eh->SetVps(Event, Vps))
return;
}
Event->SetVps(Vps);
}
void cEpgHandlers::SetComponents(cEvent *Event, cComponents *Components)
{
for (cEpgHandler *eh = First(); eh; eh = Next(eh)) {
if (eh->SetComponents(Event, Components))
return;
}
Event->SetComponents(Components);
}
void cEpgHandlers::FixEpgBugs(cEvent *Event)
{
for (cEpgHandler *eh = First(); eh; eh = Next(eh)) {
if (eh->FixEpgBugs(Event))
return;
}
Event->FixEpgBugs();
}
void cEpgHandlers::HandleEvent(cEvent *Event)
{
for (cEpgHandler *eh = First(); eh; eh = Next(eh)) {
if (eh->HandleEvent(Event))
break;
}
}
void cEpgHandlers::SortSchedule(cSchedule *Schedule)
{
for (cEpgHandler *eh = First(); eh; eh = Next(eh)) {
if (eh->SortSchedule(Schedule))
return;
}
Schedule->Sort();
}
void cEpgHandlers::DropOutdated(cSchedule *Schedule, time_t SegmentStart, time_t SegmentEnd, uchar TableID, uchar Version)
{
for (cEpgHandler *eh = First(); eh; eh = Next(eh)) {
if (eh->DropOutdated(Schedule, SegmentStart, SegmentEnd, TableID, Version))
return;
}
Schedule->DropOutdated(SegmentStart, SegmentEnd, TableID, Version);
}
void cEpgHandlers::BeginSegmentTransfer(const cChannel *Channel)
{
for (cEpgHandler *eh = First(); eh; eh = Next(eh)) {
if (eh->BeginSegmentTransfer(Channel, false))
return;
}
}
void cEpgHandlers::EndSegmentTransfer(bool Modified)
{
for (cEpgHandler *eh = First(); eh; eh = Next(eh)) {
if (eh->EndSegmentTransfer(Modified, false))
return;
}
}