blob: ef0e2803f3965fbebbf3264c0649a35be3aabbcb [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
henrikaba35d052015-07-14 17:04:08 +020031NSString* NSStringFromStdString(const std::string& stdString) {
32 // std::string may contain null termination character so we construct
33 // using length.
34 return [[NSString alloc] initWithBytes:stdString.data()
35 length:stdString.length()
36 encoding:NSUTF8StringEncoding];
37}
38
39std::string StdStringFromNSString(NSString* nsString) {
40 NSData* charData = [nsString dataUsingEncoding:NSUTF8StringEncoding];
41 return std::string(reinterpret_cast<const char*>([charData bytes]),
42 [charData length]);
43}
44
45bool CheckAndLogError(BOOL success, NSError* error) {
46 if (!success) {
47 NSString* msg =
48 [NSString stringWithFormat:@"Error: %ld, %@, %@", (long)error.code,
49 error.localizedDescription,
50 error.localizedFailureReason];
51 LOG(LS_ERROR) << StdStringFromNSString(msg);
52 return false;
53 }
54 return true;
55}
56
57// TODO(henrika): see if it is possible to move to GetThreadName in
58// platform_thread.h and base it on pthread methods instead.
59std::string GetCurrentThreadDescription() {
60 NSString* name = [NSString stringWithFormat:@"%@", [NSThread currentThread]];
61 return StdStringFromNSString(name);
62}
63
henrika45c136b2015-10-21 04:11:53 -070064std::string GetAudioSessionCategory() {
65 NSString* category = [[AVAudioSession sharedInstance] category];
66 return StdStringFromNSString(category);
67}
68
henrikaba35d052015-07-14 17:04:08 +020069std::string GetSystemName() {
70 NSString* osName = [[UIDevice currentDevice] systemName];
71 return StdStringFromNSString(osName);
72}
73
henrikaab12c472016-03-03 16:59:50 +010074std::string GetSystemVersionAsString() {
henrikaba35d052015-07-14 17:04:08 +020075 NSString* osVersion = [[UIDevice currentDevice] systemVersion];
76 return StdStringFromNSString(osVersion);
77}
78
henrikaab12c472016-03-03 16:59:50 +010079double GetSystemVersion() {
80 static dispatch_once_t once_token;
81 static double system_version;
82 dispatch_once(&once_token, ^{
83 system_version = [UIDevice currentDevice].systemVersion.doubleValue;
84 });
85 return system_version;
henrikaba35d052015-07-14 17:04:08 +020086}
87
88std::string GetDeviceType() {
89 NSString* deviceModel = [[UIDevice currentDevice] model];
90 return StdStringFromNSString(deviceModel);
91}
92
93std::string GetDeviceName() {
94 size_t size;
95 sysctlbyname("hw.machine", NULL, &size, NULL, 0);
kwiberg22feaa32016-03-17 09:17:43 -070096 std::unique_ptr<char[]> machine;
henrikaba35d052015-07-14 17:04:08 +020097 machine.reset(new char[size]);
98 sysctlbyname("hw.machine", machine.get(), &size, NULL, 0);
kthelgasonb3df3852016-12-16 06:17:58 -080099 return std::string(machine.get());
henrika3e60bf02016-02-24 14:27:09 +0100100}
101
102std::string GetProcessName() {
103 NSString* processName = [NSProcessInfo processInfo].processName;
104 return StdStringFromNSString(processName);
105}
106
107int GetProcessID() {
108 return [NSProcessInfo processInfo].processIdentifier;
109}
110
111std::string GetOSVersionString() {
112 NSString* osVersion =
113 [NSProcessInfo processInfo].operatingSystemVersionString;
114 return StdStringFromNSString(osVersion);
115}
116
117int GetProcessorCount() {
118 return [NSProcessInfo processInfo].processorCount;
119}
120
kwiberg77eab702016-09-28 17:42:01 -0700121#if defined(__IPHONE_9_0) && defined(__IPHONE_OS_VERSION_MAX_ALLOWED) \
122 && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_9_0
henrika3e60bf02016-02-24 14:27:09 +0100123bool GetLowPowerModeEnabled() {
henrikaab12c472016-03-03 16:59:50 +0100124 if (isOperatingSystemAtLeastVersion(9.0)) {
125 // lowPoweredModeEnabled is only available on iOS9+.
126 return [NSProcessInfo processInfo].lowPowerModeEnabled;
tkchinfc59c442016-02-26 00:25:45 -0800127 }
henrikaab12c472016-03-03 16:59:50 +0100128 LOG(LS_WARNING) << "webrtc::ios::GetLowPowerModeEnabled() is not "
129 "supported. Requires at least iOS 9.0";
tkchinfc59c442016-02-26 00:25:45 -0800130 return false;
henrikaba35d052015-07-14 17:04:08 +0200131}
henrikaab12c472016-03-03 16:59:50 +0100132#endif
henrikaba35d052015-07-14 17:04:08 +0200133
134} // namespace ios
135} // namespace webrtc
136
137#endif // defined(WEBRTC_IOS)