Home | History | Annotate | Download | only in desktop_capture
      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 "webrtc/modules/desktop_capture/shared_desktop_frame.h"
     15 #include "webrtc/system_wrappers/interface/scoped_ptr.h"
     16 #include "webrtc/typedefs.h"
     17 
     18 namespace webrtc {
     19 class DesktopFrame;
     20 }  // namespace webrtc
     21 
     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).
     37 class ScreenCaptureFrameQueue {
     38  public:
     39   ScreenCaptureFrameQueue();
     40   ~ScreenCaptureFrameQueue();
     41 
     42   // Moves to the next frame in the queue, moving the 'current' frame to become
     43   // the 'previous' one.
     44   void MoveToNextFrame();
     45 
     46   // Replaces the current frame with a new one allocated by the caller. The
     47   // existing frame (if any) is destroyed. Takes ownership of |frame|.
     48   void ReplaceCurrentFrame(DesktopFrame* frame);
     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.
     52   void Reset();
     53 
     54   SharedDesktopFrame* current_frame() const {
     55     return frames_[current_].get();
     56   }
     57 
     58   SharedDesktopFrame* previous_frame() const {
     59     return frames_[(current_ + kQueueLength - 1) % kQueueLength].get();
     60   }
     61 
     62  private:
     63   // Index of the current frame.
     64   int current_;
     65 
     66   static const int kQueueLength = 2;
     67   scoped_ptr<SharedDesktopFrame> frames_[kQueueLength];
     68 
     69   DISALLOW_COPY_AND_ASSIGN(ScreenCaptureFrameQueue);
     70 };
     71 
     72 }  // namespace webrtc
     73 
     74 #endif  // WEBRTC_MODULES_DESKTOP_CAPTURE_SCREEN_CAPTURE_FRAME_QUEUE_H_
     75