Line data Source code
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 WEBRTC_MODULES_DESKTOP_CAPTURE_SCREEN_CAPTURE_FRAME_QUEUE_H_
12 : #define WEBRTC_MODULES_DESKTOP_CAPTURE_SCREEN_CAPTURE_FRAME_QUEUE_H_
13 :
14 : #include <memory>
15 :
16 : #include "webrtc/base/constructormagic.h"
17 : // TODO(zijiehe): These headers are not used in this file, but to avoid build
18 : // break in remoting/host. We should add headers in each individual files.
19 : #include "webrtc/modules/desktop_capture/desktop_frame.h" // Remove
20 : #include "webrtc/modules/desktop_capture/shared_desktop_frame.h" // Remove
21 :
22 :
23 : namespace webrtc {
24 :
25 : // Represents a queue of reusable video frames. Provides access to the 'current'
26 : // frame - the frame that the caller is working with at the moment, and to the
27 : // 'previous' frame - the predecessor of the current frame swapped by
28 : // MoveToNextFrame() call, if any.
29 : //
30 : // The caller is expected to (re)allocate frames if current_frame() returns
31 : // NULL. The caller can mark all frames in the queue for reallocation (when,
32 : // say, frame dimensions change). The queue records which frames need updating
33 : // which the caller can query.
34 : //
35 : // Frame consumer is expected to never hold more than kQueueLength frames
36 : // created by this function and it should release the earliest one before trying
37 : // to capture a new frame (i.e. before MoveToNextFrame() is called).
38 : template <typename FrameType>
39 : class ScreenCaptureFrameQueue {
40 : public:
41 0 : ScreenCaptureFrameQueue() : current_(0) {}
42 0 : ~ScreenCaptureFrameQueue() = default;
43 :
44 : // Moves to the next frame in the queue, moving the 'current' frame to become
45 : // the 'previous' one.
46 0 : void MoveToNextFrame() {
47 0 : current_ = (current_ + 1) % kQueueLength;
48 0 : }
49 :
50 : // Replaces the current frame with a new one allocated by the caller. The
51 : // existing frame (if any) is destroyed. Takes ownership of |frame|.
52 0 : void ReplaceCurrentFrame(std::unique_ptr<FrameType> frame) {
53 0 : frames_[current_] = std::move(frame);
54 0 : }
55 :
56 : // Marks all frames obsolete and resets the previous frame pointer. No
57 : // frames are freed though as the caller can still access them.
58 0 : void Reset() {
59 0 : for (int i = 0; i < kQueueLength; i++) {
60 0 : frames_[i].reset();
61 : }
62 0 : current_ = 0;
63 0 : }
64 :
65 0 : FrameType* current_frame() const {
66 0 : return frames_[current_].get();
67 : }
68 :
69 0 : FrameType* previous_frame() const {
70 0 : return frames_[(current_ + kQueueLength - 1) % kQueueLength].get();
71 : }
72 :
73 : private:
74 : // Index of the current frame.
75 : int current_;
76 :
77 : static const int kQueueLength = 2;
78 : std::unique_ptr<FrameType> frames_[kQueueLength];
79 :
80 : RTC_DISALLOW_COPY_AND_ASSIGN(ScreenCaptureFrameQueue);
81 : };
82 :
83 : } // namespace webrtc
84 :
85 : #endif // WEBRTC_MODULES_DESKTOP_CAPTURE_SCREEN_CAPTURE_FRAME_QUEUE_H_
|