74e79124e3027004c78ac5af57f9f4cf569a0769
[platform/upstream/gstreamer.git] / ext / raw1394 / gstdv1394src.c
1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: t; c-basic-offset: 2 -*- */
2 /* GStreamer
3  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
4  *               <2000> Daniel Fischer <dan@f3c.com>
5  *               <2004> Wim Taymans <wim@fluendo.com>
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 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 #include <gst/gst.h>
27 #include <string.h>
28 #include <libavc1394/avc1394.h>
29 #include <libavc1394/avc1394_vcr.h>
30 #include <libavc1394/rom1394.h>
31
32 #include "gstdv1394src.h"
33
34 GST_DEBUG_CATEGORY_STATIC (dv1394src_debug);
35 #define GST_CAT_DEFAULT (dv1394src_debug)
36
37 #define PAL_FRAMESIZE 144000
38 #define PAL_FRAMERATE 25
39
40 #define NTSC_FRAMESIZE 120000
41 #define NTSC_FRAMERATE 30
42
43 /* Filter signals and args */
44 enum
45 {
46   SIGNAL_FRAME_DROPPED,
47   /* FILL ME */
48   LAST_SIGNAL
49 };
50
51 #define DEFAULT_PORT            -1
52 #define DEFAULT_CHANNEL         63
53 #define DEFAULT_CONSECUTIVE     1
54 #define DEFAULT_SKIP            0
55 #define DEFAULT_DROP_INCOMPLETE TRUE
56 #define DEFAULT_USE_AVC         TRUE
57 #define DEFAULT_GUID            0
58
59 enum
60 {
61   ARG_0,
62   ARG_PORT,
63   ARG_CHANNEL,
64   ARG_CONSECUTIVE,
65   ARG_SKIP,
66   ARG_DROP_INCOMPLETE,
67   ARG_USE_AVC,
68   ARG_GUID
69 };
70
71 static GstElementDetails gst_dv1394src_details =
72 GST_ELEMENT_DETAILS ("Firewire (1394) DV Source",
73     "Source/Video",
74     "Source for DV video data from firewire port",
75     "Erik Walthinsen <omega@temple-baptist.com>\n"
76     "Daniel Fischer <dan@f3c.com>\n" "Wim Taymans <wim@fluendo.com>");
77
78 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
79     GST_PAD_SRC,
80     GST_PAD_ALWAYS,
81     GST_STATIC_CAPS ("video/x-dv, "
82         "format = (string) { NTSC, PAL }, " "systemstream = (boolean) true")
83     );
84
85 static void gst_dv1394src_base_init (gpointer g_class);
86 static void gst_dv1394src_class_init (GstDV1394SrcClass * klass);
87 static void gst_dv1394src_init (GstDV1394Src * filter);
88 static void gst_dv1394src_dispose (GObject * obj);
89
90 static void gst_dv1394src_set_property (GObject * object, guint prop_id,
91     const GValue * value, GParamSpec * pspec);
92 static void gst_dv1394src_get_property (GObject * object, guint prop_id,
93     GValue * value, GParamSpec * pspec);
94
95 static GstElementStateReturn gst_dv1394src_change_state (GstElement * element);
96
97 static GstFlowReturn gst_dv1394src_create (GstPushSrc * psrc, GstBuffer ** buf);
98
99 #if 0
100 static const GstEventMask *gst_dv1394src_get_event_mask (GstPad * pad);
101 #endif
102 static gboolean gst_dv1394src_event (GstPad * pad, GstEvent * event);
103
104 #if 0
105 static const GstFormat *gst_dv1394src_get_formats (GstPad * pad);
106 #endif
107 static gboolean gst_dv1394src_convert (GstPad * pad,
108     GstFormat src_format, gint64 src_value,
109     GstFormat * dest_format, gint64 * dest_value);
110
111 static const GstQueryType *gst_dv1394src_get_query_types (GstPad * pad);
112 static gboolean gst_dv1394src_query (GstPad * pad, GstQuery * query);
113
114 static GstElementClass *parent_class = NULL;
115
116 static void gst_dv1394src_uri_handler_init (gpointer g_iface,
117     gpointer iface_data);
118
119 static guint gst_dv1394src_signals[LAST_SIGNAL] = { 0 };
120
121 GType
122 gst_dv1394src_get_type (void)
123 {
124   static GType gst_dv1394src_type = 0;
125
126   if (!gst_dv1394src_type) {
127     static const GTypeInfo gst_dv1394src_info = {
128       sizeof (GstDV1394Src),
129       gst_dv1394src_base_init,
130       NULL,
131       (GClassInitFunc) gst_dv1394src_class_init,
132       NULL,
133       NULL,
134       sizeof (GstDV1394Src),
135       0,
136       (GInstanceInitFunc) gst_dv1394src_init,
137     };
138     static const GInterfaceInfo urihandler_info = {
139       gst_dv1394src_uri_handler_init,
140       NULL,
141       NULL,
142     };
143
144     gst_dv1394src_type =
145         g_type_register_static (GST_TYPE_PUSH_SRC, "DV1394Src",
146         &gst_dv1394src_info, 0);
147
148     g_type_add_interface_static (gst_dv1394src_type, GST_TYPE_URI_HANDLER,
149         &urihandler_info);
150
151     GST_DEBUG_CATEGORY_INIT (dv1394src_debug, "dv1394src", 0,
152         "DV firewire source");
153   }
154   return gst_dv1394src_type;
155 }
156
157 static void
158 gst_dv1394src_base_init (gpointer g_class)
159 {
160   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
161
162   gst_element_class_add_pad_template (element_class,
163       gst_static_pad_template_get (&src_factory));
164
165   gst_element_class_set_details (element_class, &gst_dv1394src_details);
166 }
167
168 static void
169 gst_dv1394src_class_init (GstDV1394SrcClass * klass)
170 {
171   GObjectClass *gobject_class;
172   GstElementClass *gstelement_class;
173   GstBaseSrcClass *gstbasesrc_class;
174   GstPushSrcClass *gstpushsrc_class;
175
176   gobject_class = (GObjectClass *) klass;
177   gstelement_class = (GstElementClass *) klass;
178   gstbasesrc_class = (GstBaseSrcClass *) klass;
179   gstpushsrc_class = (GstPushSrcClass *) klass;
180
181   parent_class = g_type_class_ref (GST_TYPE_PUSH_SRC);
182
183   gobject_class->set_property = gst_dv1394src_set_property;
184   gobject_class->get_property = gst_dv1394src_get_property;
185   gobject_class->dispose = gst_dv1394src_dispose;
186
187   gst_dv1394src_signals[SIGNAL_FRAME_DROPPED] =
188       g_signal_new ("frame-dropped", G_TYPE_FROM_CLASS (klass),
189       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDV1394SrcClass, frame_dropped),
190       NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
191
192   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_PORT,
193       g_param_spec_int ("port", "Port", "Port number (-1 automatic)",
194           -1, 16, DEFAULT_PORT, G_PARAM_READWRITE));
195   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_CHANNEL,
196       g_param_spec_int ("channel", "Channel", "Channel number for listening",
197           0, 64, DEFAULT_CHANNEL, G_PARAM_READWRITE));
198   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_CONSECUTIVE,
199       g_param_spec_int ("consecutive", "consecutive frames",
200           "send n consecutive frames after skipping", 1, G_MAXINT,
201           DEFAULT_CONSECUTIVE, G_PARAM_READWRITE));
202   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_SKIP,
203       g_param_spec_int ("skip", "skip frames", "skip n frames",
204           0, G_MAXINT, DEFAULT_SKIP, G_PARAM_READWRITE));
205   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_DROP_INCOMPLETE,
206       g_param_spec_boolean ("drop_incomplete", "drop_incomplete",
207           "drop incomplete frames", DEFAULT_DROP_INCOMPLETE,
208           G_PARAM_READWRITE));
209   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_USE_AVC,
210       g_param_spec_boolean ("use-avc", "Use AV/C", "Use AV/C VTR control",
211           DEFAULT_USE_AVC, G_PARAM_READWRITE));
212   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_GUID,
213       g_param_spec_uint64 ("guid", "GUID",
214           "select one of multiple DV devices by its GUID. use a hexadecimal "
215           "like 0xhhhhhhhhhhhhhhhh. (0 = no guid)", 0, G_MAXUINT64,
216           DEFAULT_GUID, G_PARAM_READWRITE));
217
218   gstelement_class->change_state = gst_dv1394src_change_state;
219
220   gstbasesrc_class->negotiate = NULL;
221   gstpushsrc_class->create = gst_dv1394src_create;
222 }
223
224 static void
225 gst_dv1394src_init (GstDV1394Src * dv1394src)
226 {
227   GstPad *srcpad = GST_BASE_SRC_PAD (dv1394src);
228
229   gst_base_src_set_live (GST_BASE_SRC (dv1394src), TRUE);
230   gst_pad_use_fixed_caps (srcpad);
231
232   gst_pad_set_event_function (srcpad, gst_dv1394src_event);
233   gst_pad_set_query_function (srcpad, gst_dv1394src_query);
234   gst_pad_set_query_type_function (srcpad, gst_dv1394src_get_query_types);
235
236 #if 0
237   gst_pad_set_event_mask_function (dv1394src->srcpad,
238       gst_dv1394src_get_event_mask);
239   gst_pad_set_convert_function (dv1394src->srcpad, gst_dv1394src_convert);
240   gst_pad_set_formats_function (dv1394src->srcpad, gst_dv1394src_get_formats);
241 #endif
242
243   dv1394src->dv_lock = g_mutex_new ();
244   dv1394src->port = DEFAULT_PORT;
245   dv1394src->channel = DEFAULT_CHANNEL;
246
247   dv1394src->consecutive = DEFAULT_CONSECUTIVE;
248   dv1394src->skip = DEFAULT_SKIP;
249   dv1394src->drop_incomplete = DEFAULT_DROP_INCOMPLETE;
250   dv1394src->use_avc = DEFAULT_USE_AVC;
251   dv1394src->guid = DEFAULT_GUID;
252
253   /* initialized when first header received */
254   dv1394src->frame_size = 0;
255
256   dv1394src->buf = NULL;
257   dv1394src->frame = NULL;
258   dv1394src->frame_sequence = 0;
259 }
260
261 static void
262 gst_dv1394src_dispose (GObject * obj)
263 {
264   GstDV1394Src *dv1394src;
265
266   dv1394src = GST_DV1394SRC (obj);
267
268   G_OBJECT_CLASS (parent_class)->dispose (obj);
269 }
270
271 static void
272 gst_dv1394src_set_property (GObject * object, guint prop_id,
273     const GValue * value, GParamSpec * pspec)
274 {
275   GstDV1394Src *filter = GST_DV1394SRC (object);
276
277   switch (prop_id) {
278     case ARG_PORT:
279       filter->port = g_value_get_int (value);
280       break;
281     case ARG_CHANNEL:
282       filter->channel = g_value_get_int (value);
283       break;
284     case ARG_SKIP:
285       filter->skip = g_value_get_int (value);
286       break;
287     case ARG_CONSECUTIVE:
288       filter->consecutive = g_value_get_int (value);
289       break;
290     case ARG_DROP_INCOMPLETE:
291       filter->drop_incomplete = g_value_get_boolean (value);
292       break;
293     case ARG_USE_AVC:
294       filter->use_avc = g_value_get_boolean (value);
295       break;
296     case ARG_GUID:
297       filter->guid = g_value_get_uint64 (value);
298       break;
299     default:
300       break;
301   }
302 }
303
304 static void
305 gst_dv1394src_get_property (GObject * object, guint prop_id, GValue * value,
306     GParamSpec * pspec)
307 {
308   GstDV1394Src *filter = GST_DV1394SRC (object);
309
310   switch (prop_id) {
311     case ARG_PORT:
312       g_value_set_int (value, filter->port);
313       break;
314     case ARG_CHANNEL:
315       g_value_set_int (value, filter->channel);
316       break;
317     case ARG_SKIP:
318       g_value_set_int (value, filter->skip);
319       break;
320     case ARG_CONSECUTIVE:
321       g_value_set_int (value, filter->consecutive);
322       break;
323     case ARG_DROP_INCOMPLETE:
324       g_value_set_boolean (value, filter->drop_incomplete);
325       break;
326     case ARG_USE_AVC:
327       g_value_set_boolean (value, filter->use_avc);
328       break;
329     case ARG_GUID:
330       g_value_set_uint64 (value, filter->guid);
331       break;
332     default:
333       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
334       break;
335   }
336 }
337
338 static int
339 gst_dv1394src_iso_receive (raw1394handle_t handle, int channel, size_t len,
340     quadlet_t * data)
341 {
342   GstDV1394Src *dv1394src = GST_DV1394SRC (raw1394_get_userdata (handle));
343
344   if (len > 16) {
345     /*
346        the following code taken from kino-0.51 (Dan Dennedy/Charles Yates)
347      */
348     unsigned char *p = (unsigned char *) &data[3];
349     int section_type = p[0] >> 5;       /* section type is in bits 5 - 7 */
350     int dif_sequence = p[1] >> 4;       /* dif sequence number is in bits 4 - 7 */
351     int dif_block = p[2];
352
353     /* if we are at the beginning of a frame, 
354        we set buf=frame, and alloc a new buffer for frame
355      */
356
357     if (section_type == 0 && dif_sequence == 0) {       // dif header
358       if (!dv1394src->negotiated) {
359         GstCaps *caps;
360
361         // figure format (NTSC/PAL)
362         if (p[3] & 0x80) {
363           // PAL
364           dv1394src->frame_size = PAL_FRAMESIZE;
365           dv1394src->frame_rate = PAL_FRAMERATE;
366           GST_DEBUG ("PAL data");
367           caps = gst_caps_new_simple ("video/x-dv",
368               "format", G_TYPE_STRING, "PAL",
369               "systemstream", G_TYPE_BOOLEAN, TRUE, NULL);
370         } else {
371           // NTSC (untested)
372           dv1394src->frame_size = NTSC_FRAMESIZE;
373           dv1394src->frame_rate = NTSC_FRAMERATE;
374           GST_DEBUG
375               ("NTSC data [untested] - please report success/failure to <dan@f3c.com>");
376           caps = gst_caps_new_simple ("video/x-dv",
377               "format", G_TYPE_STRING, "NTSC",
378               "systemstream", G_TYPE_BOOLEAN, TRUE, NULL);
379         }
380         gst_pad_set_caps (GST_BASE_SRC_PAD (dv1394src), caps);
381         gst_caps_unref (caps);
382         dv1394src->negotiated = TRUE;
383       }
384       // drop last frame when not complete
385       if (!dv1394src->drop_incomplete
386           || dv1394src->bytes_in_frame == dv1394src->frame_size) {
387         dv1394src->buf = dv1394src->frame;
388       } else {
389         GST_INFO_OBJECT (GST_ELEMENT (dv1394src), "incomplete frame dropped");
390         g_signal_emit (G_OBJECT (dv1394src),
391             gst_dv1394src_signals[SIGNAL_FRAME_DROPPED], 0);
392         if (dv1394src->frame) {
393           gst_buffer_unref (dv1394src->frame);
394         }
395       }
396       dv1394src->frame = NULL;
397       if ((dv1394src->frame_sequence + 1) % (dv1394src->skip +
398               dv1394src->consecutive) < dv1394src->consecutive) {
399         //GstFormat format;
400         GstBuffer *buf;
401
402         buf = gst_buffer_new_and_alloc (dv1394src->frame_size);
403 #if 0
404         /* fill in default offset */
405         format = GST_FORMAT_DEFAULT;
406         gst_dv1394src_query (dv1394src->srcpad, GST_QUERY_POSITION, &format,
407             &GST_BUFFER_OFFSET (buf));
408         /* fill in timestamp */
409         format = GST_FORMAT_TIME;
410         gst_dv1394src_query (dv1394src->srcpad, GST_QUERY_POSITION, &format,
411             &GST_BUFFER_TIMESTAMP (buf));
412         /* fill in duration by converting one frame to time */
413         gst_dv1394src_convert (dv1394src->srcpad, GST_FORMAT_DEFAULT, 1,
414             &format, &GST_BUFFER_DURATION (buf));
415 #endif
416
417         dv1394src->frame = buf;
418       }
419       dv1394src->frame_sequence++;
420       dv1394src->bytes_in_frame = 0;
421     }
422
423     if (dv1394src->frame != NULL) {
424       guint8 *data = GST_BUFFER_DATA (dv1394src->frame);
425
426       switch (section_type) {
427         case 0:                /* 1 Header block */
428           /* p[3] |= 0x80; // hack to force PAL data */
429           memcpy (data + dif_sequence * 150 * 80, p, 480);
430           break;
431
432         case 1:                /* 2 Subcode blocks */
433           memcpy (data + dif_sequence * 150 * 80 + (1 + dif_block) * 80, p,
434               480);
435           break;
436
437         case 2:                /* 3 VAUX blocks */
438           memcpy (data + dif_sequence * 150 * 80 + (3 + dif_block) * 80, p,
439               480);
440           break;
441
442         case 3:                /* 9 Audio blocks interleaved with video */
443           memcpy (data + dif_sequence * 150 * 80 + (6 + dif_block * 16) * 80, p,
444               480);
445           break;
446
447         case 4:                /* 135 Video blocks interleaved with audio */
448           memcpy (data + dif_sequence * 150 * 80 + (7 + (dif_block / 15) +
449                   dif_block) * 80, p, 480);
450           break;
451
452         default:               /* we can't handle any other data */
453           break;
454       }
455       dv1394src->bytes_in_frame += 480;
456     }
457   }
458
459   return 0;
460 }
461
462 static int
463 gst_dv1394src_bus_reset (raw1394handle_t handle, unsigned int generation)
464 {
465   GST_INFO_OBJECT (GST_DV1394SRC (raw1394_get_userdata (handle)),
466       "have bus reset");
467   return 0;
468 }
469
470 static GstFlowReturn
471 gst_dv1394src_create (GstPushSrc * psrc, GstBuffer ** buf)
472 {
473   GstDV1394Src *dv1394src = GST_DV1394SRC (psrc);
474   GstCaps *caps;
475
476   GST_DV_LOCK (dv1394src);
477   dv1394src->buf = NULL;
478   while (dv1394src->buf == NULL)
479     raw1394_loop_iterate (dv1394src->handle);
480
481   caps = gst_pad_get_caps (GST_BASE_SRC_PAD (psrc));
482   gst_buffer_set_caps (dv1394src->buf, caps);
483   gst_caps_unref (caps);
484
485   *buf = dv1394src->buf;
486   GST_DV_UNLOCK (dv1394src);
487
488   return GST_FLOW_OK;
489 }
490
491 static int
492 gst_dv1394src_discover_avc_node (GstDV1394Src * src)
493 {
494   int node = -1;
495   int i, j = 0;
496   int m = src->num_ports;
497
498   if (src->port >= 0) {
499     /* search on explicit port */
500     j = src->port;
501     m = j + 1;
502   }
503
504   /* loop over all our ports */
505   for (; j < m && node == -1; j++) {
506     raw1394handle_t handle;
507     gint n_ports;
508     struct raw1394_portinfo pinf[16];
509
510     /* open the port */
511     handle = raw1394_new_handle ();
512     if (!handle) {
513       g_warning ("raw1394 - failed to get handle: %s.\n", strerror (errno));
514       continue;
515     }
516     if ((n_ports = raw1394_get_port_info (handle, pinf, 16)) < 0) {
517       g_warning ("raw1394 - failed to get port info: %s.\n", strerror (errno));
518       goto next;
519     }
520
521     /* tell raw1394 which host adapter to use */
522     if (raw1394_set_port (handle, j) < 0) {
523       g_warning ("raw1394 - failed to set set port: %s.\n", strerror (errno));
524       goto next;
525     }
526
527     /* now loop over all the nodes */
528     for (i = 0; i < raw1394_get_nodecount (handle); i++) {
529       /* are we looking for an explicit GUID */
530       if (src->guid != 0) {
531         if (src->guid == rom1394_get_guid (handle, i)) {
532           node = i;
533           src->port = j;
534           break;
535         }
536       } else {
537         rom1394_directory rom_dir;
538
539         /* select first AV/C Tape Reccorder Player node */
540         if (rom1394_get_directory (handle, i, &rom_dir) < 0) {
541           g_warning ("error reading config rom directory for node %d\n", i);
542           continue;
543         }
544         if ((rom1394_get_node_type (&rom_dir) == ROM1394_NODE_TYPE_AVC) &&
545             avc1394_check_subunit_type (handle, i, AVC1394_SUBUNIT_TYPE_VCR)) {
546           node = i;
547           src->port = j;
548           break;
549         }
550       }
551     }
552   next:
553     raw1394_destroy_handle (handle);
554   }
555   return node;
556 }
557
558 static GstElementStateReturn
559 gst_dv1394src_change_state (GstElement * element)
560 {
561   GstDV1394Src *dv1394src;
562   gint transition;
563   GstElementStateReturn ret;
564
565   dv1394src = GST_DV1394SRC (element);
566   transition = GST_STATE_TRANSITION (element);
567
568   switch (transition) {
569     case GST_STATE_NULL_TO_READY:
570       /* create a handle */
571       if ((dv1394src->handle = raw1394_new_handle ()) == NULL)
572         goto no_handle;
573
574       /* set this plugin as the user data */
575       raw1394_set_userdata (dv1394src->handle, dv1394src);
576
577       /* get number of ports */
578       if ((dv1394src->num_ports =
579               raw1394_get_port_info (dv1394src->handle, dv1394src->pinfo,
580                   16)) == 0)
581         goto no_ports;
582
583       if (dv1394src->use_avc || dv1394src->port == -1) {
584         /* discover AVC and optionally the port */
585         dv1394src->avc_node = gst_dv1394src_discover_avc_node (dv1394src);
586       }
587
588       /* configure our port now */
589       if (raw1394_set_port (dv1394src->handle, dv1394src->port) < 0)
590         goto cannot_set_port;
591
592       /* set the callbacks */
593       raw1394_set_iso_handler (dv1394src->handle, dv1394src->channel,
594           gst_dv1394src_iso_receive);
595       raw1394_set_bus_reset_handler (dv1394src->handle,
596           gst_dv1394src_bus_reset);
597
598       GST_DEBUG_OBJECT (dv1394src, "successfully opened up 1394 connection");
599       break;
600     case GST_STATE_PAUSED_TO_PLAYING:
601       if (raw1394_start_iso_rcv (dv1394src->handle, dv1394src->channel) < 0)
602         goto cannot_start;
603
604       if (dv1394src->use_avc) {
605         /* start the VCR */
606         if (!avc1394_vcr_is_recording (dv1394src->handle, dv1394src->avc_node)
607             && avc1394_vcr_is_playing (dv1394src->handle, dv1394src->avc_node)
608             != AVC1394_VCR_OPERAND_PLAY_FORWARD) {
609           avc1394_vcr_play (dv1394src->handle, dv1394src->avc_node);
610         }
611       }
612       break;
613     default:
614       break;
615   }
616
617   /* if we haven't failed already, give the parent class a chance to ;-) */
618   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element);
619
620   switch (transition) {
621     case GST_STATE_PLAYING_TO_PAUSED:
622       /* we need to lock here as the _create function has to be completed.
623        * The base source will not call the _create() function again. */
624       GST_DV_LOCK (dv1394src);
625       raw1394_stop_iso_rcv (dv1394src->handle, dv1394src->channel);
626       if (dv1394src->use_avc) {
627         /* pause the VCR */
628         if (!avc1394_vcr_is_recording (dv1394src->handle, dv1394src->avc_node)
629             && (avc1394_vcr_is_playing (dv1394src->handle, dv1394src->avc_node)
630                 != AVC1394_VCR_OPERAND_PLAY_FORWARD_PAUSE)) {
631           avc1394_vcr_pause (dv1394src->handle, dv1394src->avc_node);
632         }
633       }
634       GST_DV_UNLOCK (dv1394src);
635       break;
636     case GST_STATE_PAUSED_TO_READY:
637       dv1394src->negotiated = FALSE;
638       break;
639     case GST_STATE_READY_TO_NULL:
640       if (dv1394src->use_avc) {
641         /* stop the VCR */
642         avc1394_vcr_stop (dv1394src->handle, dv1394src->avc_node);
643       }
644       raw1394_destroy_handle (dv1394src->handle);
645       break;
646     default:
647       break;
648   }
649
650   return ret;
651
652 no_handle:
653   {
654     GST_ELEMENT_ERROR (dv1394src, RESOURCE, NOT_FOUND, (NULL),
655         ("can't get raw1394 handle"));
656     return GST_STATE_FAILURE;
657   }
658 no_ports:
659   {
660     GST_ELEMENT_ERROR (dv1394src, RESOURCE, NOT_FOUND, (NULL),
661         ("no ports available for raw1394"));
662     return GST_STATE_FAILURE;
663   }
664 cannot_set_port:
665   {
666     GST_ELEMENT_ERROR (dv1394src, RESOURCE, SETTINGS, (NULL),
667         ("can't set 1394 port %d", dv1394src->port));
668     return GST_STATE_FAILURE;
669   }
670 cannot_start:
671   {
672     GST_ELEMENT_ERROR (dv1394src, RESOURCE, READ, (NULL),
673         ("can't start 1394 iso receive"));
674     return GST_STATE_FAILURE;
675   }
676 }
677
678
679 #if 0
680 static const GstEventMask *
681 gst_dv1394src_get_event_mask (GstPad * pad)
682 {
683   static const GstEventMask masks[] = {
684     {0,}
685   };
686
687   return masks;
688 }
689 #endif
690
691 static gboolean
692 gst_dv1394src_event (GstPad * pad, GstEvent * event)
693 {
694   return FALSE;
695 }
696
697 #if 0
698 static const GstFormat *
699 gst_dv1394src_get_formats (GstPad * pad)
700 {
701   static GstFormat formats[] = {
702     GST_FORMAT_TIME,
703     GST_FORMAT_BYTES,
704     GST_FORMAT_DEFAULT,
705     0
706   };
707
708   return formats;
709 }
710 #endif
711
712 static gboolean
713 gst_dv1394src_convert (GstPad * pad,
714     GstFormat src_format, gint64 src_value,
715     GstFormat * dest_format, gint64 * dest_value)
716 {
717   GstDV1394Src *src;
718
719   src = GST_DV1394SRC (gst_pad_get_parent (pad));
720
721   switch (src_format) {
722     case GST_FORMAT_TIME:
723       switch (*dest_format) {
724         case GST_FORMAT_BYTES:
725           src_value *= src->frame_size;
726         case GST_FORMAT_DEFAULT:
727           *dest_value = src_value * src->frame_rate / GST_SECOND;
728           break;
729         default:
730           goto not_supported;
731       }
732       break;
733     case GST_FORMAT_BYTES:
734       src_value /= src->frame_size;
735     case GST_FORMAT_DEFAULT:
736       switch (*dest_format) {
737         case GST_FORMAT_BYTES:
738           *dest_value = src_value * src->frame_size;
739           break;
740         case GST_FORMAT_TIME:
741           if (src->frame_rate != 0)
742             *dest_value = src_value * GST_SECOND / src->frame_rate;
743           else
744             goto not_supported;
745           break;
746         default:
747           goto not_supported;
748       }
749       break;
750     default:
751       goto not_supported;
752   }
753
754   gst_object_unref (src);
755   return TRUE;
756
757 not_supported:
758   {
759     gst_object_unref (src);
760     return FALSE;
761   }
762 }
763
764 static const GstQueryType *
765 gst_dv1394src_get_query_types (GstPad * pad)
766 {
767   static const GstQueryType src_query_types[] = {
768     GST_QUERY_CONVERT,
769     GST_QUERY_POSITION,
770     0
771   };
772
773   return src_query_types;
774 }
775
776 static gboolean
777 gst_dv1394src_query (GstPad * pad, GstQuery * query)
778 {
779   gboolean res = TRUE;
780   GstDV1394Src *src;
781
782   src = GST_DV1394SRC (gst_pad_get_parent (pad));
783
784   switch (GST_QUERY_TYPE (query)) {
785     case GST_QUERY_POSITION:
786     {
787       GstFormat format;
788       gint64 current;
789
790       gst_query_parse_position (query, &format, NULL, NULL);
791
792       /* bring our current frame to the requested format */
793       res = gst_pad_query_convert (pad,
794           GST_FORMAT_DEFAULT, src->frame_sequence, &format, &current);
795
796       gst_query_set_position (query, format, current, -1);
797       break;
798     }
799     case GST_QUERY_CONVERT:
800     {
801       GstFormat src_fmt, dest_fmt;
802       gint64 src_val, dest_val;
803
804       gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
805       if (!(res =
806               gst_dv1394src_convert (pad, src_fmt, src_val, &dest_fmt,
807                   &dest_val)))
808         goto not_supported;
809       gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
810       break;
811     }
812     default:
813       goto not_supported;
814   }
815
816   gst_object_unref (src);
817   return TRUE;
818
819 not_supported:
820   {
821     gst_object_unref (src);
822     return FALSE;
823   }
824 }
825
826 /*** GSTURIHANDLER INTERFACE *************************************************/
827
828 static guint
829 gst_dv1394src_uri_get_type (void)
830 {
831   return GST_URI_SRC;
832 }
833 static gchar **
834 gst_dv1394src_uri_get_protocols (void)
835 {
836   static gchar *protocols[] = { "dv", NULL };
837
838   return protocols;
839 }
840 static const gchar *
841 gst_dv1394src_uri_get_uri (GstURIHandler * handler)
842 {
843   GstDV1394Src *gst_dv1394src = GST_DV1394SRC (handler);
844
845   return gst_dv1394src->uri;
846 }
847
848 static gboolean
849 gst_dv1394src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
850 {
851   gchar *protocol, *location;
852   gboolean ret;
853
854   ret = TRUE;
855
856   GstDV1394Src *gst_dv1394src = GST_DV1394SRC (handler);
857
858   protocol = gst_uri_get_protocol (uri);
859   if (strcmp (protocol, "dv") != 0) {
860     g_free (protocol);
861     return FALSE;
862   }
863   g_free (protocol);
864
865   location = gst_uri_get_location (uri);
866   gst_dv1394src->port = strtol (location, NULL, 10);
867   g_free (location);
868
869   return ret;
870 }
871
872 static void
873 gst_dv1394src_uri_handler_init (gpointer g_iface, gpointer iface_data)
874 {
875   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
876
877   iface->get_type = gst_dv1394src_uri_get_type;
878   iface->get_protocols = gst_dv1394src_uri_get_protocols;
879   iface->get_uri = gst_dv1394src_uri_get_uri;
880   iface->set_uri = gst_dv1394src_uri_set_uri;
881 }