2014 snapchat source code
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.

61 lines
1.9 KiB

  1. //
  2. // SCManagedVideoCapturerTimeObserver.m
  3. // Snapchat
  4. //
  5. // Created by Michel Loenngren on 4/3/17.
  6. // Copyright © 2017 Snapchat, Inc. All rights reserved.
  7. //
  8. #import "SCManagedVideoCapturerTimeObserver.h"
  9. #import "SCTimedTask.h"
  10. #import <SCFoundation/SCAssertWrapper.h>
  11. #import <SCFoundation/SCLog.h>
  12. #import <SCFoundation/SCThreadHelpers.h>
  13. @implementation SCManagedVideoCapturerTimeObserver {
  14. NSMutableArray<SCTimedTask *> *_tasks;
  15. BOOL _isProcessing;
  16. }
  17. - (instancetype)init
  18. {
  19. if (self = [super init]) {
  20. _tasks = [NSMutableArray new];
  21. _isProcessing = NO;
  22. }
  23. return self;
  24. }
  25. - (void)addTimedTask:(SCTimedTask *_Nonnull)task
  26. {
  27. SCAssert(!_isProcessing,
  28. @"[SCManagedVideoCapturerTimeObserver] Trying to add an SCTimedTask after streaming started.");
  29. SCAssert(CMTIME_IS_VALID(task.targetTime),
  30. @"[SCManagedVideoCapturerTimeObserver] Trying to add an SCTimedTask with invalid time.");
  31. [_tasks addObject:task];
  32. [_tasks sortUsingComparator:^NSComparisonResult(SCTimedTask *_Nonnull obj1, SCTimedTask *_Nonnull obj2) {
  33. return (NSComparisonResult)CMTimeCompare(obj2.targetTime, obj1.targetTime);
  34. }];
  35. SCLogGeneralInfo(@"[SCManagedVideoCapturerTimeObserver] Adding task: %@, task count: %lu", task,
  36. (unsigned long)_tasks.count);
  37. }
  38. - (void)processTime:(CMTime)relativePresentationTime
  39. sessionStartTimeDelayInSecond:(CGFloat)sessionStartTimeDelayInSecond
  40. {
  41. _isProcessing = YES;
  42. SCTimedTask *last = _tasks.lastObject;
  43. while (last && last.task && CMTimeCompare(relativePresentationTime, last.targetTime) >= 0) {
  44. [_tasks removeLastObject];
  45. void (^task)(CMTime relativePresentationTime, CGFloat sessionStartTimeDelay) = last.task;
  46. last.task = nil;
  47. runOnMainThreadAsynchronously(^{
  48. task(relativePresentationTime, sessionStartTimeDelayInSecond);
  49. });
  50. last = _tasks.lastObject;
  51. }
  52. }
  53. @end