summaryrefslogtreecommitdiff
path: root/ext/dc1394/gstdc1394.c
blob: 58357076278560fc4ffd2536f26317257826da28 (plain)
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
/* GStreamer
 * Copyright (C) <2006> Eric Jonas <jonas@mit.edu>
 * Copyright (C) <2006> Antoine Tremblay <hexa00@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/**
 * SECTION:element-dc1394
 *
 * Source for IIDC (Instrumentation & Industrial Digital Camera) firewire
 * cameras.
 * 
 * <refsect2>
 * <title>Example launch line</title>
 * |[
 * gst-launch -v dc1394 camera-number=0 ! xvimagesink
 * ]|
 * </refsect2>
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "gstdc1394.h"
#include <sys/time.h>
#include <time.h>
#include <string.h>

GST_DEBUG_CATEGORY (dc1394_debug);
#define GST_CAT_DEFAULT dc1394_debug

enum
{
  PROP_0,
  PROP_TIMESTAMP_OFFSET,
  PROP_CAMNUM,
  PROP_BUFSIZE,
  PROP_ISO_SPEED
      /* FILL ME */
};


GST_BOILERPLATE (GstDc1394, gst_dc1394, GstPushSrc, GST_TYPE_PUSH_SRC);

static void gst_dc1394_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec);
static void gst_dc1394_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec);

static GstCaps *gst_dc1394_getcaps (GstBaseSrc * bsrc);
static gboolean gst_dc1394_setcaps (GstBaseSrc * bsrc, GstCaps * caps);
static void gst_dc1394_src_fixate (GstPad * pad, GstCaps * caps);

static void gst_dc1394_get_times (GstBaseSrc * basesrc,
    GstBuffer * buffer, GstClockTime * start, GstClockTime * end);

static GstFlowReturn gst_dc1394_create (GstPushSrc * psrc, GstBuffer ** buffer);

static GstStateChangeReturn
gst_dc1394_change_state (GstElement * element, GstStateChange transition);

static gboolean gst_dc1394_parse_caps (const GstCaps * caps,
    gint * width,
    gint * height,
    gint * rate_numerator, gint * rate_denominator, gint * vmode, gint * bpp);

static gint gst_dc1394_caps_set_format_vmode_caps (GstStructure * st,
    gint mode);
static gboolean gst_dc1394_set_caps_color (GstStructure * gs, gint mc);
static void gst_dc1394_set_caps_framesize (GstStructure * gs, gint width,
    gint height);
static void gst_dc1394_set_caps_framesize_range (GstStructure * gs,
    gint minwidth, gint maxwidth, gint incwidth,
    gint minheight, gint maxheight, gint incheight);

static gint gst_dc1394_caps_set_framerate_list (GstStructure * gs,
    dc1394framerates_t * framerates);
static void gst_dc1394_framerate_const_to_frac (int framerateconst,
    GValue * framefrac);

static GstCaps *gst_dc1394_get_all_dc1394_caps (void);
static GstCaps *gst_dc1394_get_cam_caps (GstDc1394 * src);
static gboolean gst_dc1394_open_cam_with_best_caps (GstDc1394 * src);
static gint gst_dc1394_framerate_frac_to_const (gint num, gint denom);
static void gst_dc1394_framerate_const_to_frac (gint framerateconst,
    GValue * framefrac);
static gboolean
gst_dc1394_change_camera_transmission (GstDc1394 * src, gboolean on);

static void
gst_dc1394_base_init (gpointer g_class)
{
  GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);

  gst_element_class_set_details_simple (element_class, "1394 IIDC Video Source",
      "Source/Video",
      "libdc1394 based source, supports 1394 IIDC cameras",
      "Antoine Tremblay <hexa00@gmail.com>");

  gst_element_class_add_pad_template (element_class,
      gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
          gst_dc1394_get_all_dc1394_caps ()));

}

static void
gst_dc1394_class_init (GstDc1394Class * klass)
{
  GObjectClass *gobject_class;
  GstBaseSrcClass *gstbasesrc_class;
  GstPushSrcClass *gstpushsrc_class;
  GstElementClass *gstelement_class;

  gobject_class = (GObjectClass *) klass;
  gstbasesrc_class = (GstBaseSrcClass *) klass;
  gstpushsrc_class = (GstPushSrcClass *) klass;
  gstelement_class = (GstElementClass *) klass;

  gobject_class->set_property = gst_dc1394_set_property;
  gobject_class->get_property = gst_dc1394_get_property;

  g_object_class_install_property (G_OBJECT_CLASS (klass),
      PROP_TIMESTAMP_OFFSET, g_param_spec_int64 ("timestamp-offset",
          "Timestamp offset",
          "An offset added to timestamps set on buffers (in ns)", G_MININT64,
          G_MAXINT64, 0, G_PARAM_READWRITE));

  g_object_class_install_property (G_OBJECT_CLASS (klass),
      PROP_CAMNUM, g_param_spec_int ("camera-number",
          "The number of the camera on the firewire bus",
          "The number of the camera on the firewire bus", 0,
          G_MAXINT, 0, G_PARAM_READWRITE));

  g_object_class_install_property (G_OBJECT_CLASS (klass),
      PROP_BUFSIZE, g_param_spec_int ("buffer-size",
          "The number of frames in the dma ringbuffer",
          "The number of frames in the dma ringbuffer", 1,
          G_MAXINT, 10, G_PARAM_READWRITE));

  g_object_class_install_property (G_OBJECT_CLASS (klass),
      PROP_ISO_SPEED, g_param_spec_int ("iso-speed",
          "The iso bandwidth in Mbps (100, 200, 400, 800, 1600, 3200)",
          "The iso bandwidth in Mbps (100, 200, 400, 800, 1600, 3200)", 100,
          3200, 400, G_PARAM_READWRITE));

  gstbasesrc_class->get_caps = gst_dc1394_getcaps;
  gstbasesrc_class->set_caps = gst_dc1394_setcaps;

  gstbasesrc_class->get_times = gst_dc1394_get_times;
  gstpushsrc_class->create = gst_dc1394_create;

  gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_dc1394_change_state);

}

