some more fixes

This commit is contained in:
louis 2014-05-09 16:53:56 +02:00
parent 5d7fb01f67
commit 31f55fb489
12 changed files with 369 additions and 316 deletions

View File

@ -14,7 +14,9 @@ Version 0.1.0
- fixed bug that scraped events are not displayed randomly
Version 0.1.1
- ...
Version 0.1.2
- update of db api
- prepare statements only once
- added statement statistic
@ -23,5 +25,3 @@ Version 0.1.1
- added path detection for libmysql
- fixed epgd state detection
- added cppcheck to Makefile
Version 0.1.2

View File

@ -4,10 +4,11 @@
# $Id$
# External image lib to use: imagemagick, graphicsmagick
IMAGELIB = imagemagick
PLUGIN = scraper2vdr
HLIB = -L./lib -lhorchi
HLIB = -L./lib -lhorchi
### The version number of this plugin (taken from the main source file):
@ -36,7 +37,7 @@ APIVERSION = $(call PKGCFG,apiversion)
-include $(PLGCFG)
LIBS = $(HLIB) $(shell mysql_config --libs_r) -luuid
LIBS = $(HLIB) $(shell mysql_config --libs_r) -luuid -lcrypto
### The name of the distribution archive:

View File

@ -32,7 +32,7 @@ CFLAGS += -fPIC -Wreturn-type -Wall -Wno-parentheses -Wformat -pedantic -Wunused
CFLAGS += $(shell mysql_config --include)
DEFINES = -DPLGDIR='"."' -DUSEUUID -DUSEMD5
DEFINES = -DPLGDIR='"."' -DUSEUUID -DUSEMD5 -USELIBXML
all: lib $(TEST) $(DEMO)
lib: $(LIBTARGET).a

View File

