Coverage Report

Created: 2026-05-28 06:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gstreamer/subprojects/gst-plugins-base/gst-libs/gst/pbutils/gstdiscoverer.c
Line
Count
Source
1
/* GStreamer
2
 * Copyright (C) 2009 Edward Hervey <edward.hervey@collabora.co.uk>
3
 *               2009 Nokia Corporation
4
 *
5
 * This library is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Library General Public
7
 * License as published by the Free Software Foundation; either
8
 * version 2 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
 * Library General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Library General Public
16
 * License along with this library; if not, write to the
17
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18
 * Boston, MA 02110-1301, USA.
19
 */
20
21
/**
22
 * SECTION:gstdiscoverer
23
 * @title: GstDiscoverer
24
 * @short_description: Utility for discovering information on URIs.
25
 *
26
 * The #GstDiscoverer is a utility object which allows to get as much
27
 * information as possible from one or many URIs.
28
 *
29
 * It provides two APIs, allowing usage in blocking or non-blocking mode.
30
 *
31
 * The blocking mode just requires calling gst_discoverer_discover_uri()
32
 * with the URI one wishes to discover.
33
 *
34
 * The non-blocking mode requires a running #GMainLoop iterating a
35
 * #GMainContext, where one connects to the various signals, appends the
36
 * URIs to be processed (through gst_discoverer_discover_uri_async()) and then
37
 * asks for the discovery to begin (through gst_discoverer_start()).
38
 * By default this will use the GLib default main context unless you have
39
 * set a custom context using g_main_context_push_thread_default().
40
 *
41
 * All the information is returned in a #GstDiscovererInfo structure.
42
 */
43
44
#ifdef HAVE_CONFIG_H
45
#include "config.h"
46
#endif
47
48
#include <gst/video/video.h>
49
#include <gst/audio/audio.h>
50
51
#include <string.h>
52
53
#include "pbutils.h"
54
#include "pbutils-private.h"
55
56
/* For g_stat () */
57
#include <glib/gstdio.h>
58
59
/* *INDENT-OFF* */
60
GST_DEBUG_CATEGORY_STATIC (discoverer_debug);
61
1
#define GST_CAT_DEFAULT discoverer_debug
62
63
86
GST_LOG_CONTEXT_STATIC_DEFINE (warning_message_log_ctx, GST_LOG_CONTEXT_FLAG_THROTTLE,
64
    GST_LOG_CONTEXT_BUILDER_SET_HASH_FLAGS(GST_LOG_CONTEXT_USE_STRING_ARGS);
65
);
66
#define WARNING_MESSAGE_LOG_CTX GST_LOG_CONTEXT_LAZY_INIT (warning_message_log_ctx)
67
/* *INDENT-ON* */
68
69
0
#define CACHE_DIRNAME "discoverer"
70
71
typedef struct
72
{
73
  GstDiscoverer *dc;
74
  GstPad *pad;
75
  GstElement *queue;
76
  GstElement *sink;
77
  GstTagList *tags;
78
  GstToc *toc;
79
  gchar *stream_id;
80
  gulong probe_id;
81
} PrivateStream;
82
83
struct _GstDiscovererPrivate
84
{
85
  gboolean async;
86
87
  /* allowed time to discover each uri in nanoseconds */
88
  GstClockTime timeout;
89
90
  /* list of pending URI to process (current excluded) */
91
  GList *pending_uris;
92
93
  GMutex lock;
94
  /* TRUE if cleaning up discoverer */
95
  gboolean cleanup;
96
97
  /* TRUE if processing a URI */
98
  gboolean processing;
99
100
  /* TRUE if discoverer has been started */
101
  gboolean running;
102
103
  /* current items */
104
  GstDiscovererInfo *current_info;
105
  gint current_info_stream_count;
106
  GError *current_error;
107
  GstStructure *current_topology;
108
  gchar *current_cachefile;
109
  gboolean current_info_from_cache;
110
111
  GstTagList *all_tags;
112
  GstTagList *global_tags;
113
114
  /* List of private streams */
115
  GList *streams;
116
117
  /* List of these sinks and their handler IDs (to remove the probe) */
118
  guint pending_subtitle_pads;
119
120
  /* Whether we received no_more_pads */
121
  gboolean no_more_pads;
122
123
  GstState target_state;
124
  GstState current_state;
125
126
  /* Global elements */
127
  GstBin *pipeline;
128
  GstElement *uridecodebin;
129
  GstBus *bus;
130
131
  /* Custom main context variables */
132
  GMainContext *ctx;
133
  GSource *bus_source;
134
  GSource *timeout_source;
135
136
  /* reusable queries */
137
  GstQuery *seeking_query;
138
139
  /* Handler ids for various callbacks */
140
  gulong pad_added_id;
141
  gulong pad_remove_id;
142
  gulong no_more_pads_id;
143
  gulong source_chg_id;
144
  gulong bus_cb_id;
145
146
  gboolean use_cache;
147
};
148
149
54.8k
#define DISCO_LOCK(dc) g_mutex_lock (&dc->priv->lock);
150
5.19k
#define DISCO_UNLOCK(dc) g_mutex_unlock (&dc->priv->lock);
151
152
static void
153
_do_init (void)
154
1
{
155
1
  GST_DEBUG_CATEGORY_INIT (discoverer_debug, "discoverer", 0, "Discoverer");
156
1
};
157
158
15.5k
G_DEFINE_TYPE_EXTENDED (GstDiscoverer, gst_discoverer, G_TYPE_OBJECT, 0,
159
15.5k
    G_ADD_PRIVATE (GstDiscoverer) _do_init ());
160
15.5k
161
15.5k
enum
162
15.5k
{
163
15.5k
  SIGNAL_FINISHED,
164
15.5k
  SIGNAL_STARTING,
165
15.5k
  SIGNAL_DISCOVERED,
166
15.5k
  SIGNAL_SOURCE_SETUP,
167
15.5k
  SIGNAL_LOAD_SERIALIZED_INFO,
168
15.5k
  LAST_SIGNAL
169
15.5k
};
170
15.5k
171
15.5k
#define DEFAULT_PROP_TIMEOUT 15 * GST_SECOND
172
5.19k
#define DEFAULT_PROP_USE_CACHE FALSE
173
174
enum
175
{
176
  PROP_0,
177
  PROP_TIMEOUT,
178
  PROP_USE_CACHE
179
};
180
181
static guint gst_discoverer_signals[LAST_SIGNAL] = { 0 };
182
183
static void gst_discoverer_set_timeout (GstDiscoverer * dc,
184
    GstClockTime timeout);
185
static gboolean async_timeout_cb (GstDiscoverer * dc);
186
187
static void discoverer_bus_cb (GstBus * bus, GstMessage * msg,
188
    GstDiscoverer * dc);
189
static void uridecodebin_pad_added_cb (GstElement * uridecodebin, GstPad * pad,
190
    GstDiscoverer * dc);
191
static void uridecodebin_pad_removed_cb (GstElement * uridecodebin,
192
    GstPad * pad, GstDiscoverer * dc);
193
static void uridecodebin_no_more_pads_cb (GstElement * uridecodebin,
194
    GstDiscoverer * dc);
195
static void uridecodebin_source_changed_cb (GstElement * uridecodebin,
196
    GParamSpec * pspec, GstDiscoverer * dc);
197
198
static void gst_discoverer_dispose (GObject * dc);
199
static void gst_discoverer_finalize (GObject * dc);
200
static void gst_discoverer_set_property (GObject * object, guint prop_id,
201
    const GValue * value, GParamSpec * pspec);
202
static void gst_discoverer_get_property (GObject * object, guint prop_id,
203
    GValue * value, GParamSpec * pspec);
204
static gboolean _setup_locked (GstDiscoverer * dc);
205
static void handle_current_async (GstDiscoverer * dc);
206
static gboolean emit_discovered_and_next (GstDiscoverer * dc);
207
static GVariant *gst_discoverer_info_to_variant_recurse (GstDiscovererStreamInfo
208
    * sinfo, GstDiscovererSerializeFlags flags);
209
static GstDiscovererStreamInfo *_parse_discovery (GVariant * variant,
210
    GstDiscovererInfo * info);
211
static GstDiscovererInfo *load_serialized_info (GstDiscoverer * dc,
212
    gchar * uri);
213
214
static gboolean
215
_gst_discoverer_info_accumulator (GSignalInvocationHint * ihint,
216
    GValue * return_accu, const GValue * handler_return, gpointer dummy)
