1/* 2 * Copyright 2018 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 11#import "ARDBroadcastSampleHandler.h" 12 13#import <os/log.h> 14 15#import "ARDExternalSampleCapturer.h" 16#import "ARDSettingsModel.h" 17 18#import <WebRTC/RTCCallbackLogger.h> 19#import <WebRTC/RTCLogging.h> 20 21@implementation ARDBroadcastSampleHandler { 22 ARDAppClient *_client; 23 RTC_OBJC_TYPE(RTCCallbackLogger) * _callbackLogger; 24} 25 26@synthesize capturer = _capturer; 27 28- (instancetype)init { 29 if (self = [super init]) { 30 _callbackLogger = [[RTC_OBJC_TYPE(RTCCallbackLogger) alloc] init]; 31 os_log_t rtc_os_log = os_log_create("com.google.AppRTCMobile", "RTCLog"); 32 [_callbackLogger start:^(NSString *logMessage) { 33 os_log(rtc_os_log, "%{public}s", [logMessage cStringUsingEncoding:NSUTF8StringEncoding]); 34 }]; 35 } 36 return self; 37} 38 39- (void)broadcastStartedWithSetupInfo:(NSDictionary<NSString *, NSObject *> *)setupInfo { 40 // User has requested to start the broadcast. Setup info from the UI extension can be supplied but 41 // optional. 42 ARDSettingsModel *settingsModel = [[ARDSettingsModel alloc] init]; 43 44 _client = [[ARDAppClient alloc] initWithDelegate:self]; 45 _client.broadcast = YES; 46 47 NSString *roomName = nil; 48 if (setupInfo[@"roomName"]) { 49 roomName = (NSString *)setupInfo[@"roomName"]; 50 } else { 51 u_int32_t randomRoomSuffix = arc4random_uniform(1000); 52 roomName = [NSString stringWithFormat:@"broadcast_%d", randomRoomSuffix]; 53 } 54 [_client connectToRoomWithId:roomName settings:settingsModel isLoopback:NO]; 55 RTCLog(@"Broadcast started."); 56} 57 58- (void)broadcastPaused { 59 // User has requested to pause the broadcast. Samples will stop being delivered. 60} 61 62- (void)broadcastResumed { 63 // User has requested to resume the broadcast. Samples delivery will resume. 64} 65 66- (void)broadcastFinished { 67 // User has requested to finish the broadcast. 68 [_client disconnect]; 69} 70 71- (void)processSampleBuffer:(CMSampleBufferRef)sampleBuffer 72 withType:(RPSampleBufferType)sampleBufferType { 73 switch (sampleBufferType) { 74 case RPSampleBufferTypeVideo: 75 [self.capturer didCaptureSampleBuffer:sampleBuffer]; 76 break; 77 case RPSampleBufferTypeAudioApp: 78 break; 79 case RPSampleBufferTypeAudioMic: 80 break; 81 default: 82 break; 83 } 84} 85 86#pragma mark - ARDAppClientDelegate 87 88- (void)appClient:(ARDAppClient *)client didChangeState:(ARDAppClientState)state { 89 switch (state) { 90 case kARDAppClientStateConnected: 91 RTCLog(@"Client connected."); 92 break; 93 case kARDAppClientStateConnecting: 94 RTCLog("Client connecting."); 95 break; 96 case kARDAppClientStateDisconnected: 97 RTCLog(@"Client disconnected."); 98 break; 99 } 100} 101 102- (void)appClient:(ARDAppClient *)client didChangeConnectionState:(RTCIceConnectionState)state { 103 RTCLog(@"ICE state changed: %ld", (long)state); 104} 105 106- (void)appClient:(ARDAppClient *)client 107 didCreateLocalCapturer:(RTC_OBJC_TYPE(RTCCameraVideoCapturer) *)localCapturer { 108} 109 110- (void)appClient:(ARDAppClient *)client 111 didCreateLocalExternalSampleCapturer:(ARDExternalSampleCapturer *)externalSampleCapturer { 112 self.capturer = externalSampleCapturer; 113} 114 115- (void)appClient:(ARDAppClient *)client 116 didReceiveLocalVideoTrack:(RTC_OBJC_TYPE(RTCVideoTrack) *)localVideoTrack { 117} 118 119- (void)appClient:(ARDAppClient *)client 120 didReceiveRemoteVideoTrack:(RTC_OBJC_TYPE(RTCVideoTrack) *)remoteVideoTrack { 121} 122 123- (void)appClient:(ARDAppClient *)client didGetStats:(NSArray *)stats { 124} 125 126- (void)appClient:(ARDAppClient *)client didError:(NSError *)error { 127 RTCLog(@"Error: %@", error); 128} 129 130@end 131