AVIMRunLoop.m 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. //
  2. // AVIMRunLoop.m
  3. // MsfSDK
  4. //
  5. // Created by etkmao on 13-6-4.
  6. // Copyright (c) 2013年 etkmao. All rights reserved.
  7. //
  8. #import "AVIMRunLoop.h"
  9. #include <sys/time.h>
  10. @implementation AVIMRunLoop
  11. static void AVIMSourceEvent(void* info __unused)
  12. {
  13. // NSLog(@"MSFRunloop source perform");
  14. // do nothing
  15. }
  16. - (void)dealloc
  17. {
  18. DebugLog(@"%@ [%@] Release", self, [AVIMRunLoop class]);
  19. }
  20. static AVIMRunLoop *_sharedRunLoop = nil;
  21. + (AVIMRunLoop *)sharedAVIMRunLoop
  22. {
  23. static dispatch_once_t predicate;
  24. dispatch_once(&predicate, ^{
  25. _sharedRunLoop = [[AVIMRunLoop alloc] init];
  26. });
  27. return _sharedRunLoop;
  28. }
  29. - (id)init
  30. {
  31. if(self = [super init])
  32. {
  33. [self start];
  34. }
  35. return self;
  36. }
  37. - (void)runloopThreadEntry
  38. {
  39. NSAssert(![NSThread isMainThread], @"runloopThread error");
  40. CFRunLoopSourceRef source;
  41. CFRunLoopSourceContext sourceContext;
  42. bzero(&sourceContext, sizeof(sourceContext));
  43. sourceContext.perform = AVIMSourceEvent;
  44. source = CFRunLoopSourceCreate(NULL, 0, &sourceContext);
  45. CFRunLoopAddSource(CFRunLoopGetCurrent(), source, kCFRunLoopCommonModes);
  46. while (YES)
  47. {
  48. DebugLog(@"123123");
  49. [[NSRunLoop currentRunLoop] run];
  50. }
  51. }
  52. - (void)start
  53. {
  54. if(_thread)
  55. {
  56. return;
  57. }
  58. _thread = [[NSThread alloc] initWithTarget:self selector:@selector(runloopThreadEntry) object:nil];
  59. [_thread setName:@"AVIMMsgHandleThread"];
  60. if ([[IOSDeviceConfig sharedConfig] isIOS7Later])
  61. {
  62. // 设置成最低优先级,以保证AV正常
  63. [_thread setQualityOfService:NSQualityOfServiceUtility];
  64. }
  65. else
  66. {
  67. [_thread setThreadPriority:1.0];
  68. }
  69. [_thread start];
  70. }
  71. @end