3 * gstv4lmjpegsink.c: hardware MJPEG video sink plugin
5 * Copyright (C) 2001-2002 Ronald Bultje <rbultje@ronald.bitfreak.net>
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.
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.
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.
28 #include "v4lmjpegsink_calls.h"
30 GST_DEBUG_CATEGORY (v4lmjpegsink_debug);
31 #define GST_CAT_DEFAULT v4lmjpegsink_debug
33 /* elementfactory information */
34 static GstElementDetails gst_v4lmjpegsink_details = {
35 "Video (video4linux/MJPEG) sink",
37 "Writes MJPEG-encoded frames to a zoran MJPEG/video4linux device",
38 "Ronald Bultje <rbultje@ronald.bitfreak.net>"
41 /* v4lmjpegsink signals and args */
44 SIGNAL_FRAME_DISPLAYED,
61 static void gst_v4lmjpegsink_base_init (gpointer g_class);
62 static void gst_v4lmjpegsink_class_init (GstV4lMjpegSinkClass * klass);
63 static void gst_v4lmjpegsink_init (GstV4lMjpegSink * v4lmjpegsink);
65 /* the chain of buffers */
66 static GstPadLinkReturn gst_v4lmjpegsink_sinkconnect (GstPad * pad,
67 const GstCaps * vscapslist);
68 static void gst_v4lmjpegsink_chain (GstPad * pad, GstData * _data);
70 /* get/set gst object functions */
71 static void gst_v4lmjpegsink_set_property (GObject * object,
72 guint prop_id, const GValue * value, GParamSpec * pspec);
73 static void gst_v4lmjpegsink_get_property (GObject * object,
74 guint prop_id, GValue * value, GParamSpec * pspec);
75 static GstStateChangeReturn gst_v4lmjpegsink_change_state (GstElement *
77 static void gst_v4lmjpegsink_set_clock (GstElement * element, GstClock * clock);
80 static GstElementClass *parent_class = NULL;
81 static guint gst_v4lmjpegsink_signals[LAST_SIGNAL] = { 0 };
85 gst_v4lmjpegsink_get_type (void)
87 static GType v4lmjpegsink_type = 0;
89 if (!v4lmjpegsink_type) {
90 static const GTypeInfo v4lmjpegsink_info = {
91 sizeof (GstV4lMjpegSinkClass),
92 gst_v4lmjpegsink_base_init,
94 (GClassInitFunc) gst_v4lmjpegsink_class_init,
97 sizeof (GstV4lMjpegSink),
99 (GInstanceInitFunc) gst_v4lmjpegsink_init,
103 g_type_register_static (GST_TYPE_V4LELEMENT, "GstV4lMjpegSink",
104 &v4lmjpegsink_info, 0);
106 return v4lmjpegsink_type;
110 gst_v4lmjpegsink_base_init (gpointer g_class)
112 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
115 GST_STATIC_CAPS ("image/jpeg, "
116 "width = (int) [ 1, MAX ], "
117 "height = (int) [ 1, MAX ], " "framerate = (fraction) [ 0, MAX ]")
119 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
121 gst_element_class_set_details (gstelement_class, &gst_v4lmjpegsink_details);
123 gst_element_class_add_pad_template (gstelement_class,
124 gst_static_pad_template_get (&sink_template));
127 gst_v4lmjpegsink_class_init (GstV4lMjpegSinkClass * klass)
129 GObjectClass *gobject_class;
130 GstElementClass *gstelement_class;
132 gobject_class = (GObjectClass *) klass;
133 gstelement_class = (GstElementClass *) klass;
135 parent_class = g_type_class_ref (GST_TYPE_V4LELEMENT);
137 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_NUMBUFS,
138 g_param_spec_int ("num-buffers", "num-buffers", "num-buffers",
139 G_MININT, G_MAXINT, 0, G_PARAM_READWRITE));
140 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BUFSIZE,
141 g_param_spec_int ("buffer-size", "buffer-size", "buffer-size",
142 G_MININT, G_MAXINT, 0, G_PARAM_READWRITE));
144 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_X_OFFSET,
145 g_param_spec_int ("x-offset", "x-offset", "x-offset",
146 G_MININT, G_MAXINT, 0, G_PARAM_WRITABLE));
147 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_Y_OFFSET,
148 g_param_spec_int ("y-offset", "y-offset", "y-offset",
149 G_MININT, G_MAXINT, 0, G_PARAM_WRITABLE));
151 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_FRAMES_DISPLAYED,
152 g_param_spec_int ("frames-displayed", "frames-displayed",
153 "frames-displayed", G_MININT, G_MAXINT, 0, G_PARAM_READABLE));
154 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_FRAME_TIME,
155 g_param_spec_int ("frame-time", "frame-time", "frame-time", G_MININT,
156 G_MAXINT, 0, G_PARAM_READABLE));
158 GST_DEBUG_CATEGORY_INIT (v4lmjpegsink_debug, "v4lmjpegsink", 0,
159 "V4L MJPEG sink element");
160 gobject_class->set_property = gst_v4lmjpegsink_set_property;
161 gobject_class->get_property = gst_v4lmjpegsink_get_property;
163 gst_v4lmjpegsink_signals[SIGNAL_FRAME_DISPLAYED] =
164 g_signal_new ("frame-displayed", G_TYPE_FROM_CLASS (klass),
165 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstV4lMjpegSinkClass,
166 frame_displayed), NULL, NULL, g_cclosure_marshal_VOID__VOID,
169 gstelement_class->change_state = gst_v4lmjpegsink_change_state;
170 gstelement_class->set_clock = gst_v4lmjpegsink_set_clock;
175 gst_v4lmjpegsink_init (GstV4lMjpegSink * v4lmjpegsink)
177 GstElementClass *klass = GST_ELEMENT_GET_CLASS (v4lmjpegsink);
179 v4lmjpegsink->sinkpad =
180 gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
182 gst_element_add_pad (GST_ELEMENT (v4lmjpegsink), v4lmjpegsink->sinkpad);
184 gst_pad_set_chain_function (v4lmjpegsink->sinkpad, gst_v4lmjpegsink_chain);
185 gst_pad_set_link_function (v4lmjpegsink->sinkpad,
186 gst_v4lmjpegsink_sinkconnect);
188 v4lmjpegsink->clock = NULL;
190 v4lmjpegsink->width = -1;
191 v4lmjpegsink->height = -1;
193 v4lmjpegsink->x_offset = -1;
194 v4lmjpegsink->y_offset = -1;
196 v4lmjpegsink->numbufs = 64;
197 v4lmjpegsink->bufsize = 256;
199 GST_OBJECT_FLAG_SET (v4lmjpegsink, GST_ELEMENT_THREAD_SUGGESTED);
203 static GstPadLinkReturn
204 gst_v4lmjpegsink_sinkconnect (GstPad * pad, const GstCaps * vscapslist)
206 GstV4lMjpegSink *v4lmjpegsink;
207 GstStructure *structure;
209 v4lmjpegsink = GST_V4LMJPEGSINK (gst_pad_get_parent (pad));
211 /* in case the buffers are active (which means that we already
212 * did capsnego before and didn't clean up), clean up anyways */
213 if (GST_V4L_IS_ACTIVE (GST_V4LELEMENT (v4lmjpegsink)))
214 if (!gst_v4lmjpegsink_playback_deinit (v4lmjpegsink))
215 return GST_PAD_LINK_REFUSED;
217 structure = gst_caps_get_structure (vscapslist, 0);
219 gst_structure_get_int (structure, "width", &v4lmjpegsink->width);
220 gst_structure_get_int (structure, "height", &v4lmjpegsink->height);
222 if (!gst_v4lmjpegsink_set_playback (v4lmjpegsink, v4lmjpegsink->width, v4lmjpegsink->height, v4lmjpegsink->x_offset, v4lmjpegsink->y_offset, GST_V4LELEMENT (v4lmjpegsink)->vchan.norm, 0)) /* TODO: interlacing */
223 return GST_PAD_LINK_REFUSED;
225 /* set buffer info */
226 if (!gst_v4lmjpegsink_set_buffer (v4lmjpegsink,
227 v4lmjpegsink->numbufs, v4lmjpegsink->bufsize))
228 return GST_PAD_LINK_REFUSED;
229 if (!gst_v4lmjpegsink_playback_init (v4lmjpegsink))
230 return GST_PAD_LINK_REFUSED;
232 return GST_PAD_LINK_OK;
238 gst_v4lmjpegsink_set_clock (GstElement * element, GstClock * clock)
240 GstV4lMjpegSink *v4mjpegsink = GST_V4LMJPEGSINK (element);
242 v4mjpegsink->clock = clock;
247 gst_v4lmjpegsink_chain (GstPad * pad, GstData * _data)
249 GstBuffer *buf = GST_BUFFER (_data);
250 GstV4lMjpegSink *v4lmjpegsink;
253 g_return_if_fail (pad != NULL);
254 g_return_if_fail (GST_IS_PAD (pad));
255 g_return_if_fail (buf != NULL);
257 v4lmjpegsink = GST_V4LMJPEGSINK (gst_pad_get_parent (pad));
259 if (v4lmjpegsink->clock) {
260 GST_DEBUG ("videosink: clock wait: %" G_GUINT64_FORMAT,
261 GST_BUFFER_TIMESTAMP (buf));
263 gst_element_wait (GST_ELEMENT (v4lmjpegsink), GST_BUFFER_TIMESTAMP (buf));
266 if (GST_BUFFER_POOL (buf) == v4lmjpegsink->bufferpool) {
267 num = GPOINTER_TO_INT (GST_BUFFER_POOL_PRIVATE (buf));
268 gst_v4lmjpegsink_play_frame (v4lmjpegsink, num);
272 if (GST_BUFFER_SIZE (buf) > v4lmjpegsink->breq.size) {
273 GST_ELEMENT_ERROR (v4lmjpegsink, RESOURCE, WRITE, (NULL),
274 ("Buffer too big (%d KB), max. buffersize is %ld KB",
275 GST_BUFFER_SIZE (buf) / 1024, v4lmjpegsink->breq.size / 1024));
279 /* put JPEG data to the device */
280 gst_v4lmjpegsink_wait_frame (v4lmjpegsink, &num);
281 memcpy (gst_v4lmjpegsink_get_buffer (v4lmjpegsink, num),
282 GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
283 gst_v4lmjpegsink_play_frame (v4lmjpegsink, num);
288 g_signal_emit (G_OBJECT (v4lmjpegsink),
289 gst_v4lmjpegsink_signals[SIGNAL_FRAME_DISPLAYED], 0);
291 gst_buffer_unref (buf);
297 gst_v4lmjpegsink_buffer_new (GstBufferPool * pool,
298 guint64 offset, guint size, gpointer user_data)
300 GstV4lMjpegSink *v4lmjpegsink = GST_V4LMJPEGSINK (user_data);
301 GstBuffer *buffer = NULL;
305 if (!GST_V4L_IS_ACTIVE (GST_V4LELEMENT (v4lmjpegsink)))
307 if (v4lmjpegsink->breq.size < size) {
308 GST_DEBUG ("Requested buffer size is too large (%d > %ld)",
309 size, v4lmjpegsink->breq.size);
312 if (!gst_v4lmjpegsink_wait_frame (v4lmjpegsink, &num))
314 data = gst_v4lmjpegsink_get_buffer (v4lmjpegsink, num);
317 buffer = gst_buffer_new ();
318 GST_BUFFER_DATA (buffer) = data;
319 GST_BUFFER_MAXSIZE (buffer) = v4lmjpegsink->breq.size;
320 GST_BUFFER_SIZE (buffer) = size;
321 GST_BUFFER_POOL (buffer) = pool;
322 GST_BUFFER_POOL_PRIVATE (buffer) = GINT_TO_POINTER (num);
324 /* with this flag set, we don't need our own buffer_free() function */
325 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_DONTFREE);
333 gst_v4lmjpegsink_set_property (GObject * object,
334 guint prop_id, const GValue * value, GParamSpec * pspec)
336 GstV4lMjpegSink *v4lmjpegsink;
338 g_return_if_fail (GST_IS_V4LMJPEGSINK (object));
340 v4lmjpegsink = GST_V4LMJPEGSINK (object);
344 v4lmjpegsink->numbufs = g_value_get_int (value);
347 v4lmjpegsink->bufsize = g_value_get_int (value);
350 v4lmjpegsink->x_offset = g_value_get_int (value);
353 v4lmjpegsink->y_offset = g_value_get_int (value);
356 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
363 gst_v4lmjpegsink_get_property (GObject * object,
364 guint prop_id, GValue * value, GParamSpec * pspec)
366 GstV4lMjpegSink *v4lmjpegsink;
368 v4lmjpegsink = GST_V4LMJPEGSINK (object);
371 case ARG_FRAMES_DISPLAYED:
372 g_value_set_int (value, v4lmjpegsink->frames_displayed);
375 g_value_set_int (value, v4lmjpegsink->frame_time / 1000000);
378 g_value_set_int (value, v4lmjpegsink->numbufs);
381 g_value_set_int (value, v4lmjpegsink->bufsize);
384 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
390 static GstStateChangeReturn
391 gst_v4lmjpegsink_change_state (GstElement * element, GstStateChange transition)
393 GstV4lMjpegSink *v4lmjpegsink;
394 GstStateChangeReturn parent_value;
396 g_return_val_if_fail (GST_IS_V4LMJPEGSINK (element),
397 GST_STATE_CHANGE_FAILURE);
398 v4lmjpegsink = GST_V4LMJPEGSINK (element);
400 /* set up change state */
401 switch (transition) {
402 case GST_STATE_CHANGE_READY_TO_PAUSED:
403 /* we used to do buffer setup here, but that's now done
404 * right after capsnego */
406 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
408 if (!gst_v4lmjpegsink_playback_start (v4lmjpegsink))
409 return GST_STATE_CHANGE_FAILURE;
411 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
412 /* de-queue all queued buffers */
413 if (!gst_v4lmjpegsink_playback_stop (v4lmjpegsink))
414 return GST_STATE_CHANGE_FAILURE;
416 case GST_STATE_CHANGE_PAUSED_TO_READY:
417 /* stop playback, unmap all buffers */
418 if (!gst_v4lmjpegsink_playback_deinit (v4lmjpegsink))
419 return GST_STATE_CHANGE_FAILURE;
423 if (GST_ELEMENT_CLASS (parent_class)->change_state) {
425 GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
427 parent_value = GST_STATE_CHANGE_FAILURE;
430 if (GST_ELEMENT_CLASS (parent_class)->change_state)
433 return GST_STATE_CHANGE_SUCCESS;