xref: /aosp_15_r20/external/webrtc/modules/desktop_capture/screen_capture_frame_queue.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #ifndef MODULES_DESKTOP_CAPTURE_SCREEN_CAPTURE_FRAME_QUEUE_H_
12 #define MODULES_DESKTOP_CAPTURE_SCREEN_CAPTURE_FRAME_QUEUE_H_
13 
14 #include <memory>
15 
16 namespace webrtc {
17 
18 // Represents a queue of reusable video frames. Provides access to the 'current'
19 // frame - the frame that the caller is working with at the moment, and to the
20 // 'previous' frame - the predecessor of the current frame swapped by
21 // MoveToNextFrame() call, if any.
22 //
23 // The caller is expected to (re)allocate frames if current_frame() returns
24 // NULL. The caller can mark all frames in the queue for reallocation (when,
25 // say, frame dimensions change). The queue records which frames need updating
26 // which the caller can query.
27 //
28 // Frame consumer is expected to never hold more than kQueueLength frames
29 // created by this function and it should release the earliest one before trying
30 // to capture a new frame (i.e. before MoveToNextFrame() is called).
31 template <typename FrameType>
32 class ScreenCaptureFrameQueue {
33  public:
34   ScreenCaptureFrameQueue() = default;
35   ~ScreenCaptureFrameQueue() = default;
36 
37   ScreenCaptureFrameQueue(const ScreenCaptureFrameQueue&) = delete;
38   ScreenCaptureFrameQueue& operator=(const ScreenCaptureFrameQueue&) = delete;
39 
40   // Moves to the next frame in the queue, moving the 'current' frame to become
41   // the 'previous' one.
MoveToNextFrame()42   void MoveToNextFrame() { current_ = (current_ + 1) % kQueueLength; }
43 
44   // Replaces the current frame with a new one allocated by the caller. The
45   // existing frame (if any) is destroyed. Takes ownership of `frame`.
ReplaceCurrentFrame(std::unique_ptr<FrameType> frame)46   void ReplaceCurrentFrame(std::unique_ptr<FrameType> frame) {
47     frames_[current_] = std::move(frame);
48   }
49 
50   // Marks all frames obsolete and resets the previous frame pointer. No
51   // frames are freed though as the caller can still access them.
Reset()52   void Reset() {
53     for (int i = 0; i < kQueueLength; i++) {
54       frames_[i].reset();
55     }
56     current_ = 0;
57   }
58 
current_frame()59   FrameType* current_frame() const { return frames_[current_].get(); }
60 
previous_frame()61   FrameType* previous_frame() const {
62     return frames_[(current_ + kQueueLength - 1) % kQueueLength].get();
63   }
64 
65  private:
66   // Index of the current frame.
67   int current_ = 0;
68 
69   static const int kQueueLength = 2;
70   std::unique_ptr<FrameType> frames_[kQueueLength];
71 };
72 
73 }  // namespace webrtc
74 
75 #endif  // MODULES_DESKTOP_CAPTURE_SCREEN_CAPTURE_FRAME_QUEUE_H_
76