/** * gst_v4l2_buffer_pool_destroy: * @pool: the pool * * Free all resources in the pool and the pool itself. */ void gst_v4l2_buffer_pool_destroy (GstV4l2BufferPool * pool) { gint n; GST_V4L2_BUFFER_POOL_LOCK (pool); pool->running = FALSE; GST_V4L2_BUFFER_POOL_UNLOCK (pool); GST_DEBUG_OBJECT (pool->v4l2elem, "destroy pool"); /* after this point, no more buffers will be queued or dequeued; no buffer * from pool->buffers that is NULL will be set to a buffer, and no buffer that * is not NULL will be pushed out. */ /* miniobjects have no dispose, so they can't break ref-cycles, as buffers ref * the pool, we need to unref the buffer to properly finalize te pool */ for (n = 0; n < pool->buffer_count; n++) { GstBuffer *buf; GST_V4L2_BUFFER_POOL_LOCK (pool); buf = GST_BUFFER (pool->buffers[n]); GST_V4L2_BUFFER_POOL_UNLOCK (pool); if (buf) /* we own the ref if the buffer is in pool->buffers; drop it. */ gst_buffer_unref (buf); } gst_mini_object_unref (GST_MINI_OBJECT (pool)); }
static void gst_v4l2_buffer_finalize (GstV4l2Buffer * buffer) { GstV4l2BufferPool *pool; gboolean resuscitated = FALSE; gint index; pool = buffer->pool; index = buffer->vbuffer.index; GST_LOG_OBJECT (pool->v4l2elem, "finalizing buffer %p %d", buffer, index); GST_V4L2_BUFFER_POOL_LOCK (pool); if (pool->running) { if (pool->requeuebuf) { if (!gst_v4l2_buffer_pool_qbuf (pool, buffer)) { GST_WARNING ("could not requeue buffer %p %d", buffer, index); } else { resuscitated = TRUE; } } else { resuscitated = TRUE; /* XXX double check this... I think it is ok to not synchronize this * w.r.t. destruction of the pool, since the buffer is still live and * the buffer holds a ref to the pool.. */ g_async_queue_push (pool->avail_buffers, buffer); } } else { GST_LOG_OBJECT (pool->v4l2elem, "the pool is shutting down"); } if (resuscitated) { /* FIXME: check that the caps didn't change */ GST_LOG_OBJECT (pool->v4l2elem, "reviving buffer %p, %d", buffer, index); gst_buffer_ref (GST_BUFFER (buffer)); GST_BUFFER_SIZE (buffer) = 0; GST_BUFFER_FLAG_UNSET (buffer, GST_BUFFER_FLAG_DISCONT); pool->buffers[index] = buffer; } GST_V4L2_BUFFER_POOL_UNLOCK (pool); if (!resuscitated) { GST_LOG_OBJECT (pool->v4l2elem, "buffer %p (data %p, len %u) not recovered, unmapping", buffer, GST_BUFFER_DATA (buffer), buffer->mmap_length); gst_mini_object_unref (GST_MINI_OBJECT (pool)); v4l2_munmap ((void *) GST_BUFFER_DATA (buffer), buffer->mmap_length); GST_MINI_OBJECT_CLASS (v4l2buffer_parent_class)->finalize (GST_MINI_OBJECT (buffer)); } }
/** * gst_v4l2_buffer_pool_get: * @pool: the "this" object * @blocking: should this call suspend until there is a buffer available * in the buffer pool? * * Get an available buffer in the pool */ GstV4l2Buffer * gst_v4l2_buffer_pool_get (GstV4l2BufferPool * pool, gboolean blocking) { GstV4l2Buffer *buf; if (blocking) { buf = g_async_queue_pop (pool->avail_buffers); } else { buf = g_async_queue_try_pop (pool->avail_buffers); } if (buf) { GST_V4L2_BUFFER_POOL_LOCK (pool); GST_BUFFER_SIZE (buf) = buf->vbuffer.length; GST_BUFFER_FLAG_UNSET (buf, 0xffffffff); GST_V4L2_BUFFER_POOL_UNLOCK (pool); } pool->running = TRUE; return buf; }
/** * gst_v4l2_buffer_pool_dqbuf: * @pool: the pool * * Dequeue a buffer from the driver. Some generic error handling is done in * this function, but any error handling specific to v4l2src (capture) or * v4l2sink (output) can be done outside this function by checking 'errno' * * Returns: a buffer */ GstV4l2Buffer * gst_v4l2_buffer_pool_dqbuf (GstV4l2BufferPool * pool) { GstV4l2Object *v4l2object = get_v4l2_object (pool->v4l2elem); GstV4l2Buffer *pool_buffer; struct v4l2_buffer buffer; memset (&buffer, 0x00, sizeof (buffer)); buffer.type = pool->type; buffer.memory = V4L2_MEMORY_MMAP; if (v4l2_ioctl (pool->video_fd, VIDIOC_DQBUF, &buffer) >= 0) { GST_V4L2_BUFFER_POOL_LOCK (pool); /* get our GstBuffer with that index from the pool, if the buffer was * outstanding we have a serious problem. */ pool_buffer = pool->buffers[buffer.index]; if (pool_buffer == NULL) { GST_ELEMENT_ERROR (pool->v4l2elem, RESOURCE, FAILED, (_("Failed trying to get video frames from device '%s'."), v4l2object->videodev), (_("No free buffers found in the pool at index %d."), buffer.index)); GST_V4L2_BUFFER_POOL_UNLOCK (pool); return NULL; } GST_LOG_OBJECT (pool->v4l2elem, "grabbed frame %d (ix=%d), flags %08x, pool-ct=%d, buffer=%p", buffer.sequence, buffer.index, buffer.flags, pool->num_live_buffers, pool_buffer); pool->num_live_buffers++; GST_DEBUG_OBJECT (pool->v4l2elem, "num_live_buffers++: %d", pool->num_live_buffers); /* set top/bottom field first if v4l2_buffer has the information */ if (buffer.field == V4L2_FIELD_INTERLACED_TB) GST_BUFFER_FLAG_SET (pool_buffer, GST_VIDEO_BUFFER_TFF); if (buffer.field == V4L2_FIELD_INTERLACED_BT) GST_BUFFER_FLAG_UNSET (pool_buffer, GST_VIDEO_BUFFER_TFF); /* this can change at every frame, esp. with jpeg */ GST_BUFFER_SIZE (pool_buffer) = buffer.bytesused; GST_V4L2_BUFFER_POOL_UNLOCK (pool); return pool_buffer; } GST_WARNING_OBJECT (pool->v4l2elem, "problem grabbing frame %d (ix=%d), pool-ct=%d, buf.flags=%d", buffer.sequence, buffer.index, GST_MINI_OBJECT_REFCOUNT (pool), buffer.flags); switch (errno) { case EAGAIN: GST_WARNING_OBJECT (pool->v4l2elem, "Non-blocking I/O has been selected using O_NONBLOCK and" " no buffer was in the outgoing queue. device %s", v4l2object->videodev); break; case EINVAL: GST_ELEMENT_ERROR (pool->v4l2elem, RESOURCE, FAILED, (_("Failed trying to get video frames from device '%s'."), v4l2object->videodev), (_("The buffer type is not supported, or the index is out of bounds," " or no buffers have been allocated yet, or the userptr" " or length are invalid. device %s"), v4l2object->videodev)); break; case ENOMEM: GST_ELEMENT_ERROR (pool->v4l2elem, RESOURCE, FAILED, (_("Failed trying to get video frames from device '%s'. Not enough memory."), v4l2object->videodev), (_("insufficient memory to enqueue a user pointer buffer. device %s."), v4l2object->videodev)); break; case EIO: GST_INFO_OBJECT (pool->v4l2elem, "VIDIOC_DQBUF failed due to an internal error." " Can also indicate temporary problems like signal loss." " Note the driver might dequeue an (empty) buffer despite" " returning an error, or even stop capturing." " device %s", v4l2object->videodev); /* have we de-queued a buffer ? */ if (!(buffer.flags & (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE))) { GST_DEBUG_OBJECT (pool->v4l2elem, "reenqueing buffer"); /* FIXME ... should we do something here? */ } break; case EINTR: GST_WARNING_OBJECT (pool->v4l2elem, "could not sync on a buffer on device %s", v4l2object->videodev); break; default: GST_WARNING_OBJECT (pool->v4l2elem, "Grabbing frame got interrupted on %s unexpectedly. %d: %s.", v4l2object->videodev, errno, g_strerror (errno)); break; } return NULL; }