xref: /aosp_15_r20/external/virtio-media/device/src/poll.rs (revision 1b4853f54772485c5dd4001ae33a7a958bcc97a1)
1 // Copyright 2024 The ChromiumOS Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 use std::os::fd::BorrowedFd;
6 
7 /// Trait allowing sessions of a device to signal when they have an event pending.
8 ///
9 /// The worker that runs a `V4l2ProxyDevice` typically polls on file descriptors for available
10 /// CAPTURE buffers and outstanding session events. However V4L2's poll logic returns with the
11 /// `POLLERR` flag if a CAPTURE queue is polled while not streaming or if zero CAPTURE buffers have
12 /// been queued. To avoid this, the device needs to disable polling when this would happen, and
13 /// re-enable it when conditions are adequate.
14 ///
15 /// If the worker does not need such a feature, `()` can be passed as a no-op type that implements
16 /// this interface.
17 pub trait SessionPoller: Clone {
18     /// Add a newly created `session` to be polled.
19     ///
20     /// The `session` FD must signal that it is readable if there are events pending for the
21     /// session.
add_session(&self, session: BorrowedFd, session_id: u32) -> Result<(), i32>22     fn add_session(&self, session: BorrowedFd, session_id: u32) -> Result<(), i32>;
23     /// Stop polling all activity on `session`.
remove_session(&self, session: BorrowedFd)24     fn remove_session(&self, session: BorrowedFd);
25 }
26 
27 /// No-op implementation of `SessionPoller`. This should only be used when using
28 /// `VirtioMediaDeviceRunner` with a device that doesn't need to be polled, otherwise the methods
29 /// might be called, which will make the program panic.
30 impl SessionPoller for () {
add_session(&self, _session: BorrowedFd, _session_id: u32) -> Result<(), i32>31     fn add_session(&self, _session: BorrowedFd, _session_id: u32) -> Result<(), i32> {
32         panic!("this device needs a proper SessionPoller - aborting")
33     }
34 
remove_session(&self, _session: BorrowedFd)35     fn remove_session(&self, _session: BorrowedFd) {
36         panic!("this device needs a proper SessionPoller - aborting")
37     }
38 }
39