Title: [288799] trunk
Revision
288799
Author
[email protected]
Date
2022-01-30 05:19:24 -0800 (Sun, 30 Jan 2022)

Log Message

[GStreamer] GstStructure to JSON serialization
https://bugs.webkit.org/show_bug.cgi?id=235760

Patch by Philippe Normand <[email protected]> on 2022-01-30
Reviewed by Xabier Rodriguez-Calvar.

Source/WebCore:

For GstWebRTC stats logging purposes JSON is a good payload format. As the stats are
initially notified via GstStructures we need a way to export those to JSON, using a new API
in GStreamerCommon.

Covered by API tests.

* platform/graphics/gstreamer/GStreamerCommon.cpp:
(WebCore::structureValueToJSON):
(WebCore::parseStructureValue):
(WebCore::structureToJSON):
(WebCore::structureToJSONString):
* platform/graphics/gstreamer/GStreamerCommon.h:

Tools:

* TestWebKitAPI/Tests/WebCore/gstreamer/GStreamerTest.cpp:
(TestWebKitAPI::TEST_F):

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (288798 => 288799)


--- trunk/Source/WebCore/ChangeLog	2022-01-30 06:47:18 UTC (rev 288798)
+++ trunk/Source/WebCore/ChangeLog	2022-01-30 13:19:24 UTC (rev 288799)
@@ -1,3 +1,23 @@
+2022-01-30  Philippe Normand  <[email protected]>
+
+        [GStreamer] GstStructure to JSON serialization
+        https://bugs.webkit.org/show_bug.cgi?id=235760
+
+        Reviewed by Xabier Rodriguez-Calvar.
+
+        For GstWebRTC stats logging purposes JSON is a good payload format. As the stats are
+        initially notified via GstStructures we need a way to export those to JSON, using a new API
+        in GStreamerCommon.
+
+        Covered by API tests.
+
+        * platform/graphics/gstreamer/GStreamerCommon.cpp:
+        (WebCore::structureValueToJSON):
+        (WebCore::parseStructureValue):
+        (WebCore::structureToJSON):
+        (WebCore::structureToJSONString):
+        * platform/graphics/gstreamer/GStreamerCommon.h:
+
 2022-01-29  Ryosuke Niwa  <[email protected]>
 
         Delete runtime preference for enabling request animation frame

Modified: trunk/Source/WebCore/platform/graphics/gstreamer/GStreamerCommon.cpp (288798 => 288799)


--- trunk/Source/WebCore/platform/graphics/gstreamer/GStreamerCommon.cpp	2022-01-30 06:47:18 UTC (rev 288798)
+++ trunk/Source/WebCore/platform/graphics/gstreamer/GStreamerCommon.cpp	2022-01-30 13:19:24 UTC (rev 288799)
@@ -538,6 +538,77 @@
     return bin;
 }
 
+static RefPtr<JSON::Value> gstStructureToJSON(const GstStructure*);
+
+static std::optional<RefPtr<JSON::Value>> gstStructureValueToJSON(const GValue* value)
+{
+    if (GST_VALUE_HOLDS_STRUCTURE(value))
+        return gstStructureToJSON(gst_value_get_structure(value));
+
+    if (GST_VALUE_HOLDS_ARRAY(value)) {
+        unsigned size = gst_value_array_get_size(value);
+        auto array = JSON::Array::create();
+        for (unsigned i = 0; i < size; i++) {
+            if (auto innerJson = gstStructureValueToJSON(gst_value_array_get_value(value, i)))
+                array->pushValue(innerJson->releaseNonNull());
+        }
+        return array->asArray()->asValue();
+    }
+    auto valueType = G_VALUE_TYPE(value);
+    if (valueType == G_TYPE_BOOLEAN)
+        return JSON::Value::create(g_value_get_boolean(value))->asValue();
+
+    if (valueType == G_TYPE_INT)
+        return JSON::Value::create(g_value_get_int(value))->asValue();
+
+    if (valueType == G_TYPE_UINT)
+        return JSON::Value::create(static_cast<int>(g_value_get_uint(value)))->asValue();
+
+    if (valueType == G_TYPE_DOUBLE)
+        return JSON::Value::create(g_value_get_double(value))->asValue();
+
+    if (valueType == G_TYPE_FLOAT)
+        return JSON::Value::create(static_cast<double>(g_value_get_float(value)))->asValue();
+
+    // FIXME: bigint support missing in JSON.
+    if (valueType == G_TYPE_UINT64)
+        return JSON::Value::create(static_cast<int>(g_value_get_uint64(value)))->asValue();
+
+    if (valueType == G_TYPE_STRING)
+        return JSON::Value::create(makeString(g_value_get_string(value)))->asValue();
+
+    GST_WARNING("Unhandled GValue type: %s", G_VALUE_TYPE_NAME(value));
+    return { };
 }
 
