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 |
| 19 | or NSStrings.*/ |
| 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()]; |
| 40 | for (const auto &item : sequence) { |
| 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 | } |
| 94 | default: |
| 95 | RTC_NOTREACHED(); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | return nil; |
| 100 | } |
| 101 | } // namespace webrtc |
| 102 | |
| 103 | @implementation RTCStatistics |
| 104 | |
| 105 | @synthesize id = _id; |
| 106 | @synthesize timestamp_us = _timestamp_us; |
| 107 | @synthesize type = _type; |
| 108 | @synthesize values = _values; |
| 109 | |
| 110 | - (instancetype)initWithStatistics:(const webrtc::RTCStats &)statistics { |
| 111 | if (self = [super init]) { |
| 112 | _id = [NSString stringForStdString:statistics.id()]; |
| 113 | _timestamp_us = statistics.timestamp_us(); |
| 114 | _type = [NSString stringWithCString:statistics.type() encoding:NSUTF8StringEncoding]; |
| 115 | |
| 116 | NSMutableDictionary<NSString *, NSObject *> *values = [NSMutableDictionary dictionary]; |
| 117 | for (const webrtc::RTCStatsMemberInterface *member : statistics.Members()) { |
| 118 | NSObject *value = ValueFromStatsMember(member); |
| 119 | if (value) { |
| 120 | NSString *name = [NSString stringWithCString:member->name() encoding:NSUTF8StringEncoding]; |
| 121 | RTC_DCHECK(name.length > 0); |
| 122 | RTC_DCHECK(!values[name]); |
| 123 | values[name] = value; |
| 124 | } |
| 125 | } |
| 126 | _values = [values copy]; |
| 127 | } |
| 128 | |
| 129 | return self; |
| 130 | } |
| 131 | |
| 132 | - (NSString *)description { |
| 133 | return [NSString stringWithFormat:@"id = %@, type = %@, timestamp = %.0f, values = %@", |
| 134 | self.id, |
| 135 | self.type, |
| 136 | self.timestamp_us, |
| 137 | self.values]; |
| 138 | } |
| 139 | |
| 140 | @end |
| 141 | |
| 142 | @implementation RTCStatisticsReport |
| 143 | |
| 144 | @synthesize timestamp_us = _timestamp_us; |
| 145 | @synthesize statistics = _statistics; |
| 146 | |
| 147 | - (NSString *)description { |
| 148 | return [NSString |
| 149 | stringWithFormat:@"timestamp = %.0f, statistics = %@", self.timestamp_us, self.statistics]; |
| 150 | } |
| 151 | |
| 152 | @end |
| 153 | |
| 154 | @implementation RTCStatisticsReport (Private) |
| 155 | |
| 156 | - (instancetype)initWithReport:(const webrtc::RTCStatsReport &)report { |
| 157 | if (self = [super init]) { |
| 158 | _timestamp_us = report.timestamp_us(); |
| 159 | |
| 160 | NSMutableDictionary *statisticsById = |
| 161 | [NSMutableDictionary dictionaryWithCapacity:report.size()]; |
| 162 | for (const auto &stat : report) { |
| 163 | RTCStatistics *statistics = [[RTCStatistics alloc] initWithStatistics:stat]; |
| 164 | statisticsById[statistics.id] = statistics; |
| 165 | } |
| 166 | _statistics = [statisticsById copy]; |
| 167 | } |
| 168 | |
| 169 | return self; |
| 170 | } |
| 171 | |
| 172 | @end |