Fix FSF address
[platform/upstream/gst-plugins-good.git] / ext / raw1394 / gsthdv1394src.c
1 /* GStreamer
2  * Copyright (C) <2008> Edward Hervey <bilboed@bilboed.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 /**
20  * SECTION:element-hdv1394src
21  *
22  * Read MPEG-TS data from firewire port.
23  *
24  * <refsect2>
25  * <title>Example launch line</title>
26  * |[
27  * gst-launch-1.0 hdv1394src ! queue ! decodebin name=d ! queue ! xvimagesink d. ! queue ! alsasink
28  * ]| captures from the firewire port and plays the streams.
29  * |[
30  * gst-launch-1.0 hdv1394src ! queue ! filesink location=mydump.ts
31  * ]| capture to a disk file
32  * </refsect2>
33  */
34
35 #ifdef HAVE_CONFIG_H
36 #include "config.h"
37 #endif
38 #include <unistd.h>
39 #include <sys/poll.h>
40 #include <sys/socket.h>
41 #include <errno.h>
42 #include <fcntl.h>
43 #include <string.h>
44 #include <stdlib.h>
45
46 #include <libavc1394/avc1394.h>
47 #include <libavc1394/avc1394_vcr.h>
48 #include <libavc1394/rom1394.h>
49 #include <libraw1394/raw1394.h>
50 #include <libiec61883/iec61883.h>
51
52 #include <gst/gst.h>
53
54 #include "gsthdv1394src.h"
55 #include "gst1394probe.h"
56
57
58 #define CONTROL_STOP            'S'     /* stop the select call */
59 #define CONTROL_SOCKETS(src)   src->control_sock
60 #define WRITE_SOCKET(src)      src->control_sock[1]
61 #define READ_SOCKET(src)       src->control_sock[0]
62
63 #define SEND_COMMAND(src, command)          \
64 G_STMT_START {                              \
65   int G_GNUC_UNUSED _res; unsigned char c; c = command;   \
66   _res = write (WRITE_SOCKET(src), &c, 1);  \
67 } G_STMT_END
68
69 #define READ_COMMAND(src, command, res)        \
70 G_STMT_START {                                 \
71   res = read(READ_SOCKET(src), &command, 1);   \
72 } G_STMT_END
73
74
75 GST_DEBUG_CATEGORY_STATIC (hdv1394src_debug);
76 #define GST_CAT_DEFAULT (hdv1394src_debug)
77
78 #define DEFAULT_PORT    -1
79 #define DEFAULT_CHANNEL   63
80 #define DEFAULT_USE_AVC   TRUE
81 #define DEFAULT_GUID    0
82
83 enum
84 {
85   PROP_0,
86   PROP_PORT,
87   PROP_CHANNEL,
88   PROP_USE_AVC,
89   PROP_GUID,
90   PROP_DEVICE_NAME
91 };
92
93 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
94     GST_PAD_SRC,
95     GST_PAD_ALWAYS,
96     GST_STATIC_CAPS
97     ("video/mpegts,systemstream=(boolean)true,packetsize=(int)188")
98     );
99
100 static void gst_hdv1394src_uri_handler_init (gpointer g_iface,
101     gpointer iface_data);
102
103 static void gst_hdv1394src_set_property (GObject * object, guint prop_id,
104     const GValue * value, GParamSpec * pspec);
105 static void gst_hdv1394src_get_property (GObject * object, guint prop_id,
106     GValue * value, GParamSpec * pspec);
107 static void gst_hdv1394src_dispose (GObject * object);
108
109 static gboolean gst_hdv1394src_start (GstBaseSrc * bsrc);
110 static gboolean gst_hdv1394src_stop (GstBaseSrc * bsrc);
111 static gboolean gst_hdv1394src_unlock (GstBaseSrc * bsrc);
112
113 static GstFlowReturn gst_hdv1394src_create (GstPushSrc * psrc,
114     GstBuffer ** buf);
115
116 static void gst_hdv1394src_update_device_name (GstHDV1394Src * src);
117
118 #define gst_hdv1394src_parent_class parent_class
119 G_DEFINE_TYPE_WITH_CODE (GstHDV1394Src, gst_hdv1394src, GST_TYPE_PUSH_SRC,
120     G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER,
121         gst_hdv1394src_uri_handler_init));
122
123 static void
124 gst_hdv1394src_class_init (GstHDV1394SrcClass * klass)
125 {
126   GObjectClass *gobject_class;
127   GstElementClass *gstelement_class;
128   GstBaseSrcClass *gstbasesrc_class;
129   GstPushSrcClass *gstpushsrc_class;
130
131   gobject_class = (GObjectClass *) klass;
132   gstelement_class = (GstElementClass *) klass;
133   gstbasesrc_class = (GstBaseSrcClass *) klass;
134   gstpushsrc_class = (GstPushSrcClass *) klass;
135
136   gobject_class->set_property = gst_hdv1394src_set_property;
137   gobject_class->get_property = gst_hdv1394src_get_property;
138   gobject_class->dispose = gst_hdv1394src_dispose;
139
140   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_PORT,
141       g_param_spec_int ("port", "Port", "Port number (-1 automatic)",
142           -1, 16, DEFAULT_PORT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
143   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_CHANNEL,
144       g_param_spec_int ("channel", "Channel", "Channel number for listening",
145           0, 64, DEFAULT_CHANNEL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
146   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_USE_AVC,
147       g_param_spec_boolean ("use-avc", "Use AV/C", "Use AV/C VTR control",
148           DEFAULT_USE_AVC, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
149   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_GUID,
150       g_param_spec_uint64 ("guid", "GUID",
151           "select one of multiple DV devices by its GUID. use a hexadecimal "
152           "like 0xhhhhhhhhhhhhhhhh. (0 = no guid)", 0, G_MAXUINT64,
153           DEFAULT_GUID, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
154   /**
155    * GstHDV1394Src:device-name
156    *
157    * Descriptive name of the currently opened device
158    *
159    * Since: 0.10.7
160    **/
161   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_DEVICE_NAME,
162       g_param_spec_string ("device-name", "device name",
163           "user-friendly name of the device", "Default",
164           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
165
166   gstbasesrc_class->negotiate = NULL;
167   gstbasesrc_class->start = gst_hdv1394src_start;
168   gstbasesrc_class->stop = gst_hdv1394src_stop;
169   gstbasesrc_class->unlock = gst_hdv1394src_unlock;
170
171   gstpushsrc_class->create = gst_hdv1394src_create;
172
173   gst_element_class_add_pad_template (gstelement_class,
174       gst_static_pad_template_get (&src_factory));
175
176   gst_element_class_set_static_metadata (gstelement_class,
177       "Firewire (1394) HDV video source", "Source/Video",
178       "Source for MPEG-TS video data from firewire port",
179       "Edward Hervey <bilboed@bilboed.com>");
180
181   GST_DEBUG_CATEGORY_INIT (hdv1394src_debug, "hdv1394src", 0,
182       "MPEG-TS firewire source");
183 }
184
185 static void
186 gst_hdv1394src_init (GstHDV1394Src * dv1394src)
187 {
188   GstPad *srcpad = GST_BASE_SRC_PAD (dv1394src);
189
190   gst_base_src_set_live (GST_BASE_SRC (dv1394src), TRUE);
191   gst_pad_use_fixed_caps (srcpad);
192
193   dv1394src->port = DEFAULT_PORT;
194   dv1394src->channel = DEFAULT_CHANNEL;
195
196   dv1394src->use_avc = DEFAULT_USE_AVC;
197   dv1394src->guid = DEFAULT_GUID;
198   dv1394src->uri = g_strdup_printf ("hdv://%d", dv1394src->port);
199   dv1394src->device_name = g_strdup_printf ("Default");
200
201   READ_SOCKET (dv1394src) = -1;
202   WRITE_SOCKET (dv1394src) = -1;
203
204   dv1394src->frame_sequence = 0;
205 }
206
207 static void
208 gst_hdv1394src_dispose (GObject * object)
209 {
210   GstHDV1394Src *src = GST_HDV1394SRC (object);
211
212   g_free (src->uri);
213   src->uri = NULL;
214
215   g_free (src->device_name);
216   src->device_name = NULL;
217
218   G_OBJECT_CLASS (parent_class)->dispose (object);
219 }
220
221 static void
222 gst_hdv1394src_set_property (GObject * object, guint prop_id,
223     const GValue * value, GParamSpec * pspec)
224 {
225   GstHDV1394Src *filter = GST_HDV1394SRC (object);
226
227   switch (prop_id) {
228     case PROP_PORT:
229       filter->port = g_value_get_int (value);
230       g_free (filter->uri);
231       filter->uri = g_strdup_printf ("hdv://%d", filter->port);
232       break;
233     case PROP_CHANNEL:
234       filter->channel = g_value_get_int (value);
235       break;
236     case PROP_USE_AVC:
237       filter->use_avc = g_value_get_boolean (value);
238       break;
239     case PROP_GUID:
240       filter->guid = g_value_get_uint64 (value);
241       gst_hdv1394src_update_device_name (filter);
242       break;
243     default:
244       break;
245   }
246 }
247
248 static void
249 gst_hdv1394src_get_property (GObject * object, guint prop_id, GValue * value,
250     GParamSpec * pspec)
251 {
252   GstHDV1394Src *filter = GST_HDV1394SRC (object);
253
254   switch (prop_id) {
255     case PROP_PORT:
256       g_value_set_int (value, filter->port);
257       break;
258     case PROP_CHANNEL:
259       g_value_set_int (value, filter->channel);
260       break;
261     case PROP_USE_AVC:
262       g_value_set_boolean (value, filter->use_avc);
263       break;
264     case PROP_GUID:
265       g_value_set_uint64 (value, filter->guid);
266       break;
267     case PROP_DEVICE_NAME:
268       g_value_set_string (value, filter->device_name);
269       break;
270     default:
271       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
272       break;
273   }
274 }
275
276 static GstHDV1394Src *
277 gst_hdv1394src_from_raw1394handle (raw1394handle_t handle)
278 {
279   iec61883_mpeg2_t mpeg2 = (iec61883_mpeg2_t) raw1394_get_userdata (handle);
280   return GST_HDV1394SRC (iec61883_mpeg2_get_callback_data (mpeg2));
281 }
282
283 /* Within one loop iteration (which may call _receive() many times), it seems
284  * as though '*data' will always be different.
285  *
286  * We can therefore assume that any '*data' given to us will stay allocated until
287  * the next loop iteration.
288  */
289
290 static int
291 gst_hdv1394src_iec61883_receive (unsigned char *data, int len,
292     unsigned int dropped, void *cbdata)
293 {
294   GstHDV1394Src *dv1394src = GST_HDV1394SRC (cbdata);
295
296   GST_LOG ("data:%p, len:%d, dropped:%d", data, len, dropped);
297
298   /* error out if we don't have enough room ! */
299   if (G_UNLIKELY (dv1394src->outoffset > (2048 * 188 - len)))
300     return -1;
301
302   if (G_LIKELY (len == IEC61883_MPEG2_TSP_SIZE)) {
303     memcpy ((guint8 *) dv1394src->outdata + dv1394src->outoffset, data, len);
304     dv1394src->outoffset += len;
305   }
306   dv1394src->frame_sequence++;
307   return 0;
308 }
309
310 /*
311  * When an ieee1394 bus reset happens, usually a device has been removed
312  * or added.  We send a message on the message bus with the node count 
313  * and whether the capture device used in this element connected, disconnected 
314  * or was unchanged
315  * Message structure:
316  * nodecount - integer with number of nodes on bus
317  * current-device-change - integer (1 if device connected, 0 if no change to
318  *                         current device status, -1 if device disconnected)
319  */
320 static int
321 gst_hdv1394src_bus_reset (raw1394handle_t handle, unsigned int generation)
322 {
323   GstHDV1394Src *src;
324   gint nodecount;
325   GstMessage *message;
326   GstStructure *structure;
327   gint current_device_change;
328   gint i;
329
330   src = gst_hdv1394src_from_raw1394handle (handle);
331
332   GST_INFO_OBJECT (src, "have bus reset");
333
334   /* update generation - told to do so by docs */
335   raw1394_update_generation (handle, generation);
336   nodecount = raw1394_get_nodecount (handle);
337   /* allocate memory for portinfo */
338
339   /* current_device_change is -1 if camera disconnected, 0 if other device
340    * connected or 1 if camera has now connected */
341   current_device_change = -1;
342   for (i = 0; i < nodecount; i++) {
343     if (src->guid == rom1394_get_guid (handle, i)) {
344       /* Camera is with us */
345       GST_DEBUG ("Camera is with us");
346       if (!src->connected) {
347         current_device_change = 1;
348         src->connected = TRUE;
349       } else
350         current_device_change = 0;
351     }
352   }
353   if (src->connected && current_device_change == -1) {
354     GST_DEBUG ("Camera has disconnected");
355     src->connected = FALSE;
356   } else if (!src->connected && current_device_change == -1) {
357     GST_DEBUG ("Camera is still not with us");
358     current_device_change = 0;
359   }
360
361   structure = gst_structure_new ("ieee1394-bus-reset", "nodecount", G_TYPE_INT,
362       nodecount, "current-device-change", G_TYPE_INT, current_device_change,
363       NULL);
364   message = gst_message_new_element (GST_OBJECT (src), structure);
365   gst_element_post_message (GST_ELEMENT (src), message);
366
367   return 0;
368 }
369
370 static GstFlowReturn
371 gst_hdv1394src_create (GstPushSrc * psrc, GstBuffer ** buf)
372 {
373   GstHDV1394Src *dv1394src = GST_HDV1394SRC (psrc);
374   struct pollfd pollfds[2];
375
376   pollfds[0].fd = raw1394_get_fd (dv1394src->handle);
377   pollfds[0].events = POLLIN | POLLERR | POLLHUP | POLLPRI;
378   pollfds[1].fd = READ_SOCKET (dv1394src);
379   pollfds[1].events = POLLIN | POLLERR | POLLHUP | POLLPRI;
380
381   /* allocate a 2048 samples buffer */
382   dv1394src->outdata = g_malloc (2048 * 188);
383   dv1394src->outoffset = 0;
384
385   GST_DEBUG ("Create...");
386
387   while (TRUE) {
388     int res = poll (pollfds, 2, -1);
389
390     GST_LOG ("res:%d", res);
391
392     if (G_UNLIKELY (res < 0)) {
393       if (errno == EAGAIN || errno == EINTR)
394         continue;
395       else
396         goto error_while_polling;
397     }
398
399     if (G_UNLIKELY (pollfds[1].revents)) {
400       char command;
401
402       if (pollfds[1].revents & POLLIN)
403         READ_COMMAND (dv1394src, command, res);
404
405       goto told_to_stop;
406     } else if (G_LIKELY (pollfds[0].revents & POLLIN)) {
407       int pt;
408
409       pt = dv1394src->frame_sequence;
410       /* shouldn't block in theory */
411       GST_LOG ("Iterating ! (%d)", dv1394src->frame_sequence);
412       raw1394_loop_iterate (dv1394src->handle);
413       GST_LOG ("After iteration : %d (diff:%d)",
414           dv1394src->frame_sequence, dv1394src->frame_sequence - pt);
415       if (dv1394src->outoffset)
416         break;
417     }
418   }
419
420   g_assert (dv1394src->outoffset);
421
422   GST_LOG ("We have some frames (%u bytes)", (guint) dv1394src->outoffset);
423
424   /* Create the buffer */
425   *buf = gst_buffer_new_wrapped (dv1394src->outdata, dv1394src->outoffset);
426   dv1394src->outdata = NULL;
427   dv1394src->outoffset = 0;
428
429   return GST_FLOW_OK;
430
431 error_while_polling:
432   {
433     GST_ELEMENT_ERROR (dv1394src, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
434     return GST_FLOW_EOS;
435   }
436 told_to_stop:
437   {
438     GST_DEBUG_OBJECT (dv1394src, "told to stop, shutting down");
439     return GST_FLOW_FLUSHING;
440   }
441 }
442
443 static int
444 gst_hdv1394src_discover_avc_node (GstHDV1394Src * src)
445 {
446   int node = -1;
447   int i, j = 0;
448   int m = src->num_ports;
449
450   if (src->port >= 0) {
451     /* search on explicit port */
452     j = src->port;
453     m = j + 1;
454   }
455
456   /* loop over all our ports */
457   for (; j < m && node == -1; j++) {
458     raw1394handle_t handle;
459     struct raw1394_portinfo pinf[16];
460
461     /* open the port */
462     handle = raw1394_new_handle ();
463     if (!handle) {
464       GST_WARNING ("raw1394 - failed to get handle: %s.\n", strerror (errno));
465       continue;
466     }
467     if (raw1394_get_port_info (handle, pinf, 16) < 0) {
468       GST_WARNING ("raw1394 - failed to get port info: %s.\n",
469           strerror (errno));
470       goto next;
471     }
472
473     /* tell raw1394 which host adapter to use */
474     if (raw1394_set_port (handle, j) < 0) {
475       GST_WARNING ("raw1394 - failed to set set port: %s.\n", strerror (errno));
476       goto next;
477     }
478
479     /* now loop over all the nodes */
480     for (i = 0; i < raw1394_get_nodecount (handle); i++) {
481       /* are we looking for an explicit GUID ? */
482       if (src->guid != 0) {
483         if (src->guid == rom1394_get_guid (handle, i)) {
484           node = i;
485           src->port = j;
486           g_free (src->uri);
487           src->uri = g_strdup_printf ("dv://%d", src->port);
488           break;
489         }
490       } else {
491         rom1394_directory rom_dir;
492
493         /* select first AV/C Tape Recorder Player node */
494         if (rom1394_get_directory (handle, i, &rom_dir) < 0) {
495           GST_WARNING ("error reading config rom directory for node %d\n", i);
496           continue;
497         }
498         if ((rom1394_get_node_type (&rom_dir) == ROM1394_NODE_TYPE_AVC) &&
499             avc1394_check_subunit_type (handle, i, AVC1394_SUBUNIT_TYPE_VCR)) {
500           node = i;
501           src->port = j;
502           src->guid = rom1394_get_guid (handle, i);
503           g_free (src->uri);
504           src->uri = g_strdup_printf ("dv://%d", src->port);
505           g_free (src->device_name);
506           src->device_name = g_strdup (rom_dir.label);
507           break;
508         }
509         rom1394_free_directory (&rom_dir);
510       }
511     }
512   next:
513     raw1394_destroy_handle (handle);
514   }
515   return node;
516 }
517
518 static gboolean
519 gst_hdv1394src_start (GstBaseSrc * bsrc)
520 {
521   GstHDV1394Src *src = GST_HDV1394SRC (bsrc);
522   int control_sock[2];
523
524   src->connected = FALSE;
525
526   if (socketpair (PF_UNIX, SOCK_STREAM, 0, control_sock) < 0)
527     goto socket_pair;
528
529   READ_SOCKET (src) = control_sock[0];
530   WRITE_SOCKET (src) = control_sock[1];
531
532   fcntl (READ_SOCKET (src), F_SETFL, O_NONBLOCK);
533   fcntl (WRITE_SOCKET (src), F_SETFL, O_NONBLOCK);
534
535   src->handle = raw1394_new_handle ();
536
537   if (!src->handle) {
538     if (errno == EACCES)
539       goto permission_denied;
540     else if (errno == ENOENT)
541       goto not_found;
542     else
543       goto no_handle;
544   }
545
546   src->num_ports = raw1394_get_port_info (src->handle, src->pinfo, 16);
547
548   if (src->num_ports == 0)
549     goto no_ports;
550
551   if (src->use_avc || src->port == -1)
552     src->avc_node = gst_hdv1394src_discover_avc_node (src);
553
554   /* lets destroy handle and create one on port
555      this is more reliable than setting port on
556      the existing handle */
557   raw1394_destroy_handle (src->handle);
558   src->handle = raw1394_new_handle_on_port (src->port);
559   if (!src->handle)
560     goto cannot_set_port;
561
562   raw1394_set_userdata (src->handle, src);
563   raw1394_set_bus_reset_handler (src->handle, gst_hdv1394src_bus_reset);
564
565   if ((src->iec61883mpeg2 =
566           iec61883_mpeg2_recv_init (src->handle,
567               gst_hdv1394src_iec61883_receive, src)) == NULL)
568     goto cannot_initialise_dv;
569
570 #if 0
571   raw1394_set_iso_handler (src->handle, src->channel,
572       gst_hdv1394src_iso_receive);
573 #endif
574
575   GST_DEBUG_OBJECT (src, "successfully opened up 1394 connection");
576   src->connected = TRUE;
577
578   if (iec61883_mpeg2_recv_start (src->iec61883mpeg2, src->channel) != 0)
579     goto cannot_start;
580 #if 0
581   if (raw1394_start_iso_rcv (src->handle, src->channel) < 0)
582     goto cannot_start;
583 #endif
584
585   if (src->use_avc) {
586     raw1394handle_t avc_handle = raw1394_new_handle_on_port (src->port);
587
588     GST_LOG ("We have an avc_handle");
589
590     /* start the VCR */
591     if (avc_handle) {
592       if (!avc1394_vcr_is_recording (avc_handle, src->avc_node)
593           && avc1394_vcr_is_playing (avc_handle, src->avc_node)
594           != AVC1394_VCR_OPERAND_PLAY_FORWARD) {
595         GST_LOG ("Calling avc1394_vcr_play()");
596         avc1394_vcr_play (avc_handle, src->avc_node);
597       }
598       raw1394_destroy_handle (avc_handle);
599     } else {
600       GST_WARNING_OBJECT (src, "Starting VCR via avc1394 failed: %s",
601           g_strerror (errno));
602     }
603   }
604
605   return TRUE;
606
607 socket_pair:
608   {
609     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
610         GST_ERROR_SYSTEM);
611     return FALSE;
612   }
613 permission_denied:
614   {
615     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL), GST_ERROR_SYSTEM);
616     return FALSE;
617   }
618 not_found:
619   {
620     GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL), GST_ERROR_SYSTEM);
621     return FALSE;
622   }
623 no_handle:
624   {
625     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
626         ("can't get raw1394 handle (%s)", g_strerror (errno)));
627     return FALSE;
628   }
629 no_ports:
630   {
631     raw1394_destroy_handle (src->handle);
632     src->handle = NULL;
633     GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
634         ("no ports available for raw1394"));
635     return FALSE;
636   }
637 cannot_set_port:
638   {
639     GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
640         ("can't set 1394 port %d", src->port));
641     return FALSE;
642   }
643 cannot_start:
644   {
645     raw1394_destroy_handle (src->handle);
646     src->handle = NULL;
647     iec61883_mpeg2_close (src->iec61883mpeg2);
648     src->iec61883mpeg2 = NULL;
649     GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
650         ("can't start 1394 iso receive"));
651     return FALSE;
652   }
653 cannot_initialise_dv:
654   {
655     raw1394_destroy_handle (src->handle);
656     src->handle = NULL;
657     GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
658         ("can't initialise iec61883 hdv"));
659     return FALSE;
660   }
661 }
662
663 static gboolean
664 gst_hdv1394src_stop (GstBaseSrc * bsrc)
665 {
666   GstHDV1394Src *src = GST_HDV1394SRC (bsrc);
667
668   close (READ_SOCKET (src));
669   close (WRITE_SOCKET (src));
670   READ_SOCKET (src) = -1;
671   WRITE_SOCKET (src) = -1;
672
673   iec61883_mpeg2_close (src->iec61883mpeg2);
674 #if 0
675   raw1394_stop_iso_rcv (src->handle, src->channel);
676 #endif
677
678   if (src->use_avc) {
679     raw1394handle_t avc_handle = raw1394_new_handle_on_port (src->port);
680
681     /* pause and stop the VCR */
682     if (avc_handle) {
683       if (!avc1394_vcr_is_recording (avc_handle, src->avc_node)
684           && (avc1394_vcr_is_playing (avc_handle, src->avc_node)
685               != AVC1394_VCR_OPERAND_PLAY_FORWARD_PAUSE))
686         avc1394_vcr_pause (avc_handle, src->avc_node);
687       avc1394_vcr_stop (avc_handle, src->avc_node);
688       raw1394_destroy_handle (avc_handle);
689     } else {
690       GST_WARNING_OBJECT (src, "Starting VCR via avc1394 failed: %s",
691           g_strerror (errno));
692     }
693   }
694
695   raw1394_destroy_handle (src->handle);
696
697   return TRUE;
698 }
699
700 static gboolean
701 gst_hdv1394src_unlock (GstBaseSrc * bsrc)
702 {
703   GstHDV1394Src *src = GST_HDV1394SRC (bsrc);
704
705   SEND_COMMAND (src, CONTROL_STOP);
706
707   return TRUE;
708 }
709
710 static void
711 gst_hdv1394src_update_device_name (GstHDV1394Src * src)
712 {
713   raw1394handle_t handle;
714   gint portcount, port, nodecount, node;
715   rom1394_directory directory;
716
717   g_free (src->device_name);
718   src->device_name = NULL;
719
720   GST_LOG_OBJECT (src, "updating device name for current GUID");
721
722   handle = raw1394_new_handle ();
723
724   if (handle == NULL)
725     goto gethandle_failed;
726
727   portcount = raw1394_get_port_info (handle, NULL, 0);
728   for (port = 0; port < portcount; port++) {
729     if (raw1394_set_port (handle, port) >= 0) {
730       nodecount = raw1394_get_nodecount (handle);
731       for (node = 0; node < nodecount; node++) {
732         if (src->guid == rom1394_get_guid (handle, node)) {
733           if (rom1394_get_directory (handle, node, &directory) >= 0) {
734             g_free (src->device_name);
735             src->device_name = g_strdup (directory.label);
736             rom1394_free_directory (&directory);
737             goto done;
738           } else {
739             GST_WARNING ("error reading rom directory for node %d", node);
740           }
741         }
742       }
743     }
744   }
745
746   src->device_name = g_strdup ("Unknown");      /* FIXME: translate? */
747
748 done:
749
750   raw1394_destroy_handle (handle);
751   return;
752
753 /* ERRORS */
754 gethandle_failed:
755   {
756     GST_WARNING ("failed to get raw1394 handle: %s", g_strerror (errno));
757     src->device_name = g_strdup ("Unknown");    /* FIXME: translate? */
758     return;
759   }
760 }
761
762 /*** GSTURIHANDLER INTERFACE *************************************************/
763
764 static GstURIType
765 gst_hdv1394src_uri_get_type (GType type)
766 {
767   return GST_URI_SRC;
768 }
769
770 static const gchar *const *
771 gst_hdv1394src_uri_get_protocols (GType type)
772 {
773   static const gchar *protocols[] = { (char *) "hdv", NULL };
774
775   return protocols;
776 }
777
778 static gchar *
779 gst_hdv1394src_uri_get_uri (GstURIHandler * handler)
780 {
781   GstHDV1394Src *gst_hdv1394src = GST_HDV1394SRC (handler);
782
783   return gst_hdv1394src->uri;
784 }
785
786 static gboolean
787 gst_hdv1394src_uri_set_uri (GstURIHandler * handler, const gchar * uri,
788     GError ** error)
789 {
790   gchar *protocol, *location;
791   gboolean ret = TRUE;
792   GstHDV1394Src *gst_hdv1394src = GST_HDV1394SRC (handler);
793
794   protocol = gst_uri_get_protocol (uri);
795   if (strcmp (protocol, "hdv") != 0) {
796     g_free (protocol);
797     g_set_error (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
798         "Invalid HDV URI");
799     return FALSE;
800   }
801   g_free (protocol);
802
803   location = gst_uri_get_location (uri);
804   if (location && *location != '\0')
805     gst_hdv1394src->port = strtol (location, NULL, 10);
806   else
807     gst_hdv1394src->port = DEFAULT_PORT;
808   g_free (location);
809   g_free (gst_hdv1394src->uri);
810   gst_hdv1394src->uri = g_strdup_printf ("hdv://%d", gst_hdv1394src->port);
811
812   return ret;
813 }
814
815 static void
816 gst_hdv1394src_uri_handler_init (gpointer g_iface, gpointer iface_data)
817 {
818   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
819
820   iface->get_type = gst_hdv1394src_uri_get_type;
821   iface->get_protocols = gst_hdv1394src_uri_get_protocols;
822   iface->get_uri = gst_hdv1394src_uri_get_uri;
823   iface->set_uri = gst_hdv1394src_uri_set_uri;
824 }