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