static GstStateChangeReturn gst_v4l2_transform_change_state (GstElement * element, GstStateChange transition) { GstV4l2Transform *self = GST_V4L2_TRANSFORM (element); GstStateChangeReturn ret; switch (transition) { case GST_STATE_CHANGE_NULL_TO_READY: if (!gst_v4l2_transform_open (self)) return GST_STATE_CHANGE_FAILURE; break; case GST_STATE_CHANGE_PAUSED_TO_READY: gst_v4l2_object_unlock (self->v4l2output); gst_v4l2_object_unlock (self->v4l2capture); break; default: break; } ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition); switch (transition) { case GST_STATE_CHANGE_READY_TO_NULL: gst_v4l2_transform_close (self); break; default: break; } return ret; }
static gboolean gst_v4l2_video_dec_flush (GstVideoDecoder * decoder) { GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder); GST_DEBUG_OBJECT (self, "Flushed"); /* Ensure the processing thread has stopped for the reverse playback * discount case */ if (g_atomic_int_get (&self->processing)) { GST_VIDEO_DECODER_STREAM_UNLOCK (decoder); gst_v4l2_object_unlock (self->v4l2output); gst_v4l2_object_unlock (self->v4l2capture); gst_pad_stop_task (decoder->srcpad); GST_VIDEO_DECODER_STREAM_LOCK (decoder); } self->output_flow = GST_FLOW_OK; gst_v4l2_object_unlock_stop (self->v4l2output); gst_v4l2_object_unlock_stop (self->v4l2capture); return TRUE; }
static gboolean gst_v4l2_transform_sink_event (GstBaseTransform * trans, GstEvent * event) { GstV4l2Transform *self = GST_V4L2_TRANSFORM (trans); gboolean ret; /* Nothing to flush in passthrough */ if (gst_base_transform_is_passthrough (trans)) return GST_BASE_TRANSFORM_CLASS (parent_class)->sink_event (trans, event); switch (GST_EVENT_TYPE (event)) { case GST_EVENT_FLUSH_START: GST_DEBUG_OBJECT (self, "flush start"); gst_v4l2_object_unlock (self->v4l2output); gst_v4l2_object_unlock (self->v4l2capture); break; default: break; } ret = GST_BASE_TRANSFORM_CLASS (parent_class)->sink_event (trans, event); switch (GST_EVENT_TYPE (event)) { case GST_EVENT_FLUSH_STOP: /* Buffer should be back now */ GST_DEBUG_OBJECT (self, "flush stop"); gst_v4l2_object_unlock_stop (self->v4l2capture); gst_v4l2_object_unlock_stop (self->v4l2output); break; default: break; } return ret; }
static gboolean gst_v4l2_video_dec_sink_event (GstVideoDecoder * decoder, GstEvent * event) { GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder); gboolean ret; switch (GST_EVENT_TYPE (event)) { case GST_EVENT_FLUSH_START: GST_DEBUG_OBJECT (self, "flush start"); gst_v4l2_object_unlock (self->v4l2output); gst_v4l2_object_unlock (self->v4l2capture); break; default: break; } ret = GST_VIDEO_DECODER_CLASS (parent_class)->sink_event (decoder, event); switch (GST_EVENT_TYPE (event)) { case GST_EVENT_FLUSH_START: /* The processing thread should stop now, wait for it */ gst_pad_stop_task (decoder->srcpad); GST_DEBUG_OBJECT (self, "flush start done"); break; default: break; } return ret; }
static gboolean gst_v4l2_video_dec_stop (GstVideoDecoder * decoder) { GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder); GST_DEBUG_OBJECT (self, "Stopping"); gst_v4l2_object_unlock (self->v4l2output); gst_v4l2_object_unlock (self->v4l2capture); /* Wait for capture thread to stop */ gst_pad_stop_task (decoder->srcpad); GST_VIDEO_DECODER_STREAM_LOCK (decoder); self->output_flow = GST_FLOW_OK; GST_VIDEO_DECODER_STREAM_UNLOCK (decoder); /* Should have been flushed already */ g_assert (g_atomic_int_get (&self->active) == FALSE); g_assert (g_atomic_int_get (&self->processing) == FALSE); gst_v4l2_object_stop (self->v4l2output); gst_v4l2_object_stop (self->v4l2capture); if (self->input_state) { gst_video_codec_state_unref (self->input_state); self->input_state = NULL; } GST_DEBUG_OBJECT (self, "Stopped"); return TRUE; }
static GstStateChangeReturn gst_v4l2_video_dec_change_state (GstElement * element, GstStateChange transition) { GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (element); GstVideoDecoder *decoder = GST_VIDEO_DECODER (element); if (transition == GST_STATE_CHANGE_PAUSED_TO_READY) { g_atomic_int_set (&self->active, FALSE); gst_v4l2_object_unlock (self->v4l2output); gst_v4l2_object_unlock (self->v4l2capture); gst_pad_stop_task (decoder->srcpad); } return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition); }
static gboolean gst_v4l2_video_dec_start (GstVideoDecoder * decoder) { GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder); GST_DEBUG_OBJECT (self, "Starting"); gst_v4l2_object_unlock (self->v4l2output); g_atomic_int_set (&self->active, TRUE); self->output_flow = GST_FLOW_OK; return TRUE; }
static GstFlowReturn gst_v4l2_video_dec_finish (GstVideoDecoder * decoder) { GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder); GstFlowReturn ret = GST_FLOW_OK; GstBuffer *buffer; if (!g_atomic_int_get (&self->processing)) goto done; GST_DEBUG_OBJECT (self, "Finishing decoding"); GST_VIDEO_DECODER_STREAM_UNLOCK (decoder); if (gst_v4l2_decoder_cmd (self->v4l2output, V4L2_DEC_CMD_STOP, 0)) { GstTask *task = decoder->srcpad->task; /* If the decoder stop command succeeded, just wait until processing is * finished */ GST_OBJECT_LOCK (task); while (GST_TASK_STATE (task) == GST_TASK_STARTED) GST_TASK_WAIT (task); GST_OBJECT_UNLOCK (task); ret = GST_FLOW_FLUSHING; } else { /* otherwise keep queuing empty buffers until the processing thread has * stopped, _pool_process() will return FLUSHING when that happened */ while (ret == GST_FLOW_OK) { buffer = gst_buffer_new (); ret = gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (self-> v4l2output->pool), &buffer); gst_buffer_unref (buffer); } } /* and ensure the processing thread has stopped in case another error * occured. */ gst_v4l2_object_unlock (self->v4l2capture); gst_pad_stop_task (decoder->srcpad); GST_VIDEO_DECODER_STREAM_LOCK (decoder); if (ret == GST_FLOW_FLUSHING) ret = self->output_flow; GST_DEBUG_OBJECT (decoder, "Done draining buffers"); done: return ret; }
static GstFlowReturn gst_v4l2_video_dec_finish (GstVideoDecoder * decoder) { GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder); GstFlowReturn ret = GST_FLOW_OK; GstBuffer *buffer; if (!g_atomic_int_get (&self->processing)) goto done; GST_DEBUG_OBJECT (self, "Finishing decoding"); /* Keep queuing empty buffers until the processing thread has stopped, * _pool_process() will return FLUSHING when that happened */ GST_VIDEO_DECODER_STREAM_UNLOCK (decoder); while (ret == GST_FLOW_OK) { buffer = gst_buffer_new (); ret = gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (self-> v4l2output->pool), &buffer); gst_buffer_unref (buffer); } /* and ensure the processing thread has stopped in case another error * occured. */ gst_v4l2_object_unlock (self->v4l2capture); gst_pad_stop_task (decoder->srcpad); GST_VIDEO_DECODER_STREAM_LOCK (decoder); if (ret == GST_FLOW_FLUSHING) ret = self->output_flow; GST_DEBUG_OBJECT (decoder, "Done draining buffers"); done: return ret; }
static gboolean gst_v4l2src_unlock (GstBaseSrc * src) { GstV4l2Src *v4l2src = GST_V4L2SRC (src); return gst_v4l2_object_unlock (v4l2src->v4l2object); }
static void gst_v4l2_video_dec_loop (GstVideoDecoder * decoder) { GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder); GstV4l2BufferPool *v4l2_pool = GST_V4L2_BUFFER_POOL (self->v4l2capture->pool); GstBufferPool *pool; GstVideoCodecFrame *frame; GstBuffer *buffer = NULL; GstFlowReturn ret; GST_LOG_OBJECT (decoder, "Allocate output buffer"); do { /* We cannot use the base class allotate helper since it taking the internal * stream lock. we know that the acquire may need to poll until more frames * comes in and holding this lock would prevent that. */ pool = gst_video_decoder_get_buffer_pool (decoder); /* Pool may be NULL if we started going to READY state */ if (pool == NULL) { ret = GST_FLOW_FLUSHING; goto beach; } ret = gst_buffer_pool_acquire_buffer (pool, &buffer, NULL); g_object_unref (pool); if (ret != GST_FLOW_OK) goto beach; GST_LOG_OBJECT (decoder, "Process output buffer"); ret = gst_v4l2_buffer_pool_process (v4l2_pool, &buffer); } while (ret == GST_V4L2_FLOW_CORRUPTED_BUFFER); if (ret != GST_FLOW_OK) goto beach; frame = gst_v4l2_video_dec_get_oldest_frame (decoder); if (frame) { frame->output_buffer = buffer; buffer = NULL; ret = gst_video_decoder_finish_frame (decoder, frame); if (ret != GST_FLOW_OK) goto beach; } else { GST_WARNING_OBJECT (decoder, "Decoder is producing too many buffers"); gst_buffer_unref (buffer); } return; beach: GST_DEBUG_OBJECT (decoder, "Leaving output thread: %s", gst_flow_get_name (ret)); gst_buffer_replace (&buffer, NULL); self->output_flow = ret; g_atomic_int_set (&self->processing, FALSE); gst_v4l2_object_unlock (self->v4l2output); gst_pad_pause_task (decoder->srcpad); }