encodebin.c 26.5 KB
Newer Older
Edward Hervey's avatar
Edward Hervey committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/* GStreamer unit test for gstprofile
 *
 * Copyright (C) <2009> Edward Hervey <edward.hervey@collabora.co.uk>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <gst/pbutils/encoding-profile.h>
#include <gst/check/gstcheck.h>

/* Helper functions to create profiles */

static GstEncodingProfile *
create_ogg_vorbis_profile (guint presence, gchar * preset)
{
  GstEncodingContainerProfile *cprof;
  GstCaps *ogg, *vorbis;

  ogg = gst_caps_new_simple ("application/ogg", NULL);
  cprof =
      gst_encoding_container_profile_new ((gchar *) "myprofile", NULL, ogg,
      NULL);
  gst_caps_unref (ogg);

  vorbis = gst_caps_new_simple ("audio/x-vorbis", NULL);
  fail_unless (gst_encoding_container_profile_add_profile (cprof,
          (GstEncodingProfile *) gst_encoding_audio_profile_new (vorbis, preset,
              NULL, presence)));
  gst_caps_unref (vorbis);

  return (GstEncodingProfile *) cprof;
}

static GstEncodingProfile *
create_ogg_theora_vorbis_profile (guint theorapresence, guint vorbispresence)
{
  GstEncodingContainerProfile *prof;
  GstCaps *ogg, *vorbis, *theora;

  ogg = gst_caps_new_simple ("application/ogg", NULL);
  prof =
      gst_encoding_container_profile_new ((gchar *) "myprofile", NULL, ogg,
      NULL);
  gst_caps_unref (ogg);

  vorbis = gst_caps_new_simple ("audio/x-vorbis", NULL);
  fail_unless (gst_encoding_container_profile_add_profile (prof,
          (GstEncodingProfile *) gst_encoding_audio_profile_new (vorbis, NULL,
              NULL, vorbispresence)));
  gst_caps_unref (vorbis);

  theora = gst_caps_new_simple ("video/x-theora", NULL);
  fail_unless (gst_encoding_container_profile_add_profile (prof,
          (GstEncodingProfile *) gst_encoding_video_profile_new (theora, NULL,
              NULL, theorapresence)));
  gst_caps_unref (theora);

  return (GstEncodingProfile *) prof;
}

78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
static GstEncodingProfile *
create_vorbis_only_profile (void)
{
  GstEncodingProfile *prof;
  GstCaps *vorbis;

  vorbis = gst_caps_new_simple ("audio/x-vorbis", NULL);
  prof =
      (GstEncodingProfile *) gst_encoding_audio_profile_new (vorbis, NULL, NULL,
      0);
  gst_caps_unref (vorbis);

  return prof;
}

Edward Hervey's avatar
Edward Hervey committed
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
GST_START_TEST (test_encodebin_states)
{
  GstElement *ebin;
  GstEncodingProfile *prof, *prof2;
  GstCaps *ogg;
  GstPad *srcpad;
  GstPad *target;

  /* Create an encodebin and check that it correctly changes states
   * according to whether a profile is set or not */

  ebin = gst_element_factory_make ("encodebin", NULL);

  /* Check if the source pad was properly created */
  srcpad = gst_element_get_static_pad (ebin, "src");
  fail_unless (srcpad != NULL);

  /* At this point, the ghostpad has *NO* target */
  target = gst_ghost_pad_get_target (GST_GHOST_PAD (srcpad));
  fail_unless (target == NULL);
113
  gst_object_unref (srcpad);
Edward Hervey's avatar
Edward Hervey committed
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148

  /* No profile,
   * switching to READY should succeed,
   * but switching to PAUSED should fail
   */
  fail_unless_equals_int (gst_element_set_state (ebin, GST_STATE_READY),
      GST_STATE_CHANGE_SUCCESS);
  fail_unless_equals_int (gst_element_set_state (ebin, GST_STATE_PAUSED),
      GST_STATE_CHANGE_FAILURE);

  /* Set a profile on encodebin... */
  ogg = gst_caps_new_simple ("application/ogg", NULL);
  prof = (GstEncodingProfile *) gst_encoding_container_profile_new ((gchar *)
      "myprofile", NULL, ogg, NULL);
  gst_caps_unref (ogg);

  g_object_set (ebin, "profile", prof, NULL);

  /* ... and check the profile has been properly set */
  g_object_get (ebin, "profile", &prof2, NULL);

  fail_unless (gst_encoding_profile_is_equal (prof, prof2));

  gst_encoding_profile_unref (prof);
  gst_encoding_profile_unref (prof2);

  /* Make sure we can go to PAUSED */
  fail_unless_equals_int (gst_element_set_state (ebin, GST_STATE_PAUSED),
      GST_STATE_CHANGE_SUCCESS);

  /* At this point, the source pad *HAS* a target */
  srcpad = gst_element_get_static_pad (ebin, "src");
  fail_unless (srcpad != NULL);
  target = gst_ghost_pad_get_target (GST_GHOST_PAD (srcpad));
  fail_unless (target != NULL);
149
150
  gst_object_unref (target);
  gst_object_unref (srcpad);
Edward Hervey's avatar
Edward Hervey committed
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185


  /* Set back to NULL */
  fail_unless_equals_int (gst_element_set_state (ebin, GST_STATE_NULL),
      GST_STATE_CHANGE_SUCCESS);

  gst_object_unref (ebin);
};

