File: gstsample.c

package info (click to toggle)
gstreamer1.0 1.22.0-2%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 14,532 kB
  • sloc: ansic: 190,478; python: 1,705; sh: 569; lex: 188; lisp: 154; java: 81; objc: 70; makefile: 57; cpp: 55; perl: 46
file content (480 lines) | stat: -rw-r--r-- 11,970 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/* GStreamer
 * Copyright (C) 2011 Wim Taymans <wim.taymans@gmail.com>
 *
 * gstsample.c: media sample
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

/**
 * SECTION:gstsample
 * @title: GstSample
 * @short_description: A media sample
 * @see_also: #GstBuffer, #GstCaps, #GstSegment
 *
 * A #GstSample is a small object containing data, a type, timing and
 * extra arbitrary information.
 */
#define GST_DISABLE_MINIOBJECT_INLINE_FUNCTIONS
#include "gst_private.h"

#include "gstsample.h"

GST_DEBUG_CATEGORY_STATIC (gst_sample_debug);
#define GST_CAT_DEFAULT gst_sample_debug

struct _GstSample
{
  GstMiniObject mini_object;

  GstBuffer *buffer;
  GstCaps *caps;
  GstSegment segment;
  GstStructure *info;
  GstBufferList *buffer_list;
};

GType _gst_sample_type = 0;

GST_DEFINE_MINI_OBJECT_TYPE (GstSample, gst_sample);

void
_priv_gst_sample_initialize (void)
{
  _gst_sample_type = gst_sample_get_type ();

  GST_DEBUG_CATEGORY_INIT (gst_sample_debug, "sample", 0, "GstSample debug");
}

static GstSample *
_gst_sample_copy (GstSample * sample)
{
  GstSample *copy;

  copy = gst_sample_new (sample->buffer, sample->caps, &sample->segment,
      (sample->info) ? gst_structure_copy (sample->info) : NULL);

  if (sample->buffer_list) {
    copy->buffer_list = gst_buffer_list_ref (sample->buffer_list);
    gst_mini_object_add_parent (GST_MINI_OBJECT_CAST (copy->buffer_list),
        GST_MINI_OBJECT_CAST (copy));
  }

  return copy;
}

static void
_gst_sample_free (GstSample * sample)
{
  GST_LOG ("free %p", sample);

  if (sample->buffer) {
    gst_mini_object_remove_parent (GST_MINI_OBJECT_CAST (sample->buffer),
        GST_MINI_OBJECT_CAST (sample));
    gst_buffer_unref (sample->buffer);
  }

  if (sample->caps) {
    gst_mini_object_remove_parent (GST_MINI_OBJECT_CAST (sample->caps),
        GST_MINI_OBJECT_CAST (sample));
    gst_caps_unref (sample->caps);
  }

  if (sample->info) {
    gst_structure_set_parent_refcount (sample->info, NULL);
    gst_structure_free (sample->info);
  }
  if (sample->buffer_list) {
    gst_mini_object_remove_parent (GST_MINI_OBJECT_CAST (sample->buffer_list),
        GST_MINI_OBJECT_CAST (sample));
    gst_buffer_list_unref (sample->buffer_list);
  }
#ifdef USE_POISONING
  memset (sample, 0xff, sizeof (GstSample));
#endif

  g_slice_free1 (sizeof (GstSample), sample);
}

/**
 * gst_sample_new:
 * @buffer: (transfer none) (allow-none): a #GstBuffer, or %NULL
 * @caps: (transfer none) (allow-none): a #GstCaps, or %NULL
 * @segment: (transfer none) (allow-none): a #GstSegment, or %NULL
 * @info: (transfer full) (allow-none): a #GstStructure, or %NULL
 *
 * Create a new #GstSample with the provided details.
 *
 * Free-function: gst_sample_unref
 *
 * Returns: (transfer full): the new #GstSample. gst_sample_unref()
 *     after usage.
 */
GstSample *
gst_sample_new (GstBuffer * buffer, GstCaps * caps, const GstSegment * segment,
    GstStructure * info)
{
  GstSample *sample;

  sample = g_slice_new0 (GstSample);

  GST_LOG ("new %p", sample);

  gst_mini_object_init (GST_MINI_OBJECT_CAST (sample), 0, _gst_sample_type,
      (GstMiniObjectCopyFunction) _gst_sample_copy, NULL,
      (GstMiniObjectFreeFunction) _gst_sample_free);

  if (buffer) {
    sample->buffer = gst_buffer_ref (buffer);
    gst_mini_object_add_parent (GST_MINI_OBJECT_CAST (sample->buffer),
        GST_MINI_OBJECT_CAST (sample));
  }

  if (caps) {
    sample->caps = gst_caps_ref (caps);
    gst_mini_object_add_parent (GST_MINI_OBJECT_CAST (sample->caps),
        GST_MINI_OBJECT_CAST (sample));
  }

  /* FIXME 2.0: initialize with GST_FORMAT_UNDEFINED by default */
  if (segment)
    gst_segment_copy_into (segment, &sample->segment);
  else
    gst_segment_init (&sample->segment, GST_FORMAT_TIME);

  if (info) {
    if (!gst_structure_set_parent_refcount (info,
            &sample->mini_object.refcount))
      goto had_parent;

    sample->info = info;
  }
  return sample;

  /* ERRORS */
had_parent:
  {
    gst_sample_unref (sample);
    g_warning ("structure is already owned by another object");
    return NULL;
  }
}

