| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright 2016 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.webrtc; | |
| 12 | |
| 13 /** | |
| 14 * Base interface for camera1 and camera2 implementations. Extends VideoCapturer
with a | |
| 15 * switchCamera() function. Also provides subinterfaces for handling camera even
ts, and a helper | |
| 16 * class for detecting camera freezes. | |
| 17 */ | |
| 18 public interface CameraVideoCapturer extends VideoCapturer { | |
| 19 /** | |
| 20 * Camera events handler - can be used to be notifed about camera events. The
callbacks are | |
| 21 * executed from an arbitrary thread. | |
| 22 */ | |
| 23 public interface CameraEventsHandler { | |
| 24 // Camera error handler - invoked when camera can not be opened | |
| 25 // or any camera exception happens on camera thread. | |
| 26 void onCameraError(String errorDescription); | |
| 27 | |
| 28 // Called when camera is disconnected. | |
| 29 void onCameraDisconnected(); | |
| 30 | |
| 31 // Invoked when camera stops receiving frames. | |
| 32 void onCameraFreezed(String errorDescription); | |
| 33 | |
| 34 // Callback invoked when camera is opening. | |
| 35 void onCameraOpening(String cameraName); | |
| 36 | |
| 37 // Callback invoked when first camera frame is available after camera is sta
rted. | |
| 38 void onFirstFrameAvailable(); | |
| 39 | |
| 40 // Callback invoked when camera is closed. | |
| 41 void onCameraClosed(); | |
| 42 } | |
| 43 | |
| 44 /** | |
| 45 * Camera switch handler - one of these functions are invoked with the result
of switchCamera(). | |
| 46 * The callback may be called on an arbitrary thread. | |
| 47 */ | |
| 48 public interface CameraSwitchHandler { | |
| 49 // Invoked on success. |isFrontCamera| is true if the new camera is front fa
cing. | |
| 50 void onCameraSwitchDone(boolean isFrontCamera); | |
| 51 | |
| 52 // Invoked on failure, e.g. camera is stopped or only one camera available. | |
| 53 void onCameraSwitchError(String errorDescription); | |
| 54 } | |
| 55 | |
| 56 /** | |
| 57 * Switch camera to the next valid camera id. This can only be called while th
e camera is running. | |
| 58 * This function can be called from any thread. | |
| 59 */ | |
| 60 void switchCamera(CameraSwitchHandler switchEventsHandler); | |
| 61 | |
| 62 /** | |
| 63 * Helper class to log framerate and detect if the camera freezes. It will run
periodic callbacks | |
| 64 * on the SurfaceTextureHelper thread passed in the ctor, and should only be o
perated from that | |
| 65 * thread. | |
| 66 */ | |
| 67 public static class CameraStatistics { | |
| 68 private final static String TAG = "CameraStatistics"; | |
| 69 private final static int CAMERA_OBSERVER_PERIOD_MS = 2000; | |
| 70 private final static int CAMERA_FREEZE_REPORT_TIMOUT_MS = 4000; | |
| 71 | |
| 72 private final SurfaceTextureHelper surfaceTextureHelper; | |
| 73 private final CameraEventsHandler eventsHandler; | |
| 74 private int frameCount; | |
| 75 private int freezePeriodCount; | |
| 76 // Camera observer - monitors camera framerate. Observer is executed on came
ra thread. | |
| 77 private final Runnable cameraObserver = new Runnable() { | |
| 78 @Override | |
| 79 public void run() { | |
| 80 final int cameraFps = Math.round(frameCount * 1000.0f / CAMERA_OBSERVER_
PERIOD_MS); | |
| 81 Logging.d(TAG, "Camera fps: " + cameraFps + "."); | |
| 82 if (frameCount == 0) { | |
| 83 ++freezePeriodCount; | |
| 84 if (CAMERA_OBSERVER_PERIOD_MS * freezePeriodCount >= CAMERA_FREEZE_REP
ORT_TIMOUT_MS | |
| 85 && eventsHandler != null) { | |
| 86 Logging.e(TAG, "Camera freezed."); | |
| 87 if (surfaceTextureHelper.isTextureInUse()) { | |
| 88 // This can only happen if we are capturing to textures. | |
| 89 eventsHandler.onCameraFreezed("Camera failure. Client must return
video buffers."); | |
| 90 } else { | |
| 91 eventsHandler.onCameraFreezed("Camera failure."); | |
| 92 } | |
| 93 return; | |
| 94 } | |
| 95 } else { | |
| 96 freezePeriodCount = 0; | |
| 97 } | |
| 98 frameCount = 0; | |
| 99 surfaceTextureHelper.getHandler().postDelayed(this, CAMERA_OBSERVER_PERI
OD_MS); | |
| 100 } | |
| 101 }; | |
| 102 | |
| 103 public CameraStatistics( | |
| 104 SurfaceTextureHelper surfaceTextureHelper, CameraEventsHandler eventsHan
dler) { | |
| 105 if (surfaceTextureHelper == null) { | |
| 106 throw new IllegalArgumentException("SurfaceTextureHelper is null"); | |
| 107 } | |
| 108 this.surfaceTextureHelper = surfaceTextureHelper; | |
| 109 this.eventsHandler = eventsHandler; | |
| 110 this.frameCount = 0; | |
| 111 this.freezePeriodCount = 0; | |
| 112 surfaceTextureHelper.getHandler().postDelayed(cameraObserver, CAMERA_OBSER
VER_PERIOD_MS); | |
| 113 } | |
| 114 | |
| 115 private void checkThread() { | |
| 116 if (Thread.currentThread() != surfaceTextureHelper.getHandler().getLooper(
).getThread()) { | |
| 117 throw new IllegalStateException("Wrong thread"); | |
| 118 } | |
| 119 } | |
| 120 | |
| 121 public void addFrame() { | |
| 122 checkThread(); | |
| 123 ++frameCount; | |
| 124 } | |
| 125 | |
| 126 public void release() { | |
| 127 surfaceTextureHelper.getHandler().removeCallbacks(cameraObserver); | |
| 128 } | |
| 129 } | |
| 130 } | |
| OLD | NEW |