GST_END_TEST;

GST_START_TEST (test_encodebin_sink_pads_static)
{
  GstElement *ebin;
  GstEncodingProfile *prof;
  GstPad *srcpad, *sinkpad;

  /* Create an encodebin and check that it properly creates the sink pads
   * for a single-stream profile with fixed presence */

  ebin = gst_element_factory_make ("encodebin", NULL);

  /* streamprofile that has a forced presence of 1 */
  prof = create_ogg_vorbis_profile (1, NULL);

  g_object_set (ebin, "profile", prof, NULL);

  gst_encoding_profile_unref (prof);

  fail_unless_equals_int (gst_element_set_state (ebin, GST_STATE_PAUSED),
      GST_STATE_CHANGE_SUCCESS);

  /* Check if the source pad was properly created */
  srcpad = gst_element_get_static_pad (ebin, "src");
  fail_unless (srcpad != NULL);
186
  gst_object_unref (srcpad);
Edward Hervey's avatar
Edward Hervey committed
187
188
189
190

  /* Check if the audio sink pad was properly created */
  sinkpad = gst_element_get_static_pad (ebin, "audio_0");
  fail_unless (sinkpad != NULL);
191
  gst_object_unref (sinkpad);
Edward Hervey's avatar
Edward Hervey committed
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253

  /* Set back to NULL */
  fail_unless_equals_int (gst_element_set_state (ebin, GST_STATE_NULL),
      GST_STATE_CHANGE_SUCCESS);

  gst_object_unref (ebin);
};

GST_END_TEST;

GST_START_TEST (test_encodebin_sink_pads_nopreset_static)
{
  GstElement *ebin;
  GstEncodingProfile *prof;

  /* Create an encodebin with a bogus preset and check it fails switching states */

  ebin = gst_element_factory_make ("encodebin", NULL);

  /* streamprofile that has a forced presence of 1 */
  prof = create_ogg_vorbis_profile (1, (gchar *) "nowaythispresetexists");

  g_object_set (ebin, "profile", prof, NULL);

  gst_encoding_profile_unref (prof);

  /* It will go to READY... */
  fail_unless_equals_int (gst_element_set_state (ebin, GST_STATE_READY),
      GST_STATE_CHANGE_SUCCESS);
  /* ... but to not PAUSED */
  fail_unless_equals_int (gst_element_set_state (ebin, GST_STATE_PAUSED),
      GST_STATE_CHANGE_FAILURE);

  gst_element_set_state (ebin, GST_STATE_NULL);

  gst_object_unref (ebin);
};

GST_END_TEST;

