hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 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 | |
tkchin | 9eeb624 | 2016-04-27 01:54:20 -0700 | [diff] [blame] | 11 | #import "RTCPeerConnection+Private.h" |
| 12 | |
hbos | bd3dda6 | 2016-09-09 01:36:28 -0700 | [diff] [blame] | 13 | #import "RTCLegacyStatsReport+Private.h" |
Anders Carlsson | 7bca8ca | 2018-08-30 09:30:29 +0200 | [diff] [blame] | 14 | #import "RTCMediaStreamTrack+Private.h" |
Peter Hanspers | e12a1c7 | 2019-02-26 16:39:48 +0100 | [diff] [blame] | 15 | #import "RTCRtpReceiver+Private.h" |
| 16 | #import "RTCRtpSender+Private.h" |
Peter Hanspers | bed8604 | 2019-02-21 17:27:09 +0100 | [diff] [blame] | 17 | #import "RTCStatisticsReport+Private.h" |
Anders Carlsson | 7bca8ca | 2018-08-30 09:30:29 +0200 | [diff] [blame] | 18 | #import "helpers/NSString+StdString.h" |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 19 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 20 | #include "rtc_base/checks.h" |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 21 | |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 22 | namespace webrtc { |
| 23 | |
Peter Hanspers | bed8604 | 2019-02-21 17:27:09 +0100 | [diff] [blame] | 24 | class StatsCollectorCallbackAdapter : public RTCStatsCollectorCallback { |
| 25 | public: |
| 26 | StatsCollectorCallbackAdapter(RTCStatisticsCompletionHandler completion_handler) |
| 27 | : completion_handler_(completion_handler) {} |
| 28 | |
| 29 | void OnStatsDelivered(const rtc::scoped_refptr<const RTCStatsReport> &report) override { |
| 30 | RTC_DCHECK(completion_handler_); |
Danilo Bargen | 87a6e5a | 2020-05-14 12:41:53 +0000 | [diff] [blame] | 31 | RTC_OBJC_TYPE(RTCStatisticsReport) *statisticsReport = |
| 32 | [[RTC_OBJC_TYPE(RTCStatisticsReport) alloc] initWithReport:*report]; |
Peter Hanspers | bed8604 | 2019-02-21 17:27:09 +0100 | [diff] [blame] | 33 | completion_handler_(statisticsReport); |
| 34 | completion_handler_ = nil; |
| 35 | } |
| 36 | |
| 37 | private: |
| 38 | RTCStatisticsCompletionHandler completion_handler_; |
| 39 | }; |
| 40 | |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 41 | class StatsObserverAdapter : public StatsObserver { |
| 42 | public: |
Mirko Bonadei | a81e9c8 | 2020-05-04 16:14:32 +0200 | [diff] [blame] | 43 | StatsObserverAdapter( |
| 44 | void (^completionHandler)(NSArray<RTC_OBJC_TYPE(RTCLegacyStatsReport) *> *stats)) { |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 45 | completion_handler_ = completionHandler; |
| 46 | } |
| 47 | |
Mirko Bonadei | 17aff35 | 2018-07-26 12:20:40 +0200 | [diff] [blame] | 48 | ~StatsObserverAdapter() override { completion_handler_ = nil; } |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 49 | |
nisse | e8abe3e | 2017-01-18 05:00:34 -0800 | [diff] [blame] | 50 | void OnComplete(const StatsReports& reports) override { |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 51 | RTC_DCHECK(completion_handler_); |
nisse | e8abe3e | 2017-01-18 05:00:34 -0800 | [diff] [blame] | 52 | NSMutableArray *stats = [NSMutableArray arrayWithCapacity:reports.size()]; |
| 53 | for (const auto* report : reports) { |
Mirko Bonadei | a81e9c8 | 2020-05-04 16:14:32 +0200 | [diff] [blame] | 54 | RTC_OBJC_TYPE(RTCLegacyStatsReport) *statsReport = |
| 55 | [[RTC_OBJC_TYPE(RTCLegacyStatsReport) alloc] initWithNativeReport:*report]; |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 56 | [stats addObject:statsReport]; |
| 57 | } |
| 58 | completion_handler_(stats); |
| 59 | completion_handler_ = nil; |
| 60 | } |
| 61 | |
| 62 | private: |
Mirko Bonadei | a81e9c8 | 2020-05-04 16:14:32 +0200 | [diff] [blame] | 63 | void (^completion_handler_)(NSArray<RTC_OBJC_TYPE(RTCLegacyStatsReport) *> *stats); |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 64 | }; |
| 65 | } // namespace webrtc |
| 66 | |
Mirko Bonadei | a81e9c8 | 2020-05-04 16:14:32 +0200 | [diff] [blame] | 67 | @implementation RTC_OBJC_TYPE (RTCPeerConnection) |
| 68 | (Stats) |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 69 | |
Mirko Bonadei | a81e9c8 | 2020-05-04 16:14:32 +0200 | [diff] [blame] | 70 | - (void)statisticsForSender : (RTC_OBJC_TYPE(RTCRtpSender) *)sender completionHandler |
| 71 | : (RTCStatisticsCompletionHandler)completionHandler { |
Peter Hanspers | e12a1c7 | 2019-02-26 16:39:48 +0100 | [diff] [blame] | 72 | rtc::scoped_refptr<webrtc::StatsCollectorCallbackAdapter> collector( |
| 73 | new rtc::RefCountedObject<webrtc::StatsCollectorCallbackAdapter>(completionHandler)); |
| 74 | self.nativePeerConnection->GetStats(sender.nativeRtpSender, collector); |
| 75 | } |
| 76 | |
Mirko Bonadei | a81e9c8 | 2020-05-04 16:14:32 +0200 | [diff] [blame] | 77 | - (void)statisticsForReceiver:(RTC_OBJC_TYPE(RTCRtpReceiver) *)receiver |
Peter Hanspers | e12a1c7 | 2019-02-26 16:39:48 +0100 | [diff] [blame] | 78 | completionHandler:(RTCStatisticsCompletionHandler)completionHandler { |
| 79 | rtc::scoped_refptr<webrtc::StatsCollectorCallbackAdapter> collector( |
| 80 | new rtc::RefCountedObject<webrtc::StatsCollectorCallbackAdapter>(completionHandler)); |
| 81 | self.nativePeerConnection->GetStats(receiver.nativeRtpReceiver, collector); |
| 82 | } |
| 83 | |
Peter Hanspers | bed8604 | 2019-02-21 17:27:09 +0100 | [diff] [blame] | 84 | - (void)statisticsWithCompletionHandler:(RTCStatisticsCompletionHandler)completionHandler { |
| 85 | rtc::scoped_refptr<webrtc::StatsCollectorCallbackAdapter> collector( |
| 86 | new rtc::RefCountedObject<webrtc::StatsCollectorCallbackAdapter>(completionHandler)); |
| 87 | self.nativePeerConnection->GetStats(collector); |
| 88 | } |
| 89 | |
Mirko Bonadei | a81e9c8 | 2020-05-04 16:14:32 +0200 | [diff] [blame] | 90 | - (void)statsForTrack:(RTC_OBJC_TYPE(RTCMediaStreamTrack) *)mediaStreamTrack |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 91 | statsOutputLevel:(RTCStatsOutputLevel)statsOutputLevel |
| 92 | completionHandler: |
Mirko Bonadei | a81e9c8 | 2020-05-04 16:14:32 +0200 | [diff] [blame] | 93 | (void (^)(NSArray<RTC_OBJC_TYPE(RTCLegacyStatsReport) *> *stats))completionHandler { |
hjon | f396f60 | 2016-02-11 16:19:06 -0800 | [diff] [blame] | 94 | rtc::scoped_refptr<webrtc::StatsObserverAdapter> observer( |
| 95 | new rtc::RefCountedObject<webrtc::StatsObserverAdapter> |
| 96 | (completionHandler)); |
| 97 | webrtc::PeerConnectionInterface::StatsOutputLevel nativeOutputLevel = |
| 98 | [[self class] nativeStatsOutputLevelForLevel:statsOutputLevel]; |
| 99 | self.nativePeerConnection->GetStats( |
| 100 | observer, mediaStreamTrack.nativeTrack, nativeOutputLevel); |
| 101 | } |
| 102 | |
| 103 | @end |