-
Notifications
You must be signed in to change notification settings - Fork 35
Load properties from a JSON file created by tcam capture
tiscamera 1.0 / 1.1
tcam-capture can save the current property values into a JSON file.
- Start tcam-capture and select your video capture device.
- Click on "Properties" menu and configure the properties to your needs.
- Click on "Info" menu. Choose the "State" tab in the information dialog
- Click on "Save" and save the properties in a file.
- Close tcam-capture.
Create a usual C++ project, maybe copy and paste the code from tiscamera/examples/c/03-live-stream.c as basic code.
Include this:
#include <iostream>
#include <fstream>
#include <streambuf>
It is used for reading the created json file. The json file is not interpreted as JSON file. It is read simply into a string. Therefore, no JSON library is necessary.
Now do the GStreamer code:
const char* serial = "41910044"; // Replace with the serial number of your device.
GError* err = NULL;
GstElement* pipeline =
gst_parse_launch("tcambin name=source ! video/x-raw,format=BGRx ! videoconvert ! ximagesink", &err);
if (err)
{
std::cout << err->message << std::endl;
g_error_free(err);
err = NULL;
return 1;
}
/* test for error */
if (pipeline == NULL)
{
printf("Could not create pipeline. Cause: %s\n", err->message);
return 1;
}
GstElement* tcambin = gst_bin_get_by_name(GST_BIN(pipeline), "source");
if (serial != NULL)
{
GValue val = {};
g_value_init(&val, G_TYPE_STRING);
g_value_set_static_string(&val, serial);
g_object_set_property(G_OBJECT(tcambin), "serial", &val);
}
// Set the pipeline in state ready, so we can set properties
gst_element_set_state(pipeline, GST_STATE_READY);
gst_element_get_state(pipeline, NULL, NULL, 4000000000ll);
The code queries the tcambin (GstElement *) from the pipeline (in the 03-live-stream it was named "source"). The serial number is passed to the tcambin. After that, the pipeline is set into READY state.
Read the json file saved by tcam-capture. We assume, it was named "properties.json" and is saved in our current working directory:
// Read the file "properties.json" created by tcam-capture.
std::ifstream t("properties.json");
std::string jsonproperties((std::istreambuf_iterator<char>(t)),
std::istreambuf_iterator<char>());
// Create a GValue of type string and pass the content of
// jsonpropties to it
GValue val = {};
g_value_init(&val, G_TYPE_STRING);
g_value_set_static_string(&val, jsonproperties.c_str());
// Now pass the properties to the tcambin.
g_object_set_property(G_OBJECT(tcambin), "tcam-properties-json", &val);
// tcambin is no longer needed, so unref it here.
gst_object_unref(tcambin);
The tcam-properties-json
property of tcambin is set with the content of the json file.
That is all.
Complete code:
#include <iostream>
#include <fstream>
#include <streambuf>
#include <gst/gst.h>
int main(int argc, char** argv)
{
gst_init( &argc,&argv);
std::cout << "Template execuable" << std::endl;
const char* serial = "41910044"; // set this if you do not want the first found device
GError* err = NULL;
GstElement* pipeline =
gst_parse_launch("tcambin name=source ! video/x-raw,format=BGRx ! videoconvert ! ximagesink", &err);
if (err)
{
std::cout << err->message << std::endl;
g_error_free(err);
err = NULL;
return 1;
}
/* test for error */
if (pipeline == NULL)
{
printf("Could not create pipeline. Cause: %s\n", err->message);
return 1;
}
GstElement* tcambin = gst_bin_get_by_name(GST_BIN(pipeline), "source");
if (serial != NULL)
{
GValue val = {};
g_value_init(&val, G_TYPE_STRING);
g_value_set_static_string(&val, serial);
g_object_set_property(G_OBJECT(tcambin), "serial", &val);
}
// Set the pipeline in state ready, so we can set properties
gst_element_set_state(pipeline, GST_STATE_READY);
gst_element_get_state(pipeline, NULL, NULL, 4000000000ll);
// Read the file "properties.json" created by tcam-capture.
std::ifstream t("properties.json");
std::string jsonproperties((std::istreambuf_iterator<char>(t)),
std::istreambuf_iterator<char>());
// Create a GValue of type string and pass the content of
// jsonpropties to it
GValue val = {};
g_value_init(&val, G_TYPE_STRING);
g_value_set_static_string(&val, jsonproperties.c_str());
// Now pass the properties to the tcambin.
g_object_set_property(G_OBJECT(tcambin), "tcam-properties-json", &val);
// tcambin is no longer needed, so unref it here.
gst_object_unref(tcambin);
// Start the pipelnie
gst_element_set_state(pipeline, GST_STATE_PLAYING);
gst_element_get_state(pipeline, NULL, NULL, 4000000000ll);
std::cout << "Press enter to stop the stream." << std::endl;
getchar();
gst_element_set_state(pipeline, GST_STATE_NULL);
gst_element_get_state(pipeline, NULL, NULL, 4000000000ll);
gst_object_unref(pipeline);
return 0;
}