henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 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 | #if defined(WEBRTC_IOS) |
| 12 | |
henrika | 45c136b | 2015-10-21 04:11:53 -0700 | [diff] [blame] | 13 | #import <AVFoundation/AVFoundation.h> |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 14 | #import <Foundation/Foundation.h> |
| 15 | #import <sys/sysctl.h> |
| 16 | #import <UIKit/UIKit.h> |
| 17 | |
kwiberg | 22feaa3 | 2016-03-17 09:17:43 -0700 | [diff] [blame] | 18 | #include <memory> |
| 19 | |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 20 | #include "webrtc/base/checks.h" |
| 21 | #include "webrtc/base/logging.h" |
Henrik Kjellander | ff761fb | 2015-11-04 08:31:52 +0100 | [diff] [blame] | 22 | #include "webrtc/modules/utility/include/helpers_ios.h" |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 23 | |
| 24 | namespace webrtc { |
| 25 | namespace ios { |
| 26 | |
henrika | ab12c47 | 2016-03-03 16:59:50 +0100 | [diff] [blame] | 27 | bool isOperatingSystemAtLeastVersion(double version) { |
| 28 | return GetSystemVersion() >= version; |
| 29 | } |
| 30 | |
henrika | 3e60bf0 | 2016-02-24 14:27:09 +0100 | [diff] [blame] | 31 | // Internal helper method used by GetDeviceName() to return device name. |
| 32 | const char* LookUpRealName(const char* raw_name) { |
| 33 | // Lookup table which maps raw device names to real (human readable) names. |
| 34 | struct { |
| 35 | const char* raw_name; |
| 36 | const char* real_name; |
| 37 | } device_names[] = { |
| 38 | {"iPhone1,1", "iPhone 1G"}, |
| 39 | {"iPhone1,2", "iPhone 3G"}, |
| 40 | {"iPhone2,1", "iPhone 3GS"}, |
| 41 | {"iPhone3,1", "iPhone 4"}, |
| 42 | {"iPhone3,3", "Verizon iPhone 4"}, |
| 43 | {"iPhone4,1", "iPhone 4S"}, |
| 44 | {"iPhone5,1", "iPhone 5 (GSM)"}, |
| 45 | {"iPhone5,2", "iPhone 5 (GSM+CDMA)"}, |
| 46 | {"iPhone5,3", "iPhone 5c (GSM)"}, |
| 47 | {"iPhone5,4", "iPhone 5c (GSM+CDMA)"}, |
| 48 | {"iPhone6,1", "iPhone 5s (GSM)"}, |
| 49 | {"iPhone6,2", "iPhone 5s (GSM+CDMA)"}, |
| 50 | {"iPhone7,1", "iPhone 6 Plus"}, |
| 51 | {"iPhone7,2", "iPhone 6"}, |
| 52 | {"iPhone8,1", "iPhone 6s"}, |
| 53 | {"iPhone8,2", "iPhone 6s Plus"}, |
| 54 | {"iPod1,1", "iPod Touch 1G"}, |
| 55 | {"iPod2,1", "iPod Touch 2G"}, |
| 56 | {"iPod3,1", "iPod Touch 3G"}, |
| 57 | {"iPod4,1", "iPod Touch 4G"}, |
| 58 | {"iPod5,1", "iPod Touch 5G"}, |
| 59 | {"iPad1,1", "iPad"}, |
| 60 | {"iPad2,1", "iPad 2 (WiFi)"}, |
| 61 | {"iPad2,2", "iPad 2 (GSM)"}, |
| 62 | {"iPad2,3", "iPad 2 (CDMA)"}, |
| 63 | {"iPad2,4", "iPad 2 (WiFi)"}, |
| 64 | {"iPad2,5", "iPad Mini (WiFi)"}, |
| 65 | {"iPad2,6", "iPad Mini (GSM)"}, |
| 66 | {"iPad2,7", "iPad Mini (GSM+CDMA)"}, |
| 67 | {"iPad3,1", "iPad 3 (WiFi)"}, |
| 68 | {"iPad3,2", "iPad 3 (GSM+CDMA)"}, |
| 69 | {"iPad3,3", "iPad 3 (GSM)"}, |
| 70 | {"iPad3,4", "iPad 4 (WiFi)"}, |
| 71 | {"iPad3,5", "iPad 4 (GSM)"}, |
| 72 | {"iPad3,6", "iPad 4 (GSM+CDMA)"}, |
| 73 | {"iPad4,1", "iPad Air (WiFi)"}, |
| 74 | {"iPad4,2", "iPad Air (Cellular)"}, |
| 75 | {"iPad4,4", "iPad mini 2G (WiFi)"}, |
| 76 | {"iPad4,5", "iPad mini 2G (Cellular)"}, |
| 77 | {"i386", "Simulator"}, |
| 78 | {"x86_64", "Simulator"}, |
| 79 | }; |
| 80 | |
| 81 | for (auto& d : device_names) { |
| 82 | if (strcmp(d.raw_name, raw_name) == 0) |
| 83 | return d.real_name; |
| 84 | } |
| 85 | LOG(LS_WARNING) << "Failed to find device name (" << raw_name << ")"; |
| 86 | return ""; |
| 87 | } |
| 88 | |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 89 | // TODO(henrika): move to shared location. |
| 90 | // See https://code.google.com/p/webrtc/issues/detail?id=4773 for details. |
| 91 | NSString* NSStringFromStdString(const std::string& stdString) { |
| 92 | // std::string may contain null termination character so we construct |
| 93 | // using length. |
| 94 | return [[NSString alloc] initWithBytes:stdString.data() |
| 95 | length:stdString.length() |
| 96 | encoding:NSUTF8StringEncoding]; |
| 97 | } |
| 98 | |
| 99 | std::string StdStringFromNSString(NSString* nsString) { |
| 100 | NSData* charData = [nsString dataUsingEncoding:NSUTF8StringEncoding]; |
| 101 | return std::string(reinterpret_cast<const char*>([charData bytes]), |
| 102 | [charData length]); |
| 103 | } |
| 104 | |
| 105 | bool CheckAndLogError(BOOL success, NSError* error) { |
| 106 | if (!success) { |
| 107 | NSString* msg = |
| 108 | [NSString stringWithFormat:@"Error: %ld, %@, %@", (long)error.code, |
| 109 | error.localizedDescription, |
| 110 | error.localizedFailureReason]; |
| 111 | LOG(LS_ERROR) << StdStringFromNSString(msg); |
| 112 | return false; |
| 113 | } |
| 114 | return true; |
| 115 | } |
| 116 | |
| 117 | // TODO(henrika): see if it is possible to move to GetThreadName in |
| 118 | // platform_thread.h and base it on pthread methods instead. |
| 119 | std::string GetCurrentThreadDescription() { |
| 120 | NSString* name = [NSString stringWithFormat:@"%@", [NSThread currentThread]]; |
| 121 | return StdStringFromNSString(name); |
| 122 | } |
| 123 | |
henrika | 45c136b | 2015-10-21 04:11:53 -0700 | [diff] [blame] | 124 | std::string GetAudioSessionCategory() { |
| 125 | NSString* category = [[AVAudioSession sharedInstance] category]; |
| 126 | return StdStringFromNSString(category); |
| 127 | } |
| 128 | |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 129 | std::string GetSystemName() { |
| 130 | NSString* osName = [[UIDevice currentDevice] systemName]; |
| 131 | return StdStringFromNSString(osName); |
| 132 | } |
| 133 | |
henrika | ab12c47 | 2016-03-03 16:59:50 +0100 | [diff] [blame] | 134 | std::string GetSystemVersionAsString() { |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 135 | NSString* osVersion = [[UIDevice currentDevice] systemVersion]; |
| 136 | return StdStringFromNSString(osVersion); |
| 137 | } |
| 138 | |
henrika | ab12c47 | 2016-03-03 16:59:50 +0100 | [diff] [blame] | 139 | double GetSystemVersion() { |
| 140 | static dispatch_once_t once_token; |
| 141 | static double system_version; |
| 142 | dispatch_once(&once_token, ^{ |
| 143 | system_version = [UIDevice currentDevice].systemVersion.doubleValue; |
| 144 | }); |
| 145 | return system_version; |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | std::string GetDeviceType() { |
| 149 | NSString* deviceModel = [[UIDevice currentDevice] model]; |
| 150 | return StdStringFromNSString(deviceModel); |
| 151 | } |
| 152 | |
| 153 | std::string GetDeviceName() { |
| 154 | size_t size; |
| 155 | sysctlbyname("hw.machine", NULL, &size, NULL, 0); |
kwiberg | 22feaa3 | 2016-03-17 09:17:43 -0700 | [diff] [blame] | 156 | std::unique_ptr<char[]> machine; |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 157 | machine.reset(new char[size]); |
| 158 | sysctlbyname("hw.machine", machine.get(), &size, NULL, 0); |
henrika | 3e60bf0 | 2016-02-24 14:27:09 +0100 | [diff] [blame] | 159 | return std::string(LookUpRealName(machine.get())); |
| 160 | } |
| 161 | |
| 162 | std::string GetProcessName() { |
| 163 | NSString* processName = [NSProcessInfo processInfo].processName; |
| 164 | return StdStringFromNSString(processName); |
| 165 | } |
| 166 | |
| 167 | int GetProcessID() { |
| 168 | return [NSProcessInfo processInfo].processIdentifier; |
| 169 | } |
| 170 | |
| 171 | std::string GetOSVersionString() { |
| 172 | NSString* osVersion = |
| 173 | [NSProcessInfo processInfo].operatingSystemVersionString; |
| 174 | return StdStringFromNSString(osVersion); |
| 175 | } |
| 176 | |
| 177 | int GetProcessorCount() { |
| 178 | return [NSProcessInfo processInfo].processorCount; |
| 179 | } |
| 180 | |
kwiberg | 77eab70 | 2016-09-28 17:42:01 -0700 | [diff] [blame^] | 181 | #if defined(__IPHONE_9_0) && defined(__IPHONE_OS_VERSION_MAX_ALLOWED) \ |
| 182 | && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_9_0 |
henrika | 3e60bf0 | 2016-02-24 14:27:09 +0100 | [diff] [blame] | 183 | bool GetLowPowerModeEnabled() { |
henrika | ab12c47 | 2016-03-03 16:59:50 +0100 | [diff] [blame] | 184 | if (isOperatingSystemAtLeastVersion(9.0)) { |
| 185 | // lowPoweredModeEnabled is only available on iOS9+. |
| 186 | return [NSProcessInfo processInfo].lowPowerModeEnabled; |
tkchin | fc59c44 | 2016-02-26 00:25:45 -0800 | [diff] [blame] | 187 | } |
henrika | ab12c47 | 2016-03-03 16:59:50 +0100 | [diff] [blame] | 188 | LOG(LS_WARNING) << "webrtc::ios::GetLowPowerModeEnabled() is not " |
| 189 | "supported. Requires at least iOS 9.0"; |
tkchin | fc59c44 | 2016-02-26 00:25:45 -0800 | [diff] [blame] | 190 | return false; |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 191 | } |
henrika | ab12c47 | 2016-03-03 16:59:50 +0100 | [diff] [blame] | 192 | #endif |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 193 | |
| 194 | } // namespace ios |
| 195 | } // namespace webrtc |
| 196 | |
| 197 | #endif // defined(WEBRTC_IOS) |