2016-09-12 12:28:51 +02:00
|
|
|
/* GStreamer
|
|
|
|
|
*
|
2018-11-05 17:48:52 +01:00
|
|
|
* Copyright © 2018 Wim Taymans
|
2016-09-12 12:28:51 +02:00
|
|
|
*
|
2018-11-05 17:48:52 +01:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
2016-09-12 12:28:51 +02:00
|
|
|
*
|
2018-11-05 17:48:52 +01:00
|
|
|
* The above copyright notice and this permission notice (including the next
|
|
|
|
|
* paragraph) shall be included in all copies or substantial portions of the
|
|
|
|
|
* Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
2016-09-12 12:28:51 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
#include "config.h"
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include <gst/gst.h>
|
|
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
#include "gstpipewireclock.h"
|
2016-09-12 12:28:51 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
GST_DEBUG_CATEGORY_STATIC (gst_pipewire_clock_debug_category);
|
|
|
|
|
#define GST_CAT_DEFAULT gst_pipewire_clock_debug_category
|
2016-09-12 12:28:51 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
G_DEFINE_TYPE (GstPipeWireClock, gst_pipewire_clock, GST_TYPE_SYSTEM_CLOCK);
|
2016-09-12 12:28:51 +02:00
|
|
|
|
|
|
|
|
GstClock *
|
2018-08-13 15:19:20 +02:00
|
|
|
gst_pipewire_clock_new (struct pw_stream *stream, GstClockTime last_time)
|
2016-09-12 12:28:51 +02:00
|
|
|
{
|
2017-05-23 19:15:33 +02:00
|
|
|
GstPipeWireClock *clock;
|
2016-09-12 12:28:51 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
clock = g_object_new (GST_TYPE_PIPEWIRE_CLOCK, NULL);
|
2016-09-12 12:28:51 +02:00
|
|
|
clock->stream = stream;
|
2018-08-13 15:19:20 +02:00
|
|
|
clock->last_time = last_time;
|
2019-03-11 15:09:28 +01:00
|
|
|
clock->time_offset = last_time;
|
2016-09-12 12:28:51 +02:00
|
|
|
|
|
|
|
|
return GST_CLOCK_CAST (clock);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static GstClockTime
|
2017-05-23 19:15:33 +02:00
|
|
|
gst_pipewire_clock_get_internal_time (GstClock * clock)
|
2016-09-12 12:28:51 +02:00
|
|
|
{
|
2017-05-23 19:15:33 +02:00
|
|
|
GstPipeWireClock *pclock = (GstPipeWireClock *) clock;
|
2016-09-12 12:28:51 +02:00
|
|
|
GstClockTime result;
|
2017-05-23 19:15:33 +02:00
|
|
|
struct pw_time t;
|
2018-08-15 21:28:21 +02:00
|
|
|
struct timespec ts;
|
2016-09-12 12:28:51 +02:00
|
|
|
|
2018-08-13 15:19:20 +02:00
|
|
|
if (pclock->stream == NULL ||
|
|
|
|
|
pw_stream_get_time (pclock->stream, &t) < 0 ||
|
|
|
|
|
t.rate.denom == 0)
|
|
|
|
|
return pclock->last_time;
|
2016-09-12 12:28:51 +02:00
|
|
|
|
2018-08-15 21:28:21 +02:00
|
|
|
result = gst_util_uint64_scale_int (t.ticks, GST_SECOND * t.rate.num, t.rate.denom);
|
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &ts);
|
2018-11-07 09:56:24 +01:00
|
|
|
result += SPA_TIMESPEC_TO_NSEC(&ts) - t.now;
|
2018-08-15 15:51:35 +02:00
|
|
|
|
2019-03-11 15:09:28 +01:00
|
|
|
result += pclock->time_offset;
|
|
|
|
|
pclock->last_time = result;
|
|
|
|
|
|
2018-08-15 21:28:21 +02:00
|
|
|
GST_DEBUG ("%"PRId64", %d/%d %"PRId64" %"PRId64,
|
2019-12-18 11:29:11 +01:00
|
|
|
t.ticks, t.rate.num, t.rate.denom, t.now, result);
|
2016-09-12 12:28:51 +02:00
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2017-05-23 19:15:33 +02:00
|
|
|
gst_pipewire_clock_finalize (GObject * object)
|
2016-09-12 12:28:51 +02:00
|
|
|
{
|
2017-05-23 19:15:33 +02:00
|
|
|
GstPipeWireClock *clock = GST_PIPEWIRE_CLOCK (object);
|
2016-09-12 12:28:51 +02:00
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (clock, "finalize");
|
|
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
G_OBJECT_CLASS (gst_pipewire_clock_parent_class)->finalize (object);
|
2016-09-12 12:28:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2017-05-23 19:15:33 +02:00
|
|
|
gst_pipewire_clock_class_init (GstPipeWireClockClass * klass)
|
2016-09-12 12:28:51 +02:00
|
|
|
{
|
|
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
|
|
|
|
|
GstClockClass *gstclock_class = GST_CLOCK_CLASS (klass);
|
|
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
gobject_class->finalize = gst_pipewire_clock_finalize;
|
2016-09-12 12:28:51 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
gstclock_class->get_internal_time = gst_pipewire_clock_get_internal_time;
|
2016-09-12 12:28:51 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
GST_DEBUG_CATEGORY_INIT (gst_pipewire_clock_debug_category, "pipewireclock", 0,
|
|
|
|
|
"debug category for pipewireclock object");
|
2016-09-12 12:28:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2017-05-23 19:15:33 +02:00
|
|
|
gst_pipewire_clock_init (GstPipeWireClock * clock)
|
2016-09-12 12:28:51 +02:00
|
|
|
{
|
|
|
|
|
GST_OBJECT_FLAG_SET (clock, GST_CLOCK_FLAG_CAN_SET_MASTER);
|
|
|
|
|
}
|
2019-03-11 15:09:28 +01:00
|
|
|
|
|
|
|
|
void
|
|
|
|
|
gst_pipewire_clock_reset (GstPipeWireClock * clock, GstClockTime time)
|
|
|
|
|
{
|
|
|
|
|
GstClockTimeDiff time_offset;
|
|
|
|
|
|
|
|
|
|
if (clock->last_time >= time)
|
|
|
|
|
time_offset = clock->last_time - time;
|
|
|
|
|
else
|
|
|
|
|
time_offset = -(time - clock->last_time);
|
|
|
|
|
|
|
|
|
|
clock->time_offset = time_offset;
|
|
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (clock,
|
|
|
|
|
"reset clock to %" GST_TIME_FORMAT ", last %" GST_TIME_FORMAT
|
|
|
|
|
", offset %" GST_STIME_FORMAT, GST_TIME_ARGS (time),
|
|
|
|
|
GST_TIME_ARGS (clock->last_time), GST_STIME_ARGS (time_offset));
|
|
|
|
|
}
|