Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 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/jni_android.h" | |
| 8 #include "base/android/scoped_java_ref.h" | |
| 9 #include "jni/ScreenCapture_jni.h" | |
| 10 #include "media/base/video_capture_types.h" | |
| 11 #include "media/base/yuv_convert.h" | |
| 12 #include "media/capture/content/android/screen_capture_factory_android.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 media::RegisterNativesImpl(env); | |
| 23 } | |
| 24 | |
| 25 ScreenCaptureMachineAndroid::ScreenCaptureMachineAndroid() {} | |
| 26 | |
| 27 ScreenCaptureMachineAndroid::~ScreenCaptureMachineAndroid() {} | |
| 28 | |
| 29 void ScreenCaptureMachineAndroid::OnFrameAvailable(JNIEnv* env, | |
| 30 jobject obj, | |
| 31 jbyteArray data, | |
| 32 jint cropWidth, | |
| 33 jint cropHeight, | |
| 34 jlong timestamp) { | |
| 35 const media::VideoCaptureOracle::Event event = | |
| 36 media::VideoCaptureOracle::kCompositorUpdate; | |
| 37 uint64_t absolute_micro = | |
| 38 static_cast<int64>(timestamp / base::Time::kNanosecondsPerMicrosecond); | |
| 39 const base::TimeTicks start_time = | |
| 40 base::TimeTicks::FromInternalValue(absolute_micro); | |
| 41 scoped_refptr<media::VideoFrame> frame; | |
| 42 media::ThreadSafeCaptureOracle::CaptureFrameCallback capture_frame_cb; | |
| 43 | |
| 44 bool oracle_decision = oracle_proxy_->ObserveEventAndDecideCapture( | |
|
miu
2016/04/27 05:24:53
I'm not familiar with the API provided by the Andr
braveyao
2016/05/04 18:49:41
Android will send a copy of the whole display when
miu
2016/05/10 00:29:00
Acknowledged. Seems the API designer assumed the p
| |
| 45 event, gfx::Rect(), start_time, &frame, &capture_frame_cb); | |
| 46 | |
| 47 if (!oracle_decision) | |
| 48 return; | |
| 49 | |
| 50 jbyte* buffer = env->GetByteArrayElements(data, NULL); | |
| 51 if (!buffer) | |
| 52 return; | |
| 53 | |
| 54 uint8* source_argb = reinterpret_cast<uint8_t*>(buffer); | |
| 55 int source_stride = cropWidth * 4; | |
| 56 // If needed, scale the captured frame to the required size. | |
| 57 if (gfx::Size(cropWidth, cropHeight) != frame->visible_rect().size()) { | |
| 58 std::unique_ptr<uint8[]> scaled_frame_data; | |
| 59 scaled_frame_data.reset(new uint8[frame->visible_rect().width() * | |
| 60 frame->visible_rect().height() * 4]); | |
| 61 libyuv::ARGBScale(source_argb, source_stride, cropWidth, cropHeight, | |
|
miu
2016/04/27 05:24:53
Can the platform do this? Usually scaling is much
braveyao
2016/05/04 18:49:41
Exactly. You can do the setting to VirtualDisplay
miu
2016/05/10 00:29:00
On second thought, if the frame->visible_rect() is
braveyao
2016/05/18 00:41:46
Done.
| |
| 62 scaled_frame_data.get(), | |
| 63 frame->visible_rect().width() * 4, | |
| 64 frame->visible_rect().width(), | |
| 65 frame->visible_rect().height(), libyuv::kFilterBilinear); | |
| 66 source_argb = scaled_frame_data.get(); | |
| 67 source_stride = frame->visible_rect().width() * 4; | |
| 68 } | |
| 69 | |
| 70 // Populate the VideoFrame. | |
| 71 DCHECK(frame->format() == PIXEL_FORMAT_I420 || | |
| 72 frame->format() == PIXEL_FORMAT_YV12); | |
| 73 | |
| 74 media::ConvertRGB32ToYUV( | |
|
miu
2016/04/27 05:24:53
Is it possible to push colorspace conversion upstr
braveyao
2016/05/04 18:49:41
Thanks for the advice. I didn't consider this befo
miu
2016/05/10 00:29:00
In Chromium code, we have to work on many differen
braveyao
2016/05/18 00:41:46
Done.
BTW: YUV420 has better performance, but wor
| |
| 75 source_argb, frame->visible_data(media::VideoFrame::kYPlane), | |
| 76 frame->visible_data(media::VideoFrame::kUPlane), | |
| 77 frame->visible_data(media::VideoFrame::kVPlane), | |
| 78 frame->visible_rect().width(), frame->visible_rect().height(), | |
| 79 source_stride, frame->stride(media::VideoFrame::kYPlane), | |
| 80 frame->stride(media::VideoFrame::kUPlane)); | |
| 81 | |
| 82 // Deliver the populated VideoFrame. | |
| 83 capture_frame_cb.Run(frame, start_time, true); | |
| 84 | |
| 85 env->ReleaseByteArrayElements(data, buffer, JNI_ABORT); | |
| 86 } | |
| 87 | |
| 88 void ScreenCaptureMachineAndroid::OnActivityResult(JNIEnv* env, | |
| 89 jobject obj, | |
| 90 jint result) { | |
| 91 if (result == 0) { | |
| 92 oracle_proxy_->ReportError(FROM_HERE, "The user denied screen capture"); | |
| 93 return; | |
| 94 } | |
| 95 | |
| 96 media::Java_ScreenCapture_startCapture(env, obj); | |
| 97 } | |
| 98 | |
| 99 void ScreenCaptureMachineAndroid::Start( | |
| 100 const scoped_refptr<media::ThreadSafeCaptureOracle>& oracle_proxy, | |
| 101 const media::VideoCaptureParams& params, | |
| 102 const base::Callback<void(bool)> callback) { | |
| 103 DCHECK(oracle_proxy.get()); | |
| 104 oracle_proxy_ = oracle_proxy; | |
| 105 | |
| 106 j_capture_.Reset( | |
| 107 media::ScreenCaptureFactoryAndroid::createScreenCaptureMachineAndroid( | |
| 108 reinterpret_cast<intptr_t>(this))); | |
| 109 | |
| 110 if (j_capture_.obj() == NULL) { | |
| 111 LOG(ERROR) << "Failed to createScreenCaptureAndroid," | |
| 112 " Maybe android version is too low"; | |
| 113 callback.Run(false); | |
| 114 return; | |
| 115 } | |
| 116 | |
| 117 capture_format_ = params.requested_format; | |
| 118 // This capturer always outputs ARGB, non-interlaced. | |
| 119 capture_format_.pixel_format = media::PIXEL_FORMAT_ARGB; | |
|
miu
2016/04/27 05:24:53
I believe the |params| would specify I420 or YV12,
braveyao
2016/05/04 18:49:41
Yes, |params| asks for I420. Same as above comment
miu
2016/05/10 00:29:00
This is orthogonal to the other comment. The |capt
| |
| 120 | |
| 121 CHECK(capture_format_.frame_size.GetArea() > 0); | |
| 122 CHECK(!(capture_format_.frame_size.width() % 2)); | |
| 123 CHECK(!(capture_format_.frame_size.height() % 2)); | |
| 124 | |
| 125 JNIEnv* env = AttachCurrentThread(); | |
| 126 jboolean ret = media::Java_ScreenCapture_startPrompt( | |
| 127 env, j_capture_.obj(), capture_format_.frame_size.width(), | |
| 128 capture_format_.frame_size.height()); | |
| 129 if (!ret) { | |
| 130 callback.Run(false); | |
| 131 return; | |
| 132 } | |
| 133 | |
| 134 callback.Run(true); | |
| 135 } | |
| 136 | |
| 137 void ScreenCaptureMachineAndroid::Stop(const base::Closure& callback) { | |
| 138 JNIEnv* env = AttachCurrentThread(); | |
| 139 Java_ScreenCapture_stopCapture(env, j_capture_.obj()); | |
| 140 | |
| 141 callback.Run(); | |
| 142 } | |
| 143 | |
| 144 void ScreenCaptureMachineAndroid::MaybeCaptureForRefresh() {} | |
|
miu
2016/04/27 05:24:53
Depending on the behavior of the platform, you may
braveyao
2016/05/04 18:49:41
Thanks for explaining this new method.
Android als
miu
2016/05/10 00:29:00
There are concerns on the consuming-end of these v
braveyao
2016/05/18 00:41:46
Done.
| |
| 145 | |
| 146 } // namespace media | |
| OLD | NEW |