1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD 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 gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19  
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 /*
25  * Conversion parameters:
26  * inFile  = gstreamer-GstBuffer.html
27  * outPack = gstreamer
28  * outFile = Buffer
29  * strct   = GstBuffer
30  * realStrct=
31  * ctorStrct=
32  * clss    = Buffer
33  * interf  = 
34  * class Code: No
35  * interface Code: No
36  * template for:
37  * extend  = 
38  * implements:
39  * prefixes:
40  * 	- gst_buffer_
41  * 	- gst_
42  * omit structs:
43  * 	- GstBuffer
44  * omit prefixes:
45  * omit code:
46  * omit signals:
47  * imports:
48  * 	- glib.Str
49  * 	- gstreamer.Caps
50  * structWrap:
51  * 	- GstBuffer* -> Buffer
52  * 	- GstCaps* -> Caps
53  * module aliases:
54  * local aliases:
55  * overrides:
56  */
57 
58 module gstreamer.Buffer;
59 
60 public  import gstreamerc.gstreamertypes;
61 
62 private import gstreamerc.gstreamer;
63 private import glib.ConstructionException;
64 private import gobject.ObjectG;
65 
66 
67 private import glib.Str;
68 private import gstreamer.Caps;
69 
70 
71 
72 
73 /**
74  * Description
75  * Buffers are the basic unit of data transfer in GStreamer. The GstBuffer
76  * type provides all the state necessary to define a region of memory as part
77  * of a stream. Sub-buffers are also supported, allowing a smaller region of a
78  * buffer to become its own buffer, with mechanisms in place to ensure that
79  * neither memory space goes away prematurely.
80  * Buffers are usually created with gst_buffer_new(). After a buffer has been
81  * created one will typically allocate memory for it and set the size of the
82  * buffer data. The following example creates a buffer that can hold a given
83  * video frame with a given width, height and bits per plane.
84  * $(DDOC_COMMENT example)
85  * Alternatively, use gst_buffer_new_and_alloc()
86  * to create a buffer with preallocated data of a given size.
87  * The data pointed to by the buffer can be retrieved with the GST_BUFFER_DATA()
88  * macro. The size of the data can be found with GST_BUFFER_SIZE(). For buffers
89  * of size 0, the data pointer is undefined (usually NULL) and should never be used.
90  * If an element knows what pad you will push the buffer out on, it should use
91  * gst_pad_alloc_buffer() instead to create a buffer. This allows downstream
92  * elements to provide special buffers to write in, like hardware buffers.
93  * A buffer has a pointer to a GstCaps describing the media type of the data
94  * in the buffer. Attach caps to the buffer with gst_buffer_set_caps(); this
95  * is typically done before pushing out a buffer using gst_pad_push() so that
96  * the downstream element knows the type of the buffer.
97  * A buffer will usually have a timestamp, and a duration, but neither of these
98  * are guaranteed (they may be set to GST_CLOCK_TIME_NONE). Whenever a
99  * meaningful value can be given for these, they should be set. The timestamp
100  * and duration are measured in nanoseconds (they are GstClockTime values).
101  * A buffer can also have one or both of a start and an end offset. These are
102  * media-type specific. For video buffers, the start offset will generally be
103  * the frame number. For audio buffers, it will be the number of samples
104  * produced so far. For compressed data, it could be the byte offset in a
105  * source or destination file. Likewise, the end offset will be the offset of
106  * the end of the buffer. These can only be meaningfully interpreted if you
107  * know the media type of the buffer (the GstCaps set on it). Either or both
108  * can be set to GST_BUFFER_OFFSET_NONE.
109  * gst_buffer_ref() is used to increase the refcount of a buffer. This must be
110  * done when you want to keep a handle to the buffer after pushing it to the
111  * next element.
112  * To efficiently create a smaller buffer out of an existing one, you can
113  * use gst_buffer_create_sub().
114  * If a plug-in wants to modify the buffer data in-place, it should first obtain
115  * a buffer that is safe to modify by using gst_buffer_make_writable(). This
116  * function is optimized so that a copy will only be made when it is necessary.
117  * A plugin that only wishes to modify the metadata of a buffer, such as the
118  * offset, timestamp or caps, should use gst_buffer_make_metadata_writable(),
119  * which will create a subbuffer of the original buffer to ensure the caller
120  * has sole ownership, and not copy the buffer data.
121  * Several flags of the buffer can be set and unset with the
122  * GST_BUFFER_FLAG_SET() and GST_BUFFER_FLAG_UNSET() macros. Use
123  * GST_BUFFER_FLAG_IS_SET() to test if a certain GstBufferFlag is set.
124  * Buffers can be efficiently merged into a larger buffer with
125  * gst_buffer_merge() and gst_buffer_span() if the gst_buffer_is_span_fast()
126  * function returns TRUE.
127  * An element should either unref the buffer or push it out on a src pad
128  * using gst_pad_push() (see GstPad).
129  * Buffers are usually freed by unreffing them with gst_buffer_unref(). When
130  * the refcount drops to 0, any data pointed to by GST_BUFFER_MALLOCDATA() will
131  * also be freed.
132  * Last reviewed on August 11th, 2006 (0.10.10)
133  */
134 public class Buffer
135 {
136 	
137 	/** the main Gtk struct */
138 	protected GstBuffer* gstBuffer;
139 	
140 	
141 	public GstBuffer* getBufferStruct()
142 	{
143 		return gstBuffer;
144 	}
145 	
146 	
147 	/** the main Gtk struct as a void* */
148 	protected void* getStruct()
149 	{
150 		return cast(void*)gstBuffer;
151 	}
152 	
153 	/**
154 	 * Sets our main struct and passes it to the parent class
155 	 */
156 	public this (GstBuffer* gstBuffer)
157 	{
158 		this.gstBuffer = gstBuffer;
159 	}
160 	
161 	/**
162 	 */
163 	
164 	/**
165 	 * Creates a newly allocated buffer without any data.
166 	 * MT safe.
167 	 * Throws: ConstructionException GTK+ fails to create the object.
168 	 */
169 	public this ()
170 	{
171 		// GstBuffer* gst_buffer_new (void);
172 		auto p = gst_buffer_new();
173 		if(p is null)
174 		{
175 			throw new ConstructionException("null returned by gst_buffer_new()");
176 		}
177 		this(cast(GstBuffer*) p);
178 	}
179 	
180 	/**
181 	 * Creates a newly allocated buffer with data of the given size.
182 	 * The buffer memory is not cleared. If the requested amount of
183 	 * memory can't be allocated, the program will abort. Use
184 	 * gst_buffer_try_new_and_alloc() if you want to handle this case
185 	 * gracefully or have gotten the size to allocate from an untrusted
186 	 * source such as a media stream.
187 	 * Note that when size == 0, the buffer data pointer will be NULL.
188 	 * MT safe.
189 	 * Params:
190 	 * size = the size of the new buffer's data.
191 	 * Throws: ConstructionException GTK+ fails to create the object.
192 	 */
193 	public this (uint size)
194 	{
195 		// GstBuffer* gst_buffer_new_and_alloc (guint size);
196 		auto p = gst_buffer_new_and_alloc(size);
197 		if(p is null)
198 		{
199 			throw new ConstructionException("null returned by gst_buffer_new_and_alloc(size)");
200 		}
201 		this(cast(GstBuffer*) p);
202 	}
203 	
204 	/**
205 	 * Tries to create a newly allocated buffer with data of the given size. If
206 	 * the requested amount of memory can't be allocated, NULL will be returned.
207 	 * The buffer memory is not cleared.
208 	 * Note that when size == 0, the buffer data pointer will be NULL.
209 	 * MT safe.
210 	 * Params:
211 	 * size = the size of the new buffer's data.
212 	 * Returns: a new GstBuffer, or NULL if the memory couldn't be allocated. Since 0.10.13
213 	 */
214 	public static Buffer tryNewAndAlloc(uint size)
215 	{
216 		// GstBuffer* gst_buffer_try_new_and_alloc (guint size);
217 		auto p = gst_buffer_try_new_and_alloc(size);
218 		
219 		if(p is null)
220 		{
221 			return null;
222 		}
223 		
224 		return ObjectG.getDObject!(Buffer)(cast(GstBuffer*) p);
225 	}
226 	
227 	/**
228 	 * Increases the refcount of the given buffer by one.
229 	 * Note that the refcount affects the writeability
230 	 * of buf and its metadata, see gst_buffer_is_writable() and
231 	 * gst_buffer_is_metadata_writable(). It is
232 	 * important to note that keeping additional references to
233 	 * GstBuffer instances can potentially increase the number
234 	 * of memcpy operations in a pipeline.
235 	 * Returns: buf
236 	 */
237 	public Buffer doref()
238 	{
239 		// GstBuffer* gst_buffer_ref (GstBuffer *buf);
240 		auto p = gst_buffer_ref(gstBuffer);
241 		
242 		if(p is null)
243 		{
244 			return null;
245 		}
246 		
247 		return ObjectG.getDObject!(Buffer)(cast(GstBuffer*) p);
248 	}
249 	
250 	/**
251 	 * Copies the metadata from src into dest. The data, size and mallocdata
252 	 * fields are not copied.
253 	 * flags indicate which fields will be copied. Use GST_BUFFER_COPY_ALL to copy
254 	 * all the metadata fields.
255 	 * This function is typically called from a custom buffer copy function after
256 	 * creating dest and setting the data, size, mallocdata.
257 	 * Params:
258 	 * src = a source GstBuffer
259 	 * flags = flags indicating what metadata fields should be copied.
260 	 * Since 0.10.13
261 	 */
262 	public void copyMetadata(Buffer src, GstBufferCopyFlags flags)
263 	{
264 		// void gst_buffer_copy_metadata (GstBuffer *dest,  const GstBuffer *src,  GstBufferCopyFlags flags);
265 		gst_buffer_copy_metadata(gstBuffer, (src is null) ? null : src.getBufferStruct(), flags);
266 	}
267 	
268 	/**
269 	 * Similar to gst_buffer_is_writable, but this only ensures that the
270 	 * refcount of the buffer is 1, indicating that the caller is the sole
271 	 * owner and can change the buffer metadata, such as caps and timestamps.
272 	 * Returns: TRUE if the metadata is writable.
273 	 */
274 	public int isMetadataWritable()
275 	{
276 		// gboolean gst_buffer_is_metadata_writable (GstBuffer *buf);
277 		return gst_buffer_is_metadata_writable(gstBuffer);
278 	}
279 	
280 	/**
281 	 * Similar to gst_buffer_make_writable, but does not ensure that the buffer
282 	 * data array is writable. Instead, this just ensures that the returned buffer
283 	 * is solely owned by the caller, by creating a subbuffer of the original
284 	 * buffer if necessary.
285 	 * After calling this function, buf should not be referenced anymore. The
286 	 * result of this function has guaranteed writable metadata.
287 	 * Returns: A new GstBuffer with writable metadata.
288 	 */
289 	public Buffer makeMetadataWritable()
290 	{
291 		// GstBuffer* gst_buffer_make_metadata_writable (GstBuffer *buf);
292 		auto p = gst_buffer_make_metadata_writable(gstBuffer);
293 		
294 		if(p is null)
295 		{
296 			return null;
297 		}
298 		
299 		return ObjectG.getDObject!(Buffer)(cast(GstBuffer*) p);
300 	}
301 	
302 	/**
303 	 * Gets the media type of the buffer. This can be NULL if there
304 	 * is no media type attached to this buffer.
305 	 * Returns: a reference to the GstCaps. unref after usage.
306 	 * Returns: NULL if there were no caps on this buffer.
307 	 */
308 	public Caps getCaps()
309 	{
310 		// GstCaps* gst_buffer_get_caps (GstBuffer *buffer);
311 		auto p = gst_buffer_get_caps(gstBuffer);
312 		
313 		if(p is null)
314 		{
315 			return null;
316 		}
317 		
318 		return ObjectG.getDObject!(Caps)(cast(GstCaps*) p);
319 	}
320 	
321 	/**
322 	 * Sets the media type on the buffer. The refcount of the caps will
323 	 * be increased and any previous caps on the buffer will be
324 	 * unreffed.
325 	 * Params:
326 	 * caps = a GstCaps.
327 	 */
328 	public void setCaps(Caps caps)
329 	{
330 		// void gst_buffer_set_caps (GstBuffer *buffer,  GstCaps *caps);
331 		gst_buffer_set_caps(gstBuffer, (caps is null) ? null : caps.getCapsStruct());
332 	}
333 	
334 	/**
335 	 * Creates a sub-buffer from parent at offset and size.
336 	 * This sub-buffer uses the actual memory space of the parent buffer.
337 	 * This function will copy the offset and timestamp fields when the
338 	 * offset is 0. If not, they will be set to GST_CLOCK_TIME_NONE and
339 	 * GST_BUFFER_OFFSET_NONE.
340 	 * If offset equals 0 and size equals the total size of buffer, the
341 	 * duration and offset end fields are also copied. If not they will be set
342 	 * to GST_CLOCK_TIME_NONE and GST_BUFFER_OFFSET_NONE.
343 	 * MT safe.
344 	 * Returns: the new GstBuffer.
345 	 * Params:
346 	 * offset = the offset into parent GstBuffer at which the new sub-buffer
347 	 *  begins.
348 	 * size = the size of the new GstBuffer sub-buffer, in bytes.
349 	 * Returns: NULL if the arguments were invalid.
350 	 */
351 	public Buffer createSub(uint offset, uint size)
352 	{
353 		// GstBuffer* gst_buffer_create_sub (GstBuffer *parent,  guint offset,  guint size);
354 		auto p = gst_buffer_create_sub(gstBuffer, offset, size);
355 		
356 		if(p is null)
357 		{
358 			return null;
359 		}
360 		
361 		return ObjectG.getDObject!(Buffer)(cast(GstBuffer*) p);
362 	}
363 	
364 	/**
365 	 * Determines whether a gst_buffer_span() can be done without copying
366 	 * the contents, that is, whether the data areas are contiguous sub-buffers of
367 	 * the same buffer.
368 	 * MT safe.
369 	 * Params:
370 	 * buf2 = the second GstBuffer.
371 	 * Returns: TRUE if the buffers are contiguous, FALSE if a copy would be required.
372 	 */
373 	public int isSpanFast(Buffer buf2)
374 	{
375 		// gboolean gst_buffer_is_span_fast (GstBuffer *buf1,  GstBuffer *buf2);
376 		return gst_buffer_is_span_fast(gstBuffer, (buf2 is null) ? null : buf2.getBufferStruct());
377 	}
378 	
379 	/**
380 	 * Creates a new buffer that consists of part of buf1 and buf2.
381 	 * Logically, buf1 and buf2 are concatenated into a single larger
382 	 * buffer, and a new buffer is created at the given offset inside
383 	 * this space, with a given length.
384 	 * If the two source buffers are children of the same larger buffer,
385 	 * and are contiguous, the new buffer will be a child of the shared
386 	 * parent, and thus no copying is necessary. you can use
387 	 * gst_buffer_is_span_fast() to determine if a memcpy will be needed.
388 	 * MT safe.
389 	 * Returns: the new GstBuffer that spans the two source buffers.
390 	 * Params:
391 	 * offset = the offset in the first buffer from where the new
392 	 * buffer should start.
393 	 * buf2 = the second source GstBuffer to merge.
394 	 * len = the total length of the new buffer.
395 	 * Returns: NULL if the arguments are invalid.
396 	 */
397 	public Buffer span(uint offset, Buffer buf2, uint len)
398 	{
399 		// GstBuffer* gst_buffer_span (GstBuffer *buf1,  guint32 offset,  GstBuffer *buf2,  guint32 len);
400 		auto p = gst_buffer_span(gstBuffer, offset, (buf2 is null) ? null : buf2.getBufferStruct(), len);
401 		
402 		if(p is null)
403 		{
404 			return null;
405 		}
406 		
407 		return ObjectG.getDObject!(Buffer)(cast(GstBuffer*) p);
408 	}
409 	
410 	/**
411 	 * Warning
412 	 * gst_buffer_stamp is deprecated and should not be used in newly-written code. use gst_buffer_copy_metadata() instead, it provides more
413 	 * control.
414 	 * Copies additional information (the timestamp, duration, and offset start
415 	 * and end) from one buffer to the other.
416 	 * This function does not copy any buffer flags or caps and is equivalent to
417 	 * gst_buffer_copy_metadata(dest, src, GST_BUFFER_COPY_TIMESTAMPS).
418 	 * Params:
419 	 * src = buffer to stamp from
420 	 */
421 	public void stamp(Buffer src)
422 	{
423 		// void gst_buffer_stamp (GstBuffer *dest,  const GstBuffer *src);
424 		gst_buffer_stamp(gstBuffer, (src is null) ? null : src.getBufferStruct());
425 	}
426 	
427 	/**
428 	 * Create a new buffer that is the concatenation of the two source
429 	 * buffers, and unrefs the original source buffers.
430 	 * If the buffers point to contiguous areas of memory, the buffer
431 	 * is created without copying the data.
432 	 * Params:
433 	 * buf2 = the second source GstBuffer.
434 	 * Returns: the new GstBuffer which is the concatenation of the source buffers.
435 	 */
436 	public Buffer join(Buffer buf2)
437 	{
438 		// GstBuffer* gst_buffer_join (GstBuffer *buf1,  GstBuffer *buf2);
439 		auto p = gst_buffer_join(gstBuffer, (buf2 is null) ? null : buf2.getBufferStruct());
440 		
441 		if(p is null)
442 		{
443 			return null;
444 		}
445 		
446 		return ObjectG.getDObject!(Buffer)(cast(GstBuffer*) p);
447 	}
448 	
449 	/**
450 	 * Create a new buffer that is the concatenation of the two source
451 	 * buffers. The original source buffers will not be modified or
452 	 * unref'd. Make sure you unref the source buffers if they are not used
453 	 * anymore afterwards.
454 	 * If the buffers point to contiguous areas of memory, the buffer
455 	 * is created without copying the data.
456 	 * Params:
457 	 * buf2 = the second source GstBuffer to merge.
458 	 * Returns: the new GstBuffer which is the concatenation of the source buffers.
459 	 */
460 	public Buffer merge(Buffer buf2)
461 	{
462 		// GstBuffer* gst_buffer_merge (GstBuffer *buf1,  GstBuffer *buf2);
463 		auto p = gst_buffer_merge(gstBuffer, (buf2 is null) ? null : buf2.getBufferStruct());
464 		
465 		if(p is null)
466 		{
467 			return null;
468 		}
469 		
470 		return ObjectG.getDObject!(Buffer)(cast(GstBuffer*) p);
471 	}
472 }