File: cheese-effect.c

package info (click to toggle)
cheese 44.1-5
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 5,312 kB
  • sloc: ansic: 6,621; xml: 293; sh: 183; python: 55; makefile: 21
file content (441 lines) | stat: -rw-r--r-- 12,249 bytes parent folder | download | duplicates (5)
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/*
 * Copyright © 2010 Yuvaraj Pandian T <yuvipanda@yuvi.in>
 *
 * Licensed under the GNU General Public License Version 2
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <gst/gst.h>

#include "cheese-effect.h"

/**
 * SECTION:cheese-effect
 * @short_description: An effect to apply to a video capture stream
 * @stability: Unstable
 * @include: cheese/cheese-effect.h
 *
 * #CheeseEffect provides an abstraction of an effect to apply to a stream
 * from a video capture device.
 */

enum
{
  PROP_O,
  PROP_NAME,
  PROP_PIPELINE_DESC,
  PROP_CONTROL_VALVE,
  PROP_LAST
};

static GParamSpec *properties[PROP_LAST];

typedef struct
{
  gchar *name;
  gchar *pipeline_desc;
  GstElement *control_valve;
} CheeseEffectPrivate;

G_DEFINE_TYPE_WITH_PRIVATE (CheeseEffect, cheese_effect, G_TYPE_OBJECT)

static void
cheese_effect_get_property (GObject *object, guint property_id,
                            GValue *value, GParamSpec *pspec)
{
    CheeseEffectPrivate *priv = cheese_effect_get_instance_private (CHEESE_EFFECT (object));

  switch (property_id)
  {
    case PROP_NAME:
      g_value_set_string (value, priv->name);
      break;
    case PROP_PIPELINE_DESC:
      g_value_set_string (value, priv->pipeline_desc);
      break;
    case PROP_CONTROL_VALVE:
      g_value_set_object (value, priv->control_valve);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
  }
}

static void
cheese_effect_set_property (GObject *object, guint property_id,
                            const GValue *value, GParamSpec *pspec)
{
    CheeseEffectPrivate *priv = cheese_effect_get_instance_private (CHEESE_EFFECT (object));

  switch (property_id)
  {
    case PROP_NAME:
      g_free (priv->name);
      priv->name = g_value_dup_string (value);
      break;
    case PROP_PIPELINE_DESC:
      g_free (priv->pipeline_desc);
      priv->pipeline_desc = g_value_dup_string (value);
      break;
    case PROP_CONTROL_VALVE:
      if (priv->control_valve != NULL)
        g_object_unref (G_OBJECT (priv->control_valve));
      priv->control_valve = GST_ELEMENT (g_value_get_object (value));
      g_object_ref (G_OBJECT (priv->control_valve));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
  }
}

