-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnlecomposition.c
3598 lines (2898 loc) · 106 KB
/
nlecomposition.c
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
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* GStreamer
* Copyright (C) 2001 Wim Taymans <[email protected]>
* 2004-2008 Edward Hervey <[email protected]>
* 2014 Mathieu Duponchelle <[email protected]>
* 2014 Thibault Saunier <[email protected]>
*
* 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-nlecomposition
*
* A NleComposition contains NleObjects such as NleSources and NleOperations,
* and connects them dynamically to create a composition timeline.
*/
static GstStaticPadTemplate nle_composition_src_template =
GST_STATIC_PAD_TEMPLATE ("src",
GST_PAD_SRC,
GST_PAD_ALWAYS,
GST_STATIC_CAPS_ANY);
GST_DEBUG_CATEGORY_STATIC (nlecomposition_debug);
#define GST_CAT_DEFAULT nlecomposition_debug
#define _do_init \
GST_DEBUG_CATEGORY_INIT (nlecomposition_debug,"nlecomposition", GST_DEBUG_FG_BLUE | GST_DEBUG_BOLD, "NLE Composition");
#define nle_composition_parent_class parent_class
enum
{
PROP_0,
PROP_ID,
PROP_DROP_TAGS,
PROP_LAST,
};
#define DEFAULT_DROP_TAGS TRUE
/* Properties from NleObject */
enum
{
NLEOBJECT_PROP_START,
NLEOBJECT_PROP_STOP,
NLEOBJECT_PROP_DURATION,
NLEOBJECT_PROP_LAST
};
enum
{
COMMIT_SIGNAL,
COMMITED_SIGNAL,
LAST_SIGNAL
};
typedef enum
{
COMP_UPDATE_STACK_INITIALIZE,
COMP_UPDATE_STACK_ON_COMMIT,
COMP_UPDATE_STACK_ON_EOS,
COMP_UPDATE_STACK_ON_SEEK,
COMP_UPDATE_STACK_NONE
} NleUpdateStackReason;
static const char *UPDATE_PIPELINE_REASONS[] = {
"Initialize", "Commit", "EOS", "Seek", "None"
};
typedef struct
{
NleComposition *comp;
GstEvent *event;
} SeekData;
typedef struct
{
NleComposition *comp;
NleObject *object;
} ChildIOData;
typedef struct
{
NleComposition *comp;
gint32 seqnum;
NleUpdateStackReason reason;
} UpdateCompositionData;
typedef struct _Action
{
GCClosure closure;
gint priority;
} Action;
struct _NleCompositionPrivate
{
gboolean dispose_has_run;
/*
Sorted List of NleObjects , ThreadSafe
objects_start : sorted by start-time then priority
objects_stop : sorted by stop-time then priority
objects_hash : contains all controlled objects
Those list should be manipulated exclusively in the main context
or while the task is totally stopped.
*/
GList *objects_start;
GList *objects_stop;
GHashTable *objects_hash;
/* List of NleObject to be inserted or removed from the composition on the
* next commit */
GHashTable *pending_io;
gulong ghosteventprobe;
/* current stack, list of NleObject* */
GNode *current;
/* List of NleObject whose start/duration will be the same as the composition */
GList *expandables;
/* currently configured stack seek start/stop time.
* In forward playback:
* - current_stack_start: The start of the current stack or the start value
* of the seek if the stack has been seeked 'in the middle'
* - current_stack_stop: The stop time of the current stack
*
* Reconstruct pipeline ONLY if seeking outside of those values
* FIXME : current_stack_start isn't always the earliest time before which the
* timeline doesn't need to be modified
*/
GstClockTime current_stack_start;
GstClockTime current_stack_stop;
/* Seek segment handler */
/* Represents the current segment that is being played,
* In forwards playback (logic is the same but swapping start and
* stop in backward playback):
* - segment->start: start of the current segment being played,
* at each stack change it will advance to the newly configured
* stack start.
* - segment->stop is the final stop of the segment being played.
* if a seek with a stop time happened, it will be the stop time
* otherwise, it will be the composition duration.
*/
GstSegment *segment;
/* Segment representing the last seek. Simply initialized
* segment if no seek occured. */
GstSegment *seek_segment;
guint64 next_base_time;
/*
OUR sync_handler on the child_bus
We are called before nle_object_sync_handler
*/
GstPadEventFunction nle_event_pad_func;
gboolean send_stream_start;
/* Protect the actions list */
GMutex actions_lock;
GCond actions_cond;
GList *actions;
Action *current_action;
gboolean running;
gboolean initialized;
GstElement *current_bin;
gboolean seeking_itself;
gint real_eos_seqnum;
gint next_eos_seqnum;
guint32 flush_seqnum;
/* 0 means that we already received the right caps or segment */
gint seqnum_to_restart_task;
gboolean waiting_serialized_query_or_buffer;
GstEvent *stack_initialization_seek;
gboolean stack_initialization_seek_sent;
gboolean tearing_down_stack;
gboolean suppress_child_error;
NleUpdateStackReason updating_reason;
guint seek_seqnum;
/* Both protected with object lock */
gchar *id;
gboolean drop_tags;
};
#define ACTION_CALLBACK(__action) (((GCClosure*) (__action))->callback)
static guint _signals[LAST_SIGNAL] = { 0 };
static GParamSpec *nleobject_properties[NLEOBJECT_PROP_LAST];
static GParamSpec *properties[PROP_LAST];
G_DEFINE_TYPE_WITH_CODE (NleComposition, nle_composition, NLE_TYPE_OBJECT,
G_ADD_PRIVATE (NleComposition)
_do_init);
#define OBJECT_IN_ACTIVE_SEGMENT(comp,element) \
((NLE_OBJECT_START(element) < comp->priv->current_stack_stop) && \
(NLE_OBJECT_STOP(element) >= comp->priv->current_stack_start))
static void nle_composition_dispose (GObject * object);
static void nle_composition_finalize (GObject * object);
static void nle_composition_reset (NleComposition * comp);
static gboolean nle_composition_add_object (GstBin * bin, GstElement * element);
static gboolean
nle_composition_remove_object (GstBin * bin, GstElement * element);
static GstStateChangeReturn
nle_composition_change_state (GstElement * element, GstStateChange transition);
static inline void nle_composition_reset_target_pad (NleComposition * comp);
static gboolean
seek_handling (NleComposition * comp, gint32 seqnum,
NleUpdateStackReason update_stack_reason);
static gint objects_start_compare (NleObject * a, NleObject * b);
static gint objects_stop_compare (NleObject * a, NleObject * b);
static GstClockTime get_current_position (NleComposition * comp);
static gboolean update_pipeline (NleComposition * comp,
GstClockTime currenttime, gint32 seqnum,
NleUpdateStackReason update_stack_reason);
static gboolean nle_composition_commit_func (NleObject * object,
gboolean recurse);
static void update_start_stop_duration (NleComposition * comp);
static gboolean
nle_composition_event_handler (GstPad * ghostpad, GstObject * parent,
GstEvent * event);
static void _relink_single_node (NleComposition * comp, GNode * node,
GstEvent * toplevel_seek);
static void _update_pipeline_func (NleComposition * comp,
UpdateCompositionData * ucompo);
static void _commit_func (NleComposition * comp,
UpdateCompositionData * ucompo);
static GstEvent *get_new_seek_event (NleComposition * comp, gboolean initial,
gboolean updatestoponly, NleUpdateStackReason reason);
static gboolean _nle_composition_add_object (NleComposition * comp,
NleObject * object);
static gboolean _nle_composition_remove_object (NleComposition * comp,
NleObject * object);
static void _deactivate_stack (NleComposition * comp,
NleUpdateStackReason reason);
static void _set_real_eos_seqnum_from_seek (NleComposition * comp,
GstEvent * event);
static void _emit_commited_signal_func (NleComposition * comp, gpointer udata);
static void _restart_task (NleComposition * comp);
static void
_add_action (NleComposition * comp, GCallback func, gpointer data,
gint priority);
static gboolean
_is_ready_to_restart_task (NleComposition * comp, GstEvent * event);
/* COMP_REAL_START: actual position to start current playback at. */
#define COMP_REAL_START(comp) \
(MAX (comp->priv->segment->start, NLE_OBJECT_START (comp)))
#define COMP_REAL_STOP(comp) \
(GST_CLOCK_TIME_IS_VALID (comp->priv->segment->stop) ? \
(MIN (comp->priv->segment->stop, NLE_OBJECT_STOP (comp))) : \
NLE_OBJECT_STOP (comp))
#define ACTIONS_LOCK(comp) G_STMT_START { \
GST_LOG_OBJECT (comp, "Getting ACTIONS_LOCK in thread %p", \
g_thread_self()); \
g_mutex_lock(&((NleComposition*)comp)->priv->actions_lock); \
GST_LOG_OBJECT (comp, "Got ACTIONS_LOCK in thread %p", \
g_thread_self()); \
} G_STMT_END
#define ACTIONS_UNLOCK(comp) G_STMT_START { \
g_mutex_unlock(&((NleComposition*)comp)->priv->actions_lock); \
GST_LOG_OBJECT (comp, "Unlocked ACTIONS_LOCK in thread %p", \
g_thread_self()); \
} G_STMT_END
#define WAIT_FOR_AN_ACTION(comp) G_STMT_START { \
GST_LOG_OBJECT (comp, "Waiting for an action in thread %p", \
g_thread_self()); \
g_cond_wait(&((NleComposition*)comp)->priv->actions_cond, \
&((NleComposition*)comp)->priv->actions_lock); \
GST_LOG_OBJECT (comp, "Done WAITING for an action in thread %p", \
g_thread_self()); \
} G_STMT_END
#define SIGNAL_NEW_ACTION(comp) G_STMT_START { \
GST_LOG_OBJECT (comp, "Signalling new action from thread %p", \
g_thread_self()); \
g_cond_signal(&((NleComposition*)comp)->priv->actions_cond); \
} G_STMT_END
#define GET_TASK_LOCK(comp) (&(NLE_COMPOSITION(comp)->task_rec_lock))
static inline gboolean
_have_to_flush_downstream (NleUpdateStackReason update_reason)
{
if (update_reason == COMP_UPDATE_STACK_ON_COMMIT ||
update_reason == COMP_UPDATE_STACK_ON_SEEK ||
update_reason == COMP_UPDATE_STACK_INITIALIZE)
return TRUE;
return FALSE;
}
static void
_assert_proper_thread (NleComposition * comp)
{
if (comp->task && gst_task_get_state (comp->task) != GST_TASK_STOPPED &&
g_thread_self () != comp->task->thread) {
g_warning ("Trying to touch children in a thread different from"
" its dedicated thread!");
}
}
static void
_remove_actions_for_type (NleComposition * comp, GCallback callback)
{
GList *tmp;
ACTIONS_LOCK (comp);
GST_LOG_OBJECT (comp, "finding action[callback=%s], action count = %d",
GST_DEBUG_FUNCPTR_NAME (callback), g_list_length (comp->priv->actions));
tmp = g_list_first (comp->priv->actions);
while (tmp != NULL) {
Action *act = tmp->data;
GList *removed = NULL;
if (ACTION_CALLBACK (act) == callback) {
GST_LOG_OBJECT (comp, "remove action for callback %s",
GST_DEBUG_FUNCPTR_NAME (callback));
removed = tmp;
g_closure_unref ((GClosure *) act);
comp->priv->actions = g_list_remove_link (comp->priv->actions, removed);
}
tmp = g_list_next (tmp);
if (removed)
g_list_free (removed);
}
ACTIONS_UNLOCK (comp);
}
static void
_execute_actions (NleComposition * comp)
{
NleCompositionPrivate *priv = comp->priv;
ACTIONS_LOCK (comp);
if (priv->running == FALSE) {
GST_DEBUG_OBJECT (comp, "Not running anymore");
ACTIONS_UNLOCK (comp);
return;
}
if (priv->actions == NULL)
WAIT_FOR_AN_ACTION (comp);
if (comp->priv->running == FALSE) {
GST_INFO_OBJECT (comp, "Done waiting but not running anymore");
ACTIONS_UNLOCK (comp);
return;
}
if (priv->actions) {
GValue params[1] = { G_VALUE_INIT };
GList *lact;
GST_LOG_OBJECT (comp, "scheduled actions [%d]",
g_list_length (priv->actions));
g_value_init (¶ms[0], G_TYPE_OBJECT);
g_value_set_object (¶ms[0], comp);
lact = g_list_first (priv->actions);
priv->actions = g_list_remove_link (priv->actions, lact);
priv->current_action = lact->data;
ACTIONS_UNLOCK (comp);
GST_INFO_OBJECT (comp, "Invoking %p:%s",
lact->data, GST_DEBUG_FUNCPTR_NAME ((ACTION_CALLBACK (lact->data))));
g_closure_invoke (lact->data, NULL, 1, params, NULL);
g_value_unset (¶ms[0]);
ACTIONS_LOCK (comp);
g_closure_unref (lact->data);
g_list_free (lact);
priv->current_action = NULL;
ACTIONS_UNLOCK (comp);
GST_LOG_OBJECT (comp, "remaining actions [%d]",
g_list_length (priv->actions));
} else {
ACTIONS_UNLOCK (comp);
}
}
static void
_start_task (NleComposition * comp)
{
GstTask *task;
ACTIONS_LOCK (comp);
comp->priv->running = TRUE;
ACTIONS_UNLOCK (comp);
GST_OBJECT_LOCK (comp);
task = comp->task;
if (task == NULL) {
gchar *taskname =
g_strdup_printf ("%s_update_management", GST_OBJECT_NAME (comp));
task = gst_task_new ((GstTaskFunction) _execute_actions, comp, NULL);
gst_object_set_name (GST_OBJECT_CAST (task), taskname);
gst_task_set_lock (task, GET_TASK_LOCK (comp));
GST_DEBUG_OBJECT (comp, "created task %p", task);
comp->task = task;
gst_object_set_parent (GST_OBJECT (task), GST_OBJECT (comp));
gst_object_unref (task);
g_free (taskname);
}
gst_task_set_state (task, GST_TASK_STARTED);
GST_OBJECT_UNLOCK (comp);
}
static gboolean
_pause_task (NleComposition * comp)
{
GST_OBJECT_LOCK (comp);
if (comp->task == NULL) {
GST_INFO_OBJECT (comp, "No task set, it must have been stopped, returning");
GST_OBJECT_UNLOCK (comp);
return FALSE;
}
gst_task_pause (comp->task);
GST_OBJECT_UNLOCK (comp);
return TRUE;
}
static gboolean
_stop_task (NleComposition * comp)
{
gboolean res = TRUE;
GstTask *task;
GST_INFO_OBJECT (comp, "Stoping children management task");
ACTIONS_LOCK (comp);
comp->priv->running = FALSE;
/* Make sure we do not stay blocked trying to execute an action */
SIGNAL_NEW_ACTION (comp);
ACTIONS_UNLOCK (comp);
GST_DEBUG_OBJECT (comp, "stop task");
GST_OBJECT_LOCK (comp);
task = comp->task;
if (task == NULL)
goto no_task;
comp->task = NULL;
res = gst_task_set_state (task, GST_TASK_STOPPED);
GST_OBJECT_UNLOCK (comp);
if (!gst_task_join (task))
goto join_failed;
gst_object_unparent (GST_OBJECT (task));
return res;
no_task:
{
GST_OBJECT_UNLOCK (comp);
/* this is not an error */
return TRUE;
}
join_failed:
{
/* this is bad, possibly the application tried to join the task from
* the task's thread. We install the task again so that it will be stopped
* again from the right thread next time hopefully. */
GST_OBJECT_LOCK (comp);
GST_DEBUG_OBJECT (comp, "join failed");
/* we can only install this task if there was no other task */
if (comp->task == NULL)
comp->task = task;
GST_OBJECT_UNLOCK (comp);
return FALSE;
}
return res;
}
static void
_post_start_composition_update (NleComposition * comp,
gint32 seqnum, NleUpdateStackReason reason)
{
GstMessage *msg;
msg = gst_message_new_element (GST_OBJECT (comp),
gst_structure_new ("NleCompositionStartUpdate",
"reason", G_TYPE_STRING, UPDATE_PIPELINE_REASONS[reason], NULL));
gst_message_set_seqnum (msg, seqnum);
gst_element_post_message (GST_ELEMENT (comp), msg);
}
static void
_post_start_composition_update_done (NleComposition * comp,
gint32 seqnum, NleUpdateStackReason reason)
{
GstMessage *msg = gst_message_new_element (GST_OBJECT (comp),
gst_structure_new ("NleCompositionUpdateDone",
"reason", G_TYPE_STRING, UPDATE_PIPELINE_REASONS[reason],
NULL));
gst_message_set_seqnum (msg, seqnum);
gst_element_post_message (GST_ELEMENT (comp), msg);
}
static void
_seek_pipeline_func (NleComposition * comp, SeekData * seekd)
{
gdouble rate;
GstFormat format;
GstSeekFlags flags;
GstSeekType cur_type, stop_type;
gint64 cur, stop;
NleCompositionPrivate *priv = comp->priv;
gboolean initializing_stack = priv->stack_initialization_seek == seekd->event;
NleUpdateStackReason reason =
initializing_stack ? COMP_UPDATE_STACK_NONE : COMP_UPDATE_STACK_ON_SEEK;
GstClockTime segment_start, segment_stop;
gboolean reverse;
gst_event_parse_seek (seekd->event, &rate, &format, &flags,
&cur_type, &cur, &stop_type, &stop);
reverse = rate < 0;
GST_DEBUG_OBJECT (seekd->comp,
"start:%" GST_TIME_FORMAT " -- stop:%" GST_TIME_FORMAT " flags:%d",
GST_TIME_ARGS (cur), GST_TIME_ARGS (stop), flags);
if (!initializing_stack) {
segment_start = cur;
segment_stop = stop;
} else {
/* During plain playback (no seek), the segment->stop doesn't
* evolve when going from stack to stack, only the start does
* (in reverse playback, the logic is reversed) */
segment_start = reverse ? priv->segment->start : cur;
segment_stop = reverse ? stop : priv->segment->stop;
}
gst_segment_do_seek (priv->segment,
rate, format, flags, cur_type, segment_start, stop_type, segment_stop,
NULL);
gst_segment_do_seek (priv->seek_segment,
rate, format, flags, cur_type, cur, stop_type, stop, NULL);
GST_DEBUG_OBJECT (seekd->comp, "Segment now has flags:%d",
priv->segment->flags);
/* FIXME: The idea was to avoid seeking on a stack if we know we will endup
* passed the end, but then we loose the flush, wich leads to hangs. Long
* term, we should just flush the stack instead to avoid the double seek. */
#if 0
if (priv->segment->start >= NLE_OBJECT_STOP (seekd->comp)) {
GST_INFO_OBJECT (seekd->comp,
"Start %" GST_TIME_FORMAT " > comp->stop: %" GST_TIME_FORMAT
" Not seeking", GST_TIME_ARGS (priv->segment->start),
GST_TIME_ARGS (NLE_OBJECT_STOP (seekd->comp)));
GST_FIXME_OBJECT (seekd->comp, "HANDLE error async!");
return;
}
#endif
if (!initializing_stack)
_post_start_composition_update (seekd->comp,
gst_event_get_seqnum (seekd->event), COMP_UPDATE_STACK_ON_SEEK);
/* crop the segment start/stop values */
/* Only crop segment start value if we don't have a default object */
if (priv->expandables == NULL)
priv->segment->start =
MAX (priv->segment->start, NLE_OBJECT_START (seekd->comp));
priv->segment->stop =
MIN (priv->segment->stop, NLE_OBJECT_STOP (seekd->comp));
if (initializing_stack) {
GST_INFO_OBJECT (seekd->comp, "Pausing task to run initializing seek.");
_pause_task (seekd->comp);
} else {
priv->next_base_time = 0;
comp->priv->flush_seqnum = comp->priv->seek_seqnum =
gst_event_get_seqnum (seekd->event);
}
seek_handling (seekd->comp, gst_event_get_seqnum (seekd->event), reason);
if (!initializing_stack)
_post_start_composition_update_done (seekd->comp,
gst_event_get_seqnum (seekd->event), COMP_UPDATE_STACK_ON_SEEK);
}
/* Must be called with OBJECTS_LOCK taken */
static void
_process_pending_entries (NleComposition * comp, NleUpdateStackReason reason)
{
NleObject *object;
GHashTableIter iter;
gboolean deactivated_stack = FALSE;
NleCompositionPrivate *priv = comp->priv;
g_hash_table_iter_init (&iter, priv->pending_io);
while (g_hash_table_iter_next (&iter, (gpointer *) & object, NULL)) {
if (g_hash_table_contains (priv->objects_hash, object)) {
if (GST_OBJECT_PARENT (object) == GST_OBJECT_CAST (priv->current_bin) &&
deactivated_stack == FALSE) {
deactivated_stack = TRUE;
_deactivate_stack (comp, reason);
}
_nle_composition_remove_object (comp, object);
} else {
/* take a new ref on object as the current one will be released when
* object is removed from pending_io */
_nle_composition_add_object (comp, gst_object_ref (object));
}
}
g_hash_table_remove_all (priv->pending_io);
}
static inline gboolean
_commit_values (NleComposition * comp)
{
GList *tmp;
gboolean commited = FALSE;
NleCompositionPrivate *priv = comp->priv;
for (tmp = priv->objects_start; tmp; tmp = tmp->next) {
if (nle_object_commit (tmp->data, TRUE))
commited = TRUE;
}
GST_DEBUG_OBJECT (comp, "Linking up commit vmethod");
commited |= NLE_OBJECT_CLASS (parent_class)->commit (NLE_OBJECT (comp), TRUE);
return commited;
}
static gboolean
_commit_all_values (NleComposition * comp, NleUpdateStackReason reason)
{
NleCompositionPrivate *priv = comp->priv;
priv->next_base_time = 0;
_process_pending_entries (comp, reason);
if (_commit_values (comp) == FALSE) {
return FALSE;;
}
/* The topology of the composition might have changed, update the lists */
priv->objects_start = g_list_sort
(priv->objects_start, (GCompareFunc) objects_start_compare);
priv->objects_stop = g_list_sort
(priv->objects_stop, (GCompareFunc) objects_stop_compare);
return TRUE;
}
static gboolean
_initialize_stack_func (NleComposition * comp, UpdateCompositionData * ucompo)
{
NleCompositionPrivate *priv = comp->priv;
_post_start_composition_update (comp, ucompo->seqnum, ucompo->reason);
_commit_all_values (comp, ucompo->reason);
update_start_stop_duration (comp);
comp->priv->next_base_time = 0;
/* set ghostpad target */
if (!(update_pipeline (comp, COMP_REAL_START (comp),
ucompo->seqnum, COMP_UPDATE_STACK_INITIALIZE))) {
GST_FIXME_OBJECT (comp, "PLEASE signal state change failure ASYNC");
}
_post_start_composition_update_done (comp, ucompo->seqnum, ucompo->reason);
priv->initialized = TRUE;
return G_SOURCE_REMOVE;
}
static void
_remove_object_func (NleComposition * comp, ChildIOData * childio)
{
NleObject *object = childio->object;
NleCompositionPrivate *priv = comp->priv;
NleObject *in_pending_io;
in_pending_io = g_hash_table_lookup (priv->pending_io, object);
if (!g_hash_table_contains (priv->objects_hash, object)) {
if (in_pending_io) {
GST_INFO_OBJECT (comp, "Object %" GST_PTR_FORMAT " was marked"
" for addition, removing it from the addition list", object);
g_hash_table_remove (priv->pending_io, object);
return;
}
GST_ERROR_OBJECT (comp, "Object %" GST_PTR_FORMAT " is "
" not in the composition", object);
return;
}
if (in_pending_io) {
GST_WARNING_OBJECT (comp, "Object %" GST_PTR_FORMAT " is already marked"
" for removal", object);
return;
}
g_hash_table_add (priv->pending_io, gst_object_ref (object));
return;
}
static void
_add_remove_object_action (NleComposition * comp, NleObject * object)
{
ChildIOData *childio = g_new0 (ChildIOData, 1);
GST_DEBUG_OBJECT (comp, "Adding Action");
childio->comp = comp;
childio->object = object;
_add_action (comp, G_CALLBACK (_remove_object_func),
childio, G_PRIORITY_DEFAULT);
}
static void
_add_object_func (NleComposition * comp, ChildIOData * childio)
{
NleObject *object = childio->object;
NleCompositionPrivate *priv = comp->priv;
NleObject *in_pending_io;
in_pending_io = g_hash_table_lookup (priv->pending_io, object);
if (g_hash_table_contains (priv->objects_hash, object)) {
if (in_pending_io) {
GST_INFO_OBJECT (comp, "Object already in but marked in pendings"
" removing from pendings");
g_hash_table_remove (priv->pending_io, object);
return;
}
GST_ERROR_OBJECT (comp, "Object %" GST_PTR_FORMAT " is "
" already in the composition", object);
return;
}
if (in_pending_io) {
GST_WARNING_OBJECT (comp, "Object %" GST_PTR_FORMAT " is already marked"
" for addition", object);
return;
}
/* current reference is hold by the action and will be released with it,
* so take a new one */
g_hash_table_add (priv->pending_io, gst_object_ref (object));
}
static void
_add_add_object_action (NleComposition * comp, NleObject * object)
{
ChildIOData *childio = g_new0 (ChildIOData, 1);
GST_DEBUG_OBJECT (comp, "Adding Action");
childio->comp = comp;
childio->object = object;
_add_action (comp, G_CALLBACK (_add_object_func), childio,
G_PRIORITY_DEFAULT);
}
static void
_free_action (gpointer udata, Action * action)
{
GST_LOG ("freeing %p action for %s", action,
GST_DEBUG_FUNCPTR_NAME (ACTION_CALLBACK (action)));
if (ACTION_CALLBACK (action) == _seek_pipeline_func) {
SeekData *seekd = (SeekData *) udata;
gst_event_unref (seekd->event);
g_free (seekd);
} else if (ACTION_CALLBACK (action) == _add_object_func) {
ChildIOData *iodata = (ChildIOData *) udata;
gst_object_unref (iodata->object);
g_free (iodata);
} else if (ACTION_CALLBACK (action) == _remove_object_func) {
g_free (udata);
} else if (ACTION_CALLBACK (action) == _update_pipeline_func ||
ACTION_CALLBACK (action) == _commit_func ||
ACTION_CALLBACK (action) == _initialize_stack_func) {
g_free (udata);
}
}
static void
_add_action_locked (NleComposition * comp, GCallback func,
gpointer data, gint priority)
{
Action *action;
NleCompositionPrivate *priv = comp->priv;
action = (Action *) g_closure_new_simple (sizeof (Action), data);
g_closure_add_finalize_notifier ((GClosure *) action, data,
(GClosureNotify) _free_action);
ACTION_CALLBACK (action) = func;
action->priority = priority;
g_closure_set_marshal ((GClosure *) action, g_cclosure_marshal_VOID__VOID);
GST_INFO_OBJECT (comp, "Adding Action for function: %p:%s",
action, GST_DEBUG_FUNCPTR_NAME (func));
if (priority == G_PRIORITY_HIGH)
priv->actions = g_list_prepend (priv->actions, action);
else
priv->actions = g_list_append (priv->actions, action);
GST_LOG_OBJECT (comp, "the number of remaining actions: %d",
g_list_length (priv->actions));
SIGNAL_NEW_ACTION (comp);
}
static void
_add_action (NleComposition * comp, GCallback func,
gpointer data, gint priority)
{
ACTIONS_LOCK (comp);
_add_action_locked (comp, func, data, priority);
ACTIONS_UNLOCK (comp);
}
static SeekData *
create_seek_data (NleComposition * comp, GstEvent * event)
{
SeekData *seekd = g_new0 (SeekData, 1);
seekd->comp = comp;
seekd->event = event;
return seekd;
}
static void
_add_seek_action (NleComposition * comp, GstEvent * event)
{
SeekData *seekd;
GList *tmp;
guint32 seqnum = gst_event_get_seqnum (event);
ACTIONS_LOCK (comp);
/* Check if this is our current seqnum */
if (seqnum == comp->priv->next_eos_seqnum) {
GST_DEBUG_OBJECT (comp, "Not adding Action, same seqnum as previous seek");
ACTIONS_UNLOCK (comp);
return;
}
/* Check if this seqnum is already queued up but not handled yet */
for (tmp = comp->priv->actions; tmp != NULL; tmp = tmp->next) {
Action *act = tmp->data;
if (ACTION_CALLBACK (act) == G_CALLBACK (_seek_pipeline_func)) {
SeekData *tmp_data = ((GClosure *) act)->data;
if (gst_event_get_seqnum (tmp_data->event) == seqnum) {
GST_DEBUG_OBJECT (comp,
"Not adding Action, same seqnum as previous seek");
ACTIONS_UNLOCK (comp);
return;
}
}
}
/* Check if this seqnum is currently being handled */
if (comp->priv->current_action) {
Action *act = comp->priv->current_action;
if (ACTION_CALLBACK (act) == G_CALLBACK (_seek_pipeline_func)) {
SeekData *tmp_data = ((GClosure *) act)->data;
if (gst_event_get_seqnum (tmp_data->event) == seqnum) {
GST_DEBUG_OBJECT (comp,
"Not adding Action, same seqnum as previous seek");
ACTIONS_UNLOCK (comp);
return;
}
}
}
GST_DEBUG_OBJECT (comp, "Adding seek Action");
seekd = create_seek_data (comp, event);
comp->priv->next_eos_seqnum = 0;
comp->priv->real_eos_seqnum = 0;
comp->priv->seek_seqnum = 0;
_add_action_locked (comp, G_CALLBACK (_seek_pipeline_func), seekd,
G_PRIORITY_DEFAULT);
ACTIONS_UNLOCK (comp);
}
static void
_remove_update_actions (NleComposition * comp)
{
_remove_actions_for_type (comp, G_CALLBACK (_update_pipeline_func));
}
static void
_remove_seek_actions (NleComposition * comp)
{
_remove_actions_for_type (comp, G_CALLBACK (_seek_pipeline_func));
}
static void
_add_update_compo_action (NleComposition * comp,
GCallback callback, NleUpdateStackReason reason)
{
UpdateCompositionData *ucompo = g_new0 (UpdateCompositionData, 1);
ucompo->comp = comp;
ucompo->reason = reason;
ucompo->seqnum = gst_util_seqnum_next ();