static const gchar * gst_fd_sink_uri_get_uri (GstURIHandler * handler) { GstFdSink *sink = GST_FD_SINK (handler); return sink->uri; }
static gboolean gst_fd_sink_start (GstBaseSink * basesink) { GstFdSink *fdsink; GstPollFD fd = GST_POLL_FD_INIT; fdsink = GST_FD_SINK (basesink); if (!gst_fd_sink_check_fd (fdsink, fdsink->fd, NULL)) return FALSE; if ((fdsink->fdset = gst_poll_new (TRUE)) == NULL) goto socket_pair; fd.fd = fdsink->fd; gst_poll_add_fd (fdsink->fdset, &fd); gst_poll_fd_ctl_write (fdsink->fdset, &fd, TRUE); fdsink->bytes_written = 0; fdsink->current_pos = 0; fdsink->seekable = gst_fd_sink_do_seek (fdsink, 0); GST_INFO_OBJECT (fdsink, "seeking supported: %d", fdsink->seekable); return TRUE; /* ERRORS */ socket_pair: { GST_ELEMENT_ERROR (fdsink, RESOURCE, OPEN_READ_WRITE, (NULL), GST_ERROR_SYSTEM); return FALSE; } }
static gboolean gst_fd_sink_query (GstPad * pad, GstQuery * query) { GstFdSink *fdsink; GstFormat format; fdsink = GST_FD_SINK (GST_PAD_PARENT (pad)); switch (GST_QUERY_TYPE (query)) { case GST_QUERY_POSITION: gst_query_parse_position (query, &format, NULL); switch (format) { case GST_FORMAT_DEFAULT: case GST_FORMAT_BYTES: gst_query_set_position (query, GST_FORMAT_BYTES, fdsink->current_pos); return TRUE; default: return FALSE; } case GST_QUERY_FORMATS: gst_query_set_formats (query, 2, GST_FORMAT_DEFAULT, GST_FORMAT_BYTES); return TRUE; case GST_QUERY_URI: gst_query_set_uri (query, fdsink->uri); return TRUE; default: return gst_pad_query_default (pad, query); } }
static gchar * gst_fd_sink_uri_get_uri (GstURIHandler * handler) { GstFdSink *sink = GST_FD_SINK (handler); /* FIXME: make thread-safe */ return g_strdup (sink->uri); }
static void gst_fd_sink_dispose (GObject * obj) { GstFdSink *fdsink = GST_FD_SINK (obj); g_free (fdsink->uri); fdsink->uri = NULL; G_OBJECT_CLASS (parent_class)->dispose (obj); }
static gboolean gst_fd_sink_query (GstBaseSink * bsink, GstQuery * query) { gboolean res = FALSE; GstFdSink *fdsink; fdsink = GST_FD_SINK (bsink); switch (GST_QUERY_TYPE (query)) { case GST_QUERY_POSITION: { GstFormat format; gst_query_parse_position (query, &format, NULL); switch (format) { case GST_FORMAT_DEFAULT: case GST_FORMAT_BYTES: gst_query_set_position (query, GST_FORMAT_BYTES, fdsink->current_pos); res = TRUE; break; default: break; } break; } case GST_QUERY_FORMATS: gst_query_set_formats (query, 2, GST_FORMAT_DEFAULT, GST_FORMAT_BYTES); res = TRUE; break; case GST_QUERY_URI: gst_query_set_uri (query, fdsink->uri); res = TRUE; break; case GST_QUERY_SEEKING:{ GstFormat format; gst_query_parse_seeking (query, &format, NULL, NULL, NULL); if (format == GST_FORMAT_BYTES || format == GST_FORMAT_DEFAULT) { gst_query_set_seeking (query, GST_FORMAT_BYTES, fdsink->seekable, 0, -1); } else { gst_query_set_seeking (query, format, FALSE, 0, -1); } res = TRUE; break; } default: res = GST_BASE_SINK_CLASS (parent_class)->query (bsink, query); break; } return res; }
static gboolean gst_fd_sink_stop (GstBaseSink * basesink) { GstFdSink *fdsink = GST_FD_SINK (basesink); if (fdsink->fdset) { gst_poll_free (fdsink->fdset); fdsink->fdset = NULL; } return TRUE; }
static gboolean gst_fd_sink_unlock_stop (GstBaseSink * basesink) { GstFdSink *fdsink = GST_FD_SINK (basesink); GST_LOG_OBJECT (fdsink, "No longer flushing"); GST_OBJECT_LOCK (fdsink); gst_poll_set_flushing (fdsink->fdset, FALSE); GST_OBJECT_UNLOCK (fdsink); return TRUE; }
static gboolean gst_fd_sink_unlock (GstBaseSink * basesink) { GstFdSink *fdsink = GST_FD_SINK (basesink); GST_LOG_OBJECT (fdsink, "Flushing"); GST_OBJECT_LOCK (fdsink); fdsink->unlock = TRUE; gst_poll_set_flushing (fdsink->fdset, TRUE); GST_OBJECT_UNLOCK (fdsink); return TRUE; }
static gboolean gst_fd_sink_event (GstBaseSink * sink, GstEvent * event) { GstEventType type; GstFdSink *fdsink; fdsink = GST_FD_SINK (sink); type = GST_EVENT_TYPE (event); switch (type) { case GST_EVENT_SEGMENT: { const GstSegment *segment; gst_event_parse_segment (event, &segment); if (segment->format == GST_FORMAT_BYTES) { /* only try to seek and fail when we are going to a different * position */ if (fdsink->current_pos != segment->start) { /* FIXME, the seek should be performed on the pos field, start/stop are * just boundaries for valid bytes offsets. We should also fill the file * with zeroes if the new position extends the current EOF (sparse streams * and segment accumulation). */ if (!gst_fd_sink_do_seek (fdsink, (guint64) segment->start)) goto seek_failed; } } else { GST_DEBUG_OBJECT (fdsink, "Ignored SEGMENT event of format %u (%s)", (guint) segment->format, gst_format_get_name (segment->format)); } break; } default: break; } return GST_BASE_SINK_CLASS (parent_class)->event (sink, event); seek_failed: { GST_ELEMENT_ERROR (fdsink, RESOURCE, SEEK, (NULL), ("Error while seeking on file descriptor %d: %s", fdsink->fd, g_strerror (errno))); gst_event_unref (event); return FALSE; } }
static gboolean gst_fd_sink_uri_set_uri (GstURIHandler * handler, const gchar * uri, GError ** error) { GstFdSink *sink = GST_FD_SINK (handler); gint fd; if (sscanf (uri, "fd://%d", &fd) != 1) { g_set_error (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI, "File descriptor URI could not be parsed"); return FALSE; } return gst_fd_sink_update_fd (sink, fd, error); }
static void gst_fd_sink_get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec) { GstFdSink *fdsink; fdsink = GST_FD_SINK (object); switch (prop_id) { case ARG_FD: g_value_set_int (value, fdsink->fd); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); break; } }
static void gst_fd_sink_get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec) { GstFdSink *fdsink; g_return_if_fail (GST_IS_FD_SINK (object)); fdsink = GST_FD_SINK (object); switch (prop_id) { case ARG_FD: g_value_set_int (value, fdsink->fd); break; default: break; } }
static gboolean gst_fd_sink_uri_set_uri (GstURIHandler * handler, const gchar * uri) { gchar *protocol; GstFdSink *sink = GST_FD_SINK (handler); gint fd; protocol = gst_uri_get_protocol (uri); if (strcmp (protocol, "fd") != 0) { g_free (protocol); return FALSE; } g_free (protocol); if (sscanf (uri, "fd://%d", &fd) != 1) return FALSE; return gst_fd_sink_update_fd (sink, fd); }
static void gst_fd_sink_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec) { GstFdSink *fdsink; fdsink = GST_FD_SINK (object); switch (prop_id) { case ARG_FD:{ int fd; fd = g_value_get_int (value); gst_fd_sink_update_fd (fdsink, fd, NULL); break; } default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); break; } }
static void gst_fd_sink_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec) { GstFdSink *fdsink; g_return_if_fail (GST_IS_FD_SINK (object)); fdsink = GST_FD_SINK (object); switch (prop_id) { case ARG_FD:{ int fd; fd = g_value_get_int (value); gst_fd_sink_update_fd (fdsink, fd); break; } default: break; } }
static GstFlowReturn gst_fd_sink_render (GstBaseSink * sink, GstBuffer * buffer) { GstFdSink *fdsink; guint8 *data; guint size; gint written; #ifndef HAVE_WIN32 gint retval; #endif fdsink = GST_FD_SINK (sink); g_return_val_if_fail (fdsink->fd >= 0, GST_FLOW_ERROR); data = GST_BUFFER_DATA (buffer); size = GST_BUFFER_SIZE (buffer); again: #ifndef HAVE_WIN32 do { GST_DEBUG_OBJECT (fdsink, "going into select, have %d bytes to write", size); retval = gst_poll_wait (fdsink->fdset, GST_CLOCK_TIME_NONE); } while (retval == -1 && (errno == EINTR || errno == EAGAIN)); if (retval == -1) { if (errno == EBUSY) goto stopped; else goto select_error; } #endif GST_DEBUG_OBJECT (fdsink, "writing %d bytes to file descriptor %d", size, fdsink->fd); written = write (fdsink->fd, data, size); /* check for errors */ if (G_UNLIKELY (written < 0)) { /* try to write again on non-fatal errors */ if (errno == EAGAIN || errno == EINTR) goto again; /* else go to our error handler */ goto write_error; } /* all is fine when we get here */ size -= written; data += written; fdsink->bytes_written += written; fdsink->current_pos += written; GST_DEBUG_OBJECT (fdsink, "wrote %d bytes, %d left", written, size); /* short write, select and try to write the remainder */ if (G_UNLIKELY (size > 0)) goto again; return GST_FLOW_OK; #ifndef HAVE_WIN32 select_error: { GST_ELEMENT_ERROR (fdsink, RESOURCE, READ, (NULL), ("select on file descriptor: %s.", g_strerror (errno))); GST_DEBUG_OBJECT (fdsink, "Error during select"); return GST_FLOW_ERROR; } stopped: { GST_DEBUG_OBJECT (fdsink, "Select stopped"); return GST_FLOW_WRONG_STATE; } #endif write_error: { switch (errno) { case ENOSPC: GST_ELEMENT_ERROR (fdsink, RESOURCE, NO_SPACE_LEFT, (NULL), (NULL)); break; default:{ GST_ELEMENT_ERROR (fdsink, RESOURCE, WRITE, (NULL), ("Error while writing to file descriptor %d: %s", fdsink->fd, g_strerror (errno))); } } return GST_FLOW_ERROR; } }
static GstFlowReturn gst_fd_sink_render (GstBaseSink * sink, GstBuffer * buffer) { GstFdSink *fdsink; GstMapInfo info; guint8 *ptr; gsize left; gint written; #ifndef HAVE_WIN32 gint retval; #endif fdsink = GST_FD_SINK (sink); g_return_val_if_fail (fdsink->fd >= 0, GST_FLOW_ERROR); gst_buffer_map (buffer, &info, GST_MAP_READ); ptr = info.data; left = info.size; again: #ifndef HAVE_WIN32 do { GST_DEBUG_OBJECT (fdsink, "going into select, have %" G_GSIZE_FORMAT " bytes to write", info.size); retval = gst_poll_wait (fdsink->fdset, GST_CLOCK_TIME_NONE); } while (retval == -1 && (errno == EINTR || errno == EAGAIN)); if (retval == -1) { if (errno == EBUSY) goto stopped; else goto select_error; } #endif GST_DEBUG_OBJECT (fdsink, "writing %" G_GSIZE_FORMAT " bytes to" " file descriptor %d", info.size, fdsink->fd); written = write (fdsink->fd, ptr, left); /* check for errors */ if (G_UNLIKELY (written < 0)) { /* try to write again on non-fatal errors */ if (errno == EAGAIN || errno == EINTR) goto again; /* else go to our error handler */ goto write_error; } /* all is fine when we get here */ left -= written; ptr += written; fdsink->bytes_written += written; fdsink->current_pos += written; GST_DEBUG_OBJECT (fdsink, "wrote %d bytes, %" G_GSIZE_FORMAT " left", written, left); /* short write, select and try to write the remainder */ if (G_UNLIKELY (left > 0)) goto again; gst_buffer_unmap (buffer, &info); return GST_FLOW_OK; #ifndef HAVE_WIN32 select_error: { GST_ELEMENT_ERROR (fdsink, RESOURCE, READ, (NULL), ("select on file descriptor: %s.", g_strerror (errno))); GST_DEBUG_OBJECT (fdsink, "Error during select"); gst_buffer_unmap (buffer, &info); return GST_FLOW_ERROR; } stopped: { GST_DEBUG_OBJECT (fdsink, "Select stopped"); gst_buffer_unmap (buffer, &info); return GST_FLOW_FLUSHING; } #endif write_error: { switch (errno) { case ENOSPC: GST_ELEMENT_ERROR (fdsink, RESOURCE, NO_SPACE_LEFT, (NULL), (NULL)); break; default:{ GST_ELEMENT_ERROR (fdsink, RESOURCE, WRITE, (NULL), ("Error while writing to file descriptor %d: %s", fdsink->fd, g_strerror (errno))); } } gst_buffer_unmap (buffer, &info); return GST_FLOW_ERROR; } }