217
5.19k
{
218
5.19k
  GstDiscovererInfo *info;
219
220
5.19k
  info = g_value_get_object (handler_return);
221
5.19k
  GST_DEBUG ("got discoverer info %" GST_PTR_FORMAT, info);
222
223
5.19k
  g_value_set_object (return_accu, info);
224
225
  /* stop emission if we have a discoverer info */
226
5.19k
  return (info == NULL);
227
5.19k
}
228
229
static void
230
gst_discoverer_class_init (GstDiscovererClass * klass)
231
1
{
232
1
  GObjectClass *gobject_class = (GObjectClass *) klass;
233
234
1
  gobject_class->dispose = gst_discoverer_dispose;
235
1
  gobject_class->finalize = gst_discoverer_finalize;
236
237
1
  gobject_class->set_property = gst_discoverer_set_property;
238
1
  gobject_class->get_property = gst_discoverer_get_property;
239
240
1
  klass->load_serialize_info = load_serialized_info;
241
242
243
  /* properties */
244
  /**
245
   * GstDiscoverer:timeout:
246
   *
247
   * The duration (in nanoseconds) after which the discovery of an individual
248
   * URI will timeout.
249
   *
250
   * If the discovery of a URI times out, the %GST_DISCOVERER_TIMEOUT will be
251
   * set on the result flags.
252
   */
253
1
  g_object_class_install_property (gobject_class, PROP_TIMEOUT,
254
1
      g_param_spec_uint64 ("timeout", "timeout", "Timeout",
255
1
          GST_SECOND, 3600 * GST_SECOND, DEFAULT_PROP_TIMEOUT,
256
1
          G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
257
258
  /**
259
   * GstDiscoverer::use-cache:
260
   *
261
   * Whether to use a serialized version of the discoverer info from our
262
   * own cache if accessible. This allows the discovery to be much faster
263
   * as when using this option, we do not need to create a #GstPipeline
264
   * and run it, but instead, just reload the #GstDiscovererInfo in its
265
   * serialized form.
266
   *
267
   * The cache files are saved in `$XDG_CACHE_DIR/gstreamer-1.0/discoverer/`.
268
   *
269
   * Since: 1.16
270
   */
271
1
  g_object_class_install_property (gobject_class, PROP_USE_CACHE,
272
1
      g_param_spec_boolean ("use-cache", "use cache", "Use cache",
273
1
          DEFAULT_PROP_USE_CACHE,
274
1
          G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
275
276
  /* signals */
277
  /**
278
   * GstDiscoverer::finished:
279
   * @discoverer: the #GstDiscoverer
280
   *
281
   * Will be emitted in async mode when all pending URIs have been processed.
282
   */
283
1
  gst_discoverer_signals[SIGNAL_FINISHED] =
284
1
      g_signal_new ("finished", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
285
1
      G_STRUCT_OFFSET (GstDiscovererClass, finished), NULL, NULL, NULL,
286
1
      G_TYPE_NONE, 0, G_TYPE_NONE);
287
288
  /**
289
   * GstDiscoverer::starting:
290
   * @discoverer: the #GstDiscoverer
291
   *
292
   * Will be emitted when the discover starts analyzing the pending URIs
293
   */
294
1
  gst_discoverer_signals[SIGNAL_STARTING] =
295
1
      g_signal_new ("starting", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
296
1
      G_STRUCT_OFFSET (GstDiscovererClass, starting), NULL, NULL, NULL,
297
1
      G_TYPE_NONE, 0, G_TYPE_NONE);
298
299
  /**
300
   * GstDiscoverer::discovered:
301
   * @discoverer: the #GstDiscoverer
302
   * @info: the results #GstDiscovererInfo
303
   * @error: (allow-none) (type GLib.Error): #GError, which will be non-NULL
304
   *                                         if an error occurred during
305
   *                                         discovery. You must not free
306
   *                                         this #GError, it will be freed by
307
   *                                         the discoverer.
308
   *
309
   * Will be emitted in async mode when all information on a URI could be
310
   * discovered, or an error occurred.
311
   *
312
   * When an error occurs, @info might still contain some partial information,
313
   * depending on the circumstances of the error.
314
   */
315
1
  gst_discoverer_signals[SIGNAL_DISCOVERED] =
316
1
      g_signal_new ("discovered", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
317
1
      G_STRUCT_OFFSET (GstDiscovererClass, discovered), NULL, NULL, NULL,
318
1
      G_TYPE_NONE, 2, GST_TYPE_DISCOVERER_INFO,
319
1
      G_TYPE_ERROR | G_SIGNAL_TYPE_STATIC_SCOPE);
320
321
  /**
322
   * GstDiscoverer::source-setup:
323
   * @discoverer: the #GstDiscoverer
324
   * @source: source element
325
   *
326
   * This signal is emitted after the source element has been created for, so
327
   * the URI being discovered, so it can be configured by setting additional
328
   * properties (e.g. set a proxy server for an http source, or set the device
329
   * and read speed for an audio cd source).
330
   *
331
   * This signal is usually emitted from the context of a GStreamer streaming
332
   * thread.
333
   */
334
1
  gst_discoverer_signals[SIGNAL_SOURCE_SETUP] =
335
1
      g_signal_new ("source-setup", G_TYPE_FROM_CLASS (klass),
336
1
      G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDiscovererClass, source_setup),
337
1
      NULL, NULL, NULL, G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
338
339
  /**
340
   * GstDiscoverer::load-serialized-info:
341
   * @discoverer: the #GstDiscoverer
342
   * @uri: THe URI to load the serialized info for
343
   *
344
   * Retrieves information about a URI from and external source of information,
345
   * like a cache file. This is used by the discoverer to speed up the
346
   * discovery.
347
   *
348
   * Returns: (nullable) (transfer full): The #GstDiscovererInfo representing
349
   * @uri, or %NULL if no information
350
   *
351
   * Since: 1.24
352
   */
353
1
  gst_discoverer_signals[SIGNAL_LOAD_SERIALIZED_INFO] =
354
1
      g_signal_new ("load-serialized-info", G_TYPE_FROM_CLASS (klass),
355
1
      G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDiscovererClass,
356
1
          load_serialize_info), _gst_discoverer_info_accumulator, NULL, NULL,
357
1
      GST_TYPE_DISCOVERER_INFO, 1, G_TYPE_STRING);
358
1
}
359
360
static void
361
gst_discoverer_init (GstDiscoverer * dc)
362
5.19k
{
363
5.19k
  GstFormat format = GST_FORMAT_TIME;
364
365
5.19k
  dc->priv = gst_discoverer_get_instance_private (dc);
366
367
5.19k
  dc->priv->timeout = DEFAULT_PROP_TIMEOUT;
368
5.19k
  dc->priv->use_cache = DEFAULT_PROP_USE_CACHE;
369
5.19k
  dc->priv->async = FALSE;
370
371
5.19k
  g_mutex_init (&dc->priv->lock);
372
373
5.19k
  dc->priv->pending_subtitle_pads = 0;
374
375
5.19k
  dc->priv->current_state = GST_STATE_NULL;
376
5.19k
  dc->priv->target_state = GST_STATE_NULL;
377
5.19k
  dc->priv->no_more_pads = FALSE;
378
379
5.19k
  dc->priv->all_tags = NULL;
380
5.19k
  dc->priv->global_tags = NULL;
381
382
5.19k
  GST_LOG ("Creating pipeline");
383
5.19k
  dc->priv->pipeline = (GstBin *) gst_pipeline_new ("Discoverer");
384
5.19k
  GST_LOG_OBJECT (dc, "Creating uridecodebin");
385
5.19k
  dc->priv->uridecodebin =
386
5.19k
      gst_element_factory_make ("uridecodebin", "discoverer-uri");
387
5.19k
  if (G_UNLIKELY (dc->priv->uridecodebin == NULL)) {
388
0
    GST_ERROR_OBJECT (dc, "Can't create uridecodebin");
389
0
    return;
390
0
  }
391
392
5.19k
  g_object_set (dc->priv->uridecodebin, "post-stream-topology", TRUE, NULL);
393
394
5.19k
  GST_LOG_OBJECT (dc, "Adding uridecodebin to pipeline");
395
5.19k
  gst_bin_add (dc->priv->pipeline, dc->priv->uridecodebin);
396
397
5.19k
  dc->priv->pad_added_id =
398
5.19k
      g_signal_connect_object (dc->priv->uridecodebin, "pad-added",
399
5.19k
      G_CALLBACK (uridecodebin_pad_added_cb), dc, 0);
400
5.19k
  dc->priv->pad_remove_id =
401
5.19k
      g_signal_connect_object (dc->priv->uridecodebin, "pad-removed",
402
5.19k
      G_CALLBACK (uridecodebin_pad_removed_cb), dc, 0);
403
5.19k
  dc->priv->no_more_pads_id =
404
5.19k
      g_signal_connect_object (dc->priv->uridecodebin, "no-more-pads",
405
5.19k
      G_CALLBACK (uridecodebin_no_more_pads_cb), dc, 0);
406
5.19k
  dc->priv->source_chg_id =
407
5.19k
      g_signal_connect_object (dc->priv->uridecodebin, "notify::source",
408
5.19k
      G_CALLBACK (uridecodebin_source_changed_cb), dc, 0);
409
410
5.19k
  GST_LOG_OBJECT (dc, "Getting pipeline bus");
411
5.19k
  dc->priv->bus = gst_pipeline_get_bus ((GstPipeline *) dc->priv->pipeline);
412
413
5.19k
  dc->priv->bus_cb_id =
414
5.19k
      g_signal_connect_object (dc->priv->bus, "message",
415
5.19k
      G_CALLBACK (discoverer_bus_cb), dc, 0);
416
417
5.19k
  GST_DEBUG_OBJECT (dc, "Done initializing Discoverer");
418
419
  /* create queries */
420
5.19k
  dc->priv->seeking_query = gst_query_new_seeking (format);
421
5.19k
}
422
423
static void
424
discoverer_reset (GstDiscoverer * dc)
425
5.19k
{
426
5.19k
  GST_DEBUG_OBJECT (dc, "Resetting");
427
428
5.19k
  if (dc->priv->pending_uris) {
429
0
    g_list_foreach (dc->priv->pending_uris, (GFunc) g_free, NULL);
430
0
    g_list_free (dc->priv->pending_uris);
431
0
    dc->priv->pending_uris = NULL;
432
0
  }
433
434
5.19k
  if (dc->priv->pipeline)
435
5.19k
    gst_element_set_state ((GstElement *) dc->priv->pipeline, GST_STATE_NULL);
436
5.19k
}
437
438
25.9k
#define DISCONNECT_SIGNAL(o,i) G_STMT_START{           \
439
25.9k
  if ((i) && g_signal_handler_is_connected ((o), (i))) \
440
25.9k
    g_signal_handler_disconnect ((o), (i));            \
441
25.9k
  (i) = 0;                                             \
442
25.9k
}G_STMT_END
443
444
static void
445
gst_discoverer_dispose (GObject * obj)
446
5.19k
{
447
5.19k
  GstDiscoverer *dc = (GstDiscoverer *) obj;
448
449
5.19k
  GST_DEBUG_OBJECT (dc, "Disposing");
450
451
5.19k
  discoverer_reset (dc);
452
453
5.19k
  if (G_LIKELY (dc->priv->pipeline)) {
454
    /* Workaround for bug #118536 */
455
5.19k
    DISCONNECT_SIGNAL (dc->priv->uridecodebin, dc->priv->pad_added_id);
456
5.19k
    DISCONNECT_SIGNAL (dc->priv->uridecodebin, dc->priv->pad_remove_id);
457
5.19k
    DISCONNECT_SIGNAL (dc->priv->uridecodebin, dc->priv->no_more_pads_id);
458
5.19k
    DISCONNECT_SIGNAL (dc->priv->uridecodebin, dc->priv->source_chg_id);
459
5.19k
    DISCONNECT_SIGNAL (dc->priv->bus, dc->priv->bus_cb_id);
460
461
    /* pipeline was set to NULL in _reset */
462
5.19k
    gst_object_unref (dc->priv->pipeline);
463
5.19k
    if (dc->priv->bus)
464
5.19k
      gst_object_unref (dc->priv->bus);
465
466
5.19k
    dc->priv->pipeline = NULL;
467
5.19k
    dc->priv->uridecodebin = NULL;
468
5.19k
    dc->priv->bus = NULL;
469
5.19k
  }
470
471
5.19k
  gst_discoverer_stop (dc);
472
473
5.19k
  if (dc->priv->seeking_query) {
474
5.19k
    gst_query_unref (dc->priv->seeking_query);
475
5.19k
    dc->priv->seeking_query = NULL;
476
5.19k
  }
477
478
5.19k
  G_OBJECT_CLASS (gst_discoverer_parent_class)->dispose (obj);
479
5.19k
}
480
481
static void
482
gst_discoverer_finalize (GObject * obj)
483
5.19k
{
484
5.19k
  GstDiscoverer *dc = (GstDiscoverer *) obj;
485
486
5.19k
  g_mutex_clear (&dc->priv->lock);
487
488
5.19k
  G_OBJECT_CLASS (gst_discoverer_parent_class)->finalize (obj);
489
5.19k
}
490
491
static void
492
gst_discoverer_set_property (GObject * object, guint prop_id,
493
    const GValue * value, GParamSpec * pspec)
494
10.3k
{
495
10.3k
  GstDiscoverer *dc = (GstDiscoverer *) object;
496
497
10.3k
  switch (prop_id) {
498
5.19k
    case PROP_TIMEOUT:
499
5.19k
      gst_discoverer_set_timeout (dc, g_value_get_uint64 (value));
500
5.19k
      break;
501
5.19k
    case PROP_USE_CACHE:
502
5.19k
      DISCO_LOCK (dc);
503
5.19k
      dc->priv->use_cache = g_value_get_boolean (value);
504
5.19k
      DISCO_UNLOCK (dc);
505
5.19k
      break;
506
0
    default:
507
0
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
508
0
      break;
509
10.3k
  }
510
10.3k
}
511
512
static void
513
gst_discoverer_get_property (GObject * object, guint prop_id,
514
    GValue * value, GParamSpec * pspec)
515
0
{
516
0
  GstDiscoverer *dc = (GstDiscoverer *) object;
517
518
0
  switch (prop_id) {
519
0
    case PROP_TIMEOUT:
520
0
      DISCO_LOCK (dc);
521
0
      g_value_set_uint64 (value, dc->priv->timeout);
522
0
      DISCO_UNLOCK (dc);
523
0
      break;
524
0
    case PROP_USE_CACHE:
525
0
      DISCO_LOCK (dc);
526
0
      g_value_set_boolean (value, dc->priv->use_cache);
527
0
      DISCO_UNLOCK (dc);
528
0
      break;
529
0
    default:
530
0
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
531
0
      break;
532
0
  }
533
0
}
534
535
static void
536
gst_discoverer_set_timeout (GstDiscoverer * dc, GstClockTime timeout)
537
5.19k
{
538
5.19k
  g_return_if_fail (GST_CLOCK_TIME_IS_VALID (timeout));
539
540
5.19k
  GST_DEBUG_OBJECT (dc, "timeout : %" GST_TIME_FORMAT, GST_TIME_ARGS (timeout));
541
542
  /* FIXME : update current pending timeout if we're running */
543
5.19k
  DISCO_LOCK (dc);
544
5.19k
  dc->priv->timeout = timeout;
545
5.19k
  DISCO_UNLOCK (dc);
546
5.19k
}
547
548
static GstPadProbeReturn
549
_event_probe (GstPad * pad, GstPadProbeInfo * info, PrivateStream * ps)
550
3.28k
{
551
3.28k
  GstEvent *event = GST_PAD_PROBE_INFO_EVENT (info);
552
553
3.28k
  switch (GST_EVENT_TYPE (event)) {
554
0
    case GST_EVENT_TOC:{
555
0
      GstToc *tmp;
556
557
0
      gst_event_parse_toc (event, &tmp, NULL);
558
0
      GST_DEBUG_OBJECT (pad, "toc %" GST_PTR_FORMAT, tmp);
559
0
      DISCO_LOCK (ps->dc);
560
0
      ps->toc = tmp;
561
0
      if (G_LIKELY (ps->dc->priv->processing)) {
562
0
        GST_DEBUG_OBJECT (pad, "private stream %p toc %" GST_PTR_FORMAT, ps,
563
0
            tmp);
564
0
      } else
565
0
        GST_DEBUG_OBJECT (pad, "Dropping toc since preroll is done");
566
0
      DISCO_UNLOCK (ps->dc);
567
0
      break;
568
0
    }
569
636
    case GST_EVENT_STREAM_START:{
570
636
      const gchar *stream_id;
571
572
636
      gst_event_parse_stream_start (event, &stream_id);
573
574
636
      g_free (ps->stream_id);
575
636
      ps->stream_id = stream_id ? g_strdup (stream_id) : NULL;
576
636
      break;
577
0
    }
578
2.65k
    default:
579
2.65k
      break;
580
3.28k
  }
581
582
3.28k
  return GST_PAD_PROBE_OK;
583
3.28k
}
584
585
static GstStaticCaps subtitle_caps =
586
    GST_STATIC_CAPS
587
    ("application/x-ssa; application/x-ass; application/x-kate");
588
589
static gboolean
590
is_subtitle_caps (const GstCaps * caps)
591
675
{
592
675
  GstCaps *subs_caps;
593
675
  GstStructure *s;
594
675
  const gchar *name;
595
675
  gboolean ret;
596
597
675
  s = gst_caps_get_structure (caps, 0);
598
675
  if (!s)
599
0
    return FALSE;
600
601
675
  name = gst_structure_get_name (s);
602
675
  if (g_str_has_prefix (name, "text/") ||
603
67
      g_str_has_prefix (name, "subpicture/") ||
604
67
      g_str_has_prefix (name, "subtitle/") ||
605
67
      g_str_has_prefix (name, "closedcaption/") ||
606
67
      g_str_has_prefix (name, "application/x-subtitle"))
607
628
    return TRUE;
608
609
47
  subs_caps = gst_static_caps_get (&subtitle_caps);
610
47
  ret = gst_caps_can_intersect (caps, subs_caps);
611
47
  gst_caps_unref (subs_caps);
612
613
47
  return ret;
614
675
}
615
616
static GstPadProbeReturn
617
got_subtitle_data (GstPad * pad, GstPadProbeInfo * info, GstDiscoverer * dc)
618
2.95k
{
619
2.95k
  GstMessage *msg;
620
621
2.95k
  if (!(GST_IS_BUFFER (info->data) || (GST_IS_EVENT (info->data)
622
2.77k
              && (GST_EVENT_TYPE ((GstEvent *) info->data) == GST_EVENT_GAP
623
2.77k
                  || GST_EVENT_TYPE ((GstEvent *) info->data) ==
624
2.77k
                  GST_EVENT_EOS))))
625
2.77k
    return GST_PAD_PROBE_OK;
626
627
628
181
  DISCO_LOCK (dc);
629
630
181
  dc->priv->pending_subtitle_pads--;
631
632
181
  msg = gst_message_new_application (NULL,
633
181
      gst_structure_new_empty ("DiscovererDone"));
634
181
  gst_element_post_message ((GstElement *) dc->priv->pipeline, msg);
635
636
181
  DISCO_UNLOCK (dc);
637
638
181
  return GST_PAD_PROBE_REMOVE;
639
640
2.95k
}
641
642
static void
643
uridecodebin_source_changed_cb (GstElement * uridecodebin,
644
    GParamSpec * pspec, GstDiscoverer * dc)
645
5.19k
{
646
5.19k
  GstElement *src;
647
  /* get a handle to the source */
648
5.19k
  g_object_get (uridecodebin, pspec->name, &src, NULL);
649
650
5.19k
  GST_DEBUG_OBJECT (dc, "got a new source %p", src);
651
652
5.19k
  g_signal_emit (dc, gst_discoverer_signals[SIGNAL_SOURCE_SETUP], 0, src);
653
5.19k
  gst_object_unref (src);
654
5.19k
}
655
656
static void
657
uridecodebin_pad_added_cb (GstElement * uridecodebin, GstPad * pad,
658
    GstDiscoverer * dc)
659
641
{
660
641
  PrivateStream *ps;
661
641
  GstPad *sinkpad = NULL;
662
641
  GstCaps *caps;
663
641
  gchar *padname;
664
641
  gchar *tmpname;
665
666
641
  GST_DEBUG_OBJECT (dc, "pad %s:%s", GST_DEBUG_PAD_NAME (pad));
667
668
641
  DISCO_LOCK (dc);
669
641
  if (dc->priv->cleanup) {
670
5
    GST_WARNING_OBJECT (dc, "Cleanup, not adding pad");
671
5
    DISCO_UNLOCK (dc);
672
5
    return;
673
5
  }
674
636
  if (dc->priv->current_error) {
675
0
    GST_WARNING_OBJECT (dc, "Ongoing error, not adding more pads");
676
0
    DISCO_UNLOCK (dc);
677
0
    return;
678
0
  }
679
636
  ps = g_new0 (PrivateStream, 1);
680
681
636
  ps->dc = dc;
682
636
  ps->pad = pad;
683
636
  padname = gst_pad_get_name (pad);
684
636
  tmpname = g_strdup_printf ("discoverer-queue-%s", padname);
685
636
  ps->queue = gst_element_factory_make ("queue", tmpname);
686
636
  g_free (tmpname);
687
636
  tmpname = g_strdup_printf ("discoverer-sink-%s", padname);
688
636
  ps->sink = gst_element_factory_make ("fakesink", tmpname);
689
636
  g_free (tmpname);
690
636
  g_free (padname);
691
692
636
  if (G_UNLIKELY (ps->queue == NULL || ps->sink == NULL))
693
0
    goto error;
694
695
636
  g_object_set (ps->sink, "silent", TRUE, NULL);
696
636
  g_object_set (ps->queue, "max-size-buffers", 1, "silent", TRUE, NULL);
697
698
636
  sinkpad = gst_element_get_static_pad (ps->queue, "sink");
699
636
  if (sinkpad == NULL)
700
0
    goto error;
701
702
636
  caps = gst_pad_get_current_caps (pad);
703
636
  if (!caps) {
704
0
    GST_WARNING ("Couldn't get negotiated caps from %s:%s",
705
0
        GST_DEBUG_PAD_NAME (pad));
706
0
    caps = gst_pad_query_caps (pad, NULL);
707
0
  }
708
709
636
  if (caps && !gst_caps_is_empty (caps) && !gst_caps_is_any (caps)
710
636
      && is_subtitle_caps (caps)) {
711
    /* Subtitle streams are sparse and may not provide any information - don't
712
     * wait for data to preroll */
713
589
    ps->probe_id =
714
589
        gst_pad_add_probe (sinkpad, GST_PAD_PROBE_TYPE_DATA_DOWNSTREAM,
715
589
        (GstPadProbeCallback) got_subtitle_data, dc, NULL);
716
589
    g_object_set (ps->sink, "async", FALSE, NULL);
717
589
    dc->priv->pending_subtitle_pads++;
718
589
  }
719
720
636
  if (caps)
721
636
    gst_caps_unref (caps);
722
723
636
  gst_bin_add_many (dc->priv->pipeline, ps->queue, ps->sink, NULL);
724
725
636
  if (!gst_element_link_pads_full (ps->queue, "src", ps->sink, "sink",
726
636
          GST_PAD_LINK_CHECK_NOTHING))
727
0
    goto error;
728
636
  if (!gst_element_sync_state_with_parent (ps->sink))
729
0
    goto error;
730
636
  if (!gst_element_sync_state_with_parent (ps->queue))
731
0
    goto error;
732
733
636
  if (gst_pad_link_full (pad, sinkpad,
734
636
          GST_PAD_LINK_CHECK_NOTHING) != GST_PAD_LINK_OK)
735
0
    goto error;
736
636
  gst_object_unref (sinkpad);
737
738
  /* Add an event probe */
739
636
  gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM,
740
636
      (GstPadProbeCallback) _event_probe, ps, NULL);
741
742
636
  dc->priv->streams = g_list_append (dc->priv->streams, ps);
743
636
  DISCO_UNLOCK (dc);
744
745
636
  GST_DEBUG_OBJECT (dc, "Done handling pad");
746
747
636
  return;
748
749
0
error:
750
0
  GST_ERROR_OBJECT (dc, "Error while handling pad");
751
0
  if (sinkpad)
752
0
    gst_object_unref (sinkpad);
753
0
  if (ps->queue)
754
0
    gst_object_unref (ps->queue);
755
0
  if (ps->sink)
756
0
    gst_object_unref (ps->sink);
757
0
  g_free (ps);
758
0
  DISCO_UNLOCK (dc);
759
0
  return;
760
636
}
761
762
static void
763
uridecodebin_no_more_pads_cb (GstElement * uridecodebin, GstDiscoverer * dc)
764
548
{
765
548
  GstMessage *msg = gst_message_new_application (NULL,
766
548
      gst_structure_new_empty ("DiscovererDone"));
767
768
548
  DISCO_LOCK (dc);
769
548
  dc->priv->no_more_pads = TRUE;
770
548
  gst_element_post_message ((GstElement *) dc->priv->pipeline, msg);
771
548
  DISCO_UNLOCK (dc);
772
548
}
773
774
static void
775
uridecodebin_pad_removed_cb (GstElement * uridecodebin, GstPad * pad,
776
    GstDiscoverer * dc)
777
641
{
778
641
  GList *tmp;
779
641
  PrivateStream *ps;
780
641
  GstPad *sinkpad;
781
782
641
  GST_DEBUG_OBJECT (dc, "pad %s:%s", GST_DEBUG_PAD_NAME (pad));
783
784
  /* Find the PrivateStream */
785
641
  DISCO_LOCK (dc);
786
776
  for (tmp = dc->priv->streams; tmp; tmp = tmp->next) {
787
771
    ps = (PrivateStream *) tmp->data;
788
771
    if (ps->pad == pad)
789
636
      break;
790
771
  }
791
792
641
  if (tmp == NULL) {
793
5
    DISCO_UNLOCK (dc);
794
5
    GST_DEBUG_OBJECT (dc, "The removed pad wasn't controlled by us !");
795
5
    return;
796
5
  }
797
798
636
  if (ps->probe_id)
799
589
    gst_pad_remove_probe (pad, ps->probe_id);
800
801
636
  dc->priv->streams = g_list_delete_link (dc->priv->streams, tmp);
802
803
636
  gst_element_set_state (ps->sink, GST_STATE_NULL);
804
636
  gst_element_set_state (ps->queue, GST_STATE_NULL);
805
636
  gst_element_unlink (ps->queue, ps->sink);
806
807
636
  sinkpad = gst_element_get_static_pad (ps->queue, "sink");
808
636
  gst_pad_unlink (pad, sinkpad);
809
636
  gst_object_unref (sinkpad);
810
811
  /* references removed here */
812
636
  gst_bin_remove_many (dc->priv->pipeline, ps->sink, ps->queue, NULL);
813
814
636
  DISCO_UNLOCK (dc);
815
636
  if (ps->tags) {
816
56
    gst_tag_list_unref (ps->tags);
817
56
  }
818
636
  if (ps->toc) {
819
0
    gst_toc_unref (ps->toc);
820
0
  }
821
636
  g_free (ps->stream_id);
822
823
636
  g_free (ps);
824
825
636
  GST_DEBUG_OBJECT (dc, "Done handling pad");
826
636
}
827
828
static GstStructure *
829
collect_stream_information (GstDiscoverer * dc, PrivateStream * ps, guint idx)
830
14
{
831
14
  GstCaps *caps;
832
14
  GstStructure *st;
833
14
  gchar *stname;
834
835
14
  stname = g_strdup_printf ("stream-%02d", idx);
836
14
  st = gst_structure_new_empty (stname);
837
14
  g_free (stname);
838
839
  /* Get caps */
840
14
  caps = gst_pad_get_current_caps (ps->pad);
841
14
  if (!caps) {
842
0
    GST_WARNING ("Couldn't get negotiated caps from %s:%s",
843
0
        GST_DEBUG_PAD_NAME (ps->pad));
844
0
    caps = gst_pad_query_caps (ps->pad, NULL);
845
0
  }
846
14
  if (caps) {
847
14
    GST_DEBUG_OBJECT (dc, "stream-%02d, got caps %" GST_PTR_FORMAT, idx, caps);
848
14
    gst_structure_set_static_str (st, "caps", GST_TYPE_CAPS, caps, NULL);
849
14
    gst_caps_unref (caps);
850
14
  }
851
14
  if (ps->tags)
852
9
    gst_structure_set_static_str (st, "tags", GST_TYPE_TAG_LIST, ps->tags,
853
9
        NULL);
854
14
  if (ps->toc)
855
0
    gst_structure_set_static_str (st, "toc", GST_TYPE_TOC, ps->toc, NULL);
856
14
  if (ps->stream_id)
857
14
    gst_structure_set_static_str (st, "stream-id", G_TYPE_STRING, ps->stream_id,
858
14
        NULL);
859
860
14
  return st;
861
14
}
862
863
/* takes ownership of new_tags, may replace *taglist with a new one */
864
static void
865
gst_discoverer_merge_and_replace_tags (GstTagList ** taglist,
866
    GstTagList * new_tags)
867
9
{
868
9
  if (new_tags == NULL)
869
0
    return;
870
871
9
  if (*taglist == NULL) {
872
9
    *taglist = new_tags;
873
9
    return;
874
9
  }
875
876
0
  gst_tag_list_insert (*taglist, new_tags, GST_TAG_MERGE_REPLACE);
877
0
  gst_tag_list_unref (new_tags);
878
0
}
879
880
static void
881
collect_common_information (GstDiscovererStreamInfo * info,
882
    const GstStructure * st)
883
29
{
884
29
  if (gst_structure_has_field (st, "toc")) {
885
0
    gst_structure_get (st, "toc", GST_TYPE_TOC, &info->toc, NULL);
886
0
  }
887
888
29
  if (gst_structure_has_field (st, "stream-id")) {
889
14
    gst_structure_get (st, "stream-id", G_TYPE_STRING, &info->stream_id, NULL);
890
14
  }
891
29
}
892
893
static GstDiscovererStreamInfo *
894
make_info (GstDiscovererStreamInfo * parent, GType type, GstCaps * caps)
895
32
{
896
32
  GstDiscovererStreamInfo *info;
897
898
32
  if (parent)
899
13
    info = gst_discoverer_stream_info_ref (parent);
900
19
  else {
901
19
    info = g_object_new (type, NULL);
902
19
    if (caps)
903
16
      info->caps = gst_caps_ref (caps);
904
19
  }
905
32
  return info;
906
32
}
907
908
/* Parses a set of caps and tags in st and populates a GstDiscovererStreamInfo
909
 * structure (parent, if !NULL, otherwise it allocates one)
910
 */
911
static GstDiscovererStreamInfo *
912
collect_information (GstDiscoverer * dc, const GstStructure * st,
913
    GstDiscovererStreamInfo * parent)
914
32
{
915
32
  GstPad *srcpad;
916
32
  GstCaps *caps = NULL;
917
32
  GstStructure *caps_st;
918
32
  GstTagList *tags_st;
919
32
  const gchar *name;
920
32
  gint tmp, tmp2;
921
32
  guint utmp;
922
923
32
  if (!st || (!gst_structure_has_field (st, "caps")
924
0
          && !gst_structure_has_field (st, "element-srcpad"))) {
925
0
    GST_WARNING ("Couldn't find caps !");
926
0
    return make_info (parent, GST_TYPE_DISCOVERER_STREAM_INFO, NULL);
927
0
  }
928
929
32
  if (gst_structure_get (st, "element-srcpad", GST_TYPE_PAD, &srcpad, NULL)) {
930
18
    caps = gst_pad_get_current_caps (srcpad);
931
18
    gst_object_unref (srcpad);
932
18
  }
933
32
  if (!caps) {
934
22
    gst_structure_get (st, "caps", GST_TYPE_CAPS, &caps, NULL);
935
22
  }
936
937
32
  if (!caps || gst_caps_is_empty (caps) || gst_caps_is_any (caps)) {
938
3
    GST_WARNING ("Couldn't find caps !");
939
3
    if (caps)
940
3
      gst_caps_unref (caps);
941
3
    return make_info (parent, GST_TYPE_DISCOVERER_STREAM_INFO, NULL);
942
3
  }
943
944
29
  caps_st = gst_caps_get_structure (caps, 0);
945
29
  name = gst_structure_get_name (caps_st);
946
947
29
  if (g_str_has_prefix (name, "audio/")) {
948
0
    GstDiscovererAudioInfo *info;
949
0
    const gchar *format_str;
950
0
    guint64 channel_mask;
951
952
0
    info = (GstDiscovererAudioInfo *) make_info (parent,
953
0
        GST_TYPE_DISCOVERER_AUDIO_INFO, caps);
954
955
0
    if (gst_structure_get_int (caps_st, "rate", &tmp))
956
0
      info->sample_rate = (guint) tmp;
957
958
0
    if (gst_structure_get_int (caps_st, "channels", &tmp))
959
0
      info->channels = (guint) tmp;
960
961
0
    if (gst_structure_get (caps_st, "channel-mask", GST_TYPE_BITMASK,
962
0
            &channel_mask, NULL)) {
963
0
      info->channel_mask = channel_mask;
964
0
    } else if (info->channels) {
965
0
      info->channel_mask = gst_audio_channel_get_fallback_mask (info->channels);
966
0
    }
967
968
    /* FIXME: we only want to extract depth if raw audio is what's in the
969
     * container (i.e. not if there is a decoder involved) */
970
0
    format_str = gst_structure_get_string (caps_st, "format");
971
0
    if (format_str != NULL) {
972
0
      const GstAudioFormatInfo *finfo;
973
0
      GstAudioFormat format;
974
975
0
      format = gst_audio_format_from_string (format_str);
976
0
      finfo = gst_audio_format_get_info (format);
977
0
      if (finfo)
978
0
        info->depth = GST_AUDIO_FORMAT_INFO_DEPTH (finfo);
979
0
    }
980
981
0
    if (gst_structure_has_field (st, "tags")) {
982
0
      gst_structure_get (st, "tags", GST_TYPE_TAG_LIST, &tags_st, NULL);
983
0
      if (gst_tag_list_get_uint (tags_st, GST_TAG_BITRATE, &utmp) ||
984
0
          gst_tag_list_get_uint (tags_st, GST_TAG_NOMINAL_BITRATE, &utmp))
985
0
        info->bitrate = utmp;
986
987
0
      if (gst_tag_list_get_uint (tags_st, GST_TAG_MAXIMUM_BITRATE, &utmp))
988
0
        info->max_bitrate = utmp;
989
990
      /* FIXME: Is it worth it to remove the tags we've parsed? */
991
0
      gst_discoverer_merge_and_replace_tags (&info->parent.tags, tags_st);
992
0
    }
993
994
0
    collect_common_information (&info->parent, st);
995
996
0
    if (!info->language && ((GstDiscovererStreamInfo *) info)->tags) {
997
0
      gchar *language;
998
0
      if (gst_tag_list_get_string (((GstDiscovererStreamInfo *) info)->tags,
999
0
              GST_TAG_LANGUAGE_CODE, &language)) {
1000
0
        info->language = language;
1001
0
      }
1002
0
    }
1003
1004
0
    gst_caps_unref (caps);
1005
0
    return (GstDiscovererStreamInfo *) info;
1006
1007
29
  } else if (g_str_has_prefix (name, "video/") ||
1008
29
      g_str_has_prefix (name, "image/")) {
1009
0
    GstDiscovererVideoInfo *info;
1010
0
    const gchar *caps_str;
1011
1012
0
    info = (GstDiscovererVideoInfo *) make_info (parent,
1013
0
        GST_TYPE_DISCOVERER_VIDEO_INFO, caps);
1014
1015
0
    if (gst_structure_get_int (caps_st, "width", &tmp))
1016
0
      info->width = (guint) tmp;
1017
0
    if (gst_structure_get_int (caps_st, "height", &tmp))
1018
0
      info->height = (guint) tmp;
1019
1020
0
    if (gst_structure_get_fraction (caps_st, "framerate", &tmp, &tmp2)) {
1021
0
      info->framerate_num = (guint) tmp;
1022
0
      info->framerate_denom = (guint) tmp2;
1023
0
    } else {
1024
0
      info->framerate_num = 0;
1025
0
      info->framerate_denom = 1;
1026
0
    }
1027
1028
0
    if (gst_structure_get_fraction (caps_st, "pixel-aspect-ratio", &tmp, &tmp2)) {
1029
0
      info->par_num = (guint) tmp;
1030
0
      info->par_denom = (guint) tmp2;
1031
0
    } else {
1032
0
      info->par_num = 1;
1033
0
      info->par_denom = 1;
1034
0
    }
1035
1036
    /* FIXME: we only want to extract depth if raw video is what's in the
1037
     * container (i.e. not if there is a decoder involved) */
1038
0
    caps_str = gst_structure_get_string (caps_st, "format");
1039
0
    if (caps_str != NULL) {
1040
0
      const GstVideoFormatInfo *finfo;
1041
0
      GstVideoFormat format;
1042
1043
0
      format = gst_video_format_from_string (caps_str);
1044
0
      finfo = gst_video_format_get_info (format);
1045
0
      if (finfo)
1046
0
        info->depth = finfo->bits * finfo->n_components;
1047
0
    }
1048
1049
0
    caps_str = gst_structure_get_string (caps_st, "interlace-mode");
1050
0
    if (!caps_str || strcmp (caps_str, "progressive") == 0)
1051
0
      info->interlaced = FALSE;
1052
0
    else
1053
0
      info->interlaced = TRUE;
1054
1055
0
    if (gst_structure_has_field (st, "tags")) {
1056
0
      gst_structure_get (st, "tags", GST_TYPE_TAG_LIST, &tags_st, NULL);
1057
0
      if (gst_tag_list_get_uint (tags_st, GST_TAG_BITRATE, &utmp) ||
1058
0
          gst_tag_list_get_uint (tags_st, GST_TAG_NOMINAL_BITRATE, &utmp))
1059
0
        info->bitrate = utmp;
1060
1061
0
      if (gst_tag_list_get_uint (tags_st, GST_TAG_MAXIMUM_BITRATE, &utmp))
1062
0
        info->max_bitrate = utmp;
1063
1064
      /* FIXME: Is it worth it to remove the tags we've parsed? */
1065
0
      gst_discoverer_merge_and_replace_tags (&info->parent.tags, tags_st);
1066
0
    }
1067
1068
0
    collect_common_information (&info->parent, st);
1069
1070
0
    gst_caps_unref (caps);
1071
0
    return (GstDiscovererStreamInfo *) info;
1072
1073
29
  } else if (is_subtitle_caps (caps)) {
1074
29
    GstDiscovererSubtitleInfo *info;
1075
1076
29
    info = (GstDiscovererSubtitleInfo *) make_info (parent,
1077
29
        GST_TYPE_DISCOVERER_SUBTITLE_INFO, caps);
1078
1079
29
    if (gst_structure_has_field (st, "tags")) {
1080
9
      const gchar *language;
1081
1082
9
      gst_structure_get (st, "tags", GST_TYPE_TAG_LIST, &tags_st, NULL);
1083
1084
9
      language = gst_structure_get_string (caps_st, GST_TAG_LANGUAGE_CODE);
1085
9
      if (language)
1086
0
        info->language = g_strdup (language);
1087
1088
      /* FIXME: Is it worth it to remove the tags we've parsed? */
1089
9
      gst_discoverer_merge_and_replace_tags (&info->parent.tags, tags_st);
1090
9
    }
1091
1092
29
    collect_common_information (&info->parent, st);
1093
1094
29
    if (!info->language && ((GstDiscovererStreamInfo *) info)->tags) {
1095
9
      gchar *language;
1096
9
      if (gst_tag_list_get_string (((GstDiscovererStreamInfo *) info)->tags,
1097
9
              GST_TAG_LANGUAGE_CODE, &language)) {
1098
0
        info->language = language;
1099
0
      }
1100
9
    }
1101
1102
29
    gst_caps_unref (caps);
1103
29
    return (GstDiscovererStreamInfo *) info;
1104
1105
29
  } else {
1106
    /* None of the above - populate what information we can */
1107
0
    GstDiscovererStreamInfo *info;
1108
1109
0
    info = make_info (parent, GST_TYPE_DISCOVERER_STREAM_INFO, caps);
1110
1111
0
    if (gst_structure_get (st, "tags", GST_TYPE_TAG_LIST, &tags_st, NULL)) {
1112
0
      gst_discoverer_merge_and_replace_tags (&info->tags, tags_st);
1113
0
    }
1114
1115
0
    collect_common_information (info, st);
1116
1117
0
    gst_caps_unref (caps);
1118
0
    return info;
1119
0
  }
1120
1121
29
}
1122
1123
static GstStructure *
1124
find_stream_for_node (GstDiscoverer * dc, const GstStructure * topology)
1125
32
{
1126
32
  GstPad *pad;
1127
32
  GstPad *target_pad = NULL;
1128
32
  GstStructure *st = NULL;
1129
32
  PrivateStream *ps;
1130
32
  guint i;
1131
32
  GList *tmp;
1132
1133
32
  if (!dc->priv->streams) {
1134
0
    return NULL;
1135
0
  }
1136
1137
32
  if (!gst_structure_has_field (topology, "pad")) {
1138
16
    GST_DEBUG_OBJECT (dc, "Could not find pad for node %" GST_PTR_FORMAT,
1139
16
        topology);
1140
16
    return NULL;
1141
16
  }
1142
1143
16
  gst_structure_get (topology, "pad", GST_TYPE_PAD, &pad, NULL);
1144
1145
19
  for (i = 0, tmp = dc->priv->streams; tmp; tmp = tmp->next, i++) {
1146
17
    ps = (PrivateStream *) tmp->data;
1147
1148
17
    target_pad = gst_ghost_pad_get_target (GST_GHOST_PAD (ps->pad));
1149
17
    if (target_pad == NULL)
1150
0
      continue;
1151
17
    gst_object_unref (target_pad);
1152
1153
17
    if (target_pad == pad)
1154
14
      break;
1155
17
  }
1156
1157
16
  if (tmp)
1158
14
    st = collect_stream_information (dc, ps, i);
1159
1160
16
  gst_object_unref (pad);
1161
1162
16
  return st;
1163
32
}
1164
1165
/* this can fail due to {framed,parsed}={TRUE,FALSE} differences, thus we filter
1166
 * the parent */
1167
static gboolean
1168
child_is_same_stream (const GstCaps * _parent, const GstCaps * child)
1169
16
{
1170
16
  GstCaps *parent;
1171
16
  gboolean res;
1172
1173
16
  if (_parent == child)
1174
3
    return TRUE;
1175
13
  if (!_parent)
1176
3
    return FALSE;
1177
10
  if (!child)
1178
0
    return FALSE;
1179
1180
10
  parent = copy_and_clean_caps (_parent);
1181
10
  res = gst_caps_can_intersect (parent, child);
1182
10
  gst_caps_unref (parent);
1183
10
  return res;
1184
10
}
1185
1186
1187
static gboolean
1188
child_is_raw_stream (const GstCaps * parent, const GstCaps * child)
1189
13
{
1190
13
  const GstStructure *st1, *st2;
1191
13
  const gchar *name1, *name2;
1192
1193
13
  if (parent == child)
1194
0
    return TRUE;
1195
13
  if (!parent)
1196
3
    return FALSE;
1197
10
  if (!child)
1198
0
    return FALSE;
1199
1200
10
  st1 = gst_caps_get_structure (parent, 0);
1201
10
  name1 = gst_structure_get_name (st1);
1202
10
  st2 = gst_caps_get_structure (child, 0);
1203
10
  name2 = gst_structure_get_name (st2);
1204
1205
10
  if ((g_str_has_prefix (name1, "audio/") &&
1206
0
          g_str_has_prefix (name2, "audio/x-raw")) ||
1207
10
      ((g_str_has_prefix (name1, "video/") ||
1208
10
              g_str_has_prefix (name1, "image/")) &&
1209
0
          g_str_has_prefix (name2, "video/x-raw"))) {
1210
    /* child is the "raw" sub-stream corresponding to parent */
1211
0
    return TRUE;
1212
0
  }
1213
1214
10
  if (is_subtitle_caps (parent))
1215
10
    return TRUE;
1216
1217
0
  return FALSE;
1218
10
}
1219
1220
/* If a parent is non-NULL, collected stream information will be appended to it
1221
 * (and where the information exists, it will be overridden)
1222
 */
1223
static GstDiscovererStreamInfo *
1224
parse_stream_topology (GstDiscoverer * dc, const GstStructure * topology,
1225
    GstDiscovererStreamInfo * parent)
1226
37
{
1227
37
  GstDiscovererStreamInfo *res = NULL;
1228
37
  GstCaps *caps = NULL;
1229
37
  const GValue *nval = NULL;
1230
1231
37
  GST_DEBUG_OBJECT (dc, "parsing: %" GST_PTR_FORMAT, topology);
1232
1233
37
  nval = gst_structure_get_value (topology, "next");
1234
1235
37
  if (nval == NULL || GST_VALUE_HOLDS_STRUCTURE (nval)) {
1236
32
    GstStructure *st = find_stream_for_node (dc, topology);
1237
32
    gboolean add_to_list = TRUE;
1238
1239
32
    if (st) {
1240
14
      res = collect_information (dc, st, parent);
1241
14
      gst_structure_free (st);
1242
18
    } else {
1243
      /* Didn't find a stream structure, so let's just use the caps we have */
1244
18
      res = collect_information (dc, topology, parent);
1245
18
    }
1246
1247
32
    if (nval == NULL) {
1248
      /* FIXME : aggregate with information from main streams */
1249
16
      GST_DEBUG_OBJECT (dc, "Couldn't find 'next' ! might be the last entry");
1250
16
    } else {
1251
16
      GstPad *srcpad;
1252
1253
16
      st = (GstStructure *) gst_value_get_structure (nval);
1254
1255
16
      GST_DEBUG_OBJECT (dc, "next is a structure %" GST_PTR_FORMAT, st);
1256
1257
16
      if (!parent)
1258
16
        parent = res;
1259
1260
16
      if (gst_structure_get (st, "element-srcpad", GST_TYPE_PAD, &srcpad, NULL)) {
1261
16
        caps = gst_pad_get_current_caps (srcpad);
1262
16
        gst_object_unref (srcpad);
1263
16
      }
1264
16
      if (!caps) {
1265
2
        gst_structure_get (st, "caps", GST_TYPE_CAPS, &caps, NULL);
1266
2
      }
1267
1268
16
      if (caps) {
1269
16
        if (child_is_same_stream (parent->caps, caps)) {
1270
          /* We sometimes get an extra sub-stream from the parser. If this is
1271
           * the case, we just replace the parent caps with this stream's caps
1272
           * since they might contain more information */
1273
3
          gst_caps_replace (&parent->caps, caps);
1274
1275
3
          parse_stream_topology (dc, st, parent);
1276
3
          add_to_list = FALSE;
1277
13
        } else if (child_is_raw_stream (parent->caps, caps)) {
1278
          /* This is the "raw" stream corresponding to the parent. This
1279
           * contains more information than the parent, tags etc. */
1280
10
          parse_stream_topology (dc, st, parent);
1281
10
          add_to_list = FALSE;
1282
10
        } else {
1283
3
          GstDiscovererStreamInfo *next = parse_stream_topology (dc, st, NULL);
1284
3
          res->next = next;
1285
3
          next->previous = res;
1286
3
        }
1287
16
        gst_caps_unref (caps);
1288
16
      }
1289
16
    }
1290
1291
32
    if (add_to_list) {
1292
19
      res->stream_number = dc->priv->current_info_stream_count++;
1293
19
      dc->priv->current_info->stream_list =
1294
19
          g_list_append (dc->priv->current_info->stream_list, res);
1295
19
    } else {
1296
13
      gst_discoverer_stream_info_unref (res);
1297
13
    }
1298
1299
32
  } else if (GST_VALUE_HOLDS_LIST (nval)) {
1300
5
    guint i, len;
1301
5
    GstDiscovererContainerInfo *cont;
1302
5
    GstPad *srcpad;
1303
1304
5
    if (gst_structure_get (topology, "element-srcpad", GST_TYPE_PAD,
1305
5
            &srcpad, NULL)) {
1306
5
      caps = gst_pad_get_current_caps (srcpad);
1307
5
      gst_object_unref (srcpad);
1308
5
    }
1309
5
    if (!caps) {
1310
0
      gst_structure_get (topology, "caps", GST_TYPE_CAPS, &caps, NULL);
1311
0
    }
1312
1313
5
    if (!caps)
1314
5
      GST_WARNING ("Couldn't find caps !");
1315
1316
5
    len = gst_value_list_get_size (nval);
1317
5
    GST_DEBUG_OBJECT (dc, "next is a list of %d entries", len);
1318
1319
5
    cont = (GstDiscovererContainerInfo *)
1320
5
        g_object_new (GST_TYPE_DISCOVERER_CONTAINER_INFO, NULL);
1321
5
    cont->parent.caps = caps;
1322
5
    if (dc->priv->global_tags)
1323
4
      cont->tags = gst_tag_list_ref (dc->priv->global_tags);
1324
5
    res = (GstDiscovererStreamInfo *) cont;
1325
1326
11
    for (i = 0; i < len; i++) {
1327
6
      const GValue *subv = gst_value_list_get_value (nval, i);
1328
6
      const GstStructure *subst = gst_value_get_structure (subv);
1329
6
      GstDiscovererStreamInfo *substream;
1330
1331
6
      GST_DEBUG_OBJECT (dc, "%d %" GST_PTR_FORMAT, i, subst);
1332
1333
6
      substream = parse_stream_topology (dc, subst, NULL);
1334
1335
6
      substream->previous = res;
1336
6
      cont->streams =
1337
6
          g_list_append (cont->streams,
1338
6
          gst_discoverer_stream_info_ref (substream));
1339
6
    }
1340
5
  }
1341
1342
37
  return res;
1343
37
}
1344
1345
/* Required DISCO_LOCK to be taken, and will release it */
1346
static void
1347
setup_next_uri_locked (GstDiscoverer * dc)
1348
0
{
1349
0
  if (dc->priv->pending_uris != NULL) {
1350
0
    gboolean ready = _setup_locked (dc);
1351
0
    DISCO_UNLOCK (dc);
1352
1353
0
    if (!ready) {
1354
      /* Start timeout */
1355
0
      if (dc->priv->processing)
1356
0
        handle_current_async (dc);
1357
0
    } else {
1358
0
      g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
1359
0
          (GSourceFunc) emit_discovered_and_next, gst_object_ref (dc),
1360
0
          gst_object_unref);
1361
0
    }
1362
0
  } else {
1363
    /* We're done ! */
1364
0
    DISCO_UNLOCK (dc);
1365
0
    g_signal_emit (dc, gst_discoverer_signals[SIGNAL_FINISHED], 0);
1366
0
  }
1367
0
}
1368
1369
static void
1370
_ensure_info_tags (GstDiscoverer * dc)
1371
5.19k
{
1372
5.19k
  GstDiscovererInfo *info = dc->priv->current_info;
1373
1374
5.19k
  if (dc->priv->all_tags)
1375
16
    info->tags = dc->priv->all_tags;
1376
5.19k
  dc->priv->all_tags = NULL;
1377
5.19k
}
1378
1379
static void
1380
serialize_info_if_required (GstDiscoverer * dc, GstDiscovererInfo * info)
1381
5.19k
{
1382
1383
5.19k
  if (dc->priv->use_cache && dc->priv->current_cachefile
1384
0
      && info->result == GST_DISCOVERER_OK) {
1385
0
    GVariant *variant = gst_discoverer_info_to_variant (info,
1386
0
        GST_DISCOVERER_SERIALIZE_ALL);
1387
1388
0
    g_file_set_contents (dc->priv->current_cachefile,
1389
0
        g_variant_get_data (variant), g_variant_get_size (variant), NULL);
1390
0
    g_variant_unref (variant);
1391
0
  }
1392
5.19k
}
1393
1394
static void
1395
emit_discovered (GstDiscoverer * dc)
1396
0
{
1397
0
  GstDiscovererInfo *info = dc->priv->current_info;
1398
0
  GST_DEBUG_OBJECT (dc, "Emitting 'discovered' %s", info->uri);
1399
0
  g_signal_emit (dc, gst_discoverer_signals[SIGNAL_DISCOVERED], 0,
1400
0
      info, dc->priv->current_error);
1401
1402
  /* Clients get a copy of current_info since it is a boxed type */
1403
0
  gst_discoverer_info_unref (dc->priv->current_info);
1404
0
  dc->priv->current_info = NULL;
1405
0
  dc->priv->current_info_stream_count = 0;
1406
0
  g_free (dc->priv->current_cachefile);
1407
0
  dc->priv->current_cachefile = NULL;
1408
0
  dc->priv->current_info_from_cache = FALSE;
1409
0
}
1410
1411
static gboolean
1412
emit_discovered_and_next (GstDiscoverer * dc)
1413
0
{
1414
0
  emit_discovered (dc);
1415
1416
0
  DISCO_LOCK (dc);
1417
0
  setup_next_uri_locked (dc);
1418
1419
0
  return G_SOURCE_REMOVE;
1420
0
}
1421
1422
/* Called when pipeline is pre-rolled */
1423
static void
1424
discoverer_collect (GstDiscoverer * dc)
1425
5.19k
{
1426
5.19k
  GST_DEBUG_OBJECT (dc, "Collecting information");
1427
1428
  /* Stop the timeout handler if present */
1429
5.19k
  if (dc->priv->timeout_source) {
1430
0
    g_source_destroy (dc->priv->timeout_source);
1431
0
    g_source_unref (dc->priv->timeout_source);
1432
0
    dc->priv->timeout_source = NULL;
1433
0
  }
1434
1435
5.19k
  if (dc->priv->use_cache && dc->priv->current_info
1436
0
      && dc->priv->current_info_from_cache) {
1437
0
    GST_DEBUG_OBJECT (dc,
1438
0
        "Nothing to collect as the info was built from the cache");
1439
0
    return;
1440
0
  }
1441
1442
5.19k
  if (dc->priv->streams) {
1443
    /* FIXME : Make this querying optional */
1444
15
    if (TRUE) {
1445
15
      GstElement *pipeline = (GstElement *) dc->priv->pipeline;
1446
15
      gint64 dur;
1447
1448
15
      GST_DEBUG_OBJECT (dc, "Attempting to query duration");
1449
1450
15
      if (gst_element_query_duration (pipeline, GST_FORMAT_TIME, &dur)) {
1451
0
        GST_DEBUG_OBJECT (dc, "Got duration %" GST_TIME_FORMAT,
1452
0
            GST_TIME_ARGS (dur));
1453
0
        dc->priv->current_info->duration = (guint64) dur;
1454
15
      } else if (dc->priv->current_info->result != GST_DISCOVERER_ERROR) {
1455
13
        GstStateChangeReturn sret;
1456
        /* Note: We don't switch to PLAYING if we previously saw an ERROR since
1457
         * the state of various element isn't guaranteed anymore */
1458
1459
        /* Some parsers may not even return a rough estimate right away, e.g.
1460
         * because they've only processed a single frame so far, so if we
1461
         * didn't get a duration the first time, spin a bit and try again.
1462
         * Ugly, but still better than making parsers or other elements return
1463
         * completely bogus values. We need some API extensions to solve this
1464
         * better. */
1465
13
        GST_INFO ("No duration yet, try a bit harder..");
1466
        /* Make sure we don't add/remove elements while switching to PLAYING itself */
1467
13
        DISCO_LOCK (dc);
1468
13
        sret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
1469
13
        DISCO_UNLOCK (dc);
1470
13
        if (sret != GST_STATE_CHANGE_FAILURE) {
1471
13
          int i;
1472
1473
39
          for (i = 0; i < 2; ++i) {
1474
26
            g_usleep (G_USEC_PER_SEC / 20);
1475
26
            if (gst_element_query_duration (pipeline, GST_FORMAT_TIME, &dur)
1476
2
                && dur > 0) {
1477
0
              GST_DEBUG_OBJECT (dc, "Got duration %" GST_TIME_FORMAT,
1478
0
                  GST_TIME_ARGS (dur));
1479
0
              dc->priv->current_info->duration = (guint64) dur;
1480
0
              break;
1481
0
            }
1482
26
          }
1483
13
          gst_element_set_state (pipeline, GST_STATE_PAUSED);
1484
13
        }
1485
13
      }
1486
1487
15
      if (dc->priv->seeking_query) {
1488
15
        if (gst_element_query (pipeline, dc->priv->seeking_query)) {
1489
11
          GstFormat format;
1490
11
          gboolean seekable;
1491
1492
11
          gst_query_parse_seeking (dc->priv->seeking_query, &format,
1493
11
              &seekable, NULL, NULL);
1494
11
          if (format == GST_FORMAT_TIME) {
1495
11
            GST_DEBUG_OBJECT (dc, "Got seekable %d", seekable);
1496
11
            dc->priv->current_info->seekable = seekable;
1497
11
          }
1498
11
        }
1499
15
      }
1500
15
    }
1501
1502
15
    if (dc->priv->target_state == GST_STATE_PAUSED)
1503
15
      dc->priv->current_info->live = FALSE;
1504
0
    else
1505
0
      dc->priv->current_info->live = TRUE;
1506
1507
15
    DISCO_LOCK (dc);
1508
15
    if (dc->priv->current_topology) {
1509
15
      dc->priv->current_info_stream_count = 1;
1510
15
      dc->priv->current_info->stream_info = parse_stream_topology (dc,
1511
15
          dc->priv->current_topology, NULL);
1512
15
      if (dc->priv->current_info->stream_info)
1513
15
        dc->priv->current_info->stream_info->stream_number = 0;
1514
15
    }
1515
15
    DISCO_UNLOCK (dc);
1516
1517
    /*
1518
     * Images need some special handling. They do not have a duration, have
1519
     * caps named image/<foo> (th exception being MJPEG video which is also
1520
     * type image/jpeg), and should consist of precisely one stream (actually
1521
     * initially there are 2, the image and raw stream, but we squash these
1522
     * while parsing the stream topology). At some point, if we find that these
1523
     * conditions are not sufficient, we can count the number of decoders and
1524
     * parsers in the chain, and if there's more than one decoder, or any
1525
     * parser at all, we should not mark this as an image.
1526
     */
1527
15
    if (dc->priv->current_info->duration == 0 &&
1528
15
        dc->priv->current_info->stream_info != NULL &&
1529
15
        dc->priv->current_info->stream_info->next == NULL) {
1530
15
      GstDiscovererStreamInfo *stream_info;
1531
15
      GstStructure *st;
1532
1533
15
      stream_info = dc->priv->current_info->stream_info;
1534
15
      st = gst_caps_get_structure (stream_info->caps, 0);
1535
1536
15
      if (g_str_has_prefix (gst_structure_get_name (st), "image/"))
1537
0
        ((GstDiscovererVideoInfo *) stream_info)->is_image = TRUE;
1538
15
    }
1539
15
  }
1540
1541
5.19k
  _ensure_info_tags (dc);
1542
#if !GLIB_CHECK_VERSION(2,74,0)
1543
  /* Make sure the missing element details are NULL-terminated */
1544
  g_ptr_array_add (dc->priv->current_info->missing_elements_details, NULL);
1545
#endif
1546
5.19k
  serialize_info_if_required (dc, dc->priv->current_info);
1547
5.19k
  if (dc->priv->async)
1548
0
    emit_discovered (dc);
1549
5.19k
}
1550
1551
static void
1552
get_async_cb (gpointer cb_data, GSource * source, GSourceFunc * func,
1553
    gpointer * data)
