gstquery.c 64.4 KB
Newer Older
1
2
3
/* GStreamer
 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
 *                    2000 Wim Taymans <wim.taymans@chello.be>
4
 *                    2005 Wim Taymans <wim@fluendo.com>
5
 *
6
 * gstquery.c: GstQueryType registration and Query parsing/creation
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 *
 * 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., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
23

Stefan Kost's avatar
Stefan Kost committed
24
25
/**
 * SECTION:gstquery
26
27
 * @short_description: Dynamically register new query types. Provide functions
 *                     to create queries, and to set and parse values in them.
Stefan Kost's avatar
Stefan Kost committed
28
29
 * @see_also: #GstPad, #GstElement
 *
Stefan Kost's avatar
Stefan Kost committed
30
31
32
33
34
 * GstQuery functions are used to register new query types to the gstreamer
 * core and use them.
 * Queries can be performed on pads (gst_pad_query()) and elements
 * (gst_element_query()). Please note that some queries might need a running
 * pipeline to work.
Stefan Kost's avatar
Stefan Kost committed
35
 *
36
37
38
 * Queries can be created using the gst_query_new_*() functions.
 * Query values can be set using gst_query_set_*(), and parsed using
 * gst_query_parse_*() helpers.
Wim Taymans's avatar
Wim Taymans committed
39
40
 *
 * The following example shows how to query the duration of a pipeline:
Wim Taymans's avatar
Wim Taymans committed
41
 *
Wim Taymans's avatar
Wim Taymans committed
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
 * <example>
 *  <title>Query duration on a pipeline</title>
 *  <programlisting>
 *  GstQuery *query;
 *  gboolean res;
 *  query = gst_query_new_duration (GST_FORMAT_TIME);
 *  res = gst_element_query (pipeline, query);
 *  if (res) {
 *    gint64 duration;
 *    gst_query_parse_duration (query, NULL, &amp;duration);
 *    g_print ("duration = %"GST_TIME_FORMAT, GST_TIME_ARGS (duration));
 *  }
 *  else {
 *    g_print ("duration query failed...");
 *  }
 *  gst_query_unref (query);
 *  </programlisting>
 * </example>
 *
 * Last reviewed on 2006-02-14 (0.10.4)
Stefan Kost's avatar
Stefan Kost committed
62
 */
63

64
65
66
67
68

/* FIXME 0.11: suppress warnings for deprecated API such as GValueArray
 * with newer GLib versions (>= 2.31.0) */
#define GLIB_DISABLE_DEPRECATION_WARNINGS

69
70
#include "gst_private.h"
#include "gstinfo.h"
71
#include "gstquery.h"
72
#include "gstvalue.h"
73
#include "gstenumtypes.h"
74
#include "gstquark.h"
75
#include "gsturi.h"
76
#include "gstbufferpool.h"
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
77

78
79
GST_DEBUG_CATEGORY_STATIC (gst_query_debug);
#define GST_CAT_DEFAULT gst_query_debug
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
80

81
static GType _gst_query_type = 0;
82

83
84
85
86
87
88
89
90
91
typedef struct
{
  GstQuery query;

  GstStructure *structure;
} GstQueryImpl;

#define GST_QUERY_STRUCTURE(q)  (((GstQueryImpl *)(q))->structure)

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
typedef struct
{
  const gint type;
  const gchar *name;
  GQuark quark;
} GstQueryQuarks;

static GstQueryQuarks query_quarks[] = {
  {GST_QUERY_UNKNOWN, "unknown", 0},
  {GST_QUERY_POSITION, "position", 0},
  {GST_QUERY_DURATION, "duration", 0},
  {GST_QUERY_LATENCY, "latency", 0},
  {GST_QUERY_JITTER, "jitter", 0},
  {GST_QUERY_RATE, "rate", 0},
  {GST_QUERY_SEEKING, "seeking", 0},
  {GST_QUERY_SEGMENT, "segment", 0},
  {GST_QUERY_CONVERT, "convert", 0},
  {GST_QUERY_FORMATS, "formats", 0},
  {GST_QUERY_BUFFERING, "buffering", 0},
  {GST_QUERY_CUSTOM, "custom", 0},
  {GST_QUERY_URI, "uri", 0},
  {GST_QUERY_ALLOCATION, "allocation", 0},
  {GST_QUERY_SCHEDULING, "scheduling", 0},
  {GST_QUERY_ACCEPT_CAPS, "accept-caps", 0},
  {GST_QUERY_CAPS, "caps", 0},
Wim Taymans's avatar
Wim Taymans committed
118
  {GST_QUERY_DRAIN, "drain", 0},
119
120

  {0, NULL, 0}
121
122
};

123
GST_DEFINE_MINI_OBJECT_TYPE (GstQuery, gst_query);
Wim Taymans's avatar
Wim Taymans committed
124

125
void
126
_priv_gst_query_initialize (void)
127
{
128
  gint i;
129

130
  _gst_query_type = gst_query_get_type ();
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
131

132
133
  GST_DEBUG_CATEGORY_INIT (gst_query_debug, "query", 0, "query system");

134
135
  for (i = 0; query_quarks[i].name; i++) {
    query_quarks[i].quark = g_quark_from_static_string (query_quarks[i].name);
136
137
138
  }
}