@ -25,21 +25,20 @@
# include <archive_entry.h>
#endif
#ifdef VDR_PLUGIN
# include <vdr/thread.h>
#endif
#include "common.h"
#include "config.h"
#ifdef VDR_PLUGIN
cMutex logMutex;
#endif
cMyMutex logMutex;
//***************************************************************************
// Debug
//***************************************************************************
const char* getLogPrefix()
{
return logPrefix;
}
void tell(int eloquence, const char* format, ...)
{
if (EPG2VDRConfig.loglevel < eloquence)
@ -49,15 +48,12 @@ void tell(int eloquence, const char* format, ...)
char t[sizeBuffer+100]; *t = 0;
va_list ap;
#ifdef VDR_PLUGIN
cMutexLock lock(&logMutex);
#endif
logMutex.Lock();
va_start(ap, format);
#ifdef VDR_PLUGIN
snprintf(t, sizeBuffer, LOG_PREFIX);
#endif
if (getLogPrefix())
snprintf(t, sizeBuffer, "%s", getLogPrefix());
vsnprintf(t+strlen(t), sizeBuffer-strlen(t), format, ap);
@ -74,12 +70,13 @@ void tell(int eloquence, const char* format, ...)
tm->tm_hour, tm->tm_min, tm->tm_sec,
tp.tv_usec / 1000);
printf("%s %s\n", buf, t);
printf("%s %s\n", buf, t);
}
else
syslog(LOG_ERR, "%s", t);
logMutex.Unlock();
va_end(ap);
}
@ -959,12 +956,103 @@ int unzip(const char* file, const char* filter, char*& buffer, int& size, char*
#endif
//***************************************************************************
// Class LogDuration
// cMyMutex
//***************************************************************************
#ifdef VDR_PLUGIN
cMyMutex::cMyMutex (void)
{
locked = 0;
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK_NP);
pthread_mutex_init(&mutex, &attr);
}
# include <vdr/plugin.h>
cMyMutex::~cMyMutex()
{
pthread_mutex_destroy(&mutex);
}
void cMyMutex::Lock(void)
{
pthread_mutex_lock(&mutex);
locked++;
}
void cMyMutex::Unlock(void)
{
if (!--locked)
pthread_mutex_unlock(&mutex);
}
//***************************************************************************
// cMyTimeMs
//***************************************************************************
cMyTimeMs::cMyTimeMs(int Ms)
{
if (Ms >= 0)
Set(Ms);
else
begin = 0;
}
uint64_t cMyTimeMs::Now(void)
{
#define MIN_RESOLUTION 5 // ms
static bool initialized = false;
static bool monotonic = false;
struct timespec tp;
if (!initialized) {
// check if monotonic timer is available and provides enough accurate resolution:
if (clock_getres(CLOCK_MONOTONIC, &tp) == 0) {
// long Resolution = tp.tv_nsec;
// require a minimum resolution:
if (tp.tv_sec == 0 && tp.tv_nsec <= MIN_RESOLUTION * 1000000) {
if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0) {
monotonic = true;
}
else
tell(0, "cMyTimeMs: clock_gettime(CLOCK_MONOTONIC) failed");
}
else
tell(0, "cMyTimeMs: not using monotonic clock - resolution is too bad (%ld s %ld ns)", tp.tv_sec, tp.tv_nsec);
}
else
tell(0, "cMyTimeMs: clock_getres(CLOCK_MONOTONIC) failed");
initialized = true;
}
if (monotonic) {
if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0)
return (uint64_t(tp.tv_sec)) * 1000 + tp.tv_nsec / 1000000;
tell(0, "cMyTimeMs: clock_gettime(CLOCK_MONOTONIC) failed");
monotonic = false;
// fall back to gettimeofday()
}
struct timeval t;
if (gettimeofday(&t, NULL) == 0)
return (uint64_t(t.tv_sec)) * 1000 + t.tv_usec / 1000;
return 0;
}
void cMyTimeMs::Set(int Ms)
{
begin = Now() + Ms;
}
bool cMyTimeMs::TimedOut(void)
{
return Now() >= begin;
}
uint64_t cMyTimeMs::Elapsed(void)
{
return Now() - begin;
}
//***************************************************************************
// Class LogDuration
//***************************************************************************
LogDuration::LogDuration(const char* aMessage, int aLogLevel)
{
@ -973,23 +1061,21 @@ LogDuration::LogDuration(const char* aMessage, int aLogLevel)
// at last !
durationStart = cTimeMs::Now();
durationStart = cMyTimeMs::Now();
}
LogDuration::~LogDuration()
{
tell(logLevel, "duration '%s' was (%ldms)",
message, cTimeMs::Now() - durationStart);
message, cMyTimeMs::Now() - durationStart);
}
void LogDuration::show(const char* label)
{
tell(logLevel, "elapsed '%s' at '%s' was (%ldms)",
message, label, cTimeMs::Now() - durationStart);
message, label, cMyTimeMs::Now() - durationStart);
}
#endif
//***************************************************************************
// Get Unique ID
//***************************************************************************

View File