1554
0
{
1555
0
  *func = (GSourceFunc) async_timeout_cb;
1556
0
  *data = cb_data;
1557
0
}
1558
1559
/* Wrapper since GSourceCallbackFuncs don't expect a return value from ref() */
1560
static void
1561
_void_g_object_ref (gpointer object)
1562
0
{
1563
0
  g_object_ref (G_OBJECT (object));
1564
0
}
1565
1566
static void
1567
handle_current_async (GstDiscoverer * dc)
1568
0
{
1569
0
  GSource *source;
1570
0
  static GSourceCallbackFuncs cb_funcs = {
1571
0
    _void_g_object_ref,
1572
0
    g_object_unref,
1573
0
    get_async_cb,
1574
0
  };
1575
1576
  /* Attach a timeout to the main context */
1577
0
  source = g_timeout_source_new (dc->priv->timeout / GST_MSECOND);
1578
0
  g_source_set_callback_indirect (source, g_object_ref (dc), &cb_funcs);
1579
0
  g_source_attach (source, dc->priv->ctx);
1580
0
  dc->priv->timeout_source = source;
1581
0
}
1582
1583
1584
/* Returns TRUE if processing should stop */
1585
static gboolean
1586
handle_message (GstDiscoverer * dc, GstMessage * msg)
1587
64.1k
{
1588
64.1k
  gboolean done = FALSE;
1589
64.1k
  const gchar *dump_name = NULL;
1590
1591
64.1k
  GST_DEBUG_OBJECT (GST_MESSAGE_SRC (msg), "got a %s message",
1592
64.1k
      GST_MESSAGE_TYPE_NAME (msg));
1593
1594
64.1k
  switch (GST_MESSAGE_TYPE (msg)) {
1595
5.18k
    case GST_MESSAGE_ERROR:{
1596
5.18k
      GError *gerr;
1597
5.18k
      gchar *debug;
1598
1599
5.18k
      gst_message_parse_error (msg, &gerr, &debug);
1600
5.18k
      GST_WARNING_OBJECT (GST_MESSAGE_SRC (msg),
1601
5.18k
          "Got an error [debug:%s], [message:%s]", debug, gerr->message);
1602
5.18k
      dc->priv->current_error = gerr;
1603
5.18k
      g_free (debug);
1604
1605
      /* We need to stop */
1606
5.18k
      done = TRUE;
1607
5.18k
      dump_name = "gst-discoverer-error";
1608
1609
      /* Don't override missing plugin result code for missing plugin errors */
1610
5.18k
      if (dc->priv->current_info->result != GST_DISCOVERER_MISSING_PLUGINS ||
1611
0
          (!g_error_matches (gerr, GST_CORE_ERROR,
1612
0
                  GST_CORE_ERROR_MISSING_PLUGIN) &&
1613
0
              !g_error_matches (gerr, GST_STREAM_ERROR,
1614
5.18k
                  GST_STREAM_ERROR_CODEC_NOT_FOUND))) {
1615
5.18k
        GST_DEBUG_OBJECT (dc, "Setting result to ERROR");
1616
5.18k
        dc->priv->current_info->result = GST_DISCOVERER_ERROR;
1617
5.18k
      }
1618
5.18k
    }
1619
5.18k
      break;
1620
1621
86
    case GST_MESSAGE_WARNING:{
1622
86
      GError *err;
1623
86
      gchar *debug = NULL;
1624
1625
86
      gst_message_parse_warning (msg, &err, &debug);
1626
86
      GST_CTX_WARNING_OBJECT (WARNING_MESSAGE_LOG_CTX, GST_MESSAGE_SRC (msg),
1627
86
          "Got a warning [debug:%s], [message:%s]", debug, err->message);
1628
86
      g_clear_error (&err);
1629
86
      g_free (debug);
1630
86
      dump_name = "gst-discoverer-warning";
1631
86
      break;
1632
0
    }
1633
1634
0
    case GST_MESSAGE_EOS:
1635
0
      GST_DEBUG_OBJECT (dc, "Got EOS !");
1636
0
      done = TRUE;
1637
0
      dump_name = "gst-discoverer-eos";
1638
0
      break;
1639
1640
718
    case GST_MESSAGE_APPLICATION:{
1641
718
      const gchar *name =
1642
718
          gst_structure_get_name (gst_message_get_structure (msg));
1643
1644
718
      if (g_strcmp0 (name, "DiscovererDone"))
1645
0
        break;
1646
1647
      /* Maybe we already reached the target state, and all we're waiting for
1648
       * is either the subtitle tags or no_more_pads
1649
       */
1650
718
      DISCO_LOCK (dc);
1651
718
      if (dc->priv->pending_subtitle_pads == 0)
1652
13
        done = dc->priv->no_more_pads
1653
13
            && dc->priv->target_state == dc->priv->current_state;
1654
718
      DISCO_UNLOCK (dc);
1655
1656
718
      if (done)
1657
13
        dump_name = "gst-discoverer-application-message";
1658
718
    }
1659
0
      break;
1660
1661
41.7k
    case GST_MESSAGE_STATE_CHANGED:{
1662
41.7k
      GstState old, new, pending;
1663
1664
41.7k
      gst_message_parse_state_changed (msg, &old, &new, &pending);
1665
41.7k
      if (GST_MESSAGE_SRC (msg) == (GstObject *) dc->priv->pipeline) {
1666
5.30k
        DISCO_LOCK (dc);
1667
5.30k
        dc->priv->current_state = new;
1668
1669
5.30k
        if (dc->priv->pending_subtitle_pads == 0)
1670
5.19k
          done = dc->priv->no_more_pads
1671
0
              && dc->priv->target_state == dc->priv->current_state;
1672
        /* Else we should get unblocked in GST_MESSAGE_APPLICATION */
1673
1674
5.30k
        DISCO_UNLOCK (dc);
1675
5.30k
      }
1676
1677
41.7k
      if (done)
1678
0
        dump_name = "gst-discoverer-target-state";
1679
41.7k
    }
1680
41.7k
      break;
1681
1682
540
    case GST_MESSAGE_ELEMENT:
1683
540
    {
1684
540
      const GstStructure *structure;
1685
1686
540
      structure = gst_message_get_structure (msg);
1687
540
      GST_DEBUG_OBJECT (GST_MESSAGE_SRC (msg),
1688
540
          "structure %" GST_PTR_FORMAT, structure);
1689
540
      if (gst_structure_has_name (structure, "missing-plugin")) {
1690
0
        GST_DEBUG_OBJECT (GST_MESSAGE_SRC (msg),
1691
0
            "Setting result to MISSING_PLUGINS");
1692
0
        dc->priv->current_info->result = GST_DISCOVERER_MISSING_PLUGINS;
1693
        /* FIXME 2.0 Remove completely the ->misc
1694
         * Keep the old behaviour for now.
1695
         */
1696
0
        if (dc->priv->current_info->misc)
1697
0
          gst_structure_free (dc->priv->current_info->misc);
1698
0
        dc->priv->current_info->misc = gst_structure_copy (structure);
1699
0
        g_ptr_array_add (dc->priv->current_info->missing_elements_details,
1700
0
            gst_missing_plugin_message_get_installer_detail (msg));
1701
540
      } else if (gst_structure_has_name (structure, "stream-topology")) {
1702
540
        if (dc->priv->current_topology)
1703
522
          gst_structure_free (dc->priv->current_topology);
1704
540
        dc->priv->current_topology = gst_structure_copy (structure);
1705
540
      }
1706
540
    }
1707
540
      break;
1708
1709
724
    case GST_MESSAGE_TAG:
1710
724
    {
1711
724
      GstTagList *tl, *tmp = NULL;
1712
724
      GstTagScope scope;
1713
1714
724
      gst_message_parse_tag (msg, &tl);
1715
724
      scope = gst_tag_list_get_scope (tl);
1716
724
      GST_DEBUG_OBJECT (GST_MESSAGE_SRC (msg), "Got tags %" GST_PTR_FORMAT, tl);
1717
1718
724
      tmp = gst_tag_list_merge (dc->priv->all_tags, tl, GST_TAG_MERGE_APPEND);
1719
724
      if (dc->priv->all_tags)
1720
708
        gst_tag_list_unref (dc->priv->all_tags);
1721
724
      dc->priv->all_tags = tmp;
1722
1723
724
      if (scope == GST_TAG_SCOPE_STREAM) {
1724
103
        for (GList * curr = dc->priv->streams; curr; curr = curr->next) {
1725
103
          PrivateStream *ps = (PrivateStream *) curr->data;
1726
103
          if (GST_MESSAGE_SRC (msg) == GST_OBJECT_CAST (ps->sink)) {
1727
103
            tmp = gst_tag_list_merge (ps->tags, tl, GST_TAG_MERGE_APPEND);
1728
103
            if (ps->tags)
1729
47
              gst_tag_list_unref (ps->tags);
1730
103
            ps->tags = tmp;
1731
103
            GST_DEBUG_OBJECT (ps->pad, "tags %" GST_PTR_FORMAT, ps->tags);
1732
103
            break;
1733
103
          }
1734
103
        }
1735
621
      } else {
1736
621
        tmp =
1737
621
            gst_tag_list_merge (dc->priv->global_tags, tl,
1738
621
            GST_TAG_MERGE_APPEND);
1739
621
        if (dc->priv->global_tags)
1740
614
          gst_tag_list_unref (dc->priv->global_tags);
1741
621
        dc->priv->global_tags = tmp;
1742
621
      }
1743
724
      gst_tag_list_unref (tl);
1744
724
    }
1745
724
      break;
1746
0
    case GST_MESSAGE_TOC:
1747
0
    {
1748
0
      GstToc *tmp;
1749
1750
0
      gst_message_parse_toc (msg, &tmp, NULL);
1751
0
      GST_DEBUG_OBJECT (GST_MESSAGE_SRC (msg), "Got toc %" GST_PTR_FORMAT, tmp);
1752
0
      if (dc->priv->current_info->toc)
1753
0
        gst_toc_unref (dc->priv->current_info->toc);
1754
0
      dc->priv->current_info->toc = tmp;        /* transfer ownership */
1755
0
      GST_DEBUG_OBJECT (GST_MESSAGE_SRC (msg), "Current info %p, toc %"
1756
0
          GST_PTR_FORMAT, dc->priv->current_info, tmp);
1757
0
    }
1758
0
      break;
1759
1760
15.1k
    default:
1761
15.1k
      break;
1762
64.1k
  }
1763
1764
64.1k
  if (dump_name != NULL) {
1765
    /* dump graph when done or for warnings */
1766
5.28k
    GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (dc->priv->pipeline),
1767
5.28k
        GST_DEBUG_GRAPH_SHOW_ALL, dump_name);
1768
5.28k
  }
