File: nleoperation.c

package info (click to toggle)
gstreamer-editing-services1.0 1.14.4-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 8,388 kB
  • sloc: ansic: 43,396; sh: 4,773; perl: 1,583; makefile: 1,172; python: 581; xml: 80; lex: 44
file content (845 lines) | stat: -rw-r--r-- 22,480 bytes parent folder | download | duplicates (2)
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
/* GStreamer
 * Copyright (C) 2001 Wim Taymans <wim.taymans@gmail.com>
 *               2004-2008 Edward Hervey <bilboed@bilboed.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.
 */


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

#include "nle.h"

/**
 * SECTION:element-nleoperation
 *
 * <refsect2>
 * <para>
 * A NleOperation performs a transformation or mixing operation on the
 * data from one or more #NleSources, which is used to implement filters or 
 * effects.
 * </para>
 * </refsect2>
 */

static GstStaticPadTemplate nle_operation_src_template =
GST_STATIC_PAD_TEMPLATE ("src",
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS_ANY);

static GstStaticPadTemplate nle_operation_sink_template =
GST_STATIC_PAD_TEMPLATE ("sink%d",
    GST_PAD_SINK,
    GST_PAD_REQUEST,
    GST_STATIC_CAPS_ANY);

GST_DEBUG_CATEGORY_STATIC (nleoperation);
#define GST_CAT_DEFAULT nleoperation

#define _do_init \
  GST_DEBUG_CATEGORY_INIT (nleoperation, "nleoperation", GST_DEBUG_FG_BLUE | GST_DEBUG_BOLD, "GNonLin Operation element");
#define nle_operation_parent_class parent_class
G_DEFINE_TYPE_WITH_CODE (NleOperation, nle_operation, NLE_TYPE_OBJECT,
    _do_init);

enum
{
  ARG_0,
  ARG_SINKS,
};

enum
{
  INPUT_PRIORITY_CHANGED,
  LAST_SIGNAL
};

static guint nle_operation_signals[LAST_SIGNAL] = { 0 };

static void nle_operation_dispose (GObject * object);

static void nle_operation_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec);
static void nle_operation_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec);

static gboolean nle_operation_prepare (NleObject * object);
static gboolean nle_operation_cleanup (NleObject * object);

static gboolean nle_operation_add_element (GstBin * bin, GstElement * element);
static gboolean nle_operation_remove_element (GstBin * bin,
    GstElement * element);

static GstPad *nle_operation_request_new_pad (GstElement * element,
    GstPadTemplate * templ, const gchar * name, const GstCaps * caps);
static void nle_operation_release_pad (GstElement * element, GstPad * pad);

static void synchronize_sinks (NleOperation * operation);
static gboolean remove_sink_pad (NleOperation * operation, GstPad * sinkpad);


static gboolean
nle_operation_send_event (GstElement * element, GstEvent * event)
{
  gboolean res = TRUE;

  switch (GST_EVENT_TYPE (event)) {
    case GST_EVENT_SEEK:
      nle_object_seek_all_children (NLE_OBJECT (element), event);
      break;
    default:
      res = GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
      break;
  }

  return res;
}

