1 /* GStreamer
2  * Copyright (C) 2011 David Schleef <ds@entropywave.com>
3  *
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.
8  *
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.
13  *
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., 51 Franklin Street, Suite 500,
17  * Boston, MA 02110-1335, USA.
18  */
19 /**
20  * SECTION:element-gstintersubsink
21  * @title: gstintersubsink
22  *
23  * The intersubsink element is a subtitle sink element.  It is used
24  * in connection with a intersubsrc element in a different pipeline.
25  *
26  * ## Example launch line
27  * |[
28  * gst-launch-1.0 -v ... ! intersubsink
29  * ]|
30  *
31  * The intersubsink element cannot be used effectively with gst-launch-1.0,
32  * as it requires a second pipeline in the application to send audio.
33  * See the gstintertest.c example in the gst-plugins-bad source code for
34  * more details.
35  *
36  */
37 
38 #ifdef HAVE_CONFIG_H
39 #include "config.h"
40 #endif
41 
42 #include <gst/gst.h>
43 #include <gst/base/gstbasesink.h>
44 #include "gstintersubsink.h"
45 
46 GST_DEBUG_CATEGORY_STATIC (gst_inter_sub_sink_debug_category);
47 #define GST_CAT_DEFAULT gst_inter_sub_sink_debug_category
48 
49 /* prototypes */
50 static void gst_inter_sub_sink_set_property (GObject * object,
51     guint property_id, const GValue * value, GParamSpec * pspec);
52 static void gst_inter_sub_sink_get_property (GObject * object,
53     guint property_id, GValue * value, GParamSpec * pspec);
54 static void gst_inter_sub_sink_finalize (GObject * object);
55 
56 static void gst_inter_sub_sink_get_times (GstBaseSink * sink,
57     GstBuffer * buffer, GstClockTime * start, GstClockTime * end);
58 static gboolean gst_inter_sub_sink_start (GstBaseSink * sink);
59 static gboolean gst_inter_sub_sink_stop (GstBaseSink * sink);
60 static GstFlowReturn
61 gst_inter_sub_sink_render (GstBaseSink * sink, GstBuffer * buffer);
62 
63 enum
64 {
65   PROP_0,
66   PROP_CHANNEL
67 };
68 
69 #define DEFAULT_CHANNEL ("default")
70 
71 /* pad templates */
72 static GstStaticPadTemplate gst_inter_sub_sink_sink_template =
73 GST_STATIC_PAD_TEMPLATE ("sink",
74     GST_PAD_SINK,
75     GST_PAD_ALWAYS,
76     GST_STATIC_CAPS ("text/plain")
77     );
78 
79 
80 /* class initialization */
81 #define parent_class gst_inter_sub_sink_parent_class
82 G_DEFINE_TYPE (GstInterSubSink, gst_inter_sub_sink, GST_TYPE_BASE_SINK);
83 
84 static void
gst_inter_sub_sink_class_init(GstInterSubSinkClass * klass)85 gst_inter_sub_sink_class_init (GstInterSubSinkClass * klass)
86 {
87   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
88   GstBaseSinkClass *base_sink_class = GST_BASE_SINK_CLASS (klass);
89   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
90 
91   GST_DEBUG_CATEGORY_INIT (gst_inter_sub_sink_debug_category, "intersubsink", 0,
92       "debug category for intersubsink element");
93 
94   gst_element_class_add_static_pad_template (element_class,
95       &gst_inter_sub_sink_sink_template);
96 
97   gst_element_class_set_static_metadata (element_class,
98       "Internal subtitle sink",
99       "Sink/Subtitle",
100       "Virtual subtitle sink for internal process communication",
101       "David Schleef <ds@schleef.org>");
102 
103   gobject_class->set_property = gst_inter_sub_sink_set_property;
104   gobject_class->get_property = gst_inter_sub_sink_get_property;
105   gobject_class->finalize = gst_inter_sub_sink_finalize;
106   base_sink_class->get_times = GST_DEBUG_FUNCPTR (gst_inter_sub_sink_get_times);
107   base_sink_class->start = GST_DEBUG_FUNCPTR (gst_inter_sub_sink_start);
108   base_sink_class->stop = GST_DEBUG_FUNCPTR (gst_inter_sub_sink_stop);
109   base_sink_class->render = GST_DEBUG_FUNCPTR (gst_inter_sub_sink_render);
110 
111   g_object_class_install_property (gobject_class, PROP_CHANNEL,
112       g_param_spec_string ("channel", "Channel",
113           "Channel name to match inter src and sink elements",
114           DEFAULT_CHANNEL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
115 }
116 
117 static void
gst_inter_sub_sink_init(GstInterSubSink * intersubsink)118 gst_inter_sub_sink_init (GstInterSubSink * intersubsink)
119 {
120   intersubsink->channel = g_strdup (DEFAULT_CHANNEL);
121 
122   intersubsink->fps_n = 1;
123   intersubsink->fps_d = 1;
124 }
125 
126 void
gst_inter_sub_sink_set_property(GObject * object,guint property_id,const GValue * value,GParamSpec * pspec)127 gst_inter_sub_sink_set_property (GObject * object, guint property_id,
128     const GValue * value, GParamSpec * pspec)
129 {
130   GstInterSubSink *intersubsink = GST_INTER_SUB_SINK (object);
131 
132   switch (property_id) {
133     case PROP_CHANNEL:
134       g_free (intersubsink->channel);
135       intersubsink->channel = g_value_dup_string (value);
136       break;
137     default:
138       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
139       break;
140   }
141 }
142 
143 void
gst_inter_sub_sink_get_property(GObject * object,guint property_id,GValue * value,GParamSpec * pspec)144 gst_inter_sub_sink_get_property (GObject * object, guint property_id,
145     GValue * value, GParamSpec * pspec)
146 {
147   GstInterSubSink *intersubsink = GST_INTER_SUB_SINK (object);
148 
149   switch (property_id) {
150     case PROP_CHANNEL:
151       g_value_set_string (value, intersubsink->channel);
152       break;
153     default:
154       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
155       break;
156   }
157 }
158 
159 static void
gst_inter_sub_sink_finalize(GObject * object)160 gst_inter_sub_sink_finalize (GObject * object)
161 {
162   GstInterSubSink *intersubsink = GST_INTER_SUB_SINK (object);
163 
164   g_free (intersubsink->channel);
165   intersubsink->channel = NULL;
166 
167   G_OBJECT_CLASS (parent_class)->finalize (object);
168 }
169 
170 static void
gst_inter_sub_sink_get_times(GstBaseSink * sink,GstBuffer * buffer,GstClockTime * start,GstClockTime * end)171 gst_inter_sub_sink_get_times (GstBaseSink * sink, GstBuffer * buffer,
172     GstClockTime * start, GstClockTime * end)
173 {
174   GstInterSubSink *intersubsink = GST_INTER_SUB_SINK (sink);
175 
176   if (GST_BUFFER_TIMESTAMP_IS_VALID (buffer)) {
177     *start = GST_BUFFER_TIMESTAMP (buffer);
178     if (GST_BUFFER_DURATION_IS_VALID (buffer)) {
179       *end = *start + GST_BUFFER_DURATION (buffer);
180     } else {
181       if (intersubsink->fps_n > 0) {
182         *end = *start +
183             gst_util_uint64_scale_int (GST_SECOND, intersubsink->fps_d,
184             intersubsink->fps_n);
185       }
186     }
187   }
188 }
189 
190 static gboolean
gst_inter_sub_sink_start(GstBaseSink * sink)191 gst_inter_sub_sink_start (GstBaseSink * sink)
192 {
193   GstInterSubSink *intersubsink = GST_INTER_SUB_SINK (sink);
194 
195   intersubsink->surface = gst_inter_surface_get (intersubsink->channel);
196 
197   return TRUE;
198 }
199 
200 static gboolean
gst_inter_sub_sink_stop(GstBaseSink * sink)201 gst_inter_sub_sink_stop (GstBaseSink * sink)
202 {
203   GstInterSubSink *intersubsink = GST_INTER_SUB_SINK (sink);
204 
205   g_mutex_lock (&intersubsink->surface->mutex);
206   if (intersubsink->surface->sub_buffer) {
207     gst_buffer_unref (intersubsink->surface->sub_buffer);
208   }
209   intersubsink->surface->sub_buffer = NULL;
210   g_mutex_unlock (&intersubsink->surface->mutex);
211 
212   gst_inter_surface_unref (intersubsink->surface);
213   intersubsink->surface = NULL;
214 
215   return TRUE;
216 }
217 
218 static GstFlowReturn
gst_inter_sub_sink_render(GstBaseSink * sink,GstBuffer * buffer)219 gst_inter_sub_sink_render (GstBaseSink * sink, GstBuffer * buffer)
220 {
221   GstInterSubSink *intersubsink = GST_INTER_SUB_SINK (sink);
222 
223   g_mutex_lock (&intersubsink->surface->mutex);
224   if (intersubsink->surface->sub_buffer) {
225     gst_buffer_unref (intersubsink->surface->sub_buffer);
226   }
227   intersubsink->surface->sub_buffer = gst_buffer_ref (buffer);
228   //intersubsink->surface->sub_buffer_count = 0;
229   g_mutex_unlock (&intersubsink->surface->mutex);
230 
231   return GST_FLOW_OK;
232 }
233