OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "media/capture/video/android/video_capture_device_factory_android.h" | 5 #include "media/capture/video/android/video_capture_device_factory_android.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/android/context_utils.h" | |
10 #include "base/android/jni_string.h" | 9 #include "base/android/jni_string.h" |
11 #include "base/android/scoped_java_ref.h" | 10 #include "base/android/scoped_java_ref.h" |
12 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
13 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
14 #include "jni/VideoCaptureFactory_jni.h" | 13 #include "jni/VideoCaptureFactory_jni.h" |
15 #include "media/capture/video/android/video_capture_device_android.h" | 14 #include "media/capture/video/android/video_capture_device_android.h" |
16 | 15 |
17 using base::android::AttachCurrentThread; | 16 using base::android::AttachCurrentThread; |
18 using base::android::JavaRef; | 17 using base::android::JavaRef; |
19 using base::android::ScopedJavaLocalRef; | 18 using base::android::ScopedJavaLocalRef; |
20 | 19 |
21 namespace media { | 20 namespace media { |
22 | 21 |
23 // static | 22 // static |
24 ScopedJavaLocalRef<jobject> | 23 ScopedJavaLocalRef<jobject> |
25 VideoCaptureDeviceFactoryAndroid::createVideoCaptureAndroid( | 24 VideoCaptureDeviceFactoryAndroid::createVideoCaptureAndroid( |
26 int id, | 25 int id, |
27 jlong nativeVideoCaptureDeviceAndroid) { | 26 jlong nativeVideoCaptureDeviceAndroid) { |
28 return (Java_VideoCaptureFactory_createVideoCapture( | 27 return (Java_VideoCaptureFactory_createVideoCapture( |
29 AttachCurrentThread(), base::android::GetApplicationContext(), id, | 28 AttachCurrentThread(), id, nativeVideoCaptureDeviceAndroid)); |
30 nativeVideoCaptureDeviceAndroid)); | |
31 } | 29 } |
32 | 30 |
33 std::unique_ptr<VideoCaptureDevice> | 31 std::unique_ptr<VideoCaptureDevice> |
34 VideoCaptureDeviceFactoryAndroid::CreateDevice( | 32 VideoCaptureDeviceFactoryAndroid::CreateDevice( |
35 const VideoCaptureDeviceDescriptor& device_descriptor) { | 33 const VideoCaptureDeviceDescriptor& device_descriptor) { |
36 DCHECK(thread_checker_.CalledOnValidThread()); | 34 DCHECK(thread_checker_.CalledOnValidThread()); |
37 int id; | 35 int id; |
38 if (!base::StringToInt(device_descriptor.device_id, &id)) | 36 if (!base::StringToInt(device_descriptor.device_id, &id)) |
39 return std::unique_ptr<VideoCaptureDevice>(); | 37 return std::unique_ptr<VideoCaptureDevice>(); |
40 | 38 |
(...skipping 10 matching lines...) Expand all Loading... |
51 return nullptr; | 49 return nullptr; |
52 } | 50 } |
53 | 51 |
54 void VideoCaptureDeviceFactoryAndroid::GetDeviceDescriptors( | 52 void VideoCaptureDeviceFactoryAndroid::GetDeviceDescriptors( |
55 VideoCaptureDeviceDescriptors* device_descriptors) { | 53 VideoCaptureDeviceDescriptors* device_descriptors) { |
56 DCHECK(thread_checker_.CalledOnValidThread()); | 54 DCHECK(thread_checker_.CalledOnValidThread()); |
57 device_descriptors->clear(); | 55 device_descriptors->clear(); |
58 | 56 |
59 JNIEnv* env = AttachCurrentThread(); | 57 JNIEnv* env = AttachCurrentThread(); |
60 | 58 |
61 const JavaRef<jobject>& context = base::android::GetApplicationContext(); | 59 const int num_cameras = Java_VideoCaptureFactory_getNumberOfCameras(env); |
62 const int num_cameras = | |
63 Java_VideoCaptureFactory_getNumberOfCameras(env, context); | |
64 DVLOG(1) << __func__ << ": num_cameras=" << num_cameras; | 60 DVLOG(1) << __func__ << ": num_cameras=" << num_cameras; |
65 if (num_cameras <= 0) | 61 if (num_cameras <= 0) |
66 return; | 62 return; |
67 | 63 |
68 for (int camera_id = num_cameras - 1; camera_id >= 0; --camera_id) { | 64 for (int camera_id = num_cameras - 1; camera_id >= 0; --camera_id) { |
69 base::android::ScopedJavaLocalRef<jstring> device_name = | 65 base::android::ScopedJavaLocalRef<jstring> device_name = |
70 Java_VideoCaptureFactory_getDeviceName(env, camera_id, context); | 66 Java_VideoCaptureFactory_getDeviceName(env, camera_id); |
71 if (device_name.obj() == NULL) | 67 if (device_name.obj() == NULL) |
72 continue; | 68 continue; |
73 | 69 |
74 const int capture_api_type = | 70 const int capture_api_type = |
75 Java_VideoCaptureFactory_getCaptureApiType(env, camera_id, context); | 71 Java_VideoCaptureFactory_getCaptureApiType(env, camera_id); |
76 const std::string display_name = | 72 const std::string display_name = |
77 base::android::ConvertJavaStringToUTF8(device_name); | 73 base::android::ConvertJavaStringToUTF8(device_name); |
78 const std::string device_id = base::IntToString(camera_id); | 74 const std::string device_id = base::IntToString(camera_id); |
79 | 75 |
80 // Android cameras are not typically USB devices, and the model_id is | 76 // Android cameras are not typically USB devices, and the model_id is |
81 // currently only used for USB model identifiers, so this implementation | 77 // currently only used for USB model identifiers, so this implementation |
82 // just indicates an unknown device model (by not providing one). | 78 // just indicates an unknown device model (by not providing one). |
83 device_descriptors->emplace_back( | 79 device_descriptors->emplace_back( |
84 display_name, device_id, | 80 display_name, device_id, |
85 static_cast<VideoCaptureApi>(capture_api_type)); | 81 static_cast<VideoCaptureApi>(capture_api_type)); |
86 | 82 |
87 DVLOG(1) << __func__ << ": camera " | 83 DVLOG(1) << __func__ << ": camera " |
88 << "device_name=" << display_name << ", unique_id=" << device_id; | 84 << "device_name=" << display_name << ", unique_id=" << device_id; |
89 } | 85 } |
90 } | 86 } |
91 | 87 |
92 void VideoCaptureDeviceFactoryAndroid::GetSupportedFormats( | 88 void VideoCaptureDeviceFactoryAndroid::GetSupportedFormats( |
93 const VideoCaptureDeviceDescriptor& device, | 89 const VideoCaptureDeviceDescriptor& device, |
94 VideoCaptureFormats* capture_formats) { | 90 VideoCaptureFormats* capture_formats) { |
95 DCHECK(thread_checker_.CalledOnValidThread()); | 91 DCHECK(thread_checker_.CalledOnValidThread()); |
96 int id; | 92 int id; |
97 if (!base::StringToInt(device.device_id, &id)) | 93 if (!base::StringToInt(device.device_id, &id)) |
98 return; | 94 return; |
99 JNIEnv* env = AttachCurrentThread(); | 95 JNIEnv* env = AttachCurrentThread(); |
100 base::android::ScopedJavaLocalRef<jobjectArray> collected_formats = | 96 base::android::ScopedJavaLocalRef<jobjectArray> collected_formats = |
101 Java_VideoCaptureFactory_getDeviceSupportedFormats( | 97 Java_VideoCaptureFactory_getDeviceSupportedFormats(env, id); |
102 env, base::android::GetApplicationContext(), id); | |
103 if (collected_formats.is_null()) | 98 if (collected_formats.is_null()) |
104 return; | 99 return; |
105 | 100 |
106 jsize num_formats = env->GetArrayLength(collected_formats.obj()); | 101 jsize num_formats = env->GetArrayLength(collected_formats.obj()); |
107 for (int i = 0; i < num_formats; ++i) { | 102 for (int i = 0; i < num_formats; ++i) { |
108 base::android::ScopedJavaLocalRef<jobject> format( | 103 base::android::ScopedJavaLocalRef<jobject> format( |
109 env, env->GetObjectArrayElement(collected_formats.obj(), i)); | 104 env, env->GetObjectArrayElement(collected_formats.obj(), i)); |
110 | 105 |
111 VideoPixelFormat pixel_format = media::PIXEL_FORMAT_UNKNOWN; | 106 VideoPixelFormat pixel_format = media::PIXEL_FORMAT_UNKNOWN; |
112 switch (media::Java_VideoCaptureFactory_getCaptureFormatPixelFormat( | 107 switch (media::Java_VideoCaptureFactory_getCaptureFormatPixelFormat( |
(...skipping 22 matching lines...) Expand all Loading... |
135 << VideoCaptureFormat::ToString(capture_format); | 130 << VideoCaptureFormat::ToString(capture_format); |
136 } | 131 } |
137 } | 132 } |
138 | 133 |
139 bool VideoCaptureDeviceFactoryAndroid::IsLegacyOrDeprecatedDevice( | 134 bool VideoCaptureDeviceFactoryAndroid::IsLegacyOrDeprecatedDevice( |
140 const std::string& device_id) { | 135 const std::string& device_id) { |
141 int id; | 136 int id; |
142 if (!base::StringToInt(device_id, &id)) | 137 if (!base::StringToInt(device_id, &id)) |
143 return true; | 138 return true; |
144 return (Java_VideoCaptureFactory_isLegacyOrDeprecatedDevice( | 139 return (Java_VideoCaptureFactory_isLegacyOrDeprecatedDevice( |
145 AttachCurrentThread(), base::android::GetApplicationContext(), id)); | 140 AttachCurrentThread(), id)); |
146 } | 141 } |
147 | 142 |
148 // static | 143 // static |
149 VideoCaptureDeviceFactory* | 144 VideoCaptureDeviceFactory* |
150 VideoCaptureDeviceFactory::CreateVideoCaptureDeviceFactory( | 145 VideoCaptureDeviceFactory::CreateVideoCaptureDeviceFactory( |
151 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner) { | 146 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner) { |
152 return new VideoCaptureDeviceFactoryAndroid(); | 147 return new VideoCaptureDeviceFactoryAndroid(); |
153 } | 148 } |
154 | 149 |
155 } // namespace media | 150 } // namespace media |
OLD | NEW |