GST_START_TEST (test_encodebin_sink_pads_dynamic)
{
  GstElement *ebin;
  GstEncodingProfile *prof;
  GstPad *srcpad, *sinkpad;
  GstCaps *sinkcaps;

  /* Create an encodebin and check that it properly creates the sink pads
   * for a single-stream profile with a unfixed presence */

  ebin = gst_element_factory_make ("encodebin", NULL);

  /* streamprofile that has non-forced presence */
  prof = create_ogg_vorbis_profile (0, NULL);

  g_object_set (ebin, "profile", prof, NULL);

  gst_encoding_profile_unref (prof);

  /* Check if the source pad was properly created */
  srcpad = gst_element_get_static_pad (ebin, "src");
  fail_unless (srcpad != NULL);
254
  gst_object_unref (srcpad);
Edward Hervey's avatar
Edward Hervey committed
255
256
257
258
259

  /* Check if the audio sink pad can be requested */
  sinkpad = gst_element_get_request_pad (ebin, "audio_0");
  fail_unless (sinkpad != NULL);
  gst_element_release_request_pad (ebin, sinkpad);
260
  gst_object_unref (sinkpad);
Edward Hervey's avatar
Edward Hervey committed
261
262
263
264
265
266
267
268
  sinkpad = NULL;

  /* Check again with the 'request-pad' signal */
  sinkcaps = gst_caps_new_simple ("audio/x-raw-int", NULL);
  g_signal_emit_by_name (ebin, "request-pad", sinkcaps, &sinkpad);
  gst_caps_unref (sinkcaps);
  fail_unless (sinkpad != NULL);
  gst_element_release_request_pad (ebin, sinkpad);
269
270
  gst_object_unref (sinkpad);
  sinkpad = NULL;
Edward Hervey's avatar
Edward Hervey committed
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306

  fail_unless_equals_int (gst_element_set_state (ebin, GST_STATE_PAUSED),
      GST_STATE_CHANGE_SUCCESS);

  /* Set back to NULL */
  fail_unless_equals_int (gst_element_set_state (ebin, GST_STATE_NULL),
      GST_STATE_CHANGE_SUCCESS);

  gst_object_unref (ebin);
};

GST_END_TEST;

GST_START_TEST (test_encodebin_sink_pads_multiple_static)
{
  GstElement *ebin;
  GstEncodingProfile *prof;
  GstPad *srcpad, *sinkpadvorbis, *sinkpadtheora;

  /* Create an encodebin and check that it properly creates the sink pads */

  ebin = gst_element_factory_make ("encodebin", NULL);

  /* First try is with a streamprofile that has a forced presence of 1 */
  prof = create_ogg_theora_vorbis_profile (1, 1);

  g_object_set (ebin, "profile", prof, NULL);

  gst_encoding_profile_unref (prof);

  fail_unless_equals_int (gst_element_set_state (ebin, GST_STATE_PAUSED),
      GST_STATE_CHANGE_SUCCESS);

  /* Check if the source pad was properly created */
  srcpad = gst_element_get_static_pad (ebin, "src");
  fail_unless (srcpad != NULL);
307
  gst_object_unref (srcpad);
Edward Hervey's avatar
Edward Hervey committed
308
309
310
311

  /* Check if the audio sink pad was properly created */
  sinkpadvorbis = gst_element_get_static_pad (ebin, "audio_0");
  fail_unless (sinkpadvorbis != NULL);
312
  gst_object_unref (sinkpadvorbis);
Edward Hervey's avatar
Edward Hervey committed
313
314
315
316

  /* Check if the video sink pad was properly created */
  sinkpadtheora = gst_element_get_static_pad (ebin, "video_1");
  fail_unless (sinkpadtheora != NULL);
317
  gst_object_unref (sinkpadtheora);
Edward Hervey's avatar
Edward Hervey committed
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348

  /* Set back to NULL */
  fail_unless_equals_int (gst_element_set_state (ebin, GST_STATE_NULL),
      GST_STATE_CHANGE_SUCCESS);

  gst_object_unref (ebin);
};

GST_END_TEST;

GST_START_TEST (test_encodebin_sink_pads_multiple_dynamic)
{
  GstElement *ebin;
  GstEncodingProfile *prof;
  GstPad *srcpad, *sinkpadvorbis, *sinkpadtheora;

  /* Create an encodebin and check that it properly creates the sink pads
   * for a multiple-stream with unfixed presence */

  ebin = gst_element_factory_make ("encodebin", NULL);

  /* multi-stream profile that has non-forced presence */
  prof = create_ogg_theora_vorbis_profile (0, 0);

  g_object_set (ebin, "profile", prof, NULL);

  gst_encoding_profile_unref (prof);

  /* Check if the source pad was properly created */
  srcpad = gst_element_get_static_pad (ebin, "src");
  fail_unless (srcpad != NULL);
349
  gst_object_unref (srcpad);
Edward Hervey's avatar
Edward Hervey committed
350
351
352
353
354
355
356
357
358
359
360
361

  /* Check if the audio sink pad was properly created */
  sinkpadvorbis = gst_element_get_request_pad (ebin, "audio_0");
  fail_unless (sinkpadvorbis != NULL);

  /* Check if the video sink pad was properly created */
  sinkpadtheora = gst_element_get_request_pad (ebin, "video_1");
  fail_unless (sinkpadtheora != NULL);

  fail_unless_equals_int (gst_element_set_state (ebin, GST_STATE_PAUSED),
      GST_STATE_CHANGE_SUCCESS);

362
363
364
365
  /* Set back to NULL */
  fail_unless_equals_int (gst_element_set_state (ebin, GST_STATE_NULL),
      GST_STATE_CHANGE_SUCCESS);

366
367
368
369
370
  gst_element_release_request_pad (GST_ELEMENT (ebin), sinkpadvorbis);
  gst_object_unref (sinkpadvorbis);
  gst_element_release_request_pad (GST_ELEMENT (ebin), sinkpadtheora);
  gst_object_unref (sinkpadtheora);

Edward Hervey's avatar
Edward Hervey committed
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
  gst_object_unref (ebin);
};

