Skip to content

Commit 66a7df4

Browse files
committed
libuvc: add cancel fd to quit poll thread
1 parent 3907a57 commit 66a7df4

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

gear-lib/librpc/librpc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ void print_session(struct rpc_session *ss)
111111
printf("session.msg_id = 0x%08x\n", ss->msg_id);
112112
printf("session.timestamp = %" PRIu64 "(%s)\n", ss->timestamp,
113113
time_str_format_by_msec(ss->timestamp, ts, sizeof(ts)));
114-
printf("session.checksum = 0x%08lx\n", ss->cseq);
114+
printf("session.checksum = 0x%" PRIx64 "\n", ss->cseq);
115115
printf("================\n");
116116
}
117117

gear-lib/libuvc/test_libuvc.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ static struct file *fp;
3838

3939
static int on_frame(struct uvc_ctx *c, struct video_frame *frm)
4040
{
41-
printf("frm[%" PRIu64 "] size=%" PRIu64 ", ts=%" PRIu64 " ms\n", frm->frame_id, frm->total_size, frm->timestamp/1000000);
41+
static uint64_t last_ms = 0;
42+
43+
printf("frm[%" PRIu64 "] size=%" PRIu64 ", ts=%" PRIu64 " ms, gap=%" PRIu64 " ms\n",
44+
frm->frame_id, frm->total_size, frm->timestamp/1000000, frm->timestamp/1000000 - last_ms);
45+
last_ms = frm->timestamp/1000000;
4246
file_write(fp, frm->data[0], frm->total_size);
4347
return 0;
4448
}
@@ -82,7 +86,7 @@ int dummy_test()
8286
struct uvc_config conf = {
8387
.width = 320,
8488
.height = 240,
85-
.fps = {5, 1},
89+
.fps = {30, 1},
8690
.format = PIXEL_FORMAT_YUY2,
8791
};
8892
struct uvc_ctx *uvc = uvc_open(UVC_TYPE_DUMMY, "sample_yuv422p.yuv", &conf);

gear-lib/libuvc/v4l2.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <sys/uio.h>
3333
#include <sys/mman.h>
3434
#include <sys/epoll.h>
35+
#include <sys/eventfd.h>
3536
#include <linux/videodev2.h>
3637

3738
/*
@@ -78,6 +79,7 @@ static const uint32_t v4l2_cid_supported[] = {
7879

7980
struct v4l2_ctx {
8081
int fd;
82+
int cancel_fd;
8183
int channel; /*one video node may contain several input channel */
8284
int standard;
8385
uint32_t pixfmt;
@@ -206,6 +208,7 @@ static void *uvc_v4l2_open(struct uvc_ctx *uvc, const char *dev, struct uvc_conf
206208
goto failed;
207209
}
208210
c->fd = fd;
211+
c->cancel_fd = eventfd(0, 0);
209212

210213
c->channel = -1;
211214
c->standard = -1;
@@ -501,6 +504,10 @@ static int uvc_v4l2_poll_init(struct v4l2_ctx *c)
501504
printf("epoll_ctl EPOLL_CTL_ADD failed %d!\n", errno);
502505
return -1;
503506
}
507+
if (-1 == epoll_ctl(c->epfd, EPOLL_CTL_ADD, c->cancel_fd, &epev)) {
508+
printf("epoll_ctl EPOLL_CTL_ADD failed %d!\n", errno);
509+
return -1;
510+
}
504511
return 0;
505512
}
506513

@@ -603,6 +610,7 @@ static int uvc_v4l2_start_stream(struct uvc_ctx *uvc)
603610
static int uvc_v4l2_stop_stream(struct uvc_ctx *uvc)
604611
{
605612
int i;
613+
uint64_t notify = '1';
606614
enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
607615
struct v4l2_ctx *c = (struct v4l2_ctx *)uvc->opaque;
608616

@@ -613,6 +621,9 @@ static int uvc_v4l2_stop_stream(struct uvc_ctx *uvc)
613621

614622
if (uvc->on_video_frame) {
615623
c->is_streaming = false;
624+
if (sizeof(uint64_t) != write(c->cancel_fd, &notify, sizeof(uint64_t))) {
625+
perror("write error");
626+
}
616627
thread_join(c->thread);
617628
thread_destroy(c->thread);
618629
}

0 commit comments

Comments
 (0)