Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / media / video / capture / android / video_capture_device_factory_android.cc
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.
4
5 #include "media/video/capture/android/video_capture_device_factory_android.h"
6
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"
12
13 using base::android::AttachCurrentThread;
14 using base::android::ScopedJavaLocalRef;
15
16 namespace media {
17
18 // static
19 void VideoCaptureDeviceFactoryAndroid::GetDeviceNames(
20     VideoCaptureDevice::Names* device_names) {
21   device_names->clear();
22
23   JNIEnv* env = AttachCurrentThread();
24
25   int num_cameras = Java_ChromiumCameraInfo_getNumberOfCameras(
26       env, base::android::GetApplicationContext());
27   DVLOG(1) << "VideoCaptureDevice::GetDeviceNames: num_cameras=" << num_cameras;
28   if (num_cameras <= 0)
29     return;
30
31   for (int camera_id = num_cameras - 1; camera_id >= 0; --camera_id) {
32     ScopedJavaLocalRef<jobject> ci =
33         Java_ChromiumCameraInfo_getAt(env, camera_id);
34
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);
40
41     DVLOG(1) << "VideoCaptureDeviceFactoryAndroid::GetDeviceNames: camera"
42              << "device_name=" << name.name() << ", unique_id=" << name.id()
43              << ", orientation "
44              << Java_ChromiumCameraInfo_getOrientation(env, ci.obj());
45   }
46 }
47
48 // static
49 void VideoCaptureDeviceFactoryAndroid::GetDeviceSupportedFormats(
50     const VideoCaptureDevice::Name& device,
51     VideoCaptureFormats* capture_formats) {
52   int id;
53   if (!base::StringToInt(device.id(), &id))
54     return;
55   JNIEnv* env = AttachCurrentThread();
56   base::android::ScopedJavaLocalRef<jobjectArray> collected_formats =
57       Java_VideoCaptureFactory_getDeviceSupportedFormats(env, id);
58   if (collected_formats.is_null())
59     return;
60
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));
65
66     VideoPixelFormat pixel_format = media::PIXEL_FORMAT_UNKNOWN;
67     switch (media::Java_VideoCaptureFactory_getCaptureFormatPixelFormat(
68         env, format.obj())) {
69       case ANDROID_IMAGEFORMAT_YV12:
70         pixel_format = media::PIXEL_FORMAT_YV12;
71         break;
72       case ANDROID_IMAGEFORMAT_NV21:
73         pixel_format = media::PIXEL_FORMAT_NV21;
74         break;
75       default:
76         break;
77     }
78     VideoCaptureFormat capture_format(
79         gfx::Size(media::Java_VideoCaptureFactory_getCaptureFormatWidth(env,
80                       format.obj()),
81                   media::Java_VideoCaptureFactory_getCaptureFormatHeight(env,
82                       format.obj())),
83         media::Java_VideoCaptureFactory_getCaptureFormatFramerate(env,
84                                                                   format.obj()),
85         pixel_format);
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;
91   }
92 }
93
94 //static
95 ScopedJavaLocalRef<jobject>
96 VideoCaptureDeviceFactoryAndroid::createVideoCaptureAndroid(
97     int id,
98     jlong nativeVideoCaptureDeviceAndroid) {
99   return (Java_VideoCaptureFactory_createVideoCapture(
100       AttachCurrentThread(),
101       base::android::GetApplicationContext(),
102       id,
103       nativeVideoCaptureDeviceAndroid));
104 }
105
106 // static
107 bool VideoCaptureDeviceFactoryAndroid::RegisterVideoCaptureDeviceFactory(
108     JNIEnv* env) {
109   return RegisterNativesImpl(env);
110 }
111
112 }  // namespace media