1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 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
 507
 508
 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
 541
 542
 543
 544
 545
 546
 547
 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
 580
 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
 611
 612
 613
 614
 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
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 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
 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
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
// Take a look at the license at the top of the repository in the LICENSE file.

use crate::TestClock;
use glib::translate::*;
use gst::prelude::*;
use std::marker::PhantomData;
use std::mem;
use std::ops;
use std::path;
use std::ptr;

/// [`crate::Harness`] is meant to make writing unit test for GStreamer much easier.
/// It can be thought of as a way of treating a [`crate::gst::Element`] as a black box,
/// deterministically feeding it data, and controlling what data it outputs.
///
/// The basic structure of [`crate::Harness`] is two "floating" `GstPads` that connect
/// to the harnessed [`crate::gst::Element`] src and sink `GstPads` like so:
///
///
/// ```C
///   #include <gst/gst.h>
///   #include <gst/check/gstharness.h>
///   GstHarness *h;
///   GstBuffer *in_buf;
///   GstBuffer *out_buf;
///
///   // attach the harness to the src and sink pad of GstQueue
///   h = gst_harness_new ("queue");
///
///   // we must specify a caps before pushing buffers
///   gst_harness_set_src_caps_str (h, "mycaps");
///
///   // create a buffer of size 42
///   in_buf = gst_harness_create_buffer (h, 42);
///
///   // push the buffer into the queue
///   gst_harness_push (h, in_buf);
///
///   // pull the buffer from the queue
///   out_buf = gst_harness_pull (h);
///
///   // validate the buffer in is the same as buffer out
///   fail_unless (in_buf == out_buf);
///
///   // cleanup
///   gst_buffer_unref (out_buf);
///   gst_harness_teardown (h);
///
///   ]|
///
/// Another main feature of the #GstHarness is its integration with the
/// #GstTestClock. Operating the #GstTestClock can be very challenging, but
/// #GstHarness simplifies some of the most desired actions a lot, like wanting
/// to manually advance the clock while at the same time releasing a #GstClockID
/// that is waiting, with functions like gst_harness_crank_single_clock_wait().
///
/// #GstHarness also supports sub-harnesses, as a way of generating and
/// validating data. A sub-harness is another #GstHarness that is managed by
/// the "parent" harness, and can either be created by using the standard
/// gst_harness_new type functions directly on the (GstHarness *)->src_harness,
/// or using the much more convenient gst_harness_add_src() or
/// gst_harness_add_sink_parse(). If you have a decoder-element you want to test,
/// (like vp8dec) it can be very useful to add a src-harness with both a
/// src-element (videotestsrc) and an encoder (vp8enc) to feed the decoder data
/// with different configurations, by simply doing:
///
/// |[<!-- language="C" -->
///   GstHarness * h = gst_harness_new (h, "vp8dec");
///   gst_harness_add_src_parse (h, "videotestsrc is-live=1 ! vp8enc", TRUE);
/// ```
///
/// and then feeding it data with:
///
///
/// ```C
/// gst_harness_push_from_src (h);
/// ```
#[derive(Debug)]
pub struct Harness(ptr::NonNull<ffi::GstHarness>);

impl Drop for Harness {
    fn drop(&mut self) {
        unsafe {
            ffi::gst_harness_teardown(self.0.as_ptr());
        }
    }
}

unsafe impl Send for Harness {}
unsafe impl Sync for Harness {}

impl Harness {
    /// Adds a [`crate::gst::Element`] to an empty [`crate::Harness`]
    ///
    /// MT safe.
    /// ## `element`
    /// a [`crate::gst::Element`] to add to the harness (transfer none)
    /// ## `hsrc`
    /// a [`crate::gst::StaticPadTemplate`] describing the harness srcpad.
    /// [`None`] will not create a harness srcpad.
    /// ## `element_sinkpad_name`
    /// a `gchar` with the name of the element
    /// sinkpad that is then linked to the harness srcpad. Can be a static or request
    /// or a sometimes pad that has been added. [`None`] will not get/request a sinkpad
    /// from the element. (Like if the element is a src.)
    /// ## `hsink`
    /// a [`crate::gst::StaticPadTemplate`] describing the harness sinkpad.
    /// [`None`] will not create a harness sinkpad.
    /// ## `element_srcpad_name`
    /// a `gchar` with the name of the element
    /// srcpad that is then linked to the harness sinkpad, similar to the
    /// `element_sinkpad_name`.
    pub fn add_element_full<P: IsA<gst::Element>>(
        &mut self,
        element: &P,
        hsrc: Option<&gst::StaticPadTemplate>,
        element_sinkpad_name: Option<&str>,
        hsink: Option<&gst::StaticPadTemplate>,
        element_srcpad_name: Option<&str>,
    ) {
        let element_sinkpad_name = element_sinkpad_name.to_glib_none();
        let element_srcpad_name = element_srcpad_name.to_glib_none();
        unsafe {
            ffi::gst_harness_add_element_full(
                self.0.as_ptr(),
                element.as_ref().to_glib_none().0,
                hsrc.to_glib_none().0 as *mut _,
                element_sinkpad_name.0,
                hsink.to_glib_none().0 as *mut _,
                element_srcpad_name.0,
            );
        }
    }

    /// Links the specified [`crate::gst::Pad`] the [`crate::Harness`] (XXX: @-reference does not belong to Harness!) srcpad.
    ///
    /// MT safe.
    /// ## `sinkpad`
    /// a [`crate::gst::Pad`] to link to the harness srcpad
    pub fn add_element_sink_pad<P: IsA<gst::Pad>>(&mut self, sinkpad: &P) {
        unsafe {
            ffi::gst_harness_add_element_sink_pad(
                self.0.as_ptr(),
                sinkpad.as_ref().to_glib_none().0,
            );
        }
    }

    /// Links the specified [`crate::gst::Pad`] the [`crate::Harness`] (XXX: @-reference does not belong to Harness!) sinkpad. This can be useful if
    /// perhaps the srcpad did not exist at the time of creating the harness,
    /// like a demuxer that provides a sometimes-pad after receiving data.
    ///
    /// MT safe.
    /// ## `srcpad`
    /// a [`crate::gst::Pad`] to link to the harness sinkpad
    pub fn add_element_src_pad<P: IsA<gst::Pad>>(&mut self, srcpad: &P) {
        unsafe {
            ffi::gst_harness_add_element_src_pad(self.0.as_ptr(), srcpad.as_ref().to_glib_none().0);
        }
    }

    /// Parses the `launchline` and puts that in a [`crate::gst::Bin`],
    /// and then attches the supplied [`crate::Harness`] to the bin.
    ///
    /// MT safe.
    /// ## `launchline`
    /// a `gchar` describing a gst-launch type line
    pub fn add_parse(&mut self, launchline: &str) {
        unsafe {
            ffi::gst_harness_add_parse(self.0.as_ptr(), launchline.to_glib_none().0);
        }
    }

