VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gvimageinput.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2017 Graeme Walker
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 // ===
17 ///
18 /// \file gvimageinput.h
19 ///
20 
21 #ifndef GV_IMAGEINPUT__H
22 #define GV_IMAGEINPUT__H
23 
24 #include "gdef.h"
25 #include "grimagedata.h"
26 #include "grimagetype.h"
27 #include "grimageconverter.h"
28 #include "gpublisher.h"
29 #include "gtimer.h"
30 #include <string>
31 #include <vector>
32 #include <memory>
33 
34 namespace Gv
35 {
36  class ImageInput ;
37  class ImageInputSource ;
38  class ImageInputHandler ;
39  class ImageInputConversion ;
40  class ImageInputTask ;
41 }
42 
43 /// \class Gv::ImageInputConversion
44 /// A structure that describes the preferred image type for the
45 /// Gv::ImageInputHandler callback.
46 ///
48 {
49 public:
50  enum Type { none , to_jpeg , to_raw } ;
51 
53  ///< Default constructor for a 'none' conversion, with scale one.
54 
55  ImageInputConversion( Type type , int scale , bool monochrome ) ;
56  ///< Constructor.
57 
58  Type type ;
59  int scale ;
60  bool monochrome ;
61 } ;
62 
63 /// \class Gv::ImageInputTask
64 /// A private implementation class used by Gv::ImageInputSource. Represents
65 /// an image and its conversion.
66 ///
68 {
69 public:
70  ImageInputTask() ;
71  ///< Default constructor.
72 
73  bool run( Gr::ImageConverter & , Gr::Image ) ;
74  ///< Runs the conversion. Returns false on error.
75 
76  ImageInputHandler * m_handler ;
77  ImageInputConversion m_conversion ;
78  Gr::Image m_image ;
79 } ;
80 
81 /// \class Gv::ImageInputHandler
82 /// A callback interface for Gv::ImageInputSource.
83 ///
85 {
86 public:
87  virtual void onImageInput( ImageInputSource & , Gr::Image ) = 0 ;
88  ///< Called by Gv::ImageInputSource to deliver a new image.
89  ///<
90  ///< It is allowed to call removeImageInputHandler() from within
91  ///< the callback.
92 
93  virtual void onNonImageInput( ImageInputSource & , Gr::Image , const std::string & type_str ) ;
94  ///< Called by Gv::ImageInputSource to deliver a new non-image.
95  ///< This default implementation does nothing.
96 
98  ///< Returns the required image type conversion. This is called
99  ///< just before each image is delivered.
100 
101  virtual ~ImageInputHandler() ;
102  ///< Destructor.
103 
104 private:
105  void operator=( const ImageInputHandler & ) ;
106 } ;
107 
108 /// \class Gv::ImageInputSource
109 /// A base class for distributing incoming images to multiple client
110 /// objects, supporting some simple image type conversions. Derived
111 /// classes emit images by calling sendImageInput(). Client objects
112 /// register an interest by calling addImageInputHandler(), and they
113 /// implement imageInputConversion() to indicate the image format
114 /// they require.
115 ///
117 {
118 public:
119  explicit ImageInputSource( Gr::ImageConverter & , const std::string & source_name = std::string() ) ;
120  ///< Constructor.
121 
122  virtual ~ImageInputSource() ;
123  ///< Destructor.
124 
125  size_t handlers() const ;
126  ///< Returns the number of registered handlers.
127 
129  ///< Adds a handler for image callbacks.
130 
132  ///< Removes a handler for image callbacks.
133 
134  std::string name() const ;
135  ///< Returns the source name, as passed to the constructor.
136 
137  virtual void resend( ImageInputHandler & ) = 0 ;
138  ///< Asks the source to resend asynchronously the lastest available image,
139  ///< if any, to the specified handler, even if it is old or seen before.
140  ///< Some overrides may always do nothing, and others may do nothing
141  ///< only if no data is available.
142 
143 protected:
144  bool sendImageInput( Gr::Image , ImageInputHandler * one_handler_p = nullptr ) ;
145  ///< Sends a new image to all registered handlers, or optionally to just
146  ///< one of them. Returns false if there were any image conversion errors.
147 
148  void sendNonImageInput( Gr::Image non_image , const std::string & type_str ,
149  ImageInputHandler * one_handler_p = nullptr ) ;
150  ///< Sends non-image data to registered handlers (or one).
151 
152 private:
154  void operator=( const ImageInputSource & ) ;
155  void collectGarbage() ;
156 
157 private:
158  typedef std::vector<ImageInputTask> TaskList ;
159  std::string m_name ;
160  TaskList m_tasks ;
161  Gr::ImageConverter & m_converter ;
162 } ;
163 
164 /// \class Gv::ImageInput
165 /// A class for reading images from a publication channel and distributing
166 /// them to multiple client objects.
167 ///
169 {
170 public:
171  ImageInput( Gr::ImageConverter & , const std::string & channel_name , bool lazy_open ) ;
172  ///< Constructor.
173 
174  ~ImageInput() ;
175  ///< Destructor.
176 
177  int fd() const ;
178  ///< Returns the publication channel file descriptor, or -1
179  ///< if close()d.
180 
181  bool handleReadEvent() ;
182  ///< Called to process a read event on the file descriptor.
183  ///< All registered handlers are notified of any new images.
184  ///< Returns false if the publisher has disappeared, in
185  ///< which case the user should remove the fd() from the
186  ///< event loop and then call close().
187 
188  std::string info() const ;
189  ///< Returns the channel info.
190 
191  void close() ;
192  ///< Closes the input channel, releasing resources.
193 
194  bool open() ;
195  ///< Tries to re-open the input channel some time after
196  ///< handleReadEvent() has returned false. Returns true
197  ///< on success.
198 
199 private:
200  ImageInput( const ImageInput & ) ;
201  void operator=( const ImageInput & ) ;
202  void onResendTimeout() ;
203  bool receive( bool ) ;
204  virtual void onException( std::exception & ) override ;
205  virtual void resend( ImageInputHandler & ) override ;
206 
207 private:
208  G::PublisherSubscription m_channel ;
209  Gr::Image m_image ;
210  std::string m_type_str ;
211  GNet::Timer<ImageInput> m_resend_timer ;
212  ImageInputHandler * m_resend_to ;
213 } ;
214 
215 #endif
void sendNonImageInput(Gr::Image non_image, const std::string &type_str, ImageInputHandler *one_handler_p=nullptr)
Sends non-image data to registered handlers (or one).
ImageInputTask()
Default constructor.
An abstract interface for handling exceptions thrown out of event-loop callbacks (socket events and t...
Definition: geventhandler.h:44
void close()
Closes the input channel, releasing resources.
void addImageInputHandler(ImageInputHandler &)
Adds a handler for image callbacks.
virtual ~ImageInputSource()
Destructor.
void removeImageInputHandler(ImageInputHandler &)
Removes a handler for image callbacks.
virtual ~ImageInputHandler()
Destructor.
std::string info() const
Returns the channel info.
bool open()
Tries to re-open the input channel some time after handleReadEvent() has returned false...
A class for reading images from a publication channel and distributing them to multiple client object...
Definition: gvimageinput.h:168
A callback interface for Gv::ImageInputSource.
Definition: gvimageinput.h:84
virtual ImageInputConversion imageInputConversion(ImageInputSource &)=0
Returns the required image type conversion.
An easy-to-use combination of a G::PublisherChannel object and a single G::PublisherSubscriber.
Definition: gpublisher.h:229
ImageInputSource(Gr::ImageConverter &, const std::string &source_name=std::string())
Constructor.
~ImageInput()
Destructor.
A class holding shared read-only image data (Gr::ImageBuffer) and its associated image type (Gr::Imag...
Definition: grimage.h:41
A base class for distributing incoming images to multiple client objects, supporting some simple imag...
Definition: gvimageinput.h:116
virtual void onNonImageInput(ImageInputSource &, Gr::Image, const std::string &type_str)
Called by Gv::ImageInputSource to deliver a new non-image.
bool sendImageInput(Gr::Image, ImageInputHandler *one_handler_p=nullptr)
Sends a new image to all registered handlers, or optionally to just one of them.
int fd() const
Returns the publication channel file descriptor, or -1 if close()d.
A private implementation class used by Gv::ImageInputSource.
Definition: gvimageinput.h:67
ImageInputConversion()
Default constructor for a 'none' conversion, with scale one.
virtual void resend(ImageInputHandler &)=0
Asks the source to resend asynchronously the lastest available image, if any, to the specified handle...
virtual void onImageInput(ImageInputSource &, Gr::Image)=0
Called by Gv::ImageInputSource to deliver a new image.
bool handleReadEvent()
Called to process a read event on the file descriptor.
bool run(Gr::ImageConverter &, Gr::Image)
Runs the conversion. Returns false on error.
size_t handlers() const
Returns the number of registered handlers.
A structure that describes the preferred image type for the Gv::ImageInputHandler callback...
Definition: gvimageinput.h:47
An image format converter that can convert to and from the raw and jpeg formats (only), with scaling and monochrome options.
ImageInput(Gr::ImageConverter &, const std::string &channel_name, bool lazy_open)
Constructor.
A timer class template in which the timeout is delivered to the specified method. ...
Definition: gtimer.h:110
std::string name() const
Returns the source name, as passed to the constructor.