summaryrefslogtreecommitdiff
path: root/ges/ges-transition-clip.c
blob: ef4754ee50c5d96c984f6be2f70aacc1e0bb6d39 (plain)
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
/* GStreamer Editing Services
 * Copyright (C) 2009 Edward Hervey <edward.hervey@collabora.co.uk>
 *               2009 Nokia Corporation
 *
 * 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: gestransitionclip
 * @title: GESTransitionClip
 * @short_description: Transition from one clip to another in a GESLayer
 *
 * Creates an object that mixes together the two underlying objects, A and B.
 * The A object is assumed to have a higher prioirity (lower number) than the
 * B object. At the transition in point, only A will be visible, and by the
 * end only B will be visible.
 *
 * The shape of the video transition depends on the value of the "vtype"
 * property. The default value is "crossfade". For audio, only "crossfade" is
 * supported.
 *
 * The ID of the ExtractableType is the nickname of the vtype property value. Note
 * that this value can be changed after creation and the GESExtractable.asset value
 * will be updated when needed.
 */

#include <ges/ges.h>
#include "ges-internal.h"

struct _GESTransitionClipPrivate
{
  GSList *video_transitions;

  const gchar *vtype_name;
};

enum
{
  PROP_VTYPE = 5,
};

static GESTrackElement *_create_track_element (GESClip
    * self, GESTrackType type);
static void _child_added (GESContainer * container,
    GESTimelineElement * element);
static void _child_removed (GESContainer * container,
    GESTimelineElement * element);

/* Internal methods */
static void
ges_transition_clip_update_vtype_internal (GESClip *
    self, GESVideoStandardTransitionType value, gboolean set_asset)
{
  GSList *tmp;
  guint index;
  GEnumClass *enum_class;
  const gchar *asset_id = NULL;
  GESTransitionClip *trself = GES_TRANSITION_CLIP (self);

  enum_class = g_type_class_peek (GES_VIDEO_STANDARD_TRANSITION_TYPE_TYPE);
  for (index = 0; index < enum_class->n_values; index++) {
    if (enum_class->values[index].value == value) {
      asset_id = enum_class->values[index].value_nick;
      break;
    }
  }

  if (asset_id == NULL) {
    GST_WARNING_OBJECT (self, "Wrong transition type value: %i can not set it",
        value);

    return;
  }

  for (tmp = trself->priv->video_transitions; tmp; tmp = tmp->next) {
    if (!ges_video_transition_set_transition_type
        (GES_VIDEO_TRANSITION (tmp->data), value))
      return;
  }

  trself->vtype = value;
  trself->priv->vtype_name = asset_id;

  if (set_asset) {
    /* We already checked the value, so we can be sure no error will accured */
    ges_extractable_set_asset (GES_EXTRACTABLE (self),
        ges_asset_request (GES_TYPE_TRANSITION_CLIP, asset_id, NULL));
  }
}

/* GESExtractable interface overrides */
static GParameter *
extractable_get_parameters_from_id (const gchar * id, guint * n_params)
{
  GEnumClass *enum_class =
      g_type_class_peek (GES_VIDEO_STANDARD_TRANSITION_TYPE_TYPE);
  GParameter *params = g_new0 (GParameter, 1);
  GEnumValue *value = g_enum_get_value_by_nick (enum_class, id);

  params[0].name = "vtype";
  g_value_init (&params[0].value, GES_VIDEO_STANDARD_TRANSITION_TYPE_TYPE);
  g_value_set_enum (&params[0].value, value->value);
  *n_params = 1;

  return params;
}

static gchar *
extractable_check_id (GType type, const gchar * id)
{
  guint index;
  GEnumClass *enum_class;
  enum_class = g_type_class_peek (GES_VIDEO_STANDARD_TRANSITION_TYPE_TYPE);

  for (index = 0; index < enum_class->n_values; index++) {
    if (g_strcmp0 (enum_class->values[index].value_nick, id) == 0)
      return g_strdup (id);
  }

  return NULL;
}

