tee: Check for the removed pad flag also in the slow pushing path
[platform/upstream/gstreamer.git] / gst / gstdatetime.c
index 6a8f659..6d6e7c8 100644 (file)
  *
  * 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.
+ * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
  */
 
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
 
-#include "glib-compat-private.h"
 #include "gst_private.h"
+#include "glib-compat-private.h"
 #include "gstdatetime.h"
 #include "gstvalue.h"
 #include <glib.h>
@@ -56,12 +56,18 @@ typedef enum
 
 struct _GstDateTime
 {
+  GstMiniObject mini_object;
+
   GDateTime *datetime;
 
   GstDateTimeFields fields;
-  volatile gint ref_count;
 };
 
+GType _gst_date_time_type = 0;
+GST_DEFINE_MINI_OBJECT_TYPE (GstDateTime, gst_date_time);
+
+static void gst_date_time_free (GstDateTime * datetime);
+
 /**
  * gst_date_time_new_from_g_date_time:
  * @dt: (transfer full): the #GDateTime. The new #GstDateTime takes ownership.
@@ -70,7 +76,8 @@ struct _GstDateTime
  *
  * Free-function: gst_date_time_unref
  *
- * Returns: (transfer full): a newly created #GstDateTime, or NULL on error
+ * Returns: (transfer full) (nullable): a newly created #GstDateTime,
+ * or %NULL on error
  */
 GstDateTime *
 gst_date_time_new_from_g_date_time (GDateTime * dt)
@@ -81,9 +88,12 @@ gst_date_time_new_from_g_date_time (GDateTime * dt)
     return NULL;
 
   gst_dt = g_slice_new (GstDateTime);
+
+  gst_mini_object_init (GST_MINI_OBJECT_CAST (gst_dt), 0, GST_TYPE_DATE_TIME,
+      NULL, NULL, (GstMiniObjectFreeFunction) gst_date_time_free);
+
   gst_dt->datetime = dt;
   gst_dt->fields = GST_DATE_TIME_FIELDS_YMD_HMS;
-  gst_dt->ref_count = 1;
   return gst_dt;
 }
 
@@ -95,7 +105,8 @@ gst_date_time_new_from_g_date_time (GDateTime * dt)
  *
  * Free-function: g_date_time_unref
  *
- * Returns: (transfer full): a newly created #GDateTime, or NULL on error
+ * Returns: (transfer full) (nullable): a newly created #GDateTime, or
+ * %NULL on error
  */
 GDateTime *
 gst_date_time_to_g_date_time (GstDateTime * datetime)
@@ -112,8 +123,8 @@ gst_date_time_to_g_date_time (GstDateTime * datetime)
  * gst_date_time_has_year:
  * @datetime: a #GstDateTime
  *
- * Returns: TRUE if @datetime<!-- -->'s year field is set (which should always
- *     be the case), otherwise FALSE
+ * Returns: %TRUE if @datetime<!-- -->'s year field is set (which should always
+ *     be the case), otherwise %FALSE
  */
 gboolean
 gst_date_time_has_year (const GstDateTime * datetime)
@@ -127,7 +138,7 @@ gst_date_time_has_year (const GstDateTime * datetime)
  * gst_date_time_has_month:
  * @datetime: a #GstDateTime
  *
- * Returns: TRUE if @datetime<!-- -->'s month field is set, otherwise FALSE
+ * Returns: %TRUE if @datetime<!-- -->'s month field is set, otherwise %FALSE
  */
 gboolean
 gst_date_time_has_month (const GstDateTime * datetime)
@@ -141,7 +152,7 @@ gst_date_time_has_month (const GstDateTime * datetime)
  * gst_date_time_has_day:
  * @datetime: a #GstDateTime
  *
- * Returns: TRUE if @datetime<!-- -->'s day field is set, otherwise FALSE
+ * Returns: %TRUE if @datetime<!-- -->'s day field is set, otherwise %FALSE
  */
 gboolean
 gst_date_time_has_day (const GstDateTime * datetime)
@@ -155,8 +166,8 @@ gst_date_time_has_day (const GstDateTime * datetime)
  * gst_date_time_has_time:
  * @datetime: a #GstDateTime
  *
