2019-02-21 17:27:09 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright 2019 The WebRTC project authors. All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#import "RTCStatisticsReport+Private.h"
|
|
|
|
|
|
2022-05-13 14:46:42 +00:00
|
|
|
#include "helpers/NSString+StdString.h"
|
2019-02-21 17:27:09 +01:00
|
|
|
#include "rtc_base/checks.h"
|
|
|
|
|
|
|
|
|
|
namespace webrtc {
|
|
|
|
|
|
2025-01-08 05:45:56 -08:00
|
|
|
/** Converts a single value to a suitable NSNumber, NSString or NSArray
|
|
|
|
|
containing NSNumbers or NSStrings, or NSDictionary of NSString keys to
|
|
|
|
|
NSNumber values.*/
|
2024-01-12 09:25:38 +01:00
|
|
|
NSObject *ValueFromStatsAttribute(const Attribute &attribute) {
|
|
|
|
|
if (!attribute.has_value()) {
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
if (attribute.holds_alternative<bool>()) {
|
|
|
|
|
return [NSNumber numberWithBool:attribute.get<bool>()];
|
|
|
|
|
} else if (attribute.holds_alternative<int32_t>()) {
|
|
|
|
|
return [NSNumber numberWithInt:attribute.get<int32_t>()];
|
|
|
|
|
} else if (attribute.holds_alternative<uint32_t>()) {
|
|
|
|
|
return [NSNumber numberWithUnsignedInt:attribute.get<uint32_t>()];
|
|
|
|
|
} else if (attribute.holds_alternative<int64_t>()) {
|
|
|
|
|
return [NSNumber numberWithLong:attribute.get<int64_t>()];
|
|
|
|
|
} else if (attribute.holds_alternative<uint64_t>()) {
|
|
|
|
|
return [NSNumber numberWithUnsignedLong:attribute.get<uint64_t>()];
|
|
|
|
|
} else if (attribute.holds_alternative<double>()) {
|
|
|
|
|
return [NSNumber numberWithDouble:attribute.get<double>()];
|
|
|
|
|
} else if (attribute.holds_alternative<std::string>()) {
|
|
|
|
|
return [NSString stringForStdString:attribute.get<std::string>()];
|
|
|
|
|
} else if (attribute.holds_alternative<std::vector<bool>>()) {
|
|
|
|
|
std::vector<bool> sequence = attribute.get<std::vector<bool>>();
|
|
|
|
|
NSMutableArray *array = [NSMutableArray arrayWithCapacity:sequence.size()];
|
|
|
|
|
for (auto item : sequence) {
|
|
|
|
|
[array addObject:[NSNumber numberWithBool:item]];
|
2019-02-21 17:27:09 +01:00
|
|
|
}
|
2024-01-12 09:25:38 +01:00
|
|
|
return [array copy];
|
|
|
|
|
} else if (attribute.holds_alternative<std::vector<int32_t>>()) {
|
|
|
|
|
std::vector<int32_t> sequence = attribute.get<std::vector<int32_t>>();
|
2025-01-08 05:45:56 -08:00
|
|
|
NSMutableArray<NSNumber *> *array =
|
|
|
|
|
[NSMutableArray arrayWithCapacity:sequence.size()];
|
2024-01-12 09:25:38 +01:00
|
|
|
for (const auto &item : sequence) {
|
|
|
|
|
[array addObject:[NSNumber numberWithInt:item]];
|
|
|
|
|
}
|
|
|
|
|
return [array copy];
|
|
|
|
|
} else if (attribute.holds_alternative<std::vector<uint32_t>>()) {
|
|
|
|
|
std::vector<uint32_t> sequence = attribute.get<std::vector<uint32_t>>();
|
2025-01-08 05:45:56 -08:00
|
|
|
NSMutableArray<NSNumber *> *array =
|
|
|
|
|
[NSMutableArray arrayWithCapacity:sequence.size()];
|
2024-01-12 09:25:38 +01:00
|
|
|
for (const auto &item : sequence) {
|
|
|
|
|
[array addObject:[NSNumber numberWithUnsignedInt:item]];
|
|
|
|
|
}
|
|
|
|
|
return [array copy];
|
|
|
|
|
} else if (attribute.holds_alternative<std::vector<int64_t>>()) {
|
|
|
|
|
std::vector<int64_t> sequence = attribute.get<std::vector<int64_t>>();
|
2025-01-08 05:45:56 -08:00
|
|
|
NSMutableArray<NSNumber *> *array =
|
|
|
|
|
[NSMutableArray arrayWithCapacity:sequence.size()];
|
2024-01-12 09:25:38 +01:00
|
|
|
for (const auto &item : sequence) {
|
|
|
|
|
[array addObject:[NSNumber numberWithLong:item]];
|
|
|
|
|
}
|
|
|
|
|
return [array copy];
|
|
|
|
|
} else if (attribute.holds_alternative<std::vector<uint64_t>>()) {
|
|
|
|
|
std::vector<uint64_t> sequence = attribute.get<std::vector<uint64_t>>();
|
2025-01-08 05:45:56 -08:00
|
|
|
NSMutableArray<NSNumber *> *array =
|
|
|
|
|
[NSMutableArray arrayWithCapacity:sequence.size()];
|
2024-01-12 09:25:38 +01:00
|
|
|
for (const auto &item : sequence) {
|
|
|
|
|
[array addObject:[NSNumber numberWithUnsignedLong:item]];
|
|
|
|
|
}
|
|
|
|
|
return [array copy];
|
|
|
|
|
} else if (attribute.holds_alternative<std::vector<double>>()) {
|
|
|
|
|
std::vector<double> sequence = attribute.get<std::vector<double>>();
|
2025-01-08 05:45:56 -08:00
|
|
|
NSMutableArray<NSNumber *> *array =
|
|
|
|
|
[NSMutableArray arrayWithCapacity:sequence.size()];
|
2024-01-12 09:25:38 +01:00
|
|
|
for (const auto &item : sequence) {
|
|
|
|
|
[array addObject:[NSNumber numberWithDouble:item]];
|
|
|
|
|
}
|
|
|
|
|
return [array copy];
|
|
|
|
|
} else if (attribute.holds_alternative<std::vector<std::string>>()) {
|
2025-01-08 05:45:56 -08:00
|
|
|
std::vector<std::string> sequence =
|
|
|
|
|
attribute.get<std::vector<std::string>>();
|
|
|
|
|
NSMutableArray<NSString *> *array =
|
|
|
|
|
[NSMutableArray arrayWithCapacity:sequence.size()];
|
2024-01-12 09:25:38 +01:00
|
|
|
for (const auto &item : sequence) {
|
|
|
|
|
[array addObject:[NSString stringForStdString:item]];
|
|
|
|
|
}
|
|
|
|
|
return [array copy];
|
|
|
|
|
} else if (attribute.holds_alternative<std::map<std::string, uint64_t>>()) {
|
2025-01-08 05:45:56 -08:00
|
|
|
std::map<std::string, uint64_t> map =
|
|
|
|
|
attribute.get<std::map<std::string, uint64_t>>();
|
2024-01-12 09:25:38 +01:00
|
|
|
NSMutableDictionary<NSString *, NSNumber *> *dictionary =
|
|
|
|
|
[NSMutableDictionary dictionaryWithCapacity:map.size()];
|
|
|
|
|
for (const auto &item : map) {
|
|
|
|
|
dictionary[[NSString stringForStdString:item.first]] = @(item.second);
|
|
|
|
|
}
|
|
|
|
|
return [dictionary copy];
|
|
|
|
|
} else if (attribute.holds_alternative<std::map<std::string, double>>()) {
|
2025-01-08 05:45:56 -08:00
|
|
|
std::map<std::string, double> map =
|
|
|
|
|
attribute.get<std::map<std::string, double>>();
|
2024-01-12 09:25:38 +01:00
|
|
|
NSMutableDictionary<NSString *, NSNumber *> *dictionary =
|
|
|
|
|
[NSMutableDictionary dictionaryWithCapacity:map.size()];
|
|
|
|
|
for (const auto &item : map) {
|
|
|
|
|
dictionary[[NSString stringForStdString:item.first]] = @(item.second);
|
|
|
|
|
}
|
|
|
|
|
return [dictionary copy];
|
2019-02-21 17:27:09 +01:00
|
|
|
}
|
2024-01-12 09:25:38 +01:00
|
|
|
RTC_DCHECK_NOTREACHED();
|
2019-02-21 17:27:09 +01:00
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
} // namespace webrtc
|
|
|
|
|
|
2020-05-14 12:41:53 +00:00
|
|
|
@implementation RTC_OBJC_TYPE (RTCStatistics)
|
2019-02-21 17:27:09 +01:00
|
|
|
|
|
|
|
|
@synthesize id = _id;
|
|
|
|
|
@synthesize timestamp_us = _timestamp_us;
|
|
|
|
|
@synthesize type = _type;
|
|
|
|
|
@synthesize values = _values;
|
|
|
|
|
|
|
|
|
|
- (instancetype)initWithStatistics:(const webrtc::RTCStats &)statistics {
|
2024-08-23 13:51:10 -04:00
|
|
|
self = [super init];
|
|
|
|
|
if (self) {
|
2022-05-13 14:46:42 +00:00
|
|
|
_id = [NSString stringForStdString:statistics.id()];
|
2023-01-04 15:17:42 +01:00
|
|
|
_timestamp_us = statistics.timestamp().us();
|
2025-01-08 05:45:56 -08:00
|
|
|
_type = [NSString stringWithCString:statistics.type()
|
|
|
|
|
encoding:NSUTF8StringEncoding];
|
2019-02-21 17:27:09 +01:00
|
|
|
|
2025-02-03 11:26:21 +01:00
|
|
|
const std::vector<webrtc::Attribute> attributes = statistics.Attributes();
|
2025-01-08 05:45:56 -08:00
|
|
|
NSMutableDictionary<NSString *, NSObject *> *values =
|
2025-02-03 11:26:21 +01:00
|
|
|
[NSMutableDictionary dictionaryWithCapacity:attributes.size()];
|
|
|
|
|
for (const auto &attribute : attributes) {
|
2024-01-12 09:25:38 +01:00
|
|
|
NSObject *value = ValueFromStatsAttribute(attribute);
|
2019-02-21 17:27:09 +01:00
|
|
|
if (value) {
|
2024-01-12 09:25:38 +01:00
|
|
|
NSString *name = [NSString stringWithCString:attribute.name()
|
|
|
|
|
encoding:NSUTF8StringEncoding];
|
2019-02-21 17:27:09 +01:00
|
|
|
RTC_DCHECK(name.length > 0);
|
|
|
|
|
RTC_DCHECK(!values[name]);
|
|
|
|
|
values[name] = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
_values = [values copy];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSString *)description {
|
2025-01-08 05:45:56 -08:00
|
|
|
return [NSString
|
|
|
|
|
stringWithFormat:@"id = %@, type = %@, timestamp = %.0f, values = %@",
|
|
|
|
|
self.id,
|
|
|
|
|
self.type,
|
|
|
|
|
self.timestamp_us,
|
|
|
|
|
self.values];
|
2019-02-21 17:27:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
2020-05-14 12:41:53 +00:00
|
|
|
@implementation RTC_OBJC_TYPE (RTCStatisticsReport)
|
2019-02-21 17:27:09 +01:00
|
|
|
|
|
|
|
|
@synthesize timestamp_us = _timestamp_us;
|
|
|
|
|
@synthesize statistics = _statistics;
|
|
|
|
|
|
|
|
|
|
- (NSString *)description {
|
2025-01-08 05:45:56 -08:00
|
|
|
return [NSString stringWithFormat:@"timestamp = %.0f, statistics = %@",
|
|
|
|
|
self.timestamp_us,
|
|
|
|
|
self.statistics];
|
2019-02-21 17:27:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
2025-01-08 05:45:56 -08:00
|
|
|
@implementation RTC_OBJC_TYPE (RTCStatisticsReport)
|
|
|
|
|
(Private)
|
2019-02-21 17:27:09 +01:00
|
|
|
|
2025-01-08 05:45:56 -08:00
|
|
|
- (instancetype)initWithReport : (const webrtc::RTCStatsReport &)report {
|
2024-08-23 13:51:10 -04:00
|
|
|
self = [super init];
|
|
|
|
|
if (self) {
|
2023-01-04 15:17:42 +01:00
|
|
|
_timestamp_us = report.timestamp().us();
|
2019-02-21 17:27:09 +01:00
|
|
|
|
|
|
|
|
NSMutableDictionary *statisticsById =
|
|
|
|
|
[NSMutableDictionary dictionaryWithCapacity:report.size()];
|
|
|
|
|
for (const auto &stat : report) {
|
2020-05-14 12:41:53 +00:00
|
|
|
RTC_OBJC_TYPE(RTCStatistics) *statistics =
|
|
|
|
|
[[RTC_OBJC_TYPE(RTCStatistics) alloc] initWithStatistics:stat];
|
2019-02-21 17:27:09 +01:00
|
|
|
statisticsById[statistics.id] = statistics;
|
|
|
|
|
}
|
|
|
|
|
_statistics = [statisticsById copy];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|