/
gstappsink.c
1858 lines (1615 loc) · 55.1 KB
/
gstappsink.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) 2007 David Schleef <ds@schleef.org>
* (C) 2008 Wim Taymans <wim.taymans@gmail.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:gstappsink
* @title: GstAppSink
* @short_description: Easy way for applications to extract samples from a
* pipeline
* @see_also: #GstSample, #GstBaseSink, appsrc
*
* Appsink is a sink plugin that supports many different methods for making
* the application get a handle on the GStreamer data in a pipeline. Unlike
* most GStreamer elements, Appsink provides external API functions.
*
* appsink can be used by linking to the gstappsink.h header file to access the
* methods or by using the appsink action signals and properties.
*
* The normal way of retrieving samples from appsink is by using the
* gst_app_sink_pull_sample() and gst_app_sink_pull_preroll() methods.
* These methods block until a sample becomes available in the sink or when the
* sink is shut down or reaches EOS. There are also timed variants of these
* methods, gst_app_sink_try_pull_sample() and gst_app_sink_try_pull_preroll(),
* which accept a timeout parameter to limit the amount of time to wait.
*
* Appsink will internally use a queue to collect buffers from the streaming
* thread. If the application is not pulling samples fast enough, this queue
* will consume a lot of memory over time. The "max-buffers" property can be
* used to limit the queue size. The "drop" property controls whether the
* streaming thread blocks or if older buffers are dropped when the maximum
* queue size is reached. Note that blocking the streaming thread can negatively
* affect real-time performance and should be avoided.
*
* If a blocking behaviour is not desirable, setting the "emit-signals" property
* to %TRUE will make appsink emit the "new-sample" and "new-preroll" signals
* when a sample can be pulled without blocking.
*
* The "caps" property on appsink can be used to control the formats that
* appsink can receive. This property can contain non-fixed caps, the format of
* the pulled samples can be obtained by getting the sample caps.
*
* If one of the pull-preroll or pull-sample methods return %NULL, the appsink
* is stopped or in the EOS state. You can check for the EOS state with the
* "eos" property or with the gst_app_sink_is_eos() method.
*
* The eos signal can also be used to be informed when the EOS state is reached
* to avoid polling.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <gst/gst.h>
#include <gst/base/base.h>
#include <string.h>
#include "gstappsink.h"
typedef enum
{
NOONE_WAITING = 0,
STREAM_WAITING = 1 << 0, /* streaming thread is waiting for application thread */
APP_WAITING = 1 << 1, /* application thread is waiting for streaming thread */
} GstAppSinkWaitStatus;
typedef struct
{
GstAppSinkCallbacks callbacks;
gpointer user_data;
GDestroyNotify destroy_notify;
gint ref_count;
} Callbacks;
static Callbacks *
callbacks_ref (Callbacks * callbacks)
{
g_atomic_int_inc (&callbacks->ref_count);
return callbacks;
}
static void
callbacks_unref (Callbacks * callbacks)
{
if (!g_atomic_int_dec_and_test (&callbacks->ref_count))
return;
if (callbacks->destroy_notify)
callbacks->destroy_notify (callbacks->user_data);
g_free (callbacks);
}
struct _GstAppSinkPrivate
{
GstCaps *caps;
gboolean emit_signals;
guint num_buffers;
guint max_buffers;
gboolean drop;
gboolean wait_on_eos;
GstAppSinkWaitStatus wait_status;
GCond cond;
GMutex mutex;
GstQueueArray *queue;
GstBuffer *preroll_buffer;
GstCaps *preroll_caps;
GstCaps *last_caps;
GstSegment preroll_segment;
GstSegment last_segment;
gboolean flushing;
gboolean unlock;
gboolean started;
gboolean is_eos;
gboolean buffer_lists_supported;
Callbacks *callbacks;
GstSample *sample;
};
GST_DEBUG_CATEGORY_STATIC (app_sink_debug);
#define GST_CAT_DEFAULT app_sink_debug
enum
{
/* signals */
SIGNAL_EOS,
SIGNAL_NEW_PREROLL,
SIGNAL_NEW_SAMPLE,
/* actions */
SIGNAL_PULL_PREROLL,
SIGNAL_PULL_SAMPLE,
SIGNAL_TRY_PULL_PREROLL,
SIGNAL_TRY_PULL_SAMPLE,
LAST_SIGNAL
};
#define DEFAULT_PROP_EOS TRUE
#define DEFAULT_PROP_EMIT_SIGNALS FALSE
#define DEFAULT_PROP_MAX_BUFFERS 0
#define DEFAULT_PROP_DROP FALSE
#define DEFAULT_PROP_WAIT_ON_EOS TRUE
#define DEFAULT_PROP_BUFFER_LIST FALSE
enum
{
PROP_0,
PROP_CAPS,
PROP_EOS,
PROP_EMIT_SIGNALS,
PROP_MAX_BUFFERS,
PROP_DROP,
PROP_WAIT_ON_EOS,
PROP_BUFFER_LIST,
PROP_LAST
};
static GstStaticPadTemplate gst_app_sink_template =
GST_STATIC_PAD_TEMPLATE ("sink",
GST_PAD_SINK,
GST_PAD_ALWAYS,
GST_STATIC_CAPS_ANY);
static void gst_app_sink_uri_handler_init (gpointer g_iface,
gpointer iface_data);
static void gst_app_sink_dispose (GObject * object);
static void gst_app_sink_finalize (GObject * object);
static void gst_app_sink_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec);
static void gst_app_sink_get_property (GObject * object, guint prop_id,
GValue * value, GParamSpec * pspec);
static gboolean gst_app_sink_unlock_start (GstBaseSink * bsink);
static gboolean gst_app_sink_unlock_stop (GstBaseSink * bsink);
static gboolean gst_app_sink_start (GstBaseSink * psink);
static gboolean gst_app_sink_stop (GstBaseSink * psink);
static gboolean gst_app_sink_event (GstBaseSink * sink, GstEvent * event);
static gboolean gst_app_sink_query (GstBaseSink * bsink, GstQuery * query);
static GstFlowReturn gst_app_sink_preroll (GstBaseSink * psink,
GstBuffer * buffer);
static GstFlowReturn gst_app_sink_render_common (GstBaseSink * psink,
GstMiniObject * data, gboolean is_list);
static GstFlowReturn gst_app_sink_render (GstBaseSink * psink,
GstBuffer * buffer);
static GstFlowReturn gst_app_sink_render_list (GstBaseSink * psink,
GstBufferList * list);
static gboolean gst_app_sink_setcaps (GstBaseSink * sink, GstCaps * caps);
static GstCaps *gst_app_sink_getcaps (GstBaseSink * psink, GstCaps * filter);
static guint gst_app_sink_signals[LAST_SIGNAL] = { 0 };
#define gst_app_sink_parent_class parent_class
G_DEFINE_TYPE_WITH_CODE (GstAppSink, gst_app_sink, GST_TYPE_BASE_SINK,
G_ADD_PRIVATE (GstAppSink)
G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER,
gst_app_sink_uri_handler_init));
static void
gst_app_sink_class_init (GstAppSinkClass * klass)
{
GObjectClass *gobject_class = (GObjectClass *) klass;
GstElementClass *element_class = (GstElementClass *) klass;
GstBaseSinkClass *basesink_class = (GstBaseSinkClass *) klass;
GST_DEBUG_CATEGORY_INIT (app_sink_debug, "appsink", 0, "appsink element");
gobject_class->dispose = gst_app_sink_dispose;
gobject_class->finalize = gst_app_sink_finalize;
gobject_class->set_property = gst_app_sink_set_property;
gobject_class->get_property = gst_app_sink_get_property;
g_object_class_install_property (gobject_class, PROP_CAPS,
g_param_spec_boxed ("caps", "Caps",
"The allowed caps for the sink pad", GST_TYPE_CAPS,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class, PROP_EOS,
g_param_spec_boolean ("eos", "EOS",
"Check if the sink is EOS or not started", DEFAULT_PROP_EOS,
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class, PROP_EMIT_SIGNALS,
g_param_spec_boolean ("emit-signals", "Emit signals",
"Emit new-preroll and new-sample signals",
DEFAULT_PROP_EMIT_SIGNALS,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class, PROP_MAX_BUFFERS,
g_param_spec_uint ("max-buffers", "Max Buffers",
"The maximum number of buffers to queue internally (0 = unlimited)",
0, G_MAXUINT, DEFAULT_PROP_MAX_BUFFERS,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class, PROP_DROP,
g_param_spec_boolean ("drop", "Drop",
"Drop old buffers when the buffer queue is filled", DEFAULT_PROP_DROP,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class, PROP_BUFFER_LIST,
g_param_spec_boolean ("buffer-list", "Buffer List",
"Use buffer lists", DEFAULT_PROP_BUFFER_LIST,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
/**
* GstAppSink::wait-on-eos:
*
* Wait for all buffers to be processed after receiving an EOS.
*
* In cases where it is uncertain if an @appsink will have a consumer for its buffers
* when it receives an EOS, set to %FALSE to ensure that the @appsink will not hang.
*
* Since: 1.8
*/
g_object_class_install_property (gobject_class, PROP_WAIT_ON_EOS,
g_param_spec_boolean ("wait-on-eos", "Wait on EOS",
"Wait for all buffers to be processed after receiving an EOS",
DEFAULT_PROP_WAIT_ON_EOS,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
/**
* GstAppSink::eos:
* @appsink: the appsink element that emitted the signal
*
* Signal that the end-of-stream has been reached. This signal is emitted from
* the streaming thread.
*/
gst_app_sink_signals[SIGNAL_EOS] =
g_signal_new ("eos", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (GstAppSinkClass, eos),
NULL, NULL, NULL, G_TYPE_NONE, 0, G_TYPE_NONE);
/**
* GstAppSink::new-preroll:
* @appsink: the appsink element that emitted the signal
*
* Signal that a new preroll sample is available.
*
* This signal is emitted from the streaming thread and only when the
* "emit-signals" property is %TRUE.
*
* The new preroll sample can be retrieved with the "pull-preroll" action
* signal or gst_app_sink_pull_preroll() either from this signal callback
* or from any other thread.
*
* Note that this signal is only emitted when the "emit-signals" property is
* set to %TRUE, which it is not by default for performance reasons.
*/
gst_app_sink_signals[SIGNAL_NEW_PREROLL] =
g_signal_new ("new-preroll", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (GstAppSinkClass, new_preroll),
NULL, NULL, NULL, GST_TYPE_FLOW_RETURN, 0, G_TYPE_NONE);
/**
* GstAppSink::new-sample:
* @appsink: the appsink element that emitted the signal
*
* Signal that a new sample is available.
*
* This signal is emitted from the streaming thread and only when the
* "emit-signals" property is %TRUE.
*
* The new sample can be retrieved with the "pull-sample" action
* signal or gst_app_sink_pull_sample() either from this signal callback
* or from any other thread.
*
* Note that this signal is only emitted when the "emit-signals" property is
* set to %TRUE, which it is not by default for performance reasons.
*/
gst_app_sink_signals[SIGNAL_NEW_SAMPLE] =
g_signal_new ("new-sample", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (GstAppSinkClass, new_sample),
NULL, NULL, NULL, GST_TYPE_FLOW_RETURN, 0, G_TYPE_NONE);
/**
* GstAppSink::pull-preroll:
* @appsink: the appsink element to emit this signal on
*
* Get the last preroll sample in @appsink. This was the sample that caused the
* appsink to preroll in the PAUSED state.
*
* This function is typically used when dealing with a pipeline in the PAUSED
* state. Calling this function after doing a seek will give the sample right
* after the seek position.
*
* Calling this function will clear the internal reference to the preroll
* buffer.
*
* Note that the preroll sample will also be returned as the first sample
* when calling gst_app_sink_pull_sample() or the "pull-sample" action signal.
*
* If an EOS event was received before any buffers, this function returns
* %NULL. Use gst_app_sink_is_eos () to check for the EOS condition.
*
* This function blocks until a preroll sample or EOS is received or the appsink
* element is set to the READY/NULL state.
*
* Returns: a #GstSample or NULL when the appsink is stopped or EOS.
*/
gst_app_sink_signals[SIGNAL_PULL_PREROLL] =
g_signal_new ("pull-preroll", G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstAppSinkClass,
pull_preroll), NULL, NULL, NULL, GST_TYPE_SAMPLE, 0, G_TYPE_NONE);
/**
* GstAppSink::pull-sample:
* @appsink: the appsink element to emit this signal on
*
* This function blocks until a sample or EOS becomes available or the appsink
* element is set to the READY/NULL state.
*
* This function will only return samples when the appsink is in the PLAYING
* state. All rendered samples will be put in a queue so that the application
* can pull samples at its own rate.
*
* Note that when the application does not pull samples fast enough, the
* queued samples could consume a lot of memory, especially when dealing with
* raw video frames. It's possible to control the behaviour of the queue with
* the "drop" and "max-buffers" properties.
*
* If an EOS event was received before any buffers, this function returns
* %NULL. Use gst_app_sink_is_eos () to check for the EOS condition.
*
* Returns: a #GstSample or NULL when the appsink is stopped or EOS.
*/
gst_app_sink_signals[SIGNAL_PULL_SAMPLE] =
g_signal_new ("pull-sample", G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstAppSinkClass,
pull_sample), NULL, NULL, NULL, GST_TYPE_SAMPLE, 0, G_TYPE_NONE);
/**
* GstAppSink::try-pull-preroll:
* @appsink: the appsink element to emit this signal on
* @timeout: the maximum amount of time to wait for the preroll sample
*
* Get the last preroll sample in @appsink. This was the sample that caused the
* appsink to preroll in the PAUSED state.
*
* This function is typically used when dealing with a pipeline in the PAUSED
* state. Calling this function after doing a seek will give the sample right
* after the seek position.
*
* Calling this function will clear the internal reference to the preroll
* buffer.
*
* Note that the preroll sample will also be returned as the first sample
* when calling gst_app_sink_pull_sample() or the "pull-sample" action signal.
*
* If an EOS event was received before any buffers or the timeout expires,
* this function returns %NULL. Use gst_app_sink_is_eos () to check for the EOS
* condition.
*
* This function blocks until a preroll sample or EOS is received, the appsink
* element is set to the READY/NULL state, or the timeout expires.
*
* Returns: a #GstSample or NULL when the appsink is stopped or EOS or the timeout expires.
*
* Since: 1.10
*/
gst_app_sink_signals[SIGNAL_TRY_PULL_PREROLL] =
g_signal_new ("try-pull-preroll", G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
G_STRUCT_OFFSET (GstAppSinkClass, try_pull_preroll), NULL, NULL, NULL,
GST_TYPE_SAMPLE, 1, GST_TYPE_CLOCK_TIME);
/**
* GstAppSink::try-pull-sample:
* @appsink: the appsink element to emit this signal on
* @timeout: the maximum amount of time to wait for a sample
*
* This function blocks until a sample or EOS becomes available or the appsink
* element is set to the READY/NULL state or the timeout expires.
*
* This function will only return samples when the appsink is in the PLAYING
* state. All rendered samples will be put in a queue so that the application
* can pull samples at its own rate.
*
* Note that when the application does not pull samples fast enough, the
* queued samples could consume a lot of memory, especially when dealing with
* raw video frames. It's possible to control the behaviour of the queue with
* the "drop" and "max-buffers" properties.
*
* If an EOS event was received before any buffers or the timeout expires,
* this function returns %NULL. Use gst_app_sink_is_eos () to check
* for the EOS condition.
*
* Returns: a #GstSample or NULL when the appsink is stopped or EOS or the timeout expires.
*
* Since: 1.10
*/
gst_app_sink_signals[SIGNAL_TRY_PULL_SAMPLE] =
g_signal_new ("try-pull-sample", G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
G_STRUCT_OFFSET (GstAppSinkClass, try_pull_sample), NULL, NULL, NULL,
GST_TYPE_SAMPLE, 1, GST_TYPE_CLOCK_TIME);
gst_element_class_set_static_metadata (element_class, "AppSink",
"Generic/Sink", "Allow the application to get access to raw buffer",
"David Schleef <ds@schleef.org>, Wim Taymans <wim.taymans@gmail.com>");
gst_element_class_add_static_pad_template (element_class,
&gst_app_sink_template);
basesink_class->unlock = gst_app_sink_unlock_start;
basesink_class->unlock_stop = gst_app_sink_unlock_stop;
basesink_class->start = gst_app_sink_start;
basesink_class->stop = gst_app_sink_stop;
basesink_class->event = gst_app_sink_event;
basesink_class->preroll = gst_app_sink_preroll;
basesink_class->render = gst_app_sink_render;
basesink_class->render_list = gst_app_sink_render_list;
basesink_class->get_caps = gst_app_sink_getcaps;
basesink_class->set_caps = gst_app_sink_setcaps;
basesink_class->query = gst_app_sink_query;
klass->pull_preroll = gst_app_sink_pull_preroll;
klass->pull_sample = gst_app_sink_pull_sample;
klass->try_pull_preroll = gst_app_sink_try_pull_preroll;
klass->try_pull_sample = gst_app_sink_try_pull_sample;
}
static void
gst_app_sink_init (GstAppSink * appsink)
{
GstAppSinkPrivate *priv;
priv = appsink->priv = gst_app_sink_get_instance_private (appsink);
g_mutex_init (&priv->mutex);
g_cond_init (&priv->cond);
priv->queue = gst_queue_array_new (16);
priv->sample = gst_sample_new (NULL, NULL, NULL, NULL);
priv->emit_signals = DEFAULT_PROP_EMIT_SIGNALS;
priv->max_buffers = DEFAULT_PROP_MAX_BUFFERS;
priv->drop = DEFAULT_PROP_DROP;
priv->wait_on_eos = DEFAULT_PROP_WAIT_ON_EOS;
priv->buffer_lists_supported = DEFAULT_PROP_BUFFER_LIST;
priv->wait_status = NOONE_WAITING;
}
static void
gst_app_sink_dispose (GObject * obj)
{
GstAppSink *appsink = GST_APP_SINK_CAST (obj);
GstAppSinkPrivate *priv = appsink->priv;
GstMiniObject *queue_obj;
Callbacks *callbacks = NULL;
GST_OBJECT_LOCK (appsink);
if (priv->caps) {
gst_caps_unref (priv->caps);
priv->caps = NULL;
}
GST_OBJECT_UNLOCK (appsink);
g_mutex_lock (&priv->mutex);
if (priv->callbacks)
callbacks = g_steal_pointer (&priv->callbacks);
while ((queue_obj = gst_queue_array_pop_head (priv->queue)))
gst_mini_object_unref (queue_obj);
gst_buffer_replace (&priv->preroll_buffer, NULL);
gst_caps_replace (&priv->preroll_caps, NULL);
gst_caps_replace (&priv->last_caps, NULL);
if (priv->sample) {
gst_sample_unref (priv->sample);
priv->sample = NULL;
}
g_mutex_unlock (&priv->mutex);
g_clear_pointer (&callbacks, callbacks_unref);
G_OBJECT_CLASS (parent_class)->dispose (obj);
}
static void
gst_app_sink_finalize (GObject * obj)
{
GstAppSink *appsink = GST_APP_SINK_CAST (obj);
GstAppSinkPrivate *priv = appsink->priv;
g_mutex_clear (&priv->mutex);
g_cond_clear (&priv->cond);
gst_queue_array_free (priv->queue);
G_OBJECT_CLASS (parent_class)->finalize (obj);
}
static void
gst_app_sink_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec)
{
GstAppSink *appsink = GST_APP_SINK_CAST (object);
switch (prop_id) {
case PROP_CAPS:
gst_app_sink_set_caps (appsink, gst_value_get_caps (value));
break;
case PROP_EMIT_SIGNALS:
gst_app_sink_set_emit_signals (appsink, g_value_get_boolean (value));
break;
case PROP_MAX_BUFFERS:
gst_app_sink_set_max_buffers (appsink, g_value_get_uint (value));
break;
case PROP_DROP:
gst_app_sink_set_drop (appsink, g_value_get_boolean (value));
break;
case PROP_BUFFER_LIST:
gst_app_sink_set_buffer_list_support (appsink,
g_value_get_boolean (value));
break;
case PROP_WAIT_ON_EOS:
gst_app_sink_set_wait_on_eos (appsink, g_value_get_boolean (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gst_app_sink_get_property (GObject * object, guint prop_id, GValue * value,
GParamSpec * pspec)
{
GstAppSink *appsink = GST_APP_SINK_CAST (object);
switch (prop_id) {
case PROP_CAPS:
{
GstCaps *caps;
caps = gst_app_sink_get_caps (appsink);
gst_value_set_caps (value, caps);
if (caps)
gst_caps_unref (caps);
break;
}
case PROP_EOS:
g_value_set_boolean (value, gst_app_sink_is_eos (appsink));
break;
case PROP_EMIT_SIGNALS:
g_value_set_boolean (value, gst_app_sink_get_emit_signals (appsink));
break;
case PROP_MAX_BUFFERS:
g_value_set_uint (value, gst_app_sink_get_max_buffers (appsink));
break;
case PROP_DROP:
g_value_set_boolean (value, gst_app_sink_get_drop (appsink));
break;
case PROP_BUFFER_LIST:
g_value_set_boolean (value,
gst_app_sink_get_buffer_list_support (appsink));
break;
case PROP_WAIT_ON_EOS:
g_value_set_boolean (value, gst_app_sink_get_wait_on_eos (appsink));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static gboolean
gst_app_sink_unlock_start (GstBaseSink * bsink)
{
GstAppSink *appsink = GST_APP_SINK_CAST (bsink);
GstAppSinkPrivate *priv = appsink->priv;
g_mutex_lock (&priv->mutex);
GST_DEBUG_OBJECT (appsink, "unlock start");
priv->unlock = TRUE;
g_cond_signal (&priv->cond);
g_mutex_unlock (&priv->mutex);
return TRUE;
}
static gboolean
gst_app_sink_unlock_stop (GstBaseSink * bsink)
{
GstAppSink *appsink = GST_APP_SINK_CAST (bsink);
GstAppSinkPrivate *priv = appsink->priv;
g_mutex_lock (&priv->mutex);
GST_DEBUG_OBJECT (appsink, "unlock stop");
priv->unlock = FALSE;
g_cond_signal (&priv->cond);
g_mutex_unlock (&priv->mutex);
return TRUE;
}
static void
gst_app_sink_flush_unlocked (GstAppSink * appsink)
{
GstMiniObject *obj;
GstAppSinkPrivate *priv = appsink->priv;
GST_DEBUG_OBJECT (appsink, "flush stop appsink");
priv->is_eos = FALSE;
gst_buffer_replace (&priv->preroll_buffer, NULL);
#ifdef GSTREAMER_LITE
// Update last_caps if we have event pending.
// We can get in situation when preroll_caps and last_caps does not match
// and it will break HLS playback after seek. This happens when we received
// caps event (preroll_caps is updated and event stored in queue (see gst_app_sink_setcaps()))
// and then flush (during seek). After, seek is done first preroll buffer will be
// send with preroll_caps and all sequential buffers will be send with last_caps.
while ((obj = gst_queue_array_pop_head (priv->queue))) {
if (GST_IS_EVENT (obj)) {
if (GST_EVENT_TYPE (obj) == GST_EVENT_CAPS) {
GstCaps *caps = NULL;
GstEvent *event = GST_EVENT_CAST (obj);
gst_event_parse_caps (event, &caps);
gst_caps_replace (&priv->last_caps, caps);
gst_sample_set_caps(priv->sample, priv->last_caps);
}
}
gst_mini_object_unref (obj);
}
#else // GSTREAMER_LITE
while ((obj = gst_queue_array_pop_head (priv->queue)))
gst_mini_object_unref (obj);
#endif // GSTREAMER_LITE
priv->num_buffers = 0;
g_cond_signal (&priv->cond);
}
static gboolean
gst_app_sink_start (GstBaseSink * psink)
{
GstAppSink *appsink = GST_APP_SINK_CAST (psink);
GstAppSinkPrivate *priv = appsink->priv;
g_mutex_lock (&priv->mutex);
GST_DEBUG_OBJECT (appsink, "starting");
priv->wait_status = NOONE_WAITING;
priv->flushing = FALSE;
priv->started = TRUE;
gst_segment_init (&priv->preroll_segment, GST_FORMAT_TIME);
gst_segment_init (&priv->last_segment, GST_FORMAT_TIME);
priv->sample = gst_sample_make_writable (priv->sample);
gst_sample_set_buffer (priv->sample, NULL);
gst_sample_set_buffer_list (priv->sample, NULL);
gst_sample_set_caps (priv->sample, NULL);
gst_sample_set_segment (priv->sample, NULL);
g_mutex_unlock (&priv->mutex);
return TRUE;
}
static gboolean
gst_app_sink_stop (GstBaseSink * psink)
{
GstAppSink *appsink = GST_APP_SINK_CAST (psink);
GstAppSinkPrivate *priv = appsink->priv;
g_mutex_lock (&priv->mutex);
GST_DEBUG_OBJECT (appsink, "stopping");
priv->flushing = TRUE;
priv->started = FALSE;
priv->wait_status = NOONE_WAITING;
gst_app_sink_flush_unlocked (appsink);
gst_buffer_replace (&priv->preroll_buffer, NULL);
gst_caps_replace (&priv->preroll_caps, NULL);
gst_caps_replace (&priv->last_caps, NULL);
gst_segment_init (&priv->preroll_segment, GST_FORMAT_UNDEFINED);
gst_segment_init (&priv->last_segment, GST_FORMAT_UNDEFINED);
g_mutex_unlock (&priv->mutex);
return TRUE;
}
static gboolean
gst_app_sink_setcaps (GstBaseSink * sink, GstCaps * caps)
{
GstAppSink *appsink = GST_APP_SINK_CAST (sink);
GstAppSinkPrivate *priv = appsink->priv;
g_mutex_lock (&priv->mutex);
GST_DEBUG_OBJECT (appsink, "receiving CAPS");
gst_queue_array_push_tail (priv->queue, gst_event_new_caps (caps));
if (!priv->preroll_buffer)
gst_caps_replace (&priv->preroll_caps, caps);
g_mutex_unlock (&priv->mutex);
return TRUE;
}
static gboolean
gst_app_sink_event (GstBaseSink * sink, GstEvent * event)
{
GstAppSink *appsink = GST_APP_SINK_CAST (sink);
GstAppSinkPrivate *priv = appsink->priv;
switch (event->type) {
case GST_EVENT_SEGMENT:
g_mutex_lock (&priv->mutex);
GST_DEBUG_OBJECT (appsink, "receiving SEGMENT");
gst_queue_array_push_tail (priv->queue, gst_event_ref (event));
if (!priv->preroll_buffer)
gst_event_copy_segment (event, &priv->preroll_segment);
g_mutex_unlock (&priv->mutex);
break;
case GST_EVENT_EOS:{
gboolean emit = TRUE;
Callbacks *callbacks = NULL;
g_mutex_lock (&priv->mutex);
GST_DEBUG_OBJECT (appsink, "receiving EOS");
priv->is_eos = TRUE;
g_cond_signal (&priv->cond);
g_mutex_unlock (&priv->mutex);
g_mutex_lock (&priv->mutex);
/* wait until all buffers are consumed or we're flushing.
* Otherwise we might signal EOS before all buffers are
* consumed, which is a bit confusing for the application
*/
while (priv->num_buffers > 0 && !priv->flushing && priv->wait_on_eos) {
if (priv->unlock) {
/* we are asked to unlock, call the wait_preroll method */
g_mutex_unlock (&priv->mutex);
if (gst_base_sink_wait_preroll (sink) != GST_FLOW_OK) {
/* Directly go out of here */
gst_event_unref (event);
return FALSE;
}
/* we are allowed to continue now */
g_mutex_lock (&priv->mutex);
continue;
}
priv->wait_status |= STREAM_WAITING;
g_cond_wait (&priv->cond, &priv->mutex);
priv->wait_status &= ~STREAM_WAITING;
}
if (priv->flushing)
emit = FALSE;
if (emit && priv->callbacks)
callbacks = callbacks_ref (priv->callbacks);
g_mutex_unlock (&priv->mutex);
if (emit) {
/* emit EOS now */
if (callbacks && callbacks->callbacks.eos)
callbacks->callbacks.eos (appsink, callbacks->user_data);
else
g_signal_emit (appsink, gst_app_sink_signals[SIGNAL_EOS], 0);
g_clear_pointer (&callbacks, callbacks_unref);
}
break;
}
case GST_EVENT_FLUSH_START:
/* we don't have to do anything here, the base class will call unlock
* which will make sure we exit the _render method */
GST_DEBUG_OBJECT (appsink, "received FLUSH_START");
break;
case GST_EVENT_FLUSH_STOP:
g_mutex_lock (&priv->mutex);
GST_DEBUG_OBJECT (appsink, "received FLUSH_STOP");
gst_app_sink_flush_unlocked (appsink);
g_mutex_unlock (&priv->mutex);
break;
default:
break;
}
return GST_BASE_SINK_CLASS (parent_class)->event (sink, event);
}
static GstFlowReturn
gst_app_sink_preroll (GstBaseSink * psink, GstBuffer * buffer)
{
GstFlowReturn res;
GstAppSink *appsink = GST_APP_SINK_CAST (psink);
GstAppSinkPrivate *priv = appsink->priv;
gboolean emit;
Callbacks *callbacks = NULL;
g_mutex_lock (&priv->mutex);
if (priv->flushing)
goto flushing;
GST_DEBUG_OBJECT (appsink, "setting preroll buffer %p", buffer);
gst_buffer_replace (&priv->preroll_buffer, buffer);
if ((priv->wait_status & APP_WAITING))
g_cond_signal (&priv->cond);
emit = priv->emit_signals;
if (priv->callbacks)
callbacks = callbacks_ref (priv->callbacks);
g_mutex_unlock (&priv->mutex);
if (callbacks && callbacks->callbacks.new_preroll) {
res = callbacks->callbacks.new_preroll (appsink, callbacks->user_data);
} else {
res = GST_FLOW_OK;
if (emit)
g_signal_emit (appsink, gst_app_sink_signals[SIGNAL_NEW_PREROLL], 0,
&res);
}
g_clear_pointer (&callbacks, callbacks_unref);
return res;
flushing:
{
GST_DEBUG_OBJECT (appsink, "we are flushing");
g_mutex_unlock (&priv->mutex);
return GST_FLOW_FLUSHING;
}
}
static GstMiniObject *
dequeue_buffer (GstAppSink * appsink)
{
GstAppSinkPrivate *priv = appsink->priv;
GstMiniObject *obj;
do {
obj = gst_queue_array_pop_head (priv->queue);
if (GST_IS_BUFFER (obj) || GST_IS_BUFFER_LIST (obj)) {
GST_DEBUG_OBJECT (appsink, "dequeued buffer/list %p", obj);
priv->num_buffers--;
break;
} else if (GST_IS_EVENT (obj)) {
GstEvent *event = GST_EVENT_CAST (obj);
switch (GST_EVENT_TYPE (obj)) {
case GST_EVENT_CAPS:
{
GstCaps *caps;
gst_event_parse_caps (event, &caps);
GST_DEBUG_OBJECT (appsink, "activating caps %" GST_PTR_FORMAT, caps);
gst_caps_replace (&priv->last_caps, caps);
priv->sample = gst_sample_make_writable (priv->sample);
gst_sample_set_caps (priv->sample, priv->last_caps);
break;
}
case GST_EVENT_SEGMENT:
gst_event_copy_segment (event, &priv->last_segment);
priv->sample = gst_sample_make_writable (priv->sample);
gst_sample_set_segment (priv->sample, &priv->last_segment);
GST_DEBUG_OBJECT (appsink, "activated segment %" GST_SEGMENT_FORMAT,
&priv->last_segment);
break;
default:
break;
}
gst_mini_object_unref (obj);
}
} while (TRUE);
return obj;
}
static GstFlowReturn
gst_app_sink_render_common (GstBaseSink * psink, GstMiniObject * data,
gboolean is_list)
{
GstFlowReturn ret;
GstAppSink *appsink = GST_APP_SINK_CAST (psink);
GstAppSinkPrivate *priv = appsink->priv;
gboolean emit;
Callbacks *callbacks = NULL;
restart:
g_mutex_lock (&priv->mutex);
if (priv->flushing)
goto flushing;
/* queue holding caps event might have been FLUSHed,
* but caps state still present in pad caps */
if (G_UNLIKELY (!priv->last_caps &&
gst_pad_has_current_caps (GST_BASE_SINK_PAD (psink)))) {
priv->last_caps = gst_pad_get_current_caps (GST_BASE_SINK_PAD (psink));
gst_sample_set_caps (priv->sample, priv->last_caps);
GST_DEBUG_OBJECT (appsink, "activating pad caps %" GST_PTR_FORMAT,
priv->last_caps);
}
GST_DEBUG_OBJECT (appsink, "pushing render buffer/list %p on queue (%d)",
data, priv->num_buffers);
while (priv->max_buffers > 0 && priv->num_buffers >= priv->max_buffers) {
if (priv->drop) {
GstMiniObject *old;
/* we need to drop the oldest buffer/list and try again */
if ((old = dequeue_buffer (appsink))) {
GST_DEBUG_OBJECT (appsink, "dropping old buffer/list %p", old);
gst_mini_object_unref (old);
}
} else {
GST_DEBUG_OBJECT (appsink, "waiting for free space, length %d >= %d",
priv->num_buffers, priv->max_buffers);
if (priv->unlock) {
/* we are asked to unlock, call the wait_preroll method */
g_mutex_unlock (&priv->mutex);
if ((ret = gst_base_sink_wait_preroll (psink)) != GST_FLOW_OK)
goto stopping;
/* we are allowed to continue now */
goto restart;
}
/* wait for a buffer to be removed or flush */
priv->wait_status |= STREAM_WAITING;
g_cond_wait (&priv->cond, &priv->mutex);
priv->wait_status &= ~STREAM_WAITING;
if (priv->flushing)
goto flushing;
}
}
/* we need to ref the buffer/list when pushing it in the queue */
gst_queue_array_push_tail (priv->queue, gst_mini_object_ref (data));
priv->num_buffers++;
if ((priv->wait_status & APP_WAITING))
g_cond_signal (&priv->cond);
emit = priv->emit_signals;
if (priv->callbacks)
callbacks = callbacks_ref (priv->callbacks);
g_mutex_unlock (&priv->mutex);
if (callbacks && callbacks->callbacks.new_sample) {
ret = callbacks->callbacks.new_sample (appsink, callbacks->user_data);
} else {
ret = GST_FLOW_OK;
if (emit)
g_signal_emit (appsink, gst_app_sink_signals[SIGNAL_NEW_SAMPLE], 0, &ret);
}