grammar.y 29.9 KB
Newer Older
1
%{
2
3
#include "../gst_private.h"

4
#include <glib-object.h>
5
6
#include <glib.h>
#include <stdio.h>
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
7
#include <string.h>
8
#include <stdlib.h>
9

10
#include "../gst-i18n-lib.h"
11

Benjamin Otte's avatar
Benjamin Otte committed
12
#include "../gstconfig.h"
13
#include "../gstparse.h"
14
#include "../gstinfo.h"
15
#include "../gsterror.h"
16
#include "../gststructure.h"
Benjamin Otte's avatar
Benjamin Otte committed
17
#include "../gsturi.h"
18
#include "../gstutils.h"
19
#include "../gstvalue.h"
20
#include "../gstchildproxy.h"
21
#include "types.h"
22

23
24
25
26
/* All error messages in this file are user-visible and need to be translated.
 * Don't start the message with a capital, and don't end them with a period,
 * as they will be presented inside a sentence/error.
 */
Stefan Kost's avatar
Stefan Kost committed
27

28
#define YYERROR_VERBOSE 1
29

30
31
32
33
34
35
#define YYENABLE_NLS 0

#ifndef YYLTYPE_IS_TRIVIAL
#define YYLTYPE_IS_TRIVIAL 0
#endif

36
37
typedef void* yyscan_t;

38
39
40
41
int priv_gst_parse_yylex (void * yylval_param , yyscan_t yyscanner);
int priv_gst_parse_yylex_init (yyscan_t scanner);
int priv_gst_parse_yylex_destroy (yyscan_t scanner);
struct yy_buffer_state * priv_gst_parse_yy_scan_string (char* , yyscan_t);
42
43
44
void _gst_parse_yypush_buffer_state (void * new_buffer ,yyscan_t yyscanner );
void _gst_parse_yypop_buffer_state (yyscan_t yyscanner );

45
#ifdef __GST_PARSE_TRACE
David Schleef's avatar
David Schleef committed
46
47
48
static guint __strings;
static guint __links;
static guint __chains;
49
50
51
gchar *
__gst_parse_strdup (gchar *org)
{
Stefan Kost's avatar
Stefan Kost committed
52
  gchar *ret;
53
  __strings++;
54
55
56
  ret = g_strdup (org);
  /* g_print ("ALLOCATED STR   (%3u): %p %s\n", __strings, ret, ret); */
  return ret;
57
58
59
60
61
}
void
__gst_parse_strfree (gchar *str)
{
  if (str) {
62
    /* g_print ("FREEING STR     (%3u): %p %s\n", __strings - 1, str, str); */
63
64
65
66
67
68
69
    g_free (str);
    g_return_if_fail (__strings > 0);
    __strings--;
  }
}
link_t *__gst_parse_link_new ()
{
70
  link_t *ret;
71
  __links++;
72
  ret = g_slice_new0 (link_t);
73
74
  /* g_print ("ALLOCATED LINK  (%3u): %p\n", __links, ret); */
  return ret;
75
76
77
78
79
}
void
__gst_parse_link_free (link_t *data)
{
  if (data) {
80
    /* g_print ("FREEING LINK    (%3u): %p\n", __links - 1, data); */
81
    g_slice_free (link_t, data);
82
83
84
85
86
87
88
    g_return_if_fail (__links > 0);
    __links--;
  }
}
chain_t *
__gst_parse_chain_new ()
{
89
  chain_t *ret;
90
  __chains++;
91
  ret = g_slice_new0 (chain_t);
92
93
  /* g_print ("ALLOCATED CHAIN (%3u): %p\n", __chains, ret); */
  return ret;
94
95
96
97
}
void
__gst_parse_chain_free (chain_t *data)
{
98
  /* g_print ("FREEING CHAIN   (%3u): %p\n", __chains - 1, data); */
99
  g_slice_free (chain_t, data);
100
101
  g_return_if_fail (__chains > 0);
  __chains--;
102
103
104
105
106
107
108
109
110
111
112
113
}

#endif /* __GST_PARSE_TRACE */

typedef struct {
  gchar *src_pad;
  gchar *sink_pad;
  GstElement *sink;
  GstCaps *caps;
  gulong signal_id;
} DelayedLink;

114
115
116
117
118
119
typedef struct {
  gchar *name;
  gchar *value_str;
  gulong signal_id;
} DelayedSet;

120
/*** define SET_ERROR macro/function */
121

122
#ifdef G_HAVE_ISO_VARARGS
123

124
125
#  define SET_ERROR(error, type, ...) \
G_STMT_START { \
126
127
128
  GST_CAT_ERROR (GST_CAT_PIPELINE, __VA_ARGS__); \
  if ((error) && !*(error)) { \
    g_set_error ((error), GST_PARSE_ERROR, (type), __VA_ARGS__); \
129
  } \
130
131
} G_STMT_END

132
133
#elif defined(G_HAVE_GNUC_VARARGS)

134
135
#  define SET_ERROR(error, type, args...) \
G_STMT_START { \
136
137
138
  GST_CAT_ERROR (GST_CAT_PIPELINE, args ); \
  if ((error) && !*(error)) { \
    g_set_error ((error), GST_PARSE_ERROR, (type), args ); \
139
  } \
140
141
} G_STMT_END

142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#else

static inline void
SET_ERROR (GError **error, gint type, const char *format, ...)
{
  if (error) {
    if (*error) {
      g_warning ("error while parsing");
    } else {
      va_list varargs;
      char *string;

      va_start (varargs, format);
      string = g_strdup_vprintf (format, varargs);
      va_end (varargs);
Stefan Kost's avatar
Stefan Kost committed
157

158
159
160
161
162
163
164
      g_set_error (error, GST_PARSE_ERROR, type, string);

      g_free (string);
    }
  }
}

165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#endif /* G_HAVE_ISO_VARARGS */

/*** define YYPRINTF macro/function if we're debugging */

/* bison 1.35 calls this macro with side effects, we need to make sure the
   side effects work - crappy bison */

#ifndef GST_DISABLE_GST_DEBUG
#  define YYDEBUG 1

#  ifdef G_HAVE_ISO_VARARGS

/* #  define YYFPRINTF(a, ...) GST_CAT_DEBUG (GST_CAT_PIPELINE, __VA_ARGS__) */
#    define YYFPRINTF(a, ...) \
G_STMT_START { \
180
     GST_CAT_LOG (GST_CAT_PIPELINE, __VA_ARGS__); \
181
182
183
184
185
186
} G_STMT_END

#  elif defined(G_HAVE_GNUC_VARARGS)

#    define YYFPRINTF(a, args...) \
G_STMT_START { \
187
     GST_CAT_LOG (GST_CAT_PIPELINE, args); \
188
189
190
191
} G_STMT_END

#  else

192
193
194
195
196
static inline void
YYPRINTF(const char *format, ...)
{
  va_list varargs;
  gchar *temp;
Stefan Kost's avatar
Stefan Kost committed
197

198
  va_start (varargs, format);
199
  temp = g_strdup_vprintf (format, varargs);
200
201
202
203
204
  GST_CAT_LOG (GST_CAT_PIPELINE, "%s", temp);
  g_free (temp);
  va_end (varargs);
}

205
206
207
#  endif /* G_HAVE_ISO_VARARGS */

#endif /* GST_DISABLE_GST_DEBUG */
208

209
210
211
212
213
214
#define ADD_MISSING_ELEMENT(graph,name) G_STMT_START {                      \
    if ((graph)->ctx) {                                                     \
      (graph)->ctx->missing_elements =                                      \
          g_list_append ((graph)->ctx->missing_elements, g_strdup (name));  \
    } } G_STMT_END