- * Returns: TRUE if @datetime<!-- -->'s hour and minute fields are set,
- *     otherwise FALSE
+ * Returns: %TRUE if @datetime<!-- -->'s hour and minute fields are set,
+ *     otherwise %FALSE
  */
 gboolean
 gst_date_time_has_time (const GstDateTime * datetime)
@@ -170,7 +181,7 @@ gst_date_time_has_time (const GstDateTime * datetime)
  * gst_date_time_has_second:
  * @datetime: a #GstDateTime
  *
- * Returns: TRUE if @datetime<!-- -->'s second field is set, otherwise FALSE
+ * Returns: %TRUE if @datetime<!-- -->'s second field is set, otherwise %FALSE
  */
 gboolean
 gst_date_time_has_second (const GstDateTime * datetime)
@@ -185,7 +196,7 @@ gst_date_time_has_second (const GstDateTime * datetime)
  * @datetime: a #GstDateTime
  *
  * Returns the year of this #GstDateTime
- * Call gst_date_time_has_year before, to avoid warnings.
+ * Call gst_date_time_has_year() before, to avoid warnings.
  *
  * Return value: The year of this #GstDateTime
  */
@@ -202,7 +213,7 @@ gst_date_time_get_year (const GstDateTime * datetime)
  * @datetime: a #GstDateTime
  *
  * Returns the month of this #GstDateTime. January is 1, February is 2, etc..
- * Call gst_date_time_has_month before, to avoid warnings.
+ * Call gst_date_time_has_month() before, to avoid warnings.
  *
  * Return value: The month of this #GstDateTime
  */
@@ -220,7 +231,7 @@ gst_date_time_get_month (const GstDateTime * datetime)
  * @datetime: a #GstDateTime
  *
  * Returns the day of the month of this #GstDateTime.
- * Call gst_date_time_has_day before, to avoid warnings.
+ * Call gst_date_time_has_day() before, to avoid warnings.
  *
  * Return value: The day of this #GstDateTime
  */
@@ -239,7 +250,7 @@ gst_date_time_get_day (const GstDateTime * datetime)
  *
  * Retrieves the hour of the day represented by @datetime in the gregorian
  * calendar. The return is in the range of 0 to 23.
- * Call gst_date_time_has_haur before, to avoid warnings.
+ * Call gst_date_time_has_time() before, to avoid warnings.
  *
  * Return value: the hour of the day
  */
@@ -258,7 +269,7 @@ gst_date_time_get_hour (const GstDateTime * datetime)
  *
  * Retrieves the minute of the hour represented by @datetime in the gregorian
  * calendar.
- * Call gst_date_time_has_minute before, to avoid warnings.
+ * Call gst_date_time_has_time() before, to avoid warnings.
  *
  * Return value: the minute of the hour
  */
@@ -277,7 +288,7 @@ gst_date_time_get_minute (const GstDateTime * datetime)
  *
  * Retrieves the second of the minute represented by @datetime in the gregorian
  * calendar.
- * Call gst_date_time_has_second before, to avoid warnings.
+ * Call gst_date_time_has_time() before, to avoid warnings.
  *
  * Return value: the second represented by @datetime
  */
@@ -587,7 +598,7 @@ __gst_date_time_compare (const GstDateTime * dt1, const GstDateTime * dt2)
  *
  * Note that @tzoffset is a float and was chosen so for being able to handle
  * some fractional timezones, while it still keeps the readability of
- * represeting it in hours for most timezones.
+ * representing it in hours for most timezones.
  *
  * If value is -1 then all over value will be ignored. For example
  * if @month == -1, then #GstDateTime will created only for @year. If
@@ -716,9 +727,10 @@ done:
  * are (for example): 2012, 2012-06, 2012-06-23, 2012-06-23T23:30Z,
  * 2012-06-23T23:30+0100, 2012-06-23T23:30:59Z, 2012-06-23T23:30:59+0100
  *
- * Returns: a newly allocated string formatted according to ISO 8601 and
- *     only including the datetime fields that are valid, or NULL in case
- *     there was an error. The string should be freed with g_free().
+ * Returns: (nullable): a newly allocated string formatted according
+ *     to ISO 8601 and only including the datetime fields that are
+ *     valid, or %NULL in case there was an error. The string should
+ *     be freed with g_free().
  */
 gchar *
 gst_date_time_to_iso8601_string (GstDateTime * datetime)
