summaryrefslogtreecommitdiff
path: root/plugins/provider/vdrProvider/vdrProvider.cpp
blob: 9f893b614b4a087f9d05746f6a0fff7a90796179 (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
 * vdrProvider.cpp
 *
 *  Created on: 01.10.2012
 *      Author: savop
 */

#include <plugin.h>
#include <server.h>
#include <vdr/epg.h>
#include <vdr/channels.h>
#include <vdr/tools.h>
#include <vdr/plugin.h>
#include <string>
#include <sstream>
#include <algorithm>
#include <tools.h>
#include <pwd.h>
#include <unistd.h>

using namespace std;

namespace upnp {

class VdrProvider : public cUPnPResourceProvider {
private:
  time_t      lastModified;
  int         from;
  int         to;

  bool Parse(const string& line)
  {
    int pos = 0;
    if((pos = line.find_first_of('-')) != string::npos){
      from = atoi(line.substr(0, pos).c_str());
      to = atoi(line.substr(pos+1).c_str());
      return true;
    }
    return false;
  }

  int GetGroupByName(const string name)
  {
    if(name.empty()) return -1;
    int Idx = -1;
    cChannel *group = Channels.Get(++Idx);
    while (group && !(group->GroupSep() && name.compare(group->Name()) == 0))
      group = Channels.Get(++Idx);
    return group ? Idx : -1;
  }

  string GetContainerName(string uri){
    return uri.substr(6,uri.size()-7);
  }

  cCondWait sleep;

public:

  VdrProvider()
  : lastModified(0)
  , from(0)
  , to(INT_MAX)
  {
    LoadConfigFile("vdrProvider.conf");
  }

  virtual ~VdrProvider(){
    sleep.Signal();
    Cancel(5);
  }

  virtual string ProvidesSchema(){ return "vdr"; }

  virtual string GetRootContainer(){
    return ProvidesSchema() + "://";
  }

  virtual StringList GetContainerEntries(const string& uri){
    if(!HasRootContainer(uri)) return StringList();

    StringList list;
    int index;
    cChannel* channel = NULL;
    if(to == 0) to = INT_MAX;
    // Check if this is the root and we have no groups in the channels.conf:
    if(uri.compare(GetRootContainer()) == 0 && Channels.GetNextGroup(0) == -1){
      for(index = Channels.GetNextNormal(from - 1); (channel = Channels.Get(index)) && index < to; index = Channels.GetNextNormal(index)){
        list.push_back(*channel->GetChannelID().ToString());
      }
    } else {
      string u = GetContainerName(uri);
      if((index = GetGroupByName(u)) != -1){
        while((channel = Channels.Get(++index)) != NULL){
          if(channel->GroupSep()){
            // We reached the next group. So, stop here.
            if(*channel->Name())
              break;
          } else {
            list.push_back(*channel->GetChannelID().ToString());
          }
        }
      } else {
        for(index = Channels.GetNextGroup(from - 1); (channel = Channels.Get(index)) && index < to; index = Channels.GetNextGroup(index)){
          string group = tools::ToUTF8String(channel->Name()) + '/';
          list.push_back(group);
        }
      }
    }

    return list;
  }

  virtual bool IsContainer(const string& uri){
    return uri.compare(GetRootContainer()) == 0 || GetGroupByName(GetContainerName(uri)) != -1;
  }

  virtual bool IsLink(const string& uri, string& target){
    // TODO: what are Channel::RefChannel or LinkChannels ?
    return false;
  }

  virtual long GetContainerUpdateId(const string& uri){
    if(!HasRootContainer(uri)) return 0;

    // We now have containers. However, they do not support containerUpdateIDs separately.
    return (long)lastModified;
  }

  virtual bool GetMetadata(const string& uri, cMetadata& metadata){
    if(!HasRootContainer(uri)) return false;

    if(!cUPnPResourceProvider::GetMetadata(uri, metadata)) return false;

    int index = 0;
    cChannel* channel;
    if(uri.compare(GetRootContainer()) == 0){
      metadata.SetProperty(cMetadata::Property(property::object::KEY_TITLE, string("VDR Live-TV")));
      metadata.SetProperty(cMetadata::Property(property::object::KEY_DESCRIPTION, string("Watch Live-TV")));
    } else if((index = GetGroupByName(GetContainerName(uri))) != -1 && (channel = Channels.Get(index)) != NULL){
      string chanName = tools::ToUTF8String(channel->Name());
      metadata.SetProperty(cMetadata::Property(property::object::KEY_TITLE, chanName));
      metadata.SetProperty(cMetadata::Property(property::object::KEY_DESCRIPTION, chanName));
    } else {
      return false;
    }

    metadata.SetProperty(cMetadata::Property("dlna:containerType", string("Tuner_1_0")));
    struct passwd *pw;
    if((pw = getpwuid(getuid())) == NULL){
      metadata.SetProperty(cMetadata::Property(property::object::KEY_CREATOR, string("Klaus Schmidinger")));
    } else {
      string name(pw->pw_gecos); name = name.substr(0,name.find(",,,",0));
      metadata.SetProperty(cMetadata::Property(property::object::KEY_CREATOR, name));
    }

    return true;
  }

  virtual string GetHTTPUri(const string& uri, const string& currentIP, const string& pInfo){
    if(!HasRootContainer(uri)) return string();

    int port = 3000;

    stringstream ss;

    string protocolInfo = pInfo.substr(pInfo.find_last_of(':'));

    std::replace(protocolInfo.begin(), protocolInfo.end(), ';','+');

    ss << "http://" << currentIP << ":" << port
       << "/"
       << "EXT;"
       << "PROG=cat;"
       << "DLNA_contentFeatures=" << protocolInfo
       << "/"
       << uri.substr(uri.find_last_of('/')+1);

    return ss.str();
  }

  virtual void Action(){

#define SLEEP_TIMEOUT 120

    const cSchedules* Schedules;
    cSchedule* Schedule;

    const cEvent* event;

    long now = 0;
    bool modified = false;
    StringList targets;

    while(Running()){

      event = NULL;
      now = time(NULL);

      { // Reduce Scope of Schedules lock.
        cSchedulesLock lock;
        Schedules = cSchedules::Schedules(lock);
        // iterate over all the schedules, find those, which were modified and tell
        // it to the media manager. If we found an entry, there's no need to continue searching.
        for(Schedule = Schedules->First(); Schedule; Schedule = Schedules->Next(Schedule))
        {
          // Get the next event of the schedule...
          event = Schedule->GetFollowingEvent();
          if(event){
            // and check if it starts after the last modification and in two minutes from now.
            // This causes an update only if there is at least one element in the schedule
            // which starts within the next two minutes. Other elements, which will start later,
            // will be skipped.
            if(event->StartTime() > lastModified && event->StartTime() < now + SLEEP_TIMEOUT){
              modified = true;
              targets.push_back(*event->ChannelID().ToString());
              break;
            }
          } else if(Schedule->Modified() > lastModified){
            targets.push_back(*event->ChannelID().ToString());
            modified = true;
            break;
          }
        }

      }

      if(modified){
        OnContainerUpdate(GetRootContainer(), now, targets);
        targets.clear();
        modified = false;
        lastModified = now;
      }

      // Sleep 2 minutes
      sleep.Wait(SLEEP_TIMEOUT * 1000);
    }

  }

};

UPNP_REGISTER_RESOURCE_PROVIDER(VdrProvider);

}  // namespace upnp