Skip to content
This repository has been archived by the owner on Aug 4, 2022. It is now read-only.

Commit

Permalink
Bug 1578073 - Add newer webrtc.org android camera code; r=ng
Browse files Browse the repository at this point in the history
This is an import of the Android camera code as of upstream revision
26762d0425ffd15af9ddc3ae669373668827ea00 (Dec 20, 2019). This takes just the
files required to build the camera related classes.

Differential Revision: https://phabricator.services.mozilla.com/D61849
  • Loading branch information
dminor committed Feb 11, 2020
1 parent 8a6825f commit a15e6b3
Show file tree
Hide file tree
Showing 38 changed files with 5,946 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2016 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/

package org.webrtc;

import android.content.Context;

public class Camera1Capturer extends CameraCapturer {
private final boolean captureToTexture;

public Camera1Capturer(
String cameraName, CameraEventsHandler eventsHandler, boolean captureToTexture) {
super(cameraName, eventsHandler, new Camera1Enumerator(captureToTexture));

this.captureToTexture = captureToTexture;
}

@Override
protected void createCameraSession(CameraSession.CreateSessionCallback createSessionCallback,
CameraSession.Events events, Context applicationContext,
SurfaceTextureHelper surfaceTextureHelper, String cameraName, int width, int height,
int framerate) {
Camera1Session.create(createSessionCallback, events, captureToTexture, applicationContext,
surfaceTextureHelper, Camera1Enumerator.getCameraIndex(cameraName), width, height,
framerate);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
/*
* Copyright 2015 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/

package org.webrtc;

import android.os.SystemClock;
import android.support.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import org.webrtc.CameraEnumerationAndroid.CaptureFormat;

@SuppressWarnings("deprecation")
public class Camera1Enumerator implements CameraEnumerator {
private final static String TAG = "Camera1Enumerator";
// Each entry contains the supported formats for corresponding camera index. The formats for all
// cameras are enumerated on the first call to getSupportedFormats(), and cached for future
// reference.
private static List<List<CaptureFormat>> cachedSupportedFormats;

private final boolean captureToTexture;

public Camera1Enumerator() {
this(true /* captureToTexture */);
}

public Camera1Enumerator(boolean captureToTexture) {
this.captureToTexture = captureToTexture;
}

// Returns device names that can be used to create a new VideoCapturerAndroid.
@Override
public String[] getDeviceNames() {
ArrayList<String> namesList = new ArrayList<>();
for (int i = 0; i < android.hardware.Camera.getNumberOfCameras(); ++i) {
String name = getDeviceName(i);
if (name != null) {
namesList.add(name);
Logging.d(TAG, "Index: " + i + ". " + name);
} else {
Logging.e(TAG, "Index: " + i + ". Failed to query camera name.");
}
}
String[] namesArray = new String[namesList.size()];
return namesList.toArray(namesArray);
}

@Override
public boolean isFrontFacing(String deviceName) {
android.hardware.Camera.CameraInfo info = getCameraInfo(getCameraIndex(deviceName));
return info != null && info.facing == android.hardware.Camera.CameraInfo.CAMERA_FACING_FRONT;
}

@Override
public boolean isBackFacing(String deviceName) {
android.hardware.Camera.CameraInfo info = getCameraInfo(getCameraIndex(deviceName));
return info != null && info.facing == android.hardware.Camera.CameraInfo.CAMERA_FACING_BACK;
}

@Override
public List<CaptureFormat> getSupportedFormats(String deviceName) {
return getSupportedFormats(getCameraIndex(deviceName));
}

@Override
public CameraVideoCapturer createCapturer(
String deviceName, CameraVideoCapturer.CameraEventsHandler eventsHandler) {
return new Camera1Capturer(deviceName, eventsHandler, captureToTexture);
}

private static @Nullable android.hardware.Camera.CameraInfo getCameraInfo(int index) {
android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
try {
android.hardware.Camera.getCameraInfo(index, info);
} catch (Exception e) {
Logging.e(TAG, "getCameraInfo failed on index " + index, e);
return null;
}
return info;
}

static synchronized List<CaptureFormat> getSupportedFormats(int cameraId) {
if (cachedSupportedFormats == null) {
cachedSupportedFormats = new ArrayList<List<CaptureFormat>>();
for (int i = 0; i < android.hardware.Camera.getNumberOfCameras(); ++i) {
cachedSupportedFormats.add(enumerateFormats(i));
}
}
return cachedSupportedFormats.get(cameraId);
}

