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
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
// Copyright (C) 2018 Sebastian Dröge <sebastian@centricular.com>
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use glib;
use glib::object::IsA;
use glib::translate::*;
use glib_sys;
use gobject_sys;
use gst;
use gst::prelude::*;
use gst_check_sys;
use std::marker::PhantomData;
use std::mem;
use std::ops;
use std::path;
use std::ptr;
use TestClock;

/// `Harness` is meant to make writing unit test for GStreamer much easier.
/// It can be thought of as a way of treating a `gst::Element` as a black box,
/// deterministically feeding it data, and controlling what data it outputs.
///
/// The basic structure of `Harness` is two "floating" `GstPads` that connect
/// to the harnessed `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<gst_check_sys::GstHarness>,
    PhantomData<gst_check_sys::GstHarness>,
);

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

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

impl Harness {
    /// Adds a `gst::Element` to an empty `Harness`
    ///
    /// MT safe.
    /// ## `element`
    /// a `gst::Element` to add to the harness (transfer none)
    /// ## `hsrc`
    /// a `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 `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 {
            gst_check_sys::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 `gst::Pad` the `Harness` srcpad.
    ///
    /// MT safe.
    /// ## `sinkpad`
    /// a `gst::Pad` to link to the harness srcpad
    pub fn add_element_sink_pad<P: IsA<gst::Pad>>(&mut self, sinkpad: &P) {
        unsafe {
            gst_check_sys::gst_harness_add_element_sink_pad(
                self.0.as_ptr(),
                sinkpad.as_ref().to_glib_none().0,
            );
        }
    }

