pub(crate) fn poll_frames(
        &mut self,
        mut next_frame: usize,
        min_frames: usize,
        cx: &mut Context<'_>,
    ) -> Poll<Result<(Vec<u8>, usize, usize)>> {
        let start_time = match self.start_time.as_ref() {
            None => {
                self.waker = Some(cx.waker().clone());
                return Poll::Pending;
            }
            Some(time) => *time,
        };
        let vmo_frames = self.size / self.bytes_per_frame;
        if min_frames > vmo_frames {
            return Poll::Ready(Err(Error::InvalidArgs));
        }

        let now = fasync::Time::now();
        // Start time is the zero frame.
        // Bump one nano so that we catch if a frame ended exactly at now.
        let current_frame_count = self.frames_before(now + 1.nanos());
        let oldest_frame_count = current_frame_count.saturating_sub(vmo_frames);

        let missing_frames = if oldest_frame_count <= next_frame {
            0
        } else {
            oldest_frame_count - next_frame as usize
        };

        if missing_frames > 0 {
            next_frame = oldest_frame_count;
        }

        let frames_available = current_frame_count.saturating_sub(next_frame);
        if frames_available < min_frames {
            // Set the timer to wake when min_frames will be available
            let frames_to_wait = min_frames - frames_available;
            let mut timer = fasync::Timer::new(now + self.duration_from_frames(frames_to_wait));
            if timer.poll_unpin(cx).is_ready() {
                // Somehow raced to a time where they are available, try again.
                return self.poll_frames(next_frame, min_frames, cx);
            }
            self.timer = Some(timer);
            return Poll::Pending;
        } else if frames_available == 0 {
            // The minimum frames must be 0 and none are available
            return Poll::Ready(Ok((Vec::new(), next_frame, 0)));
        }

        let bytes_available = frames_available * self.bytes_per_frame;
        let mut out_vec = vec![0; bytes_available];

        let mut frame_from_idx = next_frame % vmo_frames;
        let mut frame_until_idx = current_frame_count % vmo_frames;

        // If the until idx is behind the from idx, we need to wrap-around.
        if frame_from_idx >= frame_until_idx {
            frame_until_idx += vmo_frames;
        }

        let mut ndx = 0;

        // If we wrap around, read to the end into the buffer, then set up to read the rest.
        if frame_until_idx > vmo_frames {
            let frames_to_read = vmo_frames - frame_from_idx;
            let bytes_to_read = frames_to_read * self.bytes_per_frame;
            let byte_start = frame_from_idx * self.bytes_per_frame;
            self.vmo
                .read(&mut out_vec[0..bytes_to_read], byte_start as u64)
                .map_err(|e| Error::IOError(e))?;
            frame_from_idx = 0;
            frame_until_idx -= vmo_frames;
            ndx = bytes_to_read;
        }

        let frames_to_read = frame_until_idx - frame_from_idx;
        let bytes_to_read = frames_to_read * self.bytes_per_frame;
        let byte_start = frame_from_idx * self.bytes_per_frame;

        self.vmo
            .read(&mut out_vec[ndx..ndx + bytes_to_read], byte_start as u64)
            .map_err(|e| Error::IOError(e))?;

        // We're returning frames from just after the `since` to this time.
        // Notify if we have a position responder and we read past the clock notification time.
        if current_frame_count > self.next_notify_frame && self.position_responder.is_some() {
            let end_frame_time = start_time + self.duration_from_frames(current_frame_count);
            let notify_frame_bytes = frame_until_idx * self.bytes_per_frame;
            let next_notify_frame = self.next_notify_frame + self.frames_between_notifications;
            let mut info = RingBufferPositionInfo {
                timestamp: end_frame_time.into_nanos(),
                position: notify_frame_bytes as u32,
            };
            if let Some(Ok(())) = self.position_responder.take().map(|t| t.send(&mut info)) {
                self.next_notify_frame = next_notify_frame;
            }
        }
        // Index of the last frame returned is -1 because we index from zero.
        Poll::Ready(Ok((out_vec, current_frame_count - 1, missing_frames)))
    }