static void
cheese_effect_finalize (GObject *object)
{
    CheeseEffect *effect;
    CheeseEffectPrivate *priv;

    effect = CHEESE_EFFECT (object);
    priv = cheese_effect_get_instance_private (effect);

    g_clear_pointer (&priv->name, g_free);
    g_clear_pointer (&priv->pipeline_desc, g_free);
    g_clear_pointer (&priv->control_valve, gst_object_unref);

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

static void
cheese_effect_class_init (CheeseEffectClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->get_property = cheese_effect_get_property;
  object_class->set_property = cheese_effect_set_property;
  object_class->finalize = cheese_effect_finalize;

  /**
   * CheeseEffect:name:
   *
   * Name of the effect, for display in a UI.
   */
  properties[PROP_NAME] = g_param_spec_string ("name",
                                               "Name",
                                               "Name of the effect",
                                               "",
                                               G_PARAM_READWRITE |
                                               G_PARAM_CONSTRUCT_ONLY |
                                               G_PARAM_STATIC_STRINGS);

  /**
   * CheeseEffect:pipeline-desc:
   *
   * Description of the GStreamer pipeline associated with the effect.
   */
  properties[PROP_PIPELINE_DESC] = g_param_spec_string ("pipeline-desc",
                                                        "Pipeline description",
                                                        "Description of the GStreamer pipeline associated with the effect",
                                                        "",
                                                        G_PARAM_READWRITE |
                                                        G_PARAM_CONSTRUCT_ONLY |
                                                        G_PARAM_STATIC_STRINGS);

  /**
   * CheeseEffect:control-valve:
   *
   * If the control valve is active, then the effect is currently connected to
   * a video stream, for previews.
   */
  properties[PROP_CONTROL_VALVE] = g_param_spec_object ("control-valve",
                                                        "Control valve",
                                                        "If the control valve is active, the effect is connected to a video stream",
                                                        GST_TYPE_ELEMENT,
                                                        G_PARAM_READWRITE |
                                                        G_PARAM_STATIC_STRINGS);

  g_object_class_install_properties (object_class, PROP_LAST, properties);
}


/**
 * cheese_effect_get_name:
 * @effect: a #CheeseEffect
 *
 * Get the human-readable name of the @effect.
 *
 * Returns: (transfer none): the human-readable name of the effect.
 */
const gchar *
cheese_effect_get_name (CheeseEffect *effect)
{
    CheeseEffectPrivate *priv;

  g_return_val_if_fail (CHEESE_IS_EFFECT (effect), NULL);

    priv = cheese_effect_get_instance_private (effect);

    return priv->name;
}

/**
 * cheese_effect_get_pipeline_desc:
 * @effect: a #CheeseEffect
 *
 * Get the Gstreamer pipeline description of the @effect.
 *
 * Returns: (transfer none): the Gstreamer pipeline description of the effect.
 */
const gchar *
cheese_effect_get_pipeline_desc (CheeseEffect *effect)
{
    CheeseEffectPrivate *priv;

  g_return_val_if_fail (CHEESE_IS_EFFECT (effect), NULL);

    priv = cheese_effect_get_instance_private (effect);

    return priv->pipeline_desc;
}

/**
 * cheese_effect_is_preview_connected:
 * @effect: a #CheeseEffect
 *
 * Get whether the @effect is connected to a video stream, for previews.
 *
 * Returns: %TRUE if the preview is connected, %FALSE otherwise.
 */
gboolean
cheese_effect_is_preview_connected (CheeseEffect *effect)
{
    CheeseEffectPrivate *priv;

  g_return_val_if_fail (CHEESE_IS_EFFECT (effect), FALSE);

    priv = cheese_effect_get_instance_private (effect);

    return priv->control_valve != NULL;
}

/**
 * cheese_effect_enable_preview:
 * @effect: the #CheeseEffect to enable the preview of
 *
 * Enable the preview of a #CheeseEffect.
 */
void
cheese_effect_enable_preview (CheeseEffect *effect)
{
    CheeseEffectPrivate *priv;

  g_return_if_fail (CHEESE_IS_EFFECT (effect));

    priv = cheese_effect_get_instance_private (effect);

    g_object_set (G_OBJECT (priv->control_valve), "drop", FALSE, NULL);
}

/**
 * cheese_effect_disable_preview:
 * @effect: the #CheeseEffect to disable the preview of
 *
 * Disable the preview of a #CheeseEffect.
 */
void
cheese_effect_disable_preview (CheeseEffect *effect)
{
    CheeseEffectPrivate *priv;

  g_return_if_fail (CHEESE_IS_EFFECT (effect));

    priv = cheese_effect_get_instance_private (effect);

    g_object_set (G_OBJECT (priv->control_valve), "drop", TRUE, NULL);
}

static void
cheese_effect_init (CheeseEffect *self)
{
}

/**
 * cheese_effect_new:
 * @name: name of the effect
 * @pipeline_desc: GStreamer pipeline of the new effect
 *
 * Create a new #CheeseEffect.
 *
 * Returns: (transfer full): a new #CheeseEffect
 */
CheeseEffect *
cheese_effect_new (const gchar *name, const gchar *pipeline_desc)
{
  return g_object_new (CHEESE_TYPE_EFFECT,
                       "name", name,
                       "pipeline-desc", pipeline_desc,
                       NULL);
}

/**
 * cheese_effect_load_from_file:
 * @filename: (type filename): name of the file containing the effect
 * specification
 *
 * Load effect from file.
 *
 * Returns: (transfer full): a #CheeseEffect, or %NULL on error
 */
CheeseEffect*
cheese_effect_load_from_file (const gchar *filename)
{
  const gchar GROUP_NAME[] = "Effect";
  gchar        *name, *desc;
  GError       *err = NULL;
  CheeseEffect *effect = NULL;
  GKeyFile     *keyfile = g_key_file_new ();

  g_key_file_load_from_file (keyfile, filename, G_KEY_FILE_NONE, &err);
  if (err != NULL)
    goto err_keyfile_load;

  name = g_key_file_get_locale_string (keyfile, GROUP_NAME, "Name", NULL, &err);
  if (err != NULL)
    goto err_name;

  desc = g_key_file_get_string (keyfile, GROUP_NAME, "PipelineDescription", &err);
  if (err != NULL)
    goto err_desc;

  g_key_file_free (keyfile);

  effect = cheese_effect_new (name, desc);
  g_free (name);
  g_free (desc);

  return effect;

err_desc:
    g_free (name);
err_name:
err_keyfile_load:
    g_key_file_free (keyfile);
    g_warning ("CheeseEffect: couldn't load file %s: %s", filename, err->message);
    g_clear_error (&err);
    return NULL;
}

/*
 * cheese_effect_load_effects_from_directory:
 * @directory: the directory in which to search for effects
 *
 * Only parses files ending with the '.effects' extension.
 *
 * Returns: (element-type Cheese.Effect) (transfer full): list of effects
 * loaded from files from @directory, or %NULL if any errors were encountered
 */
static GList*
cheese_effect_load_effects_from_directory (const gchar* directory)
{
  gboolean is_dir;
  GError  *err = NULL;
  GDir    *dir = NULL;
  GList   *list = NULL;

  is_dir = g_file_test (directory, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR);
  if (!is_dir)
    return NULL;

  dir = g_dir_open (directory, (guint) 0, &err);
  if (err != NULL)
  {
    g_warning ("CheeseEffect: g_dir_open: %s\n", err->message);
    g_clear_error (&err);
    return NULL;
  }

  while (TRUE) {
    CheeseEffect *effect;
    gchar        *abs_path;
    const gchar  *filename = g_dir_read_name (dir);

    /* no more files */
    if (filename == NULL)
      break;

    if (!g_str_has_suffix (filename, ".effect"))
      continue;

    abs_path = g_build_filename (directory, filename, NULL);
    effect = cheese_effect_load_from_file (abs_path);
    if (effect != NULL)
      list = g_list_prepend (list, effect);
    g_free (abs_path);
  }
  g_dir_close (dir);

  return g_list_reverse (list);
}

/*
 * cheese_effect_load_effects_from_subdirectory:
 * @directory: directory from under which to load effects
 *
 * Get a list of effects from the gnome-video-effects subdirectory under
 * @directory.
 *
 * Returns: (element-type Cheese.Effect) (transfer full): a list of
 * #CheeseEffect, or %NULL upon failure
 */
static GList*
cheese_effect_load_effects_from_subdirectory (const gchar* directory)
{
  GList *list;
  gchar *path = g_build_filename (directory, "gnome-video-effects", NULL);
  list = cheese_effect_load_effects_from_directory (path);
  g_free (path);
  return list;
}

/**
 * cheese_effect_load_effects:
 *
 * Load effects from standard directories, including the user's data directory.
 *
 * Returns: (element-type Cheese.Effect) (transfer full): a list of
 * #CheeseEffect, or %NULL if no effects could be found
 */
GList*
cheese_effect_load_effects (void)
{
  const gchar * const *data_dirs, *dir;
  GList *effect_list = NULL, *l;

  dir = g_get_user_data_dir (); /* value returned owned by GLib */
  l = cheese_effect_load_effects_from_subdirectory (dir);
  effect_list = g_list_concat (effect_list, l);

  data_dirs = g_get_system_data_dirs (); /* value returned owned by GLib */
  if (!data_dirs)
    return effect_list;

  while (*data_dirs)
  {
    dir = *data_dirs;
    l = cheese_effect_load_effects_from_subdirectory (dir);
    effect_list = g_list_concat (effect_list, l);
    data_dirs++;
  }

  return effect_list;
}