215
216
217
218
219
220
221
static void
no_free (gconstpointer foo)
{
  /* do nothing */
}

#define GST_BIN_MAKE(res, type, chainval, assign, type_string_free_func) \
222
G_STMT_START { \
223
  chain_t *chain = chainval; \
224
225
  GSList *walk; \
  GstBin *bin = (GstBin *) gst_element_factory_make (type, NULL); \
226
  if (!chain) { \
227
    SET_ERROR (graph->error, GST_PARSE_ERROR_EMPTY_BIN, \
228
        _("specified empty bin \"%s\", not allowed"), type); \
229
230
    g_slist_foreach (assign, (GFunc) gst_parse_strfree, NULL); \
    g_slist_free (assign); \
231
    gst_object_unref (bin); \
232
    type_string_free_func (type); /* Need to clean up the string */ \
233
234
    YYERROR; \
  } else if (!bin) { \
235
236
    ADD_MISSING_ELEMENT(graph, type); \
    SET_ERROR (graph->error, GST_PARSE_ERROR_NO_SUCH_ELEMENT, \
237
        _("no bin \"%s\", skipping"), type); \
238
239
240
    g_slist_foreach (assign, (GFunc) gst_parse_strfree, NULL); \
    g_slist_free (assign); \
    res = chain; \
241
  } else { \
242
    for (walk = chain->elements; walk; walk = walk->next ) \
243
244
245
246
247
      gst_bin_add (bin, GST_ELEMENT (walk->data)); \
    g_slist_free (chain->elements); \
    chain->elements = g_slist_prepend (NULL, bin); \
    res = chain; \
    /* set the properties now */ \
248
    for (walk = assign; walk; walk = walk->next) \
249
250
251
      gst_parse_element_set ((gchar *) walk->data, GST_ELEMENT (bin), graph); \
    g_slist_free (assign); \
  } \
252
} G_STMT_END
253

254
255
#define MAKE_LINK(link, _src, _src_name, _src_pads, _sink, _sink_name, _sink_pads) \
G_STMT_START { \
256
257
258
259
260
261
262
263
  link = gst_parse_link_new (); \
  link->src = _src; \
  link->sink = _sink; \
  link->src_name = _src_name; \
  link->sink_name = _sink_name; \
  link->src_pads = _src_pads; \
  link->sink_pads = _sink_pads; \
  link->caps = NULL; \
264
} G_STMT_END
265

266
267
#define MAKE_REF(link, _src, _pads) \
G_STMT_START { \
268
269
270
271
272
273
274
275
276
277
  gchar *padname = _src; \
  GSList *pads = _pads; \
  if (padname) { \
    while (*padname != '.') padname++; \
    *padname = '\0'; \
    padname++; \
    if (*padname != '\0') \
      pads = g_slist_prepend (pads, gst_parse_strdup (padname)); \
  } \
  MAKE_LINK (link, NULL, _src, pads, NULL, NULL, NULL); \
278
} G_STMT_END
279

280
281
282
283
284
285
286
287
288
static void
gst_parse_free_delayed_set (DelayedSet *set)
{
  g_free(set->name);
  g_free(set->value_str);
  g_slice_free(DelayedSet, set);
}

static void gst_parse_new_child(GstChildProxy *child_proxy, GObject *object,
289
    const gchar * name, gpointer data);
290
291
292
293
294

