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