deepstream_common.h 5.58 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
 * SPDX-FileCopyrightText: Copyright (c) 2018-2019 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
 * SPDX-License-Identifier: LicenseRef-NvidiaProprietary
 *
 * NVIDIA CORPORATION, its affiliates and licensors retain all intellectual
 * property and proprietary rights in and to this material, related
 * documentation and any modifications thereto. Any use, reproduction,
 * disclosure or distribution of this material and related documentation
 * without an express license agreement from NVIDIA CORPORATION or
 * its affiliates is strictly prohibited.
 */

#ifndef __NVGSTDS_COMMON_H__
#define __NVGSTDS_COMMON_H__

#include <gst/gst.h>

#ifdef __cplusplus
extern "C"
{
#endif

#include "deepstream_config.h"

#define NVGSTDS_ERR_MSG_V(msg, ...) \
    g_print("** ERROR: <%s:%d>: " msg "\n", __func__, __LINE__, ##__VA_ARGS__)

#define NVGSTDS_INFO_MSG_V(msg, ...) \
    g_print("** INFO: <%s:%d>: " msg "\n", __func__, __LINE__, ##__VA_ARGS__)

#define NVGSTDS_WARN_MSG_V(msg, ...) \
    g_print("** WARN: <%s:%d>: " msg "\n", __func__, __LINE__, ##__VA_ARGS__)

#define NVGSTDS_LINK_ELEMENT(elem1, elem2) \
    do { \
      if (!gst_element_link (elem1,elem2)) { \
        GstCaps *src_caps, *sink_caps; \
        src_caps = gst_pad_query_caps ((GstPad *) (elem1)->srcpads->data, NULL); \
        sink_caps = gst_pad_query_caps ((GstPad *) (elem2)->sinkpads->data, NULL); \
        NVGSTDS_ERR_MSG_V ("Failed to link '%s' (%s) and '%s' (%s)", \
            GST_ELEMENT_NAME (elem1), \
            gst_caps_to_string (src_caps), \
            GST_ELEMENT_NAME (elem2), \
            gst_caps_to_string (sink_caps)); \
        goto done; \
      } \
    } while (0)

#define NVGSTDS_LINK_ELEMENT_FULL(elem1, elem1_pad_name, elem2, elem2_pad_name) \
  do { \
    GstPad *elem1_pad = gst_element_get_static_pad(elem1, elem1_pad_name); \
    GstPad *elem2_pad = gst_element_get_static_pad(elem2, elem2_pad_name); \
    GstPadLinkReturn ret = gst_pad_link (elem1_pad,elem2_pad); \
    if (ret != GST_PAD_LINK_OK) { \
      gchar *n1 = gst_pad_get_name (elem1_pad); \
      gchar *n2 = gst_pad_get_name (elem2_pad); \
      NVGSTDS_ERR_MSG_V ("Failed to link '%s' and '%s': %d", \
          n1, n2, ret); \
      g_free (n1); \
      g_free (n2); \
      gst_object_unref (elem1_pad); \
      gst_object_unref (elem2_pad); \
      goto done; \
    } \
    gst_object_unref (elem1_pad); \
    gst_object_unref (elem2_pad); \
  } while (0)

#define NVGSTDS_BIN_ADD_GHOST_PAD_NAMED(bin, elem, pad, ghost_pad_name) \
  do { \
    GstPad *gstpad = gst_element_get_static_pad (elem, pad); \
    if (!gstpad) { \
      NVGSTDS_ERR_MSG_V ("Could not find '%s' in '%s'", pad, \
          GST_ELEMENT_NAME(elem)); \
      goto done; \
    } \
    gst_element_add_pad (bin, gst_ghost_pad_new (ghost_pad_name, gstpad)); \
    gst_object_unref (gstpad); \
  } while (0)

#define NVGSTDS_BIN_ADD_GHOST_PAD(bin, elem, pad) \
      NVGSTDS_BIN_ADD_GHOST_PAD_NAMED (bin, elem, pad, pad)

#define NVGSTDS_ELEM_ADD_PROBE(probe_id, elem, pad, probe_func, probe_type, probe_data) \
    do { \
      GstPad *gstpad = gst_element_get_static_pad (elem, pad); \
      if (!gstpad) { \
        NVGSTDS_ERR_MSG_V ("Could not find '%s' in '%s'", pad, \
            GST_ELEMENT_NAME(elem)); \
        goto done; \
      } \
      probe_id = gst_pad_add_probe(gstpad, (probe_type), probe_func, probe_data, NULL); \
      gst_object_unref (gstpad); \
    } while (0)

#define NVGSTDS_ELEM_REMOVE_PROBE(probe_id, elem, pad) \
    do { \
      if (probe_id == 0 || !elem) { \
          break; \
      } \
      GstPad *gstpad = gst_element_get_static_pad (elem, pad); \
      if (!gstpad) { \
        NVGSTDS_ERR_MSG_V ("Could not find '%s' in '%s'", pad, \
            GST_ELEMENT_NAME(elem)); \
        break; \
      } \
      gst_pad_remove_probe(gstpad, probe_id); \
      gst_object_unref (gstpad); \
    } while (0)

#define GET_FILE_PATH(path) ((path) + (((path) && strstr ((path), "file://")) ? 7 : 0))


/**
 * Function to link sink pad of an element to source pad of tee.
 *
 * @param[in] tee Tee element.
 * @param[in] sinkelem downstream element.
 *
 * @return true if link successful.
 */
gboolean
link_element_to_tee_src_pad (GstElement * tee, GstElement * sinkelem);

/**
 * Function to link source pad of an element to sink pad of muxer element.
 *
 * @param[in] streammux muxer element.
 * @param[in] elem upstream element.
 * @param[in] index pad index of muxer element.
 *
 * @return true if link successful.
 */
gboolean
link_element_to_streammux_sink_pad (GstElement *streammux, GstElement *elem,
                                    gint index);

/**
 * Function to unlink source pad of an element from sink pad of muxer element.
 *
 * @param[in] streammux muxer element.
 * @param[in] elem upstream element.
 *
 * @return true if unlinking was successful.
 */
gboolean
unlink_element_from_streammux_sink_pad (GstElement *streammux, GstElement *elem);

/**
 * Function to link sink pad of an element to source pad of demux element.
 *
 * @param[in] demux demuxer element.
 * @param[in] elem downstream element.
 * @param[in] index pad index of demuxer element.
 *
 * @return true if link successful.
 */
gboolean
link_element_to_demux_src_pad (GstElement *demux, GstElement *elem,
                               guint index);

/*
 * Function to replace string with another string.
 * Make sure @ref src is big enough to accommodate replacements.
 *
 * @param[in] str string to search in.
 * @param[in] replace string to replace.
 * @param[in] replace_with string to replace @ref replace with.
 */
void
str_replace (gchar * str, const gchar * replace, const gchar * replace_with);

#ifdef __cplusplus
}
#endif

#endif