webRtcManager+StatisticsReport.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. //
  2. // webRtcManager+StatisticsReport.m
  3. // 双子星云手机
  4. //
  5. // Created by xd h on 2024/9/10.
  6. //
  7. #import "webRtcManager+StatisticsReport.h"
  8. @implementation webRtcManager (StatisticsReport)
  9. // IsChannel:是否是P2P通道
  10. - (void)reportWebRtcRePoportTypeIsChannel:(BOOL)IsChannel withStats:(RTC_OBJC_TYPE(RTCStatisticsReport) *)stats withSessionId:(NSString*)sessionId withLogKey:(NSString*)logkey
  11. {
  12. //HLog(@"444444 %@",stats)
  13. if(!stats){
  14. [self reportWebRtcRePoportTypeIsChannel:IsChannel withStatus:0 withPublicIp:@"" withPublicPort:@"" withPrivateIp:@"" withPrivatePort:@"" withSessionId:@"" withLogKey:logkey];
  15. return;
  16. }
  17. //
  18. NSDictionary *jsonTransport = nil;
  19. NSMutableArray *pairMapArr = [NSMutableArray new];
  20. NSMutableArray *remoteCandidateMapArr = [NSMutableArray new];
  21. NSMutableArray *localCandidateMapArr = [NSMutableArray new];
  22. //1.先拿到 jsonTransport 的信息
  23. for (NSString *key in [stats.statistics allKeys]) {
  24. RTCStatistics *rtcStatistics = [stats.statistics objectForKey:key];
  25. //HLog(@"222 %@",rtcStatistics.type)
  26. if ([rtcStatistics.type isEqualToString:@"transport"]) {
  27. jsonTransport = rtcStatistics.values;
  28. continue;
  29. }
  30. if ([rtcStatistics.type isEqualToString:@"candidate-pair"]) {
  31. [pairMapArr addObject:rtcStatistics];
  32. continue;
  33. }
  34. if ([rtcStatistics.type isEqualToString:@"remote-candidate"]) {
  35. [remoteCandidateMapArr addObject:rtcStatistics];
  36. continue;
  37. }
  38. if ([rtcStatistics.type isEqualToString:@"local-candidate"]) {
  39. [localCandidateMapArr addObject:rtcStatistics];
  40. continue;
  41. }
  42. }
  43. NSString *selectedCandidatePairId = @"";
  44. NSString *localCandidateId = @"";
  45. NSString *remoteCandidateId = @"";
  46. if([[jsonTransport allKeys] containsObject:@"selectedCandidatePairId"]){
  47. selectedCandidatePairId = jsonTransport[@"selectedCandidatePairId"];
  48. }
  49. //2.找到 localCandidateId remoteCandidateId
  50. for (RTCStatistics *rtcStatistics in pairMapArr) {
  51. if([rtcStatistics isKindOfClass:[RTCStatistics class]]){
  52. NSString *curId = rtcStatistics.id;
  53. if([curId isEqualToString:selectedCandidatePairId]){
  54. NSDictionary *valuesDict = rtcStatistics.values;
  55. if([valuesDict isKindOfClass:[NSDictionary class]]){
  56. if ([[valuesDict allKeys] containsObject:@"localCandidateId"]) {
  57. localCandidateId = valuesDict[@"localCandidateId"];
  58. }
  59. if ([[valuesDict allKeys] containsObject:@"remoteCandidateId"]) {
  60. remoteCandidateId = valuesDict[@"remoteCandidateId"];
  61. }
  62. }
  63. break;
  64. }
  65. }
  66. }
  67. //3.找到 localCandidateDict
  68. NSDictionary *localCandidateDict = nil;
  69. NSString*privateIp = @"";
  70. NSString*privatePort = @"";
  71. NSString *localCandidateType = @"";
  72. for (RTCStatistics *rtcStatistics in localCandidateMapArr) {
  73. if([rtcStatistics isKindOfClass:[RTCStatistics class]]){
  74. NSString *curId = rtcStatistics.id;
  75. if([curId isEqualToString:localCandidateId]){
  76. localCandidateDict = rtcStatistics.values;
  77. if([localCandidateDict isKindOfClass:[NSDictionary class]]){
  78. if([[localCandidateDict allKeys] containsObject:@"ip"]){
  79. privateIp = localCandidateDict[@"ip"];
  80. }
  81. if([[localCandidateDict allKeys] containsObject:@"port"]){
  82. privatePort = localCandidateDict[@"port"];
  83. }
  84. if([[localCandidateDict allKeys] containsObject:@"candidateType"]){
  85. localCandidateType = localCandidateDict[@"candidateType"];
  86. }
  87. }
  88. break;
  89. }
  90. }
  91. }
  92. //4.找到 remoteCandidateDict
  93. NSDictionary *remoteCandidateDict = nil;
  94. NSString*publicIp = @"";
  95. NSString*publicPort = @"";
  96. NSString *remoteCandidateType = @"";
  97. for (RTCStatistics *rtcStatistics in remoteCandidateMapArr) {
  98. if([rtcStatistics isKindOfClass:[RTCStatistics class]]){
  99. NSString *curId = rtcStatistics.id;
  100. if([curId isEqualToString:remoteCandidateId]){
  101. remoteCandidateDict = rtcStatistics.values;
  102. if([remoteCandidateDict isKindOfClass:[NSDictionary class]]){
  103. if([[remoteCandidateDict allKeys] containsObject:@"ip"]){
  104. publicIp = remoteCandidateDict[@"ip"];
  105. }
  106. if([[remoteCandidateDict allKeys] containsObject:@"port"]){
  107. publicPort = remoteCandidateDict[@"port"];
  108. }
  109. if([[remoteCandidateDict allKeys] containsObject:@"candidateType"]){
  110. remoteCandidateType = remoteCandidateDict[@"candidateType"];
  111. }
  112. }
  113. break;
  114. }
  115. }
  116. }
  117. //
  118. int linkState = 1;
  119. if([localCandidateType isEqualToString:@"host"]
  120. && [remoteCandidateType isEqualToString:@"host"]){//内网
  121. linkState = 3;
  122. }
  123. else if([localCandidateType isEqualToString:@"relay"]
  124. ||[remoteCandidateType isEqualToString:@"relay"]){
  125. linkState = 2;
  126. //[self reportWebRtcRePoportTypeIsChannel:IsChannel withStatus:2 withPublicIp:publicIp withPublicPort:publicPort withPrivateIp:privateIp withPrivatePort:privatePort];
  127. }
  128. else{
  129. linkState = 1;
  130. //[self reportWebRtcRePoportTypeIsChannel:IsChannel withStatus:1 withPublicIp:publicIp withPublicPort:publicPort withPrivateIp:privateIp withPrivatePort:privatePort];
  131. }
  132. [self reportWebRtcRePoportTypeIsChannel:IsChannel withStatus:linkState withPublicIp:publicIp withPublicPort:publicPort withPrivateIp:privateIp withPrivatePort:privatePort withSessionId:sessionId withLogKey:logkey];
  133. }
  134. - (void)reportWebRtcRePoportTypeIsChannel:(BOOL)IsChannel withStatus:(int)linkState withPublicIp:(NSString*)publicIp withPublicPort:(NSString*)publicPort withPrivateIp:(NSString*)privateIp withPrivatePort:(NSString*)privatePort withSessionId:(NSString*)sessionId withLogKey:(NSString*)logkey
  135. {
  136. NSMutableDictionary *paraDict = [NSMutableDictionary new];
  137. [paraDict setValue:@2 forKey:@"type"];
  138. [paraDict setValue:[NSNumber numberWithInt:linkState] forKey:@"status"];
  139. int channelType = IsChannel ? 1 : 2;
  140. [paraDict setValue:[NSNumber numberWithInt:channelType] forKey:@"channelType"];
  141. int clientNetworkStatus = [pingManager shareManager].isPingOk ? 0 : 1;
  142. [paraDict setValue:[NSNumber numberWithInt:clientNetworkStatus] forKey:@"clientNetworkStatus"];
  143. if(sessionId){
  144. [paraDict setValue:sessionId forKey:@"sessionId"];
  145. }
  146. if(publicIp){
  147. [paraDict setValue:publicIp forKey:@"publicIp"];
  148. }
  149. if(publicPort){
  150. [paraDict setValue:publicPort forKey:@"publicPort"];
  151. }
  152. if(privateIp){
  153. [paraDict setValue:privateIp forKey:@"privateIp"];
  154. }
  155. if(privatePort){
  156. [paraDict setValue:privatePort forKey:@"privatePort"];
  157. }
  158. if(logkey){
  159. [paraDict setValue:logkey forKey:@"failureReason"];
  160. }
  161. if(ksharedAppDelegate.bestWebrtcServerModel){
  162. NSString * turnMsg = [[NSString alloc] initWithFormat:@"%@",ksharedAppDelegate.bestWebrtcServerModel.turnIp];
  163. [paraDict setValue:turnMsg forKey:@"coturnIp"];
  164. NSString * signalMsg = [[NSString alloc] initWithFormat:@"%@",ksharedAppDelegate.bestWebrtcServerModel.signallingIp];
  165. [paraDict setValue:signalMsg forKey:@"signalingIp"];
  166. }
  167. [[netWorkManager shareInstance] CommonPostCallBackCode:webrctLogAdd Parameters:paraDict success:^(id _Nonnull responseObject){
  168. SuperModel *curModel = [[SuperModel alloc] initWithDictionary:responseObject error:nil];
  169. if(curModel && curModel.status == 0){
  170. }
  171. else
  172. {
  173. }
  174. } failure:^(NSError * _Nonnull error) {
  175. HLog(@"")
  176. }];
  177. }
  178. // IsChannel:是否是P2P通道
  179. - (void)reportWebRtcRePoportTypeIsChannel:(BOOL)IsChannel withLocal:(RTC_OBJC_TYPE(RTCIceCandidate) *)local remoteCandidate:(RTC_OBJC_TYPE(RTCIceCandidate) *)remote withSessionId:(NSString*)sessionId withLogKey:(NSString*)logkey{
  180. // candidate:504853897 1 udp 41885695 115.227.9.195 35762 typ relay raddr 115.227.9.184 rport 43877 generation 0 ufrag OP7P network-id 1
  181. NSString*localSdpStr = local.sdp;
  182. if(!localSdpStr || localSdpStr.length==0){
  183. return;
  184. }
  185. //正常可以分割到20个非空格元素
  186. NSArray *localSdpArr = [localSdpStr componentsSeparatedByString:@" "];
  187. if(localSdpArr.count < 12){
  188. return;
  189. }
  190. NSString* localIp = localSdpArr[4];
  191. NSString* localPort = localSdpArr[5];
  192. NSString* localType = localSdpArr[7];
  193. //NSString* localRelatedAddress = localSdpArr[9];// 可能为 null
  194. //NSString* localRelatedPort = localSdpArr[11];
  195. //NSString* localNetworkId = localSdpArr[11];// 可能为 null
  196. NSString*remoteSdpStr = remote.sdp;
  197. if(!remoteSdpStr || remoteSdpStr.length==0){
  198. return;
  199. }
  200. //正常可以分割到20个非空格元素
  201. NSArray *remoteSdpArr = [remoteSdpStr componentsSeparatedByString:@" "];
  202. if(remoteSdpArr.count < 12){
  203. return;
  204. }
  205. NSString* remoteIp = remoteSdpArr[4];
  206. NSString* remotePort = remoteSdpArr[5];
  207. NSString* remoteType = remoteSdpArr[7];
  208. //NSString* remoteRelatedAddress = remoteSdpArr[9];// 可能为 null
  209. //NSString* remoteRelatedPort = remoteSdpArr[11];
  210. //NSString* remoteNetworkId = remoteSdpArr[11];// 可能为 null
  211. int linkState = 1;
  212. if([localType isEqualToString:@"host"]
  213. && [remoteType isEqualToString:@"host"]){//内网
  214. linkState = 3;
  215. }
  216. else if([localType isEqualToString:@"relay"]
  217. ||[remoteType isEqualToString:@"relay"]){
  218. linkState = 2;
  219. }
  220. else{
  221. linkState = 1;
  222. }
  223. [self reportWebRtcRePoportTypeIsChannel:IsChannel withStatus:linkState withPublicIp:remoteIp withPublicPort:remotePort withPrivateIp:localIp withPrivatePort:localPort withSessionId:sessionId withLogKey:logkey];
  224. }
  225. @end