This repository has been archived by the owner on Jul 12, 2023. It is now read-only.
/
GStreamerFilterImpl.cpp
276 lines (229 loc) · 8.16 KB
/
GStreamerFilterImpl.cpp
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
/*
* (C) Copyright 2016 Kurento (http://kurento.org/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <gst/gst.h>
#include "MediaPipeline.hpp"
#include "FilterType.hpp"
#include <GStreamerFilterImplFactory.hpp>
#include "GStreamerFilterImpl.hpp"
#include <jsonrpc/JsonSerializer.hpp>
#include <KurentoException.hpp>
#include <gst/gst.h>
#include <commons/kms-core-enumtypes.h>
#include <algorithm>
#include <sstream>
#include <stdexcept>
#include <string>
#define GST_CAT_DEFAULT kurento_gstreamer_filter_impl
GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
#define GST_DEFAULT_NAME "KurentoGStreamerFilterImpl"
namespace kurento
{
static void string2enum (const GValue *src_value, GValue *dst_value);
GStreamerFilterImpl::GStreamerFilterImpl (const boost::property_tree::ptree
&conf, std::shared_ptr<MediaPipeline>
mediaPipeline, const std::string &command,
std::shared_ptr<FilterType> filterType) : FilterImpl (conf,
std::dynamic_pointer_cast<MediaObjectImpl> ( mediaPipeline) )
{
GstElement *filter, *filter_check;
GError *error = nullptr;
this->cmd = command;
GST_DEBUG ("Command %s", command.c_str() );
switch (filterType->getValue() ) {
case FilterType::VIDEO:
g_object_set (element, "type", 2, NULL);
break;
case FilterType::AUDIO:
g_object_set (element, "type", 1, NULL);
break;
case FilterType::AUTODETECT:
g_object_set (element, "type", 0, NULL);
break;
default:
break;
}
filter = gst_parse_launch (command.c_str(), &error);
if (filter == nullptr || error != nullptr) {
std::string error_str = "GStreamer element cannot be created";
if (filter) {
g_object_unref (filter);
}
if (error != nullptr) {
if (error->message != nullptr) {
error_str += ": " + std::string (error->message);
}
g_error_free (error);
}
throw KurentoException (MARSHALL_ERROR, error_str);
} else if (GST_IS_BIN (filter) ) {
g_object_unref (filter);
throw KurentoException (MARSHALL_ERROR,
"Given command is not valid, only one element can be created");
}
g_object_set (element, "filter", filter, NULL);
g_object_get (element, "filter", &filter_check, NULL);
if (filter_check != filter) {
g_object_unref (filter);
g_object_unref (filter_check);
throw KurentoException (MARSHALL_ERROR,
"Given command is not valid, pad templates don't match");
}
g_object_unref (filter);
g_object_unref (filter_check);
// No ref held; will be released by the pipeline
gstElement = filter;
// Used by method setElementProperty() when the property is an enum
g_value_register_transform_func (G_TYPE_STRING, G_TYPE_ENUM, string2enum);
}
MediaObjectImpl *
GStreamerFilterImplFactory::createObject (const boost::property_tree::ptree
&conf, std::shared_ptr<MediaPipeline>
mediaPipeline, const std::string &command,
std::shared_ptr<FilterType> filterType) const
{
return new GStreamerFilterImpl (conf, mediaPipeline, command, filterType);
}
std::string GStreamerFilterImpl::getCommand ()
{
return this->cmd;
}
static void
string2enum (const GValue *src_value, GValue *dst_value)
{
// Find and set the requested enum value among all possible ones
//
// See:
// - https://developer.gnome.org/gobject/stable/gobject-Enumeration-and-Flag-Types.html
// - https://developer.gnome.org/gobject/stable/gobject-Generic-values.html
// - https://developer.gnome.org/gobject/stable/gobject-Standard-Parameter-and-Value-Types.html
const gchar *src_string = g_value_get_string (src_value);
const GType enum_type = G_VALUE_TYPE (dst_value);
const GEnumClass *enum_class = G_ENUM_CLASS (g_type_class_ref (enum_type));
const GEnumValue *enum_values = enum_class->values;
gboolean found = FALSE;
for (guint i = 0; i < enum_class->n_values; ++i) {
if (g_strcmp0 (src_string, enum_values[i].value_nick) == 0) {
found = TRUE;
g_value_set_enum (dst_value, enum_values[i].value);
break;
}
}
if (!found) {
gchar *message = g_strdup_printf ("Invalid value for enum %s",
G_VALUE_TYPE_NAME(dst_value));
throw std::invalid_argument(message);
g_free (message);
}
}
void GStreamerFilterImpl::setElementProperty(const std::string &propertyName,
const std::string &propertyValue)
{
// Get the property _type_ from the GStreamer element
const char* property_name = propertyName.c_str();
GParamSpec *pspec = g_object_class_find_property (
G_OBJECT_GET_CLASS (gstElement), property_name);
if (pspec == NULL) {
std::ostringstream oss;
oss << "No property named '" << property_name << "' in object '"
<< GST_ELEMENT_NAME (gstElement) << "'";
std::string message = oss.str();
GST_WARNING ("%s", message.c_str());
throw KurentoException (MARSHALL_ERROR, message);
}
// Convert the input string to the correct value type
GValue value = G_VALUE_INIT;
g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE(pspec));
if (G_IS_PARAM_SPEC_INT (pspec)) {
gint converted = 0;
try {
converted = std::stoi (propertyValue);
}
catch (std::exception &ex) {
std::ostringstream oss;
oss << "Cannot convert '" << propertyValue << "' to int: " << ex.what();
std::string message = oss.str();
GST_WARNING ("%s", message.c_str());
throw KurentoException (MARSHALL_ERROR, message);
}
g_value_set_int (&value, converted);
}
else if (G_IS_PARAM_SPEC_FLOAT (pspec)) {
gfloat converted = 0.0f;
try {
converted = std::stof (propertyValue);
}
catch (std::exception &ex) {
std::ostringstream oss;
oss << "Cannot convert '" << propertyValue << "' to float: " << ex.what();
std::string message = oss.str();
GST_WARNING ("%s", message.c_str());
throw KurentoException (MARSHALL_ERROR, message);
}
g_value_set_float (&value, converted);
}
else if (G_IS_PARAM_SPEC_DOUBLE (pspec)) {
gdouble converted = 0.0;
try {
converted = std::stod (propertyValue);
}
catch (std::exception &ex) {
std::ostringstream oss;
oss << "Cannot convert '" << propertyValue << "' to double: " << ex.what();
std::string message = oss.str();
GST_WARNING ("%s", message.c_str());
throw KurentoException (MARSHALL_ERROR, message);
}
g_value_set_double (&value, converted);
}
else if (G_IS_PARAM_SPEC_ENUM (pspec)) {
// Source type: string
GValue src_value = G_VALUE_INIT;
g_value_init (&src_value, G_TYPE_STRING);
g_value_set_static_string (&src_value, propertyValue.c_str());
// Destination type: enum
try {
g_value_transform (&src_value, &value);
}
catch (std::exception &ex) {
std::ostringstream oss;
oss << "Cannot convert '" << propertyValue << "' to enum: " << ex.what();
std::string message = oss.str();
GST_WARNING ("%s", message.c_str());
throw KurentoException (MARSHALL_ERROR, message);
}
}
else if (G_IS_PARAM_SPEC_STRING (pspec)) {
g_value_set_static_string (&value, propertyValue.c_str());
}
// else if (...) { Add here whatever types are needed }
else {
std::ostringstream oss;
oss << "Property type not implemented: " << G_PARAM_SPEC_TYPE_NAME (pspec);
std::string message = oss.str();
GST_WARNING ("%s", message.c_str());
throw KurentoException (NOT_IMPLEMENTED, message);
}
g_object_set_property (G_OBJECT (gstElement), property_name, &value);
g_value_unset (&value);
}
GStreamerFilterImpl::StaticConstructor GStreamerFilterImpl::staticConstructor;
GStreamerFilterImpl::StaticConstructor::StaticConstructor()
{
GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, GST_DEFAULT_NAME, 0,
GST_DEFAULT_NAME);
}
} /* kurento */