screen_capture_frame_queue.h 2.9 KB

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