/**
 * gst_sample_get_buffer:
 * @sample: a #GstSample
 *
 * Get the buffer associated with @sample
 *
 * Returns: (transfer none) (nullable): the buffer of @sample or %NULL
 *  when there is no buffer. The buffer remains valid as long as
 *  @sample is valid.  If you need to hold on to it for longer than
 *  that, take a ref to the buffer with gst_buffer_ref().
 */
GstBuffer *
gst_sample_get_buffer (GstSample * sample)
{
  g_return_val_if_fail (GST_IS_SAMPLE (sample), NULL);

  return sample->buffer;
}

/**
 * gst_sample_get_caps:
 * @sample: a #GstSample
 *
 * Get the caps associated with @sample
 *
 * Returns: (transfer none) (nullable): the caps of @sample or %NULL
 *  when there is no caps. The caps remain valid as long as @sample is
 *  valid.  If you need to hold on to the caps for longer than that,
 *  take a ref to the caps with gst_caps_ref().
 */
GstCaps *
gst_sample_get_caps (GstSample * sample)
{
  g_return_val_if_fail (GST_IS_SAMPLE (sample), NULL);

  return sample->caps;
}

/**
 * gst_sample_get_segment:
 * @sample: a #GstSample
 *
 * Get the segment associated with @sample
 *
 * Returns: (transfer none): the segment of @sample.
 *  The segment remains valid as long as @sample is valid.
 */
GstSegment *
gst_sample_get_segment (GstSample * sample)
{
  g_return_val_if_fail (GST_IS_SAMPLE (sample), NULL);

  return &sample->segment;
}

/**
 * gst_sample_get_info:
 * @sample: a #GstSample
 *
 * Get extra information associated with @sample.
 *
 * Returns: (transfer none) (nullable): the extra info of @sample.
 *  The info remains valid as long as @sample is valid.
 */
const GstStructure *
gst_sample_get_info (GstSample * sample)
{
  g_return_val_if_fail (GST_IS_SAMPLE (sample), NULL);

  return sample->info;
}

/**
 * gst_sample_get_buffer_list:
 * @sample: a #GstSample
 *
 * Get the buffer list associated with @sample
 *
 * Returns: (transfer none) (nullable): the buffer list of @sample or %NULL
 *  when there is no buffer list. The buffer list remains valid as long as
 *  @sample is valid.  If you need to hold on to it for longer than
 *  that, take a ref to the buffer list with gst_mini_object_ref ().
 *
 * Since: 1.6
 */
GstBufferList *
gst_sample_get_buffer_list (GstSample * sample)
{
  g_return_val_if_fail (GST_IS_SAMPLE (sample), NULL);

  return sample->buffer_list;
}

/**
 * gst_sample_set_buffer_list:
 * @sample: a #GstSample
 * @buffer_list: a #GstBufferList
 *
 * Set the buffer list associated with @sample. @sample must be writable.
 *
 * Since: 1.6
 */
void
gst_sample_set_buffer_list (GstSample * sample, GstBufferList * buffer_list)
{
  GstBufferList *old = NULL;
  g_return_if_fail (GST_IS_SAMPLE (sample));
  g_return_if_fail (gst_sample_is_writable (sample));

  old = sample->buffer_list;

  if (old == buffer_list)
    return;

  if (buffer_list) {
    sample->buffer_list = gst_buffer_list_ref (buffer_list);
    gst_mini_object_add_parent (GST_MINI_OBJECT_CAST (sample->buffer_list),
        GST_MINI_OBJECT_CAST (sample));
  } else {
    sample->buffer_list = NULL;
  }

  if (old) {
    gst_mini_object_remove_parent (GST_MINI_OBJECT_CAST (old),
        GST_MINI_OBJECT_CAST (sample));
    gst_buffer_list_unref (old);
  }
}

/**
 * gst_sample_set_buffer:
 * @sample: A #GstSample
 * @buffer: (transfer none): A #GstBuffer
 *
 * Set the buffer associated with @sample. @sample must be writable.
 *
 * Since: 1.16
 */
