refactor java code

Review URL: http://webrtc-codereview.appspot.com/29011

git-svn-id: http://webrtc.googlecode.com/svn/trunk@55 4adac7df-926f-26a2-2b94-8c16560cd09d
This commit is contained in:
leozwang@google.com 2011-06-07 17:58:15 +00:00
parent 7a60252e4f
commit 7f43de8dc9
3 changed files with 711 additions and 719 deletions

View File

@ -1,3 +1,13 @@
/*
* Copyright (c) 2011 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.videoengine; package org.webrtc.videoengine;
public class CaptureCapabilityAndroid { public class CaptureCapabilityAndroid {

View File

@ -1,7 +1,15 @@
/** /*
* Copyright (c) 2011 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.videoengine; package org.webrtc.videoengine;
import java.io.IOException; import java.io.IOException;
import java.util.Locale; import java.util.Locale;
import java.util.concurrent.locks.ReentrantLock; import java.util.concurrent.locks.ReentrantLock;
@ -17,111 +25,107 @@ import android.util.Log;
import android.view.SurfaceHolder; import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback; import android.view.SurfaceHolder.Callback;
public class VideoCaptureAndroid implements PreviewCallback, Callback {
public class VideoCaptureAndroid implements private Camera camera;
PreviewCallback, Callback{ private AndroidVideoCaptureDevice currentDevice = null;
public ReentrantLock previewBufferLock = new ReentrantLock();
private Camera _camera;
private AndroidVideoCaptureDevice _currentDevice=null;
public ReentrantLock _previewBufferLock = new ReentrantLock();
private int PIXEL_FORMAT = ImageFormat.NV21; private int PIXEL_FORMAT = ImageFormat.NV21;
PixelFormat _pixelFormat = new PixelFormat(); PixelFormat pixelFormat = new PixelFormat();
private boolean _isRunning=false; // True when the C++ layer has ordered the camera to be started. // True when the C++ layer has ordered the camera to be started.
private boolean isRunning=false;
private final int _numCaptureBuffers = 3; private final int numCaptureBuffers = 3;
private int _expectedFrameSize = 0; private int expectedFrameSize = 0;
private int _orientation = 0; private int orientation = 0;
private int _id=0; private int id = 0;
private long _context=0; // C++ callback context variable. // C++ callback context variable.
private SurfaceHolder _localPreview=null; private long context = 0;
private boolean _ownsBuffers=false; // True if this class owns the preview video buffers. private SurfaceHolder localPreview = null;
// True if this class owns the preview video buffers.
private boolean ownsBuffers = false;
// Set this to 2 for VERBOSE logging. 1 for DEBUG
//Logging private static int LOGLEVEL = 0;
private static int LOGLEVEL = 0; // Set this to 2 for VERBOSE logging. 1 for DEBUG
private static boolean VERBOSE = LOGLEVEL > 2; private static boolean VERBOSE = LOGLEVEL > 2;
private static boolean DEBUG = LOGLEVEL > 1; private static boolean DEBUG = LOGLEVEL > 1;
CaptureCapabilityAndroid currentCapability = null;
CaptureCapabilityAndroid _currentCapability=null; public static
void DeleteVideoCaptureAndroid(VideoCaptureAndroid captureAndroid) {
public static void DeleteVideoCaptureAndroid(VideoCaptureAndroid captureAndroid)
{
if(DEBUG) Log.d("*WEBRTC*", "DeleteVideoCaptureAndroid"); if(DEBUG) Log.d("*WEBRTC*", "DeleteVideoCaptureAndroid");
captureAndroid.StopCapture(); captureAndroid.StopCapture();
captureAndroid._camera.release(); captureAndroid.camera.release();
captureAndroid._camera=null; captureAndroid.camera = null;
captureAndroid._context=0; captureAndroid.context = 0;
if(DEBUG) Log.v("*WEBRTC*", "DeleteVideoCaptureAndroid ended"); if(DEBUG) Log.v("*WEBRTC*", "DeleteVideoCaptureAndroid ended");
} }
public VideoCaptureAndroid(int id, long context,Camera camera,AndroidVideoCaptureDevice device) public VideoCaptureAndroid(int in_id,
{ long in_context,
_id=id; Camera in_camera,
_context=context; AndroidVideoCaptureDevice in_device) {
_camera=camera; id = in_id;
_currentDevice=device; context = in_context;
camera = in_camera;
currentDevice = in_device;
} }
public int StartCapture(int width, int height, int frameRate) public int StartCapture(int width, int height, int frameRate) {
{ if(DEBUG) Log.d("*WEBRTC*", "StartCapture width" + width +
if(DEBUG) Log.d("*WEBRTC*", "StartCapture width" + width + " height " + height +" frame rate " + frameRate); " height " + height +" frame rate " + frameRate);
try try {
{ if (camera == null) {
if (_camera == null) Log.e("*WEBRTC*",
{ String.format(Locale.US,"Camera not initialized %d",id));
Log.e("*WEBRTC*",String.format(Locale.US,"Camera not initialized %d",_id));
return -1; return -1;
} }
_currentCapability=new CaptureCapabilityAndroid(); currentCapability = new CaptureCapabilityAndroid();
_currentCapability.width=width; currentCapability.width = width;
_currentCapability.height=height; currentCapability.height = height;
_currentCapability.maxFPS=frameRate; currentCapability.maxFPS = frameRate;
PixelFormat.getPixelFormatInfo(PIXEL_FORMAT, _pixelFormat); PixelFormat.getPixelFormatInfo(PIXEL_FORMAT, pixelFormat);
Camera.Parameters parameters = _camera.getParameters(); Camera.Parameters parameters = camera.getParameters();
parameters.setPreviewSize(_currentCapability.width, _currentCapability.height); parameters.setPreviewSize(currentCapability.width,
currentCapability.height);
parameters.setPreviewFormat(PIXEL_FORMAT ); parameters.setPreviewFormat(PIXEL_FORMAT );
parameters.setPreviewFrameRate(_currentCapability.maxFPS); parameters.setPreviewFrameRate(currentCapability.maxFPS);
_camera.setParameters(parameters); camera.setParameters(parameters);
_localPreview=ViERenderer.GetLocalRenderer(); // Get the local preview SurfaceHolder from the static render class // Get the local preview SurfaceHolder from the static render class
if(_localPreview!=null) localPreview = ViERenderer.GetLocalRenderer();
{ if(localPreview != null) {
_localPreview.addCallback(this); localPreview.addCallback(this);
} }
int bufSize = width * height * pixelFormat.bitsPerPixel / 8;
if(android.os.Build.VERSION.SDK_INT >= 7) {
int bufSize = width * height * _pixelFormat.bitsPerPixel / 8; // According to Doc addCallbackBuffer belongs to API level 8.
if(android.os.Build.VERSION.SDK_INT>=7) // But it seems like it works on Android 2.1 as well.
{
//According to Doc addCallbackBuffer belongs to API level 8. But it seems like it works on Android 2.1 as well.
// At least SE X10 and Milestone // At least SE X10 and Milestone
byte[] buffer = null; byte[] buffer = null;
for (int i = 0; i < _numCaptureBuffers; i++) for (int i = 0; i < numCaptureBuffers; i++) {
{
buffer = new byte[bufSize]; buffer = new byte[bufSize];
_camera.addCallbackBuffer(buffer); camera.addCallbackBuffer(buffer);
} }
_camera.setPreviewCallbackWithBuffer(this); camera.setPreviewCallbackWithBuffer(this);
_ownsBuffers=true; ownsBuffers = true;
} }
else else {
{ camera.setPreviewCallback(this);
_camera.setPreviewCallback(this);
} }
_camera.startPreview(); camera.startPreview();
_previewBufferLock.lock(); previewBufferLock.lock();
_expectedFrameSize = bufSize; expectedFrameSize = bufSize;
_isRunning=true; isRunning = true;
_previewBufferLock.unlock(); previewBufferLock.unlock();
} }
catch (Exception ex) { catch (Exception ex) {
Log.e("*WEBRTC*", "Failed to start camera"); Log.e("*WEBRTC*", "Failed to start camera");
@ -130,137 +134,129 @@ public class VideoCaptureAndroid implements
return 0; return 0;
} }
public int StopCapture() public int StopCapture() {
{
if(DEBUG) Log.d("*WEBRTC*", "StopCapture"); if(DEBUG) Log.d("*WEBRTC*", "StopCapture");
try try {
{ previewBufferLock.lock();
_previewBufferLock.lock(); isRunning = false;
_isRunning=false; previewBufferLock.unlock();
_previewBufferLock.unlock();
_camera.stopPreview(); camera.stopPreview();
if(android.os.Build.VERSION.SDK_INT>7) if(android.os.Build.VERSION.SDK_INT > 7) {
{ camera.setPreviewCallbackWithBuffer(null);
_camera.setPreviewCallbackWithBuffer(null);
} }
else else {
{ camera.setPreviewCallback(null);
_camera.setPreviewCallback(null);
} }
} catch (Exception ex) { }
catch (Exception ex) {
Log.e("*WEBRTC*", "Failed to stop camera"); Log.e("*WEBRTC*", "Failed to stop camera");
return -1; return -1;
} }
if(DEBUG) Log.d("*WEBRTC*", "StopCapture ended"); if(DEBUG) {
Log.d("*WEBRTC*", "StopCapture ended");
}
return 0; return 0;
} }
native void ProvideCameraFrame(byte[] data,int length, long captureObject); native void ProvideCameraFrame(byte[] data,int length, long captureObject);
public void onPreviewFrame(byte[] data, Camera camera) { public void onPreviewFrame(byte[] data, Camera camera) {
_previewBufferLock.lock(); previewBufferLock.lock();
if(VERBOSE) Log.v("*WEBRTC*",String.format(Locale.US,"preview frame length %d context %x",data.length,_context)); if(VERBOSE) {
if(_isRunning) Log.v("*WEBRTC*",
{ String.format(Locale.US, "preview frame length %d context %x",
data.length, context));
}
if(isRunning) {
// If StartCapture has been called but not StopCapture // If StartCapture has been called but not StopCapture
// Call the C++ layer with the captured frame // Call the C++ layer with the captured frame
if (data.length == _expectedFrameSize) if (data.length == expectedFrameSize) {
{ ProvideCameraFrame(data, expectedFrameSize, context);
ProvideCameraFrame(data, _expectedFrameSize, _context); if (VERBOSE) {
if (VERBOSE) Log.v("*WEBRTC*", String.format(Locale.US, "frame delivered")); Log.v("*WEBRTC*", String.format(Locale.US, "frame delivered"));
if(_ownsBuffers) }
{ if(ownsBuffers) {
// Give the video buffer to the camera service again. // Give the video buffer to the camera service again.
_camera.addCallbackBuffer(data); camera.addCallbackBuffer(data);
} }
} }
} }
_previewBufferLock.unlock(); previewBufferLock.unlock();
} }
public void surfaceChanged(SurfaceHolder holder, int format, int width, public void surfaceChanged(SurfaceHolder holder,
int height) { int format, int width, int height) {
try { try {
if(_camera!=null) if(camera != null) {
{ camera.setPreviewDisplay(localPreview);
_camera.setPreviewDisplay(_localPreview);
} }
} catch (IOException e) { } catch (IOException e) {
Log.e("*WEBRTC*", String.format(Locale.US, "Failed to set Local preview. "+ e.getMessage())); Log.e("*WEBRTC*",
String.format(Locale.US,
"Failed to set Local preview. " + e.getMessage()));
} }
} }
/*
* Sets the rotation of the preview render window. // Sets the rotation of the preview render window.
* Does not affect the captured video image. // Does not affect the captured video image.
*/ public void SetPreviewRotation(int rotation) {
public void SetPreviewRotation(int rotation) if(camera != null) {
{ previewBufferLock.lock();
if(_camera!=null) final boolean running = isRunning;
{
_previewBufferLock.lock();
final boolean running=_isRunning;
int width = 0; int width = 0;
int height = 0; int height = 0;
int framerate = 0; int framerate = 0;
if(running) if(running) {
{ width = currentCapability.width;
width=_currentCapability.width; height = currentCapability.height;
height=_currentCapability.height; framerate = currentCapability.maxFPS;
framerate=_currentCapability.maxFPS;
StopCapture(); StopCapture();
} }
int resultRotation = 0; int resultRotation = 0;
if(_currentDevice._frontCameraType==VideoCaptureDeviceInfoAndroid.FrontFacingCameraType.Android23) if(currentDevice.frontCameraType ==
{ VideoCaptureDeviceInfoAndroid.FrontFacingCameraType.Android23) {
// this is a 2.3 or later front facing camera. SetDisplayOrientation will flip the image horizontally before doing the rotation. // this is a 2.3 or later front facing camera.
// SetDisplayOrientation will flip the image horizontally
// before doing the rotation.
resultRotation=(360-rotation) % 360; // compensate the mirror resultRotation=(360-rotation) % 360; // compensate the mirror
} }
else else {
{ // Back facing or 2.2 or previous front camera // Back facing or 2.2 or previous front camera
resultRotation=rotation; resultRotation=rotation;
} }
if(android.os.Build.VERSION.SDK_INT>7) if(android.os.Build.VERSION.SDK_INT>7) {
{ camera.setDisplayOrientation(resultRotation);
_camera.setDisplayOrientation(resultRotation);
} }
else // Android 2.1 and previous else {
{ // Android 2.1 and previous
// This rotation unfortunately does not seems to work. // This rotation unfortunately does not seems to work.
// http://code.google.com/p/android/issues/detail?id=1193 // http://code.google.com/p/android/issues/detail?id=1193
Camera.Parameters parameters = _camera.getParameters(); Camera.Parameters parameters = camera.getParameters();
parameters.setRotation(resultRotation); parameters.setRotation(resultRotation);
_camera.setParameters(parameters); camera.setParameters(parameters);
} }
if(running) if(running) {
{
StartCapture(width, height, framerate); StartCapture(width, height, framerate);
} }
_previewBufferLock.unlock(); previewBufferLock.unlock();
} }
} }
public void surfaceCreated(SurfaceHolder holder) { public void surfaceCreated(SurfaceHolder holder) {
} }
public void surfaceDestroyed(SurfaceHolder holder) { // public void surfaceDestroyed(SurfaceHolder holder) {
} }
} }