GST_END_TEST;

GST_START_TEST (test_encodebin_sink_pads_dynamic_encoder)
{
  GstElement *ebin;
  GstEncodingProfile *prof;
  GstPad *srcpad, *sinkpad = NULL;
  GstCaps *vorbiscaps;

  /* Create an encodebin and check that it properly creates the sink pads
   * for a single-stream profile with a unfixed presence */

  ebin = gst_element_factory_make ("encodebin", NULL);

  /* streamprofile that has non-forced presence */
  prof = create_ogg_vorbis_profile (0, NULL);

  g_object_set (ebin, "profile", prof, NULL);

  gst_encoding_profile_unref (prof);

  /* Check if the source pad was properly created */
  srcpad = gst_element_get_static_pad (ebin, "src");
  fail_unless (srcpad != NULL);
398
  gst_object_unref (srcpad);
Edward Hervey's avatar
Edward Hervey committed
399
400
401
402
403
404
405

  /* Check if the audio sink pad was properly created */
  vorbiscaps = gst_caps_from_string ("audio/x-vorbis,channels=2,rate=44100");
  g_signal_emit_by_name (ebin, "request-pad", vorbiscaps, &sinkpad);
  gst_caps_unref (vorbiscaps);
  fail_unless (sinkpad != NULL);
  gst_element_release_request_pad (ebin, sinkpad);
406
  gst_object_unref (sinkpad);
Edward Hervey's avatar
Edward Hervey committed
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471

  fail_unless_equals_int (gst_element_set_state (ebin, GST_STATE_PAUSED),
      GST_STATE_CHANGE_SUCCESS);

  /* Set back to NULL */
  fail_unless_equals_int (gst_element_set_state (ebin, GST_STATE_NULL),
      GST_STATE_CHANGE_SUCCESS);

  gst_object_unref (ebin);
};

GST_END_TEST;

GST_START_TEST (test_encodebin_render_audio_static)
{
  GstElement *ebin, *pipeline, *audiotestsrc, *fakesink;
  GstEncodingProfile *prof;
  GstBus *bus;
  gboolean done = FALSE;

  /* Create an encodebin and render 5s of vorbis/ogg */

  pipeline = gst_pipeline_new ("encodebin-pipeline");
  bus = gst_pipeline_get_bus ((GstPipeline *) pipeline);
  audiotestsrc = gst_element_factory_make ("audiotestsrc", NULL);
  g_object_set (audiotestsrc, "num-buffers", 10, NULL);
  fakesink = gst_element_factory_make ("fakesink", NULL);

  ebin = gst_element_factory_make ("encodebin", NULL);

  prof = create_ogg_vorbis_profile (1, NULL);
  g_object_set (ebin, "profile", prof, NULL);
  gst_encoding_profile_unref (prof);

  gst_bin_add_many ((GstBin *) pipeline, audiotestsrc, ebin, fakesink, NULL);

  fail_unless (gst_element_link_many (audiotestsrc, ebin, fakesink, NULL));

  fail_unless_equals_int (gst_element_set_state (pipeline, GST_STATE_PLAYING),
      GST_STATE_CHANGE_ASYNC);

  while (!done) {
    GstMessage *msg;

    /* poll the bus until we get EOS without any errors */
    msg = gst_bus_timed_pop (bus, GST_SECOND / 10);
    if (msg) {
      switch (GST_MESSAGE_TYPE (msg)) {
        case GST_MESSAGE_ERROR:
          fail ("GST_MESSAGE_ERROR");
          break;
        case GST_MESSAGE_EOS:
          done = TRUE;
          break;
        default:
          break;
      }
      gst_message_unref (msg);
    }
  }

  /* Set back to NULL */
  fail_unless_equals_int (gst_element_set_state (pipeline, GST_STATE_NULL),
      GST_STATE_CHANGE_SUCCESS);

472
  gst_object_unref (bus);
Edward Hervey's avatar
Edward Hervey committed
473
474
475
476
477
478

  gst_object_unref (pipeline);
}

