15constexpr guint kH264NalSliceFirst = 1;
16constexpr guint kH264NalSliceIdr = 5;
17constexpr guint kH264NalSps = 7;
18constexpr guint kH264NalPps = 8;
19constexpr guint kH265NalSliceLast = 31;
20constexpr guint kH265NalVps = 32;
21constexpr guint kH265NalSps = 33;
22constexpr guint kH265NalPps = 34;
24bool isH26xRtpDepayloader(
const char* factoryName)
26 return (g_strcmp0(factoryName,
"rtph264depay") == 0) || (g_strcmp0(factoryName,
"rtph265depay") == 0);
41NalSummary summarizeNals(GstBuffer* buffer,
bool h265)
45 if (!gst_buffer_map(buffer, &map, GST_MAP_READ)) {
48 const guint8*
const data = map.data;
49 for (gsize i = 0; (i + 3) < map.size; ++i) {
50 if ((data[i] != 0) || (data[i + 1] != 0) || (data[i + 2] != 1)) {
53 const guint8 header = data[i + 3];
55 const guint nalType = (header >> 1) & 0x3FU;
56 summary.vps |= (nalType == kH265NalVps);
57 summary.sps |= (nalType == kH265NalSps);
58 summary.pps |= (nalType == kH265NalPps);
59 summary.slice |= (nalType <= kH265NalSliceLast);
61 const guint nalType = header & 0x1FU;
62 summary.sps |= (nalType == kH264NalSps);
63 summary.pps |= (nalType == kH264NalPps);
64 summary.slice |= ((nalType >= kH264NalSliceFirst) && (nalType <= kH264NalSliceIdr));
68 gst_buffer_unmap(buffer, &map);
72bool outputsByteStream(GstPad* pad)
75 const GstStructure* structure =
76 (caps && !gst_caps_is_empty(caps.get())) ? gst_caps_get_structure(caps.get(), 0) :
nullptr;
77 return structure && (g_strcmp0(gst_structure_get_string(structure,
"stream-format"),
"byte-stream") == 0);
81struct ParameterSetGate
83 explicit ParameterSetGate(
bool isH265)
89 gst_clear_buffer(&vps);
90 gst_clear_buffer(&sps);
91 gst_clear_buffer(&pps);
94 ParameterSetGate(
const ParameterSetGate&) =
delete;
95 ParameterSetGate& operator=(
const ParameterSetGate&) =
delete;
98 GstBuffer* vps =
nullptr;
99 GstBuffer* sps =
nullptr;
100 GstBuffer* pps =
nullptr;
104GstBuffer* prependMemories(GstBuffer* buffer, GstBuffer* prefix)
106 buffer = gst_buffer_make_writable(buffer);
107 for (guint i = gst_buffer_n_memory(prefix); i > 0; --i) {
108 gst_buffer_prepend_memory(buffer, gst_buffer_get_memory(prefix, i - 1));
113GstPadProbeReturn holdUntilParameterSets(GstPad* pad, GstPadProbeInfo* info, gpointer data)
115 GstBuffer* buffer = GST_PAD_PROBE_INFO_BUFFER(info);
117 return GST_PAD_PROBE_OK;
120 if (!outputsByteStream(pad)) {
121 return GST_PAD_PROBE_REMOVE;
124 auto* gate =
static_cast<ParameterSetGate*
>(data);
125 const NalSummary nals = summarizeNals(buffer, gate->h265);
128 gst_buffer_replace(&gate->vps, buffer);
131 gst_buffer_replace(&gate->sps, buffer);
134 gst_buffer_replace(&gate->pps, buffer);
136 return GST_PAD_PROBE_DROP;
139 const bool complete = (nals.sps || gate->sps) && (nals.pps || gate->pps) && (!gate->h265 || nals.vps || gate->vps);
141 return GST_PAD_PROBE_DROP;
145 GstBuffer*
const stored[] = {(gate->h265 && !nals.vps) ? gate->vps :
nullptr, nals.sps ? nullptr : gate->sps,
146 nals.pps ? nullptr : gate->pps};
147 for (
int i = 2; i >= 0; --i) {
148 const bool duplicate = (i > 0 && stored[i] == stored[i - 1]) || (i > 1 && stored[i] == stored[i - 2]);
149 if (stored[i] && !duplicate) {
150 buffer = prependMemories(buffer, stored[i]);
153 GST_PAD_PROBE_INFO_DATA(info) = buffer;
154 return GST_PAD_PROBE_REMOVE;
161 const char* factoryName =
factoryNameOf(GST_OBJECT_CAST(element));
162 if ((g_strcmp0(factoryName,
"h264parse") == 0) || (g_strcmp0(factoryName,
"h265parse") == 0)) {
166 g_object_set(element,
"config-interval", -1,
nullptr);
172 const char* factoryName =
factoryNameOf(GST_OBJECT_CAST(element));
173 if (!isH26xRtpDepayloader(factoryName)) {
178 if (requestKeyframes && g_object_class_find_property(G_OBJECT_GET_CLASS(element),
"request-keyframe")) {
179 g_object_set(element,
"request-keyframe", TRUE,
nullptr);
181 if (
const GstPadPtr srcPad =
adoptPad(gst_element_get_static_pad(element,
"src"))) {
188 (void) gst_pad_add_probe(depayloaderSrc, GST_PAD_PROBE_TYPE_BUFFER, holdUntilParameterSets,
189 new ParameterSetGate(h265),
190 [](gpointer gate) {
delete static_cast<ParameterSetGate*
>(gate); });