    /// A convenience function to allows you to call gst_pad_add_probe on a
    /// [`crate::gst::Pad`] of a [`crate::gst::Element`] that are residing inside the [`crate::Harness`],
    /// by using normal gst_pad_add_probe syntax
    ///
    /// MT safe.
    /// ## `element_name`
    /// a `gchar` with a [`crate::gst::ElementFactory`] name
    /// ## `pad_name`
    /// a `gchar` with the name of the pad to attach the probe to
    /// ## `mask`
    /// a [`crate::gst::PadProbeType`] (see gst_pad_add_probe)
    /// ## `callback`
    /// a `GstPadProbeCallback` (see gst_pad_add_probe)
    /// ## `user_data`
    /// a `gpointer` (see gst_pad_add_probe)
    /// ## `destroy_data`
    /// a `GDestroyNotify` (see gst_pad_add_probe)
    pub fn add_probe<F>(
        &mut self,
        element_name: &str,
        pad_name: &str,
        mask: gst::PadProbeType,
        func: F,
    ) where
        F: Fn(&gst::Pad, &mut gst::PadProbeInfo) -> gst::PadProbeReturn + Send + Sync + 'static,
    {
        // Reimplementation of the C code so we don't have to duplicate all the callback code

        let element = self.find_element(element_name).expect("Element not found");
        let pad = element.static_pad(pad_name).expect("Pad not found");
        pad.add_probe(mask, func);
    }

    /// Add api with params as one of the supported metadata API to propose when
    /// receiving an allocation query.
    ///
    /// MT safe.
    /// ## `api`
    /// a metadata API
    /// ## `params`
    /// API specific parameters
    #[cfg(any(feature = "v1_16", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v1_16")))]
    pub fn add_propose_allocation_meta(
        &mut self,
        api: glib::types::Type,
        params: Option<&gst::StructureRef>,
    ) {
        unsafe {
            let params = params.map(|p| p.as_ptr()).unwrap_or(ptr::null_mut());
            ffi::gst_harness_add_propose_allocation_meta(self.0.as_ptr(), api.into_glib(), params);
        }
    }

    /// Similar to gst_harness_add_sink_harness, this is a convenience to
    /// directly create a sink-harness using the `sink_element_name` name specified.
    ///
    /// MT safe.
    /// ## `sink_element_name`
    /// a `gchar` with the name of a [`crate::gst::Element`]
    pub fn add_sink(&mut self, sink_element_name: &str) {
        unsafe {
            ffi::gst_harness_add_sink(self.0.as_ptr(), sink_element_name.to_glib_none().0);
        }
    }

    /// Similar to gst_harness_add_src, this allows you to send the data coming out
    /// of your harnessed [`crate::gst::Element`] to a sink-element, allowing to test different
    /// responses the element output might create in sink elements. An example might
    /// be an existing sink providing some analytical data on the input it receives that
    /// can be useful to your testing. If the goal is to test a sink-element itself,
    /// this is better achieved using gst_harness_new directly on the sink.
    ///
    /// If a sink-harness already exists it will be replaced.
    ///
    /// MT safe.
    /// ## `sink_harness`
    /// a [`crate::Harness`] to be added as a sink-harness.
    pub fn add_sink_harness(&mut self, sink_harness: Harness) {
        unsafe {
            let sink_harness = mem::ManuallyDrop::new(sink_harness);
            ffi::gst_harness_add_sink_harness(self.0.as_ptr(), sink_harness.0.as_ptr());
        }
    }

    /// Similar to gst_harness_add_sink, this allows you to specify a launch-line
    /// instead of just an element name. See gst_harness_add_src_parse for details.
    ///
    /// MT safe.
    /// ## `launchline`
    /// a `gchar` with the name of a [`crate::gst::Element`]
    pub fn add_sink_parse(&mut self, launchline: &str) {
        unsafe {
            ffi::gst_harness_add_sink_parse(self.0.as_ptr(), launchline.to_glib_none().0);
        }
    }

    /// Similar to gst_harness_add_src_harness, this is a convenience to
    /// directly create a src-harness using the `src_element_name` name specified.
    ///
    /// MT safe.
    /// ## `src_element_name`
    /// a `gchar` with the name of a [`crate::gst::Element`]
    /// ## `has_clock_wait`
    /// a `gboolean` specifying if the [`crate::gst::Element`] uses
    /// gst_clock_wait_id internally.
    pub fn add_src(&mut self, src_element_name: &str, has_clock_wait: bool) {
        unsafe {
            ffi::gst_harness_add_src(
                self.0.as_ptr(),
                src_element_name.to_glib_none().0,
                has_clock_wait.into_glib(),
            );
        }
    }

    /// A src-harness is a great way of providing the [`crate::Harness`] with data.
    /// By adding a src-type [`crate::gst::Element`], it is then easy to use functions like
    /// gst_harness_push_from_src or gst_harness_src_crank_and_push_many
    /// to provide your harnessed element with input. The `has_clock_wait` variable
    /// is a great way to control you src-element with, in that you can have it
    /// produce a buffer for you by simply cranking the clock, and not have it
    /// spin out of control producing buffers as fast as possible.
    ///
    /// If a src-harness already exists it will be replaced.
    ///
    /// MT safe.
    /// ## `src_harness`
    /// a [`crate::Harness`] to be added as a src-harness.
    /// ## `has_clock_wait`
    /// a `gboolean` specifying if the [`crate::gst::Element`] uses
    /// gst_clock_wait_id internally.
    pub fn add_src_harness(&mut self, src_harness: Harness, has_clock_wait: bool) {
        unsafe {
            let src_harness = mem::ManuallyDrop::new(src_harness);
            ffi::gst_harness_add_src_harness(
                self.0.as_ptr(),
                src_harness.0.as_ptr(),
                has_clock_wait.into_glib(),
            );
        }
    }

    /// Similar to gst_harness_add_src, this allows you to specify a launch-line,
    /// which can be useful for both having more then one [`crate::gst::Element`] acting as your
    /// src (Like a src producing raw buffers, and then an encoder, providing encoded
    /// data), but also by allowing you to set properties like "is-live" directly on
    /// the elements.
    ///
    /// MT safe.
    /// ## `launchline`
    /// a `gchar` describing a gst-launch type line
    /// ## `has_clock_wait`
    /// a `gboolean` specifying if the [`crate::gst::Element`] uses
    /// gst_clock_wait_id internally.
    pub fn add_src_parse(&mut self, launchline: &str, has_clock_wait: bool) {
        unsafe {
            ffi::gst_harness_add_src_parse(
                self.0.as_ptr(),
                launchline.to_glib_none().0,
                has_clock_wait.into_glib(),
            );
        }
    }

    /// The number of `GstBuffers` currently in the [`crate::Harness`] sinkpad [`crate::glib::AsyncQueue`]
    ///
    /// MT safe.
    ///
    /// # Returns
    ///
    /// a `guint` number of buffers in the queue
    pub fn buffers_in_queue(&self) -> u32 {
        unsafe { ffi::gst_harness_buffers_in_queue(self.0.as_ptr()) }
    }

    /// The total number of `GstBuffers` that has arrived on the [`crate::Harness`] sinkpad.
    /// This number includes buffers that have been dropped as well as buffers
    /// that have already been pulled out.
    ///
    /// MT safe.
    ///
    /// # Returns
    ///
    /// a `guint` number of buffers received
    pub fn buffers_received(&self) -> u32 {
        unsafe { ffi::gst_harness_buffers_received(self.0.as_ptr()) }
    }

