diff --git a/docs/libs/gst-plugins-base-libs-sections.txt b/docs/libs/gst-plugins-base-libs-sections.txt index 78606bb91c..bc6733df53 100644 --- a/docs/libs/gst-plugins-base-libs-sections.txt +++ b/docs/libs/gst-plugins-base-libs-sections.txt @@ -3075,6 +3075,7 @@ gst_video_time_code_free gst_video_time_code_copy gst_video_time_code_init gst_video_time_code_init_from_date_time +gst_video_time_code_init_from_date_time_full GST_VIDEO_TIME_CODE_INIT gst_video_time_code_clear gst_video_time_code_is_valid @@ -3088,6 +3089,7 @@ gst_video_time_code_to_string gst_video_time_code_add_interval gst_video_time_code_new_from_string gst_video_time_code_new_from_date_time +gst_video_time_code_new_from_date_time_full GstVideoTimeCodeInterval diff --git a/gst-libs/gst/video/gstvideotimecode.c b/gst-libs/gst/video/gstvideotimecode.c index 153a35043e..92daab0c2e 100644 --- a/gst-libs/gst/video/gstvideotimecode.c +++ b/gst-libs/gst/video/gstvideotimecode.c @@ -230,18 +230,52 @@ gst_video_time_code_to_date_time (const GstVideoTimeCode * tc) * The resulting config->latest_daily_jam is set to * midnight, and timecode is set to the given time. * + * Will assert on invalid parameters, use gst_video_time_code_init_from_date_time_full() + * for being able to handle invalid parameters. + * * Since: 1.12 */ - void gst_video_time_code_init_from_date_time (GstVideoTimeCode * tc, guint fps_n, guint fps_d, GDateTime * dt, GstVideoTimeCodeFlags flags, guint field_count) +{ + if (!gst_video_time_code_init_from_date_time_full (tc, fps_n, fps_d, dt, + flags, field_count)) + g_return_if_fail (gst_video_time_code_is_valid (tc)); +} + +/** + * gst_video_time_code_init_from_date_time_full: + * @tc: a #GstVideoTimeCode + * @fps_n: Numerator of the frame rate + * @fps_d: Denominator of the frame rate + * @dt: #GDateTime to convert + * @flags: #GstVideoTimeCodeFlags + * @field_count: Interlaced video field count + * + * The resulting config->latest_daily_jam is set to + * midnight, and timecode is set to the given time. + * + * Returns: %TRUE if @tc could be correctly initialized to a valid timecode + * + * Since: 1.16 + */ +gboolean +gst_video_time_code_init_from_date_time_full (GstVideoTimeCode * tc, + guint fps_n, guint fps_d, + GDateTime * dt, GstVideoTimeCodeFlags flags, guint field_count) { GDateTime *jam; guint64 frames; gboolean add_a_frame = FALSE; + g_return_val_if_fail (tc != NULL, FALSE); + g_return_val_if_fail (dt != NULL, FALSE); + g_return_val_if_fail (fps_d != 0, FALSE); + + gst_video_time_code_clear (tc); + jam = g_date_time_new_local (g_date_time_get_year (dt), g_date_time_get_month (dt), g_date_time_get_day_of_month (dt), 0, 0, 0.0); @@ -273,7 +307,7 @@ gst_video_time_code_init_from_date_time (GstVideoTimeCode * tc, g_date_time_unref (jam); - g_return_if_fail (gst_video_time_code_is_valid (tc)); + return gst_video_time_code_is_valid (tc); } /** @@ -710,6 +744,10 @@ gst_video_time_code_new_from_string (const gchar * tc_str) * The resulting config->latest_daily_jam is set to * midnight, and timecode is set to the given time. * + * This might return a completely invalid timecode, use + * gst_video_time_code_new_from_date_time_full() to ensure + * that you would get %NULL instead in that case. + * * Returns: the #GstVideoTimeCode representation of @dt. * * Since: 1.12 @@ -720,11 +758,41 @@ gst_video_time_code_new_from_date_time (guint fps_n, guint fps_d, { GstVideoTimeCode *tc; tc = gst_video_time_code_new_empty (); - gst_video_time_code_init_from_date_time (tc, fps_n, fps_d, dt, flags, + gst_video_time_code_init_from_date_time_full (tc, fps_n, fps_d, dt, flags, field_count); return tc; } +/** + * gst_video_time_code_new_from_date_time_full: + * @fps_n: Numerator of the frame rate + * @fps_d: Denominator of the frame rate + * @dt: #GDateTime to convert + * @flags: #GstVideoTimeCodeFlags + * @field_count: Interlaced video field count + * + * The resulting config->latest_daily_jam is set to + * midnight, and timecode is set to the given time. + * + * Returns: the #GstVideoTimeCode representation of @dt, or %NULL if + * no valid timecode could be created. + * + * Since: 1.16 + */ +GstVideoTimeCode * +gst_video_time_code_new_from_date_time_full (guint fps_n, guint fps_d, + GDateTime * dt, GstVideoTimeCodeFlags flags, guint field_count) +{ + GstVideoTimeCode *tc; + tc = gst_video_time_code_new_empty (); + if (!gst_video_time_code_init_from_date_time_full (tc, fps_n, fps_d, dt, + flags, field_count)) { + gst_video_time_code_free (tc); + return NULL; + } + return tc; +} + /** * gst_video_time_code_init: * @tc: a #GstVideoTimeCode diff --git a/gst-libs/gst/video/gstvideotimecode.h b/gst-libs/gst/video/gstvideotimecode.h index fea5f7407f..d35269687a 100644 --- a/gst-libs/gst/video/gstvideotimecode.h +++ b/gst-libs/gst/video/gstvideotimecode.h @@ -144,12 +144,19 @@ GstVideoTimeCode * gst_video_time_code_new_empty (void); GST_VIDEO_API GstVideoTimeCode * gst_video_time_code_new_from_string (const gchar * tc_str); +GST_VIDEO_DEPRECATED_FOR(gst_video_time_code_new_from_date_time_full) +GstVideoTimeCode * gst_video_time_code_new_from_date_time (guint fps_n, + guint fps_d, + GDateTime * dt, + GstVideoTimeCodeFlags flags, + guint field_count); + GST_VIDEO_API -GstVideoTimeCode * gst_video_time_code_new_from_date_time (guint fps_n, - guint fps_d, - GDateTime * dt, - GstVideoTimeCodeFlags flags, - guint field_count); +GstVideoTimeCode * gst_video_time_code_new_from_date_time_full (guint fps_n, + guint fps_d, + GDateTime * dt, + GstVideoTimeCodeFlags flags, + guint field_count); GST_VIDEO_API void gst_video_time_code_free (GstVideoTimeCode * tc); @@ -169,13 +176,20 @@ void gst_video_time_code_init (GstVideoTimeCode * tc guint frames, guint field_count); -GST_VIDEO_API +GST_VIDEO_DEPRECATED_FOR(gst_video_time_code_init_from_date_time_full) void gst_video_time_code_init_from_date_time (GstVideoTimeCode * tc, guint fps_n, guint fps_d, GDateTime * dt, GstVideoTimeCodeFlags flags, guint field_count); +GST_VIDEO_API +gboolean gst_video_time_code_init_from_date_time_full (GstVideoTimeCode * tc, + guint fps_n, + guint fps_d, + GDateTime * dt, + GstVideoTimeCodeFlags flags, + guint field_count); GST_VIDEO_API void gst_video_time_code_clear (GstVideoTimeCode * tc); diff --git a/tests/check/libs/videotimecode.c b/tests/check/libs/videotimecode.c index 3b5ff0cdfa..38726f6411 100644 --- a/tests/check/libs/videotimecode.c +++ b/tests/check/libs/videotimecode.c @@ -614,7 +614,7 @@ GST_START_TEST (videotimecode_from_date_time_1s) GDateTime *dt; dt = g_date_time_new_local (2017, 2, 16, 0, 0, 1); - tc = gst_video_time_code_new_from_date_time (30000, 1001, dt, + tc = gst_video_time_code_new_from_date_time_full (30000, 1001, dt, GST_VIDEO_TIME_CODE_FLAGS_DROP_FRAME, 0); fail_unless_equals_int (tc->hours, 0); @@ -636,7 +636,7 @@ GST_START_TEST (videotimecode_from_date_time_halfsecond) dt = g_date_time_new_local (2017, 2, 17, 14, 13, 0); dt2 = g_date_time_add (dt, 500000); g_date_time_unref (dt); - tc = gst_video_time_code_new_from_date_time (30000, 1001, dt2, + tc = gst_video_time_code_new_from_date_time_full (30000, 1001, dt2, GST_VIDEO_TIME_CODE_FLAGS_DROP_FRAME, 0); fail_unless_equals_int (tc->hours, 14); @@ -656,7 +656,7 @@ GST_START_TEST (videotimecode_from_date_time) GDateTime *dt; dt = g_date_time_new_local (2017, 2, 17, 14, 13, 30); - tc = gst_video_time_code_new_from_date_time (30000, 1001, dt, + tc = gst_video_time_code_new_from_date_time_full (30000, 1001, dt, GST_VIDEO_TIME_CODE_FLAGS_DROP_FRAME, 0); fail_unless_equals_int (tc->hours, 14);