OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013 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/video/capture/android/video_capture_device_android.h" | |
6 | |
7 #include <string> | |
8 | |
9 #include "base/android/jni_android.h" | |
10 #include "base/android/scoped_java_ref.h" | |
11 #include "base/string_number_conversions.h" | |
12 #include "base/stringprintf.h" | |
13 #include "jni/Camera_jni.h" | |
14 #include "jni/VideoCapture_jni.h" | |
15 | |
16 using base::android::AttachCurrentThread; | |
17 using base::android::CheckException; | |
18 using base::android::GetClass; | |
19 using base::android::MethodID; | |
20 using base::android::JavaRef; | |
21 using base::android::ScopedJavaLocalRef; | |
22 | |
23 namespace { | |
24 | |
25 // TODO(wjia): add stride as part of buffer parameter. | |
26 void ResetBufferI420(uint8* buffer, int width, int height) { | |
27 int y_size = width * height; | |
28 memset(buffer, 0, y_size); | |
29 buffer += y_size; | |
30 memset(buffer, 128, y_size / 2); | |
31 } | |
32 | |
33 void RotatePlaneByPixels( | |
34 uint8* src, | |
35 uint8* dest, | |
36 int width, | |
37 int height, | |
38 int rotation, | |
39 bool flip_vert, | |
40 bool flip_horiz) { | |
41 // Consolidate cases. Only 0 and 90 are left. | |
42 if (rotation == 180 || rotation == 270) { | |
43 rotation -= 180; | |
44 flip_vert = !flip_vert; | |
45 flip_horiz = !flip_horiz; | |
46 } | |
47 | |
48 int num_rows = height; | |
49 int num_cols = width; | |
50 int src_stride = width; | |
51 int dest_row_step = width; | |
52 int dest_col_step = 1; | |
53 | |
54 if (rotation == 0) { | |
55 if (flip_horiz) { | |
56 // Use pixel copying. | |
57 dest_col_step = -1; | |
58 if (flip_vert) { | |
59 // Rotation 180. | |
60 dest_row_step = -width; | |
61 dest += height * width - 1; | |
62 } else { | |
63 dest += width - 1; | |
64 } | |
65 } else { | |
66 if (flip_vert) { | |
67 // Fast copy by rows. | |
68 dest += width * (height - 1); | |
69 for (int row = 0; row < height; ++row) { | |
70 memcpy(dest, src, width); | |
71 src += width; | |
72 dest -= width; | |
73 } | |
74 } else { | |
75 memcpy(dest, src, width * height); | |
76 } | |
77 return; | |
78 } | |
79 } else if (rotation == 90) { | |
80 int offset = (width - height) / 2; | |
81 if (width > height) { | |
82 src += offset; | |
83 num_rows = num_cols = height; | |
84 } else { | |
85 src += width * offset; | |
86 num_rows = num_cols = width; | |
87 offset = (height - width) / 2; | |
88 } | |
89 | |
90 dest_col_step = (flip_vert ? -width : width); | |
91 if (flip_horiz) { | |
92 dest_row_step = 1; | |
93 if (flip_vert) { | |
94 dest += (width > height ? width * (height - 1) + offset : | |
95 width * (height - offset - 1)); | |
96 } else { | |
97 dest += (width > height ? offset : width * offset); | |
98 } | |
99 } else { | |
100 dest_row_step = -1; | |
101 if (flip_vert) { | |
102 dest += (width > height ? width * height - offset - 1 : | |
103 width * (height - offset) - 1); | |
104 } else { | |
105 dest += (width > height ? width - offset - 1 : | |
106 width * (offset + 1) - 1); | |
107 } | |
108 } | |
109 } | |
110 | |
111 // Copy pixels. | |
112 for (int row = 0; row < num_rows; ++row) { | |
113 uint8* src_ptr = src; | |
114 uint8* dest_ptr = dest; | |
115 for (int col = 0; col < num_cols; ++col) { | |
116 *dest_ptr = *src_ptr++; | |
117 dest_ptr += dest_col_step; | |
118 } | |
119 src += src_stride; | |
120 dest += dest_row_step; | |
121 } | |
122 } | |
123 | |
124 int GetIntField(JNIEnv* env, | |
125 const JavaRef<jclass>& clazz, | |
126 const JavaRef<jobject>& instance, | |
127 const char* field_name) { | |
128 jfieldID field = GetFieldID(env, clazz, field_name, "I"); | |
129 jint int_value = env->GetIntField(instance.obj(), field); | |
130 return int_value; | |
131 } | |
132 | |
133 } // namespace | |
134 | |
135 namespace media { | |
136 | |
137 // static | |
138 void VideoCaptureDevice::GetDeviceNames(Names* device_names) { | |
139 device_names->clear(); | |
140 | |
141 JNIEnv* env = AttachCurrentThread(); | |
142 | |
143 int num_cameras = JNI_Camera::Java_Camera_getNumberOfCameras(env); | |
144 DVLOG(1) << "VideoCaptureDevice::GetDeviceNames: num_cameras=" << num_cameras; | |
145 if (num_cameras <= 0) | |
146 return; | |
147 | |
148 // TODO(wjia): switch to using same approach as Camera when | |
149 // jar_file_jni_generator.gypi supports system inner classes. | |
150 std::string camera_info_string("android/hardware/Camera$CameraInfo"); | |
151 | |
152 ScopedJavaLocalRef<jclass> camera_info_class( | |
153 GetClass(env, camera_info_string.c_str())); | |
154 jmethodID constructor = MethodID::Get<MethodID::TYPE_INSTANCE>( | |
155 env, camera_info_class.obj(), "<init>", "()V"); | |
156 | |
157 ScopedJavaLocalRef<jobject> object_camera_info( | |
158 env, env->NewObject(camera_info_class.obj(), constructor)); | |
159 | |
160 jfieldID field_facing = GetFieldID(env, camera_info_class, "facing", "I"); | |
161 jfieldID field_facing_front = GetStaticFieldID( | |
162 env, camera_info_class, "CAMERA_FACING_FRONT", "I"); | |
163 | |
164 for (int camera_id = num_cameras - 1; camera_id >= 0; --camera_id) { | |
165 JNI_Camera::Java_Camera_getCameraInfo( | |
166 env, camera_id, object_camera_info.obj()); | |
167 CheckException(env); | |
168 | |
169 Name name; | |
170 name.unique_id = StringPrintf("%d", camera_id); | |
171 std::string facing_string; | |
172 if (env->GetIntField(object_camera_info.obj(), field_facing) == | |
173 env->GetStaticIntField(camera_info_class.obj(), field_facing_front)) { | |
174 facing_string = "front"; | |
175 } else { | |
176 facing_string = "back"; | |
177 } | |
178 name.device_name = StringPrintf( | |
179 "camera %d, facing %s", camera_id, facing_string.c_str()); | |
180 device_names->push_back(name); | |
181 jfieldID field_orientation = GetFieldID( | |
182 env, camera_info_class, "orientation", "I"); | |
183 jint orientation = env->GetIntField(object_camera_info.obj(), | |
184 field_orientation); | |
185 DVLOG(1) << "VideoCaptureDevice::GetDeviceNames: camera device_name=" | |
186 << name.device_name | |
187 << ", unique_id=" | |
188 << name.unique_id | |
189 << ", orientation " | |
190 << orientation; | |
191 } | |
192 } | |
193 | |
194 // static | |
195 VideoCaptureDevice* VideoCaptureDevice::Create(const Name& device_name) { | |
196 return VideoCaptureDeviceAndroid::Create(device_name); | |
197 } | |
198 | |
199 // static | |
200 VideoCaptureDevice* VideoCaptureDeviceAndroid::Create(const Name& device_name) { | |
201 scoped_ptr<VideoCaptureDeviceAndroid> ret( | |
202 new VideoCaptureDeviceAndroid(device_name)); | |
203 if (ret->Init()) | |
204 return ret.release(); | |
205 return NULL; | |
206 } | |
207 | |
208 // static | |
209 bool VideoCaptureDeviceAndroid::RegisterVideoCaptureDevice(JNIEnv* env) { | |
210 return RegisterNativesImpl(env) && JNI_Camera::RegisterNativesImpl(env); | |
211 } | |
212 | |
213 VideoCaptureDeviceAndroid::VideoCaptureDeviceAndroid(const Name& device_name) | |
214 : state_(kIdle), | |
215 observer_(NULL), | |
216 device_name_(device_name), | |
217 current_settings_(), | |
218 rotation_(0) { | |
219 } | |
220 | |
221 VideoCaptureDeviceAndroid::~VideoCaptureDeviceAndroid() { | |
222 DeAllocate(); | |
223 } | |
224 | |
225 bool VideoCaptureDeviceAndroid::Init() { | |
226 int id; | |
227 if (!base::StringToInt(device_name_.unique_id, &id)) | |
228 return false; | |
229 | |
230 JNIEnv* env = AttachCurrentThread(); | |
231 | |
232 j_capture_.Reset(Java_VideoCapture_createVideoCapture( | |
233 env, base::android::GetApplicationContext(), id, | |
234 reinterpret_cast<jlong>(this))); | |
235 | |
236 return true; | |
237 } | |
238 | |
239 const VideoCaptureDevice::Name& VideoCaptureDeviceAndroid::device_name() { | |
240 return device_name_; | |
241 } | |
242 | |
243 void VideoCaptureDeviceAndroid::Allocate( | |
244 int width, | |
245 int height, | |
246 int frame_rate, | |
247 EventHandler* observer) { | |
248 { | |
249 base::AutoLock lock(lock_); | |
250 if (state_ != kIdle) | |
251 return; | |
252 observer_ = observer; | |
253 state_ = kAllocated; | |
254 } | |
255 | |
256 JNIEnv* env = AttachCurrentThread(); | |
257 | |
258 jboolean ret = Java_VideoCapture_allocate(env, | |
259 j_capture_.obj(), | |
260 width, | |
261 height, | |
262 frame_rate); | |
263 if (!ret) { | |
264 SetErrorState("failed to allocate"); | |
265 return; | |
266 } | |
267 | |
268 // Store current width and height. | |
269 current_settings_.width = | |
270 Java_VideoCapture_queryWidth(env, j_capture_.obj()); | |
271 current_settings_.height = | |
272 Java_VideoCapture_queryHeight(env, j_capture_.obj()); | |
273 current_settings_.frame_rate = | |
274 Java_VideoCapture_queryFrameRate(env, j_capture_.obj()); | |
275 current_settings_.color = VideoCaptureCapability::kYV12; | |
276 CHECK(current_settings_.height > 0 && !(current_settings_.height % 2)); | |
Ami GONE FROM CHROMIUM
2013/02/06 17:39:14
note you removed the width > 0 check. Not sure if
wjia(left Chromium)
2013/02/06 17:52:29
Since the width is guaranteed to be multiple of 32
Ami GONE FROM CHROMIUM
2013/02/06 18:46:45
What about width or height of 0?
(why do you che
wjia(left Chromium)
2013/02/06 19:02:28
There should be no problem about that since the qu
| |
277 | |
278 DVLOG(1) << "VideoCaptureDeviceAndroid::Allocate: queried width=" | |
279 << current_settings_.width | |
280 << ", height=" | |
281 << current_settings_.height | |
282 << ", frame_rate=" | |
283 << current_settings_.frame_rate; | |
284 // Report the frame size to the observer. | |
285 observer_->OnFrameInfo(current_settings_); | |
286 | |
287 int y_size = current_settings_.width * current_settings_.height; | |
288 rotation_buffer_.reset(new uint8[y_size * 3 / 2]); | |
289 ResetBufferI420(rotation_buffer_.get(), | |
290 current_settings_.width, | |
291 current_settings_.height); | |
292 } | |
293 | |
294 void VideoCaptureDeviceAndroid::Start() { | |
295 DVLOG(1) << "VideoCaptureDeviceAndroid::Start"; | |
296 { | |
297 base::AutoLock lock(lock_); | |
298 DCHECK_EQ(state_, kAllocated); | |
299 } | |
300 | |
301 JNIEnv* env = AttachCurrentThread(); | |
302 | |
303 jint ret = Java_VideoCapture_startCapture(env, j_capture_.obj()); | |
304 if (ret < 0) { | |
305 SetErrorState("failed to start capture"); | |
306 return; | |
307 } | |
308 | |
309 { | |
310 base::AutoLock lock(lock_); | |
311 state_ = kCapturing; | |
312 } | |
313 } | |
314 | |
315 void VideoCaptureDeviceAndroid::Stop() { | |
316 DVLOG(1) << "VideoCaptureDeviceAndroid::Stop"; | |
317 { | |
318 base::AutoLock lock(lock_); | |
319 if (state_ != kCapturing && state_ != kError) | |
320 return; | |
321 if (state_ == kCapturing) | |
322 state_ = kAllocated; | |
323 } | |
324 | |
325 JNIEnv* env = AttachCurrentThread(); | |
326 | |
327 jint ret = Java_VideoCapture_stopCapture(env, j_capture_.obj()); | |
328 if (ret < 0) { | |
329 SetErrorState("failed to stop capture"); | |
330 return; | |
331 } | |
332 } | |
333 | |
334 void VideoCaptureDeviceAndroid::DeAllocate() { | |
335 DVLOG(1) << "VideoCaptureDeviceAndroid::DeAllocate"; | |
336 { | |
337 base::AutoLock lock(lock_); | |
338 if (state_ == kIdle) | |
339 return; | |
340 | |
341 if (state_ == kCapturing) { | |
342 base::AutoUnlock unlock(lock_); | |
343 Stop(); | |
344 } | |
345 | |
346 if (state_ == kAllocated) | |
347 state_ = kIdle; | |
348 | |
349 observer_ = NULL; | |
350 } | |
351 | |
352 JNIEnv* env = AttachCurrentThread(); | |
353 | |
354 Java_VideoCapture_deallocate(env, j_capture_.obj()); | |
355 } | |
356 | |
357 void VideoCaptureDeviceAndroid::OnFrameAvailable( | |
358 JNIEnv* env, | |
359 jobject obj, | |
360 jbyteArray data, | |
361 jint length, | |
362 jint rotation, | |
363 jboolean flip_vert, | |
364 jboolean flip_horiz) { | |
365 DVLOG(3) << "VideoCaptureDeviceAndroid::OnFrameAvailable: length =" << length; | |
366 | |
367 base::AutoLock lock(lock_); | |
368 if (state_ != kCapturing || !observer_) | |
369 return; | |
370 | |
371 jbyte* buffer = env->GetByteArrayElements(data, NULL); | |
372 if (!buffer) { | |
373 LOG(ERROR) << "VideoCaptureDeviceAndroid::OnFrameAvailable: " | |
374 "failed to GetByteArrayElements"; | |
375 return; | |
376 } | |
377 | |
378 // TODO(wjia): move rotation into VideoCaptureController to remove | |
379 // one buffer copying. | |
380 // Rotate the buffer when needed. | |
381 int width = current_settings_.width; | |
382 int height = current_settings_.height; | |
383 if (rotation_ != rotation) { | |
384 rotation_ = rotation; | |
385 ResetBufferI420(rotation_buffer_.get(), width, height); | |
386 } | |
387 | |
388 uint8* src = reinterpret_cast<uint8*>(buffer); | |
389 uint8* dest = rotation_buffer_.get(); | |
390 | |
391 RotatePlaneByPixels(src, dest, width, height, rotation, flip_vert, | |
392 flip_horiz); | |
393 int y_size = width * height; | |
394 src += y_size; | |
395 dest += y_size; | |
396 RotatePlaneByPixels(src, dest, width/2, height/2, rotation, flip_vert, | |
397 flip_horiz); | |
398 src += y_size/4; | |
399 dest += y_size/4; | |
400 RotatePlaneByPixels(src, dest, width/2, height/2, rotation, flip_vert, | |
401 flip_horiz); | |
402 observer_->OnIncomingCapturedFrame(rotation_buffer_.get(), length, | |
403 base::Time::Now()); | |
404 | |
405 env->ReleaseByteArrayElements(data, buffer, JNI_ABORT); | |
406 } | |
407 | |
408 void VideoCaptureDeviceAndroid::SetErrorState(const std::string& reason) { | |
409 LOG(ERROR) << "VideoCaptureDeviceAndroid::SetErrorState: " << reason; | |
410 state_ = kError; | |
411 observer_->OnError(); | |
412 } | |
413 | |
414 } // namespace media | |
OLD | NEW |