OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 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/screen_capture/android/screen_capturer_android.h" | |
6 | |
7 #include "base/android/jni_android.h" | |
8 #include "base/android/scoped_java_ref.h" | |
9 #include "base/strings/string_number_conversions.h" | |
10 #include "jni/ScreenCapture_jni.h" | |
renyuzhuo
2016/08/10 10:02:24
where is the jni/ScreenCapture_jni.h,thank you!
| |
11 #include "media/screen_capture/android/screen_capture_factory_android.h" | |
12 #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h" | |
13 #include "third_party/webrtc/system_wrappers/interface/tick_util.h" | |
14 | |
15 using base::android::AttachCurrentThread; | |
16 | |
17 namespace media { | |
18 | |
19 // static | |
20 bool ScreenCapturerAndroid::RegisterScreenCapturer(JNIEnv* env) { | |
21 return media::RegisterNativesImpl(env); | |
22 } | |
23 | |
24 ScreenCapturerAndroid::ScreenCapturerAndroid(): callback_(NULL) {} | |
25 | |
26 ScreenCapturerAndroid::~ScreenCapturerAndroid() { | |
27 JNIEnv* env = AttachCurrentThread(); | |
28 media::Java_ScreenCapture_stopCapture(env, j_capture_.obj()); | |
29 } | |
30 | |
31 void ScreenCapturerAndroid::OnFrameAvailable(JNIEnv* env, | |
32 jobject obj, | |
33 jbyteArray data, | |
34 jint width, | |
35 jint height) { | |
36 jbyte* buffer = env->GetByteArrayElements(data, NULL); | |
37 if (!buffer) { | |
38 LOG(ERROR) << "ScreenCapturerAndroid::OnFrameAvailable: " | |
39 "failed to GetByteArrayElements"; | |
40 return; | |
41 } | |
42 | |
43 webrtc::TickTime capture_start_time = webrtc::TickTime::Now(); | |
44 webrtc::DesktopSize size(width, height); | |
45 int stride = webrtc::DesktopFrame::kBytesPerPixel * size.width(); | |
46 | |
47 queue_.MoveToNextFrame(); | |
48 | |
49 if (!queue_.current_frame()) { | |
50 rtc::scoped_ptr<webrtc::DesktopFrame> frame( | |
51 new webrtc::BasicDesktopFrame(size)); | |
52 queue_.ReplaceCurrentFrame(frame.release()); | |
53 } | |
54 | |
55 webrtc::DesktopFrame* fframe = queue_.current_frame()->Share(); | |
56 webrtc::DesktopRect screen_rect = | |
57 webrtc::DesktopRect::MakeSize(fframe->size()); | |
58 | |
59 fframe->CopyPixelsFrom( | |
60 reinterpret_cast<uint8_t*>(buffer), stride, screen_rect); | |
61 | |
62 fframe->set_capture_time_ms( | |
63 (webrtc::TickTime::Now() - capture_start_time).Milliseconds()); | |
64 callback_->OnCaptureCompleted(fframe); | |
65 | |
66 env->ReleaseByteArrayElements(data, buffer, JNI_ABORT); | |
67 } | |
68 | |
69 void ScreenCapturerAndroid::OnActivityResult(JNIEnv* env, | |
70 jobject obj, | |
71 jint result) { | |
72 if (result == 0) { | |
73 DVLOG(1) << "User canceled screen capture"; | |
74 return; | |
75 } | |
76 | |
77 media::Java_ScreenCapture_startCapture(env, obj); | |
78 } | |
79 | |
80 void ScreenCapturerAndroid::Capture(const webrtc::DesktopRegion& region) {} | |
81 | |
82 bool ScreenCapturerAndroid::GetScreenList(ScreenList* screens) { | |
83 // return webrtc::GetScreenList(screens); | |
84 // TODO(jiayl): implement screen enumeration. | |
85 Screen default_screen; | |
86 default_screen.id = 0; | |
87 screens->push_back(default_screen); | |
88 return true; | |
89 } | |
90 | |
91 bool ScreenCapturerAndroid::SelectScreen(webrtc::ScreenId id) { | |
92 return true; | |
93 } | |
94 | |
95 void ScreenCapturerAndroid::Start(webrtc::DesktopCapturer::Callback* callback) { | |
96 assert(!callback_); | |
97 assert(callback); | |
98 | |
99 callback_ = callback; | |
100 | |
101 j_capture_.Reset(media::ScreenCaptureFactoryAndroid | |
102 ::createScreenCaptureAndroid(reinterpret_cast<intptr_t>(this))); | |
103 | |
104 if (j_capture_.obj() == NULL) { | |
105 LOG(ERROR) << "ScreenCapturerAndroid::Start" | |
106 " failed to createScreenCaptureAndroid," | |
107 " Maybe android version is too low"; | |
108 return; | |
109 } | |
110 | |
111 JNIEnv* env = AttachCurrentThread(); | |
112 media::Java_ScreenCapture_start(env, j_capture_.obj()); | |
113 } | |
114 | |
115 } // namespace media | |
OLD | NEW |