sergeyu@chromium.org | 3d34f66 | 2013-06-04 18:51:23 +0000 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef MODULES_DESKTOP_CAPTURE_SCREEN_CAPTURE_FRAME_QUEUE_H_ |
| 12 | #define MODULES_DESKTOP_CAPTURE_SCREEN_CAPTURE_FRAME_QUEUE_H_ |
sergeyu@chromium.org | 3d34f66 | 2013-06-04 18:51:23 +0000 | [diff] [blame] | 13 | |
kwiberg | 2bb3afa | 2016-03-16 15:58:08 -0700 | [diff] [blame] | 14 | #include <memory> |
| 15 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #include "rtc_base/constructormagic.h" |
zijiehe | 809dcb4 | 2016-04-22 16:08:39 -0700 | [diff] [blame] | 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. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 19 | #include "modules/desktop_capture/desktop_frame.h" // Remove |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 20 | #include "modules/desktop_capture/shared_desktop_frame.h" // Remove |
torbjorng | 1bcb8f0 | 2016-04-21 14:18:19 -0700 | [diff] [blame] | 21 | |
sergeyu@chromium.org | 3d34f66 | 2013-06-04 18:51:23 +0000 | [diff] [blame] | 22 | namespace webrtc { |
| 23 | |
| 24 | // Represents a queue of reusable video frames. Provides access to the 'current' |
| 25 | // frame - the frame that the caller is working with at the moment, and to the |
| 26 | // 'previous' frame - the predecessor of the current frame swapped by |
| 27 | // MoveToNextFrame() call, if any. |
| 28 | // |
| 29 | // The caller is expected to (re)allocate frames if current_frame() returns |
| 30 | // NULL. The caller can mark all frames in the queue for reallocation (when, |
| 31 | // say, frame dimensions change). The queue records which frames need updating |
| 32 | // which the caller can query. |
| 33 | // |
| 34 | // Frame consumer is expected to never hold more than kQueueLength frames |
| 35 | // created by this function and it should release the earliest one before trying |
| 36 | // to capture a new frame (i.e. before MoveToNextFrame() is called). |
zijiehe | 809dcb4 | 2016-04-22 16:08:39 -0700 | [diff] [blame] | 37 | template <typename FrameType> |
sergeyu@chromium.org | 3d34f66 | 2013-06-04 18:51:23 +0000 | [diff] [blame] | 38 | class ScreenCaptureFrameQueue { |
| 39 | public: |
zijiehe | 809dcb4 | 2016-04-22 16:08:39 -0700 | [diff] [blame] | 40 | ScreenCaptureFrameQueue() : current_(0) {} |
| 41 | ~ScreenCaptureFrameQueue() = default; |
sergeyu@chromium.org | 3d34f66 | 2013-06-04 18:51:23 +0000 | [diff] [blame] | 42 | |
| 43 | // Moves to the next frame in the queue, moving the 'current' frame to become |
| 44 | // the 'previous' one. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 45 | void MoveToNextFrame() { current_ = (current_ + 1) % kQueueLength; } |
sergeyu@chromium.org | 3d34f66 | 2013-06-04 18:51:23 +0000 | [diff] [blame] | 46 | |
| 47 | // Replaces the current frame with a new one allocated by the caller. The |
| 48 | // existing frame (if any) is destroyed. Takes ownership of |frame|. |
sergeyu | 5d91028 | 2016-06-07 16:41:58 -0700 | [diff] [blame] | 49 | void ReplaceCurrentFrame(std::unique_ptr<FrameType> frame) { |
| 50 | frames_[current_] = std::move(frame); |
zijiehe | 809dcb4 | 2016-04-22 16:08:39 -0700 | [diff] [blame] | 51 | } |
sergeyu@chromium.org | 3d34f66 | 2013-06-04 18:51:23 +0000 | [diff] [blame] | 52 | |
| 53 | // Marks all frames obsolete and resets the previous frame pointer. No |
| 54 | // frames are freed though as the caller can still access them. |
zijiehe | 809dcb4 | 2016-04-22 16:08:39 -0700 | [diff] [blame] | 55 | void Reset() { |
| 56 | for (int i = 0; i < kQueueLength; i++) { |
| 57 | frames_[i].reset(); |
| 58 | } |
| 59 | current_ = 0; |
| 60 | } |
sergeyu@chromium.org | 3d34f66 | 2013-06-04 18:51:23 +0000 | [diff] [blame] | 61 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 62 | FrameType* current_frame() const { return frames_[current_].get(); } |
sergeyu@chromium.org | 3d34f66 | 2013-06-04 18:51:23 +0000 | [diff] [blame] | 63 | |
zijiehe | 809dcb4 | 2016-04-22 16:08:39 -0700 | [diff] [blame] | 64 | FrameType* previous_frame() const { |
sergeyu@chromium.org | 3d34f66 | 2013-06-04 18:51:23 +0000 | [diff] [blame] | 65 | return frames_[(current_ + kQueueLength - 1) % kQueueLength].get(); |
| 66 | } |
| 67 | |
| 68 | private: |
| 69 | // Index of the current frame. |
| 70 | int current_; |
| 71 | |
| 72 | static const int kQueueLength = 2; |
zijiehe | 809dcb4 | 2016-04-22 16:08:39 -0700 | [diff] [blame] | 73 | std::unique_ptr<FrameType> frames_[kQueueLength]; |
sergeyu@chromium.org | 3d34f66 | 2013-06-04 18:51:23 +0000 | [diff] [blame] | 74 | |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 75 | RTC_DISALLOW_COPY_AND_ASSIGN(ScreenCaptureFrameQueue); |
sergeyu@chromium.org | 3d34f66 | 2013-06-04 18:51:23 +0000 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | } // namespace webrtc |
| 79 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 80 | #endif // MODULES_DESKTOP_CAPTURE_SCREEN_CAPTURE_FRAME_QUEUE_H_ |