QtGStreamer  1.2.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
structure.cpp
1 /*
2  Copyright (C) 2009-2010 George Kiagiadakis <kiagiadakis.george@gmail.com>
3  Copyright (C) 2010 Collabora Multimedia.
4  @author Mauricio Piacentini <mauricio.piacentini@collabora.co.uk>
5  Copyright (C) 2011 Collabora Ltd.
6  @author George Kiagiadakis <george.kiagiadakis@collabora.co.uk>
7 
8  This library is free software; you can redistribute it and/or modify
9  it under the terms of the GNU Lesser General Public License as published
10  by the Free Software Foundation; either version 2.1 of the License, or
11  (at your option) any later version.
12 
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public License
19  along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21 #include "structure.h"
22 #include "miniobject.h"
23 #include "caps.h"
24 #include "../QGlib/string_p.h"
25 #include <gst/gst.h>
26 #include <QtCore/QDebug>
27 
28 namespace QGst {
29 
30 #ifndef DOXYGEN_RUN
31 
32 struct QTGSTREAMER_NO_EXPORT Structure::Data : public QSharedData
33 {
34  Data() : QSharedData(), structure(NULL) {}
35  Data(const Data & other);
36  virtual ~Data();
37 
38  GstStructure *structure;
39 };
40 
41 Structure::Data::Data(const Structure::Data & other)
42  : QSharedData(other), structure(NULL)
43 {
44  if (other.structure) {
45  structure = gst_structure_copy(other.structure);
46  }
47 }
48 
49 Structure::Data::~Data()
50 {
51  if (structure) {
52  gst_structure_free(structure);
53  }
54 }
55 
56 #endif //DOXYGEN_RUN
57 
58 Structure::Structure()
59  : d(new Data)
60 {
61 }
62 
63 Structure::Structure(Data* data)
64  : d(data)
65 {
66 }
67 
68 Structure::Structure(const char *name)
69  : d(new Data)
70 {
71  d->structure = gst_structure_new_empty(name);
72 }
73 
74 Structure::Structure(const GstStructure* structure)
75  : d(new Data)
76 {
77  d->structure = gst_structure_copy(structure);
78 }
79 
80 Structure::Structure(const Structure & other)
81  : d(other.d)
82 {
83 }
84 
85 Structure::~Structure()
86 {
87 }
88 
89 Structure & Structure::operator=(const Structure & other)
90 {
91  d = other.d;
92  return *this;
93 }
94 
95 bool Structure::isValid() const
96 {
97  return d->structure != NULL;
98 }
99 
100 QString Structure::name() const
101 {
102  if (d->structure) {
103  return QString::fromUtf8(gst_structure_get_name(d->structure));
104  } else {
105  return QString();
106  }
107 }
108 
109 void Structure::setName(const char *name)
110 {
111  if (!d->structure) {
112  //lazy construction
113  d->structure = gst_structure_new_empty(name);
114  } else {
115  gst_structure_set_name(d->structure, name);
116  }
117 }
118 
119 QGlib::Value Structure::value(const char *fieldName) const
120 {
121  if (d->structure) {
122  return QGlib::Value(gst_structure_get_value(d->structure, fieldName));
123  } else {
124  return QGlib::Value();
125  }
126 }
127 
128 void Structure::setValue(const char *fieldName, const QGlib::Value & value)
129 {
130  Q_ASSERT(isValid());
131  gst_structure_set_value(d->structure, fieldName, value);
132 }
133 
134 unsigned int Structure::numberOfFields() const
135 {
136  return d->structure ? gst_structure_n_fields(d->structure) : 0;
137 }
138 
139 QString Structure::fieldName(unsigned int fieldNumber) const
140 {
141  if (fieldNumber < numberOfFields()) {
142  return QString::fromUtf8(gst_structure_nth_field_name(d->structure, fieldNumber));
143  } else {
144  return QString();
145  }
146 }
147 
148 QGlib::Type Structure::fieldType(const char *fieldName) const
149 {
150  if (d->structure) {
151  return gst_structure_get_field_type(d->structure, fieldName);
152  } else {
153  return QGlib::Type::Invalid;
154  }
155 }
156 
157 bool Structure::hasField(const char *fieldName) const
158 {
159  return d->structure ? gst_structure_has_field(d->structure, fieldName) : false;
160 }
161 
162 bool Structure::hasFieldTyped(const char *fieldName, QGlib::Type type) const
163 {
164  return d->structure ? gst_structure_has_field_typed(d->structure, fieldName, type) : false;
165 }
166 
167 void Structure::removeField(const char *fieldName)
168 {
169  if (d->structure) {
170  gst_structure_remove_field(d->structure, fieldName);
171  }
172 }
173 
174 void Structure::removeAllFields()
175 {
176  if (d->structure) {
177  gst_structure_remove_all_fields(d->structure);
178  }
179 }
180 
181 QString Structure::toString() const
182 {
183  if (d->structure) {
184  return QGlib::Private::stringFromGCharPtr(gst_structure_to_string(d->structure));
185  } else {
186  return QString();
187  }
188 }
189 
190 Structure Structure::fromString(const char *str)
191 {
192  //we don't use the Structure(const GstStructure*) constructor to avoid copying
193  Structure s;
194  s.d->structure = gst_structure_from_string(str, NULL);
195  return s;
196 }
197 
198 Structure::operator GstStructure*()
199 {
200  return d->structure;
201 }
202 
203 Structure::operator const GstStructure*() const
204 {
205  return d->structure;
206 }
207 
208 //END Structure
209 
210 //BEGIN SharedStructure
211 
212 #ifndef DOXYGEN_RUN
213 
214 struct QTGSTREAMER_NO_EXPORT SharedStructure::Data : public Structure::Data
215 {
216  Data() : Structure::Data() {}
217  Data(const Data & other) : Structure::Data(other) {}
218 
219  MiniObjectPtr miniobject;
220  CapsPtr caps;
221 };
222 
223 #endif
224 
225 Structure SharedStructure::copy() const
226 {
227  return Structure(d->structure);
228 }
229 
230 SharedStructure::SharedStructure(SharedStructure::Data* data)
231  : Structure(data)
232 {
233 }
234 
235 StructurePtr SharedStructure::fromMiniObject(GstStructure *structure, const MiniObjectPtr & parent)
236 {
237  SharedStructure::Data *d = new SharedStructure::Data;
238  d->structure = structure;
239  d->miniobject = parent;
240  return StructurePtr(new SharedStructure(d));
241 }
242 
243 StructurePtr SharedStructure::fromCaps(GstStructure* structure, const CapsPtr & parent)
244 {
245  SharedStructure::Data *d = new SharedStructure::Data;
246  d->structure = structure;
247  d->caps = parent;
248  return StructurePtr(new SharedStructure(d));
249 }
250 
251 SharedStructure::~SharedStructure()
252 {
253  d->structure = NULL;
254 }
255 
256 //END SharedStructure
257 
258 QDebug operator<<(QDebug debug, const Structure & structure)
259 {
260  debug.nospace() << "QGst::Structure";
261  if (structure.isValid()) {
262  debug.nospace() << "(" << structure.toString() << ")";
263  } else {
264  debug.nospace() << "(<invalid>)";
265  }
266  return debug.space();
267 }
268 
269 } //namespace QGst
Wrapper class for GValue.
Definition: value.h:74
Wrapper for GstStructure.
Definition: structure.h:49
Wrapper class for GType.
Definition: type.h:63