Debian Codegraph @

gstreamer/src/basesrc.hg

raw
1 /* gstreamermm - a C++ wrapper for gstreamer
2 *
3 * Copyright 2008-2016 The gstreamermm Development Team
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library 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 GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free
17 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include <gst/base/gstbasesrc.h>
21 #include <gstreamermm/element.h>
22 #include <gstreamermm/pad.h>
23 #include <gstreamermm/format.h>
24 #include <gstreamermm/clock.h>
25 #include <gstreamermm/segment.h>
26
27 _DEFS(gstreamermm,gst)
28
29 namespace Gst
30 {
31
32 _WRAP_ENUM(BaseSrcFlags, GstBaseSrcFlags, NO_GTYPE)
33
34 /** A base class for getrange based source elements.
35 *
36 * This is a generice base class for source elements. The following types of
37 * sources are supported:
38 *
39 * - random access sources like files,
40 * - seekable sources,
41 * - live sources.
42 *
43 * The source can be configured to operate in any Gst::Format with the
44 * set_format() method. The currently set format determines the format of the
45 * internal Gst::Segment and any Gst::EVENT_NEWSEGMENT events. The default
46 * format for Gst::BaseSrc is Gst::FORMAT_BYTES.
47 *
48 * Gst::BaseSrc always supports push mode scheduling. If the following
49 * conditions are met, it also supports pull mode scheduling:
50 *
51 * - The format is set to Gst::FORMAT_BYTES (default).
52 * - Gst::BaseSrc::is_seekable_vfunc() returns TRUE.
53 *
54 * Since GStreamer 0.10.9, any Gst::BaseSrc can enable pull based scheduling at
55 * any time by overriding Gst::BaseSrc::check_get_range_vfunc() so that it
56 * returns TRUE.
57 *
58 * If all the conditions are met for operating in pull mode, Gst::BaseSrc is
59 * automatically seekable in push mode as well. The following conditions must
60 * be met to make the element seekable in push mode when the format is not
61 * Gst::FORMAT_BYTES:
62 *
63 * - Gst::BaseSrc::is_seekable_vfunc() returns TRUE.
64 * - Gst::BaseSrc::query_vfunc() can convert all supported seek formats to
65 * the internal format as set with set_format().
66 * - Gst::BaseSrc::do_seek_vfunc() is implemented, performs the seek and
67 * returns TRUE.
68 *
69 * When the element does not meet the requirements to operate in pull mode, the
70 * offset and length in the Gst::BaseSrc::create_vfunc() method should be
71 * ignored. It is recommended to subclass Gst::PushSrc instead, in this
72 * situation. If the element can operate in pull mode but only with specific
73 * offsets and lengths, it is allowed to generate an error when the wrong
74 * values are passed to the Gst::BaseSrc::create_vfunc() function.
75 *
76 * Gst::BaseSrc has support for live sources. Live sources are sources that
77 * when paused discard data, such as audio or video capture devices. A typical
78 * live source also produces data at a fixed rate and thus provides a clock to
79 * publish this rate. Use set_live() to activate the live source mode.
80 *
81 * A live source does not produce data in the PAUSED state. This means that the
82 * Gst::BaseSrc::create_vfunc() method will not be called in PAUSED but only in
83 * PLAYING. To signal the pipeline that the element will not produce data, the
84 * return value from the READY to PAUSED state will be
85 * Gst::STATE_CHANGE_NO_PREROLL.
86 *
87 * A typical live source will timestamp the buffers it creates with the current
88 * running time of the pipeline. This is one reason why a live source can only
89 * produce data in the PLAYING state, when the clock is actually distributed
90 * and running.
91 *
92 * Live sources that synchronize and block on the clock (an audio source, for
93 * example) can since GStreamer 0.10.12 use wait_playing() when the
94 * create_vfunc() function was interrupted by a state change to PAUSED.
95 *
96 * The Gst::BaseSrc::get_times_vfunc() method can be used to implement
97 * pseudo-live sources. It only makes sense to implement the get_times_vfunc()
98 * function if the source is a live source. The get_times_vfunc() function
99 * should return timestamps starting from 0, as if it were a non-live source.
100 * The base class will make sure that the timestamps are transformed into the
101 * current running_time. The base source will then wait for the calculated
102 * running_time before pushing out the buffer.
103 *
104 * For live sources, the base class will by default report a latency of 0. For
105 * pseudo live sources, the base class will by default measure the difference
106 * between the first buffer timestamp and the start time of get_times_vfunc()
107 * and will report this value as the latency. Subclasses should override the
108 * query_vfunc() function when this behaviour is not acceptable.
109 *
110 * TODO: Edit below paragraph for C++ and include C++ example from C API (if
111 * necessary):
112 *
113 * There is only support in Gst::BaseSrc for exactly one source pad, which
114 * should be named "src". A source implementation (subclass of Gst::BaseSrc)
115 * should install a pad template in its class_init function, like so:
116 * @code
117 * // static
118 * void SampleBaseSrc::base_init(Gst::ElementClass<SampleBaseSrc> *klass)
119 * {
120 * klass->add_pad_template(Gst::PadTemplate::create(
121 * "src",
122 * Gst::PAD_SRC,
123 * Gst::PAD_ALWAYS,
124 * Gst::Caps::create_from_string("x-application/x-foo1")
125 * ));
126 *
127 * klass->set_metadata("Source name", "Source", "My source element", "The author <[email protected]>");
128 * }
129 * @endcode
130 *
131 * <H2>Controlled shutdown of live sources in applications</H2>
132 *
133 * Applications that record from a live source may want to stop recording in a
134 * controlled way, so that the recording is stopped, but the data already in
135 * the pipeline is processed to the end (remember that many live sources would
136 * go on recording forever otherwise). For that to happen the application needs
137 * to make the source stop recording and send an EOS event down the pipeline.
138 * The application would then wait for an EOS message posted on the pipeline's
139 * bus to know when all data has been processed and the pipeline can safely be
140 * stopped.
141 *
142 * Since GStreamer 0.10.16 an application may send an EOS event to a source
143 * element to make it perform the EOS logic (send EOS event downstream or post
144 * a Gst::MESSAGE_SEGMENT_DONE on the bus). This can typically be done with the
145 * Gst::Element::send_event() function on the element or its parent bin.
146 *
147 * After the EOS has been sent to the element, the application should wait for
148 * an EOS message to be posted on the pipeline's bus. Once this EOS message is
149 * received, it may safely shut down the entire pipeline.
150 *
151 * The old behaviour for controlled shutdown introduced since GStreamer 0.10.3
152 * is still available but deprecated as it is dangerous and less flexible.
153 *
154 * Last reviewed on 2016-05-05 (1.8.0).
155 *
156 * @ingroup GstBaseClasses
157 */
158 class BaseSrc
159 : public Element
160 {
161 _CLASS_GOBJECT(BaseSrc, GstBaseSrc, GST_BASE_SRC, Element, GstElement)
162
163 public:
164 _WRAP_METHOD(FlowReturn wait_playing(), gst_base_src_wait_playing)
165 _WRAP_METHOD(bool is_live() const, gst_base_src_is_live)
166 _WRAP_METHOD(void set_live(bool live), gst_base_src_set_live)
167 _WRAP_METHOD(void set_format(Format format), gst_base_src_set_format)
168 _WRAP_METHOD(bool query_latency(bool& live, Gst::ClockTime& min_latency, Gst::ClockTime& max_latency) const, gst_base_src_query_latency)
169 _WRAP_METHOD(guint get_blocksize() const, gst_base_src_get_blocksize)
170 _WRAP_METHOD(void set_blocksize(guint blocksize), gst_base_src_set_blocksize)
171 _WRAP_METHOD(bool get_do_timestamp() const, gst_base_src_get_do_timestamp)
172 _WRAP_METHOD(void set_do_timestamp(bool timestamp), gst_base_src_set_do_timestamp)
173 _WRAP_METHOD(bool new_seamless_segment(gint64 start, gint64 stop, gint64 position), gst_base_src_new_seamless_segment)
174 _WRAP_METHOD(bool set_caps(const Glib::RefPtr<Gst::Caps>& caps), gst_base_src_set_caps)
175 _WRAP_METHOD(Gst::FlowReturn start_wait(), gst_base_src_start_wait)
176 _WRAP_METHOD(void set_dynamic_size(bool dynamic_size), gst_base_src_set_dynamic_size)
177 _WRAP_METHOD(void set_async(bool async), gst_base_src_set_async)
178 _WRAP_METHOD(bool is_async() const, gst_base_src_is_async)
179 _WRAP_METHOD(void start_complete(Gst::FlowReturn ret), gst_base_src_start_complete)
180 _WRAP_METHOD(void set_automatic_eos(bool automatic_eos), gst_base_src_set_automatic_eos)
181
182 /** Returns the memory @a allocator used by the base class and its @a params.
183 *
184 * @param params The Gst::AllocationParams of @a allocator.
185 * @return The Gst::Allocator used.
186 */
187 Glib::RefPtr<Gst::Allocator> get_allocator(Gst::AllocationParams& params);
188 _IGNORE(gst_base_src_get_allocator)
189
190 /** Gets the source Gst::Pad object of the element.
191 */
192 _MEMBER_GET_GOBJECT(src_pad, srcpad, Gst::Pad, GstPad*)
193
194 _WRAP_PROPERTY("blocksize", gulong)
195 _WRAP_PROPERTY("do-timestamp", bool)
196 _WRAP_PROPERTY("num-buffers", int)
197 _WRAP_PROPERTY("typefind", bool)
198
199 /** Called to get the caps to report.
200 */
201 _WRAP_VFUNC(Glib::RefPtr<Gst::Caps> get_caps(const Glib::RefPtr<Gst::Caps>& caps) const, "get_caps", refreturn_ctype)
202
203 /** Notify subclass of changed output caps.
204 */
205 _WRAP_VFUNC(bool set_caps(const Glib::RefPtr<Gst::Caps>& caps), "set_caps")
206
207 /** Negotiated the caps with the peer.
208 */
209 _WRAP_VFUNC(bool negotiate(), "negotiate")
210
211 /** Stop processing. Subclasses should use this to close resources.
212 */
213 _WRAP_VFUNC(bool stop(), "stop", return_value true)
214
215 /** Start processing. Subclasses should open resources and prepare to produce
216 * data.
217 */
218 _WRAP_VFUNC(bool start(), "start", return_value true)
219
220 #m4 _CONVERSION(`GstBuffer*', `const Glib::RefPtr<Gst::Buffer>&', `Glib::wrap($3, true)')
221 #m4 _CONVERSION(`GstClockTime*', `Gst::ClockTime&', `*($3)')
222 /** Given a buffer, return the start and stop time when it should be pushed
223 * out. The base class will sync on the clock using these times.
224 */
225 _WRAP_VFUNC(void get_times(const Glib::RefPtr<Gst::Buffer>& buffer, Gst::ClockTime& start, Gst::ClockTime& end) const, "get_times")
226
227 #m4 _CONVERSION(`guint64&', `guint64*', `&($3)')
228 #m4 _CONVERSION(`guint64*', `guint64&', `*($3)')
229 /** Return the total size of the resource, in the configured format.
230 */
231 _WRAP_VFUNC(bool get_size(guint64& size) const, "get_size")
232
233 /** Check if the source can seek.
234 */
235 _WRAP_VFUNC(bool is_seekable() const, "is_seekable")
236
237 /** Unlock any pending access to the resource. Subclasses should unblock any
238 * blocked function ASAP.
239 */
240 _WRAP_VFUNC(bool unlock(), "unlock")
241
242 #m4 _CONVERSION(`GstEvent*', `const Glib::RefPtr<Gst::Event>&', `Glib::wrap($3, true)')
243 /** Override this to implement custom event handling.
244 */
245 _WRAP_VFUNC(bool event(const Glib::RefPtr<Gst::Event>& event), "event")
246
247 virtual FlowReturn create_vfunc(guint64 offset, guint size, Glib::RefPtr<Gst::Buffer>& buffer);
248
249 /** Perform seeking on the resource to the indicated segment.
250 */
251 virtual bool do_seek_vfunc(Gst::Segment& segment);
252
253 // Cannot use query_vfunc because the name has been used in the Gst::Element class.
254 // We have to provide custom implementation, because query vfunc expects writable
255 // query object.
256 /** Handle a requested query.
257 */
258 virtual bool base_src_query_vfunc(const Glib::RefPtr<Gst::Query>& query);
259
260 /** Called during negotiation if caps need fixating. Implement instead of
261 * setting a fixate function on the source pad.
262 */
263 _WRAP_VFUNC(Glib::RefPtr<Gst::Caps> fixate(const Glib::RefPtr<Gst::Caps>& caps), "fixate", refreturn_ctype)
264
265 /** Clear the previous unlock request. Subclasses should clear any state they
266 * set during unlock_vfunc(), such as clearing command queues.
267 */
268 _WRAP_VFUNC(bool unlock_stop(), "unlock_stop")
269
270 /** Prepare the Gst::Segment that will be passed to the do_seek_vfunc()
271 * vmethod for executing a seek request. Sub-classes should override this if
272 * they support seeking in formats other than the configured native format.
273 * By default, it tries to convert the seek arguments to the configured
274 * native format and prepare a segment in that format.
275 */
276 virtual bool prepare_seek_segment_vfunc(const Glib::RefPtr<Gst::Event>& seek, Gst::Segment& segment);
277
278 #m4 _CONVERSION(`GstQuery*', `const Glib::RefPtr<Gst::Query>&', `Glib::wrap($3, true)')
279 /** Configure the allocation query.
280 */
281 _WRAP_VFUNC(bool decide_allocation(const Glib::RefPtr<Gst::Query>& query), "decide_allocation")
282
283 /** Ask the subclass to allocate a buffer with for offset and size. The default
284 * implementation will create a new buffer from the negotiated allocator.
285 */
286 Gst::FlowReturn alloc_vfunc(guint64 offset, guint size, Glib::RefPtr<Gst::Buffer>& buffer);
287
288 /** Ask the subclass to fill the buffer with data for offset and size. The passed
289 * buffer is guaranteed to hold the requested amount of bytes.
290 */
291 _WRAP_VFUNC(Gst::FlowReturn fill(guint64 offset, guint size, const Glib::RefPtr<Gst::Buffer>& buffer), "fill")
292 protected:
293 #m4begin
294 _PUSH(SECTION_PCC_CLASS_INIT_VFUNCS)
295 klass->do_seek = &do_seek_vfunc_callback;
296 klass->prepare_seek_segment = &prepare_seek_segment_vfunc_callback;
297 klass->create = &create_vfunc_callback;
298 klass->query = &query_vfunc_callback;
299 klass->alloc = &alloc_vfunc_callback;
300 _SECTION(SECTION_PH_VFUNCS)
301 static gboolean do_seek_vfunc_callback(GstBaseSrc* self, GstSegment* segment);
302 static gboolean prepare_seek_segment_vfunc_callback(GstBaseSrc* self, GstEvent* seek, GstSegment* segment);
303 static GstFlowReturn create_vfunc_callback(GstBaseSrc* self, guint64 offset, guint size, GstBuffer** buf);
304 static gboolean query_vfunc_callback(GstBaseSrc* self, GstQuery* query);
305 static GstFlowReturn alloc_vfunc_callback(GstBaseSrc* self, guint64 offset, guint size, GstBuffer** buf);
306 _POP()
307 #m4end
308 };
309
310 } //namespace Gst