From f69283819a83fe3c5eacc177461b29adc6314fbc Mon Sep 17 00:00:00 2001 From: Nicolas Dufresne Date: Fri, 20 Mar 2020 12:35:03 -0400 Subject: [PATCH] v4l2codecs: allocator: Add method to wait for more buffers This add function to wait for buffers to get back into the pool along with a set_flushing() method to allow unblocking this wait. --- sys/v4l2codecs/gstv4l2codecallocator.c | 39 ++++++++++++++++++++++++++ sys/v4l2codecs/gstv4l2codecallocator.h | 9 ++++++ 2 files changed, 48 insertions(+) diff --git a/sys/v4l2codecs/gstv4l2codecallocator.c b/sys/v4l2codecs/gstv4l2codecallocator.c index c87f6ae9a3..46d6fa823a 100644 --- a/sys/v4l2codecs/gstv4l2codecallocator.c +++ b/sys/v4l2codecs/gstv4l2codecallocator.c @@ -45,6 +45,9 @@ struct _GstV4l2CodecAllocator gint pool_size; gboolean detached; + GCond buffer_cond; + gboolean flushing; + GstV4l2Decoder *decoder; GstPadDirection direction; }; @@ -152,6 +155,7 @@ gst_v4l2_codec_allocator_release (GstMiniObject * mini_object) if (gst_v4l2_codec_buffer_release_mem (buf)) { GST_DEBUG_OBJECT (self, "Placing back buffer %i into pool", buf->index); g_queue_push_tail (&self->pool, buf); + g_cond_signal (&self->buffer_cond); } GST_OBJECT_UNLOCK (self); @@ -196,6 +200,7 @@ failed: static void gst_v4l2_codec_allocator_init (GstV4l2CodecAllocator * self) { + g_cond_init (&self->buffer_cond); } static void @@ -220,6 +225,8 @@ gst_v4l2_codec_allocator_finalize (GObject * object) if (!self->detached) gst_v4l2_decoder_request_buffers (self->decoder, self->direction, 0); + g_cond_clear (&self->buffer_cond); + G_OBJECT_CLASS (gst_v4l2_codec_allocator_parent_class)->finalize (object); } @@ -273,6 +280,27 @@ gst_v4l2_codec_allocator_alloc (GstV4l2CodecAllocator * self) return mem; } +gboolean +gst_v4l2_codec_allocator_create_buffer (GstV4l2CodecAllocator * self) +{ + /* TODO implement */ + return FALSE; +} + +gboolean +gst_v4l2_codec_allocator_wait_for_buffer (GstV4l2CodecAllocator * self) +{ + gboolean ret; + + GST_OBJECT_LOCK (self); + while (self->pool.length == 0 && !self->flushing) + g_cond_wait (&self->buffer_cond, GST_OBJECT_GET_LOCK (self)); + ret = !self->flushing; + GST_OBJECT_UNLOCK (self); + + return ret; +} + gboolean gst_v4l2_codec_allocator_prepare_buffer (GstV4l2CodecAllocator * self, GstBuffer * gstbuf) @@ -324,6 +352,17 @@ gst_v4l2_codec_allocator_detach (GstV4l2CodecAllocator * self) GST_OBJECT_UNLOCK (self); } +void +gst_v4l2_codec_allocator_set_flushing (GstV4l2CodecAllocator * self, + gboolean flushing) +{ + GST_OBJECT_LOCK (self); + self->flushing = flushing; + if (flushing) + g_cond_broadcast (&self->buffer_cond); + GST_OBJECT_UNLOCK (self); +} + guint32 gst_v4l2_codec_memory_get_index (GstMemory * mem) { diff --git a/sys/v4l2codecs/gstv4l2codecallocator.h b/sys/v4l2codecs/gstv4l2codecallocator.h index 24cf984f27..97fe1d2c41 100644 --- a/sys/v4l2codecs/gstv4l2codecallocator.h +++ b/sys/v4l2codecs/gstv4l2codecallocator.h @@ -38,6 +38,12 @@ GstV4l2CodecAllocator *gst_v4l2_codec_allocator_new (GstV4l2Decoder * decoder, GstMemory *gst_v4l2_codec_allocator_alloc (GstV4l2CodecAllocator * allocator); + + +gboolean gst_v4l2_codec_allocator_create_buffer (GstV4l2CodecAllocator * self); + +gboolean gst_v4l2_codec_allocator_wait_for_buffer (GstV4l2CodecAllocator * self); + gboolean gst_v4l2_codec_allocator_prepare_buffer (GstV4l2CodecAllocator * allocator, GstBuffer * buffer); @@ -45,6 +51,9 @@ guint gst_v4l2_codec_allocator_get_pool_size (GstV4l2CodecAllo void gst_v4l2_codec_allocator_detach (GstV4l2CodecAllocator * self); +void gst_v4l2_codec_allocator_set_flushing (GstV4l2CodecAllocator * self, + gboolean flushing); + guint32 gst_v4l2_codec_memory_get_index (GstMemory * mem); #endif /* _GST_V4L2_CODECS_ALLOCATOR_H_ */