1 #include "picturereader.h"
2 #include <vdr/plugin.h>
3 #include <vdr/channels.h>
7 PictureReader::PictureReader(VompClient *client)
9 logger = Log::getInstance();
14 pthread_mutex_init(&pictureLock, NULL);
17 int PictureReader::init(TCP* ttcp)
25 PictureReader::~PictureReader()
30 void PictureReader::addTVMediaRequest(TVMediaRequest& req)
32 logger->log("PictRead",Log::DEBUG,"Got TVMediaRequest, signal thread!");
34 pthread_mutex_lock(&pictureLock);
36 threadSignal(); // Signal, that we have something to do!!!
37 pthread_mutex_unlock(&pictureLock);
40 std::string PictureReader::getPictName(TVMediaRequest & req)
42 logger->log("PictRead",Log::DEBUG,
43 "Request %d %d; %d %d %d %d",req.primary_id,req.secondary_id,
44 req.type,req.type_pict,
45 req.container,req.container_member);
49 if (series.seriesId != req.primary_id ||
50 series.episodeId != req.secondary_id) {
51 series.actors.clear();
52 series.posters.clear();
53 series.banners.clear();
54 series.fanarts.clear();
56 series.seriesId = req.primary_id;
57 series.episodeId = req.secondary_id;
58 x->scraper->Service("GetSeries",&series);
60 if (req.type_pict == 0) {
61 switch (req.container) {
63 return series.episode.episodeImage.path;
66 if (series.actors.size()>req.container_member) {
67 return series.actors[req.container_member].actorThumb.path;
71 if (series.posters.size()>req.container_member) {
72 return series.posters[req.container_member].path;
76 if (series.banners.size()>req.container_member) {
77 return series.banners[req.container_member].path;
81 if (series.fanarts.size()>req.container_member) {
82 return series.fanarts[req.container_member].path;
86 return series.seasonPoster.path;
89 return std::string("");
92 } else if (req.type_pict == 1 && series.posters.size()) { //poster
93 std::string str=series.posters[0].path;
94 size_t pos=str.rfind('/');
95 if (pos!=std::string::npos) {
97 str=str+"poster_thumb.jpg";
100 } else if (req.type_pict == 2) { //poster
101 std::string str=series.seasonPoster.path;
102 size_t pos=str.rfind('/');
103 if (pos!=std::string::npos) {
105 std::ostringstream out;
106 out << str << "season_" <<series.episode.season <<"_thumb.jpg";
110 return std::string("");
113 if (movie.movieId != req.primary_id ) {
114 movie.actors.clear();
115 movie.movieId = req.primary_id;
116 x->scraper->Service("GetMovie",&movie);
118 if (req.type_pict == 0) {
120 switch (req.container) {
122 return movie.poster.path;
125 return movie.fanart.path;
128 return movie.collectionPoster.path;
131 return movie.collectionFanart.path;
134 if (movie.actors.size()>req.container_member) {
135 return movie.actors[req.container_member].actorThumb.path;
139 return std::string("");
142 } else if (req.type_pict == 1) { //poster
143 std::string str=movie.poster.path;
144 size_t pos=str.rfind('/');
145 if (pos!=std::string::npos) {
147 str=str+"poster_thumb.jpg";
151 return std::string("");
155 case 3: { // I do not know
156 // First get the recording
157 cRecordings Recordings;
159 cRecording *recording = Recordings.GetByName((char*) req.primary_name.c_str());
160 ScraperGetPosterThumb getter;
161 getter.recording = recording;
163 if (x->scraper && recording) {
164 x->scraper->Service("GetPosterThumb",&getter);
165 return getter.poster.path;
167 return std::string("");
170 case 4: { // I do not know
171 // First get the schedules
172 cSchedulesLock MutexLock;
173 const cSchedules *Schedules = cSchedules::Schedules(MutexLock);
174 const cSchedule *Schedule = NULL;
177 cChannel * channel = Channels.GetByChannelID(
178 tChannelID::FromString(req.primary_name.c_str()));
179 Schedule = Schedules->GetSchedule(channel);
181 const cEvent *event = NULL;
182 if (Schedule) event=Schedule->GetEvent(req.primary_id);
183 ScraperGetPosterThumb getter;
184 getter.event = event;
185 getter.recording = NULL;
187 if (x->scraper && event) {
188 x->scraper->Service("GetPosterThumb",&getter);
189 return getter.poster.path;
191 return std::string("");
196 return std::string("");
204 void PictureReader::threadMethod()
207 ULONG headerLength = sizeof(ULONG) * 4;
208 UCHAR buffer[headerLength];
211 // threadSetKillable(); ??
213 logger->log("PictRead",Log::DEBUG,"PictureReaderThread started");
217 threadWaitForSignal();
221 logger->log("PictRead",Log::DEBUG,"Thread was signaled, wake up");
227 pthread_mutex_lock(&pictureLock);
228 if (!pictures.empty()) {
230 req = pictures.front();
233 pthread_mutex_unlock(&pictureLock);
234 if (!newpicture) break;
235 std::string pictname = getPictName(req);
239 logger->log("PictRead",Log::DEBUG,"Load Pict %s",pictname.c_str());
242 if (pictname.length()) {
246 stat(pictname.c_str(), &st);
247 filesize = st.st_size;
248 memsize = filesize + headerLength;
250 if (memsize && memsize < 1000000) { // No pictures over 1 MB
251 mem = (UCHAR*)malloc(memsize);
253 FILE * file=fopen(pictname.c_str(),"r");
256 size_t size=fread(mem+headerLength,1,filesize,file);
259 if (size!=filesize) memsize=headerLength; // error
271 p = (ULONG*)&mem[0]; *p = htonl(5); // stream channel
272 p = (ULONG*)&mem[4]; *p = htonl(req.streamID);
273 p = (ULONG*)&mem[8]; *p = htonl(flag); // here insert flag: 0 = ok, data follows
274 p = (ULONG*)&mem[12]; *p = htonl(memsize);
276 if (!tcp->sendPacket(mem, memsize + headerLength)) {
277 logger->log("PictRead",Log::DEBUG,"Sending Picture failed");
280 if (mem != buffer && mem) free(mem);
282 } while (newpicture);
284 logger->log("PictRead",Log::DEBUG,"PictureReaderThread ended");