xpra icon
Bug tracker and wiki

This bug tracker and wiki are being discontinued
please use https://github.com/Xpra-org/xpra instead.


Ticket #1370: gsttimestamp.c

File gsttimestamp.c, 7.4 KB (added by Antoine Martin, 4 years ago)

timestamp plugin

Line 
1/*
2 * GStreamer
3 * Copyright (C) 2005 Thomas Vander Stichele <thomas@apestaart.org>
4 * Copyright (C) 2005 Ronald S. Bultje <rbultje@ronald.bitfreak.net>
5 * Copyright (C) 2017 Antoine Martin <antoine@nagafix.co.uk>
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
24 *
25 * Alternatively, the contents of this file may be used under the
26 * GNU Lesser General Public License Version 2.1 (the "LGPL"), in
27 * which case the following provisions apply instead of the ones
28 * mentioned above:
29 *
30 * This library is free software; you can redistribute it and/or
31 * modify it under the terms of the GNU Library General Public
32 * License as published by the Free Software Foundation; either
33 * version 2 of the License, or (at your option) any later version.
34 *
35 * This library is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
38 * Library General Public License for more details.
39 *
40 * You should have received a copy of the GNU Library General Public
41 * License along with this library; if not, write to the
42 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
43 * Boston, MA 02111-1307, USA.
44 */
45
46/**
47 * SECTION:element-timestamp
48 *
49 * Expose timestamps using an absolute monotonic clock.
50 *
51 * <refsect2>
52 * <title>Example launch line</title>
53 * |[
54 * gst-launch -v -m fakesrc ! timestamp ! fakesink
55 * ]|
56 * </refsect2>
57 */
58
59#define VERSION "1.0.0"
60
61#include <gst/gst.h>
62
63#include "gsttimestamp.h"
64
65GST_DEBUG_CATEGORY_STATIC (gst_timestamp_debug);
66#define GST_CAT_DEFAULT gst_timestamp_debug
67
68/* Filter signals and args */
69enum
70{
71  /* FILL ME */
72  LAST_SIGNAL
73};
74
75enum
76{
77  PROP_0,
78  PROP_DELTA
79};
80
81/* the capabilities of the inputs and outputs.
82 *
83 * describe the real formats here.
84 */
85static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
86    GST_PAD_SINK,
87    GST_PAD_ALWAYS,
88    GST_STATIC_CAPS ("ANY")
89    );
90
91static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
92    GST_PAD_SRC,
93    GST_PAD_ALWAYS,
94    GST_STATIC_CAPS ("ANY")
95    );
96
97#define gst_timestamp_class parent_class
98G_DEFINE_TYPE (GstTimestampFilter, gst_timestamp, GST_TYPE_ELEMENT);
99
100static void gst_timestamp_set_property (GObject * object, guint prop_id,
101    const GValue * value, GParamSpec * pspec);
102static void gst_timestamp_get_property (GObject * object, guint prop_id,
103    GValue * value, GParamSpec * pspec);
104
105static gboolean gst_timestamp_sink_event (GstPad * pad, GstObject * parent, GstEvent * event);
106static GstFlowReturn gst_timestamp_chain (GstPad * pad, GstObject * parent, GstBuffer * buf);
107
108
109/* initialize the timestamp class */
110static void
111gst_timestamp_class_init (GstTimestampFilterClass * klass)
112{
113  GObjectClass *gobject_class;
114  GstElementClass *gstelement_class;
115
116  gobject_class = (GObjectClass *) klass;
117  gstelement_class = (GstElementClass *) klass;
118
119  gobject_class->set_property = gst_timestamp_set_property;
120  gobject_class->get_property = gst_timestamp_get_property;
121
122  g_object_class_install_property (gobject_class, PROP_DELTA,
123      g_param_spec_int64 ("delta", "Delta", "Delta between the buffer timestamps and the system monotonic clock",
124          G_MININT64, G_MAXINT64, 0, G_PARAM_READABLE));
125
126  gst_element_class_set_details_simple(gstelement_class,
127    "Timestamp",
128    "FIXME:Generic",
129    "FIXME:Generic Template Element",
130    " <antoine@nagafix.co.uk>");
131
132  gst_element_class_add_pad_template (gstelement_class,
133      gst_static_pad_template_get (&src_factory));
134  gst_element_class_add_pad_template (gstelement_class,
135      gst_static_pad_template_get (&sink_factory));
136}
137
138/* initialize the new element
139 * instantiate pads and add them to element
140 * set pad calback functions
141 * initialize instance structure
142 */
143static void
144gst_timestamp_init (GstTimestampFilter * filter)
145{
146  filter->sinkpad = gst_pad_new_from_static_template (&sink_factory, "sink");
147  gst_pad_set_event_function (filter->sinkpad,
148                              GST_DEBUG_FUNCPTR(gst_timestamp_sink_event));
149  gst_pad_set_chain_function (filter->sinkpad,
150                              GST_DEBUG_FUNCPTR(gst_timestamp_chain));
151  GST_PAD_SET_PROXY_CAPS (filter->sinkpad);
152  gst_element_add_pad (GST_ELEMENT (filter), filter->sinkpad);
153
154  filter->srcpad = gst_pad_new_from_static_template (&src_factory, "src");
155  GST_PAD_SET_PROXY_CAPS (filter->srcpad);
156  gst_element_add_pad (GST_ELEMENT (filter), filter->srcpad);
157
158  filter->delta = 0;
159}
160
161static void
162gst_timestamp_set_property (GObject * object, guint prop_id,
163    const GValue * value, GParamSpec * pspec)
164{
165  G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
166}
167
168static void
169gst_timestamp_get_property (GObject * object, guint prop_id,
170    GValue * value, GParamSpec * pspec)
171{
172  GstTimestampFilter *filter = GST_TIMESTAMPFILTER (object);
173
174  switch (prop_id) {
175    case PROP_DELTA:
176      g_value_set_int64 (value, filter->delta);
177      break;
178    default:
179      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
180      break;
181  }
182}
183
184
185static gboolean
186gst_timestamp_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
187{
188  return gst_pad_event_default (pad, parent, event);
189}
190
191/* chain function
192 * this function does the actual processing
193 */
194static GstFlowReturn
195gst_timestamp_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
196{
197  GstTimestampFilter *filter;
198
199  filter = GST_TIMESTAMPFILTER (parent);
200  GstClockTime pts = GST_BUFFER_PTS(buf);
201
202  struct timespec ts;
203  clock_gettime(CLOCK_MONOTONIC, &ts);
204  ulong mts = ts.tv_sec*1000000000 + ts.tv_nsec;
205  //ulong delta = mts-pts;
206  //g_print ("pts=%li, mts=%li, delta=%li\n", pts, mts, delta);
207  filter->delta = mts-pts;
208  /* just push out the incoming buffer without touching it */
209  return gst_pad_push (filter->srcpad, buf);
210}
211
212
213/* entry point to initialize the plug-in
214 * initialize the plug-in itself
215 * register the element factories and other features
216 */
217static gboolean
218timestamp_init (GstPlugin *timestamp)
219{
220  /* debug category for fltering log messages
221   */
222  GST_DEBUG_CATEGORY_INIT (gst_timestamp_debug, "timestamp",
223      0, "timestamp interceptor");
224
225  return gst_element_register (timestamp, "timestamp", GST_RANK_NONE,
226      GST_TYPE_TIMESTAMPFILTER);
227}
228
229#define PACKAGE "timestamp"
230
231/* gstreamer looks for this structure to register timestamp
232 *
233 */
234GST_PLUGIN_DEFINE (
235    GST_VERSION_MAJOR,
236    GST_VERSION_MINOR,
237    timestamp,
238    "timestamp",
239    timestamp_init,
240    VERSION,
241    "LGPL",
242    "GStreamer",
243    "http://gstreamer.net/"
244)