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 package org.appspot.apprtc; 12 13 import android.os.ParcelFileDescriptor; 14 import android.util.Log; 15 import java.io.File; 16 import java.io.IOException; 17 import org.webrtc.PeerConnection; 18 19 public class RtcEventLog { 20 private static final String TAG = "RtcEventLog"; 21 private static final int OUTPUT_FILE_MAX_BYTES = 10_000_000; 22 private final PeerConnection peerConnection; 23 private RtcEventLogState state = RtcEventLogState.INACTIVE; 24 25 enum RtcEventLogState { 26 INACTIVE, 27 STARTED, 28 STOPPED, 29 } 30 RtcEventLog(PeerConnection peerConnection)31 public RtcEventLog(PeerConnection peerConnection) { 32 if (peerConnection == null) { 33 throw new NullPointerException("The peer connection is null."); 34 } 35 this.peerConnection = peerConnection; 36 } 37 start(final File outputFile)38 public void start(final File outputFile) { 39 if (state == RtcEventLogState.STARTED) { 40 Log.e(TAG, "RtcEventLog has already started."); 41 return; 42 } 43 final ParcelFileDescriptor fileDescriptor; 44 try { 45 fileDescriptor = ParcelFileDescriptor.open(outputFile, 46 ParcelFileDescriptor.MODE_READ_WRITE | ParcelFileDescriptor.MODE_CREATE 47 | ParcelFileDescriptor.MODE_TRUNCATE); 48 } catch (IOException e) { 49 Log.e(TAG, "Failed to create a new file", e); 50 return; 51 } 52 53 // Passes ownership of the file to WebRTC. 54 boolean success = 55 peerConnection.startRtcEventLog(fileDescriptor.detachFd(), OUTPUT_FILE_MAX_BYTES); 56 if (!success) { 57 Log.e(TAG, "Failed to start RTC event log."); 58 return; 59 } 60 state = RtcEventLogState.STARTED; 61 Log.d(TAG, "RtcEventLog started."); 62 } 63 stop()64 public void stop() { 65 if (state != RtcEventLogState.STARTED) { 66 Log.e(TAG, "RtcEventLog was not started."); 67 return; 68 } 69 peerConnection.stopRtcEventLog(); 70 state = RtcEventLogState.STOPPED; 71 Log.d(TAG, "RtcEventLog stopped."); 72 } 73 } 74