static void
gst_dc1394_init (GstDc1394 * src, GstDc1394Class * g_class)
{

  src->segment_start_frame = -1;
  src->segment_end_frame = -1;
  src->timestamp_offset = 0;
  src->caps = gst_dc1394_get_all_dc1394_caps ();
  src->bufsize = 10;
  src->iso_speed = 400;
  src->camnum = 0;
  src->n_frames = 0;

  gst_pad_set_fixatecaps_function (GST_BASE_SRC_PAD (src),
      gst_dc1394_src_fixate);

  gst_base_src_set_live (GST_BASE_SRC (src), TRUE);

}

static void
gst_dc1394_src_fixate (GstPad * pad, GstCaps * caps)
{

  GstDc1394 *src = GST_DC1394 (gst_pad_get_parent (pad));
  GstStructure *structure;
  int i;

  GST_LOG_OBJECT (src, " fixating caps to closest to 320x240 , 30 fps");

  for (i = 0; i < gst_caps_get_size (caps); ++i) {
    structure = gst_caps_get_structure (caps, i);

    gst_structure_fixate_field_nearest_int (structure, "width", 320);
    gst_structure_fixate_field_nearest_int (structure, "height", 240);
    gst_structure_fixate_field_nearest_fraction (structure, "framerate", 30, 1);
  }
  gst_object_unref (GST_OBJECT (src));
}

static void
gst_dc1394_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec)
{
  GstDc1394 *src = GST_DC1394 (object);

  switch (prop_id) {
    case PROP_TIMESTAMP_OFFSET:
      src->timestamp_offset = g_value_get_int64 (value);
      break;
    case PROP_CAMNUM:
      src->camnum = g_value_get_int (value);
      break;
    case PROP_BUFSIZE:
      src->bufsize = g_value_get_int (value);
      break;
    case PROP_ISO_SPEED:
      switch (g_value_get_int (value)) {
        case 100:
        case 200:
        case 300:
        case 400:
        case 800:
        case 1600:
        case 3200:
          // fallthrough
          src->iso_speed = g_value_get_int (value);
          break;
        default:
          g_warning ("%s: Invalid iso speed %d, ignoring",
              GST_ELEMENT_NAME (src), g_value_get_int (value));
          break;
      }
    default:
      break;
  }
}

