summaryrefslogtreecommitdiff
path: root/plugins/provider/vdrProvider/vdrProvider.cpp
blob: 9f14aa3a22f18bcbcc69cac4b453ed7430236684 (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
/*
 * vdrProvider.cpp
 *
 *  Created on: 01.10.2012
 *      Author: savop
 */

#include <plugin.h>
#include <vdr/epg.h>
#include <vdr/channels.h>
#include <vdr/tools.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;

  bool IsRootContainer(const string& uri){
    if(uri.find(GetRootContainer(), 0) != 0){
      isyslog("VdrProvider\tUri does not contain the root.");
      return false;
    } else {
      return true;
    }
  }

public:

  VdrProvider()
  : lastModified(0)
  {}

  virtual ~VdrProvider(){
    Cancel(2);
  }

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

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

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

    StringList list;

    // Check if this is the root:
    if(uri.compare(GetRootContainer()) == 0){
      cChannel* channel = NULL;
      for(int index = 0; (channel = Channels.Get(index)); index = Channels.GetNextNormal(index)){
        if(!channel->GroupSep()){
          list.push_back(*channel->GetChannelID().ToString());
        }
      }
    }

    return list;
  }

  virtual bool IsContainer(const string& uri){
    return uri.compare(GetRootContainer()) == 0;
  }

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

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

    // We have no containers. So just return the last modification date.
    // Containers like groups are about to come soon.
    return (long)lastModified;
  }

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

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

    metadata.SetProperty(cMetadata::Property(property::object::KEY_TITLE, string("VDR Live-TV")));
    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));
    }

    metadata.SetProperty(cMetadata::Property(property::object::KEY_DESCRIPTION, string("Watch Live-TV")));

    return true;
  }

  virtual string GetHTTPUri(const string& uri, const string& currentIP, const string& pInfo){
    if(!IsRootContainer(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(6);

    return ss.str();
  }

  virtual void Action(){

    const cSchedules* Schedules;
    long now = 0;
    bool modified = false;
    while(Running()){
      now = time(NULL);

      if(!Channels.BeingEdited() && Channels.Modified() > 0){
        OnContainerUpdate(GetRootContainer(), now);
        modified = true;
      }

      { // 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
        for(cSchedule* Schedule = Schedules->First(); Schedule; Schedule = Schedules->Next(Schedule))
        {
          if(Schedule->Modified() > lastModified && Schedule->PresentSeenWithin(30)){
            OnContainerUpdate(GetRootContainer(), now, *Schedule->ChannelID().ToString());
            modified = true;
          }
        }
      }

      if(modified){
        modified = false;
        lastModified = now;
      }

      sleep(2);
    }

  }

};

UPNP_REGISTER_RESOURCE_PROVIDER(VdrProvider);

}  // namespace upnp