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