static void
gst_dc1394_get_property (GObject * object, guint prop_id, GValue * value,
    GParamSpec * pspec)
{
  GstDc1394 *src = GST_DC1394 (object);

  switch (prop_id) {
    case PROP_TIMESTAMP_OFFSET:
      g_value_set_int64 (value, src->timestamp_offset);
      break;
    case PROP_CAMNUM:
      g_value_set_int (value, src->camnum);
      break;
    case PROP_BUFSIZE:
      g_value_set_int (value, src->bufsize);
      break;
    case PROP_ISO_SPEED:
      g_value_set_int (value, src->iso_speed);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static GstCaps *
gst_dc1394_getcaps (GstBaseSrc * bsrc)
{
  GstDc1394 *gsrc;

  gsrc = GST_DC1394 (bsrc);

  g_return_val_if_fail (gsrc->caps, NULL);

  return gst_caps_copy (gsrc->caps);
}


static gboolean
gst_dc1394_setcaps (GstBaseSrc * bsrc, GstCaps * caps)
{

  gboolean res = TRUE;
  GstDc1394 *dc1394;
  gint width, height, rate_denominator, rate_numerator;
  gint bpp, vmode;

  dc1394 = GST_DC1394 (bsrc);

  if (dc1394->caps) {
    gst_caps_unref (dc1394->caps);
  }

  dc1394->caps = gst_caps_copy (caps);

  res = gst_dc1394_parse_caps (caps, &width, &height,
      &rate_numerator, &rate_denominator, &vmode, &bpp);

  if (res) {
    /* looks ok here */
    dc1394->width = width;
    dc1394->height = height;
    dc1394->vmode = vmode;
    dc1394->rate_numerator = rate_numerator;
    dc1394->rate_denominator = rate_denominator;
    dc1394->bpp = bpp;
  }

  return res;
}

static void
gst_dc1394_get_times (GstBaseSrc * basesrc, GstBuffer * buffer,
    GstClockTime * start, GstClockTime * end)
{
  /* for live sources, sync on the timestamp of the buffer */
  if (gst_base_src_is_live (basesrc)) {
    GstClockTime timestamp = GST_BUFFER_TIMESTAMP (buffer);

    if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
      /* get duration to calculate end time */
      GstClockTime duration = GST_BUFFER_DURATION (buffer);

      if (GST_CLOCK_TIME_IS_VALID (duration)) {
        *end = timestamp + duration;
      }
      *start = timestamp;
    }
  } else {
    *start = -1;
    *end = -1;
  }
}

static GstFlowReturn
gst_dc1394_create (GstPushSrc * psrc, GstBuffer ** buffer)
{
  GstDc1394 *src;
  GstBuffer *outbuf;
  GstCaps *caps;
  dc1394video_frame_t *frame[1];
  GstFlowReturn res = GST_FLOW_OK;
  dc1394error_t err;

  src = GST_DC1394 (psrc);

  err = dc1394_capture_dequeue (src->camera, DC1394_CAPTURE_POLICY_WAIT, frame);

  if (err != DC1394_SUCCESS) {
    GST_ELEMENT_ERROR (src, RESOURCE, FAILED,
        ("failed to dequeue frame"), ("failed to dequeue frame"));
    goto error;
  }

  outbuf = gst_buffer_new_and_alloc (frame[0]->image_bytes);

  memcpy (GST_BUFFER_MALLOCDATA (outbuf), (guchar *) frame[0]->image,
      frame[0]->image_bytes * sizeof (guchar));

  GST_BUFFER_DATA (outbuf) = GST_BUFFER_MALLOCDATA (outbuf);

  caps = gst_pad_get_caps (GST_BASE_SRC_PAD (psrc));
  gst_buffer_set_caps (outbuf, caps);
  gst_caps_unref (caps);

  GST_BUFFER_TIMESTAMP (outbuf) = src->timestamp_offset + src->running_time;
  if (src->rate_numerator != 0) {
    GST_BUFFER_DURATION (outbuf) = gst_util_uint64_scale_int (GST_SECOND,
        src->rate_denominator, src->rate_numerator);
  }

  src->n_frames++;
  if (src->rate_numerator != 0) {
    src->running_time = gst_util_uint64_scale_int (src->n_frames * GST_SECOND,
        src->rate_denominator, src->rate_numerator);
  }

  if (dc1394_capture_enqueue (src->camera, frame[0]) != DC1394_SUCCESS) {
    GST_ELEMENT_ERROR (src, RESOURCE, FAILED, ("failed to enqueue frame"),
        ("failed to enqueue frame"));
    goto error;
  }

  *buffer = outbuf;

  return res;

error:
  {
    return GST_FLOW_ERROR;
  }
}


static gboolean
gst_dc1394_parse_caps (const GstCaps * caps,
    gint * width,
    gint * height,
    gint * rate_numerator, gint * rate_denominator, gint * vmode, gint * bpp)
{
  const GstStructure *structure;
  GstPadLinkReturn ret;
  const GValue *framerate;

  if (gst_caps_get_size (caps) < 1)
    return FALSE;

  structure = gst_caps_get_structure (caps, 0);

  ret = gst_structure_get_int (structure, "width", width);
  ret &= gst_structure_get_int (structure, "height", height);

  framerate = gst_structure_get_value (structure, "framerate");

  ret &= gst_structure_get_int (structure, "vmode", vmode);

  ret &= gst_structure_get_int (structure, "bpp", bpp);


  if (framerate) {
    *rate_numerator = gst_value_get_fraction_numerator (framerate);
    *rate_denominator = gst_value_get_fraction_denominator (framerate);
  } else {
    ret = FALSE;
  }

  return ret;
}

static GstStateChangeReturn
gst_dc1394_change_state (GstElement * element, GstStateChange transition)
{
  GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
  GstDc1394 *src = GST_DC1394 (element);

  switch (transition) {
    case GST_STATE_CHANGE_NULL_TO_READY:
      GST_LOG_OBJECT (src, "State change null to ready");
      src->dc1394 = dc1394_new ();
      break;
    case GST_STATE_CHANGE_READY_TO_PAUSED:
      GST_LOG_OBJECT (src, "State ready to paused");

      if (src->caps) {
        gst_caps_unref (src->caps);
        src->caps = NULL;
      }
      src->caps = gst_dc1394_get_cam_caps (src);
      if (src->caps == NULL) {
        GST_LOG_OBJECT (src,
            "Error : Set property  could not get cam caps ! , reverting to default");
        src->caps = gst_dc1394_get_all_dc1394_caps ();
        ret = GST_STATE_CHANGE_FAILURE;
      }

      break;
    case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
      GST_LOG_OBJECT (src, "State change paused to playing");

      if (!gst_dc1394_open_cam_with_best_caps (src)) {
        ret = GST_STATE_CHANGE_FAILURE;
      }

      if (src->camera && !gst_dc1394_change_camera_transmission (src, TRUE)) {
        ret = GST_STATE_CHANGE_FAILURE;
      }

      break;
    default:
      break;
  }
  if (ret == GST_STATE_CHANGE_FAILURE)
    return ret;

  ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);

  switch (transition) {
    case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
      GST_LOG_OBJECT (src, "State change playing to paused");
      break;
    case GST_STATE_CHANGE_PAUSED_TO_READY:
      GST_LOG_OBJECT (src, "State change paused to ready");

      if (src->camera && !gst_dc1394_change_camera_transmission (src, FALSE)) {

        if (src->camera) {
          dc1394_camera_free (src->camera);
        }
        src->camera = NULL;

        if (src->caps) {
          gst_caps_unref (src->caps);
          src->caps = NULL;
        }

        ret = GST_STATE_CHANGE_FAILURE;
      }

      break;
    case GST_STATE_CHANGE_READY_TO_NULL:
      GST_LOG_OBJECT (src, "State change ready to null");
      if (src->camera) {
        dc1394_camera_free (src->camera);
      }
      src->camera = NULL;

      if (src->dc1394) {
        dc1394_free (src->dc1394);
      }
      src->dc1394 = NULL;

      if (src->caps) {
        gst_caps_unref (src->caps);
        src->caps = NULL;
      }
      break;
    default:
      break;
  }

  return ret;
}