static void
gst_parse_add_delayed_set (GstElement *element, gchar *name, gchar *value_str)
{
  DelayedSet *data = g_slice_new0 (DelayedSet);
Stefan Kost's avatar
Stefan Kost committed
295

296
297
  GST_CAT_LOG_OBJECT (GST_CAT_PIPELINE, element, "delaying property set %s to %s",
    name, value_str);
Stefan Kost's avatar
Stefan Kost committed
298

299
300
301
302
303
  data->name = g_strdup(name);
  data->value_str = g_strdup(value_str);
  data->signal_id = g_signal_connect_data(element, "child-added",
      G_CALLBACK (gst_parse_new_child), data, (GClosureNotify)
      gst_parse_free_delayed_set, (GConnectFlags) 0);
Stefan Kost's avatar
Stefan Kost committed
304

305
306
307
308
  /* FIXME: we would need to listen on all intermediate bins too */
  if (GST_IS_BIN (element)) {
    gchar **names, **current;
    GstElement *parent, *child;
Stefan Kost's avatar
Stefan Kost committed
309

310
311
312
313
314
315
316
    current = names = g_strsplit (name, "::", -1);
    parent = gst_bin_get_by_name (GST_BIN_CAST (element), current[0]);
    current++;
    while (parent && current[0]) {
      child = gst_bin_get_by_name (GST_BIN (parent), current[0]);
      if (!child && current[1]) {
        char *sub_name = g_strjoinv ("::", &current[0]);
Stefan Kost's avatar
Stefan Kost committed
317

318
319
320
        gst_parse_add_delayed_set(parent, sub_name, value_str);
        g_free (sub_name);
      }
321
      gst_object_unref (parent);
322
323
324
      parent = child;
      current++;
    }
325
326
    if (parent)
      gst_object_unref (parent);
327
328
329
330
    g_strfreev (names);
  }
}

331
332
333
static void
gst_parse_new_child(GstChildProxy *child_proxy, GObject *object,
    const gchar * name, gpointer data)
334
335
336
{
  DelayedSet *set = (DelayedSet *) data;
  GParamSpec *pspec;
Stefan Kost's avatar
Stefan Kost committed
337
  GValue v = { 0, };
Wim Taymans's avatar
Wim Taymans committed
338
  GObject *target = NULL;
339
  GType value_type;
Stefan Kost's avatar
Stefan Kost committed
340

341
  GST_CAT_LOG_OBJECT (GST_CAT_PIPELINE, child_proxy, "new child %s, checking property %s",
342
      name, set->name);
343

344
  if (gst_child_proxy_lookup (child_proxy, set->name, &target, &pspec)) {
345
346
    gboolean got_value = FALSE;

347
    value_type = pspec->value_type;
348

349
350
    GST_CAT_LOG_OBJECT (GST_CAT_PIPELINE, child_proxy, "parsing delayed property %s as a %s from %s",
      pspec->name, g_type_name (value_type), set->value_str);
351
    g_value_init (&v, value_type);
352
353
354
355
    if (gst_value_deserialize (&v, set->value_str))
      got_value = TRUE;
    else if (g_type_is_a (value_type, GST_TYPE_ELEMENT)) {
       GstElement *bin;
Stefan Kost's avatar
Stefan Kost committed
356

357
358
       bin = gst_parse_bin_from_description_full (set->value_str, TRUE, NULL,
           GST_PARSE_FLAG_NO_SINGLE_ELEMENT_BINS, NULL);
359
360
361
362
       if (bin) {
         g_value_set_object (&v, bin);
         got_value = TRUE;
       }
363
364
    }
    g_signal_handler_disconnect (child_proxy, set->signal_id);
365
366
    if (!got_value)
      goto error;
Wim Taymans's avatar
Wim Taymans committed
367
    g_object_set_property (target, pspec->name, &v);
368
369
370
371
372
373
374
375
  } else {
    const gchar *obj_name = GST_OBJECT_NAME(object);
    gint len = strlen (obj_name);

    /* do a delayed set */
    if ((strlen (set->name) > (len + 2)) && !strncmp (set->name, obj_name, len) && !strncmp (&set->name[len], "::", 2)) {
      gst_parse_add_delayed_set (GST_ELEMENT(child_proxy), set->name, set->value_str);
    }
376
377
  }

378
out:
379
380
381
  if (G_IS_VALUE (&v))
    g_value_unset (&v);
  if (target)
Wim Taymans's avatar
Wim Taymans committed
382
    g_object_unref (target);
383
  return;
384
385

error:
LRN's avatar
LRN committed
386
  GST_CAT_ERROR (GST_CAT_PIPELINE, "could not set property \"%s\" in %"
Wim Taymans's avatar
Wim Taymans committed
387
      GST_PTR_FORMAT, pspec->name, target);
388
  goto out;
389
390
}

391
392
393
static void
gst_parse_element_set (gchar *value, GstElement *element, graph_t *graph)
{
394
  GParamSpec *pspec = NULL;
395
  gchar *pos = value;
Stefan Kost's avatar
Stefan Kost committed
396
  GValue v = { 0, };
Wim Taymans's avatar
Wim Taymans committed
397
  GObject *target = NULL;
398
399
  GType value_type;

400
401
402
403
  /* do nothing if assignment is for missing element */
  if (element == NULL)
    goto out;

Wim Taymans's avatar
Wim Taymans committed
404
  /* parse the string, so the property name is null-terminated and pos points
405
     to the beginning of the value */
Stefan Kost's avatar
Stefan Kost committed
406
407
408
409
410
  while (!g_ascii_isspace (*pos) && (*pos != '=')) pos++;
  if (*pos == '=') {
    *pos = '\0';
  } else {
    *pos = '\0';
411
    pos++;
Stefan Kost's avatar
Stefan Kost committed
412
413
414
415
    while (g_ascii_isspace (*pos)) pos++;
  }
  pos++;
  while (g_ascii_isspace (*pos)) pos++;
416
417
418
419
  if (*pos == '"') {
    pos++;
    pos[strlen (pos) - 1] = '\0';
  }
420
421
  gst_parse_unescape (pos);

422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
  if (GST_IS_CHILD_PROXY (element)) {
    if (!gst_child_proxy_lookup (GST_CHILD_PROXY (element), value, &target, &pspec)) {
      /* do a delayed set */
      gst_parse_add_delayed_set (element, value, pos);
    }
  } else {
    pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (element), value);
    if (pspec != NULL) {
      target = g_object_ref (element);
      GST_CAT_LOG_OBJECT (GST_CAT_PIPELINE, target, "found %s property", value);
    } else {
      SET_ERROR (graph->error, GST_PARSE_ERROR_NO_SUCH_PROPERTY, \
          _("no property \"%s\" in element \"%s\""), value, \
          GST_ELEMENT_NAME (element));
    }
  }

  if (pspec != NULL && target != NULL) {
440
441
    gboolean got_value = FALSE;

442
    value_type = pspec->value_type;
443

444
445
446
    GST_CAT_LOG_OBJECT (GST_CAT_PIPELINE, element, "parsing property %s as a %s",
        pspec->name, g_type_name (value_type));

447
    g_value_init (&v, value_type);
448
449
450
451
    if (gst_value_deserialize (&v, pos))
      got_value = TRUE;
    else if (g_type_is_a (value_type, GST_TYPE_ELEMENT)) {
       GstElement *bin;
Stefan Kost's avatar
Stefan Kost committed
452

453
454
       bin = gst_parse_bin_from_description_full (pos, TRUE, NULL,
           GST_PARSE_FLAG_NO_SINGLE_ELEMENT_BINS, NULL);
455
456
457
458
459
460
       if (bin) {
         g_value_set_object (&v, bin);
         got_value = TRUE;
       }
    }
    if (!got_value)
461
      goto error;
Wim Taymans's avatar
Wim Taymans committed
462
    g_object_set_property (target, pspec->name, &v);
463
464
465
466
  }