    /// Similar to [`Self::crank_single_clock_wait()`], this is the function to use
    /// if your harnessed element(s) are using more then one gst_clock_id_wait.
    /// Failing to do so can (and will) make it racy which [`crate::gst::ClockID`] you actually
    /// are releasing, where as this function will process all the waits at the
    /// same time, ensuring that one thread can't register another wait before
    /// both are released.
    ///
    /// MT safe.
    /// ## `waits`
    /// a `guint` describing the number of `GstClockIDs` to crank
    ///
    /// # Returns
    ///
    /// a `gboolean` [`true`] if the "crank" was successful, [`false`] if not.
    pub fn crank_multiple_clock_waits(&mut self, waits: u32) -> Result<(), glib::BoolError> {
        unsafe {
            glib::result_from_gboolean!(
                ffi::gst_harness_crank_multiple_clock_waits(self.0.as_ptr(), waits),
                "Failed to crank multiple clock waits",
            )
        }
    }

    /// A "crank" consists of three steps:
    /// 1: Wait for a [`crate::gst::ClockID`] to be registered with the [`crate::TestClock`].
    /// 2: Advance the [`crate::TestClock`] to the time the [`crate::gst::ClockID`] is waiting for.
    /// 3: Release the [`crate::gst::ClockID`] wait.
    /// Together, this provides an easy way to not have to think about the details
    /// around clocks and time, but still being able to write deterministic tests
    /// that are dependent on this. A "crank" can be though of as the notion of
    /// manually driving the clock forward to its next logical step.
    ///
    /// MT safe.
    ///
    /// # Returns
    ///
    /// a `gboolean` [`true`] if the "crank" was successful, [`false`] if not.
    pub fn crank_single_clock_wait(&mut self) -> Result<(), glib::BoolError> {
        unsafe {
            glib::result_from_gboolean!(
                ffi::gst_harness_crank_single_clock_wait(self.0.as_ptr()),
                "Failed to crank single clock wait",
            )
        }
    }

    /// Allocates a buffer using a [`crate::gst::BufferPool`] if present, or else using the
    /// configured [`crate::gst::Allocator`] and [`crate::gst::AllocationParams`]
    ///
    /// MT safe.
    /// ## `size`
    /// a `gsize` specifying the size of the buffer
    ///
    /// # Returns
    ///
    /// a [`crate::gst::Buffer`] of size `size`
    pub fn create_buffer(&mut self, size: usize) -> Result<gst::Buffer, glib::BoolError> {
        unsafe {
            Option::<_>::from_glib_full(ffi::gst_harness_create_buffer(self.0.as_ptr(), size))
                .ok_or_else(|| glib::bool_error!("Failed to create new buffer"))
        }
    }

    /// Allows you to dump the `GstBuffers` the [`crate::Harness`] sinkpad [`crate::glib::AsyncQueue`]
    /// to a file.
    ///
    /// MT safe.
    /// ## `filename`
    /// a `gchar` with a the name of a file
    pub fn dump_to_file<P: AsRef<path::Path>>(&mut self, filename: P) {
        let filename = filename.as_ref();
        unsafe {
            ffi::gst_harness_dump_to_file(self.0.as_ptr(), filename.to_glib_none().0);
        }
    }

    /// The number of `GstEvents` currently in the [`crate::Harness`] sinkpad [`crate::glib::AsyncQueue`]
    ///
    /// MT safe.
    ///
    /// # Returns
    ///
    /// a `guint` number of events in the queue
    pub fn events_in_queue(&self) -> u32 {
        unsafe { ffi::gst_harness_events_in_queue(self.0.as_ptr()) }
    }

    /// The total number of `GstEvents` that has arrived on the [`crate::Harness`] sinkpad
    /// This number includes events handled by the harness as well as events
    /// that have already been pulled out.
    ///
    /// MT safe.
    ///
    /// # Returns
    ///
    /// a `guint` number of events received
    pub fn events_received(&self) -> u32 {
        unsafe { ffi::gst_harness_events_received(self.0.as_ptr()) }
    }

    /// Most useful in conjunction with gst_harness_new_parse, this will scan the
    /// `GstElements` inside the [`crate::Harness`], and check if any of them matches
    /// `element_name`. Typical usecase being that you need to access one of the
    /// harnessed elements for properties and/or signals.
    ///
    /// MT safe.
    /// ## `element_name`
    /// a `gchar` with a [`crate::gst::ElementFactory`] name
    ///
    /// # Returns
    ///
    /// a [`crate::gst::Element`] or [`None`] if not found
    pub fn find_element(&mut self, element_name: &str) -> Option<gst::Element> {
        unsafe {
            // Work around https://gitlab.freedesktop.org/gstreamer/gstreamer/merge_requests/31
            let ptr = ffi::gst_harness_find_element(self.0.as_ptr(), element_name.to_glib_none().0);

            if ptr.is_null() {
                return None;
            }

            // Clear floating flag if it is set
            if glib::gobject_ffi::g_object_is_floating(ptr as *mut _) != glib::ffi::GFALSE {
                glib::gobject_ffi::g_object_ref_sink(ptr as *mut _);
            }

            from_glib_full(ptr)
        }
    }

    //pub fn get(&mut self, element_name: &str, first_property_name: &str, : /*Unknown conversion*//*Unimplemented*/Fundamental: VarArgs) {
    //    unsafe { TODO: call ffi::gst_harness_get() }
    //}

    //pub fn get_allocator(&mut self, allocator: /*Ignored*/gst::Allocator, params: /*Ignored*/gst::AllocationParams) {
    //    unsafe { TODO: call ffi::gst_harness_get_allocator() }
    //}

    #[doc(alias = "get_last_pushed_timestamp")]
    pub fn last_pushed_timestamp(&self) -> gst::ClockTime {
        unsafe { from_glib(ffi::gst_harness_get_last_pushed_timestamp(self.0.as_ptr())) }
    }

    #[doc(alias = "get_testclock")]
    pub fn testclock(&self) -> Option<TestClock> {
        unsafe { from_glib_full(ffi::gst_harness_get_testclock(self.0.as_ptr())) }
    }

    /// This will set the harnessed [`crate::gst::Element`] to [`crate::gst::State::Playing`].
    /// `GstElements` without a sink-[`crate::gst::Pad`] and with the [`crate::gst::ElementFlags::Source`]
    /// flag set is considered a src [`crate::gst::Element`]
    /// Non-src `GstElements` (like sinks and filters) are automatically set to
    /// playing by the [`crate::Harness`], but src `GstElements` are not to avoid them
    /// starting to produce buffers.
    /// Hence, for src [`crate::gst::Element`] you must call [`Self::play()`] explicitly.
    ///
    /// MT safe.
    pub fn play(&mut self) {
        unsafe {
            ffi::gst_harness_play(self.0.as_ptr());
        }
    }

    /// Pulls a [`crate::gst::Buffer`] from the [`crate::glib::AsyncQueue`] on the [`crate::Harness`] sinkpad. The pull
    /// will timeout in 60 seconds. This is the standard way of getting a buffer
    /// from a harnessed [`crate::gst::Element`].
    ///
    /// MT safe.
    ///
    /// # Returns
    ///
    /// a [`crate::gst::Buffer`] or [`None`] if timed out.
    pub fn pull(&mut self) -> Result<gst::Buffer, glib::BoolError> {
        unsafe {
            Option::<_>::from_glib_full(ffi::gst_harness_pull(self.0.as_ptr()))
                .ok_or_else(|| glib::bool_error!("Failed to pull buffer"))
        }
    }

