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_static_pad_template (element_class, &src_factory);
175 gst_element_class_set_details_simple (element_class,
176 "Firewire (1394) DV video source", "Source/Video",
177 "Source for DV video data from firewire port",
178 "Erik Walthinsen <omega@temple-baptist.com>, "
179 "Daniel Fischer <dan@f3c.com>, " "Wim Taymans <wim@fluendo.com>, "
180 "Zaheer Abbas Merali <zaheerabbas at merali dot org>");
184 gst_dv1394src_class_init (GstDV1394SrcClass * klass)
186 GObjectClass *gobject_class;
187 GstElementClass *gstelement_class;
188 GstBaseSrcClass *gstbasesrc_class;
189 GstPushSrcClass *gstpushsrc_class;
191 gobject_class = (GObjectClass *) klass;
192 gstelement_class = (GstElementClass *) klass;
193 gstbasesrc_class = (GstBaseSrcClass *) klass;
194 gstpushsrc_class = (GstPushSrcClass *) klass;
196 gobject_class->set_property = gst_dv1394src_set_property;
197 gobject_class->get_property = gst_dv1394src_get_property;
198 gobject_class->dispose = gst_dv1394src_dispose;
200 gstelement_class->provide_clock = gst_dv1394src_provide_clock;
201 gstelement_class->change_state = gst_dv1394_src_change_state;
203 gst_dv1394src_signals[SIGNAL_FRAME_DROPPED] =
204 g_signal_new ("frame-dropped", G_TYPE_FROM_CLASS (klass),
205 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDV1394SrcClass, frame_dropped),
206 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
208 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_PORT,
209 g_param_spec_int ("port", "Port", "Port number (-1 automatic)",
210 -1, 16, DEFAULT_PORT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
211 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_CHANNEL,
212 g_param_spec_int ("channel", "Channel", "Channel number for listening",
213 0, 64, DEFAULT_CHANNEL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
214 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_CONSECUTIVE,
215 g_param_spec_int ("consecutive", "consecutive frames",
216 "send n consecutive frames after skipping", 1, G_MAXINT,
217 DEFAULT_CONSECUTIVE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
218 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SKIP,
219 g_param_spec_int ("skip", "skip frames", "skip n frames",
220 0, G_MAXINT, DEFAULT_SKIP,
221 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
222 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_DROP_INCOMPLETE,
223 g_param_spec_boolean ("drop-incomplete", "drop incomplete",
224 "drop incomplete frames", DEFAULT_DROP_INCOMPLETE,
225 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
226 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_USE_AVC,
227 g_param_spec_boolean ("use-avc", "Use AV/C", "Use AV/C VTR control",
228 DEFAULT_USE_AVC, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
229 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_GUID,
230 g_param_spec_uint64 ("guid", "GUID",
231 "select one of multiple DV devices by its GUID. use a hexadecimal "
232 "like 0xhhhhhhhhhhhhhhhh. (0 = no guid)", 0, G_MAXUINT64,
233 DEFAULT_GUID, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
235 * GstDV1394Src:device-name
237 * Descriptive name of the currently opened device
241 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_DEVICE_NAME,
242 g_param_spec_string ("device-name", "device name",
243 "user-friendly name of the device", "Default",
244 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
246 gstbasesrc_class->negotiate = NULL;
247 gstbasesrc_class->start = gst_dv1394src_start;
248 gstbasesrc_class->stop = gst_dv1394src_stop;
249 gstbasesrc_class->unlock = gst_dv1394src_unlock;
250 gstbasesrc_class->query = gst_dv1394src_query;
252 gstpushsrc_class->create = gst_dv1394src_create;
256 gst_dv1394src_init (GstDV1394Src * dv1394src, GstDV1394SrcClass * klass)
258 GstPad *srcpad = GST_BASE_SRC_PAD (dv1394src);
260 gst_base_src_set_live (GST_BASE_SRC (dv1394src), TRUE);
261 gst_base_src_set_format (GST_BASE_SRC (dv1394src), GST_FORMAT_TIME);
262 gst_base_src_set_do_timestamp (GST_BASE_SRC (dv1394src), TRUE);
263 gst_pad_use_fixed_caps (srcpad);
265 dv1394src->port = DEFAULT_PORT;
266 dv1394src->channel = DEFAULT_CHANNEL;
268 dv1394src->consecutive = DEFAULT_CONSECUTIVE;
269 dv1394src->skip = DEFAULT_SKIP;
270 dv1394src->drop_incomplete = DEFAULT_DROP_INCOMPLETE;
271 dv1394src->use_avc = DEFAULT_USE_AVC;
272 dv1394src->guid = DEFAULT_GUID;
273 dv1394src->uri = g_strdup_printf ("dv://%d", dv1394src->port);
274 dv1394src->device_name = g_strdup_printf ("Default");
276 READ_SOCKET (dv1394src) = -1;
277 WRITE_SOCKET (dv1394src) = -1;
279 /* initialized when first header received */
280 dv1394src->frame_size = 0;
282 dv1394src->buf = NULL;
283 dv1394src->frame = NULL;
284 dv1394src->frame_sequence = 0;
286 dv1394src->provided_clock = gst_1394_clock_new ("dv1394clock");
290 gst_dv1394src_dispose (GObject * object)
292 GstDV1394Src *src = GST_DV1394SRC (object);
294 if (src->provided_clock) {
295 g_object_unref (src->provided_clock);
301 g_free (src->device_name);
302 src->device_name = NULL;
304 G_OBJECT_CLASS (parent_class)->dispose (object);
308 gst_dv1394src_set_property (GObject * object, guint prop_id,
309 const GValue * value, GParamSpec * pspec)
311 GstDV1394Src *filter = GST_DV1394SRC (object);
315 filter->port = g_value_get_int (value);
316 g_free (filter->uri);
317 filter->uri = g_strdup_printf ("dv://%d", filter->port);
320 filter->channel = g_value_get_int (value);
323 filter->skip = g_value_get_int (value);
325 case PROP_CONSECUTIVE:
326 filter->consecutive = g_value_get_int (value);
328 case PROP_DROP_INCOMPLETE:
329 filter->drop_incomplete = g_value_get_boolean (value);
332 filter->use_avc = g_value_get_boolean (value);
335 filter->guid = g_value_get_uint64 (value);
336 gst_dv1394src_update_device_name (filter);
344 gst_dv1394src_get_property (GObject * object, guint prop_id, GValue * value,
347 GstDV1394Src *filter = GST_DV1394SRC (object);
351 g_value_set_int (value, filter->port);
354 g_value_set_int (value, filter->channel);
357 g_value_set_int (value, filter->skip);
359 case PROP_CONSECUTIVE:
360 g_value_set_int (value, filter->consecutive);
362 case PROP_DROP_INCOMPLETE:
363 g_value_set_boolean (value, filter->drop_incomplete);
366 g_value_set_boolean (value, filter->use_avc);
369 g_value_set_uint64 (value, filter->guid);
371 case PROP_DEVICE_NAME:
372 g_value_set_string (value, filter->device_name);
375 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
381 gst_dv1394src_provide_clock (GstElement * element)
383 GstDV1394Src *dv1394src = GST_DV1394SRC (element);
385 return GST_CLOCK_CAST (gst_object_ref (dv1394src->provided_clock));
388 static GstStateChangeReturn
389 gst_dv1394_src_change_state (GstElement * element, GstStateChange transition)
391 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
392 GstDV1394Src *src = GST_DV1394SRC (element);
394 switch (transition) {
395 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
396 gst_element_post_message (element,
397 gst_message_new_clock_lost (GST_OBJECT_CAST (element),
398 GST_CLOCK_CAST (src->provided_clock)));
404 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
405 if (ret == GST_STATE_CHANGE_FAILURE)
408 switch (transition) {
409 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
410 gst_element_post_message (element,
411 gst_message_new_clock_provide (GST_OBJECT_CAST (element),
412 GST_CLOCK_CAST (src->provided_clock), TRUE));
421 #ifdef HAVE_LIBIEC61883
422 static GstDV1394Src *
423 gst_dv1394src_from_raw1394handle (raw1394handle_t handle)
425 iec61883_dv_t dv = (iec61883_dv_t) raw1394_get_userdata (handle);
426 iec61883_dv_fb_t dv_fb =
427 (iec61883_dv_fb_t) iec61883_dv_get_callback_data (dv);
428 return GST_DV1394SRC (iec61883_dv_fb_get_callback_data (dv_fb));
430 #else /* HAVE_LIBIEC61883 */
431 static GstDV1394Src *
432 gst_dv1394src_from_raw1394handle (raw1394handle_t handle)
434 return GST_DV1394SRC (raw1394_get_userdata (handle));
436 #endif /* HAVE_LIBIEC61883 */
438 #ifdef HAVE_LIBIEC61883
440 gst_dv1394src_iec61883_receive (unsigned char *data, int len,
441 int complete, void *cbdata)
443 GstDV1394Src *dv1394src = GST_DV1394SRC (cbdata);
445 if (G_UNLIKELY (!GST_PAD_CAPS (GST_BASE_SRC_PAD (dv1394src)))) {
447 unsigned char *p = data;
449 // figure format (NTSC/PAL)
452 dv1394src->frame_size = PAL_FRAMESIZE;
453 dv1394src->frame_rate = PAL_FRAMERATE;
454 GST_DEBUG ("PAL data");
455 caps = gst_caps_new_simple ("video/x-dv",
456 "format", G_TYPE_STRING, "PAL",
457 "systemstream", G_TYPE_BOOLEAN, TRUE, NULL);
460 dv1394src->frame_size = NTSC_FRAMESIZE;
461 dv1394src->frame_rate = NTSC_FRAMERATE;
463 ("NTSC data [untested] - please report success/failure to <dan@f3c.com>");
464 caps = gst_caps_new_simple ("video/x-dv",
465 "format", G_TYPE_STRING, "NTSC",
466 "systemstream", G_TYPE_BOOLEAN, TRUE, NULL);
468 gst_pad_set_caps (GST_BASE_SRC_PAD (dv1394src), caps);
469 gst_caps_unref (caps);
472 dv1394src->frame = NULL;
473 if (G_LIKELY ((dv1394src->frame_sequence + 1) % (dv1394src->skip +
474 dv1394src->consecutive) < dv1394src->consecutive)) {
475 if (complete && len == dv1394src->frame_size) {
479 buf = gst_buffer_new_and_alloc (dv1394src->frame_size);
481 GST_BUFFER_OFFSET (buf) = dv1394src->frame_sequence;
482 bufdata = GST_BUFFER_DATA (buf);
483 memcpy (bufdata, data, len);
484 dv1394src->buf = buf;
487 dv1394src->frame_sequence++;
493 gst_dv1394src_iso_receive (raw1394handle_t handle, int channel, size_t len,
496 GstDV1394Src *dv1394src = gst_dv1394src_from_raw1394handle (handle);
500 the following code taken from kino-0.51 (Dan Dennedy/Charles Yates)
501 Kindly relicensed under the LGPL. See the commit log for version 1.6 of
504 unsigned char *p = (unsigned char *) &data[3];
506 int section_type = p[0] >> 5; /* section type is in bits 5 - 7 */
507 int dif_sequence = p[1] >> 4; /* dif sequence number is in bits 4 - 7 */
508 int dif_block = p[2];
510 /* if we are at the beginning of a frame,
511 we set buf=frame, and alloc a new buffer for frame
513 if (section_type == 0 && dif_sequence == 0) { // dif header
514 if (!GST_PAD_CAPS (GST_BASE_SRC_PAD (dv1394src))) {
517 // figure format (NTSC/PAL)
520 dv1394src->frame_size = PAL_FRAMESIZE;
521 dv1394src->frame_rate = PAL_FRAMERATE;
522 GST_DEBUG ("PAL data");
523 caps = gst_caps_new_simple ("video/x-dv",
524 "format", G_TYPE_STRING, "PAL",
525 "systemstream", G_TYPE_BOOLEAN, TRUE, NULL);
528 dv1394src->frame_size = NTSC_FRAMESIZE;
529 dv1394src->frame_rate = NTSC_FRAMERATE;
531 ("NTSC data [untested] - please report success/failure to <dan@f3c.com>");
532 caps = gst_caps_new_simple ("video/x-dv",
533 "format", G_TYPE_STRING, "NTSC",
534 "systemstream", G_TYPE_BOOLEAN, TRUE, NULL);
536 gst_pad_set_caps (GST_BASE_SRC_PAD (dv1394src), caps);
537 gst_caps_unref (caps);
539 // drop last frame when not complete
540 if (!dv1394src->drop_incomplete
541 || dv1394src->bytes_in_frame == dv1394src->frame_size) {
542 dv1394src->buf = dv1394src->frame;
544 GST_INFO_OBJECT (GST_ELEMENT (dv1394src), "incomplete frame dropped");
545 g_signal_emit (G_OBJECT (dv1394src),
546 gst_dv1394src_signals[SIGNAL_FRAME_DROPPED], 0);
547 if (dv1394src->frame) {
548 gst_buffer_unref (dv1394src->frame);
551 if ((dv1394src->frame_sequence + 1) % (dv1394src->skip +
552 dv1394src->consecutive) < dv1394src->consecutive) {
556 buf = gst_buffer_new_and_alloc (dv1394src->frame_size);
558 /* fill in offset, duration, timestamp */
559 GST_BUFFER_OFFSET (buf) = dv1394src->frame_sequence;
560 dv1394src->frame = buf;
562 dv1394src->frame_sequence++;
563 dv1394src->bytes_in_frame = 0;
566 if (dv1394src->frame != NULL) {
567 guint8 *data = GST_BUFFER_DATA (dv1394src->frame);
569 switch (section_type) {
570 case 0: /* 1 Header block */
571 /* p[3] |= 0x80; // hack to force PAL data */
572 memcpy (data + dif_sequence * 150 * 80, p, 480);
575 case 1: /* 2 Subcode blocks */
576 memcpy (data + dif_sequence * 150 * 80 + (1 + dif_block) * 80, p,
580 case 2: /* 3 VAUX blocks */
581 memcpy (data + dif_sequence * 150 * 80 + (3 + dif_block) * 80, p,
585 case 3: /* 9 Audio blocks interleaved with video */
586 memcpy (data + dif_sequence * 150 * 80 + (6 + dif_block * 16) * 80, p,
590 case 4: /* 135 Video blocks interleaved with audio */
591 memcpy (data + dif_sequence * 150 * 80 + (7 + (dif_block / 15) +
592 dif_block) * 80, p, 480);
595 default: /* we can't handle any other data */
598 dv1394src->bytes_in_frame += 480;
606 * When an ieee1394 bus reset happens, usually a device has been removed
607 * or added. We send a message on the message bus with the node count
608 * and whether the capture device used in this element connected, disconnected
611 * nodecount - integer with number of nodes on bus
612 * current-device-change - integer (1 if device connected, 0 if no change to
613 * current device status, -1 if device disconnected)
616 gst_dv1394src_bus_reset (raw1394handle_t handle, unsigned int generation)
621 GstStructure *structure;
622 gint current_device_change;
625 src = gst_dv1394src_from_raw1394handle (handle);
627 GST_INFO_OBJECT (src, "have bus reset");
629 /* update generation - told to do so by docs */
630 raw1394_update_generation (handle, generation);
631 nodecount = raw1394_get_nodecount (handle);
632 /* allocate memory for portinfo */
634 /* current_device_change is -1 if camera disconnected, 0 if other device
635 * connected or 1 if camera has now connected */
636 current_device_change = -1;
637 for (i = 0; i < nodecount; i++) {
638 if (src->guid == rom1394_get_guid (handle, i)) {
639 /* Camera is with us */
640 GST_DEBUG ("Camera is with us");
641 if (!src->connected) {
642 current_device_change = 1;
643 src->connected = TRUE;
645 current_device_change = 0;
648 if (src->connected && current_device_change == -1) {
649 GST_DEBUG ("Camera has disconnected");
650 src->connected = FALSE;
651 } else if (!src->connected && current_device_change == -1) {
652 GST_DEBUG ("Camera is still not with us");
653 current_device_change = 0;
656 structure = gst_structure_new ("ieee1394-bus-reset", "nodecount", G_TYPE_INT,
657 nodecount, "current-device-change", G_TYPE_INT, current_device_change,
659 message = gst_message_new_element (GST_OBJECT (src), structure);
660 gst_element_post_message (GST_ELEMENT (src), message);
666 gst_dv1394src_create (GstPushSrc * psrc, GstBuffer ** buf)
668 GstDV1394Src *dv1394src = GST_DV1394SRC (psrc);
670 struct pollfd pollfds[2];
672 pollfds[0].fd = raw1394_get_fd (dv1394src->handle);
673 pollfds[0].events = POLLIN | POLLERR | POLLHUP | POLLPRI;
674 pollfds[1].fd = READ_SOCKET (dv1394src);
675 pollfds[1].events = POLLIN | POLLERR | POLLHUP | POLLPRI;
677 if (G_UNLIKELY (dv1394src->buf)) {
678 /* maybe we had an error before, and there's a stale buffer? */
679 gst_buffer_unref (dv1394src->buf);
680 dv1394src->buf = NULL;
684 int res = poll (pollfds, 2, -1);
686 if (G_UNLIKELY (res < 0)) {
687 if (errno == EAGAIN || errno == EINTR)
690 goto error_while_polling;
693 if (G_UNLIKELY (pollfds[1].revents)) {
696 if (pollfds[1].revents & POLLIN)
697 READ_COMMAND (dv1394src, command, res);
700 } else if (G_LIKELY (pollfds[0].revents & POLLIN)) {
701 /* shouldn't block in theory */
702 raw1394_loop_iterate (dv1394src->handle);
709 g_assert (dv1394src->buf);
711 caps = gst_pad_get_caps (GST_BASE_SRC_PAD (psrc));
712 gst_buffer_set_caps (dv1394src->buf, caps);
713 gst_caps_unref (caps);
715 *buf = dv1394src->buf;
716 dv1394src->buf = NULL;
721 GST_ELEMENT_ERROR (dv1394src, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
722 return GST_FLOW_UNEXPECTED;
726 GST_DEBUG_OBJECT (dv1394src, "told to stop, shutting down");
727 return GST_FLOW_WRONG_STATE;
732 gst_dv1394src_discover_avc_node (GstDV1394Src * src)
736 int m = src->num_ports;
738 if (src->port >= 0) {
739 /* search on explicit port */
744 /* loop over all our ports */
745 for (; j < m && node == -1; j++) {
746 raw1394handle_t handle;
747 struct raw1394_portinfo pinf[16];
750 handle = raw1394_new_handle ();
752 GST_WARNING ("raw1394 - failed to get handle: %s.\n", strerror (errno));
755 if (raw1394_get_port_info (handle, pinf, 16) < 0) {
756 GST_WARNING ("raw1394 - failed to get port info: %s.\n",
761 /* tell raw1394 which host adapter to use */
762 if (raw1394_set_port (handle, j) < 0) {
763 GST_WARNING ("raw1394 - failed to set set port: %s.\n", strerror (errno));
767 /* now loop over all the nodes */
768 for (i = 0; i < raw1394_get_nodecount (handle); i++) {
769 /* are we looking for an explicit GUID ? */
770 if (src->guid != 0) {
771 if (src->guid == rom1394_get_guid (handle, i)) {
775 src->uri = g_strdup_printf ("dv://%d", src->port);
779 rom1394_directory rom_dir;
781 /* select first AV/C Tape Recorder Player node */
782 if (rom1394_get_directory (handle, i, &rom_dir) < 0) {
783 GST_WARNING ("error reading config rom directory for node %d\n", i);
786 if ((rom1394_get_node_type (&rom_dir) == ROM1394_NODE_TYPE_AVC) &&
787 avc1394_check_subunit_type (handle, i, AVC1394_SUBUNIT_TYPE_VCR)) {
790 src->guid = rom1394_get_guid (handle, i);
792 src->uri = g_strdup_printf ("dv://%d", src->port);
793 g_free (src->device_name);
794 src->device_name = g_strdup (rom_dir.label);
797 rom1394_free_directory (&rom_dir);
801 raw1394_destroy_handle (handle);
807 gst_dv1394src_start (GstBaseSrc * bsrc)
809 GstDV1394Src *src = GST_DV1394SRC (bsrc);
812 src->connected = FALSE;
814 if (socketpair (PF_UNIX, SOCK_STREAM, 0, control_sock) < 0)
817 READ_SOCKET (src) = control_sock[0];
818 WRITE_SOCKET (src) = control_sock[1];
820 fcntl (READ_SOCKET (src), F_SETFL, O_NONBLOCK);
821 fcntl (WRITE_SOCKET (src), F_SETFL, O_NONBLOCK);
823 src->handle = raw1394_new_handle ();
827 goto permission_denied;
828 else if (errno == ENOENT)
834 src->num_ports = raw1394_get_port_info (src->handle, src->pinfo, 16);
836 if (src->num_ports == 0)
839 if (src->use_avc || src->port == -1)
840 src->avc_node = gst_dv1394src_discover_avc_node (src);
842 /* lets destroy handle and create one on port
843 this is more reliable than setting port on
844 the existing handle */
845 raw1394_destroy_handle (src->handle);
846 src->handle = raw1394_new_handle_on_port (src->port);
848 goto cannot_set_port;
850 raw1394_set_userdata (src->handle, src);
851 raw1394_set_bus_reset_handler (src->handle, gst_dv1394src_bus_reset);
853 #ifdef HAVE_LIBIEC61883
854 if ((src->iec61883dv =
855 iec61883_dv_fb_init (src->handle,
856 gst_dv1394src_iec61883_receive, src)) == NULL)
857 goto cannot_initialise_dv;
860 raw1394_set_iso_handler (src->handle, src->channel,
861 gst_dv1394src_iso_receive);
864 GST_DEBUG_OBJECT (src, "successfully opened up 1394 connection");
865 src->connected = TRUE;
867 #ifdef HAVE_LIBIEC61883
868 if (iec61883_dv_fb_start (src->iec61883dv, src->channel) != 0)
871 if (raw1394_start_iso_rcv (src->handle, src->channel) < 0)
876 raw1394handle_t avc_handle = raw1394_new_handle_on_port (src->port);
880 if (!avc1394_vcr_is_recording (avc_handle, src->avc_node)
881 && avc1394_vcr_is_playing (avc_handle, src->avc_node)
882 != AVC1394_VCR_OPERAND_PLAY_FORWARD)
883 avc1394_vcr_play (avc_handle, src->avc_node);
884 raw1394_destroy_handle (avc_handle);
886 GST_WARNING_OBJECT (src, "Starting VCR via avc1394 failed: %s",
891 gst_1394_clock_set_handle (src->provided_clock, src->handle);
897 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
903 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL), GST_ERROR_SYSTEM);
908 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL), GST_ERROR_SYSTEM);
913 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
914 ("can't get raw1394 handle (%s)", g_strerror (errno)));
919 raw1394_destroy_handle (src->handle);
921 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
922 ("no ports available for raw1394"));
927 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
928 ("can't set 1394 port %d", src->port));
933 raw1394_destroy_handle (src->handle);
935 #ifdef HAVE_LIBIEC61883
936 iec61883_dv_fb_close (src->iec61883dv);
937 src->iec61883dv = NULL;
939 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
940 ("can't start 1394 iso receive"));
943 #ifdef HAVE_LIBIEC61883
944 cannot_initialise_dv:
946 raw1394_destroy_handle (src->handle);
948 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
949 ("can't initialise iec61883 dv"));
956 gst_dv1394src_stop (GstBaseSrc * bsrc)
958 GstDV1394Src *src = GST_DV1394SRC (bsrc);
960 close (READ_SOCKET (src));
961 close (WRITE_SOCKET (src));
962 READ_SOCKET (src) = -1;
963 WRITE_SOCKET (src) = -1;
964 #ifdef HAVE_LIBIEC61883
965 iec61883_dv_fb_close (src->iec61883dv);
967 raw1394_stop_iso_rcv (src->handle, src->channel);
971 raw1394handle_t avc_handle = raw1394_new_handle_on_port (src->port);
973 /* pause and stop the VCR */
975 if (!avc1394_vcr_is_recording (avc_handle, src->avc_node)
976 && (avc1394_vcr_is_playing (avc_handle, src->avc_node)
977 != AVC1394_VCR_OPERAND_PLAY_FORWARD_PAUSE))
978 avc1394_vcr_pause (avc_handle, src->avc_node);
979 avc1394_vcr_stop (avc_handle, src->avc_node);
980 raw1394_destroy_handle (avc_handle);
982 GST_WARNING_OBJECT (src, "Starting VCR via avc1394 failed: %s",
987 gst_1394_clock_unset_handle (src->provided_clock);
989 raw1394_destroy_handle (src->handle);
995 gst_dv1394src_unlock (GstBaseSrc * bsrc)
997 GstDV1394Src *src = GST_DV1394SRC (bsrc);
999 SEND_COMMAND (src, CONTROL_STOP);
1005 gst_dv1394src_query (GstBaseSrc * basesrc, GstQuery * query)
1007 switch (GST_QUERY_TYPE (query)) {
1008 case GST_QUERY_LATENCY:
1010 gst_query_set_latency (query, TRUE, GST_SECOND / 25, GST_CLOCK_TIME_NONE);
1020 return GST_BASE_SRC_CLASS (parent_class)->query (basesrc, query);
1024 gst_dv1394src_update_device_name (GstDV1394Src * src)
1026 raw1394handle_t handle;
1027 gint portcount, port, nodecount, node;
1028 rom1394_directory directory;
1030 g_free (src->device_name);
1031 src->device_name = NULL;
1033 GST_LOG_OBJECT (src, "updating device name for current GUID");
1035 handle = raw1394_new_handle ();
1038 goto gethandle_failed;
1040 portcount = raw1394_get_port_info (handle, NULL, 0);
1041 for (port = 0; port < portcount; port++) {
1042 if (raw1394_set_port (handle, port) >= 0) {
1043 nodecount = raw1394_get_nodecount (handle);
1044 for (node = 0; node < nodecount; node++) {
1045 if (src->guid == rom1394_get_guid (handle, node)) {
1046 if (rom1394_get_directory (handle, node, &directory) >= 0) {
1047 g_free (src->device_name);
1048 src->device_name = g_strdup (directory.label);
1049 rom1394_free_directory (&directory);
1052 GST_WARNING ("error reading rom directory for node %d", node);
1059 src->device_name = g_strdup ("Unknown"); /* FIXME: translate? */
1063 raw1394_destroy_handle (handle);
1069 GST_WARNING ("failed to get raw1394 handle: %s", g_strerror (errno));
1070 src->device_name = g_strdup ("Unknown"); /* FIXME: translate? */
1075 /*** GSTURIHANDLER INTERFACE *************************************************/
1078 gst_dv1394src_uri_get_type (void)
1084 gst_dv1394src_uri_get_protocols (void)
1086 static gchar *protocols[] = { (char *) "dv", NULL };
1091 static const gchar *
1092 gst_dv1394src_uri_get_uri (GstURIHandler * handler)
1094 GstDV1394Src *gst_dv1394src = GST_DV1394SRC (handler);
1096 return gst_dv1394src->uri;
1100 gst_dv1394src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
1102 gchar *protocol, *location;
1103 gboolean ret = TRUE;
1104 GstDV1394Src *gst_dv1394src = GST_DV1394SRC (handler);
1106 protocol = gst_uri_get_protocol (uri);
1107 if (strcmp (protocol, "dv") != 0) {
1113 location = gst_uri_get_location (uri);
1114 if (location && *location != '\0')
1115 gst_dv1394src->port = strtol (location, NULL, 10);
1117 gst_dv1394src->port = DEFAULT_PORT;
1119 g_free (gst_dv1394src->uri);
1120 gst_dv1394src->uri = g_strdup_printf ("dv://%d", gst_dv1394src->port);
1126 gst_dv1394src_uri_handler_init (gpointer g_iface, gpointer iface_data)
1128 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
1130 iface->get_type = gst_dv1394src_uri_get_type;
1131 iface->get_protocols = gst_dv1394src_uri_get_protocols;
1132 iface->get_uri = gst_dv1394src_uri_get_uri;
1133 iface->set_uri = gst_dv1394src_uri_set_uri;