+static gboolean parseGstStructureValue(GQuark fieldId, const GValue* value, gpointer userData)
+{
+    if (auto jsonValue = gstStructureValueToJSON(value)) {
+        auto* object = reinterpret_cast<JSON::Object*>(userData);
+        object->setValue(g_quark_to_string(fieldId), jsonValue->releaseNonNull());
+    }
+    return TRUE;
+}
+
+static RefPtr<JSON::Value> gstStructureToJSON(const GstStructure* structure)
+{
+    auto jsonObject = JSON::Object::create();
+    auto resultValue = jsonObject->asObject();
+    if (!resultValue)
+        return nullptr;
+
+    gst_structure_foreach(structure, parseGstStructureValue, resultValue.get());
+    return resultValue;
+}
+
+String gstStructureToJSONString(const GstStructure* structure)
+{
+    auto value = gstStructureToJSON(structure);
+    if (!value)
+        return { };
+    return value->toJSONString();
+}
+
+}
+
 #endif // USE(GSTREAMER)

Modified: trunk/Source/WebCore/platform/graphics/gstreamer/GStreamerCommon.h (288798 => 288799)


--- trunk/Source/WebCore/platform/graphics/gstreamer/GStreamerCommon.h	2022-01-30 06:47:18 UTC (rev 288798)
+++ trunk/Source/WebCore/platform/graphics/gstreamer/GStreamerCommon.h	2022-01-30 13:19:24 UTC (rev 288799)
@@ -319,6 +319,8 @@
 GstElement* makeGStreamerElement(const char* factoryName, const char* name);
 GstElement* makeGStreamerBin(const char* description, bool ghostUnlinkedPads);
 
+String gstStructureToJSONString(const GstStructure*);
+
 }
 
 #ifndef GST_BUFFER_DTS_OR_PTS

Modified: trunk/Tools/ChangeLog (288798 => 288799)


--- trunk/Tools/ChangeLog	2022-01-30 06:47:18 UTC (rev 288798)
+++ trunk/Tools/ChangeLog	2022-01-30 13:19:24 UTC (rev 288799)
@@ -1,3 +1,13 @@
+2022-01-30  Philippe Normand  <[email protected]>
+
+        [GStreamer] GstStructure to JSON serialization
+        https://bugs.webkit.org/show_bug.cgi?id=235760
+
+        Reviewed by Xabier Rodriguez-Calvar.
+
+        * TestWebKitAPI/Tests/WebCore/gstreamer/GStreamerTest.cpp:
+        (TestWebKitAPI::TEST_F):
+
 2022-01-29  Ben Nham  <[email protected]>
 
         Add web push message decryption routines

Modified: trunk/Tools/TestWebKitAPI/Tests/WebCore/gstreamer/GStreamerTest.cpp (288798 => 288799)


--- trunk/Tools/TestWebKitAPI/Tests/WebCore/gstreamer/GStreamerTest.cpp	2022-01-30 06:47:18 UTC (rev 288798)
+++ trunk/Tools/TestWebKitAPI/Tests/WebCore/gstreamer/GStreamerTest.cpp	2022-01-30 13:19:24 UTC (rev 288799)
@@ -50,6 +50,18 @@
     // initialize GStreamer correctly.
 }
 
+TEST_F(GStreamerTest, gstStructureJSONSerializing)
+{
+    GUniquePtr<GstStructure> structure(gst_structure_new("foo", "int-val", G_TYPE_INT, 5, "str-val", G_TYPE_STRING, "foo", "bool-val", G_TYPE_BOOLEAN, TRUE, nullptr));
+    auto jsonString = gstStructureToJSONString(structure.get());
+    ASSERT_EQ(jsonString, "{\"int-val\":5,\"str-val\":\"foo\",\"bool-val\":1}");
+
+    GUniquePtr<GstStructure> innerStructure(gst_structure_new("bar", "boo", G_TYPE_BOOLEAN, FALSE, "double-val", G_TYPE_DOUBLE, 2.42, nullptr));
+    gst_structure_set(structure.get(), "inner", GST_TYPE_STRUCTURE, innerStructure.get(), nullptr);
+    jsonString = gstStructureToJSONString(structure.get());
+    ASSERT_EQ(jsonString, "{\"int-val\":5,\"str-val\":\"foo\",\"bool-val\":1,\"inner\":{\"boo\":0,\"double-val\":2.42}}");
+}
+
 } // namespace TestWebKitAPI
 
 #endif // USE(GSTREAMER)
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to