    /// Pulls a [`crate::gst::Buffer`] from the [`crate::glib::AsyncQueue`] on the [`crate::Harness`] sinkpad. The pull
    /// will block until an EOS event is received, or timeout in 60 seconds.
    /// MT safe.
    /// ## `buf`
    /// A [`crate::gst::Buffer`], or [`None`] if EOS or timeout occures
    ///  first.
    ///
    /// # Returns
    ///
    /// [`true`] on success, [`false`] on timeout.
    #[cfg(any(feature = "v1_18", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v1_18")))]
    pub fn pull_until_eos(&mut self) -> Result<Option<gst::Buffer>, glib::BoolError> {
        unsafe {
            let mut buffer = ptr::null_mut();
            let res = ffi::gst_harness_pull_until_eos(self.0.as_ptr(), &mut buffer);
            if from_glib(res) {
                Ok(from_glib_full(buffer))
            } else {
                Err(glib::bool_error!("Failed to pull buffer or EOS"))
            }
        }
    }

    /// Pulls an [`crate::gst::Event`] from the [`crate::glib::AsyncQueue`] on the [`crate::Harness`] sinkpad.
    /// Timeouts after 60 seconds similar to gst_harness_pull.
    ///
    /// MT safe.
    ///
    /// # Returns
    ///
    /// a [`crate::gst::Event`] or [`None`] if timed out.
    pub fn pull_event(&mut self) -> Result<gst::Event, glib::BoolError> {
        unsafe {
            Option::<_>::from_glib_full(ffi::gst_harness_pull_event(self.0.as_ptr()))
                .ok_or_else(|| glib::bool_error!("Failed to pull event"))
        }
    }

    /// Pulls an [`crate::gst::Event`] from the [`crate::glib::AsyncQueue`] on the [`crate::Harness`] srcpad.
    /// Timeouts after 60 seconds similar to gst_harness_pull.
    ///
    /// MT safe.
    ///
    /// # Returns
    ///
    /// a [`crate::gst::Event`] or [`None`] if timed out.
    pub fn pull_upstream_event(&mut self) -> Result<gst::Event, glib::BoolError> {
        unsafe {
            Option::<_>::from_glib_full(ffi::gst_harness_pull_upstream_event(self.0.as_ptr()))
                .ok_or_else(|| glib::bool_error!("Failed to pull event"))
        }
    }

    /// Pushes a [`crate::gst::Buffer`] on the [`crate::Harness`] srcpad. The standard way of
    /// interacting with an harnessed element.
    ///
    /// MT safe.
    /// ## `buffer`
    /// a [`crate::gst::Buffer`] to push
    ///
    /// # Returns
    ///
    /// a [`crate::gst::FlowReturn`] with the result from the push
    pub fn push(&mut self, buffer: gst::Buffer) -> Result<gst::FlowSuccess, gst::FlowError> {
        unsafe {
            gst::FlowSuccess::try_from_glib(ffi::gst_harness_push(
                self.0.as_ptr(),
                buffer.into_ptr(),
            ))
        }
    }

    /// Basically a gst_harness_push and a gst_harness_pull in one line. Reflects
    /// the fact that you often want to do exactly this in your test: Push one buffer
    /// in, and inspect the outcome.
    ///
    /// MT safe.
    /// ## `buffer`
    /// a [`crate::gst::Buffer`] to push
    ///
    /// # Returns
    ///
    /// a [`crate::gst::Buffer`] or [`None`] if timed out.
    pub fn push_and_pull(&mut self, buffer: gst::Buffer) -> Result<gst::Buffer, glib::BoolError> {
        unsafe {
            Option::<_>::from_glib_full(ffi::gst_harness_push_and_pull(
                self.0.as_ptr(),
                buffer.into_ptr(),
            ))
            .ok_or_else(|| glib::bool_error!("Failed to push and pull buffer"))
        }
    }

    /// Pushes an [`crate::gst::Event`] on the [`crate::Harness`] srcpad.
    ///
    /// MT safe.
    /// ## `event`
    /// a [`crate::gst::Event`] to push
    ///
    /// # Returns
    ///
    /// a `gboolean` with the result from the push
    pub fn push_event(&mut self, event: gst::Event) -> bool {
        unsafe {
            from_glib(ffi::gst_harness_push_event(
                self.0.as_ptr(),
                event.into_ptr(),
            ))
        }
    }

    /// Transfer data from the src-[`crate::Harness`] to the main-[`crate::Harness`]. It consists
    /// of 4 steps:
    /// 1: Make sure the src is started. (see: gst_harness_play)
    /// 2: Crank the clock (see: gst_harness_crank_single_clock_wait)
    /// 3: Pull a [`crate::gst::Buffer`] from the src-[`crate::Harness`] (see: gst_harness_pull)
    /// 4: Push the same [`crate::gst::Buffer`] into the main-[`crate::Harness`] (see: gst_harness_push)
    ///
    /// MT safe.
    ///
    /// # Returns
    ///
    /// a [`crate::gst::FlowReturn`] with the result of the push
    pub fn push_from_src(&mut self) -> Result<gst::FlowSuccess, gst::FlowError> {
        unsafe { gst::FlowSuccess::try_from_glib(ffi::gst_harness_push_from_src(self.0.as_ptr())) }
    }

    /// Transfer one [`crate::gst::Buffer`] from the main-[`crate::Harness`] to the sink-[`crate::Harness`].
    /// See gst_harness_push_from_src for details.
    ///
    /// MT safe.
    ///
    /// # Returns
    ///
    /// a [`crate::gst::FlowReturn`] with the result of the push
    pub fn push_to_sink(&mut self) -> Result<gst::FlowSuccess, gst::FlowError> {
        unsafe { gst::FlowSuccess::try_from_glib(ffi::gst_harness_push_to_sink(self.0.as_ptr())) }
    }

    /// Pushes an [`crate::gst::Event`] on the [`crate::Harness`] sinkpad.
    ///
    /// MT safe.
    /// ## `event`
    /// a [`crate::gst::Event`] to push
    ///
    /// # Returns
    ///
    /// a `gboolean` with the result from the push
    pub fn push_upstream_event(&mut self, event: gst::Event) -> bool {
        unsafe {
            from_glib(ffi::gst_harness_push_upstream_event(
                self.0.as_ptr(),
                event.into_ptr(),
            ))
        }
    }

    /// Get the min latency reported by any harnessed [`crate::gst::Element`].
    ///
    /// MT safe.
    ///
    /// # Returns
    ///
    /// a [`crate::gst::ClockTime`] with min latency
    pub fn query_latency(&self) -> gst::ClockTime {
        unsafe { from_glib(ffi::gst_harness_query_latency(self.0.as_ptr())) }
    }

    //pub fn set(&mut self, element_name: &str, first_property_name: &str, : /*Unknown conversion*//*Unimplemented*/Fundamental: VarArgs) {
    //    unsafe { TODO: call ffi::gst_harness_set() }
    //}

