static gboolean gst_webvtt_enc_event (GstPad * pad, GstObject * parent, GstEvent * event) { GstWebvttEnc *webvttenc = GST_WEBVTT_ENC (parent); gboolean ret; switch (GST_EVENT_TYPE (event)) { case GST_EVENT_CAPS: { GstCaps *caps; caps = gst_static_pad_template_get_caps (&src_template); gst_pad_set_caps (webvttenc->srcpad, caps); gst_caps_unref (caps); gst_event_unref (event); ret = TRUE; break; } default: ret = gst_pad_event_default (pad, parent, event); break; } return ret; }
static GstFlowReturn gst_webvtt_enc_chain (GstPad * pad, GstBuffer * buf) { GstWebvttEnc *webvttenc; GstBuffer *new_buffer; gchar *timing; GstFlowReturn ret; webvttenc = GST_WEBVTT_ENC (gst_pad_get_parent_element (pad)); if (!webvttenc->pushed_header) { const char *header = "WEBVTT\n\n"; new_buffer = gst_buffer_new_and_alloc (strlen (header)); memcpy (GST_BUFFER_DATA (new_buffer), header, strlen (header)); GST_BUFFER_TIMESTAMP (new_buffer) = GST_CLOCK_TIME_NONE; GST_BUFFER_DURATION (new_buffer) = GST_CLOCK_TIME_NONE; ret = gst_pad_push (webvttenc->srcpad, new_buffer); if (ret != GST_FLOW_OK) { goto out; } webvttenc->pushed_header = TRUE; } gst_object_sync_values (G_OBJECT (webvttenc), GST_BUFFER_TIMESTAMP (buf)); timing = gst_webvtt_enc_timeconvertion (webvttenc, buf); new_buffer = gst_buffer_new_and_alloc (strlen (timing) + GST_BUFFER_SIZE (buf) + 1); memcpy (GST_BUFFER_DATA (new_buffer), timing, strlen (timing)); memcpy (GST_BUFFER_DATA (new_buffer) + strlen (timing), GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf)); memcpy (GST_BUFFER_DATA (new_buffer) + GST_BUFFER_SIZE (new_buffer) - 1, "\n", 1); g_free (timing); GST_BUFFER_TIMESTAMP (new_buffer) = GST_BUFFER_TIMESTAMP (buf); GST_BUFFER_DURATION (new_buffer) = GST_BUFFER_DURATION (buf); ret = gst_pad_push (webvttenc->srcpad, new_buffer); out: gst_buffer_unref (buf); gst_object_unref (webvttenc); return ret; }
static void gst_webvtt_enc_get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec) { GstWebvttEnc *webvttenc; webvttenc = GST_WEBVTT_ENC (object); switch (prop_id) { case ARG_TIMESTAMP: g_value_set_int64 (value, webvttenc->timestamp); break; case ARG_DURATION: g_value_set_int64 (value, webvttenc->duration); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); break; } }
static GstStateChangeReturn gst_webvtt_enc_change_state (GstElement * element, GstStateChange transition) { GstStateChangeReturn ret; GstWebvttEnc *webvttenc = GST_WEBVTT_ENC (element); ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition); if (ret == GST_STATE_CHANGE_FAILURE) return ret; switch (transition) { case GST_STATE_CHANGE_PAUSED_TO_READY: gst_webvtt_enc_reset (webvttenc); break; default: break; } return ret; }
static GstFlowReturn gst_webvtt_enc_chain (GstPad * pad, GstObject * parent, GstBuffer * buf) { GstWebvttEnc *webvttenc = GST_WEBVTT_ENC (parent); GstClockTime ts, dur = GST_SECOND; GstBuffer *new_buffer; GstMapInfo map_info; GstFlowReturn ret; GString *s; gsize buf_size; if (!webvttenc->pushed_header) { const char *header = "WEBVTT\n\n"; new_buffer = gst_buffer_new_wrapped (g_strdup (header), strlen (header)); GST_BUFFER_PTS (new_buffer) = GST_CLOCK_TIME_NONE; GST_BUFFER_DURATION (new_buffer) = GST_CLOCK_TIME_NONE; ret = gst_pad_push (webvttenc->srcpad, new_buffer); if (ret != GST_FLOW_OK) goto out; webvttenc->pushed_header = TRUE; } gst_object_sync_values (GST_OBJECT (webvttenc), GST_BUFFER_PTS (buf)); ts = GST_BUFFER_PTS (buf) + webvttenc->timestamp; if (GST_BUFFER_DURATION_IS_VALID (buf)) dur = GST_BUFFER_DURATION (buf) + webvttenc->duration; else if (webvttenc->duration > 0) dur = webvttenc->duration; else dur = GST_SECOND; buf_size = gst_buffer_get_size (buf); s = g_string_sized_new (50 + buf_size + 1 + 1); /* start_time --> end_time */ gst_webvtt_enc_append_timestamp_to_string (ts, s); g_string_append_printf (s, " --> "); gst_webvtt_enc_append_timestamp_to_string (ts + dur, s); g_string_append_c (s, '\n'); /* text */ if (gst_buffer_map (buf, &map_info, GST_MAP_READ)) { g_string_append_len (s, (const gchar *) map_info.data, map_info.size); gst_buffer_unmap (buf, &map_info); } g_string_append_c (s, '\n'); buf_size = s->len; new_buffer = gst_buffer_new_wrapped (g_string_free (s, FALSE), buf_size); GST_BUFFER_TIMESTAMP (new_buffer) = GST_BUFFER_TIMESTAMP (buf); GST_BUFFER_DURATION (new_buffer) = GST_BUFFER_DURATION (buf); ret = gst_pad_push (webvttenc->srcpad, new_buffer); out: gst_buffer_unref (buf); return ret; }