Debian Codegraph @

ext/raw1394/gstdv1394src.c

raw
1 /* GStreamer
2 * Copyright (C) <1999> Erik Walthinsen <[email protected]>
3 * <2000> Daniel Fischer <[email protected]>
4 * <2004> Wim Taymans <[email protected]>
5 * <2006> Zaheer Abbas Merali <zaheerabbas at merali dot org>
6 *
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.
11 *
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.
16 *
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., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21 */
22 /**
23 * SECTION:element-dv1394src
24 * @title: dv1394src
25 *
26 * Read DV (digital video) data from firewire port.
27 *
28 * ## Example launch line
29 * |[
30 * gst-launch-1.0 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).
33 *
34 */
35
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39 #include <unistd.h>
40 #include <poll.h>
41 #include <sys/socket.h>
42 #include <errno.h>
43 #include <fcntl.h>
44 #include <string.h>
45 #include <stdlib.h>
46
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>
53 #endif
54
55 #include <gst/gst.h>
56
57 #include "gstdv1394src.h"
58 #include "gst1394probe.h"
59 #include "gst1394clock.h"
60
61
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]
66
67 #define SEND_COMMAND(src, command) \
68 G_STMT_START { \
69 int G_GNUC_UNUSED _res; unsigned char c; c = command; \
70 _res = write (WRITE_SOCKET(src), &c, 1); \
71 } G_STMT_END
72
73 #define READ_COMMAND(src, command, res) \
74 G_STMT_START { \
75 res = read(READ_SOCKET(src), &command, 1); \
76 } G_STMT_END
77
78
79 GST_DEBUG_CATEGORY_STATIC (dv1394src_debug);
80 #define GST_CAT_DEFAULT (dv1394src_debug)
81
82 #define PAL_FRAMESIZE 144000
83 #define PAL_FRAMERATE 25
84
85 #define NTSC_FRAMESIZE 120000
86 #define NTSC_FRAMERATE 30
87
88 enum
89 {
90 SIGNAL_FRAME_DROPPED,
91 /* FILL ME */
92 LAST_SIGNAL
93 };
94
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
102
103 enum
104 {
105 PROP_0,
106 PROP_PORT,
107 PROP_CHANNEL,
108 PROP_CONSECUTIVE,
109 PROP_SKIP,
110 PROP_DROP_INCOMPLETE,
111 PROP_USE_AVC,
112 PROP_GUID,
113 PROP_DEVICE_NAME
114 };
115
116 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
117 GST_PAD_SRC,
118 GST_PAD_ALWAYS,
119 GST_STATIC_CAPS ("video/x-dv, "
120 "format = (string) { NTSC, PAL }, " "systemstream = (boolean) true")
121 );
122
123 static void gst_dv1394src_uri_handler_init (gpointer g_iface,
124 gpointer iface_data);
125
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);
131
132 static GstClock *gst_dv1394src_provide_clock (GstElement * element);
133 static GstStateChangeReturn gst_dv1394_src_change_state (GstElement * element,
134 GstStateChange transition);
135
136 static gboolean gst_dv1394src_start (GstBaseSrc * bsrc);
137 static gboolean gst_dv1394src_stop (GstBaseSrc * bsrc);
138 static gboolean gst_dv1394src_unlock (GstBaseSrc * bsrc);
139
140 static GstFlowReturn gst_dv1394src_create (GstPushSrc * psrc, GstBuffer ** buf);
141
142 static gboolean gst_dv1394src_query (GstBaseSrc * src, GstQuery * query);
143 static void gst_dv1394src_update_device_name (GstDV1394Src * src);
144
145 #define gst_dv1394src_parent_class parent_class
146 G_DEFINE_TYPE_WITH_CODE (GstDV1394Src, gst_dv1394src, GST_TYPE_PUSH_SRC,
147 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER,
148 gst_dv1394src_uri_handler_init));
149 GST_ELEMENT_REGISTER_DEFINE (dv1394src, "dv1394src", GST_RANK_NONE,
150 GST_TYPE_DV1394SRC);
151
152 static guint gst_dv1394src_signals[LAST_SIGNAL] = { 0 };
153
154 static void
155 gst_dv1394src_class_init (GstDV1394SrcClass * klass)
156 {
157 GObjectClass *gobject_class;
158 GstElementClass *gstelement_class;
159 GstBaseSrcClass *gstbasesrc_class;
160 GstPushSrcClass *gstpushsrc_class;
161
162 gobject_class = (GObjectClass *) klass;
163 gstelement_class = (GstElementClass *) klass;
164 gstbasesrc_class = (GstBaseSrcClass *) klass;
165 gstpushsrc_class = (GstPushSrcClass *) klass;
166
167 gobject_class->set_property = gst_dv1394src_set_property;
168 gobject_class->get_property = gst_dv1394src_get_property;
169 gobject_class->dispose = gst_dv1394src_dispose;
170
171 gstelement_class->provide_clock = gst_dv1394src_provide_clock;
172 gstelement_class->change_state = gst_dv1394_src_change_state;
173
174 gst_dv1394src_signals[SIGNAL_FRAME_DROPPED] =
175 g_signal_new ("frame-dropped", G_TYPE_FROM_CLASS (klass),
176 G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, G_TYPE_NONE, 0);
177
178 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_PORT,
179 g_param_spec_int ("port", "Port", "Port number (-1 automatic)",
180 -1, 16, DEFAULT_PORT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
181 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_CHANNEL,
182 g_param_spec_int ("channel", "Channel", "Channel number for listening",
183 0, 64, DEFAULT_CHANNEL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
184 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_CONSECUTIVE,
185 g_param_spec_int ("consecutive", "consecutive frames",
186 "send n consecutive frames after skipping", 1, G_MAXINT,
187 DEFAULT_CONSECUTIVE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
188 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SKIP,
189 g_param_spec_int ("skip", "skip frames", "skip n frames",
190 0, G_MAXINT, DEFAULT_SKIP,
191 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
192 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_DROP_INCOMPLETE,
193 g_param_spec_boolean ("drop-incomplete", "drop incomplete",
194 "drop incomplete frames", DEFAULT_DROP_INCOMPLETE,
195 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
196 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_USE_AVC,
197 g_param_spec_boolean ("use-avc", "Use AV/C", "Use AV/C VTR control",
198 DEFAULT_USE_AVC, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
199 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_GUID,
200 g_param_spec_uint64 ("guid", "GUID",
201 "select one of multiple DV devices by its GUID. use a hexadecimal "
202 "like 0xhhhhhhhhhhhhhhhh. (0 = no guid)", 0, G_MAXUINT64,
203 DEFAULT_GUID, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
204 /**
205 * GstDV1394Src:device-name:
206 *
207 * Descriptive name of the currently opened device
208 */
209 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_DEVICE_NAME,
210 g_param_spec_string ("device-name", "device name",
211 "user-friendly name of the device", "Default",
212 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
213
214 gstbasesrc_class->negotiate = NULL;
215 gstbasesrc_class->start = gst_dv1394src_start;
216 gstbasesrc_class->stop = gst_dv1394src_stop;
217 gstbasesrc_class->unlock = gst_dv1394src_unlock;
218 gstbasesrc_class->query = gst_dv1394src_query;
219
220 gstpushsrc_class->create = gst_dv1394src_create;
221
222 gst_element_class_add_static_pad_template (gstelement_class, &src_factory);
223
224 gst_element_class_set_static_metadata (gstelement_class,
225 "Firewire (1394) DV video source", "Source/Video",
226 "Source for DV video data from firewire port",
227 "Erik Walthinsen <[email protected]>, "
228 "Daniel Fischer <[email protected]>, " "Wim Taymans <[email protected]>, "
229 "Zaheer Abbas Merali <zaheerabbas at merali dot org>");
230
231 GST_DEBUG_CATEGORY_INIT (dv1394src_debug, "dv1394src", 0,
232 "DV firewire source");
233 }
234
235 static void
236 gst_dv1394src_init (GstDV1394Src * dv1394src)
237 {
238 GstPad *srcpad = GST_BASE_SRC_PAD (dv1394src);
239
240 gst_base_src_set_live (GST_BASE_SRC (dv1394src), TRUE);
241 gst_base_src_set_format (GST_BASE_SRC (dv1394src), GST_FORMAT_TIME);
242 gst_base_src_set_do_timestamp (GST_BASE_SRC (dv1394src), TRUE);
243 gst_pad_use_fixed_caps (srcpad);
244
245 dv1394src->port = DEFAULT_PORT;
246 dv1394src->channel = DEFAULT_CHANNEL;
247
248 dv1394src->consecutive = DEFAULT_CONSECUTIVE;
249 dv1394src->skip = DEFAULT_SKIP;
250 dv1394src->drop_incomplete = DEFAULT_DROP_INCOMPLETE;
251 dv1394src->use_avc = DEFAULT_USE_AVC;
252 dv1394src->guid = DEFAULT_GUID;
253 dv1394src->uri = g_strdup_printf ("dv://%d", dv1394src->port);
254 dv1394src->device_name = g_strdup_printf ("Default");
255
256 READ_SOCKET (dv1394src) = -1;
257 WRITE_SOCKET (dv1394src) = -1;
258
259 /* initialized when first header received */
260 dv1394src->frame_size = 0;
261
262 dv1394src->buf = NULL;
263 dv1394src->frame = NULL;
264 dv1394src->frame_sequence = 0;
265
266 dv1394src->provided_clock = gst_1394_clock_new ("dv1394clock");
267 }
268
269 static void
270 gst_dv1394src_dispose (GObject * object)
271 {
272 GstDV1394Src *src = GST_DV1394SRC (object);
273
274 if (src->provided_clock) {
275 g_object_unref (src->provided_clock);
276 }
277
278 g_free (src->uri);
279 src->uri = NULL;
280
281 g_free (src->device_name);
282 src->device_name = NULL;
283
284 G_OBJECT_CLASS (parent_class)->dispose (object);
285 }
286
287 static void
288 gst_dv1394src_set_property (GObject * object, guint prop_id,
289 const GValue * value, GParamSpec * pspec)
290 {
291 GstDV1394Src *filter = GST_DV1394SRC (object);
292
293 switch (prop_id) {
294 case PROP_PORT:
295 filter->port = g_value_get_int (value);
296 g_free (filter->uri);
297 filter->uri = g_strdup_printf ("dv://%d", filter->port);
298 break;
299 case PROP_CHANNEL:
300 filter->channel = g_value_get_int (value);
301 break;
302 case PROP_SKIP:
303 filter->skip = g_value_get_int (value);
304 break;
305 case PROP_CONSECUTIVE:
306 filter->consecutive = g_value_get_int (value);
307 break;
308 case PROP_DROP_INCOMPLETE:
309 filter->drop_incomplete = g_value_get_boolean (value);
310 break;
311 case PROP_USE_AVC:
312 filter->use_avc = g_value_get_boolean (value);
313 break;
314 case PROP_GUID:
315 filter->guid = g_value_get_uint64 (value);
316 gst_dv1394src_update_device_name (filter);
317 break;
318 default:
319 break;
320 }
321 }
322
323 static void
324 gst_dv1394src_get_property (GObject * object, guint prop_id, GValue * value,
325 GParamSpec * pspec)
326 {
327 GstDV1394Src *filter = GST_DV1394SRC (object);
328
329 switch (prop_id) {
330 case PROP_PORT:
331 g_value_set_int (value, filter->port);
332 break;
333 case PROP_CHANNEL:
334 g_value_set_int (value, filter->channel);
335 break;
336 case PROP_SKIP:
337 g_value_set_int (value, filter->skip);
338 break;
339 case PROP_CONSECUTIVE:
340 g_value_set_int (value, filter->consecutive);
341 break;
342 case PROP_DROP_INCOMPLETE:
343 g_value_set_boolean (value, filter->drop_incomplete);
344 break;
345 case PROP_USE_AVC:
346 g_value_set_boolean (value, filter->use_avc);
347 break;
348 case PROP_GUID:
349 g_value_set_uint64 (value, filter->guid);
350 break;
351 case PROP_DEVICE_NAME:
352 g_value_set_string (value, filter->device_name);
353 break;
354 default:
355 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
356 break;
357 }
358 }
359
360 static GstClock *
361 gst_dv1394src_provide_clock (GstElement * element)
362 {
363 GstDV1394Src *dv1394src = GST_DV1394SRC (element);
364
365 return GST_CLOCK_CAST (gst_object_ref (dv1394src->provided_clock));
366 }
367
368 static GstStateChangeReturn
369 gst_dv1394_src_change_state (GstElement * element, GstStateChange transition)
370 {
371 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
372 GstDV1394Src *src = GST_DV1394SRC (element);
373
374 switch (transition) {
375 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
376 gst_element_post_message (element,
377 gst_message_new_clock_lost (GST_OBJECT_CAST (element),
378 GST_CLOCK_CAST (src->provided_clock)));
379 break;
380 default:
381 break;
382 }
383
384 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
385 if (ret == GST_STATE_CHANGE_FAILURE)
386 return ret;
387
388 switch (transition) {
389 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
390 gst_element_post_message (element,
391 gst_message_new_clock_provide (GST_OBJECT_CAST (element),
392 GST_CLOCK_CAST (src->provided_clock), TRUE));
393 break;
394 default:
395 break;
396 }
397
398 return ret;
399 }
400
401 #ifdef HAVE_LIBIEC61883
402 static GstDV1394Src *
403 gst_dv1394src_from_raw1394handle (raw1394handle_t handle)
404 {
405 iec61883_dv_t dv = (iec61883_dv_t) raw1394_get_userdata (handle);
406 iec61883_dv_fb_t dv_fb =
407 (iec61883_dv_fb_t) iec61883_dv_get_callback_data (dv);
408 return GST_DV1394SRC (iec61883_dv_fb_get_callback_data (dv_fb));
409 }
410 #else /* HAVE_LIBIEC61883 */
411 static GstDV1394Src *
412 gst_dv1394src_from_raw1394handle (raw1394handle_t handle)
413 {
414 return GST_DV1394SRC (raw1394_get_userdata (handle));
415 }
416 #endif /* HAVE_LIBIEC61883 */
417
418 #ifdef HAVE_LIBIEC61883
419 static int
420 gst_dv1394src_iec61883_receive (unsigned char *data, int len,
421 int complete, void *cbdata)
422 {
423 GstDV1394Src *dv1394src = GST_DV1394SRC (cbdata);
424
425 if (G_UNLIKELY (!gst_pad_has_current_caps (GST_BASE_SRC_PAD (dv1394src)))) {
426 GstCaps *caps;
427 unsigned char *p = data;
428
429 // figure format (NTSC/PAL)
430 if (p[3] & 0x80) {
431 // PAL
432 dv1394src->frame_size = PAL_FRAMESIZE;
433 dv1394src->frame_rate = PAL_FRAMERATE;
434 GST_DEBUG ("PAL data");
435 caps = gst_caps_new_simple ("video/x-dv",
436 "format", G_TYPE_STRING, "PAL",
437 "systemstream", G_TYPE_BOOLEAN, TRUE, NULL);
438 } else {
439 // NTSC (untested)
440 dv1394src->frame_size = NTSC_FRAMESIZE;
441 dv1394src->frame_rate = NTSC_FRAMERATE;
442 GST_DEBUG
443 ("NTSC data [untested] - please report success/failure to <[email protected]>");
444 caps = gst_caps_new_simple ("video/x-dv",
445 "format", G_TYPE_STRING, "NTSC",
446 "systemstream", G_TYPE_BOOLEAN, TRUE, NULL);
447 }
448 gst_pad_set_caps (GST_BASE_SRC_PAD (dv1394src), caps);
449 gst_caps_unref (caps);
450 }
451
452 dv1394src->frame = NULL;
453 if (G_LIKELY ((dv1394src->frame_sequence + 1) % (dv1394src->skip +
454 dv1394src->consecutive) < dv1394src->consecutive)) {
455 if (complete && len == dv1394src->frame_size) {
456 GstBuffer *buf;
457
458 buf = gst_buffer_new_and_alloc (dv1394src->frame_size);
459
460 GST_BUFFER_OFFSET (buf) = dv1394src->frame_sequence;
461 gst_buffer_fill (buf, 0, data, len);
462 dv1394src->buf = buf;
463 }
464 }
465 dv1394src->frame_sequence++;
466 return 0;
467 }
468
469 #else
470 static int
471 gst_dv1394src_iso_receive (raw1394handle_t handle, int channel, size_t len,
472 quadlet_t * data)
473 {
474 GstDV1394Src *dv1394src = gst_dv1394src_from_raw1394handle (handle);
475
476 if (len > 16) {
477 /*
478 the following code taken from kino-0.51 (Dan Dennedy/Charles Yates)
479 Kindly relicensed under the LGPL. See the commit log for version 1.6 of
480 this file in CVS.
481 */
482 unsigned char *p = (unsigned char *) &data[3];
483
484 int section_type = p[0] >> 5; /* section type is in bits 5 - 7 */
485 int dif_sequence = p[1] >> 4; /* dif sequence number is in bits 4 - 7 */
486 int dif_block = p[2];
487
488 /* if we are at the beginning of a frame,
489 we set buf=frame, and alloc a new buffer for frame
490 */
491 if (section_type == 0 && dif_sequence == 0) { // dif header
492 if (!GST_PAD_CAPS (GST_BASE_SRC_PAD (dv1394src))) {
493 GstCaps *caps;
494
495 // figure format (NTSC/PAL)
496 if (p[3] & 0x80) {
497 // PAL
498 dv1394src->frame_size = PAL_FRAMESIZE;
499 dv1394src->frame_rate = PAL_FRAMERATE;
500 GST_DEBUG ("PAL data");
501 caps = gst_caps_new_simple ("video/x-dv",
502 "format", G_TYPE_STRING, "PAL",
503 "systemstream", G_TYPE_BOOLEAN, TRUE, NULL);
504 } else {
505 // NTSC (untested)
506 dv1394src->frame_size = NTSC_FRAMESIZE;
507 dv1394src->frame_rate = NTSC_FRAMERATE;
508 GST_DEBUG
509 ("NTSC data [untested] - please report success/failure to <[email protected]>");
510 caps = gst_caps_new_simple ("video/x-dv",
511 "format", G_TYPE_STRING, "NTSC",
512 "systemstream", G_TYPE_BOOLEAN, TRUE, NULL);
513 }
514 gst_pad_set_caps (GST_BASE_SRC_PAD (dv1394src), caps);
515 gst_caps_unref (caps);
516 }
517 // drop last frame when not complete
518 if (!dv1394src->drop_incomplete
519 || dv1394src->bytes_in_frame == dv1394src->frame_size) {
520 dv1394src->buf = dv1394src->frame;
521 } else {
522 GST_INFO_OBJECT (GST_ELEMENT (dv1394src), "incomplete frame dropped");
523 g_signal_emit (G_OBJECT (dv1394src),
524 gst_dv1394src_signals[SIGNAL_FRAME_DROPPED], 0);
525 if (dv1394src->frame) {
526 gst_buffer_unref (dv1394src->frame);
527 }
528 }
529 if ((dv1394src->frame_sequence + 1) % (dv1394src->skip +
530 dv1394src->consecutive) < dv1394src->consecutive) {
531 GstBuffer *buf;
532 gint64 i64;
533
534 buf = gst_buffer_new_and_alloc (dv1394src->frame_size);
535
536 /* fill in offset, duration, timestamp */
537 GST_BUFFER_OFFSET (buf) = dv1394src->frame_sequence;
538 dv1394src->frame = buf;
539 }
540 dv1394src->frame_sequence++;
541 dv1394src->bytes_in_frame = 0;
542 }
543
544 if (dv1394src->frame != NULL) {
545 guint8 *data = GST_BUFFER_DATA (dv1394src->frame);
546
547 switch (section_type) {
548 case 0: /* 1 Header block */
549 /* p[3] |= 0x80; // hack to force PAL data */
550 memcpy (data + dif_sequence * 150 * 80, p, 480);
551 break;
552
553 case 1: /* 2 Subcode blocks */
554 memcpy (data + dif_sequence * 150 * 80 + (1 + dif_block) * 80, p,
555 480);
556 break;
557
558 case 2: /* 3 VAUX blocks */
559 memcpy (data + dif_sequence * 150 * 80 + (3 + dif_block) * 80, p,
560 480);
561 break;
562
563 case 3: /* 9 Audio blocks interleaved with video */
564 memcpy (data + dif_sequence * 150 * 80 + (6 + dif_block * 16) * 80, p,
565 480);
566 break;
567
568 case 4: /* 135 Video blocks interleaved with audio */
569 memcpy (data + dif_sequence * 150 * 80 + (7 + (dif_block / 15) +
570 dif_block) * 80, p, 480);
571 break;
572
573 default: /* we can't handle any other data */
574 break;
575 }
576 dv1394src->bytes_in_frame += 480;
577 }
578 }
579
580 return 0;
581 }
582 #endif
583 /*
584 * When an ieee1394 bus reset happens, usually a device has been removed
585 * or added. We send a message on the message bus with the node count
586 * and whether the capture device used in this element connected, disconnected
587 * or was unchanged
588 * Message structure:
589 * nodecount - integer with number of nodes on bus
590 * current-device-change - integer (1 if device connected, 0 if no change to
591 * current device status, -1 if device disconnected)
592 */
593 static int
594 gst_dv1394src_bus_reset (raw1394handle_t handle, unsigned int generation)
595 {
596 GstDV1394Src *src;
597 gint nodecount;
598 GstMessage *message;
599 GstStructure *structure;
600 gint current_device_change;
601 gint i;
602
603 src = gst_dv1394src_from_raw1394handle (handle);
604
605 GST_INFO_OBJECT (src, "have bus reset");
606
607 /* update generation - told to do so by docs */
608 raw1394_update_generation (handle, generation);
609 nodecount = raw1394_get_nodecount (handle);
610 /* allocate memory for portinfo */
611
612 /* current_device_change is -1 if camera disconnected, 0 if other device
613 * connected or 1 if camera has now connected */
614 current_device_change = -1;
615 for (i = 0; i < nodecount; i++) {
616 if (src->guid == rom1394_get_guid (handle, i)) {
617 /* Camera is with us */
618 GST_DEBUG ("Camera is with us");
619 if (!src->connected) {
620 current_device_change = 1;
621 src->connected = TRUE;
622 } else
623 current_device_change = 0;
624 }
625 }
626 if (src->connected && current_device_change == -1) {
627 GST_DEBUG ("Camera has disconnected");
628 src->connected = FALSE;
629 } else if (!src->connected && current_device_change == -1) {
630 GST_DEBUG ("Camera is still not with us");
631 current_device_change = 0;
632 }
633
634 structure = gst_structure_new ("ieee1394-bus-reset", "nodecount", G_TYPE_INT,
635 nodecount, "current-device-change", G_TYPE_INT, current_device_change,
636 NULL);
637 message = gst_message_new_element (GST_OBJECT (src), structure);
638 gst_element_post_message (GST_ELEMENT (src), message);
639
640 return 0;
641 }
642
643 static GstFlowReturn
644 gst_dv1394src_create (GstPushSrc * psrc, GstBuffer ** buf)
645 {
646 GstDV1394Src *dv1394src = GST_DV1394SRC (psrc);
647 struct pollfd pollfds[2];
648
649 pollfds[0].fd = raw1394_get_fd (dv1394src->handle);
650 pollfds[0].events = POLLIN | POLLERR | POLLHUP | POLLPRI;
651 pollfds[1].fd = READ_SOCKET (dv1394src);
652 pollfds[1].events = POLLIN | POLLERR | POLLHUP | POLLPRI;
653
654 if (G_UNLIKELY (dv1394src->buf)) {
655 /* maybe we had an error before, and there's a stale buffer? */
656 gst_buffer_unref (dv1394src->buf);
657 dv1394src->buf = NULL;
658 }
659
660 while (TRUE) {
661 int res = poll (pollfds, 2, -1);
662
663 if (G_UNLIKELY (res < 0)) {
664 if (errno == EAGAIN || errno == EINTR)
665 continue;
666 else
667 goto error_while_polling;
668 }
669
670 if (G_UNLIKELY (pollfds[1].revents)) {
671 char command;
672
673 if (pollfds[1].revents & POLLIN)
674 READ_COMMAND (dv1394src, command, res);
675
676 goto told_to_stop;
677 } else if (G_LIKELY (pollfds[0].revents & POLLIN)) {
678 /* shouldn't block in theory */
679 raw1394_loop_iterate (dv1394src->handle);
680
681 if (dv1394src->buf)
682 break;
683 }
684 }
685
686 g_assert (dv1394src->buf);
687
688 *buf = dv1394src->buf;
689 dv1394src->buf = NULL;
690 return GST_FLOW_OK;
691
692 error_while_polling:
693 {
694 GST_ELEMENT_ERROR (dv1394src, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
695 return GST_FLOW_EOS;
696 }
697 told_to_stop:
698 {
699 GST_DEBUG_OBJECT (dv1394src, "told to stop, shutting down");
700 return GST_FLOW_FLUSHING;
701 }
702 }
703
704 static int
705 gst_dv1394src_discover_avc_node (GstDV1394Src * src)
706 {
707 int node = -1;
708 int i, j = 0;
709 int m = src->num_ports;
710
711 if (src->port >= 0) {
712 /* search on explicit port */
713 j = src->port;
714 m = j + 1;
715 }
716
717 /* loop over all our ports */
718 for (; j < m && node == -1; j++) {
719 raw1394handle_t handle;
720 struct raw1394_portinfo pinf[16];
721
722 /* open the port */
723 handle = raw1394_new_handle ();
724 if (!handle) {
725 GST_WARNING ("raw1394 - failed to get handle: %s.\n", strerror (errno));
726 continue;
727 }
728 if (raw1394_get_port_info (handle, pinf, 16) < 0) {
729 GST_WARNING ("raw1394 - failed to get port info: %s.\n",
730 strerror (errno));
731 goto next;
732 }
733
734 /* tell raw1394 which host adapter to use */
735 if (raw1394_set_port (handle, j) < 0) {
736 GST_WARNING ("raw1394 - failed to set set port: %s.\n", strerror (errno));
737 goto next;
738 }
739
740 /* now loop over all the nodes */
741 for (i = 0; i < raw1394_get_nodecount (handle); i++) {
742 /* are we looking for an explicit GUID ? */
743 if (src->guid != 0) {
744 if (src->guid == rom1394_get_guid (handle, i)) {
745 node = i;
746 src->port = j;
747 g_free (src->uri);
748 src->uri = g_strdup_printf ("dv://%d", src->port);
749 break;
750 }
751 } else {
752 rom1394_directory rom_dir;
753
754 /* select first AV/C Tape Recorder Player node */
755 if (rom1394_get_directory (handle, i, &rom_dir) < 0) {
756 GST_WARNING ("error reading config rom directory for node %d\n", i);
757 continue;
758 }
759 if ((rom1394_get_node_type (&rom_dir) == ROM1394_NODE_TYPE_AVC) &&
760 avc1394_check_subunit_type (handle, i, AVC1394_SUBUNIT_TYPE_VCR)) {
761 node = i;
762 src->port = j;
763 src->guid = rom1394_get_guid (handle, i);
764 g_free (src->uri);
765 src->uri = g_strdup_printf ("dv://%d", src->port);
766 g_free (src->device_name);
767 src->device_name = g_strdup (rom_dir.label);
768 break;
769 }
770 rom1394_free_directory (&rom_dir);
771 }
772 }
773 next:
774 raw1394_destroy_handle (handle);
775 }
776 return node;
777 }
778
779 static gboolean
780 gst_dv1394src_start (GstBaseSrc * bsrc)
781 {
782 GstDV1394Src *src = GST_DV1394SRC (bsrc);
783 int control_sock[2];
784
785 src->connected = FALSE;
786
787 if (socketpair (PF_UNIX, SOCK_STREAM, 0, control_sock) < 0)
788 goto socket_pair;
789
790 READ_SOCKET (src) = control_sock[0];
791 WRITE_SOCKET (src) = control_sock[1];
792
793 if (fcntl (READ_SOCKET (src), F_SETFL, O_NONBLOCK) < 0)
794 GST_ERROR_OBJECT (src, "failed to make read socket non-blocking: %s",
795 g_strerror (errno));
796 if (fcntl (WRITE_SOCKET (src), F_SETFL, O_NONBLOCK) < 0)
797 GST_ERROR_OBJECT (src, "failed to make write socket non-blocking: %s",
798 g_strerror (errno));
799
800 src->handle = raw1394_new_handle ();
801
802 if (!src->handle) {
803 if (errno == EACCES)
804 goto permission_denied;
805 else if (errno == ENOENT)
806 goto not_found;
807 else
808 goto no_handle;
809 }
810
811 src->num_ports = raw1394_get_port_info (src->handle, src->pinfo, 16);
812
813 if (src->num_ports == 0)
814 goto no_ports;
815
816 if (src->use_avc || src->port == -1)
817 src->avc_node = gst_dv1394src_discover_avc_node (src);
818
819 /* lets destroy handle and create one on port
820 this is more reliable than setting port on
821 the existing handle */
822 raw1394_destroy_handle (src->handle);
823 src->handle = raw1394_new_handle_on_port (src->port);
824 if (!src->handle)
825 goto cannot_set_port;
826
827 raw1394_set_userdata (src->handle, src);
828 raw1394_set_bus_reset_handler (src->handle, gst_dv1394src_bus_reset);
829
830 #ifdef HAVE_LIBIEC61883
831 if ((src->iec61883dv =
832 iec61883_dv_fb_init (src->handle,
833 gst_dv1394src_iec61883_receive, src)) == NULL)
834 goto cannot_initialise_dv;
835
836 #else
837 raw1394_set_iso_handler (src->handle, src->channel,
838 gst_dv1394src_iso_receive);
839 #endif
840
841 GST_DEBUG_OBJECT (src, "successfully opened up 1394 connection");
842 src->connected = TRUE;
843
844 #ifdef HAVE_LIBIEC61883
845 if (iec61883_dv_fb_start (src->iec61883dv, src->channel) != 0)
846 goto cannot_start;
847 #else
848 if (raw1394_start_iso_rcv (src->handle, src->channel) < 0)
849 goto cannot_start;
850 #endif
851
852 if (src->use_avc) {
853 raw1394handle_t avc_handle = raw1394_new_handle_on_port (src->port);
854
855 /* start the VCR */
856 if (avc_handle) {
857 if (!avc1394_vcr_is_recording (avc_handle, src->avc_node)
858 && avc1394_vcr_is_playing (avc_handle, src->avc_node)
859 != AVC1394_VCR_OPERAND_PLAY_FORWARD)
860 avc1394_vcr_play (avc_handle, src->avc_node);
861 raw1394_destroy_handle (avc_handle);
862 } else {
863 GST_WARNING_OBJECT (src, "Starting VCR via avc1394 failed: %s",
864 g_strerror (errno));
865 }
866 }
867
868 gst_1394_clock_set_handle (src->provided_clock, src->handle);
869
870 return TRUE;
871
872 socket_pair:
873 {
874 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
875 GST_ERROR_SYSTEM);
876 return FALSE;
877 }
878 permission_denied:
879 {
880 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL), GST_ERROR_SYSTEM);
881 return FALSE;
882 }
883 not_found:
884 {
885 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL), GST_ERROR_SYSTEM);
886 return FALSE;
887 }
888 no_handle:
889 {
890 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
891 ("can't get raw1394 handle (%s)", g_strerror (errno)));
892 return FALSE;
893 }
894 no_ports:
895 {
896 raw1394_destroy_handle (src->handle);
897 src->handle = NULL;
898 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
899 ("no ports available for raw1394"));
900 return FALSE;
901 }
902 cannot_set_port:
903 {
904 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
905 ("can't set 1394 port %d", src->port));
906 return FALSE;
907 }
908 cannot_start:
909 {
910 raw1394_destroy_handle (src->handle);
911 src->handle = NULL;
912 #ifdef HAVE_LIBIEC61883
913 iec61883_dv_fb_close (src->iec61883dv);
914 src->iec61883dv = NULL;
915 #endif
916 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
917 ("can't start 1394 iso receive"));
918 return FALSE;
919 }
920 #ifdef HAVE_LIBIEC61883
921 cannot_initialise_dv:
922 {
923 raw1394_destroy_handle (src->handle);
924 src->handle = NULL;
925 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
926 ("can't initialise iec61883 dv"));
927 return FALSE;
928 }
929 #endif
930 }
931
932 static gboolean
933 gst_dv1394src_stop (GstBaseSrc * bsrc)
934 {
935 GstDV1394Src *src = GST_DV1394SRC (bsrc);
936
937 close (READ_SOCKET (src));
938 close (WRITE_SOCKET (src));
939 READ_SOCKET (src) = -1;
940 WRITE_SOCKET (src) = -1;
941 #ifdef HAVE_LIBIEC61883
942 iec61883_dv_fb_close (src->iec61883dv);
943 #else
944 raw1394_stop_iso_rcv (src->handle, src->channel);
945 #endif
946
947 if (src->use_avc) {
948 raw1394handle_t avc_handle = raw1394_new_handle_on_port (src->port);
949
950 /* pause and stop the VCR */
951 if (avc_handle) {
952 if (!avc1394_vcr_is_recording (avc_handle, src->avc_node)
953 && (avc1394_vcr_is_playing (avc_handle, src->avc_node)
954 != AVC1394_VCR_OPERAND_PLAY_FORWARD_PAUSE))
955 avc1394_vcr_pause (avc_handle, src->avc_node);
956 avc1394_vcr_stop (avc_handle, src->avc_node);
957 raw1394_destroy_handle (avc_handle);
958 } else {
959 GST_WARNING_OBJECT (src, "Starting VCR via avc1394 failed: %s",
960 g_strerror (errno));
961 }
962 }
963
964 gst_1394_clock_unset_handle (src->provided_clock);
965
966 raw1394_destroy_handle (src->handle);
967
968 return TRUE;
969 }
970
971 static gboolean
972 gst_dv1394src_unlock (GstBaseSrc * bsrc)
973 {
974 GstDV1394Src *src = GST_DV1394SRC (bsrc);
975
976 SEND_COMMAND (src, CONTROL_STOP);
977
978 return TRUE;
979 }
980
981 static gboolean
982 gst_dv1394src_query (GstBaseSrc * basesrc, GstQuery * query)
983 {
984 switch (GST_QUERY_TYPE (query)) {
985 case GST_QUERY_LATENCY:
986 {
987 gst_query_set_latency (query, TRUE, GST_SECOND / 25, GST_SECOND / 25);
988 }
989 break;
990 default:
991 goto not_supported;
992 }
993
994 return TRUE;
995
996 not_supported:
997 return GST_BASE_SRC_CLASS (parent_class)->query (basesrc, query);
998 }
999
1000 static void
1001 gst_dv1394src_update_device_name (GstDV1394Src * src)
1002 {
1003 raw1394handle_t handle;
1004 gint portcount, port, nodecount, node;
1005 rom1394_directory directory;
1006
1007 g_free (src->device_name);
1008 src->device_name = NULL;
1009
1010 GST_LOG_OBJECT (src, "updating device name for current GUID");
1011
1012 handle = raw1394_new_handle ();
1013
1014 if (handle == NULL)
1015 goto gethandle_failed;
1016
1017 portcount = raw1394_get_port_info (handle, NULL, 0);
1018 for (port = 0; port < portcount; port++) {
1019 if (raw1394_set_port (handle, port) >= 0) {
1020 nodecount = raw1394_get_nodecount (handle);
1021 for (node = 0; node < nodecount; node++) {
1022 if (src->guid == rom1394_get_guid (handle, node)) {
1023 if (rom1394_get_directory (handle, node, &directory) >= 0) {
1024 g_free (src->device_name);
1025 src->device_name = g_strdup (directory.label);
1026 rom1394_free_directory (&directory);
1027 goto done;
1028 } else {
1029 GST_WARNING ("error reading rom directory for node %d", node);
1030 }
1031 }
1032 }
1033 }
1034 }
1035
1036 src->device_name = g_strdup ("Unknown"); /* FIXME: translate? */
1037
1038 done:
1039
1040 raw1394_destroy_handle (handle);
1041 return;
1042
1043 /* ERRORS */
1044 gethandle_failed:
1045 {
1046 GST_WARNING ("failed to get raw1394 handle: %s", g_strerror (errno));
1047 src->device_name = g_strdup ("Unknown"); /* FIXME: translate? */
1048 return;
1049 }
1050 }
1051
1052 /*** GSTURIHANDLER INTERFACE *************************************************/
1053
1054 static GstURIType
1055 gst_dv1394src_uri_get_type (GType type)
1056 {
1057 return GST_URI_SRC;
1058 }
1059
1060 static const gchar *const *
1061 gst_dv1394src_uri_get_protocols (GType type)
1062 {
1063 static const gchar *protocols[] = { (char *) "dv", NULL };
1064
1065 return protocols;
1066 }
1067
1068 static gchar *
1069 gst_dv1394src_uri_get_uri (GstURIHandler * handler)
1070 {
1071 GstDV1394Src *gst_dv1394src = GST_DV1394SRC (handler);
1072
1073 return gst_dv1394src->uri;
1074 }
1075
1076 static gboolean
1077 gst_dv1394src_uri_set_uri (GstURIHandler * handler, const gchar * uri,
1078 GError ** error)
1079 {
1080 gchar *protocol, *location;
1081 gboolean ret = TRUE;
1082 GstDV1394Src *gst_dv1394src = GST_DV1394SRC (handler);
1083
1084 protocol = gst_uri_get_protocol (uri);
1085 if (strcmp (protocol, "dv") != 0) {
1086 g_free (protocol);
1087 g_set_error (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI, "Invalid DV URI");
1088 return FALSE;
1089 }
1090 g_free (protocol);
1091
1092 location = gst_uri_get_location (uri);
1093 if (location && *location != '\0')
1094 gst_dv1394src->port = strtol (location, NULL, 10);
1095 else
1096 gst_dv1394src->port = DEFAULT_PORT;
1097 g_free (location);
1098 g_free (gst_dv1394src->uri);
1099 gst_dv1394src->uri = g_strdup_printf ("dv://%d", gst_dv1394src->port);
1100
1101 return ret;
1102 }
1103
1104 static void
1105 gst_dv1394src_uri_handler_init (gpointer g_iface, gpointer iface_data)
1106 {
1107 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
1108
1109 iface->get_type = gst_dv1394src_uri_get_type;
1110 iface->get_protocols = gst_dv1394src_uri_get_protocols;
1111 iface->get_uri = gst_dv1394src_uri_get_uri;
1112 iface->set_uri = gst_dv1394src_uri_set_uri;
1113 }