Wim Taymans's avatar
Wim Taymans committed
139
140
/**
 * gst_query_type_get_name:
141
 * @type: the query type
Wim Taymans's avatar
Wim Taymans committed
142
143
144
145
146
 *
 * Get a printable name for the given query type. Do not modify or free.
 *
 * Returns: a reference to the static name of the query.
 */
147
const gchar *
148
gst_query_type_get_name (GstQueryType type)
149
{
150
  gint i;
151

152
153
154
155
156
  for (i = 0; query_quarks[i].name; i++) {
    if (type == query_quarks[i].type)
      return query_quarks[i].name;
  }
  return "unknown";
157
158
}

Wim Taymans's avatar
Wim Taymans committed
159
160
/**
 * gst_query_type_to_quark:
161
 * @type: the query type
Wim Taymans's avatar
Wim Taymans committed
162
163
164
165
166
 *
 * Get the unique quark for the given query type.
 *
 * Returns: the quark associated with the query type
 */
167
GQuark
168
gst_query_type_to_quark (GstQueryType type)
169
{
170
  gint i;
171

172
173
174
  for (i = 0; query_quarks[i].name; i++) {
    if (type == query_quarks[i].type)
      return query_quarks[i].quark;
175
  }
176
  return 0;
177
178
179
}

/**
180
 * gst_query_type_get_flags:
181
 * @type: a #GstQueryType
182
 *
183
 * Gets the #GstQueryTypeFlags associated with @type.
184
 *
185
 * Returns: a #GstQueryTypeFlags.
186
 */
187
188
GstQueryTypeFlags
gst_query_type_get_flags (GstQueryType type)
189
{
190
  GstQueryTypeFlags ret;
191

192
  ret = type & ((1 << GST_EVENT_NUM_SHIFT) - 1);
193

194
  return ret;
195
}
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
196

197
198
199
static void
_gst_query_free (GstQuery * query)
{
200
201
  GstStructure *s;

202
203
  g_return_if_fail (query != NULL);

204
205
206
207
  s = GST_QUERY_STRUCTURE (query);
  if (s) {
    gst_structure_set_parent_refcount (s, NULL);
    gst_structure_free (s);
208
209
  }

Wim Taymans's avatar
Wim Taymans committed
210
  g_slice_free1 (GST_MINI_OBJECT_SIZE (query), query);
211
212
213
214
215
216
}

static GstQuery *
_gst_query_copy (GstQuery * query)
{
  GstQuery *copy;
Wim Taymans's avatar
Wim Taymans committed
217
  GstStructure *s;
218

Wim Taymans's avatar
Wim Taymans committed
219
220
221
222
223
  s = GST_QUERY_STRUCTURE (query);
  if (s) {
    s = gst_structure_copy (s);
  }
  copy = gst_query_new_custom (query->type, s);
224
225
226
227

  return copy;
}

Wim Taymans's avatar
Wim Taymans committed
228
229
static void
gst_query_init (GstQueryImpl * query, gsize size, GstQueryType type)
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
230
{
Wim Taymans's avatar
Wim Taymans committed
231
  gst_mini_object_init (GST_MINI_OBJECT_CAST (query), _gst_query_type, size);
232

233
234
  query->query.mini_object.copy = (GstMiniObjectCopyFunction) _gst_query_copy;
  query->query.mini_object.free = (GstMiniObjectFreeFunction) _gst_query_free;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
235

Wim Taymans's avatar
Wim Taymans committed
236
  GST_EVENT_TYPE (query) = type;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
237
238
}

Stefan Kost's avatar
Stefan Kost committed
239
240
241
242
243
/**
 * gst_query_new_position:
 * @format: the default #GstFormat for the new query
 *
 * Constructs a new query stream position query object. Use gst_query_unref()
244
245
 * when done with it. A position query is used to query the current position
 * of playback in the streams, in some format.
Stefan Kost's avatar
Stefan Kost committed
246
 *
247
248
249
 * Free-function: gst_query_unref
 *
 * Returns: (transfer full): a new #GstQuery
Stefan Kost's avatar
Stefan Kost committed
250
 */
251
252
253
254
255
256
GstQuery *
gst_query_new_position (GstFormat format)
{
  GstQuery *query;
  GstStructure *structure;

Wim Taymans's avatar
Wim Taymans committed
257
  structure = gst_structure_new_id (GST_QUARK (QUERY_POSITION),
258
      GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
259
      GST_QUARK (CURRENT), G_TYPE_INT64, G_GINT64_CONSTANT (-1), NULL);
260

Wim Taymans's avatar
Wim Taymans committed
261
  query = gst_query_new_custom (GST_QUERY_POSITION, structure);
262
263
264
265

  return query;
}

Stefan Kost's avatar
Stefan Kost committed
266
267
/**
 * gst_query_set_position:
268
 * @query: a #GstQuery with query type GST_QUERY_POSITION
Stefan Kost's avatar
Stefan Kost committed
269
 * @format: the requested #GstFormat
270
 * @cur: the position to set
Stefan Kost's avatar
Stefan Kost committed
271
 *
272
 * Answer a position query by setting the requested value in the given format.
Stefan Kost's avatar
Stefan Kost committed
273
 */