GST_END_TEST;

479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
GST_START_TEST (test_encodebin_render_audio_only_static)
{
  GstElement *ebin, *pipeline, *audiotestsrc, *fakesink;
  GstEncodingProfile *prof;
  GstBus *bus;
  gboolean done = FALSE;
  GstPad *sinkpad;
  GstCaps *sinkcaps;

  /* Create an encodebin and render 5s of vorbis only */

  pipeline = gst_pipeline_new ("encodebin-pipeline");
  bus = gst_pipeline_get_bus ((GstPipeline *) pipeline);
  audiotestsrc = gst_element_factory_make ("audiotestsrc", NULL);
  g_object_set (audiotestsrc, "num-buffers", 10, NULL);
  fakesink = gst_element_factory_make ("fakesink", NULL);

  ebin = gst_element_factory_make ("encodebin", NULL);

  prof = create_vorbis_only_profile ();
  g_object_set (ebin, "profile", prof, NULL);
  gst_encoding_profile_unref (prof);

  gst_bin_add_many ((GstBin *) pipeline, audiotestsrc, ebin, fakesink, NULL);

  fail_unless (gst_element_link_many (audiotestsrc, ebin, fakesink, NULL));

  /* Requesting a new pad should fail */
507
508
  ASSERT_CRITICAL (gst_element_get_request_pad (ebin, "audio_0"));

509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
  sinkcaps = gst_caps_new_simple ("audio/x-raw-int", NULL);
  g_signal_emit_by_name (ebin, "request-pad", sinkcaps, &sinkpad);
  gst_caps_unref (sinkcaps);
  fail_if (sinkpad != NULL);

  fail_unless_equals_int (gst_element_set_state (pipeline, GST_STATE_PLAYING),
      GST_STATE_CHANGE_ASYNC);

  while (!done) {
    GstMessage *msg;

    /* poll the bus until we get EOS without any errors */
    msg = gst_bus_timed_pop (bus, GST_SECOND / 10);
    if (msg) {
      switch (GST_MESSAGE_TYPE (msg)) {
        case GST_MESSAGE_ERROR:
          fail ("GST_MESSAGE_ERROR");
          break;
        case GST_MESSAGE_EOS:
          done = TRUE;
          break;
        default:
          break;
      }
      gst_message_unref (msg);
    }
  }

  /* Set back to NULL */
  fail_unless_equals_int (gst_element_set_state (pipeline, GST_STATE_NULL),
      GST_STATE_CHANGE_SUCCESS);

541
  gst_object_unref (bus);
542
543
544
545
546
547

  gst_object_unref (pipeline);
}

GST_END_TEST;

Edward Hervey's avatar
Edward Hervey committed
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
GST_START_TEST (test_encodebin_render_audio_dynamic)
{
  GstElement *ebin, *pipeline, *audiotestsrc, *fakesink;
  GstEncodingProfile *prof;
  GstBus *bus;
  GstPad *sinkpad, *srcpad;
  gboolean done = FALSE;

  /* Create an encodebin and render 5s of vorbis/ogg */

  pipeline = gst_pipeline_new ("encodebin-pipeline");
  bus = gst_pipeline_get_bus ((GstPipeline *) pipeline);
  audiotestsrc = gst_element_factory_make ("audiotestsrc", NULL);
  g_object_set (audiotestsrc, "num-buffers", 10, NULL);
  fakesink = gst_element_factory_make ("fakesink", NULL);

  ebin = gst_element_factory_make ("encodebin", NULL);

  prof = create_ogg_vorbis_profile (0, NULL);
  g_object_set (ebin, "profile", prof, NULL);
  gst_encoding_profile_unref (prof);

  gst_bin_add_many ((GstBin *) pipeline, audiotestsrc, ebin, fakesink, NULL);

  srcpad = gst_element_get_static_pad (audiotestsrc, "src");
  fail_unless (srcpad != NULL);

  sinkpad = gst_element_get_request_pad (ebin, "audio_0");
  fail_unless (sinkpad != NULL);

  fail_unless_equals_int (gst_pad_link (srcpad, sinkpad), GST_PAD_LINK_OK);

580
  gst_object_unref (srcpad);
Edward Hervey's avatar
Edward Hervey committed
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610

  fail_unless (gst_element_link (ebin, fakesink));

  fail_unless_equals_int (gst_element_set_state (pipeline, GST_STATE_PLAYING),
      GST_STATE_CHANGE_ASYNC);

  while (!done) {
    GstMessage *msg;

    /* poll the bus until we get EOS without any errors */
    msg = gst_bus_timed_pop (bus, GST_SECOND / 10);
    if (msg) {
      switch (GST_MESSAGE_TYPE (msg)) {
        case GST_MESSAGE_ERROR:
          fail ("GST_MESSAGE_ERROR");
          break;
        case GST_MESSAGE_EOS:
          done = TRUE;
          break;
        default:
          break;
      }
      gst_message_unref (msg);
    }
  }

  /* Set back to NULL */
  fail_unless_equals_int (gst_element_set_state (pipeline, GST_STATE_NULL),
      GST_STATE_CHANGE_SUCCESS);

611
612
613
  gst_element_release_request_pad (GST_ELEMENT (ebin), sinkpad);
  gst_object_unref (sinkpad);

614
  gst_object_unref (bus);
Edward Hervey's avatar
Edward Hervey committed
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676

  gst_object_unref (pipeline);
}

