Updating XTBs based on .GRDs from branch master
[chromium-blink-merge.git] / media / capture / video / android / video_capture_device_factory_android.cc
blobfca882e0f2188ece1ad23aabfc0d3495e8cd9974
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/capture/video/android/video_capture_device_factory_android.h"
7 #include "base/android/jni_string.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "base/strings/stringprintf.h"
10 #include "jni/VideoCaptureFactory_jni.h"
11 #include "media/capture/video/android/video_capture_device_android.h"
13 using base::android::AttachCurrentThread;
14 using base::android::ScopedJavaLocalRef;
16 namespace media {
18 // static
19 bool VideoCaptureDeviceFactoryAndroid::RegisterVideoCaptureDeviceFactory(
20 JNIEnv* env) {
21 return RegisterNativesImpl(env);
24 // static
25 ScopedJavaLocalRef<jobject>
26 VideoCaptureDeviceFactoryAndroid::createVideoCaptureAndroid(
27 int id,
28 jlong nativeVideoCaptureDeviceAndroid) {
29 return (Java_VideoCaptureFactory_createVideoCapture(
30 AttachCurrentThread(), base::android::GetApplicationContext(), id,
31 nativeVideoCaptureDeviceAndroid));
34 scoped_ptr<VideoCaptureDevice> VideoCaptureDeviceFactoryAndroid::Create(
35 const VideoCaptureDevice::Name& device_name) {
36 DCHECK(thread_checker_.CalledOnValidThread());
37 int id;
38 if (!base::StringToInt(device_name.id(), &id))
39 return scoped_ptr<VideoCaptureDevice>();
41 scoped_ptr<VideoCaptureDeviceAndroid> video_capture_device(
42 new VideoCaptureDeviceAndroid(device_name));
44 if (video_capture_device->Init())
45 return video_capture_device.Pass();
47 DLOG(ERROR) << "Error creating Video Capture Device.";
48 return scoped_ptr<VideoCaptureDevice>();
51 void VideoCaptureDeviceFactoryAndroid::GetDeviceNames(
52 VideoCaptureDevice::Names* device_names) {
53 DCHECK(thread_checker_.CalledOnValidThread());
54 device_names->clear();
56 JNIEnv* env = AttachCurrentThread();
58 const jobject context = base::android::GetApplicationContext();
59 const int num_cameras =
60 Java_VideoCaptureFactory_getNumberOfCameras(env, context);
61 DVLOG(1) << "VideoCaptureDevice::GetDeviceNames: num_cameras=" << num_cameras;
62 if (num_cameras <= 0)
63 return;
65 for (int camera_id = num_cameras - 1; camera_id >= 0; --camera_id) {
66 base::android::ScopedJavaLocalRef<jstring> device_name =
67 Java_VideoCaptureFactory_getDeviceName(env, camera_id, context);
68 if (device_name.obj() == NULL)
69 continue;
71 const int capture_api_type =
72 Java_VideoCaptureFactory_getCaptureApiType(env, camera_id, context);
74 VideoCaptureDevice::Name name(
75 base::android::ConvertJavaStringToUTF8(device_name),
76 base::IntToString(camera_id),
77 static_cast<VideoCaptureDevice::Name::CaptureApiType>(
78 capture_api_type));
79 device_names->push_back(name);
81 DVLOG(1) << "VideoCaptureDeviceFactoryAndroid::GetDeviceNames: camera "
82 << "device_name=" << name.name() << ", unique_id=" << name.id();
86 void VideoCaptureDeviceFactoryAndroid::GetDeviceSupportedFormats(
87 const VideoCaptureDevice::Name& device,
88 VideoCaptureFormats* capture_formats) {
89 DCHECK(thread_checker_.CalledOnValidThread());
90 int id;
91 if (!base::StringToInt(device.id(), &id))
92 return;
93 JNIEnv* env = AttachCurrentThread();
94 base::android::ScopedJavaLocalRef<jobjectArray> collected_formats =
95 Java_VideoCaptureFactory_getDeviceSupportedFormats(
96 env, base::android::GetApplicationContext(), id);
97 if (collected_formats.is_null())
98 return;
100 jsize num_formats = env->GetArrayLength(collected_formats.obj());
101 for (int i = 0; i < num_formats; ++i) {
102 base::android::ScopedJavaLocalRef<jobject> format(
103 env, env->GetObjectArrayElement(collected_formats.obj(), i));
105 VideoCapturePixelFormat pixel_format =
106 media::VIDEO_CAPTURE_PIXEL_FORMAT_UNKNOWN;
107 switch (media::Java_VideoCaptureFactory_getCaptureFormatPixelFormat(
108 env, format.obj())) {
109 case VideoCaptureDeviceAndroid::ANDROID_IMAGE_FORMAT_YV12:
110 pixel_format = media::VIDEO_CAPTURE_PIXEL_FORMAT_YV12;
111 break;
112 case VideoCaptureDeviceAndroid::ANDROID_IMAGE_FORMAT_NV21:
113 pixel_format = media::VIDEO_CAPTURE_PIXEL_FORMAT_NV21;
114 break;
115 default:
116 continue;
118 VideoCaptureFormat capture_format(
119 gfx::Size(media::Java_VideoCaptureFactory_getCaptureFormatWidth(
120 env, format.obj()),
121 media::Java_VideoCaptureFactory_getCaptureFormatHeight(
122 env, format.obj())),
123 media::Java_VideoCaptureFactory_getCaptureFormatFramerate(env,
124 format.obj()),
125 pixel_format);
126 capture_formats->push_back(capture_format);
127 DVLOG(1) << device.name() << " "
128 << VideoCaptureFormat::ToString(capture_format);
132 // static
133 VideoCaptureDeviceFactory*
134 VideoCaptureDeviceFactory::CreateVideoCaptureDeviceFactory(
135 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner) {
136 return new VideoCaptureDeviceFactoryAndroid();
139 } // namespace media