274
void
Wim Taymans's avatar
Wim Taymans committed
275
gst_query_set_position (GstQuery * query, GstFormat format, gint64 cur)
276
{
277
  GstStructure *s;
278

279
280
  g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_POSITION);

281
282
283
284
285
  s = GST_QUERY_STRUCTURE (query);
  g_return_if_fail (format == g_value_get_enum (gst_structure_id_get_value (s,
              GST_QUARK (FORMAT))));

  gst_structure_id_set (s,
286
287
      GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
      GST_QUARK (CURRENT), G_TYPE_INT64, cur, NULL);
288
289
}

Stefan Kost's avatar
Stefan Kost committed
290
291
/**
 * gst_query_parse_position:
292
 * @query: a #GstQuery
293
294
295
 * @format: (out) (allow-none): the storage for the #GstFormat of the
 *     position values (may be NULL)
 * @cur: (out) (allow-none): the storage for the current position (may be NULL)
Stefan Kost's avatar
Stefan Kost committed
296
 *
297
298
 * Parse a position query, writing the format into @format, and the position
 * into @cur, if the respective parameters are non-NULL.
Stefan Kost's avatar
Stefan Kost committed
299
 */
300
void
Wim Taymans's avatar
Wim Taymans committed
301
gst_query_parse_position (GstQuery * query, GstFormat * format, gint64 * cur)
302
303
304
305
306
{
  GstStructure *structure;

  g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_POSITION);

307
  structure = GST_QUERY_STRUCTURE (query);
308
  if (format)
309
310
    *format =
        (GstFormat) g_value_get_enum (gst_structure_id_get_value (structure,
311
            GST_QUARK (FORMAT)));
312
  if (cur)
313
314
    *cur = g_value_get_int64 (gst_structure_id_get_value (structure,
            GST_QUARK (CURRENT)));
Wim Taymans's avatar
Wim Taymans committed
315
316
317
318
319
}


/**
 * gst_query_new_duration:
320
 * @format: the #GstFormat for this duration query
Wim Taymans's avatar
Wim Taymans committed
321
 *
Wim Taymans's avatar
Wim Taymans committed
322
 * Constructs a new stream duration query object to query in the given format.
323
324
 * Use gst_query_unref() when done with it. A duration query will give the
 * total length of the stream.
Wim Taymans's avatar
Wim Taymans committed
325
 *
326
327
328
 * Free-function: gst_query_unref
 *
 * Returns: (transfer full): a new #GstQuery
Wim Taymans's avatar
Wim Taymans committed
329
330
331
332
333
334
335
 */
GstQuery *
gst_query_new_duration (GstFormat format)
{
  GstQuery *query;
  GstStructure *structure;

Wim Taymans's avatar
Wim Taymans committed
336
  structure = gst_structure_new_id (GST_QUARK (QUERY_DURATION),
337
      GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
338
      GST_QUARK (DURATION), G_TYPE_INT64, G_GINT64_CONSTANT (-1), NULL);
339

Wim Taymans's avatar
Wim Taymans committed
340
  query = gst_query_new_custom (GST_QUERY_DURATION, structure);
Wim Taymans's avatar
Wim Taymans committed
341
342
343
344
345
346

  return query;
}

/**
 * gst_query_set_duration:
347
348
349
 * @query: a #GstQuery
 * @format: the #GstFormat for the duration
 * @duration: the duration of the stream
Wim Taymans's avatar
Wim Taymans committed
350
 *
351
 * Answer a duration query by setting the requested value in the given format.
Wim Taymans's avatar
Wim Taymans committed
352
353
354
355
 */
void
gst_query_set_duration (GstQuery * query, GstFormat format, gint64 duration)
{
356
  GstStructure *s;
357

Wim Taymans's avatar
Wim Taymans committed
358
359
  g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_DURATION);

360
361
362
363
  s = GST_QUERY_STRUCTURE (query);
  g_return_if_fail (format == g_value_get_enum (gst_structure_id_get_value (s,
              GST_QUARK (FORMAT))));
  gst_structure_id_set (s, GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
364
      GST_QUARK (DURATION), G_TYPE_INT64, duration, NULL);
Wim Taymans's avatar
Wim Taymans committed
365
366
367
368
}

/**
 * gst_query_parse_duration:
369
 * @query: a #GstQuery
370
371
372
 * @format: (out) (allow-none): the storage for the #GstFormat of the duration
 *     value, or NULL.
 * @duration: (out) (allow-none): the storage for the total duration, or NULL.
Wim Taymans's avatar
Wim Taymans committed
373
 *
374
375
 * Parse a duration query answer. Write the format of the duration into @format,
 * and the value into @duration, if the respective variables are non-NULL.
Wim Taymans's avatar
Wim Taymans committed
376
377
378
379
380
381
382
383
384
 */
void
gst_query_parse_duration (GstQuery * query, GstFormat * format,
    gint64 * duration)
{
  GstStructure *structure;

  g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_DURATION);

385
  structure = GST_QUERY_STRUCTURE (query);
Wim Taymans's avatar
Wim Taymans committed
386
  if (format)
387
388
    *format =
        (GstFormat) g_value_get_enum (gst_structure_id_get_value (structure,
389
            GST_QUARK (FORMAT)));
Wim Taymans's avatar
Wim Taymans committed
390
  if (duration)