@ -15,9 +15,10 @@
#include <openssl/md5.h> // MD5_*
#ifdef VDR_PLUGIN
# include <vdr/tools.h>
#endif
//#ifdef VDR_PLUGIN
//# include <vdr/tools.h>
//# include <vdr/thread.h>
//#endif
#ifdef USELIBXML
# include <libxslt/transform.h>
@ -29,10 +30,10 @@
// Misc
//***************************************************************************
#ifndef VDR_PLUGIN
inline long min(long a, long b) { return a < b ? a : b; }
inline long max(long a, long b) { return a > b ? a : b; }
#endif
// #ifndef VDR_PLUGIN
inline long min(long a, long b) { return a < b ? a : b; }
inline long max(long a, long b) { return a > b ? a : b; }
// #endif
enum Misc
{
@ -69,6 +70,9 @@ const char* toCase(Case cs, char* str);
// Tell
//***************************************************************************
extern const char* logPrefix;
const char* getLogPrefix();
void __attribute__ ((format(printf, 2, 3))) tell(int eloquence, const char* format, ...);
//***************************************************************************
@ -220,7 +224,41 @@ int unzip(const char* file, const char* filter, char*& buffer,
int& size, char* entryName);
#endif
#ifdef VDR_PLUGIN
//***************************************************************************
// cMyMutex
//***************************************************************************
class cMyMutex
{
friend class cCondVar;
private:
pthread_mutex_t mutex;
int locked;
public:
cMyMutex(void);
~cMyMutex();
void Lock(void);
void Unlock(void);
};
//***************************************************************************
// cMyTimeMs
//***************************************************************************
class cMyTimeMs
{
private:
uint64_t begin;
public:
cMyTimeMs(int Ms = 0);
static uint64_t Now(void);
void Set(int Ms = 0);
bool TimedOut(void);
uint64_t Elapsed(void);
};
//***************************************************************************
// Log Duration
@ -241,7 +279,6 @@ class LogDuration
uint64_t durationStart;
int logLevel;
};
#endif
//***************************************************************************
#endif //___COMMON_H

View File

@ -39,10 +39,13 @@ cEPG2VDRConfig::cEPG2VDRConfig(void)
seriesPort = 2006;
storeSeriesToFs = no;
#ifdef VDR_PLUGIN
// for VDR_PLUGIN
activeOnEpgd = no;
scheduleBoot = no;
#else
// for epgd
sstrcpy(cachePath, "/var/cache/epgd", sizeof(cachePath));
sstrcpy(httpPath, "/var/epgd/www", sizeof(httpPath));
sstrcpy(pluginPath, PLGDIR, sizeof(pluginPath));
@ -51,7 +54,8 @@ cEPG2VDRConfig::cEPG2VDRConfig(void)
updateThreshold = 200;
maintanance = no;
httpPort = 9999;
#endif
//
sstrcpy(dbHost, "localhost", sizeof(dbHost));
dbPort = 3306;

View File

@ -42,10 +42,13 @@ struct cEPG2VDRConfig
int seriesPort;
int storeSeriesToFs;
#ifdef VDR_PLUGIN
// for VDR_PLUGIN
int activeOnEpgd;
int scheduleBoot;
#else
// for epgd
char cachePath[256+TB];
char httpPath[256+TB];
char pluginPath[256+TB];
@ -54,7 +57,8 @@ struct cEPG2VDRConfig
int updateThreshold;
int maintanance;
int httpPort;
#endif
//
char dbHost[100+TB];
int dbPort;

View File

@ -7,6 +7,7 @@
#include "tabledef.h"
cDbConnection* connection = 0;
const char* logPrefix = "demo";
//***************************************************************************
// Init Connection

View File

