1 /* G-Streamer hardware MJPEG video sink plugin
2 * Copyright (C) 2001-2002 Ronald Bultje <rbultje@ronald.bitfreak.net>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
25 #include "v4lmjpegsink_calls.h"
27 /* elementfactory information */
28 static GstElementDetails gst_v4lmjpegsink_details = {
29 "Video (video4linux/MJPEG) sink",
31 "Writes MJPEG-encoded frames to a zoran MJPEG/video4linux device",
32 "Ronald Bultje <rbultje@ronald.bitfreak.net>"
35 /* v4lmjpegsink signals and args */
37 SIGNAL_FRAME_DISPLAYED,
53 static void gst_v4lmjpegsink_base_init (gpointer g_class);
54 static void gst_v4lmjpegsink_class_init (GstV4lMjpegSinkClass *klass);
55 static void gst_v4lmjpegsink_init (GstV4lMjpegSink *v4lmjpegsink);
57 /* the chain of buffers */
58 static GstPadLinkReturn gst_v4lmjpegsink_sinkconnect (GstPad *pad,
59 const GstCaps *vscapslist);
60 static void gst_v4lmjpegsink_chain (GstPad *pad,
63 /* get/set gst object functions */
64 static void gst_v4lmjpegsink_set_property (GObject *object,
68 static void gst_v4lmjpegsink_get_property (GObject *object,
72 static GstElementStateReturn gst_v4lmjpegsink_change_state (GstElement *element);
73 static void gst_v4lmjpegsink_set_clock (GstElement *element, GstClock *clock);
76 static GstElementClass *parent_class = NULL;
77 static guint gst_v4lmjpegsink_signals[LAST_SIGNAL] = { 0 };
81 gst_v4lmjpegsink_get_type (void)
83 static GType v4lmjpegsink_type = 0;
85 if (!v4lmjpegsink_type) {
86 static const GTypeInfo v4lmjpegsink_info = {
87 sizeof(GstV4lMjpegSinkClass),
88 gst_v4lmjpegsink_base_init,
90 (GClassInitFunc)gst_v4lmjpegsink_class_init,
93 sizeof(GstV4lMjpegSink),
95 (GInstanceInitFunc)gst_v4lmjpegsink_init,
97 v4lmjpegsink_type = g_type_register_static(GST_TYPE_V4LELEMENT, "GstV4lMjpegSink", &v4lmjpegsink_info, 0);
99 return v4lmjpegsink_type;
103 gst_v4lmjpegsink_base_init (gpointer g_class)
105 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE (
109 GST_STATIC_CAPS ("video/x-jpeg, "
110 "width = (int) [ 1, MAX ], "
111 "height = (int) [ 1, MAX ], "
112 "framerate = (double) [ 0, MAX ]")
114 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
116 gst_element_class_set_details (gstelement_class, &gst_v4lmjpegsink_details);
118 gst_element_class_add_pad_template (gstelement_class,
119 gst_static_pad_template_get(&sink_template));
122 gst_v4lmjpegsink_class_init (GstV4lMjpegSinkClass *klass)
124 GObjectClass *gobject_class;
125 GstElementClass *gstelement_class;
127 gobject_class = (GObjectClass*)klass;
128 gstelement_class = (GstElementClass*)klass;
130 parent_class = g_type_class_ref (GST_TYPE_V4LELEMENT);
132 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_NUMBUFS,
133 g_param_spec_int("num_buffers","num_buffers","num_buffers",
134 G_MININT,G_MAXINT,0,G_PARAM_READWRITE));
135 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_BUFSIZE,
136 g_param_spec_int("buffer_size","buffer_size","buffer_size",
137 G_MININT,G_MAXINT,0,G_PARAM_READWRITE));
139 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_X_OFFSET,
140 g_param_spec_int("x_offset","x_offset","x_offset",
141 G_MININT,G_MAXINT,0,G_PARAM_WRITABLE));
142 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_Y_OFFSET,
143 g_param_spec_int("y_offset","y_offset","y_offset",
144 G_MININT,G_MAXINT,0,G_PARAM_WRITABLE));
146 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_FRAMES_DISPLAYED,
147 g_param_spec_int("frames_displayed","frames_displayed","frames_displayed",
148 G_MININT,G_MAXINT,0,G_PARAM_READABLE));
149 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_FRAME_TIME,
150 g_param_spec_int("frame_time","frame_time","frame_time",
151 G_MININT,G_MAXINT,0,G_PARAM_READABLE));
153 gobject_class->set_property = gst_v4lmjpegsink_set_property;
154 gobject_class->get_property = gst_v4lmjpegsink_get_property;
156 gst_v4lmjpegsink_signals[SIGNAL_FRAME_DISPLAYED] =
157 g_signal_new ("frame_displayed", G_TYPE_FROM_CLASS(klass), G_SIGNAL_RUN_LAST,
158 G_STRUCT_OFFSET (GstV4lMjpegSinkClass, frame_displayed), NULL, NULL,
159 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
161 gstelement_class->change_state = gst_v4lmjpegsink_change_state;
162 gstelement_class->set_clock = gst_v4lmjpegsink_set_clock;
167 gst_v4lmjpegsink_init (GstV4lMjpegSink *v4lmjpegsink)
169 GstElementClass *klass = GST_ELEMENT_GET_CLASS (v4lmjpegsink);
171 v4lmjpegsink->sinkpad = gst_pad_new_from_template (
172 gst_element_class_get_pad_template (klass, "sink"), "sink");
173 gst_element_add_pad (GST_ELEMENT (v4lmjpegsink), v4lmjpegsink->sinkpad);
175 gst_pad_set_chain_function (v4lmjpegsink->sinkpad, gst_v4lmjpegsink_chain);
176 gst_pad_set_link_function (v4lmjpegsink->sinkpad, gst_v4lmjpegsink_sinkconnect);
178 v4lmjpegsink->clock = NULL;
180 v4lmjpegsink->width = -1;
181 v4lmjpegsink->height = -1;
183 v4lmjpegsink->x_offset = -1;
184 v4lmjpegsink->y_offset = -1;
186 v4lmjpegsink->numbufs = 64;
187 v4lmjpegsink->bufsize = 256;
189 GST_FLAG_SET(v4lmjpegsink, GST_ELEMENT_THREAD_SUGGESTED);
193 static GstPadLinkReturn
194 gst_v4lmjpegsink_sinkconnect (GstPad *pad,
195 const GstCaps *vscapslist)
197 GstV4lMjpegSink *v4lmjpegsink;
198 GstStructure *structure;
200 v4lmjpegsink = GST_V4LMJPEGSINK (gst_pad_get_parent (pad));
202 /* in case the buffers are active (which means that we already
203 * did capsnego before and didn't clean up), clean up anyways */
204 if (GST_V4L_IS_ACTIVE(GST_V4LELEMENT(v4lmjpegsink)))
205 if (!gst_v4lmjpegsink_playback_deinit(v4lmjpegsink))
206 return GST_PAD_LINK_REFUSED;
208 structure = gst_caps_get_structure (vscapslist, 0);
210 gst_structure_get_int (structure, "width", &v4lmjpegsink->width);
211 gst_structure_get_int (structure, "height", &v4lmjpegsink->height);
213 if (!gst_v4lmjpegsink_set_playback(v4lmjpegsink,
214 v4lmjpegsink->width, v4lmjpegsink->height,
215 v4lmjpegsink->x_offset, v4lmjpegsink->y_offset,
216 GST_V4LELEMENT(v4lmjpegsink)->vchan.norm, 0)) /* TODO: interlacing */
217 return GST_PAD_LINK_REFUSED;
219 /* set buffer info */
220 if (!gst_v4lmjpegsink_set_buffer(v4lmjpegsink,
221 v4lmjpegsink->numbufs, v4lmjpegsink->bufsize))
222 return GST_PAD_LINK_REFUSED;
223 if (!gst_v4lmjpegsink_playback_init(v4lmjpegsink))
224 return GST_PAD_LINK_REFUSED;
226 return GST_PAD_LINK_OK;
232 gst_v4lmjpegsink_set_clock (GstElement *element, GstClock *clock)
234 GstV4lMjpegSink *v4mjpegsink = GST_V4LMJPEGSINK (element);
236 v4mjpegsink->clock = clock;
241 gst_v4lmjpegsink_chain (GstPad *pad,
244 GstBuffer *buf = GST_BUFFER (_data);
245 GstV4lMjpegSink *v4lmjpegsink;
248 g_return_if_fail (pad != NULL);
249 g_return_if_fail (GST_IS_PAD (pad));
250 g_return_if_fail (buf != NULL);
252 v4lmjpegsink = GST_V4LMJPEGSINK (gst_pad_get_parent (pad));
254 if (v4lmjpegsink->clock) {
255 GST_DEBUG ("videosink: clock wait: %" G_GUINT64_FORMAT, GST_BUFFER_TIMESTAMP(buf));
257 gst_element_wait (GST_ELEMENT(v4lmjpegsink), GST_BUFFER_TIMESTAMP(buf));
261 if (GST_BUFFER_POOL(buf) == v4lmjpegsink->bufferpool)
263 num = GPOINTER_TO_INT(GST_BUFFER_POOL_PRIVATE(buf));
264 gst_v4lmjpegsink_play_frame(v4lmjpegsink, num);
270 if (GST_BUFFER_SIZE(buf) > v4lmjpegsink->breq.size)
272 GST_ELEMENT_ERROR (v4lmjpegsink, RESOURCE, WRITE, (NULL),
273 ("Buffer too big (%d KB), max. buffersize is %ld KB",
274 GST_BUFFER_SIZE(buf)/1024, v4lmjpegsink->breq.size/1024));
278 /* put JPEG data to the device */
279 gst_v4lmjpegsink_wait_frame(v4lmjpegsink, &num);
280 memcpy(gst_v4lmjpegsink_get_buffer(v4lmjpegsink, num),
281 GST_BUFFER_DATA(buf), GST_BUFFER_SIZE(buf));
282 gst_v4lmjpegsink_play_frame(v4lmjpegsink, num);
287 g_signal_emit(G_OBJECT(v4lmjpegsink),gst_v4lmjpegsink_signals[SIGNAL_FRAME_DISPLAYED],0);
289 gst_buffer_unref(buf);
295 gst_v4lmjpegsink_buffer_new (GstBufferPool *pool,
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,
338 GstV4lMjpegSink *v4lmjpegsink;
340 /* it's not null if we got it, but it might not be ours */
341 g_return_if_fail (GST_IS_V4LMJPEGSINK (object));
343 v4lmjpegsink = GST_V4LMJPEGSINK (object);
348 v4lmjpegsink->numbufs = g_value_get_int(value);
351 v4lmjpegsink->bufsize = g_value_get_int(value);
354 v4lmjpegsink->x_offset = g_value_get_int(value);
357 v4lmjpegsink->y_offset = g_value_get_int(value);
360 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
367 gst_v4lmjpegsink_get_property (GObject *object,
372 GstV4lMjpegSink *v4lmjpegsink;
374 /* it's not null if we got it, but it might not be ours */
375 v4lmjpegsink = GST_V4LMJPEGSINK(object);
378 case ARG_FRAMES_DISPLAYED:
379 g_value_set_int (value, v4lmjpegsink->frames_displayed);
382 g_value_set_int (value, v4lmjpegsink->frame_time/1000000);
385 g_value_set_int (value, v4lmjpegsink->numbufs);
388 g_value_set_int (value, v4lmjpegsink->bufsize);
391 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
397 static GstElementStateReturn
398 gst_v4lmjpegsink_change_state (GstElement *element)
400 GstV4lMjpegSink *v4lmjpegsink;
401 GstElementStateReturn parent_value;
403 g_return_val_if_fail (GST_IS_V4LMJPEGSINK (element), GST_STATE_FAILURE);
404 v4lmjpegsink = GST_V4LMJPEGSINK(element);
406 /* set up change state */
407 switch (GST_STATE_TRANSITION(element)) {
408 case GST_STATE_READY_TO_PAUSED:
409 /* we used to do buffer setup here, but that's now done
410 * right after capsnego */
412 case GST_STATE_PAUSED_TO_PLAYING:
414 if (!gst_v4lmjpegsink_playback_start(v4lmjpegsink))
415 return GST_STATE_FAILURE;
417 case GST_STATE_PLAYING_TO_PAUSED:
418 /* de-queue all queued buffers */
419 if (!gst_v4lmjpegsink_playback_stop(v4lmjpegsink))
420 return GST_STATE_FAILURE;
422 case GST_STATE_PAUSED_TO_READY:
423 /* stop playback, unmap all buffers */
424 if (!gst_v4lmjpegsink_playback_deinit(v4lmjpegsink))
425 return GST_STATE_FAILURE;
429 if (GST_ELEMENT_CLASS (parent_class)->change_state) {
430 parent_value = GST_ELEMENT_CLASS (parent_class)->change_state (element);
432 parent_value = GST_STATE_FAILURE;
435 if (GST_ELEMENT_CLASS (parent_class)->change_state)
438 return GST_STATE_SUCCESS;