1/****************************************************************************
2**
3** Copyright (C) 2016 Jolla Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qgstreamerbufferprobe_p.h"
41#include "qgstutils_p.h"
42
43QT_BEGIN_NAMESPACE
44
45QGstreamerBufferProbe::QGstreamerBufferProbe(Flags flags)
46 : m_flags(flags)
47{
48}
49
50QGstreamerBufferProbe::~QGstreamerBufferProbe()
51{
52#if !GST_CHECK_VERSION(1,0,0)
53 if (m_caps)
54 gst_caps_unref(m_caps);
55#endif
56}
57
58void QGstreamerBufferProbe::addProbeToPad(GstPad *pad, bool downstream)
59{
60 if (GstCaps *caps = qt_gst_pad_get_current_caps(pad)) {
61 probeCaps(caps);
62 gst_caps_unref(caps);
63 }
64#if GST_CHECK_VERSION(1,0,0)
65 if (m_flags & ProbeCaps) {
66 m_capsProbeId = gst_pad_add_probe(
67 pad,
68 mask: downstream
69 ? GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM
70 : GST_PAD_PROBE_TYPE_EVENT_UPSTREAM,
71 callback: capsProbe,
72 user_data: this,
73 destroy_data: nullptr);
74 }
75 if (m_flags & ProbeBuffers) {
76 m_bufferProbeId = gst_pad_add_probe(
77 pad, mask: GST_PAD_PROBE_TYPE_BUFFER, callback: bufferProbe, user_data: this, destroy_data: nullptr);
78 }
79#else
80 Q_UNUSED(downstream);
81
82 m_bufferProbeId = gst_pad_add_buffer_probe(pad, G_CALLBACK(bufferProbe), this);
83#endif
84}
85
86void QGstreamerBufferProbe::removeProbeFromPad(GstPad *pad)
87{
88#if GST_CHECK_VERSION(1,0,0)
89 if (m_capsProbeId != -1) {
90 gst_pad_remove_probe(pad, id: m_capsProbeId);
91 m_capsProbeId = -1;
92 }
93 if (m_bufferProbeId != -1) {
94 gst_pad_remove_probe(pad, id: m_bufferProbeId);
95 m_bufferProbeId = -1;
96 }
97#else
98 if (m_bufferProbeId != -1) {
99 gst_pad_remove_buffer_probe(pad, m_bufferProbeId);
100 m_bufferProbeId = -1;
101 if (m_caps) {
102 gst_caps_unref(m_caps);
103 m_caps = 0;
104 }
105 }
106#endif
107}
108
109void QGstreamerBufferProbe::probeCaps(GstCaps *)
110{
111}
112
113bool QGstreamerBufferProbe::probeBuffer(GstBuffer *)
114{
115 return true;
116}
117
118#if GST_CHECK_VERSION(1,0,0)
119GstPadProbeReturn QGstreamerBufferProbe::capsProbe(
120 GstPad *, GstPadProbeInfo *info, gpointer user_data)
121{
122 QGstreamerBufferProbe * const control = static_cast<QGstreamerBufferProbe *>(user_data);
123
124 if (GstEvent * const event = gst_pad_probe_info_get_event(info)) {
125 if (GST_EVENT_TYPE(event) == GST_EVENT_CAPS) {
126 GstCaps *caps;
127 gst_event_parse_caps(event, caps: &caps);
128
129 control->probeCaps(caps);
130 }
131 }
132 return GST_PAD_PROBE_OK;
133}
134
135GstPadProbeReturn QGstreamerBufferProbe::bufferProbe(
136 GstPad *, GstPadProbeInfo *info, gpointer user_data)
137{
138 QGstreamerBufferProbe * const control = static_cast<QGstreamerBufferProbe *>(user_data);
139 if (GstBuffer * const buffer = gst_pad_probe_info_get_buffer(info))
140 return control->probeBuffer(buffer) ? GST_PAD_PROBE_OK : GST_PAD_PROBE_DROP;
141 return GST_PAD_PROBE_OK;
142}
143#else
144gboolean QGstreamerBufferProbe::bufferProbe(GstElement *, GstBuffer *buffer, gpointer user_data)
145{
146 QGstreamerBufferProbe * const control = static_cast<QGstreamerBufferProbe *>(user_data);
147
148 if (control->m_flags & ProbeCaps) {
149 GstCaps *caps = gst_buffer_get_caps(buffer);
150 if (caps && (!control->m_caps || !gst_caps_is_equal(control->m_caps, caps))) {
151 qSwap(caps, control->m_caps);
152 control->probeCaps(control->m_caps);
153 }
154 if (caps)
155 gst_caps_unref(caps);
156 }
157
158 if (control->m_flags & ProbeBuffers) {
159 return control->probeBuffer(buffer) ? TRUE : FALSE;
160 } else {
161 return TRUE;
162 }
163}
164#endif
165
166QT_END_NAMESPACE
167

source code of qtmultimedia/src/gsttools/qgstreamerbufferprobe.cpp