[v2] media: uvcvideo: Implement V4L2_EVENT_FRAME_SYNC

Message ID 20231106-uvc-event-v2-1-7d8e36f0df16@chromium.org
State New
Headers
Series [v2] media: uvcvideo: Implement V4L2_EVENT_FRAME_SYNC |

Commit Message

Ricardo Ribalda Nov. 6, 2023, 10:52 a.m. UTC
  Add support for the frame_sync event, so user-space can become aware
earlier of new frames.

Suggested-by: Esker Wong <esker@chromium.org>
Tested-by: Esker Wong <esker@chromium.org>
Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
---
We have measured a latency of around 30msecs between frame sync
and dqbuf.
---
Changes in v2:
- Suggested by Laurent. Split sequence++ and event init.
- Link to v1: https://lore.kernel.org/r/20231020-uvc-event-v1-1-3baa0e9f6952@chromium.org
---
 drivers/media/usb/uvc/uvc_v4l2.c  | 2 ++
 drivers/media/usb/uvc/uvc_video.c | 7 +++++++
 2 files changed, 9 insertions(+)


---
base-commit: ce55c22ec8b223a90ff3e084d842f73cfba35588
change-id: 20231020-uvc-event-d3d1bbbdcb2f

Best regards,
  

Comments

Sakari Ailus Nov. 6, 2023, 11:05 a.m. UTC | #1
Hi Ricardo,