1769
64.1k
  return done;
1770
64.1k
}
1771
1772
static void
1773
handle_current_sync (GstDiscoverer * dc)
1774
5.19k
{
1775
5.19k
  GTimer *timer;
1776
5.19k
  gdouble deadline = ((gdouble) dc->priv->timeout) / GST_SECOND;
1777
5.19k
  GstMessage *msg;
1778
5.19k
  gboolean done = FALSE;
1779
1780
5.19k
  timer = g_timer_new ();
1781
5.19k
  g_timer_start (timer);
1782
1783
64.1k
  do {
1784
    /* poll bus with timeout */
1785
    /* FIXME : make the timeout more fine-tuned */
1786
64.1k
    if ((msg = gst_bus_timed_pop (dc->priv->bus, GST_SECOND / 2))) {
1787
64.1k
      done = handle_message (dc, msg);
1788
64.1k
      gst_message_unref (msg);
1789
64.1k
    }
1790
64.1k
  } while (!done && (g_timer_elapsed (timer, NULL) < deadline));
1791
1792
  /* return result */
1793
5.19k
  if (!done) {
1794
0
    GST_DEBUG_OBJECT (dc, "we timed out! Setting result to TIMEOUT");
1795
0
    dc->priv->current_info->result = GST_DISCOVERER_TIMEOUT;
1796
0
  }
1797
1798
5.19k
  DISCO_LOCK (dc);
1799
5.19k
  dc->priv->processing = FALSE;
1800
5.19k
  DISCO_UNLOCK (dc);
1801
1802
1803
5.19k
  GST_DEBUG_OBJECT (dc, "Done");
1804
1805
5.19k
  g_timer_stop (timer);
1806
5.19k
  g_timer_destroy (timer);
1807
5.19k
}
1808
1809
static gchar *
1810
_serialized_info_get_path (GstDiscoverer * dc, gchar * uri)
1811
0
{
1812
0
  GChecksum *cs = NULL;
1813
0
  GStatBuf file_status;
1814
0
  gchar *location = NULL, *res = NULL, *cache_dir = NULL, *tmp = NULL,
1815
0
      *protocol = gst_uri_get_protocol (uri), hash_dirname[3] = "00";
1816
0
  const gchar *checksum;
1817
1818
0
  if (g_ascii_strcasecmp (protocol, "file") != 0) {
1819
0
    GST_DEBUG_OBJECT (dc, "Can not work with serialized DiscovererInfo"
1820
0
        " on non local files - protocol: %s", protocol);
1821
1822
0
    goto done;
1823
0
  }
1824
1825
0
  location = gst_uri_get_location (uri);
1826
0
  if (g_stat (location, &file_status) < 0) {
1827
0
    GST_DEBUG_OBJECT (dc, "Could not get stat for file: %s", location);
1828
1829
0
    goto done;
1830
0
  }
1831
1832
0
  tmp = g_strdup_printf ("%s-%" G_GSIZE_FORMAT "-%" G_GINT64_FORMAT,
1833
0
      location, (gsize) file_status.st_size, (gint64) file_status.st_mtime);
1834
0
  cs = g_checksum_new (G_CHECKSUM_SHA1);
1835
0
  g_checksum_update (cs, (const guchar *) tmp, strlen (tmp));
1836
0
  checksum = g_checksum_get_string (cs);
1837
1838
0
  hash_dirname[0] = checksum[0];
1839
0
  hash_dirname[1] = checksum[1];
1840
0
  cache_dir =
1841
0
      g_build_filename (g_get_user_cache_dir (), "gstreamer-" GST_API_VERSION,
1842
0
      CACHE_DIRNAME, hash_dirname, NULL);
1843
0
  g_mkdir_with_parents (cache_dir, 0777);
1844
1845
0
  res = g_build_filename (cache_dir, &checksum[2], NULL);
1846
1847
0
done:
1848
0
  g_checksum_free (cs);
1849
0
  g_free (cache_dir);
1850
0
  g_free (location);
1851
0
  g_free (tmp);
1852
0
  g_free (protocol);
1853
1854
0
  return res;
1855
0
}
1856
1857
static GstDiscovererInfo *
1858
_get_info_from_cachefile (GstDiscoverer * dc, gchar * cachefile)
1859
0
{
1860
0
  gchar *data;
1861
0
  gsize length;
1862
1863
0
  if (g_file_get_contents (cachefile, &data, &length, NULL)) {
1864
0
    GstDiscovererInfo *info = NULL;
1865
0
    GVariant *variant =
1866
0
        g_variant_new_from_data (G_VARIANT_TYPE ("v"), data, length,
1867
0
        TRUE, NULL, NULL);
1868
1869
0
    info = gst_discoverer_info_from_variant (variant);
1870
0
    g_variant_unref (variant);
1871
1872
0
    if (info) {
1873
0
      dc->priv->current_cachefile = cachefile;
1874
0
      dc->priv->current_info_from_cache = TRUE;
1875
0
    } else {
1876
0
      g_free (cachefile);
1877
0
    }
1878
1879
0
    GST_INFO_OBJECT (dc, "Got info from cache: %p %s", info,
1880
0
        dc->priv->current_cachefile);
1881
0
    g_free (data);
1882
1883
0
    return info;
1884
0
  } else {
1885
0
    g_free (cachefile);
1886
0
  }
1887
1888
0
  return NULL;
1889
0
}
1890
1891
static GstDiscovererInfo *
1892
load_serialized_info (GstDiscoverer * dc, gchar * uri)
1893
5.19k
{
1894
5.19k
  GstDiscovererInfo *res = NULL;
1895
1896
5.19k
  if (dc->priv->use_cache) {
1897
0
    gchar *cachefile = _serialized_info_get_path (dc, uri);
1898
1899
0
    if (cachefile) {
1900
0
      res = _get_info_from_cachefile (dc, cachefile);
1901
0
    }
1902
0
  }
1903
1904
5.19k
  return res;
1905
5.19k
}
1906
1907
static gboolean
1908
_setup_locked (GstDiscoverer * dc)
1909
5.19k
{
1910
5.19k
  GstStateChangeReturn ret;
1911
5.19k
  gchar *uri = (gchar *) dc->priv->pending_uris->data;
1912
1913
5.19k
  dc->priv->pending_uris =
1914
5.19k
      g_list_delete_link (dc->priv->pending_uris, dc->priv->pending_uris);
1915
1916
1917
5.19k
  GST_DEBUG_OBJECT (dc, "Setting up");
1918
1919
5.19k
  g_signal_emit (dc, gst_discoverer_signals[SIGNAL_LOAD_SERIALIZED_INFO], 0,
1920
5.19k
      uri, &dc->priv->current_info);
1921
5.19k
  if (dc->priv->current_info) {
1922
    /* Make sure the URI is exactly what the user passed in */
1923
0
    g_free (dc->priv->current_info->uri);
1924
0
    dc->priv->current_info->uri = uri;
1925
1926
0
    dc->priv->processing = FALSE;
1927
0
    dc->priv->target_state = GST_STATE_NULL;
1928
1929
0
    return TRUE;
1930
0
  }
1931
1932
  /* Pop URI off the pending URI list */
1933
5.19k
  dc->priv->current_info =
1934
5.19k
      (GstDiscovererInfo *) g_object_new (GST_TYPE_DISCOVERER_INFO, NULL);
1935
5.19k
  dc->priv->current_info_stream_count = 0;
1936
5.19k
  if (dc->priv->use_cache)
1937
0
    dc->priv->current_cachefile = _serialized_info_get_path (dc, uri);
1938
5.19k
  dc->priv->current_info->uri = uri;
1939
1940
  /* set uri on uridecodebin */
1941
5.19k
  g_object_set (dc->priv->uridecodebin, "uri", dc->priv->current_info->uri,
1942
5.19k
      NULL);
1943
1944
5.19k
  GST_DEBUG_OBJECT (dc, "Current is now %s", dc->priv->current_info->uri);
1945
1946
5.19k
  dc->priv->processing = TRUE;
1947
1948
5.19k
  dc->priv->target_state = GST_STATE_PAUSED;
1949
1950
  /* set pipeline to PAUSED */
1951
5.19k
  DISCO_UNLOCK (dc);
1952
5.19k
  GST_DEBUG_OBJECT (dc, "Setting pipeline to PAUSED");
1953
5.19k
  ret =
1954
5.19k
      gst_element_set_state ((GstElement *) dc->priv->pipeline,
1955
5.19k
      dc->priv->target_state);
1956
1957
5.19k
  if (ret == GST_STATE_CHANGE_NO_PREROLL) {
1958
0
    GST_DEBUG_OBJECT (dc, "Source is live, switching to PLAYING");
1959
0
    dc->priv->target_state = GST_STATE_PLAYING;
1960
0
    ret =
1961
0
        gst_element_set_state ((GstElement *) dc->priv->pipeline,
1962
0
        dc->priv->target_state);
1963
0
  }
1964
5.19k
  DISCO_LOCK (dc);
1965
1966
1967
5.19k
  GST_DEBUG_OBJECT (dc, "Pipeline going to PAUSED : %s",
1968
5.19k
      gst_state_change_return_get_name (ret));
1969
1970
5.19k
  return FALSE;
1971
5.19k
}
1972
1973
static void
1974
discoverer_cleanup (GstDiscoverer * dc)
1975
5.19k
{
1976
5.19k
  GST_DEBUG_OBJECT (dc, "Cleaning up");
1977
1978
5.19k
  DISCO_LOCK (dc);
1979
5.19k
  dc->priv->cleanup = TRUE;
1980
5.19k
  DISCO_UNLOCK (dc);
1981
1982
5.19k
  gst_bus_set_flushing (dc->priv->bus, TRUE);
1983
1984
5.19k
  DISCO_LOCK (dc);
1985
5.19k
  if (dc->priv->current_error) {
1986
5.18k
    g_error_free (dc->priv->current_error);
1987
5.18k
    DISCO_UNLOCK (dc);
1988
5.18k
    gst_element_set_state ((GstElement *) dc->priv->pipeline, GST_STATE_NULL);
1989
5.18k
  } else {
1990
13
    DISCO_UNLOCK (dc);
1991
13
  }
1992
1993
5.19k
  gst_element_set_state ((GstElement *) dc->priv->pipeline, GST_STATE_READY);
1994
5.19k
  gst_bus_set_flushing (dc->priv->bus, FALSE);
1995
1996
5.19k
  DISCO_LOCK (dc);
1997
5.19k
  dc->priv->current_error = NULL;
1998
5.19k
  if (dc->priv->current_topology) {
1999
18
    gst_structure_free (dc->priv->current_topology);
2000
18
    dc->priv->current_topology = NULL;
2001
18
  }
2002
2003
5.19k
  dc->priv->current_info = NULL;
2004
5.19k
  dc->priv->current_info_stream_count = 0;
2005
5.19k
  g_free (dc->priv->current_cachefile);
2006
5.19k
  dc->priv->current_cachefile = NULL;
2007
5.19k
  dc->priv->current_info_from_cache = FALSE;
2008
2009
5.19k
  if (dc->priv->all_tags) {
2010
0
    gst_tag_list_unref (dc->priv->all_tags);
2011
0
    dc->priv->all_tags = NULL;
2012
0
  }
2013
2014
5.19k
  if (dc->priv->global_tags) {
2015
7
    gst_tag_list_unref (dc->priv->global_tags);
2016
7
    dc->priv->global_tags = NULL;
2017
7
  }
2018
2019
5.19k
  dc->priv->pending_subtitle_pads = 0;
2020
2021
5.19k
  dc->priv->current_state = GST_STATE_NULL;
2022
5.19k
  dc->priv->target_state = GST_STATE_NULL;
2023
5.19k
  dc->priv->no_more_pads = FALSE;
2024
5.19k
  dc->priv->cleanup = FALSE;
2025
2026
2027
  /* Try popping the next uri */
2028
5.19k
  if (dc->priv->async) {
2029
0
    setup_next_uri_locked (dc);
2030
0
  } else
2031
5.19k
    DISCO_UNLOCK (dc);
2032
2033
5.19k
  GST_DEBUG_OBJECT (dc, "out");
2034
5.19k
}
2035
2036
static void
2037
discoverer_bus_cb (GstBus * bus, GstMessage * msg, GstDiscoverer * dc)
2038
0
{
2039
0
  if (dc->priv->processing) {
2040
0
    if (handle_message (dc, msg)) {
2041
0
      GST_DEBUG_OBJECT (dc, "Stopping asynchronously");
2042
      /* Serialise with _event_probe() */
2043
0
      DISCO_LOCK (dc);
2044
0
      dc->priv->processing = FALSE;
2045
0
      DISCO_UNLOCK (dc);
2046
0
      discoverer_collect (dc);
2047
0
      discoverer_cleanup (dc);
2048
0
    }
2049
0
  }
2050
0
}
2051
2052
static gboolean
2053
async_timeout_cb (GstDiscoverer * dc)
2054
0
{
2055
0
  if (!g_source_is_destroyed (g_main_current_source ())) {
2056
0
    GST_DEBUG_OBJECT (dc, "Setting result to TIMEOUT");
2057
0
    dc->priv->current_info->result = GST_DISCOVERER_TIMEOUT;
2058
0
    dc->priv->processing = FALSE;
2059
0
    discoverer_collect (dc);
2060
0
    discoverer_cleanup (dc);
2061
0
  }
2062
0
  return FALSE;
2063
0
}
2064
2065
/* If there is a pending URI, it will pop it from the list of pending
2066
 * URIs and start the discovery on it.
2067
 *
2068
 * Returns GST_DISCOVERER_OK if the next URI was popped and is processing,
2069
 * else a error flag.
2070
 */