static void
nle_operation_class_init (NleOperationClass * klass)
{
  GObjectClass *gobject_class = (GObjectClass *) klass;
  GstBinClass *gstbin_class = (GstBinClass *) klass;

  GstElementClass *gstelement_class = (GstElementClass *) klass;
  NleObjectClass *nleobject_class = (NleObjectClass *) klass;

  gst_element_class_set_static_metadata (gstelement_class, "GNonLin Operation",
      "Filter/Editor",
      "Encapsulates filters/effects for use with NLE Objects",
      "Wim Taymans <wim.taymans@gmail.com>, Edward Hervey <bilboed@bilboed.com>");

  gobject_class->dispose = GST_DEBUG_FUNCPTR (nle_operation_dispose);

  gobject_class->set_property = GST_DEBUG_FUNCPTR (nle_operation_set_property);
  gobject_class->get_property = GST_DEBUG_FUNCPTR (nle_operation_get_property);

  /**
   * NleOperation:sinks:
   *
   * Specifies the number of sink pads the operation should provide.
   * If the sinks property is -1 (the default) pads are only created as
   * demanded via get_request_pad() calls on the element.
   */
  g_object_class_install_property (gobject_class, ARG_SINKS,
      g_param_spec_int ("sinks", "Sinks",
          "Number of input sinks (-1 for automatic handling)", -1, G_MAXINT, -1,
          G_PARAM_READWRITE));

  /**
   * NleOperation:input-priority-changed:
   * @pad: The operation's input pad whose priority changed.
   * @priority: The new priority
   *
   * Signals that the @priority of the stream being fed to the given @pad
   * might have changed.
   */
  nle_operation_signals[INPUT_PRIORITY_CHANGED] =
      g_signal_new ("input-priority-changed", G_TYPE_FROM_CLASS (klass),
      G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (NleOperationClass,
          input_priority_changed), NULL, NULL, g_cclosure_marshal_generic,
      G_TYPE_NONE, 2, GST_TYPE_PAD, G_TYPE_UINT);

  gstelement_class->request_new_pad =
      GST_DEBUG_FUNCPTR (nle_operation_request_new_pad);
  gstelement_class->release_pad = GST_DEBUG_FUNCPTR (nle_operation_release_pad);
  gstelement_class->send_event = GST_DEBUG_FUNCPTR (nle_operation_send_event);

  gstbin_class->add_element = GST_DEBUG_FUNCPTR (nle_operation_add_element);
  gstbin_class->remove_element =
      GST_DEBUG_FUNCPTR (nle_operation_remove_element);

  nleobject_class->prepare = GST_DEBUG_FUNCPTR (nle_operation_prepare);
  nleobject_class->cleanup = GST_DEBUG_FUNCPTR (nle_operation_cleanup);

  gst_element_class_add_static_pad_template (gstelement_class,
      &nle_operation_src_template);
  gst_element_class_add_static_pad_template (gstelement_class,
      &nle_operation_sink_template);
}

static void
nle_operation_dispose (GObject * object)
{
  NleOperation *oper = (NleOperation *) object;

  GST_DEBUG_OBJECT (object, "Disposing of source pad");

  nle_object_ghost_pad_set_target (NLE_OBJECT (object),
      NLE_OBJECT (object)->srcpad, NULL);

  GST_DEBUG_OBJECT (object, "Disposing of sink pad(s)");
  while (oper->sinks) {
    GstPad *ghost = (GstPad *) oper->sinks->data;
    remove_sink_pad (oper, ghost);
  }

  GST_DEBUG_OBJECT (object, "Done, calling parent class ::dispose()");
  G_OBJECT_CLASS (parent_class)->dispose (object);
}

static void
nle_operation_reset (NleOperation * operation)
{
  operation->num_sinks = 1;
  operation->realsinks = 0;
  operation->next_base_time = 0;
}

static void
nle_operation_init (NleOperation * operation)
{
  GST_OBJECT_FLAG_SET (operation, NLE_OBJECT_OPERATION);
  nle_operation_reset (operation);
  operation->element = NULL;
}

static gboolean
element_is_valid_filter (GstElement * element, gboolean * isdynamic)
{
  gboolean havesink = FALSE;
  gboolean havesrc = FALSE;
  gboolean done = FALSE;
  GstIterator *pads;
  GValue item = { 0, };

  if (isdynamic)
    *isdynamic = FALSE;

  pads = gst_element_iterate_pads (element);

  while (!done) {
    switch (gst_iterator_next (pads, &item)) {
      case GST_ITERATOR_OK:
      {
        GstPad *pad = g_value_get_object (&item);

        if (gst_pad_get_direction (pad) == GST_PAD_SRC)
          havesrc = TRUE;
        else if (gst_pad_get_direction (pad) == GST_PAD_SINK)
          havesink = TRUE;

        g_value_reset (&item);
        break;
      }
      case GST_ITERATOR_RESYNC:
        gst_iterator_resync (pads);
        havesrc = FALSE;
        havesink = FALSE;
        break;
      default:
        /* ERROR and DONE */
        done = TRUE;
        break;
    }
  }

  g_value_unset (&item);
  gst_iterator_free (pads);

  /* just look at the element's class, not the factory, since there might
   * not be a factory (in case of python elements) or the factory is the
   * wrong one (in case of a GstBin sub-class) and doesn't have complete
   * information. */
  {
    GList *tmp =
        gst_element_class_get_pad_template_list (GST_ELEMENT_GET_CLASS
        (element));

    while (tmp) {
      GstPadTemplate *template = (GstPadTemplate *) tmp->data;

      if (template->direction == GST_PAD_SRC)
        havesrc = TRUE;
      else if (template->direction == GST_PAD_SINK) {
        if (!havesink && (template->presence == GST_PAD_REQUEST) && isdynamic)
          *isdynamic = TRUE;
        havesink = TRUE;
      }
      tmp = tmp->next;
    }
  }
  return (havesink && havesrc);
}

