QtGStreamer  0.10.2
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
structure.cpp
00001 /*
00002     Copyright (C) 2009-2010  George Kiagiadakis <kiagiadakis.george@gmail.com>
00003     Copyright (C) 2010  Collabora Multimedia.
00004       @author Mauricio Piacentini <mauricio.piacentini@collabora.co.uk>
00005     Copyright (C) 2011  Collabora Ltd.
00006       @author George Kiagiadakis <george.kiagiadakis@collabora.co.uk>
00007 
00008     This library is free software; you can redistribute it and/or modify
00009     it under the terms of the GNU Lesser General Public License as published
00010     by the Free Software Foundation; either version 2.1 of the License, or
00011     (at your option) any later version.
00012 
00013     This program is distributed in the hope that it will be useful,
00014     but WITHOUT ANY WARRANTY; without even the implied warranty of
00015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016     GNU Lesser General Public License for more details.
00017 
00018     You should have received a copy of the GNU Lesser General Public License
00019     along with this program.  If not, see <http://www.gnu.org/licenses/>.
00020 */
00021 #include "structure.h"
00022 #include "miniobject.h"
00023 #include "caps.h"
00024 #include "../QGlib/string_p.h"
00025 #include <gst/gststructure.h>
00026 #include <QtCore/QDebug>
00027 
00028 namespace QGst {
00029 
00030 #ifndef DOXYGEN_RUN
00031 
00032 struct QTGSTREAMER_NO_EXPORT Structure::Data : public QSharedData
00033 {
00034     Data() : QSharedData(), structure(NULL) {}
00035     Data(const Data & other);
00036     virtual ~Data();
00037 
00038     GstStructure *structure;
00039 };
00040 
00041 Structure::Data::Data(const Structure::Data & other)
00042     : QSharedData(other), structure(NULL)
00043 {
00044     if (other.structure) {
00045         structure = gst_structure_copy(other.structure);
00046     }
00047 }
00048 
00049 Structure::Data::~Data()
00050 {
00051     if (structure) {
00052         gst_structure_free(structure);
00053     }
00054 }
00055 
00056 #endif //DOXYGEN_RUN
00057 
00058 Structure::Structure()
00059     : d(new Data)
00060 {
00061 }
00062 
00063 Structure::Structure(Data* data)
00064     : d(data)
00065 {
00066 }
00067 
00068 Structure::Structure(const char *name)
00069     : d(new Data)
00070 {
00071     d->structure = gst_structure_empty_new(name);
00072 }
00073 
00074 Structure::Structure(const GstStructure* structure)
00075     : d(new Data)
00076 {
00077     d->structure = gst_structure_copy(structure);
00078 }
00079 
00080 Structure::Structure(const Structure & other)
00081     : d(other.d)
00082 {
00083 }
00084 
00085 Structure::~Structure()
00086 {
00087 }
00088 
00089 Structure & Structure::operator=(const Structure & other)
00090 {
00091     d = other.d;
00092     return *this;
00093 }
00094 
00095 bool Structure::isValid() const
00096 {
00097     return d->structure != NULL;
00098 }
00099 
00100 QString Structure::name() const
00101 {
00102     if (d->structure) {
00103         return QString::fromUtf8(gst_structure_get_name(d->structure));
00104     } else {
00105         return QString();
00106     }
00107 }
00108 
00109 void Structure::setName(const char *name)
00110 {
00111     if (!d->structure) {
00112         //lazy construction
00113         d->structure = gst_structure_empty_new(name);
00114     } else {
00115         gst_structure_set_name(d->structure, name);
00116     }
00117 }
00118 
00119 QGlib::Value Structure::value(const char *fieldName) const
00120 {
00121     if (d->structure) {
00122         return QGlib::Value(gst_structure_get_value(d->structure, fieldName));
00123     } else {
00124         return QGlib::Value();
00125     }
00126 }
00127 
00128 void Structure::setValue(const char *fieldName, const QGlib::Value & value)
00129 {
00130     Q_ASSERT(isValid());
00131     gst_structure_set_value(d->structure, fieldName, value);
00132 }
00133 
00134 unsigned int Structure::numberOfFields() const
00135 {
00136     return d->structure ? gst_structure_n_fields(d->structure) : 0;
00137 }
00138 
00139 QString Structure::fieldName(unsigned int fieldNumber) const
00140 {
00141     if (fieldNumber < numberOfFields()) {
00142         return QString::fromUtf8(gst_structure_nth_field_name(d->structure, fieldNumber));
00143     } else {
00144         return QString();
00145     }
00146 }
00147 
00148 QGlib::Type Structure::fieldType(const char *fieldName) const
00149 {
00150     if (d->structure) {
00151         return gst_structure_get_field_type(d->structure, fieldName);
00152     } else {
00153         return QGlib::Type::Invalid;
00154     }
00155 }
00156 
00157 bool Structure::hasField(const char *fieldName) const
00158 {
00159     return d->structure ? gst_structure_has_field(d->structure, fieldName) : false;
00160 }
00161 
00162 bool Structure::hasFieldTyped(const char *fieldName, QGlib::Type type) const
00163 {
00164     return d->structure ? gst_structure_has_field_typed(d->structure, fieldName, type) : false;
00165 }
00166 
00167 void Structure::removeField(const char *fieldName)
00168 {
00169     if (d->structure) {
00170         gst_structure_remove_field(d->structure, fieldName);
00171     }
00172 }
00173 
00174 void Structure::removeAllFields()
00175 {
00176     if (d->structure) {
00177         gst_structure_remove_all_fields(d->structure);
00178     }
00179 }
00180 
00181 QString Structure::toString() const
00182 {
00183     if (d->structure) {
00184         return QGlib::Private::stringFromGCharPtr(gst_structure_to_string(d->structure));
00185     } else {
00186         return QString();
00187     }
00188 }
00189 
00190 Structure Structure::fromString(const char *str)
00191 {
00192     //we don't use the Structure(const GstStructure*) constructor to avoid copying
00193     Structure s;
00194     s.d->structure = gst_structure_from_string(str, NULL);
00195     return s;
00196 }
00197 
00198 Structure::operator GstStructure*()
00199 {
00200     return d->structure;
00201 }
00202 
00203 Structure::operator const GstStructure*() const
00204 {
00205     return d->structure;
00206 }
00207 
00208 //END Structure
00209 
00210 //BEGIN SharedStructure
00211 
00212 #ifndef DOXYGEN_RUN
00213 
00214 struct QTGSTREAMER_NO_EXPORT SharedStructure::Data : public Structure::Data
00215 {
00216     Data() : Structure::Data() {}
00217     Data(const Data & other) : Structure::Data(other) {}
00218 
00219     MiniObjectPtr miniobject;
00220     CapsPtr caps;
00221 };
00222 
00223 #endif
00224 
00225 Structure SharedStructure::copy() const
00226 {
00227     return Structure(d->structure);
00228 }
00229 
00230 SharedStructure::SharedStructure(SharedStructure::Data* data)
00231     : Structure(data)
00232 {
00233 }
00234 
00235 StructurePtr SharedStructure::fromMiniObject(GstStructure *structure, const MiniObjectPtr & parent)
00236 {
00237     SharedStructure::Data *d = new SharedStructure::Data;
00238     d->structure = structure;
00239     d->miniobject = parent;
00240     return StructurePtr(new SharedStructure(d));
00241 }
00242 
00243 StructurePtr SharedStructure::fromCaps(GstStructure* structure, const CapsPtr & parent)
00244 {
00245     SharedStructure::Data *d = new SharedStructure::Data;
00246     d->structure = structure;
00247     d->caps = parent;
00248     return StructurePtr(new SharedStructure(d));
00249 }
00250 
00251 SharedStructure::~SharedStructure()
00252 {
00253     d->structure = NULL;
00254 }
00255 
00256 //END SharedStructure
00257 
00258 QDebug operator<<(QDebug debug, const Structure & structure)
00259 {
00260     debug.nospace() << "QGst::Structure";
00261     if (structure.isValid()) {
00262         debug.nospace() << "(" << structure.toString() << ")";
00263     } else {
00264         debug.nospace() << "(<invalid>)";
00265     }
00266     return debug.space();
00267 }
00268 
00269 } //namespace QGst