391
392
    *duration = g_value_get_int64 (gst_structure_id_get_value (structure,
            GST_QUARK (DURATION)));
393
394
}

395
396
397
/**
 * gst_query_new_latency:
 *
Wim Taymans's avatar
Wim Taymans committed
398
 * Constructs a new latency query object.
399
400
401
402
 * Use gst_query_unref() when done with it. A latency query is usually performed
 * by sinks to compensate for additional latency introduced by elements in the
 * pipeline.
 *
403
404
405
 * Free-function: gst_query_unref
 *
 * Returns: (transfer full): a #GstQuery
406
407
408
409
410
411
412
413
414
 *
 * Since: 0.10.12
 */
GstQuery *
gst_query_new_latency (void)
{
  GstQuery *query;
  GstStructure *structure;

Wim Taymans's avatar
Wim Taymans committed
415
  structure = gst_structure_new_id (GST_QUARK (QUERY_LATENCY),
416
      GST_QUARK (LIVE), G_TYPE_BOOLEAN, FALSE,
417
418
      GST_QUARK (MIN_LATENCY), G_TYPE_UINT64, G_GUINT64_CONSTANT (0),
      GST_QUARK (MAX_LATENCY), G_TYPE_UINT64, G_GUINT64_CONSTANT (-1), NULL);
419

Wim Taymans's avatar
Wim Taymans committed
420
  query = gst_query_new_custom (GST_QUERY_LATENCY, structure);
421
422
423
424
425
426
427
428

  return query;
}

/**
 * gst_query_set_latency:
 * @query: a #GstQuery
 * @live: if there is a live element upstream
Wim Taymans's avatar
Wim Taymans committed
429
430
 * @min_latency: the minimal latency of the upstream elements
 * @max_latency: the maximal latency of the upstream elements
431
432
433
434
435
436
437
438
439
 *
 * Answer a latency query by setting the requested values in the given format.
 *
 * Since: 0.10.12
 */
void
gst_query_set_latency (GstQuery * query, gboolean live,
    GstClockTime min_latency, GstClockTime max_latency)
{
440
441
  GstStructure *structure;

442
443
  g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_LATENCY);

444
445
  structure = GST_QUERY_STRUCTURE (query);
  gst_structure_id_set (structure,
446
447
448
      GST_QUARK (LIVE), G_TYPE_BOOLEAN, live,
      GST_QUARK (MIN_LATENCY), G_TYPE_UINT64, min_latency,
      GST_QUARK (MAX_LATENCY), G_TYPE_UINT64, max_latency, NULL);
449
450
451
452
453
}

/**
 * gst_query_parse_latency:
 * @query: a #GstQuery
454
455
456
 * @live: (out) (allow-none): storage for live or NULL
 * @min_latency: (out) (allow-none): the storage for the min latency or NULL
 * @max_latency: (out) (allow-none): the storage for the max latency or NULL
457
 *
Wim Taymans's avatar
Wim Taymans committed
458
 * Parse a latency query answer.
459
460
461
462
463
464
465
466
467
468
469
 *
 * Since: 0.10.12
 */
void
gst_query_parse_latency (GstQuery * query, gboolean * live,
    GstClockTime * min_latency, GstClockTime * max_latency)
{
  GstStructure *structure;

  g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_LATENCY);

470
  structure = GST_QUERY_STRUCTURE (query);
471
  if (live)
472
473
474
    *live =
        g_value_get_boolean (gst_structure_id_get_value (structure,
            GST_QUARK (LIVE)));
475
  if (min_latency)
476
477
    *min_latency = g_value_get_uint64 (gst_structure_id_get_value (structure,
            GST_QUARK (MIN_LATENCY)));
478
  if (max_latency)
479
480
    *max_latency = g_value_get_uint64 (gst_structure_id_get_value (structure,
            GST_QUARK (MAX_LATENCY)));
481
482
}

483
484
/**
 * gst_query_new_convert:
485
 * @src_format: the source #GstFormat for the new query
486
 * @value: the value to convert
487
 * @dest_format: the target #GstFormat
488
 *
489
 * Constructs a new convert query object. Use gst_query_unref()
490
491
 * when done with it. A convert query is used to ask for a conversion between
 * one format and another.
492
 *
493
494
495
 * Free-function: gst_query_unref
 *
 * Returns: (transfer full): a #GstQuery
496
 */
497
GstQuery *
498
499
gst_query_new_convert (GstFormat src_format, gint64 value,
    GstFormat dest_format)
500
501
502
503
{
  GstQuery *query;
  GstStructure *structure;

Wim Taymans's avatar
Wim Taymans committed
504
  structure = gst_structure_new_id (GST_QUARK (QUERY_CONVERT),
505
506
507
      GST_QUARK (SRC_FORMAT), GST_TYPE_FORMAT, src_format,
      GST_QUARK (SRC_VALUE), G_TYPE_INT64, value,
      GST_QUARK (DEST_FORMAT), GST_TYPE_FORMAT, dest_format,
508
      GST_QUARK (DEST_VALUE), G_TYPE_INT64, G_GINT64_CONSTANT (-1), NULL);
509

Wim Taymans's avatar
Wim Taymans committed
510
  query = gst_query_new_custom (GST_QUERY_CONVERT, structure);
511
512
513
514

  return query;
}

