VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gvimageoutput.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 gvimageoutput.h
19 ///
20 
21 #ifndef GV_IMAGEOUTPUT__H
22 #define GV_IMAGEOUTPUT__H
23 
24 #include "gdef.h"
25 #include "gpath.h"
26 #include "gfatpipe.h"
27 #include "gpublisher.h"
28 #include "gdatetime.h"
29 #include "gvtimezone.h"
30 #include "grimagetype.h"
31 #include "gtimer.h"
32 #include <string>
33 
34 namespace Gv
35 {
36  class ImageOutput ;
37 }
38 
39 /// \class Gv::ImageOutput
40 /// A class for emiting images etc to a fat pipe that feeds a spawned viewer
41 /// process, and/or to a publication channel, and/or to a filesystem image
42 /// repository.
43 ///
45 {
46 public:
47  ImageOutput() ;
48  ///< Constructor.
49 
51  ///< Constructor taking a callback interface for handling
52  ///< failed viewer pings.
53 
54  ~ImageOutput() ;
55  ///< Destructor.
56 
57  void startViewer( const std::string & title = std::string() , unsigned int scale = 1U ,
58  const std::string & output_channel = std::string() ) ;
59  ///< Starts the viewer. Does periodic pings of the viewer's
60  ///< fat pipe if the relevant constructor overload was used.
61 
62  void startPublisher( const std::string & channel ) ;
63  ///< Starts the publisher channel.
64 
65  void startPublisher( const std::string & channel , const G::Item & publisher_info ) ;
66  ///< Starts the publisher channel with additional infomation describing
67  ///< the publisher.
68 
69  void startPublisher( G::Publisher * ) ;
70  ///< Starts the publisher channel by taking ownership of a new-ed publisher object.
71 
72  void saveTo( const std::string & base_dir , const std::string & name , bool fast ,
73  const Gv::Timezone & tz , bool test_mode = false ) ;
74  ///< Saves images to the filesystem inside a deeply-nested directory
75  ///< hierarchy with the given base directory. Every filename is prefixed
76  ///< with the given name. Directory paths are derived from a timestamp;
77  ///< if the 'fast' flag is true then sub-second parts of the timestamp
78  ///< are used.
79 
80  bool viewing() const ;
81  ///< Returns true if startViewer() has been called.
82 
83  bool pingViewer() ;
84  ///< Returns true if the viewer seems to be running. The transition
85  ///< from true to false can be used to terminate the caller's event
86  ///< loop.
87 
88  void pingViewerCheck() ;
89  ///< Checks that the viewer is running if it should be (see pingViewer()),
90  ///< and throws an exeception if not.
91 
92  G::Path send( const char * data , size_t size , Gr::ImageType type , G::EpochTime = G::EpochTime(0) ) ;
93  ///< Emits an image. Returns the path if saveTo()d the filesystem.
94 
95  G::Path send( const Gr::ImageBuffer & data , Gr::ImageType type , G::EpochTime = G::EpochTime(0) ) ;
96  ///< Emits an image. Returns the path if saveTo()d the filesystem.
97 
98  void sendText( const char * data , size_t size , const std::string & type ) ;
99  ///< Emits a non-image message. Non-image messages are not saveTo()d the filesystem.
100 
101  const std::string & dir() const ;
102  ///< Returns the saveTo() path.
103 
104  static void path( std::string & out , const std::string & base_dir , const std::string & name ,
105  G::EpochTime , Gr::ImageType type , bool fast , const Gv::Timezone & tz , bool test_mode ) ;
106  ///< Returns by reference the filesystem path corresponding
107  ///< to the given base directory and timestamp.
108 
109  std::string path( G::EpochTime , Gr::ImageType type , bool fast ) const ;
110  ///< Returns the filesystem path corresponding to the given
111  ///< timestamp.
112 
113  bool fast() const ;
114  ///< Returns the saveTo() fast flag.
115 
116 private:
117  ImageOutput( const ImageOutput & ) ;
118  void operator=( const ImageOutput & ) ;
119  G::Path save( const char * p , size_t n , Gr::ImageType , G::EpochTime ) ;
120  G::Path save( const Gr::ImageBuffer & , Gr::ImageType , G::EpochTime ) ;
121  G::Path openFile( std::ofstream & , Gr::ImageType , G::EpochTime ) ;
122  void commitFile( std::ofstream & , const G::Path & ) ;
123  static std::string viewer( const G::Path & ) ;
124  static std::string sanitise( const std::string & ) ;
125  void onPingTimeout() ;
126 
127 private:
128  unique_ptr<GNet::Timer<ImageOutput> > m_ping_timer_ptr ;
129  std::string m_base_dir ;
130  std::string m_name ;
131  bool m_fast ;
132  Gv::Timezone m_tz ;
133  bool m_save_test_mode ;
134  unique_ptr<G::Publisher> m_publisher ;
135  unique_ptr<G::FatPipe> m_fat_pipe ;
136  G::Path m_old_path ;
137  G::Path m_old_dir ;
138  bool m_viewer_up ;
139 } ;
140 
141 #endif
void startViewer(const std::string &title=std::string(), unsigned int scale=1U, const std::string &output_channel=std::string())
Starts the viewer.
A subsecond-resolution timestamp based on a time_t.
Definition: gdatetime.h:39
const std::string & dir() const
Returns the saveTo() path.
An abstract interface for handling exceptions thrown out of event-loop callbacks (socket events and t...
Definition: geventhandler.h:44
A representation of a timezone.
Definition: gvtimezone.h:35
G::Path send(const char *data, size_t size, Gr::ImageType type, G::EpochTime=G::EpochTime(0))
Emits an image. Returns the path if saveTo()d the filesystem.
A class for emiting images etc to a fat pipe that feeds a spawned viewer process, and/or to a publica...
Definition: gvimageoutput.h:44
An encapsulation of image type, including width, height and number of channels, with support for a st...
Definition: grimagetype.h:43
Vectors ImageBuffer
An ImageBuffer is used to hold raw image data, typically in more than one chunk.
Definition: grimagebuffer.h:47
static void path(std::string &out, const std::string &base_dir, const std::string &name, G::EpochTime, Gr::ImageType type, bool fast, const Gv::Timezone &tz, bool test_mode)
Returns by reference the filesystem path corresponding to the given base directory and timestamp...
void saveTo(const std::string &base_dir, const std::string &name, bool fast, const Gv::Timezone &tz, bool test_mode=false)
Saves images to the filesystem inside a deeply-nested directory hierarchy with the given base directo...
bool fast() const
Returns the saveTo() fast flag.
~ImageOutput()
Destructor.
bool viewing() const
Returns true if startViewer() has been called.
ImageOutput()
Constructor.
void pingViewerCheck()
Checks that the viewer is running if it should be (see pingViewer()), and throws an exeception if not...
A variant class holding a string, an item map keyed by name, or an ordered list of items...
Definition: gitem.h:41
A broadcast communication channel between unrelated processes using shared memory.
Definition: gpublisher.h:67
void startPublisher(const std::string &channel)
Starts the publisher channel.
bool pingViewer()
Returns true if the viewer seems to be running.
A Path object represents a file system path.
Definition: gpath.h:72
void sendText(const char *data, size_t size, const std::string &type)
Emits a non-image message. Non-image messages are not saveTo()d the filesystem.