-
Notifications
You must be signed in to change notification settings - Fork 289
/
gstEncoder.h
168 lines (139 loc) · 4.9 KB
/
gstEncoder.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
* Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#ifndef __GSTREAMER_ENCODER_H__
#define __GSTREAMER_ENCODER_H__
#include "gstUtility.h"
#include "videoOutput.h"
#include "RingBuffer.h"
// Forward declarations
class RTSPServer;
class WebRTCServer;
struct WebRTCPeer;
/**
* Hardware-accelerated video encoder for Jetson using GStreamer.
*
* The encoder can write the encoded video to disk in (MKV, MP4, AVI, FLV),
* or stream over the network to a remote host via RTP/RTSP using UDP/IP.
* The supported encoder codecs are H.264, H.265, VP8, VP9, and MJPEG.
*
* @note gstEncoder implements the videoOutput interface and is intended to
* be used through that as opposed to directly. videoOutput implements
* additional command-line parsing of videoOptions to construct instances.
*
* @see videoOutput
* @ingroup codec
*/
class gstEncoder : public videoOutput
{
public:
/**
* Create an encoder from the provided video options.
*/
static gstEncoder* Create( const videoOptions& options );
/**
* Create an encoder instance from resource URI and codec.
*/
static gstEncoder* Create( const URI& resource, videoOptions::Codec codec );
/**
* Destructor
*/
~gstEncoder();
/**
* Encode the next frame.
* @see videoOutput::Render()
*/
template<typename T> bool Render( T* image, uint32_t width, uint32_t height, cudaStream_t stream=0 ) { return Render((void**)image, width, height, imageFormatFromType<T>(), stream); }
/**
* Encode the next frame.
* @see videoOutput::Render()
*/
virtual bool Render( void* image, uint32_t width, uint32_t height, imageFormat format, cudaStream_t stream=0 );
/**
* Open the stream.
* @see videoOutput::Open()
*/
virtual bool Open();
/**
* Close the stream.
* @see videoOutput::Open()
*/
virtual void Close();
/**
* Return the GStreamer pipeline object.
*/
inline GstPipeline* GetPipeline() const { return GST_PIPELINE(mPipeline); }
/**
* Return the WebRTC server (only used when the protocol is "webrtc://")
*/
inline WebRTCServer* GetWebRTCServer() const { return mWebRTCServer; }
/**
* Return the interface type (gstEncoder::Type)
*/
virtual inline uint32_t GetType() const { return Type; }
/**
* Unique type identifier of gstEncoder class.
*/
static const uint32_t Type = (1 << 2);
/**
* String array of supported video file extensions, terminated
* with a NULL sentinel value. The supported extension are:
*
* - MKV
* - MP4 / QT
* - AVI
* - FLV
*
* @see IsSupportedExtension() to check a string against this list.
*/
static const char* SupportedExtensions[];
/**
* Return true if the extension is in the list of SupportedExtensions.
* @param ext string containing the extension to be checked (should not contain leading dot)
* @see SupportedExtensions for the list of supported video file extensions.
*/
static bool IsSupportedExtension( const char* ext );
protected:
gstEncoder( const videoOptions& options );
bool init();
bool initPipeline();
void destroyPipeline();
void checkMsgBus();
bool buildCapsStr();
bool buildLaunchStr();
bool encodeYUV( void* buffer, size_t size );
// appsrc callbacks
static void onNeedData( GstElement* pipeline, uint32_t size, void* user_data );
static void onEnoughData( GstElement* pipeline, void* user_data );
// WebRTC callbacks
static void onWebsocketMessage( WebRTCPeer* peer, const char* message, size_t message_size, void* user_data );
GstBus* mBus;
GstCaps* mBufferCaps;
GstElement* mAppSrc;
GstElement* mPipeline;
bool mNeedData;
std::string mCapsStr;
std::string mLaunchStr;
RingBuffer mBufferYUV;
RTSPServer* mRTSPServer;
WebRTCServer* mWebRTCServer;
};
#endif