515
516
/**
 * gst_query_set_convert:
517
 * @query: a #GstQuery
518
519
520
521
522
523
524
 * @src_format: the source #GstFormat
 * @src_value: the source value
 * @dest_format: the destination #GstFormat
 * @dest_value: the destination value
 *
 * Answer a convert query by setting the requested values.
 */
525
526
527
528
void
gst_query_set_convert (GstQuery * query, GstFormat src_format, gint64 src_value,
    GstFormat dest_format, gint64 dest_value)
{
529
530
  GstStructure *structure;

531
532
  g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_CONVERT);

533
534
  structure = GST_QUERY_STRUCTURE (query);
  gst_structure_id_set (structure,
535
536
537
      GST_QUARK (SRC_FORMAT), GST_TYPE_FORMAT, src_format,
      GST_QUARK (SRC_VALUE), G_TYPE_INT64, src_value,
      GST_QUARK (DEST_FORMAT), GST_TYPE_FORMAT, dest_format,
538
      GST_QUARK (DEST_VALUE), G_TYPE_INT64, dest_value, NULL);
539
540
}

541
542
/**
 * gst_query_parse_convert:
543
 * @query: a #GstQuery
544
545
546
547
548
549
550
 * @src_format: (out) (allow-none): the storage for the #GstFormat of the
 *     source value, or NULL
 * @src_value: (out) (allow-none): the storage for the source value, or NULL
 * @dest_format: (out) (allow-none): the storage for the #GstFormat of the
 *     destination value, or NULL
 * @dest_value: (out) (allow-none): the storage for the destination value,
 *     or NULL
551
552
553
 *
 * Parse a convert query answer. Any of @src_format, @src_value, @dest_format,
 * and @dest_value may be NULL, in which case that value is omitted.
554
 */
555
556
557
558
559
560
561
562
void
gst_query_parse_convert (GstQuery * query, GstFormat * src_format,
    gint64 * src_value, GstFormat * dest_format, gint64 * dest_value)
{
  GstStructure *structure;

  g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_CONVERT);

563
  structure = GST_QUERY_STRUCTURE (query);
564
  if (src_format)
565
566
    *src_format =
        (GstFormat) g_value_get_enum (gst_structure_id_get_value (structure,
567
            GST_QUARK (SRC_FORMAT)));
568
  if (src_value)
569
570
    *src_value = g_value_get_int64 (gst_structure_id_get_value (structure,
            GST_QUARK (SRC_VALUE)));
571
  if (dest_format)
572
573
    *dest_format =
        (GstFormat) g_value_get_enum (gst_structure_id_get_value (structure,
574
            GST_QUARK (DEST_FORMAT)));
575
  if (dest_value)
576
577
    *dest_value = g_value_get_int64 (gst_structure_id_get_value (structure,
            GST_QUARK (DEST_VALUE)));
578
579
}

580
581
582
583
/**
 * gst_query_new_segment:
 * @format: the #GstFormat for the new query
 *
584
 * Constructs a new segment query object. Use gst_query_unref()
585
586
 * when done with it. A segment query is used to discover information about the
 * currently configured segment for playback.
587
 *
588
589
590
 * Free-function: gst_query_unref
 *
 * Returns: (transfer full): a new #GstQuery
591
592
593
594
595
596
597
 */
GstQuery *
gst_query_new_segment (GstFormat format)
{
  GstQuery *query;
  GstStructure *structure;

Wim Taymans's avatar
Wim Taymans committed
598
  structure = gst_structure_new_id (GST_QUARK (QUERY_SEGMENT),
599
600
      GST_QUARK (RATE), G_TYPE_DOUBLE, (gdouble) 0.0,
      GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
601
602
      GST_QUARK (START_VALUE), G_TYPE_INT64, G_GINT64_CONSTANT (-1),
      GST_QUARK (STOP_VALUE), G_TYPE_INT64, G_GINT64_CONSTANT (-1), NULL);
603

Wim Taymans's avatar
Wim Taymans committed
604
  query = gst_query_new_custom (GST_QUERY_SEGMENT, structure);
605

606
607
608
609
610
  return query;
}

/**
 * gst_query_set_segment:
611
 * @query: a #GstQuery
612
 * @rate: the rate of the segment
613
 * @format: the #GstFormat of the segment values (@start_value and @stop_value)
614
615
616
 * @start_value: the start value
 * @stop_value: the stop value
 *
617
618
619
620
621
 * Answer a segment query by setting the requested values. The normal
 * playback segment of a pipeline is 0 to duration at the default rate of
 * 1.0. If a seek was performed on the pipeline to play a different
 * segment, this query will return the range specified in the last seek.
 *
Wim Taymans's avatar
Wim Taymans committed
622
623
624
 * @start_value and @stop_value will respectively contain the configured
 * playback range start and stop values expressed in @format.
 * The values are always between 0 and the duration of the media and
625
626
627
 * @start_value <= @stop_value. @rate will contain the playback rate. For
 * negative rates, playback will actually happen from @stop_value to
 * @start_value.
628
629
630
 */
void
gst_query_set_segment (GstQuery * query, gdouble rate, GstFormat format,
Wim Taymans's avatar
Wim Taymans committed
631
    gint64 start_value, gint64 stop_value)