static gchar *
extractable_get_id (GESExtractable * self)
{
  guint index;
  GEnumClass *enum_class;
  guint value = GES_TRANSITION_CLIP (self)->vtype;

  enum_class = g_type_class_peek (GES_VIDEO_STANDARD_TRANSITION_TYPE_TYPE);
  for (index = 0; index < enum_class->n_values; index++) {
    if (enum_class->values[index].value == value)
      return g_strdup (enum_class->values[index].value_nick);
  }

  return NULL;
}

static gboolean
extractable_set_asset (GESExtractable * self, GESAsset * asset)
{
  GEnumClass *enum_class;
  GESVideoStandardTransitionType value;
  GESTransitionClip *trans = GES_TRANSITION_CLIP (self);
  const gchar *vtype = ges_asset_get_id (asset);

  if (!(ges_clip_get_supported_formats (GES_CLIP (self)) &
          GES_TRACK_TYPE_VIDEO)) {
    return FALSE;
  }

  /* Update the transition type if we actually changed it */
  if (g_strcmp0 (vtype, trans->priv->vtype_name)) {
    guint index;

    value = GES_VIDEO_STANDARD_TRANSITION_TYPE_CROSSFADE;
    enum_class = g_type_class_peek (GES_VIDEO_STANDARD_TRANSITION_TYPE_TYPE);

    /* Find the in value in use */
    for (index = 0; index < enum_class->n_values; index++) {
      if (g_strcmp0 (enum_class->values[index].value_nick, vtype) == 0) {
        value = enum_class->values[index].value;
        break;
      }
    }
    ges_transition_clip_update_vtype_internal (GES_CLIP (self), value, FALSE);
  }

  return TRUE;
}

static void
ges_extractable_interface_init (GESExtractableInterface * iface)
{
  iface->check_id = (GESExtractableCheckId) extractable_check_id;
  iface->get_id = extractable_get_id;
  iface->get_parameters_from_id = extractable_get_parameters_from_id;
  iface->can_update_asset = TRUE;
  iface->set_asset_full = extractable_set_asset;
}

G_DEFINE_TYPE_WITH_CODE (GESTransitionClip,
    ges_transition_clip, GES_TYPE_BASE_TRANSITION_CLIP,
    G_IMPLEMENT_INTERFACE (GES_TYPE_EXTRACTABLE,
        ges_extractable_interface_init));

static void
ges_transition_clip_get_property (GObject * object,
    guint property_id, GValue * value, GParamSpec * pspec)
{
  GESTransitionClip *self = GES_TRANSITION_CLIP (object);
  switch (property_id) {
    case PROP_VTYPE:
      g_value_set_enum (value, self->vtype);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
  }
}

static void
ges_transition_clip_set_property (GObject * object,
    guint property_id, const GValue * value, GParamSpec * pspec)
{
  GESClip *self = GES_CLIP (object);

  switch (property_id) {
    case PROP_VTYPE:
      ges_transition_clip_update_vtype_internal (self,
          g_value_get_enum (value), TRUE);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
  }
}

static void
ges_transition_clip_class_init (GESTransitionClipClass * klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  GESClipClass *timobj_class = GES_CLIP_CLASS (klass);
  GESContainerClass *container_class = GES_CONTAINER_CLASS (klass);

  g_type_class_add_private (klass, sizeof (GESTransitionClipPrivate));

  object_class->get_property = ges_transition_clip_get_property;
  object_class->set_property = ges_transition_clip_set_property;

  /**
   * GESTransitionClip:vtype:
   *
   * a #GESVideoStandardTransitionType representing the wipe to use
   */
  g_object_class_install_property (object_class, PROP_VTYPE,
      g_param_spec_enum ("vtype", "VType",
          "The SMPTE video wipe to use, or 0 for crossfade",
          GES_VIDEO_STANDARD_TRANSITION_TYPE_TYPE,
          GES_VIDEO_STANDARD_TRANSITION_TYPE_CROSSFADE,
          G_PARAM_READWRITE | G_PARAM_CONSTRUCT));

  container_class->child_added = _child_added;
  container_class->child_removed = _child_removed;

  timobj_class->create_track_element = _create_track_element;
}

static void
ges_transition_clip_init (GESTransitionClip * self)
{

  self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
      GES_TYPE_TRANSITION_CLIP, GESTransitionClipPrivate);

  self->vtype = GES_VIDEO_STANDARD_TRANSITION_TYPE_NONE;
  self->priv->vtype_name = NULL;
}