out:
  gst_parse_strfree (value);
467
468
  if (G_IS_VALUE (&v))
    g_value_unset (&v);
469
  if (target)
Wim Taymans's avatar
Wim Taymans committed
470
    g_object_unref (target);
471
  return;
Stefan Kost's avatar
Stefan Kost committed
472

473
error:
474
  SET_ERROR (graph->error, GST_PARSE_ERROR_COULD_NOT_SET_PROPERTY,
Stefan Kost's avatar
Stefan Kost committed
475
476
         _("could not set property \"%s\" in element \"%s\" to \"%s\""),
	 value, GST_ELEMENT_NAME (element), pos);
477
  goto out;
478
}
479

480
481
482
483
484
485
486
487
488
static inline void
gst_parse_free_link (link_t *link)
{
  gst_parse_strfree (link->src_name);
  gst_parse_strfree (link->sink_name);
  g_slist_foreach (link->src_pads, (GFunc) gst_parse_strfree, NULL);
  g_slist_foreach (link->sink_pads, (GFunc) gst_parse_strfree, NULL);
  g_slist_free (link->src_pads);
  g_slist_free (link->sink_pads);
489
  if (link->caps) gst_caps_unref (link->caps);
Stefan Kost's avatar
Stefan Kost committed
490
  gst_parse_link_free (link);
491
}
492

493
494
495
496
497
498
static void
gst_parse_free_delayed_link (DelayedLink *link)
{
  g_free (link->src_pad);
  g_free (link->sink_pad);
  if (link->caps) gst_caps_unref (link->caps);
499
  g_slice_free (DelayedLink, link);
500
501
}

502
503
504
static void
gst_parse_found_pad (GstElement *src, GstPad *pad, gpointer data)
{
505
  DelayedLink *link = data;
506

Stefan Kost's avatar
Stefan Kost committed
507
  GST_CAT_INFO (GST_CAT_PIPELINE, "trying delayed linking %s:%s to %s:%s",
508
509
                GST_STR_NULL (GST_ELEMENT_NAME (src)), GST_STR_NULL (link->src_pad),
                GST_STR_NULL (GST_ELEMENT_NAME (link->sink)), GST_STR_NULL (link->sink_pad));
Benjamin Otte's avatar
Benjamin Otte committed
510

511
512
513
514
  if (gst_element_link_pads_filtered (src, link->src_pad, link->sink,
      link->sink_pad, link->caps)) {
    /* do this here, we don't want to get any problems later on when
     * unlocking states */
Stefan Kost's avatar
Stefan Kost committed
515
    GST_CAT_DEBUG (GST_CAT_PIPELINE, "delayed linking %s:%s to %s:%s worked",
516
517
               	   GST_STR_NULL (GST_ELEMENT_NAME (src)), GST_STR_NULL (link->src_pad),
               	   GST_STR_NULL (GST_ELEMENT_NAME (link->sink)), GST_STR_NULL (link->sink_pad));
518
519
520
    g_signal_handler_disconnect (src, link->signal_id);
  }
}
521

522
523
/* both padnames and the caps may be NULL */
static gboolean
Stefan Kost's avatar
Stefan Kost committed
524
gst_parse_perform_delayed_link (GstElement *src, const gchar *src_pad,
525
526
                                GstElement *sink, const gchar *sink_pad,
                                GstCaps *caps)
527
{
528
529
  GList *templs = gst_element_class_get_pad_template_list (
      GST_ELEMENT_GET_CLASS (src));
Stefan Kost's avatar
Stefan Kost committed
530

531
  for (; templs; templs = templs->next) {
532
    GstPadTemplate *templ = (GstPadTemplate *) templs->data;
533
534
    if ((GST_PAD_TEMPLATE_DIRECTION (templ) == GST_PAD_SRC) &&
        (GST_PAD_TEMPLATE_PRESENCE(templ) == GST_PAD_SOMETIMES))
535
    {
Stefan Kost's avatar
Stefan Kost committed
536
537
      DelayedLink *data = g_slice_new (DelayedLink);

538
539
      /* TODO: maybe we should check if src_pad matches this template's names */

Stefan Kost's avatar
Stefan Kost committed
540
      GST_CAT_DEBUG (GST_CAT_PIPELINE, "trying delayed link %s:%s to %s:%s",
541
542
                     GST_STR_NULL (GST_ELEMENT_NAME (src)), GST_STR_NULL (src_pad),
                     GST_STR_NULL (GST_ELEMENT_NAME (sink)), GST_STR_NULL (sink_pad));
543
544
545
546

      data->src_pad = g_strdup (src_pad);
      data->sink = sink;
      data->sink_pad = g_strdup (sink_pad);
547
548
549
550
551
      if (caps) {
      	data->caps = gst_caps_copy (caps);
      } else {
      	data->caps = NULL;
      }
552
553
554
      data->signal_id = g_signal_connect_data (src, "pad-added",
          G_CALLBACK (gst_parse_found_pad), data,
          (GClosureNotify) gst_parse_free_delayed_link, (GConnectFlags) 0);
555
556
557
558
559
      return TRUE;
    }
  }
  return FALSE;
}
560