void
gst_sample_set_buffer (GstSample * sample, GstBuffer * buffer)
{
  GstBuffer *old = NULL;

  g_return_if_fail (GST_IS_SAMPLE (sample));
  g_return_if_fail (gst_sample_is_writable (sample));

  old = sample->buffer;

  if (old == buffer)
    return;

  if (buffer) {
    sample->buffer = gst_buffer_ref (buffer);
    gst_mini_object_add_parent (GST_MINI_OBJECT_CAST (sample->buffer),
        GST_MINI_OBJECT_CAST (sample));
  } else {
    sample->buffer = NULL;
  }

  if (old) {
    gst_mini_object_remove_parent (GST_MINI_OBJECT_CAST (old),
        GST_MINI_OBJECT_CAST (sample));
    gst_buffer_unref (old);
  }
}

/**
 * gst_sample_set_caps:
 * @sample: A #GstSample
 * @caps: (transfer none): A #GstCaps
 *
 * Set the caps associated with @sample. @sample must be writable.
 *
 * Since: 1.16
 */
void
gst_sample_set_caps (GstSample * sample, GstCaps * caps)
{
  GstCaps *old = NULL;

  g_return_if_fail (GST_IS_SAMPLE (sample));
  g_return_if_fail (gst_sample_is_writable (sample));

  old = sample->caps;

  if (old == caps)
    return;

  if (caps) {
    sample->caps = gst_caps_ref (caps);
    gst_mini_object_add_parent (GST_MINI_OBJECT_CAST (sample->caps),
        GST_MINI_OBJECT_CAST (sample));
  } else {
    sample->caps = NULL;
  }

  if (old) {
    gst_mini_object_remove_parent (GST_MINI_OBJECT_CAST (old),
        GST_MINI_OBJECT_CAST (sample));
    gst_caps_unref (old);
  }
}

/**
 * gst_sample_set_segment:
 * @sample: A #GstSample
 * @segment: (transfer none): A #GstSegment
 *
 * Set the segment associated with @sample. @sample must be writable.
 *
 * Since: 1.16
 */
void
gst_sample_set_segment (GstSample * sample, const GstSegment * segment)
{
  g_return_if_fail (GST_IS_SAMPLE (sample));
  g_return_if_fail (gst_sample_is_writable (sample));

  /* FIXME 2.0: initialize with GST_FORMAT_UNDEFINED by default */
  if (segment)
    gst_segment_copy_into (segment, &sample->segment);
  else
    gst_segment_init (&sample->segment, GST_FORMAT_TIME);
}

/**
 * gst_sample_set_info:
 * @sample: A #GstSample
 * @info: (transfer full): A #GstStructure
 *
 * Set the info structure associated with @sample. @sample must be writable,
 * and @info must not have a parent set already.
 *
 * Since: 1.16
 */
gboolean
gst_sample_set_info (GstSample * sample, GstStructure * info)
{
  g_return_val_if_fail (GST_IS_SAMPLE (sample), FALSE);
  g_return_val_if_fail (gst_sample_is_writable (sample), FALSE);

  if (info) {
    if (!gst_structure_set_parent_refcount (info,
            &sample->mini_object.refcount))
      goto had_parent;
  }

  if (sample->info) {
    gst_structure_set_parent_refcount (sample->info, NULL);
    gst_structure_free (sample->info);
  }

  sample->info = info;

  return TRUE;

had_parent:
  g_warning ("structure is already owned by another object");
  return FALSE;
}

/**
 * gst_sample_ref: (skip)
 * @sample: a #GstSample
 *
 * Increases the refcount of the given sample by one.
 *
 * Returns: (transfer full): @sample
 */
GstSample *
gst_sample_ref (GstSample * sample)
{
  return GST_SAMPLE_CAST (gst_mini_object_ref (GST_MINI_OBJECT_CAST (sample)));
}

/**
 * gst_sample_unref: (skip)
 * @sample: (transfer full): a #GstSample
 *
 * Decreases the refcount of the sample. If the refcount reaches 0, the
 * sample will be freed.
 */
void
gst_sample_unref (GstSample * sample)
{
  gst_mini_object_unref (GST_MINI_OBJECT_CAST (sample));
}

/**
 * gst_sample_copy: (skip)
 * @buf: a #GstSample.
 *
 * Create a copy of the given sample. This will also make a newly allocated
 * copy of the data the source sample contains.
 *
 * Returns: (transfer full): a new copy of @buf.
 *
 * Since: 1.2
 */
GstSample *
gst_sample_copy (const GstSample * buf)
{
  return
      GST_SAMPLE_CAST (gst_mini_object_copy (GST_MINI_OBJECT_CONST_CAST (buf)));
}