diff options
author | louis <louis.braun@gmx.de> | 2014-05-09 16:53:56 +0200 |
---|---|---|
committer | louis <louis.braun@gmx.de> | 2014-05-09 16:53:56 +0200 |
commit | 31f55fb489a65043849bdf48c7838773c05c71c8 (patch) | |
tree | 9830df2ed329397feebc3cf1be5120ac814e70f7 /lib | |
parent | 5d7fb01f67025dba5fc9262862dce0d978bcb7ed (diff) | |
download | vdr-plugin-scraper2vdr-31f55fb489a65043849bdf48c7838773c05c71c8.tar.gz vdr-plugin-scraper2vdr-31f55fb489a65043849bdf48c7838773c05c71c8.tar.bz2 |
some more fixes
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Makefile | 2 | ||||
-rw-r--r-- | lib/common.c | 132 | ||||
-rw-r--r-- | lib/common.h | 55 | ||||
-rw-r--r-- | lib/config.c | 10 | ||||
-rw-r--r-- | lib/config.h | 10 | ||||
-rw-r--r-- | lib/demo.c | 1 | ||||
-rw-r--r-- | lib/imgtools.c | 380 | ||||
-rw-r--r-- | lib/test.c | 83 |
8 files changed, 362 insertions, 311 deletions
diff --git a/lib/Makefile b/lib/Makefile index a50f78e..26cca7b 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -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 diff --git a/lib/common.c b/lib/common.c index b5130a7..e8631ae 100644 --- a/lib/common.c +++ b/lib/common.c @@ -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 +//*************************************************************************** + +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); +} + +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 //*************************************************************************** -#ifdef VDR_PLUGIN +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; +} -# include <vdr/plugin.h> +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 //*************************************************************************** diff --git a/lib/common.h b/lib/common.h index 1829471..5aa4819 100644 --- a/lib/common.h +++ b/lib/common.h @@ -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 diff --git a/lib/config.c b/lib/config.c index d53a26a..6bb6d3d 100644 --- a/lib/config.c +++ b/lib/config.c @@ -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; diff --git a/lib/config.h b/lib/config.h index 026d9f7..4389c58 100644 --- a/lib/config.h +++ b/lib/config.h @@ -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; @@ -7,6 +7,7 @@ #include "tabledef.h" cDbConnection* connection = 0; +const char* logPrefix = "demo"; //*************************************************************************** // Init Connection diff --git a/lib/imgtools.c b/lib/imgtools.c index 168bac9..d8d5d53 100644 --- a/lib/imgtools.c +++ b/lib/imgtools.c @@ -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; +} @@ -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; -} - //*************************************************************************** // //*************************************************************************** |