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