Fix buffer parameters and object lifecycle for real-world pipeline
handlers and session managers.

- Never publish a fixed SPA_PARAM_BUFFERS_size of 0 (OpenBSD uvideo(4)
  VIDIOC_TRY_FMT reports sizeimage of the not yet allocated frame
  buffer, leaving streamConfig.frameSize at 0).
- Encode the plane offset within the fd as mapoffset, not
  chunk->offset: planes of several buffers may share one fd and
  consumers apply chunk->offset inside the already plane-sized
  mapping, crashing on the first buffer with a non-zero offset.
- Update chunk->size per frame from FrameMetadata bytesused (essential
  for MJPEG).
- Add a destructor with full teardown: destroying a node while it
  holds the camera acquisition (e.g. the session manager recreating a
  node after an error) leaked the acquisition forever, making every
  new node instance for the camera fail with -EBUSY.
- Free the request pool and frame buffer allocator when buffers are
  cleared in port_use_buffers, otherwise a later buffer renegotiation
  fails allocBuffers() with -EBUSY.

Index: spa/plugins/libcamera/libcamera-source.cpp
--- spa/plugins/libcamera/libcamera-source.cpp.orig
+++ spa/plugins/libcamera/libcamera-source.cpp
@@ -167,6 +167,7 @@ struct impl {
 	impl(spa_log *log, spa_loop *data_loop, spa_system *system,
 	     std::shared_ptr<CameraManager> manager, std::shared_ptr<Camera> camera,
 	     std::unique_ptr<CameraConfiguration> config);
+	~impl();
 
 	struct spa_dll dll;
 
@@ -1097,6 +1098,8 @@ void handle_completed_request(struct impl *impl, libca
 		b->h->dts_offset = 0;
 	}
 
+	const auto &fmd_planes = fmd.planes();
+
 	for (std::size_t i = 0; i < b->outbuf->n_datas; i++) {
 		auto *d = &b->outbuf->datas[i];
 
@@ -1104,6 +1107,21 @@ void handle_completed_request(struct impl *impl, libca
 
 		if (fmd.status != libcamera::FrameMetadata::Status::FrameSuccess)
 			d->chunk->flags |= SPA_CHUNK_FLAG_CORRUPTED;
+
+		/* Update the valid data size with what the camera actually
+		 * produced; essential for compressed formats like MJPEG where
+		 * frames are much smaller than the buffer. The last data
+		 * holds any remaining folded planes. Keep the allocation-time
+		 * size when the pipeline handler does not report bytesused. */
+		if (i < fmd_planes.size()) {
+			uint32_t size = fmd_planes[i].bytesused;
+
+			if (i == b->outbuf->n_datas - 1)
+				for (std::size_t k = i + 1; k < fmd_planes.size(); k++)
+					size += fmd_planes[k].bytesused;
+			if (size != 0)
+				d->chunk->size = size;
+		}
 	}
 
 	request->reuse(libcamera::Request::ReuseFlag::ReuseBuffers);
@@ -1254,19 +1272,22 @@ spa_libcamera_alloc_buffers(struct impl *impl, struct 
 			d[j].type = memtype;
 			d[j].flags = SPA_DATA_FLAG_READABLE;
 			d[j].fd = -1;
-			d[j].mapoffset = 0;
 			d[j].data = nullptr;
 			d[j].chunk->stride = port->streamConfig.stride;
 			d[j].chunk->flags = 0;
+			/* The plane offset within the fd is a mapping offset
+			 * (planes of several buffers may share one fd); it is
+			 * not a data offset within the mapped area, so it
+			 * belongs in mapoffset, not chunk->offset. */
+			d[j].mapoffset = planes[j].offset;
+			d[j].chunk->offset = 0;
 			/* Update parameters according to the plane information */
 			unsigned int numPlanes = planes.size();
 			if (buffers[i]->n_datas < numPlanes) {
 				if (j < buffers[i]->n_datas - 1) {
 					d[j].maxsize = planes[j].length;
-					d[j].chunk->offset = planes[j].offset;
 					d[j].chunk->size = planes[j].length;
 				} else {
-					d[j].chunk->offset = planes[j].offset;
 					for (uint8_t k = j; k < numPlanes; k++) {
 						d[j].maxsize += planes[k].length;
 						d[j].chunk->size += planes[k].length;
@@ -1274,7 +1295,6 @@ spa_libcamera_alloc_buffers(struct impl *impl, struct 
 				}
 			} else if (buffers[i]->n_datas == numPlanes) {
 				d[j].maxsize = planes[j].length;
-				d[j].chunk->offset = planes[j].offset;
 				d[j].chunk->size = planes[j].length;
 			} else {
 				spa_log_warn(impl->log, "buffer index: i: %d, data member "
@@ -1749,12 +1769,26 @@ next:
 		 * so that exact number of buffers are allocated
 		 */
 		uint32_t n_buffers = port->streamConfig.bufferCount;
+		uint32_t frame_size = port->streamConfig.frameSize;
 
+		/* Some pipeline handlers leave frameSize at 0 (e.g. when the
+		 * kernel does not report sizeimage on TRY_FMT, as on OpenBSD
+		 * with uvideo(4)). A fixed size of 0 can never be negotiated
+		 * against a consumer requesting size >= 1, so fall back to a
+		 * conservative estimate. */
+		if (frame_size == 0) {
+			frame_size = port->streamConfig.stride *
+				port->streamConfig.size.height;
+			if (frame_size == 0)
+				frame_size = port->streamConfig.size.width *
+					port->streamConfig.size.height * 4;
+		}
+
 		param = (struct spa_pod*)spa_pod_builder_add_object(&b,
 			SPA_TYPE_OBJECT_ParamBuffers, id,
 			SPA_PARAM_BUFFERS_buffers, SPA_POD_CHOICE_RANGE_Int(n_buffers, n_buffers, n_buffers),
 			SPA_PARAM_BUFFERS_blocks,  SPA_POD_Int(port->buffers_blocks),
-			SPA_PARAM_BUFFERS_size,    SPA_POD_Int(port->streamConfig.frameSize),
+			SPA_PARAM_BUFFERS_size,    SPA_POD_Int(frame_size),
 			SPA_PARAM_BUFFERS_stride,  SPA_POD_Int(port->streamConfig.stride));
 		break;
 	}
@@ -1947,6 +1981,9 @@ int impl_node_port_use_buffers(void *object,
 		spa_libcamera_stream_off(impl);
 		if ((res = spa_libcamera_clear_buffers(port)) < 0)
 			return res;
+		/* also drop the request pool and allocator, otherwise a
+		 * subsequent allocBuffers() fails with -EBUSY */
+		freeBuffers(impl, port);
 	}
 	if (n_buffers > 0 && !port->current_format)
 		return -EIO;
@@ -2133,6 +2170,22 @@ int impl_get_interface(struct spa_handle *handle, cons
 		return -ENOENT;
 
 	return 0;
+}
+
+impl::~impl()
+{
+	struct port *port = &out_ports[0];
+
+	/* A node can be destroyed while streaming or holding the camera
+	 * acquisition (e.g. the session manager tears the node down after
+	 * an error and creates a new one). Without a full teardown the
+	 * Camera stays acquired forever and every new node instance for
+	 * it fails with -EBUSY. */
+	spa_libcamera_stream_off(this);
+	spa_libcamera_clear_buffers(port);
+	freeBuffers(this, port);
+	port->current_format.reset();
+	spa_libcamera_close(this);
 }
 
 int impl_clear(struct spa_handle *handle)
