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