summaryrefslogtreecommitdiff
path: root/plugins/provider/recProvider/recProvider.cpp
blob: f2e0fa0635ae8c5f75a1a020a1932cec73400dd4 (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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
 * vdrProvider.cpp
 *
 *  Created on: 01.10.2012
 *      Author: savop
 */

#include <plugin.h>
#include <vdr/recording.h>
#include <vdr/channels.h>
#include <vdr/tools.h>
#include <vdr/config.h>
#include <vdr/videodir.h>
#include <string>
#include <sstream>
#include <tools.h>
#include <pwd.h>
#include <unistd.h>

using namespace std;

namespace upnp {

class RecProvider : public cUPnPResourceProvider {
private:

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

  bool GetFileStat(const string& uri, struct stat& fileStat){
    stringstream filename;
    filename << VideoDirectory << "/" << uri.substr(6);
    struct stat s;
    if(stat(filename.str().c_str(), &s) == 0){
      fileStat = s;
      return true;
    }

    return false;
  }

  FILE* fileFD;
  int currentFileNumber;
  int lastFileNumber;
  string filename;
  vector<size_t> offsets;

  void CloseFile(){
    fclose(fileFD);
    fileFD = NULL;
  }

  bool OpenFile(int i = 1){
    if(currentFileNumber != i && fileFD){
      CloseFile();
    }

    char fileBuf[1024]; strncpy(fileBuf, filename.c_str(), 1024);
    char* pFileNumber = fileBuf + strlen(fileBuf);
    sprintf(pFileNumber, "/%05d.ts", i);

    fileFD = fopen(fileBuf, "r");
    if(!fileFD) return false;
    currentFileNumber = i;

    return true;
  }

  bool OpenNext(){
    return OpenFile(currentFileNumber+1);
  }

  bool ScanFiles(){
    if(!OpenFile(1)) return false;
    do {
      fseek(fileFD, 0, SEEK_END);
      offsets[currentFileNumber-1] = ftell(fileFD);
    } while(OpenNext());
    lastFileNumber = currentFileNumber;
    return OpenFile(1);
  }

public:

  RecProvider()
  : fileFD(NULL)
  , currentFileNumber(1)
  , lastFileNumber(1)
  , offsets(65536)
  {
  }

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

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

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

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

    StringList list;
    string videoDir(VideoDirectory), fs, uri = u.substr(6);
    int pos = 0, vl = videoDir.length(), ul = uri.length(), vul = vl + ul + 1;

    for(cRecording* rec = Recordings.First(); rec; rec = Recordings.Next(rec)){
      char* file = strdup(rec->Name());
      file = ExchangeChars(file, true);
      fs = file;
      free(file);
      if(fs.find(uri) != string::npos){
        fs = fs.substr(ul);
        if((pos = fs.find_first_of('/')) != string::npos){
          fs = fs.substr(0, pos+1);
        } else {
          fs = string(rec->FileName()).substr(vul);
        }
        list.push_back(fs);
      }
    }

    return list;
  }

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

    stringstream filename;
    filename << VideoDirectory << "/" << uri.substr(6);

    if(!Recordings.GetByName(filename.str().c_str())){
      return true;
    }
    else {
      return false;
    }
  }

  virtual bool IsLink(const string& uri, string& target){
    return false;
  }

  virtual long GetContainerUpdateId(const string& uri){
    struct stat fileStat;
    if(GetFileStat(uri.substr(0,uri.find_last_of('/')+1), fileStat)){
      return std::max<time_t>(fileStat.st_ctim.tv_sec, fileStat.st_mtim.tv_sec);
    }

    return 0;
  }

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

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

    if(GetRootContainer().compare(uri) == 0){
      metadata.SetProperty(cMetadata::Property(property::object::KEY_TITLE, string("VDR Recordings")));
      metadata.SetProperty(cMetadata::Property(property::object::KEY_DESCRIPTION, string("Watch TV recordings")));

    } else {
      int ul = uri.length();
      string folder = uri.substr(uri.find_last_of('/', ul-2)+1, ul - 7);
      char * str = strdup(folder.c_str());
      str = ExchangeChars(str, false);
      metadata.SetProperty(cMetadata::Property(property::object::KEY_TITLE, string(str)));
      free(str);
    }

    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 bool Seekable() const {
    return true;
  }

  virtual bool Open(const string& uri){
    filename = string(VideoDirectory) + "/" + uri.substr(6);
    currentFileNumber = 1;
    return ScanFiles();
  }

  virtual size_t Read(char* buf, size_t bufLen){
    if(!fileFD) return -1;

    size_t bytesRead = 0;

    bytesRead += fread(buf, 1, bufLen, fileFD);
    if(bytesRead != bufLen && OpenNext()){
      bytesRead += fread(buf + bytesRead, 1, bufLen - bytesRead, fileFD);
    }

    return bytesRead;
  }

  virtual bool Seek(size_t offset, int origin){
    if(!fileFD) return false;

    off_t curpos = fseek(fileFD, 0, SEEK_CUR); // this should not change anything
    switch(origin){
    case SEEK_END:
        offset = offsets[lastFileNumber] + offset;
        break;
    case SEEK_CUR:
        offset = offsets[currentFileNumber-1] + curpos +  offset;
        break;
    case SEEK_SET:
        // Nothing to change
        break;
    default:
      return false;
    }

    if(!OpenFile(1)) return false;
    size_t size = 0;
    while(true){
      fseek(fileFD, 0, SEEK_END);
      size = ftell(fileFD);
      if(offset < size){
        return fseek(fileFD, offset, SEEK_SET) == 0;
      } else {
        offset -= size;
        if(OpenNext()) return false;
      }
    }

    return false;

  }

  virtual void Close(){
    currentFileNumber = 1;
    CloseFile();
  }

  virtual void Action(){
    while(Running()){
      int state = 0;
      if(Recordings.NeedsUpdate() || Recordings.StateChanged(state)){
        OnContainerUpdate(GetRootContainer(), GetContainerUpdateId(GetRootContainer()));
      }
      sleep(100);
    }
  }

};

UPNP_REGISTER_RESOURCE_PROVIDER(RecProvider);

}  // namespace upnp