Add RTCEventLog API to ObjC.

NOTRY=True

BUG=

Review-Url: https://codereview.webrtc.org/2067683002
Cr-Commit-Position: refs/heads/master@{#13144}
diff --git a/webrtc/examples/objc/AppRTCDemo/ARDAppClient.m b/webrtc/examples/objc/AppRTCDemo/ARDAppClient.m
index 8fb1841..88431c6 100644
--- a/webrtc/examples/objc/AppRTCDemo/ARDAppClient.m
+++ b/webrtc/examples/objc/AppRTCDemo/ARDAppClient.m
@@ -12,7 +12,6 @@
 
 #if defined(WEBRTC_IOS)
 #import "WebRTC/RTCAVFoundationVideoSource.h"
-#import "WebRTC/RTCTracing.h"
 #endif
 #import "WebRTC/RTCAudioTrack.h"
 #import "WebRTC/RTCConfiguration.h"
@@ -23,6 +22,7 @@
 #import "WebRTC/RTCMediaStream.h"
 #import "WebRTC/RTCPeerConnectionFactory.h"
 #import "WebRTC/RTCRtpSender.h"
+#import "WebRTC/RTCTracing.h"
 
 #import "ARDAppEngineClient.h"
 #import "ARDCEODTURNClient.h"
@@ -55,8 +55,10 @@
 
 // TODO(tkchin): Remove guard once rtc_sdk_common_objc compiles on Mac.
 #if defined(WEBRTC_IOS)
-// TODO(tkchin): Add this as a UI option.
+// TODO(tkchin): Add these as UI options.
 static BOOL const kARDAppClientEnableTracing = NO;
+static BOOL const kARDAppClientEnableRtcEventLog = YES;
+static int64_t const kARDAppClientRtcEventLogMaxSizeInBytes = 5e6;  // 5 MB.
 #endif
 
 // We need a proxy to NSTimer because it causes a strong retain cycle. When
@@ -227,11 +229,7 @@
 
 #if defined(WEBRTC_IOS)
   if (kARDAppClientEnableTracing) {
-    NSArray *paths = NSSearchPathForDirectoriesInDomains(
-        NSDocumentDirectory, NSUserDomainMask, YES);
-    NSString *documentsDirPath = paths.firstObject;
-    NSString *filePath =
-        [documentsDirPath stringByAppendingPathComponent:@"webrtc-trace.txt"];
+    NSString *filePath = [self documentsFilePathForFileName:@"webrtc-trace.txt"];
     RTCStartInternalCapture(filePath);
   }
 #endif
@@ -314,6 +312,7 @@
   self.state = kARDAppClientStateDisconnected;
 #if defined(WEBRTC_IOS)
   RTCStopInternalCapture();
+  [_factory stopRtcEventLog];
 #endif
 }
 
@@ -498,6 +497,20 @@
 
 #pragma mark - Private
 
+#if defined(WEBRTC_IOS)
+
+- (NSString *)documentsFilePathForFileName:(NSString *)fileName {
+  NSParameterAssert(fileName.length);
+  NSArray *paths = NSSearchPathForDirectoriesInDomains(
+      NSDocumentDirectory, NSUserDomainMask, YES);
+  NSString *documentsDirPath = paths.firstObject;
+  NSString *filePath =
+      [documentsDirPath stringByAppendingPathComponent:fileName];
+  return filePath;
+}
+
+#endif
+
 - (BOOL)hasJoinedRoomServerRoom {
   return _clientId.length;
 }
@@ -513,6 +526,17 @@
   }
   self.state = kARDAppClientStateConnected;
 
+#if defined(WEBRTC_IOS)
+  // Start event log.
+  if (kARDAppClientEnableRtcEventLog) {
+    NSString *filePath = [self documentsFilePathForFileName:@"webrtc-rtceventlog"];
+    if (![_factory startRtcEventLogWithFilePath:filePath
+                                 maxSizeInBytes:kARDAppClientRtcEventLogMaxSizeInBytes]) {
+      RTCLogError(@"Failed to start event logging.");
+    }
+  }
+#endif
+
   // Create peer connection.
   RTCMediaConstraints *constraints = [self defaultPeerConnectionConstraints];
   RTCConfiguration *config = [[RTCConfiguration alloc] init];
