You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.3 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #import "ARDBitrateTracker.h"
  11. #import <QuartzCore/QuartzCore.h>
  12. @implementation ARDBitrateTracker {
  13. CFTimeInterval _prevTime;
  14. NSInteger _prevByteCount;
  15. }
  16. @synthesize bitrate = _bitrate;
  17. + (NSString *)bitrateStringForBitrate:(double)bitrate {
  18. if (bitrate > 1e6) {
  19. return [NSString stringWithFormat:@"%.2fMbps", bitrate * 1e-6];
  20. } else if (bitrate > 1e3) {
  21. return [NSString stringWithFormat:@"%.0fKbps", bitrate * 1e-3];
  22. } else {
  23. return [NSString stringWithFormat:@"%.0fbps", bitrate];
  24. }
  25. }
  26. - (NSString *)bitrateString {
  27. return [[self class] bitrateStringForBitrate:_bitrate];
  28. }
  29. - (void)updateBitrateWithCurrentByteCount:(NSInteger)byteCount {
  30. CFTimeInterval currentTime = CACurrentMediaTime();
  31. if (_prevTime && (byteCount > _prevByteCount)) {
  32. _bitrate = (byteCount - _prevByteCount) * 8 / (currentTime - _prevTime);
  33. }
  34. _prevByteCount = byteCount;
  35. _prevTime = currentTime;
  36. }
  37. @end