632
{
633
634
  GstStructure *structure;

635
636
  g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_SEGMENT);

637
638
  structure = GST_QUERY_STRUCTURE (query);
  gst_structure_id_set (structure,
639
640
641
642
      GST_QUARK (RATE), G_TYPE_DOUBLE, rate,
      GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
      GST_QUARK (START_VALUE), G_TYPE_INT64, start_value,
      GST_QUARK (STOP_VALUE), G_TYPE_INT64, stop_value, NULL);
643
644
645
646
}

/**
 * gst_query_parse_segment:
647
 * @query: a #GstQuery
648
649
650
651
652
 * @rate: (out) (allow-none): the storage for the rate of the segment, or NULL
 * @format: (out) (allow-none): the storage for the #GstFormat of the values,
 *     or NULL
 * @start_value: (out) (allow-none): the storage for the start value, or NULL
 * @stop_value: (out) (allow-none): the storage for the stop value, or NULL
653
 *
Wim Taymans's avatar
Wim Taymans committed
654
 * Parse a segment query answer. Any of @rate, @format, @start_value, and
655
 * @stop_value may be NULL, which will cause this value to be omitted.
656
657
 *
 * See gst_query_set_segment() for an explanation of the function arguments.
658
659
660
 */
void
gst_query_parse_segment (GstQuery * query, gdouble * rate, GstFormat * format,
Wim Taymans's avatar
Wim Taymans committed
661
    gint64 * start_value, gint64 * stop_value)
662
663
664
665
666
{
  GstStructure *structure;

  g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_SEGMENT);

667
  structure = GST_QUERY_STRUCTURE (query);
668
  if (rate)
669
670
    *rate = g_value_get_double (gst_structure_id_get_value (structure,
            GST_QUARK (RATE)));
671
  if (format)
672
673
    *format =
        (GstFormat) g_value_get_enum (gst_structure_id_get_value (structure,
674
            GST_QUARK (FORMAT)));
675
  if (start_value)
676
677
    *start_value = g_value_get_int64 (gst_structure_id_get_value (structure,
            GST_QUARK (START_VALUE)));
678
  if (stop_value)
679
680
    *stop_value = g_value_get_int64 (gst_structure_id_get_value (structure,
            GST_QUARK (STOP_VALUE)));
681
682
683
}

/**
684
 * gst_query_new_custom:
685
686
687
 * @type: the query type
 * @structure: a structure for the query
 *
688
 * Constructs a new custom query object. Use gst_query_unref()
689
690
 * when done with it.
 *
691
692
693
 * Free-function: gst_query_unref
 *
 * Returns: (transfer full): a new #GstQuery
694
 */
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
695
GstQuery *
696
gst_query_new_custom (GstQueryType type, GstStructure * structure)
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
697
{
Wim Taymans's avatar
Wim Taymans committed
698
699
700
  GstQueryImpl *query;

  query = g_slice_new0 (GstQueryImpl);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
701

Wim Taymans's avatar
Wim Taymans committed
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
  GST_DEBUG ("creating new query %p %s", query, gst_query_type_get_name (type));

  if (structure) {
    /* structure must not have a parent */
    if (!gst_structure_set_parent_refcount (structure,
            &query->query.mini_object.refcount))
      goto had_parent;
  }
  gst_query_init (query, sizeof (GstQueryImpl), type);

  GST_QUERY_STRUCTURE (query) = structure;

  return GST_QUERY_CAST (query);

  /* ERRORS */
had_parent:
  {
    g_slice_free1 (GST_MINI_OBJECT_SIZE (query), query);
    g_warning ("structure is already owned by another object");
    return NULL;
  }
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
723
724
}

725
726
/**
 * gst_query_get_structure:
727
 * @query: a #GstQuery
728
729
730
 *
 * Get the structure of a query.
 *
731
732
733
 * Returns: (transfer none): the #GstStructure of the query. The structure is
 *     still owned by the query and will therefore be freed when the query
 *     is unreffed.
734
 */
735
const GstStructure *
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
736
737
738
739
gst_query_get_structure (GstQuery * query)
{
  g_return_val_if_fail (GST_IS_QUERY (query), NULL);

740
  return GST_QUERY_STRUCTURE (query);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
741
}
742

743
744
745
746
/**
 * gst_query_writable_structure:
 * @query: a #GstQuery
 *
747
748
 * Get the structure of a query. This method should be called with a writable
 * @query so that the returned structure is guranteed to be writable.
749
750
751
752
753
754
755
756
757
758
759
760
761
762
 *
 * Returns: (transfer none): the #GstStructure of the query. The structure is
 *     still owned by the query and will therefore be freed when the query
 *     is unreffed.
 */
GstStructure *
gst_query_writable_structure (GstQuery * query)
{
  g_return_val_if_fail (GST_IS_QUERY (query), NULL);
  g_return_val_if_fail (gst_query_is_writable (query), NULL);

  return GST_QUERY_STRUCTURE (query);
}

763
/**
Stefan Kost's avatar
Stefan Kost committed
764
 * gst_query_new_seeking:
765
766
767
 * @format: the default #GstFormat for the new query
 *
 * Constructs a new query object for querying seeking properties of
Wim Taymans's avatar
Wim Taymans committed
768
 * the stream.
769
 *
770
771
772
 * Free-function: gst_query_unref
 *
 * Returns: (transfer full): a new #GstQuery
773
774
775
776
777
778
779
 */
