blob: 3f04421a58f9ba7d5eb406803e9bab4087a5e3a3 [file] [log] [blame]
henrikaba35d052015-07-14 17:04:08 +02001/*
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
henrika45c136b2015-10-21 04:11:53 -070013#import <AVFoundation/AVFoundation.h>
henrikaba35d052015-07-14 17:04:08 +020014#import <Foundation/Foundation.h>
15#import <sys/sysctl.h>
16#import <UIKit/UIKit.h>
17
kwiberg22feaa32016-03-17 09:17:43 -070018#include <memory>
19
henrikaba35d052015-07-14 17:04:08 +020020#include "webrtc/base/checks.h"
21#include "webrtc/base/logging.h"
kthelgasonbeafee32016-12-16 02:12:42 -080022#include "webrtc/sdk/objc/Framework/Classes/helpers.h"
henrikaba35d052015-07-14 17:04:08 +020023
24namespace webrtc {
25namespace ios {
26
henrikaab12c472016-03-03 16:59:50 +010027bool isOperatingSystemAtLeastVersion(double version) {
28 return GetSystemVersion() >= version;
29}
30
henrika3e60bf02016-02-24 14:27:09 +010031// Internal helper method used by GetDeviceName() to return device name.
32const 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
henrikaba35d052015-07-14 17:04:08 +020089NSString* NSStringFromStdString(const std::string& stdString) {
90 // std::string may contain null termination character so we construct
91 // using length.
92 return [[NSString alloc] initWithBytes:stdString.data()
93 length:stdString.length()
94 encoding:NSUTF8StringEncoding];
95}
96
97std::string StdStringFromNSString(NSString* nsString) {
98 NSData* charData = [nsString dataUsingEncoding:NSUTF8StringEncoding];
99 return std::string(reinterpret_cast<const char*>([charData bytes]),
100 [charData length]);
101}
102
103bool CheckAndLogError(BOOL success, NSError* error) {
104 if (!success) {
105 NSString* msg =
106 [NSString stringWithFormat:@"Error: %ld, %@, %@", (long)error.code,
107 error.localizedDescription,
108 error.localizedFailureReason];
109 LOG(LS_ERROR) << StdStringFromNSString(msg);
110 return false;
111 }
112 return true;
113}
114
115// TODO(henrika): see if it is possible to move to GetThreadName in
116// platform_thread.h and base it on pthread methods instead.
117std::string GetCurrentThreadDescription() {
118 NSString* name = [NSString stringWithFormat:@"%@", [NSThread currentThread]];
119 return StdStringFromNSString(name);
120}
121
henrika45c136b2015-10-21 04:11:53 -0700122std::string GetAudioSessionCategory() {
123 NSString* category = [[AVAudioSession sharedInstance] category];
124 return StdStringFromNSString(category);
125}
126
henrikaba35d052015-07-14 17:04:08 +0200127std::string GetSystemName() {
128 NSString* osName = [[UIDevice currentDevice] systemName];
129 return StdStringFromNSString(osName);
130}
131
henrikaab12c472016-03-03 16:59:50 +0100132std::string GetSystemVersionAsString() {
henrikaba35d052015-07-14 17:04:08 +0200133 NSString* osVersion = [[UIDevice currentDevice] systemVersion];
134 return StdStringFromNSString(osVersion);
135}
136
henrikaab12c472016-03-03 16:59:50 +0100137double GetSystemVersion() {
138 static dispatch_once_t once_token;
139 static double system_version;
140 dispatch_once(&once_token, ^{
141 system_version = [UIDevice currentDevice].systemVersion.doubleValue;
142 });
143 return system_version;
henrikaba35d052015-07-14 17:04:08 +0200144}
145
146std::string GetDeviceType() {
147 NSString* deviceModel = [[UIDevice currentDevice] model];
148 return StdStringFromNSString(deviceModel);
149}
150
151std::string GetDeviceName() {
152 size_t size;
153 sysctlbyname("hw.machine", NULL, &size, NULL, 0);
kwiberg22feaa32016-03-17 09:17:43 -0700154 std::unique_ptr<char[]> machine;
henrikaba35d052015-07-14 17:04:08 +0200155 machine.reset(new char[size]);
156 sysctlbyname("hw.machine", machine.get(), &size, NULL, 0);
henrika3e60bf02016-02-24 14:27:09 +0100157 return std::string(LookUpRealName(machine.get()));
158}
159
160std::string GetProcessName() {
161 NSString* processName = [NSProcessInfo processInfo].processName;
162 return StdStringFromNSString(processName);
163}
164
165int GetProcessID() {
166 return [NSProcessInfo processInfo].processIdentifier;
167}
168
169std::string GetOSVersionString() {
170 NSString* osVersion =
171 [NSProcessInfo processInfo].operatingSystemVersionString;
172 return StdStringFromNSString(osVersion);
173}
174
175int GetProcessorCount() {
176 return [NSProcessInfo processInfo].processorCount;
177}
178
kwiberg77eab702016-09-28 17:42:01 -0700179#if defined(__IPHONE_9_0) && defined(__IPHONE_OS_VERSION_MAX_ALLOWED) \
180 && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_9_0
henrika3e60bf02016-02-24 14:27:09 +0100181bool GetLowPowerModeEnabled() {
henrikaab12c472016-03-03 16:59:50 +0100182 if (isOperatingSystemAtLeastVersion(9.0)) {
183 // lowPoweredModeEnabled is only available on iOS9+.
184 return [NSProcessInfo processInfo].lowPowerModeEnabled;
tkchinfc59c442016-02-26 00:25:45 -0800185 }
henrikaab12c472016-03-03 16:59:50 +0100186 LOG(LS_WARNING) << "webrtc::ios::GetLowPowerModeEnabled() is not "
187 "supported. Requires at least iOS 9.0";
tkchinfc59c442016-02-26 00:25:45 -0800188 return false;
henrikaba35d052015-07-14 17:04:08 +0200189}
henrikaab12c472016-03-03 16:59:50 +0100190#endif
henrikaba35d052015-07-14 17:04:08 +0200191
192} // namespace ios
193} // namespace webrtc
194
195#endif // defined(WEBRTC_IOS)