QtGStreamer  0.10.2
 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_buffer(GstAppSink *sink, gpointer user_data);
44  static GstFlowReturn new_buffer_list(GstAppSink *sink, gpointer user_data);
45 
46  static void eos_noop(GstAppSink*, gpointer) {}
47  static GstFlowReturn new_preroll_noop(GstAppSink*, gpointer) { return GST_FLOW_OK; }
48  static GstFlowReturn new_buffer_noop(GstAppSink*, gpointer) { return GST_FLOW_OK; }
49  static GstFlowReturn new_buffer_list_noop(GstAppSink*, gpointer) { return GST_FLOW_OK; }
50 };
51 
52 void ApplicationSink::Priv::lazyConstruct(ApplicationSink *self)
53 {
54  if (!m_appsink) {
55  m_appsink = QGst::ElementFactory::make("appsink");
56  if (!m_appsink) {
57  qWarning() << "Failed to construct appsink";
58  }
59  setCallbacks(self);
60  }
61 }
62 
63 void ApplicationSink::Priv::setCallbacks(ApplicationSink *self)
64 {
65  if (m_appsink) {
66  if (self) {
67  static GstAppSinkCallbacks callbacks = { &eos, &new_preroll,
68  &new_buffer, &new_buffer_list };
69  gst_app_sink_set_callbacks(appSink(), &callbacks, self, NULL);
70  } else {
71  static GstAppSinkCallbacks callbacks = { &eos_noop, &new_preroll_noop,
72  &new_buffer_noop, &new_buffer_list_noop };
73  gst_app_sink_set_callbacks(appSink(), &callbacks, NULL, NULL);
74  }
75  }
76 }
77 
78 void ApplicationSink::Priv::eos(GstAppSink* sink, gpointer user_data)
79 {
80  Q_UNUSED(sink);
81  static_cast<ApplicationSink*>(user_data)->eos();
82 }
83 
84 GstFlowReturn ApplicationSink::Priv::new_preroll(GstAppSink* sink, gpointer user_data)
85 {
86  Q_UNUSED(sink);
87  return static_cast<GstFlowReturn>(static_cast<ApplicationSink*>(user_data)->newPreroll());
88 }
89 
90 GstFlowReturn ApplicationSink::Priv::new_buffer(GstAppSink* sink, gpointer user_data)
91 {
92  Q_UNUSED(sink);
93  return static_cast<GstFlowReturn>(static_cast<ApplicationSink*>(user_data)->newBuffer());
94 }
95 
96 GstFlowReturn ApplicationSink::Priv::new_buffer_list(GstAppSink* sink, gpointer user_data)
97 {
98  Q_UNUSED(sink);
99  return static_cast<GstFlowReturn>(static_cast<ApplicationSink*>(user_data)->newBufferList());
100 }
101 
102 #endif //DOXYGEN_RUN
103 
104 
105 ApplicationSink::ApplicationSink()
106  : d(new Priv)
107 {
108 }
109 
110 ApplicationSink::~ApplicationSink()
111 {
112  d->setCallbacks(NULL); //remove the callbacks from the sink
113  delete d;
114 }
115 
116 ElementPtr ApplicationSink::element() const
117 {
118  d->lazyConstruct(const_cast<ApplicationSink*>(this));
119  return d->m_appsink;
120 }
121 
122 void ApplicationSink::setElement(const ElementPtr & appsink)
123 {
124  Q_ASSERT(QGlib::Type::fromInstance(appsink).isA(GST_TYPE_APP_SINK));
125  d->setCallbacks(NULL); //remove the callbacks from the previous sink
126  d->m_appsink = appsink;
127  d->setCallbacks(this);
128 }
129 
130 CapsPtr ApplicationSink::caps() const
131 {
132  CapsPtr caps;
133  if (d->appSink()) {
134  caps = CapsPtr::wrap(gst_app_sink_get_caps(d->appSink()), false);
135  }
136  return caps;
137 }
138 
139 void ApplicationSink::setCaps(const CapsPtr & caps)
140 {
141  d->lazyConstruct(this);
142  if (d->appSink()) {
143  gst_app_sink_set_caps(d->appSink(), caps);
144  }
145 }
146 
147 bool ApplicationSink::isEos() const
148 {
149  return d->appSink() ? gst_app_sink_is_eos(d->appSink()) : true;
150 }
151 
152 uint ApplicationSink::maxBuffers() const
153 {
154  return d->appSink() ? gst_app_sink_get_max_buffers(d->appSink()) : 0;
155 }
156 
157 void ApplicationSink::setMaxBuffers(uint maxbuffers)
158 {
159  d->lazyConstruct(this);
160  if (d->appSink()) {
161  gst_app_sink_set_max_buffers(d->appSink(), maxbuffers);
162  }
163 }
164 
165 bool ApplicationSink::dropEnabled() const
166 {
167  return d->appSink() ? gst_app_sink_get_drop(d->appSink()) : false;
168 }
169 
170 void ApplicationSink::enableDrop(bool enable)
171 {
172  d->lazyConstruct(this);
173  if (d->appSink()) {
174  gst_app_sink_set_drop(d->appSink(), enable);
175  }
176 }
177 
178 BufferPtr ApplicationSink::pullPreroll()
179 {
180  BufferPtr buf;
181  if (d->appSink()) {
182  buf = BufferPtr::wrap(gst_app_sink_pull_preroll(d->appSink()), false);
183  }
184  return buf;
185 }
186 
187 BufferPtr ApplicationSink::pullBuffer()
188 {
189  BufferPtr buf;
190  if (d->appSink()) {
191  buf = BufferPtr::wrap(gst_app_sink_pull_buffer(d->appSink()), false);
192  }
193  return buf;
194 }
195 
196 BufferListPtr ApplicationSink::pullBufferList()
197 {
198  BufferListPtr buf;
199  if (d->appSink()) {
200  buf = BufferListPtr::wrap(gst_app_sink_pull_buffer_list(d->appSink()), false);
201  }
202  return buf;
203 }
204 
205 void ApplicationSink::eos()
206 {
207 }
208 
209 FlowReturn ApplicationSink::newPreroll()
210 {
211  return FlowOk;
212 }
213 
214 FlowReturn ApplicationSink::newBuffer()
215 {
216  return FlowOk;
217 }
218 
219 FlowReturn ApplicationSink::newBufferList()
220 {
221  return FlowOk;
222 }
223 
224 
225 } //namespace Utils
226 } //namespace QGst