2 * Copyright (C) <2009> Collabora Ltd
3 * @author: Olivier Crete <olivier.crete@collabora.co.uk
4 * Copyright (C) <2009> Nokia Inc
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
26 #include "gstshmsrc.h"
53 GST_DEBUG_CATEGORY_STATIC (shmsrc_debug);
54 #define GST_CAT_DEFAULT shmsrc_debug
56 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
61 GST_BOILERPLATE (GstShmSrc, gst_shm_src, GstPushSrc, GST_TYPE_PUSH_SRC);
63 static void gst_shm_src_set_property (GObject * object, guint prop_id,
64 const GValue * value, GParamSpec * pspec);
65 static void gst_shm_src_get_property (GObject * object, guint prop_id,
66 GValue * value, GParamSpec * pspec);
67 static void gst_shm_src_finalize (GObject * object);
68 static gboolean gst_shm_src_start (GstBaseSrc * bsrc);
69 static gboolean gst_shm_src_stop (GstBaseSrc * bsrc);
70 static GstFlowReturn gst_shm_src_create (GstPushSrc * psrc,
72 static gboolean gst_shm_src_unlock (GstBaseSrc * bsrc);
73 static gboolean gst_shm_src_unlock_stop (GstBaseSrc * bsrc);
74 static GstStateChangeReturn gst_shm_src_change_state (GstElement * element,
75 GstStateChange transition);
77 static void gst_shm_pipe_inc (GstShmPipe * pipe);
78 static void gst_shm_pipe_dec (GstShmPipe * pipe);
80 // static guint gst_shm_src_signals[LAST_SIGNAL] = { 0 };
84 gst_shm_src_base_init (gpointer g_class)
86 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
88 gst_element_class_add_static_pad_template (element_class, &srctemplate);
90 gst_element_class_set_details_simple (element_class,
91 "Shared Memory Source",
93 "Receive data from the sharem memory sink",
94 "Olivier Crete <olivier.crete@collabora.co.uk>");
98 gst_shm_src_class_init (GstShmSrcClass * klass)
100 GObjectClass *gobject_class;
101 GstElementClass *gstelement_class;
102 GstBaseSrcClass *gstbasesrc_class;
103 GstPushSrcClass *gstpush_src_class;
105 gobject_class = (GObjectClass *) klass;
106 gstelement_class = (GstElementClass *) klass;
107 gstbasesrc_class = (GstBaseSrcClass *) klass;
108 gstpush_src_class = (GstPushSrcClass *) klass;
110 gobject_class->set_property = gst_shm_src_set_property;
111 gobject_class->get_property = gst_shm_src_get_property;
112 gobject_class->finalize = gst_shm_src_finalize;
114 gstelement_class->change_state = gst_shm_src_change_state;
116 gstbasesrc_class->start = GST_DEBUG_FUNCPTR (gst_shm_src_start);
117 gstbasesrc_class->stop = GST_DEBUG_FUNCPTR (gst_shm_src_stop);
118 gstbasesrc_class->unlock = GST_DEBUG_FUNCPTR (gst_shm_src_unlock);
119 gstbasesrc_class->unlock_stop = GST_DEBUG_FUNCPTR (gst_shm_src_unlock_stop);
121 gstpush_src_class->create = gst_shm_src_create;
123 g_object_class_install_property (gobject_class, PROP_SOCKET_PATH,
124 g_param_spec_string ("socket-path",
125 "Path to the control socket",
126 "The path to the control socket used to control the shared memory"
127 " transport", NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
129 g_object_class_install_property (gobject_class, PROP_IS_LIVE,
130 g_param_spec_boolean ("is-live", "Is this a live source",
131 "True if the element cannot produce data in PAUSED", FALSE,
132 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
134 GST_DEBUG_CATEGORY_INIT (shmsrc_debug, "shmsrc", 0, "Shared Memory Source");
138 gst_shm_src_init (GstShmSrc * self, GstShmSrcClass * g_class)
140 self->poll = gst_poll_new (TRUE);
141 gst_poll_fd_init (&self->pollfd);
145 gst_shm_src_finalize (GObject * object)
147 GstShmSrc *self = GST_SHM_SRC (object);
149 gst_poll_free (self->poll);
151 G_OBJECT_CLASS (parent_class)->finalize (object);
156 gst_shm_src_set_property (GObject * object, guint prop_id,
157 const GValue * value, GParamSpec * pspec)
159 GstShmSrc *self = GST_SHM_SRC (object);
162 case PROP_SOCKET_PATH:
163 GST_OBJECT_LOCK (object);
165 GST_WARNING_OBJECT (object, "Can not modify socket path while the "
166 "element is playing");
168 g_free (self->socket_path);
169 self->socket_path = g_value_dup_string (value);
171 GST_OBJECT_UNLOCK (object);
174 gst_base_src_set_live (GST_BASE_SRC (object),
175 g_value_get_boolean (value));
178 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
184 gst_shm_src_get_property (GObject * object, guint prop_id,
185 GValue * value, GParamSpec * pspec)
187 GstShmSrc *self = GST_SHM_SRC (object);
190 case PROP_SOCKET_PATH:
191 GST_OBJECT_LOCK (object);
192 g_value_set_string (value, self->socket_path);
193 GST_OBJECT_UNLOCK (object);
196 g_value_set_boolean (value, gst_base_src_is_live (GST_BASE_SRC (object)));
199 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
205 gst_shm_src_start_reading (GstShmSrc * self)
207 GstShmPipe *gstpipe = g_slice_new0 (GstShmPipe);
209 gstpipe->use_count = 1;
210 gstpipe->src = gst_object_ref (self);
212 if (!self->socket_path) {
213 GST_ELEMENT_ERROR (self, RESOURCE, NOT_FOUND,
214 ("No path specified for socket."), (NULL));
218 GST_DEBUG_OBJECT (self, "Opening socket %s", self->socket_path);
220 GST_OBJECT_LOCK (self);
221 gstpipe->pipe = sp_client_open (self->socket_path);
222 GST_OBJECT_UNLOCK (self);
224 if (!gstpipe->pipe) {
225 GST_ELEMENT_ERROR (self, RESOURCE, OPEN_READ_WRITE,
226 ("Could not open socket %s: %d %s", self->socket_path, errno,
227 strerror (errno)), (NULL));
228 gst_shm_pipe_dec (gstpipe);
232 self->pipe = gstpipe;
234 gst_poll_set_flushing (self->poll, FALSE);
236 gst_poll_fd_init (&self->pollfd);
237 self->pollfd.fd = sp_get_fd (self->pipe->pipe);
238 gst_poll_add_fd (self->poll, &self->pollfd);
239 gst_poll_fd_ctl_read (self->poll, &self->pollfd, TRUE);
245 gst_shm_src_stop_reading (GstShmSrc * self)
247 GST_DEBUG_OBJECT (self, "Stopping %p", self);
250 gst_shm_pipe_dec (self->pipe);
254 gst_poll_remove_fd (self->poll, &self->pollfd);
255 gst_poll_fd_init (&self->pollfd);
257 gst_poll_set_flushing (self->poll, TRUE);
261 gst_shm_src_start (GstBaseSrc * bsrc)
263 if (gst_base_src_is_live (bsrc))
266 return gst_shm_src_start_reading (GST_SHM_SRC (bsrc));
270 gst_shm_src_stop (GstBaseSrc * bsrc)
272 if (!gst_base_src_is_live (bsrc))
273 gst_shm_src_stop_reading (GST_SHM_SRC (bsrc));
280 free_buffer (gpointer data)
282 struct GstShmBuffer *gsb = data;
283 g_return_if_fail (gsb->pipe != NULL);
284 g_return_if_fail (gsb->pipe->src != NULL);
286 GST_LOG ("Freeing buffer %p", gsb->buf);
288 GST_OBJECT_LOCK (gsb->pipe->src);
289 sp_client_recv_finish (gsb->pipe->pipe, gsb->buf);
290 GST_OBJECT_UNLOCK (gsb->pipe->src);
292 gst_shm_pipe_dec (gsb->pipe);
294 g_slice_free (struct GstShmBuffer, gsb);
298 gst_shm_src_create (GstPushSrc * psrc, GstBuffer ** outbuf)
300 GstShmSrc *self = GST_SHM_SRC (psrc);
303 struct GstShmBuffer *gsb;
306 if (gst_poll_wait (self->poll, GST_CLOCK_TIME_NONE) < 0) {
308 return GST_FLOW_WRONG_STATE;
309 GST_ELEMENT_ERROR (self, RESOURCE, READ, ("Failed to read from shmsrc"),
310 ("Poll failed on fd: %s", strerror (errno)));
311 return GST_FLOW_ERROR;
315 return GST_FLOW_WRONG_STATE;
317 if (gst_poll_fd_has_closed (self->poll, &self->pollfd)) {
318 GST_ELEMENT_ERROR (self, RESOURCE, READ, ("Failed to read from shmsrc"),
319 ("Control socket has closed"));
320 return GST_FLOW_ERROR;
323 if (gst_poll_fd_has_error (self->poll, &self->pollfd)) {
324 GST_ELEMENT_ERROR (self, RESOURCE, READ, ("Failed to read from shmsrc"),
325 ("Control socket has error"));
326 return GST_FLOW_ERROR;
329 if (gst_poll_fd_can_read (self->poll, &self->pollfd)) {
331 GST_LOG_OBJECT (self, "Reading from pipe");
332 GST_OBJECT_LOCK (self);
333 rv = sp_client_recv (self->pipe->pipe, &buf);
334 GST_OBJECT_UNLOCK (self);
336 GST_ELEMENT_ERROR (self, RESOURCE, READ, ("Failed to read from shmsrc"),
337 ("Error reading control data: %d", rv));
338 return GST_FLOW_ERROR;
341 } while (buf == NULL);
343 GST_LOG_OBJECT (self, "Got buffer %p of size %d", buf, rv);
345 gsb = g_slice_new0 (struct GstShmBuffer);
347 gsb->pipe = self->pipe;
348 gst_shm_pipe_inc (self->pipe);
350 *outbuf = gst_buffer_new ();
351 GST_BUFFER_FLAG_SET (*outbuf, GST_BUFFER_FLAG_READONLY);
352 GST_BUFFER_DATA (*outbuf) = (guint8 *) buf;
353 GST_BUFFER_SIZE (*outbuf) = rv;
354 GST_BUFFER_MALLOCDATA (*outbuf) = (guint8 *) gsb;
355 GST_BUFFER_FREE_FUNC (*outbuf) = free_buffer;
360 static GstStateChangeReturn
361 gst_shm_src_change_state (GstElement * element, GstStateChange transition)
363 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
364 GstShmSrc *self = GST_SHM_SRC (element);
366 switch (transition) {
367 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
368 if (gst_base_src_is_live (GST_BASE_SRC (element)))
369 if (!gst_shm_src_start_reading (self))
370 return GST_STATE_CHANGE_FAILURE;
375 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
376 if (ret == GST_STATE_CHANGE_FAILURE)
379 switch (transition) {
380 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
381 if (gst_base_src_is_live (GST_BASE_SRC (element)))
382 gst_shm_src_stop_reading (self);
391 gst_shm_src_unlock (GstBaseSrc * bsrc)
393 GstShmSrc *self = GST_SHM_SRC (bsrc);
395 self->unlocked = TRUE;
396 gst_poll_set_flushing (self->poll, TRUE);
402 gst_shm_src_unlock_stop (GstBaseSrc * bsrc)
404 GstShmSrc *self = GST_SHM_SRC (bsrc);
406 self->unlocked = FALSE;
407 gst_poll_set_flushing (self->poll, FALSE);
413 gst_shm_pipe_inc (GstShmPipe * pipe)
415 g_return_if_fail (pipe);
416 g_return_if_fail (pipe->src);
417 g_return_if_fail (pipe->use_count > 0);
419 GST_OBJECT_LOCK (pipe->src);
421 GST_OBJECT_UNLOCK (pipe->src);
425 gst_shm_pipe_dec (GstShmPipe * pipe)
427 g_return_if_fail (pipe);
428 g_return_if_fail (pipe->src);
429 g_return_if_fail (pipe->use_count > 0);
431 GST_OBJECT_LOCK (pipe->src);
434 if (pipe->use_count > 0) {
435 GST_OBJECT_UNLOCK (pipe->src);
440 sp_close (pipe->pipe);
441 GST_OBJECT_UNLOCK (pipe->src);
443 gst_object_unref (pipe->src);
444 g_slice_free (GstShmPipe, pipe);