Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 width, | |
| 42 jint height, | |
| 43 jint rowStride, | |
| 44 jlong timestamp) { | |
| 45 scoped_refptr<VideoFrame> i420_frame = VideoFrame::CreateFrame( | |
| 46 PIXEL_FORMAT_I420, gfx::Size(width, height), gfx::Rect(width, height), | |
| 47 gfx::Size(width, height), base::TimeDelta()); | |
| 48 | |
| 49 // ABGR little endian (rgba in memory) to I420. | |
| 50 libyuv::ABGRToI420( | |
| 51 reinterpret_cast<uint8_t*>(env->GetDirectBufferAddress(buf)), rowStride, | |
| 52 i420_frame->visible_data(VideoFrame::kYPlane), | |
| 53 i420_frame->stride(VideoFrame::kYPlane), | |
| 54 i420_frame->visible_data(VideoFrame::kUPlane), | |
| 55 i420_frame->stride(VideoFrame::kUPlane), | |
| 56 i420_frame->visible_data(VideoFrame::kVPlane), | |
| 57 i420_frame->stride(VideoFrame::kVPlane), | |
| 58 i420_frame->visible_rect().width(), i420_frame->visible_rect().height()); | |
| 59 | |
| 60 OnIncomingFrameAvailable(i420_frame->visible_data(VideoFrame::kYPlane), | |
| 61 i420_frame->stride(VideoFrame::kYPlane), | |
| 62 i420_frame->visible_data(VideoFrame::kUPlane), | |
| 63 i420_frame->stride(VideoFrame::kUPlane), | |
| 64 i420_frame->visible_data(VideoFrame::kVPlane), | |
| 65 i420_frame->stride(VideoFrame::kVPlane), | |
| 66 i420_frame->visible_rect().width(), | |
| 67 i420_frame->visible_rect().height(), | |
| 68 static_cast<int64_t>(timestamp)); | |
| 69 } | |
| 70 | |
| 71 void ScreenCaptureMachineAndroid::OnI420FrameAvailable(JNIEnv* env, | |
| 72 jobject obj, | |
| 73 jobject y_buffer, | |
| 74 jint y_stride, | |
| 75 jobject u_buffer, | |
| 76 jint u_stride, | |
| 77 jobject v_buffer, | |
| 78 jint v_stride, | |
| 79 jint width, | |
| 80 jint height, | |
| 81 jlong timestamp) { | |
| 82 uint8 *y_src, *u_src, *v_src; | |
| 83 y_src = reinterpret_cast<uint8_t*>(env->GetDirectBufferAddress(y_buffer)); | |
|
mcasas
2016/05/23 18:19:55
uint8* const y_src = ...
(here and for |u_src|, |v
braveyao
2016/05/24 00:03:26
Done.
| |
| 84 CHECK(y_src); | |
| 85 u_src = reinterpret_cast<uint8_t*>(env->GetDirectBufferAddress(u_buffer)); | |
| 86 CHECK(u_src); | |
| 87 v_src = reinterpret_cast<uint8_t*>(env->GetDirectBufferAddress(v_buffer)); | |
| 88 CHECK(v_src); | |
| 89 | |
| 90 // U and V planes have |pixelStride| = 2, unpack them here. | |
| 91 int uv_plane_len = (int)env->GetDirectBufferCapacity(u_buffer); | |
|
mcasas
2016/05/23 18:19:55
Hmm if you have to unpack, then incoming data
is N
braveyao
2016/05/24 00:03:26
Nope. libyuv::NV21ToI420 doesn't apply here. Is th
mcasas
2016/05/25 17:42:58
What do you mean it doesn't apply here?
I imagine
braveyao
2016/05/25 19:00:15
I don't understand how it could be NV12/NV21, whic
mcasas
2016/06/07 22:23:19
NV12:
2 pointers:
y_plane_nv12 => yyyyyyyyyyy...
| |
| 92 for (int index = 0; index * 2 <= uv_plane_len; index++) { | |
| 93 u_src[index] = u_src[index * 2]; | |
| 94 v_src[index] = v_src[index * 2]; | |
| 95 } | |
| 96 OnIncomingFrameAvailable(y_src, y_stride, u_src, u_stride, v_src, v_stride, | |
| 97 width, height, static_cast<int64_t>(timestamp)); | |
| 98 } | |
| 99 | |
| 100 void ScreenCaptureMachineAndroid::OnActivityResult(JNIEnv* env, | |
| 101 jobject obj, | |
| 102 jboolean result) { | |
| 103 if (!result) { | |
| 104 oracle_proxy_->ReportError(FROM_HERE, "The user denied screen capture"); | |
| 105 return; | |
| 106 } | |
| 107 | |
| 108 Java_ScreenCapture_startCapture(env, obj); | |
| 109 } | |
| 110 | |
| 111 void ScreenCaptureMachineAndroid::Start( | |
| 112 const scoped_refptr<ThreadSafeCaptureOracle>& oracle_proxy, | |
| 113 const VideoCaptureParams& params, | |
| 114 const base::Callback<void(bool)> callback) { | |
| 115 DCHECK(oracle_proxy.get()); | |
| 116 oracle_proxy_ = oracle_proxy; | |
| 117 | |
| 118 j_capture_.Reset( | |
| 119 createScreenCaptureMachineAndroid(reinterpret_cast<intptr_t>(this))); | |
| 120 | |
| 121 if (j_capture_.obj() == nullptr) { | |
| 122 LOG(ERROR) << "Failed to createScreenCaptureAndroid," | |
| 123 " Maybe android version is too low"; | |
| 124 callback.Run(false); | |
| 125 return; | |
| 126 } | |
| 127 | |
| 128 DCHECK(params.requested_format.frame_size.GetArea()); | |
| 129 DCHECK(!(params.requested_format.frame_size.width() % 2)); | |
| 130 DCHECK(!(params.requested_format.frame_size.height() % 2)); | |
| 131 | |
| 132 const jboolean ret = Java_ScreenCapture_startPrompt( | |
| 133 AttachCurrentThread(), j_capture_.obj(), | |
| 134 params.requested_format.frame_size.width(), | |
| 135 params.requested_format.frame_size.height()); | |
| 136 | |
| 137 callback.Run(ret); | |
| 138 } | |
| 139 | |
| 140 void ScreenCaptureMachineAndroid::Stop(const base::Closure& callback) { | |
| 141 Java_ScreenCapture_stopCapture(AttachCurrentThread(), j_capture_.obj()); | |
| 142 | |
| 143 callback.Run(); | |
| 144 } | |
| 145 | |
| 146 // ScreenCapture on Android works in passive way and there will be no captured | |
| 147 // frame when there is no update to the screen. When oracle asks capturing for | |
| 148 // refresh, the cached last frame will be delivered. | |
| 149 void ScreenCaptureMachineAndroid::MaybeCaptureForRefresh() { | |
| 150 if (lastFrame_.get() == nullptr) | |
| 151 return; | |
| 152 | |
| 153 OnIncomingFrameAvailable(lastFrame_->visible_data(VideoFrame::kYPlane), | |
| 154 lastFrame_->stride(VideoFrame::kYPlane), | |
| 155 lastFrame_->visible_data(VideoFrame::kUPlane), | |
| 156 lastFrame_->stride(VideoFrame::kUPlane), | |
| 157 lastFrame_->visible_data(VideoFrame::kVPlane), | |
| 158 lastFrame_->stride(VideoFrame::kVPlane), | |
| 159 lastFrame_->visible_rect().width(), | |
| 160 lastFrame_->visible_rect().height(), | |
| 161 base::TimeTicks::Now().ToInternalValue()); | |
| 162 } | |
| 163 | |
| 164 void ScreenCaptureMachineAndroid::OnIncomingFrameAvailable(const uint8_t* y_src, | |
| 165 int y_stride, | |
| 166 const uint8_t* u_src, | |
| 167 int u_stride, | |
| 168 const uint8_t* v_src, | |
| 169 int v_stride, | |
| 170 int width, | |
| 171 int height, | |
| 172 int64_t timestamp) { | |
| 173 const VideoCaptureOracle::Event event = VideoCaptureOracle::kCompositorUpdate; | |
| 174 const uint64_t absolute_micro = | |
| 175 timestamp / base::Time::kNanosecondsPerMicrosecond; | |
| 176 const base::TimeTicks start_time = | |
| 177 base::TimeTicks::FromInternalValue(absolute_micro); | |
| 178 scoped_refptr<VideoFrame> frame; | |
| 179 ThreadSafeCaptureOracle::CaptureFrameCallback capture_frame_cb; | |
| 180 | |
| 181 if (!oracle_proxy_->ObserveEventAndDecideCapture( | |
| 182 event, gfx::Rect(), start_time, &frame, &capture_frame_cb)) { | |
| 183 return; | |
| 184 } | |
| 185 | |
| 186 DCHECK(frame->format() == PIXEL_FORMAT_I420 || | |
| 187 frame->format() == PIXEL_FORMAT_YV12); | |
| 188 | |
| 189 libyuv::I420Scale(y_src, y_stride, u_src, u_stride, v_src, v_stride, width, | |
| 190 height, frame->visible_data(VideoFrame::kYPlane), | |
| 191 frame->stride(VideoFrame::kYPlane), | |
| 192 frame->visible_data(VideoFrame::kUPlane), | |
| 193 frame->stride(VideoFrame::kUPlane), | |
| 194 frame->visible_data(VideoFrame::kVPlane), | |
| 195 frame->stride(VideoFrame::kVPlane), | |
| 196 frame->visible_rect().width(), | |
| 197 frame->visible_rect().height(), libyuv::kFilterBilinear); | |
| 198 | |
| 199 // Deliver the populated VideoFrame. | |
| 200 capture_frame_cb.Run(frame, start_time, true); | |
| 201 | |
| 202 lastFrame_ = frame; | |
| 203 } | |
| 204 | |
| 205 } // namespace media | |
| OLD | NEW |