File: gstqroverlay.c

package info (click to toggle)
gst-plugins-bad1.0 1.26.4-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 69,744 kB
  • sloc: ansic: 723,080; cpp: 277,142; objc: 3,556; xml: 3,351; sh: 1,035; python: 473; makefile: 175; java: 75
file content (208 lines) | stat: -rw-r--r-- 5,593 bytes parent folder | download | duplicates (4)
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
/*
 * GStreamer
 * Copyright (C) 2006 Stefan Kost <ensonic@users.sf.net>
 * Copyright (c) 2020 Anthony Violo <anthony.violo@ubicast.eu>
 * Copyright (c) 2020 Thibault Saunier <tsaunier@igalia.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/**
 * SECTION:element-qroverlay
 *
 * Element to set random data on a qroverlay.
 *
 * ## Example launch line
 *
 * ``` bash
 * gst-launch -v -m videotestsrc ! qroverlay ! fakesink silent=TRUE
 * ```
 * Since 1.22 the `qroverlay` element also supports a #GstCustomMeta called
 * `GstQROverlayMeta` which allows upstream elements to set the data to be
 * rendered on the buffers that flow through it. This custom meta
 * #GstStructure has the following fields:
 *
 * * #gchar* `data` (**mandatory**): The data to use to render the qrcode.
 * * #gboolean `keep_data` (**mandatory**): Set to %TRUE if the data from that
 *   metadata should be used as #qroverlay:data
 *
 *
 * Since: 1.20
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <gst/gst.h>
#include <gst/base/gstbasetransform.h>
#include <json-glib/json-glib.h>

#include <qrencode.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#include "gstqroverlayelements.h"
#include "gstqroverlay.h"

enum
{
  PROP_0,
  PROP_DATA,
};

struct _GstQROverlay
{
  GstBaseQROverlay parent;
  gchar *data;

  gboolean data_changed;
};

#define gst_qr_overlay_parent_class parent_class
G_DEFINE_TYPE (GstQROverlay, gst_qr_overlay, GST_TYPE_BASE_QR_OVERLAY);
GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (qroverlay, "qroverlay", GST_RANK_NONE,
    GST_TYPE_QR_OVERLAY, qroverlay_element_init (plugin));

static gchar *
get_qrcode_content (GstBaseQROverlay * base, GstBuffer * buf,
    GstVideoInfo * info, gboolean * reuse_prev)
{
  gchar *content;
  GstQROverlay *self = GST_QR_OVERLAY (base);

  GstCustomMeta *meta = gst_buffer_get_custom_meta (buf, "GstQROverlayMeta");
  if (meta) {
    gchar *data;

    if (gst_structure_get (meta->structure, "data", G_TYPE_STRING, &data, NULL)) {
      gboolean keep_data;

      GST_OBJECT_LOCK (self);
      self->data_changed = TRUE;
      if (gst_structure_get_boolean (meta->structure, "keep_data", &keep_data)
          && keep_data) {
        g_free (self->data);
        self->data = g_strdup (self->data);
      }
      GST_OBJECT_UNLOCK (self);

      *reuse_prev = FALSE;

      return data;
    }

    GST_WARNING_OBJECT (self,
        "Got a GstQROverlayMeta without a 'data' field in its struct");
  }


  GST_OBJECT_LOCK (self);
  content = g_strdup (self->data);
  *reuse_prev = !self->data_changed;
  self->data_changed = FALSE;
  GST_OBJECT_UNLOCK (self);

  return content;
}

static void
gst_qr_overlay_finalize (GObject * object)
{
  GstQROverlay *self = GST_QR_OVERLAY (object);

  g_free (self->data);

  G_OBJECT_CLASS (parent_class)->finalize (object);
}

static void
gst_qr_overlay_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec)
{
  GstQROverlay *self = GST_QR_OVERLAY (object);

  switch (prop_id) {
    case PROP_DATA:
      GST_OBJECT_LOCK (self);
      g_free (self->data);
      self->data = g_value_dup_string (value);
      self->data_changed = TRUE;
      GST_OBJECT_UNLOCK (self);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void
gst_qr_overlay_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec)
{
  GstQROverlay *self = GST_QR_OVERLAY (object);

  switch (prop_id) {
    case PROP_DATA:
      g_value_set_string (value, self->data);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void
gst_qr_overlay_class_init (GstQROverlayClass * klass)
{
  GObjectClass *gobject_class;
  GstElementClass *gstelement_class;

  gobject_class = (GObjectClass *) klass;
  gstelement_class = (GstElementClass *) klass;

  gobject_class->finalize = gst_qr_overlay_finalize;
  gobject_class->set_property = gst_qr_overlay_set_property;
  gobject_class->get_property = gst_qr_overlay_get_property;

  gst_element_class_set_details_simple (gstelement_class,
      "qroverlay",
      "Qrcode overlay containing random data",
      "Overlay Qrcodes over each buffer with data passed in",
      "Thibault Saunier <tsaunier@igalia.com>");

  gst_meta_register_custom_simple ("GstQROverlayMeta");

  g_object_class_install_property (gobject_class,
      PROP_DATA, g_param_spec_string ("data",
          "Data",
          "Data to write in the QRCode to be overlaid",
          NULL,
          G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
          GST_PARAM_CONTROLLABLE));

  GST_BASE_QR_OVERLAY_CLASS (klass)->get_content =
      GST_DEBUG_FUNCPTR (get_qrcode_content);
}

/* initialize the new element
 * initialize instance structure
 */
static void
gst_qr_overlay_init (GstQROverlay * filter)
{
}