31 const std::string & channel ,
int shmem_fd ,
int pipe_fd ) :
32 m_input_handler(input_handler) ,
33 m_scale(input_config.m_scale) ,
34 m_static(input_config.m_static) ,
36 m_rate_limit(input_config.m_rate_limit) ,
37 m_rate_timer(*this,&
ViewerInput::onRateTimeout,*this) ,
38 m_quiescent_timer(*this,&
ViewerInput::onQuiescentTimeout,*this) ,
39 m_decoder_tmp(Gr::ImageData::Contiguous) ,
49 m_quiescent_timer.startTimer( 2U ) ;
51 m_fd = m_channel->fd() ;
61 void Gv::ViewerInput::onException( std::exception & )
66 void Gv::ViewerInput::readEvent()
68 m_quiescent_timer.cancelTimer() ;
69 if( m_static && !m_first )
73 else if( read(m_buffer) && decode() )
76 m_input_handler.onInput( m_type_out.dx() , m_type_out.dy() , m_type_out.channels() , m_data_out , m_type_out.size() ) ;
80 bool Gv::ViewerInput::read( std::vector<char> & buffer )
85 if( !m_pipe->receive( buffer , &m_type_in_str , &time ) )
86 throw Closed(
"parent has gone away" ) ;
90 if( !m_channel->receive( buffer , &m_type_in_str , &time ) )
91 throw std::runtime_error(
"publisher has gone away" ) ;
97 G_DEBUG(
"Gv::ViewerInput::read: empty read" ) ;
99 if( !m_type_in.valid() )
100 G_DEBUG(
"Gv::ViewerInput::read: invalid type: [" << m_type_in_str <<
"]" ) ;
102 bool ok = !buffer.empty() && m_type_in.valid() ;
104 G_DEBUG(
"Gv::ViewerInput::read: got image: size=" << buffer.size() <<
" type=[" << m_type_in <<
"]" ) ;
108 G_DEBUG(
"Gv::ViewerInput::read: rate limiting: dropping fd " << m_fd ) ;
110 m_rate_timer.startTimer( m_rate_limit.s , m_rate_limit.us ) ;
115 void Gv::ViewerInput::onRateTimeout()
117 G_DEBUG(
"Gv::ViewerInput::onRateTimeout: rate limiting: resuming fd " << m_fd ) ;
121 void Gv::ViewerInput::onQuiescentTimeout()
123 G_ASSERT( m_channel.get() != nullptr ) ;
124 unsigned int age = m_channel->age() ;
125 std::ostringstream ss ;
127 ss <<
": idle for " << age <<
"s" ;
128 G_WARNING(
"Gv::ViewerInput::onQuiescentTimeout: quiescent channel [" << m_channel->name() <<
"]" << ss.str() ) ;
131 bool Gv::ViewerInput::decode()
135 m_decoder.setup( m_scale ,
false ) ;
136 m_data_out = &m_buffer[0] ;
137 m_type_out = m_decoder.decodeInPlace( m_type_in , m_data_out , m_buffer.size() , m_decoder_tmp ) ;
138 G_DEBUG(
"Gv::ViewerInput::decode: type=[" << m_type_out <<
"]" ) ;
141 catch( std::exception & e )
143 G_WARNING_ONCE(
"Gv::ViewerInput::decode: viewer failed to decode image: "
144 "type=[" << m_type_in <<
"] size=" << m_buffer.size() <<
": " << e.what() ) ;
145 G_LOG(
"Gv::ViewerInput::decode: failed image decode: "
146 "type=[" << m_type_in <<
"] size=" << m_buffer.size() <<
": " << e.what() ) ;
153 return &m_buffer[0] ;
158 return m_buffer.size() ;
163 return m_type_out.dx() ;
168 return m_type_out.dy() ;
173 return m_type_out.channels() ;
A subsecond-resolution timestamp based on a time_t.
An encapsulation of image type, including width, height and number of channels, with support for a st...
virtual void dropRead(Descriptor fd)=0
Removes the given event source descriptor from the list of read sources.
An easy-to-use combination of a G::PublisherChannel object and a single G::PublisherSubscriber.
A class that encapsulates a network file descriptor and hides knowledge of its o/s-spefific error val...
virtual void addRead(Descriptor fd, EventHandler &handler)=0
Adds the given event source descriptor and associated handler to the read list.
A class to read a fat pipe in the child process.
static EventLoop & instance()
Returns a reference to an instance of the class, if any.