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.

SampleHandler.m 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright @ 2021-present 8x8, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "SampleHandler.h"
  17. #import "SocketConnection.h"
  18. #import "SampleUploader.h"
  19. #import "DarwinNotificationCenter.h"
  20. @interface SampleHandler ()
  21. @property (nonatomic, retain) SocketConnection *clientConnection;
  22. @property (nonatomic, retain) SampleUploader *uploader;
  23. @end
  24. @implementation SampleHandler
  25. - (instancetype)init {
  26. self = [super init];
  27. if (self) {
  28. self.clientConnection = [[SocketConnection alloc] initWithFilePath:self.socketFilePath];
  29. [self setupConnection];
  30. self.uploader = [[SampleUploader alloc] initWithConnection:self.clientConnection];
  31. }
  32. return self;
  33. }
  34. - (void)broadcastStartedWithSetupInfo:(NSDictionary<NSString *,NSObject *> *)setupInfo {
  35. // User has requested to start the broadcast. Setup info from the UI extension can be supplied but optional.
  36. NSLog(@"broadcast started");
  37. [[DarwinNotificationCenter sharedInstance] postNotificationWithName:kBroadcastStartedNotification];
  38. [self openConnection];
  39. }
  40. - (void)broadcastPaused {
  41. // User has requested to pause the broadcast. Samples will stop being delivered.
  42. }
  43. - (void)broadcastResumed {
  44. // User has requested to resume the broadcast. Samples delivery will resume.
  45. }
  46. - (void)broadcastFinished {
  47. // User has requested to finish the broadcast.
  48. [[DarwinNotificationCenter sharedInstance] postNotificationWithName:kBroadcastStoppedNotification];
  49. [self.clientConnection close];
  50. }
  51. - (void)processSampleBuffer:(CMSampleBufferRef)sampleBuffer withType:(RPSampleBufferType)sampleBufferType {
  52. static NSUInteger frameCount = 0;
  53. switch (sampleBufferType) {
  54. case RPSampleBufferTypeVideo:
  55. // adjust frame rate by using every third frame
  56. if (++frameCount%3 == 0 && self.uploader.isReady) {
  57. [self.uploader sendSample:sampleBuffer];
  58. }
  59. break;
  60. default:
  61. break;
  62. }
  63. }
  64. // MARK: Private Methods
  65. - (NSString *)socketFilePath {
  66. // the appGroupIdentifier must match the value provided in the app's info.plist for the RTCAppGroupIdentifier key
  67. NSString *appGroupIdentifier = @"group.org.jitsi.meet.appgroup";
  68. NSURL *sharedContainer = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:appGroupIdentifier];
  69. NSString *socketFilePath = [[sharedContainer URLByAppendingPathComponent:@"rtc_SSFD"] path];
  70. return socketFilePath;
  71. }
  72. - (void)setupConnection {
  73. __weak __typeof(self) weakSelf = self;
  74. self.clientConnection.didClose = ^(NSError *error) {
  75. NSLog(@"client connection did close: %@", error);
  76. if (error) {
  77. [weakSelf finishBroadcastWithError:error];
  78. }
  79. else {
  80. NSInteger JMScreenSharingStopped = 10001;
  81. NSError *customError = [NSError errorWithDomain:RPRecordingErrorDomain
  82. code:JMScreenSharingStopped
  83. userInfo:@{NSLocalizedDescriptionKey: @"Screen sharing stopped"}];
  84. [weakSelf finishBroadcastWithError:customError];
  85. }
  86. };
  87. }
  88. - (void)openConnection {
  89. dispatch_queue_t queue = dispatch_queue_create("org.jitsi.meet.broadcast.connectTimer", 0);
  90. dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
  91. dispatch_source_set_timer(timer, dispatch_walltime(NULL, 0), 0.1 * NSEC_PER_SEC, 0.1 * NSEC_PER_SEC);
  92. dispatch_source_set_event_handler(timer, ^{
  93. BOOL success = [self.clientConnection open];
  94. if (success) {
  95. dispatch_source_cancel(timer);
  96. }
  97. });
  98. dispatch_resume(timer);
  99. }
  100. @end