OLD | NEW |
(Empty) | |
| 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 |
| 12 #import <AVFoundation/AVFoundation.h> |
| 13 #import <Foundation/Foundation.h> |
| 14 #import <sys/sysctl.h> |
| 15 #if defined(WEBRTC_IOS) |
| 16 #import <UIKit/UIKit.h> |
| 17 #endif |
| 18 |
| 19 #include <memory> |
| 20 |
| 21 #include "webrtc/base/checks.h" |
| 22 #include "webrtc/base/logging.h" |
| 23 #include "webrtc/sdk/objc/Framework/Classes/helpers.h" |
| 24 |
| 25 namespace webrtc { |
| 26 namespace ios { |
| 27 |
| 28 #if defined(WEBRTC_IOS) |
| 29 bool isOperatingSystemAtLeastVersion(double version) { |
| 30 return GetSystemVersion() >= version; |
| 31 } |
| 32 #endif |
| 33 |
| 34 NSString* NSStringFromStdString(const std::string& stdString) { |
| 35 // std::string may contain null termination character so we construct |
| 36 // using length. |
| 37 return [[NSString alloc] initWithBytes:stdString.data() |
| 38 length:stdString.length() |
| 39 encoding:NSUTF8StringEncoding]; |
| 40 } |
| 41 |
| 42 std::string StdStringFromNSString(NSString* nsString) { |
| 43 NSData* charData = [nsString dataUsingEncoding:NSUTF8StringEncoding]; |
| 44 return std::string(reinterpret_cast<const char*>([charData bytes]), |
| 45 [charData length]); |
| 46 } |
| 47 |
| 48 bool CheckAndLogError(BOOL success, NSError* error) { |
| 49 if (!success) { |
| 50 NSString* msg = |
| 51 [NSString stringWithFormat:@"Error: %ld, %@, %@", (long)error.code, |
| 52 error.localizedDescription, |
| 53 error.localizedFailureReason]; |
| 54 LOG(LS_ERROR) << StdStringFromNSString(msg); |
| 55 return false; |
| 56 } |
| 57 return true; |
| 58 } |
| 59 |
| 60 // TODO(henrika): see if it is possible to move to GetThreadName in |
| 61 // platform_thread.h and base it on pthread methods instead. |
| 62 std::string GetCurrentThreadDescription() { |
| 63 NSString* name = [NSString stringWithFormat:@"%@", [NSThread currentThread]]; |
| 64 return StdStringFromNSString(name); |
| 65 } |
| 66 |
| 67 #if defined(WEBRTC_IOS) |
| 68 std::string GetAudioSessionCategory() { |
| 69 NSString* category = [[AVAudioSession sharedInstance] category]; |
| 70 return StdStringFromNSString(category); |
| 71 } |
| 72 |
| 73 std::string GetSystemName() { |
| 74 NSString* osName = [[UIDevice currentDevice] systemName]; |
| 75 return StdStringFromNSString(osName); |
| 76 } |
| 77 |
| 78 std::string GetSystemVersionAsString() { |
| 79 NSString* osVersion = [[UIDevice currentDevice] systemVersion]; |
| 80 return StdStringFromNSString(osVersion); |
| 81 } |
| 82 |
| 83 double GetSystemVersion() { |
| 84 static dispatch_once_t once_token; |
| 85 static double system_version; |
| 86 dispatch_once(&once_token, ^{ |
| 87 system_version = [UIDevice currentDevice].systemVersion.doubleValue; |
| 88 }); |
| 89 return system_version; |
| 90 } |
| 91 |
| 92 std::string GetDeviceType() { |
| 93 NSString* deviceModel = [[UIDevice currentDevice] model]; |
| 94 return StdStringFromNSString(deviceModel); |
| 95 } |
| 96 #endif |
| 97 |
| 98 std::string GetDeviceName() { |
| 99 size_t size; |
| 100 sysctlbyname("hw.machine", NULL, &size, NULL, 0); |
| 101 std::unique_ptr<char[]> machine; |
| 102 machine.reset(new char[size]); |
| 103 sysctlbyname("hw.machine", machine.get(), &size, NULL, 0); |
| 104 return std::string(machine.get()); |
| 105 } |
| 106 |
| 107 std::string GetProcessName() { |
| 108 NSString* processName = [NSProcessInfo processInfo].processName; |
| 109 return StdStringFromNSString(processName); |
| 110 } |
| 111 |
| 112 int GetProcessID() { |
| 113 return [NSProcessInfo processInfo].processIdentifier; |
| 114 } |
| 115 |
| 116 std::string GetOSVersionString() { |
| 117 NSString* osVersion = |
| 118 [NSProcessInfo processInfo].operatingSystemVersionString; |
| 119 return StdStringFromNSString(osVersion); |
| 120 } |
| 121 |
| 122 int GetProcessorCount() { |
| 123 return [NSProcessInfo processInfo].processorCount; |
| 124 } |
| 125 |
| 126 #if defined(__IPHONE_9_0) && defined(__IPHONE_OS_VERSION_MAX_ALLOWED) \ |
| 127 && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_9_0 |
| 128 bool GetLowPowerModeEnabled() { |
| 129 if (isOperatingSystemAtLeastVersion(9.0)) { |
| 130 // lowPoweredModeEnabled is only available on iOS9+. |
| 131 return [NSProcessInfo processInfo].lowPowerModeEnabled; |
| 132 } |
| 133 LOG(LS_WARNING) << "webrtc::ios::GetLowPowerModeEnabled() is not " |
| 134 "supported. Requires at least iOS 9.0"; |
| 135 return false; |
| 136 } |
| 137 #endif |
| 138 |
| 139 } // namespace ios |
| 140 } // namespace webrtc |
| 141 |
OLD | NEW |