561
/*
562
 * performs a link and frees the struct. src and sink elements must be given
563
 * return values   0 - link performed
564
565
566
567
568
569
570
571
572
573
574
575
 *                 1 - link delayed
 *                <0 - error
 */
static gint
gst_parse_perform_link (link_t *link, graph_t *graph)
{
  GstElement *src = link->src;
  GstElement *sink = link->sink;
  GSList *srcs = link->src_pads;
  GSList *sinks = link->sink_pads;
  g_assert (GST_IS_ELEMENT (src));
  g_assert (GST_IS_ELEMENT (sink));
Stefan Kost's avatar
Stefan Kost committed
576

577
  GST_CAT_INFO (GST_CAT_PIPELINE,
Stefan Kost's avatar
Stefan Kost committed
578
      "linking %s:%s to %s:%s (%u/%u) with caps \"%" GST_PTR_FORMAT "\"",
579
580
581
      GST_ELEMENT_NAME (src), link->src_name ? link->src_name : "(any)",
      GST_ELEMENT_NAME (sink), link->sink_name ? link->sink_name : "(any)",
      g_slist_length (srcs), g_slist_length (sinks), link->caps);
582

583
  if (!srcs || !sinks) {
584
585
586
    if (gst_element_link_pads_filtered (src,
        srcs ? (const gchar *) srcs->data : NULL, sink,
        sinks ? (const gchar *) sinks->data : NULL, link->caps)) {
587
588
      goto success;
    } else {
589
590
591
      if (gst_parse_perform_delayed_link (src,
          srcs ? (const gchar *) srcs->data : NULL,
          sink, sinks ? (const gchar *) sinks->data : NULL, link->caps)) {
592
593
594
595
596
597
	goto success;
      } else {
        goto error;
      }
    }
  }
598
  if (g_slist_length (link->src_pads) != g_slist_length (link->sink_pads)) {
599
600
601
602
603
604
605
    goto error;
  }
  while (srcs && sinks) {
    const gchar *src_pad = (const gchar *) srcs->data;
    const gchar *sink_pad = (const gchar *) sinks->data;
    srcs = g_slist_next (srcs);
    sinks = g_slist_next (sinks);
606
607
    if (gst_element_link_pads_filtered (src, src_pad, sink, sink_pad,
        link->caps)) {
608
609
610
611
612
613
614
615
616
617
618
      continue;
    } else {
      if (gst_parse_perform_delayed_link (src, src_pad,
                                          sink, sink_pad,
					  link->caps)) {
	continue;
      } else {
        goto error;
      }
    }
  }
Stefan Kost's avatar
Stefan Kost committed
619

620
621
622
success:
  gst_parse_free_link (link);
  return 0;
Stefan Kost's avatar
Stefan Kost committed
623

624
error:
625
  SET_ERROR (graph->error, GST_PARSE_ERROR_LINK,
626
627
      _("could not link %s to %s"), GST_ELEMENT_NAME (src),
      GST_ELEMENT_NAME (sink));
628
629
630
631
  gst_parse_free_link (link);
  return -1;
}

632

633
static int yyerror (void *scanner, graph_t *graph, const char *s);
634
635
636
637
%}

%union {
    gchar *s;
638
639
640
641
    chain_t *c;
    link_t *l;
    GstElement *e;
    GSList *p;
642
    graph_t *g;
643
644
}

Benjamin Otte's avatar
Benjamin Otte committed
645
%token <s> PARSE_URL
646
%token <s> IDENTIFIER
647
648
%left <s> REF PADREF BINREF
%token <s> ASSIGNMENT
649
%token <s> LINK
650

651
652
653
654
655
%type <g> graph
%type <c> chain bin
%type <l> reference
%type <l> linkpart link
%type <p> linklist
Stefan Kost's avatar
Stefan Kost committed
656
%type <e> element
657
658
%type <p> padlist pads assignments

659
%left '(' ')'
660
%left ','
661
662
%right '.'
%left '!' '='
663

664
%lex-param { void *scanner }
665
666
667
%parse-param { void *scanner }
%parse-param { graph_t *graph }
%pure-parser
668

669
%start graph
670
671
%%

Stefan Kost's avatar
Stefan Kost committed
672
element:	IDENTIFIER     		      { $$ = gst_element_factory_make ($1, NULL);
673
						if ($$ == NULL) {
674
675
						  ADD_MISSING_ELEMENT (graph, $1);
						  SET_ERROR (graph->error, GST_PARSE_ERROR_NO_SUCH_ELEMENT, _("no element \"%s\""), $1);
676
677
678
						  /* if FATAL_ERRORS flag is set, we don't have to worry about backwards
						   * compatibility and can continue parsing and check for other missing
						   * elements */
679
						  if ((graph->flags & GST_PARSE_FLAG_FATAL_ERRORS) == 0) {
680
681
682
						    gst_parse_strfree ($1);
						    YYERROR;
						  }
683
684
						}
						gst_parse_strfree ($1);
685
686
687
688
689
690
691
                                              }
	|	element ASSIGNMENT	      { gst_parse_element_set ($2, $1, graph);
						$$ = $1;
	                                      }
	;