/// Retrieve the frames available starting at `next_frame`.
/// `next_frame` is a frame index, starting at zero when the buffer is started.
/// Returns a vector of bytes containing the frames, the index of the last frame returned,
/// and a count of frames that were unavailable due to buffer overwrite (slow polling).
/// If less than min_frames are available, Poll::Pending is returned and the waker
/// associated with `cx` will be woken at a time when enough frames should be available.
/// If no frames are available and min_frames is zero, an empty vector and next_frame are returned.
/// If polled before start, it will be woken when started.
/// Returns Err(Error::InvalidArgs) if min_frames is larger than the size of the VMO.
Retrieve the frames available starting at `next_frame`.
`next_frame` is a frame index, starting at zero when the buffer is started.
Returns a vector of bytes containing the frames, the index of the last frame returned,
and a count of frames that were unavailable due to buffer overwrite (slow polling).
If less than min_frames are available, Poll::Pending is returned and the waker
associated with `cx` will be woken at a time when enough frames should be available.
If no frames are available and min_frames is zero, an empty vector and next_frame are returned.
If polled before start, it will be woken when started.
Returns Err(Error::InvalidArgs) if min_frames is larger than the size of the VMO.
Retrieve the frames available starting at ` next_frame ` . ` next_frame ` is a frame index starting at zero when the buffer is started . Returns a vector of bytes containing the frames the index of the last frame returned and a count of frames that were unavailable due to buffer overwrite ( slow polling ) . If less than min_frames are available Poll :: Pending is returned and the waker associated with ` cx ` will be woken at a time when enough frames should be available . If no frames are available and min_frames is zero an empty vector and next_frame are returned . If polled before start it will be woken when started . Returns Err ( Error :: InvalidArgs ) if min_frames is larger than the size of the VMO .
pub(crate) fn poll_frames(
        &mut self,
        mut next_frame: usize,
        min_frames: usize,
        cx: &mut Context<'_>,
    ) -> Poll<Result<(Vec<u8>, usize, usize)>> {
        let start_time = match self.start_time.as_ref() {
            None => {
                self.waker = Some(cx.waker().clone());
                return Poll::Pending;
            }
            Some(time) => *time,
        };
        let vmo_frames = self.size / self.bytes_per_frame;
        if min_frames > vmo_frames {
            return Poll::Ready(Err(Error::InvalidArgs));
        }
        let now = fasync::Time::now();
        // Start time is the zero frame.
        // Bump one nano so that we catch if a frame ended exactly at now.
        let current_frame_count = self.frames_before(now + 1.nanos());
        let oldest_frame_count = current_frame_count.saturating_sub(vmo_frames);
        let missing_frames = if oldest_frame_count <= next_frame {
            0
        } else {
            oldest_frame_count - next_frame as usize
        };
        if missing_frames > 0 {
            next_frame = oldest_frame_count;
        }
        let frames_available = current_frame_count.saturating_sub(next_frame);
        if frames_available < min_frames {
            // Set the timer to wake when min_frames will be available
            let frames_to_wait = min_frames - frames_available;
            let mut timer = fasync::Timer::new(now + self.duration_from_frames(frames_to_wait));
            if timer.poll_unpin(cx).is_ready() {
                // Somehow raced to a time where they are available, try again.
                return self.poll_frames(next_frame, min_frames, cx);
            }
            self.timer = Some(timer);
            return Poll::Pending;
        } else if frames_available == 0 {
            // The minimum frames must be 0 and none are available
            return Poll::Ready(Ok((Vec::new(), next_frame, 0)));
        }
        let bytes_available = frames_available * self.bytes_per_frame;
        let mut out_vec = vec![0; bytes_available];
        let mut frame_from_idx = next_frame % vmo_frames;
        let mut frame_until_idx = current_frame_count % vmo_frames;
        // If the until idx is behind the from idx, we need to wrap-around.
        if frame_from_idx >= frame_until_idx {
            frame_until_idx += vmo_frames;
        }
        let mut ndx = 0;
        // If we wrap around, read to the end into the buffer, then set up to read the rest.
        if frame_until_idx > vmo_frames {
            let frames_to_read = vmo_frames - frame_from_idx;
            let bytes_to_read = frames_to_read * self.bytes_per_frame;
            let byte_start = frame_from_idx * self.bytes_per_frame;
            self.vmo
                .read(&mut out_vec[0..bytes_to_read], byte_start as u64)
                .map_err(|e| Error::IOError(e))?;
            frame_from_idx = 0;
            frame_until_idx -= vmo_frames;
            ndx = bytes_to_read;
        }
        let frames_to_read = frame_until_idx - frame_from_idx;
        let bytes_to_read = frames_to_read * self.bytes_per_frame;
        let byte_start = frame_from_idx * self.bytes_per_frame;
        self.vmo
            .read(&mut out_vec[ndx..ndx + bytes_to_read], byte_start as u64)
            .map_err(|e| Error::IOError(e))?;
        // We're returning frames from just after the `since` to this time.
        // Notify if we have a position responder and we read past the clock notification time.
        if current_frame_count > self.next_notify_frame && self.position_responder.is_some() {
            let end_frame_time = start_time + self.duration_from_frames(current_frame_count);
            let notify_frame_bytes = frame_until_idx * self.bytes_per_frame;
            let next_notify_frame = self.next_notify_frame + self.frames_between_notifications;
            let mut info = RingBufferPositionInfo {
                timestamp: end_frame_time.into_nanos(),
                position: notify_frame_bytes as u32,
            };
            if let Some(Ok(())) = self.position_responder.take().map(|t| t.send(&mut info)) {
                self.next_notify_frame = next_notify_frame;
            }
        }
        // Index of the last frame returned is -1 because we index from zero.
        Poll::Ready(Ok((out_vec, current_frame_count - 1, missing_frames)))
    }