GstQuery *
gst_query_new_seeking (GstFormat format)
{
  GstQuery *query;
  GstStructure *structure;

Wim Taymans's avatar
Wim Taymans committed
780
  structure = gst_structure_new_id (GST_QUARK (QUERY_SEEKING),
781
782
      GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
      GST_QUARK (SEEKABLE), G_TYPE_BOOLEAN, FALSE,
783
784
      GST_QUARK (SEGMENT_START), G_TYPE_INT64, G_GINT64_CONSTANT (-1),
      GST_QUARK (SEGMENT_END), G_TYPE_INT64, G_GINT64_CONSTANT (-1), NULL);
785

Wim Taymans's avatar
Wim Taymans committed
786
  query = gst_query_new_custom (GST_QUERY_SEEKING, structure);
787
788
789
790

  return query;
}

Wim Taymans's avatar
Wim Taymans committed
791
792
/**
 * gst_query_set_seeking:
793
794
 * @query: a #GstQuery
 * @format: the format to set for the @segment_start and @segment_end values
Wim Taymans's avatar
Wim Taymans committed
795
796
797
798
799
800
 * @seekable: the seekable flag to set
 * @segment_start: the segment_start to set
 * @segment_end: the segment_end to set
 *
 * Set the seeking query result fields in @query.
 */
801
802
803
804
void
gst_query_set_seeking (GstQuery * query, GstFormat format,
    gboolean seekable, gint64 segment_start, gint64 segment_end)
{
805
806
  GstStructure *structure;

807
  g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_SEEKING);
808
  g_return_if_fail (gst_query_is_writable (query));
809

810
811
  structure = GST_QUERY_STRUCTURE (query);
  gst_structure_id_set (structure,
812
813
814
815
      GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
      GST_QUARK (SEEKABLE), G_TYPE_BOOLEAN, seekable,
      GST_QUARK (SEGMENT_START), G_TYPE_INT64, segment_start,
      GST_QUARK (SEGMENT_END), G_TYPE_INT64, segment_end, NULL);
816
817
}

818
819
820
/**
 * gst_query_parse_seeking:
 * @query: a GST_QUERY_SEEKING type query #GstQuery
821
822
823
824
825
 * @format: (out) (allow-none): the format to set for the @segment_start
 *     and @segment_end values, or NULL
 * @seekable: (out) (allow-none): the seekable flag to set, or NULL
 * @segment_start: (out) (allow-none): the segment_start to set, or NULL
 * @segment_end: (out) (allow-none): the segment_end to set, or NULL
826
 *
Wim Taymans's avatar
Wim Taymans committed
827
 * Parse a seeking query, writing the format into @format, and
828
829
830
831
832
833
834
835
836
837
838
 * other results into the passed parameters, if the respective parameters
 * are non-NULL
 */
void
gst_query_parse_seeking (GstQuery * query, GstFormat * format,
    gboolean * seekable, gint64 * segment_start, gint64 * segment_end)
{
  GstStructure *structure;

  g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_SEEKING);

839
  structure = GST_QUERY_STRUCTURE (query);
840
  if (format)
841
842
    *format =
        (GstFormat) g_value_get_enum (gst_structure_id_get_value (structure,
843
            GST_QUARK (FORMAT)));
844
  if (seekable)
845
846
    *seekable = g_value_get_boolean (gst_structure_id_get_value (structure,
            GST_QUARK (SEEKABLE)));
847
  if (segment_start)
848
849
    *segment_start = g_value_get_int64 (gst_structure_id_get_value (structure,
            GST_QUARK (SEGMENT_START)));
850
  if (segment_end)
851
852
    *segment_end = g_value_get_int64 (gst_structure_id_get_value (structure,
            GST_QUARK (SEGMENT_END)));
853
854
}

855
static GArray *
Wim Taymans's avatar
Wim Taymans committed
856
857
ensure_array (GstStructure * s, GQuark quark, gsize element_size,
    GDestroyNotify clear_func)
Wim Taymans's avatar
Wim Taymans committed
858
{
859
  GArray *array;
Wim Taymans's avatar
Wim Taymans committed
860
861
862
863
  const GValue *value;

  value = gst_structure_id_get_value (s, quark);
  if (value) {
864
    array = (GArray *) g_value_get_boxed (value);
Wim Taymans's avatar
Wim Taymans committed
865
866
867
  } else {
    GValue new_array_val = { 0, };

868
    array = g_array_new (FALSE, TRUE, element_size);
Wim Taymans's avatar
Wim Taymans committed
869
870
    if (clear_func)
      g_array_set_clear_func (array, clear_func);
Wim Taymans's avatar
Wim Taymans committed
871

872
    g_value_init (&new_array_val, G_TYPE_ARRAY);
Wim Taymans's avatar
Wim Taymans committed
873
874
875
876
877
878
879
    g_value_take_boxed (&new_array_val, array);

    gst_structure_id_take_value (s, quark, &new_array_val);
  }
  return array;
}