2071
static GstDiscovererResult
2072
start_discovering (GstDiscoverer * dc)
2073
5.19k
{
2074
5.19k
  gboolean ready;
2075
5.19k
  GstDiscovererResult res = GST_DISCOVERER_OK;
2076
2077
5.19k
  GST_DEBUG_OBJECT (dc, "Starting");
2078
2079
5.19k
  DISCO_LOCK (dc);
2080
5.19k
  if (dc->priv->cleanup) {
2081
0
    GST_DEBUG_OBJECT (dc, "The discoverer is busy cleaning up.");
2082
0
    res = GST_DISCOVERER_BUSY;
2083
0
    DISCO_UNLOCK (dc);
2084
0
    goto beach;
2085
0
  }
2086
2087
5.19k
  if (dc->priv->pending_uris == NULL) {
2088
0
    GST_INFO_OBJECT (dc, "No URI to process");
2089
0
    res = GST_DISCOVERER_URI_INVALID;
2090
0
    DISCO_UNLOCK (dc);
2091
0
    goto beach;
2092
0
  }
2093
2094
5.19k
  if (dc->priv->current_info != NULL) {
2095
0
    GST_DEBUG_OBJECT (dc, "Already processing a file");
2096
0
    res = GST_DISCOVERER_BUSY;
2097
0
    DISCO_UNLOCK (dc);
2098
0
    goto beach;
2099
0
  }
2100
2101
5.19k
  g_signal_emit (dc, gst_discoverer_signals[SIGNAL_STARTING], 0);
2102
2103
5.19k
  ready = _setup_locked (dc);
2104
2105
5.19k
  DISCO_UNLOCK (dc);
2106
2107
5.19k
  if (dc->priv->async) {
2108
0
    if (ready) {
2109
0
      GSource *source;
2110
2111
0
      source = g_idle_source_new ();
2112
0
      g_source_set_callback (source,
2113
0
          (GSourceFunc) emit_discovered_and_next, gst_object_ref (dc),
2114
0
          gst_object_unref);
2115
0
      g_source_attach (source, dc->priv->ctx);
2116
0
      goto beach;
2117
0
    }
2118
0
    if (dc->priv->processing)
2119
0
      handle_current_async (dc);
2120
5.19k
  } else {
2121
5.19k
    if (!ready)
2122
5.19k
      handle_current_sync (dc);
2123
5.19k
  }
2124
2125
5.19k
beach:
2126
5.19k
  return res;
2127
5.19k
}
2128
2129
/* Serializing code */
2130
2131
static GVariant *
2132
_serialize_common_stream_info (GstDiscovererStreamInfo * sinfo,
2133
    GstDiscovererSerializeFlags flags)
