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>
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.
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.
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.
23 * SECTION:element-dv1394src
25 * Read DV (digital video) data from firewire port.
28 * <title>Example launch line</title>
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).
41 #include <sys/socket.h>
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>
57 #include "gstdv1394src.h"
58 #include "gst1394probe.h"
59 #include "gst1394clock.h"
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]
67 #define SEND_COMMAND(src, command) \
69 int _res; unsigned char c; c = command; \
70 _res = write (WRITE_SOCKET(src), &c, 1); \
73 #define READ_COMMAND(src, command, res) \
75 res = read(READ_SOCKET(src), &command, 1); \
79 GST_DEBUG_CATEGORY_STATIC (dv1394src_debug);
80 #define GST_CAT_DEFAULT (dv1394src_debug)
82 #define PAL_FRAMESIZE 144000
83 #define PAL_FRAMERATE 25
85 #define NTSC_FRAMESIZE 120000
86 #define NTSC_FRAMERATE 30
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
110 PROP_DROP_INCOMPLETE,
116 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
119 GST_STATIC_CAPS ("video/x-dv, "
120 "format = (string) { NTSC, PAL }, " "systemstream = (boolean) true")
123 static void gst_dv1394src_uri_handler_init (gpointer g_iface,
124 gpointer iface_data);
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);
132 static GstClock *gst_dv1394src_provide_clock (GstElement * element);
134 static gboolean gst_dv1394src_start (GstBaseSrc * bsrc);
135 static gboolean gst_dv1394src_stop (GstBaseSrc * bsrc);
136 static gboolean gst_dv1394src_unlock (GstBaseSrc * bsrc);
138 static GstFlowReturn gst_dv1394src_create (GstPushSrc * psrc, GstBuffer ** buf);
140 static gboolean gst_dv1394src_query (GstBaseSrc * src, GstQuery * query);
141 static void gst_dv1394src_update_device_name (GstDV1394Src * src);
144 _do_init (GType type)
146 static const GInterfaceInfo urihandler_info = {
147 gst_dv1394src_uri_handler_init,
151 g_type_add_interface_static (type, GST_TYPE_URI_HANDLER, &urihandler_info);
153 gst_1394_type_add_property_probe_interface (type);
155 GST_DEBUG_CATEGORY_INIT (dv1394src_debug, "dv1394src", 0,
156 "DV firewire source");
159 GST_BOILERPLATE_FULL (GstDV1394Src, gst_dv1394src, GstPushSrc,
160 GST_TYPE_PUSH_SRC, _do_init);
163 static guint gst_dv1394src_signals[LAST_SIGNAL] = { 0 };
167 gst_dv1394src_base_init (gpointer g_class)
169 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
171 gst_element_class_add_pad_template (element_class,
172 gst_static_pad_template_get (&src_factory));
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>");
183 gst_dv1394src_class_init (GstDV1394SrcClass * klass)
185 GObjectClass *gobject_class;
186 GstElementClass *gstelement_class;
187 GstBaseSrcClass *gstbasesrc_class;
188 GstPushSrcClass *gstpushsrc_class;
190 gobject_class = (GObjectClass *) klass;
191 gstelement_class = (GstElementClass *) klass;
192 gstbasesrc_class = (GstBaseSrcClass *) klass;
193 gstpushsrc_class = (GstPushSrcClass *) klass;
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;
199 gstelement_class->provide_clock = gst_dv1394src_provide_clock;
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);
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,
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));
232 * GstDV1394Src:device-name
234 * Descriptive name of the currently opened device
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));
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;
248 gstpushsrc_class->create = gst_dv1394src_create;
252 gst_dv1394src_init (GstDV1394Src * dv1394src, GstDV1394SrcClass * klass)
254 GstPad *srcpad = GST_BASE_SRC_PAD (dv1394src);
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);
261 dv1394src->port = DEFAULT_PORT;
262 dv1394src->channel = DEFAULT_CHANNEL;
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");
272 READ_SOCKET (dv1394src) = -1;
273 WRITE_SOCKET (dv1394src) = -1;
275 /* initialized when first header received */
276 dv1394src->frame_size = 0;
278 dv1394src->buf = NULL;
279 dv1394src->frame = NULL;
280 dv1394src->frame_sequence = 0;
282 dv1394src->provided_clock = gst_1394_clock_new ("dv1394clock");
286 gst_dv1394src_dispose (GObject * object)
288 GstDV1394Src *src = GST_DV1394SRC (object);
290 if (src->provided_clock) {
291 g_object_unref (src->provided_clock);
297 g_free (src->device_name);
298 src->device_name = NULL;
300 G_OBJECT_CLASS (parent_class)->dispose (object);
304 gst_dv1394src_set_property (GObject * object, guint prop_id,
305 const GValue * value, GParamSpec * pspec)
307 GstDV1394Src *filter = GST_DV1394SRC (object);
311 filter->port = g_value_get_int (value);
312 g_free (filter->uri);
313 filter->uri = g_strdup_printf ("dv://%d", filter->port);
316 filter->channel = g_value_get_int (value);
319 filter->skip = g_value_get_int (value);
321 case PROP_CONSECUTIVE:
322 filter->consecutive = g_value_get_int (value);
324 case PROP_DROP_INCOMPLETE:
325 filter->drop_incomplete = g_value_get_boolean (value);
328 filter->use_avc = g_value_get_boolean (value);
331 filter->guid = g_value_get_uint64 (value);
332 gst_dv1394src_update_device_name (filter);
340 gst_dv1394src_get_property (GObject * object, guint prop_id, GValue * value,
343 GstDV1394Src *filter = GST_DV1394SRC (object);
347 g_value_set_int (value, filter->port);
350 g_value_set_int (value, filter->channel);
353 g_value_set_int (value, filter->skip);
355 case PROP_CONSECUTIVE:
356 g_value_set_int (value, filter->consecutive);
358 case PROP_DROP_INCOMPLETE:
359 g_value_set_boolean (value, filter->drop_incomplete);
362 g_value_set_boolean (value, filter->use_avc);
365 g_value_set_uint64 (value, filter->guid);
367 case PROP_DEVICE_NAME:
368 g_value_set_string (value, filter->device_name);
371 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
377 gst_dv1394src_provide_clock (GstElement * element)
379 GstDV1394Src *dv1394src = GST_DV1394SRC (element);
381 return GST_CLOCK_CAST (gst_object_ref (dv1394src->provided_clock));
384 #ifdef HAVE_LIBIEC61883
385 static GstDV1394Src *
386 gst_dv1394src_from_raw1394handle (raw1394handle_t handle)
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));
393 #else /* HAVE_LIBIEC61883 */
394 static GstDV1394Src *
395 gst_dv1394src_from_raw1394handle (raw1394handle_t handle)
397 return GST_DV1394SRC (raw1394_get_userdata (handle));
399 #endif /* HAVE_LIBIEC61883 */
401 #ifdef HAVE_LIBIEC61883
403 gst_dv1394src_iec61883_receive (unsigned char *data, int len,
404 int complete, void *cbdata)
406 GstDV1394Src *dv1394src = GST_DV1394SRC (cbdata);
408 if (G_UNLIKELY (!GST_PAD_CAPS (GST_BASE_SRC_PAD (dv1394src)))) {
410 unsigned char *p = data;
412 // figure format (NTSC/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);
423 dv1394src->frame_size = NTSC_FRAMESIZE;
424 dv1394src->frame_rate = NTSC_FRAMERATE;
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);
431 gst_pad_set_caps (GST_BASE_SRC_PAD (dv1394src), caps);
432 gst_caps_unref (caps);
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) {
442 buf = gst_buffer_new_and_alloc (dv1394src->frame_size);
444 GST_BUFFER_OFFSET (buf) = dv1394src->frame_sequence;
445 bufdata = GST_BUFFER_DATA (buf);
446 memcpy (bufdata, data, len);
447 dv1394src->buf = buf;
450 dv1394src->frame_sequence++;
456 gst_dv1394src_iso_receive (raw1394handle_t handle, int channel, size_t len,
459 GstDV1394Src *dv1394src = gst_dv1394src_from_raw1394handle (handle);
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
467 unsigned char *p = (unsigned char *) &data[3];
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];
473 /* if we are at the beginning of a frame,
474 we set buf=frame, and alloc a new buffer for frame
476 if (section_type == 0 && dif_sequence == 0) { // dif header
477 if (!GST_PAD_CAPS (GST_BASE_SRC_PAD (dv1394src))) {
480 // figure format (NTSC/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);
491 dv1394src->frame_size = NTSC_FRAMESIZE;
492 dv1394src->frame_rate = NTSC_FRAMERATE;
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);
499 gst_pad_set_caps (GST_BASE_SRC_PAD (dv1394src), caps);
500 gst_caps_unref (caps);
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;
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);
514 if ((dv1394src->frame_sequence + 1) % (dv1394src->skip +
515 dv1394src->consecutive) < dv1394src->consecutive) {
519 buf = gst_buffer_new_and_alloc (dv1394src->frame_size);
521 /* fill in offset, duration, timestamp */
522 GST_BUFFER_OFFSET (buf) = dv1394src->frame_sequence;
523 dv1394src->frame = buf;
525 dv1394src->frame_sequence++;
526 dv1394src->bytes_in_frame = 0;
529 if (dv1394src->frame != NULL) {
530 guint8 *data = GST_BUFFER_DATA (dv1394src->frame);
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);
538 case 1: /* 2 Subcode blocks */
539 memcpy (data + dif_sequence * 150 * 80 + (1 + dif_block) * 80, p,
543 case 2: /* 3 VAUX blocks */
544 memcpy (data + dif_sequence * 150 * 80 + (3 + dif_block) * 80, p,
548 case 3: /* 9 Audio blocks interleaved with video */
549 memcpy (data + dif_sequence * 150 * 80 + (6 + dif_block * 16) * 80, p,
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);
558 default: /* we can't handle any other data */
561 dv1394src->bytes_in_frame += 480;
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
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)
579 gst_dv1394src_bus_reset (raw1394handle_t handle, unsigned int generation)
584 GstStructure *structure;
585 gint current_device_change;
588 src = gst_dv1394src_from_raw1394handle (handle);
590 GST_INFO_OBJECT (src, "have bus reset");
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 */
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;
608 current_device_change = 0;
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;
619 structure = gst_structure_new ("ieee1394-bus-reset", "nodecount", G_TYPE_INT,
620 nodecount, "current-device-change", G_TYPE_INT, current_device_change,
622 message = gst_message_new_element (GST_OBJECT (src), structure);
623 gst_element_post_message (GST_ELEMENT (src), message);
629 gst_dv1394src_create (GstPushSrc * psrc, GstBuffer ** buf)
631 GstDV1394Src *dv1394src = GST_DV1394SRC (psrc);
633 struct pollfd pollfds[2];
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;
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;
647 int res = poll (pollfds, 2, -1);
649 if (G_UNLIKELY (res < 0)) {
650 if (errno == EAGAIN || errno == EINTR)
653 goto error_while_polling;
656 if (G_UNLIKELY (pollfds[1].revents)) {
659 if (pollfds[1].revents & POLLIN)
660 READ_COMMAND (dv1394src, command, res);
663 } else if (G_LIKELY (pollfds[0].revents & POLLIN)) {
664 /* shouldn't block in theory */
665 raw1394_loop_iterate (dv1394src->handle);
672 g_assert (dv1394src->buf);
674 caps = gst_pad_get_caps (GST_BASE_SRC_PAD (psrc));
675 gst_buffer_set_caps (dv1394src->buf, caps);
676 gst_caps_unref (caps);
678 *buf = dv1394src->buf;
679 dv1394src->buf = NULL;
684 GST_ELEMENT_ERROR (dv1394src, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
685 return GST_FLOW_UNEXPECTED;
689 GST_DEBUG_OBJECT (dv1394src, "told to stop, shutting down");
690 return GST_FLOW_WRONG_STATE;
695 gst_dv1394src_discover_avc_node (GstDV1394Src * src)
699 int m = src->num_ports;
701 if (src->port >= 0) {
702 /* search on explicit port */
707 /* loop over all our ports */
708 for (; j < m && node == -1; j++) {
709 raw1394handle_t handle;
710 struct raw1394_portinfo pinf[16];
713 handle = raw1394_new_handle ();
715 GST_WARNING ("raw1394 - failed to get handle: %s.\n", strerror (errno));
718 if (raw1394_get_port_info (handle, pinf, 16) < 0) {
719 GST_WARNING ("raw1394 - failed to get port info: %s.\n",
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));
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)) {
738 src->uri = g_strdup_printf ("dv://%d", src->port);
742 rom1394_directory rom_dir;
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);
749 if ((rom1394_get_node_type (&rom_dir) == ROM1394_NODE_TYPE_AVC) &&
750 avc1394_check_subunit_type (handle, i, AVC1394_SUBUNIT_TYPE_VCR)) {
753 src->guid = rom1394_get_guid (handle, i);
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);
760 rom1394_free_directory (&rom_dir);
764 raw1394_destroy_handle (handle);
770 gst_dv1394src_start (GstBaseSrc * bsrc)
772 GstDV1394Src *src = GST_DV1394SRC (bsrc);
775 src->connected = FALSE;
777 if (socketpair (PF_UNIX, SOCK_STREAM, 0, control_sock) < 0)
780 READ_SOCKET (src) = control_sock[0];
781 WRITE_SOCKET (src) = control_sock[1];
783 fcntl (READ_SOCKET (src), F_SETFL, O_NONBLOCK);
784 fcntl (WRITE_SOCKET (src), F_SETFL, O_NONBLOCK);
786 src->handle = raw1394_new_handle ();
790 goto permission_denied;
791 else if (errno == ENOENT)
797 src->num_ports = raw1394_get_port_info (src->handle, src->pinfo, 16);
799 if (src->num_ports == 0)
802 if (src->use_avc || src->port == -1)
803 src->avc_node = gst_dv1394src_discover_avc_node (src);
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);
811 goto cannot_set_port;
813 raw1394_set_userdata (src->handle, src);
814 raw1394_set_bus_reset_handler (src->handle, gst_dv1394src_bus_reset);
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;
823 raw1394_set_iso_handler (src->handle, src->channel,
824 gst_dv1394src_iso_receive);
827 GST_DEBUG_OBJECT (src, "successfully opened up 1394 connection");
828 src->connected = TRUE;
830 #ifdef HAVE_LIBIEC61883
831 if (iec61883_dv_fb_start (src->iec61883dv, src->channel) != 0)
834 if (raw1394_start_iso_rcv (src->handle, src->channel) < 0)
839 raw1394handle_t avc_handle = raw1394_new_handle_on_port (src->port);
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);
849 GST_WARNING_OBJECT (src, "Starting VCR via avc1394 failed: %s",
854 gst_1394_clock_set_handle (src->provided_clock, src->handle);
860 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
866 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL), GST_ERROR_SYSTEM);
871 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL), GST_ERROR_SYSTEM);
876 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
877 ("can't get raw1394 handle (%s)", g_strerror (errno)));
882 raw1394_destroy_handle (src->handle);
884 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
885 ("no ports available for raw1394"));
890 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
891 ("can't set 1394 port %d", src->port));
896 raw1394_destroy_handle (src->handle);
898 #ifdef HAVE_LIBIEC61883
899 iec61883_dv_fb_close (src->iec61883dv);
900 src->iec61883dv = NULL;
902 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
903 ("can't start 1394 iso receive"));
906 #ifdef HAVE_LIBIEC61883
907 cannot_initialise_dv:
909 raw1394_destroy_handle (src->handle);
911 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
912 ("can't initialise iec61883 dv"));
919 gst_dv1394src_stop (GstBaseSrc * bsrc)
921 GstDV1394Src *src = GST_DV1394SRC (bsrc);
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);
930 raw1394_stop_iso_rcv (src->handle, src->channel);
934 raw1394handle_t avc_handle = raw1394_new_handle_on_port (src->port);
936 /* pause and stop the VCR */
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);
945 GST_WARNING_OBJECT (src, "Starting VCR via avc1394 failed: %s",
950 gst_1394_clock_unset_handle (src->provided_clock);
952 raw1394_destroy_handle (src->handle);
958 gst_dv1394src_unlock (GstBaseSrc * bsrc)
960 GstDV1394Src *src = GST_DV1394SRC (bsrc);
962 SEND_COMMAND (src, CONTROL_STOP);
968 gst_dv1394src_query (GstBaseSrc * basesrc, GstQuery * query)
970 switch (GST_QUERY_TYPE (query)) {
971 case GST_QUERY_LATENCY:
973 gst_query_set_latency (query, TRUE, GST_SECOND / 25, GST_CLOCK_TIME_NONE);
983 return GST_BASE_SRC_CLASS (parent_class)->query (basesrc, query);
987 gst_dv1394src_update_device_name (GstDV1394Src * src)
989 raw1394handle_t handle;
990 gint portcount, port, nodecount, node;
991 rom1394_directory directory;
993 g_free (src->device_name);
994 src->device_name = NULL;
996 GST_LOG_OBJECT (src, "updating device name for current GUID");
998 handle = raw1394_new_handle ();
1001 goto gethandle_failed;
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);
1015 GST_WARNING ("error reading rom directory for node %d", node);
1022 src->device_name = g_strdup ("Unknown"); /* FIXME: translate? */
1026 raw1394_destroy_handle (handle);
1032 GST_WARNING ("failed to get raw1394 handle: %s", g_strerror (errno));
1033 src->device_name = g_strdup ("Unknown"); /* FIXME: translate? */
1038 /*** GSTURIHANDLER INTERFACE *************************************************/
1041 gst_dv1394src_uri_get_type (void)
1047 gst_dv1394src_uri_get_protocols (void)
1049 static gchar *protocols[] = { "dv", NULL };
1054 static const gchar *
1055 gst_dv1394src_uri_get_uri (GstURIHandler * handler)
1057 GstDV1394Src *gst_dv1394src = GST_DV1394SRC (handler);
1059 return gst_dv1394src->uri;
1063 gst_dv1394src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
1065 gchar *protocol, *location;
1066 gboolean ret = TRUE;
1067 GstDV1394Src *gst_dv1394src = GST_DV1394SRC (handler);
1069 protocol = gst_uri_get_protocol (uri);
1070 if (strcmp (protocol, "dv") != 0) {
1076 location = gst_uri_get_location (uri);
1077 if (location && *location != '\0')
1078 gst_dv1394src->port = strtol (location, NULL, 10);
1080 gst_dv1394src->port = DEFAULT_PORT;
1082 g_free (gst_dv1394src->uri);
1083 gst_dv1394src->uri = g_strdup_printf ("dv://%d", gst_dv1394src->port);
1089 gst_dv1394src_uri_handler_init (gpointer g_iface, gpointer iface_data)
1091 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
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;