QtGStreamer  1.2.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
applicationsink.cpp
1 /*
2  Copyright (C) 2011 Collabora Ltd. <info@collabora.co.uk>
3  @author George Kiagiadakis <george.kiagiadakis@collabora.co.uk>
4 
5  This library is free software; you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published
7  by the Free Software Foundation; either version 2.1 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include "applicationsink.h"
19 #include "../elementfactory.h"
20 #include <gst/app/gstappsink.h>
21 
22 namespace QGst {
23 namespace Utils {
24 
25 #ifndef DOXYGEN_RUN
26 
27 struct QTGSTREAMERUTILS_NO_EXPORT ApplicationSink::Priv
28 {
29 public:
30  ElementPtr m_appsink;
31 
32  void lazyConstruct(ApplicationSink *self);
33  void setCallbacks(ApplicationSink *self);
34 
35  inline GstAppSink *appSink()
36  {
37  return reinterpret_cast<GstAppSink*>(static_cast<GstElement*>(m_appsink));
38  }
39 
40 private:
41  static void eos(GstAppSink *sink, gpointer user_data);
42  static GstFlowReturn new_preroll(GstAppSink *sink, gpointer user_data);
43  static GstFlowReturn new_sample(GstAppSink *sink, gpointer user_data);
44 
45  static void eos_noop(GstAppSink*, gpointer) {}
46  static GstFlowReturn new_preroll_noop(GstAppSink*, gpointer) { return GST_FLOW_OK; }
47  static GstFlowReturn new_sample_noop(GstAppSink*, gpointer) { return GST_FLOW_OK; }
48 };
49 
50 void ApplicationSink::Priv::lazyConstruct(ApplicationSink *self)
51 {
52  if (!m_appsink) {
53  m_appsink = QGst::ElementFactory::make("appsink");
54  if (!m_appsink) {
55  qWarning() << "Failed to construct appsink";
56  }
57  setCallbacks(self);
58  }
59 }
60 
61 void ApplicationSink::Priv::setCallbacks(ApplicationSink *self)
62 {
63  if (m_appsink) {
64  if (self) {
65  static GstAppSinkCallbacks callbacks = { &eos, &new_preroll, &new_sample };
66  gst_app_sink_set_callbacks(appSink(), &callbacks, self, NULL);
67  } else {
68  static GstAppSinkCallbacks callbacks = { &eos_noop, &new_preroll_noop,
69  &new_sample_noop };
70  gst_app_sink_set_callbacks(appSink(), &callbacks, NULL, NULL);
71  }
72  }
73 }
74 
75 void ApplicationSink::Priv::eos(GstAppSink* sink, gpointer user_data)
76 {
77  Q_UNUSED(sink);
78  static_cast<ApplicationSink*>(user_data)->eos();
79 }
80 
81 GstFlowReturn ApplicationSink::Priv::new_preroll(GstAppSink* sink, gpointer user_data)
82 {
83  Q_UNUSED(sink);
84  return static_cast<GstFlowReturn>(static_cast<ApplicationSink*>(user_data)->newPreroll());
85 }
86 
87 GstFlowReturn ApplicationSink::Priv::new_sample(GstAppSink* sink, gpointer user_data)
88 {
89  Q_UNUSED(sink);
90  return static_cast<GstFlowReturn>(static_cast<ApplicationSink*>(user_data)->newSample());
91 }
92 
93 #endif //DOXYGEN_RUN
94 
95 
96 ApplicationSink::ApplicationSink()
97  : d(new Priv)
98 {
99 }
100 
101 ApplicationSink::~ApplicationSink()
102 {
103  d->setCallbacks(NULL); //remove the callbacks from the sink
104  delete d;
105 }
106 
107 ElementPtr ApplicationSink::element() const
108 {
109  d->lazyConstruct(const_cast<ApplicationSink*>(this));
110  return d->m_appsink;
111 }
112 
113 void ApplicationSink::setElement(const ElementPtr & appsink)
114 {
115  Q_ASSERT(QGlib::Type::fromInstance(appsink).isA(GST_TYPE_APP_SINK));
116  d->setCallbacks(NULL); //remove the callbacks from the previous sink
117  d->m_appsink = appsink;
118  d->setCallbacks(this);
119 }
120 
121 CapsPtr ApplicationSink::caps() const
122 {
123  CapsPtr caps;
124  if (d->appSink()) {
125  caps = CapsPtr::wrap(gst_app_sink_get_caps(d->appSink()), false);
126  }
127  return caps;
128 }
129 
130 void ApplicationSink::setCaps(const CapsPtr & caps)
131 {
132  d->lazyConstruct(this);
133  if (d->appSink()) {
134  gst_app_sink_set_caps(d->appSink(), caps);
135  }
136 }
137 
138 bool ApplicationSink::isEos() const
139 {
140  return d->appSink() ? gst_app_sink_is_eos(d->appSink()) : true;
141 }
142 
143 uint ApplicationSink::maxBuffers() const
144 {
145  return d->appSink() ? gst_app_sink_get_max_buffers(d->appSink()) : 0;
146 }
147 
148 void ApplicationSink::setMaxBuffers(uint maxbuffers)
149 {
150  d->lazyConstruct(this);
151  if (d->appSink()) {
152  gst_app_sink_set_max_buffers(d->appSink(), maxbuffers);
153  }
154 }
155 
156 bool ApplicationSink::dropEnabled() const
157 {
158  return d->appSink() ? gst_app_sink_get_drop(d->appSink()) : false;
159 }
160 
161 void ApplicationSink::enableDrop(bool enable)
162 {
163  d->lazyConstruct(this);
164  if (d->appSink()) {
165  gst_app_sink_set_drop(d->appSink(), enable);
166  }
167 }
168 
169 SamplePtr ApplicationSink::pullPreroll()
170 {
171  SamplePtr buf;
172  if (d->appSink()) {
173  buf = SamplePtr::wrap(gst_app_sink_pull_preroll(d->appSink()), false);
174  }
175  return buf;
176 }
177 
178 SamplePtr ApplicationSink::pullSample()
179 {
180  SamplePtr buf;
181  if (d->appSink()) {
182  buf = SamplePtr::wrap(gst_app_sink_pull_sample(d->appSink()), false);
183  }
184  return buf;
185 }
186 
187 void ApplicationSink::eos()
188 {
189 }
190 
191 FlowReturn ApplicationSink::newPreroll()
192 {
193  return FlowOk;
194 }
195 
196 FlowReturn ApplicationSink::newSample()
197 {
198  return FlowOk;
199 }
200 
201 } //namespace Utils
202 } //namespace QGst
static RefPointer< T > wrap(typename T::CType *nativePtr, bool increaseRef=true)
Definition: refpointer.h:326