You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by GitBox <gi...@apache.org> on 2022/12/07 19:12:58 UTC
[GitHub] [nuttx-apps] xiaoxiang781216 commented on a diff in pull request #1449: apps/system: add nxcamera app
xiaoxiang781216 commented on code in PR #1449:
URL: https://github.com/apache/nuttx-apps/pull/1449#discussion_r1042553510
##########
system/nxcamera/nxcamera.c:
##########
@@ -0,0 +1,904 @@
+/****************************************************************************
+ * apps/system/nxcamera/nxcamera.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <assert.h>
+#include <debug.h>
+#include <dirent.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <nuttx/queue.h>
+#include <nuttx/video/video.h>
+#include <nuttx/video/fb.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <sys/types.h>
+
+#include "system/nxcamera.h"
Review Comment:
```suggestion
#include <system/nxcamera.h>
```
##########
system/nxcamera/nxcamera.c:
##########
@@ -0,0 +1,904 @@
+/****************************************************************************
+ * apps/system/nxcamera/nxcamera.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <assert.h>
+#include <debug.h>
+#include <dirent.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <nuttx/queue.h>
+#include <nuttx/video/video.h>
+#include <nuttx/video/fb.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <sys/types.h>
+
+#include "system/nxcamera.h"
+#ifdef CONFIG_LIBYUV
+# include "libyuv.h"
+#endif
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define NXCAMERA_STATE_IDLE 0
+#define NXCAMERA_STATE_STREAMING 1
+#define NXCAMERA_STATE_LOOPING 2
+#define NXCAMERA_STATE_PAUSED 3
+
+#define REQBUF_COUNT 3
+
+#ifndef MIN
+# define MIN(a, b) (((a) < (b)) ? (a) : (b))
+#endif
+
+#define convert_frame ConvertToARGB
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int show_image(FAR struct nxcamera_s *pcam, FAR v4l2_buffer_t *buf)
+{
+#ifdef CONFIG_LIBYUV
+ return convert_frame(pcam->bufs[buf->index],
+ pcam->buf_sizes[buf->index],
+ pcam->display_pinfo.fbmem,
+ pcam->display_pinfo.stride,
+ 0,
+ 0,
+ pcam->fmt.fmt.pix.width,
+ pcam->fmt.fmt.pix.height,
+ pcam->fmt.fmt.pix.width,
+ pcam->fmt.fmt.pix.height,
+ 0,
+ pcam->fmt.fmt.pix.pixelformat);
+#else
+ uint32_t *pbuf = pcam->bufs[buf->index];
+ vinfo("show image from %p: %lx %lx", pbuf, pbuf[0], pbuf[1]);
+ return 0;
+#endif
+}
+
+/****************************************************************************
+ * Name: nxcamera_opendevice
+ *
+ * nxcamera_opendevice() tries to open the preferred devices as specified.
+ *
+ * Return:
+ * OK if compatible device opened (searched or preferred)
+ * -ENODEV if no compatible device opened.
+ * -ENOENT if preferred device couldn't be opened.
+ *
+ ****************************************************************************/
+
+static int nxcamera_opendevice(FAR struct nxcamera_s *pcam)
+{
+ int errcode;
+
+#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE
+ if (pcam->capturedev[0] != '\0')
+ {
+#endif
+
+ if (pcam->displaydev[0] != '\0')
+ {
+#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE
+ /* If we have a preferred device, then open it */
+
+ pcam->capture_fd = open(pcam->capturedev, O_RDWR);
+#else
+ pcam->capture_fd = open(CONFIG_VIDEO_DEV_PATH, O_RDWR);
Review Comment:
where define CONFIG_VIDEO_DEV_PATH
##########
system/nxcamera/nxcamera.c:
##########
@@ -0,0 +1,904 @@
+/****************************************************************************
+ * apps/system/nxcamera/nxcamera.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <assert.h>
+#include <debug.h>
+#include <dirent.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <nuttx/queue.h>
+#include <nuttx/video/video.h>
+#include <nuttx/video/fb.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <sys/types.h>
+
+#include "system/nxcamera.h"
+#ifdef CONFIG_LIBYUV
+# include "libyuv.h"
Review Comment:
```suggestion
# include <libyuv.h>
```
##########
system/nxcamera/nxcamera.c:
##########
@@ -0,0 +1,904 @@
+/****************************************************************************
+ * apps/system/nxcamera/nxcamera.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <assert.h>
+#include <debug.h>
+#include <dirent.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <nuttx/queue.h>
+#include <nuttx/video/video.h>
+#include <nuttx/video/fb.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <sys/types.h>
+
+#include "system/nxcamera.h"
+#ifdef CONFIG_LIBYUV
+# include "libyuv.h"
+#endif
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define NXCAMERA_STATE_IDLE 0
+#define NXCAMERA_STATE_STREAMING 1
+#define NXCAMERA_STATE_LOOPING 2
+#define NXCAMERA_STATE_PAUSED 3
+
+#define REQBUF_COUNT 3
+
+#ifndef MIN
+# define MIN(a, b) (((a) < (b)) ? (a) : (b))
+#endif
+
+#define convert_frame ConvertToARGB
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int show_image(FAR struct nxcamera_s *pcam, FAR v4l2_buffer_t *buf)
+{
+#ifdef CONFIG_LIBYUV
+ return convert_frame(pcam->bufs[buf->index],
+ pcam->buf_sizes[buf->index],
+ pcam->display_pinfo.fbmem,
+ pcam->display_pinfo.stride,
+ 0,
+ 0,
+ pcam->fmt.fmt.pix.width,
+ pcam->fmt.fmt.pix.height,
+ pcam->fmt.fmt.pix.width,
+ pcam->fmt.fmt.pix.height,
+ 0,
+ pcam->fmt.fmt.pix.pixelformat);
+#else
+ uint32_t *pbuf = pcam->bufs[buf->index];
+ vinfo("show image from %p: %lx %lx", pbuf, pbuf[0], pbuf[1]);
+ return 0;
+#endif
+}
+
+/****************************************************************************
+ * Name: nxcamera_opendevice
+ *
+ * nxcamera_opendevice() tries to open the preferred devices as specified.
+ *
+ * Return:
+ * OK if compatible device opened (searched or preferred)
+ * -ENODEV if no compatible device opened.
+ * -ENOENT if preferred device couldn't be opened.
+ *
+ ****************************************************************************/
+
+static int nxcamera_opendevice(FAR struct nxcamera_s *pcam)
+{
+ int errcode;
+
+#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE
+ if (pcam->capturedev[0] != '\0')
+ {
+#endif
+
+ if (pcam->displaydev[0] != '\0')
+ {
+#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE
+ /* If we have a preferred device, then open it */
+
+ pcam->capture_fd = open(pcam->capturedev, O_RDWR);
+#else
+ pcam->capture_fd = open(CONFIG_VIDEO_DEV_PATH, O_RDWR);
+#endif
+ if (pcam->capture_fd == -1)
+ {
+ errcode = errno;
+ DEBUGASSERT(errcode > 0);
+
+ verr("ERROR: Failed to open pcam->capturedev %d\n", -errcode);
+ return -errcode;
+ }
+
+ pcam->display_fd = open(pcam->displaydev, O_RDWR);
+ if (pcam->display_fd == -1)
+ {
+ errcode = errno;
+ DEBUGASSERT(errcode > 0);
+
+ close(pcam->capture_fd);
+ pcam->capture_fd = -1;
+ verr("ERROR: Failed to open pcam->displaydev %d\n", -errcode);
+ return -errcode;
+ }
+
+ errcode = ioctl(pcam->display_fd, FBIOGET_PLANEINFO,
+ ((uintptr_t)&pcam->display_pinfo));
+
+ if (errcode == OK)
+ {
+ pcam->display_pinfo.fbmem = mmap(NULL, pcam->display_pinfo.fblen,
+ PROT_READ | PROT_WRITE,
+ MAP_SHARED | MAP_FILE,
+ pcam->display_fd, 0);
+ }
+
+ if (errcode < 0 || pcam->display_pinfo.fbmem == MAP_FAILED)
+ {
+ errcode = errno;
+ close(pcam->capture_fd);
+ close(pcam->display_fd);
+ verr("ERROR: ioctl(FBIOGET_PLANEINFO) failed: %d\n", -errcode);
+ return -errcode;
+ }
+
+ return OK;
+ }
+ else
+ {
+ /* TODO: Add file output */
+
+ return -ENOTSUP;
+ }
+
+#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE
+ }
+#endif
+
+ return -ENODEV;
+}
+
+/****************************************************************************
+ * Name: nxcamera_thread_loopthread
Review Comment:
```suggestion
* Name: nxcamera_loopthread
```
##########
system/nxcamera/nxcamera.c:
##########
@@ -0,0 +1,904 @@
+/****************************************************************************
+ * apps/system/nxcamera/nxcamera.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <assert.h>
+#include <debug.h>
+#include <dirent.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <nuttx/queue.h>
+#include <nuttx/video/video.h>
+#include <nuttx/video/fb.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <sys/types.h>
+
+#include "system/nxcamera.h"
+#ifdef CONFIG_LIBYUV
+# include "libyuv.h"
+#endif
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define NXCAMERA_STATE_IDLE 0
+#define NXCAMERA_STATE_STREAMING 1
+#define NXCAMERA_STATE_LOOPING 2
+#define NXCAMERA_STATE_PAUSED 3
+
+#define REQBUF_COUNT 3
+
+#ifndef MIN
+# define MIN(a, b) (((a) < (b)) ? (a) : (b))
+#endif
+
+#define convert_frame ConvertToARGB
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int show_image(FAR struct nxcamera_s *pcam, FAR v4l2_buffer_t *buf)
+{
+#ifdef CONFIG_LIBYUV
+ return convert_frame(pcam->bufs[buf->index],
+ pcam->buf_sizes[buf->index],
+ pcam->display_pinfo.fbmem,
+ pcam->display_pinfo.stride,
+ 0,
+ 0,
+ pcam->fmt.fmt.pix.width,
+ pcam->fmt.fmt.pix.height,
+ pcam->fmt.fmt.pix.width,
+ pcam->fmt.fmt.pix.height,
+ 0,
+ pcam->fmt.fmt.pix.pixelformat);
+#else
+ uint32_t *pbuf = pcam->bufs[buf->index];
+ vinfo("show image from %p: %lx %lx", pbuf, pbuf[0], pbuf[1]);
+ return 0;
+#endif
+}
+
+/****************************************************************************
+ * Name: nxcamera_opendevice
+ *
+ * nxcamera_opendevice() tries to open the preferred devices as specified.
+ *
+ * Return:
+ * OK if compatible device opened (searched or preferred)
+ * -ENODEV if no compatible device opened.
+ * -ENOENT if preferred device couldn't be opened.
+ *
+ ****************************************************************************/
+
+static int nxcamera_opendevice(FAR struct nxcamera_s *pcam)
+{
+ int errcode;
+
+#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE
+ if (pcam->capturedev[0] != '\0')
+ {
+#endif
+
+ if (pcam->displaydev[0] != '\0')
+ {
+#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE
+ /* If we have a preferred device, then open it */
+
+ pcam->capture_fd = open(pcam->capturedev, O_RDWR);
+#else
+ pcam->capture_fd = open(CONFIG_VIDEO_DEV_PATH, O_RDWR);
+#endif
+ if (pcam->capture_fd == -1)
+ {
+ errcode = errno;
+ DEBUGASSERT(errcode > 0);
+
+ verr("ERROR: Failed to open pcam->capturedev %d\n", -errcode);
+ return -errcode;
+ }
+
+ pcam->display_fd = open(pcam->displaydev, O_RDWR);
+ if (pcam->display_fd == -1)
+ {
+ errcode = errno;
+ DEBUGASSERT(errcode > 0);
+
+ close(pcam->capture_fd);
+ pcam->capture_fd = -1;
+ verr("ERROR: Failed to open pcam->displaydev %d\n", -errcode);
+ return -errcode;
+ }
+
+ errcode = ioctl(pcam->display_fd, FBIOGET_PLANEINFO,
+ ((uintptr_t)&pcam->display_pinfo));
+
+ if (errcode == OK)
+ {
+ pcam->display_pinfo.fbmem = mmap(NULL, pcam->display_pinfo.fblen,
+ PROT_READ | PROT_WRITE,
+ MAP_SHARED | MAP_FILE,
+ pcam->display_fd, 0);
+ }
+
+ if (errcode < 0 || pcam->display_pinfo.fbmem == MAP_FAILED)
+ {
+ errcode = errno;
+ close(pcam->capture_fd);
+ close(pcam->display_fd);
+ verr("ERROR: ioctl(FBIOGET_PLANEINFO) failed: %d\n", -errcode);
+ return -errcode;
+ }
+
+ return OK;
+ }
+ else
+ {
+ /* TODO: Add file output */
+
+ return -ENOTSUP;
+ }
+
+#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE
+ }
+#endif
+
+ return -ENODEV;
+}
+
+/****************************************************************************
+ * Name: nxcamera_thread_loopthread
+ *
+ * This is the thread that streams the video and handles video controls.
+ *
+ ****************************************************************************/
+
+static void *nxcamera_loopthread(pthread_addr_t pvarg)
+{
+ FAR struct nxcamera_s *pcam = (FAR struct nxcamera_s *)pvarg;
+ unsigned int prio;
+ ssize_t size;
+ struct video_msg_s msg;
+ bool streaming = true;
+ int x;
+ int ret;
+ struct v4l2_buffer buf;
+ uint32_t type;
+
+ vinfo("Entry\n");
+
+ for (x = 0; x < pcam->nbuffers; x++)
+ {
+ buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ buf.memory = V4L2_MEMORY_MMAP;
+ buf.index = x;
+ ret = ioctl(pcam->capture_fd, VIDIOC_QBUF, (uintptr_t)&buf);
+ if (ret < 0)
+ {
+ verr("VIDIOC_QBUF failed: %d\n", -ret);
+ goto err_out;
+ }
+ }
+
+ /* VIDIOC_STREAMON start stream */
+
+ type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ ret = ioctl(pcam->capture_fd, VIDIOC_STREAMON, (uintptr_t)&type);
+ if (ret < 0)
+ {
+ verr("VIDIOC_STREAMON failed: %d\n", -ret);
+ goto err_out;
+ }
+ else
+ {
+ pcam->loopstate = NXCAMERA_STATE_STREAMING;
+ }
+
+ /* Loop until we specifically break. streaming == true means that we are
+ * still looping waiting for the stream to complete. All of the data
+ * may have been sent, but the stream is not complete until we get
+ * VIDEO_MSG_STOP message
+ *
+ * The normal protocol for looping errors detected by the video driver
+ * is as follows:
+ *
+ * (1) The video driver will indicated the error by returning a negated
+ * error value when the next buffer is enqueued. The upper level
+ * then knows that this buffer was not queued.
+ * (2) The video driver must return all queued buffers using the
+ * VIDEO_MSG_DEQUEUE message.
+ */
+
+ while (streaming)
+ {
+ size = mq_receive(pcam->mq, (FAR char *)&msg, sizeof(msg), &prio);
+
+ /* Validate a message was received */
+
+ if (size == sizeof(msg))
+ {
+ /* Perform operation based on message id */
+
+ vinfo("message received size %zd id%d\n", size, msg.msg_id);
+ switch (msg.msg_id)
+ {
+ /* An video buffer is being dequeued by the driver */
+
+ case VIDEO_MSG_DEQUEUE:
+ if (!streaming)
+ {
+ break;
+ }
+
+ if (ret == OK && pcam->loopstate == NXCAMERA_STATE_STREAMING)
+ {
+ if (ret == OK)
+ {
+ pcam->loopstate = NXCAMERA_STATE_LOOPING;
+ }
+ }
+
+ if (ret == OK)
+ {
+ /* Go through to VIDEO_MSG_STOP */
+
+ break;
+ }
+
+ /* Someone wants to stop the stream. */
+
+ case VIDEO_MSG_STOP:
+
+ /* Send a stop message to the device */
+
+ vinfo("Stopping looping\n");
+ ioctl(pcam->capture_fd, VIDIOC_STREAMOFF, 0);
+ streaming = false;
+ break;
+
+ /* Unknown / unsupported message ID */
+
+ default:
+ break;
+ }
+ }
+
+ ret = ioctl(pcam->capture_fd, VIDIOC_DQBUF, (uintptr_t)&buf);
+ if (ret)
+ {
+ verr("Fail DQBUF %d\n", errno);
+ goto err_out;
+ }
+
+ ret = show_image(pcam, &buf);
+ if (ret < 0)
+ {
+ verr("Fail to show image %d\n", -ret);
+ goto err_out;
+ }
+
+ ret = ioctl(pcam->capture_fd, VIDIOC_QBUF, (uintptr_t)&buf);
+ if (ret)
+ {
+ verr("Fail QBUF %d\n", errno);
+ goto err_out;
+ }
+
+ usleep(1);
+ }
+
+ /* Release our video buffers and unregister / release the device */
+
+err_out:
+ vinfo("Clean-up and exit\n");
+
+ /* Cleanup */
+
+ while (sem_wait(&pcam->sem) < 0)
+ {
+ }
+
+ close(pcam->display_fd); /* Close the display device */
+ close(pcam->capture_fd); /* Close the capture device */
+ pcam->display_fd = -1; /* Mark display device as closed */
+ pcam->capture_fd = -1; /* Mark capture device as closed */
+ mq_close(pcam->mq); /* Close the message queue */
+ mq_unlink(pcam->mqname); /* Unlink the message queue */
+ pcam->loopstate = NXCAMERA_STATE_IDLE;
+ free(pcam->bufs);
+ free(pcam->buf_sizes);
+ sem_post(&pcam->sem); /* Release the semaphore */
+
+ vinfo("Exit\n");
+
+ return NULL;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: nxcamera_setdevice
+ *
+ * nxcamera_setdevice() sets the preferred video device to use with the
+ * provided nxcamera context.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE
+int nxcamera_setdevice(FAR struct nxcamera_s *pcam,
+ FAR const char *device)
+{
+ int temp_fd;
+ struct v4l2_capability caps;
+
+ DEBUGASSERT(pcam != NULL);
+ DEBUGASSERT(device != NULL);
+
+ /* Try to open the device */
+
+ temp_fd = open(device, O_RDWR);
+ if (temp_fd == -1)
+ {
+ /* Error opening the device */
+
+ return -ENOENT;
Review Comment:
```suggestion
return -errno;
```
##########
system/nxcamera/nxcamera.c:
##########
@@ -0,0 +1,904 @@
+/****************************************************************************
+ * apps/system/nxcamera/nxcamera.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <assert.h>
+#include <debug.h>
+#include <dirent.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <nuttx/queue.h>
+#include <nuttx/video/video.h>
+#include <nuttx/video/fb.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <sys/types.h>
+
+#include "system/nxcamera.h"
+#ifdef CONFIG_LIBYUV
+# include "libyuv.h"
+#endif
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define NXCAMERA_STATE_IDLE 0
+#define NXCAMERA_STATE_STREAMING 1
+#define NXCAMERA_STATE_LOOPING 2
+#define NXCAMERA_STATE_PAUSED 3
+
+#define REQBUF_COUNT 3
+
+#ifndef MIN
+# define MIN(a, b) (((a) < (b)) ? (a) : (b))
+#endif
+
+#define convert_frame ConvertToARGB
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int show_image(FAR struct nxcamera_s *pcam, FAR v4l2_buffer_t *buf)
+{
+#ifdef CONFIG_LIBYUV
+ return convert_frame(pcam->bufs[buf->index],
+ pcam->buf_sizes[buf->index],
+ pcam->display_pinfo.fbmem,
+ pcam->display_pinfo.stride,
+ 0,
+ 0,
+ pcam->fmt.fmt.pix.width,
+ pcam->fmt.fmt.pix.height,
+ pcam->fmt.fmt.pix.width,
+ pcam->fmt.fmt.pix.height,
+ 0,
+ pcam->fmt.fmt.pix.pixelformat);
+#else
+ uint32_t *pbuf = pcam->bufs[buf->index];
+ vinfo("show image from %p: %lx %lx", pbuf, pbuf[0], pbuf[1]);
+ return 0;
+#endif
+}
+
+/****************************************************************************
+ * Name: nxcamera_opendevice
+ *
+ * nxcamera_opendevice() tries to open the preferred devices as specified.
+ *
+ * Return:
+ * OK if compatible device opened (searched or preferred)
+ * -ENODEV if no compatible device opened.
+ * -ENOENT if preferred device couldn't be opened.
+ *
+ ****************************************************************************/
+
+static int nxcamera_opendevice(FAR struct nxcamera_s *pcam)
+{
+ int errcode;
+
+#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE
+ if (pcam->capturedev[0] != '\0')
+ {
+#endif
+
+ if (pcam->displaydev[0] != '\0')
+ {
+#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE
+ /* If we have a preferred device, then open it */
+
+ pcam->capture_fd = open(pcam->capturedev, O_RDWR);
+#else
+ pcam->capture_fd = open(CONFIG_VIDEO_DEV_PATH, O_RDWR);
+#endif
+ if (pcam->capture_fd == -1)
+ {
+ errcode = errno;
+ DEBUGASSERT(errcode > 0);
+
+ verr("ERROR: Failed to open pcam->capturedev %d\n", -errcode);
+ return -errcode;
+ }
+
+ pcam->display_fd = open(pcam->displaydev, O_RDWR);
+ if (pcam->display_fd == -1)
+ {
+ errcode = errno;
+ DEBUGASSERT(errcode > 0);
+
+ close(pcam->capture_fd);
+ pcam->capture_fd = -1;
+ verr("ERROR: Failed to open pcam->displaydev %d\n", -errcode);
+ return -errcode;
+ }
+
+ errcode = ioctl(pcam->display_fd, FBIOGET_PLANEINFO,
+ ((uintptr_t)&pcam->display_pinfo));
+
+ if (errcode == OK)
+ {
+ pcam->display_pinfo.fbmem = mmap(NULL, pcam->display_pinfo.fblen,
+ PROT_READ | PROT_WRITE,
+ MAP_SHARED | MAP_FILE,
+ pcam->display_fd, 0);
+ }
+
+ if (errcode < 0 || pcam->display_pinfo.fbmem == MAP_FAILED)
+ {
+ errcode = errno;
+ close(pcam->capture_fd);
+ close(pcam->display_fd);
+ verr("ERROR: ioctl(FBIOGET_PLANEINFO) failed: %d\n", -errcode);
+ return -errcode;
+ }
+
+ return OK;
+ }
+ else
+ {
+ /* TODO: Add file output */
+
+ return -ENOTSUP;
+ }
+
+#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE
+ }
+#endif
+
+ return -ENODEV;
+}
+
+/****************************************************************************
+ * Name: nxcamera_thread_loopthread
+ *
+ * This is the thread that streams the video and handles video controls.
+ *
+ ****************************************************************************/
+
+static void *nxcamera_loopthread(pthread_addr_t pvarg)
+{
+ FAR struct nxcamera_s *pcam = (FAR struct nxcamera_s *)pvarg;
+ unsigned int prio;
+ ssize_t size;
+ struct video_msg_s msg;
+ bool streaming = true;
+ int x;
+ int ret;
+ struct v4l2_buffer buf;
+ uint32_t type;
+
+ vinfo("Entry\n");
+
+ for (x = 0; x < pcam->nbuffers; x++)
+ {
+ buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ buf.memory = V4L2_MEMORY_MMAP;
+ buf.index = x;
+ ret = ioctl(pcam->capture_fd, VIDIOC_QBUF, (uintptr_t)&buf);
+ if (ret < 0)
+ {
+ verr("VIDIOC_QBUF failed: %d\n", -ret);
+ goto err_out;
+ }
+ }
+
+ /* VIDIOC_STREAMON start stream */
+
+ type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ ret = ioctl(pcam->capture_fd, VIDIOC_STREAMON, (uintptr_t)&type);
+ if (ret < 0)
+ {
+ verr("VIDIOC_STREAMON failed: %d\n", -ret);
+ goto err_out;
+ }
+ else
+ {
+ pcam->loopstate = NXCAMERA_STATE_STREAMING;
+ }
+
+ /* Loop until we specifically break. streaming == true means that we are
+ * still looping waiting for the stream to complete. All of the data
+ * may have been sent, but the stream is not complete until we get
+ * VIDEO_MSG_STOP message
+ *
+ * The normal protocol for looping errors detected by the video driver
+ * is as follows:
+ *
+ * (1) The video driver will indicated the error by returning a negated
+ * error value when the next buffer is enqueued. The upper level
+ * then knows that this buffer was not queued.
+ * (2) The video driver must return all queued buffers using the
+ * VIDEO_MSG_DEQUEUE message.
+ */
+
+ while (streaming)
+ {
+ size = mq_receive(pcam->mq, (FAR char *)&msg, sizeof(msg), &prio);
+
+ /* Validate a message was received */
+
+ if (size == sizeof(msg))
+ {
+ /* Perform operation based on message id */
+
+ vinfo("message received size %zd id%d\n", size, msg.msg_id);
+ switch (msg.msg_id)
+ {
+ /* An video buffer is being dequeued by the driver */
+
+ case VIDEO_MSG_DEQUEUE:
+ if (!streaming)
+ {
+ break;
+ }
+
+ if (ret == OK && pcam->loopstate == NXCAMERA_STATE_STREAMING)
+ {
+ if (ret == OK)
+ {
+ pcam->loopstate = NXCAMERA_STATE_LOOPING;
+ }
+ }
+
+ if (ret == OK)
+ {
+ /* Go through to VIDEO_MSG_STOP */
+
+ break;
+ }
+
+ /* Someone wants to stop the stream. */
+
+ case VIDEO_MSG_STOP:
+
+ /* Send a stop message to the device */
+
+ vinfo("Stopping looping\n");
+ ioctl(pcam->capture_fd, VIDIOC_STREAMOFF, 0);
+ streaming = false;
+ break;
+
+ /* Unknown / unsupported message ID */
+
+ default:
+ break;
+ }
+ }
+
+ ret = ioctl(pcam->capture_fd, VIDIOC_DQBUF, (uintptr_t)&buf);
+ if (ret)
+ {
+ verr("Fail DQBUF %d\n", errno);
+ goto err_out;
+ }
+
+ ret = show_image(pcam, &buf);
+ if (ret < 0)
+ {
+ verr("Fail to show image %d\n", -ret);
+ goto err_out;
+ }
+
+ ret = ioctl(pcam->capture_fd, VIDIOC_QBUF, (uintptr_t)&buf);
+ if (ret)
+ {
+ verr("Fail QBUF %d\n", errno);
+ goto err_out;
+ }
+
+ usleep(1);
+ }
+
+ /* Release our video buffers and unregister / release the device */
+
+err_out:
+ vinfo("Clean-up and exit\n");
+
+ /* Cleanup */
+
+ while (sem_wait(&pcam->sem) < 0)
+ {
+ }
+
+ close(pcam->display_fd); /* Close the display device */
+ close(pcam->capture_fd); /* Close the capture device */
+ pcam->display_fd = -1; /* Mark display device as closed */
+ pcam->capture_fd = -1; /* Mark capture device as closed */
+ mq_close(pcam->mq); /* Close the message queue */
+ mq_unlink(pcam->mqname); /* Unlink the message queue */
+ pcam->loopstate = NXCAMERA_STATE_IDLE;
+ free(pcam->bufs);
+ free(pcam->buf_sizes);
+ sem_post(&pcam->sem); /* Release the semaphore */
+
+ vinfo("Exit\n");
+
+ return NULL;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: nxcamera_setdevice
+ *
+ * nxcamera_setdevice() sets the preferred video device to use with the
+ * provided nxcamera context.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE
+int nxcamera_setdevice(FAR struct nxcamera_s *pcam,
+ FAR const char *device)
+{
+ int temp_fd;
+ struct v4l2_capability caps;
+
+ DEBUGASSERT(pcam != NULL);
+ DEBUGASSERT(device != NULL);
+
+ /* Try to open the device */
+
+ temp_fd = open(device, O_RDWR);
+ if (temp_fd == -1)
+ {
+ /* Error opening the device */
+
+ return -ENOENT;
+ }
+
+ /* Validate it's a video device by issuing an VIDIOC_QUERYCAP ioctl */
+
+ if (ioctl(temp_fd, VIDIOC_QUERYCAP, (uintptr_t)&caps) != OK)
+ {
+ /* Not an Video device! */
+
+ close(temp_fd);
+ return -ENODEV;
+ }
+
+ /* Close the file */
+
+ close(temp_fd);
+
+ /* Save the path of the preferred device */
+
+ if (caps.capabilities & V4L2_CAP_VIDEO_CAPTURE)
+ {
+ strlcpy(pcam->capturedev, device, sizeof(pcam->capturedev));
+ }
+
+ return OK;
+}
+#endif /* CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE */
+
+/****************************************************************************
+ * Name: nxcamera_setfb
+ *
+ * nxcamera_setfb() sets the output framebuffer device to use with the
+ * provided nxcamera context.
+ *
+ ****************************************************************************/
+
+int nxcamera_setfb(FAR struct nxcamera_s *pcam, FAR const char *device)
+{
+ int temp_fd;
+ struct fb_videoinfo_s vinfo;
+
+ DEBUGASSERT(pcam != NULL);
+ DEBUGASSERT(device != NULL);
+
+ /* Try to open the device */
+
+ temp_fd = open(device, O_RDWR);
+ if (temp_fd == -1)
+ {
+ /* Error opening the device */
+
+ return -ENOENT;
Review Comment:
```suggestion
return -errno;
```
##########
system/nxcamera/Makefile:
##########
@@ -0,0 +1,37 @@
+############################################################################
+# apps/system/nxcamera/Makefile
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership. The
+# ASF licenses this file to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance with the
+# License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+#
+############################################################################
+
+include $(APPDIR)/Make.defs
+
+# NxCamera Library
+
+CSRCS = nxcamera.c
+CFLAGS += -Wno-strict-prototypes
Review Comment:
why add this
##########
system/nxcamera/nxcamera.c:
##########
@@ -0,0 +1,904 @@
+/****************************************************************************
+ * apps/system/nxcamera/nxcamera.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <assert.h>
+#include <debug.h>
+#include <dirent.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <nuttx/queue.h>
+#include <nuttx/video/video.h>
+#include <nuttx/video/fb.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <sys/types.h>
+
+#include "system/nxcamera.h"
+#ifdef CONFIG_LIBYUV
+# include "libyuv.h"
+#endif
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define NXCAMERA_STATE_IDLE 0
+#define NXCAMERA_STATE_STREAMING 1
+#define NXCAMERA_STATE_LOOPING 2
+#define NXCAMERA_STATE_PAUSED 3
+
+#define REQBUF_COUNT 3
+
+#ifndef MIN
+# define MIN(a, b) (((a) < (b)) ? (a) : (b))
+#endif
+
+#define convert_frame ConvertToARGB
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int show_image(FAR struct nxcamera_s *pcam, FAR v4l2_buffer_t *buf)
+{
+#ifdef CONFIG_LIBYUV
+ return convert_frame(pcam->bufs[buf->index],
+ pcam->buf_sizes[buf->index],
+ pcam->display_pinfo.fbmem,
+ pcam->display_pinfo.stride,
+ 0,
+ 0,
+ pcam->fmt.fmt.pix.width,
+ pcam->fmt.fmt.pix.height,
+ pcam->fmt.fmt.pix.width,
+ pcam->fmt.fmt.pix.height,
+ 0,
+ pcam->fmt.fmt.pix.pixelformat);
+#else
+ uint32_t *pbuf = pcam->bufs[buf->index];
+ vinfo("show image from %p: %lx %lx", pbuf, pbuf[0], pbuf[1]);
+ return 0;
+#endif
+}
+
+/****************************************************************************
+ * Name: nxcamera_opendevice
+ *
+ * nxcamera_opendevice() tries to open the preferred devices as specified.
+ *
+ * Return:
+ * OK if compatible device opened (searched or preferred)
+ * -ENODEV if no compatible device opened.
+ * -ENOENT if preferred device couldn't be opened.
+ *
+ ****************************************************************************/
+
+static int nxcamera_opendevice(FAR struct nxcamera_s *pcam)
+{
+ int errcode;
+
+#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE
+ if (pcam->capturedev[0] != '\0')
+ {
+#endif
+
+ if (pcam->displaydev[0] != '\0')
+ {
+#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE
+ /* If we have a preferred device, then open it */
+
+ pcam->capture_fd = open(pcam->capturedev, O_RDWR);
+#else
+ pcam->capture_fd = open(CONFIG_VIDEO_DEV_PATH, O_RDWR);
+#endif
+ if (pcam->capture_fd == -1)
+ {
+ errcode = errno;
+ DEBUGASSERT(errcode > 0);
+
+ verr("ERROR: Failed to open pcam->capturedev %d\n", -errcode);
+ return -errcode;
+ }
+
+ pcam->display_fd = open(pcam->displaydev, O_RDWR);
+ if (pcam->display_fd == -1)
+ {
+ errcode = errno;
+ DEBUGASSERT(errcode > 0);
+
+ close(pcam->capture_fd);
+ pcam->capture_fd = -1;
+ verr("ERROR: Failed to open pcam->displaydev %d\n", -errcode);
+ return -errcode;
+ }
+
+ errcode = ioctl(pcam->display_fd, FBIOGET_PLANEINFO,
+ ((uintptr_t)&pcam->display_pinfo));
+
+ if (errcode == OK)
+ {
+ pcam->display_pinfo.fbmem = mmap(NULL, pcam->display_pinfo.fblen,
+ PROT_READ | PROT_WRITE,
+ MAP_SHARED | MAP_FILE,
+ pcam->display_fd, 0);
+ }
+
+ if (errcode < 0 || pcam->display_pinfo.fbmem == MAP_FAILED)
+ {
+ errcode = errno;
+ close(pcam->capture_fd);
+ close(pcam->display_fd);
+ verr("ERROR: ioctl(FBIOGET_PLANEINFO) failed: %d\n", -errcode);
+ return -errcode;
+ }
+
+ return OK;
+ }
+ else
+ {
+ /* TODO: Add file output */
+
+ return -ENOTSUP;
+ }
+
+#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE
+ }
+#endif
+
+ return -ENODEV;
+}
+
+/****************************************************************************
+ * Name: nxcamera_thread_loopthread
+ *
+ * This is the thread that streams the video and handles video controls.
+ *
+ ****************************************************************************/
+
+static void *nxcamera_loopthread(pthread_addr_t pvarg)
+{
+ FAR struct nxcamera_s *pcam = (FAR struct nxcamera_s *)pvarg;
+ unsigned int prio;
+ ssize_t size;
+ struct video_msg_s msg;
+ bool streaming = true;
+ int x;
+ int ret;
+ struct v4l2_buffer buf;
+ uint32_t type;
+
+ vinfo("Entry\n");
+
+ for (x = 0; x < pcam->nbuffers; x++)
+ {
+ buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ buf.memory = V4L2_MEMORY_MMAP;
+ buf.index = x;
+ ret = ioctl(pcam->capture_fd, VIDIOC_QBUF, (uintptr_t)&buf);
+ if (ret < 0)
+ {
+ verr("VIDIOC_QBUF failed: %d\n", -ret);
+ goto err_out;
+ }
+ }
+
+ /* VIDIOC_STREAMON start stream */
+
+ type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ ret = ioctl(pcam->capture_fd, VIDIOC_STREAMON, (uintptr_t)&type);
+ if (ret < 0)
+ {
+ verr("VIDIOC_STREAMON failed: %d\n", -ret);
+ goto err_out;
+ }
+ else
+ {
+ pcam->loopstate = NXCAMERA_STATE_STREAMING;
+ }
+
+ /* Loop until we specifically break. streaming == true means that we are
+ * still looping waiting for the stream to complete. All of the data
+ * may have been sent, but the stream is not complete until we get
+ * VIDEO_MSG_STOP message
+ *
+ * The normal protocol for looping errors detected by the video driver
+ * is as follows:
+ *
+ * (1) The video driver will indicated the error by returning a negated
+ * error value when the next buffer is enqueued. The upper level
+ * then knows that this buffer was not queued.
+ * (2) The video driver must return all queued buffers using the
+ * VIDEO_MSG_DEQUEUE message.
+ */
+
+ while (streaming)
+ {
+ size = mq_receive(pcam->mq, (FAR char *)&msg, sizeof(msg), &prio);
+
+ /* Validate a message was received */
+
+ if (size == sizeof(msg))
+ {
+ /* Perform operation based on message id */
+
+ vinfo("message received size %zd id%d\n", size, msg.msg_id);
+ switch (msg.msg_id)
+ {
+ /* An video buffer is being dequeued by the driver */
+
+ case VIDEO_MSG_DEQUEUE:
+ if (!streaming)
+ {
+ break;
+ }
+
+ if (ret == OK && pcam->loopstate == NXCAMERA_STATE_STREAMING)
+ {
+ if (ret == OK)
+ {
+ pcam->loopstate = NXCAMERA_STATE_LOOPING;
+ }
+ }
+
+ if (ret == OK)
+ {
+ /* Go through to VIDEO_MSG_STOP */
+
+ break;
+ }
+
+ /* Someone wants to stop the stream. */
+
+ case VIDEO_MSG_STOP:
+
+ /* Send a stop message to the device */
+
+ vinfo("Stopping looping\n");
+ ioctl(pcam->capture_fd, VIDIOC_STREAMOFF, 0);
+ streaming = false;
+ break;
+
+ /* Unknown / unsupported message ID */
+
+ default:
+ break;
+ }
+ }
+
+ ret = ioctl(pcam->capture_fd, VIDIOC_DQBUF, (uintptr_t)&buf);
+ if (ret)
+ {
+ verr("Fail DQBUF %d\n", errno);
+ goto err_out;
+ }
+
+ ret = show_image(pcam, &buf);
+ if (ret < 0)
+ {
+ verr("Fail to show image %d\n", -ret);
+ goto err_out;
+ }
+
+ ret = ioctl(pcam->capture_fd, VIDIOC_QBUF, (uintptr_t)&buf);
+ if (ret)
+ {
+ verr("Fail QBUF %d\n", errno);
+ goto err_out;
+ }
+
+ usleep(1);
Review Comment:
why
##########
system/nxcamera/nxcamera.c:
##########
@@ -0,0 +1,904 @@
+/****************************************************************************
+ * apps/system/nxcamera/nxcamera.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <assert.h>
+#include <debug.h>
+#include <dirent.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <nuttx/queue.h>
+#include <nuttx/video/video.h>
+#include <nuttx/video/fb.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <sys/types.h>
+
+#include "system/nxcamera.h"
+#ifdef CONFIG_LIBYUV
+# include "libyuv.h"
+#endif
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define NXCAMERA_STATE_IDLE 0
+#define NXCAMERA_STATE_STREAMING 1
+#define NXCAMERA_STATE_LOOPING 2
+#define NXCAMERA_STATE_PAUSED 3
+
+#define REQBUF_COUNT 3
+
+#ifndef MIN
+# define MIN(a, b) (((a) < (b)) ? (a) : (b))
+#endif
+
+#define convert_frame ConvertToARGB
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int show_image(FAR struct nxcamera_s *pcam, FAR v4l2_buffer_t *buf)
+{
+#ifdef CONFIG_LIBYUV
+ return convert_frame(pcam->bufs[buf->index],
+ pcam->buf_sizes[buf->index],
+ pcam->display_pinfo.fbmem,
+ pcam->display_pinfo.stride,
+ 0,
+ 0,
+ pcam->fmt.fmt.pix.width,
+ pcam->fmt.fmt.pix.height,
+ pcam->fmt.fmt.pix.width,
+ pcam->fmt.fmt.pix.height,
+ 0,
+ pcam->fmt.fmt.pix.pixelformat);
+#else
+ uint32_t *pbuf = pcam->bufs[buf->index];
+ vinfo("show image from %p: %lx %lx", pbuf, pbuf[0], pbuf[1]);
+ return 0;
+#endif
+}
+
+/****************************************************************************
+ * Name: nxcamera_opendevice
+ *
+ * nxcamera_opendevice() tries to open the preferred devices as specified.
+ *
+ * Return:
+ * OK if compatible device opened (searched or preferred)
+ * -ENODEV if no compatible device opened.
+ * -ENOENT if preferred device couldn't be opened.
+ *
+ ****************************************************************************/
+
+static int nxcamera_opendevice(FAR struct nxcamera_s *pcam)
+{
+ int errcode;
+
+#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE
+ if (pcam->capturedev[0] != '\0')
+ {
+#endif
+
+ if (pcam->displaydev[0] != '\0')
+ {
+#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE
+ /* If we have a preferred device, then open it */
+
+ pcam->capture_fd = open(pcam->capturedev, O_RDWR);
+#else
+ pcam->capture_fd = open(CONFIG_VIDEO_DEV_PATH, O_RDWR);
+#endif
+ if (pcam->capture_fd == -1)
+ {
+ errcode = errno;
+ DEBUGASSERT(errcode > 0);
+
+ verr("ERROR: Failed to open pcam->capturedev %d\n", -errcode);
+ return -errcode;
+ }
+
+ pcam->display_fd = open(pcam->displaydev, O_RDWR);
+ if (pcam->display_fd == -1)
+ {
+ errcode = errno;
+ DEBUGASSERT(errcode > 0);
+
+ close(pcam->capture_fd);
+ pcam->capture_fd = -1;
+ verr("ERROR: Failed to open pcam->displaydev %d\n", -errcode);
+ return -errcode;
+ }
+
+ errcode = ioctl(pcam->display_fd, FBIOGET_PLANEINFO,
+ ((uintptr_t)&pcam->display_pinfo));
+
+ if (errcode == OK)
+ {
+ pcam->display_pinfo.fbmem = mmap(NULL, pcam->display_pinfo.fblen,
+ PROT_READ | PROT_WRITE,
+ MAP_SHARED | MAP_FILE,
+ pcam->display_fd, 0);
+ }
+
+ if (errcode < 0 || pcam->display_pinfo.fbmem == MAP_FAILED)
+ {
+ errcode = errno;
+ close(pcam->capture_fd);
+ close(pcam->display_fd);
+ verr("ERROR: ioctl(FBIOGET_PLANEINFO) failed: %d\n", -errcode);
+ return -errcode;
+ }
+
+ return OK;
+ }
+ else
+ {
+ /* TODO: Add file output */
+
+ return -ENOTSUP;
+ }
+
+#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE
+ }
+#endif
+
+ return -ENODEV;
+}
+
+/****************************************************************************
+ * Name: nxcamera_thread_loopthread
+ *
+ * This is the thread that streams the video and handles video controls.
+ *
+ ****************************************************************************/
+
+static void *nxcamera_loopthread(pthread_addr_t pvarg)
+{
+ FAR struct nxcamera_s *pcam = (FAR struct nxcamera_s *)pvarg;
+ unsigned int prio;
+ ssize_t size;
+ struct video_msg_s msg;
+ bool streaming = true;
+ int x;
+ int ret;
+ struct v4l2_buffer buf;
+ uint32_t type;
+
+ vinfo("Entry\n");
+
+ for (x = 0; x < pcam->nbuffers; x++)
+ {
+ buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ buf.memory = V4L2_MEMORY_MMAP;
+ buf.index = x;
+ ret = ioctl(pcam->capture_fd, VIDIOC_QBUF, (uintptr_t)&buf);
+ if (ret < 0)
+ {
+ verr("VIDIOC_QBUF failed: %d\n", -ret);
+ goto err_out;
+ }
+ }
+
+ /* VIDIOC_STREAMON start stream */
+
+ type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ ret = ioctl(pcam->capture_fd, VIDIOC_STREAMON, (uintptr_t)&type);
+ if (ret < 0)
+ {
+ verr("VIDIOC_STREAMON failed: %d\n", -ret);
+ goto err_out;
+ }
+ else
+ {
+ pcam->loopstate = NXCAMERA_STATE_STREAMING;
+ }
+
+ /* Loop until we specifically break. streaming == true means that we are
+ * still looping waiting for the stream to complete. All of the data
+ * may have been sent, but the stream is not complete until we get
+ * VIDEO_MSG_STOP message
+ *
+ * The normal protocol for looping errors detected by the video driver
+ * is as follows:
+ *
+ * (1) The video driver will indicated the error by returning a negated
+ * error value when the next buffer is enqueued. The upper level
+ * then knows that this buffer was not queued.
+ * (2) The video driver must return all queued buffers using the
+ * VIDEO_MSG_DEQUEUE message.
+ */
+
+ while (streaming)
+ {
+ size = mq_receive(pcam->mq, (FAR char *)&msg, sizeof(msg), &prio);
+
+ /* Validate a message was received */
+
+ if (size == sizeof(msg))
+ {
+ /* Perform operation based on message id */
+
+ vinfo("message received size %zd id%d\n", size, msg.msg_id);
+ switch (msg.msg_id)
+ {
+ /* An video buffer is being dequeued by the driver */
+
+ case VIDEO_MSG_DEQUEUE:
+ if (!streaming)
+ {
+ break;
+ }
+
+ if (ret == OK && pcam->loopstate == NXCAMERA_STATE_STREAMING)
+ {
+ if (ret == OK)
+ {
+ pcam->loopstate = NXCAMERA_STATE_LOOPING;
+ }
+ }
+
+ if (ret == OK)
+ {
+ /* Go through to VIDEO_MSG_STOP */
+
+ break;
+ }
+
+ /* Someone wants to stop the stream. */
+
+ case VIDEO_MSG_STOP:
+
+ /* Send a stop message to the device */
+
+ vinfo("Stopping looping\n");
+ ioctl(pcam->capture_fd, VIDIOC_STREAMOFF, 0);
+ streaming = false;
+ break;
+
+ /* Unknown / unsupported message ID */
+
+ default:
+ break;
+ }
+ }
+
+ ret = ioctl(pcam->capture_fd, VIDIOC_DQBUF, (uintptr_t)&buf);
+ if (ret)
+ {
+ verr("Fail DQBUF %d\n", errno);
+ goto err_out;
+ }
+
+ ret = show_image(pcam, &buf);
+ if (ret < 0)
+ {
+ verr("Fail to show image %d\n", -ret);
+ goto err_out;
+ }
+
+ ret = ioctl(pcam->capture_fd, VIDIOC_QBUF, (uintptr_t)&buf);
+ if (ret)
+ {
+ verr("Fail QBUF %d\n", errno);
+ goto err_out;
+ }
+
+ usleep(1);
+ }
+
+ /* Release our video buffers and unregister / release the device */
+
+err_out:
+ vinfo("Clean-up and exit\n");
+
+ /* Cleanup */
+
+ while (sem_wait(&pcam->sem) < 0)
+ {
+ }
+
+ close(pcam->display_fd); /* Close the display device */
+ close(pcam->capture_fd); /* Close the capture device */
+ pcam->display_fd = -1; /* Mark display device as closed */
+ pcam->capture_fd = -1; /* Mark capture device as closed */
+ mq_close(pcam->mq); /* Close the message queue */
+ mq_unlink(pcam->mqname); /* Unlink the message queue */
+ pcam->loopstate = NXCAMERA_STATE_IDLE;
+ free(pcam->bufs);
+ free(pcam->buf_sizes);
+ sem_post(&pcam->sem); /* Release the semaphore */
+
+ vinfo("Exit\n");
+
+ return NULL;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: nxcamera_setdevice
+ *
+ * nxcamera_setdevice() sets the preferred video device to use with the
+ * provided nxcamera context.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE
+int nxcamera_setdevice(FAR struct nxcamera_s *pcam,
+ FAR const char *device)
+{
+ int temp_fd;
+ struct v4l2_capability caps;
+
+ DEBUGASSERT(pcam != NULL);
+ DEBUGASSERT(device != NULL);
+
+ /* Try to open the device */
+
+ temp_fd = open(device, O_RDWR);
+ if (temp_fd == -1)
+ {
+ /* Error opening the device */
+
+ return -ENOENT;
+ }
+
+ /* Validate it's a video device by issuing an VIDIOC_QUERYCAP ioctl */
+
+ if (ioctl(temp_fd, VIDIOC_QUERYCAP, (uintptr_t)&caps) != OK)
+ {
+ /* Not an Video device! */
+
+ close(temp_fd);
+ return -ENODEV;
+ }
+
+ /* Close the file */
+
+ close(temp_fd);
+
+ /* Save the path of the preferred device */
+
+ if (caps.capabilities & V4L2_CAP_VIDEO_CAPTURE)
+ {
+ strlcpy(pcam->capturedev, device, sizeof(pcam->capturedev));
+ }
+
+ return OK;
+}
+#endif /* CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE */
+
+/****************************************************************************
+ * Name: nxcamera_setfb
+ *
+ * nxcamera_setfb() sets the output framebuffer device to use with the
+ * provided nxcamera context.
+ *
+ ****************************************************************************/
+
+int nxcamera_setfb(FAR struct nxcamera_s *pcam, FAR const char *device)
+{
+ int temp_fd;
+ struct fb_videoinfo_s vinfo;
+
+ DEBUGASSERT(pcam != NULL);
+ DEBUGASSERT(device != NULL);
+
+ /* Try to open the device */
+
+ temp_fd = open(device, O_RDWR);
+ if (temp_fd == -1)
+ {
+ /* Error opening the device */
+
+ return -ENOENT;
+ }
+
+ /* Validate it's a fb device by issuing an FBIOGET_VIDEOINFO ioctl */
+
+ if (ioctl(temp_fd, FBIOGET_VIDEOINFO, (uintptr_t)&vinfo) != OK)
+ {
+ /* Not an Video device! */
+
+ close(temp_fd);
+ return -ENODEV;
+ }
+
+ /* Close the file */
+
+ close(temp_fd);
+
+ /* Save the path of the framebuffer device */
+
+ if (vinfo.nplanes > 0)
+ {
+ strlcpy(pcam->displaydev, device, sizeof(pcam->displaydev));
+ }
+
+ return OK;
+}
+
+/****************************************************************************
+ * Name: nxcamera_setfile
+ *
+ * nxcamera_setfile() sets the output file path to use with the provided
+ * nxcamera context.
+ *
+ ****************************************************************************/
+
+int nxcamera_setfile(FAR struct nxcamera_s *pcam, FAR const char *pfile,
+ bool isimage)
+{
+ int temp_fd;
+
+ DEBUGASSERT(pcam != NULL);
+ DEBUGASSERT(pfile != NULL);
+
+ /* Try to open the file */
+
+ temp_fd = open(pfile, O_CREAT | O_RDWR);
+ if (temp_fd == -1)
+ {
+ /* Error opening the file */
+
+ return -EACCES;
+ }
+
+ /* Close the file */
+
+ close(temp_fd);
+
+ /* Save the path of the output file */
+
+ if (isimage)
+ {
+ strlcpy(pcam->oimagepath, pfile, sizeof(pcam->oimagepath));
+ }
+ else
+ {
+ strlcpy(pcam->ovideopath, pfile, sizeof(pcam->ovideopath));
+ }
+
+ return OK;
+}
+
+/****************************************************************************
+ * Name: nxcamera_stop
+ *
+ * nxcamera_stop() stops the current playback to loop and closes the
+ * file and the associated device.
+ *
+ * Input:
+ * pcam Pointer to the initialized looper context
+ *
+ ****************************************************************************/
+
+int nxcamera_stop(FAR struct nxcamera_s *pcam)
+{
+ struct video_msg_s term_msg;
+ FAR void *value;
+
+ DEBUGASSERT(pcam != NULL);
+
+ /* Validate we are not in IDLE state */
+
+ sem_wait(&pcam->sem); /* Get the semaphore */
+ if (pcam->loopstate == NXCAMERA_STATE_IDLE)
+ {
+ sem_post(&pcam->sem); /* Release the semaphore */
+ return OK;
+ }
+
+ sem_post(&pcam->sem);
+
+ /* Notify the stream thread that it needs to cancel the stream */
+
+ term_msg.msg_id = VIDEO_MSG_STOP;
+ term_msg.u.data = 0;
+ mq_send(pcam->mq, (FAR const char *)&term_msg, sizeof(term_msg),
+ CONFIG_NXCAMERA_MSG_PRIO);
+
+ /* Join the thread. The thread will do all the cleanup. */
+
+ pthread_join(pcam->loop_id, &value);
+ pcam->loop_id = 0;
+
+ return OK;
+}
+
+/****************************************************************************
+ * Name: nxcamera_stream
+ *
+ * nxcamera_stream() tries to capture and then display the raw data using
+ * the Video system. If a capture device is specified, it will try to use
+ * that device.
+ *
+ * Input:
+ * pcam Pointer to the initialized Looper context
+ * width Capture frame width
+ * height Capture frame height
+ * framerate Capture frame rate
+ * format Capture frame pixel format
+ *
+ * Returns:
+ * OK Video is being looped
+ * -EBUSY Capture device is busy
+ * -ENOSYS No supported video format found
+ * -ENODEV No video capture or framebuffer device suitable
+ *
+ ****************************************************************************/
+
+int nxcamera_stream(FAR struct nxcamera_s *pcam,
+ uint16_t width, uint16_t height,
+ uint32_t framerate, uint32_t format)
+{
+ struct mq_attr attr;
+ struct sched_param sparam;
+ pthread_attr_t tattr;
+ FAR void *value;
+ int ret;
+ int i;
+ struct v4l2_buffer buf;
+ struct v4l2_requestbuffers req;
+ struct v4l2_streamparm parm;
+
+ DEBUGASSERT(pcam != NULL);
+
+ if (pcam->loopstate != NXCAMERA_STATE_IDLE)
+ {
+ return -EBUSY;
+ }
+
+ vinfo("==============================\n");
+ vinfo("streaming video\n");
+ vinfo("==============================\n");
+
+ /* Try to open the device */
+
+ ret = nxcamera_opendevice(pcam);
+ if (ret < 0)
+ {
+ /* Error opening the device */
+
+ verr("ERROR: nxcamera_opendevice failed: %d\n", ret);
+ return ret;
+ }
+
+ /* VIDIOC_S_FMT set format */
+
+ pcam->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ pcam->fmt.fmt.pix.width = width;
+ pcam->fmt.fmt.pix.height = height;
+ pcam->fmt.fmt.pix.field = V4L2_FIELD_ANY;
+ pcam->fmt.fmt.pix.pixelformat = format;
+
+ ret = ioctl(pcam->capture_fd, VIDIOC_S_FMT, (uintptr_t)&pcam->fmt);
+ if (ret < 0)
+ {
+ verr("VIDIOC_S_FMT failed: %d\n", -ret);
+ return ret;
+ }
+
+ memset(&parm, 0, sizeof(parm));
+ parm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ parm.parm.capture.timeperframe.denominator = framerate;
+ parm.parm.capture.timeperframe.numerator = 1;
+ ret = ioctl(pcam->capture_fd, VIDIOC_S_PARM, (uintptr_t)&parm);
+ if (ret < 0)
+ {
+ verr("VIDIOC_S_PARM failed: %d\n", -ret);
+ return ret;
+ }
+
+ /* VIDIOC_REQBUFS initiate user pointer I/O */
+
+ req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ req.memory = V4L2_MEMORY_MMAP;
+ req.count = REQBUF_COUNT;
+
+ ret = ioctl(pcam->capture_fd, VIDIOC_REQBUFS, (uintptr_t)&req);
+ if (ret < 0)
+ {
+ verr("VIDIOC_REQBUFS failed: %d\n", -ret);
+ return ret;
+ }
+
+ if (req.count < 2)
+ {
+ verr("VIDIOC_REQBUFS failed: not enough buffers\n");
+ return -ENOMEM;
+ }
+
+ pcam->nbuffers = req.count;
+ pcam->bufs = calloc(req.count, sizeof(*pcam->bufs));
+ pcam->buf_sizes = calloc(req.count, sizeof(*pcam->buf_sizes));
+ if (!pcam->bufs)
+ {
+ verr("Cannot allocate buffer pointers\n");
+ return -ENOMEM;
+ }
+
+ /* VIDIOC_QBUF enqueue buffer */
+
+ for (i = 0; i < req.count; i++)
+ {
+ buf.type = req.type;
+ buf.memory = V4L2_MEMORY_MMAP;
+ buf.index = i;
+ ret = ioctl(pcam->capture_fd, VIDIOC_QUERYBUF, (uintptr_t)&buf);
+ if (ret < 0)
+ {
+ verr("VIDIOC_QUERYBUF failed: %d\n", -ret);
+ return ret;
+ }
+
+ pcam->bufs[i] = mmap(NULL, buf.length,
+ PROT_READ | PROT_WRITE, MAP_SHARED,
+ pcam->capture_fd, buf.m.offset);
+ if (pcam->bufs[i] == MAP_FAILED)
+ {
+ verr("MMAP failed\n");
+ return ret;
+ }
+
+ pcam->buf_sizes[i] = buf.length;
+ }
+
+ /* Create a message queue for the loopthread */
+
+ attr.mq_maxmsg = req.count + 8;
+ attr.mq_msgsize = sizeof(struct video_msg_s);
+ attr.mq_curmsgs = 0;
+ attr.mq_flags = 0;
+
+ snprintf(pcam->mqname, sizeof(pcam->mqname), "/tmp/%lx",
+ (unsigned long)((uintptr_t)pcam) & 0xffffffff);
+
+ pcam->mq = mq_open(pcam->mqname, O_RDWR | O_CREAT | O_NONBLOCK, 0644,
+ &attr);
+ if (pcam->mq == (mqd_t) -1)
Review Comment:
```suggestion
if (pcam->mq == (mqd_t)-1)
```
##########
system/nxcamera/nxcamera_main.c:
##########
@@ -0,0 +1,564 @@
+/****************************************************************************
+ * apps/system/nxcamera/nxcamera_main.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/video/video.h>
+
+#include <sys/types.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <errno.h>
+#include <assert.h>
+
+#include "system/readline.h"
+#include "system/nxcamera.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define NXCAMERA_VER "1.00"
+
+#ifdef CONFIG_NXCAMERA_INCLUDE_HELP
+# define NXCAMERA_HELP_TEXT(x) x
+#else
+# define NXCAMERA_HELP_TEXT(x)
+#endif
+
+/****************************************************************************
+ * Private Type Declarations
+ ****************************************************************************/
+
+typedef CODE int (*nxcamera_func)(FAR struct nxcamera_s *cam, FAR char *arg);
+
+struct mp_cmd_s
+{
+ FAR const char *cmd; /* The command text */
+ FAR const char *arghelp; /* Text describing the args */
+ nxcamera_func pfunc; /* Pointer to command handler */
+ FAR const char *help; /* The help text */
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int nxcamera_cmd_quit(FAR struct nxcamera_s *pcam, FAR char *parg);
+static int nxcamera_cmd_stream(FAR struct nxcamera_s *pcam, FAR char *parg);
+
+#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE
+static int nxcamera_cmd_input(FAR struct nxcamera_s *pcam, FAR char *parg);
+#endif
+
+static int nxcamera_cmd_output(FAR struct nxcamera_s *pcam, FAR char *parg);
+static int nxcamera_cmd_stop(FAR struct nxcamera_s *pcam, FAR char *parg);
+static int nxcamera_cmd_set(FAR struct nxcamera_s *pcam, FAR char *parg);
+
+#ifdef CONFIG_NXCAMERA_INCLUDE_HELP
+static int nxcamera_cmd_help(FAR struct nxcamera_s *pcam, FAR char *parg);
+#endif
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct mp_cmd_s g_nxcamera_cmds[] =
+{
+#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE
+ {
+ "input",
+ "videodev",
+ nxcamera_cmd_input,
+ NXCAMERA_HELP_TEXT("Specify a preferred capture device")
+ },
+#endif
+ {
+ "output",
+ "fbdev|filename",
+ nxcamera_cmd_output,
+ NXCAMERA_HELP_TEXT("Specify a display device or filename")
+ },
+#ifdef CONFIG_NXCAMERA_INCLUDE_HELP
+ {
+ "h",
+ "",
+ nxcamera_cmd_help,
+ NXCAMERA_HELP_TEXT("Display help for commands")
+ },
+ {
+ "help",
+ "",
+ nxcamera_cmd_help,
+ NXCAMERA_HELP_TEXT("Display help for commands")
+ },
+#endif
+ {
+ "stream",
+ "width height framerate format",
+ nxcamera_cmd_stream,
+ NXCAMERA_HELP_TEXT("Video stream test (format is fourcc)")
+ },
+ {
+ "stop",
+ "",
+ nxcamera_cmd_stop,
+ NXCAMERA_HELP_TEXT("Stop stream")
+ },
+ {
+ "q",
+ "",
+ nxcamera_cmd_quit,
+ NXCAMERA_HELP_TEXT("Exit NxCamera")
+ },
+ {
+ "quit",
+ "",
+ nxcamera_cmd_quit,
+ NXCAMERA_HELP_TEXT("Exit NxCamera")
+ },
+ {
+ "set",
+ "parameter d%",
+ nxcamera_cmd_set,
+ NXCAMERA_HELP_TEXT("Set camera control parameters")
+ }
+};
+
+static const char *controls[] =
+{
+ "brightness",
+ "contrast",
+ "saturation",
+ "hue",
+ "while_balance_automatic",
+ "exposure",
+ "gain_automatic",
+ "gain",
+ "horizontal_flip",
+ "vertical_flip",
+ "power_line_frequency",
+ "sharpness",
+ "auto_exposure"
+};
+
+static const int g_nxcamera_cmd_count = sizeof(g_nxcamera_cmds) /
+ sizeof(struct mp_cmd_s);
+
+/****************************************************************************
+ * Name: nxcamera_cmd_stream
+ *
+ * nxcamera_cmd_loop() play and record the raw data file using the nxcamera
+ * context.
+ *
+ ****************************************************************************/
+
+static int nxcamera_cmd_stream(FAR struct nxcamera_s *pcam, FAR char *parg)
+{
+ int ret;
+ uint16_t width = 0;
+ uint16_t height = 0;
+ uint32_t framerate = 0;
+ uint32_t format = 0;
+ char cc[4] =
+ {
+ 0
+ };
+
+ sscanf(parg, "%hd %hd %d %s", &width, &height, &framerate, cc);
+ format = v4l2_fourcc(cc[0], cc[1], cc[2], cc[3]);
+
+ /* Try to stream raw data with settings specified */
+
+ ret = nxcamera_stream(pcam, width, height, framerate, format);
+
+ /* nxcamera_stream returned values:
+ *
+ * OK Stream is being run
+ * -EBUSY The media device is busy
+ * -ENOSYS The video format is not unsupported
+ * -ENODEV No video device suitable to capture media
+ */
+
+ ret = -ret;
+
+ switch (ret)
+ {
+ case OK:
+ break;
+
+ case ENODEV:
+ printf("No suitable Video Device found\n");
+ break;
+
+ case EBUSY:
+ printf("Video device busy\n");
+ break;
+
+ case ENOSYS:
+ printf("Unknown video format\n");
+ break;
+
+ default:
+ printf("Error stream test: %d\n", ret);
+ break;
+ }
+
+ return -ret;
+}
+
+/****************************************************************************
+ * Name: nxcamera_cmd_stop
+ *
+ * nxcamera_cmd_stop() stops stream context.
+ *
+ ****************************************************************************/
+
+static int nxcamera_cmd_stop(FAR struct nxcamera_s *pcam, FAR char *parg)
+{
+ /* Stop the stream */
+
+ return nxcamera_stop(pcam);
+}
+
+/****************************************************************************
+ * Name: nxcamera_cmd_set
+ *
+ * nxcamera_cmd_set() sets camera controls.
+ *
+ ****************************************************************************/
+
+static int nxcamera_cmd_set(FAR struct nxcamera_s *pcam, FAR char *parg)
+{
+ /* Resume the stream */
+
+ return nxcamera_set(pcam);
+}
+
+/****************************************************************************
+ * Name: nxcamera_cmd_input
+ *
+ * nxcamera_cmd_input() sets the preferred capture device for stream.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE
+static int nxcamera_cmd_input(FAR struct nxcamera_s *pcam, FAR char *parg)
+{
+ int ret;
+ char path[PATH_MAX];
+
+ /* First try to open the file directly */
+
+ ret = nxcamera_setdevice(pcam, parg);
+ if (ret == -ENOENT)
+ {
+ /* Append the /dev path and try again */
+
+ snprintf(path, sizeof(path), "/dev/%s", parg);
+ ret = nxcamera_setdevice(pcam, path);
+ }
+
+ /* Test if the device file exists */
+
+ if (ret == -ENOENT)
+ {
+ /* Device doesn't exit. Report error */
+
+ printf("Device %s not found\n", parg);
+ return ret;
+ }
+ else if (ret == -ENODEV)
+ {
+ /* Test if is is an video device */
+
+ printf("Device %s is not an video device\n", parg);
+ return ret;
+ }
+ else if (ret < 0)
+ {
+ return ret;
+ }
+
+ /* Device set successfully */
+
+ return OK;
+}
+#endif /* CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE */
+
+/****************************************************************************
+ * Name: nxcamera_cmd_output
+ *
+ * nxcamera_cmd_device() sets the output device/file for display/save.
+ *
+ ****************************************************************************/
+
+static int nxcamera_cmd_output(FAR struct nxcamera_s *pcam, FAR char *parg)
+{
+ int ret;
+ char path[PATH_MAX];
+ FAR char *ext;
+ bool isimage;
+
+ /* First try to open the device directly */
+
+ ret = nxcamera_setfb(pcam, parg);
+ if (ret == -ENOENT)
+ {
+ /* Append the /dev path and try again */
+
+ snprintf(path, sizeof(path), "/dev/%s", parg);
+ ret = nxcamera_setfb(pcam, path);
+ }
+
+ /* Device doesn't exist or is not a video device. Treat as file */
+
+ if ((ret == -ENOENT) || (ret == -ENODEV))
Review Comment:
```suggestion
if (ret == -ENOENT || ret == -ENODEV)
```
##########
system/nxcamera/nxcamera.c:
##########
@@ -0,0 +1,904 @@
+/****************************************************************************
+ * apps/system/nxcamera/nxcamera.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <assert.h>
+#include <debug.h>
+#include <dirent.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <nuttx/queue.h>
+#include <nuttx/video/video.h>
+#include <nuttx/video/fb.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <sys/types.h>
+
+#include "system/nxcamera.h"
+#ifdef CONFIG_LIBYUV
+# include "libyuv.h"
+#endif
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define NXCAMERA_STATE_IDLE 0
+#define NXCAMERA_STATE_STREAMING 1
+#define NXCAMERA_STATE_LOOPING 2
+#define NXCAMERA_STATE_PAUSED 3
+
+#define REQBUF_COUNT 3
+
+#ifndef MIN
+# define MIN(a, b) (((a) < (b)) ? (a) : (b))
+#endif
+
+#define convert_frame ConvertToARGB
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int show_image(FAR struct nxcamera_s *pcam, FAR v4l2_buffer_t *buf)
+{
+#ifdef CONFIG_LIBYUV
+ return convert_frame(pcam->bufs[buf->index],
+ pcam->buf_sizes[buf->index],
+ pcam->display_pinfo.fbmem,
+ pcam->display_pinfo.stride,
+ 0,
+ 0,
+ pcam->fmt.fmt.pix.width,
+ pcam->fmt.fmt.pix.height,
+ pcam->fmt.fmt.pix.width,
+ pcam->fmt.fmt.pix.height,
+ 0,
+ pcam->fmt.fmt.pix.pixelformat);
+#else
+ uint32_t *pbuf = pcam->bufs[buf->index];
+ vinfo("show image from %p: %lx %lx", pbuf, pbuf[0], pbuf[1]);
+ return 0;
+#endif
+}
+
+/****************************************************************************
+ * Name: nxcamera_opendevice
+ *
+ * nxcamera_opendevice() tries to open the preferred devices as specified.
+ *
+ * Return:
+ * OK if compatible device opened (searched or preferred)
+ * -ENODEV if no compatible device opened.
+ * -ENOENT if preferred device couldn't be opened.
+ *
+ ****************************************************************************/
+
+static int nxcamera_opendevice(FAR struct nxcamera_s *pcam)
+{
+ int errcode;
+
+#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE
+ if (pcam->capturedev[0] != '\0')
+ {
+#endif
+
+ if (pcam->displaydev[0] != '\0')
+ {
+#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE
+ /* If we have a preferred device, then open it */
+
+ pcam->capture_fd = open(pcam->capturedev, O_RDWR);
+#else
+ pcam->capture_fd = open(CONFIG_VIDEO_DEV_PATH, O_RDWR);
+#endif
+ if (pcam->capture_fd == -1)
+ {
+ errcode = errno;
+ DEBUGASSERT(errcode > 0);
+
+ verr("ERROR: Failed to open pcam->capturedev %d\n", -errcode);
+ return -errcode;
+ }
+
+ pcam->display_fd = open(pcam->displaydev, O_RDWR);
+ if (pcam->display_fd == -1)
+ {
+ errcode = errno;
+ DEBUGASSERT(errcode > 0);
+
+ close(pcam->capture_fd);
+ pcam->capture_fd = -1;
+ verr("ERROR: Failed to open pcam->displaydev %d\n", -errcode);
+ return -errcode;
+ }
+
+ errcode = ioctl(pcam->display_fd, FBIOGET_PLANEINFO,
+ ((uintptr_t)&pcam->display_pinfo));
+
+ if (errcode == OK)
+ {
+ pcam->display_pinfo.fbmem = mmap(NULL, pcam->display_pinfo.fblen,
+ PROT_READ | PROT_WRITE,
+ MAP_SHARED | MAP_FILE,
+ pcam->display_fd, 0);
+ }
+
+ if (errcode < 0 || pcam->display_pinfo.fbmem == MAP_FAILED)
+ {
+ errcode = errno;
+ close(pcam->capture_fd);
+ close(pcam->display_fd);
+ verr("ERROR: ioctl(FBIOGET_PLANEINFO) failed: %d\n", -errcode);
+ return -errcode;
+ }
+
+ return OK;
+ }
+ else
+ {
+ /* TODO: Add file output */
+
+ return -ENOTSUP;
+ }
+
+#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE
+ }
+#endif
+
+ return -ENODEV;
+}
+
+/****************************************************************************
+ * Name: nxcamera_thread_loopthread
+ *
+ * This is the thread that streams the video and handles video controls.
+ *
+ ****************************************************************************/
+
+static void *nxcamera_loopthread(pthread_addr_t pvarg)
+{
+ FAR struct nxcamera_s *pcam = (FAR struct nxcamera_s *)pvarg;
+ unsigned int prio;
+ ssize_t size;
+ struct video_msg_s msg;
+ bool streaming = true;
+ int x;
+ int ret;
+ struct v4l2_buffer buf;
+ uint32_t type;
+
+ vinfo("Entry\n");
+
+ for (x = 0; x < pcam->nbuffers; x++)
+ {
+ buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ buf.memory = V4L2_MEMORY_MMAP;
+ buf.index = x;
+ ret = ioctl(pcam->capture_fd, VIDIOC_QBUF, (uintptr_t)&buf);
+ if (ret < 0)
+ {
+ verr("VIDIOC_QBUF failed: %d\n", -ret);
+ goto err_out;
+ }
+ }
+
+ /* VIDIOC_STREAMON start stream */
+
+ type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ ret = ioctl(pcam->capture_fd, VIDIOC_STREAMON, (uintptr_t)&type);
+ if (ret < 0)
+ {
+ verr("VIDIOC_STREAMON failed: %d\n", -ret);
+ goto err_out;
+ }
+ else
+ {
+ pcam->loopstate = NXCAMERA_STATE_STREAMING;
+ }
+
+ /* Loop until we specifically break. streaming == true means that we are
+ * still looping waiting for the stream to complete. All of the data
+ * may have been sent, but the stream is not complete until we get
+ * VIDEO_MSG_STOP message
+ *
+ * The normal protocol for looping errors detected by the video driver
+ * is as follows:
+ *
+ * (1) The video driver will indicated the error by returning a negated
+ * error value when the next buffer is enqueued. The upper level
+ * then knows that this buffer was not queued.
+ * (2) The video driver must return all queued buffers using the
+ * VIDEO_MSG_DEQUEUE message.
+ */
+
+ while (streaming)
+ {
+ size = mq_receive(pcam->mq, (FAR char *)&msg, sizeof(msg), &prio);
+
+ /* Validate a message was received */
+
+ if (size == sizeof(msg))
+ {
+ /* Perform operation based on message id */
+
+ vinfo("message received size %zd id%d\n", size, msg.msg_id);
+ switch (msg.msg_id)
+ {
+ /* An video buffer is being dequeued by the driver */
+
+ case VIDEO_MSG_DEQUEUE:
+ if (!streaming)
+ {
+ break;
+ }
+
+ if (ret == OK && pcam->loopstate == NXCAMERA_STATE_STREAMING)
+ {
+ if (ret == OK)
+ {
+ pcam->loopstate = NXCAMERA_STATE_LOOPING;
+ }
+ }
+
+ if (ret == OK)
+ {
+ /* Go through to VIDEO_MSG_STOP */
+
+ break;
+ }
+
+ /* Someone wants to stop the stream. */
+
+ case VIDEO_MSG_STOP:
+
+ /* Send a stop message to the device */
+
+ vinfo("Stopping looping\n");
+ ioctl(pcam->capture_fd, VIDIOC_STREAMOFF, 0);
+ streaming = false;
+ break;
+
+ /* Unknown / unsupported message ID */
+
+ default:
+ break;
+ }
+ }
+
+ ret = ioctl(pcam->capture_fd, VIDIOC_DQBUF, (uintptr_t)&buf);
+ if (ret)
+ {
+ verr("Fail DQBUF %d\n", errno);
+ goto err_out;
+ }
+
+ ret = show_image(pcam, &buf);
+ if (ret < 0)
+ {
+ verr("Fail to show image %d\n", -ret);
+ goto err_out;
+ }
+
+ ret = ioctl(pcam->capture_fd, VIDIOC_QBUF, (uintptr_t)&buf);
+ if (ret)
+ {
+ verr("Fail QBUF %d\n", errno);
+ goto err_out;
+ }
+
+ usleep(1);
+ }
+
+ /* Release our video buffers and unregister / release the device */
+
+err_out:
+ vinfo("Clean-up and exit\n");
+
+ /* Cleanup */
+
+ while (sem_wait(&pcam->sem) < 0)
+ {
+ }
+
+ close(pcam->display_fd); /* Close the display device */
+ close(pcam->capture_fd); /* Close the capture device */
+ pcam->display_fd = -1; /* Mark display device as closed */
+ pcam->capture_fd = -1; /* Mark capture device as closed */
+ mq_close(pcam->mq); /* Close the message queue */
+ mq_unlink(pcam->mqname); /* Unlink the message queue */
+ pcam->loopstate = NXCAMERA_STATE_IDLE;
+ free(pcam->bufs);
+ free(pcam->buf_sizes);
+ sem_post(&pcam->sem); /* Release the semaphore */
+
+ vinfo("Exit\n");
+
+ return NULL;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: nxcamera_setdevice
+ *
+ * nxcamera_setdevice() sets the preferred video device to use with the
+ * provided nxcamera context.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE
+int nxcamera_setdevice(FAR struct nxcamera_s *pcam,
+ FAR const char *device)
+{
+ int temp_fd;
+ struct v4l2_capability caps;
+
+ DEBUGASSERT(pcam != NULL);
+ DEBUGASSERT(device != NULL);
+
+ /* Try to open the device */
+
+ temp_fd = open(device, O_RDWR);
+ if (temp_fd == -1)
+ {
+ /* Error opening the device */
+
+ return -ENOENT;
+ }
+
+ /* Validate it's a video device by issuing an VIDIOC_QUERYCAP ioctl */
+
+ if (ioctl(temp_fd, VIDIOC_QUERYCAP, (uintptr_t)&caps) != OK)
+ {
+ /* Not an Video device! */
+
+ close(temp_fd);
+ return -ENODEV;
+ }
+
+ /* Close the file */
+
+ close(temp_fd);
+
+ /* Save the path of the preferred device */
+
+ if (caps.capabilities & V4L2_CAP_VIDEO_CAPTURE)
+ {
+ strlcpy(pcam->capturedev, device, sizeof(pcam->capturedev));
+ }
+
+ return OK;
+}
+#endif /* CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE */
+
+/****************************************************************************
+ * Name: nxcamera_setfb
+ *
+ * nxcamera_setfb() sets the output framebuffer device to use with the
+ * provided nxcamera context.
+ *
+ ****************************************************************************/
+
+int nxcamera_setfb(FAR struct nxcamera_s *pcam, FAR const char *device)
+{
+ int temp_fd;
+ struct fb_videoinfo_s vinfo;
+
+ DEBUGASSERT(pcam != NULL);
+ DEBUGASSERT(device != NULL);
+
+ /* Try to open the device */
+
+ temp_fd = open(device, O_RDWR);
+ if (temp_fd == -1)
+ {
+ /* Error opening the device */
+
+ return -ENOENT;
+ }
+
+ /* Validate it's a fb device by issuing an FBIOGET_VIDEOINFO ioctl */
+
+ if (ioctl(temp_fd, FBIOGET_VIDEOINFO, (uintptr_t)&vinfo) != OK)
+ {
+ /* Not an Video device! */
+
+ close(temp_fd);
+ return -ENODEV;
+ }
+
+ /* Close the file */
+
+ close(temp_fd);
+
+ /* Save the path of the framebuffer device */
+
+ if (vinfo.nplanes > 0)
+ {
+ strlcpy(pcam->displaydev, device, sizeof(pcam->displaydev));
+ }
+
+ return OK;
+}
+
+/****************************************************************************
+ * Name: nxcamera_setfile
+ *
+ * nxcamera_setfile() sets the output file path to use with the provided
+ * nxcamera context.
+ *
+ ****************************************************************************/
+
+int nxcamera_setfile(FAR struct nxcamera_s *pcam, FAR const char *pfile,
+ bool isimage)
+{
+ int temp_fd;
+
+ DEBUGASSERT(pcam != NULL);
+ DEBUGASSERT(pfile != NULL);
+
+ /* Try to open the file */
+
+ temp_fd = open(pfile, O_CREAT | O_RDWR);
+ if (temp_fd == -1)
+ {
+ /* Error opening the file */
+
+ return -EACCES;
+ }
+
+ /* Close the file */
+
+ close(temp_fd);
+
+ /* Save the path of the output file */
+
+ if (isimage)
+ {
+ strlcpy(pcam->oimagepath, pfile, sizeof(pcam->oimagepath));
+ }
+ else
+ {
+ strlcpy(pcam->ovideopath, pfile, sizeof(pcam->ovideopath));
+ }
+
+ return OK;
+}
+
+/****************************************************************************
+ * Name: nxcamera_stop
+ *
+ * nxcamera_stop() stops the current playback to loop and closes the
+ * file and the associated device.
+ *
+ * Input:
+ * pcam Pointer to the initialized looper context
+ *
+ ****************************************************************************/
+
+int nxcamera_stop(FAR struct nxcamera_s *pcam)
+{
+ struct video_msg_s term_msg;
+ FAR void *value;
+
+ DEBUGASSERT(pcam != NULL);
+
+ /* Validate we are not in IDLE state */
+
+ sem_wait(&pcam->sem); /* Get the semaphore */
+ if (pcam->loopstate == NXCAMERA_STATE_IDLE)
+ {
+ sem_post(&pcam->sem); /* Release the semaphore */
+ return OK;
+ }
+
+ sem_post(&pcam->sem);
+
+ /* Notify the stream thread that it needs to cancel the stream */
+
+ term_msg.msg_id = VIDEO_MSG_STOP;
+ term_msg.u.data = 0;
+ mq_send(pcam->mq, (FAR const char *)&term_msg, sizeof(term_msg),
+ CONFIG_NXCAMERA_MSG_PRIO);
+
+ /* Join the thread. The thread will do all the cleanup. */
+
+ pthread_join(pcam->loop_id, &value);
+ pcam->loop_id = 0;
+
+ return OK;
+}
+
+/****************************************************************************
+ * Name: nxcamera_stream
+ *
+ * nxcamera_stream() tries to capture and then display the raw data using
+ * the Video system. If a capture device is specified, it will try to use
+ * that device.
+ *
+ * Input:
+ * pcam Pointer to the initialized Looper context
+ * width Capture frame width
+ * height Capture frame height
+ * framerate Capture frame rate
+ * format Capture frame pixel format
+ *
+ * Returns:
+ * OK Video is being looped
+ * -EBUSY Capture device is busy
+ * -ENOSYS No supported video format found
+ * -ENODEV No video capture or framebuffer device suitable
+ *
+ ****************************************************************************/
+
+int nxcamera_stream(FAR struct nxcamera_s *pcam,
+ uint16_t width, uint16_t height,
+ uint32_t framerate, uint32_t format)
+{
+ struct mq_attr attr;
+ struct sched_param sparam;
+ pthread_attr_t tattr;
+ FAR void *value;
+ int ret;
+ int i;
+ struct v4l2_buffer buf;
+ struct v4l2_requestbuffers req;
+ struct v4l2_streamparm parm;
+
+ DEBUGASSERT(pcam != NULL);
+
+ if (pcam->loopstate != NXCAMERA_STATE_IDLE)
+ {
+ return -EBUSY;
+ }
+
+ vinfo("==============================\n");
+ vinfo("streaming video\n");
+ vinfo("==============================\n");
+
+ /* Try to open the device */
+
+ ret = nxcamera_opendevice(pcam);
+ if (ret < 0)
+ {
+ /* Error opening the device */
+
+ verr("ERROR: nxcamera_opendevice failed: %d\n", ret);
+ return ret;
+ }
+
+ /* VIDIOC_S_FMT set format */
+
+ pcam->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ pcam->fmt.fmt.pix.width = width;
+ pcam->fmt.fmt.pix.height = height;
+ pcam->fmt.fmt.pix.field = V4L2_FIELD_ANY;
+ pcam->fmt.fmt.pix.pixelformat = format;
+
+ ret = ioctl(pcam->capture_fd, VIDIOC_S_FMT, (uintptr_t)&pcam->fmt);
+ if (ret < 0)
+ {
+ verr("VIDIOC_S_FMT failed: %d\n", -ret);
+ return ret;
+ }
+
+ memset(&parm, 0, sizeof(parm));
+ parm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ parm.parm.capture.timeperframe.denominator = framerate;
+ parm.parm.capture.timeperframe.numerator = 1;
+ ret = ioctl(pcam->capture_fd, VIDIOC_S_PARM, (uintptr_t)&parm);
+ if (ret < 0)
+ {
+ verr("VIDIOC_S_PARM failed: %d\n", -ret);
+ return ret;
+ }
+
+ /* VIDIOC_REQBUFS initiate user pointer I/O */
+
+ req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ req.memory = V4L2_MEMORY_MMAP;
+ req.count = REQBUF_COUNT;
+
+ ret = ioctl(pcam->capture_fd, VIDIOC_REQBUFS, (uintptr_t)&req);
+ if (ret < 0)
+ {
+ verr("VIDIOC_REQBUFS failed: %d\n", -ret);
+ return ret;
+ }
+
+ if (req.count < 2)
+ {
+ verr("VIDIOC_REQBUFS failed: not enough buffers\n");
+ return -ENOMEM;
+ }
+
+ pcam->nbuffers = req.count;
+ pcam->bufs = calloc(req.count, sizeof(*pcam->bufs));
+ pcam->buf_sizes = calloc(req.count, sizeof(*pcam->buf_sizes));
+ if (!pcam->bufs)
+ {
+ verr("Cannot allocate buffer pointers\n");
+ return -ENOMEM;
+ }
+
+ /* VIDIOC_QBUF enqueue buffer */
+
+ for (i = 0; i < req.count; i++)
+ {
+ buf.type = req.type;
+ buf.memory = V4L2_MEMORY_MMAP;
+ buf.index = i;
+ ret = ioctl(pcam->capture_fd, VIDIOC_QUERYBUF, (uintptr_t)&buf);
+ if (ret < 0)
+ {
+ verr("VIDIOC_QUERYBUF failed: %d\n", -ret);
+ return ret;
+ }
+
+ pcam->bufs[i] = mmap(NULL, buf.length,
+ PROT_READ | PROT_WRITE, MAP_SHARED,
+ pcam->capture_fd, buf.m.offset);
+ if (pcam->bufs[i] == MAP_FAILED)
+ {
+ verr("MMAP failed\n");
+ return ret;
Review Comment:
how ummap the previous buffer
##########
system/nxcamera/nxcamera.c:
##########
@@ -0,0 +1,904 @@
+/****************************************************************************
+ * apps/system/nxcamera/nxcamera.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <assert.h>
+#include <debug.h>
+#include <dirent.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <nuttx/queue.h>
+#include <nuttx/video/video.h>
+#include <nuttx/video/fb.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <sys/types.h>
+
+#include "system/nxcamera.h"
+#ifdef CONFIG_LIBYUV
+# include "libyuv.h"
+#endif
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define NXCAMERA_STATE_IDLE 0
+#define NXCAMERA_STATE_STREAMING 1
+#define NXCAMERA_STATE_LOOPING 2
+#define NXCAMERA_STATE_PAUSED 3
+
+#define REQBUF_COUNT 3
+
+#ifndef MIN
+# define MIN(a, b) (((a) < (b)) ? (a) : (b))
+#endif
+
+#define convert_frame ConvertToARGB
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int show_image(FAR struct nxcamera_s *pcam, FAR v4l2_buffer_t *buf)
+{
+#ifdef CONFIG_LIBYUV
+ return convert_frame(pcam->bufs[buf->index],
+ pcam->buf_sizes[buf->index],
+ pcam->display_pinfo.fbmem,
+ pcam->display_pinfo.stride,
+ 0,
+ 0,
+ pcam->fmt.fmt.pix.width,
+ pcam->fmt.fmt.pix.height,
+ pcam->fmt.fmt.pix.width,
+ pcam->fmt.fmt.pix.height,
+ 0,
+ pcam->fmt.fmt.pix.pixelformat);
+#else
+ uint32_t *pbuf = pcam->bufs[buf->index];
+ vinfo("show image from %p: %lx %lx", pbuf, pbuf[0], pbuf[1]);
+ return 0;
+#endif
+}
+
+/****************************************************************************
+ * Name: nxcamera_opendevice
+ *
+ * nxcamera_opendevice() tries to open the preferred devices as specified.
+ *
+ * Return:
+ * OK if compatible device opened (searched or preferred)
+ * -ENODEV if no compatible device opened.
+ * -ENOENT if preferred device couldn't be opened.
+ *
+ ****************************************************************************/
+
+static int nxcamera_opendevice(FAR struct nxcamera_s *pcam)
+{
+ int errcode;
+
+#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE
+ if (pcam->capturedev[0] != '\0')
+ {
+#endif
+
+ if (pcam->displaydev[0] != '\0')
Review Comment:
check displaydev after check capturedev at line 111?
should we open capture_fd before check/open display_fd
##########
system/nxcamera/nxcamera.c:
##########
@@ -0,0 +1,904 @@
+/****************************************************************************
+ * apps/system/nxcamera/nxcamera.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <assert.h>
+#include <debug.h>
+#include <dirent.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <nuttx/queue.h>
+#include <nuttx/video/video.h>
+#include <nuttx/video/fb.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <sys/types.h>
+
+#include "system/nxcamera.h"
+#ifdef CONFIG_LIBYUV
+# include "libyuv.h"
+#endif
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define NXCAMERA_STATE_IDLE 0
+#define NXCAMERA_STATE_STREAMING 1
+#define NXCAMERA_STATE_LOOPING 2
+#define NXCAMERA_STATE_PAUSED 3
+
+#define REQBUF_COUNT 3
+
+#ifndef MIN
+# define MIN(a, b) (((a) < (b)) ? (a) : (b))
+#endif
+
+#define convert_frame ConvertToARGB
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int show_image(FAR struct nxcamera_s *pcam, FAR v4l2_buffer_t *buf)
+{
+#ifdef CONFIG_LIBYUV
+ return convert_frame(pcam->bufs[buf->index],
+ pcam->buf_sizes[buf->index],
+ pcam->display_pinfo.fbmem,
+ pcam->display_pinfo.stride,
+ 0,
+ 0,
+ pcam->fmt.fmt.pix.width,
+ pcam->fmt.fmt.pix.height,
+ pcam->fmt.fmt.pix.width,
+ pcam->fmt.fmt.pix.height,
+ 0,
+ pcam->fmt.fmt.pix.pixelformat);
+#else
+ uint32_t *pbuf = pcam->bufs[buf->index];
+ vinfo("show image from %p: %lx %lx", pbuf, pbuf[0], pbuf[1]);
+ return 0;
+#endif
+}
+
+/****************************************************************************
+ * Name: nxcamera_opendevice
+ *
+ * nxcamera_opendevice() tries to open the preferred devices as specified.
+ *
+ * Return:
+ * OK if compatible device opened (searched or preferred)
+ * -ENODEV if no compatible device opened.
+ * -ENOENT if preferred device couldn't be opened.
+ *
+ ****************************************************************************/
+
+static int nxcamera_opendevice(FAR struct nxcamera_s *pcam)
+{
+ int errcode;
+
+#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE
+ if (pcam->capturedev[0] != '\0')
+ {
+#endif
+
+ if (pcam->displaydev[0] != '\0')
+ {
+#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE
+ /* If we have a preferred device, then open it */
+
+ pcam->capture_fd = open(pcam->capturedev, O_RDWR);
+#else
+ pcam->capture_fd = open(CONFIG_VIDEO_DEV_PATH, O_RDWR);
+#endif
+ if (pcam->capture_fd == -1)
+ {
+ errcode = errno;
+ DEBUGASSERT(errcode > 0);
+
+ verr("ERROR: Failed to open pcam->capturedev %d\n", -errcode);
+ return -errcode;
+ }
+
+ pcam->display_fd = open(pcam->displaydev, O_RDWR);
+ if (pcam->display_fd == -1)
+ {
+ errcode = errno;
+ DEBUGASSERT(errcode > 0);
+
+ close(pcam->capture_fd);
+ pcam->capture_fd = -1;
+ verr("ERROR: Failed to open pcam->displaydev %d\n", -errcode);
+ return -errcode;
+ }
+
+ errcode = ioctl(pcam->display_fd, FBIOGET_PLANEINFO,
+ ((uintptr_t)&pcam->display_pinfo));
+
+ if (errcode == OK)
+ {
+ pcam->display_pinfo.fbmem = mmap(NULL, pcam->display_pinfo.fblen,
+ PROT_READ | PROT_WRITE,
+ MAP_SHARED | MAP_FILE,
+ pcam->display_fd, 0);
+ }
+
+ if (errcode < 0 || pcam->display_pinfo.fbmem == MAP_FAILED)
+ {
+ errcode = errno;
+ close(pcam->capture_fd);
+ close(pcam->display_fd);
+ verr("ERROR: ioctl(FBIOGET_PLANEINFO) failed: %d\n", -errcode);
+ return -errcode;
+ }
+
+ return OK;
+ }
+ else
+ {
+ /* TODO: Add file output */
+
+ return -ENOTSUP;
+ }
+
+#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE
+ }
+#endif
+
+ return -ENODEV;
+}
+
+/****************************************************************************
+ * Name: nxcamera_thread_loopthread
+ *
+ * This is the thread that streams the video and handles video controls.
+ *
+ ****************************************************************************/
+
+static void *nxcamera_loopthread(pthread_addr_t pvarg)
+{
+ FAR struct nxcamera_s *pcam = (FAR struct nxcamera_s *)pvarg;
+ unsigned int prio;
+ ssize_t size;
+ struct video_msg_s msg;
+ bool streaming = true;
+ int x;
+ int ret;
+ struct v4l2_buffer buf;
+ uint32_t type;
+
+ vinfo("Entry\n");
+
+ for (x = 0; x < pcam->nbuffers; x++)
+ {
+ buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ buf.memory = V4L2_MEMORY_MMAP;
+ buf.index = x;
+ ret = ioctl(pcam->capture_fd, VIDIOC_QBUF, (uintptr_t)&buf);
+ if (ret < 0)
+ {
+ verr("VIDIOC_QBUF failed: %d\n", -ret);
+ goto err_out;
+ }
+ }
+
+ /* VIDIOC_STREAMON start stream */
+
+ type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ ret = ioctl(pcam->capture_fd, VIDIOC_STREAMON, (uintptr_t)&type);
+ if (ret < 0)
+ {
+ verr("VIDIOC_STREAMON failed: %d\n", -ret);
+ goto err_out;
+ }
+ else
+ {
+ pcam->loopstate = NXCAMERA_STATE_STREAMING;
+ }
+
+ /* Loop until we specifically break. streaming == true means that we are
+ * still looping waiting for the stream to complete. All of the data
+ * may have been sent, but the stream is not complete until we get
+ * VIDEO_MSG_STOP message
+ *
+ * The normal protocol for looping errors detected by the video driver
+ * is as follows:
+ *
+ * (1) The video driver will indicated the error by returning a negated
+ * error value when the next buffer is enqueued. The upper level
+ * then knows that this buffer was not queued.
+ * (2) The video driver must return all queued buffers using the
+ * VIDEO_MSG_DEQUEUE message.
+ */
+
+ while (streaming)
+ {
+ size = mq_receive(pcam->mq, (FAR char *)&msg, sizeof(msg), &prio);
+
+ /* Validate a message was received */
+
+ if (size == sizeof(msg))
+ {
+ /* Perform operation based on message id */
+
+ vinfo("message received size %zd id%d\n", size, msg.msg_id);
+ switch (msg.msg_id)
+ {
+ /* An video buffer is being dequeued by the driver */
+
+ case VIDEO_MSG_DEQUEUE:
+ if (!streaming)
+ {
+ break;
+ }
+
+ if (ret == OK && pcam->loopstate == NXCAMERA_STATE_STREAMING)
+ {
+ if (ret == OK)
+ {
+ pcam->loopstate = NXCAMERA_STATE_LOOPING;
+ }
+ }
+
+ if (ret == OK)
+ {
+ /* Go through to VIDEO_MSG_STOP */
+
+ break;
+ }
+
+ /* Someone wants to stop the stream. */
+
+ case VIDEO_MSG_STOP:
+
+ /* Send a stop message to the device */
+
+ vinfo("Stopping looping\n");
+ ioctl(pcam->capture_fd, VIDIOC_STREAMOFF, 0);
+ streaming = false;
+ break;
+
+ /* Unknown / unsupported message ID */
+
+ default:
+ break;
+ }
+ }
+
+ ret = ioctl(pcam->capture_fd, VIDIOC_DQBUF, (uintptr_t)&buf);
+ if (ret)
+ {
+ verr("Fail DQBUF %d\n", errno);
+ goto err_out;
+ }
+
+ ret = show_image(pcam, &buf);
+ if (ret < 0)
+ {
+ verr("Fail to show image %d\n", -ret);
+ goto err_out;
+ }
+
+ ret = ioctl(pcam->capture_fd, VIDIOC_QBUF, (uintptr_t)&buf);
+ if (ret)
+ {
+ verr("Fail QBUF %d\n", errno);
+ goto err_out;
+ }
+
+ usleep(1);
+ }
+
+ /* Release our video buffers and unregister / release the device */
+
+err_out:
+ vinfo("Clean-up and exit\n");
+
+ /* Cleanup */
+
+ while (sem_wait(&pcam->sem) < 0)
+ {
+ }
+
+ close(pcam->display_fd); /* Close the display device */
+ close(pcam->capture_fd); /* Close the capture device */
+ pcam->display_fd = -1; /* Mark display device as closed */
+ pcam->capture_fd = -1; /* Mark capture device as closed */
+ mq_close(pcam->mq); /* Close the message queue */
+ mq_unlink(pcam->mqname); /* Unlink the message queue */
+ pcam->loopstate = NXCAMERA_STATE_IDLE;
+ free(pcam->bufs);
+ free(pcam->buf_sizes);
+ sem_post(&pcam->sem); /* Release the semaphore */
+
+ vinfo("Exit\n");
+
+ return NULL;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: nxcamera_setdevice
+ *
+ * nxcamera_setdevice() sets the preferred video device to use with the
+ * provided nxcamera context.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE
+int nxcamera_setdevice(FAR struct nxcamera_s *pcam,
+ FAR const char *device)
+{
+ int temp_fd;
+ struct v4l2_capability caps;
+
+ DEBUGASSERT(pcam != NULL);
+ DEBUGASSERT(device != NULL);
+
+ /* Try to open the device */
+
+ temp_fd = open(device, O_RDWR);
+ if (temp_fd == -1)
+ {
+ /* Error opening the device */
+
+ return -ENOENT;
+ }
+
+ /* Validate it's a video device by issuing an VIDIOC_QUERYCAP ioctl */
+
+ if (ioctl(temp_fd, VIDIOC_QUERYCAP, (uintptr_t)&caps) != OK)
+ {
+ /* Not an Video device! */
+
+ close(temp_fd);
+ return -ENODEV;
+ }
+
+ /* Close the file */
+
+ close(temp_fd);
+
+ /* Save the path of the preferred device */
+
+ if (caps.capabilities & V4L2_CAP_VIDEO_CAPTURE)
Review Comment:
why not return error if not set V4L2_CAP_VIDEO_CAPTURE
##########
system/nxcamera/nxcamera.c:
##########
@@ -0,0 +1,904 @@
+/****************************************************************************
+ * apps/system/nxcamera/nxcamera.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <assert.h>
+#include <debug.h>
+#include <dirent.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <nuttx/queue.h>
+#include <nuttx/video/video.h>
+#include <nuttx/video/fb.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <sys/types.h>
+
+#include "system/nxcamera.h"
+#ifdef CONFIG_LIBYUV
+# include "libyuv.h"
+#endif
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define NXCAMERA_STATE_IDLE 0
+#define NXCAMERA_STATE_STREAMING 1
+#define NXCAMERA_STATE_LOOPING 2
+#define NXCAMERA_STATE_PAUSED 3
+
+#define REQBUF_COUNT 3
+
+#ifndef MIN
+# define MIN(a, b) (((a) < (b)) ? (a) : (b))
+#endif
+
+#define convert_frame ConvertToARGB
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int show_image(FAR struct nxcamera_s *pcam, FAR v4l2_buffer_t *buf)
+{
+#ifdef CONFIG_LIBYUV
+ return convert_frame(pcam->bufs[buf->index],
+ pcam->buf_sizes[buf->index],
+ pcam->display_pinfo.fbmem,
+ pcam->display_pinfo.stride,
+ 0,
+ 0,
+ pcam->fmt.fmt.pix.width,
+ pcam->fmt.fmt.pix.height,
+ pcam->fmt.fmt.pix.width,
+ pcam->fmt.fmt.pix.height,
+ 0,
+ pcam->fmt.fmt.pix.pixelformat);
+#else
+ uint32_t *pbuf = pcam->bufs[buf->index];
+ vinfo("show image from %p: %lx %lx", pbuf, pbuf[0], pbuf[1]);
+ return 0;
+#endif
+}
+
+/****************************************************************************
+ * Name: nxcamera_opendevice
+ *
+ * nxcamera_opendevice() tries to open the preferred devices as specified.
+ *
+ * Return:
+ * OK if compatible device opened (searched or preferred)
+ * -ENODEV if no compatible device opened.
+ * -ENOENT if preferred device couldn't be opened.
+ *
+ ****************************************************************************/
+
+static int nxcamera_opendevice(FAR struct nxcamera_s *pcam)
+{
+ int errcode;
+
+#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE
+ if (pcam->capturedev[0] != '\0')
+ {
+#endif
+
+ if (pcam->displaydev[0] != '\0')
+ {
+#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE
+ /* If we have a preferred device, then open it */
+
+ pcam->capture_fd = open(pcam->capturedev, O_RDWR);
+#else
+ pcam->capture_fd = open(CONFIG_VIDEO_DEV_PATH, O_RDWR);
+#endif
+ if (pcam->capture_fd == -1)
+ {
+ errcode = errno;
+ DEBUGASSERT(errcode > 0);
+
+ verr("ERROR: Failed to open pcam->capturedev %d\n", -errcode);
+ return -errcode;
+ }
+
+ pcam->display_fd = open(pcam->displaydev, O_RDWR);
+ if (pcam->display_fd == -1)
+ {
+ errcode = errno;
+ DEBUGASSERT(errcode > 0);
+
+ close(pcam->capture_fd);
+ pcam->capture_fd = -1;
+ verr("ERROR: Failed to open pcam->displaydev %d\n", -errcode);
+ return -errcode;
+ }
+
+ errcode = ioctl(pcam->display_fd, FBIOGET_PLANEINFO,
+ ((uintptr_t)&pcam->display_pinfo));
+
+ if (errcode == OK)
+ {
+ pcam->display_pinfo.fbmem = mmap(NULL, pcam->display_pinfo.fblen,
+ PROT_READ | PROT_WRITE,
+ MAP_SHARED | MAP_FILE,
+ pcam->display_fd, 0);
+ }
+
+ if (errcode < 0 || pcam->display_pinfo.fbmem == MAP_FAILED)
+ {
+ errcode = errno;
+ close(pcam->capture_fd);
+ close(pcam->display_fd);
+ verr("ERROR: ioctl(FBIOGET_PLANEINFO) failed: %d\n", -errcode);
+ return -errcode;
+ }
+
+ return OK;
+ }
+ else
+ {
+ /* TODO: Add file output */
+
+ return -ENOTSUP;
+ }
+
+#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE
+ }
+#endif
+
+ return -ENODEV;
+}
+
+/****************************************************************************
+ * Name: nxcamera_thread_loopthread
+ *
+ * This is the thread that streams the video and handles video controls.
+ *
+ ****************************************************************************/
+
+static void *nxcamera_loopthread(pthread_addr_t pvarg)
+{
+ FAR struct nxcamera_s *pcam = (FAR struct nxcamera_s *)pvarg;
+ unsigned int prio;
+ ssize_t size;
+ struct video_msg_s msg;
+ bool streaming = true;
+ int x;
+ int ret;
+ struct v4l2_buffer buf;
+ uint32_t type;
+
+ vinfo("Entry\n");
+
+ for (x = 0; x < pcam->nbuffers; x++)
+ {
+ buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ buf.memory = V4L2_MEMORY_MMAP;
+ buf.index = x;
+ ret = ioctl(pcam->capture_fd, VIDIOC_QBUF, (uintptr_t)&buf);
+ if (ret < 0)
+ {
+ verr("VIDIOC_QBUF failed: %d\n", -ret);
+ goto err_out;
+ }
+ }
+
+ /* VIDIOC_STREAMON start stream */
+
+ type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ ret = ioctl(pcam->capture_fd, VIDIOC_STREAMON, (uintptr_t)&type);
+ if (ret < 0)
+ {
+ verr("VIDIOC_STREAMON failed: %d\n", -ret);
+ goto err_out;
+ }
+ else
+ {
+ pcam->loopstate = NXCAMERA_STATE_STREAMING;
+ }
+
+ /* Loop until we specifically break. streaming == true means that we are
+ * still looping waiting for the stream to complete. All of the data
+ * may have been sent, but the stream is not complete until we get
+ * VIDEO_MSG_STOP message
+ *
+ * The normal protocol for looping errors detected by the video driver
+ * is as follows:
+ *
+ * (1) The video driver will indicated the error by returning a negated
+ * error value when the next buffer is enqueued. The upper level
+ * then knows that this buffer was not queued.
+ * (2) The video driver must return all queued buffers using the
+ * VIDEO_MSG_DEQUEUE message.
+ */
+
+ while (streaming)
+ {
+ size = mq_receive(pcam->mq, (FAR char *)&msg, sizeof(msg), &prio);
+
+ /* Validate a message was received */
+
+ if (size == sizeof(msg))
+ {
+ /* Perform operation based on message id */
+
+ vinfo("message received size %zd id%d\n", size, msg.msg_id);
+ switch (msg.msg_id)
+ {
+ /* An video buffer is being dequeued by the driver */
+
+ case VIDEO_MSG_DEQUEUE:
+ if (!streaming)
+ {
+ break;
+ }
+
+ if (ret == OK && pcam->loopstate == NXCAMERA_STATE_STREAMING)
+ {
+ if (ret == OK)
+ {
+ pcam->loopstate = NXCAMERA_STATE_LOOPING;
+ }
+ }
+
+ if (ret == OK)
+ {
+ /* Go through to VIDEO_MSG_STOP */
+
+ break;
+ }
+
+ /* Someone wants to stop the stream. */
+
+ case VIDEO_MSG_STOP:
+
+ /* Send a stop message to the device */
+
+ vinfo("Stopping looping\n");
+ ioctl(pcam->capture_fd, VIDIOC_STREAMOFF, 0);
+ streaming = false;
+ break;
+
+ /* Unknown / unsupported message ID */
+
+ default:
+ break;
+ }
+ }
+
+ ret = ioctl(pcam->capture_fd, VIDIOC_DQBUF, (uintptr_t)&buf);
+ if (ret)
+ {
+ verr("Fail DQBUF %d\n", errno);
+ goto err_out;
+ }
+
+ ret = show_image(pcam, &buf);
+ if (ret < 0)
+ {
+ verr("Fail to show image %d\n", -ret);
+ goto err_out;
+ }
+
+ ret = ioctl(pcam->capture_fd, VIDIOC_QBUF, (uintptr_t)&buf);
+ if (ret)
+ {
+ verr("Fail QBUF %d\n", errno);
+ goto err_out;
+ }
+
+ usleep(1);
+ }
+
+ /* Release our video buffers and unregister / release the device */
+
+err_out:
+ vinfo("Clean-up and exit\n");
+
+ /* Cleanup */
+
+ while (sem_wait(&pcam->sem) < 0)
+ {
+ }
+
+ close(pcam->display_fd); /* Close the display device */
+ close(pcam->capture_fd); /* Close the capture device */
+ pcam->display_fd = -1; /* Mark display device as closed */
+ pcam->capture_fd = -1; /* Mark capture device as closed */
+ mq_close(pcam->mq); /* Close the message queue */
+ mq_unlink(pcam->mqname); /* Unlink the message queue */
+ pcam->loopstate = NXCAMERA_STATE_IDLE;
+ free(pcam->bufs);
+ free(pcam->buf_sizes);
+ sem_post(&pcam->sem); /* Release the semaphore */
+
+ vinfo("Exit\n");
+
+ return NULL;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: nxcamera_setdevice
+ *
+ * nxcamera_setdevice() sets the preferred video device to use with the
+ * provided nxcamera context.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE
+int nxcamera_setdevice(FAR struct nxcamera_s *pcam,
+ FAR const char *device)
+{
+ int temp_fd;
+ struct v4l2_capability caps;
+
+ DEBUGASSERT(pcam != NULL);
+ DEBUGASSERT(device != NULL);
+
+ /* Try to open the device */
+
+ temp_fd = open(device, O_RDWR);
+ if (temp_fd == -1)
+ {
+ /* Error opening the device */
+
+ return -ENOENT;
+ }
+
+ /* Validate it's a video device by issuing an VIDIOC_QUERYCAP ioctl */
+
+ if (ioctl(temp_fd, VIDIOC_QUERYCAP, (uintptr_t)&caps) != OK)
+ {
+ /* Not an Video device! */
+
+ close(temp_fd);
+ return -ENODEV;
+ }
+
+ /* Close the file */
+
+ close(temp_fd);
+
+ /* Save the path of the preferred device */
+
+ if (caps.capabilities & V4L2_CAP_VIDEO_CAPTURE)
+ {
+ strlcpy(pcam->capturedev, device, sizeof(pcam->capturedev));
+ }
+
+ return OK;
+}
+#endif /* CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE */
+
+/****************************************************************************
+ * Name: nxcamera_setfb
+ *
+ * nxcamera_setfb() sets the output framebuffer device to use with the
+ * provided nxcamera context.
+ *
+ ****************************************************************************/
+
+int nxcamera_setfb(FAR struct nxcamera_s *pcam, FAR const char *device)
+{
+ int temp_fd;
+ struct fb_videoinfo_s vinfo;
+
+ DEBUGASSERT(pcam != NULL);
+ DEBUGASSERT(device != NULL);
+
+ /* Try to open the device */
+
+ temp_fd = open(device, O_RDWR);
+ if (temp_fd == -1)
+ {
+ /* Error opening the device */
+
+ return -ENOENT;
+ }
+
+ /* Validate it's a fb device by issuing an FBIOGET_VIDEOINFO ioctl */
+
+ if (ioctl(temp_fd, FBIOGET_VIDEOINFO, (uintptr_t)&vinfo) != OK)
+ {
+ /* Not an Video device! */
+
+ close(temp_fd);
+ return -ENODEV;
+ }
+
+ /* Close the file */
+
+ close(temp_fd);
+
+ /* Save the path of the framebuffer device */
+
+ if (vinfo.nplanes > 0)
+ {
+ strlcpy(pcam->displaydev, device, sizeof(pcam->displaydev));
+ }
+
+ return OK;
+}
+
+/****************************************************************************
+ * Name: nxcamera_setfile
+ *
+ * nxcamera_setfile() sets the output file path to use with the provided
+ * nxcamera context.
+ *
+ ****************************************************************************/
+
+int nxcamera_setfile(FAR struct nxcamera_s *pcam, FAR const char *pfile,
+ bool isimage)
+{
+ int temp_fd;
+
+ DEBUGASSERT(pcam != NULL);
+ DEBUGASSERT(pfile != NULL);
+
+ /* Try to open the file */
+
+ temp_fd = open(pfile, O_CREAT | O_RDWR);
+ if (temp_fd == -1)
+ {
+ /* Error opening the file */
+
+ return -EACCES;
+ }
+
+ /* Close the file */
+
+ close(temp_fd);
+
+ /* Save the path of the output file */
+
+ if (isimage)
+ {
+ strlcpy(pcam->oimagepath, pfile, sizeof(pcam->oimagepath));
+ }
+ else
+ {
+ strlcpy(pcam->ovideopath, pfile, sizeof(pcam->ovideopath));
+ }
+
+ return OK;
+}
+
+/****************************************************************************
+ * Name: nxcamera_stop
+ *
+ * nxcamera_stop() stops the current playback to loop and closes the
+ * file and the associated device.
+ *
+ * Input:
+ * pcam Pointer to the initialized looper context
+ *
+ ****************************************************************************/
+
+int nxcamera_stop(FAR struct nxcamera_s *pcam)
+{
+ struct video_msg_s term_msg;
+ FAR void *value;
+
+ DEBUGASSERT(pcam != NULL);
+
+ /* Validate we are not in IDLE state */
+
+ sem_wait(&pcam->sem); /* Get the semaphore */
+ if (pcam->loopstate == NXCAMERA_STATE_IDLE)
+ {
+ sem_post(&pcam->sem); /* Release the semaphore */
+ return OK;
+ }
+
+ sem_post(&pcam->sem);
+
+ /* Notify the stream thread that it needs to cancel the stream */
+
+ term_msg.msg_id = VIDEO_MSG_STOP;
+ term_msg.u.data = 0;
+ mq_send(pcam->mq, (FAR const char *)&term_msg, sizeof(term_msg),
+ CONFIG_NXCAMERA_MSG_PRIO);
+
+ /* Join the thread. The thread will do all the cleanup. */
+
+ pthread_join(pcam->loop_id, &value);
+ pcam->loop_id = 0;
+
+ return OK;
+}
+
+/****************************************************************************
+ * Name: nxcamera_stream
+ *
+ * nxcamera_stream() tries to capture and then display the raw data using
+ * the Video system. If a capture device is specified, it will try to use
+ * that device.
+ *
+ * Input:
+ * pcam Pointer to the initialized Looper context
+ * width Capture frame width
+ * height Capture frame height
+ * framerate Capture frame rate
+ * format Capture frame pixel format
+ *
+ * Returns:
+ * OK Video is being looped
+ * -EBUSY Capture device is busy
+ * -ENOSYS No supported video format found
+ * -ENODEV No video capture or framebuffer device suitable
+ *
+ ****************************************************************************/
+
+int nxcamera_stream(FAR struct nxcamera_s *pcam,
+ uint16_t width, uint16_t height,
+ uint32_t framerate, uint32_t format)
+{
+ struct mq_attr attr;
+ struct sched_param sparam;
+ pthread_attr_t tattr;
+ FAR void *value;
+ int ret;
+ int i;
+ struct v4l2_buffer buf;
+ struct v4l2_requestbuffers req;
+ struct v4l2_streamparm parm;
+
+ DEBUGASSERT(pcam != NULL);
+
+ if (pcam->loopstate != NXCAMERA_STATE_IDLE)
+ {
+ return -EBUSY;
+ }
+
+ vinfo("==============================\n");
+ vinfo("streaming video\n");
+ vinfo("==============================\n");
+
+ /* Try to open the device */
+
+ ret = nxcamera_opendevice(pcam);
+ if (ret < 0)
+ {
+ /* Error opening the device */
+
+ verr("ERROR: nxcamera_opendevice failed: %d\n", ret);
+ return ret;
+ }
+
+ /* VIDIOC_S_FMT set format */
+
+ pcam->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ pcam->fmt.fmt.pix.width = width;
+ pcam->fmt.fmt.pix.height = height;
+ pcam->fmt.fmt.pix.field = V4L2_FIELD_ANY;
+ pcam->fmt.fmt.pix.pixelformat = format;
+
+ ret = ioctl(pcam->capture_fd, VIDIOC_S_FMT, (uintptr_t)&pcam->fmt);
+ if (ret < 0)
+ {
+ verr("VIDIOC_S_FMT failed: %d\n", -ret);
+ return ret;
+ }
+
+ memset(&parm, 0, sizeof(parm));
+ parm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ parm.parm.capture.timeperframe.denominator = framerate;
+ parm.parm.capture.timeperframe.numerator = 1;
+ ret = ioctl(pcam->capture_fd, VIDIOC_S_PARM, (uintptr_t)&parm);
+ if (ret < 0)
+ {
+ verr("VIDIOC_S_PARM failed: %d\n", -ret);
+ return ret;
+ }
+
+ /* VIDIOC_REQBUFS initiate user pointer I/O */
+
+ req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ req.memory = V4L2_MEMORY_MMAP;
+ req.count = REQBUF_COUNT;
+
+ ret = ioctl(pcam->capture_fd, VIDIOC_REQBUFS, (uintptr_t)&req);
+ if (ret < 0)
+ {
+ verr("VIDIOC_REQBUFS failed: %d\n", -ret);
+ return ret;
+ }
+
+ if (req.count < 2)
+ {
+ verr("VIDIOC_REQBUFS failed: not enough buffers\n");
+ return -ENOMEM;
+ }
+
+ pcam->nbuffers = req.count;
+ pcam->bufs = calloc(req.count, sizeof(*pcam->bufs));
+ pcam->buf_sizes = calloc(req.count, sizeof(*pcam->buf_sizes));
+ if (!pcam->bufs)
+ {
+ verr("Cannot allocate buffer pointers\n");
+ return -ENOMEM;
+ }
+
+ /* VIDIOC_QBUF enqueue buffer */
+
+ for (i = 0; i < req.count; i++)
+ {
+ buf.type = req.type;
+ buf.memory = V4L2_MEMORY_MMAP;
+ buf.index = i;
+ ret = ioctl(pcam->capture_fd, VIDIOC_QUERYBUF, (uintptr_t)&buf);
+ if (ret < 0)
+ {
+ verr("VIDIOC_QUERYBUF failed: %d\n", -ret);
+ return ret;
+ }
+
+ pcam->bufs[i] = mmap(NULL, buf.length,
+ PROT_READ | PROT_WRITE, MAP_SHARED,
+ pcam->capture_fd, buf.m.offset);
+ if (pcam->bufs[i] == MAP_FAILED)
+ {
+ verr("MMAP failed\n");
+ return ret;
+ }
+
+ pcam->buf_sizes[i] = buf.length;
+ }
+
+ /* Create a message queue for the loopthread */
+
+ attr.mq_maxmsg = req.count + 8;
+ attr.mq_msgsize = sizeof(struct video_msg_s);
+ attr.mq_curmsgs = 0;
+ attr.mq_flags = 0;
+
+ snprintf(pcam->mqname, sizeof(pcam->mqname), "/tmp/%lx",
+ (unsigned long)((uintptr_t)pcam) & 0xffffffff);
+
+ pcam->mq = mq_open(pcam->mqname, O_RDWR | O_CREAT | O_NONBLOCK, 0644,
+ &attr);
+ if (pcam->mq == (mqd_t) -1)
+ {
+ /* Unable to open message queue! */
+
+ ret = -errno;
+ auderr("ERROR: mq_open failed: %d\n", ret);
+ goto err_out;
+ }
+
+ /* Check if there was a previous thread and join it if there was
+ * to perform clean-up.
+ */
+
+ if (pcam->loop_id != 0)
+ {
+ pthread_join(pcam->loop_id, &value);
+ }
+
+ pthread_attr_init(&tattr);
+ sparam.sched_priority = sched_get_priority_max(SCHED_FIFO) - 9;
+ pthread_attr_setschedparam(&tattr, &sparam);
+ pthread_attr_setstacksize(&tattr, CONFIG_NXCAMERA_LOOPTHREAD_STACKSIZE);
+
+ /* Add a reference count to the looper for the thread and start the
+ * thread. We increment for the thread to avoid thread start-up
+ * race conditions.
+ */
+
+ nxcamera_reference(pcam);
+ ret = pthread_create(&pcam->loop_id, &tattr, nxcamera_loopthread,
+ (pthread_addr_t)pcam);
+ pthread_attr_destroy(&tattr);
+ if (ret != OK)
+ {
+ ret = -ret;
+ verr("ERROR: Failed to create loopthread: %d\n", ret);
+ goto err_out;
+ }
+
+ /* Name the thread */
+
+ pthread_setname_np(pcam->loop_id, "nxcameraloop");
+ return OK;
+
+err_out:
+ close(pcam->display_fd);
+ pcam->display_fd = -1;
+ close(pcam->capture_fd);
+ pcam->capture_fd = -1;
+ free(pcam->bufs);
+ free(pcam->buf_sizes);
+ return ret;
+}
+
+/****************************************************************************
+ * Name: nxcamera_set
+ *
+ * nxcamera_set() sets camera 3A controls on the fly.
+ *
+ ****************************************************************************/
+
+int nxcamera_set(FAR struct nxcamera_s *pcam)
+{
+ /* TODO */
+
+ return 0;
+}
+
+/****************************************************************************
+ * Name: nxcamera_create
+ *
+ * nxcamera_create() allocates and initializes a nxcamera context for
+ * use by further nxcamera operations. This routine must be called before
+ * to perform the create for proper reference counting.
+ *
+ * Input Parameters: None
+ *
+ * Returned values:
+ * Pointer to the created context or NULL if there was an error.
+ *
+ ****************************************************************************/
+
+FAR struct nxcamera_s *nxcamera_create(void)
+{
+ FAR struct nxcamera_s *pcam;
+
+ /* Allocate the memory */
+
+ pcam = (FAR struct nxcamera_s *)malloc(sizeof(struct nxcamera_s));
+ if (pcam == NULL)
+ {
+ return NULL;
+ }
+
+ /* Initialize the context data */
+
+ pcam->loopstate = NXCAMERA_STATE_IDLE;
+ pcam->display_fd = -1;
+ pcam->capture_fd = -1;
+#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE
+ pcam->displaydev[0] = '\0';
+#endif
+ pcam->capturedev[0] = '\0';
+ pcam->mq = 0;
+ pcam->loop_id = 0;
+ pcam->crefs = 0;
+
+ sem_init(&pcam->sem, 0, 1);
+
+ return pcam;
+}
+
+/****************************************************************************
+ * Name: nxcamera_release
+ *
+ * nxcamera_release() reduces the reference count by one and if it
+ * reaches zero, frees the context.
+ *
+ * Input Parameters:
+ * pcam Pointer to the NxCamera context
+ *
+ * Returned values: None
+ *
+ ****************************************************************************/
+
+void nxcamera_release(FAR struct nxcamera_s *pcam)
+{
+ int refcount;
+ FAR void *value;
+
+ /* Grab the semaphore */
+
+ while (sem_wait(&pcam->sem) < 0)
Review Comment:
let's use pthread_mutex_t for locking
##########
system/nxcamera/nxcamera.c:
##########
@@ -0,0 +1,904 @@
+/****************************************************************************
+ * apps/system/nxcamera/nxcamera.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <assert.h>
+#include <debug.h>
+#include <dirent.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <nuttx/queue.h>
+#include <nuttx/video/video.h>
+#include <nuttx/video/fb.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <sys/types.h>
+
+#include "system/nxcamera.h"
+#ifdef CONFIG_LIBYUV
+# include "libyuv.h"
+#endif
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define NXCAMERA_STATE_IDLE 0
+#define NXCAMERA_STATE_STREAMING 1
+#define NXCAMERA_STATE_LOOPING 2
+#define NXCAMERA_STATE_PAUSED 3
+
+#define REQBUF_COUNT 3
+
+#ifndef MIN
+# define MIN(a, b) (((a) < (b)) ? (a) : (b))
+#endif
+
+#define convert_frame ConvertToARGB
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int show_image(FAR struct nxcamera_s *pcam, FAR v4l2_buffer_t *buf)
+{
+#ifdef CONFIG_LIBYUV
+ return convert_frame(pcam->bufs[buf->index],
+ pcam->buf_sizes[buf->index],
+ pcam->display_pinfo.fbmem,
+ pcam->display_pinfo.stride,
+ 0,
+ 0,
+ pcam->fmt.fmt.pix.width,
+ pcam->fmt.fmt.pix.height,
+ pcam->fmt.fmt.pix.width,
+ pcam->fmt.fmt.pix.height,
+ 0,
+ pcam->fmt.fmt.pix.pixelformat);
+#else
+ uint32_t *pbuf = pcam->bufs[buf->index];
+ vinfo("show image from %p: %lx %lx", pbuf, pbuf[0], pbuf[1]);
+ return 0;
+#endif
+}
+
+/****************************************************************************
+ * Name: nxcamera_opendevice
+ *
+ * nxcamera_opendevice() tries to open the preferred devices as specified.
+ *
+ * Return:
+ * OK if compatible device opened (searched or preferred)
+ * -ENODEV if no compatible device opened.
+ * -ENOENT if preferred device couldn't be opened.
+ *
+ ****************************************************************************/
+
+static int nxcamera_opendevice(FAR struct nxcamera_s *pcam)
+{
+ int errcode;
+
+#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE
+ if (pcam->capturedev[0] != '\0')
+ {
+#endif
+
+ if (pcam->displaydev[0] != '\0')
+ {
+#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE
+ /* If we have a preferred device, then open it */
+
+ pcam->capture_fd = open(pcam->capturedev, O_RDWR);
+#else
+ pcam->capture_fd = open(CONFIG_VIDEO_DEV_PATH, O_RDWR);
+#endif
+ if (pcam->capture_fd == -1)
+ {
+ errcode = errno;
+ DEBUGASSERT(errcode > 0);
+
+ verr("ERROR: Failed to open pcam->capturedev %d\n", -errcode);
+ return -errcode;
+ }
+
+ pcam->display_fd = open(pcam->displaydev, O_RDWR);
+ if (pcam->display_fd == -1)
+ {
+ errcode = errno;
+ DEBUGASSERT(errcode > 0);
+
+ close(pcam->capture_fd);
+ pcam->capture_fd = -1;
+ verr("ERROR: Failed to open pcam->displaydev %d\n", -errcode);
+ return -errcode;
+ }
+
+ errcode = ioctl(pcam->display_fd, FBIOGET_PLANEINFO,
+ ((uintptr_t)&pcam->display_pinfo));
+
+ if (errcode == OK)
+ {
+ pcam->display_pinfo.fbmem = mmap(NULL, pcam->display_pinfo.fblen,
+ PROT_READ | PROT_WRITE,
+ MAP_SHARED | MAP_FILE,
+ pcam->display_fd, 0);
+ }
+
+ if (errcode < 0 || pcam->display_pinfo.fbmem == MAP_FAILED)
+ {
+ errcode = errno;
+ close(pcam->capture_fd);
+ close(pcam->display_fd);
+ verr("ERROR: ioctl(FBIOGET_PLANEINFO) failed: %d\n", -errcode);
+ return -errcode;
+ }
+
+ return OK;
+ }
+ else
+ {
+ /* TODO: Add file output */
+
+ return -ENOTSUP;
+ }
+
+#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE
+ }
+#endif
+
+ return -ENODEV;
+}
+
+/****************************************************************************
+ * Name: nxcamera_thread_loopthread
+ *
+ * This is the thread that streams the video and handles video controls.
+ *
+ ****************************************************************************/
+
+static void *nxcamera_loopthread(pthread_addr_t pvarg)
+{
+ FAR struct nxcamera_s *pcam = (FAR struct nxcamera_s *)pvarg;
+ unsigned int prio;
+ ssize_t size;
+ struct video_msg_s msg;
+ bool streaming = true;
+ int x;
+ int ret;
+ struct v4l2_buffer buf;
+ uint32_t type;
+
+ vinfo("Entry\n");
+
+ for (x = 0; x < pcam->nbuffers; x++)
+ {
+ buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ buf.memory = V4L2_MEMORY_MMAP;
+ buf.index = x;
+ ret = ioctl(pcam->capture_fd, VIDIOC_QBUF, (uintptr_t)&buf);
+ if (ret < 0)
+ {
+ verr("VIDIOC_QBUF failed: %d\n", -ret);
+ goto err_out;
+ }
+ }
+
+ /* VIDIOC_STREAMON start stream */
+
+ type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ ret = ioctl(pcam->capture_fd, VIDIOC_STREAMON, (uintptr_t)&type);
+ if (ret < 0)
+ {
+ verr("VIDIOC_STREAMON failed: %d\n", -ret);
+ goto err_out;
+ }
+ else
+ {
+ pcam->loopstate = NXCAMERA_STATE_STREAMING;
+ }
+
+ /* Loop until we specifically break. streaming == true means that we are
+ * still looping waiting for the stream to complete. All of the data
+ * may have been sent, but the stream is not complete until we get
+ * VIDEO_MSG_STOP message
+ *
+ * The normal protocol for looping errors detected by the video driver
+ * is as follows:
+ *
+ * (1) The video driver will indicated the error by returning a negated
+ * error value when the next buffer is enqueued. The upper level
+ * then knows that this buffer was not queued.
+ * (2) The video driver must return all queued buffers using the
+ * VIDEO_MSG_DEQUEUE message.
+ */
+
+ while (streaming)
+ {
+ size = mq_receive(pcam->mq, (FAR char *)&msg, sizeof(msg), &prio);
+
+ /* Validate a message was received */
+
+ if (size == sizeof(msg))
+ {
+ /* Perform operation based on message id */
+
+ vinfo("message received size %zd id%d\n", size, msg.msg_id);
+ switch (msg.msg_id)
+ {
+ /* An video buffer is being dequeued by the driver */
+
+ case VIDEO_MSG_DEQUEUE:
+ if (!streaming)
+ {
+ break;
+ }
+
+ if (ret == OK && pcam->loopstate == NXCAMERA_STATE_STREAMING)
+ {
+ if (ret == OK)
+ {
+ pcam->loopstate = NXCAMERA_STATE_LOOPING;
+ }
+ }
+
+ if (ret == OK)
+ {
+ /* Go through to VIDEO_MSG_STOP */
+
+ break;
+ }
+
+ /* Someone wants to stop the stream. */
+
+ case VIDEO_MSG_STOP:
+
+ /* Send a stop message to the device */
+
+ vinfo("Stopping looping\n");
+ ioctl(pcam->capture_fd, VIDIOC_STREAMOFF, 0);
+ streaming = false;
+ break;
+
+ /* Unknown / unsupported message ID */
+
+ default:
+ break;
+ }
+ }
+
+ ret = ioctl(pcam->capture_fd, VIDIOC_DQBUF, (uintptr_t)&buf);
+ if (ret)
+ {
+ verr("Fail DQBUF %d\n", errno);
+ goto err_out;
+ }
+
+ ret = show_image(pcam, &buf);
+ if (ret < 0)
+ {
+ verr("Fail to show image %d\n", -ret);
+ goto err_out;
+ }
+
+ ret = ioctl(pcam->capture_fd, VIDIOC_QBUF, (uintptr_t)&buf);
+ if (ret)
+ {
+ verr("Fail QBUF %d\n", errno);
+ goto err_out;
+ }
+
+ usleep(1);
+ }
+
+ /* Release our video buffers and unregister / release the device */
+
+err_out:
+ vinfo("Clean-up and exit\n");
+
+ /* Cleanup */
+
+ while (sem_wait(&pcam->sem) < 0)
+ {
+ }
+
+ close(pcam->display_fd); /* Close the display device */
+ close(pcam->capture_fd); /* Close the capture device */
+ pcam->display_fd = -1; /* Mark display device as closed */
+ pcam->capture_fd = -1; /* Mark capture device as closed */
+ mq_close(pcam->mq); /* Close the message queue */
+ mq_unlink(pcam->mqname); /* Unlink the message queue */
+ pcam->loopstate = NXCAMERA_STATE_IDLE;
+ free(pcam->bufs);
+ free(pcam->buf_sizes);
+ sem_post(&pcam->sem); /* Release the semaphore */
+
+ vinfo("Exit\n");
+
+ return NULL;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: nxcamera_setdevice
+ *
+ * nxcamera_setdevice() sets the preferred video device to use with the
+ * provided nxcamera context.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE
+int nxcamera_setdevice(FAR struct nxcamera_s *pcam,
+ FAR const char *device)
+{
+ int temp_fd;
+ struct v4l2_capability caps;
+
+ DEBUGASSERT(pcam != NULL);
+ DEBUGASSERT(device != NULL);
+
+ /* Try to open the device */
+
+ temp_fd = open(device, O_RDWR);
+ if (temp_fd == -1)
+ {
+ /* Error opening the device */
+
+ return -ENOENT;
+ }
+
+ /* Validate it's a video device by issuing an VIDIOC_QUERYCAP ioctl */
+
+ if (ioctl(temp_fd, VIDIOC_QUERYCAP, (uintptr_t)&caps) != OK)
+ {
+ /* Not an Video device! */
+
+ close(temp_fd);
+ return -ENODEV;
+ }
+
+ /* Close the file */
+
+ close(temp_fd);
+
+ /* Save the path of the preferred device */
+
+ if (caps.capabilities & V4L2_CAP_VIDEO_CAPTURE)
+ {
+ strlcpy(pcam->capturedev, device, sizeof(pcam->capturedev));
+ }
+
+ return OK;
+}
+#endif /* CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE */
+
+/****************************************************************************
+ * Name: nxcamera_setfb
+ *
+ * nxcamera_setfb() sets the output framebuffer device to use with the
+ * provided nxcamera context.
+ *
+ ****************************************************************************/
+
+int nxcamera_setfb(FAR struct nxcamera_s *pcam, FAR const char *device)
+{
+ int temp_fd;
+ struct fb_videoinfo_s vinfo;
+
+ DEBUGASSERT(pcam != NULL);
+ DEBUGASSERT(device != NULL);
+
+ /* Try to open the device */
+
+ temp_fd = open(device, O_RDWR);
+ if (temp_fd == -1)
+ {
+ /* Error opening the device */
+
+ return -ENOENT;
+ }
+
+ /* Validate it's a fb device by issuing an FBIOGET_VIDEOINFO ioctl */
+
+ if (ioctl(temp_fd, FBIOGET_VIDEOINFO, (uintptr_t)&vinfo) != OK)
+ {
+ /* Not an Video device! */
+
+ close(temp_fd);
+ return -ENODEV;
+ }
+
+ /* Close the file */
+
+ close(temp_fd);
+
+ /* Save the path of the framebuffer device */
+
+ if (vinfo.nplanes > 0)
+ {
+ strlcpy(pcam->displaydev, device, sizeof(pcam->displaydev));
+ }
+
+ return OK;
+}
+
+/****************************************************************************
+ * Name: nxcamera_setfile
+ *
+ * nxcamera_setfile() sets the output file path to use with the provided
+ * nxcamera context.
+ *
+ ****************************************************************************/
+
+int nxcamera_setfile(FAR struct nxcamera_s *pcam, FAR const char *pfile,
+ bool isimage)
+{
+ int temp_fd;
+
+ DEBUGASSERT(pcam != NULL);
+ DEBUGASSERT(pfile != NULL);
+
+ /* Try to open the file */
+
+ temp_fd = open(pfile, O_CREAT | O_RDWR);
+ if (temp_fd == -1)
+ {
+ /* Error opening the file */
+
+ return -EACCES;
+ }
+
+ /* Close the file */
+
+ close(temp_fd);
+
+ /* Save the path of the output file */
+
+ if (isimage)
+ {
+ strlcpy(pcam->oimagepath, pfile, sizeof(pcam->oimagepath));
+ }
+ else
+ {
+ strlcpy(pcam->ovideopath, pfile, sizeof(pcam->ovideopath));
+ }
+
+ return OK;
+}
+
+/****************************************************************************
+ * Name: nxcamera_stop
+ *
+ * nxcamera_stop() stops the current playback to loop and closes the
+ * file and the associated device.
+ *
+ * Input:
+ * pcam Pointer to the initialized looper context
+ *
+ ****************************************************************************/
+
+int nxcamera_stop(FAR struct nxcamera_s *pcam)
+{
+ struct video_msg_s term_msg;
+ FAR void *value;
+
+ DEBUGASSERT(pcam != NULL);
+
+ /* Validate we are not in IDLE state */
+
+ sem_wait(&pcam->sem); /* Get the semaphore */
+ if (pcam->loopstate == NXCAMERA_STATE_IDLE)
+ {
+ sem_post(&pcam->sem); /* Release the semaphore */
+ return OK;
+ }
+
+ sem_post(&pcam->sem);
+
+ /* Notify the stream thread that it needs to cancel the stream */
+
+ term_msg.msg_id = VIDEO_MSG_STOP;
+ term_msg.u.data = 0;
+ mq_send(pcam->mq, (FAR const char *)&term_msg, sizeof(term_msg),
+ CONFIG_NXCAMERA_MSG_PRIO);
+
+ /* Join the thread. The thread will do all the cleanup. */
+
+ pthread_join(pcam->loop_id, &value);
+ pcam->loop_id = 0;
+
+ return OK;
+}
+
+/****************************************************************************
+ * Name: nxcamera_stream
+ *
+ * nxcamera_stream() tries to capture and then display the raw data using
+ * the Video system. If a capture device is specified, it will try to use
+ * that device.
+ *
+ * Input:
+ * pcam Pointer to the initialized Looper context
+ * width Capture frame width
+ * height Capture frame height
+ * framerate Capture frame rate
+ * format Capture frame pixel format
+ *
+ * Returns:
+ * OK Video is being looped
+ * -EBUSY Capture device is busy
+ * -ENOSYS No supported video format found
+ * -ENODEV No video capture or framebuffer device suitable
+ *
+ ****************************************************************************/
+
+int nxcamera_stream(FAR struct nxcamera_s *pcam,
+ uint16_t width, uint16_t height,
+ uint32_t framerate, uint32_t format)
+{
+ struct mq_attr attr;
+ struct sched_param sparam;
+ pthread_attr_t tattr;
+ FAR void *value;
+ int ret;
+ int i;
+ struct v4l2_buffer buf;
+ struct v4l2_requestbuffers req;
+ struct v4l2_streamparm parm;
+
+ DEBUGASSERT(pcam != NULL);
+
+ if (pcam->loopstate != NXCAMERA_STATE_IDLE)
+ {
+ return -EBUSY;
+ }
+
+ vinfo("==============================\n");
+ vinfo("streaming video\n");
+ vinfo("==============================\n");
+
+ /* Try to open the device */
+
+ ret = nxcamera_opendevice(pcam);
+ if (ret < 0)
+ {
+ /* Error opening the device */
+
+ verr("ERROR: nxcamera_opendevice failed: %d\n", ret);
+ return ret;
+ }
+
+ /* VIDIOC_S_FMT set format */
+
+ pcam->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ pcam->fmt.fmt.pix.width = width;
+ pcam->fmt.fmt.pix.height = height;
+ pcam->fmt.fmt.pix.field = V4L2_FIELD_ANY;
+ pcam->fmt.fmt.pix.pixelformat = format;
+
+ ret = ioctl(pcam->capture_fd, VIDIOC_S_FMT, (uintptr_t)&pcam->fmt);
+ if (ret < 0)
+ {
+ verr("VIDIOC_S_FMT failed: %d\n", -ret);
+ return ret;
+ }
+
+ memset(&parm, 0, sizeof(parm));
+ parm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ parm.parm.capture.timeperframe.denominator = framerate;
+ parm.parm.capture.timeperframe.numerator = 1;
+ ret = ioctl(pcam->capture_fd, VIDIOC_S_PARM, (uintptr_t)&parm);
+ if (ret < 0)
+ {
+ verr("VIDIOC_S_PARM failed: %d\n", -ret);
+ return ret;
+ }
+
+ /* VIDIOC_REQBUFS initiate user pointer I/O */
+
+ req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ req.memory = V4L2_MEMORY_MMAP;
+ req.count = REQBUF_COUNT;
+
+ ret = ioctl(pcam->capture_fd, VIDIOC_REQBUFS, (uintptr_t)&req);
+ if (ret < 0)
+ {
+ verr("VIDIOC_REQBUFS failed: %d\n", -ret);
+ return ret;
+ }
+
+ if (req.count < 2)
+ {
+ verr("VIDIOC_REQBUFS failed: not enough buffers\n");
+ return -ENOMEM;
+ }
+
+ pcam->nbuffers = req.count;
+ pcam->bufs = calloc(req.count, sizeof(*pcam->bufs));
+ pcam->buf_sizes = calloc(req.count, sizeof(*pcam->buf_sizes));
+ if (!pcam->bufs)
+ {
+ verr("Cannot allocate buffer pointers\n");
+ return -ENOMEM;
+ }
+
+ /* VIDIOC_QBUF enqueue buffer */
+
+ for (i = 0; i < req.count; i++)
+ {
+ buf.type = req.type;
+ buf.memory = V4L2_MEMORY_MMAP;
+ buf.index = i;
+ ret = ioctl(pcam->capture_fd, VIDIOC_QUERYBUF, (uintptr_t)&buf);
+ if (ret < 0)
+ {
+ verr("VIDIOC_QUERYBUF failed: %d\n", -ret);
+ return ret;
+ }
+
+ pcam->bufs[i] = mmap(NULL, buf.length,
+ PROT_READ | PROT_WRITE, MAP_SHARED,
+ pcam->capture_fd, buf.m.offset);
+ if (pcam->bufs[i] == MAP_FAILED)
+ {
+ verr("MMAP failed\n");
+ return ret;
+ }
+
+ pcam->buf_sizes[i] = buf.length;
+ }
+
+ /* Create a message queue for the loopthread */
+
+ attr.mq_maxmsg = req.count + 8;
+ attr.mq_msgsize = sizeof(struct video_msg_s);
+ attr.mq_curmsgs = 0;
+ attr.mq_flags = 0;
+
+ snprintf(pcam->mqname, sizeof(pcam->mqname), "/tmp/%lx",
+ (unsigned long)((uintptr_t)pcam) & 0xffffffff);
+
+ pcam->mq = mq_open(pcam->mqname, O_RDWR | O_CREAT | O_NONBLOCK, 0644,
+ &attr);
+ if (pcam->mq == (mqd_t) -1)
+ {
+ /* Unable to open message queue! */
+
+ ret = -errno;
+ auderr("ERROR: mq_open failed: %d\n", ret);
+ goto err_out;
+ }
+
+ /* Check if there was a previous thread and join it if there was
+ * to perform clean-up.
+ */
+
+ if (pcam->loop_id != 0)
+ {
+ pthread_join(pcam->loop_id, &value);
+ }
+
+ pthread_attr_init(&tattr);
+ sparam.sched_priority = sched_get_priority_max(SCHED_FIFO) - 9;
+ pthread_attr_setschedparam(&tattr, &sparam);
+ pthread_attr_setstacksize(&tattr, CONFIG_NXCAMERA_LOOPTHREAD_STACKSIZE);
+
+ /* Add a reference count to the looper for the thread and start the
+ * thread. We increment for the thread to avoid thread start-up
+ * race conditions.
+ */
+
+ nxcamera_reference(pcam);
+ ret = pthread_create(&pcam->loop_id, &tattr, nxcamera_loopthread,
+ (pthread_addr_t)pcam);
+ pthread_attr_destroy(&tattr);
+ if (ret != OK)
+ {
+ ret = -ret;
+ verr("ERROR: Failed to create loopthread: %d\n", ret);
+ goto err_out;
+ }
+
+ /* Name the thread */
+
+ pthread_setname_np(pcam->loop_id, "nxcameraloop");
+ return OK;
+
+err_out:
+ close(pcam->display_fd);
+ pcam->display_fd = -1;
+ close(pcam->capture_fd);
+ pcam->capture_fd = -1;
+ free(pcam->bufs);
+ free(pcam->buf_sizes);
+ return ret;
+}
+
+/****************************************************************************
+ * Name: nxcamera_set
+ *
+ * nxcamera_set() sets camera 3A controls on the fly.
+ *
+ ****************************************************************************/
+
+int nxcamera_set(FAR struct nxcamera_s *pcam)
Review Comment:
remove here and header file, let's add later
##########
system/nxcamera/nxcamera.c:
##########
@@ -0,0 +1,904 @@
+/****************************************************************************
+ * apps/system/nxcamera/nxcamera.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <assert.h>
+#include <debug.h>
+#include <dirent.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <nuttx/queue.h>
+#include <nuttx/video/video.h>
+#include <nuttx/video/fb.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <sys/types.h>
+
+#include "system/nxcamera.h"
+#ifdef CONFIG_LIBYUV
+# include "libyuv.h"
+#endif
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define NXCAMERA_STATE_IDLE 0
+#define NXCAMERA_STATE_STREAMING 1
+#define NXCAMERA_STATE_LOOPING 2
+#define NXCAMERA_STATE_PAUSED 3
+
+#define REQBUF_COUNT 3
+
+#ifndef MIN
+# define MIN(a, b) (((a) < (b)) ? (a) : (b))
+#endif
+
+#define convert_frame ConvertToARGB
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int show_image(FAR struct nxcamera_s *pcam, FAR v4l2_buffer_t *buf)
+{
+#ifdef CONFIG_LIBYUV
+ return convert_frame(pcam->bufs[buf->index],
+ pcam->buf_sizes[buf->index],
+ pcam->display_pinfo.fbmem,
+ pcam->display_pinfo.stride,
+ 0,
+ 0,
+ pcam->fmt.fmt.pix.width,
+ pcam->fmt.fmt.pix.height,
+ pcam->fmt.fmt.pix.width,
+ pcam->fmt.fmt.pix.height,
+ 0,
+ pcam->fmt.fmt.pix.pixelformat);
+#else
+ uint32_t *pbuf = pcam->bufs[buf->index];
+ vinfo("show image from %p: %lx %lx", pbuf, pbuf[0], pbuf[1]);
+ return 0;
+#endif
+}
+
+/****************************************************************************
+ * Name: nxcamera_opendevice
+ *
+ * nxcamera_opendevice() tries to open the preferred devices as specified.
+ *
+ * Return:
+ * OK if compatible device opened (searched or preferred)
+ * -ENODEV if no compatible device opened.
+ * -ENOENT if preferred device couldn't be opened.
+ *
+ ****************************************************************************/
+
+static int nxcamera_opendevice(FAR struct nxcamera_s *pcam)
+{
+ int errcode;
+
+#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE
+ if (pcam->capturedev[0] != '\0')
+ {
+#endif
+
+ if (pcam->displaydev[0] != '\0')
+ {
+#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE
+ /* If we have a preferred device, then open it */
+
+ pcam->capture_fd = open(pcam->capturedev, O_RDWR);
+#else
+ pcam->capture_fd = open(CONFIG_VIDEO_DEV_PATH, O_RDWR);
+#endif
+ if (pcam->capture_fd == -1)
+ {
+ errcode = errno;
+ DEBUGASSERT(errcode > 0);
+
+ verr("ERROR: Failed to open pcam->capturedev %d\n", -errcode);
+ return -errcode;
+ }
+
+ pcam->display_fd = open(pcam->displaydev, O_RDWR);
+ if (pcam->display_fd == -1)
+ {
+ errcode = errno;
+ DEBUGASSERT(errcode > 0);
+
+ close(pcam->capture_fd);
+ pcam->capture_fd = -1;
+ verr("ERROR: Failed to open pcam->displaydev %d\n", -errcode);
+ return -errcode;
+ }
+
+ errcode = ioctl(pcam->display_fd, FBIOGET_PLANEINFO,
+ ((uintptr_t)&pcam->display_pinfo));
+
+ if (errcode == OK)
+ {
+ pcam->display_pinfo.fbmem = mmap(NULL, pcam->display_pinfo.fblen,
+ PROT_READ | PROT_WRITE,
+ MAP_SHARED | MAP_FILE,
+ pcam->display_fd, 0);
+ }
+
+ if (errcode < 0 || pcam->display_pinfo.fbmem == MAP_FAILED)
+ {
+ errcode = errno;
+ close(pcam->capture_fd);
+ close(pcam->display_fd);
+ verr("ERROR: ioctl(FBIOGET_PLANEINFO) failed: %d\n", -errcode);
+ return -errcode;
+ }
+
+ return OK;
+ }
+ else
+ {
+ /* TODO: Add file output */
+
+ return -ENOTSUP;
+ }
+
+#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE
+ }
+#endif
+
+ return -ENODEV;
+}
+
+/****************************************************************************
+ * Name: nxcamera_thread_loopthread
+ *
+ * This is the thread that streams the video and handles video controls.
+ *
+ ****************************************************************************/
+
+static void *nxcamera_loopthread(pthread_addr_t pvarg)
+{
+ FAR struct nxcamera_s *pcam = (FAR struct nxcamera_s *)pvarg;
+ unsigned int prio;
+ ssize_t size;
+ struct video_msg_s msg;
+ bool streaming = true;
+ int x;
+ int ret;
+ struct v4l2_buffer buf;
+ uint32_t type;
+
+ vinfo("Entry\n");
+
+ for (x = 0; x < pcam->nbuffers; x++)
+ {
+ buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ buf.memory = V4L2_MEMORY_MMAP;
+ buf.index = x;
+ ret = ioctl(pcam->capture_fd, VIDIOC_QBUF, (uintptr_t)&buf);
+ if (ret < 0)
+ {
+ verr("VIDIOC_QBUF failed: %d\n", -ret);
+ goto err_out;
+ }
+ }
+
+ /* VIDIOC_STREAMON start stream */
+
+ type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ ret = ioctl(pcam->capture_fd, VIDIOC_STREAMON, (uintptr_t)&type);
+ if (ret < 0)
+ {
+ verr("VIDIOC_STREAMON failed: %d\n", -ret);
+ goto err_out;
+ }
+ else
+ {
+ pcam->loopstate = NXCAMERA_STATE_STREAMING;
+ }
+
+ /* Loop until we specifically break. streaming == true means that we are
+ * still looping waiting for the stream to complete. All of the data
+ * may have been sent, but the stream is not complete until we get
+ * VIDEO_MSG_STOP message
+ *
+ * The normal protocol for looping errors detected by the video driver
+ * is as follows:
+ *
+ * (1) The video driver will indicated the error by returning a negated
+ * error value when the next buffer is enqueued. The upper level
+ * then knows that this buffer was not queued.
+ * (2) The video driver must return all queued buffers using the
+ * VIDEO_MSG_DEQUEUE message.
+ */
+
+ while (streaming)
+ {
+ size = mq_receive(pcam->mq, (FAR char *)&msg, sizeof(msg), &prio);
+
+ /* Validate a message was received */
+
+ if (size == sizeof(msg))
+ {
+ /* Perform operation based on message id */
+
+ vinfo("message received size %zd id%d\n", size, msg.msg_id);
+ switch (msg.msg_id)
+ {
+ /* An video buffer is being dequeued by the driver */
+
+ case VIDEO_MSG_DEQUEUE:
+ if (!streaming)
+ {
+ break;
+ }
+
+ if (ret == OK && pcam->loopstate == NXCAMERA_STATE_STREAMING)
+ {
+ if (ret == OK)
+ {
+ pcam->loopstate = NXCAMERA_STATE_LOOPING;
+ }
+ }
+
+ if (ret == OK)
+ {
+ /* Go through to VIDEO_MSG_STOP */
+
+ break;
+ }
+
+ /* Someone wants to stop the stream. */
+
+ case VIDEO_MSG_STOP:
+
+ /* Send a stop message to the device */
+
+ vinfo("Stopping looping\n");
+ ioctl(pcam->capture_fd, VIDIOC_STREAMOFF, 0);
+ streaming = false;
+ break;
+
+ /* Unknown / unsupported message ID */
+
+ default:
+ break;
+ }
+ }
+
+ ret = ioctl(pcam->capture_fd, VIDIOC_DQBUF, (uintptr_t)&buf);
+ if (ret)
+ {
+ verr("Fail DQBUF %d\n", errno);
+ goto err_out;
+ }
+
+ ret = show_image(pcam, &buf);
+ if (ret < 0)
+ {
+ verr("Fail to show image %d\n", -ret);
+ goto err_out;
+ }
+
+ ret = ioctl(pcam->capture_fd, VIDIOC_QBUF, (uintptr_t)&buf);
+ if (ret)
+ {
+ verr("Fail QBUF %d\n", errno);
+ goto err_out;
+ }
+
+ usleep(1);
+ }
+
+ /* Release our video buffers and unregister / release the device */
+
+err_out:
+ vinfo("Clean-up and exit\n");
+
+ /* Cleanup */
+
+ while (sem_wait(&pcam->sem) < 0)
+ {
+ }
+
+ close(pcam->display_fd); /* Close the display device */
+ close(pcam->capture_fd); /* Close the capture device */
+ pcam->display_fd = -1; /* Mark display device as closed */
+ pcam->capture_fd = -1; /* Mark capture device as closed */
+ mq_close(pcam->mq); /* Close the message queue */
+ mq_unlink(pcam->mqname); /* Unlink the message queue */
+ pcam->loopstate = NXCAMERA_STATE_IDLE;
+ free(pcam->bufs);
+ free(pcam->buf_sizes);
+ sem_post(&pcam->sem); /* Release the semaphore */
+
+ vinfo("Exit\n");
+
+ return NULL;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: nxcamera_setdevice
+ *
+ * nxcamera_setdevice() sets the preferred video device to use with the
+ * provided nxcamera context.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE
+int nxcamera_setdevice(FAR struct nxcamera_s *pcam,
+ FAR const char *device)
+{
+ int temp_fd;
+ struct v4l2_capability caps;
+
+ DEBUGASSERT(pcam != NULL);
+ DEBUGASSERT(device != NULL);
+
+ /* Try to open the device */
+
+ temp_fd = open(device, O_RDWR);
+ if (temp_fd == -1)
+ {
+ /* Error opening the device */
+
+ return -ENOENT;
+ }
+
+ /* Validate it's a video device by issuing an VIDIOC_QUERYCAP ioctl */
+
+ if (ioctl(temp_fd, VIDIOC_QUERYCAP, (uintptr_t)&caps) != OK)
+ {
+ /* Not an Video device! */
+
+ close(temp_fd);
+ return -ENODEV;
+ }
+
+ /* Close the file */
+
+ close(temp_fd);
+
+ /* Save the path of the preferred device */
+
+ if (caps.capabilities & V4L2_CAP_VIDEO_CAPTURE)
+ {
+ strlcpy(pcam->capturedev, device, sizeof(pcam->capturedev));
+ }
+
+ return OK;
+}
+#endif /* CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE */
+
+/****************************************************************************
+ * Name: nxcamera_setfb
+ *
+ * nxcamera_setfb() sets the output framebuffer device to use with the
+ * provided nxcamera context.
+ *
+ ****************************************************************************/
+
+int nxcamera_setfb(FAR struct nxcamera_s *pcam, FAR const char *device)
+{
+ int temp_fd;
+ struct fb_videoinfo_s vinfo;
+
+ DEBUGASSERT(pcam != NULL);
+ DEBUGASSERT(device != NULL);
+
+ /* Try to open the device */
+
+ temp_fd = open(device, O_RDWR);
+ if (temp_fd == -1)
+ {
+ /* Error opening the device */
+
+ return -ENOENT;
+ }
+
+ /* Validate it's a fb device by issuing an FBIOGET_VIDEOINFO ioctl */
+
+ if (ioctl(temp_fd, FBIOGET_VIDEOINFO, (uintptr_t)&vinfo) != OK)
+ {
+ /* Not an Video device! */
+
+ close(temp_fd);
+ return -ENODEV;
+ }
+
+ /* Close the file */
+
+ close(temp_fd);
+
+ /* Save the path of the framebuffer device */
+
+ if (vinfo.nplanes > 0)
Review Comment:
should we return -errno in case of vinfo.nplanes == 0
##########
system/nxcamera/nxcamera.c:
##########
@@ -0,0 +1,904 @@
+/****************************************************************************
+ * apps/system/nxcamera/nxcamera.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <assert.h>
+#include <debug.h>
+#include <dirent.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <nuttx/queue.h>
+#include <nuttx/video/video.h>
+#include <nuttx/video/fb.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <sys/types.h>
+
+#include "system/nxcamera.h"
+#ifdef CONFIG_LIBYUV
+# include "libyuv.h"
+#endif
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define NXCAMERA_STATE_IDLE 0
+#define NXCAMERA_STATE_STREAMING 1
+#define NXCAMERA_STATE_LOOPING 2
+#define NXCAMERA_STATE_PAUSED 3
+
+#define REQBUF_COUNT 3
+
+#ifndef MIN
+# define MIN(a, b) (((a) < (b)) ? (a) : (b))
+#endif
+
+#define convert_frame ConvertToARGB
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int show_image(FAR struct nxcamera_s *pcam, FAR v4l2_buffer_t *buf)
+{
+#ifdef CONFIG_LIBYUV
+ return convert_frame(pcam->bufs[buf->index],
+ pcam->buf_sizes[buf->index],
+ pcam->display_pinfo.fbmem,
+ pcam->display_pinfo.stride,
+ 0,
+ 0,
+ pcam->fmt.fmt.pix.width,
+ pcam->fmt.fmt.pix.height,
+ pcam->fmt.fmt.pix.width,
+ pcam->fmt.fmt.pix.height,
+ 0,
+ pcam->fmt.fmt.pix.pixelformat);
+#else
+ uint32_t *pbuf = pcam->bufs[buf->index];
+ vinfo("show image from %p: %lx %lx", pbuf, pbuf[0], pbuf[1]);
+ return 0;
+#endif
+}
+
+/****************************************************************************
+ * Name: nxcamera_opendevice
+ *
+ * nxcamera_opendevice() tries to open the preferred devices as specified.
+ *
+ * Return:
+ * OK if compatible device opened (searched or preferred)
+ * -ENODEV if no compatible device opened.
+ * -ENOENT if preferred device couldn't be opened.
+ *
+ ****************************************************************************/
+
+static int nxcamera_opendevice(FAR struct nxcamera_s *pcam)
+{
+ int errcode;
+
+#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE
+ if (pcam->capturedev[0] != '\0')
+ {
+#endif
+
+ if (pcam->displaydev[0] != '\0')
+ {
+#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE
+ /* If we have a preferred device, then open it */
+
+ pcam->capture_fd = open(pcam->capturedev, O_RDWR);
+#else
+ pcam->capture_fd = open(CONFIG_VIDEO_DEV_PATH, O_RDWR);
+#endif
+ if (pcam->capture_fd == -1)
+ {
+ errcode = errno;
+ DEBUGASSERT(errcode > 0);
+
+ verr("ERROR: Failed to open pcam->capturedev %d\n", -errcode);
+ return -errcode;
+ }
+
+ pcam->display_fd = open(pcam->displaydev, O_RDWR);
+ if (pcam->display_fd == -1)
+ {
+ errcode = errno;
+ DEBUGASSERT(errcode > 0);
+
+ close(pcam->capture_fd);
+ pcam->capture_fd = -1;
+ verr("ERROR: Failed to open pcam->displaydev %d\n", -errcode);
+ return -errcode;
+ }
+
+ errcode = ioctl(pcam->display_fd, FBIOGET_PLANEINFO,
+ ((uintptr_t)&pcam->display_pinfo));
+
+ if (errcode == OK)
+ {
+ pcam->display_pinfo.fbmem = mmap(NULL, pcam->display_pinfo.fblen,
+ PROT_READ | PROT_WRITE,
+ MAP_SHARED | MAP_FILE,
+ pcam->display_fd, 0);
+ }
+
+ if (errcode < 0 || pcam->display_pinfo.fbmem == MAP_FAILED)
+ {
+ errcode = errno;
+ close(pcam->capture_fd);
+ close(pcam->display_fd);
+ verr("ERROR: ioctl(FBIOGET_PLANEINFO) failed: %d\n", -errcode);
+ return -errcode;
+ }
+
+ return OK;
+ }
+ else
+ {
+ /* TODO: Add file output */
+
+ return -ENOTSUP;
+ }
+
+#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE
+ }
+#endif
+
+ return -ENODEV;
+}
+
+/****************************************************************************
+ * Name: nxcamera_thread_loopthread
+ *
+ * This is the thread that streams the video and handles video controls.
+ *
+ ****************************************************************************/
+
+static void *nxcamera_loopthread(pthread_addr_t pvarg)
+{
+ FAR struct nxcamera_s *pcam = (FAR struct nxcamera_s *)pvarg;
+ unsigned int prio;
+ ssize_t size;
+ struct video_msg_s msg;
+ bool streaming = true;
+ int x;
+ int ret;
+ struct v4l2_buffer buf;
+ uint32_t type;
+
+ vinfo("Entry\n");
+
+ for (x = 0; x < pcam->nbuffers; x++)
+ {
+ buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ buf.memory = V4L2_MEMORY_MMAP;
+ buf.index = x;
+ ret = ioctl(pcam->capture_fd, VIDIOC_QBUF, (uintptr_t)&buf);
+ if (ret < 0)
+ {
+ verr("VIDIOC_QBUF failed: %d\n", -ret);
+ goto err_out;
+ }
+ }
+
+ /* VIDIOC_STREAMON start stream */
+
+ type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ ret = ioctl(pcam->capture_fd, VIDIOC_STREAMON, (uintptr_t)&type);
+ if (ret < 0)
+ {
+ verr("VIDIOC_STREAMON failed: %d\n", -ret);
+ goto err_out;
+ }
+ else
+ {
+ pcam->loopstate = NXCAMERA_STATE_STREAMING;
+ }
+
+ /* Loop until we specifically break. streaming == true means that we are
+ * still looping waiting for the stream to complete. All of the data
+ * may have been sent, but the stream is not complete until we get
+ * VIDEO_MSG_STOP message
+ *
+ * The normal protocol for looping errors detected by the video driver
+ * is as follows:
+ *
+ * (1) The video driver will indicated the error by returning a negated
+ * error value when the next buffer is enqueued. The upper level
+ * then knows that this buffer was not queued.
+ * (2) The video driver must return all queued buffers using the
+ * VIDEO_MSG_DEQUEUE message.
+ */
+
+ while (streaming)
+ {
+ size = mq_receive(pcam->mq, (FAR char *)&msg, sizeof(msg), &prio);
+
+ /* Validate a message was received */
+
+ if (size == sizeof(msg))
+ {
+ /* Perform operation based on message id */
+
+ vinfo("message received size %zd id%d\n", size, msg.msg_id);
+ switch (msg.msg_id)
+ {
+ /* An video buffer is being dequeued by the driver */
+
+ case VIDEO_MSG_DEQUEUE:
+ if (!streaming)
+ {
+ break;
+ }
+
+ if (ret == OK && pcam->loopstate == NXCAMERA_STATE_STREAMING)
+ {
+ if (ret == OK)
+ {
+ pcam->loopstate = NXCAMERA_STATE_LOOPING;
+ }
+ }
+
+ if (ret == OK)
+ {
+ /* Go through to VIDEO_MSG_STOP */
+
+ break;
+ }
+
+ /* Someone wants to stop the stream. */
+
+ case VIDEO_MSG_STOP:
+
+ /* Send a stop message to the device */
+
+ vinfo("Stopping looping\n");
+ ioctl(pcam->capture_fd, VIDIOC_STREAMOFF, 0);
+ streaming = false;
+ break;
+
+ /* Unknown / unsupported message ID */
+
+ default:
+ break;
+ }
+ }
+
+ ret = ioctl(pcam->capture_fd, VIDIOC_DQBUF, (uintptr_t)&buf);
+ if (ret)
+ {
+ verr("Fail DQBUF %d\n", errno);
+ goto err_out;
+ }
+
+ ret = show_image(pcam, &buf);
+ if (ret < 0)
+ {
+ verr("Fail to show image %d\n", -ret);
+ goto err_out;
+ }
+
+ ret = ioctl(pcam->capture_fd, VIDIOC_QBUF, (uintptr_t)&buf);
+ if (ret)
+ {
+ verr("Fail QBUF %d\n", errno);
+ goto err_out;
+ }
+
+ usleep(1);
+ }
+
+ /* Release our video buffers and unregister / release the device */
+
+err_out:
+ vinfo("Clean-up and exit\n");
+
+ /* Cleanup */
+
+ while (sem_wait(&pcam->sem) < 0)
+ {
+ }
+
+ close(pcam->display_fd); /* Close the display device */
+ close(pcam->capture_fd); /* Close the capture device */
+ pcam->display_fd = -1; /* Mark display device as closed */
+ pcam->capture_fd = -1; /* Mark capture device as closed */
+ mq_close(pcam->mq); /* Close the message queue */
+ mq_unlink(pcam->mqname); /* Unlink the message queue */
+ pcam->loopstate = NXCAMERA_STATE_IDLE;
+ free(pcam->bufs);
+ free(pcam->buf_sizes);
+ sem_post(&pcam->sem); /* Release the semaphore */
+
+ vinfo("Exit\n");
+
+ return NULL;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: nxcamera_setdevice
+ *
+ * nxcamera_setdevice() sets the preferred video device to use with the
+ * provided nxcamera context.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE
+int nxcamera_setdevice(FAR struct nxcamera_s *pcam,
+ FAR const char *device)
+{
+ int temp_fd;
+ struct v4l2_capability caps;
+
+ DEBUGASSERT(pcam != NULL);
+ DEBUGASSERT(device != NULL);
+
+ /* Try to open the device */
+
+ temp_fd = open(device, O_RDWR);
+ if (temp_fd == -1)
+ {
+ /* Error opening the device */
+
+ return -ENOENT;
+ }
+
+ /* Validate it's a video device by issuing an VIDIOC_QUERYCAP ioctl */
+
+ if (ioctl(temp_fd, VIDIOC_QUERYCAP, (uintptr_t)&caps) != OK)
+ {
+ /* Not an Video device! */
+
+ close(temp_fd);
+ return -ENODEV;
+ }
+
+ /* Close the file */
+
+ close(temp_fd);
+
+ /* Save the path of the preferred device */
+
+ if (caps.capabilities & V4L2_CAP_VIDEO_CAPTURE)
+ {
+ strlcpy(pcam->capturedev, device, sizeof(pcam->capturedev));
+ }
+
+ return OK;
+}
+#endif /* CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE */
+
+/****************************************************************************
+ * Name: nxcamera_setfb
+ *
+ * nxcamera_setfb() sets the output framebuffer device to use with the
+ * provided nxcamera context.
+ *
+ ****************************************************************************/
+
+int nxcamera_setfb(FAR struct nxcamera_s *pcam, FAR const char *device)
+{
+ int temp_fd;
+ struct fb_videoinfo_s vinfo;
+
+ DEBUGASSERT(pcam != NULL);
+ DEBUGASSERT(device != NULL);
+
+ /* Try to open the device */
+
+ temp_fd = open(device, O_RDWR);
+ if (temp_fd == -1)
+ {
+ /* Error opening the device */
+
+ return -ENOENT;
+ }
+
+ /* Validate it's a fb device by issuing an FBIOGET_VIDEOINFO ioctl */
+
+ if (ioctl(temp_fd, FBIOGET_VIDEOINFO, (uintptr_t)&vinfo) != OK)
+ {
+ /* Not an Video device! */
+
+ close(temp_fd);
+ return -ENODEV;
+ }
+
+ /* Close the file */
+
+ close(temp_fd);
+
+ /* Save the path of the framebuffer device */
+
+ if (vinfo.nplanes > 0)
+ {
+ strlcpy(pcam->displaydev, device, sizeof(pcam->displaydev));
+ }
+
+ return OK;
+}
+
+/****************************************************************************
+ * Name: nxcamera_setfile
+ *
+ * nxcamera_setfile() sets the output file path to use with the provided
+ * nxcamera context.
+ *
+ ****************************************************************************/
+
+int nxcamera_setfile(FAR struct nxcamera_s *pcam, FAR const char *pfile,
+ bool isimage)
+{
+ int temp_fd;
+
+ DEBUGASSERT(pcam != NULL);
+ DEBUGASSERT(pfile != NULL);
+
+ /* Try to open the file */
+
+ temp_fd = open(pfile, O_CREAT | O_RDWR);
+ if (temp_fd == -1)
+ {
+ /* Error opening the file */
+
+ return -EACCES;
Review Comment:
-errno
##########
system/nxcamera/nxcamera.c:
##########
@@ -0,0 +1,904 @@
+/****************************************************************************
+ * apps/system/nxcamera/nxcamera.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <assert.h>
+#include <debug.h>
+#include <dirent.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <nuttx/queue.h>
+#include <nuttx/video/video.h>
+#include <nuttx/video/fb.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <sys/types.h>
+
+#include "system/nxcamera.h"
+#ifdef CONFIG_LIBYUV
+# include "libyuv.h"
+#endif
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define NXCAMERA_STATE_IDLE 0
+#define NXCAMERA_STATE_STREAMING 1
+#define NXCAMERA_STATE_LOOPING 2
+#define NXCAMERA_STATE_PAUSED 3
+
+#define REQBUF_COUNT 3
+
+#ifndef MIN
+# define MIN(a, b) (((a) < (b)) ? (a) : (b))
+#endif
+
+#define convert_frame ConvertToARGB
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int show_image(FAR struct nxcamera_s *pcam, FAR v4l2_buffer_t *buf)
+{
+#ifdef CONFIG_LIBYUV
+ return convert_frame(pcam->bufs[buf->index],
+ pcam->buf_sizes[buf->index],
+ pcam->display_pinfo.fbmem,
+ pcam->display_pinfo.stride,
+ 0,
+ 0,
+ pcam->fmt.fmt.pix.width,
+ pcam->fmt.fmt.pix.height,
+ pcam->fmt.fmt.pix.width,
+ pcam->fmt.fmt.pix.height,
+ 0,
+ pcam->fmt.fmt.pix.pixelformat);
+#else
+ uint32_t *pbuf = pcam->bufs[buf->index];
+ vinfo("show image from %p: %lx %lx", pbuf, pbuf[0], pbuf[1]);
+ return 0;
+#endif
+}
+
+/****************************************************************************
+ * Name: nxcamera_opendevice
+ *
+ * nxcamera_opendevice() tries to open the preferred devices as specified.
+ *
+ * Return:
+ * OK if compatible device opened (searched or preferred)
+ * -ENODEV if no compatible device opened.
+ * -ENOENT if preferred device couldn't be opened.
+ *
+ ****************************************************************************/
+
+static int nxcamera_opendevice(FAR struct nxcamera_s *pcam)
+{
+ int errcode;
+
+#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE
+ if (pcam->capturedev[0] != '\0')
+ {
+#endif
+
+ if (pcam->displaydev[0] != '\0')
+ {
+#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE
+ /* If we have a preferred device, then open it */
+
+ pcam->capture_fd = open(pcam->capturedev, O_RDWR);
+#else
+ pcam->capture_fd = open(CONFIG_VIDEO_DEV_PATH, O_RDWR);
+#endif
+ if (pcam->capture_fd == -1)
+ {
+ errcode = errno;
+ DEBUGASSERT(errcode > 0);
+
+ verr("ERROR: Failed to open pcam->capturedev %d\n", -errcode);
+ return -errcode;
+ }
+
+ pcam->display_fd = open(pcam->displaydev, O_RDWR);
+ if (pcam->display_fd == -1)
+ {
+ errcode = errno;
+ DEBUGASSERT(errcode > 0);
+
+ close(pcam->capture_fd);
+ pcam->capture_fd = -1;
+ verr("ERROR: Failed to open pcam->displaydev %d\n", -errcode);
+ return -errcode;
+ }
+
+ errcode = ioctl(pcam->display_fd, FBIOGET_PLANEINFO,
+ ((uintptr_t)&pcam->display_pinfo));
+
+ if (errcode == OK)
+ {
+ pcam->display_pinfo.fbmem = mmap(NULL, pcam->display_pinfo.fblen,
+ PROT_READ | PROT_WRITE,
+ MAP_SHARED | MAP_FILE,
+ pcam->display_fd, 0);
+ }
+
+ if (errcode < 0 || pcam->display_pinfo.fbmem == MAP_FAILED)
+ {
+ errcode = errno;
+ close(pcam->capture_fd);
+ close(pcam->display_fd);
+ verr("ERROR: ioctl(FBIOGET_PLANEINFO) failed: %d\n", -errcode);
+ return -errcode;
+ }
+
+ return OK;
+ }
+ else
+ {
+ /* TODO: Add file output */
+
+ return -ENOTSUP;
+ }
+
+#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE
+ }
+#endif
+
+ return -ENODEV;
+}
+
+/****************************************************************************
+ * Name: nxcamera_thread_loopthread
+ *
+ * This is the thread that streams the video and handles video controls.
+ *
+ ****************************************************************************/
+
+static void *nxcamera_loopthread(pthread_addr_t pvarg)
+{
+ FAR struct nxcamera_s *pcam = (FAR struct nxcamera_s *)pvarg;
+ unsigned int prio;
+ ssize_t size;
+ struct video_msg_s msg;
+ bool streaming = true;
+ int x;
+ int ret;
+ struct v4l2_buffer buf;
+ uint32_t type;
+
+ vinfo("Entry\n");
+
+ for (x = 0; x < pcam->nbuffers; x++)
+ {
+ buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ buf.memory = V4L2_MEMORY_MMAP;
+ buf.index = x;
+ ret = ioctl(pcam->capture_fd, VIDIOC_QBUF, (uintptr_t)&buf);
+ if (ret < 0)
+ {
+ verr("VIDIOC_QBUF failed: %d\n", -ret);
+ goto err_out;
+ }
+ }
+
+ /* VIDIOC_STREAMON start stream */
+
+ type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ ret = ioctl(pcam->capture_fd, VIDIOC_STREAMON, (uintptr_t)&type);
+ if (ret < 0)
+ {
+ verr("VIDIOC_STREAMON failed: %d\n", -ret);
+ goto err_out;
+ }
+ else
+ {
+ pcam->loopstate = NXCAMERA_STATE_STREAMING;
+ }
+
+ /* Loop until we specifically break. streaming == true means that we are
+ * still looping waiting for the stream to complete. All of the data
+ * may have been sent, but the stream is not complete until we get
+ * VIDEO_MSG_STOP message
+ *
+ * The normal protocol for looping errors detected by the video driver
+ * is as follows:
+ *
+ * (1) The video driver will indicated the error by returning a negated
+ * error value when the next buffer is enqueued. The upper level
+ * then knows that this buffer was not queued.
+ * (2) The video driver must return all queued buffers using the
+ * VIDEO_MSG_DEQUEUE message.
+ */
+
+ while (streaming)
+ {
+ size = mq_receive(pcam->mq, (FAR char *)&msg, sizeof(msg), &prio);
+
+ /* Validate a message was received */
+
+ if (size == sizeof(msg))
+ {
+ /* Perform operation based on message id */
+
+ vinfo("message received size %zd id%d\n", size, msg.msg_id);
+ switch (msg.msg_id)
+ {
+ /* An video buffer is being dequeued by the driver */
+
+ case VIDEO_MSG_DEQUEUE:
+ if (!streaming)
+ {
+ break;
+ }
+
+ if (ret == OK && pcam->loopstate == NXCAMERA_STATE_STREAMING)
+ {
+ if (ret == OK)
+ {
+ pcam->loopstate = NXCAMERA_STATE_LOOPING;
+ }
+ }
+
+ if (ret == OK)
+ {
+ /* Go through to VIDEO_MSG_STOP */
+
+ break;
+ }
+
+ /* Someone wants to stop the stream. */
+
+ case VIDEO_MSG_STOP:
+
+ /* Send a stop message to the device */
+
+ vinfo("Stopping looping\n");
+ ioctl(pcam->capture_fd, VIDIOC_STREAMOFF, 0);
+ streaming = false;
+ break;
+
+ /* Unknown / unsupported message ID */
+
+ default:
+ break;
+ }
+ }
+
+ ret = ioctl(pcam->capture_fd, VIDIOC_DQBUF, (uintptr_t)&buf);
+ if (ret)
+ {
+ verr("Fail DQBUF %d\n", errno);
+ goto err_out;
+ }
+
+ ret = show_image(pcam, &buf);
+ if (ret < 0)
+ {
+ verr("Fail to show image %d\n", -ret);
+ goto err_out;
+ }
+
+ ret = ioctl(pcam->capture_fd, VIDIOC_QBUF, (uintptr_t)&buf);
+ if (ret)
+ {
+ verr("Fail QBUF %d\n", errno);
+ goto err_out;
+ }
+
+ usleep(1);
+ }
+
+ /* Release our video buffers and unregister / release the device */
+
+err_out:
+ vinfo("Clean-up and exit\n");
+
+ /* Cleanup */
+
+ while (sem_wait(&pcam->sem) < 0)
+ {
+ }
+
+ close(pcam->display_fd); /* Close the display device */
+ close(pcam->capture_fd); /* Close the capture device */
+ pcam->display_fd = -1; /* Mark display device as closed */
+ pcam->capture_fd = -1; /* Mark capture device as closed */
+ mq_close(pcam->mq); /* Close the message queue */
+ mq_unlink(pcam->mqname); /* Unlink the message queue */
+ pcam->loopstate = NXCAMERA_STATE_IDLE;
+ free(pcam->bufs);
+ free(pcam->buf_sizes);
+ sem_post(&pcam->sem); /* Release the semaphore */
+
+ vinfo("Exit\n");
+
+ return NULL;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: nxcamera_setdevice
+ *
+ * nxcamera_setdevice() sets the preferred video device to use with the
+ * provided nxcamera context.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE
+int nxcamera_setdevice(FAR struct nxcamera_s *pcam,
+ FAR const char *device)
+{
+ int temp_fd;
+ struct v4l2_capability caps;
+
+ DEBUGASSERT(pcam != NULL);
+ DEBUGASSERT(device != NULL);
+
+ /* Try to open the device */
+
+ temp_fd = open(device, O_RDWR);
+ if (temp_fd == -1)
+ {
+ /* Error opening the device */
+
+ return -ENOENT;
+ }
+
+ /* Validate it's a video device by issuing an VIDIOC_QUERYCAP ioctl */
+
+ if (ioctl(temp_fd, VIDIOC_QUERYCAP, (uintptr_t)&caps) != OK)
+ {
+ /* Not an Video device! */
+
+ close(temp_fd);
+ return -ENODEV;
+ }
+
+ /* Close the file */
+
+ close(temp_fd);
+
+ /* Save the path of the preferred device */
+
+ if (caps.capabilities & V4L2_CAP_VIDEO_CAPTURE)
+ {
+ strlcpy(pcam->capturedev, device, sizeof(pcam->capturedev));
+ }
+
+ return OK;
+}
+#endif /* CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE */
+
+/****************************************************************************
+ * Name: nxcamera_setfb
+ *
+ * nxcamera_setfb() sets the output framebuffer device to use with the
+ * provided nxcamera context.
+ *
+ ****************************************************************************/
+
+int nxcamera_setfb(FAR struct nxcamera_s *pcam, FAR const char *device)
+{
+ int temp_fd;
+ struct fb_videoinfo_s vinfo;
+
+ DEBUGASSERT(pcam != NULL);
+ DEBUGASSERT(device != NULL);
+
+ /* Try to open the device */
+
+ temp_fd = open(device, O_RDWR);
+ if (temp_fd == -1)
+ {
+ /* Error opening the device */
+
+ return -ENOENT;
+ }
+
+ /* Validate it's a fb device by issuing an FBIOGET_VIDEOINFO ioctl */
+
+ if (ioctl(temp_fd, FBIOGET_VIDEOINFO, (uintptr_t)&vinfo) != OK)
+ {
+ /* Not an Video device! */
+
+ close(temp_fd);
+ return -ENODEV;
+ }
+
+ /* Close the file */
+
+ close(temp_fd);
+
+ /* Save the path of the framebuffer device */
+
+ if (vinfo.nplanes > 0)
+ {
+ strlcpy(pcam->displaydev, device, sizeof(pcam->displaydev));
+ }
+
+ return OK;
+}
+
+/****************************************************************************
+ * Name: nxcamera_setfile
+ *
+ * nxcamera_setfile() sets the output file path to use with the provided
+ * nxcamera context.
+ *
+ ****************************************************************************/
+
+int nxcamera_setfile(FAR struct nxcamera_s *pcam, FAR const char *pfile,
+ bool isimage)
+{
+ int temp_fd;
+
+ DEBUGASSERT(pcam != NULL);
+ DEBUGASSERT(pfile != NULL);
+
+ /* Try to open the file */
+
+ temp_fd = open(pfile, O_CREAT | O_RDWR);
+ if (temp_fd == -1)
+ {
+ /* Error opening the file */
+
+ return -EACCES;
+ }
+
+ /* Close the file */
+
+ close(temp_fd);
+
+ /* Save the path of the output file */
+
+ if (isimage)
+ {
+ strlcpy(pcam->oimagepath, pfile, sizeof(pcam->oimagepath));
+ }
+ else
+ {
+ strlcpy(pcam->ovideopath, pfile, sizeof(pcam->ovideopath));
+ }
+
+ return OK;
+}
+
+/****************************************************************************
+ * Name: nxcamera_stop
+ *
+ * nxcamera_stop() stops the current playback to loop and closes the
+ * file and the associated device.
+ *
+ * Input:
+ * pcam Pointer to the initialized looper context
+ *
+ ****************************************************************************/
+
+int nxcamera_stop(FAR struct nxcamera_s *pcam)
+{
+ struct video_msg_s term_msg;
+ FAR void *value;
+
+ DEBUGASSERT(pcam != NULL);
+
+ /* Validate we are not in IDLE state */
+
+ sem_wait(&pcam->sem); /* Get the semaphore */
+ if (pcam->loopstate == NXCAMERA_STATE_IDLE)
+ {
+ sem_post(&pcam->sem); /* Release the semaphore */
+ return OK;
+ }
+
+ sem_post(&pcam->sem);
+
+ /* Notify the stream thread that it needs to cancel the stream */
+
+ term_msg.msg_id = VIDEO_MSG_STOP;
+ term_msg.u.data = 0;
+ mq_send(pcam->mq, (FAR const char *)&term_msg, sizeof(term_msg),
+ CONFIG_NXCAMERA_MSG_PRIO);
+
+ /* Join the thread. The thread will do all the cleanup. */
+
+ pthread_join(pcam->loop_id, &value);
+ pcam->loop_id = 0;
+
+ return OK;
+}
+
+/****************************************************************************
+ * Name: nxcamera_stream
+ *
+ * nxcamera_stream() tries to capture and then display the raw data using
+ * the Video system. If a capture device is specified, it will try to use
+ * that device.
+ *
+ * Input:
+ * pcam Pointer to the initialized Looper context
+ * width Capture frame width
+ * height Capture frame height
+ * framerate Capture frame rate
+ * format Capture frame pixel format
+ *
+ * Returns:
+ * OK Video is being looped
+ * -EBUSY Capture device is busy
+ * -ENOSYS No supported video format found
+ * -ENODEV No video capture or framebuffer device suitable
+ *
+ ****************************************************************************/
+
+int nxcamera_stream(FAR struct nxcamera_s *pcam,
+ uint16_t width, uint16_t height,
+ uint32_t framerate, uint32_t format)
+{
+ struct mq_attr attr;
+ struct sched_param sparam;
+ pthread_attr_t tattr;
+ FAR void *value;
+ int ret;
+ int i;
+ struct v4l2_buffer buf;
+ struct v4l2_requestbuffers req;
+ struct v4l2_streamparm parm;
+
+ DEBUGASSERT(pcam != NULL);
+
+ if (pcam->loopstate != NXCAMERA_STATE_IDLE)
+ {
+ return -EBUSY;
+ }
+
+ vinfo("==============================\n");
+ vinfo("streaming video\n");
+ vinfo("==============================\n");
+
+ /* Try to open the device */
+
+ ret = nxcamera_opendevice(pcam);
+ if (ret < 0)
+ {
+ /* Error opening the device */
+
+ verr("ERROR: nxcamera_opendevice failed: %d\n", ret);
+ return ret;
+ }
+
+ /* VIDIOC_S_FMT set format */
+
+ pcam->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ pcam->fmt.fmt.pix.width = width;
+ pcam->fmt.fmt.pix.height = height;
+ pcam->fmt.fmt.pix.field = V4L2_FIELD_ANY;
+ pcam->fmt.fmt.pix.pixelformat = format;
+
+ ret = ioctl(pcam->capture_fd, VIDIOC_S_FMT, (uintptr_t)&pcam->fmt);
+ if (ret < 0)
+ {
+ verr("VIDIOC_S_FMT failed: %d\n", -ret);
+ return ret;
+ }
+
+ memset(&parm, 0, sizeof(parm));
+ parm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ parm.parm.capture.timeperframe.denominator = framerate;
+ parm.parm.capture.timeperframe.numerator = 1;
+ ret = ioctl(pcam->capture_fd, VIDIOC_S_PARM, (uintptr_t)&parm);
+ if (ret < 0)
+ {
+ verr("VIDIOC_S_PARM failed: %d\n", -ret);
+ return ret;
+ }
+
+ /* VIDIOC_REQBUFS initiate user pointer I/O */
+
+ req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ req.memory = V4L2_MEMORY_MMAP;
+ req.count = REQBUF_COUNT;
Review Comment:
should we use Kconfig value here
##########
system/nxcamera/nxcamera.c:
##########
@@ -0,0 +1,904 @@
+/****************************************************************************
+ * apps/system/nxcamera/nxcamera.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <assert.h>
+#include <debug.h>
+#include <dirent.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <nuttx/queue.h>
+#include <nuttx/video/video.h>
+#include <nuttx/video/fb.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <sys/types.h>
+
+#include "system/nxcamera.h"
+#ifdef CONFIG_LIBYUV
+# include "libyuv.h"
+#endif
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define NXCAMERA_STATE_IDLE 0
+#define NXCAMERA_STATE_STREAMING 1
+#define NXCAMERA_STATE_LOOPING 2
+#define NXCAMERA_STATE_PAUSED 3
+
+#define REQBUF_COUNT 3
+
+#ifndef MIN
+# define MIN(a, b) (((a) < (b)) ? (a) : (b))
+#endif
+
+#define convert_frame ConvertToARGB
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int show_image(FAR struct nxcamera_s *pcam, FAR v4l2_buffer_t *buf)
+{
+#ifdef CONFIG_LIBYUV
+ return convert_frame(pcam->bufs[buf->index],
+ pcam->buf_sizes[buf->index],
+ pcam->display_pinfo.fbmem,
+ pcam->display_pinfo.stride,
+ 0,
+ 0,
+ pcam->fmt.fmt.pix.width,
+ pcam->fmt.fmt.pix.height,
+ pcam->fmt.fmt.pix.width,
+ pcam->fmt.fmt.pix.height,
+ 0,
+ pcam->fmt.fmt.pix.pixelformat);
+#else
+ uint32_t *pbuf = pcam->bufs[buf->index];
+ vinfo("show image from %p: %lx %lx", pbuf, pbuf[0], pbuf[1]);
+ return 0;
+#endif
+}
+
+/****************************************************************************
+ * Name: nxcamera_opendevice
+ *
+ * nxcamera_opendevice() tries to open the preferred devices as specified.
+ *
+ * Return:
+ * OK if compatible device opened (searched or preferred)
+ * -ENODEV if no compatible device opened.
+ * -ENOENT if preferred device couldn't be opened.
+ *
+ ****************************************************************************/
+
+static int nxcamera_opendevice(FAR struct nxcamera_s *pcam)
+{
+ int errcode;
+
+#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE
+ if (pcam->capturedev[0] != '\0')
+ {
+#endif
+
+ if (pcam->displaydev[0] != '\0')
+ {
+#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE
+ /* If we have a preferred device, then open it */
+
+ pcam->capture_fd = open(pcam->capturedev, O_RDWR);
+#else
+ pcam->capture_fd = open(CONFIG_VIDEO_DEV_PATH, O_RDWR);
+#endif
+ if (pcam->capture_fd == -1)
+ {
+ errcode = errno;
+ DEBUGASSERT(errcode > 0);
+
+ verr("ERROR: Failed to open pcam->capturedev %d\n", -errcode);
+ return -errcode;
+ }
+
+ pcam->display_fd = open(pcam->displaydev, O_RDWR);
+ if (pcam->display_fd == -1)
+ {
+ errcode = errno;
+ DEBUGASSERT(errcode > 0);
+
+ close(pcam->capture_fd);
+ pcam->capture_fd = -1;
+ verr("ERROR: Failed to open pcam->displaydev %d\n", -errcode);
+ return -errcode;
+ }
+
+ errcode = ioctl(pcam->display_fd, FBIOGET_PLANEINFO,
+ ((uintptr_t)&pcam->display_pinfo));
+
+ if (errcode == OK)
+ {
+ pcam->display_pinfo.fbmem = mmap(NULL, pcam->display_pinfo.fblen,
+ PROT_READ | PROT_WRITE,
+ MAP_SHARED | MAP_FILE,
+ pcam->display_fd, 0);
+ }
+
+ if (errcode < 0 || pcam->display_pinfo.fbmem == MAP_FAILED)
+ {
+ errcode = errno;
+ close(pcam->capture_fd);
+ close(pcam->display_fd);
+ verr("ERROR: ioctl(FBIOGET_PLANEINFO) failed: %d\n", -errcode);
+ return -errcode;
+ }
+
+ return OK;
+ }
+ else
+ {
+ /* TODO: Add file output */
+
+ return -ENOTSUP;
+ }
+
+#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE
+ }
+#endif
+
+ return -ENODEV;
+}
+
+/****************************************************************************
+ * Name: nxcamera_thread_loopthread
+ *
+ * This is the thread that streams the video and handles video controls.
+ *
+ ****************************************************************************/
+
+static void *nxcamera_loopthread(pthread_addr_t pvarg)
+{
+ FAR struct nxcamera_s *pcam = (FAR struct nxcamera_s *)pvarg;
+ unsigned int prio;
+ ssize_t size;
+ struct video_msg_s msg;
+ bool streaming = true;
+ int x;
+ int ret;
+ struct v4l2_buffer buf;
+ uint32_t type;
+
+ vinfo("Entry\n");
+
+ for (x = 0; x < pcam->nbuffers; x++)
+ {
+ buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ buf.memory = V4L2_MEMORY_MMAP;
+ buf.index = x;
+ ret = ioctl(pcam->capture_fd, VIDIOC_QBUF, (uintptr_t)&buf);
+ if (ret < 0)
+ {
+ verr("VIDIOC_QBUF failed: %d\n", -ret);
+ goto err_out;
+ }
+ }
+
+ /* VIDIOC_STREAMON start stream */
+
+ type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ ret = ioctl(pcam->capture_fd, VIDIOC_STREAMON, (uintptr_t)&type);
+ if (ret < 0)
+ {
+ verr("VIDIOC_STREAMON failed: %d\n", -ret);
+ goto err_out;
+ }
+ else
+ {
+ pcam->loopstate = NXCAMERA_STATE_STREAMING;
+ }
+
+ /* Loop until we specifically break. streaming == true means that we are
+ * still looping waiting for the stream to complete. All of the data
+ * may have been sent, but the stream is not complete until we get
+ * VIDEO_MSG_STOP message
+ *
+ * The normal protocol for looping errors detected by the video driver
+ * is as follows:
+ *
+ * (1) The video driver will indicated the error by returning a negated
+ * error value when the next buffer is enqueued. The upper level
+ * then knows that this buffer was not queued.
+ * (2) The video driver must return all queued buffers using the
+ * VIDEO_MSG_DEQUEUE message.
+ */
+
+ while (streaming)
+ {
+ size = mq_receive(pcam->mq, (FAR char *)&msg, sizeof(msg), &prio);
+
+ /* Validate a message was received */
+
+ if (size == sizeof(msg))
+ {
+ /* Perform operation based on message id */
+
+ vinfo("message received size %zd id%d\n", size, msg.msg_id);
+ switch (msg.msg_id)
+ {
+ /* An video buffer is being dequeued by the driver */
+
+ case VIDEO_MSG_DEQUEUE:
+ if (!streaming)
+ {
+ break;
+ }
+
+ if (ret == OK && pcam->loopstate == NXCAMERA_STATE_STREAMING)
+ {
+ if (ret == OK)
+ {
+ pcam->loopstate = NXCAMERA_STATE_LOOPING;
+ }
+ }
+
+ if (ret == OK)
+ {
+ /* Go through to VIDEO_MSG_STOP */
+
+ break;
+ }
+
+ /* Someone wants to stop the stream. */
+
+ case VIDEO_MSG_STOP:
+
+ /* Send a stop message to the device */
+
+ vinfo("Stopping looping\n");
+ ioctl(pcam->capture_fd, VIDIOC_STREAMOFF, 0);
+ streaming = false;
+ break;
+
+ /* Unknown / unsupported message ID */
+
+ default:
+ break;
+ }
+ }
+
+ ret = ioctl(pcam->capture_fd, VIDIOC_DQBUF, (uintptr_t)&buf);
+ if (ret)
+ {
+ verr("Fail DQBUF %d\n", errno);
+ goto err_out;
+ }
+
+ ret = show_image(pcam, &buf);
+ if (ret < 0)
+ {
+ verr("Fail to show image %d\n", -ret);
+ goto err_out;
+ }
+
+ ret = ioctl(pcam->capture_fd, VIDIOC_QBUF, (uintptr_t)&buf);
+ if (ret)
+ {
+ verr("Fail QBUF %d\n", errno);
+ goto err_out;
+ }
+
+ usleep(1);
+ }
+
+ /* Release our video buffers and unregister / release the device */
+
+err_out:
+ vinfo("Clean-up and exit\n");
+
+ /* Cleanup */
+
+ while (sem_wait(&pcam->sem) < 0)
+ {
+ }
+
+ close(pcam->display_fd); /* Close the display device */
+ close(pcam->capture_fd); /* Close the capture device */
+ pcam->display_fd = -1; /* Mark display device as closed */
+ pcam->capture_fd = -1; /* Mark capture device as closed */
+ mq_close(pcam->mq); /* Close the message queue */
+ mq_unlink(pcam->mqname); /* Unlink the message queue */
+ pcam->loopstate = NXCAMERA_STATE_IDLE;
+ free(pcam->bufs);
+ free(pcam->buf_sizes);
+ sem_post(&pcam->sem); /* Release the semaphore */
+
+ vinfo("Exit\n");
+
+ return NULL;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: nxcamera_setdevice
+ *
+ * nxcamera_setdevice() sets the preferred video device to use with the
+ * provided nxcamera context.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE
+int nxcamera_setdevice(FAR struct nxcamera_s *pcam,
+ FAR const char *device)
+{
+ int temp_fd;
+ struct v4l2_capability caps;
+
+ DEBUGASSERT(pcam != NULL);
+ DEBUGASSERT(device != NULL);
+
+ /* Try to open the device */
+
+ temp_fd = open(device, O_RDWR);
+ if (temp_fd == -1)
+ {
+ /* Error opening the device */
+
+ return -ENOENT;
+ }
+
+ /* Validate it's a video device by issuing an VIDIOC_QUERYCAP ioctl */
+
+ if (ioctl(temp_fd, VIDIOC_QUERYCAP, (uintptr_t)&caps) != OK)
+ {
+ /* Not an Video device! */
+
+ close(temp_fd);
+ return -ENODEV;
+ }
+
+ /* Close the file */
+
+ close(temp_fd);
+
+ /* Save the path of the preferred device */
+
+ if (caps.capabilities & V4L2_CAP_VIDEO_CAPTURE)
+ {
+ strlcpy(pcam->capturedev, device, sizeof(pcam->capturedev));
+ }
+
+ return OK;
+}
+#endif /* CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE */
+
+/****************************************************************************
+ * Name: nxcamera_setfb
+ *
+ * nxcamera_setfb() sets the output framebuffer device to use with the
+ * provided nxcamera context.
+ *
+ ****************************************************************************/
+
+int nxcamera_setfb(FAR struct nxcamera_s *pcam, FAR const char *device)
+{
+ int temp_fd;
+ struct fb_videoinfo_s vinfo;
+
+ DEBUGASSERT(pcam != NULL);
+ DEBUGASSERT(device != NULL);
+
+ /* Try to open the device */
+
+ temp_fd = open(device, O_RDWR);
+ if (temp_fd == -1)
+ {
+ /* Error opening the device */
+
+ return -ENOENT;
+ }
+
+ /* Validate it's a fb device by issuing an FBIOGET_VIDEOINFO ioctl */
+
+ if (ioctl(temp_fd, FBIOGET_VIDEOINFO, (uintptr_t)&vinfo) != OK)
+ {
+ /* Not an Video device! */
+
+ close(temp_fd);
+ return -ENODEV;
+ }
+
+ /* Close the file */
+
+ close(temp_fd);
+
+ /* Save the path of the framebuffer device */
+
+ if (vinfo.nplanes > 0)
+ {
+ strlcpy(pcam->displaydev, device, sizeof(pcam->displaydev));
+ }
+
+ return OK;
+}
+
+/****************************************************************************
+ * Name: nxcamera_setfile
+ *
+ * nxcamera_setfile() sets the output file path to use with the provided
+ * nxcamera context.
+ *
+ ****************************************************************************/
+
+int nxcamera_setfile(FAR struct nxcamera_s *pcam, FAR const char *pfile,
+ bool isimage)
+{
+ int temp_fd;
+
+ DEBUGASSERT(pcam != NULL);
+ DEBUGASSERT(pfile != NULL);
+
+ /* Try to open the file */
+
+ temp_fd = open(pfile, O_CREAT | O_RDWR);
+ if (temp_fd == -1)
+ {
+ /* Error opening the file */
+
+ return -EACCES;
+ }
+
+ /* Close the file */
+
+ close(temp_fd);
+
+ /* Save the path of the output file */
+
+ if (isimage)
+ {
+ strlcpy(pcam->oimagepath, pfile, sizeof(pcam->oimagepath));
+ }
+ else
+ {
+ strlcpy(pcam->ovideopath, pfile, sizeof(pcam->ovideopath));
+ }
+
+ return OK;
+}
+
+/****************************************************************************
+ * Name: nxcamera_stop
+ *
+ * nxcamera_stop() stops the current playback to loop and closes the
+ * file and the associated device.
+ *
+ * Input:
+ * pcam Pointer to the initialized looper context
+ *
+ ****************************************************************************/
+
+int nxcamera_stop(FAR struct nxcamera_s *pcam)
+{
+ struct video_msg_s term_msg;
+ FAR void *value;
+
+ DEBUGASSERT(pcam != NULL);
+
+ /* Validate we are not in IDLE state */
+
+ sem_wait(&pcam->sem); /* Get the semaphore */
+ if (pcam->loopstate == NXCAMERA_STATE_IDLE)
+ {
+ sem_post(&pcam->sem); /* Release the semaphore */
+ return OK;
+ }
+
+ sem_post(&pcam->sem);
+
+ /* Notify the stream thread that it needs to cancel the stream */
+
+ term_msg.msg_id = VIDEO_MSG_STOP;
+ term_msg.u.data = 0;
+ mq_send(pcam->mq, (FAR const char *)&term_msg, sizeof(term_msg),
+ CONFIG_NXCAMERA_MSG_PRIO);
+
+ /* Join the thread. The thread will do all the cleanup. */
+
+ pthread_join(pcam->loop_id, &value);
+ pcam->loop_id = 0;
+
+ return OK;
+}
+
+/****************************************************************************
+ * Name: nxcamera_stream
+ *
+ * nxcamera_stream() tries to capture and then display the raw data using
+ * the Video system. If a capture device is specified, it will try to use
+ * that device.
+ *
+ * Input:
+ * pcam Pointer to the initialized Looper context
+ * width Capture frame width
+ * height Capture frame height
+ * framerate Capture frame rate
+ * format Capture frame pixel format
+ *
+ * Returns:
+ * OK Video is being looped
+ * -EBUSY Capture device is busy
+ * -ENOSYS No supported video format found
+ * -ENODEV No video capture or framebuffer device suitable
+ *
+ ****************************************************************************/
+
+int nxcamera_stream(FAR struct nxcamera_s *pcam,
+ uint16_t width, uint16_t height,
+ uint32_t framerate, uint32_t format)
+{
+ struct mq_attr attr;
+ struct sched_param sparam;
+ pthread_attr_t tattr;
+ FAR void *value;
+ int ret;
+ int i;
+ struct v4l2_buffer buf;
+ struct v4l2_requestbuffers req;
+ struct v4l2_streamparm parm;
+
+ DEBUGASSERT(pcam != NULL);
+
+ if (pcam->loopstate != NXCAMERA_STATE_IDLE)
+ {
+ return -EBUSY;
+ }
+
+ vinfo("==============================\n");
+ vinfo("streaming video\n");
+ vinfo("==============================\n");
+
+ /* Try to open the device */
+
+ ret = nxcamera_opendevice(pcam);
+ if (ret < 0)
+ {
+ /* Error opening the device */
+
+ verr("ERROR: nxcamera_opendevice failed: %d\n", ret);
+ return ret;
+ }
+
+ /* VIDIOC_S_FMT set format */
+
+ pcam->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ pcam->fmt.fmt.pix.width = width;
+ pcam->fmt.fmt.pix.height = height;
+ pcam->fmt.fmt.pix.field = V4L2_FIELD_ANY;
+ pcam->fmt.fmt.pix.pixelformat = format;
+
+ ret = ioctl(pcam->capture_fd, VIDIOC_S_FMT, (uintptr_t)&pcam->fmt);
+ if (ret < 0)
+ {
+ verr("VIDIOC_S_FMT failed: %d\n", -ret);
+ return ret;
+ }
+
+ memset(&parm, 0, sizeof(parm));
+ parm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ parm.parm.capture.timeperframe.denominator = framerate;
+ parm.parm.capture.timeperframe.numerator = 1;
+ ret = ioctl(pcam->capture_fd, VIDIOC_S_PARM, (uintptr_t)&parm);
+ if (ret < 0)
+ {
+ verr("VIDIOC_S_PARM failed: %d\n", -ret);
+ return ret;
+ }
+
+ /* VIDIOC_REQBUFS initiate user pointer I/O */
+
+ req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ req.memory = V4L2_MEMORY_MMAP;
+ req.count = REQBUF_COUNT;
+
+ ret = ioctl(pcam->capture_fd, VIDIOC_REQBUFS, (uintptr_t)&req);
+ if (ret < 0)
+ {
+ verr("VIDIOC_REQBUFS failed: %d\n", -ret);
+ return ret;
+ }
+
+ if (req.count < 2)
+ {
+ verr("VIDIOC_REQBUFS failed: not enough buffers\n");
+ return -ENOMEM;
+ }
+
+ pcam->nbuffers = req.count;
+ pcam->bufs = calloc(req.count, sizeof(*pcam->bufs));
+ pcam->buf_sizes = calloc(req.count, sizeof(*pcam->buf_sizes));
+ if (!pcam->bufs)
+ {
+ verr("Cannot allocate buffer pointers\n");
+ return -ENOMEM;
+ }
+
+ /* VIDIOC_QBUF enqueue buffer */
+
+ for (i = 0; i < req.count; i++)
+ {
+ buf.type = req.type;
+ buf.memory = V4L2_MEMORY_MMAP;
+ buf.index = i;
+ ret = ioctl(pcam->capture_fd, VIDIOC_QUERYBUF, (uintptr_t)&buf);
+ if (ret < 0)
+ {
+ verr("VIDIOC_QUERYBUF failed: %d\n", -ret);
+ return ret;
Review Comment:
how to free buffer
##########
system/nxcamera/nxcamera_main.c:
##########
@@ -0,0 +1,564 @@
+/****************************************************************************
+ * apps/system/nxcamera/nxcamera_main.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/video/video.h>
+
+#include <sys/types.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <errno.h>
+#include <assert.h>
+
+#include "system/readline.h"
+#include "system/nxcamera.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define NXCAMERA_VER "1.00"
+
+#ifdef CONFIG_NXCAMERA_INCLUDE_HELP
+# define NXCAMERA_HELP_TEXT(x) x
+#else
+# define NXCAMERA_HELP_TEXT(x)
+#endif
+
+/****************************************************************************
+ * Private Type Declarations
+ ****************************************************************************/
+
+typedef CODE int (*nxcamera_func)(FAR struct nxcamera_s *cam, FAR char *arg);
+
+struct mp_cmd_s
+{
+ FAR const char *cmd; /* The command text */
+ FAR const char *arghelp; /* Text describing the args */
+ nxcamera_func pfunc; /* Pointer to command handler */
+ FAR const char *help; /* The help text */
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int nxcamera_cmd_quit(FAR struct nxcamera_s *pcam, FAR char *parg);
+static int nxcamera_cmd_stream(FAR struct nxcamera_s *pcam, FAR char *parg);
+
+#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE
+static int nxcamera_cmd_input(FAR struct nxcamera_s *pcam, FAR char *parg);
+#endif
+
+static int nxcamera_cmd_output(FAR struct nxcamera_s *pcam, FAR char *parg);
+static int nxcamera_cmd_stop(FAR struct nxcamera_s *pcam, FAR char *parg);
+static int nxcamera_cmd_set(FAR struct nxcamera_s *pcam, FAR char *parg);
+
+#ifdef CONFIG_NXCAMERA_INCLUDE_HELP
+static int nxcamera_cmd_help(FAR struct nxcamera_s *pcam, FAR char *parg);
+#endif
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct mp_cmd_s g_nxcamera_cmds[] =
+{
+#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE
+ {
+ "input",
+ "videodev",
+ nxcamera_cmd_input,
+ NXCAMERA_HELP_TEXT("Specify a preferred capture device")
+ },
+#endif
+ {
+ "output",
+ "fbdev|filename",
+ nxcamera_cmd_output,
+ NXCAMERA_HELP_TEXT("Specify a display device or filename")
+ },
+#ifdef CONFIG_NXCAMERA_INCLUDE_HELP
+ {
+ "h",
+ "",
+ nxcamera_cmd_help,
+ NXCAMERA_HELP_TEXT("Display help for commands")
+ },
+ {
+ "help",
+ "",
+ nxcamera_cmd_help,
+ NXCAMERA_HELP_TEXT("Display help for commands")
+ },
+#endif
+ {
+ "stream",
+ "width height framerate format",
+ nxcamera_cmd_stream,
+ NXCAMERA_HELP_TEXT("Video stream test (format is fourcc)")
+ },
+ {
+ "stop",
+ "",
+ nxcamera_cmd_stop,
+ NXCAMERA_HELP_TEXT("Stop stream")
+ },
+ {
+ "q",
+ "",
+ nxcamera_cmd_quit,
+ NXCAMERA_HELP_TEXT("Exit NxCamera")
+ },
+ {
+ "quit",
+ "",
+ nxcamera_cmd_quit,
+ NXCAMERA_HELP_TEXT("Exit NxCamera")
+ },
+ {
+ "set",
+ "parameter d%",
+ nxcamera_cmd_set,
+ NXCAMERA_HELP_TEXT("Set camera control parameters")
+ }
+};
+
+static const char *controls[] =
Review Comment:
```suggestion
static const char *g_controls[] =
```
##########
system/nxcamera/nxcamera.c:
##########
@@ -0,0 +1,904 @@
+/****************************************************************************
+ * apps/system/nxcamera/nxcamera.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <assert.h>
+#include <debug.h>
+#include <dirent.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <nuttx/queue.h>
+#include <nuttx/video/video.h>
+#include <nuttx/video/fb.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <sys/types.h>
+
+#include "system/nxcamera.h"
+#ifdef CONFIG_LIBYUV
+# include "libyuv.h"
+#endif
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define NXCAMERA_STATE_IDLE 0
+#define NXCAMERA_STATE_STREAMING 1
+#define NXCAMERA_STATE_LOOPING 2
+#define NXCAMERA_STATE_PAUSED 3
+
+#define REQBUF_COUNT 3
+
+#ifndef MIN
+# define MIN(a, b) (((a) < (b)) ? (a) : (b))
+#endif
+
+#define convert_frame ConvertToARGB
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int show_image(FAR struct nxcamera_s *pcam, FAR v4l2_buffer_t *buf)
+{
+#ifdef CONFIG_LIBYUV
+ return convert_frame(pcam->bufs[buf->index],
+ pcam->buf_sizes[buf->index],
+ pcam->display_pinfo.fbmem,
+ pcam->display_pinfo.stride,
+ 0,
+ 0,
+ pcam->fmt.fmt.pix.width,
+ pcam->fmt.fmt.pix.height,
+ pcam->fmt.fmt.pix.width,
+ pcam->fmt.fmt.pix.height,
+ 0,
+ pcam->fmt.fmt.pix.pixelformat);
+#else
+ uint32_t *pbuf = pcam->bufs[buf->index];
+ vinfo("show image from %p: %lx %lx", pbuf, pbuf[0], pbuf[1]);
+ return 0;
+#endif
+}
+
+/****************************************************************************
+ * Name: nxcamera_opendevice
+ *
+ * nxcamera_opendevice() tries to open the preferred devices as specified.
+ *
+ * Return:
+ * OK if compatible device opened (searched or preferred)
+ * -ENODEV if no compatible device opened.
+ * -ENOENT if preferred device couldn't be opened.
+ *
+ ****************************************************************************/
+
+static int nxcamera_opendevice(FAR struct nxcamera_s *pcam)
+{
+ int errcode;
+
+#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE
+ if (pcam->capturedev[0] != '\0')
+ {
+#endif
+
+ if (pcam->displaydev[0] != '\0')
+ {
+#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE
+ /* If we have a preferred device, then open it */
+
+ pcam->capture_fd = open(pcam->capturedev, O_RDWR);
+#else
+ pcam->capture_fd = open(CONFIG_VIDEO_DEV_PATH, O_RDWR);
+#endif
+ if (pcam->capture_fd == -1)
+ {
+ errcode = errno;
+ DEBUGASSERT(errcode > 0);
+
+ verr("ERROR: Failed to open pcam->capturedev %d\n", -errcode);
+ return -errcode;
+ }
+
+ pcam->display_fd = open(pcam->displaydev, O_RDWR);
+ if (pcam->display_fd == -1)
+ {
+ errcode = errno;
+ DEBUGASSERT(errcode > 0);
+
+ close(pcam->capture_fd);
+ pcam->capture_fd = -1;
+ verr("ERROR: Failed to open pcam->displaydev %d\n", -errcode);
+ return -errcode;
+ }
+
+ errcode = ioctl(pcam->display_fd, FBIOGET_PLANEINFO,
+ ((uintptr_t)&pcam->display_pinfo));
+
+ if (errcode == OK)
+ {
+ pcam->display_pinfo.fbmem = mmap(NULL, pcam->display_pinfo.fblen,
+ PROT_READ | PROT_WRITE,
+ MAP_SHARED | MAP_FILE,
+ pcam->display_fd, 0);
+ }
+
+ if (errcode < 0 || pcam->display_pinfo.fbmem == MAP_FAILED)
+ {
+ errcode = errno;
+ close(pcam->capture_fd);
+ close(pcam->display_fd);
+ verr("ERROR: ioctl(FBIOGET_PLANEINFO) failed: %d\n", -errcode);
+ return -errcode;
+ }
+
+ return OK;
+ }
+ else
+ {
+ /* TODO: Add file output */
+
+ return -ENOTSUP;
+ }
+
+#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE
+ }
+#endif
+
+ return -ENODEV;
+}
+
+/****************************************************************************
+ * Name: nxcamera_thread_loopthread
+ *
+ * This is the thread that streams the video and handles video controls.
+ *
+ ****************************************************************************/
+
+static void *nxcamera_loopthread(pthread_addr_t pvarg)
+{
+ FAR struct nxcamera_s *pcam = (FAR struct nxcamera_s *)pvarg;
+ unsigned int prio;
+ ssize_t size;
+ struct video_msg_s msg;
+ bool streaming = true;
+ int x;
+ int ret;
+ struct v4l2_buffer buf;
+ uint32_t type;
+
+ vinfo("Entry\n");
+
+ for (x = 0; x < pcam->nbuffers; x++)
+ {
+ buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ buf.memory = V4L2_MEMORY_MMAP;
+ buf.index = x;
+ ret = ioctl(pcam->capture_fd, VIDIOC_QBUF, (uintptr_t)&buf);
+ if (ret < 0)
+ {
+ verr("VIDIOC_QBUF failed: %d\n", -ret);
+ goto err_out;
+ }
+ }
+
+ /* VIDIOC_STREAMON start stream */
+
+ type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ ret = ioctl(pcam->capture_fd, VIDIOC_STREAMON, (uintptr_t)&type);
+ if (ret < 0)
+ {
+ verr("VIDIOC_STREAMON failed: %d\n", -ret);
+ goto err_out;
+ }
+ else
+ {
+ pcam->loopstate = NXCAMERA_STATE_STREAMING;
+ }
+
+ /* Loop until we specifically break. streaming == true means that we are
+ * still looping waiting for the stream to complete. All of the data
+ * may have been sent, but the stream is not complete until we get
+ * VIDEO_MSG_STOP message
+ *
+ * The normal protocol for looping errors detected by the video driver
+ * is as follows:
+ *
+ * (1) The video driver will indicated the error by returning a negated
+ * error value when the next buffer is enqueued. The upper level
+ * then knows that this buffer was not queued.
+ * (2) The video driver must return all queued buffers using the
+ * VIDEO_MSG_DEQUEUE message.
+ */
+
+ while (streaming)
+ {
+ size = mq_receive(pcam->mq, (FAR char *)&msg, sizeof(msg), &prio);
+
+ /* Validate a message was received */
+
+ if (size == sizeof(msg))
+ {
+ /* Perform operation based on message id */
+
+ vinfo("message received size %zd id%d\n", size, msg.msg_id);
+ switch (msg.msg_id)
+ {
+ /* An video buffer is being dequeued by the driver */
+
+ case VIDEO_MSG_DEQUEUE:
+ if (!streaming)
+ {
+ break;
+ }
+
+ if (ret == OK && pcam->loopstate == NXCAMERA_STATE_STREAMING)
+ {
+ if (ret == OK)
+ {
+ pcam->loopstate = NXCAMERA_STATE_LOOPING;
+ }
+ }
+
+ if (ret == OK)
+ {
+ /* Go through to VIDEO_MSG_STOP */
+
+ break;
+ }
+
+ /* Someone wants to stop the stream. */
+
+ case VIDEO_MSG_STOP:
+
+ /* Send a stop message to the device */
+
+ vinfo("Stopping looping\n");
+ ioctl(pcam->capture_fd, VIDIOC_STREAMOFF, 0);
+ streaming = false;
+ break;
+
+ /* Unknown / unsupported message ID */
+
+ default:
+ break;
+ }
+ }
+
+ ret = ioctl(pcam->capture_fd, VIDIOC_DQBUF, (uintptr_t)&buf);
+ if (ret)
+ {
+ verr("Fail DQBUF %d\n", errno);
+ goto err_out;
+ }
+
+ ret = show_image(pcam, &buf);
+ if (ret < 0)
+ {
+ verr("Fail to show image %d\n", -ret);
+ goto err_out;
+ }
+
+ ret = ioctl(pcam->capture_fd, VIDIOC_QBUF, (uintptr_t)&buf);
+ if (ret)
+ {
+ verr("Fail QBUF %d\n", errno);
+ goto err_out;
+ }
+
+ usleep(1);
+ }
+
+ /* Release our video buffers and unregister / release the device */
+
+err_out:
+ vinfo("Clean-up and exit\n");
+
+ /* Cleanup */
+
+ while (sem_wait(&pcam->sem) < 0)
+ {
+ }
+
+ close(pcam->display_fd); /* Close the display device */
+ close(pcam->capture_fd); /* Close the capture device */
+ pcam->display_fd = -1; /* Mark display device as closed */
+ pcam->capture_fd = -1; /* Mark capture device as closed */
+ mq_close(pcam->mq); /* Close the message queue */
+ mq_unlink(pcam->mqname); /* Unlink the message queue */
+ pcam->loopstate = NXCAMERA_STATE_IDLE;
+ free(pcam->bufs);
+ free(pcam->buf_sizes);
+ sem_post(&pcam->sem); /* Release the semaphore */
+
+ vinfo("Exit\n");
+
+ return NULL;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: nxcamera_setdevice
+ *
+ * nxcamera_setdevice() sets the preferred video device to use with the
+ * provided nxcamera context.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE
+int nxcamera_setdevice(FAR struct nxcamera_s *pcam,
+ FAR const char *device)
+{
+ int temp_fd;
+ struct v4l2_capability caps;
+
+ DEBUGASSERT(pcam != NULL);
+ DEBUGASSERT(device != NULL);
+
+ /* Try to open the device */
+
+ temp_fd = open(device, O_RDWR);
+ if (temp_fd == -1)
+ {
+ /* Error opening the device */
+
+ return -ENOENT;
+ }
+
+ /* Validate it's a video device by issuing an VIDIOC_QUERYCAP ioctl */
+
+ if (ioctl(temp_fd, VIDIOC_QUERYCAP, (uintptr_t)&caps) != OK)
+ {
+ /* Not an Video device! */
+
+ close(temp_fd);
+ return -ENODEV;
+ }
+
+ /* Close the file */
+
+ close(temp_fd);
+
+ /* Save the path of the preferred device */
+
+ if (caps.capabilities & V4L2_CAP_VIDEO_CAPTURE)
+ {
+ strlcpy(pcam->capturedev, device, sizeof(pcam->capturedev));
+ }
+
+ return OK;
+}
+#endif /* CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE */
+
+/****************************************************************************
+ * Name: nxcamera_setfb
+ *
+ * nxcamera_setfb() sets the output framebuffer device to use with the
+ * provided nxcamera context.
+ *
+ ****************************************************************************/
+
+int nxcamera_setfb(FAR struct nxcamera_s *pcam, FAR const char *device)
+{
+ int temp_fd;
+ struct fb_videoinfo_s vinfo;
+
+ DEBUGASSERT(pcam != NULL);
+ DEBUGASSERT(device != NULL);
+
+ /* Try to open the device */
+
+ temp_fd = open(device, O_RDWR);
+ if (temp_fd == -1)
+ {
+ /* Error opening the device */
+
+ return -ENOENT;
+ }
+
+ /* Validate it's a fb device by issuing an FBIOGET_VIDEOINFO ioctl */
+
+ if (ioctl(temp_fd, FBIOGET_VIDEOINFO, (uintptr_t)&vinfo) != OK)
+ {
+ /* Not an Video device! */
+
+ close(temp_fd);
+ return -ENODEV;
+ }
+
+ /* Close the file */
+
+ close(temp_fd);
+
+ /* Save the path of the framebuffer device */
+
+ if (vinfo.nplanes > 0)
+ {
+ strlcpy(pcam->displaydev, device, sizeof(pcam->displaydev));
+ }
+
+ return OK;
+}
+
+/****************************************************************************
+ * Name: nxcamera_setfile
+ *
+ * nxcamera_setfile() sets the output file path to use with the provided
+ * nxcamera context.
+ *
+ ****************************************************************************/
+
+int nxcamera_setfile(FAR struct nxcamera_s *pcam, FAR const char *pfile,
+ bool isimage)
+{
+ int temp_fd;
+
+ DEBUGASSERT(pcam != NULL);
+ DEBUGASSERT(pfile != NULL);
+
+ /* Try to open the file */
+
+ temp_fd = open(pfile, O_CREAT | O_RDWR);
+ if (temp_fd == -1)
+ {
+ /* Error opening the file */
+
+ return -EACCES;
+ }
+
+ /* Close the file */
+
+ close(temp_fd);
+
+ /* Save the path of the output file */
+
+ if (isimage)
+ {
+ strlcpy(pcam->oimagepath, pfile, sizeof(pcam->oimagepath));
+ }
+ else
+ {
+ strlcpy(pcam->ovideopath, pfile, sizeof(pcam->ovideopath));
+ }
+
+ return OK;
+}
+
+/****************************************************************************
+ * Name: nxcamera_stop
+ *
+ * nxcamera_stop() stops the current playback to loop and closes the
+ * file and the associated device.
+ *
+ * Input:
+ * pcam Pointer to the initialized looper context
+ *
+ ****************************************************************************/
+
+int nxcamera_stop(FAR struct nxcamera_s *pcam)
+{
+ struct video_msg_s term_msg;
+ FAR void *value;
+
+ DEBUGASSERT(pcam != NULL);
+
+ /* Validate we are not in IDLE state */
+
+ sem_wait(&pcam->sem); /* Get the semaphore */
+ if (pcam->loopstate == NXCAMERA_STATE_IDLE)
+ {
+ sem_post(&pcam->sem); /* Release the semaphore */
+ return OK;
+ }
+
+ sem_post(&pcam->sem);
+
+ /* Notify the stream thread that it needs to cancel the stream */
+
+ term_msg.msg_id = VIDEO_MSG_STOP;
+ term_msg.u.data = 0;
+ mq_send(pcam->mq, (FAR const char *)&term_msg, sizeof(term_msg),
+ CONFIG_NXCAMERA_MSG_PRIO);
+
+ /* Join the thread. The thread will do all the cleanup. */
+
+ pthread_join(pcam->loop_id, &value);
+ pcam->loop_id = 0;
+
+ return OK;
+}
+
+/****************************************************************************
+ * Name: nxcamera_stream
+ *
+ * nxcamera_stream() tries to capture and then display the raw data using
+ * the Video system. If a capture device is specified, it will try to use
+ * that device.
+ *
+ * Input:
+ * pcam Pointer to the initialized Looper context
+ * width Capture frame width
+ * height Capture frame height
+ * framerate Capture frame rate
+ * format Capture frame pixel format
+ *
+ * Returns:
+ * OK Video is being looped
+ * -EBUSY Capture device is busy
+ * -ENOSYS No supported video format found
+ * -ENODEV No video capture or framebuffer device suitable
+ *
+ ****************************************************************************/
+
+int nxcamera_stream(FAR struct nxcamera_s *pcam,
+ uint16_t width, uint16_t height,
+ uint32_t framerate, uint32_t format)
+{
+ struct mq_attr attr;
+ struct sched_param sparam;
+ pthread_attr_t tattr;
+ FAR void *value;
+ int ret;
+ int i;
+ struct v4l2_buffer buf;
+ struct v4l2_requestbuffers req;
+ struct v4l2_streamparm parm;
+
+ DEBUGASSERT(pcam != NULL);
+
+ if (pcam->loopstate != NXCAMERA_STATE_IDLE)
+ {
+ return -EBUSY;
+ }
+
+ vinfo("==============================\n");
+ vinfo("streaming video\n");
+ vinfo("==============================\n");
+
+ /* Try to open the device */
+
+ ret = nxcamera_opendevice(pcam);
+ if (ret < 0)
+ {
+ /* Error opening the device */
+
+ verr("ERROR: nxcamera_opendevice failed: %d\n", ret);
+ return ret;
+ }
+
+ /* VIDIOC_S_FMT set format */
+
+ pcam->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ pcam->fmt.fmt.pix.width = width;
+ pcam->fmt.fmt.pix.height = height;
+ pcam->fmt.fmt.pix.field = V4L2_FIELD_ANY;
+ pcam->fmt.fmt.pix.pixelformat = format;
+
+ ret = ioctl(pcam->capture_fd, VIDIOC_S_FMT, (uintptr_t)&pcam->fmt);
+ if (ret < 0)
+ {
+ verr("VIDIOC_S_FMT failed: %d\n", -ret);
+ return ret;
+ }
+
+ memset(&parm, 0, sizeof(parm));
+ parm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ parm.parm.capture.timeperframe.denominator = framerate;
+ parm.parm.capture.timeperframe.numerator = 1;
+ ret = ioctl(pcam->capture_fd, VIDIOC_S_PARM, (uintptr_t)&parm);
+ if (ret < 0)
+ {
+ verr("VIDIOC_S_PARM failed: %d\n", -ret);
+ return ret;
+ }
+
+ /* VIDIOC_REQBUFS initiate user pointer I/O */
+
+ req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ req.memory = V4L2_MEMORY_MMAP;
+ req.count = REQBUF_COUNT;
+
+ ret = ioctl(pcam->capture_fd, VIDIOC_REQBUFS, (uintptr_t)&req);
+ if (ret < 0)
+ {
+ verr("VIDIOC_REQBUFS failed: %d\n", -ret);
+ return ret;
+ }
+
+ if (req.count < 2)
+ {
+ verr("VIDIOC_REQBUFS failed: not enough buffers\n");
+ return -ENOMEM;
+ }
+
+ pcam->nbuffers = req.count;
+ pcam->bufs = calloc(req.count, sizeof(*pcam->bufs));
+ pcam->buf_sizes = calloc(req.count, sizeof(*pcam->buf_sizes));
+ if (!pcam->bufs)
Review Comment:
```suggestion
if (!pcam->bufs || !pcam->buf_sizes)
```
##########
system/nxcamera/nxcamera.c:
##########
@@ -0,0 +1,904 @@
+/****************************************************************************
+ * apps/system/nxcamera/nxcamera.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <assert.h>
+#include <debug.h>
+#include <dirent.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <nuttx/queue.h>
+#include <nuttx/video/video.h>
+#include <nuttx/video/fb.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <sys/types.h>
+
+#include "system/nxcamera.h"
+#ifdef CONFIG_LIBYUV
+# include "libyuv.h"
+#endif
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define NXCAMERA_STATE_IDLE 0
+#define NXCAMERA_STATE_STREAMING 1
+#define NXCAMERA_STATE_LOOPING 2
+#define NXCAMERA_STATE_PAUSED 3
+
+#define REQBUF_COUNT 3
+
+#ifndef MIN
+# define MIN(a, b) (((a) < (b)) ? (a) : (b))
+#endif
+
+#define convert_frame ConvertToARGB
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int show_image(FAR struct nxcamera_s *pcam, FAR v4l2_buffer_t *buf)
+{
+#ifdef CONFIG_LIBYUV
+ return convert_frame(pcam->bufs[buf->index],
+ pcam->buf_sizes[buf->index],
+ pcam->display_pinfo.fbmem,
+ pcam->display_pinfo.stride,
+ 0,
+ 0,
+ pcam->fmt.fmt.pix.width,
+ pcam->fmt.fmt.pix.height,
+ pcam->fmt.fmt.pix.width,
+ pcam->fmt.fmt.pix.height,
+ 0,
+ pcam->fmt.fmt.pix.pixelformat);
+#else
+ uint32_t *pbuf = pcam->bufs[buf->index];
+ vinfo("show image from %p: %lx %lx", pbuf, pbuf[0], pbuf[1]);
+ return 0;
+#endif
+}
+
+/****************************************************************************
+ * Name: nxcamera_opendevice
+ *
+ * nxcamera_opendevice() tries to open the preferred devices as specified.
+ *
+ * Return:
+ * OK if compatible device opened (searched or preferred)
+ * -ENODEV if no compatible device opened.
+ * -ENOENT if preferred device couldn't be opened.
+ *
+ ****************************************************************************/
+
+static int nxcamera_opendevice(FAR struct nxcamera_s *pcam)
+{
+ int errcode;
+
+#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE
+ if (pcam->capturedev[0] != '\0')
+ {
+#endif
+
+ if (pcam->displaydev[0] != '\0')
+ {
+#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE
+ /* If we have a preferred device, then open it */
+
+ pcam->capture_fd = open(pcam->capturedev, O_RDWR);
+#else
+ pcam->capture_fd = open(CONFIG_VIDEO_DEV_PATH, O_RDWR);
+#endif
+ if (pcam->capture_fd == -1)
+ {
+ errcode = errno;
+ DEBUGASSERT(errcode > 0);
+
+ verr("ERROR: Failed to open pcam->capturedev %d\n", -errcode);
+ return -errcode;
+ }
+
+ pcam->display_fd = open(pcam->displaydev, O_RDWR);
+ if (pcam->display_fd == -1)
+ {
+ errcode = errno;
+ DEBUGASSERT(errcode > 0);
+
+ close(pcam->capture_fd);
+ pcam->capture_fd = -1;
+ verr("ERROR: Failed to open pcam->displaydev %d\n", -errcode);
+ return -errcode;
+ }
+
+ errcode = ioctl(pcam->display_fd, FBIOGET_PLANEINFO,
+ ((uintptr_t)&pcam->display_pinfo));
+
+ if (errcode == OK)
+ {
+ pcam->display_pinfo.fbmem = mmap(NULL, pcam->display_pinfo.fblen,
+ PROT_READ | PROT_WRITE,
+ MAP_SHARED | MAP_FILE,
+ pcam->display_fd, 0);
+ }
+
+ if (errcode < 0 || pcam->display_pinfo.fbmem == MAP_FAILED)
+ {
+ errcode = errno;
+ close(pcam->capture_fd);
+ close(pcam->display_fd);
+ verr("ERROR: ioctl(FBIOGET_PLANEINFO) failed: %d\n", -errcode);
+ return -errcode;
+ }
+
+ return OK;
+ }
+ else
+ {
+ /* TODO: Add file output */
+
+ return -ENOTSUP;
+ }
+
+#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE
+ }
+#endif
+
+ return -ENODEV;
+}
+
+/****************************************************************************
+ * Name: nxcamera_thread_loopthread
+ *
+ * This is the thread that streams the video and handles video controls.
+ *
+ ****************************************************************************/
+
+static void *nxcamera_loopthread(pthread_addr_t pvarg)
+{
+ FAR struct nxcamera_s *pcam = (FAR struct nxcamera_s *)pvarg;
+ unsigned int prio;
+ ssize_t size;
+ struct video_msg_s msg;
+ bool streaming = true;
+ int x;
+ int ret;
+ struct v4l2_buffer buf;
+ uint32_t type;
+
+ vinfo("Entry\n");
+
+ for (x = 0; x < pcam->nbuffers; x++)
+ {
+ buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ buf.memory = V4L2_MEMORY_MMAP;
+ buf.index = x;
+ ret = ioctl(pcam->capture_fd, VIDIOC_QBUF, (uintptr_t)&buf);
+ if (ret < 0)
+ {
+ verr("VIDIOC_QBUF failed: %d\n", -ret);
+ goto err_out;
+ }
+ }
+
+ /* VIDIOC_STREAMON start stream */
+
+ type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ ret = ioctl(pcam->capture_fd, VIDIOC_STREAMON, (uintptr_t)&type);
+ if (ret < 0)
+ {
+ verr("VIDIOC_STREAMON failed: %d\n", -ret);
+ goto err_out;
+ }
+ else
+ {
+ pcam->loopstate = NXCAMERA_STATE_STREAMING;
+ }
+
+ /* Loop until we specifically break. streaming == true means that we are
+ * still looping waiting for the stream to complete. All of the data
+ * may have been sent, but the stream is not complete until we get
+ * VIDEO_MSG_STOP message
+ *
+ * The normal protocol for looping errors detected by the video driver
+ * is as follows:
+ *
+ * (1) The video driver will indicated the error by returning a negated
+ * error value when the next buffer is enqueued. The upper level
+ * then knows that this buffer was not queued.
+ * (2) The video driver must return all queued buffers using the
+ * VIDEO_MSG_DEQUEUE message.
+ */
+
+ while (streaming)
+ {
+ size = mq_receive(pcam->mq, (FAR char *)&msg, sizeof(msg), &prio);
+
+ /* Validate a message was received */
+
+ if (size == sizeof(msg))
+ {
+ /* Perform operation based on message id */
+
+ vinfo("message received size %zd id%d\n", size, msg.msg_id);
+ switch (msg.msg_id)
+ {
+ /* An video buffer is being dequeued by the driver */
+
+ case VIDEO_MSG_DEQUEUE:
+ if (!streaming)
+ {
+ break;
+ }
+
+ if (ret == OK && pcam->loopstate == NXCAMERA_STATE_STREAMING)
+ {
+ if (ret == OK)
+ {
+ pcam->loopstate = NXCAMERA_STATE_LOOPING;
+ }
+ }
+
+ if (ret == OK)
+ {
+ /* Go through to VIDEO_MSG_STOP */
+
+ break;
+ }
+
+ /* Someone wants to stop the stream. */
+
+ case VIDEO_MSG_STOP:
+
+ /* Send a stop message to the device */
+
+ vinfo("Stopping looping\n");
+ ioctl(pcam->capture_fd, VIDIOC_STREAMOFF, 0);
+ streaming = false;
+ break;
+
+ /* Unknown / unsupported message ID */
+
+ default:
+ break;
+ }
+ }
+
+ ret = ioctl(pcam->capture_fd, VIDIOC_DQBUF, (uintptr_t)&buf);
+ if (ret)
+ {
+ verr("Fail DQBUF %d\n", errno);
+ goto err_out;
+ }
+
+ ret = show_image(pcam, &buf);
+ if (ret < 0)
+ {
+ verr("Fail to show image %d\n", -ret);
+ goto err_out;
+ }
+
+ ret = ioctl(pcam->capture_fd, VIDIOC_QBUF, (uintptr_t)&buf);
+ if (ret)
+ {
+ verr("Fail QBUF %d\n", errno);
+ goto err_out;
+ }
+
+ usleep(1);
+ }
+
+ /* Release our video buffers and unregister / release the device */
+
+err_out:
+ vinfo("Clean-up and exit\n");
+
+ /* Cleanup */
+
+ while (sem_wait(&pcam->sem) < 0)
+ {
+ }
+
+ close(pcam->display_fd); /* Close the display device */
+ close(pcam->capture_fd); /* Close the capture device */
+ pcam->display_fd = -1; /* Mark display device as closed */
+ pcam->capture_fd = -1; /* Mark capture device as closed */
+ mq_close(pcam->mq); /* Close the message queue */
+ mq_unlink(pcam->mqname); /* Unlink the message queue */
+ pcam->loopstate = NXCAMERA_STATE_IDLE;
+ free(pcam->bufs);
+ free(pcam->buf_sizes);
+ sem_post(&pcam->sem); /* Release the semaphore */
+
+ vinfo("Exit\n");
+
+ return NULL;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: nxcamera_setdevice
+ *
+ * nxcamera_setdevice() sets the preferred video device to use with the
+ * provided nxcamera context.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE
+int nxcamera_setdevice(FAR struct nxcamera_s *pcam,
+ FAR const char *device)
+{
+ int temp_fd;
+ struct v4l2_capability caps;
+
+ DEBUGASSERT(pcam != NULL);
+ DEBUGASSERT(device != NULL);
+
+ /* Try to open the device */
+
+ temp_fd = open(device, O_RDWR);
+ if (temp_fd == -1)
+ {
+ /* Error opening the device */
+
+ return -ENOENT;
+ }
+
+ /* Validate it's a video device by issuing an VIDIOC_QUERYCAP ioctl */
+
+ if (ioctl(temp_fd, VIDIOC_QUERYCAP, (uintptr_t)&caps) != OK)
+ {
+ /* Not an Video device! */
+
+ close(temp_fd);
+ return -ENODEV;
+ }
+
+ /* Close the file */
+
+ close(temp_fd);
+
+ /* Save the path of the preferred device */
+
+ if (caps.capabilities & V4L2_CAP_VIDEO_CAPTURE)
+ {
+ strlcpy(pcam->capturedev, device, sizeof(pcam->capturedev));
+ }
+
+ return OK;
+}
+#endif /* CONFIG_NXCAMERA_INCLUDE_PREFERRED_DEVICE */
+
+/****************************************************************************
+ * Name: nxcamera_setfb
+ *
+ * nxcamera_setfb() sets the output framebuffer device to use with the
+ * provided nxcamera context.
+ *
+ ****************************************************************************/
+
+int nxcamera_setfb(FAR struct nxcamera_s *pcam, FAR const char *device)
+{
+ int temp_fd;
+ struct fb_videoinfo_s vinfo;
+
+ DEBUGASSERT(pcam != NULL);
+ DEBUGASSERT(device != NULL);
+
+ /* Try to open the device */
+
+ temp_fd = open(device, O_RDWR);
+ if (temp_fd == -1)
+ {
+ /* Error opening the device */
+
+ return -ENOENT;
+ }
+
+ /* Validate it's a fb device by issuing an FBIOGET_VIDEOINFO ioctl */
+
+ if (ioctl(temp_fd, FBIOGET_VIDEOINFO, (uintptr_t)&vinfo) != OK)
+ {
+ /* Not an Video device! */
+
+ close(temp_fd);
+ return -ENODEV;
+ }
+
+ /* Close the file */
+
+ close(temp_fd);
+
+ /* Save the path of the framebuffer device */
+
+ if (vinfo.nplanes > 0)
+ {
+ strlcpy(pcam->displaydev, device, sizeof(pcam->displaydev));
+ }
+
+ return OK;
+}
+
+/****************************************************************************
+ * Name: nxcamera_setfile
+ *
+ * nxcamera_setfile() sets the output file path to use with the provided
+ * nxcamera context.
+ *
+ ****************************************************************************/
+
+int nxcamera_setfile(FAR struct nxcamera_s *pcam, FAR const char *pfile,
+ bool isimage)
+{
+ int temp_fd;
+
+ DEBUGASSERT(pcam != NULL);
+ DEBUGASSERT(pfile != NULL);
+
+ /* Try to open the file */
+
+ temp_fd = open(pfile, O_CREAT | O_RDWR);
+ if (temp_fd == -1)
+ {
+ /* Error opening the file */
+
+ return -EACCES;
+ }
+
+ /* Close the file */
+
+ close(temp_fd);
+
+ /* Save the path of the output file */
+
+ if (isimage)
+ {
+ strlcpy(pcam->oimagepath, pfile, sizeof(pcam->oimagepath));
+ }
+ else
+ {
+ strlcpy(pcam->ovideopath, pfile, sizeof(pcam->ovideopath));
+ }
+
+ return OK;
+}
+
+/****************************************************************************
+ * Name: nxcamera_stop
+ *
+ * nxcamera_stop() stops the current playback to loop and closes the
+ * file and the associated device.
+ *
+ * Input:
+ * pcam Pointer to the initialized looper context
+ *
+ ****************************************************************************/
+
+int nxcamera_stop(FAR struct nxcamera_s *pcam)
+{
+ struct video_msg_s term_msg;
+ FAR void *value;
+
+ DEBUGASSERT(pcam != NULL);
+
+ /* Validate we are not in IDLE state */
+
+ sem_wait(&pcam->sem); /* Get the semaphore */
+ if (pcam->loopstate == NXCAMERA_STATE_IDLE)
+ {
+ sem_post(&pcam->sem); /* Release the semaphore */
+ return OK;
+ }
+
+ sem_post(&pcam->sem);
+
+ /* Notify the stream thread that it needs to cancel the stream */
+
+ term_msg.msg_id = VIDEO_MSG_STOP;
+ term_msg.u.data = 0;
+ mq_send(pcam->mq, (FAR const char *)&term_msg, sizeof(term_msg),
+ CONFIG_NXCAMERA_MSG_PRIO);
+
+ /* Join the thread. The thread will do all the cleanup. */
+
+ pthread_join(pcam->loop_id, &value);
+ pcam->loop_id = 0;
+
+ return OK;
+}
+
+/****************************************************************************
+ * Name: nxcamera_stream
+ *
+ * nxcamera_stream() tries to capture and then display the raw data using
+ * the Video system. If a capture device is specified, it will try to use
+ * that device.
+ *
+ * Input:
+ * pcam Pointer to the initialized Looper context
+ * width Capture frame width
+ * height Capture frame height
+ * framerate Capture frame rate
+ * format Capture frame pixel format
+ *
+ * Returns:
+ * OK Video is being looped
+ * -EBUSY Capture device is busy
+ * -ENOSYS No supported video format found
+ * -ENODEV No video capture or framebuffer device suitable
+ *
+ ****************************************************************************/
+
+int nxcamera_stream(FAR struct nxcamera_s *pcam,
+ uint16_t width, uint16_t height,
+ uint32_t framerate, uint32_t format)
+{
+ struct mq_attr attr;
+ struct sched_param sparam;
+ pthread_attr_t tattr;
+ FAR void *value;
+ int ret;
+ int i;
+ struct v4l2_buffer buf;
+ struct v4l2_requestbuffers req;
+ struct v4l2_streamparm parm;
+
+ DEBUGASSERT(pcam != NULL);
+
+ if (pcam->loopstate != NXCAMERA_STATE_IDLE)
+ {
+ return -EBUSY;
+ }
+
+ vinfo("==============================\n");
+ vinfo("streaming video\n");
+ vinfo("==============================\n");
+
+ /* Try to open the device */
+
+ ret = nxcamera_opendevice(pcam);
+ if (ret < 0)
+ {
+ /* Error opening the device */
+
+ verr("ERROR: nxcamera_opendevice failed: %d\n", ret);
+ return ret;
+ }
+
+ /* VIDIOC_S_FMT set format */
+
+ pcam->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ pcam->fmt.fmt.pix.width = width;
+ pcam->fmt.fmt.pix.height = height;
+ pcam->fmt.fmt.pix.field = V4L2_FIELD_ANY;
+ pcam->fmt.fmt.pix.pixelformat = format;
+
+ ret = ioctl(pcam->capture_fd, VIDIOC_S_FMT, (uintptr_t)&pcam->fmt);
+ if (ret < 0)
+ {
+ verr("VIDIOC_S_FMT failed: %d\n", -ret);
+ return ret;
+ }
+
+ memset(&parm, 0, sizeof(parm));
+ parm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ parm.parm.capture.timeperframe.denominator = framerate;
+ parm.parm.capture.timeperframe.numerator = 1;
+ ret = ioctl(pcam->capture_fd, VIDIOC_S_PARM, (uintptr_t)&parm);
+ if (ret < 0)
+ {
+ verr("VIDIOC_S_PARM failed: %d\n", -ret);
+ return ret;
+ }
+
+ /* VIDIOC_REQBUFS initiate user pointer I/O */
+
+ req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ req.memory = V4L2_MEMORY_MMAP;
+ req.count = REQBUF_COUNT;
+
+ ret = ioctl(pcam->capture_fd, VIDIOC_REQBUFS, (uintptr_t)&req);
+ if (ret < 0)
+ {
+ verr("VIDIOC_REQBUFS failed: %d\n", -ret);
+ return ret;
+ }
+
+ if (req.count < 2)
+ {
+ verr("VIDIOC_REQBUFS failed: not enough buffers\n");
+ return -ENOMEM;
+ }
+
+ pcam->nbuffers = req.count;
+ pcam->bufs = calloc(req.count, sizeof(*pcam->bufs));
+ pcam->buf_sizes = calloc(req.count, sizeof(*pcam->buf_sizes));
+ if (!pcam->bufs)
+ {
+ verr("Cannot allocate buffer pointers\n");
+ return -ENOMEM;
Review Comment:
how to release capture/display fd
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org