VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gvrtpserver.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 gvrtpserver.h
19 ///
20 
21 #ifndef GV_RTPSERVER__H
22 #define GV_RTPSERVER__H
23 
24 #include "gdef.h"
25 #include "gnet.h"
26 #include "geventhandler.h"
27 #include "gtimer.h"
28 #include "gsocket.h"
29 #include "gaddress.h"
30 #include "gravc.h"
31 #include "gvrtppacket.h"
32 #include "gvrtpjpegpacket.h"
33 #include "gvrtpavcpacket.h"
34 #include "gvrtppacketstream.h"
35 #include "gvavcreader.h"
36 #include "grimagetype.h"
37 #include "grjpeg.h"
38 #include "gpath.h"
39 #include <vector>
40 #include <string>
41 #include <exception>
42 
43 namespace Gv
44 {
45  class RtpServer ;
46  class RtpServerHandler ;
47 }
48 
49 /// \class Gv::RtpServer
50 /// An RTP server class.
51 ///
53 {
54 public:
55  G_EXCEPTION( InvalidFmtp , "invalid fmtp" ) ;
56 
57  RtpServer( RtpServerHandler & , int scale , bool monochrome , GNet::Address bind_address , const std::string & group_address ,
58  unsigned int packet_type , const G::Path & fmtp_file ,
59  int jpeg_fudge_factor , const std::string & filter_spec , unsigned int source_stale_timeout ) ;
60  ///< Constructor. The received images are delivered to the given
61  ///< callback interface.
62  ///<
63  ///< If a non-zero packet type is specified it should normally be
64  ///< 26 for "JPEG/90000" or some value in the dynamic range (96..127)
65  ///< for "H264/90000". In principle the sdp attribute "rtpmap"
66  ///< should be used to map from "H264/90000" to the dynamic packet
67  ///< type, but a fixed value of 96 is often adequate. If a zero
68  ///< packet type is specified then the type of the first packet
69  ///< received is used.
70 
71  virtual ~RtpServer() ;
72  ///< Destructor.
73 
74 private:
75  virtual void readEvent() override ;
76  virtual void onException( std::exception & ) override ;
77  void onData( const char * p , std::string::size_type n ) ;
78  void processRtpData( const char * p , std::string::size_type n ) ;
79  void processJpegPayload( const std::vector<char> & ) ;
80  void processAvcPayload( const std::vector<char> & ) ;
81  bool filter( const RtpAvcPacket & ) const ;
82  bool filter( const RtpJpegPacket & ) const ;
83  void onTimer() ;
84  void join( const std::string & ) ;
85  static std::string readFmtpFile( const G::Path & ) ;
86  static int autoscale( int scale , int dx ) ;
87 
88 private:
89  RtpServerHandler & m_handler ;
90  int m_scale ; // -1 for auto-scaling
91  bool m_monochrome ;
92  unsigned int m_packet_type ;
93  unsigned long m_source_id ;
94  time_t m_source_time ;
95  unsigned int m_source_stale_timeout ;
96  G::Path m_fmtp_file ;
97  int m_jpeg_fudge_factor ;
98  std::string m_filter_spec ;
99  std::vector<unsigned int> m_filter_list ;
100  unique_ptr<Gr::Avc::Configuration> m_avcc ;
101  GNet::DatagramSocket m_socket ;
102  std::vector<char> m_packet_buffer ;
103  RtpPacketStream m_packet_stream ;
104  std::vector<char> m_output_buffer ;
105  unsigned int m_seq_old ;
106  unique_ptr<Gv::AvcReaderStream> m_avc_reader_stream ;
107  GNet::Timer<RtpServer> m_test_timer ;
108  int m_test_index ;
109  Gr::JpegReader m_jpeg_reader ;
110  Gr::JpegWriter m_jpeg_writer ;
111  Gr::ImageData m_jpeg_image_data ;
112  std::vector<char> m_jpeg_buffer ;
113 } ;
114 
115 /// \class Gv::RtpServerHandler
116 /// A interface that Gv::RtpServer uses to deliver image data.
117 ///
119 {
120 public:
121  virtual void onImage( const std::vector<char> & , const Gr::ImageType & , bool avc_key_frame ) = 0 ;
122  ///< Called on receipt of an image. This is either an un-compressed
123  ///< JPEG image in JFIF format, or a fully-decoded AVC image.
124  ///< The recipient is free to modify the data buffer.
125 
126 protected:
127  virtual ~RtpServerHandler() ;
128 } ;
129 
130 #endif
A holder for image data, having eight bits per sample and one or three channels.
Definition: grimagedata.h:46
RtpServer(RtpServerHandler &, int scale, bool monochrome, GNet::Address bind_address, const std::string &group_address, unsigned int packet_type, const G::Path &fmtp_file, int jpeg_fudge_factor, const std::string &filter_spec, unsigned int source_stale_timeout)
Constructor.
Definition: gvrtpserver.cpp:37
An RTP payload parser for the jpeg payload type.
The GNet::Address class encapsulates a TCP/UDP transport address.
Definition: gaddress.h:55
A derivation of GNet::Socket for a datagram socket.
Definition: gsocket.h:279
An encapsulation of image type, including width, height and number of channels, with support for a st...
Definition: grimagetype.h:43
virtual ~RtpServer()
Destructor.
Definition: gvrtpserver.cpp:82
An RTP server class.
Definition: gvrtpserver.h:52
A read interface for libjpeg.
Definition: grjpeg.h:102
A class that accumulates RTP-AVC or RTP-JPEG packets and serves up AVC NALUs or JPEG JFIFs...
A base class for classes that handle asynchronous events from the event loop.
Definition: geventhandler.h:78
A write interface for libjpeg.
Definition: grjpeg.h:144
An RTP payload parser for the "H264" payload type.
A timer class template in which the timeout is delivered to the specified method. ...
Definition: gtimer.h:110
A interface that Gv::RtpServer uses to deliver image data.
Definition: gvrtpserver.h:118
A Path object represents a file system path.
Definition: gpath.h:72
virtual void onImage(const std::vector< char > &, const Gr::ImageType &, bool avc_key_frame)=0
Called on receipt of an image.