docs: fix typo s/incomming/incoming/
[platform/upstream/gst-plugins-good.git] / gst / debugutils / gsttaginject.c
1 /* GStreamer
2  * Copyright (C) 2008 Stefan Kost <ensonic@users.sf.net>
3  *
4  * gsttaginject.c:
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21 /**
22  * SECTION:element-taginject
23  *
24  * Element that injects new metadata tags, but passes incoming data through
25  * unmodified.
26  *
27  * <refsect2>
28  * <title>Example launch lines</title>
29  * |[
30  * gst-launch-1.0 audiotestsrc num-buffers=100 ! taginject tags="title=testsrc,artist=gstreamer" ! vorbisenc ! oggmux ! filesink location=test.ogg
31  * ]| set title and artist
32  * |[
33  * gst-launch-1.0 audiotestsrc num-buffers=100 ! taginject tags="keywords=\{\"testone\",\"audio\"\},title=\"audio\ testtone\"" ! vorbisenc ! oggmux ! filesink location=test.ogg
34  * ]| set keywords and title demonstrating quoting of special chars and handling lists
35  * </refsect2>
36  */
37
38 #ifdef HAVE_CONFIG_H
39 #  include "config.h"
40 #endif
41
42 #include <stdlib.h>
43
44 #include "gsttaginject.h"
45
46 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
47     GST_PAD_SINK,
48     GST_PAD_ALWAYS,
49     GST_STATIC_CAPS_ANY);
50
51 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
52     GST_PAD_SRC,
53     GST_PAD_ALWAYS,
54     GST_STATIC_CAPS_ANY);
55
56 GST_DEBUG_CATEGORY_STATIC (gst_tag_inject_debug);
57 #define GST_CAT_DEFAULT gst_tag_inject_debug
58
59 enum
60 {
61   PROP_TAGS = 1
62 };
63
64
65 #define gst_tag_inject_parent_class parent_class
66 G_DEFINE_TYPE (GstTagInject, gst_tag_inject, GST_TYPE_BASE_TRANSFORM);
67
68 static void gst_tag_inject_finalize (GObject * object);
69 static void gst_tag_inject_set_property (GObject * object, guint prop_id,
70     const GValue * value, GParamSpec * pspec);
71 static void gst_tag_inject_get_property (GObject * object, guint prop_id,
72     GValue * value, GParamSpec * pspec);
73
74 static GstFlowReturn gst_tag_inject_transform_ip (GstBaseTransform * trans,
75     GstBuffer * buf);
76 static gboolean gst_tag_inject_start (GstBaseTransform * trans);
77
78
79 static void
80 gst_tag_inject_finalize (GObject * object)
81 {
82   GstTagInject *self = GST_TAG_INJECT (object);
83
84   if (self->tags) {
85     gst_tag_list_unref (self->tags);
86     self->tags = NULL;
87   }
88
89   G_OBJECT_CLASS (parent_class)->finalize (object);
90 }
91
92 static void
93 gst_tag_inject_class_init (GstTagInjectClass * klass)
94 {
95   GObjectClass *gobject_class;
96   GstElementClass *gstelement_class;
97   GstBaseTransformClass *gstbasetrans_class;
98
99   gobject_class = G_OBJECT_CLASS (klass);
100   gstelement_class = GST_ELEMENT_CLASS (klass);
101   gstbasetrans_class = GST_BASE_TRANSFORM_CLASS (klass);
102
103   GST_DEBUG_CATEGORY_INIT (gst_tag_inject_debug, "taginject", 0,
104       "tag inject element");
105
106   gobject_class->set_property = gst_tag_inject_set_property;
107   gobject_class->get_property = gst_tag_inject_get_property;
108
109   g_object_class_install_property (gobject_class, PROP_TAGS,
110       g_param_spec_string ("tags", "taglist",
111           "List of tags to inject into the target file",
112           NULL, G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS));
113
114   gobject_class->finalize = gst_tag_inject_finalize;
115
116   gst_element_class_set_static_metadata (gstelement_class,
117       "TagInject",
118       "Generic", "inject metadata tags", "Stefan Kost <ensonic@users.sf.net>");
119   gst_element_class_add_static_pad_template (gstelement_class, &srctemplate);
120   gst_element_class_add_static_pad_template (gstelement_class, &sinktemplate);
121
122   gstbasetrans_class->transform_ip =
123       GST_DEBUG_FUNCPTR (gst_tag_inject_transform_ip);
124
125   gstbasetrans_class->start = GST_DEBUG_FUNCPTR (gst_tag_inject_start);
126 }
127
128 static void
129 gst_tag_inject_init (GstTagInject * self)
130 {
131   GstBaseTransform *trans = GST_BASE_TRANSFORM (self);
132
133   gst_base_transform_set_gap_aware (trans, TRUE);
134
135   self->tags = NULL;
136 }
137
138 static GstFlowReturn
139 gst_tag_inject_transform_ip (GstBaseTransform * trans, GstBuffer * buf)
140 {
141   GstTagInject *self = GST_TAG_INJECT (trans);
142
143   if (G_UNLIKELY (!self->tags_sent)) {
144     self->tags_sent = TRUE;
145     /* send tags */
146     if (self->tags && !gst_tag_list_is_empty (self->tags)) {
147       GST_DEBUG ("tag event :%" GST_PTR_FORMAT, self->tags);
148       gst_pad_push_event (GST_BASE_TRANSFORM_SRC_PAD (trans),
149           gst_event_new_tag (gst_tag_list_ref (self->tags)));
150     }
151   }
152
153   return GST_FLOW_OK;
154 }
155
156 static void
157 gst_tag_inject_set_property (GObject * object, guint prop_id,
158     const GValue * value, GParamSpec * pspec)
159 {
160   GstTagInject *self = GST_TAG_INJECT (object);
161
162   switch (prop_id) {
163     case PROP_TAGS:{
164       gchar *structure =
165           g_strdup_printf ("taglist,%s", g_value_get_string (value));
166       if (!(self->tags = gst_tag_list_new_from_string (structure))) {
167         GST_WARNING ("unparsable taglist = '%s'", structure);
168       }
169
170       /* make sure that tags will be send */
171       self->tags_sent = FALSE;
172       g_free (structure);
173       break;
174     }
175     default:
176       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
177       break;
178   }
179 }
180
181 static void
182 gst_tag_inject_get_property (GObject * object, guint prop_id, GValue * value,
183     GParamSpec * pspec)
184 {
185   /*GstTagInject *self = GST_TAG_INJECT (object); */
186
187   switch (prop_id) {
188     default:
189       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
190       break;
191   }
192 }
193
194 static gboolean
195 gst_tag_inject_start (GstBaseTransform * trans)
196 {
197   GstTagInject *self = GST_TAG_INJECT (trans);
198
199   /* we need to sent tags _transform_ip() once */
200   self->tags_sent = FALSE;
201
202   return TRUE;
203 }