Chromium Code Reviews| Index: media/capture/content/android/screen_capture_machine_android.cc |
| diff --git a/media/capture/content/android/screen_capture_machine_android.cc b/media/capture/content/android/screen_capture_machine_android.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..edee9e7c1a2e64d4791de486293d2b5f67b330eb |
| --- /dev/null |
| +++ b/media/capture/content/android/screen_capture_machine_android.cc |
| @@ -0,0 +1,142 @@ |
| +// Copyright 2016 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. |
| + |
| +#include "media/capture/content/android/screen_capture_machine_android.h" |
| + |
| +#include "base/android/context_utils.h" |
| +#include "base/android/jni_android.h" |
| +#include "base/android/scoped_java_ref.h" |
| +#include "jni/ScreenCapture_jni.h" |
| +#include "media/base/video_capture_types.h" |
| +#include "media/base/yuv_convert.h" |
| +#include "media/capture/content/video_capture_oracle.h" |
| +#include "third_party/libyuv/include/libyuv.h" |
| + |
| +using base::android::AttachCurrentThread; |
| + |
| +namespace media { |
| + |
| +// static |
| +bool ScreenCaptureMachineAndroid::RegisterScreenCaptureMachine(JNIEnv* env) { |
| + return RegisterNativesImpl(env); |
| +} |
| + |
| +ScreenCaptureMachineAndroid::ScreenCaptureMachineAndroid() {} |
| + |
| +ScreenCaptureMachineAndroid::~ScreenCaptureMachineAndroid() {} |
| + |
| +// static |
| +ScopedJavaLocalRef<jobject> |
| +ScreenCaptureMachineAndroid::createScreenCaptureMachineAndroid( |
| + jlong nativeScreenCaptureMachineAndroid) { |
| + return (Java_ScreenCapture_createScreenCaptureMachine( |
| + AttachCurrentThread(), base::android::GetApplicationContext(), |
| + nativeScreenCaptureMachineAndroid)); |
| +} |
| + |
| +void ScreenCaptureMachineAndroid::OnFrameAvailable(JNIEnv* env, |
| + jobject obj, |
| + jbyteArray data, |
| + jint cropWidth, |
| + jint cropHeight, |
| + jlong timestamp) { |
| + const VideoCaptureOracle::Event event = VideoCaptureOracle::kCompositorUpdate; |
| + uint64_t absolute_micro = |
| + static_cast<int64>(timestamp / base::Time::kNanosecondsPerMicrosecond); |
| + const base::TimeTicks start_time = |
| + base::TimeTicks::FromInternalValue(absolute_micro); |
| + scoped_refptr<VideoFrame> frame; |
| + ThreadSafeCaptureOracle::CaptureFrameCallback capture_frame_cb; |
| + |
| + bool oracle_decision = oracle_proxy_->ObserveEventAndDecideCapture( |
| + event, gfx::Rect(), start_time, &frame, &capture_frame_cb); |
| + |
| + if (!oracle_decision) |
| + return; |
| + |
| + jbyte* buffer = env->GetByteArrayElements(data, NULL); |
| + if (!buffer) |
| + return; |
| + |
| + uint8* source_argb = reinterpret_cast<uint8_t*>(buffer); |
| + int source_stride = cropWidth * 4; |
| + |
| + DCHECK(gfx::Size(cropWidth, cropHeight) == frame->visible_rect().size()); |
|
miu
2016/05/10 00:29:00
This CHECK will fail unless the client request fix
|
| + DCHECK(frame->format() == PIXEL_FORMAT_I420 || |
| + frame->format() == PIXEL_FORMAT_YV12); |
| + |
| + // TODO(braveyao): Some reports show that they failed to get YUV_420_888 |
| + // format from |
| + // MediaProjection on some devices. Check if it's a real problem and study if |
| + // it's |
| + // possible to move the colorspace conversion to GPU. |
| + ConvertRGB32ToYUV( |
| + source_argb, frame->visible_data(VideoFrame::kYPlane), |
| + frame->visible_data(VideoFrame::kUPlane), |
| + frame->visible_data(VideoFrame::kVPlane), frame->visible_rect().width(), |
| + frame->visible_rect().height(), source_stride, |
| + frame->stride(VideoFrame::kYPlane), frame->stride(VideoFrame::kUPlane)); |
| + |
| + // Deliver the populated VideoFrame. |
| + capture_frame_cb.Run(frame, start_time, true); |
| + |
| + env->ReleaseByteArrayElements(data, buffer, JNI_ABORT); |
| +} |
| + |
| +void ScreenCaptureMachineAndroid::OnActivityResult(JNIEnv* env, |
| + jobject obj, |
| + jboolean result) { |
| + if (!result) { |
| + oracle_proxy_->ReportError(FROM_HERE, "The user denied screen capture"); |
| + return; |
| + } |
| + |
| + Java_ScreenCapture_startCapture(env, obj); |
| +} |
| + |
| +void ScreenCaptureMachineAndroid::Start( |
| + const scoped_refptr<ThreadSafeCaptureOracle>& oracle_proxy, |
| + const VideoCaptureParams& params, |
| + const base::Callback<void(bool)> callback) { |
| + DCHECK(oracle_proxy.get()); |
| + oracle_proxy_ = oracle_proxy; |
| + |
| + j_capture_.Reset( |
| + createScreenCaptureMachineAndroid(reinterpret_cast<intptr_t>(this))); |
| + |
| + if (j_capture_.obj() == NULL) { |
| + LOG(ERROR) << "Failed to createScreenCaptureAndroid," |
| + " Maybe android version is too low"; |
| + callback.Run(false); |
| + return; |
| + } |
| + |
| + DCHECK(params.requested_format.frame_size.GetArea() > 0); |
| + DCHECK(!(params.requested_format.frame_size.width() % 2)); |
| + DCHECK(!(params.requested_format.frame_size.height() % 2)); |
| + |
| + jboolean ret = Java_ScreenCapture_startPrompt( |
| + AttachCurrentThread(), j_capture_.obj(), |
| + params.requested_format.frame_size.width(), |
| + params.requested_format.frame_size.height()); |
| + if (!ret) { |
| + callback.Run(false); |
| + return; |
| + } |
| + |
| + callback.Run(true); |
| +} |
| + |
| +void ScreenCaptureMachineAndroid::Stop(const base::Closure& callback) { |
| + Java_ScreenCapture_stopCapture(AttachCurrentThread(), j_capture_.obj()); |
| + |
| + callback.Run(); |
| +} |
| + |
| +// TODO(braveyao): On Android if the screen has no change, there will be no |
| +// captured frame, which would "pause" capture. Check how to satisfy downstream |
| +// consumers needing the extra "refreshing" frames. |
| +void ScreenCaptureMachineAndroid::MaybeCaptureForRefresh() {} |
| + |
| +} // namespace media |