View File

@ -1,3 +1,13 @@
/*
* Copyright (c) 2011 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.videoengine; package org.webrtc.videoengine;
import java.io.File; import java.io.File;
@ -17,89 +27,90 @@ import android.util.Log;
public class VideoCaptureDeviceInfoAndroid { public class VideoCaptureDeviceInfoAndroid {
//Context //Context
Context _context; Context context;
//Logging // Set this to 2 for VERBOSE logging. 1 for DEBUG
private static int LOGLEVEL = 0; // Set this to 2 for VERBOSE logging. 1 for DEBUG private static int LOGLEVEL = 0;
private static boolean VERBOSE = LOGLEVEL > 2; private static boolean VERBOSE = LOGLEVEL > 2;
private static boolean DEBUG = LOGLEVEL > 1; private static boolean DEBUG = LOGLEVEL > 1;
/* Private class with info about all available cameras and the capabilities*/ // Private class with info about all available cameras and the capabilities
public class AndroidVideoCaptureDevice public class AndroidVideoCaptureDevice {
{ AndroidVideoCaptureDevice() {
AndroidVideoCaptureDevice() frontCameraType = FrontFacingCameraType.None;
{ index = 0;
_frontCameraType=FrontFacingCameraType.None;
_index=0;
} }
public String _deviceUniqueName; public String deviceUniqueName;
public CaptureCapabilityAndroid _captureCapabilies[]; public CaptureCapabilityAndroid captureCapabilies[];
public FrontFacingCameraType _frontCameraType; public FrontFacingCameraType frontCameraType;
public int _orientation; //Orientation of camera as described in android.hardware.Camera.CameraInfo.Orientation // Orientation of camera as described in
public int _index; // Camera index used in Camera.Open on Android 2.3 and onwards // android.hardware.Camera.CameraInfo.Orientation
public int orientation;
// Camera index used in Camera.Open on Android 2.3 and onwards
public int index;
} }
public enum FrontFacingCameraType
{ public enum FrontFacingCameraType {
None, // This is not a front facing camera None, // This is not a front facing camera
GalaxyS, // Galaxy S front facing camera. GalaxyS, // Galaxy S front facing camera.
HTCEvo, // HTC Evo front facing camera HTCEvo, // HTC Evo front facing camera
Android23 // Android 2.3 front facing camera. Android23, // Android 2.3 front facing camera.
} }
String currentDeviceUniqueId;
int id;
List<AndroidVideoCaptureDevice> deviceList;
String _currentDeviceUniqueId; public static VideoCaptureDeviceInfoAndroid
int _id; CreateVideoCaptureDeviceInfoAndroid(int in_id, Context in_context) {
List<AndroidVideoCaptureDevice> _deviceList; if(DEBUG) {
Log.d("*WEBRTC*",
String.format(Locale.US, "VideoCaptureDeviceInfoAndroid"));
}
VideoCaptureDeviceInfoAndroid self =
public static VideoCaptureDeviceInfoAndroid CreateVideoCaptureDeviceInfoAndroid(int id, Context context) new VideoCaptureDeviceInfoAndroid(in_id, in_context);
{ if(self != null && self.Init() == 0) {
if(DEBUG) Log.d("*WEBRTC*",String.format(Locale.US,"VideoCaptureDeviceInfoAndroid"));
VideoCaptureDeviceInfoAndroid self = new VideoCaptureDeviceInfoAndroid(id,context);
if(self!=null && self.Init()==0)
{
return self; return self;
} }
else else {
{ if(DEBUG) {
if(DEBUG) Log.d("*WEBRTC*", "Failed to create VideoCaptureDeviceInfoAndroid."); Log.d("*WEBRTC*", "Failed to create VideoCaptureDeviceInfoAndroid.");
}
} }
return null; return null;
} }
private VideoCaptureDeviceInfoAndroid(int id, Context context) private VideoCaptureDeviceInfoAndroid(int in_id,
{ Context in_context) {
_id=id; id = in_id;
_context=context; context = in_context;
_deviceList= new ArrayList<AndroidVideoCaptureDevice>(); deviceList = new ArrayList<AndroidVideoCaptureDevice>();
} }
private int Init() private int Init() {
{ // Populate the deviceList with available cameras and their capabilities.
// Populate the _deviceList with available cameras and their capabilities.
Camera camera = null; Camera camera = null;
try{ try{
if(android.os.Build.VERSION.SDK_INT>8) // From Android 2.3 and onwards if(android.os.Build.VERSION.SDK_INT > 8) {
{ // From Android 2.3 and onwards
for(int i=0; i<Camera.getNumberOfCameras();++i) for(int i = 0; i < Camera.getNumberOfCameras(); ++i) {
{
AndroidVideoCaptureDevice newDevice = new AndroidVideoCaptureDevice(); AndroidVideoCaptureDevice newDevice = new AndroidVideoCaptureDevice();
Camera.CameraInfo info = new Camera.CameraInfo(); Camera.CameraInfo info = new Camera.CameraInfo();
Camera.getCameraInfo(i, info); Camera.getCameraInfo(i, info);
newDevice._index=i; newDevice.index = i;
newDevice._orientation=info.orientation; newDevice.orientation=info.orientation;
if(info.facing == Camera.CameraInfo.CAMERA_FACING_BACK) if(info.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
{ newDevice.deviceUniqueName =
newDevice._deviceUniqueName="Camera " + i +", Facing back, Orientation "+ info.orientation; "Camera " + i +", Facing back, Orientation "+ info.orientation;
} }
else else {
{ newDevice.deviceUniqueName =
newDevice._deviceUniqueName="Camera " + i +", Facing front, Orientation "+ info.orientation; "Camera " + i +", Facing front, Orientation "+ info.orientation;
newDevice._frontCameraType=FrontFacingCameraType.Android23; newDevice.frontCameraType = FrontFacingCameraType.Android23;
} }
camera = Camera.open(i); camera = Camera.open(i);
@ -107,84 +118,81 @@ public class VideoCaptureDeviceInfoAndroid {
AddDeviceInfo(newDevice, parameters); AddDeviceInfo(newDevice, parameters);
camera.release(); camera.release();
camera = null; camera = null;
_deviceList.add(newDevice); deviceList.add(newDevice);
} }
} }
else // Prior to Android 2.3 else {
{ // Prior to Android 2.3
AndroidVideoCaptureDevice newDevice; AndroidVideoCaptureDevice newDevice;
Camera.Parameters parameters; Camera.Parameters parameters;
newDevice = new AndroidVideoCaptureDevice(); newDevice = new AndroidVideoCaptureDevice();
camera = Camera.open(); camera = Camera.open();
parameters = camera.getParameters(); parameters = camera.getParameters();
newDevice._deviceUniqueName="Camera 1, Facing back"; newDevice.deviceUniqueName = "Camera 1, Facing back";
newDevice._orientation=90; newDevice.orientation = 90;
AddDeviceInfo(newDevice, parameters); AddDeviceInfo(newDevice, parameters);
_deviceList.add(newDevice); deviceList.add(newDevice);
camera.release(); camera.release();
camera=null; camera=null;
newDevice = new AndroidVideoCaptureDevice(); newDevice = new AndroidVideoCaptureDevice();
newDevice._deviceUniqueName="Camera 2, Facing front"; newDevice.deviceUniqueName = "Camera 2, Facing front";
parameters = SearchOldFrontFacingCameras(newDevice); parameters = SearchOldFrontFacingCameras(newDevice);
if(parameters!=null) if(parameters != null) {
{
AddDeviceInfo(newDevice, parameters); AddDeviceInfo(newDevice, parameters);
_deviceList.add(newDevice); deviceList.add(newDevice);
} }
} }
}catch (Exception ex) { }
Log.e("*WEBRTC*", "VideoCaptureDeviceInfoAndroid:Init Failed to init VideoCaptureDeviceInfo ex " +ex.getLocalizedMessage()); catch (Exception ex) {
Log.e("*WEBRTC*", "Failed to init VideoCaptureDeviceInfo ex" +
ex.getLocalizedMessage());
return -1; return -1;
} }
VerifyCapabilities(); VerifyCapabilities();
return 0; return 0;
} }
/*
* Adds the capture capabilities of the currently opened device // Adds the capture capabilities of the currently opened device
*/ private void AddDeviceInfo(AndroidVideoCaptureDevice newDevice,
private void AddDeviceInfo(AndroidVideoCaptureDevice newDevice,Camera.Parameters parameters) Camera.Parameters parameters) {
{
List<Size> sizes = parameters.getSupportedPreviewSizes(); List<Size> sizes = parameters.getSupportedPreviewSizes();
List<Integer> frameRates = parameters.getSupportedPreviewFrameRates(); List<Integer> frameRates = parameters.getSupportedPreviewFrameRates();
int maxFPS=0; int maxFPS=0;
for(Integer frameRate:frameRates) for(Integer frameRate:frameRates) {
{ if(VERBOSE) {
if(VERBOSE) Log.v("*WEBRTC*", "VideoCaptureDeviceInfoAndroid:Init supports frameRate "+ frameRate); Log.v("*WEBRTC*",
if(frameRate>maxFPS) "VideoCaptureDeviceInfoAndroid:frameRate " + frameRate);
{ }
if(frameRate > maxFPS) {
maxFPS = frameRate; maxFPS = frameRate;
} }
} }
newDevice._captureCapabilies= new CaptureCapabilityAndroid[sizes.size()]; newDevice.captureCapabilies = new CaptureCapabilityAndroid[sizes.size()];
for(int i=0;i<sizes.size();++i) for(int i = 0; i < sizes.size(); ++i) {
{
Size s = sizes.get(i); Size s = sizes.get(i);
newDevice._captureCapabilies[i]=new CaptureCapabilityAndroid(); newDevice.captureCapabilies[i] = new CaptureCapabilityAndroid();
newDevice._captureCapabilies[i].height=s.height; newDevice.captureCapabilies[i].height = s.height;
newDevice._captureCapabilies[i].width=s.width; newDevice.captureCapabilies[i].width = s.width;
newDevice._captureCapabilies[i].maxFPS=maxFPS; newDevice.captureCapabilies[i].maxFPS = maxFPS;
}
} }
} // Function that make sure device specific capabilities are
// in the capability list.
/* // Ie Galaxy S supports CIF but does not list CIF as a supported capability.
* Function that make sure device specific capabilities are in the capability list. // Motorola Droid Camera does not work with frame rate above 15fps.
* Ie Galaxy S supports CIF but does not list CIF as a supported capability. // http://code.google.com/p/android/issues/detail?id=5514#c0
* Motorola Droid Camera does not work with frame rate above 15fps. private void VerifyCapabilities() {
* http://code.google.com/p/android/issues/detail?id=5514#c0
*/
private void VerifyCapabilities()
{
// Nexus S or Galaxy S // Nexus S or Galaxy S
if(android.os.Build.DEVICE.equals("GT-I9000") || android.os.Build.DEVICE.equals("crespo")) if(android.os.Build.DEVICE.equals("GT-I9000") ||
{ android.os.Build.DEVICE.equals("crespo")) {
CaptureCapabilityAndroid specificCapability=new CaptureCapabilityAndroid(); CaptureCapabilityAndroid specificCapability =
new CaptureCapabilityAndroid();
specificCapability.width = 352; specificCapability.width = 352;
specificCapability.height = 288; specificCapability.height = 288;
specificCapability.maxFPS = 15; specificCapability.maxFPS = 15;
@ -201,110 +209,88 @@ public class VideoCaptureDeviceInfoAndroid {
specificCapability.height = 240; specificCapability.height = 240;
specificCapability.maxFPS = 15; specificCapability.maxFPS = 15;
AddDeviceSpecificCapability(specificCapability); AddDeviceSpecificCapability(specificCapability);
} }
// Motorola Milestone Camera server does not work at 30fps even though it reports that it can // Motorola Milestone Camera server does not work at 30fps
if(android.os.Build.MANUFACTURER.equals("motorola") && android.os.Build.DEVICE.equals("umts_sholes")) // even though it reports that it can
{ if(android.os.Build.MANUFACTURER.equals("motorola") &&
for(AndroidVideoCaptureDevice device:_deviceList) android.os.Build.DEVICE.equals("umts_sholes")) {
{ for(AndroidVideoCaptureDevice device:deviceList) {
for(CaptureCapabilityAndroid capability:device._captureCapabilies) for(CaptureCapabilityAndroid capability:device.captureCapabilies) {
{
capability.maxFPS=15; capability.maxFPS=15;
} }
} }
} }
} }
private void AddDeviceSpecificCapability(CaptureCapabilityAndroid specificCapability)
{ private void AddDeviceSpecificCapability(
for(AndroidVideoCaptureDevice device:_deviceList) CaptureCapabilityAndroid specificCapability) {
{ for(AndroidVideoCaptureDevice device:deviceList) {
boolean foundCapability = false; boolean foundCapability = false;
for(CaptureCapabilityAndroid capability:device._captureCapabilies) for(CaptureCapabilityAndroid capability:device.captureCapabilies) {
{ if(capability.width == specificCapability.width &&
if(capability.width==specificCapability.width && capability.height==specificCapability.height) capability.height == specificCapability.height) {
{
foundCapability = true; foundCapability = true;
break; break;
} }
} }
if(foundCapability==false) if(foundCapability==false) {
{ CaptureCapabilityAndroid newCaptureCapabilies[]=
CaptureCapabilityAndroid newCaptureCapabilies[]= new CaptureCapabilityAndroid[device._captureCapabilies.length+1]; new CaptureCapabilityAndroid[device.captureCapabilies.length+1];
for(int i=0;i<device._captureCapabilies.length;++i) for(int i = 0; i < device.captureCapabilies.length; ++i) {
{ newCaptureCapabilies[i+1] = device.captureCapabilies[i];
newCaptureCapabilies[i+1]=device._captureCapabilies[i];
} }
newCaptureCapabilies[0] = specificCapability; newCaptureCapabilies[0] = specificCapability;
device._captureCapabilies=newCaptureCapabilies; device.captureCapabilies = newCaptureCapabilies;
} }
} }
} }
/* // Returns the number of Capture devices that is supported
* Returns the number of Capture devices that is supported public int NumberOfDevices() {
*/ return deviceList.size();
public int NumberOfDevices()
{
return _deviceList.size();
} }
public String GetDeviceUniqueName(int deviceNumber) public String GetDeviceUniqueName(int deviceNumber) {
{ if(deviceNumber < 0 || deviceNumber >= deviceList.size()) {
if(deviceNumber<0 || deviceNumber>=_deviceList.size())
{
return null; return null;
} }
return _deviceList.get(deviceNumber)._deviceUniqueName; return deviceList.get(deviceNumber).deviceUniqueName;
} }
public CaptureCapabilityAndroid[] public CaptureCapabilityAndroid[] GetCapabilityArray (String deviceUniqueId)
GetCapabilityArray (String deviceUniqueId)
{ {
for (AndroidVideoCaptureDevice device: _deviceList) for (AndroidVideoCaptureDevice device: deviceList) {
{ if(device.deviceUniqueName.equals(deviceUniqueId)) {
if(device._deviceUniqueName.equals(deviceUniqueId)) return (CaptureCapabilityAndroid[]) device.captureCapabilies;
{
return (CaptureCapabilityAndroid[]) device._captureCapabilies;
} }
} }
return null; return null;
} }
/* Returns the camera orientation as described by // Returns the camera orientation as described by
* android.hardware.Camera.CameraInfo.orientation // android.hardware.Camera.CameraInfo.orientation
*/ public int GetOrientation(String deviceUniqueId) {
public int GetOrientation(String deviceUniqueId) for (AndroidVideoCaptureDevice device: deviceList) {
{ if(device.deviceUniqueName.equals(deviceUniqueId)) {
for (AndroidVideoCaptureDevice device: _deviceList) return device.orientation;
{
if(device._deviceUniqueName.equals(deviceUniqueId))
{
return device._orientation;
} }
} }
return -1; return -1;
} }
/* // Returns an instance of VideoCaptureAndroid.
* Returns an instance of VideoCaptureAndroid. public VideoCaptureAndroid AllocateCamera(int id, long context,
*/ String deviceUniqueId) {
public VideoCaptureAndroid AllocateCamera(int id, long context,String deviceUniqueId) try {
{
try
{
if(DEBUG) Log.d("*WEBRTC*", "AllocateCamera " + deviceUniqueId); if(DEBUG) Log.d("*WEBRTC*", "AllocateCamera " + deviceUniqueId);
Camera camera = null; Camera camera = null;
AndroidVideoCaptureDevice deviceToUse = null; AndroidVideoCaptureDevice deviceToUse = null;
for (AndroidVideoCaptureDevice device: _deviceList) for (AndroidVideoCaptureDevice device: deviceList) {
{ if(device.deviceUniqueName.equals(deviceUniqueId)) {
if(device._deviceUniqueName.equals(deviceUniqueId)) // Found the wanted camera // Found the wanted camera
{
deviceToUse = device; deviceToUse = device;
switch(device._frontCameraType) switch(device.frontCameraType) {
{
case GalaxyS: case GalaxyS:
camera = AllocateGalaxySFrontCamera(); camera = AllocateGalaxySFrontCamera();
break; break;
@ -312,67 +298,73 @@ public class VideoCaptureDeviceInfoAndroid {
camera = AllocateEVOFrontFacingCamera(); camera = AllocateEVOFrontFacingCamera();
break; break;
default: default:
if(android.os.Build.VERSION.SDK_INT>8) // From Android 2.3 and onwards) // From Android 2.3 and onwards)
camera=Camera.open(device._index); if(android.os.Build.VERSION.SDK_INT>8)
camera=Camera.open(device.index);
else else
camera=Camera.open(); // Default camera camera=Camera.open(); // Default camera
} }
} }
} }
if(camera==null) if(camera == null) {
{
return null; return null;
} }
if(VERBOSE) Log.v("*WEBRTC*", "AllocateCamera - creating VideoCaptureAndroid"); if(VERBOSE) {
Log.v("*WEBRTC*", "AllocateCamera - creating VideoCaptureAndroid");
}
return new VideoCaptureAndroid(id,context,camera,deviceToUse); return new VideoCaptureAndroid(id,context,camera,deviceToUse);
}catch (Exception ex) { }catch (Exception ex) {
Log.e("*WEBRTC*", "AllocateCamera Failed to open camera- ex " +ex.getLocalizedMessage()); Log.e("*WEBRTC*", "AllocateCamera Failed to open camera- ex " +
ex.getLocalizedMessage());
} }
return null; return null;
} }
/* // Searches for a front facing camera device. This is device specific code.
* Searches for a front facing camera device. This is device specific code. private Camera.Parameters
*/ SearchOldFrontFacingCameras(AndroidVideoCaptureDevice newDevice)
private Camera.Parameters SearchOldFrontFacingCameras(AndroidVideoCaptureDevice newDevice) throws SecurityException, IllegalArgumentException, NoSuchMethodException, ClassNotFoundException, IllegalAccessException, InvocationTargetException throws SecurityException, IllegalArgumentException,
{ NoSuchMethodException, ClassNotFoundException,
//Check the id of the opened camera device (Returns null on X10 and 1 on Samsung Galaxy S. IllegalAccessException, InvocationTargetException {
// Check the id of the opened camera device
// Returns null on X10 and 1 on Samsung Galaxy S.
Camera camera = Camera.open(); Camera camera = Camera.open();
Camera.Parameters parameters = camera.getParameters(); Camera.Parameters parameters = camera.getParameters();
String cameraId = parameters.get("camera-id"); String cameraId = parameters.get("camera-id");
if(cameraId!=null && cameraId.equals("1")) // This might be a Samsung Galaxy S with a front facing camera. if(cameraId != null && cameraId.equals("1")) {
{ // This might be a Samsung Galaxy S with a front facing camera.
try try {
{
parameters.set("camera-id", 2); parameters.set("camera-id", 2);
camera.setParameters(parameters); camera.setParameters(parameters);
parameters = camera.getParameters(); parameters = camera.getParameters();
newDevice._frontCameraType=FrontFacingCameraType.GalaxyS; newDevice.frontCameraType = FrontFacingCameraType.GalaxyS;
newDevice._orientation=0; newDevice.orientation = 0;
camera.release(); camera.release();
return parameters; return parameters;
} }
catch (Exception ex) { catch (Exception ex) {
//Nope - it did not work. //Nope - it did not work.
Log.e("*WEBRTC*", "VideoCaptureDeviceInfoAndroid:Init Failed to open front camera camera - ex " +ex.getLocalizedMessage()); Log.e("*WEBRTC*", "Init Failed to open front camera camera - ex " +
ex.getLocalizedMessage());
} }
} }
camera.release(); camera.release();
//Check for Evo front facing camera. //Check for Evo front facing camera
File file = new File("/system/framework/com.htc.hardware.twinCamDevice.jar"); File file =
new File("/system/framework/com.htc.hardware.twinCamDevice.jar");
boolean exists = file.exists(); boolean exists = file.exists();
if (!exists){ if (!exists){
file = new File("/system/framework/com.sprint.hardware.twinCamDevice.jar"); file =
new File("/system/framework/com.sprint.hardware.twinCamDevice.jar");
exists = file.exists(); exists = file.exists();
} }
if(exists) if(exists) {
{ newDevice.frontCameraType = FrontFacingCameraType.HTCEvo;
newDevice._frontCameraType=FrontFacingCameraType.HTCEvo; newDevice.orientation = 0;
newDevice._orientation=0;
Camera evCamera = AllocateEVOFrontFacingCamera(); Camera evCamera = AllocateEVOFrontFacingCamera();
parameters = evCamera.getParameters(); parameters = evCamera.getParameters();
evCamera.release(); evCamera.release();
@ -381,31 +373,30 @@ public class VideoCaptureDeviceInfoAndroid {
return null; return null;
} }
// Returns a handle to HTC front facing camera.
/* // The caller is responsible to release it on completion.
* Returns a handle to HTC front facing camera. private Camera AllocateEVOFrontFacingCamera()
* The caller is responsible to release it on completion. throws SecurityException, NoSuchMethodException,
*/ ClassNotFoundException, IllegalArgumentException,
private Camera AllocateEVOFrontFacingCamera() throws SecurityException, NoSuchMethodException, ClassNotFoundException, IllegalArgumentException, IllegalAccessException, InvocationTargetException IllegalAccessException, InvocationTargetException {
{
String classPath = null; String classPath = null;
File file = new File("/system/framework/com.htc.hardware.twinCamDevice.jar"); File file =
new File("/system/framework/com.htc.hardware.twinCamDevice.jar");
classPath = "com.htc.hardware.twinCamDevice.FrontFacingCamera"; classPath = "com.htc.hardware.twinCamDevice.FrontFacingCamera";
boolean exists = file.exists(); boolean exists = file.exists();
if (!exists){ if (!exists){
file = new File("/system/framework/com.sprint.hardware.twinCamDevice.jar"); file =
new File("/system/framework/com.sprint.hardware.twinCamDevice.jar");
classPath = "com.sprint.hardware.twinCamDevice.FrontFacingCamera"; classPath = "com.sprint.hardware.twinCamDevice.FrontFacingCamera";
exists = file.exists(); exists = file.exists();
} }
if(!exists) if(!exists) {
{
return null; return null;
} }
String dexOutputDir = ""; String dexOutputDir = "";
if(_context!=null){ if(context != null) {
dexOutputDir = _context.getFilesDir().getAbsolutePath(); dexOutputDir = context.getFilesDir().getAbsolutePath();
File mFilesDir = new File(dexOutputDir, "dexfiles"); File mFilesDir = new File(dexOutputDir, "dexfiles");
if(!mFilesDir.exists()){ if(!mFilesDir.exists()){
//Log.e("*WEBRTCN*", "Directory doesn't exists"); //Log.e("*WEBRTCN*", "Directory doesn't exists");
@ -417,22 +408,18 @@ public class VideoCaptureDeviceInfoAndroid {
dexOutputDir += "/dexfiles"; dexOutputDir += "/dexfiles";
DexClassLoader loader = new DexClassLoader( DexClassLoader loader =
file.getAbsolutePath(), new DexClassLoader(file.getAbsolutePath(), dexOutputDir,
dexOutputDir, null, ClassLoader.getSystemClassLoader());
null,
ClassLoader.getSystemClassLoader()
);
Method method = loader.loadClass(classPath).getDeclaredMethod("getFrontFacingCamera", (Class[]) null); Method method = loader.loadClass(classPath).getDeclaredMethod(
"getFrontFacingCamera", (Class[]) null);
Camera camera = (Camera) method.invoke((Object[])null,(Object[]) null); Camera camera = (Camera) method.invoke((Object[])null,(Object[]) null);
return camera; return camera;
} }
/* // Returns a handle to Galaxy S front camera.
* Returns a handle to Galaxy S front camera. // The caller is responsible to release it on completion.
* The caller is responsible to release it on completion.
*/
private Camera AllocateGalaxySFrontCamera() private Camera AllocateGalaxySFrontCamera()
{ {
Camera camera = Camera.open(); Camera camera = Camera.open();
@ -442,5 +429,4 @@ public class VideoCaptureDeviceInfoAndroid {
return camera; return camera;
} }
} }