/*
 * get_src_pad:
 * element: a #GstElement
 *
 * Returns: The src pad for the given element. A reference was added to the
 * returned pad, remove it when you don't need that pad anymore.
 * Returns NULL if there's no source pad.
 */

static GstPad *
get_src_pad (GstElement * element)
{
  GstIterator *it;
  GstIteratorResult itres;
  GValue item = { 0, };
  GstPad *srcpad = NULL;

  it = gst_element_iterate_src_pads (element);
  itres = gst_iterator_next (it, &item);
  if (itres != GST_ITERATOR_OK) {
    GST_DEBUG ("%s doesn't have a src pad !", GST_ELEMENT_NAME (element));
  } else {
    srcpad = g_value_get_object (&item);
    gst_object_ref (srcpad);
  }
  g_value_reset (&item);
  gst_iterator_free (it);

  return srcpad;
}

/* get_nb_static_sinks:
 * 
 * Returns : The number of static sink pads of the controlled element.
 */
static guint
get_nb_static_sinks (NleOperation * oper)
{
  GstIterator *sinkpads;
  gboolean done = FALSE;
  guint nbsinks = 0;
  GValue item = { 0, };

  sinkpads = gst_element_iterate_sink_pads (oper->element);

  while (!done) {
    switch (gst_iterator_next (sinkpads, &item)) {
      case GST_ITERATOR_OK:{
        nbsinks++;
        g_value_unset (&item);
      }
        break;
      case GST_ITERATOR_RESYNC:
        nbsinks = 0;
        gst_iterator_resync (sinkpads);
        break;
      default:
        /* ERROR and DONE */
        done = TRUE;
        break;
    }
  }

  g_value_reset (&item);
  gst_iterator_free (sinkpads);

  GST_DEBUG ("We found %d static sinks", nbsinks);

  return nbsinks;
}

static gboolean
nle_operation_add_element (GstBin * bin, GstElement * element)
{
  NleOperation *operation = (NleOperation *) bin;
  gboolean res = FALSE;
  gboolean isdynamic;

  GST_DEBUG_OBJECT (bin, "element:%s", GST_ELEMENT_NAME (element));

  if (operation->element) {
    GST_WARNING_OBJECT (operation,
        "We already control an element : %s , remove it first",
        GST_OBJECT_NAME (operation->element));
  } else {
    if (!element_is_valid_filter (element, &isdynamic)) {
      GST_WARNING_OBJECT (operation,
          "Element %s is not a valid filter element",
          GST_ELEMENT_NAME (element));
    } else {
      if ((res = GST_BIN_CLASS (parent_class)->add_element (bin, element))) {
        GstPad *srcpad;

        srcpad = get_src_pad (element);
        if (!srcpad)
          return FALSE;

        operation->element = element;
        operation->dynamicsinks = isdynamic;

        nle_object_ghost_pad_set_target (NLE_OBJECT (operation),
            NLE_OBJECT (operation)->srcpad, srcpad);

        /* Remove the reference get_src_pad gave us */
        gst_object_unref (srcpad);

        /* Figure out number of static sink pads */
        operation->num_sinks = get_nb_static_sinks (operation);

        /* Finally sync the ghostpads with the real pads */
        synchronize_sinks (operation);
      }
    }
  }

  return res;
}