assignments:	/* NOP */		      { $$ = NULL; }
	|	assignments ASSIGNMENT	      { $$ = g_slist_prepend ($1, $2); }
Stefan Kost's avatar
Stefan Kost committed
692
	;
693
694
bin:	        '(' assignments chain ')' { GST_BIN_MAKE ($$, "bin", $3, $2, no_free); }
        |       BINREF assignments chain ')'  { GST_BIN_MAKE ($$, $1, $3, $2, gst_parse_strfree);
695
696
						gst_parse_strfree ($1);
					      }
697
        |       BINREF assignments ')'	      { GST_BIN_MAKE ($$, $1, NULL, $2, gst_parse_strfree);
698
699
						gst_parse_strfree ($1);
					      }
700
        |       BINREF assignments error ')'  { GST_BIN_MAKE ($$, $1, NULL, $2, gst_parse_strfree);
701
702
						gst_parse_strfree ($1);
					      }
703
	;
Stefan Kost's avatar
Stefan Kost committed
704

705
706
707
pads:		PADREF 			      { $$ = g_slist_prepend (NULL, $1); }
	|	PADREF padlist		      { $$ = $2;
						$$ = g_slist_prepend ($$, $1);
Stefan Kost's avatar
Stefan Kost committed
708
					      }
Benjamin Otte's avatar
Benjamin Otte committed
709
	;
710
711
712
padlist:	',' IDENTIFIER		      { $$ = g_slist_prepend (NULL, $2); }
	|	',' IDENTIFIER padlist	      { $$ = g_slist_prepend ($3, $2); }
	;
Stefan Kost's avatar
Stefan Kost committed
713

714
715
716
717
718
719
720
721
reference:	REF 			      { MAKE_REF ($$, $1, NULL); }
	|	REF padlist		      { MAKE_REF ($$, $1, $2); }
	;

linkpart:	reference		      { $$ = $1; }
	|	pads			      { MAKE_REF ($$, NULL, $1); }
	|	/* NOP */		      { MAKE_REF ($$, NULL, NULL); }
	;
Stefan Kost's avatar
Stefan Kost committed
722

723
724
725
link:		linkpart LINK linkpart	      { $$ = $1;
						if ($2) {
						  $$->caps = gst_caps_from_string ($2);
726
						  if ($$->caps == NULL)
727
						    SET_ERROR (graph->error, GST_PARSE_ERROR_LINK, _("could not parse caps \"%s\""), $2);
728
729
						  gst_parse_strfree ($2);
						}
730
731
732
733
734
						$$->sink_name = $3->src_name;
						$$->sink_pads = $3->src_pads;
						gst_parse_link_free ($3);
					      }
	;
Stefan Kost's avatar
Stefan Kost committed
735

736
737
linklist:	link			      { $$ = g_slist_prepend (NULL, $1); }
	|	link linklist		      { $$ = g_slist_prepend ($2, $1); }
738
	|	linklist error		      { $$ = $1; }
Stefan Kost's avatar
Stefan Kost committed
739
	;
740

741
742
743
744
745
746
747
748
chain:   	element			      { $$ = gst_parse_chain_new ();
						$$->first = $$->last = $1;
						$$->front = $$->back = NULL;
						$$->elements = g_slist_prepend (NULL, $1);
					      }
	|	bin			      { $$ = $1; }
	|	chain chain		      { if ($1->back && $2->front) {
						  if (!$1->back->sink_name) {
749
						    SET_ERROR (graph->error, GST_PARSE_ERROR_LINK, _("link without source element"));
750
751
						    gst_parse_free_link ($1->back);
						  } else {
752
						    graph->links = g_slist_prepend (graph->links, $1->back);
753
754
						  }
						  if (!$2->front->src_name) {
755
						    SET_ERROR (graph->error, GST_PARSE_ERROR_LINK, _("link without sink element"));
756
757
						    gst_parse_free_link ($2->front);
						  } else {
758
						    graph->links = g_slist_prepend (graph->links, $2->front);
759
760
761
762
763
764
765
766
767
768
						  }
						  $1->back = NULL;
						} else if ($1->back) {
						  if (!$1->back->sink_name) {
						    $1->back->sink = $2->first;
						  }
						} else if ($2->front) {
						  if (!$2->front->src_name) {
						    $2->front->src = $1->last;
						  }
769
						  $1->back = $2->front;
770
						}
Stefan Kost's avatar
Stefan Kost committed
771

772
						if ($1->back) {
773
						  graph->links = g_slist_prepend (graph->links, $1->back);
774
						}
775
776
777
						$1->last = $2->last;
						$1->back = $2->back;
						$1->elements = g_slist_concat ($1->elements, $2->elements);
778
779
						if ($2)
						  gst_parse_chain_free ($2);
780
781
782
783
						$$ = $1;
					      }
	|	chain linklist		      { GSList *walk;
						if ($1->back) {
784
						  $2 = g_slist_prepend ($2, $1->back);
785
786
787
788
						  $1->back = NULL;
						} else {
						  if (!((link_t *) $2->data)->src_name) {
						    ((link_t *) $2->data)->src = $1->last;
Stefan Kost's avatar
Stefan Kost committed
789
						  }
790
						}
791
						for (walk = $2; walk; walk = walk->next) {
792
						  link_t *link = (link_t *) walk->data;
793
						  if (!link->sink_name && walk->next) {
794
						    SET_ERROR (graph->error, GST_PARSE_ERROR_LINK, _("link without sink element"));
795
796
						    gst_parse_free_link (link);
						  } else if (!link->src_name && !link->src) {
797
						    SET_ERROR (graph->error, GST_PARSE_ERROR_LINK, _("link without source element"));
798
799
						    gst_parse_free_link (link);
						  } else {
800
						    if (walk->next) {
801
						      graph->links = g_slist_prepend (graph->links, link);
802
803
804
805
806
807
808
809
						    } else {
						      $1->back = link;
						    }
						  }
						}
						g_slist_free ($2);
						$$ = $1;
					      }