    /// Links the specified `gst::Pad` the `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 `gst::Pad` to link to the harness sinkpad
    pub fn add_element_src_pad<P: IsA<gst::Pad>>(&mut self, srcpad: &P) {
        unsafe {
            gst_check_sys::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 `gst::Bin`,
    /// and then attches the supplied `Harness` to the bin.
    ///
    /// MT safe.
    /// ## `launchline`
    /// a `gchar` describing a gst-launch type line
    pub fn add_parse(&mut self, launchline: &str) {
        unsafe {
            gst_check_sys::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
    /// `gst::Pad` of a `gst::Element` that are residing inside the `Harness`,
    /// by using normal gst_pad_add_probe syntax
    ///
    /// MT safe.
    /// ## `element_name`
    /// a `gchar` with a `gst::ElementFactory` name
    /// ## `pad_name`
    /// a `gchar` with the name of the pad to attach the probe to
    /// ## `mask`
    /// a `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.get_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.
    ///
    /// Feature: `v1_16`
    ///
    /// ## `api`
    /// a metadata API
    /// ## `params`
    /// API specific parameters
    #[cfg(any(feature = "v1_16", feature = "dox"))]
    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());
            gst_check_sys::gst_harness_add_propose_allocation_meta(
                self.0.as_ptr(),
                api.to_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 `gst::Element`
    pub fn add_sink(&mut self, sink_element_name: &str) {
        unsafe {
            gst_check_sys::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 `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 `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);
            gst_check_sys::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 `gst::Element`
    pub fn add_sink_parse(&mut self, launchline: &str) {
        unsafe {
            gst_check_sys::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 `gst::Element`
    /// ## `has_clock_wait`
    /// a `gboolean` specifying if the `gst::Element` uses
    /// gst_clock_wait_id internally.
    pub fn add_src(&mut self, src_element_name: &str, has_clock_wait: bool) {
        unsafe {
            gst_check_sys::gst_harness_add_src(
                self.0.as_ptr(),
                src_element_name.to_glib_none().0,
                has_clock_wait.to_glib(),
            );
        }
    }

    /// A src-harness is a great way of providing the `Harness` with data.
    /// By adding a src-type `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 `Harness` to be added as a src-harness.
    /// ## `has_clock_wait`
    /// a `gboolean` specifying if the `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);
            gst_check_sys::gst_harness_add_src_harness(
                self.0.as_ptr(),
                src_harness.0.as_ptr(),
                has_clock_wait.to_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 `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 `gst::Element` uses
    /// gst_clock_wait_id internally.
    pub fn add_src_parse(&mut self, launchline: &str, has_clock_wait: bool) {
        unsafe {
            gst_check_sys::gst_harness_add_src_parse(
                self.0.as_ptr(),
                launchline.to_glib_none().0,
                has_clock_wait.to_glib(),
            );
        }
    }

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

    /// The total number of `GstBuffers` that has arrived on the `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 { gst_check_sys::gst_harness_buffers_received(self.0.as_ptr()) }
    }

    /// Similar to `Harness::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 `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!(
                gst_check_sys::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 `gst::ClockID` to be registered with the `TestClock`.
    /// 2: Advance the `TestClock` to the time the `gst::ClockID` is waiting for.
    /// 3: Release the `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!(
                gst_check_sys::gst_harness_crank_single_clock_wait(self.0.as_ptr()),
                "Failed to crank single clock wait",
            )
        }
    }

    /// Allocates a buffer using a `gst::BufferPool` if present, or else using the
    /// configured `gst::Allocator` and `gst::AllocationParams`
    ///
    /// MT safe.
    /// ## `size`
    /// a `gsize` specifying the size of the buffer
    ///
    /// # Returns
    ///
    /// a `gst::Buffer` of size `size`
    pub fn create_buffer(&mut self, size: usize) -> Result<gst::Buffer, glib::BoolError> {
        unsafe {
            Option::<_>::from_glib_full(gst_check_sys::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 `Harness` sinkpad `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 {
            gst_check_sys::gst_harness_dump_to_file(self.0.as_ptr(), filename.to_glib_none().0);
        }
    }

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

    /// The total number of `GstEvents` that has arrived on the `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 { gst_check_sys::gst_harness_events_received(self.0.as_ptr()) }
    }

    /// Most useful in conjunction with gst_harness_new_parse, this will scan the
    /// `GstElements` inside the `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 `gst::ElementFactory` name
    ///
    /// # Returns
    ///
    /// a `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 = gst_check_sys::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 gobject_sys::g_object_is_floating(ptr as *mut _) != glib_sys::GFALSE {
                gobject_sys::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 gst_check_sys::gst_harness_get() }
    //}

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

    /// Get the timestamp of the last `gst::Buffer` pushed on the `Harness` srcpad,
    /// typically with gst_harness_push or gst_harness_push_from_src.
    ///
    /// MT safe.
    ///
    /// # Returns
    ///
    /// a `gst::ClockTime` with the timestamp or `GST_CLOCK_TIME_NONE` if no
    /// `gst::Buffer` has been pushed on the `Harness` srcpad
    pub fn get_last_pushed_timestamp(&self) -> gst::ClockTime {
        unsafe {
            from_glib(gst_check_sys::gst_harness_get_last_pushed_timestamp(
                self.0.as_ptr(),
            ))
        }
    }

    /// Get the `TestClock`. Useful if specific operations on the testclock is
    /// needed.
    ///
    /// MT safe.
    ///
    /// # Returns
    ///
    /// a `TestClock`, or `None` if the testclock is not
    /// present.
    pub fn get_testclock(&self) -> Option<TestClock> {
        unsafe { from_glib_full(gst_check_sys::gst_harness_get_testclock(self.0.as_ptr())) }
    }

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

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

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

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

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

    /// 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 `gst::Buffer` to push
    ///
    /// # Returns
    ///
    /// a `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(gst_check_sys::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 `gst::Event` on the `Harness` srcpad.
    ///
    /// MT safe.
    /// ## `event`
    /// a `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(gst_check_sys::gst_harness_push_event(
                self.0.as_ptr(),
                event.into_ptr(),
            ))
        }
    }

    /// Transfer data from the src-`Harness` to the main-`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 `gst::Buffer` from the src-`Harness` (see: gst_harness_pull)
    /// 4: Push the same `gst::Buffer` into the main-`Harness` (see: gst_harness_push)
    ///
    /// MT safe.
    ///
    /// # Returns
    ///
    /// a `gst::FlowReturn` with the result of the push
    pub fn push_from_src(&mut self) -> Result<gst::FlowSuccess, gst::FlowError> {
        let ret: gst::FlowReturn =
            unsafe { from_glib(gst_check_sys::gst_harness_push_from_src(self.0.as_ptr())) };
        ret.into_result()
    }

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

    /// Pushes an `gst::Event` on the `Harness` sinkpad.
    ///
    /// MT safe.
    /// ## `event`
    /// a `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(gst_check_sys::gst_harness_push_upstream_event(
                self.0.as_ptr(),
                event.into_ptr(),
            ))
        }
    }

    /// Get the min latency reported by any harnessed `gst::Element`.
    ///
    /// MT safe.
    ///
    /// # Returns
    ///
    /// a `gst::ClockTime` with min latency
    pub fn query_latency(&self) -> gst::ClockTime {
        unsafe { from_glib(gst_check_sys::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 gst_check_sys::gst_harness_set() }
    //}

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

    /// Sets the `Harness` srcpad and sinkpad caps.
    ///
    /// MT safe.
    /// ## `in_`
    /// a `gst::Caps` to set on the harness srcpad
    /// ## `out`
    /// a `gst::Caps` to set on the harness sinkpad
    pub fn set_caps(&mut self, in_: gst::Caps, out: gst::Caps) {
        unsafe {
            gst_check_sys::gst_harness_set_caps(self.0.as_ptr(), in_.into_ptr(), out.into_ptr());
        }
    }

    /// Sets the `Harness` srcpad and sinkpad caps using strings.
    ///
    /// MT safe.
    /// ## `in_`
    /// a `gchar` describing a `gst::Caps` to set on the harness srcpad
    /// ## `out`
    /// a `gchar` describing a `gst::Caps` to set on the harness sinkpad
    pub fn set_caps_str(&mut self, in_: &str, out: &str) {
        unsafe {
            gst_check_sys::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
    /// `gst::Element` inside the sinkpads `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 {
            gst_check_sys::gst_harness_set_drop_buffers(self.0.as_ptr(), drop_buffers.to_glib());
        }
    }

    /// As a convenience, a src-harness will forward `gst::EventType::StreamStart`,
    /// `gst::EventType::Caps` and `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 `gst::QueryType::Allocation`.
    ///
    /// If forwarding is disabled, the user will have to either manually push
    /// these events from the src-harness using `Harness::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 {
            gst_check_sys::gst_harness_set_forwarding(self.0.as_ptr(), forwarding.to_glib());
        }
    }

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

    /// Sets the `Harness` sinkpad caps.
    ///
    /// MT safe.
    /// ## `caps`
    /// a `gst::Caps` to set on the harness sinkpad
    pub fn set_sink_caps(&mut self, caps: gst::Caps) {
        unsafe {
            gst_check_sys::gst_harness_set_sink_caps(self.0.as_ptr(), caps.into_ptr());
        }
    }

    /// Sets the `Harness` sinkpad caps using a string.
    ///
    /// MT safe.
    /// ## `str`
    /// a `gchar` describing a `gst::Caps` to set on the harness sinkpad
    pub fn set_sink_caps_str(&mut self, str: &str) {
        unsafe {
            gst_check_sys::gst_harness_set_sink_caps_str(self.0.as_ptr(), str.to_glib_none().0);
        }
    }

    /// Sets the `Harness` srcpad caps. This must be done before any buffers
    /// can legally be pushed from the harness to the element.
    ///
    /// MT safe.
    /// ## `caps`
    /// a `gst::Caps` to set on the harness srcpad
    pub fn set_src_caps(&mut self, caps: gst::Caps) {
        unsafe {
            gst_check_sys::gst_harness_set_src_caps(self.0.as_ptr(), caps.into_ptr());
        }
    }

    /// Sets the `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 `gst::Caps` to set on the harness srcpad
    pub fn set_src_caps_str(&mut self, str: &str) {
        unsafe {
            gst_check_sys::gst_harness_set_src_caps_str(self.0.as_ptr(), str.to_glib_none().0);
        }
    }

    /// Advance the `TestClock` to a specific time.
    ///
    /// MT safe.
    /// ## `time`
    /// a `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!(
                gst_check_sys::gst_harness_set_time(self.0.as_ptr(), time.to_glib()),
                "Failed to set time",
            )
        }
    }

    /// Sets the min latency reported by `Harness` when receiving a latency-query
    /// ## `latency`
    /// a `gst::ClockTime` specifying the latency
    pub fn set_upstream_latency(&mut self, latency: gst::ClockTime) {
        unsafe {
            gst_check_sys::gst_harness_set_upstream_latency(self.0.as_ptr(), latency.to_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 `gst::FlowReturn` with the result of the push
    pub fn sink_push_many(&mut self, pushes: u32) -> Result<gst::FlowSuccess, gst::FlowError> {
        let ret: gst::FlowReturn = unsafe {
            from_glib(gst_check_sys::gst_harness_sink_push_many(
                self.0.as_ptr(),
                pushes as i32,
            ))
        };
        ret.into_result()
    }

    /// Transfer data from the src-`Harness` to the main-`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 `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> {
        let ret: gst::FlowReturn = unsafe {
            from_glib(gst_check_sys::gst_harness_src_crank_and_push_many(
                self.0.as_ptr(),
                cranks as i32,
                pushes as i32,
            ))
        };
        ret.into_result()
    }

    /// Similar to what gst_harness_src_push does with `GstBuffers`, this transfers
    /// a `gst::Event` from the src-`Harness` to the main-`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(gst_check_sys::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 gst_check_sys::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 gst_check_sys::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 gst_check_sys::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 gst_check_sys::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 gst_check_sys::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 gst_check_sys::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 gst_check_sys::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 gst_check_sys::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 gst_check_sys::gst_harness_stress_requestpad_start_full() }
    //}

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

    /// Pulls all pending data from the harness and returns it as a single buffer.
    ///
    /// Feature: `v1_14`
    ///
    ///
    /// # Returns
    ///
    /// the data as a buffer. Unref with `gst_buffer_unref`
    ///  when no longer needed.
    #[cfg(any(feature = "v1_14", feature = "dox"))]
    pub fn take_all_data_as_buffer(&mut self) -> Result<gst::Buffer, glib::BoolError> {
        unsafe {
            Option::<_>::from_glib_full(gst_check_sys::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 `glib::Bytes`.
    ///
    /// Feature: `v1_14`
    ///
    ///
    /// # Returns
    ///
    /// a pointer to the data, newly allocated. Free
    ///  with `g_free` when no longer needed.
    #[cfg(any(feature = "v1_14", feature = "dox"))]
    pub fn take_all_data_as_bytes(&mut self) -> Result<glib::Bytes, glib::BoolError> {
        unsafe {
            Option::<_>::from_glib_full(gst_check_sys::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 `gst::Buffer` from the `glib::AsyncQueue` on the `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 `gst::Buffer` or `None` if no buffers are present in the `glib::AsyncQueue`
    pub fn try_pull(&mut self) -> Option<gst::Buffer> {
        unsafe { from_glib_full(gst_check_sys::gst_harness_try_pull(self.0.as_ptr())) }
    }

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

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

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

    /// The total number of `GstEvents` that has arrived on the `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 { gst_check_sys::gst_harness_upstream_events_received(self.0.as_ptr()) }
    }

    /// Sets the system `gst::Clock` on the `Harness` `gst::Element`
    ///
    /// MT safe.
    pub fn use_systemclock(&mut self) {
        unsafe {
            gst_check_sys::gst_harness_use_systemclock(self.0.as_ptr());
        }
    }

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

    /// Waits for `timeout` seconds until `waits` number of `gst::ClockID` waits is
    /// registered with the `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 `gst::ClockID` registered with
    /// the `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!(
                gst_check_sys::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 gst_check_sys::GstHarness) -> Harness {
        assert!(!ptr.is_null());

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

    /// Creates a new harness. Works like `Harness::new_with_padnames`, except it
    /// assumes the `gst::Element` sinkpad is named "sink" and srcpad is named "src"
    ///
    /// MT safe.
    /// ## `element_name`
    /// a `gchar` describing the `gst::Element` name
    ///
    /// # Returns
    ///
    /// a `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(gst_check_sys::gst_harness_new(
                element_name.to_glib_none().0,
            ))
        }
    }

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

    /// Creates a new harness.
    ///
    /// MT safe.
    /// ## `element`
    /// a `gst::Element` to attach the harness to (transfer none)
    /// ## `hsrc`
    /// a `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 `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 `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(gst_check_sys::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 `gst::Bin`,
    /// and then attches the harness to the bin.
    ///
    /// MT safe.
    /// ## `launchline`
    /// a `gchar` describing a gst-launch type line
    ///
    /// # Returns
    ///
    /// a `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(gst_check_sys::gst_harness_new_parse(
                launchline.to_glib_none().0,
            ))
        }
    }

    /// Creates a new harness. Works in the same way as `Harness::new_full`, only
    /// that generic padtemplates are used for the harness src and sinkpads, which
    /// will be sufficient in most usecases.
    ///
    /// MT safe.
    /// ## `element`
    /// a `gst::Element` to attach the harness to (transfer none)
    /// ## `element_sinkpad_name`
    /// a `gchar` with the name of the element
    /// sinkpad that is then linked to the harness srcpad. `None` does not attach a
    /// sinkpad
    /// ## `element_srcpad_name`
    /// a `gchar` with the name of the element
    /// srcpad that is then linked to the harness sinkpad. `None` does not attach a
    /// srcpad
    ///
    /// # Returns
    ///
    /// a `Harness`, or `None` if the harness could
    /// not be created
    pub fn new_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(gst_check_sys::gst_harness_new_with_element(
                element.as_ref().to_glib_none().0,
                element_sinkpad_name.0,
                element_srcpad_name.0,
            ))
        }
    }

    /// Creates a new harness. Works like `Harness::new_with_element`,
    /// except you specify the factoryname of the `gst::Element`
    ///
    /// MT safe.
    /// ## `element_name`
    /// a `gchar` describing the `gst::Element` name
    /// ## `element_sinkpad_name`
    /// a `gchar` with the name of the element
    /// sinkpad that is then linked to the harness srcpad. `None` does not attach a
    /// sinkpad
    /// ## `element_srcpad_name`
    /// a `gchar` with the name of the element
    /// srcpad that is then linked to the harness sinkpad. `None` does not attach a
    /// srcpad
    ///
    /// # Returns
    ///
    /// a `Harness`, or `None` if the harness could
    /// not be created
    pub fn new_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(gst_check_sys::gst_harness_new_with_padnames(
                element_name.to_glib_none().0,
                element_sinkpad_name.0,
                element_srcpad_name.0,
            ))
        }
    }

    /// Creates a new harness, like `Harness::new_full`, except it
    /// assumes the `gst::Element` sinkpad is named "sink" and srcpad is named "src"
    ///
    /// MT safe.
    /// ## `element_name`
    /// a `gchar` describing the `gst::Element` name
    /// ## `hsrc`
    /// a `gst::StaticPadTemplate` describing the harness srcpad.
    /// `None` will not create a harness srcpad.
    /// ## `hsink`
    /// a `gst::StaticPadTemplate` describing the harness sinkpad.
    /// `None` will not create a harness sinkpad.
    ///
    /// # Returns
    ///
    /// a `Harness`, or `None` if the harness could
    /// not be created
    pub fn new_with_templates(
        element_name: &str,
        hsrc: Option<&gst::StaticPadTemplate>,
        hsink: Option<&gst::StaticPadTemplate>,
    ) -> Harness {
        assert_initialized_main_thread!();
        unsafe {
            Self::from_glib_full(gst_check_sys::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 gst_check_sys::gst_harness_stress_thread_stop() }
    //}

    pub fn get_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 gobject_sys::g_object_is_floating(ptr as *mut _) != glib_sys::GFALSE {
                gobject_sys::g_object_ref_sink(ptr as *mut _);
            }

            from_glib_none(ptr)
        }
    }

    pub fn get_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 gobject_sys::g_object_is_floating(ptr as *mut _) != glib_sys::GFALSE {
                gobject_sys::g_object_ref_sink(ptr as *mut _);
            }

            from_glib_none(ptr)
        }
    }

    pub fn get_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 gobject_sys::g_object_is_floating(ptr as *mut _) != glib_sys::GFALSE {
                gobject_sys::g_object_ref_sink(ptr as *mut _);
            }

            from_glib_none(ptr)
        }
    }

    pub fn get_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,
                    )),
                    PhantomData,
                ))
            }
        }
    }

    pub fn get_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,
                    )),
                    PhantomData,
                ))
            }
        }
    }

    pub fn get_mut_sink_harness(&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,
                    )),
                    PhantomData,
                ))
            }
        }
    }

    pub fn get_mut_src_harness(&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,
                    )),
                    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());
    }
}