static gint
gst_dc1394_caps_set_format_vmode_caps (GstStructure * gs, gint mode)
{
  gint retval = 0;

  switch (mode) {
    case DC1394_VIDEO_MODE_160x120_YUV444:
      gst_dc1394_set_caps_color (gs, DC1394_COLOR_CODING_YUV444);
      gst_dc1394_set_caps_framesize (gs, 160, 120);
      break;
    case DC1394_VIDEO_MODE_320x240_YUV422:
      gst_dc1394_set_caps_color (gs, DC1394_COLOR_CODING_YUV422);
      gst_dc1394_set_caps_framesize (gs, 320, 240);
      break;
    case DC1394_VIDEO_MODE_640x480_YUV411:
      gst_dc1394_set_caps_color (gs, DC1394_COLOR_CODING_YUV411);
      gst_dc1394_set_caps_framesize (gs, 640, 480);
      break;
    case DC1394_VIDEO_MODE_640x480_YUV422:
      gst_dc1394_set_caps_color (gs, DC1394_COLOR_CODING_YUV422);
      gst_dc1394_set_caps_framesize (gs, 640, 480);
      break;
    case DC1394_VIDEO_MODE_640x480_RGB8:
      gst_dc1394_set_caps_color (gs, DC1394_COLOR_CODING_RGB8);
      gst_dc1394_set_caps_framesize (gs, 640, 480);
      break;
    case DC1394_VIDEO_MODE_640x480_MONO8:
      gst_dc1394_set_caps_color (gs, DC1394_COLOR_CODING_MONO8);
      gst_dc1394_set_caps_framesize (gs, 640, 480);
      break;
    case DC1394_VIDEO_MODE_640x480_MONO16:
      gst_dc1394_set_caps_color (gs, DC1394_COLOR_CODING_MONO16);
      gst_dc1394_set_caps_framesize (gs, 640, 480);
      break;
    case DC1394_VIDEO_MODE_800x600_YUV422:
      gst_dc1394_set_caps_color (gs, DC1394_COLOR_CODING_YUV422);
      gst_dc1394_set_caps_framesize (gs, 800, 600);
      break;
    case DC1394_VIDEO_MODE_800x600_RGB8:
      gst_dc1394_set_caps_color (gs, DC1394_COLOR_CODING_RGB8);
      gst_dc1394_set_caps_framesize (gs, 800, 600);
      break;
    case DC1394_VIDEO_MODE_800x600_MONO8:
      gst_dc1394_set_caps_color (gs, DC1394_COLOR_CODING_MONO8);
      gst_dc1394_set_caps_framesize (gs, 800, 600);
      break;
    case DC1394_VIDEO_MODE_1024x768_YUV422:
      gst_dc1394_set_caps_color (gs, DC1394_COLOR_CODING_YUV422);
      gst_dc1394_set_caps_framesize (gs, 1024, 768);
      break;
    case DC1394_VIDEO_MODE_1024x768_RGB8:
      gst_dc1394_set_caps_color (gs, DC1394_COLOR_CODING_RGB8);
      gst_dc1394_set_caps_framesize (gs, 1024, 768);
      break;
    case DC1394_VIDEO_MODE_1024x768_MONO8:
      gst_dc1394_set_caps_color (gs, DC1394_COLOR_CODING_MONO8);
      gst_dc1394_set_caps_framesize (gs, 1024, 768);
      break;
    case DC1394_VIDEO_MODE_800x600_MONO16:
      gst_dc1394_set_caps_color (gs, DC1394_COLOR_CODING_MONO16);
      gst_dc1394_set_caps_framesize (gs, 800, 600);
      break;
    case DC1394_VIDEO_MODE_1024x768_MONO16:
      gst_dc1394_set_caps_color (gs, DC1394_COLOR_CODING_MONO16);
      gst_dc1394_set_caps_framesize (gs, 1024, 768);
      break;
    case DC1394_VIDEO_MODE_1280x960_YUV422:
      gst_dc1394_set_caps_color (gs, DC1394_COLOR_CODING_YUV422);
      gst_dc1394_set_caps_framesize (gs, 1280, 960);
      break;
    case DC1394_VIDEO_MODE_1280x960_RGB8:
      gst_dc1394_set_caps_color (gs, DC1394_COLOR_CODING_RGB8);
      gst_dc1394_set_caps_framesize (gs, 1280, 960);
      break;
    case DC1394_VIDEO_MODE_1280x960_MONO8:
      gst_dc1394_set_caps_color (gs, DC1394_COLOR_CODING_MONO8);
      gst_dc1394_set_caps_framesize (gs, 1280, 960);
      break;
    case DC1394_VIDEO_MODE_1600x1200_YUV422:
      gst_dc1394_set_caps_color (gs, DC1394_COLOR_CODING_YUV422);
      gst_dc1394_set_caps_framesize (gs, 1600, 1200);
      break;
    case DC1394_VIDEO_MODE_1600x1200_RGB8:
      gst_dc1394_set_caps_color (gs, DC1394_COLOR_CODING_RGB8);
      gst_dc1394_set_caps_framesize (gs, 1600, 1200);
      break;
    case DC1394_VIDEO_MODE_1600x1200_MONO8:
      gst_dc1394_set_caps_color (gs, DC1394_COLOR_CODING_MONO8);
      gst_dc1394_set_caps_framesize (gs, 1600, 1200);
      break;
    case DC1394_VIDEO_MODE_1280x960_MONO16:
      gst_dc1394_set_caps_color (gs, DC1394_COLOR_CODING_MONO16);
      gst_dc1394_set_caps_framesize (gs, 1280, 960);
      break;
    case DC1394_VIDEO_MODE_1600x1200_MONO16:
      gst_dc1394_set_caps_color (gs, DC1394_COLOR_CODING_MONO8);
      gst_dc1394_set_caps_framesize (gs, 1600, 1200);
      break;

    default:
      retval = -1;
  }

  return retval;

}


