Lightweight automatic checks and in-place corrections are performed before commiting changes if you run:
$ pip install pre-commit
$ pre-commit install
pre-commit installed at .git/hooks/pre-commit
For more information about pre-commit, please refer to .
Currently, pre-commit will check the code with clang-format. In addition to that, this project has additional conventions:
- Avoid function prototypes if possible.
- Functions (method) must be sorted as follows A. Local (static) functions, and among them sorted by by class hierarchy order B. Global (not API, but global among the library). C. Public API functions.
Example 1
The following example shows the functions order in case 2(A).
This is the hierarchy structure of gstwebcanvassrc
.
GObject
+----GInitiallyUnowned
+----GstObject
+----GstElement
+----GstBaseSrc
+----GstPushSrc
+----GstWebCanvasSrc
So, in the .c file "methods" of GObject must be at the end of the document, then GstElement "GstObject", then GstElement "methods", then GstElement "methods", then GstBaseSrc "methods", then GstPushSrc "methods" and finally GstWebCanvasSrc methods, like the following example shows:
static void
gst_web_canvas_src_init (GstWebCanvasSrc *self)
{
/* ... */
}
static void
gst_web_canvas_src_create_internal (GstWebCanvasSrc *self, GstBuffer **buffer)
{
/* ... */
}
static GstFlowReturn
gst_web_canvas_src_create (GstPushSrc *psrc, GstBuffer **buffer)
{
/* ... */
}
static void
gst_web_canvas_src_set_property (
GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
{
/* ... */
}
static void
gst_web_canvas_src_class_init (GstWebCanvasSrcClass *klass)
{
/* ... */
gobject_class->set_property = gst_web_canvas_src_set_property;
gstpushsrc_class->create = gst_web_canvas_src_create;
}
Example 2
The following example shows the functions order in case 2 (A and B):
Suppose the implementation of an hipotheticaly GstWebDOM abstract class library (intented to be used by multiple elements):
// Example of 2(A)
static void
gst_web_dom_base_init (GstWebDOMBase *self)
{
/* ... */
}
// Example of 2(A)
static void
gst_web_dom_base_class_init (GstWebDOMBase *klass)
{
/* ... */
}
// Example of 2(B): Global internal function used by multiple files in gst-libs/gstwebdom/*.c, but not exposed.
void
gst_web_dom_base_element_parse_common (GstWebDOMBase *self)
{
/* ... */
}
// Example of 2(C): Public API.
val *
gst_web_dom_base_get_element (GstWebDOMBase *self)
{
/* ... */
return val;
}
GStreamer logging macros are explained in official documentation.
In addition, consider:
- Use
GST_[ERROR/WARNING/...]_OBJECT
instead ofGST_[ERROR/WARNING/...]
if creating a new GstObject derivative. - If some information must be shown repeatedly (i.e. per buffer), use
GST_LOG
orGST_LOG_OBJECT
. - If in the code there is an error and it is non-recoverable, then use
GST_ERROR
orGST_ERROR_OBJECT
. - If in the code there is an error and it is recoverable, use
GST_WARNING
orGST_WARNING_OBJECT
. - Use descriptive messages for actions:
a. Before an Action: Log the action you are about to perform. This prepares anyone reading the logs to understand what is coming next.
Example:
GST_INFO("Creating buffer");
orGST_INFO("Initializing decoder");
. b. After an Action: Log the result or outcome of the action. This confirms what happened and provides useful context, such as object addresses or results. Example:GST_INFO("Created buffer %p", buffer);
orGST_INFO("Decoder initialized");
. c. Error Handling: Clearly log when an error occurs and explain what went wrong. Example:GST_ERROR("Failed to create buffer %p, no memory available.");
.
#define
macros must be always at start of the document.- If really needed to
#define
a macro in the middle of the document, always undefine it.
- Prefer
G_DEFINE_TYPE*
,G_DEFINE_FINAL_TYPE*
orG_DEFINE_ABSTRACT_TYPE*
macros over defining the GObject definition boilerplate. - In "methods" definition use
self
as argument name, i.e.static void gst_web_canvas_src_init (GstWebCanvasSrc *self) { /* ... */ }
Use header files if defined functions will be reusable within the project or by external libraries. For example, if defining the gstwebexamplesrc.c
element file, avoid creating gstwebexamplesrc.h
if not needed.
Currently, pre-commit will check the code with clang-format. In addition to that no guidelines are defined for now.
Follow this pattern for each sample:
├── foo
│ ├── gstfoo-example.c
│ ├── html # If needed
│ │ ├── index.html
│ │ └── scripts # If needed
│ │ └── index.js
│ └── meson.build
For each gst[file]-example.c
executable, add the following deubg category (if logging needed):
GST_DEBUG_CATEGORY_STATIC (example_dbg);
#define GST_CAT_DEFAULT example_dbg
No specific guidelines defined here, but take inspiration of https://www.conventionalcommits.org/en/v1.0.0/