1 // Copyright 2014 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.
5 #include "media/video/capture/android/video_capture_device_factory_android.h"
7 #include "base/android/jni_string.h"
8 #include "base/android/scoped_java_ref.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/stringprintf.h"
11 #include "jni/VideoCaptureFactory_jni.h"
13 using base::android::AttachCurrentThread;
14 using base::android::ScopedJavaLocalRef;
19 void VideoCaptureDeviceFactoryAndroid::GetDeviceNames(
20 VideoCaptureDevice::Names* device_names) {
21 device_names->clear();
23 JNIEnv* env = AttachCurrentThread();
25 int num_cameras = Java_ChromiumCameraInfo_getNumberOfCameras(
26 env, base::android::GetApplicationContext());
27 DVLOG(1) << "VideoCaptureDevice::GetDeviceNames: num_cameras=" << num_cameras;
31 for (int camera_id = num_cameras - 1; camera_id >= 0; --camera_id) {
32 ScopedJavaLocalRef<jobject> ci =
33 Java_ChromiumCameraInfo_getAt(env, camera_id);
35 VideoCaptureDevice::Name name(
36 base::android::ConvertJavaStringToUTF8(
37 Java_ChromiumCameraInfo_getDeviceName(env, ci.obj())),
38 base::StringPrintf("%d", Java_ChromiumCameraInfo_getId(env, ci.obj())));
39 device_names->push_back(name);
41 DVLOG(1) << "VideoCaptureDeviceFactoryAndroid::GetDeviceNames: camera"
42 << "device_name=" << name.name() << ", unique_id=" << name.id()
44 << Java_ChromiumCameraInfo_getOrientation(env, ci.obj());
49 void VideoCaptureDeviceFactoryAndroid::GetDeviceSupportedFormats(
50 const VideoCaptureDevice::Name& device,
51 VideoCaptureFormats* capture_formats) {
53 if (!base::StringToInt(device.id(), &id))
55 JNIEnv* env = AttachCurrentThread();
56 base::android::ScopedJavaLocalRef<jobjectArray> collected_formats =
57 Java_VideoCaptureFactory_getDeviceSupportedFormats(env, id);
58 if (collected_formats.is_null())
61 jsize num_formats = env->GetArrayLength(collected_formats.obj());
62 for (int i = 0; i < num_formats; ++i) {
63 base::android::ScopedJavaLocalRef<jobject> format(
64 env, env->GetObjectArrayElement(collected_formats.obj(), i));
66 VideoPixelFormat pixel_format = media::PIXEL_FORMAT_UNKNOWN;
67 switch (media::Java_VideoCaptureFactory_getCaptureFormatPixelFormat(
69 case ANDROID_IMAGEFORMAT_YV12:
70 pixel_format = media::PIXEL_FORMAT_YV12;
72 case ANDROID_IMAGEFORMAT_NV21:
73 pixel_format = media::PIXEL_FORMAT_NV21;
78 VideoCaptureFormat capture_format(
79 gfx::Size(media::Java_VideoCaptureFactory_getCaptureFormatWidth(env,
81 media::Java_VideoCaptureFactory_getCaptureFormatHeight(env,
83 media::Java_VideoCaptureFactory_getCaptureFormatFramerate(env,
86 capture_formats->push_back(capture_format);
87 DVLOG(1) << device.name() << " resolution: "
88 << capture_format.frame_size.ToString() << ", fps: "
89 << capture_format.frame_rate << ", pixel format: "
90 << capture_format.pixel_format;
95 ScopedJavaLocalRef<jobject>
96 VideoCaptureDeviceFactoryAndroid::createVideoCaptureAndroid(
98 jlong nativeVideoCaptureDeviceAndroid) {
99 return (Java_VideoCaptureFactory_createVideoCapture(
100 AttachCurrentThread(),
101 base::android::GetApplicationContext(),
103 nativeVideoCaptureDeviceAndroid));
107 bool VideoCaptureDeviceFactoryAndroid::RegisterVideoCaptureDeviceFactory(
109 return RegisterNativesImpl(env);