2134
0
{
2135
0
  GVariant *common;
2136
0
  GVariant *nextv = NULL;
2137
0
  gchar *caps_str = NULL, *tags_str = NULL, *misc_str = NULL;
2138
2139
0
  if (sinfo->caps && (flags & GST_DISCOVERER_SERIALIZE_CAPS))
2140
0
    caps_str = gst_caps_to_string (sinfo->caps);
2141
2142
0
  if (sinfo->tags && (flags & GST_DISCOVERER_SERIALIZE_TAGS))
2143
0
    tags_str = gst_tag_list_to_string (sinfo->tags);
2144
2145
0
  if (sinfo->misc && (flags & GST_DISCOVERER_SERIALIZE_MISC))
2146
0
    misc_str = gst_structure_to_string (sinfo->misc);
2147
2148
2149
0
  if (sinfo->next)
2150
0
    nextv = gst_discoverer_info_to_variant_recurse (sinfo->next, flags);
2151
0
  else
2152
0
    nextv = g_variant_new ("()");
2153
2154
0
  common =
2155
0
      g_variant_new ("(msmsmsmsv)", sinfo->stream_id, caps_str, tags_str,
2156
0
      misc_str, nextv);
2157
2158
0
  g_free (caps_str);
2159
0
  g_free (tags_str);
2160
0
  g_free (misc_str);
2161
2162
0
  return common;
2163
0
}
2164
2165
static GVariant *
2166
_serialize_info (GstDiscovererInfo * info, GstDiscovererSerializeFlags flags)
2167
0
{
2168
0
  gchar *tags_str = NULL;
2169
0
  GVariant *ret;
2170
2171
0
  if (info->tags && (flags & GST_DISCOVERER_SERIALIZE_TAGS))
2172
0
    tags_str = gst_tag_list_to_string (info->tags);
2173
2174
0
  ret =
2175
0
      g_variant_new ("(mstbmsb)", info->uri, info->duration, info->seekable,
2176
0
      tags_str, info->live);
2177
2178
0
  g_free (tags_str);
2179
2180
0
  return ret;
2181
0
}
2182
2183
static GVariant *
2184
_serialize_audio_stream_info (GstDiscovererAudioInfo * ainfo)
2185
0
{
2186
0
  return g_variant_new ("(uuuuumst)",
2187
0
      ainfo->channels,
2188
0
      ainfo->sample_rate,
2189
0
      ainfo->bitrate, ainfo->max_bitrate, ainfo->depth, ainfo->language,
2190
0
      ainfo->channel_mask);
2191
0
}
2192
2193
static GVariant *
2194
_serialize_video_stream_info (GstDiscovererVideoInfo * vinfo)
2195
0
{
2196
0
  return g_variant_new ("(uuuuuuubuub)",
2197
0
      vinfo->width,
2198
0
      vinfo->height,
2199
0
      vinfo->depth,
2200
0
      vinfo->framerate_num,
2201
0
      vinfo->framerate_denom,
2202
0
      vinfo->par_num,
2203
0
      vinfo->par_denom,
2204
0
      vinfo->interlaced, vinfo->bitrate, vinfo->max_bitrate, vinfo->is_image);
2205
0
}
2206
2207
static GVariant *
2208
_serialize_subtitle_stream_info (GstDiscovererSubtitleInfo * sinfo)
2209
0
{
2210
0
  return g_variant_new ("ms", sinfo->language);
2211
0
}
2212
2213
static GVariant *
2214
gst_discoverer_info_to_variant_recurse (GstDiscovererStreamInfo * sinfo,
2215
    GstDiscovererSerializeFlags flags)