GST_END_TEST;

GST_START_TEST (test_encodebin_render_audio_video_static)
{
  GstElement *ebin, *pipeline, *audiotestsrc, *videotestsrc, *fakesink;
  GstEncodingProfile *prof;
  GstBus *bus;
  gboolean done = FALSE;

  /* Create an encodebin and render 5s of vorbis/ogg */

  pipeline = gst_pipeline_new ("encodebin-pipeline");
  bus = gst_pipeline_get_bus ((GstPipeline *) pipeline);
  audiotestsrc = gst_element_factory_make ("audiotestsrc", NULL);
  g_object_set (audiotestsrc, "num-buffers", 10, NULL);
  videotestsrc = gst_element_factory_make ("videotestsrc", NULL);
  g_object_set (videotestsrc, "num-buffers", 5, NULL);
  fakesink = gst_element_factory_make ("fakesink", NULL);

  ebin = gst_element_factory_make ("encodebin", NULL);

  prof = create_ogg_theora_vorbis_profile (1, 1);
  g_object_set (ebin, "profile", prof, NULL);
  gst_encoding_profile_unref (prof);

  gst_bin_add_many ((GstBin *) pipeline, audiotestsrc, videotestsrc, ebin,
      fakesink, NULL);

  fail_unless (gst_element_link (videotestsrc, ebin));
  fail_unless (gst_element_link_many (audiotestsrc, ebin, fakesink, NULL));

  fail_unless_equals_int (gst_element_set_state (pipeline, GST_STATE_PLAYING),
      GST_STATE_CHANGE_ASYNC);

  while (!done) {
    GstMessage *msg;

    /* poll the bus until we get EOS without any errors */
    msg = gst_bus_timed_pop (bus, GST_SECOND / 10);
    if (msg) {
      switch (GST_MESSAGE_TYPE (msg)) {
        case GST_MESSAGE_ERROR:
          fail ("GST_MESSAGE_ERROR");
          break;
        case GST_MESSAGE_EOS:
          done = TRUE;
          break;
        default:
          break;
      }
      gst_message_unref (msg);
    }
  }

  /* Set back to NULL */
  fail_unless_equals_int (gst_element_set_state (pipeline, GST_STATE_NULL),
      GST_STATE_CHANGE_SUCCESS);

677
  gst_object_unref (bus);
Edward Hervey's avatar
Edward Hervey committed
678
679
680
681
682
683
684
685
686
687
688
689

  gst_object_unref (pipeline);
}

GST_END_TEST;

