| OLD | NEW |
| (Empty) |
| 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/capture/video/android/video_capture_device_factory_android.h" | |
| 6 | |
| 7 #include <utility> | |
| 8 | |
| 9 #include "base/android/context_utils.h" | |
| 10 #include "base/android/jni_string.h" | |
| 11 #include "base/strings/string_number_conversions.h" | |
| 12 #include "base/strings/stringprintf.h" | |
| 13 #include "jni/VideoCaptureFactory_jni.h" | |
| 14 #include "media/capture/video/android/video_capture_device_android.h" | |
| 15 | |
| 16 using base::android::AttachCurrentThread; | |
| 17 using base::android::ScopedJavaLocalRef; | |
| 18 | |
| 19 namespace media { | |
| 20 | |
| 21 // static | |
| 22 bool VideoCaptureDeviceFactoryAndroid::RegisterVideoCaptureDeviceFactory( | |
| 23 JNIEnv* env) { | |
| 24 return RegisterNativesImpl(env); | |
| 25 } | |
| 26 | |
| 27 // static | |
| 28 ScopedJavaLocalRef<jobject> | |
| 29 VideoCaptureDeviceFactoryAndroid::createVideoCaptureAndroid( | |
| 30 int id, | |
| 31 jlong nativeVideoCaptureDeviceAndroid) { | |
| 32 return (Java_VideoCaptureFactory_createVideoCapture( | |
| 33 AttachCurrentThread(), base::android::GetApplicationContext(), id, | |
| 34 nativeVideoCaptureDeviceAndroid)); | |
| 35 } | |
| 36 | |
| 37 std::unique_ptr<VideoCaptureDevice> VideoCaptureDeviceFactoryAndroid::Create( | |
| 38 const VideoCaptureDevice::Name& device_name) { | |
| 39 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 40 int id; | |
| 41 if (!base::StringToInt(device_name.id(), &id)) | |
| 42 return std::unique_ptr<VideoCaptureDevice>(); | |
| 43 | |
| 44 std::unique_ptr<VideoCaptureDeviceAndroid> video_capture_device( | |
| 45 new VideoCaptureDeviceAndroid(device_name)); | |
| 46 | |
| 47 if (video_capture_device->Init()) | |
| 48 return std::move(video_capture_device); | |
| 49 | |
| 50 DLOG(ERROR) << "Error creating Video Capture Device."; | |
| 51 return std::unique_ptr<VideoCaptureDevice>(); | |
| 52 } | |
| 53 | |
| 54 void VideoCaptureDeviceFactoryAndroid::GetDeviceNames( | |
| 55 VideoCaptureDevice::Names* device_names) { | |
| 56 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 57 device_names->clear(); | |
| 58 | |
| 59 JNIEnv* env = AttachCurrentThread(); | |
| 60 | |
| 61 const jobject context = base::android::GetApplicationContext(); | |
| 62 const int num_cameras = | |
| 63 Java_VideoCaptureFactory_getNumberOfCameras(env, context); | |
| 64 DVLOG(1) << "VideoCaptureDevice::GetDeviceNames: num_cameras=" << num_cameras; | |
| 65 if (num_cameras <= 0) | |
| 66 return; | |
| 67 | |
| 68 for (int camera_id = num_cameras - 1; camera_id >= 0; --camera_id) { | |
| 69 base::android::ScopedJavaLocalRef<jstring> device_name = | |
| 70 Java_VideoCaptureFactory_getDeviceName(env, camera_id, context); | |
| 71 if (device_name.obj() == NULL) | |
| 72 continue; | |
| 73 | |
| 74 const int capture_api_type = | |
| 75 Java_VideoCaptureFactory_getCaptureApiType(env, camera_id, context); | |
| 76 | |
| 77 VideoCaptureDevice::Name name( | |
| 78 base::android::ConvertJavaStringToUTF8(device_name), | |
| 79 base::IntToString(camera_id), | |
| 80 static_cast<VideoCaptureDevice::Name::CaptureApiType>( | |
| 81 capture_api_type)); | |
| 82 device_names->push_back(name); | |
| 83 | |
| 84 DVLOG(1) << "VideoCaptureDeviceFactoryAndroid::GetDeviceNames: camera " | |
| 85 << "device_name=" << name.name() << ", unique_id=" << name.id(); | |
| 86 } | |
| 87 } | |
| 88 | |
| 89 void VideoCaptureDeviceFactoryAndroid::GetDeviceSupportedFormats( | |
| 90 const VideoCaptureDevice::Name& device, | |
| 91 VideoCaptureFormats* capture_formats) { | |
| 92 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 93 int id; | |
| 94 if (!base::StringToInt(device.id(), &id)) | |
| 95 return; | |
| 96 JNIEnv* env = AttachCurrentThread(); | |
| 97 base::android::ScopedJavaLocalRef<jobjectArray> collected_formats = | |
| 98 Java_VideoCaptureFactory_getDeviceSupportedFormats( | |
| 99 env, base::android::GetApplicationContext(), id); | |
| 100 if (collected_formats.is_null()) | |
| 101 return; | |
| 102 | |
| 103 jsize num_formats = env->GetArrayLength(collected_formats.obj()); | |
| 104 for (int i = 0; i < num_formats; ++i) { | |
| 105 base::android::ScopedJavaLocalRef<jobject> format( | |
| 106 env, env->GetObjectArrayElement(collected_formats.obj(), i)); | |
| 107 | |
| 108 VideoPixelFormat pixel_format = | |
| 109 media::PIXEL_FORMAT_UNKNOWN; | |
| 110 switch (media::Java_VideoCaptureFactory_getCaptureFormatPixelFormat( | |
| 111 env, format.obj())) { | |
| 112 case VideoCaptureDeviceAndroid::ANDROID_IMAGE_FORMAT_YV12: | |
| 113 pixel_format = media::PIXEL_FORMAT_YV12; | |
| 114 break; | |
| 115 case VideoCaptureDeviceAndroid::ANDROID_IMAGE_FORMAT_NV21: | |
| 116 pixel_format = media::PIXEL_FORMAT_NV21; | |
| 117 break; | |
| 118 default: | |
| 119 continue; | |
| 120 } | |
| 121 VideoCaptureFormat capture_format( | |
| 122 gfx::Size(media::Java_VideoCaptureFactory_getCaptureFormatWidth( | |
| 123 env, format.obj()), | |
| 124 media::Java_VideoCaptureFactory_getCaptureFormatHeight( | |
| 125 env, format.obj())), | |
| 126 media::Java_VideoCaptureFactory_getCaptureFormatFramerate(env, | |
| 127 format.obj()), | |
| 128 pixel_format); | |
| 129 capture_formats->push_back(capture_format); | |
| 130 DVLOG(1) << device.name() << " " | |
| 131 << VideoCaptureFormat::ToString(capture_format); | |
| 132 } | |
| 133 } | |
| 134 | |
| 135 // static | |
| 136 VideoCaptureDeviceFactory* | |
| 137 VideoCaptureDeviceFactory::CreateVideoCaptureDeviceFactory( | |
| 138 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner) { | |
| 139 return new VideoCaptureDeviceFactoryAndroid(); | |
| 140 } | |
| 141 | |
| 142 } // namespace media | |
| OLD | NEW |