@ -1,190 +1,190 @@
/*
* imgtools.c
*
* See the README file for copyright information and how to reach the author.
*
*/
#include "imgtools.h"
//***************************************************************************
// Image converting stuff
//***************************************************************************
int fromJpeg(Imlib_Image& image, unsigned char* buffer, int size)
{
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
int w, h;
DATA8 *ptr, *line[16], *data;
DATA32 *ptr2, *dest;
int x, y;
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_decompress(&cinfo);
jpeg_mem_src(&cinfo, buffer, size);
jpeg_read_header(&cinfo, TRUE);
cinfo.do_fancy_upsampling = FALSE;
cinfo.do_block_smoothing = FALSE;
jpeg_start_decompress(&cinfo);
w = cinfo.output_width;
h = cinfo.output_height;
image = imlib_create_image(w, h);
imlib_context_set_image(image);
dest = ptr2 = imlib_image_get_data();
data = (DATA8*)malloc(w * 16 * cinfo.output_components);
for (int i = 0; i < cinfo.rec_outbuf_height; i++)
line[i] = data + (i * w * cinfo.output_components);
for (int l = 0; l < h; l += cinfo.rec_outbuf_height)
{
jpeg_read_scanlines(&cinfo, line, cinfo.rec_outbuf_height);
int scans = cinfo.rec_outbuf_height;
if (h - l < scans)
scans = h - l;
ptr = data;
for (y = 0; y < scans; y++)
{
for (x = 0; x < w; x++)
{
*ptr2 = (0xff000000) | ((ptr[0]) << 16) | ((ptr[1]) << 8) | (ptr[2]);
ptr += cinfo.output_components;
ptr2++;
}
}
}
free(data);
imlib_image_put_back_data(dest);
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
return success;
}
long toJpeg(Imlib_Image image, MemoryStruct* data, int quality)
{
struct jpeg_compress_struct cinfo = { 0 };
struct jpeg_error_mgr jerr;
DATA32* ptr;
DATA8* buf;
long unsigned int size = data->size;
imlib_context_set_image(image);
data->clear();
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);
jpeg_mem_dest(&cinfo, (unsigned char**)(&data->memory), &size);
cinfo.image_width = imlib_image_get_width();
cinfo.image_height = imlib_image_get_height();
cinfo.input_components = 3;
cinfo.in_color_space = JCS_RGB;
jpeg_set_defaults(&cinfo);
jpeg_set_quality(&cinfo, quality, TRUE);
jpeg_start_compress(&cinfo, TRUE);
// get data pointer
if (!(ptr = imlib_image_get_data_for_reading_only()))
return 0;
// allocate a small buffer to convert image data */
buf = (DATA8*)malloc(imlib_image_get_width() * 3 * sizeof(DATA8));
while (cinfo.next_scanline < cinfo.image_height)
{
// convert scanline from ARGB to RGB packed
for (int j = 0, i = 0; i < imlib_image_get_width(); i++)
{
buf[j++] = ((*ptr) >> 16) & 0xff;
buf[j++] = ((*ptr) >> 8) & 0xff;
buf[j++] = ((*ptr)) & 0xff;
ptr++;
}
// write scanline
jpeg_write_scanlines(&cinfo, (JSAMPROW*)(&buf), 1);
}
free(buf);
jpeg_finish_compress(&cinfo);
jpeg_destroy_compress(&cinfo);
return data->size;
}
int scaleImageToJpegBuffer(Imlib_Image image, MemoryStruct* data, int width, int height)
{
if (width && height)
{
Imlib_Image scaledImage;
imlib_context_set_image(image);
int imgWidth = imlib_image_get_width();
int imgHeight = imlib_image_get_height();
double ratio = (double)imgWidth / (double)imgHeight;
if ((double)width/(double)imgWidth < (double)height/(double)imgHeight)
height = (int)((double)width / ratio);
else
width = (int)((double)height * ratio);
scaledImage = imlib_create_image(width, height);
imlib_context_set_image(scaledImage);
imlib_context_set_color(240, 240, 240, 255);
imlib_image_fill_rectangle(0, 0, width, height);
imlib_blend_image_onto_image(image, 0, 0, 0,
imgWidth, imgHeight, 0, 0,
width, height);
toJpeg(scaledImage, data, 70);
imlib_context_set_image(scaledImage);
imlib_free_image();
tell(1, "Scaled image to %d/%d, now %d bytes", width, height, (int)data->size);
}
else
{
toJpeg(image, data, 70);
}
return success;
}
int scaleJpegBuffer(MemoryStruct* data, int width, int height)
{
Imlib_Image image;
fromJpeg(image, (unsigned char*)data->memory, data->size);
scaleImageToJpegBuffer(image, data, width, height);
imlib_context_set_image(image);
imlib_free_image();
return success;
}
/*
* imgtools.c
*
* See the README file for copyright information and how to reach the author.
*
*/
#include "imgtools.h"
//***************************************************************************
// Image converting stuff
//***************************************************************************
int fromJpeg(Imlib_Image& image, unsigned char* buffer, int size)
{
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
int w, h;
DATA8 *ptr, *line[16], *data;
DATA32 *ptr2, *dest;
int x, y;
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_decompress(&cinfo);
jpeg_mem_src(&cinfo, buffer, size);
jpeg_read_header(&cinfo, TRUE);
cinfo.do_fancy_upsampling = FALSE;
cinfo.do_block_smoothing = FALSE;
jpeg_start_decompress(&cinfo);
w = cinfo.output_width;
h = cinfo.output_height;
image = imlib_create_image(w, h);
imlib_context_set_image(image);
dest = ptr2 = imlib_image_get_data();
data = (DATA8*)malloc(w * 16 * cinfo.output_components);
for (int i = 0; i < cinfo.rec_outbuf_height; i++)
line[i] = data + (i * w * cinfo.output_components);
for (int l = 0; l < h; l += cinfo.rec_outbuf_height)
{
jpeg_read_scanlines(&cinfo, line, cinfo.rec_outbuf_height);
int scans = cinfo.rec_outbuf_height;
if (h - l < scans)
scans = h - l;
ptr = data;
for (y = 0; y < scans; y++)
{
for (x = 0; x < w; x++)
{
*ptr2 = (0xff000000) | ((ptr[0]) << 16) | ((ptr[1]) << 8) | (ptr[2]);
ptr += cinfo.output_components;
ptr2++;
}
}
}
free(data);
imlib_image_put_back_data(dest);
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
return success;
}
long toJpeg(Imlib_Image image, MemoryStruct* data, int quality)
{
struct jpeg_compress_struct cinfo = { 0 };
struct jpeg_error_mgr jerr;
DATA32* ptr;
DATA8* buf;
long unsigned int size = data->size;
imlib_context_set_image(image);
data->clear();
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);
jpeg_mem_dest(&cinfo, (unsigned char**)(&data->memory), &size);
cinfo.image_width = imlib_image_get_width();
cinfo.image_height = imlib_image_get_height();
cinfo.input_components = 3;
cinfo.in_color_space = JCS_RGB;
jpeg_set_defaults(&cinfo);
jpeg_set_quality(&cinfo, quality, TRUE);
jpeg_start_compress(&cinfo, TRUE);
// get data pointer
if (!(ptr = imlib_image_get_data_for_reading_only()))
return 0;
// allocate a small buffer to convert image data */
buf = (DATA8*)malloc(imlib_image_get_width() * 3 * sizeof(DATA8));
while (cinfo.next_scanline < cinfo.image_height)
{
// convert scanline from ARGB to RGB packed
for (int j = 0, i = 0; i < imlib_image_get_width(); i++)
{
buf[j++] = ((*ptr) >> 16) & 0xff;
buf[j++] = ((*ptr) >> 8) & 0xff;
buf[j++] = ((*ptr)) & 0xff;
ptr++;
}
// write scanline
jpeg_write_scanlines(&cinfo, (JSAMPROW*)(&buf), 1);
}
free(buf);
jpeg_finish_compress(&cinfo);
jpeg_destroy_compress(&cinfo);
return data->size;
}
int scaleImageToJpegBuffer(Imlib_Image image, MemoryStruct* data, int width, int height)
{
if (width && height)
{
Imlib_Image scaledImage;
imlib_context_set_image(image);
int imgWidth = imlib_image_get_width();
int imgHeight = imlib_image_get_height();
double ratio = (double)imgWidth / (double)imgHeight;
if ((double)width/(double)imgWidth < (double)height/(double)imgHeight)
height = (int)((double)width / ratio);
else
width = (int)((double)height * ratio);
scaledImage = imlib_create_image(width, height);
imlib_context_set_image(scaledImage);
imlib_context_set_color(240, 240, 240, 255);
imlib_image_fill_rectangle(0, 0, width, height);
imlib_blend_image_onto_image(image, 0, 0, 0,
imgWidth, imgHeight, 0, 0,
width, height);
toJpeg(scaledImage, data, 70);
imlib_context_set_image(scaledImage);
imlib_free_image();
tell(1, "Scaled image to %d/%d, now %d bytes", width, height, (int)data->size);
}
else
{
toJpeg(image, data, 70);
}
return success;
}
int scaleJpegBuffer(MemoryStruct* data, int width, int height)
{
Imlib_Image image;
fromJpeg(image, (unsigned char*)data->memory, data->size);
scaleImageToJpegBuffer(image, data, width, height);
imlib_context_set_image(image);
imlib_free_image();
return success;
}