pub ( crate ) fn poll_frames ( & mut self , mut next_frame : usize , min_frames : usize , cx : & mut Context < ' _ > , ) -> Poll < Result < ( Vec < u8 > , usize , usize ) > > { let start_time = match self . start_time . as_ref ( ) { None => { self . waker = Some ( cx . waker ( ) . clone ( ) ) ; return Poll :: Pending ; } Some ( time ) => * time , } ; let vmo_frames = self . size / self . bytes_per_frame ; if min_frames > vmo_frames { return Poll :: Ready ( Err ( Error :: InvalidArgs ) ) ; } let now = fasync :: Time :: now ( ) ; // Start time is the zero frame. // Bump one nano so that we catch if a frame ended exactly at now. let current_frame_count = self . frames_before ( now + 1 . nanos ( ) ) ; let oldest_frame_count = current_frame_count . saturating_sub ( vmo_frames ) ; let missing_frames = if oldest_frame_count <= next_frame { 0 } else { oldest_frame_count - next_frame as usize } ; if missing_frames > 0 { next_frame = oldest_frame_count ; } let frames_available = current_frame_count . saturating_sub ( next_frame ) ; if frames_available < min_frames { // Set the timer to wake when min_frames will be available let frames_to_wait = min_frames - frames_available ; let mut timer = fasync :: Timer :: new ( now + self . duration_from_frames ( frames_to_wait ) ) ; if timer . poll_unpin ( cx ) . is_ready ( ) { // Somehow raced to a time where they are available, try again. return self . poll_frames ( next_frame , min_frames , cx ) ; } self . timer = Some ( timer ) ; return Poll :: Pending ; } else if frames_available == 0 { // The minimum frames must be 0 and none are available return Poll :: Ready ( Ok ( ( Vec :: new ( ) , next_frame , 0 ) ) ) ; } let bytes_available = frames_available * self . bytes_per_frame ; let mut out_vec = vec ! [ 0 bytes_available ] ; let mut frame_from_idx = next_frame % vmo_frames ; let mut frame_until_idx = current_frame_count % vmo_frames ; // If the until idx is behind the from idx, we need to wrap-around. if frame_from_idx >= frame_until_idx { frame_until_idx += vmo_frames ; } let mut ndx = 0 ; // If we wrap around, read to the end into the buffer, then set up to read the rest. if frame_until_idx > vmo_frames { let frames_to_read = vmo_frames - frame_from_idx ; let bytes_to_read = frames_to_read * self . bytes_per_frame ; let byte_start = frame_from_idx * self . bytes_per_frame ; self . vmo . read ( & mut out_vec [ 0 .. bytes_to_read ] , byte_start as u64 ) . map_err ( | e | Error :: IOError ( e ) ) ? ; frame_from_idx = 0 ; frame_until_idx -= vmo_frames ; ndx = bytes_to_read ; } let frames_to_read = frame_until_idx - frame_from_idx ; let bytes_to_read = frames_to_read * self . bytes_per_frame ; let byte_start = frame_from_idx * self . bytes_per_frame ; self . vmo . read ( & mut out_vec [ ndx .. ndx + bytes_to_read ] , byte_start as u64 ) . map_err ( | e | Error :: IOError ( e ) ) ? ; // We're returning frames from just after the `since` to this time. // Notify if we have a position responder and we read past the clock notification time. if current_frame_count > self . next_notify_frame && self . position_responder . is_some ( ) { let end_frame_time = start_time + self . duration_from_frames ( current_frame_count ) ; let notify_frame_bytes = frame_until_idx * self . bytes_per_frame ; let next_notify_frame = self . next_notify_frame + self . frames_between_notifications ; let mut info = RingBufferPositionInfo { timestamp : end_frame_time . into_nanos ( ) , position : notify_frame_bytes as u32 , } ; if let Some ( Ok ( ( ) ) ) = self . position_responder . take ( ) . map ( | t | t . send ( & mut info ) ) { self . next_notify_frame = next_notify_frame ; } } // Index of the last frame returned is -1 because we index from zero. Poll :: Ready ( Ok ( ( out_vec , current_frame_count - 1 , missing_frames ) ) ) }
Retrieve the frames available starting at `next_frame`.
Retrieve the frames available starting at ` next_frame ` .
&mut self
next_frame
usize
min_frames
usize
cx
&mut Context<'_>
&mut self
next_frame
usize
min_frames
usize
cx
&mut Context<'_>