File: ges-video-test-source.c

package info (click to toggle)
gstreamer-editing-services1.0 1.26.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 5,192 kB
  • sloc: ansic: 63,500; python: 4,167; lex: 57; makefile: 28; cpp: 20
file content (333 lines) | stat: -rw-r--r-- 9,236 bytes parent folder | download | duplicates (3)
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
/* GStreamer Editing Services
 * Copyright (C) 2010 Brandon Lewis <brandon.lewis@collabora.co.uk>
 *               2010 Nokia Corporation
 * Copyright (C) 2020 Igalia S.L
 *     Author: 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., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

/**
 * SECTION:gesvideotestsource
 * @title: GESVideoTestSource
 * @short_description: produce solid colors and patterns, possibly with a time
 * overlay.
 */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "ges-internal.h"
#include "ges-track-element.h"
#include "ges-video-test-source.h"

#define DEFAULT_VPATTERN GES_VIDEO_TEST_PATTERN_SMPTE

struct _GESVideoTestSourcePrivate
{
  GESVideoTestPattern pattern;

  GstElement *capsfilter;
};

static void
ges_extractable_interface_init (GESExtractableInterface * iface)
{
  iface->check_id = ges_test_source_asset_check_id;
  iface->asset_type = GES_TYPE_TRACK_ELEMENT_ASSET;
}

static GstStructure *
ges_video_test_source_asset_get_config (GESAsset * asset)
{
  const gchar *id = ges_asset_get_id (asset);
  if (g_strcmp0 (id, g_type_name (ges_asset_get_extractable_type (asset))))
    return gst_structure_from_string (ges_asset_get_id (asset), NULL);

  return NULL;
}

G_DEFINE_TYPE_WITH_CODE (GESVideoTestSource, ges_video_test_source,
    GES_TYPE_VIDEO_SOURCE, G_ADD_PRIVATE (GESVideoTestSource)
    G_IMPLEMENT_INTERFACE (GES_TYPE_EXTRACTABLE,
        ges_extractable_interface_init));

static GstElement *ges_video_test_source_create_source (GESSource * source);

static gboolean
get_natural_size (GESVideoSource * source, gint * width, gint * height)
{
  gboolean res = FALSE;
  GESTimelineElement *parent = GES_TIMELINE_ELEMENT_PARENT (source);

  if (parent) {
    GESAsset *asset = ges_extractable_get_asset (GES_EXTRACTABLE (parent));

    if (asset)
      res = ges_test_clip_asset_get_natural_size (asset, width, height);
  }

  if (!res) {
    *width = DEFAULT_WIDTH;
    *height = DEFAULT_HEIGHT;
  }

  return TRUE;
}

static gboolean
_set_parent (GESTimelineElement * element, GESTimelineElement * parent)
{
  gint width, height, fps_n, fps_d;
  GstCaps *caps;
  GESVideoTestSource *self = GES_VIDEO_TEST_SOURCE (element);


  if (!parent)
    goto done;

  g_assert (self->priv->capsfilter);
  /* Setting the parent ourself as we need it to get the natural size */
  element->parent = parent;
  if (!ges_video_source_get_natural_size (GES_VIDEO_SOURCE (self), &width,
          &height)) {
    width = DEFAULT_WIDTH;
    height = DEFAULT_HEIGHT;
  }

  if (!ges_timeline_element_get_natural_framerate (parent, &fps_n, &fps_d)) {
    fps_n = DEFAULT_FRAMERATE_N;
    fps_d = DEFAULT_FRAMERATE_D;
  }

  caps = gst_caps_new_simple ("video/x-raw",
      "width", G_TYPE_INT, width,
      "height", G_TYPE_INT, height,
      "framerate", GST_TYPE_FRACTION, fps_n, fps_d, NULL);
  g_object_set (self->priv->capsfilter, "caps", caps, NULL);
  gst_caps_unref (caps);

done:
  return
      GES_TIMELINE_ELEMENT_CLASS
      (ges_video_test_source_parent_class)->set_parent (element, parent);
}

static gboolean
_get_natural_framerate (GESTimelineElement * element, gint * fps_n,
    gint * fps_d)
{
  gboolean res = FALSE;
  GESTimelineElement *parent = GES_TIMELINE_ELEMENT_PARENT (element);

  if (parent) {
    GESAsset *asset = ges_extractable_get_asset (GES_EXTRACTABLE (parent));

    if (asset) {
      res =
          ges_clip_asset_get_natural_framerate (GES_CLIP_ASSET (asset), fps_n,
          fps_d);
    }
  }

  if (!res) {
    *fps_n = DEFAULT_FRAMERATE_N;
    *fps_d = DEFAULT_FRAMERATE_D;
  }

  return TRUE;
}

static void
dispose (GObject * object)
{
  G_OBJECT_CLASS (ges_video_test_source_parent_class)->dispose (object);
}

static void
ges_video_test_source_class_init (GESVideoTestSourceClass * klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  GESVideoSourceClass *vsource_class = GES_VIDEO_SOURCE_CLASS (klass);
  GESSourceClass *source_class = GES_SOURCE_CLASS (klass);

  source_class->create_source = ges_video_test_source_create_source;
  vsource_class->ABI.abi.get_natural_size = get_natural_size;

  object_class->dispose = dispose;

  GES_TIMELINE_ELEMENT_CLASS (klass)->set_parent = _set_parent;
  GES_TIMELINE_ELEMENT_CLASS (klass)->get_natural_framerate =
      _get_natural_framerate;
}