static gboolean
gst_dc1394_set_caps_color (GstStructure * gs, gint mc)
{
  gboolean ret = TRUE;
  gint fourcc;

  switch (mc) {
    case DC1394_COLOR_CODING_YUV444:
      gst_structure_set_name (gs, "video/x-raw-yuv");

      fourcc = GST_MAKE_FOURCC ('I', 'Y', 'U', '2');
      gst_structure_set (gs,
          "format", GST_TYPE_FOURCC, fourcc, "bpp", G_TYPE_INT, 16, NULL);
      break;

    case DC1394_COLOR_CODING_YUV422:
      gst_structure_set_name (gs, "video/x-raw-yuv");
      fourcc = GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y');
      gst_structure_set (gs,
          "format", GST_TYPE_FOURCC, fourcc, "bpp", G_TYPE_INT, 16, NULL);
      break;

    case DC1394_COLOR_CODING_YUV411:
      gst_structure_set_name (gs, "video/x-raw-yuv");
      fourcc = GST_MAKE_FOURCC ('I', 'Y', 'U', '1');
      gst_structure_set (gs,
          "format", GST_TYPE_FOURCC, fourcc, "bpp", G_TYPE_INT, 12, NULL);
      break;
    case DC1394_COLOR_CODING_RGB8:
      gst_structure_set_name (gs, "video/x-raw-rgb");
      gst_structure_set (gs,
          "bpp", G_TYPE_INT, 24,
          "depth", G_TYPE_INT, 24,
          "endianness", G_TYPE_INT, G_BIG_ENDIAN,
          "red_mask", G_TYPE_INT, 0xFF0000,
          "green_mask", G_TYPE_INT, 0x00FF00,
          "blue_mask", G_TYPE_INT, 0x0000FF, NULL);
      break;
    case DC1394_COLOR_CODING_MONO8:
      gst_structure_set_name (gs, "video/x-raw-gray");
      gst_structure_set (gs,
          "bpp", G_TYPE_INT, 8, "depth", G_TYPE_INT, 8, NULL);

      break;
    case DC1394_COLOR_CODING_MONO16:
      gst_structure_set_name (gs, "video/x-raw-gray");
      gst_structure_set (gs,
          "bpp", G_TYPE_INT, 16, "depth", G_TYPE_INT, 16, NULL);
      // there is no fourcc for this format
      break;
    default:
      GST_DEBUG ("Ignoring unsupported color format %d", mc);
      ret = FALSE;
      break;
  }
  return ret;
}


static void
gst_dc1394_set_caps_framesize (GstStructure * gs, gint width, gint height)
{
  gst_structure_set (gs,
      "width", G_TYPE_INT, width, "height", G_TYPE_INT, height, NULL);
}

static void
gst_dc1394_set_caps_framesize_range (GstStructure * gs,
    gint minwidth,
    gint maxwidth,
    gint incwidth, gint minheight, gint maxheight, gint incheight)
{
  /* 
     Format 7 cameras allow you to change the camera width/height in multiples
     of incwidth/incheight up to some max. This sets the necessary
     list structure in the gst caps structure
   */

  GValue widthlist = { 0 };
  GValue widthval = { 0 };
  GValue heightlist = { 0 };
  GValue heightval = { 0 };
  gint x = 0;

  g_value_init (&widthlist, GST_TYPE_LIST);
  g_value_init (&widthval, G_TYPE_INT);
  for (x = minwidth; x <= maxwidth; x += incwidth) {
    g_value_set_int (&widthval, x);
    gst_value_list_append_value (&widthlist, &widthval);
  }
  gst_structure_set_value (gs, "width", &widthlist);

  g_value_unset (&widthlist);
  g_value_unset (&widthval);

  g_value_init (&heightlist, GST_TYPE_LIST);
  g_value_init (&heightval, G_TYPE_INT);
  for (x = minheight; x <= maxheight; x += incheight) {
    g_value_set_int (&heightval, x);
    gst_value_list_append_value (&heightlist, &heightval);
  }

  gst_structure_set_value (gs, "height", &heightlist);

  g_value_unset (&heightlist);
  g_value_unset (&heightval);
}


static gint
gst_dc1394_caps_set_framerate_list (GstStructure * gs,
    dc1394framerates_t * framerates)
{
  GValue framefrac = { 0 };
  GValue frameratelist = { 0 };
  gint f;

  g_value_init (&frameratelist, GST_TYPE_LIST);
  g_value_init (&framefrac, GST_TYPE_FRACTION);

  // figure out the frame rate
  for (f = framerates->num - 1; f >= 0; f--) {
    /* reverse order so we place the faster frame rates higher in 
       the sequence */
    if (framerates->framerates[f]) {
      gst_dc1394_framerate_const_to_frac (framerates->framerates[f],
          &framefrac);

      gst_value_list_append_value (&frameratelist, &framefrac);
    }
  }
  gst_structure_set_value (gs, "framerate", &frameratelist);

  g_value_unset (&framefrac);
  g_value_unset (&frameratelist);
  return 0;
}



