]> git.vomp.tv Git - vompclient.git/blob - src/imageloader.h
Switch to cmake
[vompclient.git] / src / imageloader.h
1 /*
2     Copyright 2021 Chris Tallon
3
4     This file is part of VOMP.
5
6     VOMP is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     VOMP is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with VOMP.  If not, see <https://www.gnu.org/licenses/>.
18 */
19
20 #ifndef IMAGELOADER_H
21 #define IMAGELOADER_H
22
23 #include <memory>
24 #include <vector>
25 #include <queue>
26 #include <list>
27 #include <mutex>
28 #include <thread>
29 #include <condition_variable>
30
31 #include "defines.h"
32 #include "osdvectortypes.h"
33 #include "osdvector.h"
34
35 #include "image.h"
36
37
38 class LogNT;
39 class VDR_ResponsePacket;
40
41 class PictureReader;
42 class PictureDecoder
43 {
44   public:
45     PictureDecoder() {}
46     virtual ~PictureDecoder() {}
47
48     // its is always guaranted, that after getDecodedPicture a call to decodePicture follows, if the return value was true;
49     virtual unsigned char* decodePicture(LoadingIndex index, unsigned char* buffer, unsigned int length, bool freemem = true) = 0;
50
51     virtual bool getDecodedPicture(struct PictureInfo& pict_inf) = 0;
52     virtual void freeReference(void* ref) = 0;
53
54     virtual void init() {};
55     virtual void shutdown() {};
56 };
57
58 class ImageLoader
59 {
60   public:
61     ImageStatic createStatic(int sa_id);
62     ImageGeneric createGeneric();
63     ImageRecFolder createRecFolder(const char* recFileName);
64     ImageRecThumb createRecThumb(const char* recFileName);
65     ImageEventThumb createEventThumb(ULONG channel, ULONG event);
66     ImageChannelLogo createChannelLogo(ULONG channel, int sa_id = -1);
67
68     ImageLoader();
69     ~ImageLoader();
70     static ImageLoader* getInstance();
71
72     bool init();
73     void shutdown();
74     void ensureLoaded(Image& image);
75     void ensureLoaded(ImageGeneric& imageGeneric);
76     void decodeStatic(Image& t);
77     void downloadDone(VDR_ResponsePacket* vresp);
78     bool pictureRendered(LoadingIndex index, OsdImage& oimage);
79     void garbageCollect();
80
81     void addDecoder(PictureDecoder* decoder);
82     void removeDecoder(PictureDecoder* decoder);
83
84     void dumpImages();
85
86   private:
87     static ImageLoader* instance;
88
89     LogNT* logger{};
90
91     std::vector<Image> mImages;  // Master vector of Image objects
92     std::mutex mImagesLock;
93
94     struct PictureReaderWorkItem
95     {
96       VDR_ResponsePacket* vresp{};
97       // or
98       Image image;
99     };
100
101     std::queue<PictureReaderWorkItem> pictureReaderWork;
102     std::mutex pictureReaderWorkLock;
103
104     void pictureReaderThreadMethod();
105     std::mutex pictureReaderThreadMutex;
106     std::thread pictureReaderThread;
107     bool pictureReaderThreadRun{};
108     bool pictureReaderThreadQuit{};
109     std::condition_variable pictureReaderThreadCond;
110     std::mutex pictureReaderCondSigtex; // Careful with this, lock for minimal time
111
112     std::mutex decoders_lock;
113     std::list<PictureDecoder*> decoders;
114
115     Image loadingIndexToImage(LoadingIndex index);
116     void pictureReaderProcessOne();
117 };
118
119 #endif