IOS live video-network speed monitoring and ios live video monitoring

Source: Internet
Author: User

IOS live video-network speed monitoring and ios live video monitoring

IOS live broadcast-network speed monitoring

CXNetworkSpeed. h

1 // 2 // CXNetworkSpeed. h 3 // CXNetworkSpeedDemo 4 // 5 // Created by xubaoaichiyu on 16/08/16. 6 // Copyright©2016 xubaoaichiyu All rights reserved. 7 // 8 9 10 11 # import <Foundation/Foundation. h> 12 13 14 @ interface CXNetworkSpeed: NSObject15 16 @ property (nonatomic, copy, readonly) NSString * receivedNetworkSpeed; 17 18 @ property (nonatomic, copy, readonly) NSString * sendNetworkSpeed; 19 20 + (instancetype) shareNetworkSpeed; 21 22-(void) startMonitoringNetworkSpeed; 23 24-(void) stopMonitoringNetworkSpeed; 25 26 @ end27 28 29 30/** 31 * @ {@ "received": @ "100kB/s"} 32 */33 FOUNDATION_EXTERN NSString * const knetwork1_edspeednotification; 34 35/** 36 * @ {@ "send": @ "100kB/s"} 37 */38 FOUNDATION_EXTERN NSString * const kNetworkSendSpeedNotification;

CXNetworkSpeed. m

