webrtc_m130/sdk/objc/api/peerconnection/RTCStatisticsReport.mm

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

199 lines
7.5 KiB
Plaintext
Raw Normal View History

/*
* 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"
Revert "Add a prefix for objc category." This reverts commit 181ea6e414c5982015ce161e6368120be3658ec4. Reason for revert: Breaks downstream project. Kári will help to land it next week. Original change's description: > Add a prefix for objc category. > > According to the Google Objective-C style [1], category names should > start with an appropriate prefix. WebRTC has some category definitions > for system interfaces, but it doesn't use prefixes. > > $ otool -ov WebRTC.framework/WebRTC | grep -E "^[0-9a-z]{16} 0x[0-9a-z]+ __OBJC_._CATEGORY" | grep -v "_RTC" > 0000000002160840 0x217c3c0 __OBJC_$_CATEGORY_UIDevice_$_H264Profile > 0000000002160850 0x21808b8 __OBJC_$_CATEGORY_AVCaptureSession_$_DevicePosition > 0000000002160858 0x2180968 __OBJC_$_CATEGORY_NSString_$_StdString > 0000000002160860 0x21809c8 __OBJC_$_CATEGORY_NSString_$_AbslStringView > > To avoid conflicts, prefix the names and methods of those categories. > Also remove sdk/objc/Framework/Classes/Common/NSString+StdString.h as > it is not used by any other files. > > [1] https://google.github.io/styleguide/objcguide.html#category-naming > > Bug: webrtc:13884 > Change-Id: I2cf2742af198ab4e0bfb15c0476d72971e50ceee > Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/262341 > Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org> > Reviewed-by: Kári Helgason <kthelgason@webrtc.org> > Commit-Queue: Daniel.L (Byoungchan) Lee <daniel.l@hpcnt.com> > Reviewed-by: Artem Titov <titovartem@webrtc.org> > Cr-Commit-Position: refs/heads/main@{#36880} Bug: webrtc:13884 Change-Id: I85257088e4a3a62e01ff925ab5e77af83b078ef3 No-Presubmit: true No-Tree-Checks: true No-Try: true Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/262420 Bot-Commit: rubber-stamper@appspot.gserviceaccount.com <rubber-stamper@appspot.gserviceaccount.com> Commit-Queue: Artem Titov <titovartem@webrtc.org> Auto-Submit: Artem Titov <titovartem@webrtc.org> Owners-Override: Artem Titov <titovartem@webrtc.org> Cr-Commit-Position: refs/heads/main@{#36885}
2022-05-13 14:46:42 +00:00
#include "helpers/NSString+StdString.h"
#include "rtc_base/checks.h"
namespace webrtc {
/** Converts a single value to a suitable NSNumber, NSString or NSArray
containing NSNumbers or NSStrings, or NSDictionary of NSString keys to
NSNumber values.*/
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]];
}
return [array copy];
} else if (attribute.holds_alternative<std::vector<int32_t>>()) {
std::vector<int32_t> sequence = attribute.get<std::vector<int32_t>>();
NSMutableArray<NSNumber *> *array =
[NSMutableArray arrayWithCapacity:sequence.size()];
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>>();
NSMutableArray<NSNumber *> *array =
[NSMutableArray arrayWithCapacity:sequence.size()];
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>>();
NSMutableArray<NSNumber *> *array =
[NSMutableArray arrayWithCapacity:sequence.size()];
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>>();
NSMutableArray<NSNumber *> *array =
[NSMutableArray arrayWithCapacity:sequence.size()];
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>>();
NSMutableArray<NSNumber *> *array =
[NSMutableArray arrayWithCapacity:sequence.size()];
for (const auto &item : sequence) {
[array addObject:[NSNumber numberWithDouble:item]];
}
return [array copy];
} else if (attribute.holds_alternative<std::vector<std::string>>()) {
std::vector<std::string> sequence =
attribute.get<std::vector<std::string>>();
NSMutableArray<NSString *> *array =
[NSMutableArray arrayWithCapacity:sequence.size()];
for (const auto &item : sequence) {
[array addObject:[NSString stringForStdString:item]];
}
return [array copy];
} else if (attribute.holds_alternative<std::map<std::string, uint64_t>>()) {
std::map<std::string, uint64_t> map =
attribute.get<std::map<std::string, uint64_t>>();
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>>()) {
std::map<std::string, double> map =
attribute.get<std::map<std::string, double>>();
NSMutableDictionary<NSString *, NSNumber *> *dictionary =
[NSMutableDictionary dictionaryWithCapacity:map.size()];
for (const auto &item : map) {
dictionary[[NSString stringForStdString:item.first]] = @(item.second);
}
return [dictionary copy];
}
RTC_DCHECK_NOTREACHED();
return nil;
}
} // namespace webrtc
@implementation RTC_OBJC_TYPE (RTCStatistics)
@synthesize id = _id;
@synthesize timestamp_us = _timestamp_us;
@synthesize type = _type;
@synthesize values = _values;
- (instancetype)initWithStatistics:(const webrtc::RTCStats &)statistics {
self = [super init];
if (self) {
Revert "Add a prefix for objc category." This reverts commit 181ea6e414c5982015ce161e6368120be3658ec4. Reason for revert: Breaks downstream project. Kári will help to land it next week. Original change's description: > Add a prefix for objc category. > > According to the Google Objective-C style [1], category names should > start with an appropriate prefix. WebRTC has some category definitions > for system interfaces, but it doesn't use prefixes. > > $ otool -ov WebRTC.framework/WebRTC | grep -E "^[0-9a-z]{16} 0x[0-9a-z]+ __OBJC_._CATEGORY" | grep -v "_RTC" > 0000000002160840 0x217c3c0 __OBJC_$_CATEGORY_UIDevice_$_H264Profile > 0000000002160850 0x21808b8 __OBJC_$_CATEGORY_AVCaptureSession_$_DevicePosition > 0000000002160858 0x2180968 __OBJC_$_CATEGORY_NSString_$_StdString > 0000000002160860 0x21809c8 __OBJC_$_CATEGORY_NSString_$_AbslStringView > > To avoid conflicts, prefix the names and methods of those categories. > Also remove sdk/objc/Framework/Classes/Common/NSString+StdString.h as > it is not used by any other files. > > [1] https://google.github.io/styleguide/objcguide.html#category-naming > > Bug: webrtc:13884 > Change-Id: I2cf2742af198ab4e0bfb15c0476d72971e50ceee > Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/262341 > Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org> > Reviewed-by: Kári Helgason <kthelgason@webrtc.org> > Commit-Queue: Daniel.L (Byoungchan) Lee <daniel.l@hpcnt.com> > Reviewed-by: Artem Titov <titovartem@webrtc.org> > Cr-Commit-Position: refs/heads/main@{#36880} Bug: webrtc:13884 Change-Id: I85257088e4a3a62e01ff925ab5e77af83b078ef3 No-Presubmit: true No-Tree-Checks: true No-Try: true Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/262420 Bot-Commit: rubber-stamper@appspot.gserviceaccount.com <rubber-stamper@appspot.gserviceaccount.com> Commit-Queue: Artem Titov <titovartem@webrtc.org> Auto-Submit: Artem Titov <titovartem@webrtc.org> Owners-Override: Artem Titov <titovartem@webrtc.org> Cr-Commit-Position: refs/heads/main@{#36885}
2022-05-13 14:46:42 +00:00
_id = [NSString stringForStdString:statistics.id()];
_timestamp_us = statistics.timestamp().us();
_type = [NSString stringWithCString:statistics.type()
encoding:NSUTF8StringEncoding];
const std::vector<webrtc::Attribute> attributes = statistics.Attributes();
NSMutableDictionary<NSString *, NSObject *> *values =
[NSMutableDictionary dictionaryWithCapacity:attributes.size()];
for (const auto &attribute : attributes) {
NSObject *value = ValueFromStatsAttribute(attribute);
if (value) {
NSString *name = [NSString stringWithCString:attribute.name()
encoding:NSUTF8StringEncoding];
RTC_DCHECK(name.length > 0);
RTC_DCHECK(!values[name]);
values[name] = value;
}
}
_values = [values copy];
}
return self;
}
- (NSString *)description {
return [NSString
stringWithFormat:@"id = %@, type = %@, timestamp = %.0f, values = %@",
self.id,
self.type,
self.timestamp_us,
self.values];
}
@end
@implementation RTC_OBJC_TYPE (RTCStatisticsReport)
@synthesize timestamp_us = _timestamp_us;
@synthesize statistics = _statistics;
- (NSString *)description {
return [NSString stringWithFormat:@"timestamp = %.0f, statistics = %@",
self.timestamp_us,
self.statistics];
}
@end
@implementation RTC_OBJC_TYPE (RTCStatisticsReport)
(Private)
- (instancetype)initWithReport : (const webrtc::RTCStatsReport &)report {
self = [super init];
if (self) {
_timestamp_us = report.timestamp().us();
NSMutableDictionary *statisticsById =
[NSMutableDictionary dictionaryWithCapacity:report.size()];
for (const auto &stat : report) {
RTC_OBJC_TYPE(RTCStatistics) *statistics =
[[RTC_OBJC_TYPE(RTCStatistics) alloc] initWithStatistics:stat];
statisticsById[statistics.id] = statistics;
}
_statistics = [statisticsById copy];
}
return self;
}
@end