Chromium Code Reviews| Index: media/screen_capture/android/screen_capturer_android.cc |
| diff --git a/media/screen_capture/android/screen_capturer_android.cc b/media/screen_capture/android/screen_capturer_android.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..678ee0d978e53f3f0967d83cf52e7be8cee17372 |
| --- /dev/null |
| +++ b/media/screen_capture/android/screen_capturer_android.cc |
| @@ -0,0 +1,115 @@ |
| +// Copyright (c) 2015 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/screen_capture/android/screen_capturer_android.h" |
| + |
| +#include "base/android/jni_android.h" |
| +#include "base/android/scoped_java_ref.h" |
| +#include "base/strings/string_number_conversions.h" |
| +#include "jni/ScreenCapture_jni.h" |
|
renyuzhuo
2016/08/10 10:02:24
where is the jni/ScreenCapture_jni.h,thank you!
|
| +#include "media/screen_capture/android/screen_capture_factory_android.h" |
| +#include "third_party/webrtc/modules/desktop_capture/desktop_frame.h" |
| +#include "third_party/webrtc/system_wrappers/interface/tick_util.h" |
| + |
| +using base::android::AttachCurrentThread; |
| + |
| +namespace media { |
| + |
| +// static |
| +bool ScreenCapturerAndroid::RegisterScreenCapturer(JNIEnv* env) { |
| + return media::RegisterNativesImpl(env); |
| +} |
| + |
| +ScreenCapturerAndroid::ScreenCapturerAndroid(): callback_(NULL) {} |
| + |
| +ScreenCapturerAndroid::~ScreenCapturerAndroid() { |
| + JNIEnv* env = AttachCurrentThread(); |
| + media::Java_ScreenCapture_stopCapture(env, j_capture_.obj()); |
| +} |
| + |
| +void ScreenCapturerAndroid::OnFrameAvailable(JNIEnv* env, |
| + jobject obj, |
| + jbyteArray data, |
| + jint width, |
| + jint height) { |
| + jbyte* buffer = env->GetByteArrayElements(data, NULL); |
| + if (!buffer) { |
| + LOG(ERROR) << "ScreenCapturerAndroid::OnFrameAvailable: " |
| + "failed to GetByteArrayElements"; |
| + return; |
| + } |
| + |
| + webrtc::TickTime capture_start_time = webrtc::TickTime::Now(); |
| + webrtc::DesktopSize size(width, height); |
| + int stride = webrtc::DesktopFrame::kBytesPerPixel * size.width(); |
| + |
| + queue_.MoveToNextFrame(); |
| + |
| + if (!queue_.current_frame()) { |
| + rtc::scoped_ptr<webrtc::DesktopFrame> frame( |
| + new webrtc::BasicDesktopFrame(size)); |
| + queue_.ReplaceCurrentFrame(frame.release()); |
| + } |
| + |
| + webrtc::DesktopFrame* fframe = queue_.current_frame()->Share(); |
| + webrtc::DesktopRect screen_rect = |
| + webrtc::DesktopRect::MakeSize(fframe->size()); |
| + |
| + fframe->CopyPixelsFrom( |
| + reinterpret_cast<uint8_t*>(buffer), stride, screen_rect); |
| + |
| + fframe->set_capture_time_ms( |
| + (webrtc::TickTime::Now() - capture_start_time).Milliseconds()); |
| + callback_->OnCaptureCompleted(fframe); |
| + |
| + env->ReleaseByteArrayElements(data, buffer, JNI_ABORT); |
| +} |
| + |
| +void ScreenCapturerAndroid::OnActivityResult(JNIEnv* env, |
| + jobject obj, |
| + jint result) { |
| + if (result == 0) { |
| + DVLOG(1) << "User canceled screen capture"; |
| + return; |
| + } |
| + |
| + media::Java_ScreenCapture_startCapture(env, obj); |
| +} |
| + |
| +void ScreenCapturerAndroid::Capture(const webrtc::DesktopRegion& region) {} |
| + |
| +bool ScreenCapturerAndroid::GetScreenList(ScreenList* screens) { |
| +// return webrtc::GetScreenList(screens); |
| + // TODO(jiayl): implement screen enumeration. |
| + Screen default_screen; |
| + default_screen.id = 0; |
| + screens->push_back(default_screen); |
| + return true; |
| +} |
| + |
| +bool ScreenCapturerAndroid::SelectScreen(webrtc::ScreenId id) { |
| + return true; |
| +} |
| + |
| +void ScreenCapturerAndroid::Start(webrtc::DesktopCapturer::Callback* callback) { |
| + assert(!callback_); |
| + assert(callback); |
| + |
| + callback_ = callback; |
| + |
| + j_capture_.Reset(media::ScreenCaptureFactoryAndroid |
| + ::createScreenCaptureAndroid(reinterpret_cast<intptr_t>(this))); |
| + |
| + if (j_capture_.obj() == NULL) { |
| + LOG(ERROR) << "ScreenCapturerAndroid::Start" |
| + " failed to createScreenCaptureAndroid," |
| + " Maybe android version is too low"; |
| + return; |
| + } |
| + |
| + JNIEnv* env = AttachCurrentThread(); |
| + media::Java_ScreenCapture_start(env, j_capture_.obj()); |
| +} |
| + |
| +} // namespace media |