Peter Hanspers | bed8604 | 2019-02-21 17:27:09 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 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 | #import "RTCStatisticsReport+Private.h" |
| 12 | |
| 13 | #include "helpers/NSString+StdString.h" |
| 14 | #include "rtc_base/checks.h" |
| 15 | |
| 16 | namespace webrtc { |
| 17 | |
| 18 | /** Converts a single value to a suitable NSNumber, NSString or NSArray containing NSNumbers |
Byoungchan Lee | 0a52ede | 2021-05-22 08:41:02 +0900 | [diff] [blame] | 19 | or NSStrings, or NSDictionary of NSString keys to NSNumber values.*/ |
Peter Hanspers | bed8604 | 2019-02-21 17:27:09 +0100 | [diff] [blame] | 20 | NSObject *ValueFromStatsMember(const RTCStatsMemberInterface *member) { |
| 21 | if (member->is_defined()) { |
| 22 | switch (member->type()) { |
| 23 | case RTCStatsMemberInterface::kBool: |
| 24 | return [NSNumber numberWithBool:*member->cast_to<RTCStatsMember<bool>>()]; |
| 25 | case RTCStatsMemberInterface::kInt32: |
| 26 | return [NSNumber numberWithInt:*member->cast_to<RTCStatsMember<int32_t>>()]; |
| 27 | case RTCStatsMemberInterface::kUint32: |
| 28 | return [NSNumber numberWithUnsignedInt:*member->cast_to<RTCStatsMember<uint32_t>>()]; |
| 29 | case RTCStatsMemberInterface::kInt64: |
| 30 | return [NSNumber numberWithLong:*member->cast_to<RTCStatsMember<int64_t>>()]; |
| 31 | case RTCStatsMemberInterface::kUint64: |
| 32 | return [NSNumber numberWithUnsignedLong:*member->cast_to<RTCStatsMember<uint64_t>>()]; |
| 33 | case RTCStatsMemberInterface::kDouble: |
| 34 | return [NSNumber numberWithDouble:*member->cast_to<RTCStatsMember<double>>()]; |
| 35 | case RTCStatsMemberInterface::kString: |
| 36 | return [NSString stringForStdString:*member->cast_to<RTCStatsMember<std::string>>()]; |
| 37 | case RTCStatsMemberInterface::kSequenceBool: { |
| 38 | std::vector<bool> sequence = *member->cast_to<RTCStatsMember<std::vector<bool>>>(); |
| 39 | NSMutableArray *array = [NSMutableArray arrayWithCapacity:sequence.size()]; |
Mirko Bonadei | 85fae2a | 2020-11-05 16:08:13 +0100 | [diff] [blame] | 40 | for (auto item : sequence) { |
Peter Hanspers | bed8604 | 2019-02-21 17:27:09 +0100 | [diff] [blame] | 41 | [array addObject:[NSNumber numberWithBool:item]]; |
| 42 | } |
| 43 | return [array copy]; |
| 44 | } |
| 45 | case RTCStatsMemberInterface::kSequenceInt32: { |
| 46 | std::vector<int32_t> sequence = *member->cast_to<RTCStatsMember<std::vector<int32_t>>>(); |
| 47 | NSMutableArray<NSNumber *> *array = [NSMutableArray arrayWithCapacity:sequence.size()]; |
| 48 | for (const auto &item : sequence) { |
| 49 | [array addObject:[NSNumber numberWithInt:item]]; |
| 50 | } |
| 51 | return [array copy]; |
| 52 | } |
| 53 | case RTCStatsMemberInterface::kSequenceUint32: { |
| 54 | std::vector<uint32_t> sequence = *member->cast_to<RTCStatsMember<std::vector<uint32_t>>>(); |
| 55 | NSMutableArray<NSNumber *> *array = [NSMutableArray arrayWithCapacity:sequence.size()]; |
| 56 | for (const auto &item : sequence) { |
| 57 | [array addObject:[NSNumber numberWithUnsignedInt:item]]; |
| 58 | } |
| 59 | return [array copy]; |
| 60 | } |
| 61 | case RTCStatsMemberInterface::kSequenceInt64: { |
| 62 | std::vector<int64_t> sequence = *member->cast_to<RTCStatsMember<std::vector<int64_t>>>(); |
| 63 | NSMutableArray<NSNumber *> *array = [NSMutableArray arrayWithCapacity:sequence.size()]; |
| 64 | for (const auto &item : sequence) { |
| 65 | [array addObject:[NSNumber numberWithLong:item]]; |
| 66 | } |
| 67 | return [array copy]; |
| 68 | } |
| 69 | case RTCStatsMemberInterface::kSequenceUint64: { |
| 70 | std::vector<uint64_t> sequence = *member->cast_to<RTCStatsMember<std::vector<uint64_t>>>(); |
| 71 | NSMutableArray<NSNumber *> *array = [NSMutableArray arrayWithCapacity:sequence.size()]; |
| 72 | for (const auto &item : sequence) { |
| 73 | [array addObject:[NSNumber numberWithUnsignedLong:item]]; |
| 74 | } |
| 75 | return [array copy]; |
| 76 | } |
| 77 | case RTCStatsMemberInterface::kSequenceDouble: { |
| 78 | std::vector<double> sequence = *member->cast_to<RTCStatsMember<std::vector<double>>>(); |
| 79 | NSMutableArray<NSNumber *> *array = [NSMutableArray arrayWithCapacity:sequence.size()]; |
| 80 | for (const auto &item : sequence) { |
| 81 | [array addObject:[NSNumber numberWithDouble:item]]; |
| 82 | } |
| 83 | return [array copy]; |
| 84 | } |
| 85 | case RTCStatsMemberInterface::kSequenceString: { |
| 86 | std::vector<std::string> sequence = |
| 87 | *member->cast_to<RTCStatsMember<std::vector<std::string>>>(); |
| 88 | NSMutableArray<NSString *> *array = [NSMutableArray arrayWithCapacity:sequence.size()]; |
| 89 | for (const auto &item : sequence) { |
| 90 | [array addObject:[NSString stringForStdString:item]]; |
| 91 | } |
| 92 | return [array copy]; |
| 93 | } |
Byoungchan Lee | 0a52ede | 2021-05-22 08:41:02 +0900 | [diff] [blame] | 94 | case RTCStatsMemberInterface::kMapStringUint64: { |
| 95 | std::map<std::string, uint64_t> map = |
| 96 | *member->cast_to<RTCStatsMember<std::map<std::string, uint64_t>>>(); |
| 97 | NSMutableDictionary<NSString *, NSNumber *> *dictionary = |
| 98 | [NSMutableDictionary dictionaryWithCapacity:map.size()]; |
| 99 | for (const auto &item : map) { |
| 100 | dictionary[[NSString stringForStdString:item.first]] = @(item.second); |
| 101 | } |
| 102 | return [dictionary copy]; |
| 103 | } |
| 104 | case RTCStatsMemberInterface::kMapStringDouble: { |
| 105 | std::map<std::string, double> map = |
| 106 | *member->cast_to<RTCStatsMember<std::map<std::string, double>>>(); |
| 107 | NSMutableDictionary<NSString *, NSNumber *> *dictionary = |
| 108 | [NSMutableDictionary dictionaryWithCapacity:map.size()]; |
| 109 | for (const auto &item : map) { |
| 110 | dictionary[[NSString stringForStdString:item.first]] = @(item.second); |
| 111 | } |
| 112 | return [dictionary copy]; |
| 113 | } |
Peter Hanspers | bed8604 | 2019-02-21 17:27:09 +0100 | [diff] [blame] | 114 | default: |
| 115 | RTC_NOTREACHED(); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | return nil; |
| 120 | } |
| 121 | } // namespace webrtc |
| 122 | |
Danilo Bargen | 87a6e5a | 2020-05-14 12:41:53 +0000 | [diff] [blame] | 123 | @implementation RTC_OBJC_TYPE (RTCStatistics) |
Peter Hanspers | bed8604 | 2019-02-21 17:27:09 +0100 | [diff] [blame] | 124 | |
| 125 | @synthesize id = _id; |
| 126 | @synthesize timestamp_us = _timestamp_us; |
| 127 | @synthesize type = _type; |
| 128 | @synthesize values = _values; |
| 129 | |
| 130 | - (instancetype)initWithStatistics:(const webrtc::RTCStats &)statistics { |
| 131 | if (self = [super init]) { |
| 132 | _id = [NSString stringForStdString:statistics.id()]; |
| 133 | _timestamp_us = statistics.timestamp_us(); |
| 134 | _type = [NSString stringWithCString:statistics.type() encoding:NSUTF8StringEncoding]; |
| 135 | |
| 136 | NSMutableDictionary<NSString *, NSObject *> *values = [NSMutableDictionary dictionary]; |
| 137 | for (const webrtc::RTCStatsMemberInterface *member : statistics.Members()) { |
| 138 | NSObject *value = ValueFromStatsMember(member); |
| 139 | if (value) { |
| 140 | NSString *name = [NSString stringWithCString:member->name() encoding:NSUTF8StringEncoding]; |
| 141 | RTC_DCHECK(name.length > 0); |
| 142 | RTC_DCHECK(!values[name]); |
| 143 | values[name] = value; |
| 144 | } |
| 145 | } |
| 146 | _values = [values copy]; |
| 147 | } |
| 148 | |
| 149 | return self; |
| 150 | } |
| 151 | |
| 152 | - (NSString *)description { |
| 153 | return [NSString stringWithFormat:@"id = %@, type = %@, timestamp = %.0f, values = %@", |
| 154 | self.id, |
| 155 | self.type, |
| 156 | self.timestamp_us, |
| 157 | self.values]; |
| 158 | } |
| 159 | |
| 160 | @end |
| 161 | |
Danilo Bargen | 87a6e5a | 2020-05-14 12:41:53 +0000 | [diff] [blame] | 162 | @implementation RTC_OBJC_TYPE (RTCStatisticsReport) |
Peter Hanspers | bed8604 | 2019-02-21 17:27:09 +0100 | [diff] [blame] | 163 | |
| 164 | @synthesize timestamp_us = _timestamp_us; |
| 165 | @synthesize statistics = _statistics; |
| 166 | |
| 167 | - (NSString *)description { |
| 168 | return [NSString |
| 169 | stringWithFormat:@"timestamp = %.0f, statistics = %@", self.timestamp_us, self.statistics]; |
| 170 | } |
| 171 | |
| 172 | @end |
| 173 | |
Danilo Bargen | 87a6e5a | 2020-05-14 12:41:53 +0000 | [diff] [blame] | 174 | @implementation RTC_OBJC_TYPE (RTCStatisticsReport) (Private) |
Peter Hanspers | bed8604 | 2019-02-21 17:27:09 +0100 | [diff] [blame] | 175 | |
Danilo Bargen | 87a6e5a | 2020-05-14 12:41:53 +0000 | [diff] [blame] | 176 | - (instancetype)initWithReport : (const webrtc::RTCStatsReport &)report { |
Peter Hanspers | bed8604 | 2019-02-21 17:27:09 +0100 | [diff] [blame] | 177 | if (self = [super init]) { |
| 178 | _timestamp_us = report.timestamp_us(); |
| 179 | |
| 180 | NSMutableDictionary *statisticsById = |
| 181 | [NSMutableDictionary dictionaryWithCapacity:report.size()]; |
| 182 | for (const auto &stat : report) { |
Danilo Bargen | 87a6e5a | 2020-05-14 12:41:53 +0000 | [diff] [blame] | 183 | RTC_OBJC_TYPE(RTCStatistics) *statistics = |
| 184 | [[RTC_OBJC_TYPE(RTCStatistics) alloc] initWithStatistics:stat]; |
Peter Hanspers | bed8604 | 2019-02-21 17:27:09 +0100 | [diff] [blame] | 185 | statisticsById[statistics.id] = statistics; |
| 186 | } |
| 187 | _statistics = [statisticsById copy]; |
| 188 | } |
| 189 | |
| 190 | return self; |
| 191 | } |
| 192 | |
| 193 | @end |