1 /* GStreamer
2  *
3  * Copyright (C) 2006 Thomas Vander Stichele <thomas at apestaart dot org>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20 
21 #include "config.h"
22 #include <string.h>
23 
24 #include <gst/check/gstcheck.h>
25 #include <gst/audio/audio.h>
26 #include "../../gst/gdp/dataprotocol.c"
27 
28 /* For ease of programming we use globals to keep refs for our floating
29  * src and sink pads we create; otherwise we always have to do get_pad,
30  * get_peer, and then remove references in every test function */
31 static GstPad *mysrcpad, *mysinkpad, *myshsinkpad;
32 
33 #define FORMATS "{ S8, "GST_AUDIO_NE(S16)" }"
34 
35 #define AUDIO_CAPS_TEMPLATE_STRING \
36     "audio/x-raw, " \
37     "format = (string) "FORMATS", " \
38     "rate = (int) [ 1, MAX ], " \
39     "channels = (int) [ 1, 8 ]"
40 
41 #define AUDIO_CAPS_STRING \
42     "audio/x-raw, " \
43     "format = (string) "GST_AUDIO_NE(S16)", " \
44     "rate = (int) 1000, " \
45     "channels = (int) 2"
46 
47 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
48     GST_PAD_SINK,
49     GST_PAD_ALWAYS,
50     GST_STATIC_CAPS (AUDIO_CAPS_TEMPLATE_STRING)
51     );
52 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
53     GST_PAD_SRC,
54     GST_PAD_ALWAYS,
55     GST_STATIC_CAPS ("application/x-gdp")
56     );
57 
58 /* takes over reference for outcaps */
59 static GstElement *
setup_gdpdepay(void)60 setup_gdpdepay (void)
61 {
62   GstElement *gdpdepay;
63 
64   GST_DEBUG ("setup_gdpdepay");
65   gdpdepay = gst_check_setup_element ("gdpdepay");
66   mysrcpad = gst_check_setup_src_pad (gdpdepay, &srctemplate);
67   mysinkpad = gst_check_setup_sink_pad (gdpdepay, &sinktemplate);
68   gst_pad_set_active (mysrcpad, TRUE);
69   gst_pad_set_active (mysinkpad, TRUE);
70 
71   return gdpdepay;
72 }
73 
74 static void
cleanup_gdpdepay(GstElement * gdpdepay)75 cleanup_gdpdepay (GstElement * gdpdepay)
76 {
77   GST_DEBUG ("cleanup_gdpdepay");
78 
79   gst_pad_set_active (mysrcpad, FALSE);
80   if (mysinkpad)
81     gst_pad_set_active (mysinkpad, FALSE);
82   if (myshsinkpad)
83     gst_pad_set_active (myshsinkpad, FALSE);
84   gst_check_teardown_src_pad (gdpdepay);
85   gst_check_teardown_sink_pad (gdpdepay);
86   gst_check_teardown_element (gdpdepay);
87   mysinkpad = NULL;
88   myshsinkpad = NULL;
89 }
90 
91 static void
gdpdepay_push_mem_per_byte(const gchar * reason,GstBuffer * buf,guint nth)92 gdpdepay_push_mem_per_byte (const gchar * reason, GstBuffer * buf, guint nth)
93 {
94   int i;
95   GstBuffer *inbuffer;
96   GstMapInfo map;
97   GstMemory *mem;
98 
99   mem = gst_buffer_peek_memory (buf, nth);
100   fail_unless (mem != NULL);
101 
102   gst_memory_map (mem, &map, GST_MAP_READ);
103 
104   for (i = 0; i < map.size; ++i) {
105     inbuffer = gst_buffer_new_and_alloc (1);
106     gst_buffer_fill (inbuffer, 0, map.data + i, 1);
107     fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK,
108         "%s: failed pushing byte buffer", reason);
109   }
110 
111   gst_memory_unmap (mem, &map);
112 }
113 
GST_START_TEST(test_audio_per_byte)114 GST_START_TEST (test_audio_per_byte)
115 {
116   GstCaps *caps;
117   GstPad *srcpad;
118   GstElement *gdpdepay;
119   GstBuffer *buffer, *outbuffer;
120   GstEvent *event;
121   GstSegment segment;
122 
123   gdpdepay = setup_gdpdepay ();
124   srcpad = gst_element_get_static_pad (gdpdepay, "src");
125 
126   fail_unless (gst_element_set_state (gdpdepay,
127           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
128       "could not set to playing");
129 
130   caps = gst_pad_query_caps (srcpad, NULL);
131   fail_unless (gst_caps_is_any (caps));
132   gst_caps_unref (caps);
133   fail_if (gst_pad_get_current_caps (srcpad));
134 
135   caps = gst_caps_new_empty_simple ("application/x-gdp");
136   gst_check_setup_events (mysrcpad, gdpdepay, caps, GST_FORMAT_BYTES);
137   gst_caps_unref (caps);
138 
139   /* send stream-start event */
140   event = gst_event_new_stream_start ("s-s-id-1234");
141   buffer = gst_dp_payload_event (event, 0);
142   gst_event_unref (event);
143   fail_unless (buffer != NULL);
144   gdpdepay_push_mem_per_byte ("stream-start header", buffer, 0);
145   fail_unless_equals_int (g_list_length (buffers), 0);
146   gdpdepay_push_mem_per_byte ("stream-start payload", buffer, 1);
147   fail_unless_equals_int (g_list_length (buffers), 0);
148   gst_buffer_unref (buffer);
149 
150   /* create caps and buffer packets and push them */
151   caps = gst_caps_from_string (AUDIO_CAPS_STRING);
152   buffer = gst_dp_payload_caps (caps, 0);
153   gst_caps_unref (caps);
154   fail_unless (buffer != NULL);
155   gdpdepay_push_mem_per_byte ("caps header", buffer, 0);
156   fail_unless_equals_int (g_list_length (buffers), 0);
157   gdpdepay_push_mem_per_byte ("caps payload", buffer, 1);
158   fail_unless_equals_int (g_list_length (buffers), 0);
159   caps = gst_pad_query_caps (srcpad, NULL);
160   fail_if (gst_caps_is_any (caps));
161   gst_caps_unref (caps);
162   gst_buffer_unref (buffer);
163 
164   /* send segment */
165   gst_segment_init (&segment, GST_FORMAT_TIME);
166   event = gst_event_new_segment (&segment);
167   buffer = gst_dp_payload_event (event, 0);
168   gst_event_unref (event);
169   fail_unless (buffer != NULL);
170   gdpdepay_push_mem_per_byte ("segment header", buffer, 0);
171   fail_unless_equals_int (g_list_length (buffers), 0);
172   gdpdepay_push_mem_per_byte ("segment payload", buffer, 1);
173   fail_unless_equals_int (g_list_length (buffers), 0);
174   gst_buffer_unref (buffer);
175 
176   buffer = gst_buffer_new_and_alloc (4);
177   gst_buffer_fill (buffer, 0, "f00d", 4);
178   GST_BUFFER_TIMESTAMP (buffer) = GST_SECOND;
179   GST_BUFFER_DURATION (buffer) = GST_SECOND / 10;
180   outbuffer = gst_dp_payload_buffer (buffer, 0);
181   gst_buffer_unref (buffer);
182   fail_unless (outbuffer != NULL);
183   gdpdepay_push_mem_per_byte ("buffer header", outbuffer, 0);
184   fail_unless_equals_int (g_list_length (buffers), 0);
185   gdpdepay_push_mem_per_byte ("buffer payload", outbuffer, 1);
186   gst_buffer_unref (outbuffer);
187 
188   fail_unless_equals_int (g_list_length (buffers), 1);
189   fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
190   fail_unless_equals_uint64 (GST_BUFFER_TIMESTAMP (outbuffer), GST_SECOND);
191   fail_unless_equals_uint64 (GST_BUFFER_DURATION (outbuffer), GST_SECOND / 10);
192 
193   buffers = g_list_remove (buffers, outbuffer);
194   gst_buffer_unref (outbuffer);
195 
196   fail_unless (gst_element_set_state (gdpdepay,
197           GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
198 
199   ASSERT_OBJECT_REFCOUNT (gdpdepay, "gdpdepay", 1);
200   g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL);
201   g_list_free (buffers);
202   buffers = NULL;
203   gst_object_unref (srcpad);
204   cleanup_gdpdepay (gdpdepay);
205 }
206 
207 GST_END_TEST;
208 
GST_START_TEST(test_audio_in_one_buffer)209 GST_START_TEST (test_audio_in_one_buffer)
210 {
211   GstCaps *caps;
212   GstPad *srcpad;
213   GstElement *gdpdepay;
214   GstBuffer *buffer, *inbuffer;
215   GstBuffer *caps_buf, *streamstart_buf, *segment_buf, *data_buf;
216   GstEvent *event;
217   GstSegment segment;
218 
219   gdpdepay = setup_gdpdepay ();
220   srcpad = gst_element_get_static_pad (gdpdepay, "src");
221 
222   fail_unless (gst_element_set_state (gdpdepay,
223           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
224       "could not set to playing");
225 
226   /* make sure no caps are set yet */
227   caps = gst_pad_query_caps (srcpad, NULL);
228   fail_unless (gst_caps_is_any (caps));
229   gst_caps_unref (caps);
230   fail_if (gst_pad_get_current_caps (srcpad));
231 
232   caps = gst_caps_new_empty_simple ("application/x-gdp");
233   gst_check_setup_events (mysrcpad, gdpdepay, caps, GST_FORMAT_BYTES);
234   gst_caps_unref (caps);
235 
236   /* create stream-start event */
237   event = gst_event_new_stream_start ("s-s-id-1234");
238   streamstart_buf = gst_dp_payload_event (event, 0);
239   gst_event_unref (event);
240 
241   /* create caps and buffer packets and push them as one buffer */
242   caps = gst_caps_from_string (AUDIO_CAPS_STRING);
243   caps_buf = gst_dp_payload_caps (caps, 0);
244   gst_caps_unref (caps);
245 
246   /* create segment */
247   gst_segment_init (&segment, GST_FORMAT_TIME);
248   event = gst_event_new_segment (&segment);
249   segment_buf = gst_dp_payload_event (event, 0);
250   gst_event_unref (event);
251 
252   buffer = gst_buffer_new_and_alloc (4);
253   gst_buffer_fill (buffer, 0, "f00d", 4);
254   data_buf = gst_dp_payload_buffer (buffer, 0);
255   gst_buffer_unref (buffer);
256 
257   inbuffer = gst_buffer_append (streamstart_buf, caps_buf);
258   inbuffer = gst_buffer_append (inbuffer, segment_buf);
259   inbuffer = gst_buffer_append (inbuffer, data_buf);
260 
261   /* now push it */
262   gst_pad_push (mysrcpad, inbuffer);
263 
264   /* the buffer is still queued */
265   fail_unless_equals_int (g_list_length (buffers), 1);
266 
267   fail_unless (gst_element_set_state (gdpdepay,
268           GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
269 
270   gst_object_unref (srcpad);
271   g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL);
272   g_list_free (buffers);
273   buffers = NULL;
274   ASSERT_OBJECT_REFCOUNT (gdpdepay, "gdpdepay", 1);
275   cleanup_gdpdepay (gdpdepay);
276 }
277 
278 GST_END_TEST;
279 
280 static GstStaticPadTemplate shsinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
281     GST_PAD_SINK,
282     GST_PAD_ALWAYS,
283     GST_STATIC_CAPS ("application/x-gst-test-streamheader")
284     );
285 
286 static GstElement *
setup_gdpdepay_streamheader(void)287 setup_gdpdepay_streamheader (void)
288 {
289   GstElement *gdpdepay;
290 
291   GST_DEBUG ("setup_gdpdepay");
292   gdpdepay = gst_check_setup_element ("gdpdepay");
293   mysrcpad = gst_check_setup_src_pad (gdpdepay, &srctemplate);
294   myshsinkpad = gst_check_setup_sink_pad (gdpdepay, &shsinktemplate);
295   gst_pad_set_active (mysrcpad, TRUE);
296   gst_pad_set_active (myshsinkpad, TRUE);
297 
298   return gdpdepay;
299 }
300 
301 /* this tests deserialization of a GDP stream where the serialized caps
302  * have a streamheader set */
GST_START_TEST(test_streamheader)303 GST_START_TEST (test_streamheader)
304 {
305   GstCaps *caps;
306   GstPad *srcpad;
307   GstElement *gdpdepay;
308   GstBuffer *buffer, *inbuffer, *outbuffer, *shbuffer;
309   GstBuffer *caps_buf, *ss_buf, *segment_buf, *data_buf;
310   GstEvent *event;
311   GstSegment segment;
312   GstStructure *structure;
313   GValue array = { 0 };
314   GValue value = { 0 };
315 
316   gdpdepay = setup_gdpdepay_streamheader ();
317   srcpad = gst_element_get_static_pad (gdpdepay, "src");
318   ASSERT_OBJECT_REFCOUNT (gdpdepay, "gdpdepay", 1);
319 
320   fail_unless (gst_element_set_state (gdpdepay,
321           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
322       "could not set to playing");
323 
324   /* make sure no caps are set yet */
325   caps = gst_pad_query_caps (srcpad, NULL);
326   fail_unless (gst_caps_is_any (caps));
327   gst_caps_unref (caps);
328   fail_if (gst_pad_get_current_caps (srcpad));
329 
330   caps = gst_caps_new_empty_simple ("application/x-gdp");
331   gst_check_setup_events (mysrcpad, gdpdepay, caps, GST_FORMAT_BYTES);
332   gst_caps_unref (caps);
333 
334   /* create a streamheader buffer and the caps containing it */
335   caps = gst_caps_from_string ("application/x-gst-test-streamheader");
336   structure = gst_caps_get_structure (caps, 0);
337   buffer = gst_buffer_new_and_alloc (4);
338   gst_buffer_fill (buffer, 0, "f00d", 4);
339   GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_HEADER);
340   g_value_init (&array, GST_TYPE_ARRAY);
341   g_value_init (&value, GST_TYPE_BUFFER);
342   shbuffer = gst_buffer_copy (buffer);
343   gst_value_set_buffer (&value, shbuffer);
344   gst_buffer_unref (shbuffer);
345   gst_value_array_append_value (&array, &value);
346   g_value_unset (&value);
347   gst_structure_set_value (structure, "streamheader", &array);
348   g_value_unset (&array);
349 
350 
351   /* basic events */
352   /* create stream-start event */
353   event = gst_event_new_stream_start ("s-s-id-1234");
354   ss_buf = gst_dp_payload_event (event, 0);
355   gst_event_unref (event);
356 
357   /* create segment */
358   gst_segment_init (&segment, GST_FORMAT_TIME);
359   event = gst_event_new_segment (&segment);
360   segment_buf = gst_dp_payload_event (event, 0);
361   gst_event_unref (event);
362 
363   /* create GDP packets for the caps and the buffer, and put them in one
364    * GDP buffer */
365   caps_buf = gst_dp_payload_caps (caps, 0);
366   gst_caps_unref (caps);
367 
368   data_buf = gst_dp_payload_buffer (buffer, 0);
369   gst_buffer_unref (buffer);
370 
371   inbuffer = gst_buffer_append (ss_buf, caps_buf);
372   inbuffer = gst_buffer_append (inbuffer, segment_buf);
373   inbuffer = gst_buffer_append (inbuffer, data_buf);
374 
375   /* now push it */
376   ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
377   gst_pad_push (mysrcpad, inbuffer);
378 
379   /* our only output buffer is the streamheader buffer */
380   fail_unless_equals_int (g_list_length (buffers), 1);
381   fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
382   buffers = g_list_remove (buffers, outbuffer);
383   ASSERT_BUFFER_REFCOUNT (outbuffer, "outbuffer", 1);
384   fail_unless (GST_BUFFER_FLAG_IS_SET (outbuffer, GST_BUFFER_FLAG_HEADER));
385 
386   /* FIXME: get streamheader, compare data with buffer */
387   gst_buffer_unref (outbuffer);
388 
389   /* clean up */
390   fail_unless (gst_element_set_state (gdpdepay,
391           GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
392 
393   gst_object_unref (srcpad);
394   g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL);
395   g_list_free (buffers);
396   buffers = NULL;
397   ASSERT_OBJECT_REFCOUNT (gdpdepay, "gdpdepay", 1);
398   cleanup_gdpdepay (gdpdepay);
399 }
400 
401 GST_END_TEST;
402 
403 static Suite *
gdpdepay_suite(void)404 gdpdepay_suite (void)
405 {
406   Suite *s = suite_create ("gdpdepay");
407   TCase *tc_chain = tcase_create ("general");
408 
409   suite_add_tcase (s, tc_chain);
410   tcase_add_test (tc_chain, test_audio_per_byte);
411   tcase_add_test (tc_chain, test_audio_in_one_buffer);
412   tcase_add_test (tc_chain, test_streamheader);
413 
414   return s;
415 }
416 
417 GST_CHECK_MAIN (gdpdepay);
418