Jetson Inference
DNN Vision Library
gstEncoder.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  */
22 
23 #ifndef __GSTREAMER_ENCODER_H__
24 #define __GSTREAMER_ENCODER_H__
25 
26 #include "gstUtility.h"
27 #include "videoOutput.h"
28 #include "RingBuffer.h"
29 
30 
31 // Forward declarations
32 class RTSPServer;
33 class WebRTCServer;
34 struct WebRTCPeer;
35 
36 
51 class gstEncoder : public videoOutput
52 {
53 public:
57  static gstEncoder* Create( const videoOptions& options );
58 
62  static gstEncoder* Create( const URI& resource, videoOptions::Codec codec );
63 
67  ~gstEncoder();
68 
73  template<typename T> bool Render( T* image, uint32_t width, uint32_t height ) { return Render((void**)image, width, height, imageFormatFromType<T>()); }
74 
79  virtual bool Render( void* image, uint32_t width, uint32_t height, imageFormat format );
80 
85  virtual bool Open();
86 
91  virtual void Close();
92 
96  inline GstPipeline* GetPipeline() const { return GST_PIPELINE(mPipeline); }
97 
101  inline WebRTCServer* GetWebRTCServer() const { return mWebRTCServer; }
102 
106  virtual inline uint32_t GetType() const { return Type; }
107 
111  static const uint32_t Type = (1 << 2);
112 
124  static const char* SupportedExtensions[];
125 
131  static bool IsSupportedExtension( const char* ext );
132 
133 protected:
134  gstEncoder( const videoOptions& options );
135 
136  bool init();
137  bool initPipeline();
138  void destroyPipeline();
139 
140  void checkMsgBus();
141  bool buildCapsStr();
142  bool buildLaunchStr();
143  bool encodeYUV( void* buffer, size_t size );
144 
145  // appsrc callbacks
146  static void onNeedData( GstElement* pipeline, uint32_t size, void* user_data );
147  static void onEnoughData( GstElement* pipeline, void* user_data );
148 
149  // WebRTC callbacks
150  static void onWebsocketMessage( WebRTCPeer* peer, const char* message, size_t message_size, void* user_data );
151 
152  GstBus* mBus;
153  GstCaps* mBufferCaps;
154  GstElement* mAppSrc;
155  GstElement* mPipeline;
156  bool mNeedData;
157 
158  std::string mCapsStr;
159  std::string mLaunchStr;
160 
162 
165 };
166 
167 
168 #endif
gstEncoder::buildCapsStr
bool buildCapsStr()
gstEncoder::onEnoughData
static void onEnoughData(GstElement *pipeline, void *user_data)
gstEncoder::mLaunchStr
std::string mLaunchStr
Definition: gstEncoder.h:159
RTSPServer
RTSP server for transmitting encoded GStreamer pipelines to client devices.
Definition: RTSPServer.h:55
gstEncoder::SupportedExtensions
static const char * SupportedExtensions[]
String array of supported video file extensions, terminated with a NULL sentinel value.
Definition: gstEncoder.h:124
gstEncoder::mBufferYUV
RingBuffer mBufferYUV
Definition: gstEncoder.h:161
gstEncoder::destroyPipeline
void destroyPipeline()
gstEncoder::Type
static const uint32_t Type
Unique type identifier of gstEncoder class.
Definition: gstEncoder.h:111
gstEncoder::Open
virtual bool Open()
Open the stream.
gstEncoder::initPipeline
bool initPipeline()
gstEncoder::mBus
GstBus * mBus
Definition: gstEncoder.h:152
WebRTCServer
WebRTC signalling server for establishing and negotiating connections with peers for bi-directional m...
Definition: WebRTCServer.h:116
gstEncoder::mPipeline
GstElement * mPipeline
Definition: gstEncoder.h:155
gstEncoder::GetWebRTCServer
WebRTCServer * GetWebRTCServer() const
Return the WebRTC server (only used when the protocol is "webrtc://")
Definition: gstEncoder.h:101
gstEncoder::Create
static gstEncoder * Create(const videoOptions &options)
Create an encoder from the provided video options.
gstEncoder::mBufferCaps
GstCaps * mBufferCaps
Definition: gstEncoder.h:153
gstEncoder::init
bool init()
gstEncoder::GetType
virtual uint32_t GetType() const
Return the interface type (gstEncoder::Type)
Definition: gstEncoder.h:106
gstUtility.h
gstEncoder::mAppSrc
GstElement * mAppSrc
Definition: gstEncoder.h:154
videoOptions::Codec
Codec
Video codecs.
Definition: videoOptions.h:198
gstEncoder::Close
virtual void Close()
Close the stream.
gstEncoder::encodeYUV
bool encodeYUV(void *buffer, size_t size)
URI
Resource URI of a video device, IP stream, or file/directory.
Definition: URI.h:101
gstEncoder::buildLaunchStr
bool buildLaunchStr()
gstEncoder::onWebsocketMessage
static void onWebsocketMessage(WebRTCPeer *peer, const char *message, size_t message_size, void *user_data)
gstEncoder::mCapsStr
std::string mCapsStr
Definition: gstEncoder.h:158
gstEncoder::~gstEncoder
~gstEncoder()
Destructor.
RingBuffer.h
gstEncoder::IsSupportedExtension
static bool IsSupportedExtension(const char *ext)
Return true if the extension is in the list of SupportedExtensions.
gstEncoder::Render
bool Render(T *image, uint32_t width, uint32_t height)
Encode the next frame.
Definition: gstEncoder.h:73
RingBuffer
Thread-safe circular ring buffer queue.
Definition: RingBuffer.h:33
gstEncoder::GetPipeline
GstPipeline * GetPipeline() const
Return the GStreamer pipeline object.
Definition: gstEncoder.h:96
gstEncoder::gstEncoder
gstEncoder(const videoOptions &options)
videoOutput
The videoOutput API is for rendering and transmitting frames to video input devices such as display w...
Definition: videoOutput.h:104
gstEncoder::mRTSPServer
RTSPServer * mRTSPServer
Definition: gstEncoder.h:163
gstEncoder::mNeedData
bool mNeedData
Definition: gstEncoder.h:156
videoOptions
The videoOptions struct contains common settings that are used to configure and query videoSource and...
Definition: videoOptions.h:37
gstEncoder::checkMsgBus
void checkMsgBus()
WebRTCPeer
Remote peer that has connected.
Definition: WebRTCServer.h:85
gstEncoder::mWebRTCServer
WebRTCServer * mWebRTCServer
Definition: gstEncoder.h:164
videoOutput.h
imageFormat
imageFormat
The imageFormat enum is used to identify the pixel format and colorspace of an image.
Definition: imageFormat.h:49
gstEncoder::onNeedData
static void onNeedData(GstElement *pipeline, uint32_t size, void *user_data)
gstEncoder
Hardware-accelerated video encoder for Jetson using GStreamer.
Definition: gstEncoder.h:51