@@ -736,16 +748,24 @@ gst_date_time_to_iso8601_string (GstDateTime * datetime)
  * @string: ISO 8601-formatted datetime string.
  *
  * Tries to parse common variants of ISO-8601 datetime strings into a
- * #GstDateTime.
+ * #GstDateTime. Possible input formats are (for example):
+ * 2012-06-30T22:46:43Z, 2012, 2012-06, 2012-06-30, 2012-06-30T22:46:43-0430,
+ * 2012-06-30T22:46Z, 2012-06-30T22:46-0430, 2012-06-30 22:46,
+ * 2012-06-30 22:46:43, 2012-06-00, 2012-00-00, 2012-00-30, 22:46:43Z, 22:46Z,
+ * 22:46:43-0430, 22:46-0430, 22:46:30, 22:46
+ * If no date is provided, it is assumed to be "today" in the timezone
+ * provided (if any), otherwise UTC.
  *
  * Free-function: gst_date_time_unref
  *
- * Returns: (transfer full): a newly created #GstDateTime, or NULL on error
+ * Returns: (transfer full) (nullable): a newly created #GstDateTime,
+ * or %NULL on error
  */
 GstDateTime *
 gst_date_time_new_from_iso8601_string (const gchar * string)
 {
   gint year = -1, month = -1, day = -1, hour = -1, minute = -1;
+  gint gmt_offset_hour = -99, gmt_offset_min = -99;
   gdouble second = -1.0;
   gfloat tzoffset = 0.0;
   guint64 usecs;
@@ -757,40 +777,45 @@ gst_date_time_new_from_iso8601_string (const gchar * string)
 
   len = strlen (string);
 
-  if (len < 4 || !g_ascii_isdigit (string[0]) || !g_ascii_isdigit (string[1])
-      || !g_ascii_isdigit (string[2]) || !g_ascii_isdigit (string[3]))
+  /* The input string is expected to start either with a year (4 digits) or
+   * with an hour (2 digits). Hour must be followed by minute. In any case,
+   * the string must be at least 4 characters long and start with 2 digits */
+  if (len < 4 || !g_ascii_isdigit (string[0]) || !g_ascii_isdigit (string[1]))
     return NULL;
 
-  ret = sscanf (string, "%04d-%02d-%02d", &year, &month, &day);
+  if (g_ascii_isdigit (string[2]) && g_ascii_isdigit (string[3])) {
+    ret = sscanf (string, "%04d-%02d-%02d", &year, &month, &day);
 
-  if (ret == 0)
-    return NULL;
+    if (ret == 0)
+      return NULL;
 
-  if (ret == 3 && day <= 0) {
-    ret = 2;
-    day = -1;
-  }
+    if (ret == 3 && day <= 0) {
+      ret = 2;
+      day = -1;
+    }
 
-  if (ret >= 2 && month <= 0) {
-    ret = 1;
-    month = day = -1;
-  }
+    if (ret >= 2 && month <= 0) {
+      ret = 1;
+      month = day = -1;
+    }
 
-  if (ret >= 1 && year <= 0)
-    return NULL;
+    if (ret >= 1 && (year <= 0 || year > 9999 || month > 12 || day > 31))
+      return NULL;
 
-  else if (ret >= 1 && len < 16)
-    /* YMD is 10 chars. XMD + HM will be 16 chars. if it is less,
-     * it make no sense to continue. We will stay with YMD. */
-    goto ymd;
+    else if (ret >= 1 && len < 16)
+      /* YMD is 10 chars. XMD + HM will be 16 chars. if it is less,
+       * it make no sense to continue. We will stay with YMD. */
+      goto ymd;
 
-  string += 10;
-  /* Exit if there is no expeceted value on this stage */
-  if (!(*string == 'T' || *string == '-' || *string == ' '))
-    goto ymd;
+    string += 10;
+    /* Exit if there is no expeceted value on this stage */
+    if (!(*string == 'T' || *string == '-' || *string == ' '))
+      goto ymd;
 
-  /* if hour or minute fails, then we will use onlly ymd. */
-  hour = g_ascii_strtoull (string + 1, (gchar **) & string, 10);
+    string += 1;
+  }
+  /* if hour or minute fails, then we will use only ymd. */
+  hour = g_ascii_strtoull (string, (gchar **) & string, 10);
   if (hour > 24 || *string != ':')
     goto ymd;
 
@@ -825,7 +850,7 @@ gst_date_time_new_from_iso8601_string (const gchar * string)
     goto ymd_hms;
   else {
     /* reuse some code from gst-plugins-base/gst-libs/gst/tag/gstxmptag.c */
-    gint gmt_offset_hour = -1, gmt_offset_min = -1, gmt_offset = -1;
+    gint gmt_offset = -1;
     gchar *plus_pos = NULL;
     gchar *neg_pos = NULL;
     gchar *pos = NULL;
@@ -840,7 +865,7 @@ gst_date_time_new_from_iso8601_string (const gchar * string)
     else if (neg_pos)
       pos = neg_pos + 1;
 
-    if (pos) {
+    if (pos && strlen (pos) >= 3) {
       gint ret_tz;
       if (pos[2] == ':')
         ret_tz = sscanf (pos, "%d:%d", &gmt_offset_hour, &gmt_offset_min);
@@ -850,9 +875,11 @@ gst_date_time_new_from_iso8601_string (const gchar * string)
       GST_DEBUG ("Parsing timezone: %s", pos);
 
       if (ret_tz == 2) {
+        if (neg_pos != NULL && neg_pos + 1 == pos) {
+          gmt_offset_hour *= -1;
+          gmt_offset_min *= -1;
+        }
         gmt_offset = gmt_offset_hour * 60 + gmt_offset_min;
-        if (neg_pos != NULL && neg_pos + 1 == pos)
-          gmt_offset *= -1;
 
         tzoffset = gmt_offset / 60.0;
 
@@ -863,16 +890,43 @@ gst_date_time_new_from_iso8601_string (const gchar * string)
   }
 
 ymd_hms:
+  if (year == -1 || month == -1 || day == -1) {
+    GDateTime *now_utc, *now_in_given_tz;
+
+    /* No date was supplied: make it today */
+    now_utc = g_date_time_new_now_utc ();
+    if (tzoffset != 0.0) {
+      /* If a timezone offset was supplied, get the date of that timezone */
+      g_assert (gmt_offset_min != -99);
+      g_assert (gmt_offset_hour != -99);
+      now_in_given_tz =
+          g_date_time_add_minutes (now_utc,
+          (60 * gmt_offset_hour) + gmt_offset_min);
+      g_date_time_unref (now_utc);
+    } else {
+      now_in_given_tz = now_utc;
+    }
+    g_date_time_get_ymd (now_in_given_tz, &year, &month, &day);
+    g_date_time_unref (now_in_given_tz);
+  }
   return gst_date_time_new (tzoffset, year, month, day, hour, minute, second);
 ymd:
+  if (year == -1) {
+    /* No date was supplied and time failed to parse */
+    return NULL;
+  }
   return gst_date_time_new_ymd (year, month, day);
 }
 
-
 static void
 gst_date_time_free (GstDateTime * datetime)
 {
   g_date_time_unref (datetime->datetime);
+
+#ifdef USE_POISONING
+  memset (datetime, 0xff, sizeof (GstDateTime));
+#endif
+
   g_slice_free (GstDateTime, datetime);
 }
 
@@ -887,10 +941,7 @@ gst_date_time_free (GstDateTime * datetime)
 GstDateTime *
 gst_date_time_ref (GstDateTime * datetime)
 {
-  g_return_val_if_fail (datetime != NULL, NULL);
-  g_return_val_if_fail (datetime->ref_count > 0, NULL);
-  g_atomic_int_inc (&datetime->ref_count);
-  return datetime;
+  return (GstDateTime *) gst_mini_object_ref (GST_MINI_OBJECT_CAST (datetime));
 }
 
 /**
@@ -903,9 +954,11 @@ gst_date_time_ref (GstDateTime * datetime)
 void
 gst_date_time_unref (GstDateTime * datetime)
 {
-  g_return_if_fail (datetime != NULL);
-  g_return_if_fail (datetime->ref_count > 0);
+  gst_mini_object_unref (GST_MINI_OBJECT_CAST (datetime));
+}
 
-  if (g_atomic_int_dec_and_test (&datetime->ref_count))
-    gst_date_time_free (datetime);
+void
+_priv_gst_date_time_initialize (void)
+{
+  _gst_date_time_type = gst_date_time_get_type ();
 }