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, | |
Ami GONE FROM CHROMIUM
2013/02/06 20:13:42
clockwise? Counter? Should have a comment.
wjia(left Chromium)
2013/02/06 23:20:39
Done.
| |
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; | |
Ami GONE FROM CHROMIUM
2013/02/06 20:13:42
So in this case width <= height, so offset is <= 0
wjia(left Chromium)
2013/02/06 23:20:39
hrmm, the offset is calculated 2 lines behind. Tha
| |
86 num_rows = num_cols = width; | |
Ami GONE FROM CHROMIUM
2013/02/06 20:13:42
I don't get this; doesn't this result in only rota
wjia(left Chromium)
2013/02/06 23:20:39
Yes, a square is the overlapped area of src and de
| |
87 offset = (height - width) / 2; | |
88 } | |
89 | |
90 dest_col_step = (flip_vert ? -width : width); | |
91 if (flip_horiz) { | |
92 dest_row_step = 1; | |
Ami GONE FROM CHROMIUM
2013/02/06 20:13:42
Set this like dest_col_step at l.90?
wjia(left Chromium)
2013/02/06 23:20:39
This one is correct. I have added some comments ab
| |
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); | |
Ami GONE FROM CHROMIUM
2013/02/06 20:13:42
I'm not 100% sure I understand how offset is being
wjia(left Chromium)
2013/02/06 23:20:39
"offset" is the cropped/padded pixels on one line
| |
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 } | |
Ami GONE FROM CHROMIUM
2013/02/06 20:13:42
else NOTREACHED?
wjia(left Chromium)
2013/02/06 23:20:39
l99 is the "else".
| |
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 } | |
Ami GONE FROM CHROMIUM
2013/02/06 20:13:42
This is a crazy function to have no test for.
Espe
wjia(left Chromium)
2013/02/06 23:20:39
Agree that we need a test for this function. I hav
| |
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); | |
Yaron
2013/02/06 19:25:51
This is unnecessary (handled by the binding)
wjia(left Chromium)
2013/02/06 23:20:39
Done.
| |
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_.width > 0 && !(current_settings_.width % 2)); | |
277 CHECK(current_settings_.height > 0 && !(current_settings_.height % 2)); | |
278 | |
279 DVLOG(1) << "VideoCaptureDeviceAndroid::Allocate: queried width=" | |
280 << current_settings_.width | |
281 << ", height=" | |
282 << current_settings_.height | |
283 << ", frame_rate=" | |
284 << current_settings_.frame_rate; | |
285 // Report the frame size to the observer. | |
286 observer_->OnFrameInfo(current_settings_); | |
287 | |
288 int y_size = current_settings_.width * current_settings_.height; | |
289 rotation_buffer_.reset(new uint8[y_size * 3 / 2]); | |
290 ResetBufferI420(rotation_buffer_.get(), | |
291 current_settings_.width, | |
292 current_settings_.height); | |
293 } | |
294 | |
295 void VideoCaptureDeviceAndroid::Start() { | |
296 DVLOG(1) << "VideoCaptureDeviceAndroid::Start"; | |
297 { | |
298 base::AutoLock lock(lock_); | |
299 DCHECK_EQ(state_, kAllocated); | |
300 } | |
301 | |
302 JNIEnv* env = AttachCurrentThread(); | |
303 | |
304 jint ret = Java_VideoCapture_startCapture(env, j_capture_.obj()); | |
305 if (ret < 0) { | |
306 SetErrorState("failed to start capture"); | |
307 return; | |
308 } | |
309 | |
310 { | |
311 base::AutoLock lock(lock_); | |
312 state_ = kCapturing; | |
313 } | |
314 } | |
315 | |
316 void VideoCaptureDeviceAndroid::Stop() { | |
317 DVLOG(1) << "VideoCaptureDeviceAndroid::Stop"; | |
318 { | |
319 base::AutoLock lock(lock_); | |
320 if (state_ != kCapturing && state_ != kError) | |
321 return; | |
322 if (state_ == kCapturing) | |
323 state_ = kAllocated; | |
324 } | |
325 | |
326 JNIEnv* env = AttachCurrentThread(); | |
327 | |
328 jint ret = Java_VideoCapture_stopCapture(env, j_capture_.obj()); | |
329 if (ret < 0) { | |
330 SetErrorState("failed to stop capture"); | |
331 return; | |
332 } | |
333 } | |
334 | |
335 void VideoCaptureDeviceAndroid::DeAllocate() { | |
336 DVLOG(1) << "VideoCaptureDeviceAndroid::DeAllocate"; | |
337 { | |
338 base::AutoLock lock(lock_); | |
339 if (state_ == kIdle) | |
340 return; | |
341 | |
342 if (state_ == kCapturing) { | |
343 base::AutoUnlock unlock(lock_); | |
344 Stop(); | |
345 } | |
346 | |
347 if (state_ == kAllocated) | |
348 state_ = kIdle; | |
349 | |
350 observer_ = NULL; | |
351 } | |
352 | |
353 JNIEnv* env = AttachCurrentThread(); | |
354 | |
355 Java_VideoCapture_deallocate(env, j_capture_.obj()); | |
356 } | |
357 | |
358 void VideoCaptureDeviceAndroid::OnFrameAvailable( | |
359 JNIEnv* env, | |
360 jobject obj, | |
361 jbyteArray data, | |
362 jint length, | |
363 jint rotation, | |
364 jboolean flip_vert, | |
365 jboolean flip_horiz) { | |
366 DVLOG(3) << "VideoCaptureDeviceAndroid::OnFrameAvailable: length =" << length; | |
367 | |
368 base::AutoLock lock(lock_); | |
369 if (state_ != kCapturing || !observer_) | |
370 return; | |
371 | |
372 jbyte* buffer = env->GetByteArrayElements(data, NULL); | |
373 if (!buffer) { | |
374 LOG(ERROR) << "VideoCaptureDeviceAndroid::OnFrameAvailable: " | |
375 "failed to GetByteArrayElements"; | |
376 return; | |
377 } | |
378 | |
379 // TODO(wjia): move rotation into VideoCaptureController to remove | |
380 // one buffer copying. | |
381 // Rotate the buffer when needed. | |
382 int width = current_settings_.width; | |
383 int height = current_settings_.height; | |
384 if (rotation_ != rotation) { | |
385 rotation_ = rotation; | |
386 ResetBufferI420(rotation_buffer_.get(), width, height); | |
Ami GONE FROM CHROMIUM
2013/02/06 20:13:42
Is there no need to call observer_->OnFrameInfo()
wjia(left Chromium)
2013/02/06 23:20:39
No, we don't support dimension change in one captu
Ami GONE FROM CHROMIUM
2013/02/07 05:37:34
See above; I think we need to honor rotation from
wjia(left Chromium)
2013/02/12 00:23:45
To support resolution change on the fly, we need a
| |
387 } | |
388 | |
389 uint8* src = reinterpret_cast<uint8*>(buffer); | |
390 uint8* dest = rotation_buffer_.get(); | |
391 | |
392 RotatePlaneByPixels(src, dest, width, height, rotation, flip_vert, | |
393 flip_horiz); | |
394 int y_size = width * height; | |
395 src += y_size; | |
396 dest += y_size; | |
397 RotatePlaneByPixels(src, dest, width/2, height/2, rotation, flip_vert, | |
398 flip_horiz); | |
399 src += y_size/4; | |
400 dest += y_size/4; | |
401 RotatePlaneByPixels(src, dest, width/2, height/2, rotation, flip_vert, | |
402 flip_horiz); | |
403 observer_->OnIncomingCapturedFrame(rotation_buffer_.get(), length, | |
404 base::Time::Now()); | |
405 | |
406 env->ReleaseByteArrayElements(data, buffer, JNI_ABORT); | |
407 } | |
408 | |
409 void VideoCaptureDeviceAndroid::SetErrorState(const std::string& reason) { | |
410 LOG(ERROR) << "VideoCaptureDeviceAndroid::SetErrorState: " << reason; | |
411 { | |
412 base::AutoLock lock(lock_); | |
413 state_ = kError; | |
414 } | |
415 observer_->OnError(); | |
416 } | |
417 | |
418 } // namespace media | |
OLD | NEW |