880
881
882
883
/**
 * gst_query_new_formats:
 *
 * Constructs a new query object for querying formats of
Wim Taymans's avatar
Wim Taymans committed
884
 * the stream.
885
 *
886
887
888
 * Free-function: gst_query_unref
 *
 * Returns: (transfer full): a new #GstQuery
889
890
891
892
893
894
895
896
897
 *
 * Since: 0.10.4
 */
GstQuery *
gst_query_new_formats (void)
{
  GstQuery *query;
  GstStructure *structure;

Wim Taymans's avatar
Wim Taymans committed
898
  structure = gst_structure_new_id_empty (GST_QUARK (QUERY_FORMATS));
Wim Taymans's avatar
Wim Taymans committed
899
  query = gst_query_new_custom (GST_QUERY_FORMATS, structure);
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914

  return query;
}

static void
gst_query_list_add_format (GValue * list, GstFormat format)
{
  GValue item = { 0, };

  g_value_init (&item, GST_TYPE_FORMAT);
  g_value_set_enum (&item, format);
  gst_value_list_append_value (list, &item);
  g_value_unset (&item);
}

Wim Taymans's avatar
Wim Taymans committed
915
916
/**
 * gst_query_set_formats:
917
918
919
 * @query: a #GstQuery
 * @n_formats: the number of formats to set.
 * @...: A number of @GstFormats equal to @n_formats.
Wim Taymans's avatar
Wim Taymans committed
920
 *
921
922
 * Set the formats query result fields in @query. The number of formats passed
 * must be equal to @n_formats.
Wim Taymans's avatar
Wim Taymans committed
923
 */
924
925
926
927
928
929
void
gst_query_set_formats (GstQuery * query, gint n_formats, ...)
{
  va_list ap;
  GValue list = { 0, };
  gint i;
930
  GstStructure *structure;
931

932
  g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_FORMATS);
933
  g_return_if_fail (gst_query_is_writable (query));
934

935
936
937
938
  g_value_init (&list, GST_TYPE_LIST);

  va_start (ap, n_formats);
  for (i = 0; i < n_formats; i++) {
939
    gst_query_list_add_format (&list, va_arg (ap, GstFormat));
940
941
942
  }
  va_end (ap);

943
944
  structure = GST_QUERY_STRUCTURE (query);
  gst_structure_set_value (structure, "formats", &list);
945
946
947

  g_value_unset (&list);

948
}
949
950
951
952
953

/**
 * gst_query_set_formatsv:
 * @query: a #GstQuery
 * @n_formats: the number of formats to set.
954
955
 * @formats: (in) (array length=n_formats): an array containing @n_formats
 *     @GstFormat values.
956
957
958
959
960
961
962
 *
 * Set the formats query result fields in @query. The number of formats passed
 * in the @formats array must be equal to @n_formats.
 *
 * Since: 0.10.4
 */
void
963
964
gst_query_set_formatsv (GstQuery * query, gint n_formats,
    const GstFormat * formats)
965
966
967
{
  GValue list = { 0, };
  gint i;
968
  GstStructure *structure;
969
970

  g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_FORMATS);
971
  g_return_if_fail (gst_query_is_writable (query));
972
973
974
975
976

  g_value_init (&list, GST_TYPE_LIST);
  for (i = 0; i < n_formats; i++) {
    gst_query_list_add_format (&list, formats[i]);
  }
977
978
  structure = GST_QUERY_STRUCTURE (query);
  gst_structure_set_value (structure, "formats", &list);
979
980

  g_value_unset (&list);
981
982
983
}

/**
984
 * gst_query_parse_n_formats:
985
 * @query: a #GstQuery
Wim Taymans's avatar
Wim Taymans committed
986
 * @n_formats: (out) (allow-none): the number of formats in this query.
987
 *
Wim Taymans's avatar
Wim Taymans committed
988
 * Parse the number of formats in the formats @query.
989
990
991
992
 *
 * Since: 0.10.4
 */
void
993
gst_query_parse_n_formats (GstQuery * query, guint * n_formats)
994
{
995
996
  GstStructure *structure;

997
998
999
1000
1001
  g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_FORMATS);

  if (n_formats) {
    const GValue *list;

1002
1003
    structure = GST_QUERY_STRUCTURE (query);
    list = gst_structure_get_value (structure, "formats");
1004
1005
1006
1007
1008
1009
1010
1011
    if (list == NULL)
      *n_formats = 0;
    else
      *n_formats = gst_value_list_get_size (list);
  }
}

/**
1012
 * gst_query_parse_nth_format:
1013
 * @query: a #GstQuery
Johan Dahlin's avatar
Johan Dahlin committed
1014
 * @nth: (out): the nth format to retrieve.
Wim Taymans's avatar
Wim Taymans committed
1015
 * @format: (out) (allow-none): a pointer to store the nth format
1016
 *
Wim Taymans's avatar
Wim Taymans committed
1017
 * Parse the format query and retrieve the @nth format from it into
1018
1019
1020
1021
 * @format. If the list contains less elements than @nth, @format will be
 * set to GST_FORMAT_UNDEFINED.
 */
void
1022
gst_query_parse_nth_format (GstQuery * query, guint nth, GstFormat * format)
1023
{
1024
1025
  GstStructure *structure;

1026
1027
1028
1029
1030
  g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_FORMATS);

  if (format) {
    const GValue *list;