summaryrefslogtreecommitdiffstats
path: root/media/video/capture/screen/screen_capturer.h
blob: d28b9351eeb6d2ea7ac5f15c55f1cd89996afe31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef MEDIA_VIDEO_CAPTURE_SCREEN_SCREEN_CAPTURER_H_
#define MEDIA_VIDEO_CAPTURE_SCREEN_SCREEN_CAPTURER_H_

#include "base/basictypes.h"
#include "base/callback.h"
#include "base/memory/ref_counted.h"
#include "base/shared_memory.h"
#include "media/base/media_export.h"
#include "media/video/capture/screen/shared_buffer.h"
#include "third_party/skia/include/core/SkRegion.h"

namespace media {

class ScreenCaptureData;
struct MouseCursorShape;
class SharedBuffer;

// Class used to capture video frames asynchronously.
//
// The full capture sequence is as follows:
//
// (1) Start
//     This is when pre-capture steps are executed, such as flagging the
//     display to prevent it from sleeping during a session.
//
// (2) InvalidateRegion
//     This is an optional step where regions of the screen are marked as
//     invalid. Some platforms (Windows, for now) won't use this and will
//     instead calculate the diff-regions later (in step (2). Other
//     platforms (Mac) will use this to mark all the changed regions of the
//     screen. Some limited rect-merging (e.g., to eliminate exact
//     duplicates) may be done here.
//
// (3) CaptureFrame
//     This is where the bits for the invalid rects are packaged up and sent
//     to the encoder.
//     A screen capture is performed if needed. For example, Windows requires
//     a capture to calculate the diff from the previous screen, whereas the
//     Mac version does not.
//
// (4) Stop
//     This is when post-capture steps are executed, such as releasing the
//     assertion that prevents the display from sleeping.
//
// Implementation has to ensure the following guarantees:
// 1. Double buffering
//    Since data can be read while another capture action is happening.
class MEDIA_EXPORT ScreenCapturer {
 public:
  // Provides callbacks used by the capturer to pass captured video frames and
  // mouse cursor shapes to the processing pipeline.
  class MEDIA_EXPORT Delegate {
   public:
    // Creates a shared memory buffer of the given size. Returns NULL if shared
    // buffers are not supported.
    virtual scoped_refptr<SharedBuffer> CreateSharedBuffer(uint32 size);

    // Notifies the delegate that the buffer is no longer used and can be
    // released.
    virtual void ReleaseSharedBuffer(scoped_refptr<SharedBuffer> buffer);

    // Called when a video frame has been captured. |capture_data| describes
    // a captured frame.
    virtual void OnCaptureCompleted(
        scoped_refptr<ScreenCaptureData> capture_data) = 0;

    // Called when the cursor shape has changed.
    // TODO(sergeyu): Move cursor shape capturing to a separate class.
    virtual void OnCursorShapeChanged(
        scoped_ptr<MouseCursorShape> cursor_shape) = 0;

   protected:
    virtual ~Delegate() {}
  };

  virtual ~ScreenCapturer() {}

  // Creates platform-specific capturer.
  static scoped_ptr<ScreenCapturer> Create();

#if defined(OS_LINUX)
  // Creates platform-specific capturer and instructs it whether it should use
  // X DAMAGE support.
  static scoped_ptr<ScreenCapturer> CreateWithXDamage(bool use_x_damage);
#elif defined(OS_WIN)
  // Creates Windows-specific capturer and instructs it whether or not to
  // disable desktop compositing.
  static scoped_ptr<ScreenCapturer> CreateWithDisableAero(bool disable_aero);
#endif

  // Called at the beginning of a capturing session. |delegate| must remain
  // valid until Stop() is called.
  virtual void Start(Delegate* delegate) = 0;

  // Called at the end of a capturing session.
  virtual void Stop() = 0;

  // Invalidates the specified region.
  virtual void InvalidateRegion(const SkRegion& invalid_region) = 0;

  // Captures the screen data associated with each of the accumulated
  // dirty region. When the capture is complete, the delegate is notified even
  // if the dirty region is empty.
  //
  // It is OK to call this method while another thread is reading
  // data of the previous capture. There can be at most one concurrent read
  // going on when this method is called.
  virtual void CaptureFrame() = 0;
};

}  // namespace media

#endif  // MEDIA_VIDEO_CAPTURE_SCREEN_SCREEN_CAPTURER_H_