static void
_child_removed (GESContainer * container, GESTimelineElement * element)
{
  GESTransitionClipPrivate *priv = GES_TRANSITION_CLIP (container)->priv;

  /* If this is called, we should be sure the trackelement exists */
  if (GES_IS_VIDEO_TRANSITION (element)) {
    GST_DEBUG_OBJECT (container, "%" GST_PTR_FORMAT " removed", element);
    priv->video_transitions = g_slist_remove (priv->video_transitions, element);
    gst_object_unref (element);
  }
}

static void
_child_added (GESContainer * container, GESTimelineElement * element)
{
  GESTransitionClipPrivate *priv = GES_TRANSITION_CLIP (container)->priv;

  if (GES_IS_VIDEO_TRANSITION (element)) {
    GST_DEBUG_OBJECT (container, "%" GST_PTR_FORMAT " added", element);
    priv->video_transitions =
        g_slist_prepend (priv->video_transitions, gst_object_ref (element));
  }
}

static GESTrackElement *
_create_track_element (GESClip * clip, GESTrackType type)
{
  GESTransitionClip *transition = (GESTransitionClip *) clip;
  GESTrackElement *res = NULL;
  GESTrackType supportedformats;

  GST_DEBUG ("Creating a GESTransition");

  supportedformats = ges_clip_get_supported_formats (clip);
  if (type == GES_TRACK_TYPE_VIDEO) {
    if (supportedformats == GES_TRACK_TYPE_UNKNOWN ||
        supportedformats & GES_TRACK_TYPE_VIDEO) {
      GESVideoTransition *trans;

      trans = ges_video_transition_new ();
      ges_video_transition_set_transition_type (trans, transition->vtype);

      res = GES_TRACK_ELEMENT (trans);
    } else {
      GST_DEBUG ("Not creating transition as video track not on"
          " supportedformats");
    }

  } else if (type == GES_TRACK_TYPE_AUDIO) {

    if (supportedformats == GES_TRACK_TYPE_UNKNOWN ||
        supportedformats & GES_TRACK_TYPE_AUDIO)
      res = GES_TRACK_ELEMENT (ges_audio_transition_new ());
    else
      GST_DEBUG ("Not creating transition as audio track"
          " not on supportedformats");

  } else
    GST_WARNING ("Transitions don't handle this track type");

  return res;
}

/**
 * ges_transition_clip_new:
 * @vtype: the type of transition to create
 *
 * Creates a new #GESTransitionClip.
 *
 * Returns: (transfer floating) (nullable): a newly created #GESTransitionClip,
 * or %NULL if something went wrong.
 */
GESTransitionClip *
ges_transition_clip_new (GESVideoStandardTransitionType vtype)
{
  GEnumValue *value;
  GEnumClass *klass;
  GESTransitionClip *ret = NULL;

  klass =
      G_ENUM_CLASS (g_type_class_ref (GES_VIDEO_STANDARD_TRANSITION_TYPE_TYPE));
  if (!klass) {
    GST_ERROR ("Could not find the StandarTransitionType enum class");
    return NULL;
  }

  value = g_enum_get_value (klass, vtype);
  if (!value) {
    GST_ERROR ("Could not find enum value for %i", vtype);
    return NULL;
  }

  ret = ges_transition_clip_new_for_nick (((gchar *) value->value_nick));
  g_type_class_unref (klass);

  return ret;
}

/**
 * ges_transition_clip_new_for_nick:
 * @nick: a string representing the type of transition to create
 *
 * Creates a new #GESTransitionClip for the provided @nick.
 *
 * Returns: (transfer floating) (nullable): The newly created #GESTransitionClip,
 * or %NULL if something went wrong
 */

GESTransitionClip *
ges_transition_clip_new_for_nick (gchar * nick)
{
  GESTransitionClip *ret = NULL;
  GESAsset *asset = ges_asset_request (GES_TYPE_TRANSITION_CLIP, nick, NULL);

  if (asset != NULL) {
    ret = GES_TRANSITION_CLIP (ges_asset_extract (asset, NULL));

    gst_object_unref (asset);
  } else
    GST_WARNING ("No asset found for nick: %s", nick);

  return ret;
}