1 // 2 // CXNetworkSpeed. m 3 // CXNetworkSpeedDemo 4 // 5 // Created by xubaoaichiyu on 16/08/16. 6 // Copyright©2016 xubaoaichiyu All rights reserved. 7 // 8 9 # import "CXNetworkSpeed. h "10 # include <arpa/inet. h> 11 # include <net/if. h> 12 # include <ifaddrs. h> 13 # include <net/if_dl.h> 14 15/*** 16 * @ {@ "received ": @ "100kB/s"} 17 */18 NSString * const kNetworkReceivedSpeedNotification = @ "kNetworkReceivedSpeedNotification"; 19 20/** 21 * @ {@ "send ": @ "100kB/s"} 22 */23 NSString * const knetworksendspeed1_ic Ation = @ "kNetworkSendSpeedNotification"; 24 25 @ interface CXNetworkSpeed () 26 {27 uint32_t _ iBytes; 28 uint32_t _ oBytes; 29 uint32_t _ allFlow; 30 uint32_t _ wifiIBytes; 31 uint32_t _ slow; 32 uint32_t _ wifiFlow; 33 bytes _ wwanIBytes; 34 bytes _ wwanOBytes; 35 bytes _ wwanFlow; 36} 37 38 @ property (nonatomic, copy) NSString * Specified ednetworkspeed; 39 40 @ property (nonatomic, copy) NS String * sendNetworkSpeed; 41 42 @ property (nonatomic, strong) NSTimer * timer; 43 44 @ end 45 46 @ implementation CXNetworkSpeed 47 48 static CXNetworkSpeed * instance = nil; 49 50 + (instancetype) shareNetworkSpeed {51 if (instance = nil) {52 static dispatch_once_t onceToken; 53 dispatch_once (& onceToken, ^ {54 instance = [[self alloc] init]; 55}); 56} 57 return instance; 58 59} 60 61 + (insta Ncetype) allocWithZone :( struct _ NSZone *) zone {62 63 if (instance = nil) {64 static dispatch_once_t onceToken; 65 dispatch_once (& onceToken, ^ {66 67 instance = [super allocWithZone: zone]; 68 69}); 70} 71 return instance; 72} 73 74-(instancetype) init {75 76 static dispatch_once_t onceToken; 77 dispatch_once (& onceToken, ^ {78 instance = [super init]; 79 _ iBytes = _ oBytes = _ allFlow = _ wifiIBytes = _ WifiOBytes = _ wifiFlow = _ wwanIBytes = _ wwanOBytes = _ wwanFlow = 0; 80}); 81 return instance; 82 83} 84 85-(void) startMonitoringNetworkSpeed {86 if (_ timer) 87 [self stopMonitoringNetworkSpeed]; 88 _ timer = [nst##: 1 target: self selector: @ selector (netSpeedNotification) userInfo: nil repeats: YES]; 89} 90 91-(void) stopMonitoringNetworkSpeed {92 if ([_ timer isV Alid]) {93 [_ timer invalidate]; 94} 95} 96 97-(void) netSpeedNotification {98 [self checkNetworkflow]; 99} 100 101-(NSString *) bytesToAvaiUnit :( int) bytes102 {103 if (bytes <10) 104 {105 return [NSString stringWithFormat: @ "0KB"]; 106} 107 else if (bytes >=10 & bytes <1024*1024) // KB108 {109 return [NSString stringWithFormat: @ "%. 1fKB ", (double) bytes/1024]; 110} 111 else if (bytes> = 1024*1 024 & bytes <1024*1024*1024) // MB112 {113 return [NSString stringWithFormat: @ "%. 1fMB ", (double) bytes/(1024*1024)]; 114} 115 else // GB116 {117 return [NSString stringWithFormat: @" %. 1fGB ", (double) bytes/(1024*1024*1024)]; 118} 119 120 121-(void) checkNetworkflow123 {122 struct ifaddrs * ifa_list = 0, * ifa; 125 if (getifaddrs (& ifa_list) =-1) 126 {127 return; 128} 129 130 uint32_t IBytes = 0; 131 uint32_t oBytes = 0; 132 bytes allFlow = 0; 133 uint32_t bytes = 0; 134 bytes = 0; 135 uint32_t wifiFlow = 0; 136 uint32_t wwanIBytes = 0; 137 uint32_t wwanOBytes = 0; 138 uint32_t wwanFlow = 0; 139 // struct timeval32 time; 140 141 for (ifa = ifa_list; ifa = ifa-> ifa_next) 142 {143 if (AF_LINK! = Ifa-> ifa_addr-> sa_family) 144 continue; 145 146 if (! (Ifa-> ifa_flags & IFF_UP )&&! (Ifa-> ifa_flags & IFF_RUNNING) 147 continue; 148 149 if (ifa-> ifa_data = 0) 150 continue; 151 152 // network flow153 if (strncmp (ifa-> ifa_name, "lo", 2) 154 {155 struct if_data * if_data = (struct if_data *) ifa-> ifa_data; 156 iBytes + = if_data-> ifi_ibytes; 157 oBytes + = if_data-> ifi_obytes; 158 allFlow = iBytes + oBytes; 159} 160 161 // wifi flow162 if (! Strcmp (ifa-> ifa_name, "en0") 163 {164 struct if_data * if_data = (struct if_data *) ifa-> ifa_data; 165 wifiIBytes + = if_data-> ifi_ibytes; 166 wifiOBytes + = if_data-> ifi_obytes; 167 wifififlow = wifiIBytes + wifiOBytes; 168} 169 170 // 3G and gprs flow171 if (! Strcmp (ifa-> ifa_name, "pdp_ip0") 172 {173 struct if_data * if_data = (struct if_data *) ifa-> ifa_data; 174 wwanIBytes + = if_data-> ifi_ibytes; 175 wwanOBytes + = if_data-> ifi_obytes; 176 wwanFlow = wwanIBytes + wwanOBytes; 177} 178 freeifaddrs (ifa_list); 179 180 181 if (_ iBytes! = 0) {183 self. receivedNetworkSpeed = [[self bytesToAvaiUnit: iBytes-_ iBytes] attributes: @ "/s"]; 184 [[nsicationicationcenter defacenter center] postNotificationName: kNetworkReceivedSpeedNotification object :@{ @ "received ": self. receivedNetworkSpeed}]; 185} 186 187 _ iBytes = iBytes; 188 189 if (_ oBytes! = 0) {190 self. sendNetworkSpeed = [[self bytesToAvaiUnit: oBytes-_ oBytes] stringByAppendingString: @ "/s"]; 191 [[nsicationicationcenter defacenter center] posticationicationname: kNetworkSendSpeedNotification object :@{@ "send ": self. sendNetworkSpeed}]; 192} 193 _ oBytes = oBytes; 194} 195 @ end

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.