View File

@ -13,25 +13,7 @@
#include "dbdict.h"
cDbConnection* connection = 0;
//***************************************************************************
//
//***************************************************************************
class cTimeMs
{
private:
uint64_t begin;
public:
cTimeMs(int Ms = 0);
static uint64_t Now(void);
void Set(int Ms = 0);
bool TimedOut(void);
uint64_t Elapsed(void);
};
const char* logPrefix = "test";
//***************************************************************************
// Init Connection
@ -273,69 +255,6 @@ void chkStatement3()
delete mapDb;
}
// --- cTimeMs ---------------------------------------------------------------
cTimeMs::cTimeMs(int Ms)
{
if (Ms >= 0)
Set(Ms);
else
begin = 0;
}
uint64_t cTimeMs::Now(void)
{
#define MIN_RESOLUTION 5 // ms
static bool initialized = false;
static bool monotonic = false;
struct timespec tp;
if (!initialized) {
// check if monotonic timer is available and provides enough accurate resolution:
if (clock_getres(CLOCK_MONOTONIC, &tp) == 0) {
// long Resolution = tp.tv_nsec;
// require a minimum resolution:
if (tp.tv_sec == 0 && tp.tv_nsec <= MIN_RESOLUTION * 1000000) {
if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0) {
monotonic = true;
}
else
tell(0, "cTimeMs: clock_gettime(CLOCK_MONOTONIC) failed");
}
else
tell(0, "cTimeMs: not using monotonic clock - resolution is too bad (%ld s %ld ns)", tp.tv_sec, tp.tv_nsec);
}
else
tell(0, "cTimeMs: clock_getres(CLOCK_MONOTONIC) failed");
initialized = true;
}
if (monotonic) {
if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0)
return (uint64_t(tp.tv_sec)) * 1000 + tp.tv_nsec / 1000000;
tell(0, "cTimeMs: clock_gettime(CLOCK_MONOTONIC) failed");
monotonic = false;
// fall back to gettimeofday()
}
struct timeval t;
if (gettimeofday(&t, NULL) == 0)
return (uint64_t(t.tv_sec)) * 1000 + t.tv_usec / 1000;
return 0;
}
void cTimeMs::Set(int Ms)
{
begin = Now() + Ms;
}
bool cTimeMs::TimedOut(void)
{
return Now() >= begin;
}
uint64_t cTimeMs::Elapsed(void)
{
return Now() - begin;
}
//***************************************************************************
//
//***************************************************************************

View File

@ -7,6 +7,7 @@
*/
#include "scraper2vdr.h"
const char* logPrefix = LOG_PREFIX;
#if defined (APIVERSNUM) && (APIVERSNUM < 10600)
# error VDR API versions < 1.6.0 are not supported !

View File

@ -354,7 +354,7 @@ void cScrapManager::DumpMovies(void) {
}
void cScrapManager::DumpRecordings(void) {
tell(0, "%d recordings in memory:", (int)recordings.size());
tell(0, "%ld recordings in memory:", recordings.size());
for (map<sRecordingsKey, sEventsValue>::iterator it = recordings.begin(); it != recordings.end(); it++) {
sRecordingsKey key = it->first;
sEventsValue val = it->second;