Chromium Code Reviews| Index: services/camera/src/org/chromium/services/camera/CameraServiceImpl.java |
| diff --git a/services/camera/src/org/chromium/services/camera/CameraServiceImpl.java b/services/camera/src/org/chromium/services/camera/CameraServiceImpl.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..da48bd040f53b116dd22c6644d3c9ea19c0b9eb9 |
| --- /dev/null |
| +++ b/services/camera/src/org/chromium/services/camera/CameraServiceImpl.java |
| @@ -0,0 +1,206 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.services.camera; |
| + |
| +import android.content.Context; |
| +import android.graphics.ImageFormat; |
| +import android.hardware.camera2.CameraAccessException; |
| +import android.hardware.camera2.CameraCaptureSession; |
| +import android.hardware.camera2.CameraCharacteristics; |
| +import android.hardware.camera2.CameraDevice; |
| +import android.hardware.camera2.CameraManager; |
| +import android.hardware.camera2.CameraMetadata; |
| +import android.hardware.camera2.CaptureRequest; |
| +import android.hardware.camera2.params.StreamConfigurationMap; |
| +import android.media.Image; |
| +import android.media.ImageReader; |
| +import android.os.Handler; |
| +import android.os.HandlerThread; |
| +import android.util.Log; |
| +import android.util.Size; |
| + |
| +import org.chromium.mojo.system.Core; |
| +import org.chromium.mojo.system.DataPipe; |
| +import org.chromium.mojo.system.MojoException; |
| +import org.chromium.mojo.system.MojoResult; |
| +import org.chromium.mojo.system.Pair; |
| +import org.chromium.mojom.mojo.CameraVideoService; |
| +import org.chromium.mojom.mojo.Shell; |
| + |
| +import java.nio.ByteBuffer; |
| +import java.util.Arrays; |
| + |
| +/** |
| + * Implementation of AuthenticationService from services/camera/camera.mojom |
| + */ |
| +public class CameraServiceImpl implements CameraVideoService { |
| + private final Core mCore; |
| + private final Context mContext; |
| + private static final String TAG = "CameraServiceImpl"; |
| + |
| + private CameraDevice mCameraDevice; |
| + private HandlerThread mBackgroundThread; |
| + private Handler mHandler; |
| + |
| + private ImageReader mImageReader; |
| + private DataPipe.ProducerHandle mProducerHandle; |
| + |
| + public CameraServiceImpl(Context context, Core core, Shell shell) { |
|
alhaad1
2015/10/02 06:33:27
The 'Shell' handle seems unused.
gautham
2015/10/02 22:29:12
Done.
|
| + mContext = context; |
| + mCore = core; |
| + startBackgroundThread(); |
| + openCamera(); |
| + } |
| + |
| + private final ImageReader.OnImageAvailableListener mOnImageAvailableListener = |
| + new ImageReader.OnImageAvailableListener() { |
| + @Override |
| + public void onImageAvailable(ImageReader reader) { |
| + Image img = reader.acquireLatestImage(); |
| + if (mProducerHandle != null) { |
|
qsr
2015/10/02 08:48:31
mProducerHandler is accessed from multiple thread.
gautham
2015/10/02 22:29:12
Done.
|
| + try { |
| + ByteBuffer buffer = img.getPlanes()[0].getBuffer(); |
| + mProducerHandle.writeData(buffer, DataPipe.WriteFlags.none()); |
|
alhaad1
2015/10/02 06:33:27
Should we check if the number of bytes written is
gautham
2015/10/02 22:29:12
I did not set the ALL_OR_NONE because I figured th
|
| + mProducerHandle.close(); |
|
qsr
2015/10/02 08:48:31
This close should be a in finally block somewhere,
gautham
2015/10/02 22:29:12
Done.
|
| + mProducerHandle = null; |
| + } catch (MojoException e) { |
| + if (e.getMojoResult() == MojoResult.SHOULD_WAIT) { |
| + mCore.wait(mProducerHandle, |
| + Core.HandleSignals.WRITABLE, Core.DEADLINE_INFINITE); |
|
qsr
2015/10/02 08:48:31
Why this wait? You are not in a loop.
gautham
2015/10/02 22:29:12
Done.
|
| + } else { |
| + Log.e(TAG, "Failed to write to producer :" + e); |
| + } |
| + } |
| + } |
| + img.close(); |
| + } |
| + }; |
| + |
| + private void openCamera() { |
| + CameraManager manager = (CameraManager) mContext.getSystemService(Context.CAMERA_SERVICE); |
| + try { |
| + for (String cameraId : manager.getCameraIdList()) { |
| + CameraCharacteristics characteristics = |
| + manager.getCameraCharacteristics(cameraId); |
| + StreamConfigurationMap map = characteristics |
| + .get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP); |
| + Size size = chooseVideoSize(map.getOutputSizes(ImageFormat.JPEG)); |
| + // We don't use a front facing camera in this sample. |
| + Integer facing = characteristics.get(CameraCharacteristics.LENS_FACING); |
| + if (facing != null && facing == CameraCharacteristics.LENS_FACING_FRONT) { |
| + continue; |
| + } |
| + mImageReader = ImageReader |
| + .newInstance(size.getWidth(), size.getHeight(), ImageFormat.JPEG, 30 * 600); |
|
alhaad1
2015/10/02 06:33:27
Just curios as to the reasoning behind 30 * 600?
A
gautham
2015/10/02 22:29:12
My understanding of the acquireLatestImage vs acqu
|
| + mImageReader.setOnImageAvailableListener(mOnImageAvailableListener, mHandler); |
| + manager.openCamera(cameraId, mCameraStateCallback, mHandler); |
| + break; |
| + } |
| + } catch (CameraAccessException e) { |
| + e.printStackTrace(); |
| + } |
| + } |
| + |
| + private void closeCamera() { |
| + if (mCameraDevice != null) { |
| + mCameraDevice.close(); |
| + mCameraDevice = null; |
| + } |
| + } |
| + |
| + private void startBackgroundThread() { |
| + mBackgroundThread = new HandlerThread("CameraServiceImplThread"); |
| + mBackgroundThread.start(); |
| + mHandler = new Handler(mBackgroundThread.getLooper()); |
| + } |
| + |
| + private void stopBackgroundThread() { |
| + mBackgroundThread.quitSafely(); |
| + try { |
| + mBackgroundThread.join(); |
| + mBackgroundThread = null; |
| + mHandler = null; |
| + } catch (InterruptedException e) { |
| + e.printStackTrace(); |
|
qsr
2015/10/02 08:48:30
Prefer log to printStackTrace here and everywhere.
gautham
2015/10/02 22:29:12
Done.
|
| + } |
| + } |
| + |
| + private static Size chooseVideoSize(Size[] choices) { |
| + for (Size size : choices) { |
| + if (size.getWidth() == size.getHeight() * 4 / 3 && size.getWidth() <= 1080) { |
|
qsr
2015/10/02 08:48:31
Why this particular conditions?
gautham
2015/10/02 22:29:12
I pulled this from some other code. My understandi
|
| + return size; |
| + } |
| + } |
| + Log.e(TAG, "Couldn't find any suitable video size"); |
| + return choices[choices.length - 1]; |
| + } |
| + |
| + private final CameraDevice.StateCallback mCameraStateCallback = |
| + new CameraDevice.StateCallback() { |
| + @Override |
| + public void onOpened(CameraDevice device) { |
| + mCameraDevice = device; |
| + startPreview(); |
| + } |
| + |
| + @Override |
| + public void onDisconnected(CameraDevice cameraDevice) { |
| + } |
| + |
| + @Override |
| + public void onError(CameraDevice cameraDevice, int error) { |
| + Log.e(TAG, "Failed to connect to camera:" + error); |
| + } |
| + }; |
| + |
| + private void startPreview() { |
| + try { |
| + final CaptureRequest.Builder request = |
| + mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW); |
| + request.addTarget(mImageReader.getSurface()); |
| + mCameraDevice.createCaptureSession( |
| + Arrays.asList(mImageReader.getSurface()), |
| + new CameraCaptureSession.StateCallback() { |
| + @Override |
| + public void onConfigured(CameraCaptureSession session) { |
| + request.set(CaptureRequest.CONTROL_MODE, |
| + CameraMetadata.CONTROL_MODE_AUTO); |
| + try { |
| + session.setRepeatingRequest(request.build(), null, mHandler); |
| + } catch (CameraAccessException e) { |
| + Log.e(TAG, "Failed to set repeating request :" + e); |
|
qsr
2015/10/02 08:48:31
You can also pass the exception itself to the log
gautham
2015/10/02 22:29:12
Done.
|
| + } |
| + } |
| + |
| + @Override |
| + public void onConfigureFailed(CameraCaptureSession session) { |
| + Log.e(TAG, "Could not configure session capture"); |
| + } |
| + }, mHandler); |
| + } catch (CameraAccessException e) { |
| + Log.e(TAG, "Failed to start preview for camera :" + e); |
| + } |
| + } |
| + |
| + @Override |
| + public void getVideoFrames(CameraVideoService.GetVideoFramesResponse callback) { |
| + Pair<DataPipe.ProducerHandle, DataPipe.ConsumerHandle> handles = mCore.createDataPipe(null); |
| + callback.call(handles.second); |
| + mProducerHandle = handles.first; |
| + } |
| + |
| + @Override |
| + public void close() { |
| + stopBackgroundThread(); |
| + closeCamera(); |
| + if (mProducerHandle != null) { |
| + mProducerHandle.close(); |
| + } |
| + } |
| + |
| + @Override |
| + public void onConnectionError(MojoException e) { |
| + } |
| +} |