2216
0
{
2217
0
  GVariant *stream_variant = NULL;
2218
0
  GVariant *common_stream_variant =
2219
0
      _serialize_common_stream_info (sinfo, flags);
2220
2221
0
  if (GST_IS_DISCOVERER_CONTAINER_INFO (sinfo)) {
2222
0
    GList *tmp;
2223
0
    GList *streams =
2224
0
        gst_discoverer_container_info_get_streams (GST_DISCOVERER_CONTAINER_INFO
2225
0
        (sinfo));
2226
2227
0
    if (g_list_length (streams) > 0) {
2228
0
      GVariantBuilder children;
2229
0
      GVariant *child_variant;
2230
0
      g_variant_builder_init (&children, G_VARIANT_TYPE_ARRAY);
2231
2232
0
      for (tmp = streams; tmp; tmp = tmp->next) {
2233
0
        child_variant =
2234
0
            gst_discoverer_info_to_variant_recurse (tmp->data, flags);
2235
0
        g_variant_builder_add (&children, "v", child_variant);
2236
0
      }
2237
0
      stream_variant =
2238
0
          g_variant_new ("(yvav)", 'c', common_stream_variant, &children);
2239
0
    } else {
2240
0
      stream_variant =
2241
0
          g_variant_new ("(yvav)", 'c', common_stream_variant, NULL);
2242
0
    }
2243
2244
0
    gst_discoverer_stream_info_list_free (streams);
2245
0
  } else if (GST_IS_DISCOVERER_AUDIO_INFO (sinfo)) {
2246
0
    GVariant *audio_stream_info =
2247
0
        _serialize_audio_stream_info (GST_DISCOVERER_AUDIO_INFO (sinfo));
2248
0
    stream_variant =
2249
0
        g_variant_new ("(yvv)", 'a', common_stream_variant, audio_stream_info);
2250
0
  } else if (GST_IS_DISCOVERER_VIDEO_INFO (sinfo)) {
2251
0
    GVariant *video_stream_info =
2252
0
        _serialize_video_stream_info (GST_DISCOVERER_VIDEO_INFO (sinfo));
2253
0
    stream_variant =
2254
0
        g_variant_new ("(yvv)", 'v', common_stream_variant, video_stream_info);
2255
0
  } else if (GST_IS_DISCOVERER_SUBTITLE_INFO (sinfo)) {
2256
0
    GVariant *subtitle_stream_info =
2257
0
        _serialize_subtitle_stream_info (GST_DISCOVERER_SUBTITLE_INFO (sinfo));
2258
0
    stream_variant =
2259
0
        g_variant_new ("(yvv)", 's', common_stream_variant,
2260
0
        subtitle_stream_info);
2261
0
  } else {
2262
0
    GVariant *nextv = NULL;
2263
0
    GstDiscovererStreamInfo *ninfo =
2264
0
        gst_discoverer_stream_info_get_next (sinfo);
2265
2266
0
    if (ninfo) {
2267
0
      nextv = gst_discoverer_info_to_variant_recurse (ninfo, flags);
2268
0
      stream_variant =
2269
0
          g_variant_new ("(yvv)", 'n', common_stream_variant,
2270
0
          g_variant_new ("v", nextv));
2271
0
    } else {
2272
0
      stream_variant = g_variant_new ("(yv)", 'n', common_stream_variant);
2273
0
    }
2274
0
  }
2275
2276
0
  return stream_variant;
2277
0
}
2278
2279
/* Parsing code */
2280
2281
0
#define GET_FROM_TUPLE(v, t, n, val) G_STMT_START{         \
2282
0
  GVariant *child = g_variant_get_child_value (v, n); \
2283
0
  *val = g_variant_get_##t(child); \
2284
0
  g_variant_unref (child); \
2285
0
}G_STMT_END
2286
2287
static const gchar *
2288
_maybe_get_string_from_tuple (GVariant * tuple, guint index)
2289
0
{
2290
0
  const gchar *ret = NULL;
2291
0
  GVariant *maybe;
2292
0
  GET_FROM_TUPLE (tuple, maybe, index, &maybe);
2293
0
  if (maybe) {
2294
0
    ret = g_variant_get_string (maybe, NULL);
2295
0
    g_variant_unref (maybe);
2296
0
  }
2297
2298
0
  return ret;
2299
0
}
2300
2301
static void
2302
_parse_info (GstDiscovererInfo * info, GVariant * info_variant)
2303
0
{
2304
0
  const gchar *str;
2305
2306
0
  str = _maybe_get_string_from_tuple (info_variant, 0);
2307
0
  if (str)
2308
0
    info->uri = g_strdup (str);
2309
2310
0
  GET_FROM_TUPLE (info_variant, uint64, 1, &info->duration);
2311
0
  GET_FROM_TUPLE (info_variant, boolean, 2, &info->seekable);
2312
2313
0
  str = _maybe_get_string_from_tuple (info_variant, 3);
2314
0
  if (str)
2315
0
    info->tags = gst_tag_list_new_from_string (str);
2316
2317
0
  GET_FROM_TUPLE (info_variant, boolean, 4, &info->live);
2318
0
}
2319
2320
static void
2321
_parse_common_stream_info (GstDiscovererStreamInfo * sinfo, GVariant * common,
2322
    GstDiscovererInfo * info)
2323
0
{
2324
0
  const gchar *str;
2325
2326
0
  str = _maybe_get_string_from_tuple (common, 0);
2327
0
  if (str)
2328
0
    sinfo->stream_id = g_strdup (str);
2329
2330
0
  str = _maybe_get_string_from_tuple (common, 1);
2331
0
  if (str)
2332
0
    sinfo->caps = gst_caps_from_string (str);
2333
2334
0
  str = _maybe_get_string_from_tuple (common, 2);
2335
0
  if (str)
2336
0
    sinfo->tags = gst_tag_list_new_from_string (str);
2337
2338
0
  str = _maybe_get_string_from_tuple (common, 3);
2339
0
  if (str)
2340
0
    sinfo->misc = gst_structure_new_from_string (str);
2341
2342
0
  if (g_variant_n_children (common) > 4) {
2343
0
    GVariant *nextv;
2344
2345
0
    GET_FROM_TUPLE (common, variant, 4, &nextv);
2346
0
    if (g_variant_n_children (nextv) > 0) {
2347
0
      sinfo->next = _parse_discovery (nextv, info);
2348
0
    }
2349
0
    g_variant_unref (nextv);
2350
0
  }
2351
2352
0
  g_variant_unref (common);
2353
0
}
2354
2355
static void
2356
_parse_audio_stream_info (GstDiscovererAudioInfo * ainfo, GVariant * specific)
2357
0
{
2358
0
  const gchar *str;
2359
2360
0
  GET_FROM_TUPLE (specific, uint32, 0, &ainfo->channels);
2361
0
  GET_FROM_TUPLE (specific, uint32, 1, &ainfo->sample_rate);
2362
0
  GET_FROM_TUPLE (specific, uint32, 2, &ainfo->bitrate);
2363
0
  GET_FROM_TUPLE (specific, uint32, 3, &ainfo->max_bitrate);
2364
0
  GET_FROM_TUPLE (specific, uint32, 4, &ainfo->depth);
2365
2366
0
  str = _maybe_get_string_from_tuple (specific, 5);
2367
2368
0
  if (str)
2369
0
    ainfo->language = g_strdup (str);
2370
2371
0
  GET_FROM_TUPLE (specific, uint64, 6, &ainfo->channel_mask);
2372
2373
0
  g_variant_unref (specific);
2374
0
}
2375
2376
static void
2377
_parse_video_stream_info (GstDiscovererVideoInfo * vinfo, GVariant * specific)
2378
0
{
2379
0
  GET_FROM_TUPLE (specific, uint32, 0, &vinfo->width);
2380
0
  GET_FROM_TUPLE (specific, uint32, 1, &vinfo->height);
2381
0
  GET_FROM_TUPLE (specific, uint32, 2, &vinfo->depth);
2382
0
  GET_FROM_TUPLE (specific, uint32, 3, &vinfo->framerate_num);
2383
0
  GET_FROM_TUPLE (specific, uint32, 4, &vinfo->framerate_denom);
2384
0
  GET_FROM_TUPLE (specific, uint32, 5, &vinfo->par_num);
2385
0
  GET_FROM_TUPLE (specific, uint32, 6, &vinfo->par_denom);
2386
0
  GET_FROM_TUPLE (specific, boolean, 7, &vinfo->interlaced);
2387
0
  GET_FROM_TUPLE (specific, uint32, 8, &vinfo->bitrate);
2388
0
  GET_FROM_TUPLE (specific, uint32, 9, &vinfo->max_bitrate);
2389
0
  GET_FROM_TUPLE (specific, boolean, 10, &vinfo->is_image);
2390
2391
0
  g_variant_unref (specific);
2392
0
}
2393
2394
static void
2395
_parse_subtitle_stream_info (GstDiscovererSubtitleInfo * sinfo,
2396
    GVariant * specific)