static gboolean
nle_operation_remove_element (GstBin * bin, GstElement * element)
{
  NleOperation *operation = (NleOperation *) bin;
  gboolean res = FALSE;

  if (operation->element) {
    if ((res = GST_BIN_CLASS (parent_class)->remove_element (bin, element)))
      operation->element = NULL;
  } else {
    GST_WARNING_OBJECT (bin,
        "Element %s is not the one controlled by this operation",
        GST_ELEMENT_NAME (element));
  }
  return res;
}

static void
nle_operation_set_sinks (NleOperation * operation, guint sinks)
{
  /* FIXME : Check if sinkpad of element is on-demand .... */

  operation->num_sinks = sinks;
  synchronize_sinks (operation);
}

static void
nle_operation_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec)
{
  NleOperation *operation = (NleOperation *) object;

  switch (prop_id) {
    case ARG_SINKS:
      nle_operation_set_sinks (operation, g_value_get_int (value));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void
nle_operation_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec)
{
  NleOperation *operation = (NleOperation *) object;

  switch (prop_id) {
    case ARG_SINKS:
      g_value_set_int (value, operation->num_sinks);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}


/*
 * Returns the first unused sink pad of the controlled element.
 * Only use with static element. Unref after usage.
 * Returns NULL if there's no more unused sink pads.
 */
static GstPad *
get_unused_static_sink_pad (NleOperation * operation)
{
  GstIterator *pads;
  gboolean done = FALSE;
  GValue item = { 0, };
  GstPad *ret = NULL;

  if (!operation->element)
    return NULL;

  pads = gst_element_iterate_pads (operation->element);

  while (!done) {
    switch (gst_iterator_next (pads, &item)) {
      case GST_ITERATOR_OK:
      {
        GstPad *pad = g_value_get_object (&item);

        if (gst_pad_get_direction (pad) == GST_PAD_SINK) {
          GList *tmp;
          gboolean istaken = FALSE;

          /* 1. figure out if one of our sink ghostpads has this pad as target */
          for (tmp = operation->sinks; tmp; tmp = tmp->next) {
            GstGhostPad *gpad = (GstGhostPad *) tmp->data;
            GstPad *target = gst_ghost_pad_get_target (gpad);

            GST_LOG ("found ghostpad with target %s:%s",
                GST_DEBUG_PAD_NAME (target));

            if (target) {
              if (target == pad)
                istaken = TRUE;
              gst_object_unref (target);
            }
          }

          /* 2. if not taken, return that pad */
          if (!istaken) {
            gst_object_ref (pad);
            ret = pad;
            done = TRUE;
          }
        }
        g_value_reset (&item);
        break;
      }
      case GST_ITERATOR_RESYNC:
        gst_iterator_resync (pads);
        break;
      default:
        /* ERROR and DONE */
        done = TRUE;
        break;
    }
  }

  g_value_unset (&item);
  gst_iterator_free (pads);

  if (ret)
    GST_DEBUG_OBJECT (operation, "found free sink pad %s:%s",
        GST_DEBUG_PAD_NAME (ret));
  else
    GST_DEBUG_OBJECT (operation, "Couldn't find an unused sink pad");

  return ret;
}

GstPad *
get_unlinked_sink_ghost_pad (NleOperation * operation)
{
  GstIterator *pads;
  gboolean done = FALSE;
  GValue item = { 0, };
  GstPad *ret = NULL;

  if (!operation->element)
    return NULL;

  pads = gst_element_iterate_sink_pads ((GstElement *) operation);

  while (!done) {
    switch (gst_iterator_next (pads, &item)) {
      case GST_ITERATOR_OK:
      {
        GstPad *pad = g_value_get_object (&item);
        GstPad *peer = gst_pad_get_peer (pad);

        if (peer == NULL) {
          ret = pad;
          gst_object_ref (ret);
          done = TRUE;
        } else {
          gst_object_unref (peer);
        }
        g_value_reset (&item);
        break;
      }
      case GST_ITERATOR_RESYNC:
        gst_iterator_resync (pads);
        break;
      default:
        /* ERROR and DONE */
        done = TRUE;
        break;
    }
  }

  g_value_unset (&item);
  gst_iterator_free (pads);

  if (ret)
    GST_DEBUG_OBJECT (operation, "found unlinked ghost sink pad %s:%s",
        GST_DEBUG_PAD_NAME (ret));
  else
    GST_DEBUG_OBJECT (operation, "Couldn't find an unlinked ghost sink pad");

  return ret;

}

static GstPad *
get_request_sink_pad (NleOperation * operation)
{
  GstPad *pad = NULL;
  GList *templates;

  if (!operation->element)
    return NULL;

  templates = gst_element_class_get_pad_template_list
      (GST_ELEMENT_GET_CLASS (operation->element));

  for (; templates; templates = templates->next) {
    GstPadTemplate *templ = (GstPadTemplate *) templates->data;

    GST_LOG_OBJECT (operation->element, "Trying template %s",
        GST_PAD_TEMPLATE_NAME_TEMPLATE (templ));

    if ((GST_PAD_TEMPLATE_DIRECTION (templ) == GST_PAD_SINK) &&
        (GST_PAD_TEMPLATE_PRESENCE (templ) == GST_PAD_REQUEST)) {
      pad =
          gst_element_get_request_pad (operation->element,
          GST_PAD_TEMPLATE_NAME_TEMPLATE (templ));
      if (pad)
        break;
    }
  }

  return pad;
}

static GstPad *
add_sink_pad (NleOperation * operation)
{
  GstPad *gpad = NULL;
  GstPad *ret = NULL;

  if (!operation->element)
    return NULL;

  /* FIXME : implement */
  GST_LOG_OBJECT (operation, "element:%s , dynamicsinks:%d",
      GST_ELEMENT_NAME (operation->element), operation->dynamicsinks);


  if (!operation->dynamicsinks) {
    /* static sink pads */
    ret = get_unused_static_sink_pad (operation);
    if (ret) {
      gpad = nle_object_ghost_pad ((NleObject *) operation, GST_PAD_NAME (ret),
          ret);
      gst_object_unref (ret);
    }
  }

  if (!gpad) {
    /* request sink pads */
    ret = get_request_sink_pad (operation);
    if (ret) {
      gpad = nle_object_ghost_pad ((NleObject *) operation, GST_PAD_NAME (ret),
          ret);
      gst_object_unref (ret);
    }
  }

  if (gpad) {
    operation->sinks = g_list_append (operation->sinks, gpad);
    operation->realsinks++;
    GST_DEBUG ("Created new pad %s:%s ghosting %s:%s",
        GST_DEBUG_PAD_NAME (gpad), GST_DEBUG_PAD_NAME (ret));
  } else {
    GST_WARNING ("Couldn't find a usable sink pad!");
  }

  return gpad;
}

static gboolean
remove_sink_pad (NleOperation * operation, GstPad * sinkpad)
{
  gboolean ret = TRUE;

  GST_DEBUG ("sinkpad %s:%s", GST_DEBUG_PAD_NAME (sinkpad));

  /*
     We can't remove any random pad.
     We should remove an unused pad ... which is hard to figure out in a
     thread-safe way.
   */

  if ((sinkpad == NULL) && operation->dynamicsinks) {
    /* Find an unlinked sinkpad */
    if ((sinkpad = get_unlinked_sink_ghost_pad (operation)) == NULL) {
      ret = FALSE;
      goto beach;
    }
  }

  if (sinkpad) {
    GstPad *target = gst_ghost_pad_get_target ((GstGhostPad *) sinkpad);

    if (target) {
      /* release the target pad */
      nle_object_ghost_pad_set_target ((NleObject *) operation, sinkpad, NULL);
      if (operation->dynamicsinks)
        gst_element_release_request_pad (operation->element, target);
      gst_object_unref (target);
    }
    operation->sinks = g_list_remove (operation->sinks, sinkpad);
    nle_object_remove_ghost_pad ((NleObject *) operation, sinkpad);
    operation->realsinks--;
  }

beach:
  return ret;
}

static void
synchronize_sinks (NleOperation * operation)
{

  GST_DEBUG_OBJECT (operation, "num_sinks:%d , realsinks:%d, dynamicsinks:%d",
      operation->num_sinks, operation->realsinks, operation->dynamicsinks);

  if (operation->num_sinks == operation->realsinks)
    return;

  if (operation->num_sinks > operation->realsinks) {
    while (operation->num_sinks > operation->realsinks) /* Add pad */
      if (!(add_sink_pad (operation))) {
        break;
      }
  } else {
    /* Remove pad */
    /* FIXME, which one do we remove ? :) */
    while (operation->num_sinks < operation->realsinks)
      if (!remove_sink_pad (operation, NULL))
        break;
  }
}

static gboolean
nle_operation_prepare (NleObject * object)
{
  /* Prepare the pads */
  synchronize_sinks ((NleOperation *) object);

  return TRUE;
}

static gboolean
nle_operation_cleanup (NleObject * object)
{
  NleOperation *oper = (NleOperation *) object;

  if (oper->dynamicsinks) {
    GST_DEBUG ("Resetting dynamic sinks");
    nle_operation_set_sinks (oper, 0);
  }

  return TRUE;
}

void
nle_operation_hard_cleanup (NleOperation * operation)
{
  gboolean done = FALSE;

  GValue item = { 0, };
  GstIterator *pads;

  GST_INFO_OBJECT (operation, "Hard reset of the operation");

  pads = gst_element_iterate_sink_pads (GST_ELEMENT (operation));
  while (!done) {
    switch (gst_iterator_next (pads, &item)) {
      case GST_ITERATOR_OK:
      {
        GstPad *sinkpad = g_value_get_object (&item);
        GstPad *srcpad = gst_pad_get_peer (sinkpad);

        if (srcpad) {
          GST_ERROR ("Unlinking %" GST_PTR_FORMAT " and  %"
              GST_PTR_FORMAT, srcpad, sinkpad);
          gst_pad_unlink (srcpad, sinkpad);
          gst_object_unref (srcpad);
        }

        g_value_reset (&item);
        break;
      }
      case GST_ITERATOR_RESYNC:
        gst_iterator_resync (pads);
        break;
      default:
        /* ERROR and DONE */
        done = TRUE;
        break;
    }
  }
  nle_object_cleanup (NLE_OBJECT (operation));

  gst_iterator_free (pads);
}


static GstPad *
nle_operation_request_new_pad (GstElement * element, GstPadTemplate * templ,
    const gchar * name, const GstCaps * caps)
{
  NleOperation *operation = (NleOperation *) element;
  GstPad *ret;

  GST_DEBUG ("template:%s name:%s", templ->name_template, name);

  if (operation->num_sinks == operation->realsinks) {
    GST_WARNING_OBJECT (element,
        "We already have the maximum number of pads : %d",
        operation->num_sinks);
    return NULL;
  }

  ret = add_sink_pad ((NleOperation *) element);

  return ret;
}

static void
nle_operation_release_pad (GstElement * element, GstPad * pad)
{
  GST_DEBUG ("pad %s:%s", GST_DEBUG_PAD_NAME (pad));

  remove_sink_pad ((NleOperation *) element, pad);
}

void
nle_operation_signal_input_priority_changed (NleOperation * operation,
    GstPad * pad, guint32 priority)
{
  GST_DEBUG_OBJECT (operation, "pad:%s:%s, priority:%d",
      GST_DEBUG_PAD_NAME (pad), priority);
  g_signal_emit (operation, nle_operation_signals[INPUT_PRIORITY_CHANGED],
      0, pad, priority);
}

void
nle_operation_update_base_time (NleOperation * operation,
    GstClockTime timestamp)
{
  if (!nle_object_to_media_time (NLE_OBJECT (operation),
          timestamp, &operation->next_base_time)) {
    GST_WARNING_OBJECT (operation, "Trying to set a basetime outside of "
        "ourself");

    return;
  }

  GST_INFO_OBJECT (operation, "Setting next_basetime to %"
      GST_TIME_FORMAT, GST_TIME_ARGS (operation->next_base_time));
}