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
210 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_DEVICE_NAME,
211 g_param_spec_string ("device-name", "device name",
212 "user-friendly name of the device", "Default",
213 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
215 gstbasesrc_class->negotiate = NULL;
216 gstbasesrc_class->start = gst_dv1394src_start;
217 gstbasesrc_class->stop = gst_dv1394src_stop;
218 gstbasesrc_class->unlock = gst_dv1394src_unlock;
219 gstbasesrc_class->query = gst_dv1394src_query;
221 gstpushsrc_class->create = gst_dv1394src_create;
223 gst_element_class_add_pad_template (gstelement_class,
224 gst_static_pad_template_get (&src_factory));
226 gst_element_class_set_static_metadata (gstelement_class,
227 "Firewire (1394) DV video source", "Source/Video",
228 "Source for DV video data from firewire port",
229 "Erik Walthinsen <omega@temple-baptist.com>, "
230 "Daniel Fischer <dan@f3c.com>, " "Wim Taymans <wim@fluendo.com>, "
231 "Zaheer Abbas Merali <zaheerabbas at merali dot org>");
233 GST_DEBUG_CATEGORY_INIT (dv1394src_debug, "dv1394src", 0,
234 "DV firewire source");
238 gst_dv1394src_init (GstDV1394Src * dv1394src)
240 GstPad *srcpad = GST_BASE_SRC_PAD (dv1394src);
242 gst_base_src_set_live (GST_BASE_SRC (dv1394src), TRUE);
243 gst_base_src_set_format (GST_BASE_SRC (dv1394src), GST_FORMAT_TIME);
244 gst_base_src_set_do_timestamp (GST_BASE_SRC (dv1394src), TRUE);
245 gst_pad_use_fixed_caps (srcpad);
247 dv1394src->port = DEFAULT_PORT;
248 dv1394src->channel = DEFAULT_CHANNEL;
250 dv1394src->consecutive = DEFAULT_CONSECUTIVE;
251 dv1394src->skip = DEFAULT_SKIP;
252 dv1394src->drop_incomplete = DEFAULT_DROP_INCOMPLETE;
253 dv1394src->use_avc = DEFAULT_USE_AVC;
254 dv1394src->guid = DEFAULT_GUID;
255 dv1394src->uri = g_strdup_printf ("dv://%d", dv1394src->port);
256 dv1394src->device_name = g_strdup_printf ("Default");
258 READ_SOCKET (dv1394src) = -1;
259 WRITE_SOCKET (dv1394src) = -1;
261 /* initialized when first header received */
262 dv1394src->frame_size = 0;
264 dv1394src->buf = NULL;
265 dv1394src->frame = NULL;
266 dv1394src->frame_sequence = 0;
268 dv1394src->provided_clock = gst_1394_clock_new ("dv1394clock");
272 gst_dv1394src_dispose (GObject * object)
274 GstDV1394Src *src = GST_DV1394SRC (object);
276 if (src->provided_clock) {
277 g_object_unref (src->provided_clock);
283 g_free (src->device_name);
284 src->device_name = NULL;
286 G_OBJECT_CLASS (parent_class)->dispose (object);
290 gst_dv1394src_set_property (GObject * object, guint prop_id,
291 const GValue * value, GParamSpec * pspec)
293 GstDV1394Src *filter = GST_DV1394SRC (object);
297 filter->port = g_value_get_int (value);
298 g_free (filter->uri);
299 filter->uri = g_strdup_printf ("dv://%d", filter->port);
302 filter->channel = g_value_get_int (value);
305 filter->skip = g_value_get_int (value);
307 case PROP_CONSECUTIVE:
308 filter->consecutive = g_value_get_int (value);
310 case PROP_DROP_INCOMPLETE:
311 filter->drop_incomplete = g_value_get_boolean (value);
314 filter->use_avc = g_value_get_boolean (value);
317 filter->guid = g_value_get_uint64 (value);
318 gst_dv1394src_update_device_name (filter);
326 gst_dv1394src_get_property (GObject * object, guint prop_id, GValue * value,
329 GstDV1394Src *filter = GST_DV1394SRC (object);
333 g_value_set_int (value, filter->port);
336 g_value_set_int (value, filter->channel);
339 g_value_set_int (value, filter->skip);
341 case PROP_CONSECUTIVE:
342 g_value_set_int (value, filter->consecutive);
344 case PROP_DROP_INCOMPLETE:
345 g_value_set_boolean (value, filter->drop_incomplete);
348 g_value_set_boolean (value, filter->use_avc);
351 g_value_set_uint64 (value, filter->guid);
353 case PROP_DEVICE_NAME:
354 g_value_set_string (value, filter->device_name);
357 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
363 gst_dv1394src_provide_clock (GstElement * element)
365 GstDV1394Src *dv1394src = GST_DV1394SRC (element);
367 return GST_CLOCK_CAST (gst_object_ref (dv1394src->provided_clock));
370 static GstStateChangeReturn
371 gst_dv1394_src_change_state (GstElement * element, GstStateChange transition)
373 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
374 GstDV1394Src *src = GST_DV1394SRC (element);
376 switch (transition) {
377 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
378 gst_element_post_message (element,
379 gst_message_new_clock_lost (GST_OBJECT_CAST (element),
380 GST_CLOCK_CAST (src->provided_clock)));
386 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
387 if (ret == GST_STATE_CHANGE_FAILURE)
390 switch (transition) {
391 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
392 gst_element_post_message (element,
393 gst_message_new_clock_provide (GST_OBJECT_CAST (element),
394 GST_CLOCK_CAST (src->provided_clock), TRUE));
403 #ifdef HAVE_LIBIEC61883
404 static GstDV1394Src *
405 gst_dv1394src_from_raw1394handle (raw1394handle_t handle)
407 iec61883_dv_t dv = (iec61883_dv_t) raw1394_get_userdata (handle);
408 iec61883_dv_fb_t dv_fb =
409 (iec61883_dv_fb_t) iec61883_dv_get_callback_data (dv);
410 return GST_DV1394SRC (iec61883_dv_fb_get_callback_data (dv_fb));
412 #else /* HAVE_LIBIEC61883 */
413 static GstDV1394Src *
414 gst_dv1394src_from_raw1394handle (raw1394handle_t handle)
416 return GST_DV1394SRC (raw1394_get_userdata (handle));
418 #endif /* HAVE_LIBIEC61883 */
420 #ifdef HAVE_LIBIEC61883
422 gst_dv1394src_iec61883_receive (unsigned char *data, int len,
423 int complete, void *cbdata)
425 GstDV1394Src *dv1394src = GST_DV1394SRC (cbdata);
427 if (G_UNLIKELY (!gst_pad_has_current_caps (GST_BASE_SRC_PAD (dv1394src)))) {
429 unsigned char *p = data;
431 // figure format (NTSC/PAL)
434 dv1394src->frame_size = PAL_FRAMESIZE;
435 dv1394src->frame_rate = PAL_FRAMERATE;
436 GST_DEBUG ("PAL data");
437 caps = gst_caps_new_simple ("video/x-dv",
438 "format", G_TYPE_STRING, "PAL",
439 "systemstream", G_TYPE_BOOLEAN, TRUE, NULL);
442 dv1394src->frame_size = NTSC_FRAMESIZE;
443 dv1394src->frame_rate = NTSC_FRAMERATE;
445 ("NTSC data [untested] - please report success/failure to <dan@f3c.com>");
446 caps = gst_caps_new_simple ("video/x-dv",
447 "format", G_TYPE_STRING, "NTSC",
448 "systemstream", G_TYPE_BOOLEAN, TRUE, NULL);
450 gst_pad_set_caps (GST_BASE_SRC_PAD (dv1394src), caps);
451 gst_caps_unref (caps);
454 dv1394src->frame = NULL;
455 if (G_LIKELY ((dv1394src->frame_sequence + 1) % (dv1394src->skip +
456 dv1394src->consecutive) < dv1394src->consecutive)) {
457 if (complete && len == dv1394src->frame_size) {
460 buf = gst_buffer_new_and_alloc (dv1394src->frame_size);
462 GST_BUFFER_OFFSET (buf) = dv1394src->frame_sequence;
463 gst_buffer_fill (buf, 0, data, len);
464 dv1394src->buf = buf;
467 dv1394src->frame_sequence++;
473 gst_dv1394src_iso_receive (raw1394handle_t handle, int channel, size_t len,
476 GstDV1394Src *dv1394src = gst_dv1394src_from_raw1394handle (handle);
480 the following code taken from kino-0.51 (Dan Dennedy/Charles Yates)
481 Kindly relicensed under the LGPL. See the commit log for version 1.6 of
484 unsigned char *p = (unsigned char *) &data[3];
486 int section_type = p[0] >> 5; /* section type is in bits 5 - 7 */
487 int dif_sequence = p[1] >> 4; /* dif sequence number is in bits 4 - 7 */
488 int dif_block = p[2];
490 /* if we are at the beginning of a frame,
491 we set buf=frame, and alloc a new buffer for frame
493 if (section_type == 0 && dif_sequence == 0) { // dif header
494 if (!GST_PAD_CAPS (GST_BASE_SRC_PAD (dv1394src))) {
497 // figure format (NTSC/PAL)
500 dv1394src->frame_size = PAL_FRAMESIZE;
501 dv1394src->frame_rate = PAL_FRAMERATE;
502 GST_DEBUG ("PAL data");
503 caps = gst_caps_new_simple ("video/x-dv",
504 "format", G_TYPE_STRING, "PAL",
505 "systemstream", G_TYPE_BOOLEAN, TRUE, NULL);
508 dv1394src->frame_size = NTSC_FRAMESIZE;
509 dv1394src->frame_rate = NTSC_FRAMERATE;
511 ("NTSC data [untested] - please report success/failure to <dan@f3c.com>");
512 caps = gst_caps_new_simple ("video/x-dv",
513 "format", G_TYPE_STRING, "NTSC",
514 "systemstream", G_TYPE_BOOLEAN, TRUE, NULL);
516 gst_pad_set_caps (GST_BASE_SRC_PAD (dv1394src), caps);
517 gst_caps_unref (caps);
519 // drop last frame when not complete
520 if (!dv1394src->drop_incomplete
521 || dv1394src->bytes_in_frame == dv1394src->frame_size) {
522 dv1394src->buf = dv1394src->frame;
524 GST_INFO_OBJECT (GST_ELEMENT (dv1394src), "incomplete frame dropped");
525 g_signal_emit (G_OBJECT (dv1394src),
526 gst_dv1394src_signals[SIGNAL_FRAME_DROPPED], 0);
527 if (dv1394src->frame) {
528 gst_buffer_unref (dv1394src->frame);
531 if ((dv1394src->frame_sequence + 1) % (dv1394src->skip +
532 dv1394src->consecutive) < dv1394src->consecutive) {
536 buf = gst_buffer_new_and_alloc (dv1394src->frame_size);
538 /* fill in offset, duration, timestamp */
539 GST_BUFFER_OFFSET (buf) = dv1394src->frame_sequence;
540 dv1394src->frame = buf;
542 dv1394src->frame_sequence++;
543 dv1394src->bytes_in_frame = 0;
546 if (dv1394src->frame != NULL) {
547 guint8 *data = GST_BUFFER_DATA (dv1394src->frame);
549 switch (section_type) {
550 case 0: /* 1 Header block */
551 /* p[3] |= 0x80; // hack to force PAL data */
552 memcpy (data + dif_sequence * 150 * 80, p, 480);
555 case 1: /* 2 Subcode blocks */
556 memcpy (data + dif_sequence * 150 * 80 + (1 + dif_block) * 80, p,
560 case 2: /* 3 VAUX blocks */
561 memcpy (data + dif_sequence * 150 * 80 + (3 + dif_block) * 80, p,
565 case 3: /* 9 Audio blocks interleaved with video */
566 memcpy (data + dif_sequence * 150 * 80 + (6 + dif_block * 16) * 80, p,
570 case 4: /* 135 Video blocks interleaved with audio */
571 memcpy (data + dif_sequence * 150 * 80 + (7 + (dif_block / 15) +
572 dif_block) * 80, p, 480);
575 default: /* we can't handle any other data */
578 dv1394src->bytes_in_frame += 480;
586 * When an ieee1394 bus reset happens, usually a device has been removed
587 * or added. We send a message on the message bus with the node count
588 * and whether the capture device used in this element connected, disconnected
591 * nodecount - integer with number of nodes on bus
592 * current-device-change - integer (1 if device connected, 0 if no change to
593 * current device status, -1 if device disconnected)
596 gst_dv1394src_bus_reset (raw1394handle_t handle, unsigned int generation)
601 GstStructure *structure;
602 gint current_device_change;
605 src = gst_dv1394src_from_raw1394handle (handle);
607 GST_INFO_OBJECT (src, "have bus reset");
609 /* update generation - told to do so by docs */
610 raw1394_update_generation (handle, generation);
611 nodecount = raw1394_get_nodecount (handle);
612 /* allocate memory for portinfo */
614 /* current_device_change is -1 if camera disconnected, 0 if other device
615 * connected or 1 if camera has now connected */
616 current_device_change = -1;
617 for (i = 0; i < nodecount; i++) {
618 if (src->guid == rom1394_get_guid (handle, i)) {
619 /* Camera is with us */
620 GST_DEBUG ("Camera is with us");
621 if (!src->connected) {
622 current_device_change = 1;
623 src->connected = TRUE;
625 current_device_change = 0;
628 if (src->connected && current_device_change == -1) {
629 GST_DEBUG ("Camera has disconnected");
630 src->connected = FALSE;
631 } else if (!src->connected && current_device_change == -1) {
632 GST_DEBUG ("Camera is still not with us");
633 current_device_change = 0;
636 structure = gst_structure_new ("ieee1394-bus-reset", "nodecount", G_TYPE_INT,
637 nodecount, "current-device-change", G_TYPE_INT, current_device_change,
639 message = gst_message_new_element (GST_OBJECT (src), structure);
640 gst_element_post_message (GST_ELEMENT (src), message);
646 gst_dv1394src_create (GstPushSrc * psrc, GstBuffer ** buf)
648 GstDV1394Src *dv1394src = GST_DV1394SRC (psrc);
649 struct pollfd pollfds[2];
651 pollfds[0].fd = raw1394_get_fd (dv1394src->handle);
652 pollfds[0].events = POLLIN | POLLERR | POLLHUP | POLLPRI;
653 pollfds[1].fd = READ_SOCKET (dv1394src);
654 pollfds[1].events = POLLIN | POLLERR | POLLHUP | POLLPRI;
656 if (G_UNLIKELY (dv1394src->buf)) {
657 /* maybe we had an error before, and there's a stale buffer? */
658 gst_buffer_unref (dv1394src->buf);
659 dv1394src->buf = NULL;
663 int res = poll (pollfds, 2, -1);
665 if (G_UNLIKELY (res < 0)) {
666 if (errno == EAGAIN || errno == EINTR)
669 goto error_while_polling;
672 if (G_UNLIKELY (pollfds[1].revents)) {
675 if (pollfds[1].revents & POLLIN)
676 READ_COMMAND (dv1394src, command, res);
679 } else if (G_LIKELY (pollfds[0].revents & POLLIN)) {
680 /* shouldn't block in theory */
681 raw1394_loop_iterate (dv1394src->handle);
688 g_assert (dv1394src->buf);
690 *buf = dv1394src->buf;
691 dv1394src->buf = NULL;
696 GST_ELEMENT_ERROR (dv1394src, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
701 GST_DEBUG_OBJECT (dv1394src, "told to stop, shutting down");
702 return GST_FLOW_FLUSHING;
707 gst_dv1394src_discover_avc_node (GstDV1394Src * src)
711 int m = src->num_ports;
713 if (src->port >= 0) {
714 /* search on explicit port */
719 /* loop over all our ports */
720 for (; j < m && node == -1; j++) {
721 raw1394handle_t handle;
722 struct raw1394_portinfo pinf[16];
725 handle = raw1394_new_handle ();
727 GST_WARNING ("raw1394 - failed to get handle: %s.\n", strerror (errno));
730 if (raw1394_get_port_info (handle, pinf, 16) < 0) {
731 GST_WARNING ("raw1394 - failed to get port info: %s.\n",
736 /* tell raw1394 which host adapter to use */
737 if (raw1394_set_port (handle, j) < 0) {
738 GST_WARNING ("raw1394 - failed to set set port: %s.\n", strerror (errno));
742 /* now loop over all the nodes */
743 for (i = 0; i < raw1394_get_nodecount (handle); i++) {
744 /* are we looking for an explicit GUID ? */
745 if (src->guid != 0) {
746 if (src->guid == rom1394_get_guid (handle, i)) {
750 src->uri = g_strdup_printf ("dv://%d", src->port);
754 rom1394_directory rom_dir;
756 /* select first AV/C Tape Recorder Player node */
757 if (rom1394_get_directory (handle, i, &rom_dir) < 0) {
758 GST_WARNING ("error reading config rom directory for node %d\n", i);
761 if ((rom1394_get_node_type (&rom_dir) == ROM1394_NODE_TYPE_AVC) &&
762 avc1394_check_subunit_type (handle, i, AVC1394_SUBUNIT_TYPE_VCR)) {
765 src->guid = rom1394_get_guid (handle, i);
767 src->uri = g_strdup_printf ("dv://%d", src->port);
768 g_free (src->device_name);
769 src->device_name = g_strdup (rom_dir.label);
772 rom1394_free_directory (&rom_dir);
776 raw1394_destroy_handle (handle);
782 gst_dv1394src_start (GstBaseSrc * bsrc)
784 GstDV1394Src *src = GST_DV1394SRC (bsrc);
787 src->connected = FALSE;
789 if (socketpair (PF_UNIX, SOCK_STREAM, 0, control_sock) < 0)
792 READ_SOCKET (src) = control_sock[0];
793 WRITE_SOCKET (src) = control_sock[1];
795 fcntl (READ_SOCKET (src), F_SETFL, O_NONBLOCK);
796 fcntl (WRITE_SOCKET (src), F_SETFL, O_NONBLOCK);
798 src->handle = raw1394_new_handle ();
802 goto permission_denied;
803 else if (errno == ENOENT)
809 src->num_ports = raw1394_get_port_info (src->handle, src->pinfo, 16);
811 if (src->num_ports == 0)
814 if (src->use_avc || src->port == -1)
815 src->avc_node = gst_dv1394src_discover_avc_node (src);
817 /* lets destroy handle and create one on port
818 this is more reliable than setting port on
819 the existing handle */
820 raw1394_destroy_handle (src->handle);
821 src->handle = raw1394_new_handle_on_port (src->port);
823 goto cannot_set_port;
825 raw1394_set_userdata (src->handle, src);
826 raw1394_set_bus_reset_handler (src->handle, gst_dv1394src_bus_reset);
828 #ifdef HAVE_LIBIEC61883
829 if ((src->iec61883dv =
830 iec61883_dv_fb_init (src->handle,
831 gst_dv1394src_iec61883_receive, src)) == NULL)
832 goto cannot_initialise_dv;
835 raw1394_set_iso_handler (src->handle, src->channel,
836 gst_dv1394src_iso_receive);
839 GST_DEBUG_OBJECT (src, "successfully opened up 1394 connection");
840 src->connected = TRUE;
842 #ifdef HAVE_LIBIEC61883
843 if (iec61883_dv_fb_start (src->iec61883dv, src->channel) != 0)
846 if (raw1394_start_iso_rcv (src->handle, src->channel) < 0)
851 raw1394handle_t avc_handle = raw1394_new_handle_on_port (src->port);
855 if (!avc1394_vcr_is_recording (avc_handle, src->avc_node)
856 && avc1394_vcr_is_playing (avc_handle, src->avc_node)
857 != AVC1394_VCR_OPERAND_PLAY_FORWARD)
858 avc1394_vcr_play (avc_handle, src->avc_node);
859 raw1394_destroy_handle (avc_handle);
861 GST_WARNING_OBJECT (src, "Starting VCR via avc1394 failed: %s",
866 gst_1394_clock_set_handle (src->provided_clock, src->handle);
872 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
878 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL), GST_ERROR_SYSTEM);
883 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL), GST_ERROR_SYSTEM);
888 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
889 ("can't get raw1394 handle (%s)", g_strerror (errno)));
894 raw1394_destroy_handle (src->handle);
896 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
897 ("no ports available for raw1394"));
902 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
903 ("can't set 1394 port %d", src->port));
908 raw1394_destroy_handle (src->handle);
910 #ifdef HAVE_LIBIEC61883
911 iec61883_dv_fb_close (src->iec61883dv);
912 src->iec61883dv = NULL;
914 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
915 ("can't start 1394 iso receive"));
918 #ifdef HAVE_LIBIEC61883
919 cannot_initialise_dv:
921 raw1394_destroy_handle (src->handle);
923 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
924 ("can't initialise iec61883 dv"));
931 gst_dv1394src_stop (GstBaseSrc * bsrc)
933 GstDV1394Src *src = GST_DV1394SRC (bsrc);
935 close (READ_SOCKET (src));
936 close (WRITE_SOCKET (src));
937 READ_SOCKET (src) = -1;
938 WRITE_SOCKET (src) = -1;
939 #ifdef HAVE_LIBIEC61883
940 iec61883_dv_fb_close (src->iec61883dv);
942 raw1394_stop_iso_rcv (src->handle, src->channel);
946 raw1394handle_t avc_handle = raw1394_new_handle_on_port (src->port);
948 /* pause and stop the VCR */
950 if (!avc1394_vcr_is_recording (avc_handle, src->avc_node)
951 && (avc1394_vcr_is_playing (avc_handle, src->avc_node)
952 != AVC1394_VCR_OPERAND_PLAY_FORWARD_PAUSE))
953 avc1394_vcr_pause (avc_handle, src->avc_node);
954 avc1394_vcr_stop (avc_handle, src->avc_node);
955 raw1394_destroy_handle (avc_handle);
957 GST_WARNING_OBJECT (src, "Starting VCR via avc1394 failed: %s",
962 gst_1394_clock_unset_handle (src->provided_clock);
964 raw1394_destroy_handle (src->handle);
970 gst_dv1394src_unlock (GstBaseSrc * bsrc)
972 GstDV1394Src *src = GST_DV1394SRC (bsrc);
974 SEND_COMMAND (src, CONTROL_STOP);
980 gst_dv1394src_query (GstBaseSrc * basesrc, GstQuery * query)
982 switch (GST_QUERY_TYPE (query)) {
983 case GST_QUERY_LATENCY:
985 gst_query_set_latency (query, TRUE, GST_SECOND / 25, GST_CLOCK_TIME_NONE);
995 return GST_BASE_SRC_CLASS (parent_class)->query (basesrc, query);
999 gst_dv1394src_update_device_name (GstDV1394Src * src)
1001 raw1394handle_t handle;
1002 gint portcount, port, nodecount, node;
1003 rom1394_directory directory;
1005 g_free (src->device_name);
1006 src->device_name = NULL;
1008 GST_LOG_OBJECT (src, "updating device name for current GUID");
1010 handle = raw1394_new_handle ();
1013 goto gethandle_failed;
1015 portcount = raw1394_get_port_info (handle, NULL, 0);
1016 for (port = 0; port < portcount; port++) {
1017 if (raw1394_set_port (handle, port) >= 0) {
1018 nodecount = raw1394_get_nodecount (handle);
1019 for (node = 0; node < nodecount; node++) {
1020 if (src->guid == rom1394_get_guid (handle, node)) {
1021 if (rom1394_get_directory (handle, node, &directory) >= 0) {
1022 g_free (src->device_name);
1023 src->device_name = g_strdup (directory.label);
1024 rom1394_free_directory (&directory);
1027 GST_WARNING ("error reading rom directory for node %d", node);
1034 src->device_name = g_strdup ("Unknown"); /* FIXME: translate? */
1038 raw1394_destroy_handle (handle);
1044 GST_WARNING ("failed to get raw1394 handle: %s", g_strerror (errno));
1045 src->device_name = g_strdup ("Unknown"); /* FIXME: translate? */
1050 /*** GSTURIHANDLER INTERFACE *************************************************/
1053 gst_dv1394src_uri_get_type (GType type)
1058 static const gchar *const *
1059 gst_dv1394src_uri_get_protocols (GType type)
1061 static const gchar *protocols[] = { (char *) "dv", NULL };
1067 gst_dv1394src_uri_get_uri (GstURIHandler * handler)
1069 GstDV1394Src *gst_dv1394src = GST_DV1394SRC (handler);
1071 return gst_dv1394src->uri;
1075 gst_dv1394src_uri_set_uri (GstURIHandler * handler, const gchar * uri,
1078 gchar *protocol, *location;
1079 gboolean ret = TRUE;
1080 GstDV1394Src *gst_dv1394src = GST_DV1394SRC (handler);
1082 protocol = gst_uri_get_protocol (uri);
1083 if (strcmp (protocol, "dv") != 0) {
1085 g_set_error (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI, "Invalid DV URI");
1090 location = gst_uri_get_location (uri);
1091 if (location && *location != '\0')
1092 gst_dv1394src->port = strtol (location, NULL, 10);
1094 gst_dv1394src->port = DEFAULT_PORT;
1096 g_free (gst_dv1394src->uri);
1097 gst_dv1394src->uri = g_strdup_printf ("dv://%d", gst_dv1394src->port);
1103 gst_dv1394src_uri_handler_init (gpointer g_iface, gpointer iface_data)
1105 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
1107 iface->get_type = gst_dv1394src_uri_get_type;
1108 iface->get_protocols = gst_dv1394src_uri_get_protocols;
1109 iface->get_uri = gst_dv1394src_uri_get_uri;
1110 iface->set_uri = gst_dv1394src_uri_set_uri;