OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2016 The WebRTC project authors. All Rights Reserved. | 2 * Copyright 2016 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 17 matching lines...) Expand all Loading... |
28 * Capturing is done by {@code MediaProjection} on a {@code SurfaceTexture}. We
interact with this | 28 * Capturing is done by {@code MediaProjection} on a {@code SurfaceTexture}. We
interact with this |
29 * {@code SurfaceTexture} using a {@code SurfaceTextureHelper}. | 29 * {@code SurfaceTexture} using a {@code SurfaceTextureHelper}. |
30 * The {@code SurfaceTextureHelper} is created by the native code and passed to
this capturer in | 30 * The {@code SurfaceTextureHelper} is created by the native code and passed to
this capturer in |
31 * {@code VideoCapturer.initialize()}. On receiving a new frame, this capturer p
asses it | 31 * {@code VideoCapturer.initialize()}. On receiving a new frame, this capturer p
asses it |
32 * as a texture to the native code via {@code CapturerObserver.onTextureFrameCap
tured()}. This takes | 32 * as a texture to the native code via {@code CapturerObserver.onTextureFrameCap
tured()}. This takes |
33 * place on the HandlerThread of the given {@code SurfaceTextureHelper}. When do
ne with each frame, | 33 * place on the HandlerThread of the given {@code SurfaceTextureHelper}. When do
ne with each frame, |
34 * the native code returns the buffer to the {@code SurfaceTextureHelper} to be
used for new | 34 * the native code returns the buffer to the {@code SurfaceTextureHelper} to be
used for new |
35 * frames. At any time, at most one frame is being processed. | 35 * frames. At any time, at most one frame is being processed. |
36 */ | 36 */ |
37 @TargetApi(21) | 37 @TargetApi(21) |
38 public class ScreenCapturerAndroid implements | 38 public class ScreenCapturerAndroid |
39 VideoCapturer, SurfaceTextureHelper.OnTextureFrameAvailableListener { | 39 implements VideoCapturer, SurfaceTextureHelper.OnTextureFrameAvailableListen
er { |
40 | 40 private static final int DISPLAY_FLAGS = |
41 private static final int DISPLAY_FLAGS = DisplayManager.VIRTUAL_DISPLAY_FLAG_P
UBLIC | 41 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC | DisplayManager.VIRTUAL_DISPLA
Y_FLAG_PRESENTATION; |
42 | DisplayManager.VIRTUAL_DISPLAY_FLAG_PRESENTATION; | |
43 // DPI for VirtualDisplay, does not seem to matter for us. | 42 // DPI for VirtualDisplay, does not seem to matter for us. |
44 private static final int VIRTUAL_DISPLAY_DPI = 400; | 43 private static final int VIRTUAL_DISPLAY_DPI = 400; |
45 | 44 |
46 private final Intent mediaProjectionPermissionResultData; | 45 private final Intent mediaProjectionPermissionResultData; |
47 private final MediaProjection.Callback mediaProjectionCallback; | 46 private final MediaProjection.Callback mediaProjectionCallback; |
48 | 47 |
49 private int width; | 48 private int width; |
50 private int height; | 49 private int height; |
51 private VirtualDisplay virtualDisplay; | 50 private VirtualDisplay virtualDisplay; |
52 private SurfaceTextureHelper surfaceTextureHelper; | 51 private SurfaceTextureHelper surfaceTextureHelper; |
53 private CapturerObserver capturerObserver; | 52 private CapturerObserver capturerObserver; |
54 private long numCapturedFrames = 0; | 53 private long numCapturedFrames = 0; |
55 private MediaProjection mediaProjection; | 54 private MediaProjection mediaProjection; |
56 private boolean isDisposed = false; | 55 private boolean isDisposed = false; |
57 private MediaProjectionManager mediaProjectionManager; | 56 private MediaProjectionManager mediaProjectionManager; |
58 | 57 |
59 /** | 58 /** |
60 * Constructs a new Screen Capturer. | 59 * Constructs a new Screen Capturer. |
61 * | 60 * |
62 * @param mediaProjectionPermissionResultData the result data of MediaProjecti
on permission | 61 * @param mediaProjectionPermissionResultData the result data of MediaProjecti
on permission |
63 * activity; the calling app must validate that result code is Activity.RE
SULT_OK before | 62 * activity; the calling app must validate that result code is Activity.RE
SULT_OK before |
64 * calling this method. | 63 * calling this method. |
65 * @param mediaProjectionCallback MediaProjection callback to implement applic
ation specific | 64 * @param mediaProjectionCallback MediaProjection callback to implement applic
ation specific |
66 * logic in events such as when the user revokes a previously granted capt
ure permission. | 65 * logic in events such as when the user revokes a previously granted capt
ure permission. |
67 **/ | 66 **/ |
68 public ScreenCapturerAndroid( | 67 public ScreenCapturerAndroid(Intent mediaProjectionPermissionResultData, |
69 Intent mediaProjectionPermissionResultData, | |
70 MediaProjection.Callback mediaProjectionCallback) { | 68 MediaProjection.Callback mediaProjectionCallback) { |
71 this.mediaProjectionPermissionResultData = mediaProjectionPermissionResultDa
ta; | 69 this.mediaProjectionPermissionResultData = mediaProjectionPermissionResultDa
ta; |
72 this.mediaProjectionCallback = mediaProjectionCallback; | 70 this.mediaProjectionCallback = mediaProjectionCallback; |
73 } | 71 } |
74 | 72 |
75 private void checkNotDisposed() { | 73 private void checkNotDisposed() { |
76 if (isDisposed) { | 74 if (isDisposed) { |
77 throw new RuntimeException("capturer is disposed."); | 75 throw new RuntimeException("capturer is disposed."); |
78 } | 76 } |
79 } | 77 } |
80 | 78 |
81 @Override | 79 @Override |
82 public synchronized void initialize( | 80 public synchronized void initialize(final SurfaceTextureHelper surfaceTextureH
elper, |
83 final SurfaceTextureHelper surfaceTextureHelper, | 81 final Context applicationContext, final VideoCapturer.CapturerObserver cap
turerObserver) { |
84 final Context applicationContext, | |
85 final VideoCapturer.CapturerObserver capturerObserver) { | |
86 checkNotDisposed(); | 82 checkNotDisposed(); |
87 | 83 |
88 if (capturerObserver == null) { | 84 if (capturerObserver == null) { |
89 throw new RuntimeException("capturerObserver not set."); | 85 throw new RuntimeException("capturerObserver not set."); |
90 } | 86 } |
91 this.capturerObserver = capturerObserver; | 87 this.capturerObserver = capturerObserver; |
92 | 88 |
93 if (surfaceTextureHelper == null) { | 89 if (surfaceTextureHelper == null) { |
94 throw new RuntimeException("surfaceTextureHelper not set."); | 90 throw new RuntimeException("surfaceTextureHelper not set."); |
95 } | 91 } |
96 this.surfaceTextureHelper = surfaceTextureHelper; | 92 this.surfaceTextureHelper = surfaceTextureHelper; |
97 | 93 |
98 mediaProjectionManager = (MediaProjectionManager) | 94 mediaProjectionManager = (MediaProjectionManager) applicationContext.getSyst
emService( |
99 applicationContext.getSystemService(Context.MEDIA_PROJECTION_SERVICE); | 95 Context.MEDIA_PROJECTION_SERVICE); |
100 } | 96 } |
101 | 97 |
102 @Override | 98 @Override |
103 public synchronized void startCapture(final int width, final int height, | 99 public synchronized void startCapture( |
104 final int ignoredFramerate) { | 100 final int width, final int height, final int ignoredFramerate) { |
105 checkNotDisposed(); | 101 checkNotDisposed(); |
106 | 102 |
107 this.width = width; | 103 this.width = width; |
108 this.height = height; | 104 this.height = height; |
109 | 105 |
110 mediaProjection = mediaProjectionManager.getMediaProjection( | 106 mediaProjection = mediaProjectionManager.getMediaProjection( |
111 Activity.RESULT_OK, mediaProjectionPermissionResultData); | 107 Activity.RESULT_OK, mediaProjectionPermissionResultData); |
112 | 108 |
113 // Let MediaProjection callback use the SurfaceTextureHelper thread. | 109 // Let MediaProjection callback use the SurfaceTextureHelper thread. |
114 mediaProjection.registerCallback(mediaProjectionCallback, surfaceTextureHelp
er.getHandler()); | 110 mediaProjection.registerCallback(mediaProjectionCallback, surfaceTextureHelp
er.getHandler()); |
(...skipping 21 matching lines...) Expand all Loading... |
136 // Unregister the callback before stopping, otherwise the callback rec
ursively | 132 // Unregister the callback before stopping, otherwise the callback rec
ursively |
137 // calls this method. | 133 // calls this method. |
138 mediaProjection.unregisterCallback(mediaProjectionCallback); | 134 mediaProjection.unregisterCallback(mediaProjectionCallback); |
139 mediaProjection.stop(); | 135 mediaProjection.stop(); |
140 mediaProjection = null; | 136 mediaProjection = null; |
141 } | 137 } |
142 } | 138 } |
143 }); | 139 }); |
144 } | 140 } |
145 | 141 |
146 | |
147 @Override | 142 @Override |
148 public synchronized void dispose() { | 143 public synchronized void dispose() { |
149 isDisposed = true; | 144 isDisposed = true; |
150 } | 145 } |
151 | 146 |
152 /** | 147 /** |
153 * Changes output video format. This method can be used to scale the output | 148 * Changes output video format. This method can be used to scale the output |
154 * video, or to change orientation when the captured screen is rotated for exa
mple. | 149 * video, or to change orientation when the captured screen is rotated for exa
mple. |
155 * | 150 * |
156 * @param width new output video width | 151 * @param width new output video width |
(...skipping 20 matching lines...) Expand all Loading... |
177 @Override | 172 @Override |
178 public void run() { | 173 public void run() { |
179 virtualDisplay.release(); | 174 virtualDisplay.release(); |
180 createVirtualDisplay(); | 175 createVirtualDisplay(); |
181 } | 176 } |
182 }); | 177 }); |
183 } | 178 } |
184 | 179 |
185 private void createVirtualDisplay() { | 180 private void createVirtualDisplay() { |
186 surfaceTextureHelper.getSurfaceTexture().setDefaultBufferSize(width, height)
; | 181 surfaceTextureHelper.getSurfaceTexture().setDefaultBufferSize(width, height)
; |
187 virtualDisplay = mediaProjection.createVirtualDisplay( | 182 virtualDisplay = mediaProjection.createVirtualDisplay("WebRTC_ScreenCapture"
, width, height, |
188 "WebRTC_ScreenCapture", width, height, VIRTUAL_DISPLAY_DPI, | 183 VIRTUAL_DISPLAY_DPI, DISPLAY_FLAGS, new Surface(surfaceTextureHelper.get
SurfaceTexture()), |
189 DISPLAY_FLAGS, new Surface(surfaceTextureHelper.getSurfaceTexture()), | |
190 null /* callback */, null /* callback handler */); | 184 null /* callback */, null /* callback handler */); |
191 } | 185 } |
192 | 186 |
193 // This is called on the internal looper thread of {@Code SurfaceTextureHelper
}. | 187 // This is called on the internal looper thread of {@Code SurfaceTextureHelper
}. |
194 @Override | 188 @Override |
195 public void onTextureFrameAvailable(int oesTextureId, float[] transformMatrix,
long timestampNs) { | 189 public void onTextureFrameAvailable(int oesTextureId, float[] transformMatrix,
long timestampNs) { |
196 numCapturedFrames++; | 190 numCapturedFrames++; |
197 capturerObserver.onTextureFrameCaptured(width, height, oesTextureId, transfo
rmMatrix, | 191 capturerObserver.onTextureFrameCaptured( |
198 0 /* rotation */, timestampNs); | 192 width, height, oesTextureId, transformMatrix, 0 /* rotation */, timestam
pNs); |
199 } | 193 } |
200 | 194 |
201 @Override | 195 @Override |
202 public boolean isScreencast() { | 196 public boolean isScreencast() { |
203 return true; | 197 return true; |
204 } | 198 } |
205 | 199 |
206 public long getNumCapturedFrames() { | 200 public long getNumCapturedFrames() { |
207 return numCapturedFrames; | 201 return numCapturedFrames; |
208 } | 202 } |
209 } | 203 } |
210 | |
OLD | NEW |