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