On Mon, Nov 06, 2023 at 10:52:27AM +0000, Ricardo Ribalda wrote:
> Add support for the frame_sync event, so user-space can become aware
> earlier of new frames.
> 
> Suggested-by: Esker Wong <esker@chromium.org>
> Tested-by: Esker Wong <esker@chromium.org>
> Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
> ---
> We have measured a latency of around 30msecs between frame sync
> and dqbuf.
> ---
> Changes in v2:
> - Suggested by Laurent. Split sequence++ and event init.
> - Link to v1: https://lore.kernel.org/r/20231020-uvc-event-v1-1-3baa0e9f6952@chromium.org
> ---
>  drivers/media/usb/uvc/uvc_v4l2.c  | 2 ++
>  drivers/media/usb/uvc/uvc_video.c | 7 +++++++
>  2 files changed, 9 insertions(+)
> 
> diff --git a/drivers/media/usb/uvc/uvc_v4l2.c b/drivers/media/usb/uvc/uvc_v4l2.c
> index f4988f03640a..9f3fb5fd2375 100644
> --- a/drivers/media/usb/uvc/uvc_v4l2.c
> +++ b/drivers/media/usb/uvc/uvc_v4l2.c
> @@ -1352,6 +1352,8 @@ static int uvc_ioctl_subscribe_event(struct v4l2_fh *fh,
>  	switch (sub->type) {
>  	case V4L2_EVENT_CTRL:
>  		return v4l2_event_subscribe(fh, sub, 0, &uvc_ctrl_sub_ev_ops);
> +	case V4L2_EVENT_FRAME_SYNC:
> +		return v4l2_event_subscribe(fh, sub, 0, NULL);
>  	default:
>  		return -EINVAL;
>  	}
> diff --git a/drivers/media/usb/uvc/uvc_video.c b/drivers/media/usb/uvc/uvc_video.c
> index 28dde08ec6c5..4f3a510ca4fe 100644
> --- a/drivers/media/usb/uvc/uvc_video.c
> +++ b/drivers/media/usb/uvc/uvc_video.c
> @@ -1073,9 +1073,16 @@ static int uvc_video_decode_start(struct uvc_streaming *stream,
>  	 * that discontinuous sequence numbers always indicate lost frames.
>  	 */
>  	if (stream->last_fid != fid) {
> +		struct v4l2_event event = {
> +			.type = V4L2_EVENT_FRAME_SYNC,
> +		};
> +
>  		stream->sequence++;
>  		if (stream->sequence)
>  			uvc_video_stats_update(stream);
> +
> +		event.u.frame_sync.frame_sequence = stream->sequence,
> +		v4l2_event_queue(&stream->vdev, &event);

uvc_video_decode_start() is called when the reception of the entire frame
has been completed. However, the documentation for V4L2_EVENT_FRAME_SYNC
says that the event is "Triggered immediately when the reception of a frame
has begun.". The functionality here doesn't seem to fit to this patch.

Wouldn't V4L2_EVENT_VSYNC be a better fit, even if we don't really have a
concept of vertical sync in the case of USB? That event doesn't have the
sequence though but I guess it's not an issue at least if your case.

Another technically correct option could be to create a new event for this
but I'm not sure it's worth it.

>  	}
>  
>  	uvc_video_clock_decode(stream, buf, data, len);
>
  
Ricardo Ribalda Nov. 6, 2023, 11:58 a.m. UTC | #2
Hi Sakari

On Mon, 6 Nov 2023 at 12:06, Sakari Ailus <sakari.ailus@iki.fi> wrote:
>
> Hi Ricardo,
>
> On Mon, Nov 06, 2023 at 10:52:27AM +0000, Ricardo Ribalda wrote:
> > Add support for the frame_sync event, so user-space can become aware
> > earlier of new frames.
> >
> > Suggested-by: Esker Wong <esker@chromium.org>
> > Tested-by: Esker Wong <esker@chromium.org>
> > Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
> > ---
> > We have measured a latency of around 30msecs between frame sync
> > and dqbuf.
> > ---
> > Changes in v2:
> > - Suggested by Laurent. Split sequence++ and event init.
> > - Link to v1: https://lore.kernel.org/r/20231020-uvc-event-v1-1-3baa0e9f6952@chromium.org
> > ---
> >  drivers/media/usb/uvc/uvc_v4l2.c  | 2 ++
> >  drivers/media/usb/uvc/uvc_video.c | 7 +++++++
> >  2 files changed, 9 insertions(+)
> >
> > diff --git a/drivers/media/usb/uvc/uvc_v4l2.c b/drivers/media/usb/uvc/uvc_v4l2.c
> > index f4988f03640a..9f3fb5fd2375 100644
> > --- a/drivers/media/usb/uvc/uvc_v4l2.c
> > +++ b/drivers/media/usb/uvc/uvc_v4l2.c
> > @@ -1352,6 +1352,8 @@ static int uvc_ioctl_subscribe_event(struct v4l2_fh *fh,
> >       switch (sub->type) {
> >       case V4L2_EVENT_CTRL:
> >               return v4l2_event_subscribe(fh, sub, 0, &uvc_ctrl_sub_ev_ops);
> > +     case V4L2_EVENT_FRAME_SYNC:
> > +             return v4l2_event_subscribe(fh, sub, 0, NULL);
> >       default:
> >               return -EINVAL;
> >       }
> > diff --git a/drivers/media/usb/uvc/uvc_video.c b/drivers/media/usb/uvc/uvc_video.c
> > index 28dde08ec6c5..4f3a510ca4fe 100644
> > --- a/drivers/media/usb/uvc/uvc_video.c
> > +++ b/drivers/media/usb/uvc/uvc_video.c
> > @@ -1073,9 +1073,16 @@ static int uvc_video_decode_start(struct uvc_streaming *stream,
> >        * that discontinuous sequence numbers always indicate lost frames.
> >        */
> >       if (stream->last_fid != fid) {
> > +             struct v4l2_event event = {
> > +                     .type = V4L2_EVENT_FRAME_SYNC,
> > +             };
> > +
> >               stream->sequence++;
> >               if (stream->sequence)
> >                       uvc_video_stats_update(stream);
> > +
> > +             event.u.frame_sync.frame_sequence = stream->sequence,
> > +             v4l2_event_queue(&stream->vdev, &event);
>
> uvc_video_decode_start() is called when the reception of the entire frame
> has been completed. However, the documentation for V4L2_EVENT_FRAME_SYNC
> says that the event is "Triggered immediately when the reception of a frame
> has begun.". The functionality here doesn't seem to fit to this patch.

If I understand correctly, there are two types of transfers, BULK and ISOC.

For bulk frames are typically divided in multiple packages and the
event is generated in the first package. So I guess we fit the
description

For isoc we receive the whole frame at once and we trigger the event
before we process it... It is maybe a bit more borderline
implementation of the event description, but not very far away.


>
> Wouldn't V4L2_EVENT_VSYNC be a better fit, even if we don't really have a
> concept of vertical sync in the case of USB? That event doesn't have the
> sequence though but I guess it's not an issue at least if your case.

All that said, If Esker does not need the sequence number I have no
issue converting it. @Esker Wong WDYT?

>
> Another technically correct option could be to create a new event for this
> but I'm not sure it's worth it.
>
> >       }
> >
> >       uvc_video_clock_decode(stream, buf, data, len);
> >
>
> --
> Regards,
>
> Sakari Ailus
  
Esker Wong Nov. 7, 2023, 5:06 a.m. UTC | #3
[send again in text mode]
Hi Sakari,

Sequence number is important to us. We need it to measure the latency
from this event to the time we display the frame.

Regards,
Esker


On Mon, Nov 6, 2023 at 7:06 PM Sakari Ailus <sakari.ailus@iki.fi> wrote:
>
> Hi Ricardo,
>
> On Mon, Nov 06, 2023 at 10:52:27AM +0000, Ricardo Ribalda wrote:
> > Add support for the frame_sync event, so user-space can become aware
> > earlier of new frames.
> >
> > Suggested-by: Esker Wong <esker@chromium.org>
> > Tested-by: Esker Wong <esker@chromium.org>
> > Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
> > ---
> > We have measured a latency of around 30msecs between frame sync
> > and dqbuf.
> > ---
> > Changes in v2:
> > - Suggested by Laurent. Split sequence++ and event init.
> > - Link to v1: https://lore.kernel.org/r/20231020-uvc-event-v1-1-3baa0e9f6952@chromium.org
> > ---
> >  drivers/media/usb/uvc/uvc_v4l2.c  | 2 ++
> >  drivers/media/usb/uvc/uvc_video.c | 7 +++++++
> >  2 files changed, 9 insertions(+)
> >
> > diff --git a/drivers/media/usb/uvc/uvc_v4l2.c b/drivers/media/usb/uvc/uvc_v4l2.c
> > index f4988f03640a..9f3fb5fd2375 100644
> > --- a/drivers/media/usb/uvc/uvc_v4l2.c
> > +++ b/drivers/media/usb/uvc/uvc_v4l2.c
> > @@ -1352,6 +1352,8 @@ static int uvc_ioctl_subscribe_event(struct v4l2_fh *fh,
> >       switch (sub->type) {
> >       case V4L2_EVENT_CTRL:
> >               return v4l2_event_subscribe(fh, sub, 0, &uvc_ctrl_sub_ev_ops);
> > +     case V4L2_EVENT_FRAME_SYNC:
> > +             return v4l2_event_subscribe(fh, sub, 0, NULL);
> >       default:
> >               return -EINVAL;
> >       }
> > diff --git a/drivers/media/usb/uvc/uvc_video.c b/drivers/media/usb/uvc/uvc_video.c
> > index 28dde08ec6c5..4f3a510ca4fe 100644
> > --- a/drivers/media/usb/uvc/uvc_video.c
> > +++ b/drivers/media/usb/uvc/uvc_video.c
> > @@ -1073,9 +1073,16 @@ static int uvc_video_decode_start(struct uvc_streaming *stream,
> >        * that discontinuous sequence numbers always indicate lost frames.
> >        */
> >       if (stream->last_fid != fid) {
> > +             struct v4l2_event event = {
> > +                     .type = V4L2_EVENT_FRAME_SYNC,
> > +             };
> > +
> >               stream->sequence++;
> >               if (stream->sequence)
> >                       uvc_video_stats_update(stream);
> > +
> > +             event.u.frame_sync.frame_sequence = stream->sequence,
> > +             v4l2_event_queue(&stream->vdev, &event);
>
> uvc_video_decode_start() is called when the reception of the entire frame
> has been completed. However, the documentation for V4L2_EVENT_FRAME_SYNC
> says that the event is "Triggered immediately when the reception of a frame
> has begun.". The functionality here doesn't seem to fit to this patch.
>
> Wouldn't V4L2_EVENT_VSYNC be a better fit, even if we don't really have a
> concept of vertical sync in the case of USB? That event doesn't have the
> sequence though but I guess it's not an issue at least if your case.
>
> Another technically correct option could be to create a new event for this
> but I'm not sure it's worth it.
>
> >       }
> >
> >       uvc_video_clock_decode(stream, buf, data, len);
> >
>
> --
> Regards,
>
> Sakari Ailus
  
Sakari Ailus Nov. 7, 2023, 7:36 a.m. UTC | #4
Hi Esker,

On Tue, Nov 07, 2023 at 01:06:20PM +0800, Esker Wong wrote:
> [send again in text mode]
> Hi Sakari,
> 
> Sequence number is important to us. We need it to measure the latency
> from this event to the time we display the frame.

I think we could also add the sequence number to V4L2_EVENT_VSYNC.

Cc Hans.

> On Mon, Nov 6, 2023 at 7:06 PM Sakari Ailus <sakari.ailus@iki.fi> wrote:
> >
> > Hi Ricardo,
> >
> > On Mon, Nov 06, 2023 at 10:52:27AM +0000, Ricardo Ribalda wrote:
> > > Add support for the frame_sync event, so user-space can become aware
> > > earlier of new frames.
> > >
> > > Suggested-by: Esker Wong <esker@chromium.org>
> > > Tested-by: Esker Wong <esker@chromium.org>
> > > Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
> > > ---
> > > We have measured a latency of around 30msecs between frame sync
> > > and dqbuf.
> > > ---
> > > Changes in v2:
> > > - Suggested by Laurent. Split sequence++ and event init.
> > > - Link to v1: https://lore.kernel.org/r/20231020-uvc-event-v1-1-3baa0e9f6952@chromium.org
> > > ---
> > >  drivers/media/usb/uvc/uvc_v4l2.c  | 2 ++
> > >  drivers/media/usb/uvc/uvc_video.c | 7 +++++++
> > >  2 files changed, 9 insertions(+)
> > >
> > > diff --git a/drivers/media/usb/uvc/uvc_v4l2.c b/drivers/media/usb/uvc/uvc_v4l2.c
> > > index f4988f03640a..9f3fb5fd2375 100644
> > > --- a/drivers/media/usb/uvc/uvc_v4l2.c
> > > +++ b/drivers/media/usb/uvc/uvc_v4l2.c
> > > @@ -1352,6 +1352,8 @@ static int uvc_ioctl_subscribe_event(struct v4l2_fh *fh,
> > >       switch (sub->type) {
> > >       case V4L2_EVENT_CTRL:
> > >               return v4l2_event_subscribe(fh, sub, 0, &uvc_ctrl_sub_ev_ops);
> > > +     case V4L2_EVENT_FRAME_SYNC:
> > > +             return v4l2_event_subscribe(fh, sub, 0, NULL);
> > >       default:
> > >               return -EINVAL;
> > >       }
> > > diff --git a/drivers/media/usb/uvc/uvc_video.c b/drivers/media/usb/uvc/uvc_video.c
> > > index 28dde08ec6c5..4f3a510ca4fe 100644
> > > --- a/drivers/media/usb/uvc/uvc_video.c
> > > +++ b/drivers/media/usb/uvc/uvc_video.c
> > > @@ -1073,9 +1073,16 @@ static int uvc_video_decode_start(struct uvc_streaming *stream,
> > >        * that discontinuous sequence numbers always indicate lost frames.
> > >        */
> > >       if (stream->last_fid != fid) {
> > > +             struct v4l2_event event = {
> > > +                     .type = V4L2_EVENT_FRAME_SYNC,
> > > +             };
> > > +
> > >               stream->sequence++;
> > >               if (stream->sequence)
> > >                       uvc_video_stats_update(stream);
> > > +
> > > +             event.u.frame_sync.frame_sequence = stream->sequence,
> > > +             v4l2_event_queue(&stream->vdev, &event);
> >
> > uvc_video_decode_start() is called when the reception of the entire frame
> > has been completed. However, the documentation for V4L2_EVENT_FRAME_SYNC
> > says that the event is "Triggered immediately when the reception of a frame
> > has begun.". The functionality here doesn't seem to fit to this patch.
> >
> > Wouldn't V4L2_EVENT_VSYNC be a better fit, even if we don't really have a
> > concept of vertical sync in the case of USB? That event doesn't have the
> > sequence though but I guess it's not an issue at least if your case.
> >
> > Another technically correct option could be to create a new event for this
> > but I'm not sure it's worth it.
> >
> > >       }
> > >
> > >       uvc_video_clock_decode(stream, buf, data, len);
> > >
> >
> > --
> > Regards,
> >
> > Sakari Ailus
  
Ricardo Ribalda Nov. 7, 2023, 10:58 a.m. UTC | #5
Hi Sakari

On Tue, 7 Nov 2023 at 08:37, Sakari Ailus <sakari.ailus@iki.fi> wrote:
>
> Hi Esker,
>
> On Tue, Nov 07, 2023 at 01:06:20PM +0800, Esker Wong wrote:
> > [send again in text mode]
> > Hi Sakari,
> >
> > Sequence number is important to us. We need it to measure the latency
> > from this event to the time we display the frame.
>
> I think we could also add the sequence number to V4L2_EVENT_VSYNC.
>
> Cc Hans.
I think you forgot to add him :)

@Hans

What do you prefer?

- Use V4L2_EVENT_FRAME_SYNC because we trigger it as soon as the
driver knows about the frame
- Use V4L2_EVENT_VSYNC, but extending it to support the frame sequence
- Use a new EVENT

Thanks!


>
> > On Mon, Nov 6, 2023 at 7:06 PM Sakari Ailus <sakari.ailus@iki.fi> wrote:
> > >
> > > Hi Ricardo,
> > >
> > > On Mon, Nov 06, 2023 at 10:52:27AM +0000, Ricardo Ribalda wrote:
> > > > Add support for the frame_sync event, so user-space can become aware
> > > > earlier of new frames.
> > > >
> > > > Suggested-by: Esker Wong <esker@chromium.org>
> > > > Tested-by: Esker Wong <esker@chromium.org>
> > > > Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
> > > > ---
> > > > We have measured a latency of around 30msecs between frame sync
> > > > and dqbuf.
> > > > ---
> > > > Changes in v2:
> > > > - Suggested by Laurent. Split sequence++ and event init.
> > > > - Link to v1: https://lore.kernel.org/r/20231020-uvc-event-v1-1-3baa0e9f6952@chromium.org
> > > > ---
> > > >  drivers/media/usb/uvc/uvc_v4l2.c  | 2 ++
> > > >  drivers/media/usb/uvc/uvc_video.c | 7 +++++++
> > > >  2 files changed, 9 insertions(+)
> > > >
> > > > diff --git a/drivers/media/usb/uvc/uvc_v4l2.c b/drivers/media/usb/uvc/uvc_v4l2.c
> > > > index f4988f03640a..9f3fb5fd2375 100644
> > > > --- a/drivers/media/usb/uvc/uvc_v4l2.c
> > > > +++ b/drivers/media/usb/uvc/uvc_v4l2.c
> > > > @@ -1352,6 +1352,8 @@ static int uvc_ioctl_subscribe_event(struct v4l2_fh *fh,
> > > >       switch (sub->type) {
> > > >       case V4L2_EVENT_CTRL:
> > > >               return v4l2_event_subscribe(fh, sub, 0, &uvc_ctrl_sub_ev_ops);
> > > > +     case V4L2_EVENT_FRAME_SYNC:
> > > > +             return v4l2_event_subscribe(fh, sub, 0, NULL);
> > > >       default:
> > > >               return -EINVAL;
> > > >       }
> > > > diff --git a/drivers/media/usb/uvc/uvc_video.c b/drivers/media/usb/uvc/uvc_video.c
> > > > index 28dde08ec6c5..4f3a510ca4fe 100644
> > > > --- a/drivers/media/usb/uvc/uvc_video.c
> > > > +++ b/drivers/media/usb/uvc/uvc_video.c
> > > > @@ -1073,9 +1073,16 @@ static int uvc_video_decode_start(struct uvc_streaming *stream,
> > > >        * that discontinuous sequence numbers always indicate lost frames.
> > > >        */
> > > >       if (stream->last_fid != fid) {
> > > > +             struct v4l2_event event = {
> > > > +                     .type = V4L2_EVENT_FRAME_SYNC,
> > > > +             };
> > > > +
> > > >               stream->sequence++;
> > > >               if (stream->sequence)
> > > >                       uvc_video_stats_update(stream);
> > > > +
> > > > +             event.u.frame_sync.frame_sequence = stream->sequence,
> > > > +             v4l2_event_queue(&stream->vdev, &event);
> > >
> > > uvc_video_decode_start() is called when the reception of the entire frame
> > > has been completed. However, the documentation for V4L2_EVENT_FRAME_SYNC
> > > says that the event is "Triggered immediately when the reception of a frame
> > > has begun.". The functionality here doesn't seem to fit to this patch.
> > >
> > > Wouldn't V4L2_EVENT_VSYNC be a better fit, even if we don't really have a
> > > concept of vertical sync in the case of USB? That event doesn't have the
> > > sequence though but I guess it's not an issue at least if your case.
> > >
> > > Another technically correct option could be to create a new event for this
> > > but I'm not sure it's worth it.
> > >
> > > >       }
> > > >
> > > >       uvc_video_clock_decode(stream, buf, data, len);
> > > >
> > >
> > > --
> > > Regards,
> > >
> > > Sakari Ailus
>
> --
> Regards,
>
> Sakari Ailus
  
Nicolas Dufresne Nov. 7, 2023, 7:27 p.m. UTC | #6
Hi,

Le mardi 07 novembre 2023 à 13:06 +0800, Esker Wong a écrit :
> [send again in text mode]
> Hi Sakari,
> 
> Sequence number is important to us. We need it to measure the latency
> from this event to the time we display the frame.

how much precision do you expect, because as described, this number
will be completely false for bulk.

Aren't UVC timestamp support to allow measuring latency properly ?

Nicolas

> 
> Regards,
> Esker
> 
> 
> On Mon, Nov 6, 2023 at 7:06 PM Sakari Ailus <sakari.ailus@iki.fi> wrote:
> > 
> > Hi Ricardo,
> > 
> > On Mon, Nov 06, 2023 at 10:52:27AM +0000, Ricardo Ribalda wrote:
> > > Add support for the frame_sync event, so user-space can become aware
> > > earlier of new frames.
> > > 
> > > Suggested-by: Esker Wong <esker@chromium.org>
> > > Tested-by: Esker Wong <esker@chromium.org>
> > > Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
> > > ---
> > > We have measured a latency of around 30msecs between frame sync
> > > and dqbuf.
> > > ---
> > > Changes in v2:
> > > - Suggested by Laurent. Split sequence++ and event init.
> > > - Link to v1: https://lore.kernel.org/r/20231020-uvc-event-v1-1-3baa0e9f6952@chromium.org
> > > ---
> > >  drivers/media/usb/uvc/uvc_v4l2.c  | 2 ++
> > >  drivers/media/usb/uvc/uvc_video.c | 7 +++++++
> > >  2 files changed, 9 insertions(+)
> > > 
> > > diff --git a/drivers/media/usb/uvc/uvc_v4l2.c b/drivers/media/usb/uvc/uvc_v4l2.c
> > > index f4988f03640a..9f3fb5fd2375 100644
> > > --- a/drivers/media/usb/uvc/uvc_v4l2.c
> > > +++ b/drivers/media/usb/uvc/uvc_v4l2.c
> > > @@ -1352,6 +1352,8 @@ static int uvc_ioctl_subscribe_event(struct v4l2_fh *fh,
> > >       switch (sub->type) {
> > >       case V4L2_EVENT_CTRL:
> > >               return v4l2_event_subscribe(fh, sub, 0, &uvc_ctrl_sub_ev_ops);
> > > +     case V4L2_EVENT_FRAME_SYNC:
> > > +             return v4l2_event_subscribe(fh, sub, 0, NULL);
> > >       default:
> > >               return -EINVAL;
> > >       }
> > > diff --git a/drivers/media/usb/uvc/uvc_video.c b/drivers/media/usb/uvc/uvc_video.c
> > > index 28dde08ec6c5..4f3a510ca4fe 100644
> > > --- a/drivers/media/usb/uvc/uvc_video.c
> > > +++ b/drivers/media/usb/uvc/uvc_video.c
> > > @@ -1073,9 +1073,16 @@ static int uvc_video_decode_start(struct uvc_streaming *stream,
> > >        * that discontinuous sequence numbers always indicate lost frames.
> > >        */
> > >       if (stream->last_fid != fid) {
> > > +             struct v4l2_event event = {
> > > +                     .type = V4L2_EVENT_FRAME_SYNC,
> > > +             };
> > > +
> > >               stream->sequence++;
> > >               if (stream->sequence)
> > >                       uvc_video_stats_update(stream);
> > > +
> > > +             event.u.frame_sync.frame_sequence = stream->sequence,
> > > +             v4l2_event_queue(&stream->vdev, &event);
> > 
> > uvc_video_decode_start() is called when the reception of the entire frame
> > has been completed. However, the documentation for V4L2_EVENT_FRAME_SYNC
> > says that the event is "Triggered immediately when the reception of a frame
> > has begun.". The functionality here doesn't seem to fit to this patch.
> > 
> > Wouldn't V4L2_EVENT_VSYNC be a better fit, even if we don't really have a
> > concept of vertical sync in the case of USB? That event doesn't have the
> > sequence though but I guess it's not an issue at least if your case.
> > 
> > Another technically correct option could be to create a new event for this
> > but I'm not sure it's worth it.
> > 
> > >       }
> > > 
> > >       uvc_video_clock_decode(stream, buf, data, len);
> > > 
> > 
> > --
> > Regards,
> > 
> > Sakari Ailus
  
Esker Wong Nov. 8, 2023, 6:53 a.m. UTC | #7
Hi Nicholas and Sakari,

We need it as precise as possible. Currently the earliest time of a
frame we can have in userspace  is the dqbuf.

And for UVC timestamp, it is somewhat awkward for us to use. Since
other functions in our stacks do not necessarily contain such
timestamps. So we want some event to be trigger and we can get the
system time directly.

If the V4L2_EVENT_FRAME_SYNC will be earlier then V4L2_EVENT_VSYNC,
then it has value. We would want to know the delay of a frame being
captured to the time it is displayed.

I'm not sure for bulk is the V4L2_EVENT_VSYNC more accurate?

Esker


On Wed, Nov 8, 2023 at 3:27 AM <nicolas@ndufresne.ca> wrote:
>
> Hi,
>
> Le mardi 07 novembre 2023 à 13:06 +0800, Esker Wong a écrit :
> > [send again in text mode]
> > Hi Sakari,
> >
> > Sequence number is important to us. We need it to measure the latency
> > from this event to the time we display the frame.
>
> how much precision do you expect, because as described, this number
> will be completely false for bulk.
>
> Aren't UVC timestamp support to allow measuring latency properly ?
>
> Nicolas
>
> >
> > Regards,
> > Esker
> >
> >
> > On Mon, Nov 6, 2023 at 7:06 PM Sakari Ailus <sakari.ailus@iki.fi> wrote:
> > >
> > > Hi Ricardo,
> > >
> > > On Mon, Nov 06, 2023 at 10:52:27AM +0000, Ricardo Ribalda wrote:
> > > > Add support for the frame_sync event, so user-space can become aware
> > > > earlier of new frames.
> > > >
> > > > Suggested-by: Esker Wong <esker@chromium.org>
> > > > Tested-by: Esker Wong <esker@chromium.org>
> > > > Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
> > > > ---
> > > > We have measured a latency of around 30msecs between frame sync
> > > > and dqbuf.
> > > > ---
> > > > Changes in v2:
> > > > - Suggested by Laurent. Split sequence++ and event init.
> > > > - Link to v1: https://lore.kernel.org/r/20231020-uvc-event-v1-1-3baa0e9f6952@chromium.org
> > > > ---
> > > >  drivers/media/usb/uvc/uvc_v4l2.c  | 2 ++
> > > >  drivers/media/usb/uvc/uvc_video.c | 7 +++++++
> > > >  2 files changed, 9 insertions(+)
> > > >
> > > > diff --git a/drivers/media/usb/uvc/uvc_v4l2.c b/drivers/media/usb/uvc/uvc_v4l2.c
> > > > index f4988f03640a..9f3fb5fd2375 100644
> > > > --- a/drivers/media/usb/uvc/uvc_v4l2.c
> > > > +++ b/drivers/media/usb/uvc/uvc_v4l2.c
> > > > @@ -1352,6 +1352,8 @@ static int uvc_ioctl_subscribe_event(struct v4l2_fh *fh,
> > > >       switch (sub->type) {
> > > >       case V4L2_EVENT_CTRL:
> > > >               return v4l2_event_subscribe(fh, sub, 0, &uvc_ctrl_sub_ev_ops);
> > > > +     case V4L2_EVENT_FRAME_SYNC:
> > > > +             return v4l2_event_subscribe(fh, sub, 0, NULL);
> > > >       default:
> > > >               return -EINVAL;
> > > >       }
> > > > diff --git a/drivers/media/usb/uvc/uvc_video.c b/drivers/media/usb/uvc/uvc_video.c
> > > > index 28dde08ec6c5..4f3a510ca4fe 100644
> > > > --- a/drivers/media/usb/uvc/uvc_video.c
> > > > +++ b/drivers/media/usb/uvc/uvc_video.c
> > > > @@ -1073,9 +1073,16 @@ static int uvc_video_decode_start(struct uvc_streaming *stream,
> > > >        * that discontinuous sequence numbers always indicate lost frames.
> > > >        */
> > > >       if (stream->last_fid != fid) {
> > > > +             struct v4l2_event event = {
> > > > +                     .type = V4L2_EVENT_FRAME_SYNC,
> > > > +             };
> > > > +
> > > >               stream->sequence++;
> > > >               if (stream->sequence)
> > > >                       uvc_video_stats_update(stream);
> > > > +
> > > > +             event.u.frame_sync.frame_sequence = stream->sequence,
> > > > +             v4l2_event_queue(&stream->vdev, &event);
> > >
> > > uvc_video_decode_start() is called when the reception of the entire frame
> > > has been completed. However, the documentation for V4L2_EVENT_FRAME_SYNC
> > > says that the event is "Triggered immediately when the reception of a frame
> > > has begun.". The functionality here doesn't seem to fit to this patch.
> > >
> > > Wouldn't V4L2_EVENT_VSYNC be a better fit, even if we don't really have a
> > > concept of vertical sync in the case of USB? That event doesn't have the
> > > sequence though but I guess it's not an issue at least if your case.
> > >
> > > Another technically correct option could be to create a new event for this
> > > but I'm not sure it's worth it.
> > >
> > > >       }
> > > >
> > > >       uvc_video_clock_decode(stream, buf, data, len);
> > > >
> > >
> > > --
> > > Regards,
> > >
> > > Sakari Ailus
>
  
Ricardo Ribalda Nov. 8, 2023, 7:04 a.m. UTC | #8
Hi Esker

On Wed, 8 Nov 2023 at 07:54, Esker Wong <esker@google.com> wrote:
>
> Hi Nicholas and Sakari,
>
> We need it as precise as possible. Currently the earliest time of a
> frame we can have in userspace  is the dqbuf.
>
> And for UVC timestamp, it is somewhat awkward for us to use. Since
> other functions in our stacks do not necessarily contain such
> timestamps. So we want some event to be trigger and we can get the
> system time directly.

Not to mention that the UVC timestamping requires a bit of love.

@Laurent Pinchart, @Kieran Bingham  any progress reviewing :P :
https://patchwork.linuxtv.org/project/linux-media/list/?series=10083



>
> If the V4L2_EVENT_FRAME_SYNC will be earlier then V4L2_EVENT_VSYNC,
> then it has value. We would want to know the delay of a frame being
> captured to the time it is displayed.
>
> I'm not sure for bulk is the V4L2_EVENT_VSYNC more accurate?

 V4L2_EVENT_VSYNC wont be more accurate than V4L2_EVENT_FRAME_SYNC.

My understanding is that Sakari thinks that the description of
V4L2_EVENT_FRAME_SYNC
https://www.kernel.org/doc/html/v4.9/media/uapi/v4l/vidioc-dqevent.html#description
 does not match the current implementation, and suggests using
V4L2_EVENT_VSYNC instead.


>
> Esker
>
>
> On Wed, Nov 8, 2023 at 3:27 AM <nicolas@ndufresne.ca> wrote:
> >
> > Hi,
> >
> > Le mardi 07 novembre 2023 à 13:06 +0800, Esker Wong a écrit :
> > > [send again in text mode]
> > > Hi Sakari,
> > >
> > > Sequence number is important to us. We need it to measure the latency
> > > from this event to the time we display the frame.
> >
> > how much precision do you expect, because as described, this number
> > will be completely false for bulk.
> >
> > Aren't UVC timestamp support to allow measuring latency properly ?
> >
> > Nicolas
> >
> > >
> > > Regards,
> > > Esker
> > >
> > >
> > > On Mon, Nov 6, 2023 at 7:06 PM Sakari Ailus <sakari.ailus@iki.fi> wrote:
> > > >
> > > > Hi Ricardo,
> > > >
> > > > On Mon, Nov 06, 2023 at 10:52:27AM +0000, Ricardo Ribalda wrote:
> > > > > Add support for the frame_sync event, so user-space can become aware
> > > > > earlier of new frames.
> > > > >
> > > > > Suggested-by: Esker Wong <esker@chromium.org>
> > > > > Tested-by: Esker Wong <esker@chromium.org>
> > > > > Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
> > > > > ---
> > > > > We have measured a latency of around 30msecs between frame sync
> > > > > and dqbuf.
> > > > > ---
> > > > > Changes in v2:
> > > > > - Suggested by Laurent. Split sequence++ and event init.
> > > > > - Link to v1: https://lore.kernel.org/r/20231020-uvc-event-v1-1-3baa0e9f6952@chromium.org
> > > > > ---
> > > > >  drivers/media/usb/uvc/uvc_v4l2.c  | 2 ++
> > > > >  drivers/media/usb/uvc/uvc_video.c | 7 +++++++
> > > > >  2 files changed, 9 insertions(+)
> > > > >
> > > > > diff --git a/drivers/media/usb/uvc/uvc_v4l2.c b/drivers/media/usb/uvc/uvc_v4l2.c
> > > > > index f4988f03640a..9f3fb5fd2375 100644
> > > > > --- a/drivers/media/usb/uvc/uvc_v4l2.c
> > > > > +++ b/drivers/media/usb/uvc/uvc_v4l2.c
> > > > > @@ -1352,6 +1352,8 @@ static int uvc_ioctl_subscribe_event(struct v4l2_fh *fh,
> > > > >       switch (sub->type) {
> > > > >       case V4L2_EVENT_CTRL:
> > > > >               return v4l2_event_subscribe(fh, sub, 0, &uvc_ctrl_sub_ev_ops);
> > > > > +     case V4L2_EVENT_FRAME_SYNC:
> > > > > +             return v4l2_event_subscribe(fh, sub, 0, NULL);
> > > > >       default:
> > > > >               return -EINVAL;
> > > > >       }
> > > > > diff --git a/drivers/media/usb/uvc/uvc_video.c b/drivers/media/usb/uvc/uvc_video.c
> > > > > index 28dde08ec6c5..4f3a510ca4fe 100644
> > > > > --- a/drivers/media/usb/uvc/uvc_video.c
> > > > > +++ b/drivers/media/usb/uvc/uvc_video.c
> > > > > @@ -1073,9 +1073,16 @@ static int uvc_video_decode_start(struct uvc_streaming *stream,
> > > > >        * that discontinuous sequence numbers always indicate lost frames.
> > > > >        */
> > > > >       if (stream->last_fid != fid) {
> > > > > +             struct v4l2_event event = {
> > > > > +                     .type = V4L2_EVENT_FRAME_SYNC,
> > > > > +             };
> > > > > +
> > > > >               stream->sequence++;
> > > > >               if (stream->sequence)
> > > > >                       uvc_video_stats_update(stream);
> > > > > +
> > > > > +             event.u.frame_sync.frame_sequence = stream->sequence,
> > > > > +             v4l2_event_queue(&stream->vdev, &event);
> > > >
> > > > uvc_video_decode_start() is called when the reception of the entire frame
> > > > has been completed. However, the documentation for V4L2_EVENT_FRAME_SYNC
> > > > says that the event is "Triggered immediately when the reception of a frame
> > > > has begun.". The functionality here doesn't seem to fit to this patch.
> > > >
> > > > Wouldn't V4L2_EVENT_VSYNC be a better fit, even if we don't really have a
> > > > concept of vertical sync in the case of USB? That event doesn't have the
> > > > sequence though but I guess it's not an issue at least if your case.
> > > >
> > > > Another technically correct option could be to create a new event for this
> > > > but I'm not sure it's worth it.
> > > >
> > > > >       }
> > > > >
> > > > >       uvc_video_clock_decode(stream, buf, data, len);
> > > > >
> > > >
> > > > --
> > > > Regards,
> > > >
> > > > Sakari Ailus
> >
  
Nicolas Dufresne Nov. 8, 2023, 8:32 p.m. UTC | #9
Le mercredi 08 novembre 2023 à 08:04 +0100, Ricardo Ribalda a écrit :
> Hi Esker
> 
> On Wed, 8 Nov 2023 at 07:54, Esker Wong <esker@google.com> wrote:
> > 
> > Hi Nicholas and Sakari,
> > 
> > We need it as precise as possible. Currently the earliest time of a
> > frame we can have in userspace  is the dqbuf.
> > 
> > And for UVC timestamp, it is somewhat awkward for us to use. Since
> > other functions in our stacks do not necessarily contain such
> > timestamps. So we want some event to be trigger and we can get the
> > system time directly.

The fact that you interpret the time from FRAME_SYNC to DQBUF (well the
READ IO notification) as the actual latency is yours of course. It
assumes that the camera on the other end does not introduce other
source of latency (or that these are negligible). You are also going to
introduce a lot of jitter, since it relies on when the OS decides to
wake up your process.

I think my opinion resides in if you can accurately *enough* implement
what the spec says for FRAME_SYNC then do it, otherwise just don't lie.
I think for ISO, "after the first chunk" i a small lie, but acceptable.
But for BULK, the way it was explained is that it will be always very
close to DQBUF time. and it should not emit FRAME_SYNC for this type of
UVC device. If it fits other events fine of course, I'm just making a
judgment on if its fits V4L2_EVENT_FRAME_SYNC or not.

In term of accuracy, if timestamp was passed with the FRAME_SYNC event,
it would not matter how fast your process the event anymore and greatly
improve accuracy.

> 
> Not to mention that the UVC timestamping requires a bit of love.
> 
> @Laurent Pinchart, @Kieran Bingham  any progress reviewing :P :
> https://patchwork.linuxtv.org/project/linux-media/list/?series=10083

Thanks for working on this by the way, hope someone will find the time
to review this. The timestamps should in theory provide a jitter free
measurement of the delay Esker is trying to measure, and if it wasn't
of bugs (and crazy complexity) it would in the worst case match the
transfer time.

Nicolas

> Esker
> 
> 
> > 
> > If the V4L2_EVENT_FRAME_SYNC will be earlier then V4L2_EVENT_VSYNC,
> > then it has value. We would want to know the delay of a frame being
> > captured to the time it is displayed.
> > 
> > I'm not sure for bulk is the V4L2_EVENT_VSYNC more accurate?
> 
>  V4L2_EVENT_VSYNC wont be more accurate than V4L2_EVENT_FRAME_SYNC.
> 
> My understanding is that Sakari thinks that the description of
> V4L2_EVENT_FRAME_SYNC
> https://www.kernel.org/doc/html/v4.9/media/uapi/v4l/vidioc-dqevent.html#description
>  does not match the current implementation, and suggests using
> V4L2_EVENT_VSYNC instead.
> 
> 
> > 
> > Esker
> > 
> > 
> > On Wed, Nov 8, 2023 at 3:27 AM <nicolas@ndufresne.ca> wrote:
> > > 
> > > Hi,
> > > 
> > > Le mardi 07 novembre 2023 à 13:06 +0800, Esker Wong a écrit :
> > > > [send again in text mode]
> > > > Hi Sakari,
> > > > 
> > > > Sequence number is important to us. We need it to measure the latency
> > > > from this event to the time we display the frame.
> > > 
> > > how much precision do you expect, because as described, this number
> > > will be completely false for bulk.
> > > 
> > > Aren't UVC timestamp support to allow measuring latency properly ?
> > > 
> > > Nicolas
> > > 
> > > > 
> > > > Regards,
> > > > Esker
> > > > 
> > > > 
> > > > On Mon, Nov 6, 2023 at 7:06 PM Sakari Ailus <sakari.ailus@iki.fi> wrote:
> > > > > 
> > > > > Hi Ricardo,
> > > > > 
> > > > > On Mon, Nov 06, 2023 at 10:52:27AM +0000, Ricardo Ribalda wrote:
> > > > > > Add support for the frame_sync event, so user-space can become aware
> > > > > > earlier of new frames.
> > > > > > 
> > > > > > Suggested-by: Esker Wong <esker@chromium.org>
> > > > > > Tested-by: Esker Wong <esker@chromium.org>
> > > > > > Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
> > > > > > ---
> > > > > > We have measured a latency of around 30msecs between frame sync
> > > > > > and dqbuf.
> > > > > > ---
> > > > > > Changes in v2:
> > > > > > - Suggested by Laurent. Split sequence++ and event init.
> > > > > > - Link to v1: https://lore.kernel.org/r/20231020-uvc-event-v1-1-3baa0e9f6952@chromium.org
> > > > > > ---
> > > > > >  drivers/media/usb/uvc/uvc_v4l2.c  | 2 ++
> > > > > >  drivers/media/usb/uvc/uvc_video.c | 7 +++++++
> > > > > >  2 files changed, 9 insertions(+)
> > > > > > 
> > > > > > diff --git a/drivers/media/usb/uvc/uvc_v4l2.c b/drivers/media/usb/uvc/uvc_v4l2.c
> > > > > > index f4988f03640a..9f3fb5fd2375 100644
> > > > > > --- a/drivers/media/usb/uvc/uvc_v4l2.c
> > > > > > +++ b/drivers/media/usb/uvc/uvc_v4l2.c
> > > > > > @@ -1352,6 +1352,8 @@ static int uvc_ioctl_subscribe_event(struct v4l2_fh *fh,
> > > > > >       switch (sub->type) {
> > > > > >       case V4L2_EVENT_CTRL:
> > > > > >               return v4l2_event_subscribe(fh, sub, 0, &uvc_ctrl_sub_ev_ops);
> > > > > > +     case V4L2_EVENT_FRAME_SYNC:
> > > > > > +             return v4l2_event_subscribe(fh, sub, 0, NULL);
> > > > > >       default:
> > > > > >               return -EINVAL;
> > > > > >       }
> > > > > > diff --git a/drivers/media/usb/uvc/uvc_video.c b/drivers/media/usb/uvc/uvc_video.c
> > > > > > index 28dde08ec6c5..4f3a510ca4fe 100644
> > > > > > --- a/drivers/media/usb/uvc/uvc_video.c
> > > > > > +++ b/drivers/media/usb/uvc/uvc_video.c
> > > > > > @@ -1073,9 +1073,16 @@ static int uvc_video_decode_start(struct uvc_streaming *stream,
> > > > > >        * that discontinuous sequence numbers always indicate lost frames.
> > > > > >        */
> > > > > >       if (stream->last_fid != fid) {
> > > > > > +             struct v4l2_event event = {
> > > > > > +                     .type = V4L2_EVENT_FRAME_SYNC,
> > > > > > +             };
> > > > > > +
> > > > > >               stream->sequence++;
> > > > > >               if (stream->sequence)
> > > > > >                       uvc_video_stats_update(stream);
> > > > > > +
> > > > > > +             event.u.frame_sync.frame_sequence = stream->sequence,
> > > > > > +             v4l2_event_queue(&stream->vdev, &event);
> > > > > 
> > > > > uvc_video_decode_start() is called when the reception of the entire frame
> > > > > has been completed. However, the documentation for V4L2_EVENT_FRAME_SYNC
> > > > > says that the event is "Triggered immediately when the reception of a frame
> > > > > has begun.". The functionality here doesn't seem to fit to this patch.
> > > > > 
> > > > > Wouldn't V4L2_EVENT_VSYNC be a better fit, even if we don't really have a
> > > > > concept of vertical sync in the case of USB? That event doesn't have the
> > > > > sequence though but I guess it's not an issue at least if your case.
> > > > > 
> > > > > Another technically correct option could be to create a new event for this
> > > > > but I'm not sure it's worth it.
> > > > > 
> > > > > >       }
> > > > > > 
> > > > > >       uvc_video_clock_decode(stream, buf, data, len);
> > > > > > 
> > > > > 
> > > > > --
> > > > > Regards,
> > > > > 
> > > > > Sakari Ailus
> > > 
> 
> 
>
  
Laurent Pinchart Nov. 8, 2023, 10:10 p.m. UTC | #10
Hello,

On Wed, Nov 08, 2023 at 03:32:23PM -0500, nicolas@ndufresne.ca wrote:
> Le mercredi 08 novembre 2023 à 08:04 +0100, Ricardo Ribalda a écrit :
> > On Wed, 8 Nov 2023 at 07:54, Esker Wong wrote:
> > > 
> > > Hi Nicholas and Sakari,
> > > 
> > > We need it as precise as possible. Currently the earliest time of a
> > > frame we can have in userspace  is the dqbuf.
> > > 
> > > And for UVC timestamp, it is somewhat awkward for us to use. Since
> > > other functions in our stacks do not necessarily contain such
> > > timestamps. So we want some event to be trigger and we can get the
> > > system time directly.
> 
> The fact that you interpret the time from FRAME_SYNC to DQBUF (well the
> READ IO notification) as the actual latency is yours of course. It
> assumes that the camera on the other end does not introduce other
> source of latency (or that these are negligible). You are also going to
> introduce a lot of jitter, since it relies on when the OS decides to
> wake up your process.
> 
> I think my opinion resides in if you can accurately *enough* implement
> what the spec says for FRAME_SYNC then do it, otherwise just don't lie.
> I think for ISO, "after the first chunk" i a small lie, but acceptable.
> But for BULK, the way it was explained is that it will be always very
> close to DQBUF time. and it should not emit FRAME_SYNC for this type of
> UVC device. If it fits other events fine of course, I'm just making a
> judgment on if its fits V4L2_EVENT_FRAME_SYNC or not.

I agree. V4L2_EVENT_FRAME_SYNC should be fine for isoc-based devices as
it should be "close enough" to the start of frame. For bulk it woul dbe
too much of a lie, so I would not emit it for bulk-based devices.

> In term of accuracy, if timestamp was passed with the FRAME_SYNC event,
> it would not matter how fast your process the event anymore and greatly
> improve accuracy.
> 
> > Not to mention that the UVC timestamping requires a bit of love.
> > 
> > @Laurent Pinchart, @Kieran Bingham  any progress reviewing :P :
> > https://patchwork.linuxtv.org/project/linux-media/list/?series=10083
> 
> Thanks for working on this by the way, hope someone will find the time
> to review this. The timestamps should in theory provide a jitter free
> measurement of the delay Esker is trying to measure, and if it wasn't
> of bugs (and crazy complexity) it would in the worst case match the
> transfer time.

Assuming the device firmware isn't too buggy, the UVC timestamps should
indeed provide much better accuracy than when V4L2_EVENT_FRAME_SYNC
could give. I think the biggest problem will be to figure out if a
particular device can be trusted.

> > > If the V4L2_EVENT_FRAME_SYNC will be earlier then V4L2_EVENT_VSYNC,
> > > then it has value. We would want to know the delay of a frame being
> > > captured to the time it is displayed.
> > > 
> > > I'm not sure for bulk is the V4L2_EVENT_VSYNC more accurate?
> > 
> >  V4L2_EVENT_VSYNC wont be more accurate than V4L2_EVENT_FRAME_SYNC.
> > 
> > My understanding is that Sakari thinks that the description of
> > V4L2_EVENT_FRAME_SYNC
> > https://www.kernel.org/doc/html/v4.9/media/uapi/v4l/vidioc-dqevent.html#description
> >  does not match the current implementation, and suggests using
> > V4L2_EVENT_VSYNC instead.
> > 
> > > On Wed, Nov 8, 2023 at 3:27 AM <nicolas@ndufresne.ca> wrote:
> > > > Le mardi 07 novembre 2023 à 13:06 +0800, Esker Wong a écrit :
> > > > > [send again in text mode]
> > > > > Hi Sakari,
> > > > > 
> > > > > Sequence number is important to us. We need it to measure the latency
> > > > > from this event to the time we display the frame.
> > > > 
> > > > how much precision do you expect, because as described, this number
> > > > will be completely false for bulk.
> > > > 
> > > > Aren't UVC timestamp support to allow measuring latency properly ?
> > > > 
> > > > > On Mon, Nov 6, 2023 at 7:06 PM Sakari Ailus wrote:
> > > > > > On Mon, Nov 06, 2023 at 10:52:27AM +0000, Ricardo Ribalda wrote:
> > > > > > > Add support for the frame_sync event, so user-space can become aware
> > > > > > > earlier of new frames.
> > > > > > > 
> > > > > > > Suggested-by: Esker Wong <esker@chromium.org>
> > > > > > > Tested-by: Esker Wong <esker@chromium.org>
> > > > > > > Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
> > > > > > > ---
> > > > > > > We have measured a latency of around 30msecs between frame sync
> > > > > > > and dqbuf.
> > > > > > > ---
> > > > > > > Changes in v2:
> > > > > > > - Suggested by Laurent. Split sequence++ and event init.
> > > > > > > - Link to v1: https://lore.kernel.org/r/20231020-uvc-event-v1-1-3baa0e9f6952@chromium.org
> > > > > > > ---
> > > > > > >  drivers/media/usb/uvc/uvc_v4l2.c  | 2 ++
> > > > > > >  drivers/media/usb/uvc/uvc_video.c | 7 +++++++
> > > > > > >  2 files changed, 9 insertions(+)
> > > > > > > 
> > > > > > > diff --git a/drivers/media/usb/uvc/uvc_v4l2.c b/drivers/media/usb/uvc/uvc_v4l2.c
> > > > > > > index f4988f03640a..9f3fb5fd2375 100644
> > > > > > > --- a/drivers/media/usb/uvc/uvc_v4l2.c
> > > > > > > +++ b/drivers/media/usb/uvc/uvc_v4l2.c
> > > > > > > @@ -1352,6 +1352,8 @@ static int uvc_ioctl_subscribe_event(struct v4l2_fh *fh,
> > > > > > >       switch (sub->type) {
> > > > > > >       case V4L2_EVENT_CTRL:
> > > > > > >               return v4l2_event_subscribe(fh, sub, 0, &uvc_ctrl_sub_ev_ops);
> > > > > > > +     case V4L2_EVENT_FRAME_SYNC:
> > > > > > > +             return v4l2_event_subscribe(fh, sub, 0, NULL);
> > > > > > >       default:
> > > > > > >               return -EINVAL;
> > > > > > >       }
> > > > > > > diff --git a/drivers/media/usb/uvc/uvc_video.c b/drivers/media/usb/uvc/uvc_video.c
> > > > > > > index 28dde08ec6c5..4f3a510ca4fe 100644
> > > > > > > --- a/drivers/media/usb/uvc/uvc_video.c
> > > > > > > +++ b/drivers/media/usb/uvc/uvc_video.c
> > > > > > > @@ -1073,9 +1073,16 @@ static int uvc_video_decode_start(struct uvc_streaming *stream,
> > > > > > >        * that discontinuous sequence numbers always indicate lost frames.
> > > > > > >        */
> > > > > > >       if (stream->last_fid != fid) {
> > > > > > > +             struct v4l2_event event = {
> > > > > > > +                     .type = V4L2_EVENT_FRAME_SYNC,
> > > > > > > +             };
> > > > > > > +
> > > > > > >               stream->sequence++;
> > > > > > >               if (stream->sequence)
> > > > > > >                       uvc_video_stats_update(stream);
> > > > > > > +
> > > > > > > +             event.u.frame_sync.frame_sequence = stream->sequence,
> > > > > > > +             v4l2_event_queue(&stream->vdev, &event);
> > > > > > 
> > > > > > uvc_video_decode_start() is called when the reception of the entire frame
> > > > > > has been completed. However, the documentation for V4L2_EVENT_FRAME_SYNC
> > > > > > says that the event is "Triggered immediately when the reception of a frame
> > > > > > has begun.". The functionality here doesn't seem to fit to this patch.
> > > > > > 
> > > > > > Wouldn't V4L2_EVENT_VSYNC be a better fit, even if we don't really have a
> > > > > > concept of vertical sync in the case of USB? That event doesn't have the
> > > > > > sequence though but I guess it's not an issue at least if your case.
> > > > > > 
> > > > > > Another technically correct option could be to create a new event for this
> > > > > > but I'm not sure it's worth it.
> > > > > > 
> > > > > > >       }
> > > > > > > 
> > > > > > >       uvc_video_clock_decode(stream, buf, data, len);
> > > > > > >
  
Ricardo Ribalda Nov. 8, 2023, 10:46 p.m. UTC | #11
Hi Nicolas

On Wed, 8 Nov 2023 at 21:32, <nicolas@ndufresne.ca> wrote:
>
> The fact that you interpret the time from FRAME_SYNC to DQBUF (well the
> READ IO notification) as the actual latency is yours of course. It
> assumes that the camera on the other end does not introduce other

We want to use this signal to measure how much power is used since we
start receiving the frame until we can use it.
I agree with you that the latency between capture and dqbuf should be
measured using the timestamp. That is not our use case here.

> source of latency (or that these are negligible). You are also going to
> introduce a lot of jitter, since it relies on when the OS decides to
> wake up your process.

We have measured a jitter of around 2.5 msec, which is acceptable for our needs.

>
> I think my opinion resides in if you can accurately *enough* implement
> what the spec says for FRAME_SYNC then do it, otherwise just don't lie.

What the specs says is:
```
Triggered immediately when the reception of a frame has begun
```
In my opinion, that is true for usb devices, we are triggering it as
soon as the transfer has started to the eyes of the driver. We cannot
trigger earlier than that.


> I think for ISO, "after the first chunk" i a small lie, but acceptable.
> But for BULK, the way it was explained is that it will be always very
> close to DQBUF time. and it should not emit FRAME_SYNC for this type of
> UVC device. If it fits other events fine of course, I'm just making a
> judgment on if its fits V4L2_EVENT_FRAME_SYNC or not.

nit: I believe that you have swapped iso and bulk on this description

>
> In term of accuracy, if timestamp was passed with the FRAME_SYNC event,
> it would not matter how fast your process the event anymore and greatly
> improve accuracy.

+1 to that. If we could easily change the uAPI for FRAME_SYNC that
should definitely be implemented.

>
> >
> > Not to mention that the UVC timestamping requires a bit of love.
> >
> > @Laurent Pinchart, @Kieran Bingham  any progress reviewing :P :
> > https://patchwork.linuxtv.org/project/linux-media/list/?series=10083
>
> Thanks for working on this by the way, hope someone will find the time
> to review this. The timestamps should in theory provide a jitter free

It already has a couple of Reviewed-by stamped in.... ;)

> measurement of the delay Esker is trying to measure, and if it wasn't
> of bugs (and crazy complexity) it would in the worst case match the
> transfer time.

Sorry to repeat myself, but just to avoid the confusion: Esker needs
to know how much power is used since we start receiving a frame until
it is available for dqbuf, not de frame latency.

Regards!
>
> Nicolas
>
  
Laurent Pinchart Nov. 9, 2023, 12:03 a.m. UTC | #12
Hi Ricardo,

On Wed, Nov 08, 2023 at 11:46:40PM +0100, Ricardo Ribalda wrote:
> On Wed, 8 Nov 2023 at 21:32, <nicolas@ndufresne.ca> wrote:
> >
> > The fact that you interpret the time from FRAME_SYNC to DQBUF (well the
> > READ IO notification) as the actual latency is yours of course. It
> > assumes that the camera on the other end does not introduce other
> 
> We want to use this signal to measure how much power is used since we
> start receiving the frame until we can use it.
> I agree with you that the latency between capture and dqbuf should be
> measured using the timestamp. That is not our use case here.
> 
> > source of latency (or that these are negligible). You are also going to
> > introduce a lot of jitter, since it relies on when the OS decides to
> > wake up your process.
> 
> We have measured a jitter of around 2.5 msec, which is acceptable for our needs.
> 
> > I think my opinion resides in if you can accurately *enough* implement
> > what the spec says for FRAME_SYNC then do it, otherwise just don't lie.
> 
> What the specs says is:
> ```
> Triggered immediately when the reception of a frame has begun
> ```
> In my opinion, that is true for usb devices, we are triggering it as
> soon as the transfer has started to the eyes of the driver. We cannot
> trigger earlier than that.
> 
> 
> > I think for ISO, "after the first chunk" i a small lie, but acceptable.
> > But for BULK, the way it was explained is that it will be always very
> > close to DQBUF time. and it should not emit FRAME_SYNC for this type of
> > UVC device. If it fits other events fine of course, I'm just making a
> > judgment on if its fits V4L2_EVENT_FRAME_SYNC or not.
> 
> nit: I believe that you have swapped iso and bulk on this description

I've confused the USB packet size and the UVC payload size. The latter
is typically much bigger for bulk devices than isoc devices, but the
former will be in similar order of magnitudes in a large number of
cases, but not all cases.

The URB size is the result of the USB packet size and number of packets
per URB. The uvcvideo driver currently sets the number of packets per
URB to 32 at most (and lowers it if the frame size is small, or if not
enough memory can be allocated). This could be increased or made dynamic
in the future, as higher speeds typically benefit from larger URB sizes.
The packet size differs between bulk and isoc endpoints.

For bulk, the packet size can be up to 512 bytes for USB 2.0 and 1024
bytes for USB 3.0, and the device can select a smaller size. The largest
URB size (again based on the current implementation of the uvcvideo
driver) is thus 32 KiB.

For isochronous the situation is more complicated. The term "packet" as
used in the uvcvideo driver actually means all the data transferred in
one service interval, thus made of multiple isoc packets. It is heavily
dependent on the USB speed, and the device can advertise different
supported sizes (which translate directly to the reserved bandwidth for
the transfer), with the driver picking the smallest bandwidth large
enough for the data rate required by the resolution and frame rate. The
theoretical worst case is 1024 bytes per isoc packet * 16 isoc packets
per burst * 6 burst per interval * 32 "packets" per URB, equal to 3 MiB.

Even with the largest URB size you have witnessed of ~1 MiB, we will end
up lying quite a bit if we consider the URB completion callback for the
first URB of the frame as indicating the start of reception.

> > In term of accuracy, if timestamp was passed with the FRAME_SYNC event,
> > it would not matter how fast your process the event anymore and greatly
> > improve accuracy.
> 
> +1 to that. If we could easily change the uAPI for FRAME_SYNC that
> should definitely be implemented.
> 
> > > Not to mention that the UVC timestamping requires a bit of love.
> > >
> > > @Laurent Pinchart, @Kieran Bingham  any progress reviewing :P :
> > > https://patchwork.linuxtv.org/project/linux-media/list/?series=10083
> >
> > Thanks for working on this by the way, hope someone will find the time
> > to review this. The timestamps should in theory provide a jitter free
> 
> It already has a couple of Reviewed-by stamped in.... ;)
> 
> > measurement of the delay Esker is trying to measure, and if it wasn't
> > of bugs (and crazy complexity) it would in the worst case match the
> > transfer time.
> 
> Sorry to repeat myself, but just to avoid the confusion: Esker needs
> to know how much power is used since we start receiving a frame until
> it is available for dqbuf, not de frame latency.

As I think everybody is aware, the earliest notification you get on the
CPU side is the *end* of reception of the first URB, which can possibly
be significantly later than the start of reception of the frame.

Based on what I understand, the goal is to measure the CPU power
consumption related to CPU processing of the frame. If that's the case,
there's good and bad news. The good news is that the CPU doesn't process
the frame at all until the URB has been received (if you were to measure
the power consumption of the USB host controller too, it would be a
different story), so the delay shouldn't be a problem. The bad news is
that I don't see how the information you're trying to get will help you,
as there's plenty of other things unrelated to the uvcvideo driver that
can take CPU time while a frame is being received. That may not be any
of my business, but from the point of view of the uvcvideo driver, I'm
less inclined to accept a possibly significant V4L2_EVENT_FRAME_SYNC lie
if the use case ends up making little sense :-)
  
Ricardo Ribalda Nov. 9, 2023, 12:27 a.m. UTC | #13
Hi Laurent

On Thu, 9 Nov 2023 at 01:03, Laurent Pinchart
<laurent.pinchart@ideasonboard.com> wrote:
>
> Hi Ricardo,
>
> On Wed, Nov 08, 2023 at 11:46:40PM +0100, Ricardo Ribalda wrote:
> > On Wed, 8 Nov 2023 at 21:32, <nicolas@ndufresne.ca> wrote:
> > >
> > > The fact that you interpret the time from FRAME_SYNC to DQBUF (well the
> > > READ IO notification) as the actual latency is yours of course. It
> > > assumes that the camera on the other end does not introduce other
> >
> > We want to use this signal to measure how much power is used since we
> > start receiving the frame until we can use it.
> > I agree with you that the latency between capture and dqbuf should be
> > measured using the timestamp. That is not our use case here.
> >
> > > source of latency (or that these are negligible). You are also going to
> > > introduce a lot of jitter, since it relies on when the OS decides to
> > > wake up your process.
> >
> > We have measured a jitter of around 2.5 msec, which is acceptable for our needs.
> >
> > > I think my opinion resides in if you can accurately *enough* implement
> > > what the spec says for FRAME_SYNC then do it, otherwise just don't lie.
> >
> > What the specs says is:
> > ```
> > Triggered immediately when the reception of a frame has begun
> > ```
> > In my opinion, that is true for usb devices, we are triggering it as
> > soon as the transfer has started to the eyes of the driver. We cannot
> > trigger earlier than that.
> >
> >
> > > I think for ISO, "after the first chunk" i a small lie, but acceptable.
> > > But for BULK, the way it was explained is that it will be always very
> > > close to DQBUF time. and it should not emit FRAME_SYNC for this type of
> > > UVC device. If it fits other events fine of course, I'm just making a
> > > judgment on if its fits V4L2_EVENT_FRAME_SYNC or not.
> >
> > nit: I believe that you have swapped iso and bulk on this description
>
> I've confused the USB packet size and the UVC payload size. The latter
> is typically much bigger for bulk devices than isoc devices, but the
> former will be in similar order of magnitudes in a large number of
> cases, but not all cases.
>
> The URB size is the result of the USB packet size and number of packets
> per URB. The uvcvideo driver currently sets the number of packets per
> URB to 32 at most (and lowers it if the frame size is small, or if not
> enough memory can be allocated). This could be increased or made dynamic
> in the future, as higher speeds typically benefit from larger URB sizes.
> The packet size differs between bulk and isoc endpoints.
>
> For bulk, the packet size can be up to 512 bytes for USB 2.0 and 1024
> bytes for USB 3.0, and the device can select a smaller size. The largest
> URB size (again based on the current implementation of the uvcvideo
> driver) is thus 32 KiB.
>
> For isochronous the situation is more complicated. The term "packet" as
> used in the uvcvideo driver actually means all the data transferred in
> one service interval, thus made of multiple isoc packets. It is heavily
> dependent on the USB speed, and the device can advertise different
> supported sizes (which translate directly to the reserved bandwidth for
> the transfer), with the driver picking the smallest bandwidth large
> enough for the data rate required by the resolution and frame rate. The
> theoretical worst case is 1024 bytes per isoc packet * 16 isoc packets
> per burst * 6 burst per interval * 32 "packets" per URB, equal to 3 MiB.
>
> Even with the largest URB size you have witnessed of ~1 MiB, we will end
> up lying quite a bit if we consider the URB completion callback for the
> first URB of the frame as indicating the start of reception.
>
> > > In term of accuracy, if timestamp was passed with the FRAME_SYNC event,
> > > it would not matter how fast your process the event anymore and greatly
> > > improve accuracy.
> >
> > +1 to that. If we could easily change the uAPI for FRAME_SYNC that
> > should definitely be implemented.
> >
> > > > Not to mention that the UVC timestamping requires a bit of love.
> > > >
> > > > @Laurent Pinchart, @Kieran Bingham  any progress reviewing :P :
> > > > https://patchwork.linuxtv.org/project/linux-media/list/?series=10083
> > >
> > > Thanks for working on this by the way, hope someone will find the time
> > > to review this. The timestamps should in theory provide a jitter free
> >
> > It already has a couple of Reviewed-by stamped in.... ;)
> >
> > > measurement of the delay Esker is trying to measure, and if it wasn't
> > > of bugs (and crazy complexity) it would in the worst case match the
> > > transfer time.
> >
> > Sorry to repeat myself, but just to avoid the confusion: Esker needs
> > to know how much power is used since we start receiving a frame until
> > it is available for dqbuf, not de frame latency.
>
> As I think everybody is aware, the earliest notification you get on the
> CPU side is the *end* of reception of the first URB, which can possibly
> be significantly later than the start of reception of the frame.
>
> Based on what I understand, the goal is to measure the CPU power
> consumption related to CPU processing of the frame. If that's the case,
> there's good and bad news. The good news is that the CPU doesn't process
> the frame at all until the URB has been received (if you were to measure
> the power consumption of the USB host controller too, it would be a
> different story), so the delay shouldn't be a problem. The bad news is
> that I don't see how the information you're trying to get will help you,
> as there's plenty of other things unrelated to the uvcvideo driver that
> can take CPU time while a frame is being received. That may not be any
> of my business, but from the point of view of the uvcvideo driver, I'm
> less inclined to accept a possibly significant V4L2_EVENT_FRAME_SYNC lie
> if the use case ends up making little sense :-)

Just to add some numbers to add some context:

 V4L2_EVENT_FRAME_SYNC for BULK and ISO will be delayed from:
```
Triggered immediately when the reception of a frame has begun
```
one urb.

For bulk devices this is a maximum of 0.05 msec (32KiB/600MBps)
For 1MiB transfer isoc devices (which is the biggest we have seen),
that is 1.8 msec.
In both cases, this is smaller than the jitter to process the event
itself by userspace.

The time from V4L2_EVENT_FRAME_SYNC to DQBUF is around 30 msec.

I do not know how much delay is considered acceptable... but if we
take the delay argument to the extreme, we could say that
V4L2_EVENT_FRAME_SYNC can never be implemented, because the event will
always be delayed by something.

Regards

>
> --
> Regards,
>
> Laurent Pinchart
  
Esker Wong Nov. 9, 2023, 12:59 a.m. UTC | #14
Hi Laurent,

The use case here we want is actually the latency. We want to know
what is the most accurate delay the user will feel when the camera
capture the frame to the frame get displayed. So anytime earlier then
the dqbuf should be an improvement here. It would help us know better
of a camera quality.

Regards,
Esker

On Thu, Nov 9, 2023 at 8:03 AM Laurent Pinchart
<laurent.pinchart@ideasonboard.com> wrote:
>
> Hi Ricardo,
>
> On Wed, Nov 08, 2023 at 11:46:40PM +0100, Ricardo Ribalda wrote:
> > On Wed, 8 Nov 2023 at 21:32, <nicolas@ndufresne.ca> wrote:
> > >
> > > The fact that you interpret the time from FRAME_SYNC to DQBUF (well the
> > > READ IO notification) as the actual latency is yours of course. It
> > > assumes that the camera on the other end does not introduce other
> >
> > We want to use this signal to measure how much power is used since we
> > start receiving the frame until we can use it.
> > I agree with you that the latency between capture and dqbuf should be
> > measured using the timestamp. That is not our use case here.
> >
> > > source of latency (or that these are negligible). You are also going to
> > > introduce a lot of jitter, since it relies on when the OS decides to
> > > wake up your process.
> >
> > We have measured a jitter of around 2.5 msec, which is acceptable for our needs.
> >
> > > I think my opinion resides in if you can accurately *enough* implement
> > > what the spec says for FRAME_SYNC then do it, otherwise just don't lie.
> >
> > What the specs says is:
> > ```
> > Triggered immediately when the reception of a frame has begun
> > ```
> > In my opinion, that is true for usb devices, we are triggering it as
> > soon as the transfer has started to the eyes of the driver. We cannot
> > trigger earlier than that.
> >
> >
> > > I think for ISO, "after the first chunk" i a small lie, but acceptable.
> > > But for BULK, the way it was explained is that it will be always very
> > > close to DQBUF time. and it should not emit FRAME_SYNC for this type of
> > > UVC device. If it fits other events fine of course, I'm just making a
> > > judgment on if its fits V4L2_EVENT_FRAME_SYNC or not.
> >
> > nit: I believe that you have swapped iso and bulk on this description
>
> I've confused the USB packet size and the UVC payload size. The latter
> is typically much bigger for bulk devices than isoc devices, but the
> former will be in similar order of magnitudes in a large number of
> cases, but not all cases.
>
> The URB size is the result of the USB packet size and number of packets
> per URB. The uvcvideo driver currently sets the number of packets per
> URB to 32 at most (and lowers it if the frame size is small, or if not
> enough memory can be allocated). This could be increased or made dynamic
> in the future, as higher speeds typically benefit from larger URB sizes.
> The packet size differs between bulk and isoc endpoints.
>
> For bulk, the packet size can be up to 512 bytes for USB 2.0 and 1024
> bytes for USB 3.0, and the device can select a smaller size. The largest
> URB size (again based on the current implementation of the uvcvideo
> driver) is thus 32 KiB.
>
> For isochronous the situation is more complicated. The term "packet" as
> used in the uvcvideo driver actually means all the data transferred in
> one service interval, thus made of multiple isoc packets. It is heavily
> dependent on the USB speed, and the device can advertise different
> supported sizes (which translate directly to the reserved bandwidth for
> the transfer), with the driver picking the smallest bandwidth large
> enough for the data rate required by the resolution and frame rate. The
> theoretical worst case is 1024 bytes per isoc packet * 16 isoc packets
> per burst * 6 burst per interval * 32 "packets" per URB, equal to 3 MiB.
>
> Even with the largest URB size you have witnessed of ~1 MiB, we will end
> up lying quite a bit if we consider the URB completion callback for the
> first URB of the frame as indicating the start of reception.
>
> > > In term of accuracy, if timestamp was passed with the FRAME_SYNC event,
> > > it would not matter how fast your process the event anymore and greatly
> > > improve accuracy.
> >
> > +1 to that. If we could easily change the uAPI for FRAME_SYNC that
> > should definitely be implemented.
> >
> > > > Not to mention that the UVC timestamping requires a bit of love.
> > > >
> > > > @Laurent Pinchart, @Kieran Bingham  any progress reviewing :P :
> > > > https://patchwork.linuxtv.org/project/linux-media/list/?series=10083
> > >
> > > Thanks for working on this by the way, hope someone will find the time
> > > to review this. The timestamps should in theory provide a jitter free
> >
> > It already has a couple of Reviewed-by stamped in.... ;)
> >
> > > measurement of the delay Esker is trying to measure, and if it wasn't
> > > of bugs (and crazy complexity) it would in the worst case match the
> > > transfer time.
> >
> > Sorry to repeat myself, but just to avoid the confusion: Esker needs
> > to know how much power is used since we start receiving a frame until
> > it is available for dqbuf, not de frame latency.
>
> As I think everybody is aware, the earliest notification you get on the
> CPU side is the *end* of reception of the first URB, which can possibly
> be significantly later than the start of reception of the frame.
>
> Based on what I understand, the goal is to measure the CPU power
> consumption related to CPU processing of the frame. If that's the case,
> there's good and bad news. The good news is that the CPU doesn't process
> the frame at all until the URB has been received (if you were to measure
> the power consumption of the USB host controller too, it would be a
> different story), so the delay shouldn't be a problem. The bad news is
> that I don't see how the information you're trying to get will help you,
> as there's plenty of other things unrelated to the uvcvideo driver that
> can take CPU time while a frame is being received. That may not be any
> of my business, but from the point of view of the uvcvideo driver, I'm
> less inclined to accept a possibly significant V4L2_EVENT_FRAME_SYNC lie
> if the use case ends up making little sense :-)
>
> --
> Regards,
>
> Laurent Pinchart
  
Laurent Pinchart Nov. 9, 2023, 11:34 a.m. UTC | #15
Hi Esker,

On Thu, Nov 09, 2023 at 08:59:13AM +0800, Esker Wong wrote:
> Hi Laurent,
> 
> The use case here we want is actually the latency. We want to know
> what is the most accurate delay the user will feel when the camera
> capture the frame to the frame get displayed. So anytime earlier then
> the dqbuf should be an improvement here. It would help us know better
> of a camera quality.

If it's the latency you're after, wouldn't it be best to use the
timestamp produced by the camera ? That's the most accurate information.
It is expressed relative to the camera internal clock, but UVC transmits
information that enables conversion of the value to a system timestamp.

> On Thu, Nov 9, 2023 at 8:03 AM Laurent Pinchart wrote:
> > On Wed, Nov 08, 2023 at 11:46:40PM +0100, Ricardo Ribalda wrote:
> > > On Wed, 8 Nov 2023 at 21:32, <nicolas@ndufresne.ca> wrote:
> > > >
> > > > The fact that you interpret the time from FRAME_SYNC to DQBUF (well the
> > > > READ IO notification) as the actual latency is yours of course. It
> > > > assumes that the camera on the other end does not introduce other
> > >
> > > We want to use this signal to measure how much power is used since we
> > > start receiving the frame until we can use it.
> > > I agree with you that the latency between capture and dqbuf should be
> > > measured using the timestamp. That is not our use case here.
> > >
> > > > source of latency (or that these are negligible). You are also going to
> > > > introduce a lot of jitter, since it relies on when the OS decides to
> > > > wake up your process.
> > >
> > > We have measured a jitter of around 2.5 msec, which is acceptable for our needs.
> > >
> > > > I think my opinion resides in if you can accurately *enough* implement
> > > > what the spec says for FRAME_SYNC then do it, otherwise just don't lie.
> > >
> > > What the specs says is:
> > > ```
> > > Triggered immediately when the reception of a frame has begun
> > > ```
> > > In my opinion, that is true for usb devices, we are triggering it as
> > > soon as the transfer has started to the eyes of the driver. We cannot
> > > trigger earlier than that.
> > >
> > >
> > > > I think for ISO, "after the first chunk" i a small lie, but acceptable.
> > > > But for BULK, the way it was explained is that it will be always very
> > > > close to DQBUF time. and it should not emit FRAME_SYNC for this type of
> > > > UVC device. If it fits other events fine of course, I'm just making a
> > > > judgment on if its fits V4L2_EVENT_FRAME_SYNC or not.
> > >
> > > nit: I believe that you have swapped iso and bulk on this description
> >
> > I've confused the USB packet size and the UVC payload size. The latter
> > is typically much bigger for bulk devices than isoc devices, but the
> > former will be in similar order of magnitudes in a large number of
> > cases, but not all cases.
> >
> > The URB size is the result of the USB packet size and number of packets
> > per URB. The uvcvideo driver currently sets the number of packets per
> > URB to 32 at most (and lowers it if the frame size is small, or if not
> > enough memory can be allocated). This could be increased or made dynamic
> > in the future, as higher speeds typically benefit from larger URB sizes.
> > The packet size differs between bulk and isoc endpoints.
> >
> > For bulk, the packet size can be up to 512 bytes for USB 2.0 and 1024
> > bytes for USB 3.0, and the device can select a smaller size. The largest
> > URB size (again based on the current implementation of the uvcvideo
> > driver) is thus 32 KiB.
> >
> > For isochronous the situation is more complicated. The term "packet" as
> > used in the uvcvideo driver actually means all the data transferred in
> > one service interval, thus made of multiple isoc packets. It is heavily
> > dependent on the USB speed, and the device can advertise different
> > supported sizes (which translate directly to the reserved bandwidth for
> > the transfer), with the driver picking the smallest bandwidth large
> > enough for the data rate required by the resolution and frame rate. The
> > theoretical worst case is 1024 bytes per isoc packet * 16 isoc packets
> > per burst * 6 burst per interval * 32 "packets" per URB, equal to 3 MiB.
> >
> > Even with the largest URB size you have witnessed of ~1 MiB, we will end
> > up lying quite a bit if we consider the URB completion callback for the
> > first URB of the frame as indicating the start of reception.
> >
> > > > In term of accuracy, if timestamp was passed with the FRAME_SYNC event,
> > > > it would not matter how fast your process the event anymore and greatly
> > > > improve accuracy.
> > >
> > > +1 to that. If we could easily change the uAPI for FRAME_SYNC that
> > > should definitely be implemented.
> > >
> > > > > Not to mention that the UVC timestamping requires a bit of love.
> > > > >
> > > > > @Laurent Pinchart, @Kieran Bingham  any progress reviewing :P :
> > > > > https://patchwork.linuxtv.org/project/linux-media/list/?series=10083
> > > >
> > > > Thanks for working on this by the way, hope someone will find the time
> > > > to review this. The timestamps should in theory provide a jitter free
> > >
> > > It already has a couple of Reviewed-by stamped in.... ;)
> > >
> > > > measurement of the delay Esker is trying to measure, and if it wasn't
> > > > of bugs (and crazy complexity) it would in the worst case match the
> > > > transfer time.
> > >
> > > Sorry to repeat myself, but just to avoid the confusion: Esker needs
> > > to know how much power is used since we start receiving a frame until
> > > it is available for dqbuf, not de frame latency.
> >
> > As I think everybody is aware, the earliest notification you get on the
> > CPU side is the *end* of reception of the first URB, which can possibly
> > be significantly later than the start of reception of the frame.
> >
> > Based on what I understand, the goal is to measure the CPU power
> > consumption related to CPU processing of the frame. If that's the case,
> > there's good and bad news. The good news is that the CPU doesn't process
> > the frame at all until the URB has been received (if you were to measure
> > the power consumption of the USB host controller too, it would be a
> > different story), so the delay shouldn't be a problem. The bad news is
> > that I don't see how the information you're trying to get will help you,
> > as there's plenty of other things unrelated to the uvcvideo driver that
> > can take CPU time while a frame is being received. That may not be any
> > of my business, but from the point of view of the uvcvideo driver, I'm
> > less inclined to accept a possibly significant V4L2_EVENT_FRAME_SYNC lie
> > if the use case ends up making little sense :-)
  
Esker Wong Nov. 9, 2023, 11:41 a.m. UTC | #16
Hi Laurent,

We not only need t(dqbuf) - t(exposure), we are also interested in
t(dqbuf) - t(frame_recieved) to know more detail inside the camera for
analysis.

Thanks,
Esker








On Thu, Nov 9, 2023 at 7:34 PM Laurent Pinchart
<laurent.pinchart@ideasonboard.com> wrote:
>
> Hi Esker,
>
> On Thu, Nov 09, 2023 at 08:59:13AM +0800, Esker Wong wrote:
> > Hi Laurent,
> >
> > The use case here we want is actually the latency. We want to know
> > what is the most accurate delay the user will feel when the camera
> > capture the frame to the frame get displayed. So anytime earlier then
> > the dqbuf should be an improvement here. It would help us know better
> > of a camera quality.
>
> If it's the latency you're after, wouldn't it be best to use the
> timestamp produced by the camera ? That's the most accurate information.
> It is expressed relative to the camera internal clock, but UVC transmits
> information that enables conversion of the value to a system timestamp.
>
> > On Thu, Nov 9, 2023 at 8:03 AM Laurent Pinchart wrote:
> > > On Wed, Nov 08, 2023 at 11:46:40PM +0100, Ricardo Ribalda wrote:
> > > > On Wed, 8 Nov 2023 at 21:32, <nicolas@ndufresne.ca> wrote:
> > > > >
> > > > > The fact that you interpret the time from FRAME_SYNC to DQBUF (well the
> > > > > READ IO notification) as the actual latency is yours of course. It
> > > > > assumes that the camera on the other end does not introduce other
> > > >
> > > > We want to use this signal to measure how much power is used since we
> > > > start receiving the frame until we can use it.
> > > > I agree with you that the latency between capture and dqbuf should be
> > > > measured using the timestamp. That is not our use case here.
> > > >
> > > > > source of latency (or that these are negligible). You are also going to
> > > > > introduce a lot of jitter, since it relies on when the OS decides to
> > > > > wake up your process.
> > > >
> > > > We have measured a jitter of around 2.5 msec, which is acceptable for our needs.
> > > >
> > > > > I think my opinion resides in if you can accurately *enough* implement
> > > > > what the spec says for FRAME_SYNC then do it, otherwise just don't lie.
> > > >
> > > > What the specs says is:
> > > > ```
> > > > Triggered immediately when the reception of a frame has begun
> > > > ```
> > > > In my opinion, that is true for usb devices, we are triggering it as
> > > > soon as the transfer has started to the eyes of the driver. We cannot
> > > > trigger earlier than that.
> > > >
> > > >
> > > > > I think for ISO, "after the first chunk" i a small lie, but acceptable.
> > > > > But for BULK, the way it was explained is that it will be always very
> > > > > close to DQBUF time. and it should not emit FRAME_SYNC for this type of
> > > > > UVC device. If it fits other events fine of course, I'm just making a
> > > > > judgment on if its fits V4L2_EVENT_FRAME_SYNC or not.
> > > >
> > > > nit: I believe that you have swapped iso and bulk on this description
> > >
> > > I've confused the USB packet size and the UVC payload size. The latter
> > > is typically much bigger for bulk devices than isoc devices, but the
> > > former will be in similar order of magnitudes in a large number of
> > > cases, but not all cases.
> > >
> > > The URB size is the result of the USB packet size and number of packets
> > > per URB. The uvcvideo driver currently sets the number of packets per
> > > URB to 32 at most (and lowers it if the frame size is small, or if not
> > > enough memory can be allocated). This could be increased or made dynamic
> > > in the future, as higher speeds typically benefit from larger URB sizes.
> > > The packet size differs between bulk and isoc endpoints.
> > >
> > > For bulk, the packet size can be up to 512 bytes for USB 2.0 and 1024
> > > bytes for USB 3.0, and the device can select a smaller size. The largest
> > > URB size (again based on the current implementation of the uvcvideo
> > > driver) is thus 32 KiB.
> > >
> > > For isochronous the situation is more complicated. The term "packet" as
> > > used in the uvcvideo driver actually means all the data transferred in
> > > one service interval, thus made of multiple isoc packets. It is heavily
> > > dependent on the USB speed, and the device can advertise different
> > > supported sizes (which translate directly to the reserved bandwidth for
> > > the transfer), with the driver picking the smallest bandwidth large
> > > enough for the data rate required by the resolution and frame rate. The
> > > theoretical worst case is 1024 bytes per isoc packet * 16 isoc packets
> > > per burst * 6 burst per interval * 32 "packets" per URB, equal to 3 MiB.
> > >
> > > Even with the largest URB size you have witnessed of ~1 MiB, we will end
> > > up lying quite a bit if we consider the URB completion callback for the
> > > first URB of the frame as indicating the start of reception.
> > >
> > > > > In term of accuracy, if timestamp was passed with the FRAME_SYNC event,
> > > > > it would not matter how fast your process the event anymore and greatly
> > > > > improve accuracy.
> > > >
> > > > +1 to that. If we could easily change the uAPI for FRAME_SYNC that
> > > > should definitely be implemented.
> > > >
> > > > > > Not to mention that the UVC timestamping requires a bit of love.
> > > > > >
> > > > > > @Laurent Pinchart, @Kieran Bingham  any progress reviewing :P :
> > > > > > https://patchwork.linuxtv.org/project/linux-media/list/?series=10083
> > > > >
> > > > > Thanks for working on this by the way, hope someone will find the time
> > > > > to review this. The timestamps should in theory provide a jitter free
> > > >
> > > > It already has a couple of Reviewed-by stamped in.... ;)
> > > >
> > > > > measurement of the delay Esker is trying to measure, and if it wasn't
> > > > > of bugs (and crazy complexity) it would in the worst case match the
> > > > > transfer time.
> > > >
> > > > Sorry to repeat myself, but just to avoid the confusion: Esker needs
> > > > to know how much power is used since we start receiving a frame until
> > > > it is available for dqbuf, not de frame latency.
> > >
> > > As I think everybody is aware, the earliest notification you get on the
> > > CPU side is the *end* of reception of the first URB, which can possibly
> > > be significantly later than the start of reception of the frame.
> > >
> > > Based on what I understand, the goal is to measure the CPU power
> > > consumption related to CPU processing of the frame. If that's the case,
> > > there's good and bad news. The good news is that the CPU doesn't process
> > > the frame at all until the URB has been received (if you were to measure
> > > the power consumption of the USB host controller too, it would be a
> > > different story), so the delay shouldn't be a problem. The bad news is
> > > that I don't see how the information you're trying to get will help you,
> > > as there's plenty of other things unrelated to the uvcvideo driver that
> > > can take CPU time while a frame is being received. That may not be any
> > > of my business, but from the point of view of the uvcvideo driver, I'm
> > > less inclined to accept a possibly significant V4L2_EVENT_FRAME_SYNC lie
> > > if the use case ends up making little sense :-)
>
> --
> Regards,
>
> Laurent Pinchart
  
Nicolas Dufresne Nov. 23, 2023, 6:22 p.m. UTC | #17
Le jeudi 09 novembre 2023 à 01:27 +0100, Ricardo Ribalda a écrit :
> Hi Laurent
> 
> On Thu, 9 Nov 2023 at 01:03, Laurent Pinchart
> <laurent.pinchart@ideasonboard.com> wrote:
> > 
> > Hi Ricardo,
> > 
> > On Wed, Nov 08, 2023 at 11:46:40PM +0100, Ricardo Ribalda wrote:
> > > On Wed, 8 Nov 2023 at 21:32, <nicolas@ndufresne.ca> wrote:
> > > > 
> > > > The fact that you interpret the time from FRAME_SYNC to DQBUF (well the
> > > > READ IO notification) as the actual latency is yours of course. It
> > > > assumes that the camera on the other end does not introduce other
> > > 
> > > We want to use this signal to measure how much power is used since we
> > > start receiving the frame until we can use it.
> > > I agree with you that the latency between capture and dqbuf should be
> > > measured using the timestamp. That is not our use case here.
> > > 
> > > > source of latency (or that these are negligible). You are also going to
> > > > introduce a lot of jitter, since it relies on when the OS decides to
> > > > wake up your process.
> > > 
> > > We have measured a jitter of around 2.5 msec, which is acceptable for our needs.
> > > 
> > > > I think my opinion resides in if you can accurately *enough* implement
> > > > what the spec says for FRAME_SYNC then do it, otherwise just don't lie.
> > > 
> > > What the specs says is:
> > > ```
> > > Triggered immediately when the reception of a frame has begun
> > > ```
> > > In my opinion, that is true for usb devices, we are triggering it as
> > > soon as the transfer has started to the eyes of the driver. We cannot
> > > trigger earlier than that.
> > > 
> > > 
> > > > I think for ISO, "after the first chunk" i a small lie, but acceptable.
> > > > But for BULK, the way it was explained is that it will be always very
> > > > close to DQBUF time. and it should not emit FRAME_SYNC for this type of
> > > > UVC device. If it fits other events fine of course, I'm just making a
> > > > judgment on if its fits V4L2_EVENT_FRAME_SYNC or not.
> > > 
> > > nit: I believe that you have swapped iso and bulk on this description
> > 
> > I've confused the USB packet size and the UVC payload size. The latter
> > is typically much bigger for bulk devices than isoc devices, but the
> > former will be in similar order of magnitudes in a large number of
> > cases, but not all cases.
> > 
> > The URB size is the result of the USB packet size and number of packets
> > per URB. The uvcvideo driver currently sets the number of packets per
> > URB to 32 at most (and lowers it if the frame size is small, or if not
> > enough memory can be allocated). This could be increased or made dynamic
> > in the future, as higher speeds typically benefit from larger URB sizes.
> > The packet size differs between bulk and isoc endpoints.
> > 
> > For bulk, the packet size can be up to 512 bytes for USB 2.0 and 1024
> > bytes for USB 3.0, and the device can select a smaller size. The largest
> > URB size (again based on the current implementation of the uvcvideo
> > driver) is thus 32 KiB.
> > 
> > For isochronous the situation is more complicated. The term "packet" as
> > used in the uvcvideo driver actually means all the data transferred in
> > one service interval, thus made of multiple isoc packets. It is heavily
> > dependent on the USB speed, and the device can advertise different
> > supported sizes (which translate directly to the reserved bandwidth for
> > the transfer), with the driver picking the smallest bandwidth large
> > enough for the data rate required by the resolution and frame rate. The
> > theoretical worst case is 1024 bytes per isoc packet * 16 isoc packets
> > per burst * 6 burst per interval * 32 "packets" per URB, equal to 3 MiB.
> > 
> > Even with the largest URB size you have witnessed of ~1 MiB, we will end
> > up lying quite a bit if we consider the URB completion callback for the
> > first URB of the frame as indicating the start of reception.
> > 
> > > > In term of accuracy, if timestamp was passed with the FRAME_SYNC event,
> > > > it would not matter how fast your process the event anymore and greatly
> > > > improve accuracy.
> > > 
> > > +1 to that. If we could easily change the uAPI for FRAME_SYNC that
> > > should definitely be implemented.
> > > 
> > > > > Not to mention that the UVC timestamping requires a bit of love.
> > > > > 
> > > > > @Laurent Pinchart, @Kieran Bingham  any progress reviewing :P :
> > > > > https://patchwork.linuxtv.org/project/linux-media/list/?series=10083
> > > > 
> > > > Thanks for working on this by the way, hope someone will find the time
> > > > to review this. The timestamps should in theory provide a jitter free
> > > 
> > > It already has a couple of Reviewed-by stamped in.... ;)
> > > 
> > > > measurement of the delay Esker is trying to measure, and if it wasn't
> > > > of bugs (and crazy complexity) it would in the worst case match the
> > > > transfer time.
> > > 
> > > Sorry to repeat myself, but just to avoid the confusion: Esker needs
> > > to know how much power is used since we start receiving a frame until
> > > it is available for dqbuf, not de frame latency.
> > 
> > As I think everybody is aware, the earliest notification you get on the
> > CPU side is the *end* of reception of the first URB, which can possibly
> > be significantly later than the start of reception of the frame.
> > 
> > Based on what I understand, the goal is to measure the CPU power
> > consumption related to CPU processing of the frame. If that's the case,
> > there's good and bad news. The good news is that the CPU doesn't process
> > the frame at all until the URB has been received (if you were to measure
> > the power consumption of the USB host controller too, it would be a
> > different story), so the delay shouldn't be a problem. The bad news is
> > that I don't see how the information you're trying to get will help you,
> > as there's plenty of other things unrelated to the uvcvideo driver that
> > can take CPU time while a frame is being received. That may not be any
> > of my business, but from the point of view of the uvcvideo driver, I'm
> > less inclined to accept a possibly significant V4L2_EVENT_FRAME_SYNC lie
> > if the use case ends up making little sense :-)
> 
> Just to add some numbers to add some context:
> 
>  V4L2_EVENT_FRAME_SYNC for BULK and ISO will be delayed from:
> ```
> Triggered immediately when the reception of a frame has begun
> ```
> one urb.
> 
> For bulk devices this is a maximum of 0.05 msec (32KiB/600MBps)

I lack a bit of knowledge on how to scale this to different devices, with
different speed/framesize. My only bulk device is:

https://inogeni.com/product/4k2usb3/

Which is USB 3.0, and have raw (NV12) resolution from 640x480 (max 60pfs) to 4K
(max 30fps). What would the error look like with that ?

> For 1MiB transfer isoc devices (which is the biggest we have seen),
> that is 1.8 msec.
> In both cases, this is smaller than the jitter to process the event
> itself by userspace.
> 
> The time from V4L2_EVENT_FRAME_SYNC to DQBUF is around 30 msec.
> 
> I do not know how much delay is considered acceptable... but if we
> take the delay argument to the extreme, we could say that
> V4L2_EVENT_FRAME_SYNC can never be implemented, because the event will
> always be delayed by something.

We have v4l2_event.timestamp for all events, so the jitter to process the event
by userpace can be removed reliably already.

Nicolas

p.s. missed it earlier

> 
> > 
> > --
> > Regards,
> > 
> > Laurent Pinchart
> 
> 
>
  
Ricardo Ribalda Nov. 24, 2023, 9:58 a.m. UTC | #18
> >
> > For bulk devices this is a maximum of 0.05 msec (32KiB/600MBps)
>
> I lack a bit of knowledge on how to scale this to different devices, with
> different speed/framesize. My only bulk device is:
>
> https://inogeni.com/product/4k2usb3/
>
> Which is USB 3.0, and have raw (NV12) resolution from 640x480 (max 60pfs) to 4K
> (max 30fps). What would the error look like with that ?

For bulk devices the maximum delay from packing multiple packets into
a urb is 0.05 msec (32KiB/600MBps)

Laurent's message <20231109000327.GE21616@pendragon.ideasonboard.com>
explains where those numbers come from :).

Regards!

>
> > For 1MiB transfer isoc devices (which is the biggest we have seen),
> > that is 1.8 msec.
> > In both cases, this is smaller than the jitter to process the event
> > itself by userspace.
> >
> > The time from V4L2_EVENT_FRAME_SYNC to DQBUF is around 30 msec.
> >
> > I do not know how much delay is considered acceptable... but if we
> > take the delay argument to the extreme, we could say that
> > V4L2_EVENT_FRAME_SYNC can never be implemented, because the event will
> > always be delayed by something.
>
> We have v4l2_event.timestamp for all events, so the jitter to process the event
> by userpace can be removed reliably already.
>
> Nicolas
>
> p.s. missed it earlier
>
> >
> > >
> > > --
> > > Regards,
> > >
> > > Laurent Pinchart
> >
> >
> >
>


--
Ricardo Ribalda
  

Patch

diff --git a/drivers/media/usb/uvc/uvc_v4l2.c b/drivers/media/usb/uvc/uvc_v4l2.c
index f4988f03640a..9f3fb5fd2375 100644
--- a/drivers/media/usb/uvc/uvc_v4l2.c
+++ b/drivers/media/usb/uvc/uvc_v4l2.c
@@ -1352,6 +1352,8 @@  static int uvc_ioctl_subscribe_event(struct v4l2_fh *fh,
 	switch (sub->type) {
 	case V4L2_EVENT_CTRL:
 		return v4l2_event_subscribe(fh, sub, 0, &uvc_ctrl_sub_ev_ops);
+	case V4L2_EVENT_FRAME_SYNC:
+		return v4l2_event_subscribe(fh, sub, 0, NULL);
 	default:
 		return -EINVAL;
 	}
diff --git a/drivers/media/usb/uvc/uvc_video.c b/drivers/media/usb/uvc/uvc_video.c
index 28dde08ec6c5..4f3a510ca4fe 100644
--- a/drivers/media/usb/uvc/uvc_video.c
+++ b/drivers/media/usb/uvc/uvc_video.c
@@ -1073,9 +1073,16 @@  static int uvc_video_decode_start(struct uvc_streaming *stream,
 	 * that discontinuous sequence numbers always indicate lost frames.
 	 */
 	if (stream->last_fid != fid) {
+		struct v4l2_event event = {
+			.type = V4L2_EVENT_FRAME_SYNC,
+		};
+
 		stream->sequence++;
 		if (stream->sequence)
 			uvc_video_stats_update(stream);
+
+		event.u.frame_sync.frame_sequence = stream->sequence,
+		v4l2_event_queue(&stream->vdev, &event);
 	}
 
 	uvc_video_clock_decode(stream, buf, data, len);