static void
gst_dc1394_framerate_const_to_frac (gint framerateconst, GValue * framefrac)
{

  // frac must have been already initialized

  switch (framerateconst) {
    case DC1394_FRAMERATE_1_875:
      gst_value_set_fraction (framefrac, 15, 8);
      break;
    case DC1394_FRAMERATE_3_75:
      gst_value_set_fraction (framefrac, 15, 4);
      break;
    case DC1394_FRAMERATE_7_5:
      gst_value_set_fraction (framefrac, 15, 2);
      break;
    case DC1394_FRAMERATE_15:
      gst_value_set_fraction (framefrac, 15, 1);
      break;
    case DC1394_FRAMERATE_30:
      gst_value_set_fraction (framefrac, 30, 1);
      break;
    case DC1394_FRAMERATE_60:
      gst_value_set_fraction (framefrac, 60, 1);
      break;
    case DC1394_FRAMERATE_120:
      gst_value_set_fraction (framefrac, 120, 1);
      break;
    case DC1394_FRAMERATE_240:
      gst_value_set_fraction (framefrac, 240, 1);
      break;
  }
}

static GstCaps *
gst_dc1394_get_all_dc1394_caps (void)
{
  /* 
     generate all possible caps

   */

  GstCaps *gcaps;
  gint i = 0;

  gcaps = gst_caps_new_empty ();
  // first, the fixed mode caps
  for (i = DC1394_VIDEO_MODE_MIN; i < DC1394_VIDEO_MODE_EXIF; i++) {
    GstStructure *gs = gst_structure_empty_new ("video");
    gint ret = gst_dc1394_caps_set_format_vmode_caps (gs, i);

    gst_structure_set (gs,
        "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
    gst_structure_set (gs, "vmode", G_TYPE_INT, i, NULL);
    if (ret >= 0) {
      gst_caps_append_structure (gcaps, gs);
    }
  }

  // then Format 7 options

  for (i = DC1394_COLOR_CODING_MIN; i <= DC1394_COLOR_CODING_MAX; i++) {
    GstStructure *gs = gst_structure_empty_new ("video");

    //int ret =  gst_dc1394_caps_set_format_vmode_caps(gs, i); 

    gst_structure_set (gs, "vmode", GST_TYPE_INT_RANGE, 1, G_MAXINT, NULL);

    gst_structure_set (gs,
        "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
    gst_structure_set (gs,
        "width", GST_TYPE_INT_RANGE, 1, G_MAXINT,
        "height", GST_TYPE_INT_RANGE, 1, G_MAXINT, NULL);

    if (gst_dc1394_set_caps_color (gs, i)) {
      gst_caps_append_structure (gcaps, gs);
    }
  }
  return gcaps;

}

GstCaps *
gst_dc1394_get_cam_caps (GstDc1394 * src)
{

  dc1394camera_t *camera = NULL;
  dc1394camera_list_t *cameras = NULL;
  dc1394error_t camerr;
  gint i, j;
  dc1394video_modes_t modes;
  dc1394framerates_t framerates;
  GstCaps *gcaps = NULL;

  gcaps = gst_caps_new_empty ();

  camerr = dc1394_camera_enumerate (src->dc1394, &cameras);

  if (camerr != DC1394_SUCCESS || cameras == NULL) {
    GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND,
        ("Can't find cameras error : %d", camerr),
        ("Can't find cameras error : %d", camerr));
    goto error;
  }

  if (cameras->num == 0) {
    GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, ("There were no cameras"),
        ("There were no cameras"));
    goto error;
  }

  if (src->camnum > (cameras->num - 1)) {
    GST_ELEMENT_ERROR (src, RESOURCE, FAILED, ("Invalid camera number"),
        ("Invalid camera number %d", src->camnum));
    goto error;
  }

  camera =
      dc1394_camera_new_unit (src->dc1394, cameras->ids[src->camnum].guid,
      cameras->ids[src->camnum].unit);

  dc1394_camera_free_list (cameras);
  cameras = NULL;

  camerr = dc1394_video_get_supported_modes (camera, &modes);
  if (camerr != DC1394_SUCCESS) {
    GST_ELEMENT_ERROR (src, RESOURCE, FAILED, ("Error getting supported modes"),
        ("Error getting supported modes"));
    goto error;
  }

  for (i = modes.num - 1; i >= 0; i--) {
    int m = modes.modes[i];

    if (m < DC1394_VIDEO_MODE_EXIF) {

      GstStructure *gs = gst_structure_empty_new ("video");

      gst_structure_set (gs, "vmode", G_TYPE_INT, m, NULL);

      if (gst_dc1394_caps_set_format_vmode_caps (gs, m) < 0) {
        GST_ELEMENT_ERROR (src, STREAM, FAILED,
            ("attempt to set mode to %d failed", m),
            ("attempt to set mode to %d failed", m));
        goto error;
      } else {

        camerr = dc1394_video_get_supported_framerates (camera, m, &framerates);
        gst_dc1394_caps_set_framerate_list (gs, &framerates);
        gst_caps_append_structure (gcaps, gs);

      }
    } else {
      // FORMAT 7
      guint maxx, maxy;
      GstStructure *gs = gst_structure_empty_new ("video");
      dc1394color_codings_t colormodes;
      guint xunit, yunit;

      gst_structure_set (gs, "vmode", G_TYPE_INT, m, NULL);

      // Get the maximum frame size
      camerr = dc1394_format7_get_max_image_size (camera, m, &maxx, &maxy);
      if (camerr != DC1394_SUCCESS) {
        GST_ELEMENT_ERROR (src, RESOURCE, FAILED,
            ("Error getting format 7 max image size"),
            ("Error getting format 7 max image size"));
        goto error;
      }
      GST_LOG_OBJECT (src, "Format 7 maxx=%d maxy=%d", maxx, maxy);

      camerr = dc1394_format7_get_unit_size (camera, m, &xunit, &yunit);
      if (camerr != DC1394_SUCCESS) {
        GST_ELEMENT_ERROR (src, RESOURCE, FAILED,
            ("Error getting format 7 image unit size"),
            ("Error getting format 7 image unit size"));
        goto error;
      }
      GST_LOG_OBJECT (src, "Format 7 unitx=%d unity=%d", xunit, yunit);

      gst_dc1394_set_caps_framesize_range (gs, xunit, maxx, xunit,
          yunit, maxy, yunit);

      // note that format 7 has no concept of a framerate, so we pass the 
      // full range
      gst_structure_set (gs,
          "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);

      // get the available color codings
      camerr = dc1394_format7_get_color_codings (camera, m, &colormodes);
      if (camerr != DC1394_SUCCESS) {
        GST_ELEMENT_ERROR (src, RESOURCE, FAILED,
            ("Error getting format 7 color modes"),
            ("Error getting format 7 color modes"));
        goto error;
      }

      for (j = 0; j < colormodes.num; j++) {
        GstStructure *newgs = gst_structure_copy (gs);

        gst_dc1394_set_caps_color (newgs, colormodes.codings[j]);
        GST_LOG_OBJECT (src, "Format 7 colormode set : %d",
            colormodes.codings[j]);
        // note that since there are multiple color modes, we append
        // multiple structures.
        gst_caps_append_structure (gcaps, newgs);
      }
    }
  }

  if (camera) {
    dc1394_camera_free (camera);
  }

  return gcaps;

error:

  if (gcaps) {
    gst_caps_unref (gcaps);
  }

  if (cameras) {
    dc1394_camera_free_list (cameras);
    cameras = NULL;
  }

  if (camera) {
    dc1394_camera_free (camera);
    camera = NULL;
  }

  return NULL;
}

static gint
gst_dc1394_framerate_frac_to_const (gint num, gint denom)
{
  // frac must have been already initialized
  int retvalue = -1;

  if (num == 15 && denom == 8)
    retvalue = DC1394_FRAMERATE_1_875;

  if (num == 15 && denom == 4)
    retvalue = DC1394_FRAMERATE_3_75;

  if (num == 15 && denom == 2)
    retvalue = DC1394_FRAMERATE_7_5;

  if (num == 15 && denom == 1)
    retvalue = DC1394_FRAMERATE_15;


  if (num == 30 && denom == 1)
    retvalue = DC1394_FRAMERATE_30;

  if (num == 60 && denom == 1)
    retvalue = DC1394_FRAMERATE_60;

  return retvalue;
}


static gboolean
gst_dc1394_open_cam_with_best_caps (GstDc1394 * src)
{
  dc1394camera_list_t *cameras = NULL;
  gint err = 0;
  int framerateconst;

  GST_LOG_OBJECT (src, "Opening the camera!!!");


  if (dc1394_camera_enumerate (src->dc1394, &cameras) != DC1394_SUCCESS) {
    GST_ELEMENT_ERROR (src, RESOURCE, FAILED, ("Can't find cameras"),
        ("Can't find cameras"));
    goto error;
  }

  GST_LOG_OBJECT (src, "Found  %d  cameras", cameras->num);

  if (src->camnum > (cameras->num - 1)) {
    GST_ELEMENT_ERROR (src, RESOURCE, FAILED, ("Invalid camera number"),
        ("Invalid camera number"));
    goto error;
  }

  GST_LOG_OBJECT (src, "Opening camera : %d", src->camnum);

  src->camera =
      dc1394_camera_new_unit (src->dc1394, cameras->ids[src->camnum].guid,
      cameras->ids[src->camnum].unit);

  dc1394_camera_free_list (cameras);
  cameras = NULL;

  // figure out mode
  framerateconst = gst_dc1394_framerate_frac_to_const (src->rate_numerator,
      src->rate_denominator);

  GST_LOG_OBJECT (src, "The dma buffer queue size is %d  buffers",
      src->bufsize);

  switch (src->iso_speed) {
    case 100:
      err = dc1394_video_set_iso_speed (src->camera, DC1394_ISO_SPEED_100);
      break;
    case 200:
      err = dc1394_video_set_iso_speed (src->camera, DC1394_ISO_SPEED_200);
      break;
    case 400:
      err = dc1394_video_set_iso_speed (src->camera, DC1394_ISO_SPEED_400);
      break;
    case 800:
      if (src->camera->bmode_capable > 0) {
        dc1394_video_set_operation_mode (src->camera,
            DC1394_OPERATION_MODE_1394B);
        err = dc1394_video_set_iso_speed (src->camera, DC1394_ISO_SPEED_800);
      }
      break;
    case 1600:
      if (src->camera->bmode_capable > 0) {
        dc1394_video_set_operation_mode (src->camera,
            DC1394_OPERATION_MODE_1394B);
        err = dc1394_video_set_iso_speed (src->camera, DC1394_ISO_SPEED_1600);
      }
      break;
    case 3200:
      if (src->camera->bmode_capable > 0) {
        dc1394_video_set_operation_mode (src->camera,
            DC1394_OPERATION_MODE_1394B);
        err = dc1394_video_set_iso_speed (src->camera, DC1394_ISO_SPEED_3200);
      }
      break;
    default:
      GST_ELEMENT_ERROR (src, RESOURCE, FAILED, ("Invalid ISO speed"),
          ("Invalid ISO speed"));
      goto error;
      break;
  }

  if (err != DC1394_SUCCESS) {
    GST_ELEMENT_ERROR (src, RESOURCE, FAILED, ("Could not set ISO speed"),
        ("Could not set ISO speed"));
    goto error;
  }

  GST_LOG_OBJECT (src, "Setting mode :  %d", src->vmode);
  err = dc1394_video_set_mode (src->camera, src->vmode);

  if (err != DC1394_SUCCESS) {
    GST_ELEMENT_ERROR (src, RESOURCE, FAILED, ("Could not set video mode %d",
            src->vmode), ("Could not set video mode %d", src->vmode));
    goto error;
  }

  GST_LOG_OBJECT (src, "Setting framerate :  %d", framerateconst);
  dc1394_video_set_framerate (src->camera, framerateconst);

  if (err != DC1394_SUCCESS) {
    GST_ELEMENT_ERROR (src, RESOURCE, FAILED, ("Could not set framerate to %d",
            framerateconst), ("Could not set framerate to %d", framerateconst));
    goto error;
  }
  // set any format-7 parameters if this is a format-7 mode
  if (src->vmode >= DC1394_VIDEO_MODE_FORMAT7_MIN &&
      src->vmode <= DC1394_VIDEO_MODE_FORMAT7_MAX) {
    // the big thing we care about right now is frame size
    err = dc1394_format7_set_image_size (src->camera, src->vmode,
        src->width, src->height);
    if (err != DC1394_SUCCESS) {
      GST_ELEMENT_ERROR (src, RESOURCE, FAILED,
          ("Could not set format 7 image size to %d x %d", src->width,
              src->height), ("Could not set format 7 image size to %d x %d",
              src->width, src->height));

      goto error;
    }

  }
  err = dc1394_capture_setup (src->camera, src->bufsize,
      DC1394_CAPTURE_FLAGS_DEFAULT);
  if (err != DC1394_SUCCESS) {
    GST_ELEMENT_ERROR (src, RESOURCE, FAILED, ("Error setting capture mode"),
        ("Error setting capture mode"));
  }
  if (err != DC1394_SUCCESS) {
    if (err == DC1394_NO_BANDWIDTH) {
      GST_LOG_OBJECT (src, "Capture setup_dma failed."
          "Trying to cleanup the iso_channels_and_bandwidth and retrying");

      // try to cleanup the bandwidth and retry 
      err = dc1394_iso_release_all (src->camera);
      if (err != DC1394_SUCCESS) {
        GST_ELEMENT_ERROR (src, RESOURCE, FAILED,
            ("Could not cleanup bandwidth"), ("Could not cleanup bandwidth"));
        goto error;
      } else {
        err =
            dc1394_capture_setup (src->camera, src->bufsize,
            DC1394_CAPTURE_FLAGS_DEFAULT);
        if (err != DC1394_SUCCESS) {
          GST_ELEMENT_ERROR (src, RESOURCE, FAILED,
              ("unable to setup camera error %d", err),
              ("unable to setup camera error %d", err));
          goto error;
        }
      }
    } else {
      GST_ELEMENT_ERROR (src, RESOURCE, FAILED,
          ("unable to setup camera error %d", err),
          ("unable to setup camera error %d", err));
      goto error;

    }
  }


  return TRUE;

error:

  if (src->camera) {
    dc1394_camera_free (src->camera);
    src->camera = NULL;
  }

  return FALSE;;

}


gboolean
gst_dc1394_change_camera_transmission (GstDc1394 * src, gboolean on)
{
  dc1394switch_t status = DC1394_OFF;
  dc1394error_t err = DC1394_FAILURE;
  gint i = 0;

  g_return_val_if_fail (src->camera, FALSE);

  if (on) {

    status = dc1394_video_set_transmission (src->camera, DC1394_ON);

    i = 0;
    while (status == DC1394_OFF && i++ < 5) {
      g_usleep (50000);
      if (dc1394_video_get_transmission (src->camera,
              &status) != DC1394_SUCCESS) {
        if (status == DC1394_OFF) {
          GST_LOG_OBJECT (src, "camera is still off , retrying");
        }
      }
    }

    if (i == 5) {
      GST_ELEMENT_ERROR (src, RESOURCE, FAILED,
          ("Camera doesn't seem to want to turn on!"),
          ("Camera doesn't seem to want to turn on!"));
      return FALSE;
    }

    GST_LOG_OBJECT (src, "got transmision status ON");

  } else {

    if (dc1394_video_set_transmission (src->camera,
            DC1394_OFF) != DC1394_SUCCESS) {
      GST_ELEMENT_ERROR (src, RESOURCE, FAILED, ("Unable to stop transmision"),
          ("Unable to stop transmision"));
      return FALSE;
    }

    GST_LOG_OBJECT (src, "Stopping capture");

    err = dc1394_capture_stop (src->camera);
    if (err > 0) {
      GST_ELEMENT_ERROR (src, RESOURCE, FAILED, ("Capture stop error : %d ",
              err), ("Capture stop error : %d ", err));
      return FALSE;
    } else {
      GST_LOG_OBJECT (src, "Capture stoped successfully");
    }
  }

  return TRUE;
}


static gboolean
plugin_init (GstPlugin * plugin)
{
  GST_DEBUG_CATEGORY_INIT (dc1394_debug, "dc1394", 0, "DC1394 interface");

  return gst_element_register (plugin, "dc1394src", GST_RANK_NONE,
      GST_TYPE_DC1394);

}


GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
    GST_VERSION_MINOR,
    "dc1394",
    "1394 IIDC Video Source",
    plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)