810
	|	chain error		      { $$ = $1; }
Benjamin Otte's avatar
Benjamin Otte committed
811
812
	|	link chain		      { if ($2->front) {
						  if (!$2->front->src_name) {
813
						    SET_ERROR (graph->error, GST_PARSE_ERROR_LINK, _("link without source element"));
Benjamin Otte's avatar
Benjamin Otte committed
814
815
						    gst_parse_free_link ($2->front);
						  } else {
816
						    graph->links = g_slist_prepend (graph->links, $2->front);
Benjamin Otte's avatar
Benjamin Otte committed
817
818
819
820
821
822
823
824
825
826
						  }
						}
						if (!$1->sink_name) {
						  $1->sink = $2->first;
						}
						$2->front = $1;
						$$ = $2;
					      }
	|	PARSE_URL chain		      { $$ = $2;
						if ($$->front) {
Stefan Kost's avatar
Stefan Kost committed
827
						  GstElement *element =
828
829
							  gst_element_make_from_uri (GST_URI_SRC, $1, NULL, NULL);
						  /* FIXME: get and parse error properly */
Benjamin Otte's avatar
Benjamin Otte committed
830
						  if (!element) {
Stefan Kost's avatar
Stefan Kost committed
831
						    SET_ERROR (graph->error, GST_PARSE_ERROR_NO_SUCH_ELEMENT,
832
							    _("no source element for URI \"%s\""), $1);
Benjamin Otte's avatar
Benjamin Otte committed
833
834
						  } else {
						    $$->front->src = element;
835
836
						    graph->links = g_slist_prepend (
							    graph->links, $$->front);
Benjamin Otte's avatar
Benjamin Otte committed
837
838
839
840
						    $$->front = NULL;
						    $$->elements = g_slist_prepend ($$->elements, element);
						  }
						} else {
Stefan Kost's avatar
Stefan Kost committed
841
						  SET_ERROR (graph->error, GST_PARSE_ERROR_LINK,
842
							  _("no element to link URI \"%s\" to"), $1);
Benjamin Otte's avatar
Benjamin Otte committed
843
844
845
846
						}
						g_free ($1);
					      }
	|	link PARSE_URL		      { GstElement *element =
847
848
							  gst_element_make_from_uri (GST_URI_SINK, $2, NULL, NULL);
						/* FIXME: get and parse error properly */
Benjamin Otte's avatar
Benjamin Otte committed
849
						if (!element) {
Stefan Kost's avatar
Stefan Kost committed
850
						  SET_ERROR (graph->error, GST_PARSE_ERROR_NO_SUCH_ELEMENT,
851
							  _("no sink element for URI \"%s\""), $2);
852
853
						  gst_parse_link_free ($1);
						  g_free ($2);
Benjamin Otte's avatar
Benjamin Otte committed
854
855
						  YYERROR;
						} else if ($1->sink_name || $1->sink_pads) {
856
                                                  gst_object_unref (element);
Stefan Kost's avatar
Stefan Kost committed
857
						  SET_ERROR (graph->error, GST_PARSE_ERROR_LINK,
858
							  _("could not link sink element for URI \"%s\""), $2);
859
860
						  gst_parse_link_free ($1);
						  g_free ($2);
Benjamin Otte's avatar
Benjamin Otte committed
861
862
863
864
865
866
867
868
869
870
						  YYERROR;
						} else {
						  $$ = gst_parse_chain_new ();
						  $$->first = $$->last = element;
						  $$->front = $1;
						  $$->front->sink = element;
						  $$->elements = g_slist_prepend (NULL, element);
						}
						g_free ($2);
					      }
871
	;
872
873
graph:		/* NOP */		      { SET_ERROR (graph->error, GST_PARSE_ERROR_EMPTY, _("empty pipeline not allowed"));
						$$ = graph;
874
					      }
875
	|	chain			      { $$ = graph;
876
877
						if ($1->front) {
						  if (!$1->front->src_name) {
878
						    SET_ERROR (graph->error, GST_PARSE_ERROR_LINK, _("link without source element"));
879
880
881
882
883
884
885
886
						    gst_parse_free_link ($1->front);
						  } else {
						    $$->links = g_slist_prepend ($$->links, $1->front);
						  }
						  $1->front = NULL;
						}
						if ($1->back) {
						  if (!$1->back->sink_name) {
887
						    SET_ERROR (graph->error, GST_PARSE_ERROR_LINK, _("link without sink element"));
888
889
890
891
892
893
894
895
896
						    gst_parse_free_link ($1->back);
						  } else {
						    $$->links = g_slist_prepend ($$->links, $1->back);
						  }
						  $1->back = NULL;
						}
						$$->chain = $1;
					      }
	;
897
898
899
900

%%


901
static int
902
yyerror (void *scanner, graph_t *graph, const char *s)
903
{
904
  /* FIXME: This should go into the GError somehow, but how? */
905
  GST_WARNING ("Error during parsing: %s", s);
906
907
908
  return -1;
}

909

910
GstElement *
911
priv_gst_parse_launch (const gchar *str, GError **error, GstParseContext *ctx,
912
    GstParseFlags flags)