static void
ges_video_test_source_init (GESVideoTestSource * self)
{
  self->priv = ges_video_test_source_get_instance_private (self);

  self->priv->pattern = DEFAULT_VPATTERN;
}

static GstStructure *
ges_video_test_source_get_config (GESVideoTestSource * self)
{
  GESAsset *asset = ges_extractable_get_asset (GES_EXTRACTABLE (self));
  if (asset)
    return ges_video_test_source_asset_get_config (asset);

  return NULL;
}

static GstElement *
ges_video_test_source_create_overlay (GESVideoTestSource * self)
{
  const gchar *bindesc = NULL;
  GstStructure *config = ges_video_test_source_get_config (self);

  if (!config)
    return NULL;

  if (gst_structure_has_name (config, "time-overlay")) {
    gboolean disable_timecodestamper;

    if (gst_structure_get_boolean (config, "disable-timecodestamper",
            &disable_timecodestamper))
      bindesc = "timeoverlay";
    else
      bindesc = "timecodestamper ! timeoverlay";
  }
  gst_structure_free (config);

  if (!bindesc)
    return NULL;

  return gst_parse_bin_from_description (bindesc, TRUE, NULL);
}

static GstElement *
ges_video_test_source_create_source (GESSource * source)
{
  GstCaps *caps;
  gint pattern;
  GstElement *testsrc, *res;
  GstElement *overlay;
  const gchar *props[] =
      { "pattern", "background-color", "foreground-color", NULL };
  GPtrArray *elements;
  GESVideoTestSource *self = GES_VIDEO_TEST_SOURCE (source);
  GESTrackElement *element = GES_TRACK_ELEMENT (source);

  g_assert (!GES_TIMELINE_ELEMENT_PARENT (source));
  testsrc = gst_element_factory_make ("videotestsrc", NULL);
  self->priv->capsfilter = gst_element_factory_make ("capsfilter", NULL);
  pattern = self->priv->pattern;

  g_object_set (testsrc, "pattern", pattern, NULL);

  elements = g_ptr_array_new ();
  g_ptr_array_add (elements, self->priv->capsfilter);
  caps = gst_caps_new_simple ("video/x-raw",
      "width", G_TYPE_INT, DEFAULT_WIDTH,
      "height", G_TYPE_INT, DEFAULT_HEIGHT,
      "framerate", GST_TYPE_FRACTION, DEFAULT_FRAMERATE_N, DEFAULT_FRAMERATE_D,
      NULL);
  g_object_set (self->priv->capsfilter, "caps", caps, NULL);
  gst_caps_unref (caps);

  overlay = ges_video_test_source_create_overlay (self);
  if (overlay) {
    const gchar *overlay_props[] =
        { "time-mode", "text-y", "text-x", "text-width", "test-height",
      "halignment", "valignment", "font-desc", NULL
    };

    ges_track_element_add_children_props (element, overlay, NULL,
        NULL, overlay_props);
    g_ptr_array_add (elements, overlay);
  }

  ges_track_element_add_children_props (element, testsrc, NULL, NULL, props);

  res = ges_source_create_topbin (GES_SOURCE (element), "videotestsrc", testsrc,
      elements);

  return res;
}

/**
 * ges_video_test_source_set_pattern:
 * @self: a #GESVideoTestSource
 * @pattern: a #GESVideoTestPattern
 *
 * Sets the source to use the given @pattern.
 */
void
ges_video_test_source_set_pattern (GESVideoTestSource
    * self, GESVideoTestPattern pattern)
{
  GstElement *element =
      ges_track_element_get_element (GES_TRACK_ELEMENT (self));

  self->priv->pattern = pattern;

  if (element) {
    GValue val = { 0 };

    g_value_init (&val, GES_VIDEO_TEST_PATTERN_TYPE);
    g_value_set_enum (&val, pattern);
    ges_track_element_set_child_property (GES_TRACK_ELEMENT (self), "pattern",
        &val);
  }
}

/**
 * ges_video_test_source_get_pattern:
 * @source: a #GESVideoTestPattern
 *
 * Get the video pattern used by the @source.
 *
 * Returns: The video pattern used by the @source.
 */
GESVideoTestPattern
ges_video_test_source_get_pattern (GESVideoTestSource * source)
{
  GValue val = { 0 };

  ges_track_element_get_child_property (GES_TRACK_ELEMENT (source), "pattern",
      &val);
  return g_value_get_enum (&val);
}

/**
 * ges_video_test_source_new:
 *
 * Creates a new #GESVideoTestSource.
 *
 * Returns: (transfer floating) (nullable): The newly created
 * #GESVideoTestSource, or %NULL if there was an error.
 */
GESVideoTestSource *
ges_video_test_source_new (void)
{
  GESVideoTestSource *res;
  GESAsset *asset = ges_asset_request (GES_TYPE_VIDEO_TEST_SOURCE, NULL, NULL);

  res = GES_VIDEO_TEST_SOURCE (ges_asset_extract (asset, NULL));
  gst_object_unref (asset);

  return res;
}