File: signals.c

package info (click to toggle)
gst-plugins-base1.0 1.26.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 24,840 kB
  • sloc: ansic: 398,488; cpp: 3,961; objc: 2,502; python: 292; sh: 66; makefile: 51
file content (174 lines) | stat: -rw-r--r-- 5,964 bytes parent folder | download | duplicates (7)
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
#include <gst/gst.h>
#include <gst/base/gstaggregator.h>

#define MAKE_AND_ADD(var, pipe, name, label)                                   \
  G_STMT_START                                                                 \
  {                                                                            \
    GError* make_and_add_err = NULL;                                           \
    if (G_UNLIKELY(!(var = (gst_parse_bin_from_description_full(               \
                       name,                                                   \
                       TRUE,                                                   \
                       NULL,                                                   \
                       GST_PARSE_FLAG_NO_SINGLE_ELEMENT_BINS,                  \
                       &make_and_add_err))))) {                                \
      GST_ERROR(                                                               \
        "Could not create element %s (%s)", name, make_and_add_err->message);  \
      g_clear_error(&make_and_add_err);                                        \
      goto label;                                                              \
    }                                                                          \
    if (G_UNLIKELY(!gst_bin_add(GST_BIN_CAST(pipe), var))) {                   \
      GST_ERROR("Could not add element %s", name);                             \
      goto label;                                                              \
    }                                                                          \
  }                                                                            \
  G_STMT_END

static gboolean
check_aggregated_buffer (GstElement * agg, GstPad * pad,
    GHashTable * consumed_buffers)
{
  GstSample *sample;
  GList *pad_consumed_buffers;
  GList *tmp;

  sample =
      gst_aggregator_peek_next_sample (GST_AGGREGATOR (agg),
      GST_AGGREGATOR_PAD (pad));

  pad_consumed_buffers = g_hash_table_lookup (consumed_buffers, pad);

  for (tmp = pad_consumed_buffers; tmp; tmp = tmp->next) {
    GstBuffer *consumed_buffer = (GstBuffer *) tmp->data;
    gboolean aggregated = FALSE;

    if (sample) {
      aggregated =
          consumed_buffer == gst_sample_get_buffer (sample) ? TRUE : FALSE;
    }

    gst_printerr ("One consumed buffer: %" GST_PTR_FORMAT
        ", it was%s aggregated\n", consumed_buffer, aggregated ? "" : " not");
  }

  if (sample) {
    gst_sample_unref (sample);
  }

  g_list_free_full (pad_consumed_buffers, (GDestroyNotify) gst_buffer_unref);
  g_hash_table_steal (consumed_buffers, pad);

  return TRUE;
}

static void
samples_selected_cb (GstElement * agg, GstSegment * segment, GstClockTime pts,
    GstClockTime dts, GstClockTime duration, GstStructure * info,
    GHashTable * consumed_buffers)
{
  gst_printerr
      ("Compositor has selected the samples it will aggregate for output buffer with PTS %"
      GST_TIME_FORMAT " and duration %" GST_TIME_FORMAT "\n",
      GST_TIME_ARGS (pts), GST_TIME_ARGS (duration));
  gst_element_foreach_sink_pad (agg,
      (GstElementForeachPadFunc) check_aggregated_buffer, consumed_buffers);
}

static void
pad_buffer_consumed_cb (GstAggregatorPad * pad, GstBuffer * buffer,
    GHashTable * consumed_buffers)
{
  GList *pad_consumed_buffers;
  gboolean was_empty;

  pad_consumed_buffers = g_hash_table_lookup (consumed_buffers, pad);

  was_empty = (pad_consumed_buffers == NULL);

  pad_consumed_buffers =
      g_list_append (pad_consumed_buffers, gst_buffer_ref (buffer));

  /* we know the list's head pointer doesn't change when items get appended */
  if (was_empty)
    g_hash_table_insert (consumed_buffers, pad, pad_consumed_buffers);
}

static gboolean
unref_consumed_buffers (gpointer key, GList * pad_consumed_buffers)
{
  g_list_free_full (pad_consumed_buffers, (GDestroyNotify) gst_buffer_unref);

  return TRUE;
}

int
main (int ac, char **av)
{
  int ret = 0;
  GstElement *pipe;
  GstBus *bus;
  GstElement *vsrc, *vcfltr1, *compositor, *vcfltr2, *vsink;
  GstCaps *caps;
  GstPad *pad;
  GHashTable *consumed_buffers =
      g_hash_table_new (g_direct_hash, g_direct_equal);

  gst_init (NULL, NULL);

  pipe = gst_pipeline_new (NULL);

  MAKE_AND_ADD (vsrc, pipe, "videotestsrc", err);
  MAKE_AND_ADD (vcfltr1, pipe, "capsfilter", err);
  MAKE_AND_ADD (compositor, pipe, "compositor", err);
  MAKE_AND_ADD (vcfltr2, pipe, "capsfilter", err);
  MAKE_AND_ADD (vsink, pipe, "autovideosink", err);

  if (!gst_element_link_many (vsrc, vcfltr1, compositor, vcfltr2, vsink, NULL)) {
    GST_ERROR ("Failed to link pipeline");
    goto err;
  }

  caps =
      gst_caps_new_simple ("video/x-raw", "framerate", GST_TYPE_FRACTION, 30, 1,
      NULL);
  g_object_set (vcfltr1, "caps", caps, NULL);
  gst_caps_unref (caps);

  caps =
      gst_caps_new_simple ("video/x-raw", "framerate", GST_TYPE_FRACTION, 6, 1,
      NULL);
  g_object_set (vcfltr2, "caps", caps, NULL);
  gst_caps_unref (caps);

  g_object_set (vsrc, "num-buffers", 300, NULL);

  g_object_set (compositor, "emit-signals", TRUE, NULL);
  g_signal_connect (compositor, "samples-selected",
      G_CALLBACK (samples_selected_cb), consumed_buffers);

  pad = gst_element_get_static_pad (compositor, "sink_0");
  g_object_set (pad, "emit-signals", TRUE, NULL);
  g_signal_connect (pad, "buffer-consumed", G_CALLBACK (pad_buffer_consumed_cb),
      consumed_buffers);
  gst_object_unref (pad);

  gst_element_set_state (pipe, GST_STATE_PLAYING);

  bus = gst_pipeline_get_bus (GST_PIPELINE (pipe));

  gst_message_unref (gst_bus_timed_pop_filtered (bus, GST_CLOCK_TIME_NONE,
          GST_MESSAGE_EOS));

  gst_object_unref (bus);

done:
  g_hash_table_foreach_remove (consumed_buffers,
      (GHRFunc) unref_consumed_buffers, NULL);
  g_hash_table_unref (consumed_buffers);
  gst_element_set_state (pipe, GST_STATE_NULL);
  gst_object_unref (pipe);
  return ret;

err:
  ret = 1;
  goto done;
}