2397
0
{
2398
0
  GVariant *maybe;
2399
2400
0
  maybe = g_variant_get_maybe (specific);
2401
0
  if (maybe) {
2402
0
    sinfo->language = g_strdup (g_variant_get_string (maybe, NULL));
2403
0
    g_variant_unref (maybe);
2404
0
  }
2405
2406
0
  g_variant_unref (specific);
2407
0
}
2408
2409
static GstDiscovererStreamInfo *
2410
_parse_discovery (GVariant * variant, GstDiscovererInfo * info)
2411
0
{
2412
0
  gchar type;
2413
0
  GVariant *common = g_variant_get_child_value (variant, 1);
2414
0
  GVariant *specific = NULL;
2415
0
  GstDiscovererStreamInfo *sinfo = NULL;
2416
2417
0
  if (g_variant_n_children (variant) > 2)
2418
0
    specific = g_variant_get_child_value (variant, 2);
2419
2420
0
  GET_FROM_TUPLE (variant, byte, 0, &type);
2421
0
  switch (type) {
2422
0
    case 'c':
2423
0
      sinfo = g_object_new (GST_TYPE_DISCOVERER_CONTAINER_INFO, NULL);
2424
0
      break;
2425
0
    case 'a':
2426
0
      sinfo = g_object_new (GST_TYPE_DISCOVERER_AUDIO_INFO, NULL);
2427
0
      _parse_audio_stream_info (GST_DISCOVERER_AUDIO_INFO (sinfo),
2428
0
          g_variant_get_child_value (specific, 0));
2429
0
      break;
2430
0
    case 'v':
2431
0
      sinfo = g_object_new (GST_TYPE_DISCOVERER_VIDEO_INFO, NULL);
2432
0
      _parse_video_stream_info (GST_DISCOVERER_VIDEO_INFO (sinfo),
2433
0
          g_variant_get_child_value (specific, 0));
2434
0
      break;
2435
0
    case 's':
2436
0
      sinfo = g_object_new (GST_TYPE_DISCOVERER_SUBTITLE_INFO, NULL);
2437
0
      _parse_subtitle_stream_info (GST_DISCOVERER_SUBTITLE_INFO (sinfo),
2438
0
          g_variant_get_child_value (specific, 0));
2439
0
      break;
2440
0
    case 'n':
2441
0
      sinfo = g_object_new (GST_TYPE_DISCOVERER_STREAM_INFO, NULL);
2442
0
      break;
2443
0
    default:
2444
0
      GST_WARNING ("Unexpected discoverer info type %d", type);
2445
0
      goto out;
2446
0
  }
2447
2448
0
  _parse_common_stream_info (sinfo, g_variant_get_child_value (common, 0),
2449
0
      info);
2450
2451
0
  if (!GST_IS_DISCOVERER_CONTAINER_INFO (sinfo)) {
2452
0
    info->stream_list = g_list_append (info->stream_list, sinfo);
2453
0
  }
2454
2455
0
  if (!info->stream_info) {
2456
0
    info->stream_info = sinfo;
2457
0
  }
2458
2459
0
  if (GST_IS_DISCOVERER_CONTAINER_INFO (sinfo)) {
2460
0
    GVariantIter iter;
2461
0
    GVariant *child;
2462
2463
0
    GstDiscovererContainerInfo *cinfo = GST_DISCOVERER_CONTAINER_INFO (sinfo);
2464
0
    g_variant_iter_init (&iter, specific);
2465
0
    cinfo->tags = sinfo->tags;
2466
0
    while ((child = g_variant_iter_next_value (&iter))) {
2467
0
      GstDiscovererStreamInfo *child_info;
2468
0
      child_info = _parse_discovery (g_variant_get_variant (child), info);
2469
0
      if (child_info != NULL) {
2470
0
        cinfo->streams =
2471
0
            g_list_append (cinfo->streams,
2472
0
            gst_discoverer_stream_info_ref (child_info));
2473
0
      }
2474
0
      g_variant_unref (child);
2475
0
    }
2476
0
  }
2477
2478
0
out:
2479
2480
0
  g_variant_unref (common);
2481
0
  if (specific)
2482
0
    g_variant_unref (specific);
2483
0
  g_variant_unref (variant);
2484
0
  return sinfo;
2485
0
}
2486
2487
/**
2488
 * gst_discoverer_start:
2489
 * @discoverer: A #GstDiscoverer
2490
 *
2491
 * Allow asynchronous discovering of URIs to take place.
2492
 * A #GMainLoop must be available for #GstDiscoverer to properly work in
2493
 * asynchronous mode.
2494
 */
2495
void
2496
gst_discoverer_start (GstDiscoverer * discoverer)
2497
0
{
2498
0
  GSource *source;
2499
0
  GMainContext *ctx = NULL;
2500
2501
0
  g_return_if_fail (GST_IS_DISCOVERER (discoverer));
2502
2503
0
  GST_DEBUG_OBJECT (discoverer, "Starting...");
2504
2505
0
  if (discoverer->priv->async) {
2506
0
    GST_DEBUG_OBJECT (discoverer, "We were already started");
2507
0
    return;
2508
0
  }
2509
2510
0
  discoverer->priv->async = TRUE;
2511
0
  discoverer->priv->running = TRUE;
2512
2513
0
  ctx = g_main_context_get_thread_default ();
2514
2515
  /* Connect to bus signals */
2516
0
  if (ctx == NULL)
2517
0
    ctx = g_main_context_default ();
2518
2519
0
  source = gst_bus_create_watch (discoverer->priv->bus);
2520
0
  g_source_set_callback (source, (GSourceFunc) gst_bus_async_signal_func,
2521
0
      NULL, NULL);
2522
0
  g_source_attach (source, ctx);
2523
0
  discoverer->priv->bus_source = source;
2524
0
  discoverer->priv->ctx = g_main_context_ref (ctx);
2525
2526
0
  start_discovering (discoverer);
2527
0
  GST_DEBUG_OBJECT (discoverer, "Started");
2528
0
}
2529
2530
/**
2531
 * gst_discoverer_stop:
2532
 * @discoverer: A #GstDiscoverer
2533
 *
2534
 * Stop the discovery of any pending URIs and clears the list of
2535
 * pending URIS (if any).
2536
 */
2537
void
2538
gst_discoverer_stop (GstDiscoverer * discoverer)
2539
5.19k
{
2540
5.19k
  g_return_if_fail (GST_IS_DISCOVERER (discoverer));
2541
2542
5.19k
  GST_DEBUG_OBJECT (discoverer, "Stopping...");
2543
2544
5.19k
  if (!discoverer->priv->async) {
2545
5.19k
    GST_DEBUG_OBJECT (discoverer,
2546
5.19k
        "We were already stopped, or running synchronously");
2547
5.19k
    return;
2548
5.19k
  }
2549
2550
0
  DISCO_LOCK (discoverer);
2551
0
  if (discoverer->priv->processing) {
2552
    /* We prevent any further processing by setting the bus to
2553
     * flushing and setting the pipeline to READY.
2554
     * _reset() will take care of the rest of the cleanup */
2555
0
    if (discoverer->priv->bus)
2556
0
      gst_bus_set_flushing (discoverer->priv->bus, TRUE);
2557
0
    if (discoverer->priv->pipeline)
2558
0
      gst_element_set_state ((GstElement *) discoverer->priv->pipeline,
2559
0
          GST_STATE_READY);
2560
0
  }
2561
0
  discoverer->priv->running = FALSE;
2562
0
  DISCO_UNLOCK (discoverer);
2563
2564
  /* Remove timeout handler */
2565
0
  if (discoverer->priv->timeout_source) {
2566
0
    g_source_destroy (discoverer->priv->timeout_source);
2567
0
    g_source_unref (discoverer->priv->timeout_source);
2568
0
    discoverer->priv->timeout_source = NULL;
2569
0
  }
2570
  /* Remove signal watch */
2571
0
  if (discoverer->priv->bus_source) {
2572
0
    g_source_destroy (discoverer->priv->bus_source);
2573
0
    g_source_unref (discoverer->priv->bus_source);
2574
0
    discoverer->priv->bus_source = NULL;
2575
0
  }
2576
  /* Unref main context */
2577
0
  if (discoverer->priv->ctx) {
2578
0
    g_main_context_unref (discoverer->priv->ctx);
2579
0
    discoverer->priv->ctx = NULL;
2580
0
  }
2581
0
  discoverer_reset (discoverer);
2582
2583
0
  discoverer->priv->async = FALSE;
2584
2585
0
  GST_DEBUG_OBJECT (discoverer, "Stopped");
2586
0
}
2587
2588
/**
2589
 * gst_discoverer_discover_uri_async:
2590
 * @discoverer: A #GstDiscoverer
2591
 * @uri: the URI to add.
2592
 *
2593
 * Appends the given @uri to the list of URIs to discoverer. The actual
2594
 * discovery of the @uri will only take place if gst_discoverer_start() has
2595
 * been called.
2596
 *
2597
 * A copy of @uri will be made internally, so the caller can safely g_free()
2598
 * afterwards.
2599
 *
2600
 * Returns: %TRUE if the @uri was successfully appended to the list of pending
2601
 * uris, else %FALSE
2602
 */
2603
gboolean
2604
gst_discoverer_discover_uri_async (GstDiscoverer * discoverer,
2605
    const gchar * uri)
2606
0
{
2607
0
  gboolean can_run;
2608
2609
0
  g_return_val_if_fail (GST_IS_DISCOVERER (discoverer), FALSE);
2610
2611
0
  GST_DEBUG_OBJECT (discoverer, "uri : %s", uri);
2612
2613
0
  DISCO_LOCK (discoverer);
2614
0
  can_run = (discoverer->priv->pending_uris == NULL);
2615
0
  discoverer->priv->pending_uris =
2616
0
      g_list_append (discoverer->priv->pending_uris, g_strdup (uri));
2617
0
  DISCO_UNLOCK (discoverer);
2618
2619
0
  if (can_run)
2620
0
    start_discovering (discoverer);
2621
2622
0
  return TRUE;
2623
0
}
2624
2625
2626
/* Synchronous mode */
2627
/**
2628
 * gst_discoverer_discover_uri:
2629
 * @discoverer: A #GstDiscoverer
2630
 * @uri: The URI to run on.
2631
 * @err: If an error occurred, this field will be filled in.
2632
 *
2633
 * Synchronously discovers the given @uri.
2634
 *
2635
 * A copy of @uri will be made internally, so the caller can safely g_free()
2636
 * afterwards.
2637
 *
2638
 * Returns: (transfer full): the result of the scanning. Can be %NULL if an
2639
 * error occurred.
2640
 */
2641
GstDiscovererInfo *
2642
gst_discoverer_discover_uri (GstDiscoverer * discoverer, const gchar * uri,
2643
    GError ** err)
2644
5.19k
{
2645
5.19k
  GstDiscovererResult res = 0;
2646
5.19k
  GstDiscovererInfo *info;
2647
2648
5.19k
  g_return_val_if_fail (GST_IS_DISCOVERER (discoverer), NULL);
2649
5.19k
  g_return_val_if_fail (uri, NULL);
2650
2651
5.19k
  GST_DEBUG_OBJECT (discoverer, "uri:%s", uri);
2652
2653
5.19k
  DISCO_LOCK (discoverer);
2654
5.19k
  if (G_UNLIKELY (discoverer->priv->current_info)) {
2655
0
    DISCO_UNLOCK (discoverer);
2656
0
    GST_WARNING_OBJECT (discoverer, "Already handling a uri");
2657
0
    if (err)
2658
0
      *err = g_error_new (GST_CORE_ERROR, GST_CORE_ERROR_FAILED,
2659
0
          "Already handling a uri");
2660
0
    return NULL;
2661
0
  }
2662
2663
5.19k
  discoverer->priv->pending_uris =
2664
5.19k
      g_list_append (discoverer->priv->pending_uris, g_strdup (uri));
2665
5.19k
  DISCO_UNLOCK (discoverer);
2666
2667
5.19k
  res = start_discovering (discoverer);
2668
5.19k
  discoverer_collect (discoverer);
2669
2670
  /* Get results */
2671
5.19k
  if (err) {
2672
5.19k
    if (discoverer->priv->current_error)
2673
5.18k
      *err = g_error_copy (discoverer->priv->current_error);
2674
13
    else
2675
13
      *err = NULL;
2676
5.19k
  }
2677
5.19k
  if (res != GST_DISCOVERER_OK) {
2678
0
    GST_DEBUG_OBJECT (discoverer, "Setting result to %d (was %d)", res,
2679
0
        discoverer->priv->current_info->result);
2680
0
    discoverer->priv->current_info->result = res;
2681
0
  }
2682
2683
5.19k
  info = discoverer->priv->current_info;
2684
5.19k
  discoverer_cleanup (discoverer);
2685
2686
5.19k
  return info;
2687
5.19k
}
2688
2689
/**
2690
 * gst_discoverer_new:
2691
 * @timeout: timeout per file, in nanoseconds. Allowed are values between
2692
 *     one second (#GST_SECOND) and one hour (3600 * #GST_SECOND)
2693
 * @err: a pointer to a #GError. can be %NULL
2694
 *
2695
 * Creates a new #GstDiscoverer with the provided timeout.
2696
 *
2697
 * Returns: (transfer full): The new #GstDiscoverer.
2698
 * If an error occurred when creating the discoverer, @err will be set
2699
 * accordingly and %NULL will be returned. If @err is set, the caller must
2700
 * free it when no longer needed using g_error_free().
2701
 */
2702
GstDiscoverer *
2703
gst_discoverer_new (GstClockTime timeout, GError ** err)
2704
5.19k
{
2705
5.19k
  GstDiscoverer *res;
2706
2707
5.19k
  g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timeout), NULL);
2708
2709
5.19k
  res = g_object_new (GST_TYPE_DISCOVERER, "timeout", timeout, NULL);
2710
5.19k
  if (res->priv->uridecodebin == NULL) {
2711
0
    if (err)
2712
0
      *err = g_error_new (GST_CORE_ERROR, GST_CORE_ERROR_MISSING_PLUGIN,
2713
0
          "Couldn't create 'uridecodebin' element");
2714
0
    gst_object_unref (res);
2715
0
    res = NULL;
2716
0
  }
2717
5.19k
  return res;
2718
5.19k
}
2719
2720
/**
2721
 * gst_discoverer_info_to_variant:
2722
 * @info: A #GstDiscovererInfo
2723
 * @flags: A combination of #GstDiscovererSerializeFlags to specify
2724
 * what needs to be serialized.
2725
 *
2726
 * Serializes @info to a #GVariant that can be parsed again
2727
 * through gst_discoverer_info_from_variant().
2728
 *
2729
 * Note that any #GstToc (s) that might have been discovered will not be serialized
2730
 * for now.
2731
 *
2732
 * Returns: (transfer full): A newly-allocated #GVariant representing @info.
2733
 *
2734
 * Since: 1.6
2735
 */
2736
GVariant *
2737
gst_discoverer_info_to_variant (GstDiscovererInfo * info,
2738
    GstDiscovererSerializeFlags flags)
2739
0
{
2740
  /* FIXME: implement TOC support */
2741
0
  GVariant *stream_variant;
2742
0
  GVariant *variant, *info_variant;
2743
0
  GstDiscovererStreamInfo *sinfo;
2744
0
  GVariant *wrapper;
2745
2746
0
  g_return_val_if_fail (GST_IS_DISCOVERER_INFO (info), NULL);
2747
0
  g_return_val_if_fail (gst_discoverer_info_get_result (info) ==
2748
0
      GST_DISCOVERER_OK
2749
0
      || gst_discoverer_info_get_result (info) ==
2750
0
      GST_DISCOVERER_MISSING_PLUGINS, NULL);
2751
2752
0
  sinfo = gst_discoverer_info_get_stream_info (info);
2753
0
  stream_variant = gst_discoverer_info_to_variant_recurse (sinfo, flags);
2754
0
  info_variant = _serialize_info (info, flags);
2755
2756
0
  variant = g_variant_new ("(vv)", info_variant, stream_variant);
2757
2758
  /* Returning a wrapper implies some small overhead, but simplifies
2759
   * deserializing from bytes */
2760
0
  wrapper = g_variant_new_variant (variant);
2761
2762
0
  gst_discoverer_stream_info_unref (sinfo);
2763
0
  return wrapper;
2764
0
}
2765
2766
/**
2767
 * gst_discoverer_info_from_variant:
2768
 * @variant: A #GVariant to deserialize into a #GstDiscovererInfo.
2769
 *
2770
 * Parses a #GVariant as produced by gst_discoverer_info_to_variant()
2771
 * back to a #GstDiscovererInfo.
2772
 *
2773
 * Returns: (transfer full) (nullable): A newly-allocated #GstDiscovererInfo.
2774
 *
2775
 * Since: 1.6
2776
 */
2777
GstDiscovererInfo *
2778
gst_discoverer_info_from_variant (GVariant * variant)
2779
0
{
2780
0
  GVariant *info_specific_variant;
2781
0
  GVariant *wrapped;
2782
2783
0
  g_return_val_if_fail (variant, NULL);
2784
2785
0
  GVariant *info_variant = g_variant_get_variant (variant);
2786
2787
0
  if (!info_variant) {
2788
0
    GST_WARNING ("Failed to get variant from serialized info");
2789
2790
0
    return NULL;
2791
0
  }
2792
2793
0
  GET_FROM_TUPLE (info_variant, variant, 0, &info_specific_variant);
2794
0
  GET_FROM_TUPLE (info_variant, variant, 1, &wrapped);
2795
2796
0
  GstDiscovererInfo *info = g_object_new (GST_TYPE_DISCOVERER_INFO, NULL);
2797
0
  _parse_info (info, info_specific_variant);
2798
0
  _parse_discovery (wrapped, info);
2799
0
  g_variant_unref (info_specific_variant);
2800
0
  g_variant_unref (info_variant);
2801
2802
0
  return info;
2803
0
}