diff --git a/webrtc/sdk/objc/Framework/Classes/RTCPeerConnectionFactory.mm b/webrtc/sdk/objc/Framework/Classes/RTCPeerConnectionFactory.mm
index 2f5a87b..a1e701b 100644
--- a/webrtc/sdk/objc/Framework/Classes/RTCPeerConnectionFactory.mm
+++ b/webrtc/sdk/objc/Framework/Classes/RTCPeerConnectionFactory.mm
@@ -10,6 +10,8 @@
 
 #import "RTCPeerConnectionFactory+Private.h"
 
+#include <memory>
+
 #import "NSString+StdString.h"
 #import "RTCAVFoundationVideoSource+Private.h"
 #import "RTCAudioTrack+Private.h"
@@ -17,13 +19,15 @@
 #import "RTCPeerConnection+Private.h"
 #import "RTCVideoSource+Private.h"
 #import "RTCVideoTrack+Private.h"
+#import "WebRTC/RTCLogging.h"
 
-#include <memory>
+#include "webrtc/base/checks.h"
 
 @implementation RTCPeerConnectionFactory {
   std::unique_ptr<rtc::Thread> _networkThread;
   std::unique_ptr<rtc::Thread> _workerThread;
   std::unique_ptr<rtc::Thread> _signalingThread;
+  BOOL _hasStartedRtcEventLog;
 }
 
 @synthesize nativeFactory = _nativeFactory;
@@ -50,6 +54,29 @@
   return self;
 }
 
+- (BOOL)startRtcEventLogWithFilePath:(NSString *)filePath
+                      maxSizeInBytes:(int64_t)maxSizeInBytes {
+  RTC_DCHECK(filePath.length);
+  RTC_DCHECK_GT(maxSizeInBytes, 0);
+  RTC_DCHECK(!_hasStartedRtcEventLog);
+  if (_hasStartedRtcEventLog) {
+    RTCLogError(@"Event logging already started.");
+    return NO;
+  }
+  int fd = open(filePath.UTF8String, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
+  if (fd < 0) {
+    RTCLogError(@"Error opening file: %@. Error: %d", filePath, errno);
+    return NO;
+  }
+  _hasStartedRtcEventLog = _nativeFactory->StartRtcEventLog(fd, maxSizeInBytes);
+  return _hasStartedRtcEventLog;
+}
+
+- (void)stopRtcEventLog {
+  _nativeFactory->StopRtcEventLog();
+  _hasStartedRtcEventLog = NO;
+}
+
 - (RTCAVFoundationVideoSource *)avFoundationVideoSourceWithConstraints:
     (nullable RTCMediaConstraints *)constraints {
   return [[RTCAVFoundationVideoSource alloc] initWithFactory:self
diff --git a/webrtc/sdk/objc/Framework/Headers/WebRTC/RTCPeerConnectionFactory.h b/webrtc/sdk/objc/Framework/Headers/WebRTC/RTCPeerConnectionFactory.h
index f21c107..f968ab7 100644
--- a/webrtc/sdk/objc/Framework/Headers/WebRTC/RTCPeerConnectionFactory.h
+++ b/webrtc/sdk/objc/Framework/Headers/WebRTC/RTCPeerConnectionFactory.h
@@ -53,6 +53,11 @@
                                               delegate:
     (nullable id<RTCPeerConnectionDelegate>)delegate;
 
+/** Temporary interface. Use at your own risk. See peerconnectioninterface.h for details. */
+- (BOOL)startRtcEventLogWithFilePath:(NSString *)filePath
+                      maxSizeInBytes:(int64_t)maxSizeInBytes;
+- (void)stopRtcEventLog;
+
 @end
 
 NS_ASSUME_NONNULL_END