gst_element_class_set_details => gst_element_class_set_details_simple
[platform/upstream/gst-plugins-good.git] / ext / raw1394 / gstdv1394src.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  *               <2000> Daniel Fischer <dan@f3c.com>
4  *               <2004> Wim Taymans <wim@fluendo.com>
5  *               <2006> Zaheer Abbas Merali <zaheerabbas at merali dot org>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22 /**
23  * SECTION:element-dv1394src
24  *
25  * Read DV (digital video) data from firewire port.
26  *
27  * <refsect2>
28  * <title>Example launch line</title>
29  * |[
30  * gst-launch dv1394src ! queue ! dvdemux name=d ! queue ! dvdec ! xvimagesink d. ! queue ! alsasink
31  * ]| This pipeline captures from the firewire port and displays it (might need
32  * format converters for audio/video).
33  * </refsect2>
34  */
35
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39 #include <unistd.h>
40 #include <sys/poll.h>
41 #include <sys/socket.h>
42 #include <errno.h>
43 #include <fcntl.h>
44 #include <string.h>
45 #include <stdlib.h>
46
47 #include <libavc1394/avc1394.h>
48 #include <libavc1394/avc1394_vcr.h>
49 #include <libavc1394/rom1394.h>
50 #include <libraw1394/raw1394.h>
51 #ifdef HAVE_LIBIEC61883
52 #include <libiec61883/iec61883.h>
53 #endif
54
55 #include <gst/gst.h>
56
57 #include "gstdv1394src.h"
58 #include "gst1394probe.h"
59 #include "gst1394clock.h"
60
61
62 #define CONTROL_STOP            'S'     /* stop the select call */
63 #define CONTROL_SOCKETS(src)   src->control_sock
64 #define WRITE_SOCKET(src)      src->control_sock[1]
65 #define READ_SOCKET(src)       src->control_sock[0]
66
67 #define SEND_COMMAND(src, command)          \
68 G_STMT_START {                              \
69   int _res; unsigned char c; c = command;   \
70   _res = write (WRITE_SOCKET(src), &c, 1);  \
71 } G_STMT_END
72
73 #define READ_COMMAND(src, command, res)        \
74 G_STMT_START {                                 \
75   res = read(READ_SOCKET(src), &command, 1);   \
76 } G_STMT_END
77
78
79 GST_DEBUG_CATEGORY_STATIC (dv1394src_debug);
80 #define GST_CAT_DEFAULT (dv1394src_debug)
81
82 #define PAL_FRAMESIZE 144000
83 #define PAL_FRAMERATE 25
84
85 #define NTSC_FRAMESIZE 120000
86 #define NTSC_FRAMERATE 30
87
88 enum
89 {
90   SIGNAL_FRAME_DROPPED,
91   /* FILL ME */
92   LAST_SIGNAL
93 };
94
95 #define DEFAULT_PORT    -1
96 #define DEFAULT_CHANNEL   63
97 #define DEFAULT_CONSECUTIVE 1
98 #define DEFAULT_SKIP    0
99 #define DEFAULT_DROP_INCOMPLETE TRUE
100 #define DEFAULT_USE_AVC   TRUE
101 #define DEFAULT_GUID    0
102
103 enum
104 {
105   PROP_0,
106   PROP_PORT,
107   PROP_CHANNEL,
108   PROP_CONSECUTIVE,
109   PROP_SKIP,
110   PROP_DROP_INCOMPLETE,
111   PROP_USE_AVC,
112   PROP_GUID,
113   PROP_DEVICE_NAME
114 };
115
116 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
117     GST_PAD_SRC,
118     GST_PAD_ALWAYS,
119     GST_STATIC_CAPS ("video/x-dv, "
120         "format = (string) { NTSC, PAL }, " "systemstream = (boolean) true")
121     );
122
123 static void gst_dv1394src_uri_handler_init (gpointer g_iface,
124     gpointer iface_data);
125
126 static void gst_dv1394src_set_property (GObject * object, guint prop_id,
127     const GValue * value, GParamSpec * pspec);
128 static void gst_dv1394src_get_property (GObject * object, guint prop_id,
129     GValue * value, GParamSpec * pspec);
130 static void gst_dv1394src_dispose (GObject * object);
131
132 static GstClock *gst_dv1394src_provide_clock (GstElement * element);
133
134 static gboolean gst_dv1394src_start (GstBaseSrc * bsrc);
135 static gboolean gst_dv1394src_stop (GstBaseSrc * bsrc);
136 static gboolean gst_dv1394src_unlock (GstBaseSrc * bsrc);
137
138 static GstFlowReturn gst_dv1394src_create (GstPushSrc * psrc, GstBuffer ** buf);
139
140 static gboolean gst_dv1394src_query (GstBaseSrc * src, GstQuery * query);
141 static void gst_dv1394src_update_device_name (GstDV1394Src * src);
142
143 static void
144 _do_init (GType type)
145 {
146   static const GInterfaceInfo urihandler_info = {
147     gst_dv1394src_uri_handler_init,
148     NULL,
149     NULL,
150   };
151   g_type_add_interface_static (type, GST_TYPE_URI_HANDLER, &urihandler_info);
152
153   gst_1394_type_add_property_probe_interface (type);
154
155   GST_DEBUG_CATEGORY_INIT (dv1394src_debug, "dv1394src", 0,
156       "DV firewire source");
157 }
158
159 GST_BOILERPLATE_FULL (GstDV1394Src, gst_dv1394src, GstPushSrc,
160     GST_TYPE_PUSH_SRC, _do_init);
161
162
163 static guint gst_dv1394src_signals[LAST_SIGNAL] = { 0 };
164
165
166 static void
167 gst_dv1394src_base_init (gpointer g_class)
168 {
169   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
170
171   gst_element_class_add_pad_template (element_class,
172       gst_static_pad_template_get (&src_factory));
173
174   gst_element_class_set_details_simple (element_class,
175       "Firewire (1394) DV video source", "Source/Video",
176       "Source for DV video data from firewire port",
177       "Erik Walthinsen <omega@temple-baptist.com>, "
178       "Daniel Fischer <dan@f3c.com>, " "Wim Taymans <wim@fluendo.com>, "
179       "Zaheer Abbas Merali <zaheerabbas at merali dot org>");
180 }
181
182 static void
183 gst_dv1394src_class_init (GstDV1394SrcClass * klass)
184 {
185   GObjectClass *gobject_class;
186   GstElementClass *gstelement_class;
187   GstBaseSrcClass *gstbasesrc_class;
188   GstPushSrcClass *gstpushsrc_class;
189
190   gobject_class = (GObjectClass *) klass;
191   gstelement_class = (GstElementClass *) klass;
192   gstbasesrc_class = (GstBaseSrcClass *) klass;
193   gstpushsrc_class = (GstPushSrcClass *) klass;
194
195   gobject_class->set_property = gst_dv1394src_set_property;
196   gobject_class->get_property = gst_dv1394src_get_property;
197   gobject_class->dispose = gst_dv1394src_dispose;
198
199   gstelement_class->provide_clock = gst_dv1394src_provide_clock;
200
201   gst_dv1394src_signals[SIGNAL_FRAME_DROPPED] =
202       g_signal_new ("frame-dropped", G_TYPE_FROM_CLASS (klass),
203       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDV1394SrcClass, frame_dropped),
204       NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
205
206   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_PORT,
207       g_param_spec_int ("port", "Port", "Port number (-1 automatic)",
208           -1, 16, DEFAULT_PORT, G_PARAM_READWRITE));
209   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_CHANNEL,
210       g_param_spec_int ("channel", "Channel", "Channel number for listening",
211           0, 64, DEFAULT_CHANNEL, G_PARAM_READWRITE));
212   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_CONSECUTIVE,
213       g_param_spec_int ("consecutive", "consecutive frames",
214           "send n consecutive frames after skipping", 1, G_MAXINT,
215           DEFAULT_CONSECUTIVE, G_PARAM_READWRITE));
216   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SKIP,
217       g_param_spec_int ("skip", "skip frames", "skip n frames",
218           0, G_MAXINT, DEFAULT_SKIP, G_PARAM_READWRITE));
219   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_DROP_INCOMPLETE,
220       g_param_spec_boolean ("drop_incomplete", "drop_incomplete",
221           "drop incomplete frames", DEFAULT_DROP_INCOMPLETE,
222           G_PARAM_READWRITE));
223   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_USE_AVC,
224       g_param_spec_boolean ("use-avc", "Use AV/C", "Use AV/C VTR control",
225           DEFAULT_USE_AVC, G_PARAM_READWRITE));
226   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_GUID,
227       g_param_spec_uint64 ("guid", "GUID",
228           "select one of multiple DV devices by its GUID. use a hexadecimal "
229           "like 0xhhhhhhhhhhhhhhhh. (0 = no guid)", 0, G_MAXUINT64,
230           DEFAULT_GUID, G_PARAM_READWRITE));
231   /**
232    * GstDV1394Src:device-name
233    *
234    * Descriptive name of the currently opened device
235    *
236    * Since: 0.10.7
237    **/
238   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_DEVICE_NAME,
239       g_param_spec_string ("device-name", "device name",
240           "user-friendly name of the device", "Default", G_PARAM_READABLE));
241
242   gstbasesrc_class->negotiate = NULL;
243   gstbasesrc_class->start = gst_dv1394src_start;
244   gstbasesrc_class->stop = gst_dv1394src_stop;
245   gstbasesrc_class->unlock = gst_dv1394src_unlock;
246   gstbasesrc_class->query = gst_dv1394src_query;
247
248   gstpushsrc_class->create = gst_dv1394src_create;
249 }
250
251 static void
252 gst_dv1394src_init (GstDV1394Src * dv1394src, GstDV1394SrcClass * klass)
253 {
254   GstPad *srcpad = GST_BASE_SRC_PAD (dv1394src);
255
256   gst_base_src_set_live (GST_BASE_SRC (dv1394src), TRUE);
257   gst_base_src_set_format (GST_BASE_SRC (dv1394src), GST_FORMAT_TIME);
258   gst_base_src_set_do_timestamp (GST_BASE_SRC (dv1394src), TRUE);
259   gst_pad_use_fixed_caps (srcpad);
260
261   dv1394src->port = DEFAULT_PORT;
262   dv1394src->channel = DEFAULT_CHANNEL;
263
264   dv1394src->consecutive = DEFAULT_CONSECUTIVE;
265   dv1394src->skip = DEFAULT_SKIP;
266   dv1394src->drop_incomplete = DEFAULT_DROP_INCOMPLETE;
267   dv1394src->use_avc = DEFAULT_USE_AVC;
268   dv1394src->guid = DEFAULT_GUID;
269   dv1394src->uri = g_strdup_printf ("dv://%d", dv1394src->port);
270   dv1394src->device_name = g_strdup_printf ("Default");
271
272   READ_SOCKET (dv1394src) = -1;
273   WRITE_SOCKET (dv1394src) = -1;
274
275   /* initialized when first header received */
276   dv1394src->frame_size = 0;
277
278   dv1394src->buf = NULL;
279   dv1394src->frame = NULL;
280   dv1394src->frame_sequence = 0;
281
282   dv1394src->provided_clock = gst_1394_clock_new ("dv1394clock");
283 }
284
285 static void
286 gst_dv1394src_dispose (GObject * object)
287 {
288   GstDV1394Src *src = GST_DV1394SRC (object);
289
290   if (src->provided_clock) {
291     g_object_unref (src->provided_clock);
292   }
293
294   g_free (src->uri);
295   src->uri = NULL;
296
297   g_free (src->device_name);
298   src->device_name = NULL;
299
300   G_OBJECT_CLASS (parent_class)->dispose (object);
301 }
302
303 static void
304 gst_dv1394src_set_property (GObject * object, guint prop_id,
305     const GValue * value, GParamSpec * pspec)
306 {
307   GstDV1394Src *filter = GST_DV1394SRC (object);
308
309   switch (prop_id) {
310     case PROP_PORT:
311       filter->port = g_value_get_int (value);
312       g_free (filter->uri);
313       filter->uri = g_strdup_printf ("dv://%d", filter->port);
314       break;
315     case PROP_CHANNEL:
316       filter->channel = g_value_get_int (value);
317       break;
318     case PROP_SKIP:
319       filter->skip = g_value_get_int (value);
320       break;
321     case PROP_CONSECUTIVE:
322       filter->consecutive = g_value_get_int (value);
323       break;
324     case PROP_DROP_INCOMPLETE:
325       filter->drop_incomplete = g_value_get_boolean (value);
326       break;
327     case PROP_USE_AVC:
328       filter->use_avc = g_value_get_boolean (value);
329       break;
330     case PROP_GUID:
331       filter->guid = g_value_get_uint64 (value);
332       gst_dv1394src_update_device_name (filter);
333       break;
334     default:
335       break;
336   }
337 }
338
339 static void
340 gst_dv1394src_get_property (GObject * object, guint prop_id, GValue * value,
341     GParamSpec * pspec)
342 {
343   GstDV1394Src *filter = GST_DV1394SRC (object);
344
345   switch (prop_id) {
346     case PROP_PORT:
347       g_value_set_int (value, filter->port);
348       break;
349     case PROP_CHANNEL:
350       g_value_set_int (value, filter->channel);
351       break;
352     case PROP_SKIP:
353       g_value_set_int (value, filter->skip);
354       break;
355     case PROP_CONSECUTIVE:
356       g_value_set_int (value, filter->consecutive);
357       break;
358     case PROP_DROP_INCOMPLETE:
359       g_value_set_boolean (value, filter->drop_incomplete);
360       break;
361     case PROP_USE_AVC:
362       g_value_set_boolean (value, filter->use_avc);
363       break;
364     case PROP_GUID:
365       g_value_set_uint64 (value, filter->guid);
366       break;
367     case PROP_DEVICE_NAME:
368       g_value_set_string (value, filter->device_name);
369       break;
370     default:
371       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
372       break;
373   }
374 }
375
376 static GstClock *
377 gst_dv1394src_provide_clock (GstElement * element)
378 {
379   GstDV1394Src *dv1394src = GST_DV1394SRC (element);
380
381   return GST_CLOCK_CAST (gst_object_ref (dv1394src->provided_clock));
382 }
383
384 #ifdef HAVE_LIBIEC61883
385 static GstDV1394Src *
386 gst_dv1394src_from_raw1394handle (raw1394handle_t handle)
387 {
388   iec61883_dv_t dv = (iec61883_dv_t) raw1394_get_userdata (handle);
389   iec61883_dv_fb_t dv_fb =
390       (iec61883_dv_fb_t) iec61883_dv_get_callback_data (dv);
391   return GST_DV1394SRC (iec61883_dv_fb_get_callback_data (dv_fb));
392 }
393 #else /* HAVE_LIBIEC61883 */
394 static GstDV1394Src *
395 gst_dv1394src_from_raw1394handle (raw1394handle_t handle)
396 {
397   return GST_DV1394SRC (raw1394_get_userdata (handle));
398 }
399 #endif /* HAVE_LIBIEC61883 */
400
401 #ifdef HAVE_LIBIEC61883
402 static int
403 gst_dv1394src_iec61883_receive (unsigned char *data, int len,
404     int complete, void *cbdata)
405 {
406   GstDV1394Src *dv1394src = GST_DV1394SRC (cbdata);
407
408   if (G_UNLIKELY (!GST_PAD_CAPS (GST_BASE_SRC_PAD (dv1394src)))) {
409     GstCaps *caps;
410     unsigned char *p = data;
411
412     // figure format (NTSC/PAL)
413     if (p[3] & 0x80) {
414       // PAL
415       dv1394src->frame_size = PAL_FRAMESIZE;
416       dv1394src->frame_rate = PAL_FRAMERATE;
417       GST_DEBUG ("PAL data");
418       caps = gst_caps_new_simple ("video/x-dv",
419           "format", G_TYPE_STRING, "PAL",
420           "systemstream", G_TYPE_BOOLEAN, TRUE, NULL);
421     } else {
422       // NTSC (untested)
423       dv1394src->frame_size = NTSC_FRAMESIZE;
424       dv1394src->frame_rate = NTSC_FRAMERATE;
425       GST_DEBUG
426           ("NTSC data [untested] - please report success/failure to <dan@f3c.com>");
427       caps = gst_caps_new_simple ("video/x-dv",
428           "format", G_TYPE_STRING, "NTSC",
429           "systemstream", G_TYPE_BOOLEAN, TRUE, NULL);
430     }
431     gst_pad_set_caps (GST_BASE_SRC_PAD (dv1394src), caps);
432     gst_caps_unref (caps);
433   }
434
435   dv1394src->frame = NULL;
436   if (G_LIKELY ((dv1394src->frame_sequence + 1) % (dv1394src->skip +
437               dv1394src->consecutive) < dv1394src->consecutive)) {
438     if (complete && len == dv1394src->frame_size) {
439       guint8 *bufdata;
440       GstBuffer *buf;
441
442       buf = gst_buffer_new_and_alloc (dv1394src->frame_size);
443
444       GST_BUFFER_OFFSET (buf) = dv1394src->frame_sequence;
445       bufdata = GST_BUFFER_DATA (buf);
446       memcpy (bufdata, data, len);
447       dv1394src->buf = buf;
448     }
449   }
450   dv1394src->frame_sequence++;
451   return 0;
452 }
453
454 #else
455 static int
456 gst_dv1394src_iso_receive (raw1394handle_t handle, int channel, size_t len,
457     quadlet_t * data)
458 {
459   GstDV1394Src *dv1394src = gst_dv1394src_from_raw1394handle (handle);
460
461   if (len > 16) {
462     /*
463        the following code taken from kino-0.51 (Dan Dennedy/Charles Yates)
464        Kindly relicensed under the LGPL. See the commit log for version 1.6 of
465        this file in CVS.
466      */
467     unsigned char *p = (unsigned char *) &data[3];
468
469     int section_type = p[0] >> 5;       /* section type is in bits 5 - 7 */
470     int dif_sequence = p[1] >> 4;       /* dif sequence number is in bits 4 - 7 */
471     int dif_block = p[2];
472
473     /* if we are at the beginning of a frame, 
474        we set buf=frame, and alloc a new buffer for frame
475      */
476     if (section_type == 0 && dif_sequence == 0) {       // dif header
477       if (!GST_PAD_CAPS (GST_BASE_SRC_PAD (dv1394src))) {
478         GstCaps *caps;
479
480         // figure format (NTSC/PAL)
481         if (p[3] & 0x80) {
482           // PAL
483           dv1394src->frame_size = PAL_FRAMESIZE;
484           dv1394src->frame_rate = PAL_FRAMERATE;
485           GST_DEBUG ("PAL data");
486           caps = gst_caps_new_simple ("video/x-dv",
487               "format", G_TYPE_STRING, "PAL",
488               "systemstream", G_TYPE_BOOLEAN, TRUE, NULL);
489         } else {
490           // NTSC (untested)
491           dv1394src->frame_size = NTSC_FRAMESIZE;
492           dv1394src->frame_rate = NTSC_FRAMERATE;
493           GST_DEBUG
494               ("NTSC data [untested] - please report success/failure to <dan@f3c.com>");
495           caps = gst_caps_new_simple ("video/x-dv",
496               "format", G_TYPE_STRING, "NTSC",
497               "systemstream", G_TYPE_BOOLEAN, TRUE, NULL);
498         }
499         gst_pad_set_caps (GST_BASE_SRC_PAD (dv1394src), caps);
500         gst_caps_unref (caps);
501       }
502       // drop last frame when not complete
503       if (!dv1394src->drop_incomplete
504           || dv1394src->bytes_in_frame == dv1394src->frame_size) {
505         dv1394src->buf = dv1394src->frame;
506       } else {
507         GST_INFO_OBJECT (GST_ELEMENT (dv1394src), "incomplete frame dropped");
508         g_signal_emit (G_OBJECT (dv1394src),
509             gst_dv1394src_signals[SIGNAL_FRAME_DROPPED], 0);
510         if (dv1394src->frame) {
511           gst_buffer_unref (dv1394src->frame);
512         }
513       }
514       if ((dv1394src->frame_sequence + 1) % (dv1394src->skip +
515               dv1394src->consecutive) < dv1394src->consecutive) {
516         GstBuffer *buf;
517         gint64 i64;
518
519         buf = gst_buffer_new_and_alloc (dv1394src->frame_size);
520
521         /* fill in offset, duration, timestamp */
522         GST_BUFFER_OFFSET (buf) = dv1394src->frame_sequence;
523         dv1394src->frame = buf;
524       }
525       dv1394src->frame_sequence++;
526       dv1394src->bytes_in_frame = 0;
527     }
528
529     if (dv1394src->frame != NULL) {
530       guint8 *data = GST_BUFFER_DATA (dv1394src->frame);
531
532       switch (section_type) {
533         case 0:                /* 1 Header block */
534           /* p[3] |= 0x80; // hack to force PAL data */
535           memcpy (data + dif_sequence * 150 * 80, p, 480);
536           break;
537
538         case 1:                /* 2 Subcode blocks */
539           memcpy (data + dif_sequence * 150 * 80 + (1 + dif_block) * 80, p,
540               480);
541           break;
542
543         case 2:                /* 3 VAUX blocks */
544           memcpy (data + dif_sequence * 150 * 80 + (3 + dif_block) * 80, p,
545               480);
546           break;
547
548         case 3:                /* 9 Audio blocks interleaved with video */
549           memcpy (data + dif_sequence * 150 * 80 + (6 + dif_block * 16) * 80, p,
550               480);
551           break;
552
553         case 4:                /* 135 Video blocks interleaved with audio */
554           memcpy (data + dif_sequence * 150 * 80 + (7 + (dif_block / 15) +
555                   dif_block) * 80, p, 480);
556           break;
557
558         default:               /* we can't handle any other data */
559           break;
560       }
561       dv1394src->bytes_in_frame += 480;
562     }
563   }
564
565   return 0;
566 }
567 #endif
568 /*
569  * When an ieee1394 bus reset happens, usually a device has been removed
570  * or added.  We send a message on the message bus with the node count 
571  * and whether the capture device used in this element connected, disconnected 
572  * or was unchanged
573  * Message structure:
574  * nodecount - integer with number of nodes on bus
575  * current-device-change - integer (1 if device connected, 0 if no change to
576  *                         current device status, -1 if device disconnected)
577  */
578 static int
579 gst_dv1394src_bus_reset (raw1394handle_t handle, unsigned int generation)
580 {
581   GstDV1394Src *src;
582   gint nodecount;
583   GstMessage *message;
584   GstStructure *structure;
585   gint current_device_change;
586   gint i;
587
588   src = gst_dv1394src_from_raw1394handle (handle);
589
590   GST_INFO_OBJECT (src, "have bus reset");
591
592   /* update generation - told to do so by docs */
593   raw1394_update_generation (handle, generation);
594   nodecount = raw1394_get_nodecount (handle);
595   /* allocate memory for portinfo */
596
597   /* current_device_change is -1 if camera disconnected, 0 if other device
598    * connected or 1 if camera has now connected */
599   current_device_change = -1;
600   for (i = 0; i < nodecount; i++) {
601     if (src->guid == rom1394_get_guid (handle, i)) {
602       /* Camera is with us */
603       GST_DEBUG ("Camera is with us");
604       if (!src->connected) {
605         current_device_change = 1;
606         src->connected = TRUE;
607       } else
608         current_device_change = 0;
609     }
610   }
611   if (src->connected && current_device_change == -1) {
612     GST_DEBUG ("Camera has disconnected");
613     src->connected = FALSE;
614   } else if (!src->connected && current_device_change == -1) {
615     GST_DEBUG ("Camera is still not with us");
616     current_device_change = 0;
617   }
618
619   structure = gst_structure_new ("ieee1394-bus-reset", "nodecount", G_TYPE_INT,
620       nodecount, "current-device-change", G_TYPE_INT, current_device_change,
621       NULL);
622   message = gst_message_new_element (GST_OBJECT (src), structure);
623   gst_element_post_message (GST_ELEMENT (src), message);
624
625   return 0;
626 }
627
628 static GstFlowReturn
629 gst_dv1394src_create (GstPushSrc * psrc, GstBuffer ** buf)
630 {
631   GstDV1394Src *dv1394src = GST_DV1394SRC (psrc);
632   GstCaps *caps;
633   struct pollfd pollfds[2];
634
635   pollfds[0].fd = raw1394_get_fd (dv1394src->handle);
636   pollfds[0].events = POLLIN | POLLERR | POLLHUP | POLLPRI;
637   pollfds[1].fd = READ_SOCKET (dv1394src);
638   pollfds[1].events = POLLIN | POLLERR | POLLHUP | POLLPRI;
639
640   if (G_UNLIKELY (dv1394src->buf)) {
641     /* maybe we had an error before, and there's a stale buffer? */
642     gst_buffer_unref (dv1394src->buf);
643     dv1394src->buf = NULL;
644   }
645
646   while (TRUE) {
647     int res = poll (pollfds, 2, -1);
648
649     if (G_UNLIKELY (res < 0)) {
650       if (errno == EAGAIN || errno == EINTR)
651         continue;
652       else
653         goto error_while_polling;
654     }
655
656     if (G_UNLIKELY (pollfds[1].revents)) {
657       char command;
658
659       if (pollfds[1].revents & POLLIN)
660         READ_COMMAND (dv1394src, command, res);
661
662       goto told_to_stop;
663     } else if (G_LIKELY (pollfds[0].revents & POLLIN)) {
664       /* shouldn't block in theory */
665       raw1394_loop_iterate (dv1394src->handle);
666
667       if (dv1394src->buf)
668         break;
669     }
670   }
671
672   g_assert (dv1394src->buf);
673
674   caps = gst_pad_get_caps (GST_BASE_SRC_PAD (psrc));
675   gst_buffer_set_caps (dv1394src->buf, caps);
676   gst_caps_unref (caps);
677
678   *buf = dv1394src->buf;
679   dv1394src->buf = NULL;
680   return GST_FLOW_OK;
681
682 error_while_polling:
683   {
684     GST_ELEMENT_ERROR (dv1394src, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
685     return GST_FLOW_UNEXPECTED;
686   }
687 told_to_stop:
688   {
689     GST_DEBUG_OBJECT (dv1394src, "told to stop, shutting down");
690     return GST_FLOW_WRONG_STATE;
691   }
692 }
693
694 static int
695 gst_dv1394src_discover_avc_node (GstDV1394Src * src)
696 {
697   int node = -1;
698   int i, j = 0;
699   int m = src->num_ports;
700
701   if (src->port >= 0) {
702     /* search on explicit port */
703     j = src->port;
704     m = j + 1;
705   }
706
707   /* loop over all our ports */
708   for (; j < m && node == -1; j++) {
709     raw1394handle_t handle;
710     struct raw1394_portinfo pinf[16];
711
712     /* open the port */
713     handle = raw1394_new_handle ();
714     if (!handle) {
715       GST_WARNING ("raw1394 - failed to get handle: %s.\n", strerror (errno));
716       continue;
717     }
718     if (raw1394_get_port_info (handle, pinf, 16) < 0) {
719       GST_WARNING ("raw1394 - failed to get port info: %s.\n",
720           strerror (errno));
721       goto next;
722     }
723
724     /* tell raw1394 which host adapter to use */
725     if (raw1394_set_port (handle, j) < 0) {
726       GST_WARNING ("raw1394 - failed to set set port: %s.\n", strerror (errno));
727       goto next;
728     }
729
730     /* now loop over all the nodes */
731     for (i = 0; i < raw1394_get_nodecount (handle); i++) {
732       /* are we looking for an explicit GUID ? */
733       if (src->guid != 0) {
734         if (src->guid == rom1394_get_guid (handle, i)) {
735           node = i;
736           src->port = j;
737           g_free (src->uri);
738           src->uri = g_strdup_printf ("dv://%d", src->port);
739           break;
740         }
741       } else {
742         rom1394_directory rom_dir;
743
744         /* select first AV/C Tape Recorder Player node */
745         if (rom1394_get_directory (handle, i, &rom_dir) < 0) {
746           GST_WARNING ("error reading config rom directory for node %d\n", i);
747           continue;
748         }
749         if ((rom1394_get_node_type (&rom_dir) == ROM1394_NODE_TYPE_AVC) &&
750             avc1394_check_subunit_type (handle, i, AVC1394_SUBUNIT_TYPE_VCR)) {
751           node = i;
752           src->port = j;
753           src->guid = rom1394_get_guid (handle, i);
754           g_free (src->uri);
755           src->uri = g_strdup_printf ("dv://%d", src->port);
756           g_free (src->device_name);
757           src->device_name = g_strdup (rom_dir.label);
758           break;
759         }
760         rom1394_free_directory (&rom_dir);
761       }
762     }
763   next:
764     raw1394_destroy_handle (handle);
765   }
766   return node;
767 }
768
769 static gboolean
770 gst_dv1394src_start (GstBaseSrc * bsrc)
771 {
772   GstDV1394Src *src = GST_DV1394SRC (bsrc);
773   int control_sock[2];
774
775   src->connected = FALSE;
776
777   if (socketpair (PF_UNIX, SOCK_STREAM, 0, control_sock) < 0)
778     goto socket_pair;
779
780   READ_SOCKET (src) = control_sock[0];
781   WRITE_SOCKET (src) = control_sock[1];
782
783   fcntl (READ_SOCKET (src), F_SETFL, O_NONBLOCK);
784   fcntl (WRITE_SOCKET (src), F_SETFL, O_NONBLOCK);
785
786   src->handle = raw1394_new_handle ();
787
788   if (!src->handle) {
789     if (errno == EACCES)
790       goto permission_denied;
791     else if (errno == ENOENT)
792       goto not_found;
793     else
794       goto no_handle;
795   }
796
797   src->num_ports = raw1394_get_port_info (src->handle, src->pinfo, 16);
798
799   if (src->num_ports == 0)
800     goto no_ports;
801
802   if (src->use_avc || src->port == -1)
803     src->avc_node = gst_dv1394src_discover_avc_node (src);
804
805   /* lets destroy handle and create one on port
806      this is more reliable than setting port on
807      the existing handle */
808   raw1394_destroy_handle (src->handle);
809   src->handle = raw1394_new_handle_on_port (src->port);
810   if (!src->handle)
811     goto cannot_set_port;
812
813   raw1394_set_userdata (src->handle, src);
814   raw1394_set_bus_reset_handler (src->handle, gst_dv1394src_bus_reset);
815
816 #ifdef HAVE_LIBIEC61883
817   if ((src->iec61883dv =
818           iec61883_dv_fb_init (src->handle,
819               gst_dv1394src_iec61883_receive, src)) == NULL)
820     goto cannot_initialise_dv;
821
822 #else
823   raw1394_set_iso_handler (src->handle, src->channel,
824       gst_dv1394src_iso_receive);
825 #endif
826
827   GST_DEBUG_OBJECT (src, "successfully opened up 1394 connection");
828   src->connected = TRUE;
829
830 #ifdef HAVE_LIBIEC61883
831   if (iec61883_dv_fb_start (src->iec61883dv, src->channel) != 0)
832     goto cannot_start;
833 #else
834   if (raw1394_start_iso_rcv (src->handle, src->channel) < 0)
835     goto cannot_start;
836 #endif
837
838   if (src->use_avc) {
839     raw1394handle_t avc_handle = raw1394_new_handle_on_port (src->port);
840
841     /* start the VCR */
842     if (avc_handle) {
843       if (!avc1394_vcr_is_recording (avc_handle, src->avc_node)
844           && avc1394_vcr_is_playing (avc_handle, src->avc_node)
845           != AVC1394_VCR_OPERAND_PLAY_FORWARD)
846         avc1394_vcr_play (avc_handle, src->avc_node);
847       raw1394_destroy_handle (avc_handle);
848     } else {
849       GST_WARNING_OBJECT (src, "Starting VCR via avc1394 failed: %s",
850           g_strerror (errno));
851     }
852   }
853
854   gst_1394_clock_set_handle (src->provided_clock, src->handle);
855
856   return TRUE;
857
858 socket_pair:
859   {
860     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
861         GST_ERROR_SYSTEM);
862     return FALSE;
863   }
864 permission_denied:
865   {
866     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL), GST_ERROR_SYSTEM);
867     return FALSE;
868   }
869 not_found:
870   {
871     GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL), GST_ERROR_SYSTEM);
872     return FALSE;
873   }
874 no_handle:
875   {
876     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
877         ("can't get raw1394 handle (%s)", g_strerror (errno)));
878     return FALSE;
879   }
880 no_ports:
881   {
882     raw1394_destroy_handle (src->handle);
883     src->handle = NULL;
884     GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
885         ("no ports available for raw1394"));
886     return FALSE;
887   }
888 cannot_set_port:
889   {
890     GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
891         ("can't set 1394 port %d", src->port));
892     return FALSE;
893   }
894 cannot_start:
895   {
896     raw1394_destroy_handle (src->handle);
897     src->handle = NULL;
898 #ifdef HAVE_LIBIEC61883
899     iec61883_dv_fb_close (src->iec61883dv);
900     src->iec61883dv = NULL;
901 #endif
902     GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
903         ("can't start 1394 iso receive"));
904     return FALSE;
905   }
906 #ifdef HAVE_LIBIEC61883
907 cannot_initialise_dv:
908   {
909     raw1394_destroy_handle (src->handle);
910     src->handle = NULL;
911     GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
912         ("can't initialise iec61883 dv"));
913     return FALSE;
914   }
915 #endif
916 }
917
918 static gboolean
919 gst_dv1394src_stop (GstBaseSrc * bsrc)
920 {
921   GstDV1394Src *src = GST_DV1394SRC (bsrc);
922
923   close (READ_SOCKET (src));
924   close (WRITE_SOCKET (src));
925   READ_SOCKET (src) = -1;
926   WRITE_SOCKET (src) = -1;
927 #ifdef HAVE_LIBIEC61883
928   iec61883_dv_fb_close (src->iec61883dv);
929 #else
930   raw1394_stop_iso_rcv (src->handle, src->channel);
931 #endif
932
933   if (src->use_avc) {
934     raw1394handle_t avc_handle = raw1394_new_handle_on_port (src->port);
935
936     /* pause and stop the VCR */
937     if (avc_handle) {
938       if (!avc1394_vcr_is_recording (avc_handle, src->avc_node)
939           && (avc1394_vcr_is_playing (avc_handle, src->avc_node)
940               != AVC1394_VCR_OPERAND_PLAY_FORWARD_PAUSE))
941         avc1394_vcr_pause (avc_handle, src->avc_node);
942       avc1394_vcr_stop (avc_handle, src->avc_node);
943       raw1394_destroy_handle (avc_handle);
944     } else {
945       GST_WARNING_OBJECT (src, "Starting VCR via avc1394 failed: %s",
946           g_strerror (errno));
947     }
948   }
949
950   gst_1394_clock_unset_handle (src->provided_clock);
951
952   raw1394_destroy_handle (src->handle);
953
954   return TRUE;
955 }
956
957 static gboolean
958 gst_dv1394src_unlock (GstBaseSrc * bsrc)
959 {
960   GstDV1394Src *src = GST_DV1394SRC (bsrc);
961
962   SEND_COMMAND (src, CONTROL_STOP);
963
964   return TRUE;
965 }
966
967 static gboolean
968 gst_dv1394src_query (GstBaseSrc * basesrc, GstQuery * query)
969 {
970   switch (GST_QUERY_TYPE (query)) {
971     case GST_QUERY_LATENCY:
972     {
973       gst_query_set_latency (query, TRUE, GST_SECOND / 25, GST_CLOCK_TIME_NONE);
974     }
975       break;
976     default:
977       goto not_supported;
978   }
979
980   return TRUE;
981
982 not_supported:
983   return GST_BASE_SRC_CLASS (parent_class)->query (basesrc, query);
984 }
985
986 static void
987 gst_dv1394src_update_device_name (GstDV1394Src * src)
988 {
989   raw1394handle_t handle;
990   gint portcount, port, nodecount, node;
991   rom1394_directory directory;
992
993   g_free (src->device_name);
994   src->device_name = NULL;
995
996   GST_LOG_OBJECT (src, "updating device name for current GUID");
997
998   handle = raw1394_new_handle ();
999
1000   if (handle == NULL)
1001     goto gethandle_failed;
1002
1003   portcount = raw1394_get_port_info (handle, NULL, 0);
1004   for (port = 0; port < portcount; port++) {
1005     if (raw1394_set_port (handle, port) >= 0) {
1006       nodecount = raw1394_get_nodecount (handle);
1007       for (node = 0; node < nodecount; node++) {
1008         if (src->guid == rom1394_get_guid (handle, node)) {
1009           if (rom1394_get_directory (handle, node, &directory) >= 0) {
1010             g_free (src->device_name);
1011             src->device_name = g_strdup (directory.label);
1012             rom1394_free_directory (&directory);
1013             goto done;
1014           } else {
1015             GST_WARNING ("error reading rom directory for node %d", node);
1016           }
1017         }
1018       }
1019     }
1020   }
1021
1022   src->device_name = g_strdup ("Unknown");      /* FIXME: translate? */
1023
1024 done:
1025
1026   raw1394_destroy_handle (handle);
1027   return;
1028
1029 /* ERRORS */
1030 gethandle_failed:
1031   {
1032     GST_WARNING ("failed to get raw1394 handle: %s", g_strerror (errno));
1033     src->device_name = g_strdup ("Unknown");    /* FIXME: translate? */
1034     return;
1035   }
1036 }
1037
1038 /*** GSTURIHANDLER INTERFACE *************************************************/
1039
1040 static guint
1041 gst_dv1394src_uri_get_type (void)
1042 {
1043   return GST_URI_SRC;
1044 }
1045
1046 static gchar **
1047 gst_dv1394src_uri_get_protocols (void)
1048 {
1049   static gchar *protocols[] = { "dv", NULL };
1050
1051   return protocols;
1052 }
1053
1054 static const gchar *
1055 gst_dv1394src_uri_get_uri (GstURIHandler * handler)
1056 {
1057   GstDV1394Src *gst_dv1394src = GST_DV1394SRC (handler);
1058
1059   return gst_dv1394src->uri;
1060 }
1061
1062 static gboolean
1063 gst_dv1394src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
1064 {
1065   gchar *protocol, *location;
1066   gboolean ret = TRUE;
1067   GstDV1394Src *gst_dv1394src = GST_DV1394SRC (handler);
1068
1069   protocol = gst_uri_get_protocol (uri);
1070   if (strcmp (protocol, "dv") != 0) {
1071     g_free (protocol);
1072     return FALSE;
1073   }
1074   g_free (protocol);
1075
1076   location = gst_uri_get_location (uri);
1077   if (location && *location != '\0')
1078     gst_dv1394src->port = strtol (location, NULL, 10);
1079   else
1080     gst_dv1394src->port = DEFAULT_PORT;
1081   g_free (location);
1082   g_free (gst_dv1394src->uri);
1083   gst_dv1394src->uri = g_strdup_printf ("dv://%d", gst_dv1394src->port);
1084
1085   return ret;
1086 }
1087
1088 static void
1089 gst_dv1394src_uri_handler_init (gpointer g_iface, gpointer iface_data)
1090 {
1091   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
1092
1093   iface->get_type = gst_dv1394src_uri_get_type;
1094   iface->get_protocols = gst_dv1394src_uri_get_protocols;
1095   iface->get_uri = gst_dv1394src_uri_get_uri;
1096   iface->set_uri = gst_dv1394src_uri_set_uri;
1097 }