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., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
23 * SECTION:element-dv1394src
25 * Read DV (digital video) data from firewire port.
28 * <title>Example launch line</title>
30 * gst-launch-1.0 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 G_GNUC_UNUSED _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);
133 static GstStateChangeReturn gst_dv1394_src_change_state (GstElement * element,
134 GstStateChange transition);
136 static gboolean gst_dv1394src_start (GstBaseSrc * bsrc);
137 static gboolean gst_dv1394src_stop (GstBaseSrc * bsrc);
138 static gboolean gst_dv1394src_unlock (GstBaseSrc * bsrc);
140 static GstFlowReturn gst_dv1394src_create (GstPushSrc * psrc, GstBuffer ** buf);
142 static gboolean gst_dv1394src_query (GstBaseSrc * src, GstQuery * query);
143 static void gst_dv1394src_update_device_name (GstDV1394Src * src);
145 #define gst_dv1394src_parent_class parent_class
146 G_DEFINE_TYPE_WITH_CODE (GstDV1394Src, gst_dv1394src, GST_TYPE_PUSH_SRC,
147 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER,
148 gst_dv1394src_uri_handler_init));
150 static guint gst_dv1394src_signals[LAST_SIGNAL] = { 0 };
153 gst_dv1394src_class_init (GstDV1394SrcClass * klass)
155 GObjectClass *gobject_class;
156 GstElementClass *gstelement_class;
157 GstBaseSrcClass *gstbasesrc_class;
158 GstPushSrcClass *gstpushsrc_class;
160 gobject_class = (GObjectClass *) klass;
161 gstelement_class = (GstElementClass *) klass;
162 gstbasesrc_class = (GstBaseSrcClass *) klass;
163 gstpushsrc_class = (GstPushSrcClass *) klass;
165 gobject_class->set_property = gst_dv1394src_set_property;
166 gobject_class->get_property = gst_dv1394src_get_property;
167 gobject_class->dispose = gst_dv1394src_dispose;
169 gstelement_class->provide_clock = gst_dv1394src_provide_clock;
170 gstelement_class->change_state = gst_dv1394_src_change_state;
172 gst_dv1394src_signals[SIGNAL_FRAME_DROPPED] =
173 g_signal_new ("frame-dropped", G_TYPE_FROM_CLASS (klass),
174 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDV1394SrcClass, frame_dropped),
175 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
177 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_PORT,
178 g_param_spec_int ("port", "Port", "Port number (-1 automatic)",
179 -1, 16, DEFAULT_PORT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
180 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_CHANNEL,
181 g_param_spec_int ("channel", "Channel", "Channel number for listening",
182 0, 64, DEFAULT_CHANNEL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
183 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_CONSECUTIVE,
184 g_param_spec_int ("consecutive", "consecutive frames",
185 "send n consecutive frames after skipping", 1, G_MAXINT,
186 DEFAULT_CONSECUTIVE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
187 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SKIP,
188 g_param_spec_int ("skip", "skip frames", "skip n frames",
189 0, G_MAXINT, DEFAULT_SKIP,
190 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
191 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_DROP_INCOMPLETE,
192 g_param_spec_boolean ("drop-incomplete", "drop incomplete",
193 "drop incomplete frames", DEFAULT_DROP_INCOMPLETE,
194 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
195 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_USE_AVC,
196 g_param_spec_boolean ("use-avc", "Use AV/C", "Use AV/C VTR control",
197 DEFAULT_USE_AVC, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
198 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_GUID,
199 g_param_spec_uint64 ("guid", "GUID",
200 "select one of multiple DV devices by its GUID. use a hexadecimal "
201 "like 0xhhhhhhhhhhhhhhhh. (0 = no guid)", 0, G_MAXUINT64,
202 DEFAULT_GUID, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
204 * GstDV1394Src:device-name:
206 * Descriptive name of the currently opened device
208 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_DEVICE_NAME,
209 g_param_spec_string ("device-name", "device name",
210 "user-friendly name of the device", "Default",
211 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
213 gstbasesrc_class->negotiate = NULL;
214 gstbasesrc_class->start = gst_dv1394src_start;
215 gstbasesrc_class->stop = gst_dv1394src_stop;
216 gstbasesrc_class->unlock = gst_dv1394src_unlock;
217 gstbasesrc_class->query = gst_dv1394src_query;
219 gstpushsrc_class->create = gst_dv1394src_create;
221 gst_element_class_add_static_pad_template (gstelement_class, &src_factory);
223 gst_element_class_set_static_metadata (gstelement_class,
224 "Firewire (1394) DV video source", "Source/Video",
225 "Source for DV video data from firewire port",
226 "Erik Walthinsen <omega@temple-baptist.com>, "
227 "Daniel Fischer <dan@f3c.com>, " "Wim Taymans <wim@fluendo.com>, "
228 "Zaheer Abbas Merali <zaheerabbas at merali dot org>");
230 GST_DEBUG_CATEGORY_INIT (dv1394src_debug, "dv1394src", 0,
231 "DV firewire source");
235 gst_dv1394src_init (GstDV1394Src * dv1394src)
237 GstPad *srcpad = GST_BASE_SRC_PAD (dv1394src);
239 gst_base_src_set_live (GST_BASE_SRC (dv1394src), TRUE);
240 gst_base_src_set_format (GST_BASE_SRC (dv1394src), GST_FORMAT_TIME);
241 gst_base_src_set_do_timestamp (GST_BASE_SRC (dv1394src), TRUE);
242 gst_pad_use_fixed_caps (srcpad);
244 dv1394src->port = DEFAULT_PORT;
245 dv1394src->channel = DEFAULT_CHANNEL;
247 dv1394src->consecutive = DEFAULT_CONSECUTIVE;
248 dv1394src->skip = DEFAULT_SKIP;
249 dv1394src->drop_incomplete = DEFAULT_DROP_INCOMPLETE;
250 dv1394src->use_avc = DEFAULT_USE_AVC;
251 dv1394src->guid = DEFAULT_GUID;
252 dv1394src->uri = g_strdup_printf ("dv://%d", dv1394src->port);
253 dv1394src->device_name = g_strdup_printf ("Default");
255 READ_SOCKET (dv1394src) = -1;
256 WRITE_SOCKET (dv1394src) = -1;
258 /* initialized when first header received */
259 dv1394src->frame_size = 0;
261 dv1394src->buf = NULL;
262 dv1394src->frame = NULL;
263 dv1394src->frame_sequence = 0;
265 dv1394src->provided_clock = gst_1394_clock_new ("dv1394clock");
269 gst_dv1394src_dispose (GObject * object)
271 GstDV1394Src *src = GST_DV1394SRC (object);
273 if (src->provided_clock) {
274 g_object_unref (src->provided_clock);
280 g_free (src->device_name);
281 src->device_name = NULL;
283 G_OBJECT_CLASS (parent_class)->dispose (object);
287 gst_dv1394src_set_property (GObject * object, guint prop_id,
288 const GValue * value, GParamSpec * pspec)
290 GstDV1394Src *filter = GST_DV1394SRC (object);
294 filter->port = g_value_get_int (value);
295 g_free (filter->uri);
296 filter->uri = g_strdup_printf ("dv://%d", filter->port);
299 filter->channel = g_value_get_int (value);
302 filter->skip = g_value_get_int (value);
304 case PROP_CONSECUTIVE:
305 filter->consecutive = g_value_get_int (value);
307 case PROP_DROP_INCOMPLETE:
308 filter->drop_incomplete = g_value_get_boolean (value);
311 filter->use_avc = g_value_get_boolean (value);
314 filter->guid = g_value_get_uint64 (value);
315 gst_dv1394src_update_device_name (filter);
323 gst_dv1394src_get_property (GObject * object, guint prop_id, GValue * value,
326 GstDV1394Src *filter = GST_DV1394SRC (object);
330 g_value_set_int (value, filter->port);
333 g_value_set_int (value, filter->channel);
336 g_value_set_int (value, filter->skip);
338 case PROP_CONSECUTIVE:
339 g_value_set_int (value, filter->consecutive);
341 case PROP_DROP_INCOMPLETE:
342 g_value_set_boolean (value, filter->drop_incomplete);
345 g_value_set_boolean (value, filter->use_avc);
348 g_value_set_uint64 (value, filter->guid);
350 case PROP_DEVICE_NAME:
351 g_value_set_string (value, filter->device_name);
354 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
360 gst_dv1394src_provide_clock (GstElement * element)
362 GstDV1394Src *dv1394src = GST_DV1394SRC (element);
364 return GST_CLOCK_CAST (gst_object_ref (dv1394src->provided_clock));
367 static GstStateChangeReturn
368 gst_dv1394_src_change_state (GstElement * element, GstStateChange transition)
370 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
371 GstDV1394Src *src = GST_DV1394SRC (element);
373 switch (transition) {
374 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
375 gst_element_post_message (element,
376 gst_message_new_clock_lost (GST_OBJECT_CAST (element),
377 GST_CLOCK_CAST (src->provided_clock)));
383 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
384 if (ret == GST_STATE_CHANGE_FAILURE)
387 switch (transition) {
388 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
389 gst_element_post_message (element,
390 gst_message_new_clock_provide (GST_OBJECT_CAST (element),
391 GST_CLOCK_CAST (src->provided_clock), TRUE));
400 #ifdef HAVE_LIBIEC61883
401 static GstDV1394Src *
402 gst_dv1394src_from_raw1394handle (raw1394handle_t handle)
404 iec61883_dv_t dv = (iec61883_dv_t) raw1394_get_userdata (handle);
405 iec61883_dv_fb_t dv_fb =
406 (iec61883_dv_fb_t) iec61883_dv_get_callback_data (dv);
407 return GST_DV1394SRC (iec61883_dv_fb_get_callback_data (dv_fb));
409 #else /* HAVE_LIBIEC61883 */
410 static GstDV1394Src *
411 gst_dv1394src_from_raw1394handle (raw1394handle_t handle)
413 return GST_DV1394SRC (raw1394_get_userdata (handle));
415 #endif /* HAVE_LIBIEC61883 */
417 #ifdef HAVE_LIBIEC61883
419 gst_dv1394src_iec61883_receive (unsigned char *data, int len,
420 int complete, void *cbdata)
422 GstDV1394Src *dv1394src = GST_DV1394SRC (cbdata);
424 if (G_UNLIKELY (!gst_pad_has_current_caps (GST_BASE_SRC_PAD (dv1394src)))) {
426 unsigned char *p = data;
428 // figure format (NTSC/PAL)
431 dv1394src->frame_size = PAL_FRAMESIZE;
432 dv1394src->frame_rate = PAL_FRAMERATE;
433 GST_DEBUG ("PAL data");
434 caps = gst_caps_new_simple ("video/x-dv",
435 "format", G_TYPE_STRING, "PAL",
436 "systemstream", G_TYPE_BOOLEAN, TRUE, NULL);
439 dv1394src->frame_size = NTSC_FRAMESIZE;
440 dv1394src->frame_rate = NTSC_FRAMERATE;
442 ("NTSC data [untested] - please report success/failure to <dan@f3c.com>");
443 caps = gst_caps_new_simple ("video/x-dv",
444 "format", G_TYPE_STRING, "NTSC",
445 "systemstream", G_TYPE_BOOLEAN, TRUE, NULL);
447 gst_pad_set_caps (GST_BASE_SRC_PAD (dv1394src), caps);
448 gst_caps_unref (caps);
451 dv1394src->frame = NULL;
452 if (G_LIKELY ((dv1394src->frame_sequence + 1) % (dv1394src->skip +
453 dv1394src->consecutive) < dv1394src->consecutive)) {
454 if (complete && len == dv1394src->frame_size) {
457 buf = gst_buffer_new_and_alloc (dv1394src->frame_size);
459 GST_BUFFER_OFFSET (buf) = dv1394src->frame_sequence;
460 gst_buffer_fill (buf, 0, data, len);
461 dv1394src->buf = buf;
464 dv1394src->frame_sequence++;
470 gst_dv1394src_iso_receive (raw1394handle_t handle, int channel, size_t len,
473 GstDV1394Src *dv1394src = gst_dv1394src_from_raw1394handle (handle);
477 the following code taken from kino-0.51 (Dan Dennedy/Charles Yates)
478 Kindly relicensed under the LGPL. See the commit log for version 1.6 of
481 unsigned char *p = (unsigned char *) &data[3];
483 int section_type = p[0] >> 5; /* section type is in bits 5 - 7 */
484 int dif_sequence = p[1] >> 4; /* dif sequence number is in bits 4 - 7 */
485 int dif_block = p[2];
487 /* if we are at the beginning of a frame,
488 we set buf=frame, and alloc a new buffer for frame
490 if (section_type == 0 && dif_sequence == 0) { // dif header
491 if (!GST_PAD_CAPS (GST_BASE_SRC_PAD (dv1394src))) {
494 // figure format (NTSC/PAL)
497 dv1394src->frame_size = PAL_FRAMESIZE;
498 dv1394src->frame_rate = PAL_FRAMERATE;
499 GST_DEBUG ("PAL data");
500 caps = gst_caps_new_simple ("video/x-dv",
501 "format", G_TYPE_STRING, "PAL",
502 "systemstream", G_TYPE_BOOLEAN, TRUE, NULL);
505 dv1394src->frame_size = NTSC_FRAMESIZE;
506 dv1394src->frame_rate = NTSC_FRAMERATE;
508 ("NTSC data [untested] - please report success/failure to <dan@f3c.com>");
509 caps = gst_caps_new_simple ("video/x-dv",
510 "format", G_TYPE_STRING, "NTSC",
511 "systemstream", G_TYPE_BOOLEAN, TRUE, NULL);
513 gst_pad_set_caps (GST_BASE_SRC_PAD (dv1394src), caps);
514 gst_caps_unref (caps);
516 // drop last frame when not complete
517 if (!dv1394src->drop_incomplete
518 || dv1394src->bytes_in_frame == dv1394src->frame_size) {
519 dv1394src->buf = dv1394src->frame;
521 GST_INFO_OBJECT (GST_ELEMENT (dv1394src), "incomplete frame dropped");
522 g_signal_emit (G_OBJECT (dv1394src),
523 gst_dv1394src_signals[SIGNAL_FRAME_DROPPED], 0);
524 if (dv1394src->frame) {
525 gst_buffer_unref (dv1394src->frame);
528 if ((dv1394src->frame_sequence + 1) % (dv1394src->skip +
529 dv1394src->consecutive) < dv1394src->consecutive) {
533 buf = gst_buffer_new_and_alloc (dv1394src->frame_size);
535 /* fill in offset, duration, timestamp */
536 GST_BUFFER_OFFSET (buf) = dv1394src->frame_sequence;
537 dv1394src->frame = buf;
539 dv1394src->frame_sequence++;
540 dv1394src->bytes_in_frame = 0;
543 if (dv1394src->frame != NULL) {
544 guint8 *data = GST_BUFFER_DATA (dv1394src->frame);
546 switch (section_type) {
547 case 0: /* 1 Header block */
548 /* p[3] |= 0x80; // hack to force PAL data */
549 memcpy (data + dif_sequence * 150 * 80, p, 480);
552 case 1: /* 2 Subcode blocks */
553 memcpy (data + dif_sequence * 150 * 80 + (1 + dif_block) * 80, p,
557 case 2: /* 3 VAUX blocks */
558 memcpy (data + dif_sequence * 150 * 80 + (3 + dif_block) * 80, p,
562 case 3: /* 9 Audio blocks interleaved with video */
563 memcpy (data + dif_sequence * 150 * 80 + (6 + dif_block * 16) * 80, p,
567 case 4: /* 135 Video blocks interleaved with audio */
568 memcpy (data + dif_sequence * 150 * 80 + (7 + (dif_block / 15) +
569 dif_block) * 80, p, 480);
572 default: /* we can't handle any other data */
575 dv1394src->bytes_in_frame += 480;
583 * When an ieee1394 bus reset happens, usually a device has been removed
584 * or added. We send a message on the message bus with the node count
585 * and whether the capture device used in this element connected, disconnected
588 * nodecount - integer with number of nodes on bus
589 * current-device-change - integer (1 if device connected, 0 if no change to
590 * current device status, -1 if device disconnected)
593 gst_dv1394src_bus_reset (raw1394handle_t handle, unsigned int generation)
598 GstStructure *structure;
599 gint current_device_change;
602 src = gst_dv1394src_from_raw1394handle (handle);
604 GST_INFO_OBJECT (src, "have bus reset");
606 /* update generation - told to do so by docs */
607 raw1394_update_generation (handle, generation);
608 nodecount = raw1394_get_nodecount (handle);
609 /* allocate memory for portinfo */
611 /* current_device_change is -1 if camera disconnected, 0 if other device
612 * connected or 1 if camera has now connected */
613 current_device_change = -1;
614 for (i = 0; i < nodecount; i++) {
615 if (src->guid == rom1394_get_guid (handle, i)) {
616 /* Camera is with us */
617 GST_DEBUG ("Camera is with us");
618 if (!src->connected) {
619 current_device_change = 1;
620 src->connected = TRUE;
622 current_device_change = 0;
625 if (src->connected && current_device_change == -1) {
626 GST_DEBUG ("Camera has disconnected");
627 src->connected = FALSE;
628 } else if (!src->connected && current_device_change == -1) {
629 GST_DEBUG ("Camera is still not with us");
630 current_device_change = 0;
633 structure = gst_structure_new ("ieee1394-bus-reset", "nodecount", G_TYPE_INT,
634 nodecount, "current-device-change", G_TYPE_INT, current_device_change,
636 message = gst_message_new_element (GST_OBJECT (src), structure);
637 gst_element_post_message (GST_ELEMENT (src), message);
643 gst_dv1394src_create (GstPushSrc * psrc, GstBuffer ** buf)
645 GstDV1394Src *dv1394src = GST_DV1394SRC (psrc);
646 struct pollfd pollfds[2];
648 pollfds[0].fd = raw1394_get_fd (dv1394src->handle);
649 pollfds[0].events = POLLIN | POLLERR | POLLHUP | POLLPRI;
650 pollfds[1].fd = READ_SOCKET (dv1394src);
651 pollfds[1].events = POLLIN | POLLERR | POLLHUP | POLLPRI;
653 if (G_UNLIKELY (dv1394src->buf)) {
654 /* maybe we had an error before, and there's a stale buffer? */
655 gst_buffer_unref (dv1394src->buf);
656 dv1394src->buf = NULL;
660 int res = poll (pollfds, 2, -1);
662 if (G_UNLIKELY (res < 0)) {
663 if (errno == EAGAIN || errno == EINTR)
666 goto error_while_polling;
669 if (G_UNLIKELY (pollfds[1].revents)) {
672 if (pollfds[1].revents & POLLIN)
673 READ_COMMAND (dv1394src, command, res);
676 } else if (G_LIKELY (pollfds[0].revents & POLLIN)) {
677 /* shouldn't block in theory */
678 raw1394_loop_iterate (dv1394src->handle);
685 g_assert (dv1394src->buf);
687 *buf = dv1394src->buf;
688 dv1394src->buf = NULL;
693 GST_ELEMENT_ERROR (dv1394src, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
698 GST_DEBUG_OBJECT (dv1394src, "told to stop, shutting down");
699 return GST_FLOW_FLUSHING;
704 gst_dv1394src_discover_avc_node (GstDV1394Src * src)
708 int m = src->num_ports;
710 if (src->port >= 0) {
711 /* search on explicit port */
716 /* loop over all our ports */
717 for (; j < m && node == -1; j++) {
718 raw1394handle_t handle;
719 struct raw1394_portinfo pinf[16];
722 handle = raw1394_new_handle ();
724 GST_WARNING ("raw1394 - failed to get handle: %s.\n", strerror (errno));
727 if (raw1394_get_port_info (handle, pinf, 16) < 0) {
728 GST_WARNING ("raw1394 - failed to get port info: %s.\n",
733 /* tell raw1394 which host adapter to use */
734 if (raw1394_set_port (handle, j) < 0) {
735 GST_WARNING ("raw1394 - failed to set set port: %s.\n", strerror (errno));
739 /* now loop over all the nodes */
740 for (i = 0; i < raw1394_get_nodecount (handle); i++) {
741 /* are we looking for an explicit GUID ? */
742 if (src->guid != 0) {
743 if (src->guid == rom1394_get_guid (handle, i)) {
747 src->uri = g_strdup_printf ("dv://%d", src->port);
751 rom1394_directory rom_dir;
753 /* select first AV/C Tape Recorder Player node */
754 if (rom1394_get_directory (handle, i, &rom_dir) < 0) {
755 GST_WARNING ("error reading config rom directory for node %d\n", i);
758 if ((rom1394_get_node_type (&rom_dir) == ROM1394_NODE_TYPE_AVC) &&
759 avc1394_check_subunit_type (handle, i, AVC1394_SUBUNIT_TYPE_VCR)) {
762 src->guid = rom1394_get_guid (handle, i);
764 src->uri = g_strdup_printf ("dv://%d", src->port);
765 g_free (src->device_name);
766 src->device_name = g_strdup (rom_dir.label);
769 rom1394_free_directory (&rom_dir);
773 raw1394_destroy_handle (handle);
779 gst_dv1394src_start (GstBaseSrc * bsrc)
781 GstDV1394Src *src = GST_DV1394SRC (bsrc);
784 src->connected = FALSE;
786 if (socketpair (PF_UNIX, SOCK_STREAM, 0, control_sock) < 0)
789 READ_SOCKET (src) = control_sock[0];
790 WRITE_SOCKET (src) = control_sock[1];
792 if (fcntl (READ_SOCKET (src), F_SETFL, O_NONBLOCK) < 0)
793 GST_ERROR_OBJECT (src, "failed to make read socket non-blocking: %s",
795 if (fcntl (WRITE_SOCKET (src), F_SETFL, O_NONBLOCK) < 0)
796 GST_ERROR_OBJECT (src, "failed to make write socket non-blocking: %s",
799 src->handle = raw1394_new_handle ();
803 goto permission_denied;
804 else if (errno == ENOENT)
810 src->num_ports = raw1394_get_port_info (src->handle, src->pinfo, 16);
812 if (src->num_ports == 0)
815 if (src->use_avc || src->port == -1)
816 src->avc_node = gst_dv1394src_discover_avc_node (src);
818 /* lets destroy handle and create one on port
819 this is more reliable than setting port on
820 the existing handle */
821 raw1394_destroy_handle (src->handle);
822 src->handle = raw1394_new_handle_on_port (src->port);
824 goto cannot_set_port;
826 raw1394_set_userdata (src->handle, src);
827 raw1394_set_bus_reset_handler (src->handle, gst_dv1394src_bus_reset);
829 #ifdef HAVE_LIBIEC61883
830 if ((src->iec61883dv =
831 iec61883_dv_fb_init (src->handle,
832 gst_dv1394src_iec61883_receive, src)) == NULL)
833 goto cannot_initialise_dv;
836 raw1394_set_iso_handler (src->handle, src->channel,
837 gst_dv1394src_iso_receive);
840 GST_DEBUG_OBJECT (src, "successfully opened up 1394 connection");
841 src->connected = TRUE;
843 #ifdef HAVE_LIBIEC61883
844 if (iec61883_dv_fb_start (src->iec61883dv, src->channel) != 0)
847 if (raw1394_start_iso_rcv (src->handle, src->channel) < 0)
852 raw1394handle_t avc_handle = raw1394_new_handle_on_port (src->port);
856 if (!avc1394_vcr_is_recording (avc_handle, src->avc_node)
857 && avc1394_vcr_is_playing (avc_handle, src->avc_node)
858 != AVC1394_VCR_OPERAND_PLAY_FORWARD)
859 avc1394_vcr_play (avc_handle, src->avc_node);
860 raw1394_destroy_handle (avc_handle);
862 GST_WARNING_OBJECT (src, "Starting VCR via avc1394 failed: %s",
867 gst_1394_clock_set_handle (src->provided_clock, src->handle);
873 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
879 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL), GST_ERROR_SYSTEM);
884 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL), GST_ERROR_SYSTEM);
889 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
890 ("can't get raw1394 handle (%s)", g_strerror (errno)));
895 raw1394_destroy_handle (src->handle);
897 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
898 ("no ports available for raw1394"));
903 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
904 ("can't set 1394 port %d", src->port));
909 raw1394_destroy_handle (src->handle);
911 #ifdef HAVE_LIBIEC61883
912 iec61883_dv_fb_close (src->iec61883dv);
913 src->iec61883dv = NULL;
915 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
916 ("can't start 1394 iso receive"));
919 #ifdef HAVE_LIBIEC61883
920 cannot_initialise_dv:
922 raw1394_destroy_handle (src->handle);
924 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
925 ("can't initialise iec61883 dv"));
932 gst_dv1394src_stop (GstBaseSrc * bsrc)
934 GstDV1394Src *src = GST_DV1394SRC (bsrc);
936 close (READ_SOCKET (src));
937 close (WRITE_SOCKET (src));
938 READ_SOCKET (src) = -1;
939 WRITE_SOCKET (src) = -1;
940 #ifdef HAVE_LIBIEC61883
941 iec61883_dv_fb_close (src->iec61883dv);
943 raw1394_stop_iso_rcv (src->handle, src->channel);
947 raw1394handle_t avc_handle = raw1394_new_handle_on_port (src->port);
949 /* pause and stop the VCR */
951 if (!avc1394_vcr_is_recording (avc_handle, src->avc_node)
952 && (avc1394_vcr_is_playing (avc_handle, src->avc_node)
953 != AVC1394_VCR_OPERAND_PLAY_FORWARD_PAUSE))
954 avc1394_vcr_pause (avc_handle, src->avc_node);
955 avc1394_vcr_stop (avc_handle, src->avc_node);
956 raw1394_destroy_handle (avc_handle);
958 GST_WARNING_OBJECT (src, "Starting VCR via avc1394 failed: %s",
963 gst_1394_clock_unset_handle (src->provided_clock);
965 raw1394_destroy_handle (src->handle);
971 gst_dv1394src_unlock (GstBaseSrc * bsrc)
973 GstDV1394Src *src = GST_DV1394SRC (bsrc);
975 SEND_COMMAND (src, CONTROL_STOP);
981 gst_dv1394src_query (GstBaseSrc * basesrc, GstQuery * query)
983 switch (GST_QUERY_TYPE (query)) {
984 case GST_QUERY_LATENCY:
986 gst_query_set_latency (query, TRUE, GST_SECOND / 25, GST_SECOND / 25);
996 return GST_BASE_SRC_CLASS (parent_class)->query (basesrc, query);
1000 gst_dv1394src_update_device_name (GstDV1394Src * src)
1002 raw1394handle_t handle;
1003 gint portcount, port, nodecount, node;
1004 rom1394_directory directory;
1006 g_free (src->device_name);
1007 src->device_name = NULL;
1009 GST_LOG_OBJECT (src, "updating device name for current GUID");
1011 handle = raw1394_new_handle ();
1014 goto gethandle_failed;
1016 portcount = raw1394_get_port_info (handle, NULL, 0);
1017 for (port = 0; port < portcount; port++) {
1018 if (raw1394_set_port (handle, port) >= 0) {
1019 nodecount = raw1394_get_nodecount (handle);
1020 for (node = 0; node < nodecount; node++) {
1021 if (src->guid == rom1394_get_guid (handle, node)) {
1022 if (rom1394_get_directory (handle, node, &directory) >= 0) {
1023 g_free (src->device_name);
1024 src->device_name = g_strdup (directory.label);
1025 rom1394_free_directory (&directory);
1028 GST_WARNING ("error reading rom directory for node %d", node);
1035 src->device_name = g_strdup ("Unknown"); /* FIXME: translate? */
1039 raw1394_destroy_handle (handle);
1045 GST_WARNING ("failed to get raw1394 handle: %s", g_strerror (errno));
1046 src->device_name = g_strdup ("Unknown"); /* FIXME: translate? */
1051 /*** GSTURIHANDLER INTERFACE *************************************************/
1054 gst_dv1394src_uri_get_type (GType type)
1059 static const gchar *const *
1060 gst_dv1394src_uri_get_protocols (GType type)
1062 static const gchar *protocols[] = { (char *) "dv", NULL };
1068 gst_dv1394src_uri_get_uri (GstURIHandler * handler)
1070 GstDV1394Src *gst_dv1394src = GST_DV1394SRC (handler);
1072 return gst_dv1394src->uri;
1076 gst_dv1394src_uri_set_uri (GstURIHandler * handler, const gchar * uri,
1079 gchar *protocol, *location;
1080 gboolean ret = TRUE;
1081 GstDV1394Src *gst_dv1394src = GST_DV1394SRC (handler);
1083 protocol = gst_uri_get_protocol (uri);
1084 if (strcmp (protocol, "dv") != 0) {
1086 g_set_error (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI, "Invalid DV URI");
1091 location = gst_uri_get_location (uri);
1092 if (location && *location != '\0')
1093 gst_dv1394src->port = strtol (location, NULL, 10);
1095 gst_dv1394src->port = DEFAULT_PORT;
1097 g_free (gst_dv1394src->uri);
1098 gst_dv1394src->uri = g_strdup_printf ("dv://%d", gst_dv1394src->port);
1104 gst_dv1394src_uri_handler_init (gpointer g_iface, gpointer iface_data)
1106 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
1108 iface->get_type = gst_dv1394src_uri_get_type;
1109 iface->get_protocols = gst_dv1394src_uri_get_protocols;
1110 iface->get_uri = gst_dv1394src_uri_get_uri;
1111 iface->set_uri = gst_dv1394src_uri_set_uri;