diff options
author | Thiago Santos <thiagoss@osg.samsung.com> | 2015-09-01 20:10:12 -0300 |
---|---|---|
committer | Thiago Santos <thiagoss@osg.samsung.com> | 2015-11-01 08:51:51 -0300 |
commit | efc0110a158e6b87101164db9816c1b6b422ed8c (patch) | |
tree | 113577276486ce02f4faa80ba7763c09cc12409f | |
parent | 162398a18877fd0c75794012388526320bce7c3d (diff) |
capsquerycache: implement cacheing
Cache the results for NULL and the last filter used, those should
cover the majority of cases a pad has to reply to a caps query
-rw-r--r-- | gst/gstcapsquerycache.c | 108 | ||||
-rw-r--r-- | gst/gstcapsquerycache.h | 4 | ||||
-rw-r--r-- | tests/check/gst/gstcapsquerycache.c | 74 |
3 files changed, 184 insertions, 2 deletions
diff --git a/gst/gstcapsquerycache.c b/gst/gstcapsquerycache.c index d88cc0c5f..79b48681a 100644 --- a/gst/gstcapsquerycache.c +++ b/gst/gstcapsquerycache.c @@ -35,7 +35,11 @@ typedef struct _GstCapsQueryCacheImpl { - GstCapsQueryCache caps; + GstCapsQueryCache capscache; + + GstCaps *null_result; + GstCaps *last_filter; + GstCaps *last_filter_result; } GstCapsQueryCacheImpl; @@ -43,20 +47,36 @@ GType _gst_caps_query_cache_type = 0; GST_DEFINE_MINI_OBJECT_TYPE (GstCapsQueryCache, gst_caps_query_cache); +#define GST_CAPS_QUERY_CACHE_IMPL(c) ((GstCapsQueryCacheImpl *)(c)) + /* creation/deletion */ static void _gst_caps_query_cache_free (GstCapsQueryCache * capscache) { - /* TODO implement */ + GstCapsQueryCacheImpl *impl = GST_CAPS_QUERY_CACHE_IMPL (capscache); + + if (impl->null_result) + gst_caps_unref (impl->null_result); + if (impl->last_filter) + gst_caps_unref (impl->last_filter); + if (impl->last_filter_result) + gst_caps_unref (impl->last_filter_result); + g_slice_free1 (sizeof (GstCapsQueryCacheImpl), capscache); } static void gst_caps_query_cache_init (GstCapsQueryCache * capscache) { + GstCapsQueryCacheImpl *impl = GST_CAPS_QUERY_CACHE_IMPL (capscache); + gst_mini_object_init (GST_MINI_OBJECT_CAST (capscache), 0, _gst_caps_query_cache_type, NULL, NULL, (GstMiniObjectFreeFunction) _gst_caps_query_cache_free); + + impl->null_result = NULL; + impl->last_filter = NULL; + impl->last_filter_result = NULL; } /** @@ -75,3 +95,87 @@ gst_caps_query_cache_new (void) return capscache; } + +void +gst_caps_query_cache_flush (GstCapsQueryCache * capscache) +{ + GstCapsQueryCacheImpl *impl = GST_CAPS_QUERY_CACHE_IMPL (capscache); + + if (impl->null_result) + gst_caps_unref (impl->null_result); + if (impl->last_filter) + gst_caps_unref (impl->last_filter); + if (impl->last_filter_result) + gst_caps_unref (impl->last_filter_result); + + impl->null_result = NULL; + impl->last_filter = NULL; + impl->last_filter_result = NULL; +} + +static GstCaps * +gst_caps_query_cache_get (GstCapsQueryCache * capscache, GstCaps * key) +{ + GstCapsQueryCacheImpl *impl = GST_CAPS_QUERY_CACHE_IMPL (capscache); + + g_return_val_if_fail (key == NULL || GST_IS_CAPS (key), NULL); + + if (key) { + if (impl->last_filter && gst_caps_is_equal (impl->last_filter, key)) { + return impl->last_filter_result; + } + return NULL; + } else { + return impl->null_result; + } +} + +static void +gst_caps_query_cache_put (GstCapsQueryCache * capscache, GstCaps * key, + GstCaps * value) +{ + GstCapsQueryCacheImpl *impl = GST_CAPS_QUERY_CACHE_IMPL (capscache); + + g_return_if_fail (key == NULL || GST_IS_CAPS (key)); + g_return_if_fail (GST_IS_CAPS (value)); + + if (key) { + gst_caps_replace (&impl->last_filter, key); + gst_caps_replace (&impl->last_filter_result, value); + } else { + gst_caps_replace (&impl->null_result, value); + } +} + +void +gst_caps_query_cache_put_from_query (GstCapsQueryCache * capscache, + GstQuery * query) +{ + GstCaps *filter; + GstCaps *caps; + + g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_CAPS); + + gst_query_parse_caps (query, &filter); + gst_query_parse_caps_result (query, &caps); + + if (caps) { + gst_caps_query_cache_put (capscache, filter, caps); + } +} + +GstCaps * +gst_caps_query_cache_get_from_query (GstCapsQueryCache * capscache, + GstQuery * query) +{ + GstCaps *filter; + GstCaps *caps; + + g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_CAPS, NULL); + + gst_query_parse_caps (query, &filter); + + caps = gst_caps_query_cache_get (capscache, filter); + + return caps; +} diff --git a/gst/gstcapsquerycache.h b/gst/gstcapsquerycache.h index d0f904bd4..eb314352b 100644 --- a/gst/gstcapsquerycache.h +++ b/gst/gstcapsquerycache.h @@ -51,6 +51,10 @@ GType gst_caps_query_cache_get_type (void); GstCapsQueryCache * gst_caps_query_cache_new (void); +void gst_caps_query_cache_put_from_query (GstCapsQueryCache * capscache, GstQuery * query); +GstCaps * gst_caps_query_cache_get_from_query (GstCapsQueryCache * capscache, GstQuery * query); +void gst_caps_query_cache_flush (GstCapsQueryCache * capscache); + G_END_DECLS #endif /* __GST_CAPS_QUERY_CACHE_H__ */ diff --git a/tests/check/gst/gstcapsquerycache.c b/tests/check/gst/gstcapsquerycache.c index 581a8d7c5..ea259c45c 100644 --- a/tests/check/gst/gstcapsquerycache.c +++ b/tests/check/gst/gstcapsquerycache.c @@ -34,6 +34,78 @@ GST_START_TEST (test_create) GST_END_TEST; +GST_START_TEST (test_put_and_get_caps) +{ + GstCapsQueryCache *capscache; + GstCaps *filter; + GstCaps *result; + GstCaps *cached_result; + GstQuery *query; + + capscache = gst_caps_query_cache_new (); + + filter = gst_caps_from_string ("some/caps, options=(int){1, 2}"); + result = + gst_caps_from_string ("some/caps, options=(int){1, 2}, extra=(int)1"); + + query = gst_query_new_caps (filter); + gst_query_set_caps_result (query, result); + + gst_caps_query_cache_put_from_query (capscache, query); + gst_query_unref (query); + + query = gst_query_new_caps (NULL); + fail_unless (gst_caps_query_cache_get_from_query (capscache, query) == NULL); + gst_query_unref (query); + + query = gst_query_new_caps (filter); + cached_result = gst_caps_query_cache_get_from_query (capscache, query); + fail_unless (gst_caps_is_equal (cached_result, result)); + gst_query_unref (query); + + gst_caps_unref (filter); + gst_caps_unref (result); + gst_mini_object_unref (GST_MINI_OBJECT_CAST (capscache)); +} + +GST_END_TEST; + +GST_START_TEST (test_put_and_get_null_caps) +{ + GstCapsQueryCache *capscache; + GstCaps *filter; + GstCaps *result; + GstCaps *cached_result; + GstQuery *query; + + capscache = gst_caps_query_cache_new (); + + filter = gst_caps_from_string ("some/caps, options=(int){1, 2}"); + result = + gst_caps_from_string ("some/caps, options=(int){1, 2}, extra=(int)1"); + + query = gst_query_new_caps (NULL); + gst_query_set_caps_result (query, result); + + gst_caps_query_cache_put_from_query (capscache, query); + gst_query_unref (query); + + query = gst_query_new_caps (filter); + fail_unless (gst_caps_query_cache_get_from_query (capscache, query) == NULL); + gst_query_unref (query); + + query = gst_query_new_caps (NULL); + cached_result = gst_caps_query_cache_get_from_query (capscache, query); + fail_unless (gst_caps_is_equal (cached_result, result)); + gst_query_unref (query); + + gst_caps_unref (filter); + gst_caps_unref (result); + gst_mini_object_unref (GST_MINI_OBJECT_CAST (capscache)); +} + +GST_END_TEST; + static Suite * gst_caps_query_cache_suite (void) { @@ -42,6 +114,8 @@ gst_caps_query_cache_suite (void) suite_add_tcase (s, tc_chain); tcase_add_test (tc_chain, test_create); + tcase_add_test (tc_chain, test_put_and_get_null_caps); + tcase_add_test (tc_chain, test_put_and_get_caps); return s; } |