VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gvviewerinput.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 gvviewerinput.h
19 ///
20 
21 #ifndef GV_VIEWERINPUT__H
22 #define GV_VIEWERINPUT__H
23 
24 #include "gdef.h"
25 #include "gnet.h"
26 #include "gdatetime.h"
27 #include "geventhandler.h"
28 #include "gtimer.h"
29 #include "gpublisher.h"
30 #include "gfatpipe.h"
31 #include "grimagetype.h"
32 #include "grimagedata.h"
33 #include "grimagedecoder.h"
34 #include <string>
35 #include <vector>
36 #include <iostream>
37 
38 namespace Gv
39 {
40  class ViewerInput ;
41  class ViewerInputHandler ;
42  struct ViewerInputConfig ;
43 }
44 
45 /// \class Gv::ViewerInputHandler
46 /// A callback interface for Gv::ViewerInput.
47 ///
49 {
50 public:
51  virtual void onInput( int dx , int dy , int channels , const char * p , size_t n ) = 0 ;
52  ///< Called to deliver a raw image.
53 
54  virtual ~ViewerInputHandler() ;
55  ///< Destructor.
56 } ;
57 
58 /// \class Gv::ViewerInputConfig
59 /// A structure to hold configuration for a Gv::ViewerInput object.
60 ///
62 {
63  int m_scale ;
64  bool m_static ; // one frame
65  G::EpochTime m_rate_limit ; // inter-frame delay
66  explicit ViewerInputConfig( int scale = 1 , bool static_ = false ) :
67  m_scale(scale) ,
68  m_static(static_) ,
69  m_rate_limit(0)
70  {
71  }
72 } ;
73 
74 /// \class Gv::ViewerInput
75 /// An input channel for images. Images are decoded as necessary.
76 ///
78 {
79 public:
80  G_EXCEPTION( Closed , "viewer input channel closed" ) ;
81  typedef ViewerInputHandler Handler ;
82 
84  const std::string & channel , int shmem_fd , int pipe_fd ) ;
85  ///< Constructor.
86 
87  ~ViewerInput() ;
88  ///< Destructor.
89 
90  int fd() const ;
91  ///< Returns the file descriptor that is being used for notification.
92 
93  const char * data() const ;
94  ///< Returns the data as a standard luma or rgb-interleaved buffer.
95 
96  size_t size() const ;
97  ///< Returns the data() size.
98 
99  int dx() const ;
100  ///< Returns the image width.
101 
102  int dy() const ;
103  ///< Returns the image height.
104 
105  int channels() const ;
106  ///< Returns the number of channels (one or three).
107 
108  Gr::ImageType type() const ;
109  ///< Returns the type for the decoded image.
110 
111 private:
112  ViewerInput( const ViewerInput & ) ;
113  void operator=( const ViewerInput & ) ;
114  virtual void readEvent() override ;
115  virtual void onException( std::exception & ) override ;
116  static Gr::ImageType subsample( std::vector<char> & , const Gr::ImageType & , int , bool , bool ) ;
117  void onRateTimeout() ;
118  void onQuiescentTimeout() ;
119  bool read( std::vector<char> & ) ;
120  bool decode() ;
121 
122 public:
123  ViewerInputHandler & m_input_handler ;
124  int m_scale ;
125  bool m_static ;
126  bool m_first ;
127  G::EpochTime m_rate_limit ;
128  int m_fd ;
129  unique_ptr<G::FatPipe::Receiver> m_pipe ;
130  unique_ptr<G::PublisherSubscription> m_channel ;
131  std::vector<char> m_buffer ;
132  std::vector<char> m_tmp ;
133  Gr::ImageType m_type_in ;
134  std::string m_type_in_str ;
135  Gr::ImageType m_type_out ; // raw
136  GNet::Timer<ViewerInput> m_rate_timer ;
137  GNet::Timer<ViewerInput> m_quiescent_timer ;
138  Gr::ImageData m_decoder_tmp ;
139  Gr::ImageDecoder m_decoder ;
140  char * m_data_out ;
141 } ;
142 
143 #endif
A subsecond-resolution timestamp based on a time_t.
Definition: gdatetime.h:39
A holder for image data, having eight bits per sample and one or three channels.
Definition: grimagedata.h:46
An input channel for images.
Definition: gvviewerinput.h:77
~ViewerInput()
Destructor.
virtual void onInput(int dx, int dy, int channels, const char *p, size_t n)=0
Called to deliver a raw image.
A callback interface for Gv::ViewerInput.
Definition: gvviewerinput.h:48
An encapsulation of image type, including width, height and number of channels, with support for a st...
Definition: grimagetype.h:43
virtual ~ViewerInputHandler()
Destructor.
Gr::ImageType type() const
Returns the type for the decoded image.
An interface for decoding encoded images into a raw format.
size_t size() const
Returns the data() size.
A structure to hold configuration for a Gv::ViewerInput object.
Definition: gvviewerinput.h:61
ViewerInput(ViewerInputHandler &, const ViewerInputConfig &, const std::string &channel, int shmem_fd, int pipe_fd)
Constructor.
A base class for classes that handle asynchronous events from the event loop.
Definition: geventhandler.h:78
int fd() const
Returns the file descriptor that is being used for notification.
int channels() const
Returns the number of channels (one or three).
A timer class template in which the timeout is delivered to the specified method. ...
Definition: gtimer.h:110
int dy() const
Returns the image height.
const char * data() const
Returns the data as a standard luma or rgb-interleaved buffer.
int dx() const
Returns the image width.