    /// Setting this will make the harness block in the chain-function, and
    /// then release when [`Self::pull()`] or [`Self::try_pull()`] is called.
    /// Can be useful when wanting to control a src-element that is not implementing
    /// [`crate::gst::Clock::id_wait()`] so it can't be controlled by the [`crate::TestClock`], since
    /// it otherwise would produce buffers as fast as possible.
    ///
    /// MT safe.
    pub fn set_blocking_push_mode(&mut self) {
        unsafe {
            ffi::gst_harness_set_blocking_push_mode(self.0.as_ptr());
        }
    }

    /// Sets the [`crate::Harness`] (XXX: @-reference does not belong to Harness!) srcpad and sinkpad caps.
    ///
    /// MT safe.
    /// ## `in_`
    /// a [`crate::gst::Caps`] to set on the harness srcpad
    /// ## `out`
    /// a [`crate::gst::Caps`] to set on the harness sinkpad
    pub fn set_caps(&mut self, in_: gst::Caps, out: gst::Caps) {
        unsafe {
            ffi::gst_harness_set_caps(self.0.as_ptr(), in_.into_ptr(), out.into_ptr());
        }
    }

    /// Sets the [`crate::Harness`] (XXX: @-reference does not belong to Harness!) srcpad and sinkpad caps using strings.
    ///
    /// MT safe.
    /// ## `in_`
    /// a `gchar` describing a [`crate::gst::Caps`] to set on the harness srcpad
    /// ## `out`
    /// a `gchar` describing a [`crate::gst::Caps`] to set on the harness sinkpad
    pub fn set_caps_str(&mut self, in_: &str, out: &str) {
        unsafe {
            ffi::gst_harness_set_caps_str(
                self.0.as_ptr(),
                in_.to_glib_none().0,
                out.to_glib_none().0,
            );
        }
    }

    /// When set to [`true`], instead of placing the buffers arriving from the harnessed
    /// [`crate::gst::Element`] inside the sinkpads [`crate::glib::AsyncQueue`], they are instead unreffed.
    ///
    /// MT safe.
    /// ## `drop_buffers`
    /// a `gboolean` specifying to drop outgoing buffers or not
    pub fn set_drop_buffers(&mut self, drop_buffers: bool) {
        unsafe {
            ffi::gst_harness_set_drop_buffers(self.0.as_ptr(), drop_buffers.into_glib());
        }
    }

    /// As a convenience, a src-harness will forward [`crate::gst::EventType::StreamStart`],
    /// [`crate::gst::EventType::Caps`] and [`crate::gst::EventType::Segment`] to the main-harness if forwarding
    /// is enabled, and forward any sticky-events from the main-harness to
    /// the sink-harness. It will also forward the [`crate::gst::QueryType::Allocation`].
    ///
    /// If forwarding is disabled, the user will have to either manually push
    /// these events from the src-harness using [`Self::src_push_event()`], or
    /// create and push them manually. While this will allow full control and
    /// inspection of these events, for the most cases having forwarding enabled
    /// will be sufficient when writing a test where the src-harness' main function
    /// is providing data for the main-harness.
    ///
    /// Forwarding is enabled by default.
    ///
    /// MT safe.
    /// ## `forwarding`
    /// a `gboolean` to enable/disable forwarding
    pub fn set_forwarding(&mut self, forwarding: bool) {
        unsafe {
            ffi::gst_harness_set_forwarding(self.0.as_ptr(), forwarding.into_glib());
        }
    }

    //pub fn set_propose_allocator<P: IsA<gst::Allocator>>(&mut self, allocator: Option<&P>, params: Option<&gst::AllocationParams>) {
    //    unsafe { TODO: call ffi::gst_harness_set_propose_allocator() }
    //}

    /// Sets the [`crate::Harness`] (XXX: @-reference does not belong to Harness!) sinkpad caps.
    ///
    /// MT safe.
    /// ## `caps`
    /// a [`crate::gst::Caps`] to set on the harness sinkpad
    pub fn set_sink_caps(&mut self, caps: gst::Caps) {
        unsafe {
            ffi::gst_harness_set_sink_caps(self.0.as_ptr(), caps.into_ptr());
        }
    }

    /// Sets the [`crate::Harness`] (XXX: @-reference does not belong to Harness!) sinkpad caps using a string.
    ///
    /// MT safe.
    /// ## `str`
    /// a `gchar` describing a [`crate::gst::Caps`] to set on the harness sinkpad
    pub fn set_sink_caps_str(&mut self, str: &str) {
        unsafe {
            ffi::gst_harness_set_sink_caps_str(self.0.as_ptr(), str.to_glib_none().0);
        }
    }

    /// Sets the [`crate::Harness`] (XXX: @-reference does not belong to Harness!) srcpad caps. This must be done before any buffers
    /// can legally be pushed from the harness to the element.
    ///
    /// MT safe.
    /// ## `caps`
    /// a [`crate::gst::Caps`] to set on the harness srcpad
    pub fn set_src_caps(&mut self, caps: gst::Caps) {
        unsafe {
            ffi::gst_harness_set_src_caps(self.0.as_ptr(), caps.into_ptr());
        }
    }

    /// Sets the [`crate::Harness`] (XXX: @-reference does not belong to Harness!) srcpad caps using a string. This must be done before
    /// any buffers can legally be pushed from the harness to the element.
    ///
    /// MT safe.
    /// ## `str`
    /// a `gchar` describing a [`crate::gst::Caps`] to set on the harness srcpad
    pub fn set_src_caps_str(&mut self, str: &str) {
        unsafe {
            ffi::gst_harness_set_src_caps_str(self.0.as_ptr(), str.to_glib_none().0);
        }
    }

    /// Advance the [`crate::TestClock`] to a specific time.
    ///
    /// MT safe.
    /// ## `time`
    /// a [`crate::gst::ClockTime`] to advance the clock to
    ///
    /// # Returns
    ///
    /// a `gboolean` [`true`] if the time could be set. [`false`] if not.
    pub fn set_time(&mut self, time: gst::ClockTime) -> Result<(), glib::BoolError> {
        unsafe {
            glib::result_from_gboolean!(
                ffi::gst_harness_set_time(self.0.as_ptr(), time.into_glib()),
                "Failed to set time",
            )
        }
    }

    /// Sets the min latency reported by [`crate::Harness`] when receiving a latency-query
    /// ## `latency`
    /// a [`crate::gst::ClockTime`] specifying the latency
    pub fn set_upstream_latency(&mut self, latency: gst::ClockTime) {
        unsafe {
            ffi::gst_harness_set_upstream_latency(self.0.as_ptr(), latency.into_glib());
        }
    }

    /// Convenience that calls gst_harness_push_to_sink `pushes` number of times.
    /// Will abort the pushing if any one push fails.
    ///
    /// MT safe.
    /// ## `pushes`
    /// a `gint` with the number of calls to gst_harness_push_to_sink
    ///
    /// # Returns
    ///
    /// a [`crate::gst::FlowReturn`] with the result of the push
    pub fn sink_push_many(&mut self, pushes: u32) -> Result<gst::FlowSuccess, gst::FlowError> {
        unsafe {
            gst::FlowSuccess::try_from_glib(ffi::gst_harness_sink_push_many(
                self.0.as_ptr(),
                pushes as i32,
            ))
        }
    }

