1 /* 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 #include "webrtc/modules/video_capture/android/device_info_android.h" 12 13 #include <algorithm> 14 #include <sstream> 15 #include <vector> 16 17 #include "json/json.h" 18 #include "third_party/icu/source/common/unicode/unistr.h" 19 #include "webrtc/modules/video_capture/android/video_capture_android.h" 20 #include "webrtc/system_wrappers/interface/logging.h" 21 #include "webrtc/system_wrappers/interface/ref_count.h" 22 #include "webrtc/system_wrappers/interface/trace.h" 23 24 namespace webrtc { 25 26 namespace videocapturemodule { 27 28 // Helper for storing lists of pairs of ints. Used e.g. for resolutions & FPS 29 // ranges. 30 typedef std::pair<int, int> IntPair; 31 typedef std::vector<IntPair> IntPairs; 32 33 static std::string IntPairsToString(const IntPairs& pairs, char separator) { 34 std::stringstream stream; 35 for (size_t i = 0; i < pairs.size(); ++i) { 36 if (i > 0) 37 stream << ", "; 38 stream << "(" << pairs[i].first << separator << pairs[i].second << ")"; 39 } 40 return stream.str(); 41 } 42 43 struct AndroidCameraInfo { 44 std::string name; 45 bool front_facing; 46 int orientation; 47 IntPairs resolutions; // Pairs are: (width,height). 48 // Pairs are (min,max) in units of FPS*1000 ("milli-frame-per-second"). 49 IntPairs mfpsRanges; 50 51 std::string ToString() { 52 std::stringstream stream; 53 stream << "Name: [" << name << "], MFPS ranges: [" 54 << IntPairsToString(mfpsRanges, ':') 55 << "], front_facing: " << front_facing 56 << ", orientation: " << orientation << ", resolutions: [" 57 << IntPairsToString(resolutions, 'x') << "]"; 58 return stream.str(); 59 } 60 }; 61 62 // Camera info; populated during DeviceInfoAndroid::Initialize() and immutable 63 // thereafter. 64 static std::vector<AndroidCameraInfo>* g_camera_info = NULL; 65 66 // Set |*index| to the index of |name| in g_camera_info or return false if no 67 // match found. 68 static bool FindCameraIndexByName(const std::string& name, size_t* index) { 69 for (size_t i = 0; i < g_camera_info->size(); ++i) { 70 if (g_camera_info->at(i).name == name) { 71 *index = i; 72 return true; 73 } 74 } 75 return false; 76 } 77 78 // Returns a pointer to the named member of g_camera_info, or NULL if no match 79 // is found. 80 static AndroidCameraInfo* FindCameraInfoByName(const std::string& name) { 81 size_t index = 0; 82 if (FindCameraIndexByName(name, &index)) 83 return &g_camera_info->at(index); 84 return NULL; 85 } 86 87 // static 88 void DeviceInfoAndroid::Initialize(JNIEnv* jni) { 89 // TODO(henrike): this "if" would make a lot more sense as an assert, but 90 // Java_org_webrtc_videoengineapp_ViEAndroidJavaAPI_GetVideoEngine() and 91 // Java_org_webrtc_videoengineapp_ViEAndroidJavaAPI_Terminate() conspire to 92 // prevent this. Once that code is made to only 93 // VideoEngine::SetAndroidObjects() once per process, this can turn into an 94 // assert. 95 if (g_camera_info) 96 return; 97 98 g_camera_info = new std::vector<AndroidCameraInfo>(); 99 jclass j_info_class = 100 jni->FindClass("org/webrtc/videoengine/VideoCaptureDeviceInfoAndroid"); 101 assert(j_info_class); 102 jmethodID j_initialize = jni->GetStaticMethodID( 103 j_info_class, "getDeviceInfo", "()Ljava/lang/String;"); 104 jstring j_json_info = static_cast<jstring>( 105 jni->CallStaticObjectMethod(j_info_class, j_initialize)); 106 107 const jchar* jchars = jni->GetStringChars(j_json_info, NULL); 108 icu::UnicodeString ustr(jchars, jni->GetStringLength(j_json_info)); 109 jni->ReleaseStringChars(j_json_info, jchars); 110 std::string json_info; 111 ustr.toUTF8String(json_info); 112 113 Json::Value cameras; 114 Json::Reader reader(Json::Features::strictMode()); 115 bool parsed = reader.parse(json_info, cameras); 116 if (!parsed) { 117 std::stringstream stream; 118 stream << "Failed to parse configuration:\n" 119 << reader.getFormattedErrorMessages(); 120 assert(false); 121 return; 122 } 123 for (Json::ArrayIndex i = 0; i < cameras.size(); ++i) { 124 const Json::Value& camera = cameras[i]; 125 AndroidCameraInfo info; 126 info.name = camera["name"].asString(); 127 info.front_facing = camera["front_facing"].asBool(); 128 info.orientation = camera["orientation"].asInt(); 129 Json::Value sizes = camera["sizes"]; 130 for (Json::ArrayIndex j = 0; j < sizes.size(); ++j) { 131 const Json::Value& size = sizes[j]; 132 info.resolutions.push_back(std::make_pair( 133 size["width"].asInt(), size["height"].asInt())); 134 } 135 Json::Value mfpsRanges = camera["mfpsRanges"]; 136 for (Json::ArrayIndex j = 0; j < mfpsRanges.size(); ++j) { 137 const Json::Value& mfpsRange = mfpsRanges[j]; 138 info.mfpsRanges.push_back(std::make_pair(mfpsRange["min_mfps"].asInt(), 139 mfpsRange["max_mfps"].asInt())); 140 } 141 g_camera_info->push_back(info); 142 } 143 } 144 145 void DeviceInfoAndroid::DeInitialize() { 146 if (g_camera_info) { 147 delete g_camera_info; 148 g_camera_info = NULL; 149 } 150 } 151 152 VideoCaptureModule::DeviceInfo* VideoCaptureImpl::CreateDeviceInfo( 153 const int32_t id) { 154 return new videocapturemodule::DeviceInfoAndroid(id); 155 } 156 157 DeviceInfoAndroid::DeviceInfoAndroid(const int32_t id) : 158 DeviceInfoImpl(id) { 159 } 160 161 DeviceInfoAndroid::~DeviceInfoAndroid() { 162 } 163 164 bool DeviceInfoAndroid::FindCameraIndex(const char* deviceUniqueIdUTF8, 165 size_t* index) { 166 return FindCameraIndexByName(deviceUniqueIdUTF8, index); 167 } 168 169 int32_t DeviceInfoAndroid::Init() { 170 return 0; 171 } 172 173 uint32_t DeviceInfoAndroid::NumberOfDevices() { 174 return g_camera_info->size(); 175 } 176 177 int32_t DeviceInfoAndroid::GetDeviceName( 178 uint32_t deviceNumber, 179 char* deviceNameUTF8, 180 uint32_t deviceNameLength, 181 char* deviceUniqueIdUTF8, 182 uint32_t deviceUniqueIdUTF8Length, 183 char* /*productUniqueIdUTF8*/, 184 uint32_t /*productUniqueIdUTF8Length*/) { 185 if (deviceNumber >= g_camera_info->size()) 186 return -1; 187 const AndroidCameraInfo& info = g_camera_info->at(deviceNumber); 188 if (info.name.length() + 1 > deviceNameLength || 189 info.name.length() + 1 > deviceUniqueIdUTF8Length) { 190 return -1; 191 } 192 memcpy(deviceNameUTF8, info.name.c_str(), info.name.length() + 1); 193 memcpy(deviceUniqueIdUTF8, info.name.c_str(), info.name.length() + 1); 194 return 0; 195 } 196 197 int32_t DeviceInfoAndroid::CreateCapabilityMap( 198 const char* deviceUniqueIdUTF8) { 199 _captureCapabilities.clear(); 200 const AndroidCameraInfo* info = FindCameraInfoByName(deviceUniqueIdUTF8); 201 if (info == NULL) 202 return -1; 203 204 for (size_t i = 0; i < info->resolutions.size(); ++i) { 205 for (size_t j = 0; j < info->mfpsRanges.size(); ++j) { 206 const IntPair& size = info->resolutions[i]; 207 const IntPair& mfpsRange = info->mfpsRanges[j]; 208 VideoCaptureCapability cap; 209 cap.width = size.first; 210 cap.height = size.second; 211 cap.maxFPS = mfpsRange.second / 1000; 212 cap.expectedCaptureDelay = kExpectedCaptureDelay; 213 cap.rawType = kVideoNV21; 214 _captureCapabilities.push_back(cap); 215 } 216 } 217 return _captureCapabilities.size(); 218 } 219 220 int32_t DeviceInfoAndroid::GetOrientation( 221 const char* deviceUniqueIdUTF8, 222 VideoCaptureRotation& orientation) { 223 const AndroidCameraInfo* info = FindCameraInfoByName(deviceUniqueIdUTF8); 224 if (info == NULL || 225 !VideoCaptureImpl::RotationFromDegrees(info->orientation, &orientation)) { 226 return -1; 227 } 228 return 0; 229 } 230 231 void DeviceInfoAndroid::GetMFpsRange(const char* deviceUniqueIdUTF8, 232 int max_fps_to_match, 233 int* min_mfps, int* max_mfps) { 234 const AndroidCameraInfo* info = FindCameraInfoByName(deviceUniqueIdUTF8); 235 if (info == NULL) 236 return; 237 // Rely on CameraParameters.getSupportedPreviewFpsRange() to sort its return 238 // value (per its documentation) and return the first (most flexible) range 239 // whose high end is at least as high as that requested. 240 for (size_t i = 0; i < info->mfpsRanges.size(); ++i) { 241 if (info->mfpsRanges[i].second / 1000 >= max_fps_to_match) { 242 *min_mfps = info->mfpsRanges[i].first; 243 *max_mfps = info->mfpsRanges[i].second; 244 return; 245 } 246 } 247 } 248 249 } // namespace videocapturemodule 250 } // namespace webrtc 251