GST_START_TEST (test_encodebin_render_audio_video_dynamic)
{
  GstElement *ebin, *pipeline, *audiotestsrc, *videotestsrc, *fakesink;
  GstEncodingProfile *prof;
  GstBus *bus;
  gboolean done = FALSE;
690
  GstPad *sinkpad1, *sinkpad2, *srcpad;
Edward Hervey's avatar
Edward Hervey committed
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713

  /* Create an encodebin and render 5s of vorbis/ogg */

  pipeline = gst_pipeline_new ("encodebin-pipeline");
  bus = gst_pipeline_get_bus ((GstPipeline *) pipeline);
  audiotestsrc = gst_element_factory_make ("audiotestsrc", NULL);
  g_object_set (audiotestsrc, "num-buffers", 10, NULL);
  videotestsrc = gst_element_factory_make ("videotestsrc", NULL);
  g_object_set (videotestsrc, "num-buffers", 5, NULL);
  fakesink = gst_element_factory_make ("fakesink", NULL);

  ebin = gst_element_factory_make ("encodebin", NULL);

  prof = create_ogg_theora_vorbis_profile (0, 0);
  g_object_set (ebin, "profile", prof, NULL);
  gst_encoding_profile_unref (prof);

  gst_bin_add_many ((GstBin *) pipeline, audiotestsrc, videotestsrc, ebin,
      fakesink, NULL);

  fail_unless (gst_element_link (ebin, fakesink));

  srcpad = gst_element_get_static_pad (audiotestsrc, "src");
714
  sinkpad1 = gst_element_get_request_pad (ebin, "audio_0");
Edward Hervey's avatar
Edward Hervey committed
715
  fail_unless (srcpad != NULL);
716
717
718
  fail_unless (sinkpad1 != NULL);
  fail_unless_equals_int (gst_pad_link (srcpad, sinkpad1), GST_PAD_LINK_OK);
  gst_object_unref (srcpad);
Edward Hervey's avatar
Edward Hervey committed
719
720

  srcpad = gst_element_get_static_pad (videotestsrc, "src");
721
722
723
  sinkpad2 = gst_element_get_request_pad (ebin, "video_1");
  fail_unless_equals_int (gst_pad_link (srcpad, sinkpad2), GST_PAD_LINK_OK);
  gst_object_unref (srcpad);
Edward Hervey's avatar
Edward Hervey committed
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747

  fail_unless_equals_int (gst_element_set_state (pipeline, GST_STATE_PLAYING),
      GST_STATE_CHANGE_ASYNC);

  while (!done) {
    GstMessage *msg;

    /* poll the bus until we get EOS without any errors */
    msg = gst_bus_timed_pop (bus, GST_SECOND / 10);
    if (msg) {
      switch (GST_MESSAGE_TYPE (msg)) {
        case GST_MESSAGE_ERROR:
          fail ("GST_MESSAGE_ERROR");
          break;
        case GST_MESSAGE_EOS:
          done = TRUE;
          break;
        default:
          break;
      }
      gst_message_unref (msg);
    }
  }

748
749
750
751
  /* Set back to NULL */
  fail_unless_equals_int (gst_element_set_state (pipeline, GST_STATE_NULL),
      GST_STATE_CHANGE_SUCCESS);

752
753
754
755
756
757
  gst_element_release_request_pad (GST_ELEMENT (ebin), sinkpad1);
  gst_object_unref (sinkpad1);
  gst_element_release_request_pad (GST_ELEMENT (ebin), sinkpad2);
  gst_object_unref (sinkpad2);

  gst_object_unref (bus);
Edward Hervey's avatar
Edward Hervey committed
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813

  gst_object_unref (pipeline);
}

GST_END_TEST;

GST_START_TEST (test_encodebin_impossible_element_combination)
{
  GstElement *ebin;
  GstEncodingProfile *prof;
  GstCaps *ogg, *x264;

  ebin = gst_element_factory_make ("x264enc", NULL);
  if (ebin == NULL) {
    GST_DEBUG ("No available h264 encoder, skipping test");
    return;
  }
  gst_object_unref (ebin);

  /* Make sure that impossible combinations of encoders and muxer
   * properly fail. In this case we try putting h264 in ogg.
   *
   * To properly test we abort early, we use a presence of zero for the
   * h264 stream profile. */

  ebin = gst_element_factory_make ("encodebin", NULL);

  ogg = gst_caps_new_simple ("application/ogg", NULL);
  prof = (GstEncodingProfile *) gst_encoding_container_profile_new ((gchar *)
      "myprofile", NULL, ogg, NULL);
  gst_caps_unref (ogg);

  x264 = gst_caps_new_simple ("video/x-h264", NULL);
  fail_unless (gst_encoding_container_profile_add_profile
      (GST_ENCODING_CONTAINER_PROFILE (prof),
          (GstEncodingProfile *) gst_encoding_video_profile_new (x264, NULL,
              NULL, 0)));
  gst_caps_unref (x264);

  g_object_set (ebin, "profile", prof, NULL);
  gst_encoding_profile_unref (prof);

  /* It will go to READY... */
  fail_unless_equals_int (gst_element_set_state (ebin, GST_STATE_READY),
      GST_STATE_CHANGE_SUCCESS);
  /* ... but to not PAUSED */
  fail_unless_equals_int (gst_element_set_state (ebin, GST_STATE_PAUSED),
      GST_STATE_CHANGE_FAILURE);

  gst_element_set_state (ebin, GST_STATE_NULL);

  gst_object_unref (ebin);
};