    /// Transfer data from the src-[`crate::Harness`] to the main-[`crate::Harness`]. Similar to
    /// gst_harness_push_from_src, this variant allows you to specify how many cranks
    /// and how many pushes to perform. This can be useful for both moving a lot
    /// of data at the same time, as well as cases when one crank does not equal one
    /// buffer to push and v.v.
    ///
    /// MT safe.
    /// ## `cranks`
    /// a `gint` with the number of calls to gst_harness_crank_single_clock_wait
    /// ## `pushes`
    /// a `gint` with the number of calls to gst_harness_push
    ///
    /// # Returns
    ///
    /// a [`crate::gst::FlowReturn`] with the result of the push
    pub fn src_crank_and_push_many(
        &mut self,
        cranks: u32,
        pushes: u32,
    ) -> Result<gst::FlowSuccess, gst::FlowError> {
        unsafe {
            gst::FlowSuccess::try_from_glib(ffi::gst_harness_src_crank_and_push_many(
                self.0.as_ptr(),
                cranks as i32,
                pushes as i32,
            ))
        }
    }

    /// Similar to what gst_harness_src_push does with `GstBuffers`, this transfers
    /// a [`crate::gst::Event`] from the src-[`crate::Harness`] to the main-[`crate::Harness`]. Note that
    /// some `GstEvents` are being transferred automagically. Look at sink_forward_pad
    /// for details.
    ///
    /// MT safe.
    ///
    /// # Returns
    ///
    /// a `gboolean` with the result of the push
    pub fn src_push_event(&mut self) -> bool {
        unsafe { from_glib(ffi::gst_harness_src_push_event(self.0.as_ptr())) }
    }

    //pub fn stress_custom_start<'a, P: Into<Option<&'a /*Ignored*/glib::Func>>, Q: Into<Option</*Unimplemented*/Fundamental: Pointer>>>(&mut self, init: P, callback: /*Unknown conversion*//*Unimplemented*/Func, data: Q, sleep: libc::c_ulong) -> /*Ignored*/Option<HarnessThread> {
    //    unsafe { TODO: call ffi::gst_harness_stress_custom_start() }
    //}

    //pub fn stress_property_start_full(&mut self, name: &str, value: /*Ignored*/&glib::Value, sleep: libc::c_ulong) -> /*Ignored*/Option<HarnessThread> {
    //    unsafe { TODO: call ffi::gst_harness_stress_property_start_full() }
    //}

    //pub fn stress_push_buffer_start_full(&mut self, caps: &mut gst::Caps, segment: /*Ignored*/&gst::Segment, buf: &mut gst::Buffer, sleep: libc::c_ulong) -> /*Ignored*/Option<HarnessThread> {
    //    unsafe { TODO: call ffi::gst_harness_stress_push_buffer_start_full() }
    //}

    //pub fn stress_push_buffer_with_cb_start_full<P: Into<Option</*Unimplemented*/Fundamental: Pointer>>>(&mut self, caps: &mut gst::Caps, segment: /*Ignored*/&gst::Segment, func: /*Unknown conversion*//*Unimplemented*/HarnessPrepareBufferFunc, data: P, notify: /*Unknown conversion*//*Unimplemented*/DestroyNotify, sleep: libc::c_ulong) -> /*Ignored*/Option<HarnessThread> {
    //    unsafe { TODO: call ffi::gst_harness_stress_push_buffer_with_cb_start_full() }
    //}

    //pub fn stress_push_event_start_full(&mut self, event: &mut gst::Event, sleep: libc::c_ulong) -> /*Ignored*/Option<HarnessThread> {
    //    unsafe { TODO: call ffi::gst_harness_stress_push_event_start_full() }
    //}

    //pub fn stress_push_event_with_cb_start_full<P: Into<Option</*Unimplemented*/Fundamental: Pointer>>>(&mut self, func: /*Unknown conversion*//*Unimplemented*/HarnessPrepareEventFunc, data: P, notify: /*Unknown conversion*//*Unimplemented*/DestroyNotify, sleep: libc::c_ulong) -> /*Ignored*/Option<HarnessThread> {
    //    unsafe { TODO: call ffi::gst_harness_stress_push_event_with_cb_start_full() }
    //}

    //pub fn stress_push_upstream_event_start_full(&mut self, event: &mut gst::Event, sleep: libc::c_ulong) -> /*Ignored*/Option<HarnessThread> {
    //    unsafe { TODO: call ffi::gst_harness_stress_push_upstream_event_start_full() }
    //}

    //pub fn stress_push_upstream_event_with_cb_start_full<P: Into<Option</*Unimplemented*/Fundamental: Pointer>>>(&mut self, func: /*Unknown conversion*//*Unimplemented*/HarnessPrepareEventFunc, data: P, notify: /*Unknown conversion*//*Unimplemented*/DestroyNotify, sleep: libc::c_ulong) -> /*Ignored*/Option<HarnessThread> {
    //    unsafe { TODO: call ffi::gst_harness_stress_push_upstream_event_with_cb_start_full() }
    //}

    //pub fn stress_requestpad_start_full(&mut self, templ: /*Ignored*/&gst::PadTemplate, name: &str, caps: &mut gst::Caps, release: bool, sleep: libc::c_ulong) -> /*Ignored*/Option<HarnessThread> {
    //    unsafe { TODO: call ffi::gst_harness_stress_requestpad_start_full() }
    //}

    //pub fn stress_statechange_start_full(&mut self, sleep: libc::c_ulong) -> /*Ignored*/Option<HarnessThread> {
    //    unsafe { TODO: call ffi::gst_harness_stress_statechange_start_full() }
    //}

    /// Pulls all pending data from the harness and returns it as a single buffer.
    ///
    /// # Returns
    ///
    /// the data as a buffer. Unref with `gst_buffer_unref()`
    ///  when no longer needed.
    #[cfg(any(feature = "v1_14", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v1_14")))]
    pub fn take_all_data_as_buffer(&mut self) -> Result<gst::Buffer, glib::BoolError> {
        unsafe {
            Option::<_>::from_glib_full(ffi::gst_harness_take_all_data_as_buffer(self.0.as_ptr()))
                .ok_or_else(|| glib::bool_error!("Failed to take all data as buffer"))
        }
    }

    /// Pulls all pending data from the harness and returns it as a single [`crate::glib::Bytes`].
    ///
    /// # Returns
    ///
    /// a pointer to the data, newly allocated. Free
    ///  with `g_free()` when no longer needed.
    #[cfg(any(feature = "v1_14", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v1_14")))]
    pub fn take_all_data_as_bytes(&mut self) -> Result<glib::Bytes, glib::BoolError> {
        unsafe {
            Option::<_>::from_glib_full(ffi::gst_harness_take_all_data_as_bytes(self.0.as_ptr()))
                .ok_or_else(|| glib::bool_error!("Failed to take all data as bytes"))
        }
    }

