gst_plugins_base/tests/check/libs/rtp.c
changeset 2 5505e8908944
parent 0 0e761a78d257
equal deleted inserted replaced
1:4c282e7dd6d3 2:5505e8908944
       
     1 /* GStreamer unit tests for the RTP support library
       
     2  *
       
     3  * Copyright (C) 2007 Tim-Philipp Müller <tim centricular net>
       
     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., 59 Temple Place - Suite 330,
       
    18  * Boston, MA 02111-1307, USA.
       
    19  */
       
    20 
       
    21 #ifdef HAVE_CONFIG_H
       
    22 #include "config.h"
       
    23 #endif
       
    24 
       
    25 #include <gst/check/gstcheck.h>
       
    26 
       
    27 #include <gst/rtp/gstrtpbuffer.h>
       
    28 #include <string.h>
       
    29 
       
    30 #define RTP_HEADER_LEN 12
       
    31 
       
    32 GST_START_TEST (test_rtp_buffer)
       
    33 {
       
    34   GstBuffer *buf;
       
    35   guint8 *data;
       
    36 
       
    37   /* check GstRTPHeader structure alignment and packing */
       
    38   buf = gst_rtp_buffer_new_allocate (16, 4, 0);
       
    39   fail_unless (buf != NULL);
       
    40   fail_unless_equals_int (GST_BUFFER_SIZE (buf), RTP_HEADER_LEN + 16 + 4);
       
    41   data = GST_BUFFER_DATA (buf);
       
    42 
       
    43   /* check defaults */
       
    44   fail_unless_equals_int (gst_rtp_buffer_get_version (buf), 2);
       
    45   fail_unless (gst_rtp_buffer_get_padding (buf) == FALSE);
       
    46   fail_unless (gst_rtp_buffer_get_extension (buf) == FALSE);
       
    47   fail_unless_equals_int (gst_rtp_buffer_get_csrc_count (buf), 0);
       
    48   fail_unless (gst_rtp_buffer_get_marker (buf) == FALSE);
       
    49   fail_unless (gst_rtp_buffer_get_payload_type (buf) == 0);
       
    50   fail_unless_equals_int (GST_READ_UINT16_BE (data), 0x8000);
       
    51 
       
    52   /* check version in bitfield */
       
    53   gst_rtp_buffer_set_version (buf, 3);
       
    54   fail_unless_equals_int (gst_rtp_buffer_get_version (buf), 3);
       
    55   fail_unless_equals_int ((data[0] & 0xC0) >> 6, 3);
       
    56   gst_rtp_buffer_set_version (buf, 2);
       
    57   fail_unless_equals_int (gst_rtp_buffer_get_version (buf), 2);
       
    58   fail_unless_equals_int ((data[0] & 0xC0) >> 6, 2);
       
    59 
       
    60   /* check padding bit */
       
    61   gst_rtp_buffer_set_padding (buf, TRUE);
       
    62   fail_unless (gst_rtp_buffer_get_padding (buf) == TRUE);
       
    63   fail_unless_equals_int ((data[0] & 0x20) >> 5, 1);
       
    64   gst_rtp_buffer_set_padding (buf, FALSE);
       
    65   fail_unless (gst_rtp_buffer_get_padding (buf) == FALSE);
       
    66   fail_unless_equals_int ((data[0] & 0x20) >> 5, 0);
       
    67 
       
    68   /* check marker bit */
       
    69   gst_rtp_buffer_set_marker (buf, TRUE);
       
    70   fail_unless (gst_rtp_buffer_get_marker (buf) == TRUE);
       
    71   fail_unless_equals_int ((data[1] & 0x80) >> 7, 1);
       
    72   gst_rtp_buffer_set_marker (buf, FALSE);
       
    73   fail_unless (gst_rtp_buffer_get_marker (buf) == FALSE);
       
    74   fail_unless_equals_int ((data[1] & 0x80) >> 7, 0);
       
    75 
       
    76   /* check sequence offset */
       
    77   gst_rtp_buffer_set_seq (buf, 0xF2C9);
       
    78   fail_unless_equals_int (gst_rtp_buffer_get_seq (buf), 0xF2C9);
       
    79   fail_unless_equals_int (GST_READ_UINT16_BE (data + 2), 0xF2C9);
       
    80   gst_rtp_buffer_set_seq (buf, 0);
       
    81   fail_unless_equals_int (gst_rtp_buffer_get_seq (buf), 0);
       
    82   fail_unless_equals_int (GST_READ_UINT16_BE (data + 2), 0);
       
    83 
       
    84   /* check timestamp offset */
       
    85   gst_rtp_buffer_set_timestamp (buf, 432191);
       
    86   fail_unless_equals_int (GST_READ_UINT32_BE (data + 4), 432191);
       
    87   fail_unless_equals_int (gst_rtp_buffer_get_timestamp (buf), 432191);
       
    88   gst_rtp_buffer_set_timestamp (buf, 0);
       
    89   fail_unless_equals_int (gst_rtp_buffer_get_timestamp (buf), 0);
       
    90   fail_unless_equals_int (GST_READ_UINT32_BE (data + 4), 0);
       
    91 
       
    92   /* check ssrc offset */
       
    93   gst_rtp_buffer_set_ssrc (buf, 0xf04043C2);
       
    94   fail_unless_equals_int (gst_rtp_buffer_get_ssrc (buf), (gint) 0xf04043c2);
       
    95   fail_unless_equals_int (GST_READ_UINT32_BE (data + 4 + 4), (gint) 0xf04043c2);
       
    96   gst_rtp_buffer_set_ssrc (buf, 0);
       
    97   fail_unless_equals_int (gst_rtp_buffer_get_ssrc (buf), 0);
       
    98   fail_unless_equals_int (GST_READ_UINT32_BE (data + 4 + 4), 0);
       
    99 
       
   100   /* check csrc bits */
       
   101   fail_unless_equals_int (gst_rtp_buffer_get_csrc_count (buf), 0);
       
   102   ASSERT_CRITICAL (gst_rtp_buffer_get_csrc (buf, 0));
       
   103   fail_unless_equals_int (data[0] & 0xf, 0);
       
   104   gst_buffer_unref (buf);
       
   105 
       
   106   /* and again, this time with CSRCs */
       
   107   buf = gst_rtp_buffer_new_allocate (16, 4, 3);
       
   108   fail_unless (buf != NULL);
       
   109   fail_unless_equals_int (GST_BUFFER_SIZE (buf),
       
   110       RTP_HEADER_LEN + 16 + 4 + 4 * 3);
       
   111 
       
   112   data = GST_BUFFER_DATA (buf);
       
   113 
       
   114   fail_unless_equals_int (gst_rtp_buffer_get_csrc_count (buf), 3);
       
   115   ASSERT_CRITICAL (gst_rtp_buffer_get_csrc (buf, 3));
       
   116   fail_unless_equals_int (data[0] & 0xf, 3);
       
   117   fail_unless_equals_int (gst_rtp_buffer_get_csrc (buf, 0), 0);
       
   118   fail_unless_equals_int (gst_rtp_buffer_get_csrc (buf, 1), 0);
       
   119   fail_unless_equals_int (gst_rtp_buffer_get_csrc (buf, 2), 0);
       
   120 
       
   121   data += RTP_HEADER_LEN;       /* skip the other header stuff */
       
   122   gst_rtp_buffer_set_csrc (buf, 0, 0xf7c0);
       
   123   fail_unless_equals_int (GST_READ_UINT32_BE (data + 0 * 4), 0xf7c0);
       
   124   gst_rtp_buffer_set_csrc (buf, 1, 0xf7c1);
       
   125   fail_unless_equals_int (GST_READ_UINT32_BE (data + 1 * 4), 0xf7c1);
       
   126   gst_rtp_buffer_set_csrc (buf, 2, 0xf7c2);
       
   127   fail_unless_equals_int (GST_READ_UINT32_BE (data + 2 * 4), 0xf7c2);
       
   128   ASSERT_CRITICAL (gst_rtp_buffer_set_csrc (buf, 3, 0xf123));
       
   129   gst_buffer_unref (buf);
       
   130 }
       
   131 
       
   132 GST_END_TEST;
       
   133 
       
   134 GST_START_TEST (test_rtp_buffer_set_extension_data)
       
   135 {
       
   136   GstBuffer *buf;
       
   137   guint8 *data;
       
   138   guint16 bits;
       
   139   guint size;
       
   140   gpointer pointer;
       
   141 
       
   142   /* check GstRTPHeader structure alignment and packing */
       
   143   buf = gst_rtp_buffer_new_allocate (4, 0, 0);
       
   144   data = GST_BUFFER_DATA (buf);
       
   145 
       
   146   /* should be impossible to set the extension data */
       
   147   fail_unless (gst_rtp_buffer_set_extension_data (buf, 0, 4) == FALSE);
       
   148   fail_unless (gst_rtp_buffer_get_extension (buf) == TRUE);
       
   149 
       
   150   /* should be possible to set the extension data */
       
   151   fail_unless (gst_rtp_buffer_set_extension_data (buf, 270, 0) == TRUE);
       
   152   fail_unless (gst_rtp_buffer_get_extension (buf) == TRUE);
       
   153   gst_rtp_buffer_get_extension_data (buf, &bits, &pointer, &size);
       
   154   fail_unless (bits == 270);
       
   155   fail_unless (size == 0);
       
   156   fail_unless (pointer == GST_BUFFER_DATA (buf) + 16);
       
   157   pointer = gst_rtp_buffer_get_payload (buf);
       
   158   fail_unless (pointer == GST_BUFFER_DATA (buf) + 16);
       
   159   gst_buffer_unref (buf);
       
   160 
       
   161   buf = gst_rtp_buffer_new_allocate (20, 0, 0);
       
   162   data = GST_BUFFER_DATA (buf);
       
   163   fail_unless (gst_rtp_buffer_get_extension (buf) == FALSE);
       
   164   fail_unless (gst_rtp_buffer_set_extension_data (buf, 333, 2) == TRUE);
       
   165   fail_unless (gst_rtp_buffer_get_extension (buf) == TRUE);
       
   166   gst_rtp_buffer_get_extension_data (buf, &bits, &pointer, &size);
       
   167   fail_unless (bits == 333);
       
   168   fail_unless (size == 2);
       
   169   fail_unless (pointer == GST_BUFFER_DATA (buf) + 16);
       
   170   pointer = gst_rtp_buffer_get_payload (buf);
       
   171   fail_unless (pointer == GST_BUFFER_DATA (buf) + 24);
       
   172   gst_buffer_unref (buf);
       
   173 }
       
   174 
       
   175 GST_END_TEST;
       
   176 
       
   177 static Suite *
       
   178 rtp_suite (void)
       
   179 {
       
   180   Suite *s = suite_create ("rtp support library");
       
   181   TCase *tc_chain = tcase_create ("general");
       
   182 
       
   183   suite_add_tcase (s, tc_chain);
       
   184   tcase_add_test (tc_chain, test_rtp_buffer);
       
   185   tcase_add_test (tc_chain, test_rtp_buffer_set_extension_data);
       
   186   return s;
       
   187 }
       
   188 
       
   189 int
       
   190 main (int argc, char **argv)
       
   191 {
       
   192   int nf;
       
   193 
       
   194   Suite *s = rtp_suite ();
       
   195   SRunner *sr = srunner_create (s);
       
   196 
       
   197   gst_check_init (&argc, &argv);
       
   198 
       
   199   srunner_run_all (sr, CK_NORMAL);
       
   200   nf = srunner_ntests_failed (sr);
       
   201   srunner_free (sr);
       
   202 
       
   203   return nf;
       
   204 }