GST_END_TEST;

814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
static void
_test_encodebin_reuse (GstEncodingProfile * prof1, GstEncodingProfile * prof2)
{
  GstElement *ebin;

  ebin = gst_element_factory_make ("encodebin", NULL);

  /* Set a profile on encodebin... */
  if (prof1)
    g_object_set (ebin, "profile", prof1, NULL);

  /* Make sure we can go to PAUSED */
  fail_unless_equals_int (gst_element_set_state (ebin, GST_STATE_PAUSED),
      GST_STATE_CHANGE_SUCCESS);

  /* Set back to NULL */
  fail_unless_equals_int (gst_element_set_state (ebin, GST_STATE_NULL),
      GST_STATE_CHANGE_SUCCESS);

  if (prof2)
    g_object_set (ebin, "profile", prof2, NULL);

  /* Make sure we can go to PLAYING */
  fail_unless_equals_int (gst_element_set_state (ebin, GST_STATE_PAUSED),
      GST_STATE_CHANGE_SUCCESS);

  /* Set back to NULL */
  fail_unless_equals_int (gst_element_set_state (ebin, GST_STATE_NULL),
      GST_STATE_CHANGE_SUCCESS);

  gst_object_unref (ebin);
}

GST_START_TEST (test_encodebin_reuse)
{
  GstEncodingProfile *prof1;
  GstEncodingProfile *prof2;
  GstEncodingProfile *prof3;
  GstCaps *caps;

  caps = gst_caps_new_simple ("application/ogg", NULL);
  prof1 = (GstEncodingProfile *) gst_encoding_container_profile_new ((gchar *)
      "myprofile", NULL, caps, NULL);
  gst_caps_unref (caps);

  prof2 = create_ogg_theora_vorbis_profile (1, 1);
  prof3 = create_vorbis_only_profile ();

  _test_encodebin_reuse (prof1, NULL);
  _test_encodebin_reuse (prof1, prof1);

  _test_encodebin_reuse (prof1, prof2);

  _test_encodebin_reuse (prof2, prof3);

  gst_encoding_profile_unref (prof1);
870
871
  gst_encoding_profile_unref (prof2);
  gst_encoding_profile_unref (prof3);
872
873
874
875
};

GST_END_TEST;

Edward Hervey's avatar
Edward Hervey committed
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891

static Suite *
encodebin_suite (void)
{
  Suite *s = suite_create ("encodebin element");
  TCase *tc_chain = tcase_create ("general");

  suite_add_tcase (s, tc_chain);
  tcase_add_test (tc_chain, test_encodebin_states);
  tcase_add_test (tc_chain, test_encodebin_sink_pads_static);
  tcase_add_test (tc_chain, test_encodebin_sink_pads_nopreset_static);
  tcase_add_test (tc_chain, test_encodebin_sink_pads_dynamic);
  tcase_add_test (tc_chain, test_encodebin_sink_pads_multiple_static);
  tcase_add_test (tc_chain, test_encodebin_sink_pads_multiple_dynamic);
  tcase_add_test (tc_chain, test_encodebin_sink_pads_dynamic_encoder);
  tcase_add_test (tc_chain, test_encodebin_render_audio_static);
892
  tcase_add_test (tc_chain, test_encodebin_render_audio_only_static);
Edward Hervey's avatar
Edward Hervey committed
893
894
895
896
  tcase_add_test (tc_chain, test_encodebin_render_audio_dynamic);
  tcase_add_test (tc_chain, test_encodebin_render_audio_video_static);
  tcase_add_test (tc_chain, test_encodebin_render_audio_video_dynamic);
  tcase_add_test (tc_chain, test_encodebin_impossible_element_combination);
897
  tcase_add_test (tc_chain, test_encodebin_reuse);
Edward Hervey's avatar
Edward Hervey committed
898
899
900
901
902

  return s;
}

GST_CHECK_MAIN (encodebin);