6b5741f027c5fcf9c87a434fd449286f8d8f0701
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-bad / gst / codectimestamper / gstcodectimestamper.c
1 /* GStreamer
2  * Copyright (C) 2022 Seungha Yang <seungha@centricular.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 St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 /* TODO:
21  * Parse POC and correct PTS if it's is unknown
22  * Reverse playback support
23  */
24
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28
29 #include <gst/base/base.h>
30 #include "gstcodectimestamper.h"
31 #include <string.h>
32
33 GST_DEBUG_CATEGORY_STATIC (gst_codec_timestamper_debug);
34 #define GST_CAT_DEFAULT gst_codec_timestamper_debug
35
36 typedef struct
37 {
38   GstBuffer *buffer;
39   GList *events;
40
41   GstClockTime pts;
42 } GstCodecTimestamperFrame;
43
44 struct _GstCodecTimestamperPrivate
45 {
46   GRecMutex lock;
47
48   GstSegment in_segment;
49
50   GList *current_frame_events;
51   GstQueueArray *queue;
52   GArray *timestamp_queue;
53
54   gint fps_n;
55   gint fps_d;
56
57   guint max_bframes;
58   guint max_dpb_frames;
59   guint max_reorder_frames;
60   gboolean interlaced;
61   guint window_size;
62   GstClockTime last_dts;
63   GstClockTime dts_offset;
64   GstClockTime time_adjustment;
65
66   GstClockTime latency;
67 };
68
69 static void gst_codec_timestamper_class_init (GstCodecTimestamperClass * klass);
70 static void gst_codec_timestamper_init (GstCodecTimestamper * self,
71     GstCodecTimestamperClass * klass);
72 static void gst_codec_timestamper_finalize (GObject * object);
73
74 static GstFlowReturn gst_codec_timestamper_chain (GstPad * pad,
75     GstObject * parent, GstBuffer * buffer);
76 static gboolean gst_codec_timestamper_sink_event (GstPad * pad,
77     GstObject * parent, GstEvent * event);
78 static gboolean gst_codec_timestamper_src_query (GstPad * pad,
79     GstObject * parent, GstQuery * query);
80 static GstStateChangeReturn
81 gst_codec_timestamper_change_state (GstElement * element,
82     GstStateChange transition);
83 static void
84 gst_codec_timestamper_clear_frame (GstCodecTimestamperFrame * frame);
85 static void gst_codec_timestamper_reset (GstCodecTimestamper * self);
86 static void gst_codec_timestamper_drain (GstCodecTimestamper * self);
87
88 static GTypeClass *parent_class = NULL;
89 static gint private_offset = 0;
90
91 /* we can't use G_DEFINE_ABSTRACT_TYPE because we need the klass in the _init
92  * method to get to the padtemplates */
93 GType
94 gst_codec_timestamper_get_type (void)
95 {
96   static gsize type = 0;
97
98   if (g_once_init_enter (&type)) {
99     GType _type;
100     static const GTypeInfo info = {
101       sizeof (GstCodecTimestamperClass),
102       NULL,
103       NULL,
104       (GClassInitFunc) gst_codec_timestamper_class_init,
105       NULL,
106       NULL,
107       sizeof (GstCodecTimestamper),
108       0,
109       (GInstanceInitFunc) gst_codec_timestamper_init,
110     };
111
112     _type = g_type_register_static (GST_TYPE_ELEMENT,
113         "GstCodecTimestamper", &info, G_TYPE_FLAG_ABSTRACT);
114
115     private_offset = g_type_add_instance_private (_type,
116         sizeof (GstCodecTimestamperPrivate));
117
118     g_once_init_leave (&type, _type);
119   }
120   return type;
121 }
122
123 static inline GstCodecTimestamperPrivate *
124 gst_codec_timestamper_get_instance_private (GstCodecTimestamper * self)
125 {
126   return (G_STRUCT_MEMBER_P (self, private_offset));
127 }
128
129 static void
130 gst_codec_timestamper_class_init (GstCodecTimestamperClass * klass)
131 {
132   GObjectClass *object_class = G_OBJECT_CLASS (klass);
133   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
134
135   parent_class = g_type_class_peek_parent (klass);
136   if (private_offset)
137     g_type_class_adjust_private_offset (klass, &private_offset);
138
139   object_class->finalize = gst_codec_timestamper_finalize;
140
141   element_class->change_state =
142       GST_DEBUG_FUNCPTR (gst_codec_timestamper_change_state);
143
144   GST_DEBUG_CATEGORY_INIT (gst_codec_timestamper_debug, "codectimestamper", 0,
145       "codectimestamper");
146
147   /**
148    * GstCodecTimestamper:
149    *
150    * Since: 1.22
151    */
152   gst_type_mark_as_plugin_api (GST_TYPE_CODEC_TIMESTAMPER, 0);
153 }
154
155 static void
156 gst_codec_timestamper_init (GstCodecTimestamper * self,
157     GstCodecTimestamperClass * klass)
158 {
159   GstCodecTimestamperPrivate *priv;
160   GstPadTemplate *template;
161
162   self->priv = priv = gst_codec_timestamper_get_instance_private (self);
163
164   template = gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass),
165       "sink");
166   self->sinkpad = gst_pad_new_from_template (template, "sink");
167   gst_pad_set_chain_function (self->sinkpad,
168       GST_DEBUG_FUNCPTR (gst_codec_timestamper_chain));
169   gst_pad_set_event_function (self->sinkpad,
170       GST_DEBUG_FUNCPTR (gst_codec_timestamper_sink_event));
171   GST_PAD_SET_PROXY_SCHEDULING (self->sinkpad);
172   gst_element_add_pad (GST_ELEMENT (self), self->sinkpad);
173
174   template = gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass),
175       "src");
176   self->srcpad = gst_pad_new_from_template (template, "src");
177   gst_pad_set_query_function (self->srcpad,
178       GST_DEBUG_FUNCPTR (gst_codec_timestamper_src_query));
179   GST_PAD_SET_PROXY_SCHEDULING (self->srcpad);
180
181   gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
182
183   priv->queue =
184       gst_queue_array_new_for_struct (sizeof (GstCodecTimestamperFrame), 16);
185   gst_queue_array_set_clear_func (priv->queue,
186       (GDestroyNotify) gst_codec_timestamper_clear_frame);
187   priv->timestamp_queue =
188       g_array_sized_new (FALSE, FALSE, sizeof (GstClockTime), 16);
189
190   g_rec_mutex_init (&priv->lock);
191 }
192
193 static void
194 gst_codec_timestamper_finalize (GObject * object)
195 {
196   GstCodecTimestamper *self = GST_CODEC_TIMESTAMPER (object);
197   GstCodecTimestamperPrivate *priv = self->priv;
198
199   gst_queue_array_free (priv->queue);
200   g_array_unref (priv->timestamp_queue);
201   g_rec_mutex_clear (&priv->lock);
202
203   G_OBJECT_CLASS (parent_class)->finalize (object);
204 }
205
206 static gboolean
207 gst_codec_timestamper_set_caps (GstCodecTimestamper * self, GstCaps * caps)
208 {
209   GstCodecTimestamperClass *klass = GST_CODEC_TIMESTAMPER_GET_CLASS (self);
210   GstCodecTimestamperPrivate *priv = self->priv;
211   GstStructure *s = gst_caps_get_structure (caps, 0);
212
213   priv->fps_n = 0;
214   priv->fps_d = 1;
215
216   gst_structure_get_fraction (s, "framerate", &priv->fps_n, &priv->fps_d);
217
218   if (priv->fps_n <= 0 || priv->fps_d <= 0) {
219     GST_WARNING_OBJECT (self, "Unknown frame rate, assume 25/1");
220     priv->fps_n = 25;
221     priv->fps_d = 1;
222   }
223
224   if (!klass->set_caps (self, caps))
225     return FALSE;
226
227   return TRUE;
228 }
229
230 static gboolean
231 gst_codec_timestamper_push_event (GstCodecTimestamper * self, GstEvent * event)
232 {
233   GstCodecTimestamperPrivate *priv = self->priv;
234
235   if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
236     GstSegment segment;
237     guint32 seqnum;
238
239     gst_event_copy_segment (event, &segment);
240
241     if (segment.format != GST_FORMAT_TIME) {
242       GST_ELEMENT_ERROR (self, CORE, EVENT, (NULL),
243           ("Non-time format segment"));
244       gst_event_unref (event);
245       return FALSE;
246     }
247
248     if (priv->time_adjustment != GST_CLOCK_TIME_NONE) {
249       segment.start += priv->time_adjustment;
250       if (GST_CLOCK_TIME_IS_VALID (segment.position))
251         segment.position += priv->time_adjustment;
252       if (GST_CLOCK_TIME_IS_VALID (segment.stop))
253         segment.stop += priv->time_adjustment;
254     }
255
256     seqnum = gst_event_get_seqnum (event);
257
258     gst_event_unref (event);
259     event = gst_event_new_segment (&segment);
260     gst_event_set_seqnum (event, seqnum);
261   }
262
263   return gst_pad_push_event (self->srcpad, event);
264 }
265
266 static void
267 gst_codec_timestamper_flush_events (GstCodecTimestamper * self, GList ** events)
268 {
269   GList *iter;
270
271   for (iter = *events; iter; iter = g_list_next (iter)) {
272     GstEvent *ev = GST_EVENT (iter->data);
273
274     if (GST_EVENT_IS_STICKY (ev) && GST_EVENT_TYPE (ev) != GST_EVENT_EOS &&
275         GST_EVENT_TYPE (ev) != GST_EVENT_SEGMENT) {
276       gst_pad_store_sticky_event (self->srcpad, ev);
277     }
278
279     gst_event_unref (ev);
280   }
281
282   g_clear_pointer (events, g_list_free);
283 }
284
285 static void
286 gst_codec_timestamper_flush (GstCodecTimestamper * self)
287 {
288   GstCodecTimestamperPrivate *priv = self->priv;
289
290   while (gst_queue_array_get_length (priv->queue) > 0) {
291     GstCodecTimestamperFrame *frame = (GstCodecTimestamperFrame *)
292         gst_queue_array_pop_head_struct (priv->queue);
293
294     gst_codec_timestamper_flush_events (self, &frame->events);
295     gst_codec_timestamper_clear_frame (frame);
296   }
297
298   gst_codec_timestamper_flush_events (self, &priv->current_frame_events);
299
300   priv->time_adjustment = GST_CLOCK_TIME_NONE;
301   priv->last_dts = GST_CLOCK_TIME_NONE;
302   g_rec_mutex_lock (&priv->lock);
303   priv->latency = GST_CLOCK_TIME_NONE;
304   g_rec_mutex_unlock (&priv->lock);
305 }
306
307 static gboolean
308 gst_codec_timestamper_sink_event (GstPad * pad, GstObject * parent,
309     GstEvent * event)
310 {
311   GstCodecTimestamper *self = GST_CODEC_TIMESTAMPER (parent);
312   GstCodecTimestamperPrivate *priv = self->priv;
313
314   switch (GST_EVENT_TYPE (event)) {
315     case GST_EVENT_CAPS:{
316       GstCaps *caps;
317
318       gst_event_parse_caps (event, &caps);
319       gst_codec_timestamper_set_caps (self, caps);
320       break;
321     }
322     case GST_EVENT_SEGMENT:{
323       GstSegment segment;
324
325       gst_event_copy_segment (event, &segment);
326       if (segment.format != GST_FORMAT_TIME) {
327         GST_WARNING_OBJECT (self, "Not a time format segment");
328         gst_event_unref (event);
329         return FALSE;
330       }
331
332       if (segment.rate < 0) {
333         GST_WARNING_OBJECT (self, "Negative rate is not supported");
334         gst_event_unref (event);
335         return FALSE;
336       }
337
338       /* Drain on segment update */
339       if (memcmp (&self->in_segment, &segment, sizeof (GstSegment)))
340         gst_codec_timestamper_drain (self);
341
342       self->in_segment = segment;
343       break;
344     }
345     case GST_EVENT_EOS:
346       gst_codec_timestamper_drain (self);
347       if (priv->current_frame_events) {
348         GList *iter;
349
350         for (iter = priv->current_frame_events; iter; iter = g_list_next (iter))
351           gst_codec_timestamper_push_event (self, GST_EVENT (iter->data));
352
353         g_clear_pointer (&priv->current_frame_events, g_list_free);
354       }
355       break;
356     case GST_EVENT_FLUSH_STOP:
357       gst_codec_timestamper_flush (self);
358       break;
359     default:
360       break;
361   }
362
363   if (!GST_EVENT_IS_SERIALIZED (event) ||
364       GST_EVENT_TYPE (event) == GST_EVENT_EOS ||
365       GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_STOP) {
366     return gst_pad_event_default (pad, parent, event);
367   }
368
369   /* Store event to serialize queued frames */
370   priv->current_frame_events = g_list_append (priv->current_frame_events,
371       event);
372
373   return TRUE;
374 }
375
376 static void
377 gst_codec_timestamper_frame_init (GstCodecTimestamperFrame * frame)
378 {
379   memset (frame, 0, sizeof (GstCodecTimestamperFrame));
380
381   frame->pts = GST_CLOCK_TIME_NONE;
382 }
383
384 static void
385 gst_codec_timestamper_clear_frame (GstCodecTimestamperFrame * frame)
386 {
387   if (!frame)
388     return;
389
390   gst_clear_buffer (&frame->buffer);
391   if (frame->events) {
392     g_list_free_full (frame->events, (GDestroyNotify) gst_event_unref);
393     frame->events = NULL;
394   }
395 }
396
397 static GstFlowReturn
398 gst_codec_timestamper_output_frame (GstCodecTimestamper * self,
399     GstCodecTimestamperFrame * frame)
400 {
401   GstCodecTimestamperPrivate *priv = self->priv;
402   GList *iter;
403   GstFlowReturn ret;
404   GstClockTime dts = GST_CLOCK_TIME_NONE;
405
406   for (iter = frame->events; iter; iter = g_list_next (iter)) {
407     GstEvent *event = GST_EVENT (iter->data);
408
409     gst_codec_timestamper_push_event (self, event);
410   }
411
412   g_clear_pointer (&frame->events, g_list_free);
413
414   if (GST_CLOCK_TIME_IS_VALID (frame->pts)) {
415     g_assert (priv->timestamp_queue->len > 0);
416     dts = g_array_index (priv->timestamp_queue, GstClockTime, 0);
417     g_array_remove_index (priv->timestamp_queue, 0);
418
419     if (GST_CLOCK_TIME_IS_VALID (priv->dts_offset))
420       dts -= priv->dts_offset;
421   }
422
423   if (GST_CLOCK_TIME_IS_VALID (dts)) {
424     if (!GST_CLOCK_TIME_IS_VALID (priv->last_dts))
425       priv->last_dts = dts;
426
427     /* make sure DTS <= PTS */
428     if (GST_CLOCK_TIME_IS_VALID (frame->pts)) {
429       if (dts > frame->pts) {
430         if (frame->pts >= priv->last_dts)
431           dts = frame->pts;
432         else
433           dts = GST_CLOCK_TIME_NONE;
434       }
435
436       if (GST_CLOCK_TIME_IS_VALID (dts))
437         priv->last_dts = dts;
438     }
439   }
440
441   frame->buffer = gst_buffer_make_writable (frame->buffer);
442   GST_BUFFER_PTS (frame->buffer) = frame->pts;
443   GST_BUFFER_DTS (frame->buffer) = dts;
444
445   GST_TRACE_OBJECT (self, "Output %" GST_PTR_FORMAT, frame->buffer);
446
447   ret = gst_pad_push (self->srcpad, g_steal_pointer (&frame->buffer));
448
449   return ret;
450 }
451
452 static GstFlowReturn
453 gst_codec_timestamper_process_output_frame (GstCodecTimestamper * self)
454 {
455   GstCodecTimestamperPrivate *priv = self->priv;
456   guint len;
457   GstCodecTimestamperFrame *frame;
458
459   len = gst_queue_array_get_length (priv->queue);
460   if (len < priv->window_size) {
461     GST_TRACE_OBJECT (self, "Need more data, queued %d/%d", len,
462         priv->window_size);
463     return GST_FLOW_OK;
464   }
465
466   frame = (GstCodecTimestamperFrame *)
467       gst_queue_array_pop_head_struct (priv->queue);
468
469   return gst_codec_timestamper_output_frame (self, frame);
470 }
471
472 static void
473 gst_codec_timestamper_drain (GstCodecTimestamper * self)
474 {
475   GstCodecTimestamperPrivate *priv = self->priv;
476
477   while (gst_queue_array_get_length (priv->queue) > 0) {
478     GstCodecTimestamperFrame *frame = (GstCodecTimestamperFrame *)
479         gst_queue_array_pop_head_struct (priv->queue);
480     gst_codec_timestamper_output_frame (self, frame);
481   }
482
483   priv->time_adjustment = GST_CLOCK_TIME_NONE;
484   priv->last_dts = GST_CLOCK_TIME_NONE;
485 }
486
487 static gint
488 pts_compare_func (const GstClockTime * a, const GstClockTime * b)
489 {
490   return (*a) - (*b);
491 }
492
493 static GstFlowReturn
494 gst_codec_timestamper_chain (GstPad * pad, GstObject * parent,
495     GstBuffer * buffer)
496 {
497   GstCodecTimestamper *self = GST_CODEC_TIMESTAMPER (parent);
498   GstCodecTimestamperPrivate *priv = self->priv;
499   GstCodecTimestamperClass *klass = GST_CODEC_TIMESTAMPER_GET_CLASS (self);
500   GstClockTime pts, dts;
501   /* The same hack as x264 for negative DTS */
502   static const GstClockTime min_pts = GST_SECOND * 60 * 60 * 1000;
503   GstCodecTimestamperFrame frame;
504   GstFlowReturn ret;
505
506   gst_codec_timestamper_frame_init (&frame);
507
508   GST_TRACE_OBJECT (self, "Handle %" GST_PTR_FORMAT, buffer);
509
510   pts = GST_BUFFER_PTS (buffer);
511   dts = GST_BUFFER_DTS (buffer);
512
513   if (!GST_CLOCK_TIME_IS_VALID (priv->time_adjustment)) {
514     GstClockTime start_time = GST_CLOCK_TIME_NONE;
515
516     if (GST_CLOCK_TIME_IS_VALID (pts))
517       start_time = MAX (pts, self->in_segment.start);
518     else if (GST_CLOCK_TIME_IS_VALID (dts))
519       start_time = MAX (dts, self->in_segment.start);
520     else
521       start_time = priv->in_segment.start;
522
523     if (start_time < min_pts)
524       priv->time_adjustment = min_pts - start_time;
525   }
526
527   if (GST_CLOCK_TIME_IS_VALID (priv->time_adjustment)) {
528     if (GST_CLOCK_TIME_IS_VALID (pts))
529       pts += priv->time_adjustment;
530     if (GST_CLOCK_TIME_IS_VALID (dts))
531       dts += priv->time_adjustment;
532   }
533
534   ret = klass->handle_buffer (self, buffer);
535   if (ret != GST_FLOW_OK) {
536     GST_INFO_OBJECT (self, "Handle buffer returned %s",
537         gst_flow_get_name (ret));
538
539     gst_buffer_unref (buffer);
540     return ret;
541   }
542
543   frame.pts = pts;
544   frame.buffer = buffer;
545   frame.events = priv->current_frame_events;
546   priv->current_frame_events = NULL;
547
548   gst_queue_array_push_tail_struct (priv->queue, &frame);
549   if (GST_CLOCK_TIME_IS_VALID (frame.pts)) {
550     g_array_append_val (priv->timestamp_queue, frame.pts);
551     g_array_sort (priv->timestamp_queue, (GCompareFunc) pts_compare_func);
552   }
553
554   return gst_codec_timestamper_process_output_frame (self);
555 }
556
557 static gboolean
558 gst_codec_timestamper_src_query (GstPad * pad, GstObject * parent,
559     GstQuery * query)
560 {
561   GstCodecTimestamper *self = GST_CODEC_TIMESTAMPER (parent);
562   GstCodecTimestamperPrivate *priv = self->priv;
563
564   switch (GST_QUERY_TYPE (query)) {
565     case GST_QUERY_LATENCY:{
566       gboolean ret;
567
568       ret = gst_pad_peer_query (self->sinkpad, query);
569       if (ret) {
570         GstClockTime min, max;
571         gboolean live;
572
573         gst_query_parse_latency (query, &live, &min, &max);
574
575         g_rec_mutex_lock (&priv->lock);
576         if (GST_CLOCK_TIME_IS_VALID (priv->latency))
577           min += priv->latency;
578         g_rec_mutex_unlock (&priv->lock);
579
580         gst_query_set_latency (query, live, min, max);
581       }
582
583       return ret;
584     }
585     default:
586       break;
587   }
588
589   return gst_pad_query_default (pad, parent, query);
590 }
591
592 static void
593 gst_codec_timestamper_reset (GstCodecTimestamper * self)
594 {
595   GstCodecTimestamperPrivate *priv = self->priv;
596
597   gst_queue_array_clear (priv->queue);
598   g_array_set_size (priv->timestamp_queue, 0);
599   priv->fps_n = 0;
600   priv->fps_d = 1;
601   priv->dts_offset = 0;
602   priv->time_adjustment = GST_CLOCK_TIME_NONE;
603   priv->latency = GST_CLOCK_TIME_NONE;
604   priv->window_size = 0;
605   priv->last_dts = GST_CLOCK_TIME_NONE;
606
607   if (priv->current_frame_events) {
608     g_list_free_full (priv->current_frame_events,
609         (GDestroyNotify) gst_event_unref);
610     priv->current_frame_events = NULL;
611   }
612 }
613
614 static gboolean
615 gst_codec_timestamper_start (GstCodecTimestamper * self)
616 {
617   GstCodecTimestamperClass *klass = GST_CODEC_TIMESTAMPER_GET_CLASS (self);
618
619   gst_codec_timestamper_reset (self);
620
621   if (klass->start)
622     return klass->start (self);
623
624   return TRUE;
625 }
626
627 static gboolean
628 gst_codec_timestamper_stop (GstCodecTimestamper * self)
629 {
630   GstCodecTimestamperClass *klass = GST_CODEC_TIMESTAMPER_GET_CLASS (self);
631
632   gst_codec_timestamper_reset (self);
633
634   if (klass->stop)
635     return klass->stop (self);
636
637   return TRUE;
638 }
639
640 static GstStateChangeReturn
641 gst_codec_timestamper_change_state (GstElement * element,
642     GstStateChange transition)
643 {
644   GstCodecTimestamper *self = GST_CODEC_TIMESTAMPER (element);
645   GstStateChangeReturn ret;
646
647   switch (transition) {
648     case GST_STATE_CHANGE_READY_TO_PAUSED:
649       gst_codec_timestamper_start (self);
650       break;
651     default:
652       break;
653   }
654
655   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
656
657   switch (transition) {
658     case GST_STATE_CHANGE_PAUSED_TO_READY:
659       gst_codec_timestamper_stop (self);
660       break;
661     default:
662       break;
663   }
664
665   return ret;
666 }
667
668 void
669 gst_codec_timestamper_set_window_size (GstCodecTimestamper * timestamper,
670     guint window_size)
671 {
672   GstCodecTimestamperPrivate *priv = timestamper->priv;
673   gboolean updated = FALSE;
674   GstClockTime latency = 0;
675
676   g_rec_mutex_lock (&priv->lock);
677   priv->dts_offset = 0;
678   priv->window_size = 0;
679
680   if (window_size) {
681     priv->dts_offset = gst_util_uint64_scale_int (window_size * GST_SECOND,
682         priv->fps_d, priv->fps_n);
683
684     /* Add margin to be robust against PTS errors and in order for boundary
685      * frames' PTS can be referenced */
686     window_size += 2;
687     latency = gst_util_uint64_scale_int (window_size * GST_SECOND,
688         priv->fps_d, priv->fps_n);
689
690     priv->window_size = window_size;
691   }
692
693   if (priv->latency != latency) {
694     updated = TRUE;
695     priv->latency = latency;
696   }
697
698   GST_DEBUG_OBJECT (timestamper,
699       "New window size %d, latency %" GST_TIME_FORMAT ", framerate %d/%d",
700       priv->window_size, GST_TIME_ARGS (latency), priv->fps_n, priv->fps_d);
701   g_rec_mutex_unlock (&priv->lock);
702
703   if (updated) {
704     gst_codec_timestamper_drain (timestamper);
705     gst_element_post_message (GST_ELEMENT_CAST (timestamper),
706         gst_message_new_latency (GST_OBJECT_CAST (timestamper)));
707   }
708 }