This repository has been archived by the owner on Sep 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
picturereader.c
397 lines (351 loc) · 10.5 KB
/
picturereader.c
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#include "picturereader.h"
#include <vdr/plugin.h>
#include <vdr/channels.h>
#include <sstream>
#include <algorithm>
PictureReader::PictureReader(VompClient *client)
{
logger = Log::getInstance();
inittedOK = 0;
tcp = NULL;
x = client;
pthread_mutex_init(&pictureLock, NULL);
}
int PictureReader::init(TCP* ttcp)
{
tcp = ttcp;
threadStart();
return inittedOK;
}
PictureReader::~PictureReader()
{
threadStop();
}
void PictureReader::addTVMediaRequest(TVMediaRequest& req)
{
logger->log("PictRead",Log::DEBUG,"Got TVMediaRequest, signal thread!");
pthread_mutex_lock(&pictureLock);
pictures.push(req);
threadSignal(); // Signal, that we have something to do!!!
pthread_mutex_unlock(&pictureLock);
}
bool PictureReader::epgImageExists(int event)
{
if (x->imageDir) {
std::ostringstream file;
file<< std::string(x->imageDir)<< event << std::string(".jpg");
if (!access(file.str().c_str() ,F_OK))
{
return true;
}
std::ostringstream file2;
file2 << std::string(x->imageDir) << event << std::string("_0.jpg");
if (!access(file2.str().c_str() ,F_OK))
{
return true;
}
} else if (x->cacheDir) {
std::ostringstream file;
file<<std::string(x->cacheDir)<<std::string("/../epgimages/")
<< event << std::string(".jpg");
if (!access(file.str().c_str() ,F_OK))
{
return true;
}
std::ostringstream file2;
file2<<std::string(x->cacheDir)<<std::string("/../epgimages/")<<
event <<std::string("_0.jpg");
if (!access(file2.str().c_str() ,F_OK))
{
return true;
}
}
return false;
}
std::string PictureReader::getPictName(TVMediaRequest & req)
{
logger->log("PictRead",Log::DEBUG,
"Request %d %d; %d %d %d %d",req.primary_id,req.secondary_id,
req.type,req.type_pict,
req.container,req.container_member);
switch (req.type) {
case 0: { //serie
if (series.seriesId != (int)req.primary_id ||
series.episodeId != (int)req.secondary_id) {
series.actors.clear();
series.posters.clear();
series.banners.clear();
series.fanarts.clear();
series.seriesId = req.primary_id;
series.episodeId = req.secondary_id;
x->scraper->Service("GetSeries",&series);
}
if (req.type_pict == 0) {
switch (req.container) {
case 0: {
return series.episode.episodeImage.path;
} break;
case 1: {
if (series.actors.size()>req.container_member) {
return series.actors[req.container_member].actorThumb.path;
}
} break;
case 2: {
if (series.posters.size()>req.container_member) {
return series.posters[req.container_member].path;
}
} break;
case 3: {
if (series.banners.size()>req.container_member) {
return series.banners[req.container_member].path;
}
} break;
case 4: {
if (series.fanarts.size()>req.container_member) {
return series.fanarts[req.container_member].path;
}
} break;
case 5: {
return series.seasonPoster.path;
} break;
default: {
return std::string("");
} break;
};
} else if (req.type_pict == 1 && series.posters.size()) { //poster
std::string str=series.posters[0].path;
size_t pos=str.rfind('/');
if (pos!=std::string::npos) {
str.resize(pos);
str=str+"poster_thumb.jpg";
return str;
}
} else if (req.type_pict == 2) { //poster
std::string str=series.seasonPoster.path;
size_t pos=str.rfind('/');
if (pos!=std::string::npos) {
str.resize(pos);
std::ostringstream out;
out << str << "season_" <<series.episode.season <<"_thumb.jpg";
return out.str();
}
}
return std::string("");
} break;
case 1: { //movie
if (movie.movieId != (int)req.primary_id ) {
movie.actors.clear();
movie.movieId = req.primary_id;
x->scraper->Service("GetMovie",&movie);
}
if (req.type_pict == 0) {
switch (req.container) {
case 0: {
return movie.poster.path;
} break;
case 1: {
return movie.fanart.path;
} break;
case 2: {
return movie.collectionPoster.path;
} break;
case 3: {
return movie.collectionFanart.path;
} break;
case 4: {
if (movie.actors.size()>req.container_member) {
return movie.actors[req.container_member].actorThumb.path;
}
} break;
default: {
return std::string("");
} break;
};
} else if (req.type_pict == 1) { //poster
std::string str=movie.poster.path;
size_t pos=str.rfind('/');
if (pos!=std::string::npos) {
str.resize(pos);
str=str+"poster_thumb.jpg";
return str;
}
}
return std::string("");
} break;
case 3: { // I do not know
// First get the recording
#if VDRVERSNUM >= 20301
LOCK_RECORDINGS_READ;
const cRecordings* tRecordings = Recordings;
#else
cThreadLock RecordingsLock(&Recordings);
cRecordings* tRecordings = &Recordings;
#endif
const cRecording *recording = tRecordings->GetByName((char*) req.primary_name.c_str());
ScraperGetPosterThumb getter;
getter.recording = recording;
getter.event = NULL;
if (x->scraper && recording) {
x->scraper->Service("GetPosterThumb",&getter);
return getter.poster.path;
} else {
return std::string("");
}
}; break;
case 4: { // I do not know
// First get the schedules
#if VDRVERSNUM >= 20301
LOCK_CHANNELS_READ;
const cChannels* tChannels = Channels;
#else
cChannels* tChannels = &Channels;
#endif
#if VDRVERSNUM < 10300
cMutexLock MutexLock;
const cSchedules *tSchedules = cSIProcessor::Schedules(MutexLock);
#elif VDRVERSNUM < 20301
cSchedulesLock MutexLock;
const cSchedules *tSchedules = cSchedules::Schedules(MutexLock);
#else
LOCK_SCHEDULES_READ;
const cSchedules *tSchedules = Schedules;
#endif
const cSchedule *Schedule = NULL;
if (tSchedules)
{
const cChannel* channel = tChannels->GetByChannelID(tChannelID::FromString(req.primary_name.c_str()));
Schedule = tSchedules->GetSchedule(channel);
}
const cEvent *event = NULL;
if (Schedule) event=Schedule->GetEvent(req.primary_id);
ScraperGetPosterThumb getter;
getter.event = event;
getter.recording = NULL;
if (x->scraper && event) {
x->scraper->Service("GetPosterThumb",&getter);
if (getter.poster.width) return getter.poster.path;
}
if (x->imageDir) {
std::ostringstream file;
file<< std::string(x->imageDir)<< req.primary_id << std::string(".jpg");
if (!access(file.str().c_str() ,F_OK))
{
return file.str();
}
std::ostringstream file2;
file2 << std::string(x->imageDir) << req.primary_id << std::string("_0.jpg");
if (!access(file2.str().c_str() ,F_OK))
{
return file2.str();
}
} else if (x->cacheDir) {
std::ostringstream file;
file<<std::string(x->cacheDir)<<std::string("/../epgimages/")
<<req.primary_id << std::string(".jpg");
if (!access(file.str().c_str() ,F_OK))
{
return file.str();
}
std::ostringstream file2;
file2<<std::string(x->cacheDir)<<std::string("/../epgimages/")<<
req.primary_id<<std::string("_0.jpg");
if (!access(file2.str().c_str() ,F_OK))
{
return file2.str();
}
}
return std::string("");
}; break;
case 5: { // Channel logo
std::transform(req.primary_name.begin(),req.primary_name.end(),
req.primary_name.begin(),::tolower);
if (x->logoDir) {
std::string file=std::string(x->logoDir)+req.primary_name+std::string(".png");
if (!access(file.c_str() ,F_OK))
{
return file;
}
}
// if noopacity is there steal the logos
if (x->resourceDir) {
std::string file=std::string(x->resourceDir)
+std::string("/skinnopacity/logos/")+req.primary_name+std::string(".png");
if (!access(file.c_str() ,F_OK))
{
return file;
}
}
return std::string("");
}; break;
default:
return std::string("");
break;
};
return std::string("");
}
void PictureReader::threadMethod()
{
ULONG *p;
ULONG headerLength = sizeof(ULONG) * 4;
UCHAR buffer[headerLength];
// threadSetKillable(); ??
logger->log("PictRead",Log::DEBUG,"PictureReaderThread started");
while(1)
{
threadLock();
threadWaitForSignal();
threadUnlock();
threadCheckExit();
bool newpicture;
logger->log("PictRead",Log::DEBUG,"Thread was signaled, wake up");
do
{
newpicture = false;
TVMediaRequest req;
pthread_mutex_lock(&pictureLock);
if (!pictures.empty()) {
newpicture = true;
req = pictures.front();
pictures.pop();
}
pthread_mutex_unlock(&pictureLock);
if (!newpicture) break;
std::string pictname = getPictName(req);
UCHAR * mem = NULL;
ULONG memsize = 0;
ULONG flag = 2;
logger->log("PictRead",Log::DEBUG,"Load Pict %s",pictname.c_str());
if (pictname.length()) {
struct stat st;
ULONG filesize = 0 ;
stat(pictname.c_str(), &st);
filesize = st.st_size;
memsize = filesize + headerLength;
if (memsize && memsize < 1000000) { // No pictures over 1 MB
mem = (UCHAR*)malloc(memsize);
if (mem) {
FILE * file=fopen(pictname.c_str(),"r");
if (file) {
size_t size=fread(mem+headerLength,1,filesize,file);
fclose(file);
if (size!=filesize) memsize=headerLength; // error
else flag = 0;
}
}
}
}
if (!mem) {
mem = buffer;
}
p = (ULONG*)&mem[0]; *p = htonl(5); // stream channel
p = (ULONG*)&mem[4]; *p = htonl(req.streamID);
p = (ULONG*)&mem[8]; *p = htonl(flag); // here insert flag: 0 = ok, data follows
p = (ULONG*)&mem[12]; *p = htonl(memsize);
if (!tcp->sendPacket(mem, memsize + headerLength)) {
logger->log("PictRead",Log::DEBUG,"Sending Picture failed");
}
if (mem != buffer && mem) free(mem);
} while (newpicture);
}
logger->log("PictRead",Log::DEBUG,"PictureReaderThread ended");
}