gstreamer_core/tsrc/check/gst/gstsystemclock/src/gstsystemclock.c
changeset 8 4a7fac7dd34a
parent 2 5505e8908944
child 30 7e817e7e631c
child 34 1b8125c02661
equal deleted inserted replaced
7:71e347f905f2 8:4a7fac7dd34a
     1 /*
     1  /*
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
     2  *  Copyright © 2008 Nokia Corporation.
     3 *
     3  *  This material, including documentation and any related 
     4 * This library is free software; you can redistribute it and/or
     4  *  computer programs, is protected by copyright controlled by 
     5 * modify it under the terms of the GNU Lesser General Public
     5  *  Nokia Corporation. All rights are reserved. Copying, 
     6 * License as published by the Free Software Foundation; either
     6  *  including reproducing, storing, adapting or translating, any 
     7 * version 2 of the License, or (at your option) any later version.
     7  *  or all of this material requires the prior written consent of 
     8 *
     8  *  Nokia Corporation. This material also contains confidential 
     9 * This library is distributed in the hope that it will be useful,
     9  *  information which may not be disclosed to others without the 
    10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  *  prior written consent of Nokia Corporation.
    11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    11  * ============================================================================
    12 * Lesser General Public License for more details.
    12  */
    13 *
       
    14 * You should have received a copy of the GNU Lesser General Public
       
    15 * License along with this library; if not, write to the
       
    16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
       
    17 * Boston, MA 02111-1307, USA.
       
    18 *
       
    19 * Description:
       
    20 *
       
    21 */
       
    22 
    13 
    23 /* GStreamer
    14 /* GStreamer
    24  * Copyright (C) 2005 Wim Taymans <wim@fluendo.com>
    15  * Copyright (C) 2005 Wim Taymans <wim@fluendo.com>
    25  *
    16  *
    26  * gstsystemclock.c: Unit test for GstSystemClock
    17  * gstsystemclock.c: Unit test for GstSystemClock
    51 
    42 
    52 //char* xmlfile = "gstsystemclock";
    43 //char* xmlfile = "gstsystemclock";
    53 
    44 
    54 void create_xml(int result)
    45 void create_xml(int result)
    55 {
    46 {
       
    47 
    56     if(result)
    48     if(result)
       
    49     {
    57         assert_failed = 1;
    50         assert_failed = 1;
    58     
    51     } 
       
    52 
    59     testResultXml(xmlfile);
    53     testResultXml(xmlfile);
    60     close_log_file();
    54     close_log_file();
       
    55 
       
    56     if(result)
       
    57     {
       
    58         exit (-1);
       
    59     }    
       
    60 
    61 }
    61 }
    62 
    62 
    63 
    63 
    64 /* see if the defines make sense */
    64 /* see if the defines make sense */
    65 void test_range()
    65 void test_range()
    92   std_log(LOG_FILENAME_LINE, "Test Successful");
    92   std_log(LOG_FILENAME_LINE, "Test Successful");
    93   create_xml(0);
    93   create_xml(0);
    94 }
    94 }
    95 
    95 
    96 
    96 
    97 #define TIME_UNIT (GST_SECOND / 5)
    97 #define TIME_UNIT (GST_SECOND)
    98 static void
    98 static void
    99 gst_clock_debug (GstClock * clock)
    99 gst_clock_debug (GstClock * clock)
   100 {
   100 {
   101   GstClockTime time;
   101   GstClockTime time;
   102 
   102 
   120   fail_if (TRUE);
   120   fail_if (TRUE);
   121 
   121 
   122   return FALSE;
   122   return FALSE;
   123 }
   123 }
   124 
   124 
       
   125 GMutex *store_lock;
   125 static gboolean
   126 static gboolean
   126 store_callback (GstClock * clock, GstClockTime time,
   127 store_callback (GstClock * clock, GstClockTime time,
   127     GstClockID id, gpointer user_data)
   128     GstClockID id, gpointer user_data)
   128 {
   129 {
   129   GList **list = user_data;
   130   GList **list = user_data;
   130 
   131 
   131   GST_DEBUG ("unlocked async id %p", id);
   132   GST_DEBUG ("unlocked async id %p", id);
       
   133   g_mutex_lock (store_lock);
   132   *list = g_list_append (*list, id);
   134   *list = g_list_append (*list, id);
       
   135   g_mutex_unlock (store_lock);
   133   return FALSE;
   136   return FALSE;
   134 }
   137 }
   135 
   138 
   136 static gboolean
   139 static gboolean
   137 notify_callback (GstClock * clock, GstClockTime time,
   140 notify_callback (GstClock * clock, GstClockTime time,
   149 {
   152 {
   150   GstClock *clock;
   153   GstClock *clock;
   151   GstClockID id, id2;
   154   GstClockID id, id2;
   152   GstClockTime base;
   155   GstClockTime base;
   153   GstClockReturn result;
   156   GstClockReturn result;
       
   157   GstClockTime        time_my;
       
   158   int value;
   154   
   159   
   155 	xmlfile = "test_single_shot";
   160 	xmlfile = "test_single_shot";
   156   std_log(LOG_FILENAME_LINE, "Test Started test_single_shot");
   161   std_log(LOG_FILENAME_LINE, "Test Started test_single_shot");
       
   162   GST_DEBUG ("test_single_shot started ");
   157 
   163 
   158   clock = gst_system_clock_obtain ();
   164   clock = gst_system_clock_obtain ();
   159   fail_unless (clock != NULL, "Could not create instance of GstSystemClock");
   165   fail_unless (clock != NULL, "Could not create instance of GstSystemClock");
   160   
   166   
   161 
   167 
   182   fail_unless (result == GST_CLOCK_EARLY,
   188   fail_unless (result == GST_CLOCK_EARLY,
   183       "Waiting did not return EARLY(result=%d)", result);
   189       "Waiting did not return EARLY(result=%d)", result);
   184       
   190       
   185   gst_clock_id_unref (id);
   191   gst_clock_id_unref (id);
   186 
   192 
   187   id = gst_clock_new_single_shot_id (clock, base + 2 * TIME_UNIT);
   193     id = gst_clock_new_single_shot_id (clock, base + (2 * TIME_UNIT));
   188   GST_DEBUG ("waiting one second async id %p", id);
   194   GST_DEBUG ("waiting one second async id %p", id);
   189   result = gst_clock_id_wait_async (id, ok_callback, NULL);
   195   result = gst_clock_id_wait_async (id, ok_callback, NULL);
   190   gst_clock_id_unref (id);
   196   fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
   191   fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
   197   
   192   
   198   g_usleep (TIME_UNIT / (2 * 1000));
   193   g_usleep (TIME_UNIT / (2 * 1000));
   199     gst_clock_id_unschedule (id);
   194 
   200     gst_clock_id_unref (id);
   195   id = gst_clock_new_single_shot_id (clock, base + 5 * TIME_UNIT);
   201 
       
   202     id = gst_clock_new_single_shot_id (clock, base + (5 * TIME_UNIT));
   196   GST_DEBUG ("waiting one second async, with cancel on id %p", id);
   203   GST_DEBUG ("waiting one second async, with cancel on id %p", id);
   197   result = gst_clock_id_wait_async (id, error_callback, NULL);
   204   result = gst_clock_id_wait_async (id, error_callback, NULL);
   198   fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
   205   fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
   199   
   206   
   200   g_usleep (TIME_UNIT / (2 * 1000));
   207   g_usleep (TIME_UNIT / (2 * 1000));
   202   gst_clock_id_unschedule (id);
   209   gst_clock_id_unschedule (id);
   203   gst_clock_id_unref (id);
   210   gst_clock_id_unref (id);
   204   GST_DEBUG ("canceled id %p", id);
   211   GST_DEBUG ("canceled id %p", id);
   205 
   212 
   206   GST_DEBUG ("waiting multiple one second async, with cancel");
   213   GST_DEBUG ("waiting multiple one second async, with cancel");
   207   id = gst_clock_new_single_shot_id (clock, base + 5 * TIME_UNIT);
   214     id = gst_clock_new_single_shot_id (clock, base + (5 * TIME_UNIT));
   208   id2 = gst_clock_new_single_shot_id (clock, base + 6 * TIME_UNIT);
   215     id2 = gst_clock_new_single_shot_id (clock, base + (6 * TIME_UNIT));
   209   GST_DEBUG ("waiting id %p", id);
   216   GST_DEBUG ("waiting id %p", id);
   210   result = gst_clock_id_wait_async (id, ok_callback, NULL);
   217   result = gst_clock_id_wait_async (id, ok_callback, NULL);
   211   fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
   218   fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
   212   
   219   
   213   gst_clock_id_unref (id);
       
   214   GST_DEBUG ("waiting id %p", id2);
   220   GST_DEBUG ("waiting id %p", id2);
   215   result = gst_clock_id_wait_async (id2, error_callback, NULL);
   221   result = gst_clock_id_wait_async (id2, error_callback, NULL);
   216   fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
   222   fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
   217   
   223   
   218   g_usleep (TIME_UNIT / (2 * 1000));
   224   g_usleep (TIME_UNIT / (2 * 1000));
   219   GST_DEBUG ("cancel id %p after half a time unit", id2);
   225   GST_DEBUG ("cancel id %p after half a time unit", id2);
   220   gst_clock_id_unschedule (id2);
   226   gst_clock_id_unschedule (id2);
   221   GST_DEBUG ("canceled id %p", id2);
   227   GST_DEBUG ("canceled id %p", id2);
   222   gst_clock_id_unref (id2);
   228   gst_clock_id_unref (id2);
   223   g_usleep (TIME_UNIT / (2 * 1000));
   229     g_usleep (TIME_UNIT / 1000 * 5);
   224   
   230     fail_unless (((GstClockEntry *) id)->status == GST_CLOCK_OK,
       
   231         "Waiting did not finish");
       
   232     gst_clock_id_unref (id);
       
   233   
       
   234     gst_object_unref (clock);
       
   235   GST_DEBUG ("test_single_shot finished ");
   225   std_log(LOG_FILENAME_LINE, "Test Successful");
   236   std_log(LOG_FILENAME_LINE, "Test Successful");
   226   create_xml(0);
   237   create_xml(0);
   227 }
   238 }
   228 
   239 
   229 
   240 
   315 
   326 
   316 	xmlfile = "test_async_order";
   327 	xmlfile = "test_async_order";
   317   std_log(LOG_FILENAME_LINE, "Test Started test_async_order");
   328   std_log(LOG_FILENAME_LINE, "Test Started test_async_order");
   318   
   329   
   319   
   330   
       
   331   store_lock = g_mutex_new ();
   320   clock = gst_system_clock_obtain ();
   332   clock = gst_system_clock_obtain ();
   321   fail_unless (clock != NULL, "Could not create instance of GstSystemClock");
   333   fail_unless (clock != NULL, "Could not create instance of GstSystemClock");
   322   
   334   
   323 
   335 
   324   gst_clock_debug (clock);
   336   gst_clock_debug (clock);
   333   result = gst_clock_id_wait_async (id2, store_callback, &cb_list);
   345   result = gst_clock_id_wait_async (id2, store_callback, &cb_list);
   334   fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
   346   fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
   335   
   347   
   336   g_usleep (TIME_UNIT / 1000);
   348   g_usleep (TIME_UNIT / 1000);
   337   // at this point at least one of the timers should have timed out 
   349   // at this point at least one of the timers should have timed out 
       
   350     g_mutex_lock (store_lock);
   338   fail_unless (cb_list != NULL, "expected notification");
   351   fail_unless (cb_list != NULL, "expected notification");
   339   
   352   
   340   fail_unless (cb_list->data == id2,
   353   fail_unless (cb_list->data == id2,
   341       "Expected notification for id2 to come first");
   354       "Expected notification for id2 to come first");
   342       
   355     g_mutex_unlock (store_lock);
   343   g_usleep (TIME_UNIT / 1000);
   356   g_usleep (TIME_UNIT / 1000);
       
   357     g_mutex_lock (store_lock);
   344   // now both should have timed out 
   358   // now both should have timed out 
   345   next = g_list_next (cb_list);
   359   next = g_list_next (cb_list);
   346   fail_unless (next != NULL, "expected second notification");
   360   fail_unless (next != NULL, "expected second notification");
   347   
   361   
   348   fail_unless (next->data == id1, "Missing notification for id1");
   362   fail_unless (next->data == id1, "Missing notification for id1");
       
   363     g_mutex_unlock (store_lock);
   349   
   364   
   350   gst_clock_id_unref (id1);
   365   gst_clock_id_unref (id1);
   351   gst_clock_id_unref (id2);
   366   gst_clock_id_unref (id2);
   352   g_list_free (cb_list);
   367   g_list_free (cb_list);
       
   368     gst_object_unref (clock);
       
   369     g_mutex_free (store_lock);
   353   
   370   
   354   std_log(LOG_FILENAME_LINE, "Test Successful");
   371   std_log(LOG_FILENAME_LINE, "Test Successful");
   355   create_xml(0);
   372   create_xml(0);
   356 }
   373 }
   357 
   374 
   453   GstClockID id;
   470   GstClockID id;
   454   
   471   
   455   xmlfile = "test_mixed";
   472   xmlfile = "test_mixed";
   456   std_log(LOG_FILENAME_LINE, "Test Started test_mixed");
   473   std_log(LOG_FILENAME_LINE, "Test Started test_mixed");
   457 
   474 
   458 
   475   GST_DEBUG ("test_mixed started ");
   459   info.clock = gst_system_clock_obtain ();
   476   info.clock = gst_system_clock_obtain ();
   460   fail_unless (info.clock != NULL,
   477   fail_unless (info.clock != NULL,
   461       "Could not create instance of GstSystemClock");
   478       "Could not create instance of GstSystemClock");
   462       
   479       
   463 
   480 
   493   gst_clock_id_unref (id);
   510   gst_clock_id_unref (id);
   494   gst_clock_id_unref (info.id);
   511   gst_clock_id_unref (info.id);
   495   gst_object_unref (info.clock);
   512   gst_object_unref (info.clock);
   496   
   513   
   497   std_log(LOG_FILENAME_LINE, "Test Successful");
   514   std_log(LOG_FILENAME_LINE, "Test Successful");
       
   515   GST_DEBUG ("test_mixed finished ");
   498   create_xml(0);
   516   create_xml(0);
   499   
   517   
   500 }
   518 }
   501 
   519 
   502 void test_diff()
   520 void test_diff()
   540   std_log(LOG_FILENAME_LINE, "Test Successful");
   558   std_log(LOG_FILENAME_LINE, "Test Successful");
   541   create_xml(0);
   559   create_xml(0);
   542   
   560   
   543 } 
   561 } 
   544 
   562 
   545 
   563 struct test_async_sync_interaction_data
   546 
   564 {
       
   565   GMutex *lock;
       
   566   GstClockID sync_id;
       
   567   GstClockID sync_id2;
       
   568   GstClockID async_id;
       
   569   GstClockID async_id2;
       
   570   GstClockID async_id3;
       
   571 };
       
   572 static gboolean
       
   573 test_async_sync_interaction_cb (GstClock * clock, GstClockTime time,
       
   574     GstClockID id, gpointer user_data)
       
   575 {
       
   576   struct test_async_sync_interaction_data *td =
       
   577       (struct test_async_sync_interaction_data *) (user_data);
       
   578   g_mutex_lock (td->lock);
       
   579   if (id == td->async_id)
       
   580     goto out;
       
   581   if (id != td->async_id2 && id != td->async_id3)
       
   582     goto out;
       
   583   if (id == td->async_id3) {
       
   584     gst_clock_id_unschedule (td->sync_id);
       
   585     gst_clock_id_unschedule (td->async_id2);
       
   586   }
       
   587 out:
       
   588   g_mutex_unlock (td->lock);
       
   589   return FALSE;
       
   590 }
       
   591 void test_async_sync_interaction()
       
   592 {
       
   593   GstClock *clock;
       
   594   GstClockReturn result;
       
   595   GstClockTime base;
       
   596   GstClockTimeDiff jitter;
       
   597   struct test_async_sync_interaction_data td;
       
   598   int i;
       
   599   xmlfile = "test_async_sync_interaction";
       
   600 std_log(LOG_FILENAME_LINE, "Test Started test_async_sync_interaction");
       
   601     clock = gst_system_clock_obtain ();
       
   602      fail_unless (clock != NULL, "Could not create instance of GstSystemClock");
       
   603      td.lock = g_mutex_new ();
       
   604      for (i = 0; i < 50; i++) {
       
   605        gst_clock_debug (clock);
       
   606        base = gst_clock_get_time (clock);
       
   607        g_mutex_lock (td.lock);
       
   608        td.async_id = gst_clock_new_single_shot_id (clock, base + 40 * GST_MSECOND);
       
   609        td.async_id2 =
       
   610            gst_clock_new_single_shot_id (clock, base + 30 * GST_MSECOND);
       
   611        td.async_id3 =
       
   612            gst_clock_new_single_shot_id (clock, base + 20 * GST_MSECOND);
       
   613        td.sync_id2 = gst_clock_new_single_shot_id (clock, base + 10 * GST_MSECOND);
       
   614        td.sync_id = gst_clock_new_single_shot_id (clock, base + 50 * GST_MSECOND);
       
   615        g_mutex_unlock (td.lock);
       
   616        result = gst_clock_id_wait_async (td.async_id,
       
   617            test_async_sync_interaction_cb, &td);
       
   618        fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
       
   619        result = gst_clock_id_wait (td.sync_id2, &jitter);
       
   620        fail_unless (result == GST_CLOCK_OK || result == GST_CLOCK_EARLY,
       
   621            "Waiting did not return OK or EARLY");
       
   622        result = gst_clock_id_wait_async (td.async_id2,
       
   623            test_async_sync_interaction_cb, &td);
       
   624        fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
       
   625        gst_clock_id_unschedule (td.async_id);
       
   626        result = gst_clock_id_wait_async (td.async_id3,
       
   627            test_async_sync_interaction_cb, &td);
       
   628        fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
       
   629        result = gst_clock_id_wait (td.sync_id, &jitter);
       
   630        fail_unless (result == GST_CLOCK_UNSCHEDULED || result == GST_CLOCK_EARLY,
       
   631            "Waiting did not return UNSCHEDULED");
       
   632        gst_clock_id_unschedule (td.async_id3);
       
   633        g_mutex_lock (td.lock);
       
   634        gst_clock_id_unref (td.sync_id);
       
   635        gst_clock_id_unref (td.sync_id2);
       
   636        gst_clock_id_unref (td.async_id);
       
   637        gst_clock_id_unref (td.async_id2);
       
   638        gst_clock_id_unref (td.async_id3);
       
   639        g_mutex_unlock (td.lock);
       
   640      }
       
   641      g_mutex_free (td.lock);
       
   642      gst_object_unref (clock);
       
   643      std_log(LOG_FILENAME_LINE, "Test Successful");
       
   644      create_xml(0);
       
   645    }
   547 /*void main(int argc,char** argv)
   646 /*void main(int argc,char** argv)
   548 {
   647 {
   549         gst_init(&argc,&argv);
   648         gst_init(&argc,&argv);
   550         test_range();
   649         test_range();
   551         test_mixed();
   650         test_mixed();
   558 //        test_async_order();
   657 //        test_async_order();
   559         
   658         
   560 }*/
   659 }*/
   561 
   660 
   562 
   661 
   563 void (*fn[8]) (void) = {
   662 void (*fn[]) (void) = {
   564         test_range,
   663         test_range,
   565         test_diff,
   664         test_diff,
   566         test_signedness,
   665         test_signedness,
   567         test_single_shot,
   666         test_single_shot,
   568         test_periodic_shot,
   667         test_periodic_shot,
   569         test_periodic_multi,
   668         test_periodic_multi,
   570         test_async_order,
   669         test_async_order,
   571         test_mixed
   670         test_mixed,
       
   671         test_async_sync_interaction
   572 };
   672 };
   573 
   673 
   574 char *args[] = {
   674 char *args[] = {
   575         "test_range",
   675         "test_range",
   576         "test_diff",
   676         "test_diff",
   578         "test_single_shot",
   678         "test_single_shot",
   579         "test_periodic_shot",
   679         "test_periodic_shot",
   580         "test_periodic_multi",
   680         "test_periodic_multi",
   581         "test_async_order",
   681         "test_async_order",
   582         "test_mixed",
   682         "test_mixed",
       
   683         "test_async_sync_interaction"
   583 };
   684 };
   584 
   685 
   585 GST_CHECK_MAIN (gst_systemclock);
   686 GST_CHECK_MAIN (gst_systemclock);
   586 
   687 
   587 
   688