blob: 3d7567b7f0f6362c1931f3ca286358801fb8e58c [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
18#include "webrtc/base/checks.h"
19#include "webrtc/base/logging.h"
20#include "webrtc/base/scoped_ptr.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010021#include "webrtc/modules/utility/include/helpers_ios.h"
henrikaba35d052015-07-14 17:04:08 +020022
23namespace webrtc {
24namespace ios {
25
henrikaab12c472016-03-03 16:59:50 +010026bool isOperatingSystemAtLeastVersion(double version) {
27 return GetSystemVersion() >= version;
28}
29
henrika3e60bf02016-02-24 14:27:09 +010030// Internal helper method used by GetDeviceName() to return device name.
31const char* LookUpRealName(const char* raw_name) {
32 // Lookup table which maps raw device names to real (human readable) names.
33 struct {
34 const char* raw_name;
35 const char* real_name;
36 } device_names[] = {
37 {"iPhone1,1", "iPhone 1G"},
38 {"iPhone1,2", "iPhone 3G"},
39 {"iPhone2,1", "iPhone 3GS"},
40 {"iPhone3,1", "iPhone 4"},
41 {"iPhone3,3", "Verizon iPhone 4"},
42 {"iPhone4,1", "iPhone 4S"},
43 {"iPhone5,1", "iPhone 5 (GSM)"},
44 {"iPhone5,2", "iPhone 5 (GSM+CDMA)"},
45 {"iPhone5,3", "iPhone 5c (GSM)"},
46 {"iPhone5,4", "iPhone 5c (GSM+CDMA)"},
47 {"iPhone6,1", "iPhone 5s (GSM)"},
48 {"iPhone6,2", "iPhone 5s (GSM+CDMA)"},
49 {"iPhone7,1", "iPhone 6 Plus"},
50 {"iPhone7,2", "iPhone 6"},
51 {"iPhone8,1", "iPhone 6s"},
52 {"iPhone8,2", "iPhone 6s Plus"},
53 {"iPod1,1", "iPod Touch 1G"},
54 {"iPod2,1", "iPod Touch 2G"},
55 {"iPod3,1", "iPod Touch 3G"},
56 {"iPod4,1", "iPod Touch 4G"},
57 {"iPod5,1", "iPod Touch 5G"},
58 {"iPad1,1", "iPad"},
59 {"iPad2,1", "iPad 2 (WiFi)"},
60 {"iPad2,2", "iPad 2 (GSM)"},
61 {"iPad2,3", "iPad 2 (CDMA)"},
62 {"iPad2,4", "iPad 2 (WiFi)"},
63 {"iPad2,5", "iPad Mini (WiFi)"},
64 {"iPad2,6", "iPad Mini (GSM)"},
65 {"iPad2,7", "iPad Mini (GSM+CDMA)"},
66 {"iPad3,1", "iPad 3 (WiFi)"},
67 {"iPad3,2", "iPad 3 (GSM+CDMA)"},
68 {"iPad3,3", "iPad 3 (GSM)"},
69 {"iPad3,4", "iPad 4 (WiFi)"},
70 {"iPad3,5", "iPad 4 (GSM)"},
71 {"iPad3,6", "iPad 4 (GSM+CDMA)"},
72 {"iPad4,1", "iPad Air (WiFi)"},
73 {"iPad4,2", "iPad Air (Cellular)"},
74 {"iPad4,4", "iPad mini 2G (WiFi)"},
75 {"iPad4,5", "iPad mini 2G (Cellular)"},
76 {"i386", "Simulator"},
77 {"x86_64", "Simulator"},
78 };
79
80 for (auto& d : device_names) {
81 if (strcmp(d.raw_name, raw_name) == 0)
82 return d.real_name;
83 }
84 LOG(LS_WARNING) << "Failed to find device name (" << raw_name << ")";
85 return "";
86}
87
henrikaba35d052015-07-14 17:04:08 +020088// TODO(henrika): move to shared location.
89// See https://code.google.com/p/webrtc/issues/detail?id=4773 for details.
90NSString* NSStringFromStdString(const std::string& stdString) {
91 // std::string may contain null termination character so we construct
92 // using length.
93 return [[NSString alloc] initWithBytes:stdString.data()
94 length:stdString.length()
95 encoding:NSUTF8StringEncoding];
96}
97
98std::string StdStringFromNSString(NSString* nsString) {
99 NSData* charData = [nsString dataUsingEncoding:NSUTF8StringEncoding];
100 return std::string(reinterpret_cast<const char*>([charData bytes]),
101 [charData length]);
102}
103
104bool CheckAndLogError(BOOL success, NSError* error) {
105 if (!success) {
106 NSString* msg =
107 [NSString stringWithFormat:@"Error: %ld, %@, %@", (long)error.code,
108 error.localizedDescription,
109 error.localizedFailureReason];
110 LOG(LS_ERROR) << StdStringFromNSString(msg);
111 return false;
112 }
113 return true;
114}
115
116// TODO(henrika): see if it is possible to move to GetThreadName in
117// platform_thread.h and base it on pthread methods instead.
118std::string GetCurrentThreadDescription() {
119 NSString* name = [NSString stringWithFormat:@"%@", [NSThread currentThread]];
120 return StdStringFromNSString(name);
121}
122
henrika45c136b2015-10-21 04:11:53 -0700123std::string GetAudioSessionCategory() {
124 NSString* category = [[AVAudioSession sharedInstance] category];
125 return StdStringFromNSString(category);
126}
127
henrikaba35d052015-07-14 17:04:08 +0200128std::string GetSystemName() {
129 NSString* osName = [[UIDevice currentDevice] systemName];
130 return StdStringFromNSString(osName);
131}
132
henrikaab12c472016-03-03 16:59:50 +0100133std::string GetSystemVersionAsString() {
henrikaba35d052015-07-14 17:04:08 +0200134 NSString* osVersion = [[UIDevice currentDevice] systemVersion];
135 return StdStringFromNSString(osVersion);
136}
137
henrikaab12c472016-03-03 16:59:50 +0100138double GetSystemVersion() {
139 static dispatch_once_t once_token;
140 static double system_version;
141 dispatch_once(&once_token, ^{
142 system_version = [UIDevice currentDevice].systemVersion.doubleValue;
143 });
144 return system_version;
henrikaba35d052015-07-14 17:04:08 +0200145}
146
147std::string GetDeviceType() {
148 NSString* deviceModel = [[UIDevice currentDevice] model];
149 return StdStringFromNSString(deviceModel);
150}
151
152std::string GetDeviceName() {
153 size_t size;
154 sysctlbyname("hw.machine", NULL, &size, NULL, 0);
155 rtc::scoped_ptr<char[]> machine;
156 machine.reset(new char[size]);
157 sysctlbyname("hw.machine", machine.get(), &size, NULL, 0);
henrika3e60bf02016-02-24 14:27:09 +0100158 return std::string(LookUpRealName(machine.get()));
159}
160
161std::string GetProcessName() {
162 NSString* processName = [NSProcessInfo processInfo].processName;
163 return StdStringFromNSString(processName);
164}
165
166int GetProcessID() {
167 return [NSProcessInfo processInfo].processIdentifier;
168}
169
170std::string GetOSVersionString() {
171 NSString* osVersion =
172 [NSProcessInfo processInfo].operatingSystemVersionString;
173 return StdStringFromNSString(osVersion);
174}
175
176int GetProcessorCount() {
177 return [NSProcessInfo processInfo].processorCount;
178}
179
henrikaab12c472016-03-03 16:59:50 +0100180#if defined(__IPHONE_9_0) && __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)