    /// Pulls a [`crate::gst::Buffer`] from the [`crate::glib::AsyncQueue`] on the [`crate::Harness`] sinkpad. Unlike
    /// gst_harness_pull this will not wait for any buffers if not any are present,
    /// and return [`None`] straight away.
    ///
    /// MT safe.
    ///
    /// # Returns
    ///
    /// a [`crate::gst::Buffer`] or [`None`] if no buffers are present in the [`crate::glib::AsyncQueue`]
    pub fn try_pull(&mut self) -> Option<gst::Buffer> {
        unsafe { from_glib_full(ffi::gst_harness_try_pull(self.0.as_ptr())) }
    }

    /// Pulls an [`crate::gst::Event`] from the [`crate::glib::AsyncQueue`] on the [`crate::Harness`] sinkpad.
    /// See gst_harness_try_pull for details.
    ///
    /// MT safe.
    ///
    /// # Returns
    ///
    /// a [`crate::gst::Event`] or [`None`] if no buffers are present in the [`crate::glib::AsyncQueue`]
    pub fn try_pull_event(&mut self) -> Option<gst::Event> {
        unsafe { from_glib_full(ffi::gst_harness_try_pull_event(self.0.as_ptr())) }
    }

    /// Pulls an [`crate::gst::Event`] from the [`crate::glib::AsyncQueue`] on the [`crate::Harness`] srcpad.
    /// See gst_harness_try_pull for details.
    ///
    /// MT safe.
    ///
    /// # Returns
    ///
    /// a [`crate::gst::Event`] or [`None`] if no buffers are present in the [`crate::glib::AsyncQueue`]
    pub fn try_pull_upstream_event(&mut self) -> Option<gst::Event> {
        unsafe { from_glib_full(ffi::gst_harness_try_pull_upstream_event(self.0.as_ptr())) }
    }

    /// The number of `GstEvents` currently in the [`crate::Harness`] srcpad [`crate::glib::AsyncQueue`]
    ///
    /// MT safe.
    ///
    /// # Returns
    ///
    /// a `guint` number of events in the queue
    pub fn upstream_events_in_queue(&self) -> u32 {
        unsafe { ffi::gst_harness_upstream_events_in_queue(self.0.as_ptr()) }
    }

    /// The total number of `GstEvents` that has arrived on the [`crate::Harness`] srcpad
    /// This number includes events handled by the harness as well as events
    /// that have already been pulled out.
    ///
    /// MT safe.
    ///
    /// # Returns
    ///
    /// a `guint` number of events received
    pub fn upstream_events_received(&self) -> u32 {
        unsafe { ffi::gst_harness_upstream_events_received(self.0.as_ptr()) }
    }

    /// Sets the system [`crate::gst::Clock`] on the [`crate::Harness`] (XXX: @-reference does not belong to Harness!) [`crate::gst::Element`]
    ///
    /// MT safe.
    pub fn use_systemclock(&mut self) {
        unsafe {
            ffi::gst_harness_use_systemclock(self.0.as_ptr());
        }
    }

    /// Sets the [`crate::TestClock`] on the [`crate::Harness`] [`crate::gst::Element`]
    ///
    /// MT safe.
    pub fn use_testclock(&mut self) {
        unsafe {
            ffi::gst_harness_use_testclock(self.0.as_ptr());
        }
    }

    /// Waits for `timeout` seconds until `waits` number of [`crate::gst::ClockID`] waits is
    /// registered with the [`crate::TestClock`]. Useful for writing deterministic tests,
    /// where you want to make sure that an expected number of waits have been
    /// reached.
    ///
    /// MT safe.
    /// ## `waits`
    /// a `guint` describing the numbers of [`crate::gst::ClockID`] registered with
    /// the [`crate::TestClock`]
    /// ## `timeout`
    /// a `guint` describing how many seconds to wait for `waits` to be true
    ///
    /// # Returns
    ///
    /// a `gboolean` [`true`] if the waits have been registered, [`false`] if not.
    /// (Could be that it timed out waiting or that more waits than waits was found)
    pub fn wait_for_clock_id_waits(
        &mut self,
        waits: u32,
        timeout: u32,
    ) -> Result<(), glib::BoolError> {
        unsafe {
            glib::result_from_gboolean!(
                ffi::gst_harness_wait_for_clock_id_waits(self.0.as_ptr(), waits, timeout),
                "Failed to wait for clock id waits",
            )
        }
    }

    unsafe fn from_glib_full(ptr: *mut ffi::GstHarness) -> Harness {
        assert!(!ptr.is_null());

        Harness(ptr::NonNull::new_unchecked(ptr))
    }

    /// Creates a new harness. Works like [`Self::new_with_padnames()`], except it
    /// assumes the [`crate::gst::Element`] sinkpad is named "sink" and srcpad is named "src"
    ///
    /// MT safe.
    /// ## `element_name`
    /// a `gchar` describing the [`crate::gst::Element`] name
    ///
    /// # Returns
    ///
    /// a [`crate::Harness`], or [`None`] if the harness could
    /// not be created
    pub fn new(element_name: &str) -> Harness {
        assert_initialized_main_thread!();
        unsafe { Self::from_glib_full(ffi::gst_harness_new(element_name.to_glib_none().0)) }
    }

    /// Creates a new empty harness. Use [`Self::add_element_full()`] to add
    /// an [`crate::gst::Element`] to it.
    ///
    /// MT safe.
    ///
    /// # Returns
    ///
    /// a [`crate::Harness`], or [`None`] if the harness could
    /// not be created
    pub fn new_empty() -> Harness {
        assert_initialized_main_thread!();
        unsafe { Self::from_glib_full(ffi::gst_harness_new_empty()) }
    }

    /// Creates a new harness.
    ///
    /// MT safe.
    /// ## `element`
    /// a [`crate::gst::Element`] to attach the harness to (transfer none)
    /// ## `hsrc`
    /// a [`crate::gst::StaticPadTemplate`] describing the harness srcpad.
    /// [`None`] will not create a harness srcpad.
    /// ## `element_sinkpad_name`
    /// a `gchar` with the name of the element
    /// sinkpad that is then linked to the harness srcpad. Can be a static or request
    /// or a sometimes pad that has been added. [`None`] will not get/request a sinkpad
    /// from the element. (Like if the element is a src.)
    /// ## `hsink`
    /// a [`crate::gst::StaticPadTemplate`] describing the harness sinkpad.
    /// [`None`] will not create a harness sinkpad.
    /// ## `element_srcpad_name`
    /// a `gchar` with the name of the element
    /// srcpad that is then linked to the harness sinkpad, similar to the
    /// `element_sinkpad_name`.
    ///
    /// # Returns
    ///
    /// a [`crate::Harness`], or [`None`] if the harness could
    /// not be created
    pub fn new_full<P: IsA<gst::Element>>(
        element: &P,
        hsrc: Option<&gst::StaticPadTemplate>,
        element_sinkpad_name: Option<&str>,
        hsink: Option<&gst::StaticPadTemplate>,
        element_srcpad_name: Option<&str>,
    ) -> Harness {
        assert_initialized_main_thread!();
        let element_sinkpad_name = element_sinkpad_name.to_glib_none();
        let element_srcpad_name = element_srcpad_name.to_glib_none();
        unsafe {
            Self::from_glib_full(ffi::gst_harness_new_full(
                element.as_ref().to_glib_none().0,
                hsrc.to_glib_none().0 as *mut _,
                element_sinkpad_name.0,
                hsink.to_glib_none().0 as *mut _,
                element_srcpad_name.0,
            ))
        }
    }

