-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
342 lines (300 loc) · 11.3 KB
/
main.cpp
File metadata and controls
342 lines (300 loc) · 11.3 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#include <cstdio>
#include <gst/gst.h>
#include <gst/app/gstappsink.h>
#include <gst/rtsp/gstrtsptransport.h>
#include <string>
#include <iostream>
#include <cassert>
#include <map>
#include <utility>
#include <vector>
class GStreamPipeline
{
private:
typedef std::pair<GstElement*, GstCaps*> gstream_pair;
typedef std::map<std::string, gstream_pair> gstream_map;
gstream_map m_pipeline_map;
GstElement* m_pipeline;
GMainLoop* m_loop;
/**
@brief creates a gstreame element
@param element Gstelement to link
@param caps caps filter can be NULL
@param child_element child to link
@param element_name element name
@param child_element_name child_element_name
@return true on success false on failure
*/
bool linkElement(GstElement* element, GstCaps* caps, GstElement* child_element, const std::string& element_name, const std::string& child_element_name)
{
if(G_UNLIKELY(!GST_IS_ELEMENT(element)))
{
std::cout << "Failed to link: " << element_name << " was the pipeline created?\n";
return false;
}
if(G_UNLIKELY(!GST_IS_ELEMENT(child_element)))
{
std::cout << "Failed to link " << element_name << " to " << child_element_name << " was the pipeline created?\n";
return false;
}
if(GST_IS_CAPS(caps))
{
if(G_UNLIKELY(!gst_element_link_filtered(element, child_element, caps)))
{
gchar* tmp_str = gst_caps_to_string(caps);
std::cout << "Failed to link " << element_name << " to " << child_element_name << " with caps: " << tmp_str << '\n';
gst_caps_unref(caps);
g_free(tmp_str);
return false;
}
gst_caps_unref(caps);
return true;
}
else if(G_UNLIKELY(!gst_element_link(element, child_element)))
{
std::cout << "Failed to link " << element_name << " to " << child_element_name << '\n';
return false;
}
return true;
}
/**
@brief creates a gstream element
@param element gstElement plugin name
@param element_name element name
@param pipe to attach
@returns contructed element or NULL on failure
*/
GstElement* createElement(const std::string& element, const std::string& element_name, GstElement* pipe)
{
GstElement* output;
if(G_UNLIKELY(pipe == NULL))
return NULL;
if (G_UNLIKELY (!(output = (gst_element_factory_make (element.c_str(), element_name.c_str())))))
return NULL;
if (G_UNLIKELY (!gst_bin_add (GST_BIN_CAST (pipe), output)))
{
gst_object_unref(G_OBJECT(output));
return NULL;
}
return output;
}
public:
/**
@brief constructs a pipeline with the provided arguments
@param pipeline_name name of the pipeline to construct
@param init_gstream initialize gstreamer in constructor?
@param create_main_loop create a GMainLoop in constructor?
*/
GStreamPipeline(const std::string& pipeline_name, const bool init_gstream = true, const bool create_main_loop = true)
: m_pipeline_map(), m_pipeline(NULL), m_loop(NULL)
{
if(init_gstream)
gst_init(NULL, NULL);
if(create_main_loop)
m_loop = g_main_loop_new(NULL, FALSE);
m_pipeline = gst_pipeline_new(pipeline_name.c_str());
}
/**
@brief does nothing
*/
~GStreamPipeline()
{}
GStreamPipeline(GStreamPipeline&&) = delete;
GStreamPipeline(const GStreamPipeline&) = delete;
/**
@brief adds an element to the pipeline
@param element GstElement plugin name
@param element_name element_name
@returns true on success false on failure
*/
bool addElement(const std::string& element, const std::string& element_name)
{
GstElement* new_element = createElement(element, element_name, m_pipeline);
if(G_UNLIKELY(!GST_IS_ELEMENT(new_element)))
{
std::cout << "Failed to add element with name " << element << '\n';
return false;
}
m_pipeline_map[element_name] = {new_element, NULL};
return true;
}
/**
@brief adds a element to the pipeline
@param element GstElement plugin name
@param element_name element_name
@param element_caps element_caps string
*/
bool addElement(const std::string& element, const std::string& element_name, const std::string& element_caps)
{
GstElement* new_element = createElement(element, element_name, m_pipeline);
GstCaps* caps = gst_caps_from_string(element_caps.c_str());
if(G_UNLIKELY(!GST_IS_ELEMENT(new_element)))
{
std::cout << "Failed to add element with name " << element << '\n';
return false;
}
if(G_UNLIKELY(!GST_IS_CAPS(caps)))
{
std::cout << "Failed to create element caps with string " << element_caps << '\n';
return false;
}
m_pipeline_map[element_name] = {new_element, caps};
return false;
}
/**
@brief Links the elements by list of name
@param element_name list of nanes
@return if successfully linked
*/
bool linkElementsByName(const std::vector<std::string>& element_names)
{
for(auto begin = element_names.begin(); begin != element_names.end(); begin++)
{
if(begin + 1 == element_names.end())
break;
auto found = m_pipeline_map.find(*begin);
auto child_found = m_pipeline_map.find(*(begin + 1));
if(found == m_pipeline_map.end())
{
std::cout << "Failed to link elements. Could not find element by name: " << *begin << '\n';
return false;
}
if(child_found == m_pipeline_map.end())
{
std::cout << "Failed to link elements. Could not find child element by name: " << *begin++;
return false;
}
gstream_pair parent_pair = found->second;
gstream_pair child_pair = child_found->second;
if(!linkElement(parent_pair.first, parent_pair.second, child_pair.first, found->first, child_found->first))
return false;
}
return true;
}
/**
@brief Sets the element signal to supplied callback
@param element element name
@param signal_name name of the signal to attach the callback to
@param callback function callback
@param user_data user data. can be NULL
@returns true if the signal was set correctly. Doesn't guaranteed the Element was actually set because the API doesn't have any way to check
*/
bool setElementSignal(const std::string& element_name, const std::string& signal_name, GCallback callback, gpointer user_data)
{
auto found = m_pipeline_map.find(element_name);
if(found == m_pipeline_map.end())
return false;
GstElement* element = std::get<GstElement*>(found->second);
if(!GST_IS_ELEMENT(element))
return false;
g_signal_connect(G_OBJECT(element), signal_name.c_str(), G_CALLBACK (callback), user_data);
return true;
}
/**
@brief Sets the element property to the provided value
@param element elements name
@param propery_name the properties name the value will be set to
@param propery_value the value to set
@return true if the elements property was set correctly. Doesn't guaranteed the property was actually set because the API doesn't have any way to check
*/
template<typename T>
bool setElementProperty(const std::string& element_name, const std::string& property_name, T property_value)
{
auto found = m_pipeline_map.find(element_name);
if(found == m_pipeline_map.end())
return false;
GstElement* element = std::get<GstElement*>(found->second);
if(!GST_IS_ELEMENT(element))
return false;
g_object_set(G_OBJECT(element), property_name.c_str(), property_value, NULL);
return true;
}
/**
@brief Returns the GstElement by name
@param element_name the GstElement name
@returns See above
*/
GstElement* getElementByName(const std::string& element_name)
{
auto found = m_pipeline_map.find(element_name);
return found != m_pipeline_map.end() ? found->second.first : NULL;
}
/**
@brief Runs the main loop
*/
void runMainLoop()
{
g_main_loop_run(m_loop);
}
/**
@brief sets the element state
@param element_name
@param element_state
@returns if the state was set
*/
bool setElementState(const std::string& element_name, GstState state)
{
auto found = m_pipeline_map.find(element_name);
if(found == m_pipeline_map.end())
return false;
return gst_element_set_state(found->second.first, state) != GST_STATE_CHANGE_FAILURE;
}
/**
@brief sets the pipeline state
@param state pipeline state to set
@returns if the state was set
*/
bool setPipelineState(GstState state)
{
return gst_element_set_state(m_pipeline, state) != GST_STATE_CHANGE_FAILURE;
}
/**
@brief attaches the pipeline to a bin
@returns attached bin, or NULL if failed
*/
GstElement* attachToBin()
{
GstElement* bin = gst_bin_new(NULL);
if(!gst_bin_add(GST_BIN(bin), m_pipeline))
{
gst_object_unref(bin);
return NULL;
}
return bin;
}
};
static void onPadAdded(GstElement* element, GstPad* pad, GstElement* data)
{
// Link two Element with named pad
GstPad *sink_pad = gst_element_get_static_pad (GST_ELEMENT(data), "sink");
if(gst_pad_is_linked(sink_pad))
{
g_print("rtspsrc and depay are already linked. Ignoring\n");
return;
}
gst_element_link_pads(element, gst_pad_get_name(pad), GST_ELEMENT(data), "sink");
}
void runSimplePipeline(const std::string& location)
{
GStreamPipeline pipeline("RTSP_SERVER");
pipeline.addElement("rtspsrc","rtspsrc");
pipeline.addElement("rtph264depay", "videodepay");
pipeline.addElement("h264parse", "h264parse");
pipeline.addElement("avdec_h264", "videodecode");
pipeline.addElement("videoscale", "videoscale");
pipeline.addElement("videorate", "videorate");
pipeline.addElement("videoconvert", "videoconvert", "video/x-raw, format=(string)I420");
pipeline.addElement("autovideosink", "videosink");
pipeline.setElementProperty("rtspsrc", "location", location.c_str()); // location of the stream
pipeline.setElementProperty("rtspsrc", "protocols", GST_RTSP_LOWER_TRANS_UDP); // set the protocol
pipeline.setElementProperty("rtspsrc", "latency", 0); // latency set to min
pipeline.linkElementsByName({"videodepay", "h264parse", "videodecode", "videoscale", "videorate", "videoconvert", "videosink"});
pipeline.setElementSignal("rtspsrc", "pad-added", G_CALLBACK(onPadAdded), pipeline.getElementByName("videodepay"));
pipeline.setPipelineState(GST_STATE_PLAYING);
pipeline.runMainLoop();
}
int main(int argc, char* argv[])
{
assert(argc > 1 && "Provide location of the stream to the program EG: rtsp://192.168.68.52:8554/test");
runSimplePipeline(argv[1]);
}