private static List<CaptureFormat> enumerateFormats(int cameraId) {
Logging.d(TAG, "Get supported formats for camera index " + cameraId + ".");
final long startTimeMs = SystemClock.elapsedRealtime();
final android.hardware.Camera.Parameters parameters;
android.hardware.Camera camera = null;
try {
Logging.d(TAG, "Opening camera with index " + cameraId);
camera = android.hardware.Camera.open(cameraId);
parameters = camera.getParameters();
} catch (RuntimeException e) {
Logging.e(TAG, "Open camera failed on camera index " + cameraId, e);
return new ArrayList<CaptureFormat>();
} finally {
if (camera != null) {
camera.release();
}
}

final List<CaptureFormat> formatList = new ArrayList<CaptureFormat>();
try {
int minFps = 0;
int maxFps = 0;
final List<int[]> listFpsRange = parameters.getSupportedPreviewFpsRange();
if (listFpsRange != null) {
// getSupportedPreviewFpsRange() returns a sorted list. Take the fps range
// corresponding to the highest fps.
final int[] range = listFpsRange.get(listFpsRange.size() - 1);
minFps = range[android.hardware.Camera.Parameters.PREVIEW_FPS_MIN_INDEX];
maxFps = range[android.hardware.Camera.Parameters.PREVIEW_FPS_MAX_INDEX];
}
for (android.hardware.Camera.Size size : parameters.getSupportedPreviewSizes()) {
formatList.add(new CaptureFormat(size.width, size.height, minFps, maxFps));
}
} catch (Exception e) {
Logging.e(TAG, "getSupportedFormats() failed on camera index " + cameraId, e);
}

final long endTimeMs = SystemClock.elapsedRealtime();
Logging.d(TAG, "Get supported formats for camera index " + cameraId + " done."
+ " Time spent: " + (endTimeMs - startTimeMs) + " ms.");
return formatList;
}

// Convert from android.hardware.Camera.Size to Size.
static List<Size> convertSizes(List<android.hardware.Camera.Size> cameraSizes) {
final List<Size> sizes = new ArrayList<Size>();
for (android.hardware.Camera.Size size : cameraSizes) {
sizes.add(new Size(size.width, size.height));
}
return sizes;
}

// Convert from int[2] to CaptureFormat.FramerateRange.
static List<CaptureFormat.FramerateRange> convertFramerates(List<int[]> arrayRanges) {
final List<CaptureFormat.FramerateRange> ranges = new ArrayList<CaptureFormat.FramerateRange>();
for (int[] range : arrayRanges) {
ranges.add(new CaptureFormat.FramerateRange(
range[android.hardware.Camera.Parameters.PREVIEW_FPS_MIN_INDEX],
range[android.hardware.Camera.Parameters.PREVIEW_FPS_MAX_INDEX]));
}
return ranges;
}

// Returns the camera index for camera with name |deviceName|, or throws IllegalArgumentException
// if no such camera can be found.
static int getCameraIndex(String deviceName) {
Logging.d(TAG, "getCameraIndex: " + deviceName);
for (int i = 0; i < android.hardware.Camera.getNumberOfCameras(); ++i) {
if (deviceName.equals(getDeviceName(i))) {
return i;
}
}
throw new IllegalArgumentException("No such camera: " + deviceName);
}

// Returns the name of the camera with camera index. Returns null if the
// camera can not be used.
static @Nullable String getDeviceName(int index) {
android.hardware.Camera.CameraInfo info = getCameraInfo(index);
if (info == null) {
return null;
}

String facing =
(info.facing == android.hardware.Camera.CameraInfo.CAMERA_FACING_FRONT) ? "front" : "back";
return "Camera " + index + ", Facing " + facing + ", Orientation " + info.orientation;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2016 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/

package org.webrtc;

import android.annotation.TargetApi;
import android.content.Context;
import android.hardware.camera2.CameraManager;
import android.support.annotation.Nullable;

@TargetApi(21)
public class Camera2Capturer extends CameraCapturer {
private final Context context;
@Nullable private final CameraManager cameraManager;

public Camera2Capturer(Context context, String cameraName, CameraEventsHandler eventsHandler) {
super(cameraName, eventsHandler, new Camera2Enumerator(context));

this.context = context;
cameraManager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
}

@Override
protected void createCameraSession(CameraSession.CreateSessionCallback createSessionCallback,
CameraSession.Events events, Context applicationContext,
SurfaceTextureHelper surfaceTextureHelper, String cameraName, int width, int height,
int framerate) {
Camera2Session.create(createSessionCallback, events, applicationContext, cameraManager,
surfaceTextureHelper, cameraName, width, height, framerate);
}
}
Loading

0 comments on commit a15e6b3

Please sign in to comment.