    /// Creates a new harness, parsing the `launchline` and putting that in a [`crate::gst::Bin`],
    /// and then attches the harness to the bin.
    ///
    /// MT safe.
    /// ## `launchline`
    /// a `gchar` describing a gst-launch type line
    ///
    /// # Returns
    ///
    /// a [`crate::Harness`], or [`None`] if the harness could
    /// not be created
    pub fn new_parse(launchline: &str) -> Harness {
        assert_initialized_main_thread!();
        unsafe { Self::from_glib_full(ffi::gst_harness_new_parse(launchline.to_glib_none().0)) }
    }

    pub fn with_element<P: IsA<gst::Element>>(
        element: &P,
        element_sinkpad_name: Option<&str>,
        element_srcpad_name: Option<&str>,
    ) -> Harness {
        assert_initialized_main_thread!();
        let element_sinkpad_name = element_sinkpad_name.to_glib_none();
        let element_srcpad_name = element_srcpad_name.to_glib_none();
        unsafe {
            Self::from_glib_full(ffi::gst_harness_new_with_element(
                element.as_ref().to_glib_none().0,
                element_sinkpad_name.0,
                element_srcpad_name.0,
            ))
        }
    }

    pub fn with_padnames(
        element_name: &str,
        element_sinkpad_name: Option<&str>,
        element_srcpad_name: Option<&str>,
    ) -> Harness {
        assert_initialized_main_thread!();
        let element_sinkpad_name = element_sinkpad_name.to_glib_none();
        let element_srcpad_name = element_srcpad_name.to_glib_none();
        unsafe {
            Self::from_glib_full(ffi::gst_harness_new_with_padnames(
                element_name.to_glib_none().0,
                element_sinkpad_name.0,
                element_srcpad_name.0,
            ))
        }
    }

    pub fn with_templates(
        element_name: &str,
        hsrc: Option<&gst::StaticPadTemplate>,
        hsink: Option<&gst::StaticPadTemplate>,
    ) -> Harness {
        assert_initialized_main_thread!();
        unsafe {
            Self::from_glib_full(ffi::gst_harness_new_with_templates(
                element_name.to_glib_none().0,
                hsrc.to_glib_none().0 as *mut _,
                hsink.to_glib_none().0 as *mut _,
            ))
        }
    }

    //pub fn stress_thread_stop(t: /*Ignored*/&mut HarnessThread) -> u32 {
    //    unsafe { TODO: call ffi::gst_harness_stress_thread_stop() }
    //}

    #[doc(alias = "get_element")]
    pub fn element(&self) -> Option<gst::Element> {
        unsafe {
            // Work around https://gitlab.freedesktop.org/gstreamer/gstreamer/merge_requests/31
            let ptr = (*self.0.as_ptr()).element;

            if ptr.is_null() {
                return None;
            }

            // Clear floating flag if it is set
            if glib::gobject_ffi::g_object_is_floating(ptr as *mut _) != glib::ffi::GFALSE {
                glib::gobject_ffi::g_object_ref_sink(ptr as *mut _);
            }

            from_glib_none(ptr)
        }
    }

    #[doc(alias = "get_sinkpad")]
    pub fn sinkpad(&self) -> Option<gst::Pad> {
        unsafe {
            // Work around https://gitlab.freedesktop.org/gstreamer/gstreamer/merge_requests/31
            let ptr = (*self.0.as_ptr()).sinkpad;

            if ptr.is_null() {
                return None;
            }

            // Clear floating flag if it is set
            if glib::gobject_ffi::g_object_is_floating(ptr as *mut _) != glib::ffi::GFALSE {
                glib::gobject_ffi::g_object_ref_sink(ptr as *mut _);
            }

            from_glib_none(ptr)
        }
    }

    #[doc(alias = "get_srcpad")]
    pub fn srcpad(&self) -> Option<gst::Pad> {
        unsafe {
            // Work around https://gitlab.freedesktop.org/gstreamer/gstreamer/merge_requests/31
            let ptr = (*self.0.as_ptr()).srcpad;

            if ptr.is_null() {
                return None;
            }

            // Clear floating flag if it is set
            if glib::gobject_ffi::g_object_is_floating(ptr as *mut _) != glib::ffi::GFALSE {
                glib::gobject_ffi::g_object_ref_sink(ptr as *mut _);
            }

            from_glib_none(ptr)
        }
    }

    #[doc(alias = "get_sink_harness")]
    pub fn sink_harness(&self) -> Option<Ref> {
        unsafe {
            let sink_harness = (*self.0.as_ptr()).sink_harness;
            if sink_harness.is_null() {
                None
            } else {
                Some(Ref(
                    mem::ManuallyDrop::new(Harness(ptr::NonNull::new_unchecked(sink_harness))),
                    PhantomData,
                ))
            }
        }
    }

    #[doc(alias = "get_src_harness")]
    pub fn src_harness(&self) -> Option<Ref> {
        unsafe {
            let src_harness = (*self.0.as_ptr()).src_harness;
            if src_harness.is_null() {
                None
            } else {
                Some(Ref(
                    mem::ManuallyDrop::new(Harness(ptr::NonNull::new_unchecked(src_harness))),
                    PhantomData,
                ))
            }
        }
    }

    #[doc(alias = "get_mut_sink_harness")]
    pub fn sink_harness_mut(&mut self) -> Option<RefMut> {
        unsafe {
            let sink_harness = (*self.0.as_ptr()).sink_harness;
            if sink_harness.is_null() {
                None
            } else {
                Some(RefMut(
                    mem::ManuallyDrop::new(Harness(ptr::NonNull::new_unchecked(sink_harness))),
                    PhantomData,
                ))
            }
        }
    }

    #[doc(alias = "get_mut_src_harness")]
    pub fn src_harness_mut(&mut self) -> Option<RefMut> {
        unsafe {
            let src_harness = (*self.0.as_ptr()).src_harness;
            if src_harness.is_null() {
                None
            } else {
                Some(RefMut(
                    mem::ManuallyDrop::new(Harness(ptr::NonNull::new_unchecked(src_harness))),
                    PhantomData,
                ))
            }
        }
    }
}

#[derive(Debug)]
pub struct Ref<'a>(mem::ManuallyDrop<Harness>, PhantomData<&'a Harness>);

impl<'a> ops::Deref for Ref<'a> {
    type Target = Harness;

    fn deref(&self) -> &Harness {
        &*self.0
    }
}

#[derive(Debug)]
pub struct RefMut<'a>(mem::ManuallyDrop<Harness>, PhantomData<&'a mut Harness>);

impl<'a> ops::Deref for RefMut<'a> {
    type Target = Harness;

    fn deref(&self) -> &Harness {
        &*self.0
    }
}

impl<'a> ops::DerefMut for RefMut<'a> {
    fn deref_mut(&mut self) -> &mut Harness {
        &mut *self.0
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_identity_push_pull() {
        gst::init().unwrap();

        let mut h = Harness::new("identity");
        h.set_src_caps_str("application/test");
        let buf = gst::Buffer::new();
        let buf = h.push_and_pull(buf);
        assert!(buf.is_ok());
    }
}