2009-10-22 10:39:58 +00:00
|
|
|
/* GStreamer
|
|
|
|
*
|
|
|
|
* Copyright (C) 2009 Nokia Corporation and its subsidary(-ies)
|
|
|
|
* contact: <stefan.kost@nokia.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., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <gst/check/gstcheck.h>
|
|
|
|
|
|
|
|
/* For ease of programming we use globals to keep refs for our floating
|
|
|
|
* src and sink pads we create; otherwise we always have to do get_pad,
|
|
|
|
* get_peer, and then remove references in every test function */
|
|
|
|
static GstPad *mysrcpad, *mysinkpad;
|
|
|
|
/* we also have a list of src buffers */
|
|
|
|
static GList *inbuffers = NULL;
|
2009-11-25 08:17:34 +00:00
|
|
|
static gint num_dropped = 0;
|
2009-10-22 10:39:58 +00:00
|
|
|
|
|
|
|
#define RTP_CAPS_STRING \
|
|
|
|
"application/x-rtp, " \
|
|
|
|
"media = (string)audio, " \
|
|
|
|
"payload = (int) 0, " \
|
|
|
|
"clock-rate = (int) 8000, " \
|
|
|
|
"encoding-name = (string)PCMU"
|
|
|
|
|
|
|
|
#define RTP_FRAME_SIZE 20
|
|
|
|
|
|
|
|
static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
|
|
|
|
GST_PAD_SINK,
|
|
|
|
GST_PAD_ALWAYS,
|
|
|
|
GST_STATIC_CAPS ("application/x-rtp")
|
|
|
|
);
|
|
|
|
static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
|
|
|
|
GST_PAD_SRC,
|
|
|
|
GST_PAD_ALWAYS,
|
|
|
|
GST_STATIC_CAPS ("application/x-rtp, "
|
|
|
|
"clock-rate = (int) [ 1, 2147483647 ]")
|
|
|
|
);
|
|
|
|
|
2009-11-25 08:17:34 +00:00
|
|
|
static void
|
|
|
|
buffer_dropped (gpointer mem)
|
|
|
|
{
|
|
|
|
if (mem) {
|
|
|
|
GST_DEBUG ("dropping buffer: data=%p", mem);
|
|
|
|
g_free (mem);
|
|
|
|
num_dropped++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-22 10:39:58 +00:00
|
|
|
static GstElement *
|
|
|
|
setup_jitterbuffer (gint num_buffers)
|
|
|
|
{
|
|
|
|
GstElement *jitterbuffer;
|
|
|
|
GstClock *clock;
|
|
|
|
GstBuffer *buffer;
|
|
|
|
GstCaps *caps;
|
2009-11-25 08:17:34 +00:00
|
|
|
/* a 20 sample audio block (2,5 ms) generated with
|
2011-08-19 12:01:45 +00:00
|
|
|
* gst-launch audiotestsrc wave=silence blocksize=40 num-buffers=3 !
|
|
|
|
* "audio/x-raw,channels=1,rate=8000" ! mulawenc ! rtppcmupay !
|
2009-10-22 10:39:58 +00:00
|
|
|
* fakesink dump=1
|
|
|
|
*/
|
|
|
|
guint8 in[] = { /* first 4 bytes are rtp-header, next 4 bytes are timestamp */
|
|
|
|
0x80, 0x80, 0x1c, 0x24, 0x46, 0xcd, 0xb7, 0x11, 0x3c, 0x3a, 0x7c, 0x5b,
|
|
|
|
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
|
|
|
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
|
|
|
|
};
|
|
|
|
GstClockTime ts = G_GUINT64_CONSTANT (0);
|
|
|
|
GstClockTime tso = gst_util_uint64_scale (RTP_FRAME_SIZE, GST_SECOND, 8000);
|
2009-11-25 08:17:34 +00:00
|
|
|
/*guint latency = GST_TIME_AS_MSECONDS (num_buffers * tso); */
|
2009-10-22 10:39:58 +00:00
|
|
|
gint i;
|
|
|
|
|
|
|
|
GST_DEBUG ("setup_jitterbuffer");
|
|
|
|
jitterbuffer = gst_check_setup_element ("gstrtpjitterbuffer");
|
|
|
|
/* we need a clock here */
|
|
|
|
clock = gst_system_clock_obtain ();
|
|
|
|
gst_element_set_clock (jitterbuffer, clock);
|
|
|
|
gst_object_unref (clock);
|
|
|
|
/* setup latency */
|
|
|
|
/* latency would be 7 for 3 buffers here, default is 200
|
|
|
|
g_object_set (G_OBJECT (jitterbuffer), "latency", latency, NULL);
|
|
|
|
GST_INFO_OBJECT (jitterbuffer, "set latency to %u ms", latency);
|
|
|
|
*/
|
|
|
|
|
|
|
|
mysrcpad = gst_check_setup_src_pad (jitterbuffer, &srctemplate, NULL);
|
|
|
|
mysinkpad = gst_check_setup_sink_pad (jitterbuffer, &sinktemplate, NULL);
|
|
|
|
gst_pad_set_active (mysrcpad, TRUE);
|
|
|
|
gst_pad_set_active (mysinkpad, TRUE);
|
|
|
|
|
|
|
|
/* create n buffers */
|
|
|
|
caps = gst_caps_from_string (RTP_CAPS_STRING);
|
|
|
|
for (i = 0; i < num_buffers; i++) {
|
|
|
|
buffer = gst_buffer_new_and_alloc (sizeof (in));
|
|
|
|
memcpy (GST_BUFFER_DATA (buffer), in, sizeof (in));
|
|
|
|
gst_buffer_set_caps (buffer, caps);
|
|
|
|
GST_BUFFER_TIMESTAMP (buffer) = ts;
|
|
|
|
GST_BUFFER_DURATION (buffer) = tso;
|
2009-11-25 08:17:34 +00:00
|
|
|
GST_BUFFER_FREE_FUNC (buffer) = buffer_dropped;
|
|
|
|
GST_DEBUG ("created buffer: %p, data=%p", buffer, GST_BUFFER_DATA (buffer));
|
2009-10-22 10:39:58 +00:00
|
|
|
|
|
|
|
if (!i)
|
|
|
|
GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
|
|
|
|
|
|
|
|
inbuffers = g_list_append (inbuffers, buffer);
|
|
|
|
|
|
|
|
/* hackish way to update the rtp header */
|
|
|
|
in[1] = 0x00;
|
|
|
|
in[3]++; /* seqnumber */
|
|
|
|
in[7] += RTP_FRAME_SIZE; /* inc. timestamp with framesize */
|
|
|
|
ts += tso;
|
|
|
|
}
|
|
|
|
gst_caps_unref (caps);
|
2009-11-25 08:17:34 +00:00
|
|
|
num_dropped = 0;
|
2009-10-22 10:39:58 +00:00
|
|
|
|
|
|
|
return jitterbuffer;
|
|
|
|
}
|
|
|
|
|
2009-11-28 11:25:06 +00:00
|
|
|
static GstStateChangeReturn
|
|
|
|
start_jitterbuffer (GstElement * jitterbuffer)
|
|
|
|
{
|
|
|
|
GstStateChangeReturn ret;
|
|
|
|
GstClockTime now;
|
|
|
|
GstClock *clock;
|
|
|
|
|
|
|
|
clock = gst_element_get_clock (jitterbuffer);
|
|
|
|
now = gst_clock_get_time (clock);
|
|
|
|
gst_object_unref (clock);
|
|
|
|
|
|
|
|
gst_element_set_base_time (jitterbuffer, now);
|
|
|
|
ret = gst_element_set_state (jitterbuffer, GST_STATE_PLAYING);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-10-22 10:39:58 +00:00
|
|
|
static void
|
|
|
|
cleanup_jitterbuffer (GstElement * jitterbuffer)
|
|
|
|
{
|
|
|
|
GST_DEBUG ("cleanup_jitterbuffer");
|
|
|
|
|
|
|
|
g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL);
|
|
|
|
g_list_free (buffers);
|
|
|
|
buffers = NULL;
|
|
|
|
|
|
|
|
g_list_free (inbuffers);
|
|
|
|
inbuffers = NULL;
|
|
|
|
|
|
|
|
gst_pad_set_active (mysrcpad, FALSE);
|
|
|
|
gst_pad_set_active (mysinkpad, FALSE);
|
|
|
|
gst_check_teardown_src_pad (jitterbuffer);
|
|
|
|
gst_check_teardown_sink_pad (jitterbuffer);
|
|
|
|
gst_check_teardown_element (jitterbuffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
check_jitterbuffer_results (GstElement * jitterbuffer, gint num_buffers)
|
|
|
|
{
|
|
|
|
GstBuffer *buffer;
|
|
|
|
GList *node;
|
|
|
|
GstClockTime ts = G_GUINT64_CONSTANT (0);
|
|
|
|
GstClockTime tso = gst_util_uint64_scale (RTP_FRAME_SIZE, GST_SECOND, 8000);
|
|
|
|
guint8 *data;
|
|
|
|
guint16 prev_sn = 0, cur_sn;
|
|
|
|
guint32 prev_ts = 0, cur_ts;
|
|
|
|
|
2009-11-28 11:25:06 +00:00
|
|
|
/* sleep for twice the latency */
|
|
|
|
g_usleep (400 * 1000);
|
2009-11-25 08:17:34 +00:00
|
|
|
|
|
|
|
GST_INFO ("of %d buffer %d/%d received/dropped", num_buffers,
|
|
|
|
g_list_length (buffers), num_dropped);
|
|
|
|
/* if this fails, not all buffers have been processed */
|
|
|
|
fail_unless_equals_int ((g_list_length (buffers) + num_dropped), num_buffers);
|
|
|
|
|
2009-10-22 10:39:58 +00:00
|
|
|
/* check the buffer list */
|
|
|
|
fail_unless_equals_int (g_list_length (buffers), num_buffers);
|
|
|
|
for (node = buffers; node; node = g_list_next (node)) {
|
|
|
|
fail_if ((buffer = (GstBuffer *) node->data) == NULL);
|
|
|
|
fail_if (GST_BUFFER_TIMESTAMP (buffer) != ts);
|
|
|
|
data = GST_BUFFER_DATA (buffer);
|
|
|
|
cur_sn = ((guint16) data[2] << 8) | data[3];
|
|
|
|
cur_ts = ((guint32) data[4] << 24) | ((guint32) data[5] << 16) |
|
|
|
|
((guint32) data[6] << 8) | data[7];
|
|
|
|
|
|
|
|
if (node != buffers) {
|
|
|
|
fail_unless (cur_sn > prev_sn);
|
|
|
|
fail_unless (cur_ts > prev_ts);
|
|
|
|
|
|
|
|
prev_sn = cur_sn;
|
|
|
|
prev_ts = cur_ts;
|
|
|
|
}
|
|
|
|
ts += tso;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_START_TEST (test_push_forward_seq)
|
|
|
|
{
|
|
|
|
GstElement *jitterbuffer;
|
|
|
|
const guint num_buffers = 3;
|
|
|
|
GstBuffer *buffer;
|
|
|
|
GList *node;
|
|
|
|
|
|
|
|
jitterbuffer = setup_jitterbuffer (num_buffers);
|
2009-11-28 11:25:06 +00:00
|
|
|
fail_unless (start_jitterbuffer (jitterbuffer)
|
|
|
|
== GST_STATE_CHANGE_SUCCESS, "could not set to playing");
|
2009-10-22 10:39:58 +00:00
|
|
|
|
|
|
|
/* push buffers: 0,1,2, */
|
|
|
|
for (node = inbuffers; node; node = g_list_next (node)) {
|
|
|
|
buffer = (GstBuffer *) node->data;
|
|
|
|
fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check the buffer list */
|
|
|
|
check_jitterbuffer_results (jitterbuffer, num_buffers);
|
|
|
|
|
|
|
|
/* cleanup */
|
|
|
|
cleanup_jitterbuffer (jitterbuffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_END_TEST;
|
|
|
|
|
|
|
|
GST_START_TEST (test_push_backward_seq)
|
|
|
|
{
|
|
|
|
GstElement *jitterbuffer;
|
2009-11-25 14:05:10 +00:00
|
|
|
const guint num_buffers = 4;
|
2009-10-22 10:39:58 +00:00
|
|
|
GstBuffer *buffer;
|
|
|
|
GList *node;
|
|
|
|
|
|
|
|
jitterbuffer = setup_jitterbuffer (num_buffers);
|
2009-11-28 11:25:06 +00:00
|
|
|
fail_unless (start_jitterbuffer (jitterbuffer)
|
|
|
|
== GST_STATE_CHANGE_SUCCESS, "could not set to playing");
|
2009-10-22 10:39:58 +00:00
|
|
|
|
2009-11-25 14:05:10 +00:00
|
|
|
/* push buffers: 0,3,2,1 */
|
|
|
|
buffer = (GstBuffer *) inbuffers->data;
|
|
|
|
fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
|
|
|
|
for (node = g_list_last (inbuffers); node != inbuffers;
|
|
|
|
node = g_list_previous (node)) {
|
2009-10-22 10:39:58 +00:00
|
|
|
buffer = (GstBuffer *) node->data;
|
|
|
|
fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check the buffer list */
|
|
|
|
check_jitterbuffer_results (jitterbuffer, num_buffers);
|
|
|
|
|
|
|
|
/* cleanup */
|
|
|
|
cleanup_jitterbuffer (jitterbuffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_END_TEST;
|
|
|
|
|
|
|
|
GST_START_TEST (test_push_unordered)
|
|
|
|
{
|
|
|
|
GstElement *jitterbuffer;
|
2009-11-25 14:05:10 +00:00
|
|
|
const guint num_buffers = 4;
|
2009-10-22 10:39:58 +00:00
|
|
|
GstBuffer *buffer;
|
|
|
|
|
|
|
|
jitterbuffer = setup_jitterbuffer (num_buffers);
|
2009-11-28 11:25:06 +00:00
|
|
|
fail_unless (start_jitterbuffer (jitterbuffer)
|
|
|
|
== GST_STATE_CHANGE_SUCCESS, "could not set to playing");
|
2009-10-22 10:39:58 +00:00
|
|
|
|
2009-11-25 14:05:10 +00:00
|
|
|
/* push buffers; 0,2,1,3 */
|
2009-10-22 10:39:58 +00:00
|
|
|
buffer = (GstBuffer *) inbuffers->data;
|
|
|
|
fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
|
|
|
|
buffer = g_list_nth_data (inbuffers, 2);
|
|
|
|
fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
|
|
|
|
buffer = g_list_nth_data (inbuffers, 1);
|
|
|
|
fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
|
2009-11-25 14:05:10 +00:00
|
|
|
buffer = g_list_nth_data (inbuffers, 3);
|
|
|
|
fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
|
2009-10-22 10:39:58 +00:00
|
|
|
|
|
|
|
/* check the buffer list */
|
|
|
|
check_jitterbuffer_results (jitterbuffer, num_buffers);
|
|
|
|
|
|
|
|
/* cleanup */
|
|
|
|
cleanup_jitterbuffer (jitterbuffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_END_TEST;
|
|
|
|
|
2009-11-25 14:05:10 +00:00
|
|
|
GST_START_TEST (test_basetime)
|
|
|
|
{
|
|
|
|
GstElement *jitterbuffer;
|
|
|
|
const guint num_buffers = 3;
|
|
|
|
GstBuffer *buffer;
|
|
|
|
GList *node;
|
|
|
|
GstClockTime tso = gst_util_uint64_scale (RTP_FRAME_SIZE, GST_SECOND, 8000);
|
|
|
|
|
|
|
|
jitterbuffer = setup_jitterbuffer (num_buffers);
|
2009-11-28 11:25:06 +00:00
|
|
|
fail_unless (start_jitterbuffer (jitterbuffer)
|
|
|
|
== GST_STATE_CHANGE_SUCCESS, "could not set to playing");
|
2009-11-25 14:05:10 +00:00
|
|
|
|
|
|
|
/* push buffers: 2,1,0 */
|
|
|
|
for (node = g_list_last (inbuffers); node; node = g_list_previous (node)) {
|
|
|
|
buffer = (GstBuffer *) node->data;
|
|
|
|
fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
|
|
|
|
}
|
|
|
|
|
2009-11-28 11:25:06 +00:00
|
|
|
/* sleep for twice the latency */
|
|
|
|
g_usleep (400 * 1000);
|
2009-11-25 14:05:10 +00:00
|
|
|
|
|
|
|
/* if this fails, not all buffers have been processed */
|
|
|
|
fail_unless_equals_int ((g_list_length (buffers) + num_dropped), num_buffers);
|
|
|
|
|
|
|
|
buffer = (GstBuffer *) buffers->data;
|
|
|
|
fail_unless (GST_BUFFER_TIMESTAMP (buffer) != (num_buffers * tso));
|
|
|
|
|
|
|
|
/* cleanup */
|
|
|
|
cleanup_jitterbuffer (jitterbuffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_END_TEST;
|
|
|
|
|
|
|
|
|
2009-10-22 10:39:58 +00:00
|
|
|
static Suite *
|
|
|
|
rtpjitterbuffer_suite (void)
|
|
|
|
{
|
|
|
|
Suite *s = suite_create ("rtpjitterbuffer");
|
|
|
|
TCase *tc_chain = tcase_create ("general");
|
|
|
|
|
|
|
|
suite_add_tcase (s, tc_chain);
|
2009-11-25 08:17:34 +00:00
|
|
|
tcase_add_test (tc_chain, test_push_forward_seq);
|
2009-11-28 11:25:06 +00:00
|
|
|
tcase_add_test (tc_chain, test_push_backward_seq);
|
|
|
|
tcase_add_test (tc_chain, test_push_unordered);
|
|
|
|
tcase_add_test (tc_chain, test_basetime);
|
2009-10-22 10:39:58 +00:00
|
|
|
|
|
|
|
/* FIXME: test buffer lists */
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_CHECK_MAIN (rtpjitterbuffer);
|