66//
77
88#import " BeautyAPI.h"
9+ #import " APIReporter.h"
910
10- static NSString *const beautyAPIVnersio = @" 1.0.3 " ;
11+ static NSString *const beautyAPIVersion = @" 1.0.7 " ;
1112
1213@implementation BeautyStats
1314@end
@@ -24,6 +25,8 @@ @interface BeautyAPI ()
2425@property (nonatomic , assign ) CFTimeInterval preTime;
2526@property (nonatomic , strong ) NSMutableArray *statsArray;
2627@property (nonatomic , assign ) AgoraVideoRenderMode renderMode;
28+ @property (nonatomic , strong ) APIReporter *reporter;
29+ @property (nonatomic , assign ) BOOL isFirstFrame;
2730
2831@end
2932
@@ -34,13 +37,29 @@ @interface BeautyAPI ()<AgoraVideoFrameDelegate>
3437
3538@implementation BeautyAPI
3639
40+ - (instancetype )init {
41+ if (self == [super init ]) {
42+ _isFrontCamera = YES ;
43+ }
44+ return self;
45+ }
46+
3747- (NSMutableArray *)statsArray {
3848 if (_statsArray == nil ) {
3949 _statsArray = [NSMutableArray new ];
4050 }
4151 return _statsArray;
4252}
4353
54+ - (APIReporter *)reporter {
55+ if (_reporter == nil ) {
56+ _reporter = [[APIReporter alloc ] initWithType: (APITypeBeauty)
57+ version: beautyAPIVersion
58+ engine: self .config.rtcEngine];
59+ }
60+ return _reporter;
61+ }
62+
4463- (int )initialize : (BeautyConfig *)config {
4564 if (config.cameraConfig == nil ) {
4665 CameraConfig *cameraConfig = [[CameraConfig alloc ] init ];
@@ -50,7 +69,6 @@ - (int)initialize:(BeautyConfig *)config {
5069 }
5170 [LogUtil log: [NSString stringWithFormat: @" RTC Version == %@ " , [AgoraRtcEngineKit getSdkVersion ]]];
5271 [LogUtil log: [NSString stringWithFormat: @" BeautyAPI Version == %@ " , [self getVersion ]]];
53- _isFrontCamera = YES ;
5472 self.config = config;
5573 if (self.config .statsDuration <= 0 ) {
5674 self.config .statsDuration = 1 ;
@@ -64,6 +82,7 @@ - (int)initialize:(BeautyConfig *)config {
6482 return -1 ;
6583 }
6684 [LogUtil log: [NSString stringWithFormat: @" beautyRender == %@ " , config.beautyRender.description]];
85+ [self .reporter startDurationEventWithName: @" initialize-release" ];
6786 self.beautyRender = config.beautyRender ;
6887 if (config.captureMode == CaptureModeAgora) {
6988#if __has_include(<AgoraRtcKit/AgoraRtcKit.h>)
@@ -79,22 +98,25 @@ - (int)initialize:(BeautyConfig *)config {
7998 }
8099 };
81100 [self rtcReportWithEvent: @" initialize" label: dict];
101+ [self setupMirror ];
82102#else
83103 [LogUtil log: @" rtc 未导入" level: (LogLevelError)];
84104 return -1 ;
85105#endif
86106 } else {
87107 [LogUtil log: @" captureMode == Custom" ];
88108 }
109+ [self setupMirror ];
89110 return 0 ;
90111}
91112
92113- (int )switchCamera {
93114 _isFrontCamera = !_isFrontCamera;
94- [self setupMirror ];
95115 NSDictionary *dict = @{ @" cameraPosition" : @(_isFrontCamera) };
96116 [self rtcReportWithEvent: @" cameraPosition" label: dict];
97- return [self .config.rtcEngine switchCamera ];
117+ int res = [self .config.rtcEngine switchCamera ];
118+ [self setupMirror ];
119+ return res;
98120}
99121
100122- (AgoraVideoMirrorMode)setupMirror {
@@ -190,6 +212,7 @@ - (int)destroy {
190212 [self .config.beautyRender destroy ];
191213 self.config = nil ;
192214 [LogUtil log: @" destroy" ];
215+ [self .reporter endDurationEventWithName: @" initialize-release" ext: @{}];
193216 return 0 ;
194217}
195218
@@ -198,39 +221,34 @@ - (void)rtcReportWithEvent: (NSString *)event label: (NSDictionary *)label {
198221 [LogUtil log: @" rtc 不能为空" level: (LogLevelError)];
199222 return ;
200223 }
201- NSString *jsonString = [self convertToJson: label];
202- [self .config.rtcEngine sendCustomReportMessage: @" scenarioAPI"
203- category: [NSString stringWithFormat: @" beauty_iOS_%@ " ,[self getVersion ]]
204- event: event
205- label: jsonString
206- value: 0 ];
207- }
208-
209- - (NSString *)convertToJson : (NSDictionary *)object {
210- NSError *error = nil ;
211- NSData *jsonData = [NSJSONSerialization dataWithJSONObject: object
212- options: 0
213- error: &error];
214- if (error) {
215- // 转换失败
216- NSLog (@" Error: %@ " , error.localizedDescription );
217- return nil ;
218- }
219- NSString *jsonString = [[NSString alloc ] initWithData: jsonData
220- encoding: NSUTF8StringEncoding];
221- return jsonString;
224+ [self .reporter reportFuncEventWithName: event value: label ext: @{}];
222225}
223226
224227- (NSString *)getVersion {
225- return beautyAPIVnersio ;
228+ return beautyAPIVersion ;
226229}
227230
228231#pragma mark - VideoFrameDelegate
229232#if __has_include(<AgoraRtcKit/AgoraRtcKit.h>)
233+ - (BOOL )onCaptureVideoFrame : (AgoraOutputVideoFrame *)videoFrame {
234+ return [self onCaptureVideoFrame: videoFrame sourceType: (AgoraVideoSourceTypeCamera)];
235+ }
230236- (BOOL )onCaptureVideoFrame : (AgoraOutputVideoFrame *)videoFrame sourceType : (AgoraVideoSourceType)sourceType {
231237 if (!self.isEnable ) { return YES ; }
232238 CFTimeInterval startTime = CACurrentMediaTime ();
239+ if (!self.isFirstFrame ) {
240+ [self .reporter startDurationEventWithName: @" first_beauty_frame" ];
241+ }
233242 CVPixelBufferRef pixelBuffer = [self .config.beautyRender onCapture: videoFrame.pixelBuffer];
243+ if (!self.isFirstFrame ) {
244+ [self .reporter endDurationEventWithName: @" first_beauty_frame" ext: @{
245+ @" width" : @(CVPixelBufferGetWidth (pixelBuffer)),
246+ @" height" : @(CVPixelBufferGetHeight (pixelBuffer)),
247+ @" camera_facing" : _isFrontCamera ? @" front" : @" back" ,
248+ @" buffer_type" : @" pixelbuffer"
249+ }];
250+ self.isFirstFrame = YES ;
251+ }
234252 CFTimeInterval endTime = CACurrentMediaTime ();
235253 if (self.config .statsEnable ) {
236254 [self .statsArray addObject: @(endTime - startTime)];
@@ -239,7 +257,7 @@ - (BOOL)onCaptureVideoFrame:(AgoraOutputVideoFrame *)videoFrame sourceType:(Agor
239257 if (self.config .eventCallback && self.preTime > 0 && self.config .statsEnable ) {
240258 CFTimeInterval time = startTime - self.preTime ;
241259 if (time > self.config .statsDuration && self.statsArray .count > 0 ) {
242- NSArray *sortArray = [self .statsArray sortedArrayUsingComparator: ^NSComparisonResult (NSNumber * _Nonnull obj1, NSNumber * _Nonnull obj2) {
260+ NSArray *sortArray = [self .statsArray sortedArrayUsingComparator: ^NSComparisonResult (NSNumber * _Nonnull obj1, NSNumber * _Nonnull obj2) {
243261 return obj1.doubleValue > obj2.doubleValue ;
244262 }];
245263 double totalValue = 0 ;
0 commit comments