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 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);
146 _do_init (GType type)
148 static const GInterfaceInfo urihandler_info = {
149 gst_dv1394src_uri_handler_init,
153 g_type_add_interface_static (type, GST_TYPE_URI_HANDLER, &urihandler_info);
155 gst_1394_type_add_property_probe_interface (type);
157 GST_DEBUG_CATEGORY_INIT (dv1394src_debug, "dv1394src", 0,
158 "DV firewire source");
161 GST_BOILERPLATE_FULL (GstDV1394Src, gst_dv1394src, GstPushSrc,
162 GST_TYPE_PUSH_SRC, _do_init);
165 static guint gst_dv1394src_signals[LAST_SIGNAL] = { 0 };
169 gst_dv1394src_base_init (gpointer g_class)
171 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
173 gst_element_class_add_pad_template (element_class,
174 gst_static_pad_template_get (&src_factory));
176 gst_element_class_set_details_simple (element_class,
177 "Firewire (1394) DV video source", "Source/Video",
178 "Source for DV video data from firewire port",
179 "Erik Walthinsen <omega@temple-baptist.com>, "
180 "Daniel Fischer <dan@f3c.com>, " "Wim Taymans <wim@fluendo.com>, "
181 "Zaheer Abbas Merali <zaheerabbas at merali dot org>");
185 gst_dv1394src_class_init (GstDV1394SrcClass * klass)
187 GObjectClass *gobject_class;
188 GstElementClass *gstelement_class;
189 GstBaseSrcClass *gstbasesrc_class;
190 GstPushSrcClass *gstpushsrc_class;
192 gobject_class = (GObjectClass *) klass;
193 gstelement_class = (GstElementClass *) klass;
194 gstbasesrc_class = (GstBaseSrcClass *) klass;
195 gstpushsrc_class = (GstPushSrcClass *) klass;
197 gobject_class->set_property = gst_dv1394src_set_property;
198 gobject_class->get_property = gst_dv1394src_get_property;
199 gobject_class->dispose = gst_dv1394src_dispose;
201 gstelement_class->provide_clock = gst_dv1394src_provide_clock;
202 gstelement_class->change_state = gst_dv1394_src_change_state;
204 gst_dv1394src_signals[SIGNAL_FRAME_DROPPED] =
205 g_signal_new ("frame-dropped", G_TYPE_FROM_CLASS (klass),
206 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDV1394SrcClass, frame_dropped),
207 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
209 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_PORT,
210 g_param_spec_int ("port", "Port", "Port number (-1 automatic)",
211 -1, 16, DEFAULT_PORT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
212 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_CHANNEL,
213 g_param_spec_int ("channel", "Channel", "Channel number for listening",
214 0, 64, DEFAULT_CHANNEL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
215 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_CONSECUTIVE,
216 g_param_spec_int ("consecutive", "consecutive frames",
217 "send n consecutive frames after skipping", 1, G_MAXINT,
218 DEFAULT_CONSECUTIVE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
219 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SKIP,
220 g_param_spec_int ("skip", "skip frames", "skip n frames",
221 0, G_MAXINT, DEFAULT_SKIP,
222 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
223 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_DROP_INCOMPLETE,
224 g_param_spec_boolean ("drop-incomplete", "drop incomplete",
225 "drop incomplete frames", DEFAULT_DROP_INCOMPLETE,
226 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
227 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_USE_AVC,
228 g_param_spec_boolean ("use-avc", "Use AV/C", "Use AV/C VTR control",
229 DEFAULT_USE_AVC, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
230 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_GUID,
231 g_param_spec_uint64 ("guid", "GUID",
232 "select one of multiple DV devices by its GUID. use a hexadecimal "
233 "like 0xhhhhhhhhhhhhhhhh. (0 = no guid)", 0, G_MAXUINT64,
234 DEFAULT_GUID, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
236 * GstDV1394Src:device-name
238 * Descriptive name of the currently opened device
242 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_DEVICE_NAME,
243 g_param_spec_string ("device-name", "device name",
244 "user-friendly name of the device", "Default",
245 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
247 gstbasesrc_class->negotiate = NULL;
248 gstbasesrc_class->start = gst_dv1394src_start;
249 gstbasesrc_class->stop = gst_dv1394src_stop;
250 gstbasesrc_class->unlock = gst_dv1394src_unlock;
251 gstbasesrc_class->query = gst_dv1394src_query;
253 gstpushsrc_class->create = gst_dv1394src_create;
257 gst_dv1394src_init (GstDV1394Src * dv1394src, GstDV1394SrcClass * klass)
259 GstPad *srcpad = GST_BASE_SRC_PAD (dv1394src);
261 gst_base_src_set_live (GST_BASE_SRC (dv1394src), TRUE);
262 gst_base_src_set_format (GST_BASE_SRC (dv1394src), GST_FORMAT_TIME);
263 gst_base_src_set_do_timestamp (GST_BASE_SRC (dv1394src), TRUE);
264 gst_pad_use_fixed_caps (srcpad);
266 dv1394src->port = DEFAULT_PORT;
267 dv1394src->channel = DEFAULT_CHANNEL;
269 dv1394src->consecutive = DEFAULT_CONSECUTIVE;
270 dv1394src->skip = DEFAULT_SKIP;
271 dv1394src->drop_incomplete = DEFAULT_DROP_INCOMPLETE;
272 dv1394src->use_avc = DEFAULT_USE_AVC;
273 dv1394src->guid = DEFAULT_GUID;
274 dv1394src->uri = g_strdup_printf ("dv://%d", dv1394src->port);
275 dv1394src->device_name = g_strdup_printf ("Default");
277 READ_SOCKET (dv1394src) = -1;
278 WRITE_SOCKET (dv1394src) = -1;
280 /* initialized when first header received */
281 dv1394src->frame_size = 0;
283 dv1394src->buf = NULL;
284 dv1394src->frame = NULL;
285 dv1394src->frame_sequence = 0;
287 dv1394src->provided_clock = gst_1394_clock_new ("dv1394clock");
291 gst_dv1394src_dispose (GObject * object)
293 GstDV1394Src *src = GST_DV1394SRC (object);
295 if (src->provided_clock) {
296 g_object_unref (src->provided_clock);
302 g_free (src->device_name);
303 src->device_name = NULL;
305 G_OBJECT_CLASS (parent_class)->dispose (object);
309 gst_dv1394src_set_property (GObject * object, guint prop_id,
310 const GValue * value, GParamSpec * pspec)
312 GstDV1394Src *filter = GST_DV1394SRC (object);
316 filter->port = g_value_get_int (value);
317 g_free (filter->uri);
318 filter->uri = g_strdup_printf ("dv://%d", filter->port);
321 filter->channel = g_value_get_int (value);
324 filter->skip = g_value_get_int (value);
326 case PROP_CONSECUTIVE:
327 filter->consecutive = g_value_get_int (value);
329 case PROP_DROP_INCOMPLETE:
330 filter->drop_incomplete = g_value_get_boolean (value);
333 filter->use_avc = g_value_get_boolean (value);
336 filter->guid = g_value_get_uint64 (value);
337 gst_dv1394src_update_device_name (filter);
345 gst_dv1394src_get_property (GObject * object, guint prop_id, GValue * value,
348 GstDV1394Src *filter = GST_DV1394SRC (object);
352 g_value_set_int (value, filter->port);
355 g_value_set_int (value, filter->channel);
358 g_value_set_int (value, filter->skip);
360 case PROP_CONSECUTIVE:
361 g_value_set_int (value, filter->consecutive);
363 case PROP_DROP_INCOMPLETE:
364 g_value_set_boolean (value, filter->drop_incomplete);
367 g_value_set_boolean (value, filter->use_avc);
370 g_value_set_uint64 (value, filter->guid);
372 case PROP_DEVICE_NAME:
373 g_value_set_string (value, filter->device_name);
376 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
382 gst_dv1394src_provide_clock (GstElement * element)
384 GstDV1394Src *dv1394src = GST_DV1394SRC (element);
386 return GST_CLOCK_CAST (gst_object_ref (dv1394src->provided_clock));
389 static GstStateChangeReturn
390 gst_dv1394_src_change_state (GstElement * element, GstStateChange transition)
392 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
393 GstDV1394Src *src = GST_DV1394SRC (element);
395 switch (transition) {
396 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
397 gst_element_post_message (element,
398 gst_message_new_clock_lost (GST_OBJECT_CAST (element),
399 GST_CLOCK_CAST (src->provided_clock)));
405 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
406 if (ret == GST_STATE_CHANGE_FAILURE)
409 switch (transition) {
410 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
411 gst_element_post_message (element,
412 gst_message_new_clock_provide (GST_OBJECT_CAST (element),
413 GST_CLOCK_CAST (src->provided_clock), TRUE));
422 #ifdef HAVE_LIBIEC61883
423 static GstDV1394Src *
424 gst_dv1394src_from_raw1394handle (raw1394handle_t handle)
426 iec61883_dv_t dv = (iec61883_dv_t) raw1394_get_userdata (handle);
427 iec61883_dv_fb_t dv_fb =
428 (iec61883_dv_fb_t) iec61883_dv_get_callback_data (dv);
429 return GST_DV1394SRC (iec61883_dv_fb_get_callback_data (dv_fb));
431 #else /* HAVE_LIBIEC61883 */
432 static GstDV1394Src *
433 gst_dv1394src_from_raw1394handle (raw1394handle_t handle)
435 return GST_DV1394SRC (raw1394_get_userdata (handle));
437 #endif /* HAVE_LIBIEC61883 */
439 #ifdef HAVE_LIBIEC61883
441 gst_dv1394src_iec61883_receive (unsigned char *data, int len,
442 int complete, void *cbdata)
444 GstDV1394Src *dv1394src = GST_DV1394SRC (cbdata);
446 if (G_UNLIKELY (!GST_PAD_CAPS (GST_BASE_SRC_PAD (dv1394src)))) {
448 unsigned char *p = data;
450 // figure format (NTSC/PAL)
453 dv1394src->frame_size = PAL_FRAMESIZE;
454 dv1394src->frame_rate = PAL_FRAMERATE;
455 GST_DEBUG ("PAL data");
456 caps = gst_caps_new_simple ("video/x-dv",
457 "format", G_TYPE_STRING, "PAL",
458 "systemstream", G_TYPE_BOOLEAN, TRUE, NULL);
461 dv1394src->frame_size = NTSC_FRAMESIZE;
462 dv1394src->frame_rate = NTSC_FRAMERATE;
464 ("NTSC data [untested] - please report success/failure to <dan@f3c.com>");
465 caps = gst_caps_new_simple ("video/x-dv",
466 "format", G_TYPE_STRING, "NTSC",
467 "systemstream", G_TYPE_BOOLEAN, TRUE, NULL);
469 gst_pad_set_caps (GST_BASE_SRC_PAD (dv1394src), caps);
470 gst_caps_unref (caps);
473 dv1394src->frame = NULL;
474 if (G_LIKELY ((dv1394src->frame_sequence + 1) % (dv1394src->skip +
475 dv1394src->consecutive) < dv1394src->consecutive)) {
476 if (complete && len == dv1394src->frame_size) {
480 buf = gst_buffer_new_and_alloc (dv1394src->frame_size);
482 GST_BUFFER_OFFSET (buf) = dv1394src->frame_sequence;
483 bufdata = GST_BUFFER_DATA (buf);
484 memcpy (bufdata, data, len);
485 dv1394src->buf = buf;
488 dv1394src->frame_sequence++;
494 gst_dv1394src_iso_receive (raw1394handle_t handle, int channel, size_t len,
497 GstDV1394Src *dv1394src = gst_dv1394src_from_raw1394handle (handle);
501 the following code taken from kino-0.51 (Dan Dennedy/Charles Yates)
502 Kindly relicensed under the LGPL. See the commit log for version 1.6 of
505 unsigned char *p = (unsigned char *) &data[3];
507 int section_type = p[0] >> 5; /* section type is in bits 5 - 7 */
508 int dif_sequence = p[1] >> 4; /* dif sequence number is in bits 4 - 7 */
509 int dif_block = p[2];
511 /* if we are at the beginning of a frame,
512 we set buf=frame, and alloc a new buffer for frame
514 if (section_type == 0 && dif_sequence == 0) { // dif header
515 if (!GST_PAD_CAPS (GST_BASE_SRC_PAD (dv1394src))) {
518 // figure format (NTSC/PAL)
521 dv1394src->frame_size = PAL_FRAMESIZE;
522 dv1394src->frame_rate = PAL_FRAMERATE;
523 GST_DEBUG ("PAL data");
524 caps = gst_caps_new_simple ("video/x-dv",
525 "format", G_TYPE_STRING, "PAL",
526 "systemstream", G_TYPE_BOOLEAN, TRUE, NULL);
529 dv1394src->frame_size = NTSC_FRAMESIZE;
530 dv1394src->frame_rate = NTSC_FRAMERATE;
532 ("NTSC data [untested] - please report success/failure to <dan@f3c.com>");
533 caps = gst_caps_new_simple ("video/x-dv",
534 "format", G_TYPE_STRING, "NTSC",
535 "systemstream", G_TYPE_BOOLEAN, TRUE, NULL);
537 gst_pad_set_caps (GST_BASE_SRC_PAD (dv1394src), caps);
538 gst_caps_unref (caps);
540 // drop last frame when not complete
541 if (!dv1394src->drop_incomplete
542 || dv1394src->bytes_in_frame == dv1394src->frame_size) {
543 dv1394src->buf = dv1394src->frame;
545 GST_INFO_OBJECT (GST_ELEMENT (dv1394src), "incomplete frame dropped");
546 g_signal_emit (G_OBJECT (dv1394src),
547 gst_dv1394src_signals[SIGNAL_FRAME_DROPPED], 0);
548 if (dv1394src->frame) {
549 gst_buffer_unref (dv1394src->frame);
552 if ((dv1394src->frame_sequence + 1) % (dv1394src->skip +
553 dv1394src->consecutive) < dv1394src->consecutive) {
557 buf = gst_buffer_new_and_alloc (dv1394src->frame_size);
559 /* fill in offset, duration, timestamp */
560 GST_BUFFER_OFFSET (buf) = dv1394src->frame_sequence;
561 dv1394src->frame = buf;
563 dv1394src->frame_sequence++;
564 dv1394src->bytes_in_frame = 0;
567 if (dv1394src->frame != NULL) {
568 guint8 *data = GST_BUFFER_DATA (dv1394src->frame);
570 switch (section_type) {
571 case 0: /* 1 Header block */
572 /* p[3] |= 0x80; // hack to force PAL data */
573 memcpy (data + dif_sequence * 150 * 80, p, 480);
576 case 1: /* 2 Subcode blocks */
577 memcpy (data + dif_sequence * 150 * 80 + (1 + dif_block) * 80, p,
581 case 2: /* 3 VAUX blocks */
582 memcpy (data + dif_sequence * 150 * 80 + (3 + dif_block) * 80, p,
586 case 3: /* 9 Audio blocks interleaved with video */
587 memcpy (data + dif_sequence * 150 * 80 + (6 + dif_block * 16) * 80, p,
591 case 4: /* 135 Video blocks interleaved with audio */
592 memcpy (data + dif_sequence * 150 * 80 + (7 + (dif_block / 15) +
593 dif_block) * 80, p, 480);
596 default: /* we can't handle any other data */
599 dv1394src->bytes_in_frame += 480;
607 * When an ieee1394 bus reset happens, usually a device has been removed
608 * or added. We send a message on the message bus with the node count
609 * and whether the capture device used in this element connected, disconnected
612 * nodecount - integer with number of nodes on bus
613 * current-device-change - integer (1 if device connected, 0 if no change to
614 * current device status, -1 if device disconnected)
617 gst_dv1394src_bus_reset (raw1394handle_t handle, unsigned int generation)
622 GstStructure *structure;
623 gint current_device_change;
626 src = gst_dv1394src_from_raw1394handle (handle);
628 GST_INFO_OBJECT (src, "have bus reset");
630 /* update generation - told to do so by docs */
631 raw1394_update_generation (handle, generation);
632 nodecount = raw1394_get_nodecount (handle);
633 /* allocate memory for portinfo */
635 /* current_device_change is -1 if camera disconnected, 0 if other device
636 * connected or 1 if camera has now connected */
637 current_device_change = -1;
638 for (i = 0; i < nodecount; i++) {
639 if (src->guid == rom1394_get_guid (handle, i)) {
640 /* Camera is with us */
641 GST_DEBUG ("Camera is with us");
642 if (!src->connected) {
643 current_device_change = 1;
644 src->connected = TRUE;
646 current_device_change = 0;
649 if (src->connected && current_device_change == -1) {
650 GST_DEBUG ("Camera has disconnected");
651 src->connected = FALSE;
652 } else if (!src->connected && current_device_change == -1) {
653 GST_DEBUG ("Camera is still not with us");
654 current_device_change = 0;
657 structure = gst_structure_new ("ieee1394-bus-reset", "nodecount", G_TYPE_INT,
658 nodecount, "current-device-change", G_TYPE_INT, current_device_change,
660 message = gst_message_new_element (GST_OBJECT (src), structure);
661 gst_element_post_message (GST_ELEMENT (src), message);
667 gst_dv1394src_create (GstPushSrc * psrc, GstBuffer ** buf)
669 GstDV1394Src *dv1394src = GST_DV1394SRC (psrc);
671 struct pollfd pollfds[2];
673 pollfds[0].fd = raw1394_get_fd (dv1394src->handle);
674 pollfds[0].events = POLLIN | POLLERR | POLLHUP | POLLPRI;
675 pollfds[1].fd = READ_SOCKET (dv1394src);
676 pollfds[1].events = POLLIN | POLLERR | POLLHUP | POLLPRI;
678 if (G_UNLIKELY (dv1394src->buf)) {
679 /* maybe we had an error before, and there's a stale buffer? */
680 gst_buffer_unref (dv1394src->buf);
681 dv1394src->buf = NULL;
685 int res = poll (pollfds, 2, -1);
687 if (G_UNLIKELY (res < 0)) {
688 if (errno == EAGAIN || errno == EINTR)
691 goto error_while_polling;
694 if (G_UNLIKELY (pollfds[1].revents)) {
697 if (pollfds[1].revents & POLLIN)
698 READ_COMMAND (dv1394src, command, res);
701 } else if (G_LIKELY (pollfds[0].revents & POLLIN)) {
702 /* shouldn't block in theory */
703 raw1394_loop_iterate (dv1394src->handle);
710 g_assert (dv1394src->buf);
712 caps = gst_pad_get_caps (GST_BASE_SRC_PAD (psrc));
713 gst_buffer_set_caps (dv1394src->buf, caps);
714 gst_caps_unref (caps);
716 *buf = dv1394src->buf;
717 dv1394src->buf = NULL;
722 GST_ELEMENT_ERROR (dv1394src, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
723 return GST_FLOW_UNEXPECTED;
727 GST_DEBUG_OBJECT (dv1394src, "told to stop, shutting down");
728 return GST_FLOW_WRONG_STATE;
733 gst_dv1394src_discover_avc_node (GstDV1394Src * src)
737 int m = src->num_ports;
739 if (src->port >= 0) {
740 /* search on explicit port */
745 /* loop over all our ports */
746 for (; j < m && node == -1; j++) {
747 raw1394handle_t handle;
748 struct raw1394_portinfo pinf[16];
751 handle = raw1394_new_handle ();
753 GST_WARNING ("raw1394 - failed to get handle: %s.\n", strerror (errno));
756 if (raw1394_get_port_info (handle, pinf, 16) < 0) {
757 GST_WARNING ("raw1394 - failed to get port info: %s.\n",
762 /* tell raw1394 which host adapter to use */
763 if (raw1394_set_port (handle, j) < 0) {
764 GST_WARNING ("raw1394 - failed to set set port: %s.\n", strerror (errno));
768 /* now loop over all the nodes */
769 for (i = 0; i < raw1394_get_nodecount (handle); i++) {
770 /* are we looking for an explicit GUID ? */
771 if (src->guid != 0) {
772 if (src->guid == rom1394_get_guid (handle, i)) {
776 src->uri = g_strdup_printf ("dv://%d", src->port);
780 rom1394_directory rom_dir;
782 /* select first AV/C Tape Recorder Player node */
783 if (rom1394_get_directory (handle, i, &rom_dir) < 0) {
784 GST_WARNING ("error reading config rom directory for node %d\n", i);
787 if ((rom1394_get_node_type (&rom_dir) == ROM1394_NODE_TYPE_AVC) &&
788 avc1394_check_subunit_type (handle, i, AVC1394_SUBUNIT_TYPE_VCR)) {
791 src->guid = rom1394_get_guid (handle, i);
793 src->uri = g_strdup_printf ("dv://%d", src->port);
794 g_free (src->device_name);
795 src->device_name = g_strdup (rom_dir.label);
798 rom1394_free_directory (&rom_dir);
802 raw1394_destroy_handle (handle);
808 gst_dv1394src_start (GstBaseSrc * bsrc)
810 GstDV1394Src *src = GST_DV1394SRC (bsrc);
813 src->connected = FALSE;
815 if (socketpair (PF_UNIX, SOCK_STREAM, 0, control_sock) < 0)
818 READ_SOCKET (src) = control_sock[0];
819 WRITE_SOCKET (src) = control_sock[1];
821 fcntl (READ_SOCKET (src), F_SETFL, O_NONBLOCK);
822 fcntl (WRITE_SOCKET (src), F_SETFL, O_NONBLOCK);
824 src->handle = raw1394_new_handle ();
828 goto permission_denied;
829 else if (errno == ENOENT)
835 src->num_ports = raw1394_get_port_info (src->handle, src->pinfo, 16);
837 if (src->num_ports == 0)
840 if (src->use_avc || src->port == -1)
841 src->avc_node = gst_dv1394src_discover_avc_node (src);
843 /* lets destroy handle and create one on port
844 this is more reliable than setting port on
845 the existing handle */
846 raw1394_destroy_handle (src->handle);
847 src->handle = raw1394_new_handle_on_port (src->port);
849 goto cannot_set_port;
851 raw1394_set_userdata (src->handle, src);
852 raw1394_set_bus_reset_handler (src->handle, gst_dv1394src_bus_reset);
854 #ifdef HAVE_LIBIEC61883
855 if ((src->iec61883dv =
856 iec61883_dv_fb_init (src->handle,
857 gst_dv1394src_iec61883_receive, src)) == NULL)
858 goto cannot_initialise_dv;
861 raw1394_set_iso_handler (src->handle, src->channel,
862 gst_dv1394src_iso_receive);
865 GST_DEBUG_OBJECT (src, "successfully opened up 1394 connection");
866 src->connected = TRUE;
868 #ifdef HAVE_LIBIEC61883
869 if (iec61883_dv_fb_start (src->iec61883dv, src->channel) != 0)
872 if (raw1394_start_iso_rcv (src->handle, src->channel) < 0)
877 raw1394handle_t avc_handle = raw1394_new_handle_on_port (src->port);
881 if (!avc1394_vcr_is_recording (avc_handle, src->avc_node)
882 && avc1394_vcr_is_playing (avc_handle, src->avc_node)
883 != AVC1394_VCR_OPERAND_PLAY_FORWARD)
884 avc1394_vcr_play (avc_handle, src->avc_node);
885 raw1394_destroy_handle (avc_handle);
887 GST_WARNING_OBJECT (src, "Starting VCR via avc1394 failed: %s",
892 gst_1394_clock_set_handle (src->provided_clock, src->handle);
898 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
904 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL), GST_ERROR_SYSTEM);
909 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL), GST_ERROR_SYSTEM);
914 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
915 ("can't get raw1394 handle (%s)", g_strerror (errno)));
920 raw1394_destroy_handle (src->handle);
922 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
923 ("no ports available for raw1394"));
928 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
929 ("can't set 1394 port %d", src->port));
934 raw1394_destroy_handle (src->handle);
936 #ifdef HAVE_LIBIEC61883
937 iec61883_dv_fb_close (src->iec61883dv);
938 src->iec61883dv = NULL;
940 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
941 ("can't start 1394 iso receive"));
944 #ifdef HAVE_LIBIEC61883
945 cannot_initialise_dv:
947 raw1394_destroy_handle (src->handle);
949 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
950 ("can't initialise iec61883 dv"));
957 gst_dv1394src_stop (GstBaseSrc * bsrc)
959 GstDV1394Src *src = GST_DV1394SRC (bsrc);
961 close (READ_SOCKET (src));
962 close (WRITE_SOCKET (src));
963 READ_SOCKET (src) = -1;
964 WRITE_SOCKET (src) = -1;
965 #ifdef HAVE_LIBIEC61883
966 iec61883_dv_fb_close (src->iec61883dv);
968 raw1394_stop_iso_rcv (src->handle, src->channel);
972 raw1394handle_t avc_handle = raw1394_new_handle_on_port (src->port);
974 /* pause and stop the VCR */
976 if (!avc1394_vcr_is_recording (avc_handle, src->avc_node)
977 && (avc1394_vcr_is_playing (avc_handle, src->avc_node)
978 != AVC1394_VCR_OPERAND_PLAY_FORWARD_PAUSE))
979 avc1394_vcr_pause (avc_handle, src->avc_node);
980 avc1394_vcr_stop (avc_handle, src->avc_node);
981 raw1394_destroy_handle (avc_handle);
983 GST_WARNING_OBJECT (src, "Starting VCR via avc1394 failed: %s",
988 gst_1394_clock_unset_handle (src->provided_clock);
990 raw1394_destroy_handle (src->handle);
996 gst_dv1394src_unlock (GstBaseSrc * bsrc)
998 GstDV1394Src *src = GST_DV1394SRC (bsrc);
1000 SEND_COMMAND (src, CONTROL_STOP);
1006 gst_dv1394src_query (GstBaseSrc * basesrc, GstQuery * query)
1008 switch (GST_QUERY_TYPE (query)) {
1009 case GST_QUERY_LATENCY:
1011 gst_query_set_latency (query, TRUE, GST_SECOND / 25, GST_CLOCK_TIME_NONE);
1021 return GST_BASE_SRC_CLASS (parent_class)->query (basesrc, query);
1025 gst_dv1394src_update_device_name (GstDV1394Src * src)
1027 raw1394handle_t handle;
1028 gint portcount, port, nodecount, node;
1029 rom1394_directory directory;
1031 g_free (src->device_name);
1032 src->device_name = NULL;
1034 GST_LOG_OBJECT (src, "updating device name for current GUID");
1036 handle = raw1394_new_handle ();
1039 goto gethandle_failed;
1041 portcount = raw1394_get_port_info (handle, NULL, 0);
1042 for (port = 0; port < portcount; port++) {
1043 if (raw1394_set_port (handle, port) >= 0) {
1044 nodecount = raw1394_get_nodecount (handle);
1045 for (node = 0; node < nodecount; node++) {
1046 if (src->guid == rom1394_get_guid (handle, node)) {
1047 if (rom1394_get_directory (handle, node, &directory) >= 0) {
1048 g_free (src->device_name);
1049 src->device_name = g_strdup (directory.label);
1050 rom1394_free_directory (&directory);
1053 GST_WARNING ("error reading rom directory for node %d", node);
1060 src->device_name = g_strdup ("Unknown"); /* FIXME: translate? */
1064 raw1394_destroy_handle (handle);
1070 GST_WARNING ("failed to get raw1394 handle: %s", g_strerror (errno));
1071 src->device_name = g_strdup ("Unknown"); /* FIXME: translate? */
1076 /*** GSTURIHANDLER INTERFACE *************************************************/
1079 gst_dv1394src_uri_get_type (void)
1085 gst_dv1394src_uri_get_protocols (void)
1087 static gchar *protocols[] = { (char *) "dv", NULL };
1092 static const gchar *
1093 gst_dv1394src_uri_get_uri (GstURIHandler * handler)
1095 GstDV1394Src *gst_dv1394src = GST_DV1394SRC (handler);
1097 return gst_dv1394src->uri;
1101 gst_dv1394src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
1103 gchar *protocol, *location;
1104 gboolean ret = TRUE;
1105 GstDV1394Src *gst_dv1394src = GST_DV1394SRC (handler);
1107 protocol = gst_uri_get_protocol (uri);
1108 if (strcmp (protocol, "dv") != 0) {
1114 location = gst_uri_get_location (uri);
1115 if (location && *location != '\0')
1116 gst_dv1394src->port = strtol (location, NULL, 10);
1118 gst_dv1394src->port = DEFAULT_PORT;
1120 g_free (gst_dv1394src->uri);
1121 gst_dv1394src->uri = g_strdup_printf ("dv://%d", gst_dv1394src->port);
1127 gst_dv1394src_uri_handler_init (gpointer g_iface, gpointer iface_data)
1129 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
1131 iface->get_type = gst_dv1394src_uri_get_type;
1132 iface->get_protocols = gst_dv1394src_uri_get_protocols;
1133 iface->get_uri = gst_dv1394src_uri_get_uri;
1134 iface->set_uri = gst_dv1394src_uri_set_uri;