Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(648)

Side by Side Diff: media/capture/content/android/screen_capture_machine_android.cc

Issue 1917023003: ScreenCapture for Android phase1, part I (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: correct RGBA frame convert-scale error and rebase Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "media/capture/content/android/screen_capture_machine_android.h"
6
7 #include "base/android/context_utils.h"
8 #include "base/android/jni_android.h"
9 #include "base/android/scoped_java_ref.h"
10 #include "jni/ScreenCapture_jni.h"
11 #include "media/base/video_capture_types.h"
12 #include "media/base/yuv_convert.h"
13 #include "media/capture/content/video_capture_oracle.h"
14 #include "third_party/libyuv/include/libyuv.h"
15
16 using base::android::AttachCurrentThread;
17
18 namespace media {
19
20 // static
21 bool ScreenCaptureMachineAndroid::RegisterScreenCaptureMachine(JNIEnv* env) {
22 return RegisterNativesImpl(env);
23 }
24
25 ScreenCaptureMachineAndroid::ScreenCaptureMachineAndroid() {}
26
27 ScreenCaptureMachineAndroid::~ScreenCaptureMachineAndroid() {}
28
29 // static
30 ScopedJavaLocalRef<jobject>
31 ScreenCaptureMachineAndroid::createScreenCaptureMachineAndroid(
32 jlong nativeScreenCaptureMachineAndroid) {
33 return (Java_ScreenCapture_createScreenCaptureMachine(
34 AttachCurrentThread(), base::android::GetApplicationContext(),
35 nativeScreenCaptureMachineAndroid));
36 }
37
38 void ScreenCaptureMachineAndroid::OnRGBAFrameAvailable(JNIEnv* env,
39 jobject obj,
40 jobject buf,
41 jint row_stride,
42 jint left,
43 jint top,
44 jint width,
45 jint height,
46 jlong timestamp) {
47 const VideoCaptureOracle::Event event = VideoCaptureOracle::kCompositorUpdate;
48 const uint64_t absolute_micro =
49 timestamp / base::Time::kNanosecondsPerMicrosecond;
50 const base::TimeTicks start_time =
51 base::TimeTicks() + base::TimeDelta::FromMicroseconds(absolute_micro);
52 scoped_refptr<VideoFrame> frame;
53 ThreadSafeCaptureOracle::CaptureFrameCallback capture_frame_cb;
54
55 if (!oracle_proxy_->ObserveEventAndDecideCapture(
56 event, gfx::Rect(), start_time, &frame, &capture_frame_cb)) {
57 return;
58 }
59
60 DCHECK(frame->format() == PIXEL_FORMAT_I420 ||
61 frame->format() == PIXEL_FORMAT_YV12);
62
63 scoped_refptr<VideoFrame> unscaled_frame = frame;
64 if (frame->visible_rect().width() != width ||
65 frame->visible_rect().height() != height) {
66 unscaled_frame = VideoFrame::CreateFrame(
67 PIXEL_FORMAT_I420, gfx::Size(width, height), gfx::Rect(width, height),
68 gfx::Size(width, height), base::TimeDelta());
69 }
70
71 uint8_t* const src =
72 reinterpret_cast<uint8_t*>(env->GetDirectBufferAddress(buf));
73 CHECK(src);
74
75 const int offset = top * row_stride + left * 4;
76 // ABGR little endian (rgba in memory) to I420.
mcasas 2016/06/22 22:23:44 I wonder if this holds true for all Android proces
braveyao 2016/06/22 23:56:13 Quote Android guys, "we depend on locked RGBA buff
77 libyuv::ABGRToI420(src + offset, row_stride,
78 unscaled_frame->visible_data(VideoFrame::kYPlane),
79 unscaled_frame->stride(VideoFrame::kYPlane),
80 unscaled_frame->visible_data(VideoFrame::kUPlane),
81 unscaled_frame->stride(VideoFrame::kUPlane),
82 unscaled_frame->visible_data(VideoFrame::kVPlane),
83 unscaled_frame->stride(VideoFrame::kVPlane),
84 unscaled_frame->visible_rect().width(),
85 unscaled_frame->visible_rect().height());
86
87 if (unscaled_frame != frame) {
88 libyuv::I420Scale(unscaled_frame->visible_data(VideoFrame::kYPlane),
89 unscaled_frame->stride(VideoFrame::kYPlane),
90 unscaled_frame->visible_data(VideoFrame::kUPlane),
91 unscaled_frame->stride(VideoFrame::kUPlane),
92 unscaled_frame->visible_data(VideoFrame::kVPlane),
93 unscaled_frame->stride(VideoFrame::kVPlane),
94 unscaled_frame->visible_rect().width(),
95 unscaled_frame->visible_rect().height(),
96 frame->visible_data(VideoFrame::kYPlane),
97 frame->stride(VideoFrame::kYPlane),
98 frame->visible_data(VideoFrame::kUPlane),
99 frame->stride(VideoFrame::kUPlane),
100 frame->visible_data(VideoFrame::kVPlane),
101 frame->stride(VideoFrame::kVPlane),
102 frame->visible_rect().width(),
103 frame->visible_rect().height(), libyuv::kFilterBilinear);
104 }
mcasas 2016/06/22 22:23:44 In response to your comment to my comment in PS8 l
braveyao 2016/06/22 23:56:13 Acknowledged.
105
106 capture_frame_cb.Run(frame, start_time, true);
107
108 lastFrame_ = frame;
109 }
110
111 void ScreenCaptureMachineAndroid::OnI420FrameAvailable(JNIEnv* env,
112 jobject obj,
113 jobject y_buffer,
114 jint y_stride,
115 jobject u_buffer,
116 jobject v_buffer,
117 jint uv_row_stride,
118 jint uv_pixel_stride,
119 jint left,
120 jint top,
121 jint width,
122 jint height,
123 jlong timestamp) {
124 uint8_t* const y_src =
125 reinterpret_cast<uint8_t*>(env->GetDirectBufferAddress(y_buffer));
126 CHECK(y_src);
127 uint8_t* u_src =
128 reinterpret_cast<uint8_t*>(env->GetDirectBufferAddress(u_buffer));
129 CHECK(u_src);
130 uint8_t* v_src =
131 reinterpret_cast<uint8_t*>(env->GetDirectBufferAddress(v_buffer));
132 CHECK(v_src);
133
134 // De-interleave the U and V planes into temporary buffers, if needed.
135 int uv_stride = uv_row_stride;
136 std::unique_ptr<uint8_t[]> u_tmp, v_tmp;
137 if (uv_pixel_stride != 1) {
138 // U and V planes are actually interleaved, unpack them here.
139 // TODO(braveyao): According to
140 // https://developer.android.com/reference/android/graphics/ImageFormat.html #YUV_420_888,
141 // how U and V planes are interlaced is not guaranteed, so there is no an
142 // existing libyuv function suitable for such a job. Filed a request at
143 // https://bugs.chromium.org/p/libyuv/issues/detail?id=604. Switch to new
144 // function when it's available.
145 const int uv_plane_len = (int)env->GetDirectBufferCapacity(u_buffer);
146 u_tmp.reset(new uint8_t[(uv_plane_len + 1) / uv_pixel_stride]);
147 v_tmp.reset(new uint8_t[(uv_plane_len + 1) / uv_pixel_stride]);
148 for (int index = 0; index * uv_pixel_stride <= uv_plane_len; index++) {
149 u_tmp[index] = u_src[index * uv_pixel_stride];
150 v_tmp[index] = v_src[index * uv_pixel_stride];
151 }
152 u_src = u_tmp.get();
153 v_src = v_tmp.get();
154 uv_stride /= uv_pixel_stride;
155 }
156
157 const int y_offset = top * y_stride + left;
158 const int uv_offset = (top / 2) * uv_stride + left / 2;
159 OnIncomingFrameAvailable(y_src + y_offset, y_stride, u_src + uv_offset,
160 uv_stride, v_src + uv_offset, uv_stride, width,
161 height, static_cast<int64_t>(timestamp));
162 }
163
164 void ScreenCaptureMachineAndroid::OnActivityResult(JNIEnv* env,
165 jobject obj,
166 jboolean result) {
167 if (!result) {
168 oracle_proxy_->ReportError(FROM_HERE, "The user denied screen capture");
169 return;
170 }
171
172 Java_ScreenCapture_startCapture(env, obj);
173 }
174
175 void ScreenCaptureMachineAndroid::Start(
176 const scoped_refptr<ThreadSafeCaptureOracle>& oracle_proxy,
177 const VideoCaptureParams& params,
178 const base::Callback<void(bool)> callback) {
179 DCHECK(oracle_proxy.get());
180 oracle_proxy_ = oracle_proxy;
181
182 j_capture_.Reset(
183 createScreenCaptureMachineAndroid(reinterpret_cast<intptr_t>(this)));
184
185 if (j_capture_.obj() == nullptr) {
186 DLOG(ERROR) << "Failed to createScreenCaptureAndroid";
187 callback.Run(false);
188 return;
189 }
190
191 DCHECK(params.requested_format.frame_size.GetArea());
192 DCHECK(!(params.requested_format.frame_size.width() % 2));
193 DCHECK(!(params.requested_format.frame_size.height() % 2));
194
195 const jboolean ret = Java_ScreenCapture_startPrompt(
196 AttachCurrentThread(), j_capture_.obj(),
197 params.requested_format.frame_size.width(),
198 params.requested_format.frame_size.height());
199
200 callback.Run(ret);
201 }
202
203 void ScreenCaptureMachineAndroid::Stop(const base::Closure& callback) {
204 Java_ScreenCapture_stopCapture(AttachCurrentThread(), j_capture_.obj());
205
206 callback.Run();
207 }
208
209 // ScreenCapture on Android works in a passive way and there are no captured
210 // frames when there is no update to the screen. When the oracle asks for a
211 // capture refresh, the cached captured frame is redelivered.
212 void ScreenCaptureMachineAndroid::MaybeCaptureForRefresh() {
213 if (lastFrame_.get() == nullptr)
214 return;
215
216 OnIncomingFrameAvailable(lastFrame_->visible_data(VideoFrame::kYPlane),
217 lastFrame_->stride(VideoFrame::kYPlane),
218 lastFrame_->visible_data(VideoFrame::kUPlane),
219 lastFrame_->stride(VideoFrame::kUPlane),
220 lastFrame_->visible_data(VideoFrame::kVPlane),
221 lastFrame_->stride(VideoFrame::kVPlane),
222 lastFrame_->visible_rect().width(),
223 lastFrame_->visible_rect().height(),
224 base::TimeTicks::Now().ToInternalValue());
225 }
226
227 void ScreenCaptureMachineAndroid::OnIncomingFrameAvailable(const uint8_t* y_src,
228 int y_stride,
229 const uint8_t* u_src,
230 int u_stride,
231 const uint8_t* v_src,
232 int v_stride,
233 int width,
234 int height,
235 int64_t timestamp) {
236 const VideoCaptureOracle::Event event = VideoCaptureOracle::kCompositorUpdate;
237 const uint64_t absolute_micro =
238 timestamp / base::Time::kNanosecondsPerMicrosecond;
239 const base::TimeTicks start_time =
240 base::TimeTicks() + base::TimeDelta::FromMicroseconds(absolute_micro);
241 scoped_refptr<VideoFrame> frame;
242 ThreadSafeCaptureOracle::CaptureFrameCallback capture_frame_cb;
243
244 if (!oracle_proxy_->ObserveEventAndDecideCapture(
245 event, gfx::Rect(), start_time, &frame, &capture_frame_cb)) {
246 return;
247 }
248
249 DCHECK(frame->format() == PIXEL_FORMAT_I420 ||
250 frame->format() == PIXEL_FORMAT_YV12);
251
252 libyuv::I420Scale(y_src, y_stride, u_src, u_stride, v_src, v_stride, width,
253 height, frame->visible_data(VideoFrame::kYPlane),
254 frame->stride(VideoFrame::kYPlane),
255 frame->visible_data(VideoFrame::kUPlane),
256 frame->stride(VideoFrame::kUPlane),
257 frame->visible_data(VideoFrame::kVPlane),
258 frame->stride(VideoFrame::kVPlane),
259 frame->visible_rect().width(),
260 frame->visible_rect().height(), libyuv::kFilterBilinear);
261
262 capture_frame_cb.Run(frame, start_time, true);
263
264 lastFrame_ = frame;
265 }
266
267 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698