913
{
914
915
916
917
918
  graph_t g;
  gchar *dstr;
  GSList *walk;
  GstBin *bin = NULL;
  GstElement *ret;
919
920
  yyscan_t scanner;

921
  g_return_val_if_fail (str != NULL, NULL);
922
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
923

924
925
926
  g.chain = NULL;
  g.links = NULL;
  g.error = error;
927
928
  g.ctx = ctx;
  g.flags = flags;
Stefan Kost's avatar
Stefan Kost committed
929

930
#ifdef __GST_PARSE_TRACE
931
  GST_CAT_DEBUG (GST_CAT_PIPELINE, "TRACE: tracing enabled");
932
933
934
935
  __strings = __chains = __links = 0;
#endif /* __GST_PARSE_TRACE */

  dstr = g_strdup (str);
936
937
  priv_gst_parse_yylex_init (&scanner);
  priv_gst_parse_yy_scan_string (dstr, scanner);
938

939
#ifndef YYDEBUG
940
  yydebug = 1;
941
942
#endif

943
  if (yyparse (scanner, &g) != 0) {
944
945
    SET_ERROR (error, GST_PARSE_ERROR_SYNTAX,
        "Unrecoverable syntax error while parsing pipeline %s", str);
Stefan Kost's avatar
Stefan Kost committed
946

947
    priv_gst_parse_yylex_destroy (scanner);
948
    g_free (dstr);
Stefan Kost's avatar
Stefan Kost committed
949

950
951
    goto error1;
  }
952
  priv_gst_parse_yylex_destroy (scanner);
953
  g_free (dstr);
Stefan Kost's avatar
Stefan Kost committed
954

955
956
957
  GST_CAT_DEBUG (GST_CAT_PIPELINE, "got %u elements and %u links",
      g.chain ? g_slist_length (g.chain->elements) : 0,
      g_slist_length (g.links));
Stefan Kost's avatar
Stefan Kost committed
958

959
960
  if (!g.chain) {
    ret = NULL;
961
  } else if (!g.chain->elements->next) {
Stefan Kost's avatar
Stefan Kost committed
962
    /* only one toplevel element */
963
964
    ret = (GstElement *) g.chain->elements->data;
    g_slist_free (g.chain->elements);
965
966
    if (GST_IS_BIN (ret))
      bin = GST_BIN (ret);
967
    gst_parse_chain_free (g.chain);
Stefan Kost's avatar
Stefan Kost committed
968
  } else {
969
970
971
    /* put all elements in our bin */
    bin = GST_BIN (gst_element_factory_make ("pipeline", NULL));
    g_assert (bin);
Stefan Kost's avatar
Stefan Kost committed
972

973
974
975
976
    for (walk = g.chain->elements; walk; walk = walk->next) {
      if (walk->data != NULL)
        gst_bin_add (bin, GST_ELEMENT (walk->data));
    }
Stefan Kost's avatar
Stefan Kost committed
977

978
979
    g_slist_free (g.chain->elements);
    ret = GST_ELEMENT (bin);
980
    gst_parse_chain_free (g.chain);
981
  }
Stefan Kost's avatar
Stefan Kost committed
982

983
  /* remove links */
984
  for (walk = g.links; walk; walk = walk->next) {
985
986
987
988
989
    link_t *l = (link_t *) walk->data;
    if (!l->src) {
      if (l->src_name) {
        if (bin) {
          l->src = gst_bin_get_by_name_recurse_up (bin, l->src_name);
990
991
          if (l->src)
            gst_object_unref (l->src);
992
993
994
995
996
        } else {
          l->src = strcmp (GST_ELEMENT_NAME (ret), l->src_name) == 0 ? ret : NULL;
        }
      }
      if (!l->src) {
997
998
999
1000
1001
1002
        if (l->src_name) {
          SET_ERROR (error, GST_PARSE_ERROR_NO_SUCH_ELEMENT,
              "No element named \"%s\" - omitting link", l->src_name);
        } else {
          /* probably a missing element which we've handled already */
        }
1003
1004
1005
1006
1007
1008
1009
1010
        gst_parse_free_link (l);
        continue;
      }
    }
    if (!l->sink) {
      if (l->sink_name) {
        if (bin) {
          l->sink = gst_bin_get_by_name_recurse_up (bin, l->sink_name);
1011
1012
          if (l->sink)
            gst_object_unref (l->sink);
1013
1014
1015
1016
1017
        } else {
          l->sink = strcmp (GST_ELEMENT_NAME (ret), l->sink_name) == 0 ? ret : NULL;
        }
      }
      if (!l->sink) {
1018
1019
1020
1021
1022
1023
        if (l->sink_name) {
          SET_ERROR (error, GST_PARSE_ERROR_NO_SUCH_ELEMENT,
              "No element named \"%s\" - omitting link", l->sink_name);
        } else {
          /* probably a missing element which we've handled already */
        }
1024
1025
1026
1027
1028
1029
1030
        gst_parse_free_link (l);
        continue;
      }
    }
    gst_parse_perform_link (l, &g);
  }
  g_slist_free (g.links);
1031

1032
1033
out:
#ifdef __GST_PARSE_TRACE
1034
1035
1036
  GST_CAT_DEBUG (GST_CAT_PIPELINE,
      "TRACE: %u strings, %u chains and %u links left", __strings, __chains,
      __links);
1037
  if (__strings || __chains || __links) {
1038
1039
    g_warning ("TRACE: %u strings, %u chains and %u links left", __strings,
        __chains, __links);
1040
1041
  }
#endif /* __GST_PARSE_TRACE */
1042

1043
  return ret;
Stefan Kost's avatar
Stefan Kost committed
1044

1045
1046
error1:
  if (g.chain) {
1047
    g_slist_foreach (g.chain->elements, (GFunc)gst_object_unref, NULL);
1048
    g_slist_free (g.chain->elements);
1049
    gst_parse_chain_free (g.chain);
1050
  }
1051
1052

  g_slist_foreach (g.links, (GFunc)gst_parse_free_link, NULL);
1053
  g_slist_free (g.links);
Stefan Kost's avatar
Stefan Kost committed
1054

1055
1056
  if (error)
    g_assert (*error);
1057
  ret = NULL;
Stefan Kost's avatar
Stefan Kost committed
1058

1059
  goto out;
1060
}