systemclock: Reorganize defined checks for parts of GstSystemClock
authortyler-aicradle <tyler@safex.ai>
Wed, 26 May 2021 17:55:42 +0000 (12:55 -0500)
committertyler-aicradle <tyler@safex.ai>
Thu, 27 May 2021 14:25:24 +0000 (09:25 -0500)
The gst_system_clock_get_internal_time and
gst_system_clock_get_resolution functions had some nested defined checks
making this code somewhat harder to reason about and much harder to
change. The logical meaning of the checks has changed but the actual
code coming out of the pre-processor should not have changed
significantly. The main logical change was flattening the checks for
existence of posix timing functionality, from what I can tell these
functions aren't available on Windows where they were trying to be
included. I have checked the Linux and macOS output and they are
functionally unchanged.

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/829>

gst/gstsystemclock.c

index 665da53..531819c 100644 (file)
@@ -825,15 +825,12 @@ clock_type_to_posix_id (GstClockType clock_type)
 static GstClockTime
 gst_system_clock_get_internal_time (GstClock * clock)
 {
-#if defined __APPLE__
   GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
+#if defined __APPLE__
   uint64_t mach_t = mach_absolute_time ();
   return gst_util_uint64_scale (mach_t, sysclock->priv->mach_timebase.numer,
       sysclock->priv->mach_timebase.denom);
-#else
-#ifdef G_OS_WIN32
-  GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
-
+#elif G_OS_WIN32
   if (sysclock->priv->frequency.QuadPart != 0) {
     LARGE_INTEGER now;
 
@@ -842,66 +839,51 @@ gst_system_clock_get_internal_time (GstClock * clock)
 
     return gst_util_uint64_scale (now.QuadPart,
         GST_SECOND, sysclock->priv->frequency.QuadPart);
-  } else
-#endif /* G_OS_WIN32 */
-#if !defined HAVE_POSIX_TIMERS || !defined HAVE_CLOCK_GETTIME
-  {
+  } else {
     gint64 monotime;
 
     monotime = g_get_monotonic_time ();
 
     return monotime * 1000;
   }
-#else
-  {
-    GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
-    clockid_t ptype;
-    struct timespec ts;
+#elif defined HAVE_POSIX_TIMERS && defined HAVE_CLOCK_GETTIME
+  clockid_t ptype;
+  struct timespec ts;
 
-    ptype = clock_type_to_posix_id (sysclock->priv->clock_type);
+  ptype = clock_type_to_posix_id (sysclock->priv->clock_type);
 
-    if (G_UNLIKELY (clock_gettime (ptype, &ts)))
-      return GST_CLOCK_TIME_NONE;
+  if (G_UNLIKELY (clock_gettime (ptype, &ts)))
+    return GST_CLOCK_TIME_NONE;
 
-    return GST_TIMESPEC_TO_TIME (ts);
-  }
-#endif
+  return GST_TIMESPEC_TO_TIME (ts);
 #endif /* __APPLE__ */
 }
 
 static guint64
 gst_system_clock_get_resolution (GstClock * clock)
 {
-#if defined __APPLE__
   GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
+#if defined __APPLE__
   return gst_util_uint64_scale (GST_NSECOND,
       sysclock->priv->mach_timebase.numer, sysclock->priv->mach_timebase.denom);
-#else
-#ifdef G_OS_WIN32
-  GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
-
+#elif G_OS_WIN32
   if (sysclock->priv->frequency.QuadPart != 0) {
     return GST_SECOND / sysclock->priv->frequency.QuadPart;
-  } else
-#endif /* G_OS_WIN32 */
-#if defined(HAVE_POSIX_TIMERS) && defined(HAVE_CLOCK_GETTIME)
-  {
-    GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
-    clockid_t ptype;
-    struct timespec ts;
+  } else {
+    return 1 * GST_USECOND;
+  }
+#elif defined(HAVE_POSIX_TIMERS) && defined(HAVE_CLOCK_GETTIME)
+  clockid_t ptype;
+  struct timespec ts;
 
-    ptype = clock_type_to_posix_id (sysclock->priv->clock_type);
+  ptype = clock_type_to_posix_id (sysclock->priv->clock_type);
 
-    if (G_UNLIKELY (clock_getres (ptype, &ts)))
-      return GST_CLOCK_TIME_NONE;
+  if (G_UNLIKELY (clock_getres (ptype, &ts)))
+    return GST_CLOCK_TIME_NONE;
 
-    return GST_TIMESPEC_TO_TIME (ts);
-  }
+  return GST_TIMESPEC_TO_TIME (ts);
 #else
-  {
-    return 1 * GST_USECOND;
-  }
-#endif
+  return 1 * GST_USECOND;
 #endif /* __APPLE__ */
 }